igc_main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018 Intel Corporation */
  3. #include <linux/module.h>
  4. #include <linux/types.h>
  5. #include "igc.h"
  6. #include "igc_hw.h"
  7. #define DRV_VERSION "0.0.1-k"
  8. #define DRV_SUMMARY "Intel(R) 2.5G Ethernet Linux Driver"
  9. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  10. MODULE_DESCRIPTION(DRV_SUMMARY);
  11. MODULE_LICENSE("GPL v2");
  12. MODULE_VERSION(DRV_VERSION);
  13. char igc_driver_name[] = "igc";
  14. char igc_driver_version[] = DRV_VERSION;
  15. static const char igc_driver_string[] = DRV_SUMMARY;
  16. static const char igc_copyright[] =
  17. "Copyright(c) 2018 Intel Corporation.";
  18. static const struct pci_device_id igc_pci_tbl[] = {
  19. { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_LM) },
  20. { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_V) },
  21. /* required last entry */
  22. {0, }
  23. };
  24. MODULE_DEVICE_TABLE(pci, igc_pci_tbl);
  25. /* forward declaration */
  26. static int igc_sw_init(struct igc_adapter *);
  27. /* PCIe configuration access */
  28. void igc_read_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
  29. {
  30. struct igc_adapter *adapter = hw->back;
  31. pci_read_config_word(adapter->pdev, reg, value);
  32. }
  33. void igc_write_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
  34. {
  35. struct igc_adapter *adapter = hw->back;
  36. pci_write_config_word(adapter->pdev, reg, *value);
  37. }
  38. s32 igc_read_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
  39. {
  40. struct igc_adapter *adapter = hw->back;
  41. u16 cap_offset;
  42. cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
  43. if (!cap_offset)
  44. return -IGC_ERR_CONFIG;
  45. pci_read_config_word(adapter->pdev, cap_offset + reg, value);
  46. return IGC_SUCCESS;
  47. }
  48. s32 igc_write_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
  49. {
  50. struct igc_adapter *adapter = hw->back;
  51. u16 cap_offset;
  52. cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
  53. if (!cap_offset)
  54. return -IGC_ERR_CONFIG;
  55. pci_write_config_word(adapter->pdev, cap_offset + reg, *value);
  56. return IGC_SUCCESS;
  57. }
  58. u32 igc_rd32(struct igc_hw *hw, u32 reg)
  59. {
  60. u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
  61. u32 value = 0;
  62. if (IGC_REMOVED(hw_addr))
  63. return ~value;
  64. value = readl(&hw_addr[reg]);
  65. /* reads should not return all F's */
  66. if (!(~value) && (!reg || !(~readl(hw_addr))))
  67. hw->hw_addr = NULL;
  68. return value;
  69. }
  70. /**
  71. * igc_probe - Device Initialization Routine
  72. * @pdev: PCI device information struct
  73. * @ent: entry in igc_pci_tbl
  74. *
  75. * Returns 0 on success, negative on failure
  76. *
  77. * igc_probe initializes an adapter identified by a pci_dev structure.
  78. * The OS initialization, configuring the adapter private structure,
  79. * and a hardware reset occur.
  80. */
  81. static int igc_probe(struct pci_dev *pdev,
  82. const struct pci_device_id *ent)
  83. {
  84. struct igc_adapter *adapter;
  85. int err, pci_using_dac;
  86. err = pci_enable_device_mem(pdev);
  87. if (err)
  88. return err;
  89. pci_using_dac = 0;
  90. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  91. if (!err) {
  92. err = dma_set_coherent_mask(&pdev->dev,
  93. DMA_BIT_MASK(64));
  94. if (!err)
  95. pci_using_dac = 1;
  96. } else {
  97. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  98. if (err) {
  99. err = dma_set_coherent_mask(&pdev->dev,
  100. DMA_BIT_MASK(32));
  101. if (err) {
  102. IGC_ERR("Wrong DMA configuration, aborting\n");
  103. goto err_dma;
  104. }
  105. }
  106. }
  107. err = pci_request_selected_regions(pdev,
  108. pci_select_bars(pdev,
  109. IORESOURCE_MEM),
  110. igc_driver_name);
  111. if (err)
  112. goto err_pci_reg;
  113. pci_set_master(pdev);
  114. err = pci_save_state(pdev);
  115. /* setup the private structure */
  116. err = igc_sw_init(adapter);
  117. if (err)
  118. goto err_sw_init;
  119. return 0;
  120. err_sw_init:
  121. err_pci_reg:
  122. err_dma:
  123. pci_disable_device(pdev);
  124. return err;
  125. }
  126. /**
  127. * igc_remove - Device Removal Routine
  128. * @pdev: PCI device information struct
  129. *
  130. * igc_remove is called by the PCI subsystem to alert the driver
  131. * that it should release a PCI device. This could be caused by a
  132. * Hot-Plug event, or because the driver is going to be removed from
  133. * memory.
  134. */
  135. static void igc_remove(struct pci_dev *pdev)
  136. {
  137. pci_release_selected_regions(pdev,
  138. pci_select_bars(pdev, IORESOURCE_MEM));
  139. pci_disable_device(pdev);
  140. }
  141. static struct pci_driver igc_driver = {
  142. .name = igc_driver_name,
  143. .id_table = igc_pci_tbl,
  144. .probe = igc_probe,
  145. .remove = igc_remove,
  146. };
  147. /**
  148. * igc_sw_init - Initialize general software structures (struct igc_adapter)
  149. * @adapter: board private structure to initialize
  150. *
  151. * igc_sw_init initializes the Adapter private data structure.
  152. * Fields are initialized based on PCI device information and
  153. * OS network device settings (MTU size).
  154. */
  155. static int igc_sw_init(struct igc_adapter *adapter)
  156. {
  157. struct pci_dev *pdev = adapter->pdev;
  158. struct igc_hw *hw = &adapter->hw;
  159. /* PCI config space info */
  160. hw->vendor_id = pdev->vendor;
  161. hw->device_id = pdev->device;
  162. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  163. hw->subsystem_device_id = pdev->subsystem_device;
  164. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  165. pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
  166. return 0;
  167. }
  168. /**
  169. * igc_init_module - Driver Registration Routine
  170. *
  171. * igc_init_module is the first routine called when the driver is
  172. * loaded. All it does is register with the PCI subsystem.
  173. */
  174. static int __init igc_init_module(void)
  175. {
  176. int ret;
  177. pr_info("%s - version %s\n",
  178. igc_driver_string, igc_driver_version);
  179. pr_info("%s\n", igc_copyright);
  180. ret = pci_register_driver(&igc_driver);
  181. return ret;
  182. }
  183. module_init(igc_init_module);
  184. /**
  185. * igc_exit_module - Driver Exit Cleanup Routine
  186. *
  187. * igc_exit_module is called just before the driver is removed
  188. * from memory.
  189. */
  190. static void __exit igc_exit_module(void)
  191. {
  192. pci_unregister_driver(&igc_driver);
  193. }
  194. module_exit(igc_exit_module);
  195. /* igc_main.c */