1 /*
2 * Xen PCI Frontend.
3 *
4 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
5 */
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/mm.h>
9 #include <xen/xenbus.h>
10 #include <xen/events.h>
11 #include <xen/grant_table.h>
12 #include <xen/page.h>
13 #include <linux/spinlock.h>
14 #include <linux/pci.h>
15 #include <linux/msi.h>
16 #include <xen/interface/io/pciif.h>
17 #include <asm/xen/pci.h>
18 #include <linux/interrupt.h>
19 #include <linux/atomic.h>
20 #include <linux/workqueue.h>
21 #include <linux/bitops.h>
22 #include <linux/time.h>
23 #include <xen/platform_pci.h>
24
25 #include <asm/xen/swiotlb-xen.h>
26 #define INVALID_GRANT_REF (0)
27 #define INVALID_EVTCHN (-1)
28
29 struct pci_bus_entry {
30 struct list_head list;
31 struct pci_bus *bus;
32 };
33
34 #define _PDEVB_op_active (0)
35 #define PDEVB_op_active (1 << (_PDEVB_op_active))
36
37 struct pcifront_device {
38 struct xenbus_device *xdev;
39 struct list_head root_buses;
40
41 int evtchn;
42 int gnt_ref;
43
44 int irq;
45
46 /* Lock this when doing any operations in sh_info */
47 spinlock_t sh_info_lock;
48 struct xen_pci_sharedinfo *sh_info;
49 struct work_struct op_work;
50 unsigned long flags;
51
52 };
53
54 struct pcifront_sd {
55 struct pci_sysdata sd;
56 struct pcifront_device *pdev;
57 };
58
59 static inline struct pcifront_device *
pcifront_get_pdev(struct pcifront_sd * sd)60 pcifront_get_pdev(struct pcifront_sd *sd)
61 {
62 return sd->pdev;
63 }
64
pcifront_init_sd(struct pcifront_sd * sd,unsigned int domain,unsigned int bus,struct pcifront_device * pdev)65 static inline void pcifront_init_sd(struct pcifront_sd *sd,
66 unsigned int domain, unsigned int bus,
67 struct pcifront_device *pdev)
68 {
69 /* Because we do not expose that information via XenBus. */
70 sd->sd.node = first_online_node;
71 sd->sd.domain = domain;
72 sd->pdev = pdev;
73 }
74
75 static DEFINE_SPINLOCK(pcifront_dev_lock);
76 static struct pcifront_device *pcifront_dev;
77
78 static int verbose_request;
79 module_param(verbose_request, int, 0644);
80
errno_to_pcibios_err(int errno)81 static int errno_to_pcibios_err(int errno)
82 {
83 switch (errno) {
84 case XEN_PCI_ERR_success:
85 return PCIBIOS_SUCCESSFUL;
86
87 case XEN_PCI_ERR_dev_not_found:
88 return PCIBIOS_DEVICE_NOT_FOUND;
89
90 case XEN_PCI_ERR_invalid_offset:
91 case XEN_PCI_ERR_op_failed:
92 return PCIBIOS_BAD_REGISTER_NUMBER;
93
94 case XEN_PCI_ERR_not_implemented:
95 return PCIBIOS_FUNC_NOT_SUPPORTED;
96
97 case XEN_PCI_ERR_access_denied:
98 return PCIBIOS_SET_FAILED;
99 }
100 return errno;
101 }
102
schedule_pcifront_aer_op(struct pcifront_device * pdev)103 static inline void schedule_pcifront_aer_op(struct pcifront_device *pdev)
104 {
105 if (test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
106 && !test_and_set_bit(_PDEVB_op_active, &pdev->flags)) {
107 dev_dbg(&pdev->xdev->dev, "schedule aer frontend job\n");
108 schedule_work(&pdev->op_work);
109 }
110 }
111
do_pci_op(struct pcifront_device * pdev,struct xen_pci_op * op)112 static int do_pci_op(struct pcifront_device *pdev, struct xen_pci_op *op)
113 {
114 int err = 0;
115 struct xen_pci_op *active_op = &pdev->sh_info->op;
116 unsigned long irq_flags;
117 evtchn_port_t port = pdev->evtchn;
118 unsigned irq = pdev->irq;
119 s64 ns, ns_timeout;
120 struct timeval tv;
121
122 spin_lock_irqsave(&pdev->sh_info_lock, irq_flags);
123
124 memcpy(active_op, op, sizeof(struct xen_pci_op));
125
126 /* Go */
127 wmb();
128 set_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
129 notify_remote_via_evtchn(port);
130
131 /*
132 * We set a poll timeout of 3 seconds but give up on return after
133 * 2 seconds. It is better to time out too late rather than too early
134 * (in the latter case we end up continually re-executing poll() with a
135 * timeout in the past). 1s difference gives plenty of slack for error.
136 */
137 do_gettimeofday(&tv);
138 ns_timeout = timeval_to_ns(&tv) + 2 * (s64)NSEC_PER_SEC;
139
140 xen_clear_irq_pending(irq);
141
142 while (test_bit(_XEN_PCIF_active,
143 (unsigned long *)&pdev->sh_info->flags)) {
144 xen_poll_irq_timeout(irq, jiffies + 3*HZ);
145 xen_clear_irq_pending(irq);
146 do_gettimeofday(&tv);
147 ns = timeval_to_ns(&tv);
148 if (ns > ns_timeout) {
149 dev_err(&pdev->xdev->dev,
150 "pciback not responding!!!\n");
151 clear_bit(_XEN_PCIF_active,
152 (unsigned long *)&pdev->sh_info->flags);
153 err = XEN_PCI_ERR_dev_not_found;
154 goto out;
155 }
156 }
157
158 /*
159 * We might lose backend service request since we
160 * reuse same evtchn with pci_conf backend response. So re-schedule
161 * aer pcifront service.
162 */
163 if (test_bit(_XEN_PCIB_active,
164 (unsigned long *)&pdev->sh_info->flags)) {
165 dev_err(&pdev->xdev->dev,
166 "schedule aer pcifront service\n");
167 schedule_pcifront_aer_op(pdev);
168 }
169
170 memcpy(op, active_op, sizeof(struct xen_pci_op));
171
172 err = op->err;
173 out:
174 spin_unlock_irqrestore(&pdev->sh_info_lock, irq_flags);
175 return err;
176 }
177
178 /* Access to this function is spinlocked in drivers/pci/access.c */
pcifront_bus_read(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)179 static int pcifront_bus_read(struct pci_bus *bus, unsigned int devfn,
180 int where, int size, u32 *val)
181 {
182 int err = 0;
183 struct xen_pci_op op = {
184 .cmd = XEN_PCI_OP_conf_read,
185 .domain = pci_domain_nr(bus),
186 .bus = bus->number,
187 .devfn = devfn,
188 .offset = where,
189 .size = size,
190 };
191 struct pcifront_sd *sd = bus->sysdata;
192 struct pcifront_device *pdev = pcifront_get_pdev(sd);
193
194 if (verbose_request)
195 dev_info(&pdev->xdev->dev,
196 "read dev=%04x:%02x:%02x.%d - offset %x size %d\n",
197 pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
198 PCI_FUNC(devfn), where, size);
199
200 err = do_pci_op(pdev, &op);
201
202 if (likely(!err)) {
203 if (verbose_request)
204 dev_info(&pdev->xdev->dev, "read got back value %x\n",
205 op.value);
206
207 *val = op.value;
208 } else if (err == -ENODEV) {
209 /* No device here, pretend that it just returned 0 */
210 err = 0;
211 *val = 0;
212 }
213
214 return errno_to_pcibios_err(err);
215 }
216
217 /* Access to this function is spinlocked in drivers/pci/access.c */
pcifront_bus_write(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)218 static int pcifront_bus_write(struct pci_bus *bus, unsigned int devfn,
219 int where, int size, u32 val)
220 {
221 struct xen_pci_op op = {
222 .cmd = XEN_PCI_OP_conf_write,
223 .domain = pci_domain_nr(bus),
224 .bus = bus->number,
225 .devfn = devfn,
226 .offset = where,
227 .size = size,
228 .value = val,
229 };
230 struct pcifront_sd *sd = bus->sysdata;
231 struct pcifront_device *pdev = pcifront_get_pdev(sd);
232
233 if (verbose_request)
234 dev_info(&pdev->xdev->dev,
235 "write dev=%04x:%02x:%02x.%d - "
236 "offset %x size %d val %x\n",
237 pci_domain_nr(bus), bus->number,
238 PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val);
239
240 return errno_to_pcibios_err(do_pci_op(pdev, &op));
241 }
242
243 static struct pci_ops pcifront_bus_ops = {
244 .read = pcifront_bus_read,
245 .write = pcifront_bus_write,
246 };
247
248 #ifdef CONFIG_PCI_MSI
pci_frontend_enable_msix(struct pci_dev * dev,int vector[],int nvec)249 static int pci_frontend_enable_msix(struct pci_dev *dev,
250 int vector[], int nvec)
251 {
252 int err;
253 int i;
254 struct xen_pci_op op = {
255 .cmd = XEN_PCI_OP_enable_msix,
256 .domain = pci_domain_nr(dev->bus),
257 .bus = dev->bus->number,
258 .devfn = dev->devfn,
259 .value = nvec,
260 };
261 struct pcifront_sd *sd = dev->bus->sysdata;
262 struct pcifront_device *pdev = pcifront_get_pdev(sd);
263 struct msi_desc *entry;
264
265 if (nvec > SH_INFO_MAX_VEC) {
266 dev_err(&dev->dev, "too much vector for pci frontend: %x."
267 " Increase SH_INFO_MAX_VEC.\n", nvec);
268 return -EINVAL;
269 }
270
271 i = 0;
272 list_for_each_entry(entry, &dev->msi_list, list) {
273 op.msix_entries[i].entry = entry->msi_attrib.entry_nr;
274 /* Vector is useless at this point. */
275 op.msix_entries[i].vector = -1;
276 i++;
277 }
278
279 err = do_pci_op(pdev, &op);
280
281 if (likely(!err)) {
282 if (likely(!op.value)) {
283 /* we get the result */
284 for (i = 0; i < nvec; i++) {
285 if (op.msix_entries[i].vector <= 0) {
286 dev_warn(&dev->dev, "MSI-X entry %d is invalid: %d!\n",
287 i, op.msix_entries[i].vector);
288 err = -EINVAL;
289 vector[i] = -1;
290 continue;
291 }
292 vector[i] = op.msix_entries[i].vector;
293 }
294 } else {
295 printk(KERN_DEBUG "enable msix get value %x\n",
296 op.value);
297 err = op.value;
298 }
299 } else {
300 dev_err(&dev->dev, "enable msix get err %x\n", err);
301 }
302 return err;
303 }
304
pci_frontend_disable_msix(struct pci_dev * dev)305 static void pci_frontend_disable_msix(struct pci_dev *dev)
306 {
307 int err;
308 struct xen_pci_op op = {
309 .cmd = XEN_PCI_OP_disable_msix,
310 .domain = pci_domain_nr(dev->bus),
311 .bus = dev->bus->number,
312 .devfn = dev->devfn,
313 };
314 struct pcifront_sd *sd = dev->bus->sysdata;
315 struct pcifront_device *pdev = pcifront_get_pdev(sd);
316
317 err = do_pci_op(pdev, &op);
318
319 /* What should do for error ? */
320 if (err)
321 dev_err(&dev->dev, "pci_disable_msix get err %x\n", err);
322 }
323
pci_frontend_enable_msi(struct pci_dev * dev,int vector[])324 static int pci_frontend_enable_msi(struct pci_dev *dev, int vector[])
325 {
326 int err;
327 struct xen_pci_op op = {
328 .cmd = XEN_PCI_OP_enable_msi,
329 .domain = pci_domain_nr(dev->bus),
330 .bus = dev->bus->number,
331 .devfn = dev->devfn,
332 };
333 struct pcifront_sd *sd = dev->bus->sysdata;
334 struct pcifront_device *pdev = pcifront_get_pdev(sd);
335
336 err = do_pci_op(pdev, &op);
337 if (likely(!err)) {
338 vector[0] = op.value;
339 if (op.value <= 0) {
340 dev_warn(&dev->dev, "MSI entry is invalid: %d!\n",
341 op.value);
342 err = -EINVAL;
343 vector[0] = -1;
344 }
345 } else {
346 dev_err(&dev->dev, "pci frontend enable msi failed for dev "
347 "%x:%x\n", op.bus, op.devfn);
348 err = -EINVAL;
349 }
350 return err;
351 }
352
pci_frontend_disable_msi(struct pci_dev * dev)353 static void pci_frontend_disable_msi(struct pci_dev *dev)
354 {
355 int err;
356 struct xen_pci_op op = {
357 .cmd = XEN_PCI_OP_disable_msi,
358 .domain = pci_domain_nr(dev->bus),
359 .bus = dev->bus->number,
360 .devfn = dev->devfn,
361 };
362 struct pcifront_sd *sd = dev->bus->sysdata;
363 struct pcifront_device *pdev = pcifront_get_pdev(sd);
364
365 err = do_pci_op(pdev, &op);
366 if (err == XEN_PCI_ERR_dev_not_found) {
367 /* XXX No response from backend, what shall we do? */
368 printk(KERN_DEBUG "get no response from backend for disable MSI\n");
369 return;
370 }
371 if (err)
372 /* how can pciback notify us fail? */
373 printk(KERN_DEBUG "get fake response frombackend\n");
374 }
375
376 static struct xen_pci_frontend_ops pci_frontend_ops = {
377 .enable_msi = pci_frontend_enable_msi,
378 .disable_msi = pci_frontend_disable_msi,
379 .enable_msix = pci_frontend_enable_msix,
380 .disable_msix = pci_frontend_disable_msix,
381 };
382
pci_frontend_registrar(int enable)383 static void pci_frontend_registrar(int enable)
384 {
385 if (enable)
386 xen_pci_frontend = &pci_frontend_ops;
387 else
388 xen_pci_frontend = NULL;
389 };
390 #else
pci_frontend_registrar(int enable)391 static inline void pci_frontend_registrar(int enable) { };
392 #endif /* CONFIG_PCI_MSI */
393
394 /* Claim resources for the PCI frontend as-is, backend won't allow changes */
pcifront_claim_resource(struct pci_dev * dev,void * data)395 static int pcifront_claim_resource(struct pci_dev *dev, void *data)
396 {
397 struct pcifront_device *pdev = data;
398 int i;
399 struct resource *r;
400
401 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
402 r = &dev->resource[i];
403
404 if (!r->parent && r->start && r->flags) {
405 dev_info(&pdev->xdev->dev, "claiming resource %s/%d\n",
406 pci_name(dev), i);
407 if (pci_claim_resource(dev, i)) {
408 dev_err(&pdev->xdev->dev, "Could not claim resource %s/%d! "
409 "Device offline. Try using e820_host=1 in the guest config.\n",
410 pci_name(dev), i);
411 }
412 }
413 }
414
415 return 0;
416 }
417
pcifront_scan_bus(struct pcifront_device * pdev,unsigned int domain,unsigned int bus,struct pci_bus * b)418 static int pcifront_scan_bus(struct pcifront_device *pdev,
419 unsigned int domain, unsigned int bus,
420 struct pci_bus *b)
421 {
422 struct pci_dev *d;
423 unsigned int devfn;
424
425 /* Scan the bus for functions and add.
426 * We omit handling of PCI bridge attachment because pciback prevents
427 * bridges from being exported.
428 */
429 for (devfn = 0; devfn < 0x100; devfn++) {
430 d = pci_get_slot(b, devfn);
431 if (d) {
432 /* Device is already known. */
433 pci_dev_put(d);
434 continue;
435 }
436
437 d = pci_scan_single_device(b, devfn);
438 if (d)
439 dev_info(&pdev->xdev->dev, "New device on "
440 "%04x:%02x:%02x.%d found.\n", domain, bus,
441 PCI_SLOT(devfn), PCI_FUNC(devfn));
442 }
443
444 return 0;
445 }
446
pcifront_scan_root(struct pcifront_device * pdev,unsigned int domain,unsigned int bus)447 static int pcifront_scan_root(struct pcifront_device *pdev,
448 unsigned int domain, unsigned int bus)
449 {
450 struct pci_bus *b;
451 struct pcifront_sd *sd = NULL;
452 struct pci_bus_entry *bus_entry = NULL;
453 int err = 0;
454
455 #ifndef CONFIG_PCI_DOMAINS
456 if (domain != 0) {
457 dev_err(&pdev->xdev->dev,
458 "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
459 dev_err(&pdev->xdev->dev,
460 "Please compile with CONFIG_PCI_DOMAINS\n");
461 err = -EINVAL;
462 goto err_out;
463 }
464 #endif
465
466 dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n",
467 domain, bus);
468
469 bus_entry = kzalloc(sizeof(*bus_entry), GFP_KERNEL);
470 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
471 if (!bus_entry || !sd) {
472 err = -ENOMEM;
473 goto err_out;
474 }
475 pcifront_init_sd(sd, domain, bus, pdev);
476
477 pci_lock_rescan_remove();
478
479 b = pci_scan_bus_parented(&pdev->xdev->dev, bus,
480 &pcifront_bus_ops, sd);
481 if (!b) {
482 dev_err(&pdev->xdev->dev,
483 "Error creating PCI Frontend Bus!\n");
484 err = -ENOMEM;
485 pci_unlock_rescan_remove();
486 goto err_out;
487 }
488
489 bus_entry->bus = b;
490
491 list_add(&bus_entry->list, &pdev->root_buses);
492
493 /* pci_scan_bus_parented skips devices which do not have a have
494 * devfn==0. The pcifront_scan_bus enumerates all devfn. */
495 err = pcifront_scan_bus(pdev, domain, bus, b);
496
497 /* Claim resources before going "live" with our devices */
498 pci_walk_bus(b, pcifront_claim_resource, pdev);
499
500 /* Create SysFS and notify udev of the devices. Aka: "going live" */
501 pci_bus_add_devices(b);
502
503 pci_unlock_rescan_remove();
504 return err;
505
506 err_out:
507 kfree(bus_entry);
508 kfree(sd);
509
510 return err;
511 }
512
pcifront_rescan_root(struct pcifront_device * pdev,unsigned int domain,unsigned int bus)513 static int pcifront_rescan_root(struct pcifront_device *pdev,
514 unsigned int domain, unsigned int bus)
515 {
516 int err;
517 struct pci_bus *b;
518
519 #ifndef CONFIG_PCI_DOMAINS
520 if (domain != 0) {
521 dev_err(&pdev->xdev->dev,
522 "PCI Root in non-zero PCI Domain! domain=%d\n", domain);
523 dev_err(&pdev->xdev->dev,
524 "Please compile with CONFIG_PCI_DOMAINS\n");
525 return -EINVAL;
526 }
527 #endif
528
529 dev_info(&pdev->xdev->dev, "Rescanning PCI Frontend Bus %04x:%02x\n",
530 domain, bus);
531
532 b = pci_find_bus(domain, bus);
533 if (!b)
534 /* If the bus is unknown, create it. */
535 return pcifront_scan_root(pdev, domain, bus);
536
537 err = pcifront_scan_bus(pdev, domain, bus, b);
538
539 /* Claim resources before going "live" with our devices */
540 pci_walk_bus(b, pcifront_claim_resource, pdev);
541
542 /* Create SysFS and notify udev of the devices. Aka: "going live" */
543 pci_bus_add_devices(b);
544
545 return err;
546 }
547
free_root_bus_devs(struct pci_bus * bus)548 static void free_root_bus_devs(struct pci_bus *bus)
549 {
550 struct pci_dev *dev;
551
552 while (!list_empty(&bus->devices)) {
553 dev = container_of(bus->devices.next, struct pci_dev,
554 bus_list);
555 dev_dbg(&dev->dev, "removing device\n");
556 pci_stop_and_remove_bus_device(dev);
557 }
558 }
559
pcifront_free_roots(struct pcifront_device * pdev)560 static void pcifront_free_roots(struct pcifront_device *pdev)
561 {
562 struct pci_bus_entry *bus_entry, *t;
563
564 dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n");
565
566 pci_lock_rescan_remove();
567 list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) {
568 list_del(&bus_entry->list);
569
570 free_root_bus_devs(bus_entry->bus);
571
572 kfree(bus_entry->bus->sysdata);
573
574 device_unregister(bus_entry->bus->bridge);
575 pci_remove_bus(bus_entry->bus);
576
577 kfree(bus_entry);
578 }
579 pci_unlock_rescan_remove();
580 }
581
pcifront_common_process(int cmd,struct pcifront_device * pdev,pci_channel_state_t state)582 static pci_ers_result_t pcifront_common_process(int cmd,
583 struct pcifront_device *pdev,
584 pci_channel_state_t state)
585 {
586 pci_ers_result_t result;
587 struct pci_driver *pdrv;
588 int bus = pdev->sh_info->aer_op.bus;
589 int devfn = pdev->sh_info->aer_op.devfn;
590 struct pci_dev *pcidev;
591 int flag = 0;
592
593 dev_dbg(&pdev->xdev->dev,
594 "pcifront AER process: cmd %x (bus:%x, devfn%x)",
595 cmd, bus, devfn);
596 result = PCI_ERS_RESULT_NONE;
597
598 pcidev = pci_get_bus_and_slot(bus, devfn);
599 if (!pcidev || !pcidev->driver) {
600 dev_err(&pdev->xdev->dev, "device or AER driver is NULL\n");
601 if (pcidev)
602 pci_dev_put(pcidev);
603 return result;
604 }
605 pdrv = pcidev->driver;
606
607 if (pdrv) {
608 if (pdrv->err_handler && pdrv->err_handler->error_detected) {
609 dev_dbg(&pcidev->dev,
610 "trying to call AER service\n");
611 if (pcidev) {
612 flag = 1;
613 switch (cmd) {
614 case XEN_PCI_OP_aer_detected:
615 result = pdrv->err_handler->
616 error_detected(pcidev, state);
617 break;
618 case XEN_PCI_OP_aer_mmio:
619 result = pdrv->err_handler->
620 mmio_enabled(pcidev);
621 break;
622 case XEN_PCI_OP_aer_slotreset:
623 result = pdrv->err_handler->
624 slot_reset(pcidev);
625 break;
626 case XEN_PCI_OP_aer_resume:
627 pdrv->err_handler->resume(pcidev);
628 break;
629 default:
630 dev_err(&pdev->xdev->dev,
631 "bad request in aer recovery "
632 "operation!\n");
633
634 }
635 }
636 }
637 }
638 if (!flag)
639 result = PCI_ERS_RESULT_NONE;
640
641 return result;
642 }
643
644
pcifront_do_aer(struct work_struct * data)645 static void pcifront_do_aer(struct work_struct *data)
646 {
647 struct pcifront_device *pdev =
648 container_of(data, struct pcifront_device, op_work);
649 int cmd = pdev->sh_info->aer_op.cmd;
650 pci_channel_state_t state =
651 (pci_channel_state_t)pdev->sh_info->aer_op.err;
652
653 /*If a pci_conf op is in progress,
654 we have to wait until it is done before service aer op*/
655 dev_dbg(&pdev->xdev->dev,
656 "pcifront service aer bus %x devfn %x\n",
657 pdev->sh_info->aer_op.bus, pdev->sh_info->aer_op.devfn);
658
659 pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
660
661 /* Post the operation to the guest. */
662 wmb();
663 clear_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags);
664 notify_remote_via_evtchn(pdev->evtchn);
665
666 /*in case of we lost an aer request in four lines time_window*/
667 smp_mb__before_atomic();
668 clear_bit(_PDEVB_op_active, &pdev->flags);
669 smp_mb__after_atomic();
670
671 schedule_pcifront_aer_op(pdev);
672
673 }
674
pcifront_handler_aer(int irq,void * dev)675 static irqreturn_t pcifront_handler_aer(int irq, void *dev)
676 {
677 struct pcifront_device *pdev = dev;
678 schedule_pcifront_aer_op(pdev);
679 return IRQ_HANDLED;
680 }
pcifront_connect_and_init_dma(struct pcifront_device * pdev)681 static int pcifront_connect_and_init_dma(struct pcifront_device *pdev)
682 {
683 int err = 0;
684
685 spin_lock(&pcifront_dev_lock);
686
687 if (!pcifront_dev) {
688 dev_info(&pdev->xdev->dev, "Installing PCI frontend\n");
689 pcifront_dev = pdev;
690 } else
691 err = -EEXIST;
692
693 spin_unlock(&pcifront_dev_lock);
694
695 if (!err && !swiotlb_nr_tbl()) {
696 err = pci_xen_swiotlb_init_late();
697 if (err)
698 dev_err(&pdev->xdev->dev, "Could not setup SWIOTLB!\n");
699 }
700 return err;
701 }
702
pcifront_disconnect(struct pcifront_device * pdev)703 static void pcifront_disconnect(struct pcifront_device *pdev)
704 {
705 spin_lock(&pcifront_dev_lock);
706
707 if (pdev == pcifront_dev) {
708 dev_info(&pdev->xdev->dev,
709 "Disconnecting PCI Frontend Buses\n");
710 pcifront_dev = NULL;
711 }
712
713 spin_unlock(&pcifront_dev_lock);
714 }
alloc_pdev(struct xenbus_device * xdev)715 static struct pcifront_device *alloc_pdev(struct xenbus_device *xdev)
716 {
717 struct pcifront_device *pdev;
718
719 pdev = kzalloc(sizeof(struct pcifront_device), GFP_KERNEL);
720 if (pdev == NULL)
721 goto out;
722
723 pdev->sh_info =
724 (struct xen_pci_sharedinfo *)__get_free_page(GFP_KERNEL);
725 if (pdev->sh_info == NULL) {
726 kfree(pdev);
727 pdev = NULL;
728 goto out;
729 }
730 pdev->sh_info->flags = 0;
731
732 /*Flag for registering PV AER handler*/
733 set_bit(_XEN_PCIB_AERHANDLER, (void *)&pdev->sh_info->flags);
734
735 dev_set_drvdata(&xdev->dev, pdev);
736 pdev->xdev = xdev;
737
738 INIT_LIST_HEAD(&pdev->root_buses);
739
740 spin_lock_init(&pdev->sh_info_lock);
741
742 pdev->evtchn = INVALID_EVTCHN;
743 pdev->gnt_ref = INVALID_GRANT_REF;
744 pdev->irq = -1;
745
746 INIT_WORK(&pdev->op_work, pcifront_do_aer);
747
748 dev_dbg(&xdev->dev, "Allocated pdev @ 0x%p pdev->sh_info @ 0x%p\n",
749 pdev, pdev->sh_info);
750 out:
751 return pdev;
752 }
753
free_pdev(struct pcifront_device * pdev)754 static void free_pdev(struct pcifront_device *pdev)
755 {
756 dev_dbg(&pdev->xdev->dev, "freeing pdev @ 0x%p\n", pdev);
757
758 pcifront_free_roots(pdev);
759
760 cancel_work_sync(&pdev->op_work);
761
762 if (pdev->irq >= 0)
763 unbind_from_irqhandler(pdev->irq, pdev);
764
765 if (pdev->evtchn != INVALID_EVTCHN)
766 xenbus_free_evtchn(pdev->xdev, pdev->evtchn);
767
768 if (pdev->gnt_ref != INVALID_GRANT_REF)
769 gnttab_end_foreign_access(pdev->gnt_ref, 0 /* r/w page */,
770 (unsigned long)pdev->sh_info);
771 else
772 free_page((unsigned long)pdev->sh_info);
773
774 dev_set_drvdata(&pdev->xdev->dev, NULL);
775
776 kfree(pdev);
777 }
778
pcifront_publish_info(struct pcifront_device * pdev)779 static int pcifront_publish_info(struct pcifront_device *pdev)
780 {
781 int err = 0;
782 struct xenbus_transaction trans;
783
784 err = xenbus_grant_ring(pdev->xdev, virt_to_mfn(pdev->sh_info));
785 if (err < 0)
786 goto out;
787
788 pdev->gnt_ref = err;
789
790 err = xenbus_alloc_evtchn(pdev->xdev, &pdev->evtchn);
791 if (err)
792 goto out;
793
794 err = bind_evtchn_to_irqhandler(pdev->evtchn, pcifront_handler_aer,
795 0, "pcifront", pdev);
796
797 if (err < 0)
798 return err;
799
800 pdev->irq = err;
801
802 do_publish:
803 err = xenbus_transaction_start(&trans);
804 if (err) {
805 xenbus_dev_fatal(pdev->xdev, err,
806 "Error writing configuration for backend "
807 "(start transaction)");
808 goto out;
809 }
810
811 err = xenbus_printf(trans, pdev->xdev->nodename,
812 "pci-op-ref", "%u", pdev->gnt_ref);
813 if (!err)
814 err = xenbus_printf(trans, pdev->xdev->nodename,
815 "event-channel", "%u", pdev->evtchn);
816 if (!err)
817 err = xenbus_printf(trans, pdev->xdev->nodename,
818 "magic", XEN_PCI_MAGIC);
819
820 if (err) {
821 xenbus_transaction_end(trans, 1);
822 xenbus_dev_fatal(pdev->xdev, err,
823 "Error writing configuration for backend");
824 goto out;
825 } else {
826 err = xenbus_transaction_end(trans, 0);
827 if (err == -EAGAIN)
828 goto do_publish;
829 else if (err) {
830 xenbus_dev_fatal(pdev->xdev, err,
831 "Error completing transaction "
832 "for backend");
833 goto out;
834 }
835 }
836
837 xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
838
839 dev_dbg(&pdev->xdev->dev, "publishing successful!\n");
840
841 out:
842 return err;
843 }
844
pcifront_try_connect(struct pcifront_device * pdev)845 static int pcifront_try_connect(struct pcifront_device *pdev)
846 {
847 int err = -EFAULT;
848 int i, num_roots, len;
849 char str[64];
850 unsigned int domain, bus;
851
852
853 /* Only connect once */
854 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
855 XenbusStateInitialised)
856 goto out;
857
858 err = pcifront_connect_and_init_dma(pdev);
859 if (err && err != -EEXIST) {
860 xenbus_dev_fatal(pdev->xdev, err,
861 "Error setting up PCI Frontend");
862 goto out;
863 }
864
865 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
866 "root_num", "%d", &num_roots);
867 if (err == -ENOENT) {
868 xenbus_dev_error(pdev->xdev, err,
869 "No PCI Roots found, trying 0000:00");
870 err = pcifront_scan_root(pdev, 0, 0);
871 num_roots = 0;
872 } else if (err != 1) {
873 if (err == 0)
874 err = -EINVAL;
875 xenbus_dev_fatal(pdev->xdev, err,
876 "Error reading number of PCI roots");
877 goto out;
878 }
879
880 for (i = 0; i < num_roots; i++) {
881 len = snprintf(str, sizeof(str), "root-%d", i);
882 if (unlikely(len >= (sizeof(str) - 1))) {
883 err = -ENOMEM;
884 goto out;
885 }
886
887 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
888 "%x:%x", &domain, &bus);
889 if (err != 2) {
890 if (err >= 0)
891 err = -EINVAL;
892 xenbus_dev_fatal(pdev->xdev, err,
893 "Error reading PCI root %d", i);
894 goto out;
895 }
896
897 err = pcifront_scan_root(pdev, domain, bus);
898 if (err) {
899 xenbus_dev_fatal(pdev->xdev, err,
900 "Error scanning PCI root %04x:%02x",
901 domain, bus);
902 goto out;
903 }
904 }
905
906 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
907
908 out:
909 return err;
910 }
911
pcifront_try_disconnect(struct pcifront_device * pdev)912 static int pcifront_try_disconnect(struct pcifront_device *pdev)
913 {
914 int err = 0;
915 enum xenbus_state prev_state;
916
917
918 prev_state = xenbus_read_driver_state(pdev->xdev->nodename);
919
920 if (prev_state >= XenbusStateClosing)
921 goto out;
922
923 if (prev_state == XenbusStateConnected) {
924 pcifront_free_roots(pdev);
925 pcifront_disconnect(pdev);
926 }
927
928 err = xenbus_switch_state(pdev->xdev, XenbusStateClosed);
929
930 out:
931
932 return err;
933 }
934
pcifront_attach_devices(struct pcifront_device * pdev)935 static int pcifront_attach_devices(struct pcifront_device *pdev)
936 {
937 int err = -EFAULT;
938 int i, num_roots, len;
939 unsigned int domain, bus;
940 char str[64];
941
942 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
943 XenbusStateReconfiguring)
944 goto out;
945
946 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
947 "root_num", "%d", &num_roots);
948 if (err == -ENOENT) {
949 xenbus_dev_error(pdev->xdev, err,
950 "No PCI Roots found, trying 0000:00");
951 err = pcifront_rescan_root(pdev, 0, 0);
952 num_roots = 0;
953 } else if (err != 1) {
954 if (err == 0)
955 err = -EINVAL;
956 xenbus_dev_fatal(pdev->xdev, err,
957 "Error reading number of PCI roots");
958 goto out;
959 }
960
961 for (i = 0; i < num_roots; i++) {
962 len = snprintf(str, sizeof(str), "root-%d", i);
963 if (unlikely(len >= (sizeof(str) - 1))) {
964 err = -ENOMEM;
965 goto out;
966 }
967
968 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
969 "%x:%x", &domain, &bus);
970 if (err != 2) {
971 if (err >= 0)
972 err = -EINVAL;
973 xenbus_dev_fatal(pdev->xdev, err,
974 "Error reading PCI root %d", i);
975 goto out;
976 }
977
978 err = pcifront_rescan_root(pdev, domain, bus);
979 if (err) {
980 xenbus_dev_fatal(pdev->xdev, err,
981 "Error scanning PCI root %04x:%02x",
982 domain, bus);
983 goto out;
984 }
985 }
986
987 xenbus_switch_state(pdev->xdev, XenbusStateConnected);
988
989 out:
990 return err;
991 }
992
pcifront_detach_devices(struct pcifront_device * pdev)993 static int pcifront_detach_devices(struct pcifront_device *pdev)
994 {
995 int err = 0;
996 int i, num_devs;
997 unsigned int domain, bus, slot, func;
998 struct pci_dev *pci_dev;
999 char str[64];
1000
1001 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
1002 XenbusStateConnected)
1003 goto out;
1004
1005 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "num_devs", "%d",
1006 &num_devs);
1007 if (err != 1) {
1008 if (err >= 0)
1009 err = -EINVAL;
1010 xenbus_dev_fatal(pdev->xdev, err,
1011 "Error reading number of PCI devices");
1012 goto out;
1013 }
1014
1015 /* Find devices being detached and remove them. */
1016 for (i = 0; i < num_devs; i++) {
1017 int l, state;
1018 l = snprintf(str, sizeof(str), "state-%d", i);
1019 if (unlikely(l >= (sizeof(str) - 1))) {
1020 err = -ENOMEM;
1021 goto out;
1022 }
1023 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str, "%d",
1024 &state);
1025 if (err != 1)
1026 state = XenbusStateUnknown;
1027
1028 if (state != XenbusStateClosing)
1029 continue;
1030
1031 /* Remove device. */
1032 l = snprintf(str, sizeof(str), "vdev-%d", i);
1033 if (unlikely(l >= (sizeof(str) - 1))) {
1034 err = -ENOMEM;
1035 goto out;
1036 }
1037 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
1038 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
1039 if (err != 4) {
1040 if (err >= 0)
1041 err = -EINVAL;
1042 xenbus_dev_fatal(pdev->xdev, err,
1043 "Error reading PCI device %d", i);
1044 goto out;
1045 }
1046
1047 pci_dev = pci_get_domain_bus_and_slot(domain, bus,
1048 PCI_DEVFN(slot, func));
1049 if (!pci_dev) {
1050 dev_dbg(&pdev->xdev->dev,
1051 "Cannot get PCI device %04x:%02x:%02x.%d\n",
1052 domain, bus, slot, func);
1053 continue;
1054 }
1055 pci_lock_rescan_remove();
1056 pci_stop_and_remove_bus_device(pci_dev);
1057 pci_dev_put(pci_dev);
1058 pci_unlock_rescan_remove();
1059
1060 dev_dbg(&pdev->xdev->dev,
1061 "PCI device %04x:%02x:%02x.%d removed.\n",
1062 domain, bus, slot, func);
1063 }
1064
1065 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfiguring);
1066
1067 out:
1068 return err;
1069 }
1070
pcifront_backend_changed(struct xenbus_device * xdev,enum xenbus_state be_state)1071 static void __init_refok pcifront_backend_changed(struct xenbus_device *xdev,
1072 enum xenbus_state be_state)
1073 {
1074 struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1075
1076 switch (be_state) {
1077 case XenbusStateUnknown:
1078 case XenbusStateInitialising:
1079 case XenbusStateInitWait:
1080 case XenbusStateInitialised:
1081 break;
1082
1083 case XenbusStateConnected:
1084 pcifront_try_connect(pdev);
1085 break;
1086
1087 case XenbusStateClosed:
1088 if (xdev->state == XenbusStateClosed)
1089 break;
1090 /* Missed the backend's CLOSING state -- fallthrough */
1091 case XenbusStateClosing:
1092 dev_warn(&xdev->dev, "backend going away!\n");
1093 pcifront_try_disconnect(pdev);
1094 break;
1095
1096 case XenbusStateReconfiguring:
1097 pcifront_detach_devices(pdev);
1098 break;
1099
1100 case XenbusStateReconfigured:
1101 pcifront_attach_devices(pdev);
1102 break;
1103 }
1104 }
1105
pcifront_xenbus_probe(struct xenbus_device * xdev,const struct xenbus_device_id * id)1106 static int pcifront_xenbus_probe(struct xenbus_device *xdev,
1107 const struct xenbus_device_id *id)
1108 {
1109 int err = 0;
1110 struct pcifront_device *pdev = alloc_pdev(xdev);
1111
1112 if (pdev == NULL) {
1113 err = -ENOMEM;
1114 xenbus_dev_fatal(xdev, err,
1115 "Error allocating pcifront_device struct");
1116 goto out;
1117 }
1118
1119 err = pcifront_publish_info(pdev);
1120 if (err)
1121 free_pdev(pdev);
1122
1123 out:
1124 return err;
1125 }
1126
pcifront_xenbus_remove(struct xenbus_device * xdev)1127 static int pcifront_xenbus_remove(struct xenbus_device *xdev)
1128 {
1129 struct pcifront_device *pdev = dev_get_drvdata(&xdev->dev);
1130 if (pdev)
1131 free_pdev(pdev);
1132
1133 return 0;
1134 }
1135
1136 static const struct xenbus_device_id xenpci_ids[] = {
1137 {"pci"},
1138 {""},
1139 };
1140
1141 static struct xenbus_driver xenpci_driver = {
1142 .name = "pcifront",
1143 .ids = xenpci_ids,
1144 .probe = pcifront_xenbus_probe,
1145 .remove = pcifront_xenbus_remove,
1146 .otherend_changed = pcifront_backend_changed,
1147 };
1148
pcifront_init(void)1149 static int __init pcifront_init(void)
1150 {
1151 if (!xen_pv_domain() || xen_initial_domain())
1152 return -ENODEV;
1153
1154 if (!xen_has_pv_devices())
1155 return -ENODEV;
1156
1157 pci_frontend_registrar(1 /* enable */);
1158
1159 return xenbus_register_frontend(&xenpci_driver);
1160 }
1161
pcifront_cleanup(void)1162 static void __exit pcifront_cleanup(void)
1163 {
1164 xenbus_unregister_driver(&xenpci_driver);
1165 pci_frontend_registrar(0 /* disable */);
1166 }
1167 module_init(pcifront_init);
1168 module_exit(pcifront_cleanup);
1169
1170 MODULE_DESCRIPTION("Xen PCI passthrough frontend.");
1171 MODULE_LICENSE("GPL");
1172 MODULE_ALIAS("xen:pci");
1173