Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / pcifront / pci_op.c
1 /*
2  * PCI Frontend Operations - Communicates with frontend
3  *
4  *   Author: Ryan Wilson <hap9@epoch.ncsc.mil>
5  */
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/pci.h>
9 #include <linux/spinlock.h>
10 #include <asm/bitops.h>
11 #include <linux/time.h>
12 #include <xen/evtchn.h>
13 #include "pcifront.h"
14
15 static int verbose_request = 0;
16 module_param(verbose_request, int, 0644);
17
18 static void pcifront_init_sd(struct pcifront_sd *sd,
19                              unsigned int domain, unsigned int bus,
20                              struct pcifront_device *pdev)
21 {
22 #ifdef __ia64__
23         int err, i, j, k, len, root_num, res_count;
24         struct acpi_resource res;
25         unsigned int d, b, byte;
26         unsigned long magic;
27         char str[64], tmp[3];
28         unsigned char *buf, *bufp;
29         u8 *ptr;
30
31         memset(sd, 0, sizeof(*sd));
32
33         sd->segment = domain;
34         sd->node = -1;  /* Revisit for NUMA */
35         sd->platform_data = pdev;
36
37         /* Look for resources for this controller in xenbus. */
38         err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, "root_num",
39                            "%d", &root_num);
40         if (err != 1)
41                 return;
42
43         for (i = 0; i < root_num; i++) {
44                 len = snprintf(str, sizeof(str), "root-%d", i);
45                 if (unlikely(len >= (sizeof(str) - 1)))
46                         return;
47
48                 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
49                                    str, "%x:%x", &d, &b);
50                 if (err != 2)
51                         return;
52
53                 if (d == domain && b == bus)
54                         break;
55         }
56
57         if (i == root_num)
58                 return;
59
60         len = snprintf(str, sizeof(str), "root-resource-magic");
61
62         err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
63                            str, "%lx", &magic);
64
65         if (err != 1)
66                 return; /* No resources, nothing to do */
67
68         if (magic != (sizeof(res) * 2) + 1) {
69                 dev_warn(&pdev->xdev->dev,
70                          "pcifront: resource magic mismatch\n");
71                 return;
72         }
73
74         len = snprintf(str, sizeof(str), "root-%d-resources", i);
75         if (unlikely(len >= (sizeof(str) - 1)))
76                 return;
77
78         err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend,
79                            str, "%d", &res_count);
80
81         if (err != 1)
82                 return; /* No resources, nothing to do */
83
84         sd->window = kzalloc(sizeof(*sd->window) * res_count, GFP_KERNEL);
85         if (!sd->window)
86                 return;
87
88         /* magic is also the size of the byte stream in xenbus */
89         buf = kmalloc(magic, GFP_KERNEL);
90         if (!buf) {
91                 kfree(sd->window);
92                 sd->window = NULL;
93                 return;
94         }
95
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);
100
101                 len = snprintf(str, sizeof(str), "root-%d-resource-%d", i, j);
102                 if (unlikely(len >= (sizeof(str) - 1)))
103                         return;
104
105                 err = xenbus_scanf(XBT_NIL, pdev->xdev->otherend, str,
106                                    "%s", buf);
107                 if (err != 1) {
108                         dev_warn(&pdev->xdev->dev,
109                                  "pcifront: error reading resource %d on bus %04x:%02x\n",
110                                  j, domain, bus);
111                         continue;
112                 }
113
114                 bufp = buf;
115                 ptr = (u8 *)&res;
116                 memset(tmp, 0, sizeof(tmp));
117
118                 /* Copy ASCII byte stream into structure */
119                 for (k = 0; k < magic - 1; k += 2) {
120                         memcpy(tmp, bufp, 2);
121                         bufp += 2;
122
123                         sscanf(tmp, "%02x", &byte);
124                         *ptr = byte;
125                         ptr++;
126                 }
127
128                 xen_add_resource(sd, domain, bus, &res);
129                 sd->windows++;
130         }
131         kfree(buf);
132 #else
133         sd->domain = domain;
134         sd->pdev = pdev;
135 #endif
136 }
137
138 static int errno_to_pcibios_err(int errno)
139 {
140         switch (errno) {
141         case XEN_PCI_ERR_success:
142                 return PCIBIOS_SUCCESSFUL;
143
144         case XEN_PCI_ERR_dev_not_found:
145                 return PCIBIOS_DEVICE_NOT_FOUND;
146
147         case XEN_PCI_ERR_invalid_offset:
148         case XEN_PCI_ERR_op_failed:
149                 return PCIBIOS_BAD_REGISTER_NUMBER;
150
151         case XEN_PCI_ERR_not_implemented:
152                 return PCIBIOS_FUNC_NOT_SUPPORTED;
153
154         case XEN_PCI_ERR_access_denied:
155                 return PCIBIOS_SET_FAILED;
156         }
157         return errno;
158 }
159
160 static inline void schedule_pcifront_aer_op(struct pcifront_device *pdev)
161 {
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);
166         }
167 }
168
169 static int do_pci_op(struct pcifront_device *pdev, struct xen_pci_op *op)
170 {
171         int err = 0;
172         struct xen_pci_op *active_op = &pdev->sh_info->op;
173         unsigned long irq_flags;
174         evtchn_port_t port = pdev->evtchn;
175         s64 ns, ns_timeout;
176         struct timeval tv;
177
178         spin_lock_irqsave(&pdev->sh_info_lock, irq_flags);
179
180         memcpy(active_op, op, sizeof(struct xen_pci_op));
181
182         /* Go */
183         wmb();
184         set_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
185         notify_remote_via_evtchn(port);
186
187         /*
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.
192          */
193         do_gettimeofday(&tv);
194         ns_timeout = timeval_to_ns(&tv) + 2 * (s64)NSEC_PER_SEC;
195
196         clear_evtchn(port);
197
198         while (test_bit(_XEN_PCIF_active,
199                         (unsigned long *)&pdev->sh_info->flags)) {
200                 if (HYPERVISOR_poll(&port, 1, jiffies + 3*HZ))
201                         BUG();
202                 clear_evtchn(port);
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;
211                         goto out;
212                 }
213         }
214
215         /*
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.
219         */
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);
224         }
225
226         memcpy(op, active_op, sizeof(struct xen_pci_op));
227
228         err = op->err;
229       out:
230         spin_unlock_irqrestore(&pdev->sh_info_lock, irq_flags);
231         return err;
232 }
233
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)
237 {
238         int err;
239         struct xen_pci_op op = {
240                 .cmd    = XEN_PCI_OP_conf_read,
241                 .domain = pci_domain_nr(bus),
242                 .bus    = bus->number,
243                 .devfn  = devfn,
244                 .offset = where,
245                 .size   = size,
246         };
247         struct pcifront_sd *sd = bus->sysdata;
248         struct pcifront_device *pdev = pcifront_get_pdev(sd);
249
250         if (verbose_request)
251                 dev_info(&pdev->xdev->dev, "read %02x.%u offset %x size %d\n",
252                          PCI_SLOT(devfn), PCI_FUNC(devfn), where, size);
253
254         err = do_pci_op(pdev, &op);
255
256         if (likely(!err)) {
257                 if (verbose_request)
258                         dev_info(&pdev->xdev->dev, "read %02x.%u = %x\n",
259                                  PCI_SLOT(devfn), PCI_FUNC(devfn), op.value);
260
261                 *val = op.value;
262         } else if (err == -ENODEV) {
263                 /* No device here, pretend that it just returned 0 */
264                 err = 0;
265                 *val = 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);
269
270         return errno_to_pcibios_err(err);
271 }
272
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)
276 {
277         struct xen_pci_op op = {
278                 .cmd    = XEN_PCI_OP_conf_write,
279                 .domain = pci_domain_nr(bus),
280                 .bus    = bus->number,
281                 .devfn  = devfn,
282                 .offset = where,
283                 .size   = size,
284                 .value  = val,
285         };
286         struct pcifront_sd *sd = bus->sysdata;
287         struct pcifront_device *pdev = pcifront_get_pdev(sd);
288
289         if (verbose_request)
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);
293
294         return errno_to_pcibios_err(do_pci_op(pdev, &op));
295 }
296
297 static struct pci_ops pcifront_bus_ops = {
298         .read = pcifront_bus_read,
299         .write = pcifront_bus_write,
300 };
301
302 #ifdef CONFIG_PCI_MSI
303 int pci_frontend_enable_msix(struct pci_dev *dev,
304                 struct msix_entry *entries,
305                 int nvec)
306 {
307         int err;
308         int i;
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,
313                 .devfn = dev->devfn,
314                 .value = nvec,
315         };
316         struct pcifront_sd *sd = dev->bus->sysdata;
317         struct pcifront_device *pdev = pcifront_get_pdev(sd);
318
319         if (nvec < 0 || nvec > SH_INFO_MAX_VEC) {
320                 dev_err(&dev->dev, "too many (%d) vectors for pci frontend\n",
321                         nvec);
322                 return -EINVAL;
323         }
324
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;
328         }
329
330         err = do_pci_op(pdev, &op);
331
332         if (!err) {
333                 if (!op.value) {
334                         /* we get the result */
335                         for (i = 0; i < nvec; i++)
336                                 entries[i].vector = op.msix_entries[i].vector;
337                 } else {
338                         dev_err(&dev->dev, "enable MSI-X => %#x\n", op.value);
339                         err = op.value;
340                 }
341         } else {
342                 dev_err(&dev->dev, "enable MSI-X -> %d\n", err);
343                 err = -EINVAL;
344         }
345         return err;
346 }
347
348 void pci_frontend_disable_msix(struct pci_dev *dev)
349 {
350         int err;
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,
355                 .devfn = dev->devfn,
356         };
357         struct pcifront_sd *sd = dev->bus->sysdata;
358         struct pcifront_device *pdev = pcifront_get_pdev(sd);
359
360         err = do_pci_op(pdev, &op);
361
362         /* What should do for error ? */
363         if (err)
364                 dev_err(&dev->dev, "disable MSI-X -> %d\n", err);
365 }
366
367 int pci_frontend_enable_msi(struct pci_dev *dev)
368 {
369         int err;
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,
374                 .devfn = dev->devfn,
375         };
376         struct pcifront_sd *sd = dev->bus->sysdata;
377         struct pcifront_device *pdev = pcifront_get_pdev(sd);
378
379         err = do_pci_op(pdev, &op);
380         if (likely(!err))
381                 dev->irq = op.value;
382         else {
383                 dev_err(&dev->dev, "enable MSI -> %d\n", err);
384                 err = -EINVAL;
385         }
386         return err;
387 }
388
389 void pci_frontend_disable_msi(struct pci_dev *dev)
390 {
391         int err;
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,
396                 .devfn = dev->devfn,
397         };
398         struct pcifront_sd *sd = dev->bus->sysdata;
399         struct pcifront_device *pdev = pcifront_get_pdev(sd);
400
401         err = do_pci_op(pdev, &op);
402         if (likely(!err))
403                 dev->irq = op.value;
404         else
405                 dev_err(&dev->dev, "disable MSI -> %d\n", err);
406 }
407 #endif /* CONFIG_PCI_MSI */
408
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)
411 {
412         struct pcifront_device *pdev = data;
413         int i;
414         struct resource *r;
415
416         for (i = 0; i < PCI_NUM_RESOURCES; i++) {
417                 r = &dev->resource[i];
418
419                 if (!r->parent && r->start && r->flags) {
420                         dev_dbg(&pdev->xdev->dev, "claiming resource %s/%d\n",
421                                 pci_name(dev), i);
422                         pci_claim_resource(dev, i);
423                 }
424         }
425
426         return 0;
427 }
428
429 int __devinit pcifront_scan_root(struct pcifront_device *pdev,
430                                  unsigned int domain, unsigned int bus)
431 {
432         struct pci_bus *b;
433         struct pcifront_sd *sd;
434         struct pci_bus_entry *bus_entry;
435         int err = 0;
436
437 #ifndef CONFIG_PCI_DOMAINS
438         if (domain != 0) {
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");
443                 return -EINVAL;
444         }
445 #endif
446
447         dev_info(&pdev->xdev->dev, "Creating PCI Frontend Bus %04x:%02x\n",
448                  domain, bus);
449
450         bus_entry = kmalloc(sizeof(*bus_entry), GFP_KERNEL);
451         sd = kmalloc(sizeof(*sd), GFP_KERNEL);
452         if (!bus_entry || !sd) {
453                 err = -ENOMEM;
454                 goto err_out;
455         }
456         pcifront_init_sd(sd, domain, bus, pdev);
457
458         b = pci_scan_bus_parented(&pdev->xdev->dev, bus,
459                                   &pcifront_bus_ops, sd);
460         if (!b) {
461                 dev_err(&pdev->xdev->dev,
462                         "Error creating PCI Frontend Bus!\n");
463                 err = -ENOMEM;
464                 goto err_out;
465         }
466
467         pcifront_setup_root_resources(b, sd);
468         bus_entry->bus = b;
469
470         list_add(&bus_entry->list, &pdev->root_buses);
471
472         /* Claim resources before going "live" with our devices */
473         pci_walk_bus(b, pcifront_claim_resource, pdev);
474
475         pci_bus_add_devices(b);
476
477         return 0;
478
479       err_out:
480         kfree(bus_entry);
481         kfree(sd);
482
483         return err;
484 }
485
486 int __devinit pcifront_rescan_root(struct pcifront_device *pdev,
487                                    unsigned int domain, unsigned int bus)
488 {
489         struct pci_bus *b;
490         struct pci_dev *d;
491         unsigned int devfn;
492
493 #ifndef CONFIG_PCI_DOMAINS
494         if (domain != 0) {
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");
499                 return -EINVAL;
500         }
501 #endif
502
503         dev_info(&pdev->xdev->dev, "Rescanning PCI Frontend Bus %04x:%02x\n",
504                  domain, bus);
505
506         b = pci_find_bus(domain, bus);
507         if(!b)
508                 /* If the bus is unknown, create it. */
509                 return pcifront_scan_root(pdev, domain, bus);
510
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.
514          */ 
515         for (devfn = 0; devfn < 0x100; devfn++) {
516                 d = pci_get_slot(b, devfn);
517                 if(d) {
518                         /* Device is already known. */
519                         pci_dev_put(d);
520                         continue;
521                 }
522
523                 d = pci_scan_single_device(b, devfn);
524                 if (d)
525                         dev_info(&pdev->xdev->dev,
526                                  "New device on %04x:%02x:%02x.%u\n",
527                                  domain, bus,
528                                  PCI_SLOT(devfn), PCI_FUNC(devfn));
529         }
530
531         /* Claim resources before going "live" with our devices */
532         pci_walk_bus(b, pcifront_claim_resource, pdev);
533
534         /* Create SysFS and notify udev of the devices. Aka: "going live" */
535         pci_bus_add_devices(b);
536
537         return 0;
538 }
539
540 static void free_root_bus_devs(struct pci_bus *bus)
541 {
542         struct pci_dev *dev;
543
544         while (!list_empty(&bus->devices)) {
545                 dev = container_of(bus->devices.next, struct pci_dev,
546                                    bus_list);
547                 dev_dbg(&dev->dev, "removing device\n");
548                 pci_stop_and_remove_bus_device(dev);
549         }
550 }
551
552 void pcifront_free_roots(struct pcifront_device *pdev)
553 {
554         struct pci_bus_entry *bus_entry, *t;
555
556         dev_dbg(&pdev->xdev->dev, "cleaning up root buses\n");
557
558         list_for_each_entry_safe(bus_entry, t, &pdev->root_buses, list) {
559                 list_del(&bus_entry->list);
560
561                 free_root_bus_devs(bus_entry->bus);
562
563                 kfree(bus_entry->bus->sysdata);
564
565                 device_unregister(bus_entry->bus->bridge);
566                 pci_remove_bus(bus_entry->bus);
567
568                 kfree(bus_entry);
569         }
570 }
571
572 static pci_ers_result_t pcifront_common_process( int cmd, struct pcifront_device *pdev,
573         pci_channel_state_t state)
574 {
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;
580
581         dev_dbg(&pdev->xdev->dev, 
582                 "pcifront AER process: cmd %x (bus %x devfn %x)",
583                 cmd, bus, devfn);
584
585         pcidev = pci_get_bus_and_slot(bus, devfn);
586         if (!pcidev || !pcidev->driver) {
587                 pci_dev_put(pcidev);
588                 dev_err(&pdev->xdev->dev, "AER device or driver is NULL\n");
589                 return result;
590         }
591         pdrv = pcidev->driver;
592
593         if (pdrv->err_handler) {
594                 dev_dbg(&pcidev->dev, "trying to call AER service\n");
595                 switch(cmd) {
596                 case XEN_PCI_OP_aer_detected:
597                         if (pdrv->err_handler->error_detected)
598                                 result = pdrv->err_handler->error_detected(pcidev, state);
599                         break;
600                 case XEN_PCI_OP_aer_mmio:
601                         if (pdrv->err_handler->mmio_enabled)
602                                 result = pdrv->err_handler->mmio_enabled(pcidev);
603                         break;
604                 case XEN_PCI_OP_aer_slotreset:
605                         if (pdrv->err_handler->slot_reset)
606                                 result = pdrv->err_handler->slot_reset(pcidev);
607                         break;
608                 case XEN_PCI_OP_aer_resume:
609                         if (pdrv->err_handler->resume)
610                                 pdrv->err_handler->resume(pcidev);
611                         break;
612                 default:
613                         dev_err(&pdev->xdev->dev,
614                                 "bad request %x in aer recovery operation!\n",
615                                 cmd);
616                         break;
617                 }
618         }
619
620         return result;
621 }
622
623
624 void pcifront_do_aer(struct work_struct *data)
625 {
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;
630
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);
636
637         pdev->sh_info->aer_op.err = pcifront_common_process(cmd, pdev, state);
638
639         wmb();
640         clear_bit(_XEN_PCIB_active, (unsigned long*)&pdev->sh_info->flags);
641         notify_remote_via_evtchn(pdev->evtchn);
642
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();
647
648         schedule_pcifront_aer_op(pdev);
649
650 }
651
652 irqreturn_t pcifront_handler_aer(int irq, void *dev)
653 {
654         struct pcifront_device *pdev = dev;
655         schedule_pcifront_aer_op(pdev);
656         return IRQ_HANDLED;
657 }