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_nic_start(aq_nic);
70 aq_nic_deinit(aq_nic);
74 static int aq_ndev_close(struct net_device *ndev)
77 struct aq_nic_s *aq_nic = netdev_priv(ndev);
79 err = aq_nic_stop(aq_nic);
82 aq_nic_deinit(aq_nic);
88 static int aq_ndev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
90 struct aq_nic_s *aq_nic = netdev_priv(ndev);
92 return aq_nic_xmit(aq_nic, skb);
95 static int aq_ndev_change_mtu(struct net_device *ndev, int new_mtu)
97 struct aq_nic_s *aq_nic = netdev_priv(ndev);
98 int err = aq_nic_set_mtu(aq_nic, new_mtu + ETH_HLEN);
108 static int aq_ndev_set_features(struct net_device *ndev,
109 netdev_features_t features)
111 bool is_vlan_rx_strip = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
112 bool is_vlan_tx_insert = !!(features & NETIF_F_HW_VLAN_CTAG_TX);
113 struct aq_nic_s *aq_nic = netdev_priv(ndev);
114 bool need_ndev_restart = false;
115 struct aq_nic_cfg_s *aq_cfg;
119 aq_cfg = aq_nic_get_cfg(aq_nic);
121 if (!(features & NETIF_F_NTUPLE)) {
122 if (aq_nic->ndev->features & NETIF_F_NTUPLE) {
123 err = aq_clear_rxnfc_all_rules(aq_nic);
128 if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER)) {
129 if (aq_nic->ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) {
130 err = aq_filters_vlan_offload_off(aq_nic);
136 aq_cfg->features = features;
138 if (aq_cfg->aq_hw_caps->hw_features & NETIF_F_LRO) {
139 is_lro = features & NETIF_F_LRO;
141 if (aq_cfg->is_lro != is_lro) {
142 aq_cfg->is_lro = is_lro;
143 need_ndev_restart = true;
147 if ((aq_nic->ndev->features ^ features) & NETIF_F_RXCSUM) {
148 err = aq_nic->aq_hw_ops->hw_set_offload(aq_nic->aq_hw,
155 if (aq_cfg->is_vlan_rx_strip != is_vlan_rx_strip) {
156 aq_cfg->is_vlan_rx_strip = is_vlan_rx_strip;
157 need_ndev_restart = true;
159 if (aq_cfg->is_vlan_tx_insert != is_vlan_tx_insert) {
160 aq_cfg->is_vlan_tx_insert = is_vlan_tx_insert;
161 need_ndev_restart = true;
164 if (need_ndev_restart && netif_running(ndev)) {
173 static int aq_ndev_set_mac_address(struct net_device *ndev, void *addr)
175 struct aq_nic_s *aq_nic = netdev_priv(ndev);
178 err = eth_mac_addr(ndev, addr);
181 err = aq_nic_set_mac(aq_nic, ndev);
189 static void aq_ndev_set_multicast_settings(struct net_device *ndev)
191 struct aq_nic_s *aq_nic = netdev_priv(ndev);
193 aq_nic_set_packet_filter(aq_nic, ndev->flags);
195 aq_nic_set_multicast_list(aq_nic, ndev);
198 static int aq_ndo_vlan_rx_add_vid(struct net_device *ndev, __be16 proto,
201 struct aq_nic_s *aq_nic = netdev_priv(ndev);
203 if (!aq_nic->aq_hw_ops->hw_filter_vlan_set)
206 set_bit(vid, aq_nic->active_vlans);
208 return aq_filters_vlans_update(aq_nic);
211 static int aq_ndo_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto,
214 struct aq_nic_s *aq_nic = netdev_priv(ndev);
216 if (!aq_nic->aq_hw_ops->hw_filter_vlan_set)
219 clear_bit(vid, aq_nic->active_vlans);
221 if (-ENOENT == aq_del_fvlan_by_vlan(aq_nic, vid))
222 return aq_filters_vlans_update(aq_nic);
227 static const struct net_device_ops aq_ndev_ops = {
228 .ndo_open = aq_ndev_open,
229 .ndo_stop = aq_ndev_close,
230 .ndo_start_xmit = aq_ndev_start_xmit,
231 .ndo_set_rx_mode = aq_ndev_set_multicast_settings,
232 .ndo_change_mtu = aq_ndev_change_mtu,
233 .ndo_set_mac_address = aq_ndev_set_mac_address,
234 .ndo_set_features = aq_ndev_set_features,
235 .ndo_vlan_rx_add_vid = aq_ndo_vlan_rx_add_vid,
236 .ndo_vlan_rx_kill_vid = aq_ndo_vlan_rx_kill_vid,
239 static int __init aq_ndev_init_module(void)
243 aq_ndev_wq = create_singlethread_workqueue(aq_ndev_driver_name);
245 pr_err("Failed to create workqueue\n");
249 ret = aq_pci_func_register_driver();
251 destroy_workqueue(aq_ndev_wq);
258 static void __exit aq_ndev_exit_module(void)
260 aq_pci_func_unregister_driver();
263 destroy_workqueue(aq_ndev_wq);
268 module_init(aq_ndev_init_module);
269 module_exit(aq_ndev_exit_module);