1 // SPDX-License-Identifier: GPL-2.0-only
3 * aQuantia Corporation Network Driver
4 * Copyright (C) 2014-2019 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"
14 #include "aq_filters.h"
16 #include <linux/netdevice.h>
17 #include <linux/module.h>
19 #include <linux/udp.h>
21 MODULE_LICENSE("GPL v2");
22 MODULE_AUTHOR(AQ_CFG_DRV_AUTHOR
);
23 MODULE_DESCRIPTION(AQ_CFG_DRV_DESC
);
25 static const char aq_ndev_driver_name
[] = AQ_CFG_DRV_NAME
;
27 static const struct net_device_ops aq_ndev_ops
;
29 static struct workqueue_struct
*aq_ndev_wq
;
31 void aq_ndev_schedule_work(struct work_struct
*work
)
33 queue_work(aq_ndev_wq
, work
);
36 struct net_device
*aq_ndev_alloc(void)
38 struct net_device
*ndev
= NULL
;
39 struct aq_nic_s
*aq_nic
= NULL
;
41 ndev
= alloc_etherdev_mq(sizeof(struct aq_nic_s
), AQ_CFG_VECS_MAX
);
45 aq_nic
= netdev_priv(ndev
);
47 ndev
->netdev_ops
= &aq_ndev_ops
;
48 ndev
->ethtool_ops
= &aq_ethtool_ops
;
53 static int aq_ndev_open(struct net_device
*ndev
)
55 struct aq_nic_s
*aq_nic
= netdev_priv(ndev
);
58 err
= aq_nic_init(aq_nic
);
62 err
= aq_reapply_rxnfc_all_rules(aq_nic
);
66 err
= aq_filters_vlans_update(aq_nic
);
70 err
= aq_nic_start(aq_nic
);
76 aq_nic_deinit(aq_nic
, true);
81 static int aq_ndev_close(struct net_device
*ndev
)
83 struct aq_nic_s
*aq_nic
= netdev_priv(ndev
);
86 err
= aq_nic_stop(aq_nic
);
89 aq_nic_deinit(aq_nic
, true);
95 static int aq_ndev_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
97 struct aq_nic_s
*aq_nic
= netdev_priv(ndev
);
99 if (unlikely(aq_utils_obj_test(&aq_nic
->flags
, AQ_NIC_PTP_DPATH_UP
))) {
100 /* Hardware adds the Timestamp for PTPv2 802.AS1
101 * and PTPv2 IPv4 UDP.
102 * We have to push even general 320 port messages to the ptp
103 * queue explicitly. This is a limitation of current firmware
104 * and hardware PTP design of the chip. Otherwise ptp stream
107 if (unlikely(skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
) ||
108 unlikely((ip_hdr(skb
)->version
== 4) &&
109 (ip_hdr(skb
)->protocol
== IPPROTO_UDP
) &&
110 ((udp_hdr(skb
)->dest
== htons(319)) ||
111 (udp_hdr(skb
)->dest
== htons(320)))) ||
112 unlikely(eth_hdr(skb
)->h_proto
== htons(ETH_P_1588
)))
113 return aq_ptp_xmit(aq_nic
, skb
);
116 skb_tx_timestamp(skb
);
117 return aq_nic_xmit(aq_nic
, skb
);
120 static int aq_ndev_change_mtu(struct net_device
*ndev
, int new_mtu
)
122 struct aq_nic_s
*aq_nic
= netdev_priv(ndev
);
125 err
= aq_nic_set_mtu(aq_nic
, new_mtu
+ ETH_HLEN
);
135 static int aq_ndev_set_features(struct net_device
*ndev
,
136 netdev_features_t features
)
138 bool is_vlan_tx_insert
= !!(features
& NETIF_F_HW_VLAN_CTAG_TX
);
139 bool is_vlan_rx_strip
= !!(features
& NETIF_F_HW_VLAN_CTAG_RX
);
140 struct aq_nic_s
*aq_nic
= netdev_priv(ndev
);
141 bool need_ndev_restart
= false;
142 struct aq_nic_cfg_s
*aq_cfg
;
146 aq_cfg
= aq_nic_get_cfg(aq_nic
);
148 if (!(features
& NETIF_F_NTUPLE
)) {
149 if (aq_nic
->ndev
->features
& NETIF_F_NTUPLE
) {
150 err
= aq_clear_rxnfc_all_rules(aq_nic
);
155 if (!(features
& NETIF_F_HW_VLAN_CTAG_FILTER
)) {
156 if (aq_nic
->ndev
->features
& NETIF_F_HW_VLAN_CTAG_FILTER
) {
157 err
= aq_filters_vlan_offload_off(aq_nic
);
163 aq_cfg
->features
= features
;
165 if (aq_cfg
->aq_hw_caps
->hw_features
& NETIF_F_LRO
) {
166 is_lro
= features
& NETIF_F_LRO
;
168 if (aq_cfg
->is_lro
!= is_lro
) {
169 aq_cfg
->is_lro
= is_lro
;
170 need_ndev_restart
= true;
174 if ((aq_nic
->ndev
->features
^ features
) & NETIF_F_RXCSUM
) {
175 err
= aq_nic
->aq_hw_ops
->hw_set_offload(aq_nic
->aq_hw
,
182 if (aq_cfg
->is_vlan_rx_strip
!= is_vlan_rx_strip
) {
183 aq_cfg
->is_vlan_rx_strip
= is_vlan_rx_strip
;
184 need_ndev_restart
= true;
186 if (aq_cfg
->is_vlan_tx_insert
!= is_vlan_tx_insert
) {
187 aq_cfg
->is_vlan_tx_insert
= is_vlan_tx_insert
;
188 need_ndev_restart
= true;
191 if (need_ndev_restart
&& netif_running(ndev
)) {
200 static int aq_ndev_set_mac_address(struct net_device
*ndev
, void *addr
)
202 struct aq_nic_s
*aq_nic
= netdev_priv(ndev
);
205 err
= eth_mac_addr(ndev
, addr
);
208 err
= aq_nic_set_mac(aq_nic
, ndev
);
216 static void aq_ndev_set_multicast_settings(struct net_device
*ndev
)
218 struct aq_nic_s
*aq_nic
= netdev_priv(ndev
);
220 (void)aq_nic_set_multicast_list(aq_nic
, ndev
);
223 static int aq_ndev_config_hwtstamp(struct aq_nic_s
*aq_nic
,
224 struct hwtstamp_config
*config
)
229 switch (config
->tx_type
) {
230 case HWTSTAMP_TX_OFF
:
237 switch (config
->rx_filter
) {
238 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
239 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
240 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
241 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
242 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
243 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
244 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
245 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
246 config
->rx_filter
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
248 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
249 case HWTSTAMP_FILTER_NONE
:
255 return aq_ptp_hwtstamp_config_set(aq_nic
->aq_ptp
, config
);
258 static int aq_ndev_hwtstamp_set(struct aq_nic_s
*aq_nic
, struct ifreq
*ifr
)
260 struct hwtstamp_config config
;
266 if (copy_from_user(&config
, ifr
->ifr_data
, sizeof(config
)))
269 ret_val
= aq_ndev_config_hwtstamp(aq_nic
, &config
);
273 return copy_to_user(ifr
->ifr_data
, &config
, sizeof(config
)) ?
277 static int aq_ndev_hwtstamp_get(struct aq_nic_s
*aq_nic
, struct ifreq
*ifr
)
279 struct hwtstamp_config config
;
284 aq_ptp_hwtstamp_config_get(aq_nic
->aq_ptp
, &config
);
285 return copy_to_user(ifr
->ifr_data
, &config
, sizeof(config
)) ?
289 static int aq_ndev_ioctl(struct net_device
*netdev
, struct ifreq
*ifr
, int cmd
)
291 struct aq_nic_s
*aq_nic
= netdev_priv(netdev
);
295 return aq_ndev_hwtstamp_set(aq_nic
, ifr
);
298 return aq_ndev_hwtstamp_get(aq_nic
, ifr
);
304 static int aq_ndo_vlan_rx_add_vid(struct net_device
*ndev
, __be16 proto
,
307 struct aq_nic_s
*aq_nic
= netdev_priv(ndev
);
309 if (!aq_nic
->aq_hw_ops
->hw_filter_vlan_set
)
312 set_bit(vid
, aq_nic
->active_vlans
);
314 return aq_filters_vlans_update(aq_nic
);
317 static int aq_ndo_vlan_rx_kill_vid(struct net_device
*ndev
, __be16 proto
,
320 struct aq_nic_s
*aq_nic
= netdev_priv(ndev
);
322 if (!aq_nic
->aq_hw_ops
->hw_filter_vlan_set
)
325 clear_bit(vid
, aq_nic
->active_vlans
);
327 if (-ENOENT
== aq_del_fvlan_by_vlan(aq_nic
, vid
))
328 return aq_filters_vlans_update(aq_nic
);
333 static const struct net_device_ops aq_ndev_ops
= {
334 .ndo_open
= aq_ndev_open
,
335 .ndo_stop
= aq_ndev_close
,
336 .ndo_start_xmit
= aq_ndev_start_xmit
,
337 .ndo_set_rx_mode
= aq_ndev_set_multicast_settings
,
338 .ndo_change_mtu
= aq_ndev_change_mtu
,
339 .ndo_set_mac_address
= aq_ndev_set_mac_address
,
340 .ndo_set_features
= aq_ndev_set_features
,
341 .ndo_do_ioctl
= aq_ndev_ioctl
,
342 .ndo_vlan_rx_add_vid
= aq_ndo_vlan_rx_add_vid
,
343 .ndo_vlan_rx_kill_vid
= aq_ndo_vlan_rx_kill_vid
,
346 static int __init
aq_ndev_init_module(void)
350 aq_ndev_wq
= create_singlethread_workqueue(aq_ndev_driver_name
);
352 pr_err("Failed to create workqueue\n");
356 ret
= aq_pci_func_register_driver();
358 destroy_workqueue(aq_ndev_wq
);
365 static void __exit
aq_ndev_exit_module(void)
367 aq_pci_func_unregister_driver();
370 destroy_workqueue(aq_ndev_wq
);
375 module_init(aq_ndev_init_module
);
376 module_exit(aq_ndev_exit_module
);