1 // SPDX-License-Identifier: GPL-2.0-only
3 * aQuantia Corporation Network Driver
4 * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
7 /* File aq_main.c: Main file for aQuantia Linux driver. */
11 #include "aq_pci_func.h"
12 #include "aq_ethtool.h"
13 #include "aq_filters.h"
15 #include <linux/netdevice.h>
16 #include <linux/module.h>
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);
23 static const char aq_ndev_driver_name[] = AQ_CFG_DRV_NAME;
25 static const struct net_device_ops aq_ndev_ops;
27 static struct workqueue_struct *aq_ndev_wq;
29 void aq_ndev_schedule_work(struct work_struct *work)
31 queue_work(aq_ndev_wq, work);
34 struct net_device *aq_ndev_alloc(void)
36 struct net_device *ndev = NULL;
37 struct aq_nic_s *aq_nic = NULL;
39 ndev = alloc_etherdev_mq(sizeof(struct aq_nic_s), AQ_CFG_VECS_MAX);
43 aq_nic = netdev_priv(ndev);
45 ndev->netdev_ops = &aq_ndev_ops;
46 ndev->ethtool_ops = &aq_ethtool_ops;
51 static int aq_ndev_open(struct net_device *ndev)
54 struct aq_nic_s *aq_nic = netdev_priv(ndev);
56 err = aq_nic_init(aq_nic);
60 err = aq_reapply_rxnfc_all_rules(aq_nic);
64 err = aq_filters_vlans_update(aq_nic);
68 err = aq_nic_start(aq_nic);
74 aq_nic_deinit(aq_nic);
78 static int aq_ndev_close(struct net_device *ndev)
81 struct aq_nic_s *aq_nic = netdev_priv(ndev);
83 err = aq_nic_stop(aq_nic);
86 aq_nic_deinit(aq_nic);
92 static int aq_ndev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
94 struct aq_nic_s *aq_nic = netdev_priv(ndev);
96 return aq_nic_xmit(aq_nic, skb);
99 static int aq_ndev_change_mtu(struct net_device *ndev, int new_mtu)
101 struct aq_nic_s *aq_nic = netdev_priv(ndev);
102 int err = aq_nic_set_mtu(aq_nic, new_mtu + ETH_HLEN);
112 static int aq_ndev_set_features(struct net_device *ndev,
113 netdev_features_t features)
115 bool is_vlan_rx_strip = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
116 bool is_vlan_tx_insert = !!(features & NETIF_F_HW_VLAN_CTAG_TX);
117 struct aq_nic_s *aq_nic = netdev_priv(ndev);
118 bool need_ndev_restart = false;
119 struct aq_nic_cfg_s *aq_cfg;
123 aq_cfg = aq_nic_get_cfg(aq_nic);
125 if (!(features & NETIF_F_NTUPLE)) {
126 if (aq_nic->ndev->features & NETIF_F_NTUPLE) {
127 err = aq_clear_rxnfc_all_rules(aq_nic);
132 if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER)) {
133 if (aq_nic->ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) {
134 err = aq_filters_vlan_offload_off(aq_nic);
140 aq_cfg->features = features;
142 if (aq_cfg->aq_hw_caps->hw_features & NETIF_F_LRO) {
143 is_lro = features & NETIF_F_LRO;
145 if (aq_cfg->is_lro != is_lro) {
146 aq_cfg->is_lro = is_lro;
147 need_ndev_restart = true;
151 if ((aq_nic->ndev->features ^ features) & NETIF_F_RXCSUM) {
152 err = aq_nic->aq_hw_ops->hw_set_offload(aq_nic->aq_hw,
159 if (aq_cfg->is_vlan_rx_strip != is_vlan_rx_strip) {
160 aq_cfg->is_vlan_rx_strip = is_vlan_rx_strip;
161 need_ndev_restart = true;
163 if (aq_cfg->is_vlan_tx_insert != is_vlan_tx_insert) {
164 aq_cfg->is_vlan_tx_insert = is_vlan_tx_insert;
165 need_ndev_restart = true;
168 if (need_ndev_restart && netif_running(ndev)) {
177 static int aq_ndev_set_mac_address(struct net_device *ndev, void *addr)
179 struct aq_nic_s *aq_nic = netdev_priv(ndev);
182 err = eth_mac_addr(ndev, addr);
185 err = aq_nic_set_mac(aq_nic, ndev);
193 static void aq_ndev_set_multicast_settings(struct net_device *ndev)
195 struct aq_nic_s *aq_nic = netdev_priv(ndev);
197 (void)aq_nic_set_multicast_list(aq_nic, ndev);
200 static int aq_ndo_vlan_rx_add_vid(struct net_device *ndev, __be16 proto,
203 struct aq_nic_s *aq_nic = netdev_priv(ndev);
205 if (!aq_nic->aq_hw_ops->hw_filter_vlan_set)
208 set_bit(vid, aq_nic->active_vlans);
210 return aq_filters_vlans_update(aq_nic);
213 static int aq_ndo_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto,
216 struct aq_nic_s *aq_nic = netdev_priv(ndev);
218 if (!aq_nic->aq_hw_ops->hw_filter_vlan_set)
221 clear_bit(vid, aq_nic->active_vlans);
223 if (-ENOENT == aq_del_fvlan_by_vlan(aq_nic, vid))
224 return aq_filters_vlans_update(aq_nic);
229 static const struct net_device_ops aq_ndev_ops = {
230 .ndo_open = aq_ndev_open,
231 .ndo_stop = aq_ndev_close,
232 .ndo_start_xmit = aq_ndev_start_xmit,
233 .ndo_set_rx_mode = aq_ndev_set_multicast_settings,
234 .ndo_change_mtu = aq_ndev_change_mtu,
235 .ndo_set_mac_address = aq_ndev_set_mac_address,
236 .ndo_set_features = aq_ndev_set_features,
237 .ndo_vlan_rx_add_vid = aq_ndo_vlan_rx_add_vid,
238 .ndo_vlan_rx_kill_vid = aq_ndo_vlan_rx_kill_vid,
241 static int __init aq_ndev_init_module(void)
245 aq_ndev_wq = create_singlethread_workqueue(aq_ndev_driver_name);
247 pr_err("Failed to create workqueue\n");
251 ret = aq_pci_func_register_driver();
253 destroy_workqueue(aq_ndev_wq);
260 static void __exit aq_ndev_exit_module(void)
262 aq_pci_func_unregister_driver();
265 destroy_workqueue(aq_ndev_wq);
270 module_init(aq_ndev_init_module);
271 module_exit(aq_ndev_exit_module);