1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * aQuantia Corporation Network Driver 4 * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved 5 */ 6 7 /* File aq_main.c: Main file for aQuantia Linux driver. */ 8 9 #include "aq_main.h" 10 #include "aq_nic.h" 11 #include "aq_pci_func.h" 12 #include "aq_ethtool.h" 13 #include "aq_filters.h" 14 15 #include <linux/netdevice.h> 16 #include <linux/module.h> 17 18 MODULE_LICENSE("GPL v2"); 19 MODULE_VERSION(AQ_CFG_DRV_VERSION); 20 MODULE_AUTHOR(AQ_CFG_DRV_AUTHOR); 21 MODULE_DESCRIPTION(AQ_CFG_DRV_DESC); 22 23 static const char aq_ndev_driver_name[] = AQ_CFG_DRV_NAME; 24 25 static const struct net_device_ops aq_ndev_ops; 26 27 static struct workqueue_struct *aq_ndev_wq; 28 29 void aq_ndev_schedule_work(struct work_struct *work) 30 { 31 queue_work(aq_ndev_wq, work); 32 } 33 34 struct net_device *aq_ndev_alloc(void) 35 { 36 struct net_device *ndev = NULL; 37 struct aq_nic_s *aq_nic = NULL; 38 39 ndev = alloc_etherdev_mq(sizeof(struct aq_nic_s), AQ_CFG_VECS_MAX); 40 if (!ndev) 41 return NULL; 42 43 aq_nic = netdev_priv(ndev); 44 aq_nic->ndev = ndev; 45 ndev->netdev_ops = &aq_ndev_ops; 46 ndev->ethtool_ops = &aq_ethtool_ops; 47 48 return ndev; 49 } 50 51 static int aq_ndev_open(struct net_device *ndev) 52 { 53 int err = 0; 54 struct aq_nic_s *aq_nic = netdev_priv(ndev); 55 56 err = aq_nic_init(aq_nic); 57 if (err < 0) 58 goto err_exit; 59 60 err = aq_reapply_rxnfc_all_rules(aq_nic); 61 if (err < 0) 62 goto err_exit; 63 64 err = aq_nic_start(aq_nic); 65 if (err < 0) 66 goto err_exit; 67 68 err_exit: 69 if (err < 0) 70 aq_nic_deinit(aq_nic); 71 return err; 72 } 73 74 static int aq_ndev_close(struct net_device *ndev) 75 { 76 int err = 0; 77 struct aq_nic_s *aq_nic = netdev_priv(ndev); 78 79 err = aq_nic_stop(aq_nic); 80 if (err < 0) 81 goto err_exit; 82 aq_nic_deinit(aq_nic); 83 84 err_exit: 85 return err; 86 } 87 88 static int aq_ndev_start_xmit(struct sk_buff *skb, struct net_device *ndev) 89 { 90 struct aq_nic_s *aq_nic = netdev_priv(ndev); 91 92 return aq_nic_xmit(aq_nic, skb); 93 } 94 95 static int aq_ndev_change_mtu(struct net_device *ndev, int new_mtu) 96 { 97 struct aq_nic_s *aq_nic = netdev_priv(ndev); 98 int err = aq_nic_set_mtu(aq_nic, new_mtu + ETH_HLEN); 99 100 if (err < 0) 101 goto err_exit; 102 ndev->mtu = new_mtu; 103 104 err_exit: 105 return err; 106 } 107 108 static int aq_ndev_set_features(struct net_device *ndev, 109 netdev_features_t features) 110 { 111 struct aq_nic_s *aq_nic = netdev_priv(ndev); 112 struct aq_nic_cfg_s *aq_cfg = aq_nic_get_cfg(aq_nic); 113 bool is_lro = false; 114 int err = 0; 115 116 if (!(features & NETIF_F_NTUPLE)) { 117 if (aq_nic->ndev->features & NETIF_F_NTUPLE) { 118 err = aq_clear_rxnfc_all_rules(aq_nic); 119 if (unlikely(err)) 120 goto err_exit; 121 } 122 } 123 if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER)) { 124 if (aq_nic->ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) { 125 err = aq_filters_vlan_offload_off(aq_nic); 126 if (unlikely(err)) 127 goto err_exit; 128 } 129 } 130 131 aq_cfg->features = features; 132 133 if (aq_cfg->aq_hw_caps->hw_features & NETIF_F_LRO) { 134 is_lro = features & NETIF_F_LRO; 135 136 if (aq_cfg->is_lro != is_lro) { 137 aq_cfg->is_lro = is_lro; 138 139 if (netif_running(ndev)) { 140 aq_ndev_close(ndev); 141 aq_ndev_open(ndev); 142 } 143 } 144 } 145 if ((aq_nic->ndev->features ^ features) & NETIF_F_RXCSUM) 146 err = aq_nic->aq_hw_ops->hw_set_offload(aq_nic->aq_hw, 147 aq_cfg); 148 149 err_exit: 150 return err; 151 } 152 153 static int aq_ndev_set_mac_address(struct net_device *ndev, void *addr) 154 { 155 struct aq_nic_s *aq_nic = netdev_priv(ndev); 156 int err = 0; 157 158 err = eth_mac_addr(ndev, addr); 159 if (err < 0) 160 goto err_exit; 161 err = aq_nic_set_mac(aq_nic, ndev); 162 if (err < 0) 163 goto err_exit; 164 165 err_exit: 166 return err; 167 } 168 169 static void aq_ndev_set_multicast_settings(struct net_device *ndev) 170 { 171 struct aq_nic_s *aq_nic = netdev_priv(ndev); 172 173 aq_nic_set_packet_filter(aq_nic, ndev->flags); 174 175 aq_nic_set_multicast_list(aq_nic, ndev); 176 } 177 178 static int aq_ndo_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, 179 u16 vid) 180 { 181 struct aq_nic_s *aq_nic = netdev_priv(ndev); 182 183 if (!aq_nic->aq_hw_ops->hw_filter_vlan_set) 184 return -EOPNOTSUPP; 185 186 set_bit(vid, aq_nic->active_vlans); 187 188 return aq_filters_vlans_update(aq_nic); 189 } 190 191 static int aq_ndo_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, 192 u16 vid) 193 { 194 struct aq_nic_s *aq_nic = netdev_priv(ndev); 195 196 if (!aq_nic->aq_hw_ops->hw_filter_vlan_set) 197 return -EOPNOTSUPP; 198 199 clear_bit(vid, aq_nic->active_vlans); 200 201 if (-ENOENT == aq_del_fvlan_by_vlan(aq_nic, vid)) 202 return aq_filters_vlans_update(aq_nic); 203 204 return 0; 205 } 206 207 static const struct net_device_ops aq_ndev_ops = { 208 .ndo_open = aq_ndev_open, 209 .ndo_stop = aq_ndev_close, 210 .ndo_start_xmit = aq_ndev_start_xmit, 211 .ndo_set_rx_mode = aq_ndev_set_multicast_settings, 212 .ndo_change_mtu = aq_ndev_change_mtu, 213 .ndo_set_mac_address = aq_ndev_set_mac_address, 214 .ndo_set_features = aq_ndev_set_features, 215 .ndo_vlan_rx_add_vid = aq_ndo_vlan_rx_add_vid, 216 .ndo_vlan_rx_kill_vid = aq_ndo_vlan_rx_kill_vid, 217 }; 218 219 static int __init aq_ndev_init_module(void) 220 { 221 int ret; 222 223 aq_ndev_wq = create_singlethread_workqueue(aq_ndev_driver_name); 224 if (!aq_ndev_wq) { 225 pr_err("Failed to create workqueue\n"); 226 return -ENOMEM; 227 } 228 229 ret = aq_pci_func_register_driver(); 230 if (ret) { 231 destroy_workqueue(aq_ndev_wq); 232 return ret; 233 } 234 235 return 0; 236 } 237 238 static void __exit aq_ndev_exit_module(void) 239 { 240 aq_pci_func_unregister_driver(); 241 242 if (aq_ndev_wq) { 243 destroy_workqueue(aq_ndev_wq); 244 aq_ndev_wq = NULL; 245 } 246 } 247 248 module_init(aq_ndev_init_module); 249 module_exit(aq_ndev_exit_module); 250