aq_main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * aQuantia Corporation Network Driver
  3. * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. */
  9. /* File aq_main.c: Main file for aQuantia Linux driver. */
  10. #include "aq_main.h"
  11. #include "aq_nic.h"
  12. #include "aq_pci_func.h"
  13. #include "aq_ethtool.h"
  14. #include "hw_atl/hw_atl_a0.h"
  15. #include "hw_atl/hw_atl_b0.h"
  16. #include <linux/netdevice.h>
  17. #include <linux/module.h>
  18. static const struct pci_device_id aq_pci_tbl[] = {
  19. { PCI_VDEVICE(AQUANTIA, HW_ATL_DEVICE_ID_0001), },
  20. { PCI_VDEVICE(AQUANTIA, HW_ATL_DEVICE_ID_D100), },
  21. { PCI_VDEVICE(AQUANTIA, HW_ATL_DEVICE_ID_D107), },
  22. { PCI_VDEVICE(AQUANTIA, HW_ATL_DEVICE_ID_D108), },
  23. { PCI_VDEVICE(AQUANTIA, HW_ATL_DEVICE_ID_D109), },
  24. {}
  25. };
  26. MODULE_DEVICE_TABLE(pci, aq_pci_tbl);
  27. MODULE_LICENSE("GPL v2");
  28. MODULE_VERSION(AQ_CFG_DRV_VERSION);
  29. MODULE_AUTHOR(AQ_CFG_DRV_AUTHOR);
  30. MODULE_DESCRIPTION(AQ_CFG_DRV_DESC);
  31. static struct aq_hw_ops *aq_pci_probe_get_hw_ops_by_id(struct pci_dev *pdev)
  32. {
  33. struct aq_hw_ops *ops = NULL;
  34. ops = hw_atl_a0_get_ops_by_id(pdev);
  35. if (!ops)
  36. ops = hw_atl_b0_get_ops_by_id(pdev);
  37. return ops;
  38. }
  39. static int aq_ndev_open(struct net_device *ndev)
  40. {
  41. struct aq_nic_s *aq_nic = NULL;
  42. int err = 0;
  43. aq_nic = aq_nic_alloc_hot(ndev);
  44. if (!aq_nic) {
  45. err = -ENOMEM;
  46. goto err_exit;
  47. }
  48. err = aq_nic_init(aq_nic);
  49. if (err < 0)
  50. goto err_exit;
  51. err = aq_nic_start(aq_nic);
  52. if (err < 0)
  53. goto err_exit;
  54. err_exit:
  55. if (err < 0)
  56. aq_nic_deinit(aq_nic);
  57. return err;
  58. }
  59. static int aq_ndev_close(struct net_device *ndev)
  60. {
  61. int err = 0;
  62. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  63. err = aq_nic_stop(aq_nic);
  64. if (err < 0)
  65. goto err_exit;
  66. aq_nic_deinit(aq_nic);
  67. aq_nic_free_hot_resources(aq_nic);
  68. err_exit:
  69. return err;
  70. }
  71. static int aq_ndev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  72. {
  73. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  74. return aq_nic_xmit(aq_nic, skb);
  75. }
  76. static int aq_ndev_change_mtu(struct net_device *ndev, int new_mtu)
  77. {
  78. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  79. int err = aq_nic_set_mtu(aq_nic, new_mtu + ETH_HLEN);
  80. if (err < 0)
  81. goto err_exit;
  82. ndev->mtu = new_mtu;
  83. err_exit:
  84. return err;
  85. }
  86. static int aq_ndev_set_features(struct net_device *ndev,
  87. netdev_features_t features)
  88. {
  89. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  90. struct aq_nic_cfg_s *aq_cfg = aq_nic_get_cfg(aq_nic);
  91. bool is_lro = false;
  92. if (aq_cfg->hw_features & NETIF_F_LRO) {
  93. is_lro = features & NETIF_F_LRO;
  94. if (aq_cfg->is_lro != is_lro) {
  95. aq_cfg->is_lro = is_lro;
  96. if (netif_running(ndev)) {
  97. aq_ndev_close(ndev);
  98. aq_ndev_open(ndev);
  99. }
  100. }
  101. }
  102. return 0;
  103. }
  104. static int aq_ndev_set_mac_address(struct net_device *ndev, void *addr)
  105. {
  106. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  107. int err = 0;
  108. err = eth_mac_addr(ndev, addr);
  109. if (err < 0)
  110. goto err_exit;
  111. err = aq_nic_set_mac(aq_nic, ndev);
  112. if (err < 0)
  113. goto err_exit;
  114. err_exit:
  115. return err;
  116. }
  117. static void aq_ndev_set_multicast_settings(struct net_device *ndev)
  118. {
  119. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  120. int err = 0;
  121. err = aq_nic_set_packet_filter(aq_nic, ndev->flags);
  122. if (err < 0)
  123. goto err_exit;
  124. if (netdev_mc_count(ndev)) {
  125. err = aq_nic_set_multicast_list(aq_nic, ndev);
  126. if (err < 0)
  127. goto err_exit;
  128. }
  129. err_exit:;
  130. }
  131. static const struct net_device_ops aq_ndev_ops = {
  132. .ndo_open = aq_ndev_open,
  133. .ndo_stop = aq_ndev_close,
  134. .ndo_start_xmit = aq_ndev_start_xmit,
  135. .ndo_set_rx_mode = aq_ndev_set_multicast_settings,
  136. .ndo_change_mtu = aq_ndev_change_mtu,
  137. .ndo_set_mac_address = aq_ndev_set_mac_address,
  138. .ndo_set_features = aq_ndev_set_features
  139. };
  140. static int aq_pci_probe(struct pci_dev *pdev,
  141. const struct pci_device_id *pci_id)
  142. {
  143. struct aq_hw_ops *aq_hw_ops = NULL;
  144. struct aq_pci_func_s *aq_pci_func = NULL;
  145. int err = 0;
  146. err = pci_enable_device(pdev);
  147. if (err < 0)
  148. goto err_exit;
  149. aq_hw_ops = aq_pci_probe_get_hw_ops_by_id(pdev);
  150. aq_pci_func = aq_pci_func_alloc(aq_hw_ops, pdev,
  151. &aq_ndev_ops, &aq_ethtool_ops);
  152. if (!aq_pci_func) {
  153. err = -ENOMEM;
  154. goto err_exit;
  155. }
  156. err = aq_pci_func_init(aq_pci_func);
  157. if (err < 0)
  158. goto err_exit;
  159. err_exit:
  160. if (err < 0) {
  161. if (aq_pci_func)
  162. aq_pci_func_free(aq_pci_func);
  163. }
  164. return err;
  165. }
  166. static void aq_pci_remove(struct pci_dev *pdev)
  167. {
  168. struct aq_pci_func_s *aq_pci_func = pci_get_drvdata(pdev);
  169. aq_pci_func_deinit(aq_pci_func);
  170. aq_pci_func_free(aq_pci_func);
  171. }
  172. static int aq_pci_suspend(struct pci_dev *pdev, pm_message_t pm_msg)
  173. {
  174. struct aq_pci_func_s *aq_pci_func = pci_get_drvdata(pdev);
  175. return aq_pci_func_change_pm_state(aq_pci_func, &pm_msg);
  176. }
  177. static int aq_pci_resume(struct pci_dev *pdev)
  178. {
  179. struct aq_pci_func_s *aq_pci_func = pci_get_drvdata(pdev);
  180. pm_message_t pm_msg = PMSG_RESTORE;
  181. return aq_pci_func_change_pm_state(aq_pci_func, &pm_msg);
  182. }
  183. static struct pci_driver aq_pci_ops = {
  184. .name = AQ_CFG_DRV_NAME,
  185. .id_table = aq_pci_tbl,
  186. .probe = aq_pci_probe,
  187. .remove = aq_pci_remove,
  188. .suspend = aq_pci_suspend,
  189. .resume = aq_pci_resume,
  190. };
  191. module_pci_driver(aq_pci_ops);