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