aq_main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <linux/netdevice.h>
  15. #include <linux/module.h>
  16. MODULE_LICENSE("GPL v2");
  17. MODULE_VERSION(AQ_CFG_DRV_VERSION);
  18. MODULE_AUTHOR(AQ_CFG_DRV_AUTHOR);
  19. MODULE_DESCRIPTION(AQ_CFG_DRV_DESC);
  20. static const struct net_device_ops aq_ndev_ops;
  21. struct net_device *aq_ndev_alloc(void)
  22. {
  23. struct net_device *ndev = NULL;
  24. struct aq_nic_s *aq_nic = NULL;
  25. ndev = alloc_etherdev_mq(sizeof(struct aq_nic_s), AQ_CFG_VECS_MAX);
  26. if (!ndev)
  27. return NULL;
  28. aq_nic = netdev_priv(ndev);
  29. aq_nic->ndev = ndev;
  30. ndev->netdev_ops = &aq_ndev_ops;
  31. ndev->ethtool_ops = &aq_ethtool_ops;
  32. return ndev;
  33. }
  34. static int aq_ndev_open(struct net_device *ndev)
  35. {
  36. int err = 0;
  37. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  38. err = aq_nic_init(aq_nic);
  39. if (err < 0)
  40. goto err_exit;
  41. err = aq_nic_start(aq_nic);
  42. if (err < 0) {
  43. aq_nic_stop(aq_nic);
  44. goto err_exit;
  45. }
  46. err_exit:
  47. if (err < 0)
  48. aq_nic_deinit(aq_nic);
  49. return err;
  50. }
  51. static int aq_ndev_close(struct net_device *ndev)
  52. {
  53. int err = 0;
  54. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  55. err = aq_nic_stop(aq_nic);
  56. if (err < 0)
  57. goto err_exit;
  58. aq_nic_deinit(aq_nic);
  59. err_exit:
  60. return err;
  61. }
  62. static int aq_ndev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  63. {
  64. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  65. return aq_nic_xmit(aq_nic, skb);
  66. }
  67. static int aq_ndev_change_mtu(struct net_device *ndev, int new_mtu)
  68. {
  69. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  70. int err = aq_nic_set_mtu(aq_nic, new_mtu + ETH_HLEN);
  71. if (err < 0)
  72. goto err_exit;
  73. ndev->mtu = new_mtu;
  74. err_exit:
  75. return err;
  76. }
  77. static int aq_ndev_set_features(struct net_device *ndev,
  78. netdev_features_t features)
  79. {
  80. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  81. struct aq_nic_cfg_s *aq_cfg = aq_nic_get_cfg(aq_nic);
  82. bool is_lro = false;
  83. if (aq_cfg->hw_features & NETIF_F_LRO) {
  84. is_lro = features & NETIF_F_LRO;
  85. if (aq_cfg->is_lro != is_lro) {
  86. aq_cfg->is_lro = is_lro;
  87. if (netif_running(ndev)) {
  88. aq_ndev_close(ndev);
  89. aq_ndev_open(ndev);
  90. }
  91. }
  92. }
  93. return 0;
  94. }
  95. static int aq_ndev_set_mac_address(struct net_device *ndev, void *addr)
  96. {
  97. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  98. int err = 0;
  99. err = eth_mac_addr(ndev, addr);
  100. if (err < 0)
  101. goto err_exit;
  102. err = aq_nic_set_mac(aq_nic, ndev);
  103. if (err < 0)
  104. goto err_exit;
  105. err_exit:
  106. return err;
  107. }
  108. static void aq_ndev_set_multicast_settings(struct net_device *ndev)
  109. {
  110. struct aq_nic_s *aq_nic = netdev_priv(ndev);
  111. aq_nic_set_packet_filter(aq_nic, ndev->flags);
  112. aq_nic_set_multicast_list(aq_nic, ndev);
  113. }
  114. static const struct net_device_ops aq_ndev_ops = {
  115. .ndo_open = aq_ndev_open,
  116. .ndo_stop = aq_ndev_close,
  117. .ndo_start_xmit = aq_ndev_start_xmit,
  118. .ndo_set_rx_mode = aq_ndev_set_multicast_settings,
  119. .ndo_change_mtu = aq_ndev_change_mtu,
  120. .ndo_set_mac_address = aq_ndev_set_mac_address,
  121. .ndo_set_features = aq_ndev_set_features
  122. };