portdrv_pci.c 5.0 KB

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