portdrv_pci.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 void pcie_portdrv_restore_config(struct pci_dev *dev)
  81. {
  82. struct pcie_port_device_ext *p_ext = pci_get_drvdata(dev);
  83. pci_restore_state(dev);
  84. if (p_ext->interrupt_mode == PCIE_PORT_MSI_MODE)
  85. pci_restore_msi_state(dev);
  86. pci_enable_device(dev);
  87. pci_set_master(dev);
  88. }
  89. /*
  90. * pcie_portdrv_probe - Probe PCI-Express port devices
  91. * @dev: PCI-Express port device being probed
  92. *
  93. * If detected invokes the pcie_port_device_register() method for
  94. * this port device.
  95. *
  96. */
  97. static int __devinit pcie_portdrv_probe (struct pci_dev *dev,
  98. const struct pci_device_id *id )
  99. {
  100. int status;
  101. status = pcie_port_device_probe(dev);
  102. if (status)
  103. return status;
  104. if (pci_enable_device(dev) < 0)
  105. return -ENODEV;
  106. pci_set_master(dev);
  107. if (!dev->irq) {
  108. printk(KERN_WARNING
  109. "%s->Dev[%04x:%04x] has invalid IRQ. Check vendor BIOS\n",
  110. __FUNCTION__, dev->device, dev->vendor);
  111. }
  112. if (pcie_port_device_register(dev))
  113. return -ENOMEM;
  114. return 0;
  115. }
  116. static void pcie_portdrv_remove (struct pci_dev *dev)
  117. {
  118. pcie_port_device_remove(dev);
  119. kfree(pci_get_drvdata(dev));
  120. }
  121. #ifdef CONFIG_PM
  122. static int pcie_portdrv_suspend (struct pci_dev *dev, pm_message_t state)
  123. {
  124. int ret = pcie_port_device_suspend(dev, state);
  125. pcie_portdrv_save_config(dev);
  126. return ret;
  127. }
  128. static int pcie_portdrv_resume (struct pci_dev *dev)
  129. {
  130. pcie_portdrv_restore_config(dev);
  131. return pcie_port_device_resume(dev);
  132. }
  133. #endif
  134. /*
  135. * LINUX Device Driver Model
  136. */
  137. static const struct pci_device_id port_pci_ids[] = { {
  138. /* handle any PCI-Express port */
  139. PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0),
  140. }, { /* end: all zeroes */ }
  141. };
  142. MODULE_DEVICE_TABLE(pci, port_pci_ids);
  143. static struct pci_driver pcie_portdrv = {
  144. .name = (char *)device_name,
  145. .id_table = &port_pci_ids[0],
  146. .probe = pcie_portdrv_probe,
  147. .remove = pcie_portdrv_remove,
  148. #ifdef CONFIG_PM
  149. .suspend = pcie_portdrv_suspend,
  150. .resume = pcie_portdrv_resume,
  151. #endif /* PM */
  152. };
  153. static int __init pcie_portdrv_init(void)
  154. {
  155. int retval = 0;
  156. pcie_port_bus_register();
  157. retval = pci_register_driver(&pcie_portdrv);
  158. if (retval)
  159. pcie_port_bus_unregister();
  160. return retval;
  161. }
  162. static void __exit pcie_portdrv_exit(void)
  163. {
  164. pci_unregister_driver(&pcie_portdrv);
  165. pcie_port_bus_unregister();
  166. }
  167. module_init(pcie_portdrv_init);
  168. module_exit(pcie_portdrv_exit);