portdrv_pci.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * File: portdrv_pci.c
  3. * Purpose: PCI Express Port Bus Driver
  4. *
  5. * Copyright (C) 2004 Intel
  6. * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
  7. */
  8. #include <linux/module.h>
  9. #include <linux/pci.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/pm.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/pcieport_if.h>
  16. #include "portdrv.h"
  17. /*
  18. * Version Information
  19. */
  20. #define DRIVER_VERSION "v1.0"
  21. #define DRIVER_AUTHOR "tom.l.nguyen@intel.com"
  22. #define DRIVER_DESC "PCIE Port Bus Driver"
  23. MODULE_AUTHOR(DRIVER_AUTHOR);
  24. MODULE_DESCRIPTION(DRIVER_DESC);
  25. MODULE_LICENSE("GPL");
  26. /* global data */
  27. static const char device_name[] = "pcieport-driver";
  28. static void pci_save_msi_state(struct pci_dev *dev)
  29. {
  30. struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
  31. int i = 0, pos;
  32. u16 control;
  33. if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSI)) <= 0)
  34. return;
  35. pci_read_config_dword(dev, pos, &p_ext->saved_msi_config_space[i++]);
  36. control = p_ext->saved_msi_config_space[0] >> 16;
  37. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
  38. &p_ext->saved_msi_config_space[i++]);
  39. if (control & PCI_MSI_FLAGS_64BIT) {
  40. pci_read_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
  41. &p_ext->saved_msi_config_space[i++]);
  42. pci_read_config_dword(dev, pos + PCI_MSI_DATA_64,
  43. &p_ext->saved_msi_config_space[i++]);
  44. } else
  45. pci_read_config_dword(dev, pos + PCI_MSI_DATA_32,
  46. &p_ext->saved_msi_config_space[i++]);
  47. if (control & PCI_MSI_FLAGS_MASKBIT)
  48. pci_read_config_dword(dev, pos + PCI_MSI_MASK_BIT,
  49. &p_ext->saved_msi_config_space[i++]);
  50. }
  51. static void pci_restore_msi_state(struct pci_dev *dev)
  52. {
  53. struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
  54. int i = 0, pos;
  55. u16 control;
  56. if ((pos = pci_find_capability(dev, PCI_CAP_ID_MSI)) <= 0)
  57. return;
  58. control = p_ext->saved_msi_config_space[i++] >> 16;
  59. pci_write_config_word(dev, pos + PCI_MSI_FLAGS, control);
  60. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_LO,
  61. p_ext->saved_msi_config_space[i++]);
  62. if (control & PCI_MSI_FLAGS_64BIT) {
  63. pci_write_config_dword(dev, pos + PCI_MSI_ADDRESS_HI,
  64. p_ext->saved_msi_config_space[i++]);
  65. pci_write_config_dword(dev, pos + PCI_MSI_DATA_64,
  66. p_ext->saved_msi_config_space[i++]);
  67. } else
  68. pci_write_config_dword(dev, pos + PCI_MSI_DATA_32,
  69. p_ext->saved_msi_config_space[i++]);
  70. if (control & PCI_MSI_FLAGS_MASKBIT)
  71. pci_write_config_dword(dev, pos + PCI_MSI_MASK_BIT,
  72. p_ext->saved_msi_config_space[i++]);
  73. }
  74. static void pcie_portdrv_save_config(struct pci_dev *dev)
  75. {
  76. struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
  77. pci_save_state(dev);
  78. if (p_ext->interrupt_mode == PCIE_PORT_MSI_MODE)
  79. pci_save_msi_state(dev);
  80. }
  81. static int pcie_portdrv_restore_config(struct pci_dev *dev)
  82. {
  83. struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
  84. int retval;
  85. pci_restore_state(dev);
  86. if (p_ext->interrupt_mode == PCIE_PORT_MSI_MODE)
  87. pci_restore_msi_state(dev);
  88. retval = pci_enable_device(dev);
  89. if (retval)
  90. return retval;
  91. pci_set_master(dev);
  92. return 0;
  93. }
  94. /*
  95. * pcie_portdrv_probe - Probe PCI-Express port devices
  96. * @dev: PCI-Express port device being probed
  97. *
  98. * If detected invokes the pcie_port_device_register() method for
  99. * this port device.
  100. *
  101. */
  102. static int __devinit pcie_portdrv_probe (struct pci_dev *dev,
  103. const struct pci_device_id *id )
  104. {
  105. int status;
  106. status = pcie_port_device_probe(dev);
  107. if (status)
  108. return status;
  109. if (pci_enable_device(dev) < 0)
  110. return -ENODEV;
  111. pci_set_master(dev);
  112. if (!dev->irq) {
  113. printk(KERN_WARNING
  114. "%s->Dev[%04x:%04x] has invalid IRQ. Check vendor BIOS\n",
  115. __FUNCTION__, dev->device, dev->vendor);
  116. }
  117. if (pcie_port_device_register(dev))
  118. return -ENOMEM;
  119. return 0;
  120. }
  121. static void pcie_portdrv_remove (struct pci_dev *dev)
  122. {
  123. pcie_port_device_remove(dev);
  124. kfree(pci_get_drvdata(dev));
  125. }
  126. #ifdef CONFIG_PM
  127. static int pcie_portdrv_suspend (struct pci_dev *dev, pm_message_t state)
  128. {
  129. int ret = pcie_port_device_suspend(dev, state);
  130. pcie_portdrv_save_config(dev);
  131. return ret;
  132. }
  133. static int pcie_portdrv_resume (struct pci_dev *dev)
  134. {
  135. pcie_portdrv_restore_config(dev);
  136. return pcie_port_device_resume(dev);
  137. }
  138. #endif
  139. /*
  140. * LINUX Device Driver Model
  141. */
  142. static const struct pci_device_id port_pci_ids[] = { {
  143. /* handle any PCI-Express port */
  144. PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0),
  145. }, { /* end: all zeroes */ }
  146. };
  147. MODULE_DEVICE_TABLE(pci, port_pci_ids);
  148. static struct pci_driver pcie_portdrv = {
  149. .name = (char *)device_name,
  150. .id_table = &port_pci_ids[0],
  151. .probe = pcie_portdrv_probe,
  152. .remove = pcie_portdrv_remove,
  153. #ifdef CONFIG_PM
  154. .suspend = pcie_portdrv_suspend,
  155. .resume = pcie_portdrv_resume,
  156. #endif /* PM */
  157. };
  158. static int __init pcie_portdrv_init(void)
  159. {
  160. int retval = 0;
  161. pcie_port_bus_register();
  162. retval = pci_register_driver(&pcie_portdrv);
  163. if (retval)
  164. pcie_port_bus_unregister();
  165. return retval;
  166. }
  167. static void __exit pcie_portdrv_exit(void)
  168. {
  169. pci_unregister_driver(&pcie_portdrv);
  170. pcie_port_bus_unregister();
  171. }
  172. module_init(pcie_portdrv_init);
  173. module_exit(pcie_portdrv_exit);