ia64/linux-2.6.18-xen.hg

changeset 736:412b24a36929

pciback: error handler for PCIE_AER.

This patch is the main implementation for enabling PCIE_AER handling,
adding related pci error handler in pciback and pcifront.

When a device sends a PCIE error message to the root port, it will
trigger an interrupt. The irq handler will then collect roor error
status register, then schedule a work to process the error based on
the error type.

If the error is non-correctable error (fatal or non-fatal), AER
service driver will call the callback funtions of the endpoint's
driver. For bridge, it will broadcast the error to the downstream
ports. Pciback error handler will be called accordingly. Pciback then
ask pcifront help to call the end-device driver for finally completing
the related pci error handling jobs.

Signed-off-by: Jiang Yunhong <yunhong.jiang@intel.com>
Signed-off-by: Ke Liping <liping.ke@intel.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Nov 24 11:04:54 2008 +0000 (2008-11-24)
parents 5888ffa4b252
children 163a3807cb1f
files drivers/xen/pciback/controller.c drivers/xen/pciback/passthrough.c drivers/xen/pciback/pci_stub.c drivers/xen/pciback/pciback.h drivers/xen/pciback/pciback_ops.c drivers/xen/pciback/slot.c drivers/xen/pciback/vpci.c drivers/xen/pciback/xenbus.c drivers/xen/pcifront/pci_op.c drivers/xen/pcifront/pcifront.h drivers/xen/pcifront/xenbus.c include/xen/interface/io/pciif.h
line diff
     1.1 --- a/drivers/xen/pciback/controller.c	Mon Nov 24 11:03:21 2008 +0000
     1.2 +++ b/drivers/xen/pciback/controller.c	Mon Nov 24 11:04:54 2008 +0000
     1.3 @@ -406,3 +406,38 @@ void pciback_release_devices(struct pcib
     1.4  	kfree(dev_data);
     1.5  	pdev->pci_dev_data = NULL;
     1.6  }
     1.7 +
     1.8 +int pciback_get_pcifront_dev(struct pci_dev *pcidev, 
     1.9 +		struct pciback_device *pdev, 
    1.10 +		unsigned int *domain, unsigned int *bus, unsigned int *devfn)
    1.11 +{
    1.12 +	struct controller_dev_data *dev_data = pdev->pci_dev_data;
    1.13 +	struct controller_dev_entry *dev_entry;
    1.14 +	struct controller_list_entry *cntrl_entry;
    1.15 +	unsigned long flags;
    1.16 +	int found = 0;
    1.17 +	spin_lock_irqsave(&dev_data->lock, flags);
    1.18 +
    1.19 +	list_for_each_entry(cntrl_entry, &dev_data->list, list) {
    1.20 +		list_for_each_entry(dev_entry, &cntrl_entry->dev_list, list) {
    1.21 +			if ( (dev_entry->dev->bus->number == 
    1.22 +					pcidev->bus->number) &&
    1.23 +			  	(dev_entry->dev->devfn ==
    1.24 +					pcidev->devfn) &&
    1.25 +				(pci_domain_nr(dev_entry->dev->bus) ==
    1.26 +					pci_domain_nr(pcidev->bus)))
    1.27 +			{
    1.28 +				found = 1;
    1.29 +				*domain = cntrl_entry->domain;
    1.30 +				*bus = cntrl_entry->bus;
    1.31 +				*devfn = dev_entry->devfn;
    1.32 +				goto out;
    1.33 +			}
    1.34 +		}
    1.35 +	}
    1.36 +out:
    1.37 +	spin_unlock_irqrestore(&dev_data->lock, flags);
    1.38 +	return found;
    1.39 +
    1.40 +}
    1.41 +
     2.1 --- a/drivers/xen/pciback/passthrough.c	Mon Nov 24 11:03:21 2008 +0000
     2.2 +++ b/drivers/xen/pciback/passthrough.c	Mon Nov 24 11:04:54 2008 +0000
     2.3 @@ -164,3 +164,13 @@ void pciback_release_devices(struct pcib
     2.4  	kfree(dev_data);
     2.5  	pdev->pci_dev_data = NULL;
     2.6  }
     2.7 +
     2.8 +int pciback_get_pcifront_dev(struct pci_dev *pcidev, struct pciback_device *pdev, 
     2.9 +		unsigned int *domain, unsigned int *bus, unsigned int *devfn)
    2.10 +
    2.11 +{
    2.12 +	*domain = pci_domain_nr(pcidev->bus);
    2.13 +	*bus = pcidev->bus->number;
    2.14 +	*devfn = pcidev->devfn;
    2.15 +	return 1;
    2.16 +}
     3.1 --- a/drivers/xen/pciback/pci_stub.c	Mon Nov 24 11:03:21 2008 +0000
     3.2 +++ b/drivers/xen/pciback/pci_stub.c	Mon Nov 24 11:04:54 2008 +0000
     3.3 @@ -6,15 +6,24 @@
     3.4   */
     3.5  #include <linux/module.h>
     3.6  #include <linux/init.h>
     3.7 +#include <linux/rwsem.h>
     3.8  #include <linux/list.h>
     3.9  #include <linux/spinlock.h>
    3.10  #include <linux/kref.h>
    3.11 +#include <linux/pci.h>
    3.12 +#include <linux/wait.h>
    3.13  #include <asm/atomic.h>
    3.14 +#include <xen/evtchn.h>
    3.15  #include "pciback.h"
    3.16  #include "conf_space.h"
    3.17  #include "conf_space_quirks.h"
    3.18  
    3.19  static char *pci_devs_to_hide = NULL;
    3.20 +wait_queue_head_t aer_wait_queue;
    3.21 +/*Add sem for sync AER handling and pciback remove/reconfigue ops,
    3.22 +* We want to avoid in middle of AER ops, pciback devices is being removed
    3.23 +*/
    3.24 +static DECLARE_RWSEM(pcistub_sem);
    3.25  module_param_named(hide, pci_devs_to_hide, charp, 0444);
    3.26  
    3.27  struct pcistub_device_id {
    3.28 @@ -207,6 +216,10 @@ void pcistub_put_pci_dev(struct pci_dev 
    3.29  
    3.30  	spin_unlock_irqrestore(&pcistub_devices_lock, flags);
    3.31  
    3.32 +	/*hold this lock for avoiding breaking link between
    3.33 +	* pcistub and pciback when AER is in processing
    3.34 +	*/
    3.35 +	down_write(&pcistub_sem);
    3.36  	/* Cleanup our device
    3.37  	 * (so it's ready for the next domain)
    3.38  	 */
    3.39 @@ -219,6 +232,7 @@ void pcistub_put_pci_dev(struct pci_dev 
    3.40  	spin_unlock_irqrestore(&found_psdev->lock, flags);
    3.41  
    3.42  	pcistub_device_put(found_psdev);
    3.43 +	up_write(&pcistub_sem);
    3.44  }
    3.45  
    3.46  static int __devinit pcistub_match_one(struct pci_dev *dev,
    3.47 @@ -279,6 +293,8 @@ static int __devinit pcistub_init_device
    3.48  	pci_set_drvdata(dev, dev_data);
    3.49  
    3.50  	dev_dbg(&dev->dev, "initializing config\n");
    3.51 +
    3.52 +	init_waitqueue_head(&aer_wait_queue);
    3.53  	err = pciback_config_init_dev(dev);
    3.54  	if (err)
    3.55  		goto out;
    3.56 @@ -477,6 +493,308 @@ static const struct pci_device_id pcistu
    3.57  	{0,},
    3.58  };
    3.59  
    3.60 +static void kill_domain_by_device(struct pcistub_device *psdev)
    3.61 +{
    3.62 +	struct xenbus_transaction xbt;
    3.63 +	int err;
    3.64 +	char nodename[1024];
    3.65 +
    3.66 +	if (!psdev) 
    3.67 +		dev_err(&psdev->dev->dev,
    3.68 +			"device is NULL when do AER recovery/kill_domain\n");
    3.69 +	sprintf(nodename, "/local/domain/0/backend/pci/%d/0", 
    3.70 +		psdev->pdev->xdev->otherend_id);
    3.71 +	nodename[strlen(nodename)] = '\0';
    3.72 +
    3.73 +again:
    3.74 +	err = xenbus_transaction_start(&xbt);
    3.75 +	if (err)
    3.76 +	{
    3.77 +		dev_err(&psdev->dev->dev,
    3.78 +			"error %d when start xenbus transaction\n", err);
    3.79 +		return;
    3.80 +	}
    3.81 +	/*PV AER handlers will set this flag*/
    3.82 +	xenbus_printf(xbt, nodename, "aerState" , "aerfail" );
    3.83 +	err = xenbus_transaction_end(xbt, 0);
    3.84 +	if (err)
    3.85 +	{
    3.86 +		if (err == -EAGAIN)
    3.87 +			goto again;
    3.88 +		dev_err(&psdev->dev->dev,
    3.89 +			"error %d when end xenbus transaction\n", err);
    3.90 +		return;
    3.91 +	}
    3.92 +}
    3.93 +
    3.94 +/* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
    3.95 + * backend need to have cooperation. In pciback, those steps will do similar
    3.96 + * jobs: send service request and waiting for front_end response. 
    3.97 +*/
    3.98 +static pci_ers_result_t common_process(struct pcistub_device *psdev, 
    3.99 +		pci_channel_state_t state, int aer_cmd, pci_ers_result_t result)
   3.100 +{
   3.101 +	pci_ers_result_t res = result;
   3.102 +	struct xen_pcie_aer_op *aer_op;
   3.103 +	int ret;
   3.104 +
   3.105 +	/*with PV AER drivers*/
   3.106 +	aer_op = &(psdev->pdev->sh_info->aer_op);
   3.107 +	aer_op->cmd = aer_cmd ;
   3.108 +	/*useful for error_detected callback*/
   3.109 +	aer_op->err = state;
   3.110 +	/*pcifront_end BDF*/
   3.111 +	ret = pciback_get_pcifront_dev(psdev->dev, psdev->pdev,
   3.112 +		&aer_op->domain, &aer_op->bus, &aer_op->devfn);
   3.113 +	if (!ret) {
   3.114 +		dev_err(&psdev->dev->dev,
   3.115 +			"pciback: failed to get pcifront device\n");
   3.116 +		return PCI_ERS_RESULT_NONE; 
   3.117 +	}
   3.118 +	wmb();
   3.119 +
   3.120 +	dev_dbg(&psdev->dev->dev, 
   3.121 +			"pciback: aer_op %x dom %x bus %x devfn %x\n",  
   3.122 +			aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
   3.123 +	/*local flag to mark there's aer request, pciback callback will use this
   3.124 +	* flag to judge whether we need to check pci-front give aer service
   3.125 +	* ack signal
   3.126 +	*/
   3.127 +	set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
   3.128 +
   3.129 +	/*It is possible that a pcifront conf_read_write ops request invokes
   3.130 +	* the callback which cause the spurious execution of wake_up. 
   3.131 +	* Yet it is harmless and better than a spinlock here
   3.132 +	*/
   3.133 +	set_bit(_XEN_PCIB_active, 
   3.134 +		(unsigned long *)&psdev->pdev->sh_info->flags);
   3.135 +	wmb();
   3.136 +	notify_remote_via_irq(psdev->pdev->evtchn_irq);
   3.137 +
   3.138 +	ret = wait_event_timeout(aer_wait_queue, !(test_bit(_XEN_PCIB_active,
   3.139 +                (unsigned long *)&psdev->pdev->sh_info->flags)), 300*HZ);
   3.140 +
   3.141 +	if (!ret) {
   3.142 +		if (test_bit(_XEN_PCIB_active, 
   3.143 +			(unsigned long *)&psdev->pdev->sh_info->flags)) {
   3.144 +			dev_err(&psdev->dev->dev, 
   3.145 +				"pcifront aer process not responding!\n");
   3.146 +			clear_bit(_XEN_PCIB_active,
   3.147 +			  (unsigned long *)&psdev->pdev->sh_info->flags);
   3.148 +			aer_op->err = PCI_ERS_RESULT_NONE;
   3.149 +			return res;
   3.150 +		}
   3.151 +	}
   3.152 +	clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
   3.153 +
   3.154 +	if ( test_bit( _XEN_PCIF_active,
   3.155 +		(unsigned long*)&psdev->pdev->sh_info->flags)) {
   3.156 +		dev_dbg(&psdev->dev->dev, 
   3.157 +			"schedule pci_conf service in pciback \n");
   3.158 +		test_and_schedule_op(psdev->pdev);
   3.159 +	}
   3.160 +
   3.161 +	res = (pci_ers_result_t)aer_op->err;
   3.162 +	return res;
   3.163 +} 
   3.164 +
   3.165 +/*
   3.166 +* pciback_slot_reset: it will send the slot_reset request to  pcifront in case
   3.167 +* of the device driver could provide this service, and then wait for pcifront
   3.168 +* ack.
   3.169 +* @dev: pointer to PCI devices
   3.170 +* return value is used by aer_core do_recovery policy
   3.171 +*/
   3.172 +static pci_ers_result_t pciback_slot_reset(struct pci_dev *dev)
   3.173 +{
   3.174 +	struct pcistub_device *psdev;
   3.175 +	pci_ers_result_t result;
   3.176 +
   3.177 +	result = PCI_ERS_RESULT_RECOVERED;
   3.178 +	dev_dbg(&dev->dev, "pciback_slot_reset(bus:%x,devfn:%x)\n",
   3.179 +		dev->bus->number, dev->devfn);
   3.180 +
   3.181 +	down_write(&pcistub_sem);
   3.182 +	psdev = pcistub_device_find(pci_domain_nr(dev->bus),
   3.183 +				dev->bus->number,
   3.184 +				PCI_SLOT(dev->devfn),
   3.185 +				PCI_FUNC(dev->devfn));
   3.186 +	if ( !psdev || !psdev->pdev || !psdev->pdev->sh_info )
   3.187 +	{
   3.188 +		dev_err(&dev->dev, 
   3.189 +			"pciback device is not found/in use/connected!\n");
   3.190 +		goto end;
   3.191 +	}
   3.192 +	if ( !test_bit(_XEN_PCIB_AERHANDLER, 
   3.193 +		(unsigned long *)&psdev->pdev->sh_info->flags) ) {
   3.194 +		dev_err(&dev->dev, 
   3.195 +			"guest with no AER driver should have been killed\n");
   3.196 +		goto release;
   3.197 +	}
   3.198 +	result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
   3.199 +
   3.200 +	if (result == PCI_ERS_RESULT_NONE ||
   3.201 +		result == PCI_ERS_RESULT_DISCONNECT) {
   3.202 +		dev_dbg(&dev->dev, 
   3.203 +			"No AER slot_reset service or disconnected!\n");
   3.204 +		kill_domain_by_device(psdev);
   3.205 +	}
   3.206 +release:
   3.207 +	pcistub_device_put(psdev);
   3.208 +end:
   3.209 +	up_write(&pcistub_sem);
   3.210 +	return result;
   3.211 +
   3.212 +}
   3.213 +
   3.214 +
   3.215 +/*pciback_mmio_enabled: it will send the mmio_enabled request to  pcifront 
   3.216 +* in case of the device driver could provide this service, and then wait 
   3.217 +* for pcifront ack.
   3.218 +* @dev: pointer to PCI devices
   3.219 +* return value is used by aer_core do_recovery policy
   3.220 +*/
   3.221 +
   3.222 +static pci_ers_result_t pciback_mmio_enabled(struct pci_dev *dev)
   3.223 +{
   3.224 +	struct pcistub_device *psdev;
   3.225 +	pci_ers_result_t result;
   3.226 +
   3.227 +	result = PCI_ERS_RESULT_RECOVERED;
   3.228 +	dev_dbg(&dev->dev, "pciback_mmio_enabled(bus:%x,devfn:%x)\n",
   3.229 +		dev->bus->number, dev->devfn);
   3.230 +
   3.231 +	down_write(&pcistub_sem);
   3.232 +	psdev = pcistub_device_find(pci_domain_nr(dev->bus),
   3.233 +				dev->bus->number,
   3.234 +				PCI_SLOT(dev->devfn),
   3.235 +				PCI_FUNC(dev->devfn));
   3.236 +	if ( !psdev || !psdev->pdev || !psdev->pdev->sh_info)
   3.237 +	{
   3.238 +		dev_err(&dev->dev, 
   3.239 +			"pciback device is not found/in use/connected!\n");
   3.240 +		goto end;
   3.241 +	}
   3.242 +	if ( !test_bit(_XEN_PCIB_AERHANDLER, 
   3.243 +		(unsigned long *)&psdev->pdev->sh_info->flags) ) {
   3.244 +		dev_err(&dev->dev, 
   3.245 +			"guest with no AER driver should have been killed\n");
   3.246 +		goto release;
   3.247 +	}
   3.248 +	result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
   3.249 +
   3.250 +	if (result == PCI_ERS_RESULT_NONE ||
   3.251 +		result == PCI_ERS_RESULT_DISCONNECT) {
   3.252 +		dev_dbg(&dev->dev, 
   3.253 +			"No AER mmio_enabled service or disconnected!\n");
   3.254 +		kill_domain_by_device(psdev);
   3.255 +	}
   3.256 +release:
   3.257 +	pcistub_device_put(psdev);
   3.258 +end:
   3.259 +	up_write(&pcistub_sem);
   3.260 +	return result;
   3.261 +}
   3.262 +
   3.263 +/*pciback_error_detected: it will send the error_detected request to  pcifront 
   3.264 +* in case of the device driver could provide this service, and then wait 
   3.265 +* for pcifront ack.
   3.266 +* @dev: pointer to PCI devices
   3.267 +* @error: the current PCI connection state
   3.268 +* return value is used by aer_core do_recovery policy
   3.269 +*/
   3.270 +
   3.271 +static pci_ers_result_t pciback_error_detected(struct pci_dev *dev,
   3.272 +	pci_channel_state_t error)
   3.273 +{
   3.274 +	struct pcistub_device *psdev;
   3.275 +	pci_ers_result_t result;
   3.276 +
   3.277 +	result = PCI_ERS_RESULT_CAN_RECOVER;
   3.278 +	dev_dbg(&dev->dev, "pciback_error_detected(bus:%x,devfn:%x)\n",
   3.279 +		dev->bus->number, dev->devfn);
   3.280 +
   3.281 +	down_write(&pcistub_sem);
   3.282 +	psdev = pcistub_device_find(pci_domain_nr(dev->bus),
   3.283 +				dev->bus->number,
   3.284 +				PCI_SLOT(dev->devfn),
   3.285 +				PCI_FUNC(dev->devfn));
   3.286 +	if ( !psdev || !psdev->pdev || !psdev->pdev->sh_info)
   3.287 +	{
   3.288 +		dev_err(&dev->dev, 
   3.289 +			"pciback device is not found/in use/connected!\n");
   3.290 +		goto end;
   3.291 +	}
   3.292 +	/*Guest owns the device yet no aer handler regiested, kill guest*/
   3.293 +	if ( !test_bit(_XEN_PCIB_AERHANDLER, 
   3.294 +		(unsigned long *)&psdev->pdev->sh_info->flags) ) {
   3.295 +		dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
   3.296 +		kill_domain_by_device(psdev);
   3.297 +		goto release;
   3.298 +	}
   3.299 +	result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
   3.300 +
   3.301 +	if (result == PCI_ERS_RESULT_NONE ||
   3.302 +		result == PCI_ERS_RESULT_DISCONNECT) {
   3.303 +		dev_dbg(&dev->dev, 
   3.304 +			"No AER error_detected service or disconnected!\n");
   3.305 +		kill_domain_by_device(psdev);
   3.306 +	}
   3.307 +release:
   3.308 +	pcistub_device_put(psdev);
   3.309 +end:
   3.310 +	up_write(&pcistub_sem);
   3.311 +	return result;
   3.312 +}
   3.313 +
   3.314 +/*pciback_error_resume: it will send the error_resume request to  pcifront 
   3.315 +* in case of the device driver could provide this service, and then wait 
   3.316 +* for pcifront ack.
   3.317 +* @dev: pointer to PCI devices
   3.318 +*/
   3.319 +
   3.320 +static void pciback_error_resume(struct pci_dev *dev)
   3.321 +{
   3.322 +	struct pcistub_device *psdev;
   3.323 +
   3.324 +	dev_dbg(&dev->dev, "pciback_error_resume(bus:%x,devfn:%x)\n",
   3.325 +		dev->bus->number, dev->devfn);
   3.326 +
   3.327 +	down_write(&pcistub_sem);
   3.328 +	psdev = pcistub_device_find(pci_domain_nr(dev->bus),
   3.329 +				dev->bus->number,
   3.330 +				PCI_SLOT(dev->devfn),
   3.331 +				PCI_FUNC(dev->devfn));
   3.332 +	if ( !psdev || !psdev->pdev || !psdev->pdev->sh_info)
   3.333 +	{
   3.334 +		dev_err(&dev->dev, 
   3.335 +			"pciback device is not found/in use/connected!\n");
   3.336 +		goto end;
   3.337 +	}
   3.338 +
   3.339 +	if ( !test_bit(_XEN_PCIB_AERHANDLER, 
   3.340 +		(unsigned long *)&psdev->pdev->sh_info->flags) ) {
   3.341 +		dev_err(&dev->dev, 
   3.342 +			"guest with no AER driver should have been killed\n");
   3.343 +		kill_domain_by_device(psdev);
   3.344 +		goto release;
   3.345 +	}
   3.346 +	common_process(psdev, 1, XEN_PCI_OP_aer_resume, PCI_ERS_RESULT_RECOVERED);
   3.347 +release:
   3.348 +	pcistub_device_put(psdev);
   3.349 +end:
   3.350 +	up_write(&pcistub_sem);
   3.351 +	return;
   3.352 +}
   3.353 +
   3.354 +/*add pciback AER handling*/
   3.355 +static struct pci_error_handlers pciback_error_handler = {
   3.356 +	.error_detected = pciback_error_detected,
   3.357 +	.mmio_enabled = pciback_mmio_enabled,
   3.358 +	.slot_reset = pciback_slot_reset,
   3.359 +	.resume = pciback_error_resume,
   3.360 +};
   3.361 +
   3.362  /*
   3.363   * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
   3.364   * for a normal device. I don't want it to be loaded automatically.
   3.365 @@ -487,6 +805,7 @@ static struct pci_driver pciback_pci_dri
   3.366  	.id_table = pcistub_ids,
   3.367  	.probe = pcistub_probe,
   3.368  	.remove = pcistub_remove,
   3.369 +	.err_handler = &pciback_error_handler,
   3.370  };
   3.371  
   3.372  static inline int str_to_slot(const char *buf, int *domain, int *bus,
     4.1 --- a/drivers/xen/pciback/pciback.h	Mon Nov 24 11:03:21 2008 +0000
     4.2 +++ b/drivers/xen/pciback/pciback.h	Mon Nov 24 11:04:54 2008 +0000
     4.3 @@ -22,6 +22,8 @@ struct pci_dev_entry {
     4.4  
     4.5  #define _PDEVF_op_active 	(0)
     4.6  #define PDEVF_op_active 	(1<<(_PDEVF_op_active))
     4.7 +#define _PCIB_op_pending	(1)
     4.8 +#define PCIB_op_pending		(1<<(_PCIB_op_pending))
     4.9  
    4.10  struct pciback_device {
    4.11  	void *pci_dev_data;
    4.12 @@ -81,6 +83,16 @@ void pciback_release_pci_dev(struct pcib
    4.13  struct pci_dev *pciback_get_pci_dev(struct pciback_device *pdev,
    4.14  				    unsigned int domain, unsigned int bus,
    4.15  				    unsigned int devfn);
    4.16 +
    4.17 +/** 
    4.18 +* Add for domain0 PCIE-AER handling. Get guest domain/bus/devfn in pciback
    4.19 +* before sending aer request to pcifront, so that guest could identify 
    4.20 +* device, coopearte with pciback to finish aer recovery job if device driver
    4.21 +* has the capability
    4.22 +*/
    4.23 +
    4.24 +int pciback_get_pcifront_dev(struct pci_dev *pcidev, struct pciback_device *pdev, 
    4.25 +				unsigned int *domain, unsigned int *bus, unsigned int *devfn);
    4.26  int pciback_init_devices(struct pciback_device *pdev);
    4.27  int pciback_publish_pci_roots(struct pciback_device *pdev,
    4.28  			      publish_pci_root_cb cb);
    4.29 @@ -108,4 +120,7 @@ int pciback_disable_msix(struct pciback_
    4.30                          struct pci_dev *dev, struct xen_pci_op *op);
    4.31  #endif
    4.32  extern int verbose_request;
    4.33 +
    4.34 +void test_and_schedule_op(struct pciback_device *pdev);
    4.35  #endif
    4.36 +
     5.1 --- a/drivers/xen/pciback/pciback_ops.c	Mon Nov 24 11:03:21 2008 +0000
     5.2 +++ b/drivers/xen/pciback/pciback_ops.c	Mon Nov 24 11:04:54 2008 +0000
     5.3 @@ -4,6 +4,7 @@
     5.4   *   Author: Ryan Wilson <hap9@epoch.ncsc.mil>
     5.5   */
     5.6  #include <linux/module.h>
     5.7 +#include <linux/wait.h>
     5.8  #include <asm/bitops.h>
     5.9  #include <xen/evtchn.h>
    5.10  #include "pciback.h"
    5.11 @@ -37,14 +38,29 @@ void pciback_reset_device(struct pci_dev
    5.12  		}
    5.13  	}
    5.14  }
    5.15 -
    5.16 -static inline void test_and_schedule_op(struct pciback_device *pdev)
    5.17 +extern wait_queue_head_t aer_wait_queue;
    5.18 +extern struct workqueue_struct *pciback_wq;
    5.19 +/*
    5.20 +* Now the same evtchn is used for both pcifront conf_read_write request
    5.21 +* as well as pcie aer front end ack. We use a new work_queue to schedule
    5.22 +* pciback conf_read_write service for avoiding confict with aer_core 
    5.23 +* do_recovery job which also use the system default work_queue
    5.24 +*/
    5.25 +void test_and_schedule_op(struct pciback_device *pdev)
    5.26  {
    5.27  	/* Check that frontend is requesting an operation and that we are not
    5.28  	 * already processing a request */
    5.29  	if (test_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags)
    5.30  	    && !test_and_set_bit(_PDEVF_op_active, &pdev->flags))
    5.31 -		schedule_work(&pdev->op_work);
    5.32 +	{
    5.33 +		queue_work(pciback_wq, &pdev->op_work);
    5.34 +	}
    5.35 +	/*_XEN_PCIB_active should have been cleared by pcifront. And also make
    5.36 +	sure pciback is waiting for ack by checking _PCIB_op_pending*/
    5.37 +	if (!test_bit(_XEN_PCIB_active,(unsigned long *)&pdev->sh_info->flags)
    5.38 +	    &&test_bit(_PCIB_op_pending, &pdev->flags)) {
    5.39 +		wake_up(&aer_wait_queue);
    5.40 +	}
    5.41  }
    5.42  
    5.43  /* Performing the configuration space reads/writes must not be done in atomic
    5.44 @@ -103,7 +119,8 @@ void pciback_do_op(void *data)
    5.45  	smp_mb__after_clear_bit(); /* /before/ final check for work */
    5.46  
    5.47  	/* Check to see if the driver domain tried to start another request in
    5.48 -	 * between clearing _XEN_PCIF_active and clearing _PDEVF_op_active. */
    5.49 +	 * between clearing _XEN_PCIF_active and clearing _PDEVF_op_active. 
    5.50 +	*/
    5.51  	test_and_schedule_op(pdev);
    5.52  }
    5.53  
     6.1 --- a/drivers/xen/pciback/slot.c	Mon Nov 24 11:03:21 2008 +0000
     6.2 +++ b/drivers/xen/pciback/slot.c	Mon Nov 24 11:04:54 2008 +0000
     6.3 @@ -155,3 +155,33 @@ void pciback_release_devices(struct pcib
     6.4  	kfree(slot_dev);
     6.5  	pdev->pci_dev_data = NULL;
     6.6  }
     6.7 +
     6.8 +int pciback_get_pcifront_dev(struct pci_dev *pcidev, struct pciback_device *pdev, 
     6.9 +		unsigned int *domain, unsigned int *bus, unsigned int *devfn)
    6.10 +{
    6.11 +	int slot, busnr;
    6.12 +	struct slot_dev_data *slot_dev = pdev->pci_dev_data;
    6.13 +	struct pci_dev *dev;
    6.14 +	int found = 0;
    6.15 +	unsigned long flags;
    6.16 +
    6.17 +	spin_lock_irqsave(&slot_dev->lock, flags);
    6.18 +
    6.19 +	for (busnr = 0; busnr < PCI_BUS_NBR; bus++)
    6.20 +		for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
    6.21 +			dev = slot_dev->slots[busnr][slot];
    6.22 +			if (dev && dev->bus->number == pcidev->bus->number
    6.23 +				&& dev->devfn == pcidev->devfn
    6.24 +				&& pci_domain_nr(dev->bus) == pci_domain_nr(pcidev->bus)) {
    6.25 +				found = 1;
    6.26 +				*domain = 0;
    6.27 +				*bus = busnr;
    6.28 +				*devfn = PCI_DEVFN(slot,0);
    6.29 +				goto out;
    6.30 +			}
    6.31 +		}
    6.32 +out:
    6.33 +	spin_unlock_irqrestore(&slot_dev->lock, flags);
    6.34 +	return found;
    6.35 +
    6.36 +}
     7.1 --- a/drivers/xen/pciback/vpci.c	Mon Nov 24 11:03:21 2008 +0000
     7.2 +++ b/drivers/xen/pciback/vpci.c	Mon Nov 24 11:04:54 2008 +0000
     7.3 @@ -210,3 +210,33 @@ void pciback_release_devices(struct pcib
     7.4  	kfree(vpci_dev);
     7.5  	pdev->pci_dev_data = NULL;
     7.6  }
     7.7 +
     7.8 +int pciback_get_pcifront_dev(struct pci_dev *pcidev, struct pciback_device *pdev, 
     7.9 +		unsigned int *domain, unsigned int *bus, unsigned int *devfn)
    7.10 +{
    7.11 +	struct pci_dev_entry *entry;
    7.12 +	struct pci_dev *dev = NULL;
    7.13 +	struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
    7.14 +	unsigned long flags;
    7.15 +	int found = 0, slot;
    7.16 +
    7.17 +	spin_lock_irqsave(&vpci_dev->lock, flags);
    7.18 +	for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
    7.19 +		list_for_each_entry(entry,
    7.20 +			    &vpci_dev->dev_list[slot],
    7.21 +			    list) {
    7.22 +			dev = entry->dev;
    7.23 +			if (dev && dev->bus->number == pcidev->bus->number
    7.24 +				&& pci_domain_nr(dev->bus) == pci_domain_nr(pcidev->bus)
    7.25 +				&& dev->devfn == pcidev->devfn)
    7.26 +			{
    7.27 +				found = 1;
    7.28 +				*domain = 0;
    7.29 +				*bus = 0;
    7.30 +				*devfn = PCI_DEVFN(slot, PCI_FUNC(pcidev->devfn));
    7.31 +			}
    7.32 +		}		
    7.33 +	}
    7.34 +	spin_unlock_irqrestore(&vpci_dev->lock, flags);
    7.35 +	return found;
    7.36 +}
     8.1 --- a/drivers/xen/pciback/xenbus.c	Mon Nov 24 11:03:21 2008 +0000
     8.2 +++ b/drivers/xen/pciback/xenbus.c	Mon Nov 24 11:04:54 2008 +0000
     8.3 @@ -12,6 +12,7 @@
     8.4  #include "pciback.h"
     8.5  
     8.6  #define INVALID_EVTCHN_IRQ  (-1)
     8.7 +struct workqueue_struct *pciback_wq;
     8.8  
     8.9  static struct pciback_device *alloc_pdev(struct xenbus_device *xdev)
    8.10  {
    8.11 @@ -694,11 +695,17 @@ int __init pciback_xenbus_register(void)
    8.12  {
    8.13  	if (!is_running_on_xen())
    8.14  		return -ENODEV;
    8.15 -
    8.16 +	pciback_wq = create_workqueue("pciback_workqueue");
    8.17 +	if (!pciback_wq) {
    8.18 +		printk(KERN_ERR "pciback_xenbus_register: create"
    8.19 +			"pciback_workqueue failed\n");
    8.20 +		return -EFAULT;
    8.21 +	}
    8.22  	return xenbus_register_backend(&xenbus_pciback_driver);
    8.23  }
    8.24  
    8.25  void __exit pciback_xenbus_unregister(void)
    8.26  {
    8.27 +	destroy_workqueue(pciback_wq);
    8.28  	xenbus_unregister_driver(&xenbus_pciback_driver);
    8.29  }
     9.1 --- a/drivers/xen/pcifront/pci_op.c	Mon Nov 24 11:03:21 2008 +0000
     9.2 +++ b/drivers/xen/pcifront/pci_op.c	Mon Nov 24 11:04:54 2008 +0000
     9.3 @@ -8,6 +8,7 @@
     9.4  #include <linux/init.h>
     9.5  #include <linux/pci.h>
     9.6  #include <linux/spinlock.h>
     9.7 +#include <asm/bitops.h>
     9.8  #include <linux/time.h>
     9.9  #include <xen/evtchn.h>
    9.10  #include "pcifront.h"
    9.11 @@ -153,6 +154,15 @@ static int errno_to_pcibios_err(int errn
    9.12  	return errno;
    9.13  }
    9.14  
    9.15 +static inline void schedule_pcifront_aer_op(struct pcifront_device *pdev)
    9.16 +{
    9.17 +	if (test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
    9.18 +		&& !test_and_set_bit(_PDEVB_op_active, &pdev->flags)) {
    9.19 +		dev_dbg(&pdev->xdev->dev, "schedule aer frontend job\n");
    9.20 +		schedule_work(&pdev->op_work);
    9.21 +	}
    9.22 +}
    9.23 +
    9.24  static int do_pci_op(struct pcifront_device *pdev, struct xen_pci_op *op)
    9.25  {
    9.26  	int err = 0;
    9.27 @@ -199,6 +209,18 @@ static int do_pci_op(struct pcifront_dev
    9.28  		}
    9.29  	}
    9.30  
    9.31 +	/*
    9.32 +	* We might lose backend service request since we 
    9.33 +	* reuse same evtchn with pci_conf backend response. So re-schedule
    9.34 +	* aer pcifront service.
    9.35 +	*/
    9.36 +	if (test_bit(_XEN_PCIB_active, 
    9.37 +			(unsigned long*)&pdev->sh_info->flags)) {
    9.38 +		dev_err(&pdev->xdev->dev, 
    9.39 +			"schedule aer pcifront service\n");
    9.40 +		schedule_pcifront_aer_op(pdev);
    9.41 +	}
    9.42 +
    9.43  	memcpy(op, active_op, sizeof(struct xen_pci_op));
    9.44  
    9.45  	err = op->err;
    9.46 @@ -549,3 +571,96 @@ void pcifront_free_roots(struct pcifront
    9.47  		kfree(bus_entry);
    9.48  	}
    9.49  }
    9.50 +
    9.51 +static pci_ers_result_t pcifront_common_process( int cmd, struct pcifront_device *pdev,
    9.52 +	pci_channel_state_t state)
    9.53 +{
    9.54 +	pci_ers_result_t result;
    9.55 +	struct pci_driver *pdrv;
    9.56 +	int bus = pdev->sh_info->aer_op.bus;
    9.57 +	int devfn = pdev->sh_info->aer_op.devfn;
    9.58 +	struct pci_dev *pcidev;
    9.59 +	int flag = 0;
    9.60 +
    9.61 +	dev_dbg(&pdev->xdev->dev, 
    9.62 +		"pcifront AER process: cmd %x (bus:%x, devfn%x)",
    9.63 +		cmd, bus, devfn);
    9.64 +	result = PCI_ERS_RESULT_NONE;
    9.65 +
    9.66 +	pcidev = pci_get_bus_and_slot(bus, devfn);
    9.67 +	if (!pcidev || !pcidev->driver){
    9.68 +		dev_err(&pcidev->dev, 
    9.69 +			"device or driver is NULL\n");
    9.70 +		return result;
    9.71 +	}
    9.72 +	pdrv = pcidev->driver;
    9.73 +
    9.74 +	if (get_driver(&pdrv->driver)) {
    9.75 +		if (pdrv->err_handler && pdrv->err_handler->error_detected) {
    9.76 +			dev_dbg(&pcidev->dev,
    9.77 +				"trying to call AER service\n");
    9.78 +			if (pcidev) {
    9.79 +				flag = 1;
    9.80 +				switch(cmd) {
    9.81 +				case XEN_PCI_OP_aer_detected:
    9.82 +					result = pdrv->err_handler->error_detected(pcidev, state);
    9.83 +					break;
    9.84 +				case XEN_PCI_OP_aer_mmio:
    9.85 +					result = pdrv->err_handler->mmio_enabled(pcidev);
    9.86 +					break;
    9.87 +				case XEN_PCI_OP_aer_slotreset:
    9.88 +					result = pdrv->err_handler->slot_reset(pcidev);
    9.89 +					break;
    9.90 +				case XEN_PCI_OP_aer_resume:
    9.91 +					pdrv->err_handler->resume(pcidev);
    9.92 +					break;
    9.93 +				default:
    9.94 +					dev_err(&pdev->xdev->dev,
    9.95 +						"bad request in aer recovery operation!\n");
    9.96 +
    9.97 +				}
    9.98 +			}
    9.99 +		}
   9.100 +		put_driver(&pdrv->driver);
   9.101 +	}
   9.102 +	if (!flag)
   9.103 +		result = PCI_ERS_RESULT_NONE;
   9.104 +
   9.105 +	return result;
   9.106 +}
   9.107 +
   9.108 +
   9.109 +void pcifront_do_aer(void *data)
   9.110 +{
   9.111 +	struct pcifront_device *pdev = data;
   9.112 +	int cmd = pdev->sh_info->aer_op.cmd;
   9.113 +	pci_channel_state_t state = 
   9.114 +		(pci_channel_state_t)pdev->sh_info->aer_op.err;
   9.115 +
   9.116 +	/*If a pci_conf op is in progress, 
   9.117 +		we have to wait until it is done before service aer op*/
   9.118 +	dev_dbg(&pdev->xdev->dev, 
   9.119 +		"pcifront service aer bus %x devfn %x\n", pdev->sh_info->aer_op.bus,
   9.120 +		pdev->sh_info->aer_op.devfn);
   9.121 +
   9.122 +	pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
   9.123 +
   9.124 +	wmb();
   9.125 +	clear_bit(_XEN_PCIB_active, (unsigned long*)&pdev->sh_info->flags);
   9.126 +	notify_remote_via_evtchn(pdev->evtchn);
   9.127 +
   9.128 +	/*in case of we lost an aer request in four lines time_window*/
   9.129 +	smp_mb__before_clear_bit();
   9.130 +	clear_bit( _PDEVB_op_active, &pdev->flags);
   9.131 +	smp_mb__after_clear_bit();
   9.132 +
   9.133 +	schedule_pcifront_aer_op(pdev);
   9.134 +
   9.135 +}
   9.136 +
   9.137 +irqreturn_t pcifront_handler_aer(int irq, void *dev, struct pt_regs *regs)
   9.138 +{
   9.139 +	struct pcifront_device *pdev = dev;
   9.140 +	schedule_pcifront_aer_op(pdev);
   9.141 +	return IRQ_HANDLED;
   9.142 +}
    10.1 --- a/drivers/xen/pcifront/pcifront.h	Mon Nov 24 11:03:21 2008 +0000
    10.2 +++ b/drivers/xen/pcifront/pcifront.h	Mon Nov 24 11:04:54 2008 +0000
    10.3 @@ -10,13 +10,19 @@
    10.4  #include <linux/pci.h>
    10.5  #include <xen/xenbus.h>
    10.6  #include <xen/interface/io/pciif.h>
    10.7 +#include <linux/interrupt.h>
    10.8  #include <xen/pcifront.h>
    10.9 +#include <asm/atomic.h>
   10.10 +#include <linux/workqueue.h>
   10.11  
   10.12  struct pci_bus_entry {
   10.13  	struct list_head list;
   10.14  	struct pci_bus *bus;
   10.15  };
   10.16  
   10.17 +#define _PDEVB_op_active		(0)
   10.18 +#define PDEVB_op_active 		(1 << (_PDEVB_op_active))
   10.19 +
   10.20  struct pcifront_device {
   10.21  	struct xenbus_device *xdev;
   10.22  	struct list_head root_buses;
   10.23 @@ -28,6 +34,9 @@ struct pcifront_device {
   10.24  	/* Lock this when doing any operations in sh_info */
   10.25  	spinlock_t sh_info_lock;
   10.26  	struct xen_pci_sharedinfo *sh_info;
   10.27 +	struct work_struct op_work;
   10.28 +	unsigned long flags;
   10.29 +
   10.30  };
   10.31  
   10.32  int pcifront_connect(struct pcifront_device *pdev);
   10.33 @@ -39,4 +48,8 @@ int pcifront_rescan_root(struct pcifront
   10.34  			 unsigned int domain, unsigned int bus);
   10.35  void pcifront_free_roots(struct pcifront_device *pdev);
   10.36  
   10.37 +void pcifront_do_aer( void *data);
   10.38 +
   10.39 +irqreturn_t pcifront_handler_aer(int irq, void *dev, struct pt_regs *regs);
   10.40 +
   10.41  #endif	/* __XEN_PCIFRONT_H__ */
    11.1 --- a/drivers/xen/pcifront/xenbus.c	Mon Nov 24 11:03:21 2008 +0000
    11.2 +++ b/drivers/xen/pcifront/xenbus.c	Mon Nov 24 11:04:54 2008 +0000
    11.3 @@ -7,6 +7,7 @@
    11.4  #include <linux/init.h>
    11.5  #include <linux/mm.h>
    11.6  #include <xen/xenbus.h>
    11.7 +#include <xen/evtchn.h>
    11.8  #include <xen/gnttab.h>
    11.9  #include "pcifront.h"
   11.10  
   11.11 @@ -34,6 +35,9 @@ static struct pcifront_device *alloc_pde
   11.12  	}
   11.13  	pdev->sh_info->flags = 0;
   11.14  
   11.15 +	/*Flag for registering PV AER handler*/
   11.16 +	set_bit(_XEN_PCIB_AERHANDLER, (void*)&pdev->sh_info->flags);
   11.17 +
   11.18  	xdev->dev.driver_data = pdev;
   11.19  	pdev->xdev = xdev;
   11.20  
   11.21 @@ -45,6 +49,8 @@ static struct pcifront_device *alloc_pde
   11.22  	pdev->evtchn = INVALID_EVTCHN;
   11.23  	pdev->gnt_ref = INVALID_GRANT_REF;
   11.24  
   11.25 +	INIT_WORK(&pdev->op_work, pcifront_do_aer, pdev);
   11.26 +
   11.27  	dev_dbg(&xdev->dev, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n",
   11.28  		pdev, pdev->sh_info);
   11.29        out:
   11.30 @@ -57,6 +63,11 @@ static void free_pdev(struct pcifront_de
   11.31  
   11.32  	pcifront_free_roots(pdev);
   11.33  
   11.34 +	/*For PCIE_AER error handling job*/
   11.35 +	cancel_delayed_work(&pdev->op_work);
   11.36 +	flush_scheduled_work();
   11.37 +	unbind_from_irqhandler(pdev->evtchn, pdev);
   11.38 +
   11.39  	if (pdev->evtchn != INVALID_EVTCHN)
   11.40  		xenbus_free_evtchn(pdev->xdev, pdev->evtchn);
   11.41  
   11.42 @@ -84,6 +95,9 @@ static int pcifront_publish_info(struct 
   11.43  	if (err)
   11.44  		goto out;
   11.45  
   11.46 +	bind_caller_port_to_irqhandler(pdev->evtchn, pcifront_handler_aer, 
   11.47 +		SA_SAMPLE_RANDOM, "pcifront", pdev); 
   11.48 +
   11.49        do_publish:
   11.50  	err = xenbus_transaction_start(&trans);
   11.51  	if (err) {
    12.1 --- a/include/xen/interface/io/pciif.h	Mon Nov 24 11:03:21 2008 +0000
    12.2 +++ b/include/xen/interface/io/pciif.h	Mon Nov 24 11:04:54 2008 +0000
    12.3 @@ -30,14 +30,22 @@
    12.4  /* xen_pci_sharedinfo flags */
    12.5  #define _XEN_PCIF_active     (0)
    12.6  #define XEN_PCIF_active      (1<<_XEN_PCI_active)
    12.7 +#define _XEN_PCIB_AERHANDLER (1)
    12.8 +#define XEN_PCIB_AERHANDLER  (1<<_XEN_PCIB_AERHANDLER)
    12.9 +#define _XEN_PCIB_active     (2)
   12.10 +#define XEN_PCIB_active      (1<<_XEN_PCIB_active)
   12.11  
   12.12  /* xen_pci_op commands */
   12.13 -#define XEN_PCI_OP_conf_read    (0)
   12.14 -#define XEN_PCI_OP_conf_write   (1)
   12.15 -#define XEN_PCI_OP_enable_msi   (2)
   12.16 -#define XEN_PCI_OP_disable_msi  (3)
   12.17 -#define XEN_PCI_OP_enable_msix  (4)
   12.18 -#define XEN_PCI_OP_disable_msix (5)
   12.19 +#define XEN_PCI_OP_conf_read    	(0)
   12.20 +#define XEN_PCI_OP_conf_write   	(1)
   12.21 +#define XEN_PCI_OP_enable_msi   	(2)
   12.22 +#define XEN_PCI_OP_disable_msi  	(3)
   12.23 +#define XEN_PCI_OP_enable_msix  	(4)
   12.24 +#define XEN_PCI_OP_disable_msix 	(5)
   12.25 +#define XEN_PCI_OP_aer_detected 	(6)
   12.26 +#define XEN_PCI_OP_aer_resume		(7)
   12.27 +#define XEN_PCI_OP_aer_mmio		(8)
   12.28 +#define XEN_PCI_OP_aer_slotreset	(9)
   12.29  
   12.30  /* xen_pci_op error numbers */
   12.31  #define XEN_PCI_ERR_success          (0)
   12.32 @@ -82,10 +90,25 @@ struct xen_pci_op {
   12.33      struct xen_msix_entry msix_entries[SH_INFO_MAX_VEC];
   12.34  };
   12.35  
   12.36 +/*used for pcie aer handling*/
   12.37 +struct xen_pcie_aer_op
   12.38 +{
   12.39 +
   12.40 +    /* IN: what action to perform: XEN_PCI_OP_* */
   12.41 +    uint32_t cmd;
   12.42 +    /*IN/OUT: return aer_op result or carry error_detected state as input*/
   12.43 +    int32_t err;
   12.44 +
   12.45 +    /* IN: which device to touch */
   12.46 +    uint32_t domain; /* PCI Domain/Segment*/
   12.47 +    uint32_t bus;
   12.48 +    uint32_t devfn;
   12.49 +};
   12.50  struct xen_pci_sharedinfo {
   12.51      /* flags - XEN_PCIF_* */
   12.52      uint32_t flags;
   12.53      struct xen_pci_op op;
   12.54 +    struct xen_pcie_aer_op aer_op;
   12.55  };
   12.56  
   12.57  #endif /* __XEN_PCI_COMMON_H__ */