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