Added patch headers.
[linux-flexiantxendom0-3.2.10.git] / drivers / pci / msi-xen.c
1 /*
2  * File:        msi.c
3  * Purpose:     PCI Message Signaled Interrupt (MSI)
4  *
5  * Copyright (C) 2003-2004 Intel
6  * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7  */
8
9 #include <linux/err.h>
10 #include <linux/mm.h>
11 #include <linux/irq.h>
12 #include <linux/interrupt.h>
13 #include <linux/init.h>
14 #include <linux/ioport.h>
15 #include <linux/pci.h>
16 #include <linux/proc_fs.h>
17 #include <linux/msi.h>
18 #include <linux/smp.h>
19 #include <linux/errno.h>
20 #include <linux/io.h>
21 #include <linux/slab.h>
22
23 #include <xen/evtchn.h>
24
25 #include "pci.h"
26 #include "msi.h"
27
28 static int pci_msi_enable = 1;
29
30 static LIST_HEAD(msi_dev_head);
31 DEFINE_SPINLOCK(msi_dev_lock);
32
33 struct msi_dev_list {
34         struct pci_dev *dev;
35         struct list_head list;
36         spinlock_t pirq_list_lock;
37         struct list_head pirq_list_head;
38         /* Store default pre-assigned irq */
39         unsigned int default_irq;
40 };
41
42 struct msi_pirq_entry {
43         struct list_head list;
44         int pirq;
45         int entry_nr;
46 };
47
48 /* Arch hooks */
49
50 #ifndef arch_msi_check_device
51 int arch_msi_check_device(struct pci_dev *dev, int nvec, int type)
52 {
53         return 0;
54 }
55 #endif
56
57 #ifndef arch_setup_msi_irqs
58 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
59 {
60         struct msi_desc *entry;
61         int ret;
62
63         /*
64          * If an architecture wants to support multiple MSI, it needs to
65          * override arch_setup_msi_irqs()
66          */
67         if (type == PCI_CAP_ID_MSI && nvec > 1)
68                 return 1;
69
70         list_for_each_entry(entry, &dev->msi_list, list) {
71                 ret = arch_setup_msi_irq(dev, entry);
72                 if (ret < 0)
73                         return ret;
74                 if (ret > 0)
75                         return -ENOSPC;
76         }
77
78         return 0;
79 }
80 #endif
81
82 #ifndef arch_teardown_msi_irqs
83 void arch_teardown_msi_irqs(struct pci_dev *dev)
84 {
85         struct msi_desc *entry;
86
87         list_for_each_entry(entry, &dev->msi_list, list) {
88                 int i, nvec;
89                 if (entry->irq == 0)
90                         continue;
91                 nvec = 1 << entry->msi_attrib.multiple;
92                 for (i = 0; i < nvec; i++)
93                         arch_teardown_msi_irq(entry->irq + i);
94         }
95 }
96 #endif
97
98 static void msi_set_enable(struct pci_dev *dev, int pos, int enable)
99 {
100         u16 control;
101
102         BUG_ON(!pos);
103
104         pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
105         control &= ~PCI_MSI_FLAGS_ENABLE;
106         if (enable)
107                 control |= PCI_MSI_FLAGS_ENABLE;
108         pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
109 }
110
111 static void msix_set_enable(struct pci_dev *dev, int enable)
112 {
113         int pos;
114         u16 control;
115
116         pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
117         if (pos) {
118                 pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
119                 control &= ~PCI_MSIX_FLAGS_ENABLE;
120                 if (enable)
121                         control |= PCI_MSIX_FLAGS_ENABLE;
122                 pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
123         }
124 }
125
126 static struct msi_dev_list *get_msi_dev_pirq_list(struct pci_dev *dev)
127 {
128         struct msi_dev_list *msi_dev_list, *ret = NULL;
129         unsigned long flags;
130
131         spin_lock_irqsave(&msi_dev_lock, flags);
132
133         list_for_each_entry(msi_dev_list, &msi_dev_head, list)
134                 if ( msi_dev_list->dev == dev )
135                         ret = msi_dev_list;
136
137         if ( ret ) {
138                 spin_unlock_irqrestore(&msi_dev_lock, flags);
139                 return ret;
140         }
141
142         /* Has not allocate msi_dev until now. */
143         ret = kzalloc(sizeof(struct msi_dev_list), GFP_ATOMIC);
144
145         /* Failed to allocate msi_dev structure */
146         if ( !ret ) {
147                 spin_unlock_irqrestore(&msi_dev_lock, flags);
148                 return NULL;
149         }
150
151         ret->dev = dev;
152         spin_lock_init(&ret->pirq_list_lock);
153         INIT_LIST_HEAD(&ret->pirq_list_head);
154         list_add_tail(&ret->list, &msi_dev_head);
155         spin_unlock_irqrestore(&msi_dev_lock, flags);
156         return ret;
157 }
158
159 static int attach_pirq_entry(int pirq, int entry_nr,
160                              struct msi_dev_list *msi_dev_entry)
161 {
162         struct msi_pirq_entry *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
163         unsigned long flags;
164
165         if (!entry)
166                 return -ENOMEM;
167         entry->pirq = pirq;
168         entry->entry_nr = entry_nr;
169         spin_lock_irqsave(&msi_dev_entry->pirq_list_lock, flags);
170         list_add_tail(&entry->list, &msi_dev_entry->pirq_list_head);
171         spin_unlock_irqrestore(&msi_dev_entry->pirq_list_lock, flags);
172         return 0;
173 }
174
175 static void detach_pirq_entry(int entry_nr,
176                                                         struct msi_dev_list *msi_dev_entry)
177 {
178         unsigned long flags;
179         struct msi_pirq_entry *pirq_entry;
180
181         list_for_each_entry(pirq_entry, &msi_dev_entry->pirq_list_head, list) {
182                 if (pirq_entry->entry_nr == entry_nr) {
183                         spin_lock_irqsave(&msi_dev_entry->pirq_list_lock, flags);
184                         list_del(&pirq_entry->list);
185                         spin_unlock_irqrestore(&msi_dev_entry->pirq_list_lock, flags);
186                         kfree(pirq_entry);
187                         return;
188                 }
189         }
190 }
191
192 /*
193  * pciback will provide device's owner
194  */
195 static int (*get_owner)(struct pci_dev *dev);
196
197 int register_msi_get_owner(int (*func)(struct pci_dev *dev))
198 {
199         if (get_owner) {
200                 printk(KERN_WARNING "register msi_get_owner again\n");
201                 return -EEXIST;
202         }
203         get_owner = func;
204         return 0;
205 }
206 EXPORT_SYMBOL(register_msi_get_owner);
207
208 int unregister_msi_get_owner(int (*func)(struct pci_dev *dev))
209 {
210         if (get_owner != func)
211                 return -EINVAL;
212         get_owner = NULL;
213         return 0;
214 }
215 EXPORT_SYMBOL(unregister_msi_get_owner);
216
217 static int msi_get_dev_owner(struct pci_dev *dev)
218 {
219         int owner;
220
221         BUG_ON(!is_initial_xendomain());
222         if (get_owner && (owner = get_owner(dev)) >= 0) {
223                 dev_info(&dev->dev, "get owner: %x \n", owner);
224                 return owner;
225         }
226
227         return DOMID_SELF;
228 }
229
230 static int msi_unmap_pirq(struct pci_dev *dev, int pirq)
231 {
232         struct physdev_unmap_pirq unmap;
233         int rc;
234
235         unmap.domid = msi_get_dev_owner(dev);
236         /* See comments in msi_map_vector, input parameter pirq means
237          * irq number only if the device belongs to dom0 itself.
238          */
239         unmap.pirq = (unmap.domid != DOMID_SELF)
240                 ? pirq : evtchn_get_xen_pirq(pirq);
241
242         if ((rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap)))
243                 dev_warn(&dev->dev, "unmap irq %x failed\n", pirq);
244
245         if (rc < 0)
246                 return rc;
247
248         if (unmap.domid == DOMID_SELF)
249                 evtchn_map_pirq(pirq, 0);
250
251         return 0;
252 }
253
254 static u64 find_table_base(struct pci_dev *dev, int pos)
255 {
256         u8 bar;
257         u32 reg;
258         unsigned long flags;
259
260         pci_read_config_dword(dev, msix_table_offset_reg(pos), &reg);
261         bar = reg & PCI_MSIX_FLAGS_BIRMASK;
262
263         flags = pci_resource_flags(dev, bar);
264         if (flags & (IORESOURCE_DISABLED | IORESOURCE_UNSET | IORESOURCE_BUSY))
265                 return 0;
266
267         return pci_resource_start(dev, bar);
268 }
269
270 /*
271  * Protected by msi_lock
272  */
273 static int msi_map_vector(struct pci_dev *dev, int entry_nr, u64 table_base)
274 {
275         struct physdev_map_pirq map_irq;
276         int rc;
277         domid_t domid = DOMID_SELF;
278
279         domid = msi_get_dev_owner(dev);
280
281         map_irq.domid = domid;
282         map_irq.type = MAP_PIRQ_TYPE_MSI;
283         map_irq.index = -1;
284         map_irq.pirq = -1;
285         map_irq.bus = dev->bus->number;
286         map_irq.devfn = dev->devfn;
287         map_irq.entry_nr = entry_nr;
288         map_irq.table_base = table_base;
289
290         if ((rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq)))
291                 dev_warn(&dev->dev, "map irq failed\n");
292
293         if (rc < 0)
294                 return rc;
295         /* This happens when MSI support is not enabled in older Xen. */
296         if (rc == 0 && map_irq.pirq < 0)
297                 return -ENOSYS;
298
299         BUG_ON(map_irq.pirq <= 0);
300
301         /* If mapping of this particular MSI is on behalf of another domain,
302          * we do not need to get an irq in dom0. This also implies:
303          * dev->irq in dom0 will be 'Xen pirq' if this device belongs to
304          * to another domain, and will be 'Linux irq' if it belongs to dom0.
305          */
306         if (domid == DOMID_SELF) {
307                 rc = evtchn_map_pirq(-1, map_irq.pirq);
308                 dev_printk(KERN_DEBUG, &dev->dev,
309                            "irq %d (%d) for MSI/MSI-X\n",
310                            rc, map_irq.pirq);
311                 return rc;
312         }
313         dev_printk(KERN_DEBUG, &dev->dev, "irq %d for dom%d MSI/MSI-X\n",
314                    map_irq.pirq, domid);
315         return map_irq.pirq;
316 }
317
318 static void pci_intx_for_msi(struct pci_dev *dev, int enable)
319 {
320         if (!(dev->dev_flags & PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG))
321                 pci_intx(dev, enable);
322 }
323
324 void pci_restore_msi_state(struct pci_dev *dev)
325 {
326         int rc;
327         struct physdev_restore_msi restore;
328
329         if (!dev->msi_enabled && !dev->msix_enabled)
330                 return;
331
332         pci_intx_for_msi(dev, 0);
333         if (dev->msi_enabled) {
334                 int pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
335
336                 msi_set_enable(dev, pos, 0);
337         }
338         if (dev->msix_enabled)
339                 msix_set_enable(dev, 0);
340
341         restore.bus = dev->bus->number;
342         restore.devfn = dev->devfn;
343         rc = HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi, &restore);
344         WARN(rc && rc != -ENOSYS, "restore_msi -> %d\n", rc);
345 }
346 EXPORT_SYMBOL_GPL(pci_restore_msi_state);
347
348 /**
349  * msi_capability_init - configure device's MSI capability structure
350  * @dev: pointer to the pci_dev data structure of MSI device function
351  * @nvec: number of interrupts to allocate
352  *
353  * Setup the MSI capability structure of the device with the requested
354  * number of interrupts.  A return value of zero indicates the successful
355  * setup of an entry with the new MSI irq.  A negative return value indicates
356  * an error, and a positive return value indicates the number of interrupts
357  * which could have been allocated.
358  */
359 static int msi_capability_init(struct pci_dev *dev, int nvec)
360 {
361         int pos, pirq;
362         u16 control;
363
364         pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
365         msi_set_enable(dev, pos, 0);    /* Disable MSI during set up */
366
367         pci_read_config_word(dev, msi_control_reg(pos), &control);
368
369         WARN_ON(nvec > 1); /* XXX */
370         pirq = msi_map_vector(dev, 0, 0);
371         if (pirq < 0)
372                 return -EBUSY;
373
374         /* Set MSI enabled bits  */
375         pci_intx_for_msi(dev, 0);
376         msi_set_enable(dev, pos, 1);
377         dev->msi_enabled = 1;
378
379         dev->irq = pirq;
380         return 0;
381 }
382
383 /**
384  * msix_capability_init - configure device's MSI-X capability
385  * @dev: pointer to the pci_dev data structure of MSI-X device function
386  * @entries: pointer to an array of struct msix_entry entries
387  * @nvec: number of @entries
388  *
389  * Setup the MSI-X capability structure of device function with a
390  * single MSI-X irq. A return of zero indicates the successful setup of
391  * requested MSI-X entries with allocated irqs or non-zero for otherwise.
392  **/
393 static int msix_capability_init(struct pci_dev *dev,
394                                 struct msix_entry *entries, int nvec)
395 {
396         u64 table_base;
397         int pirq, i, j, mapped, pos;
398         u16 control;
399         struct msi_dev_list *msi_dev_entry = get_msi_dev_pirq_list(dev);
400         struct msi_pirq_entry *pirq_entry;
401
402         if (!msi_dev_entry)
403                 return -ENOMEM;
404
405         msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
406
407         pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
408         pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
409
410         /* Ensure MSI-X is disabled while it is set up */
411         control &= ~PCI_MSIX_FLAGS_ENABLE;
412         pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
413
414         table_base = find_table_base(dev, pos);
415         if (!table_base)
416                 return -ENODEV;
417
418         /*
419          * Some devices require MSI-X to be enabled before we can touch the
420          * MSI-X registers.  We need to mask all the vectors to prevent
421          * interrupts coming in before they're fully set up.
422          */
423         control |= PCI_MSIX_FLAGS_MASKALL | PCI_MSIX_FLAGS_ENABLE;
424         pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
425
426         for (i = 0; i < nvec; i++) {
427                 mapped = 0;
428                 list_for_each_entry(pirq_entry, &msi_dev_entry->pirq_list_head, list) {
429                         if (pirq_entry->entry_nr == entries[i].entry) {
430                                 dev_warn(&dev->dev,
431                                          "msix entry %d was not freed\n",
432                                          entries[i].entry);
433                                 (entries + i)->vector = pirq_entry->pirq;
434                                 mapped = 1;
435                                 break;
436                         }
437                 }
438                 if (mapped)
439                         continue;
440                 pirq = msi_map_vector(dev, entries[i].entry, table_base);
441                 if (pirq < 0)
442                         break;
443                 attach_pirq_entry(pirq, entries[i].entry, msi_dev_entry);
444                 (entries + i)->vector = pirq;
445         }
446
447         if (i != nvec) {
448                 int avail = i - 1;
449                 for (j = --i; j >= 0; j--) {
450                         msi_unmap_pirq(dev, entries[j].vector);
451                         detach_pirq_entry(entries[j].entry, msi_dev_entry);
452                         entries[j].vector = 0;
453                 }
454                 /* If we had some success report the number of irqs
455                  * we succeeded in setting up.
456                  */
457                 if (avail <= 0)
458                         avail = -EBUSY;
459                 return avail;
460         }
461
462         /* Set MSI-X enabled bits and unmask the function */
463         pci_intx_for_msi(dev, 0);
464         dev->msix_enabled = 1;
465
466         control &= ~PCI_MSIX_FLAGS_MASKALL;
467         pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
468
469         return 0;
470 }
471
472 /**
473  * pci_msi_check_device - check whether MSI may be enabled on a device
474  * @dev: pointer to the pci_dev data structure of MSI device function
475  * @nvec: how many MSIs have been requested ?
476  * @type: are we checking for MSI or MSI-X ?
477  *
478  * Look at global flags, the device itself, and its parent busses
479  * to determine if MSI/-X are supported for the device. If MSI/-X is
480  * supported return 0, else return an error code.
481  **/
482 static int pci_msi_check_device(struct pci_dev *dev, int nvec, int type)
483 {
484         struct pci_bus *bus;
485         int ret;
486
487         /* MSI must be globally enabled and supported by the device */
488         if (!pci_msi_enable || !dev || dev->no_msi)
489                 return -EINVAL;
490
491         /*
492          * You can't ask to have 0 or less MSIs configured.
493          *  a) it's stupid ..
494          *  b) the list manipulation code assumes nvec >= 1.
495          */
496         if (nvec < 1)
497                 return -ERANGE;
498
499         /*
500          * Any bridge which does NOT route MSI transactions from its
501          * secondary bus to its primary bus must set NO_MSI flag on
502          * the secondary pci_bus.
503          * We expect only arch-specific PCI host bus controller driver
504          * or quirks for specific PCI bridges to be setting NO_MSI.
505          */
506         for (bus = dev->bus; bus; bus = bus->parent)
507                 if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
508                         return -EINVAL;
509
510         ret = arch_msi_check_device(dev, nvec, type);
511         if (ret)
512                 return ret;
513
514         if (!pci_find_capability(dev, type))
515                 return -EINVAL;
516
517         return 0;
518 }
519
520 /**
521  * pci_enable_msi_block - configure device's MSI capability structure
522  * @dev: device to configure
523  * @nvec: number of interrupts to configure
524  *
525  * Allocate IRQs for a device with the MSI capability.
526  * This function returns a negative errno if an error occurs.  If it
527  * is unable to allocate the number of interrupts requested, it returns
528  * the number of interrupts it might be able to allocate.  If it successfully
529  * allocates at least the number of interrupts requested, it returns 0 and
530  * updates the @dev's irq member to the lowest new interrupt number; the
531  * other interrupt numbers allocated to this device are consecutive.
532  */
533 extern int pci_frontend_enable_msi(struct pci_dev *dev);
534 int pci_enable_msi_block(struct pci_dev *dev, unsigned int nvec)
535 {
536         int temp, status, pos, maxvec;
537         u16 msgctl;
538         struct msi_dev_list *msi_dev_entry = get_msi_dev_pirq_list(dev);
539
540         pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
541         if (!pos)
542                 return -EINVAL;
543         pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &msgctl);
544         maxvec = 1 << ((msgctl & PCI_MSI_FLAGS_QMASK) >> 1);
545         if (nvec > maxvec)
546                 return maxvec;
547
548         status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSI);
549         if (status)
550                 return status;
551
552 #ifdef CONFIG_XEN_PCIDEV_FRONTEND
553         if (!is_initial_xendomain())
554         {
555                 int ret;
556
557                 temp = dev->irq;
558                 WARN_ON(nvec > 1); /* XXX */
559                 ret = pci_frontend_enable_msi(dev);
560                 if (ret)
561                         return ret;
562
563                 dev->irq = evtchn_map_pirq(-1, dev->irq);
564                 dev->msi_enabled = 1;
565                 msi_dev_entry->default_irq = temp;
566
567                 return ret;
568         }
569 #endif
570
571         temp = dev->irq;
572
573         /* Check whether driver already requested MSI-X irqs */
574         if (dev->msix_enabled) {
575                 dev_info(&dev->dev, "can't enable MSI "
576                          "(MSI-X already enabled)\n");
577                 return -EINVAL;
578         }
579
580         status = msi_capability_init(dev, nvec);
581         if ( !status )
582                 msi_dev_entry->default_irq = temp;
583
584         return status;
585 }
586 EXPORT_SYMBOL(pci_enable_msi_block);
587
588 extern void pci_frontend_disable_msi(struct pci_dev* dev);
589 void pci_msi_shutdown(struct pci_dev *dev)
590 {
591         int pirq, pos;
592         struct msi_dev_list *msi_dev_entry = get_msi_dev_pirq_list(dev);
593
594         if (!pci_msi_enable || !dev || !dev->msi_enabled)
595                 return;
596
597 #ifdef CONFIG_XEN_PCIDEV_FRONTEND
598         if (!is_initial_xendomain()) {
599                 evtchn_map_pirq(dev->irq, 0);
600                 pci_frontend_disable_msi(dev);
601                 dev->irq = msi_dev_entry->default_irq;
602                 dev->msi_enabled = 0;
603                 return;
604         }
605 #endif
606
607         pirq = dev->irq;
608         /* Restore dev->irq to its default pin-assertion vector */
609         dev->irq = msi_dev_entry->default_irq;
610         msi_unmap_pirq(dev, pirq);
611
612         /* Disable MSI mode */
613         pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
614         msi_set_enable(dev, pos, 0);
615         pci_intx_for_msi(dev, 1);
616         dev->msi_enabled = 0;
617 }
618
619 void pci_disable_msi(struct pci_dev *dev)
620 {
621         pci_msi_shutdown(dev);
622 }
623 EXPORT_SYMBOL(pci_disable_msi);
624
625 /**
626  * pci_msix_table_size - return the number of device's MSI-X table entries
627  * @dev: pointer to the pci_dev data structure of MSI-X device function
628  */
629 int pci_msix_table_size(struct pci_dev *dev)
630 {
631         int pos;
632         u16 control;
633
634         pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
635         if (!pos)
636                 return 0;
637
638         pci_read_config_word(dev, msi_control_reg(pos), &control);
639         return multi_msix_capable(control);
640 }
641
642 /**
643  * pci_enable_msix - configure device's MSI-X capability structure
644  * @dev: pointer to the pci_dev data structure of MSI-X device function
645  * @entries: pointer to an array of MSI-X entries
646  * @nvec: number of MSI-X irqs requested for allocation by device driver
647  *
648  * Setup the MSI-X capability structure of device function with the number
649  * of requested irqs upon its software driver call to request for
650  * MSI-X mode enabled on its hardware device function. A return of zero
651  * indicates the successful configuration of MSI-X capability structure
652  * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
653  * Or a return of > 0 indicates that driver request is exceeding the number
654  * of irqs or MSI-X vectors available. Driver should use the returned value to
655  * re-send its request.
656  **/
657 extern int pci_frontend_enable_msix(struct pci_dev *dev,
658                 struct msix_entry *entries, int nvec);
659 int pci_enable_msix(struct pci_dev *dev, struct msix_entry *entries, int nvec)
660 {
661         int status, nr_entries;
662         int i, j, temp;
663         struct msi_dev_list *msi_dev_entry = get_msi_dev_pirq_list(dev);
664
665         if (!entries)
666                 return -EINVAL;
667
668 #ifdef CONFIG_XEN_PCIDEV_FRONTEND
669         if (!is_initial_xendomain()) {
670                 struct msi_pirq_entry *pirq_entry;
671                 int ret, irq;
672
673                 temp = dev->irq;
674                 ret = pci_frontend_enable_msix(dev, entries, nvec);
675                 if (ret) {
676                         dev_warn(&dev->dev,
677                                  "got %x from frontend_enable_msix\n", ret);
678                         return ret;
679                 }
680                 dev->msix_enabled = 1;
681                 msi_dev_entry->default_irq = temp;
682
683                 for (i = 0; i < nvec; i++) {
684                         int mapped = 0;
685
686                         list_for_each_entry(pirq_entry, &msi_dev_entry->pirq_list_head, list) {
687                                 if (pirq_entry->entry_nr == entries[i].entry) {
688                                         irq = pirq_entry->pirq;
689                                         BUG_ON(entries[i].vector != evtchn_get_xen_pirq(irq));
690                                         entries[i].vector = irq;
691                                         mapped = 1;
692                                         break;
693                                 }
694                         }
695                         if (mapped)
696                                 continue;
697                         irq = evtchn_map_pirq(-1, entries[i].vector);
698                         attach_pirq_entry(irq, entries[i].entry, msi_dev_entry);
699                         entries[i].vector = irq;
700                 }
701         return 0;
702         }
703 #endif
704
705         status = pci_msi_check_device(dev, nvec, PCI_CAP_ID_MSIX);
706         if (status)
707                 return status;
708
709         nr_entries = pci_msix_table_size(dev);
710         if (nvec > nr_entries)
711                 return nr_entries;
712
713         /* Check for any invalid entries */
714         for (i = 0; i < nvec; i++) {
715                 if (entries[i].entry >= nr_entries)
716                         return -EINVAL;         /* invalid entry */
717                 for (j = i + 1; j < nvec; j++) {
718                         if (entries[i].entry == entries[j].entry)
719                                 return -EINVAL; /* duplicate entry */
720                 }
721         }
722
723         temp = dev->irq;
724         /* Check whether driver already requested for MSI vector */
725         if (dev->msi_enabled) {
726                 dev_info(&dev->dev, "can't enable MSI-X "
727                        "(MSI IRQ already assigned)\n");
728                 return -EINVAL;
729         }
730
731         status = msix_capability_init(dev, entries, nvec);
732
733         if ( !status )
734                 msi_dev_entry->default_irq = temp;
735
736         return status;
737 }
738 EXPORT_SYMBOL(pci_enable_msix);
739
740 extern void pci_frontend_disable_msix(struct pci_dev* dev);
741 void pci_msix_shutdown(struct pci_dev *dev)
742 {
743         if (!pci_msi_enable || !dev || !dev->msix_enabled)
744                 return;
745
746 #ifdef CONFIG_XEN_PCIDEV_FRONTEND
747         if (!is_initial_xendomain()) {
748                 struct msi_dev_list *msi_dev_entry;
749                 struct msi_pirq_entry *pirq_entry, *tmp;
750
751                 pci_frontend_disable_msix(dev);
752
753                 msi_dev_entry = get_msi_dev_pirq_list(dev);
754                 list_for_each_entry_safe(pirq_entry, tmp,
755                                          &msi_dev_entry->pirq_list_head, list) {
756                         evtchn_map_pirq(pirq_entry->pirq, 0);
757                         list_del(&pirq_entry->list);
758                         kfree(pirq_entry);
759                 }
760
761                 dev->irq = msi_dev_entry->default_irq;
762                 dev->msix_enabled = 0;
763                 return;
764         }
765 #endif
766
767         msi_remove_pci_irq_vectors(dev);
768
769         /* Disable MSI mode */
770         msix_set_enable(dev, 0);
771         pci_intx_for_msi(dev, 1);
772         dev->msix_enabled = 0;
773 }
774
775 void pci_disable_msix(struct pci_dev *dev)
776 {
777         pci_msix_shutdown(dev);
778 }
779 EXPORT_SYMBOL(pci_disable_msix);
780
781 /**
782  * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
783  * @dev: pointer to the pci_dev data structure of MSI(X) device function
784  *
785  * Being called during hotplug remove, from which the device function
786  * is hot-removed. All previous assigned MSI/MSI-X irqs, if
787  * allocated for this device function, are reclaimed to unused state,
788  * which may be used later on.
789  **/
790 void msi_remove_pci_irq_vectors(struct pci_dev *dev)
791 {
792         unsigned long flags;
793         struct msi_dev_list *msi_dev_entry;
794         struct msi_pirq_entry *pirq_entry, *tmp;
795
796         if (!pci_msi_enable || !dev)
797                 return;
798
799         msi_dev_entry = get_msi_dev_pirq_list(dev);
800
801         spin_lock_irqsave(&msi_dev_entry->pirq_list_lock, flags);
802         if (!list_empty(&msi_dev_entry->pirq_list_head))
803                 list_for_each_entry_safe(pirq_entry, tmp,
804                                          &msi_dev_entry->pirq_list_head, list) {
805                         msi_unmap_pirq(dev, pirq_entry->pirq);
806                         list_del(&pirq_entry->list);
807                         kfree(pirq_entry);
808                 }
809         spin_unlock_irqrestore(&msi_dev_entry->pirq_list_lock, flags);
810         dev->irq = msi_dev_entry->default_irq;
811 }
812
813 void pci_no_msi(void)
814 {
815         pci_msi_enable = 0;
816 }
817
818 /**
819  * pci_msi_enabled - is MSI enabled?
820  *
821  * Returns true if MSI has not been disabled by the command-line option
822  * pci=nomsi.
823  **/
824 int pci_msi_enabled(void)
825 {
826         return pci_msi_enable;
827 }
828 EXPORT_SYMBOL(pci_msi_enabled);
829
830 void pci_msi_init_pci_dev(struct pci_dev *dev)
831 {
832 #ifndef CONFIG_XEN
833         INIT_LIST_HEAD(&dev->msi_list);
834 #endif
835 }