msi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. #include <linux/err.h>
  9. #include <linux/mm.h>
  10. #include <linux/irq.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/init.h>
  13. #include <linux/ioport.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/pci.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/msi.h>
  18. #include <asm/errno.h>
  19. #include <asm/io.h>
  20. #include <asm/smp.h>
  21. #include "pci.h"
  22. #include "msi.h"
  23. static int pci_msi_enable = 1;
  24. static void msi_set_enable(struct pci_dev *dev, int enable)
  25. {
  26. int pos;
  27. u16 control;
  28. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  29. if (pos) {
  30. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  31. control &= ~PCI_MSI_FLAGS_ENABLE;
  32. if (enable)
  33. control |= PCI_MSI_FLAGS_ENABLE;
  34. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  35. }
  36. }
  37. static void msix_set_enable(struct pci_dev *dev, int enable)
  38. {
  39. int pos;
  40. u16 control;
  41. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  42. if (pos) {
  43. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  44. control &= ~PCI_MSIX_FLAGS_ENABLE;
  45. if (enable)
  46. control |= PCI_MSIX_FLAGS_ENABLE;
  47. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  48. }
  49. }
  50. static void msix_flush_writes(unsigned int irq)
  51. {
  52. struct msi_desc *entry;
  53. entry = get_irq_msi(irq);
  54. BUG_ON(!entry || !entry->dev);
  55. switch (entry->msi_attrib.type) {
  56. case PCI_CAP_ID_MSI:
  57. /* nothing to do */
  58. break;
  59. case PCI_CAP_ID_MSIX:
  60. {
  61. int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  62. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
  63. readl(entry->mask_base + offset);
  64. break;
  65. }
  66. default:
  67. BUG();
  68. break;
  69. }
  70. }
  71. static void msi_set_mask_bit(unsigned int irq, int flag)
  72. {
  73. struct msi_desc *entry;
  74. entry = get_irq_msi(irq);
  75. BUG_ON(!entry || !entry->dev);
  76. switch (entry->msi_attrib.type) {
  77. case PCI_CAP_ID_MSI:
  78. if (entry->msi_attrib.maskbit) {
  79. int pos;
  80. u32 mask_bits;
  81. pos = (long)entry->mask_base;
  82. pci_read_config_dword(entry->dev, pos, &mask_bits);
  83. mask_bits &= ~(1);
  84. mask_bits |= flag;
  85. pci_write_config_dword(entry->dev, pos, mask_bits);
  86. } else {
  87. msi_set_enable(entry->dev, !flag);
  88. }
  89. break;
  90. case PCI_CAP_ID_MSIX:
  91. {
  92. int offset = entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE +
  93. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET;
  94. writel(flag, entry->mask_base + offset);
  95. readl(entry->mask_base + offset);
  96. break;
  97. }
  98. default:
  99. BUG();
  100. break;
  101. }
  102. entry->msi_attrib.masked = !!flag;
  103. }
  104. void read_msi_msg(unsigned int irq, struct msi_msg *msg)
  105. {
  106. struct msi_desc *entry = get_irq_msi(irq);
  107. switch(entry->msi_attrib.type) {
  108. case PCI_CAP_ID_MSI:
  109. {
  110. struct pci_dev *dev = entry->dev;
  111. int pos = entry->msi_attrib.pos;
  112. u16 data;
  113. pci_read_config_dword(dev, msi_lower_address_reg(pos),
  114. &msg->address_lo);
  115. if (entry->msi_attrib.is_64) {
  116. pci_read_config_dword(dev, msi_upper_address_reg(pos),
  117. &msg->address_hi);
  118. pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
  119. } else {
  120. msg->address_hi = 0;
  121. pci_read_config_word(dev, msi_data_reg(pos, 1), &data);
  122. }
  123. msg->data = data;
  124. break;
  125. }
  126. case PCI_CAP_ID_MSIX:
  127. {
  128. void __iomem *base;
  129. base = entry->mask_base +
  130. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  131. msg->address_lo = readl(base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  132. msg->address_hi = readl(base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  133. msg->data = readl(base + PCI_MSIX_ENTRY_DATA_OFFSET);
  134. break;
  135. }
  136. default:
  137. BUG();
  138. }
  139. }
  140. void write_msi_msg(unsigned int irq, struct msi_msg *msg)
  141. {
  142. struct msi_desc *entry = get_irq_msi(irq);
  143. switch (entry->msi_attrib.type) {
  144. case PCI_CAP_ID_MSI:
  145. {
  146. struct pci_dev *dev = entry->dev;
  147. int pos = entry->msi_attrib.pos;
  148. pci_write_config_dword(dev, msi_lower_address_reg(pos),
  149. msg->address_lo);
  150. if (entry->msi_attrib.is_64) {
  151. pci_write_config_dword(dev, msi_upper_address_reg(pos),
  152. msg->address_hi);
  153. pci_write_config_word(dev, msi_data_reg(pos, 1),
  154. msg->data);
  155. } else {
  156. pci_write_config_word(dev, msi_data_reg(pos, 0),
  157. msg->data);
  158. }
  159. break;
  160. }
  161. case PCI_CAP_ID_MSIX:
  162. {
  163. void __iomem *base;
  164. base = entry->mask_base +
  165. entry->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE;
  166. writel(msg->address_lo,
  167. base + PCI_MSIX_ENTRY_LOWER_ADDR_OFFSET);
  168. writel(msg->address_hi,
  169. base + PCI_MSIX_ENTRY_UPPER_ADDR_OFFSET);
  170. writel(msg->data, base + PCI_MSIX_ENTRY_DATA_OFFSET);
  171. break;
  172. }
  173. default:
  174. BUG();
  175. }
  176. entry->msg = *msg;
  177. }
  178. void mask_msi_irq(unsigned int irq)
  179. {
  180. msi_set_mask_bit(irq, 1);
  181. msix_flush_writes(irq);
  182. }
  183. void unmask_msi_irq(unsigned int irq)
  184. {
  185. msi_set_mask_bit(irq, 0);
  186. msix_flush_writes(irq);
  187. }
  188. static int msi_free_irq(struct pci_dev* dev, int irq);
  189. static struct msi_desc* alloc_msi_entry(void)
  190. {
  191. struct msi_desc *entry;
  192. entry = kzalloc(sizeof(struct msi_desc), GFP_KERNEL);
  193. if (!entry)
  194. return NULL;
  195. entry->link.tail = entry->link.head = 0; /* single message */
  196. entry->dev = NULL;
  197. return entry;
  198. }
  199. #ifdef CONFIG_PM
  200. static void __pci_restore_msi_state(struct pci_dev *dev)
  201. {
  202. int pos;
  203. u16 control;
  204. struct msi_desc *entry;
  205. if (!dev->msi_enabled)
  206. return;
  207. entry = get_irq_msi(dev->irq);
  208. pos = entry->msi_attrib.pos;
  209. pci_intx(dev, 0); /* disable intx */
  210. msi_set_enable(dev, 0);
  211. write_msi_msg(dev->irq, &entry->msg);
  212. if (entry->msi_attrib.maskbit)
  213. msi_set_mask_bit(dev->irq, entry->msi_attrib.masked);
  214. pci_read_config_word(dev, pos + PCI_MSI_FLAGS, &control);
  215. control &= ~(PCI_MSI_FLAGS_QSIZE | PCI_MSI_FLAGS_ENABLE);
  216. if (entry->msi_attrib.maskbit || !entry->msi_attrib.masked)
  217. control |= PCI_MSI_FLAGS_ENABLE;
  218. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  219. }
  220. static void __pci_restore_msix_state(struct pci_dev *dev)
  221. {
  222. int pos;
  223. int irq, head, tail = 0;
  224. struct msi_desc *entry;
  225. u16 control;
  226. if (!dev->msix_enabled)
  227. return;
  228. /* route the table */
  229. pci_intx(dev, 0); /* disable intx */
  230. msix_set_enable(dev, 0);
  231. irq = head = dev->first_msi_irq;
  232. entry = get_irq_msi(irq);
  233. pos = entry->msi_attrib.pos;
  234. while (head != tail) {
  235. entry = get_irq_msi(irq);
  236. write_msi_msg(irq, &entry->msg);
  237. msi_set_mask_bit(irq, entry->msi_attrib.masked);
  238. tail = entry->link.tail;
  239. irq = tail;
  240. }
  241. pci_read_config_word(dev, pos + PCI_MSIX_FLAGS, &control);
  242. control &= ~PCI_MSIX_FLAGS_MASKALL;
  243. control |= PCI_MSIX_FLAGS_ENABLE;
  244. pci_write_config_word(dev, pos + PCI_MSIX_FLAGS, control);
  245. }
  246. void pci_restore_msi_state(struct pci_dev *dev)
  247. {
  248. __pci_restore_msi_state(dev);
  249. __pci_restore_msix_state(dev);
  250. }
  251. #endif /* CONFIG_PM */
  252. /**
  253. * msi_capability_init - configure device's MSI capability structure
  254. * @dev: pointer to the pci_dev data structure of MSI device function
  255. *
  256. * Setup the MSI capability structure of device function with a single
  257. * MSI irq, regardless of device function is capable of handling
  258. * multiple messages. A return of zero indicates the successful setup
  259. * of an entry zero with the new MSI irq or non-zero for otherwise.
  260. **/
  261. static int msi_capability_init(struct pci_dev *dev)
  262. {
  263. struct msi_desc *entry;
  264. int pos, irq;
  265. u16 control;
  266. msi_set_enable(dev, 0); /* Ensure msi is disabled as I set it up */
  267. pos = pci_find_capability(dev, PCI_CAP_ID_MSI);
  268. pci_read_config_word(dev, msi_control_reg(pos), &control);
  269. /* MSI Entry Initialization */
  270. entry = alloc_msi_entry();
  271. if (!entry)
  272. return -ENOMEM;
  273. entry->msi_attrib.type = PCI_CAP_ID_MSI;
  274. entry->msi_attrib.is_64 = is_64bit_address(control);
  275. entry->msi_attrib.entry_nr = 0;
  276. entry->msi_attrib.maskbit = is_mask_bit_support(control);
  277. entry->msi_attrib.masked = 1;
  278. entry->msi_attrib.default_irq = dev->irq; /* Save IOAPIC IRQ */
  279. entry->msi_attrib.pos = pos;
  280. if (is_mask_bit_support(control)) {
  281. entry->mask_base = (void __iomem *)(long)msi_mask_bits_reg(pos,
  282. is_64bit_address(control));
  283. }
  284. entry->dev = dev;
  285. if (entry->msi_attrib.maskbit) {
  286. unsigned int maskbits, temp;
  287. /* All MSIs are unmasked by default, Mask them all */
  288. pci_read_config_dword(dev,
  289. msi_mask_bits_reg(pos, is_64bit_address(control)),
  290. &maskbits);
  291. temp = (1 << multi_msi_capable(control));
  292. temp = ((temp - 1) & ~temp);
  293. maskbits |= temp;
  294. pci_write_config_dword(dev,
  295. msi_mask_bits_reg(pos, is_64bit_address(control)),
  296. maskbits);
  297. }
  298. /* Configure MSI capability structure */
  299. irq = arch_setup_msi_irq(dev, entry);
  300. if (irq < 0) {
  301. kfree(entry);
  302. return irq;
  303. }
  304. entry->link.head = irq;
  305. entry->link.tail = irq;
  306. dev->first_msi_irq = irq;
  307. set_irq_msi(irq, entry);
  308. /* Set MSI enabled bits */
  309. pci_intx(dev, 0); /* disable intx */
  310. msi_set_enable(dev, 1);
  311. dev->msi_enabled = 1;
  312. dev->irq = irq;
  313. return 0;
  314. }
  315. /**
  316. * msix_capability_init - configure device's MSI-X capability
  317. * @dev: pointer to the pci_dev data structure of MSI-X device function
  318. * @entries: pointer to an array of struct msix_entry entries
  319. * @nvec: number of @entries
  320. *
  321. * Setup the MSI-X capability structure of device function with a
  322. * single MSI-X irq. A return of zero indicates the successful setup of
  323. * requested MSI-X entries with allocated irqs or non-zero for otherwise.
  324. **/
  325. static int msix_capability_init(struct pci_dev *dev,
  326. struct msix_entry *entries, int nvec)
  327. {
  328. struct msi_desc *head = NULL, *tail = NULL, *entry = NULL;
  329. int irq, pos, i, j, nr_entries, temp = 0;
  330. unsigned long phys_addr;
  331. u32 table_offset;
  332. u16 control;
  333. u8 bir;
  334. void __iomem *base;
  335. msix_set_enable(dev, 0);/* Ensure msix is disabled as I set it up */
  336. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  337. /* Request & Map MSI-X table region */
  338. pci_read_config_word(dev, msi_control_reg(pos), &control);
  339. nr_entries = multi_msix_capable(control);
  340. pci_read_config_dword(dev, msix_table_offset_reg(pos), &table_offset);
  341. bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
  342. table_offset &= ~PCI_MSIX_FLAGS_BIRMASK;
  343. phys_addr = pci_resource_start (dev, bir) + table_offset;
  344. base = ioremap_nocache(phys_addr, nr_entries * PCI_MSIX_ENTRY_SIZE);
  345. if (base == NULL)
  346. return -ENOMEM;
  347. /* MSI-X Table Initialization */
  348. for (i = 0; i < nvec; i++) {
  349. entry = alloc_msi_entry();
  350. if (!entry)
  351. break;
  352. j = entries[i].entry;
  353. entry->msi_attrib.type = PCI_CAP_ID_MSIX;
  354. entry->msi_attrib.is_64 = 1;
  355. entry->msi_attrib.entry_nr = j;
  356. entry->msi_attrib.maskbit = 1;
  357. entry->msi_attrib.masked = 1;
  358. entry->msi_attrib.default_irq = dev->irq;
  359. entry->msi_attrib.pos = pos;
  360. entry->dev = dev;
  361. entry->mask_base = base;
  362. /* Configure MSI-X capability structure */
  363. irq = arch_setup_msi_irq(dev, entry);
  364. if (irq < 0) {
  365. kfree(entry);
  366. break;
  367. }
  368. entries[i].vector = irq;
  369. if (!head) {
  370. entry->link.head = irq;
  371. entry->link.tail = irq;
  372. head = entry;
  373. } else {
  374. entry->link.head = temp;
  375. entry->link.tail = tail->link.tail;
  376. tail->link.tail = irq;
  377. head->link.head = irq;
  378. }
  379. temp = irq;
  380. tail = entry;
  381. set_irq_msi(irq, entry);
  382. }
  383. if (i != nvec) {
  384. int avail = i - 1;
  385. i--;
  386. for (; i >= 0; i--) {
  387. irq = (entries + i)->vector;
  388. msi_free_irq(dev, irq);
  389. (entries + i)->vector = 0;
  390. }
  391. /* If we had some success report the number of irqs
  392. * we succeeded in setting up.
  393. */
  394. if (avail <= 0)
  395. avail = -EBUSY;
  396. return avail;
  397. }
  398. dev->first_msi_irq = entries[0].vector;
  399. /* Set MSI-X enabled bits */
  400. pci_intx(dev, 0); /* disable intx */
  401. msix_set_enable(dev, 1);
  402. dev->msix_enabled = 1;
  403. return 0;
  404. }
  405. /**
  406. * pci_msi_supported - check whether MSI may be enabled on device
  407. * @dev: pointer to the pci_dev data structure of MSI device function
  408. * @type: are we checking for MSI or MSI-X ?
  409. *
  410. * Look at global flags, the device itself, and its parent busses
  411. * to return 0 if MSI are supported for the device.
  412. **/
  413. static
  414. int pci_msi_supported(struct pci_dev * dev, int type)
  415. {
  416. struct pci_bus *bus;
  417. /* MSI must be globally enabled and supported by the device */
  418. if (!pci_msi_enable || !dev || dev->no_msi)
  419. return -EINVAL;
  420. /* Any bridge which does NOT route MSI transactions from it's
  421. * secondary bus to it's primary bus must set NO_MSI flag on
  422. * the secondary pci_bus.
  423. * We expect only arch-specific PCI host bus controller driver
  424. * or quirks for specific PCI bridges to be setting NO_MSI.
  425. */
  426. for (bus = dev->bus; bus; bus = bus->parent)
  427. if (bus->bus_flags & PCI_BUS_FLAGS_NO_MSI)
  428. return -EINVAL;
  429. if (!pci_find_capability(dev, type))
  430. return -EINVAL;
  431. return 0;
  432. }
  433. /**
  434. * pci_enable_msi - configure device's MSI capability structure
  435. * @dev: pointer to the pci_dev data structure of MSI device function
  436. *
  437. * Setup the MSI capability structure of device function with
  438. * a single MSI irq upon its software driver call to request for
  439. * MSI mode enabled on its hardware device function. A return of zero
  440. * indicates the successful setup of an entry zero with the new MSI
  441. * irq or non-zero for otherwise.
  442. **/
  443. int pci_enable_msi(struct pci_dev* dev)
  444. {
  445. int status;
  446. if (pci_msi_supported(dev, PCI_CAP_ID_MSI))
  447. return -EINVAL;
  448. WARN_ON(!!dev->msi_enabled);
  449. /* Check whether driver already requested for MSI-X irqs */
  450. if (dev->msix_enabled) {
  451. printk(KERN_INFO "PCI: %s: Can't enable MSI. "
  452. "Device already has MSI-X enabled\n",
  453. pci_name(dev));
  454. return -EINVAL;
  455. }
  456. status = msi_capability_init(dev);
  457. return status;
  458. }
  459. EXPORT_SYMBOL(pci_enable_msi);
  460. void pci_disable_msi(struct pci_dev* dev)
  461. {
  462. struct msi_desc *entry;
  463. int default_irq;
  464. if (!pci_msi_enable || !dev || !dev->msi_enabled)
  465. return;
  466. msi_set_enable(dev, 0);
  467. pci_intx(dev, 1); /* enable intx */
  468. dev->msi_enabled = 0;
  469. entry = get_irq_msi(dev->first_msi_irq);
  470. if (!entry || !entry->dev || entry->msi_attrib.type != PCI_CAP_ID_MSI) {
  471. return;
  472. }
  473. default_irq = entry->msi_attrib.default_irq;
  474. msi_free_irq(dev, dev->first_msi_irq);
  475. /* Restore dev->irq to its default pin-assertion irq */
  476. dev->irq = default_irq;
  477. dev->first_msi_irq = 0;
  478. }
  479. EXPORT_SYMBOL(pci_disable_msi);
  480. static int msi_free_irq(struct pci_dev* dev, int irq)
  481. {
  482. struct msi_desc *entry;
  483. int head, entry_nr, type;
  484. void __iomem *base;
  485. BUG_ON(irq_has_action(irq));
  486. entry = get_irq_msi(irq);
  487. if (!entry || entry->dev != dev) {
  488. return -EINVAL;
  489. }
  490. type = entry->msi_attrib.type;
  491. entry_nr = entry->msi_attrib.entry_nr;
  492. head = entry->link.head;
  493. base = entry->mask_base;
  494. get_irq_msi(entry->link.head)->link.tail = entry->link.tail;
  495. get_irq_msi(entry->link.tail)->link.head = entry->link.head;
  496. arch_teardown_msi_irq(irq);
  497. kfree(entry);
  498. if (type == PCI_CAP_ID_MSIX) {
  499. writel(1, base + entry_nr * PCI_MSIX_ENTRY_SIZE +
  500. PCI_MSIX_ENTRY_VECTOR_CTRL_OFFSET);
  501. if (head == irq)
  502. iounmap(base);
  503. }
  504. return 0;
  505. }
  506. /**
  507. * pci_enable_msix - configure device's MSI-X capability structure
  508. * @dev: pointer to the pci_dev data structure of MSI-X device function
  509. * @entries: pointer to an array of MSI-X entries
  510. * @nvec: number of MSI-X irqs requested for allocation by device driver
  511. *
  512. * Setup the MSI-X capability structure of device function with the number
  513. * of requested irqs upon its software driver call to request for
  514. * MSI-X mode enabled on its hardware device function. A return of zero
  515. * indicates the successful configuration of MSI-X capability structure
  516. * with new allocated MSI-X irqs. A return of < 0 indicates a failure.
  517. * Or a return of > 0 indicates that driver request is exceeding the number
  518. * of irqs available. Driver should use the returned value to re-send
  519. * its request.
  520. **/
  521. int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec)
  522. {
  523. int status, pos, nr_entries;
  524. int i, j;
  525. u16 control;
  526. if (!entries || pci_msi_supported(dev, PCI_CAP_ID_MSIX))
  527. return -EINVAL;
  528. pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
  529. pci_read_config_word(dev, msi_control_reg(pos), &control);
  530. nr_entries = multi_msix_capable(control);
  531. if (nvec > nr_entries)
  532. return -EINVAL;
  533. /* Check for any invalid entries */
  534. for (i = 0; i < nvec; i++) {
  535. if (entries[i].entry >= nr_entries)
  536. return -EINVAL; /* invalid entry */
  537. for (j = i + 1; j < nvec; j++) {
  538. if (entries[i].entry == entries[j].entry)
  539. return -EINVAL; /* duplicate entry */
  540. }
  541. }
  542. WARN_ON(!!dev->msix_enabled);
  543. /* Check whether driver already requested for MSI irq */
  544. if (dev->msi_enabled) {
  545. printk(KERN_INFO "PCI: %s: Can't enable MSI-X. "
  546. "Device already has an MSI irq assigned\n",
  547. pci_name(dev));
  548. return -EINVAL;
  549. }
  550. status = msix_capability_init(dev, entries, nvec);
  551. return status;
  552. }
  553. EXPORT_SYMBOL(pci_enable_msix);
  554. static void msix_free_all_irqs(struct pci_dev *dev)
  555. {
  556. int irq, head, tail = 0;
  557. irq = head = dev->first_msi_irq;
  558. while (head != tail) {
  559. tail = get_irq_msi(irq)->link.tail;
  560. if (irq != head)
  561. msi_free_irq(dev, irq);
  562. irq = tail;
  563. }
  564. msi_free_irq(dev, irq);
  565. dev->first_msi_irq = 0;
  566. }
  567. void pci_disable_msix(struct pci_dev* dev)
  568. {
  569. if (!pci_msi_enable || !dev || !dev->msix_enabled)
  570. return;
  571. msix_set_enable(dev, 0);
  572. pci_intx(dev, 1); /* enable intx */
  573. dev->msix_enabled = 0;
  574. msix_free_all_irqs(dev);
  575. }
  576. EXPORT_SYMBOL(pci_disable_msix);
  577. /**
  578. * msi_remove_pci_irq_vectors - reclaim MSI(X) irqs to unused state
  579. * @dev: pointer to the pci_dev data structure of MSI(X) device function
  580. *
  581. * Being called during hotplug remove, from which the device function
  582. * is hot-removed. All previous assigned MSI/MSI-X irqs, if
  583. * allocated for this device function, are reclaimed to unused state,
  584. * which may be used later on.
  585. **/
  586. void msi_remove_pci_irq_vectors(struct pci_dev* dev)
  587. {
  588. if (!pci_msi_enable || !dev)
  589. return;
  590. if (dev->msi_enabled)
  591. msi_free_irq(dev, dev->first_msi_irq);
  592. if (dev->msix_enabled)
  593. msix_free_all_irqs(dev);
  594. }
  595. void pci_no_msi(void)
  596. {
  597. pci_msi_enable = 0;
  598. }