1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Atlantic Network Driver
4 * Copyright (C) 2014-2019 aQuantia Corporation
5 * Copyright (C) 2019-2020 Marvell International Ltd.
8 /* File aq_main.c: Main file for aQuantia Linux driver. */
12 #include "aq_pci_func.h"
13 #include "aq_ethtool.h"
15 #include "aq_filters.h"
16 #include "aq_hw_utils.h"
18 #include <linux/netdevice.h>
19 #include <linux/module.h>
21 #include <linux/udp.h>
22 #include <net/pkt_cls.h>
24 MODULE_LICENSE("GPL v2");
25 MODULE_AUTHOR(AQ_CFG_DRV_AUTHOR);
26 MODULE_DESCRIPTION(AQ_CFG_DRV_DESC);
28 static const char aq_ndev_driver_name[] = AQ_CFG_DRV_NAME;
30 static const struct net_device_ops aq_ndev_ops;
32 static struct workqueue_struct *aq_ndev_wq;
34 void aq_ndev_schedule_work(struct work_struct *work)
36 queue_work(aq_ndev_wq, work);
39 struct net_device *aq_ndev_alloc(void)
41 struct net_device *ndev = NULL;
42 struct aq_nic_s *aq_nic = NULL;
44 ndev = alloc_etherdev_mq(sizeof(struct aq_nic_s), AQ_HW_QUEUES_MAX);
48 aq_nic = netdev_priv(ndev);
50 ndev->netdev_ops = &aq_ndev_ops;
51 ndev->ethtool_ops = &aq_ethtool_ops;
56 static int aq_ndev_open(struct net_device *ndev)
58 struct aq_nic_s *aq_nic = netdev_priv(ndev);
61 err = aq_nic_init(aq_nic);
65 err = aq_reapply_rxnfc_all_rules(aq_nic);
69 err = aq_filters_vlans_update(aq_nic);
73 err = aq_nic_start(aq_nic);
81 aq_nic_deinit(aq_nic, true);
86 static int aq_ndev_close(struct net_device *ndev)
88 struct aq_nic_s *aq_nic = netdev_priv(ndev);
91 err = aq_nic_stop(aq_nic);
94 aq_nic_deinit(aq_nic, true);
100 static netdev_tx_t aq_ndev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
102 struct aq_nic_s *aq_nic = netdev_priv(ndev);
104 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
105 if (unlikely(aq_utils_obj_test(&aq_nic->flags, AQ_NIC_PTP_DPATH_UP))) {
106 /* Hardware adds the Timestamp for PTPv2 802.AS1
107 * and PTPv2 IPv4 UDP.
108 * We have to push even general 320 port messages to the ptp
109 * queue explicitly. This is a limitation of current firmware
110 * and hardware PTP design of the chip. Otherwise ptp stream
113 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ||
114 unlikely((ip_hdr(skb)->version == 4) &&
115 (ip_hdr(skb)->protocol == IPPROTO_UDP) &&
116 ((udp_hdr(skb)->dest == htons(319)) ||
117 (udp_hdr(skb)->dest == htons(320)))) ||
118 unlikely(eth_hdr(skb)->h_proto == htons(ETH_P_1588)))
119 return aq_ptp_xmit(aq_nic, skb);
123 skb_tx_timestamp(skb);
124 return aq_nic_xmit(aq_nic, skb);
127 static int aq_ndev_change_mtu(struct net_device *ndev, int new_mtu)
129 struct aq_nic_s *aq_nic = netdev_priv(ndev);
132 err = aq_nic_set_mtu(aq_nic, new_mtu + ETH_HLEN);
142 static int aq_ndev_set_features(struct net_device *ndev,
143 netdev_features_t features)
145 bool is_vlan_tx_insert = !!(features & NETIF_F_HW_VLAN_CTAG_TX);
146 bool is_vlan_rx_strip = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
147 struct aq_nic_s *aq_nic = netdev_priv(ndev);
148 bool need_ndev_restart = false;
149 struct aq_nic_cfg_s *aq_cfg;
153 aq_cfg = aq_nic_get_cfg(aq_nic);
155 if (!(features & NETIF_F_NTUPLE)) {
156 if (aq_nic->ndev->features & NETIF_F_NTUPLE) {
157 err = aq_clear_rxnfc_all_rules(aq_nic);
162 if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER)) {
163 if (aq_nic->ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) {
164 err = aq_filters_vlan_offload_off(aq_nic);
170 aq_cfg->features = features;
172 if (aq_cfg->aq_hw_caps->hw_features & NETIF_F_LRO) {
173 is_lro = features & NETIF_F_LRO;
175 if (aq_cfg->is_lro != is_lro) {
176 aq_cfg->is_lro = is_lro;
177 need_ndev_restart = true;
181 if ((aq_nic->ndev->features ^ features) & NETIF_F_RXCSUM) {
182 err = aq_nic->aq_hw_ops->hw_set_offload(aq_nic->aq_hw,
189 if (aq_cfg->is_vlan_rx_strip != is_vlan_rx_strip) {
190 aq_cfg->is_vlan_rx_strip = is_vlan_rx_strip;
191 need_ndev_restart = true;
193 if (aq_cfg->is_vlan_tx_insert != is_vlan_tx_insert) {
194 aq_cfg->is_vlan_tx_insert = is_vlan_tx_insert;
195 need_ndev_restart = true;
198 if (need_ndev_restart && netif_running(ndev)) {
207 static int aq_ndev_set_mac_address(struct net_device *ndev, void *addr)
209 struct aq_nic_s *aq_nic = netdev_priv(ndev);
212 err = eth_mac_addr(ndev, addr);
215 err = aq_nic_set_mac(aq_nic, ndev);
223 static void aq_ndev_set_multicast_settings(struct net_device *ndev)
225 struct aq_nic_s *aq_nic = netdev_priv(ndev);
227 (void)aq_nic_set_multicast_list(aq_nic, ndev);
230 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
231 static int aq_ndev_config_hwtstamp(struct aq_nic_s *aq_nic,
232 struct hwtstamp_config *config)
237 switch (config->tx_type) {
238 case HWTSTAMP_TX_OFF:
245 switch (config->rx_filter) {
246 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
247 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
248 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
249 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
250 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
251 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
252 case HWTSTAMP_FILTER_PTP_V2_SYNC:
253 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
254 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
256 case HWTSTAMP_FILTER_PTP_V2_EVENT:
257 case HWTSTAMP_FILTER_NONE:
263 return aq_ptp_hwtstamp_config_set(aq_nic->aq_ptp, config);
267 static int aq_ndev_hwtstamp_set(struct aq_nic_s *aq_nic, struct ifreq *ifr)
269 struct hwtstamp_config config;
270 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
277 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
279 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
280 ret_val = aq_ndev_config_hwtstamp(aq_nic, &config);
285 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
289 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
290 static int aq_ndev_hwtstamp_get(struct aq_nic_s *aq_nic, struct ifreq *ifr)
292 struct hwtstamp_config config;
297 aq_ptp_hwtstamp_config_get(aq_nic->aq_ptp, &config);
298 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
303 static int aq_ndev_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
305 struct aq_nic_s *aq_nic = netdev_priv(netdev);
309 return aq_ndev_hwtstamp_set(aq_nic, ifr);
311 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
313 return aq_ndev_hwtstamp_get(aq_nic, ifr);
320 static int aq_ndo_vlan_rx_add_vid(struct net_device *ndev, __be16 proto,
323 struct aq_nic_s *aq_nic = netdev_priv(ndev);
325 if (!aq_nic->aq_hw_ops->hw_filter_vlan_set)
328 set_bit(vid, aq_nic->active_vlans);
330 return aq_filters_vlans_update(aq_nic);
333 static int aq_ndo_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto,
336 struct aq_nic_s *aq_nic = netdev_priv(ndev);
338 if (!aq_nic->aq_hw_ops->hw_filter_vlan_set)
341 clear_bit(vid, aq_nic->active_vlans);
343 if (-ENOENT == aq_del_fvlan_by_vlan(aq_nic, vid))
344 return aq_filters_vlans_update(aq_nic);
349 static int aq_validate_mqprio_opt(struct aq_nic_s *self,
350 struct tc_mqprio_qopt_offload *mqprio,
351 const unsigned int num_tc)
353 const bool has_min_rate = !!(mqprio->flags & TC_MQPRIO_F_MIN_RATE);
354 struct aq_nic_cfg_s *aq_nic_cfg = aq_nic_get_cfg(self);
355 const unsigned int tcs_max = min_t(u8, aq_nic_cfg->aq_hw_caps->tcs_max,
358 if (num_tc > tcs_max) {
359 netdev_err(self->ndev, "Too many TCs requested\n");
363 if (num_tc != 0 && !is_power_of_2(num_tc)) {
364 netdev_err(self->ndev, "TC count should be power of 2\n");
368 if (has_min_rate && !ATL_HW_IS_CHIP_FEATURE(self->aq_hw, ANTIGUA)) {
369 netdev_err(self->ndev, "Min tx rate is not supported\n");
376 static int aq_ndo_setup_tc(struct net_device *dev, enum tc_setup_type type,
379 struct tc_mqprio_qopt_offload *mqprio = type_data;
380 struct aq_nic_s *aq_nic = netdev_priv(dev);
386 if (type != TC_SETUP_QDISC_MQPRIO)
389 has_min_rate = !!(mqprio->flags & TC_MQPRIO_F_MIN_RATE);
390 has_max_rate = !!(mqprio->flags & TC_MQPRIO_F_MAX_RATE);
392 err = aq_validate_mqprio_opt(aq_nic, mqprio, mqprio->qopt.num_tc);
396 for (i = 0; i < mqprio->qopt.num_tc; i++) {
398 u64 max_rate = mqprio->max_rate[i];
400 do_div(max_rate, AQ_MBPS_DIVISOR);
401 aq_nic_setup_tc_max_rate(aq_nic, i, (u32)max_rate);
405 u64 min_rate = mqprio->min_rate[i];
407 do_div(min_rate, AQ_MBPS_DIVISOR);
408 aq_nic_setup_tc_min_rate(aq_nic, i, (u32)min_rate);
412 return aq_nic_setup_tc_mqprio(aq_nic, mqprio->qopt.num_tc,
413 mqprio->qopt.prio_tc_map);
416 static const struct net_device_ops aq_ndev_ops = {
417 .ndo_open = aq_ndev_open,
418 .ndo_stop = aq_ndev_close,
419 .ndo_start_xmit = aq_ndev_start_xmit,
420 .ndo_set_rx_mode = aq_ndev_set_multicast_settings,
421 .ndo_change_mtu = aq_ndev_change_mtu,
422 .ndo_set_mac_address = aq_ndev_set_mac_address,
423 .ndo_set_features = aq_ndev_set_features,
424 .ndo_eth_ioctl = aq_ndev_ioctl,
425 .ndo_vlan_rx_add_vid = aq_ndo_vlan_rx_add_vid,
426 .ndo_vlan_rx_kill_vid = aq_ndo_vlan_rx_kill_vid,
427 .ndo_setup_tc = aq_ndo_setup_tc,
430 static int __init aq_ndev_init_module(void)
434 aq_ndev_wq = create_singlethread_workqueue(aq_ndev_driver_name);
436 pr_err("Failed to create workqueue\n");
440 ret = aq_pci_func_register_driver();
442 destroy_workqueue(aq_ndev_wq);
449 static void __exit aq_ndev_exit_module(void)
451 aq_pci_func_unregister_driver();
454 destroy_workqueue(aq_ndev_wq);
459 module_init(aq_ndev_init_module);
460 module_exit(aq_ndev_exit_module);