2 * PCI Frontend Operations - Communicates with frontend
4 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
6 #include <linux/module.h>
7 #include <linux/init.h>
9 #include <linux/spinlock.h>
10 #include <asm/bitops.h>
11 #include <linux/time.h>
12 #include <xen/evtchn.h>
15 static int verbose_request = 0;
16 module_param(verbose_request, int, 0644);
18 static void pcifront_init_sd(struct pcifront_sd *sd,
19 unsigned int domain, unsigned int bus,
20 struct pcifront_device *pdev)
23 int err, i, j, k, len, root_num, res_count;
24 struct acpi_resource res;
25 unsigned int d, b, byte;
28 unsigned char *buf, *bufp;
31 memset(sd, 0, sizeof(*sd));
34 sd->node = -1; /* Revisit for NUMA */
35 sd->platform_data = pdev;
37 /* Look for resources for this controller in xenbus. */
38 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "root_num",
43 for (i = 0; i < root_num; i++) {
44 len = snprintf(str, sizeof(str), "root-%d", i);
45 if (unlikely(len >= (sizeof(str) - 1)))
48 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
49 str, "%x:%x", &d, &b);
53 if (d == domain && b == bus)
60 len = snprintf(str, sizeof(str), "root-resource-magic");
62 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
66 return; /* No resources, nothing to do */
68 if (magic != (sizeof(res) * 2) + 1) {
69 dev_warn(&pdev->xdev->dev,
70 "pcifront: resource magic mismatch\n");
74 len = snprintf(str, sizeof(str), "root-%d-resources", i);
75 if (unlikely(len >= (sizeof(str) - 1)))
78 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
79 str, "%d", &res_count);
82 return; /* No resources, nothing to do */
84 sd->window = kzalloc(sizeof(*sd->window) * res_count, GFP_KERNEL);
88 /* magic is also the size of the byte stream in xenbus */
89 buf = kmalloc(magic, GFP_KERNEL);
96 /* Read the resources out of xenbus */
97 for (j = 0; j < res_count; j++) {
98 memset(&res, 0, sizeof(res));
99 memset(buf, 0, magic);
101 len = snprintf(str, sizeof(str), "root-%d-resource-%d", i, j);
102 if (unlikely(len >= (sizeof(str) - 1)))
105 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
108 dev_warn(&pdev->xdev->dev,
109 "pcifront: error reading resource %d on bus %04x:%02x\n",
116 memset(tmp, 0, sizeof(tmp));
118 /* Copy ASCII byte stream into structure */
119 for (k = 0; k < magic - 1; k += 2) {
120 memcpy(tmp, bufp, 2);
123 sscanf(tmp, "%02x", &byte);
128 xen_add_resource(sd, domain, bus, &res);
138 static int errno_to_pcibios_err(int errno)
141 case XEN_PCI_ERR_success:
142 return PCIBIOS_SUCCESSFUL;
144 case XEN_PCI_ERR_dev_not_found:
145 return PCIBIOS_DEVICE_NOT_FOUND;
147 case XEN_PCI_ERR_invalid_offset:
148 case XEN_PCI_ERR_op_failed:
149 return PCIBIOS_BAD_REGISTER_NUMBER;
151 case XEN_PCI_ERR_not_implemented:
152 return PCIBIOS_FUNC_NOT_SUPPORTED;
154 case XEN_PCI_ERR_access_denied:
155 return PCIBIOS_SET_FAILED;
160 static inline void schedule_pcifront_aer_op(struct pcifront_device *pdev)
162 if (test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
163 && !test_and_set_bit(_PDEVB_op_active, &pdev->flags)) {
164 dev_dbg(&pdev->xdev->dev, "schedule aer frontend job\n");
165 schedule_work(&pdev->op_work);
169 static int do_pci_op(struct pcifront_device *pdev, struct xen_pci_op *op)
172 struct xen_pci_op *active_op = &pdev->sh_info->op;
173 unsigned long irq_flags;
174 evtchn_port_t port = pdev->evtchn;
178 spin_lock_irqsave(&pdev->sh_info_lock, irq_flags);
180 memcpy(active_op, op, sizeof(struct xen_pci_op));
184 set_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
185 notify_remote_via_evtchn(port);
188 * We set a poll timeout of 3 seconds but give up on return after
189 * 2 seconds. It is better to time out too late rather than too early
190 * (in the latter case we end up continually re-executing poll() with a
191 * timeout in the past). 1s difference gives plenty of slack for error.
193 do_gettimeofday(&tv);
194 ns_timeout = timeval_to_ns(&tv) + 2 * (s64)NSEC_PER_SEC;
198 while (test_bit(_XEN_PCIF_active,
199 (unsigned long *)&pdev->sh_info->flags)) {
200 if (HYPERVISOR_poll(&port, 1, jiffies + 3*HZ))
203 do_gettimeofday(&tv);
204 ns = timeval_to_ns(&tv);
205 if (ns > ns_timeout) {
206 dev_err(&pdev->xdev->dev,
207 "pciback not responding!!!\n");
208 clear_bit(_XEN_PCIF_active,
209 (unsigned long *)&pdev->sh_info->flags);
210 err = XEN_PCI_ERR_dev_not_found;
216 * We might lose backend service request since we
217 * reuse same evtchn with pci_conf backend response. So re-schedule
218 * aer pcifront service.
220 if (test_bit(_XEN_PCIB_active,
221 (unsigned long*)&pdev->sh_info->flags)) {
222 dev_info(&pdev->xdev->dev, "schedule aer pcifront service\n");
223 schedule_pcifront_aer_op(pdev);
226 memcpy(op, active_op, sizeof(struct xen_pci_op));
230 spin_unlock_irqrestore(&pdev->sh_info_lock, irq_flags);
234 /* Access to this function is spinlocked in drivers/pci/access.c */
235 static int pcifront_bus_read(struct pci_bus *bus, unsigned int devfn,
236 int where, int size, u32 *val)
239 struct xen_pci_op op = {
240 .cmd = XEN_PCI_OP_conf_read,
241 .domain = pci_domain_nr(bus),
247 struct pcifront_sd *sd = bus->sysdata;
248 struct pcifront_device *pdev = pcifront_get_pdev(sd);
251 dev_info(&pdev->xdev->dev, "read %02x.%u offset %x size %d\n",
252 PCI_SLOT(devfn), PCI_FUNC(devfn), where, size);
254 err = do_pci_op(pdev, &op);
258 dev_info(&pdev->xdev->dev, "read %02x.%u = %x\n",
259 PCI_SLOT(devfn), PCI_FUNC(devfn), op.value);
262 } else if (err == -ENODEV) {
263 /* No device here, pretend that it just returned 0 */
266 } else if (verbose_request)
267 dev_info(&pdev->xdev->dev, "read %02x.%u -> %d\n",
268 PCI_SLOT(devfn), PCI_FUNC(devfn), err);
270 return errno_to_pcibios_err(err);
273 /* Access to this function is spinlocked in drivers/pci/access.c */
274 static int pcifront_bus_write(struct pci_bus *bus, unsigned int devfn,
275 int where, int size, u32 val)
277 struct xen_pci_op op = {
278 .cmd = XEN_PCI_OP_conf_write,
279 .domain = pci_domain_nr(bus),
286 struct pcifront_sd *sd = bus->sysdata;
287 struct pcifront_device *pdev = pcifront_get_pdev(sd);
290 dev_info(&pdev->xdev->dev,
291 "write %02x.%u offset %x size %d val %x\n",
292 PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val);
294 return errno_to_pcibios_err(do_pci_op(pdev, &op));
297 static struct pci_ops pcifront_bus_ops = {
298 .read = pcifront_bus_read,
299 .write = pcifront_bus_write,
302 #ifdef CONFIG_PCI_MSI
303 int pci_frontend_enable_msix(struct pci_dev *dev,
304 struct msix_entry *entries,
309 struct xen_pci_op op = {
310 .cmd = XEN_PCI_OP_enable_msix,
311 .domain = pci_domain_nr(dev->bus),
312 .bus = dev->bus->number,
316 struct pcifront_sd *sd = dev->bus->sysdata;
317 struct pcifront_device *pdev = pcifront_get_pdev(sd);
319 if (nvec < 0 || nvec > SH_INFO_MAX_VEC) {
320 dev_err(&dev->dev, "too many (%d) vectors for pci frontend\n",
325 for (i = 0; i < nvec; i++) {
326 op.msix_entries[i].entry = entries[i].entry;
327 op.msix_entries[i].vector = entries[i].vector;
330 err = do_pci_op(pdev, &op);
334 /* we get the result */
335 for (i = 0; i < nvec; i++)
336 entries[i].vector = op.msix_entries[i].vector;
338 dev_err(&dev->dev, "enable MSI-X => %#x\n", op.value);
342 dev_err(&dev->dev, "enable MSI-X -> %d\n", err);
348 void pci_frontend_disable_msix(struct pci_dev *dev)
351 struct xen_pci_op op = {
352 .cmd = XEN_PCI_OP_disable_msix,
353 .domain = pci_domain_nr(dev->bus),
354 .bus = dev->bus->number,
357 struct pcifront_sd *sd = dev->bus->sysdata;
358 struct pcifront_device *pdev = pcifront_get_pdev(sd);
360 err = do_pci_op(pdev, &op);
362 /* What should do for error ? */
364 dev_err(&dev->dev, "disable MSI-X -> %d\n", err);
367 int pci_frontend_enable_msi(struct pci_dev *dev)
370 struct xen_pci_op op = {
371 .cmd = XEN_PCI_OP_enable_msi,
372 .domain = pci_domain_nr(dev->bus),
373 .bus = dev->bus->number,
376 struct pcifront_sd *sd = dev->bus->sysdata;
377 struct pcifront_device *pdev = pcifront_get_pdev(sd);
379 err = do_pci_op(pdev, &op);
383 dev_err(&dev->dev, "enable MSI -> %d\n", err);
389 void pci_frontend_disable_msi(struct pci_dev *dev)
392 struct xen_pci_op op = {
393 .cmd = XEN_PCI_OP_disable_msi,
394 .domain = pci_domain_nr(dev->bus),
395 .bus = dev->bus->number,
398 struct pcifront_sd *sd = dev->bus->sysdata;
399 struct pcifront_device *pdev = pcifront_get_pdev(sd);
401 err = do_pci_op(pdev, &op);
405 dev_err(&dev->dev, "disable MSI -> %d\n", err);
407 #endif /* CONFIG_PCI_MSI */
409 /* Claim resources for the PCI frontend as-is, backend won't allow changes */
410 static int __devinit pcifront_claim_resource(struct pci_dev *dev, void *data)
412 struct pcifront_device *pdev = data;
416 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
417 r = &dev->resource[i];
419 if (!r->parent && r->start && r->flags) {
420 dev_dbg(&pdev->xdev->dev, "claiming resource %s/%d\n",
422 pci_claim_resource(dev, i);
429 int __devinit pcifront_scan_root(struct pcifront_device *pdev,
430 unsigned int domain, unsigned int bus)
433 struct pcifront_sd *sd;
434 struct pci_bus_entry *bus_entry;
437 #ifndef CONFIG_PCI_DOMAINS
439 dev_err(&pdev->xdev->dev,
440 "PCI root in non-zero domain %x!\n", domain);
441 dev_err(&pdev->xdev->dev,
442 "Please compile with CONFIG_PCI_DOMAINS\n");
447 dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n",
450 bus_entry = kmalloc(sizeof(*bus_entry), GFP_KERNEL);
451 sd = kmalloc(sizeof(*sd), GFP_KERNEL);
452 if (!bus_entry || !sd) {
456 pcifront_init_sd(sd, domain, bus, pdev);
458 b = pci_scan_bus_parented(&pdev->xdev->dev, bus,
459 &pcifront_bus_ops, sd);
461 dev_err(&pdev->xdev->dev,
462 "Error creating PCI Frontend Bus!\n");
467 pcifront_setup_root_resources(b, sd);
470 list_add(&bus_entry->list, &pdev->root_buses);
472 /* Claim resources before going "live" with our devices */
473 pci_walk_bus(b, pcifront_claim_resource, pdev);
475 pci_bus_add_devices(b);
486 int __devinit pcifront_rescan_root(struct pcifront_device *pdev,
487 unsigned int domain, unsigned int bus)
493 #ifndef CONFIG_PCI_DOMAINS
495 dev_err(&pdev->xdev->dev,
496 "PCI root in non-zero domain %x\n", domain);
497 dev_err(&pdev->xdev->dev,
498 "Please compile with CONFIG_PCI_DOMAINS\n");
503 dev_info(&pdev->xdev->dev, "Rescanning PCI Frontend Bus %04x:%02x\n",
506 b = pci_find_bus(domain, bus);
508 /* If the bus is unknown, create it. */
509 return pcifront_scan_root(pdev, domain, bus);
511 /* Rescan the bus for newly attached functions and add.
512 * We omit handling of PCI bridge attachment because pciback prevents
513 * bridges from being exported.
515 for (devfn = 0; devfn < 0x100; devfn++) {
516 d = pci_get_slot(b, devfn);
518 /* Device is already known. */
523 d = pci_scan_single_device(b, devfn);
525 dev_info(&pdev->xdev->dev,
526 "New device on %04x:%02x:%02x.%u\n",
528 PCI_SLOT(devfn), PCI_FUNC(devfn));
531 /* Claim resources before going "live" with our devices */
532 pci_walk_bus(b, pcifront_claim_resource, pdev);
534 /* Create SysFS and notify udev of the devices. Aka: "going live" */
535 pci_bus_add_devices(b);
540 static void free_root_bus_devs(struct pci_bus *bus)
544 while (!list_empty(&bus->devices)) {
545 dev = container_of(bus->devices.next, struct pci_dev,
547 dev_dbg(&dev->dev, "removing device\n");
548 pci_stop_and_remove_bus_device(dev);
552 void pcifront_free_roots(struct pcifront_device *pdev)
554 struct pci_bus_entry *bus_entry, *t;
556 dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n");
558 list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) {
559 list_del(&bus_entry->list);
561 free_root_bus_devs(bus_entry->bus);
563 kfree(bus_entry->bus->sysdata);
565 device_unregister(bus_entry->bus->bridge);
566 pci_remove_bus(bus_entry->bus);
572 static pci_ers_result_t pcifront_common_process( int cmd, struct pcifront_device *pdev,
573 pci_channel_state_t state)
575 pci_ers_result_t result = PCI_ERS_RESULT_NONE;
576 struct pci_driver *pdrv;
577 int bus = pdev->sh_info->aer_op.bus;
578 int devfn = pdev->sh_info->aer_op.devfn;
579 struct pci_dev *pcidev;
581 dev_dbg(&pdev->xdev->dev,
582 "pcifront AER process: cmd %x (bus %x devfn %x)",
585 pcidev = pci_get_bus_and_slot(bus, devfn);
586 if (!pcidev || !pcidev->driver) {
588 dev_err(&pdev->xdev->dev, "AER device or driver is NULL\n");
591 pdrv = pcidev->driver;
593 if (pdrv->err_handler) {
594 dev_dbg(&pcidev->dev, "trying to call AER service\n");
596 case XEN_PCI_OP_aer_detected:
597 if (pdrv->err_handler->error_detected)
598 result = pdrv->err_handler->error_detected(pcidev, state);
600 case XEN_PCI_OP_aer_mmio:
601 if (pdrv->err_handler->mmio_enabled)
602 result = pdrv->err_handler->mmio_enabled(pcidev);
604 case XEN_PCI_OP_aer_slotreset:
605 if (pdrv->err_handler->slot_reset)
606 result = pdrv->err_handler->slot_reset(pcidev);
608 case XEN_PCI_OP_aer_resume:
609 if (pdrv->err_handler->resume)
610 pdrv->err_handler->resume(pcidev);
613 dev_err(&pdev->xdev->dev,
614 "bad request %x in aer recovery operation!\n",
624 void pcifront_do_aer(struct work_struct *data)
626 struct pcifront_device *pdev = container_of(data, struct pcifront_device, op_work);
627 int cmd = pdev->sh_info->aer_op.cmd;
628 pci_channel_state_t state =
629 (pci_channel_state_t)pdev->sh_info->aer_op.err;
631 /*If a pci_conf op is in progress,
632 we have to wait until it is done before service aer op*/
633 dev_dbg(&pdev->xdev->dev,
634 "pcifront service aer bus %x devfn %x\n", pdev->sh_info->aer_op.bus,
635 pdev->sh_info->aer_op.devfn);
637 pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
640 clear_bit(_XEN_PCIB_active, (unsigned long*)&pdev->sh_info->flags);
641 notify_remote_via_evtchn(pdev->evtchn);
643 /*in case of we lost an aer request in four lines time_window*/
644 smp_mb__before_clear_bit();
645 clear_bit( _PDEVB_op_active, &pdev->flags);
646 smp_mb__after_clear_bit();
648 schedule_pcifront_aer_op(pdev);
652 irqreturn_t pcifront_handler_aer(int irq, void *dev)
654 struct pcifront_device *pdev = dev;
655 schedule_pcifront_aer_op(pdev);