1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Atlantic Network Driver
3  *
4  * Copyright (C) 2014-2019 aQuantia Corporation
5  * Copyright (C) 2019-2020 Marvell International Ltd.
6  */
7 
8 /* File aq_main.c: Main file for aQuantia Linux driver. */
9 
10 #include "aq_main.h"
11 #include "aq_nic.h"
12 #include "aq_pci_func.h"
13 #include "aq_ethtool.h"
14 #include "aq_ptp.h"
15 #include "aq_filters.h"
16 #include "aq_hw_utils.h"
17 
18 #include <linux/netdevice.h>
19 #include <linux/module.h>
20 #include <linux/ip.h>
21 #include <linux/udp.h>
22 #include <net/pkt_cls.h>
23 
24 MODULE_LICENSE("GPL v2");
25 MODULE_AUTHOR(AQ_CFG_DRV_AUTHOR);
26 MODULE_DESCRIPTION(AQ_CFG_DRV_DESC);
27 
28 static const char aq_ndev_driver_name[] = AQ_CFG_DRV_NAME;
29 
30 static const struct net_device_ops aq_ndev_ops;
31 
32 static struct workqueue_struct *aq_ndev_wq;
33 
34 void aq_ndev_schedule_work(struct work_struct *work)
35 {
36 	queue_work(aq_ndev_wq, work);
37 }
38 
39 struct net_device *aq_ndev_alloc(void)
40 {
41 	struct net_device *ndev = NULL;
42 	struct aq_nic_s *aq_nic = NULL;
43 
44 	ndev = alloc_etherdev_mq(sizeof(struct aq_nic_s), AQ_HW_QUEUES_MAX);
45 	if (!ndev)
46 		return NULL;
47 
48 	aq_nic = netdev_priv(ndev);
49 	aq_nic->ndev = ndev;
50 	ndev->netdev_ops = &aq_ndev_ops;
51 	ndev->ethtool_ops = &aq_ethtool_ops;
52 
53 	return ndev;
54 }
55 
56 static int aq_ndev_open(struct net_device *ndev)
57 {
58 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
59 	int err = 0;
60 
61 	err = aq_nic_init(aq_nic);
62 	if (err < 0)
63 		goto err_exit;
64 
65 	err = aq_reapply_rxnfc_all_rules(aq_nic);
66 	if (err < 0)
67 		goto err_exit;
68 
69 	err = aq_filters_vlans_update(aq_nic);
70 	if (err < 0)
71 		goto err_exit;
72 
73 	err = aq_nic_start(aq_nic);
74 	if (err < 0)
75 		goto err_exit;
76 
77 err_exit:
78 	if (err < 0)
79 		aq_nic_deinit(aq_nic, true);
80 
81 	return err;
82 }
83 
84 static int aq_ndev_close(struct net_device *ndev)
85 {
86 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
87 	int err = 0;
88 
89 	err = aq_nic_stop(aq_nic);
90 	if (err < 0)
91 		goto err_exit;
92 	aq_nic_deinit(aq_nic, true);
93 
94 err_exit:
95 	return err;
96 }
97 
98 static netdev_tx_t aq_ndev_start_xmit(struct sk_buff *skb, struct net_device *ndev)
99 {
100 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
101 
102 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
103 	if (unlikely(aq_utils_obj_test(&aq_nic->flags, AQ_NIC_PTP_DPATH_UP))) {
104 		/* Hardware adds the Timestamp for PTPv2 802.AS1
105 		 * and PTPv2 IPv4 UDP.
106 		 * We have to push even general 320 port messages to the ptp
107 		 * queue explicitly. This is a limitation of current firmware
108 		 * and hardware PTP design of the chip. Otherwise ptp stream
109 		 * will fail to sync
110 		 */
111 		if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ||
112 		    unlikely((ip_hdr(skb)->version == 4) &&
113 			     (ip_hdr(skb)->protocol == IPPROTO_UDP) &&
114 			     ((udp_hdr(skb)->dest == htons(319)) ||
115 			      (udp_hdr(skb)->dest == htons(320)))) ||
116 		    unlikely(eth_hdr(skb)->h_proto == htons(ETH_P_1588)))
117 			return aq_ptp_xmit(aq_nic, skb);
118 	}
119 #endif
120 
121 	skb_tx_timestamp(skb);
122 	return aq_nic_xmit(aq_nic, skb);
123 }
124 
125 static int aq_ndev_change_mtu(struct net_device *ndev, int new_mtu)
126 {
127 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
128 	int err;
129 
130 	err = aq_nic_set_mtu(aq_nic, new_mtu + ETH_HLEN);
131 
132 	if (err < 0)
133 		goto err_exit;
134 	ndev->mtu = new_mtu;
135 
136 err_exit:
137 	return err;
138 }
139 
140 static int aq_ndev_set_features(struct net_device *ndev,
141 				netdev_features_t features)
142 {
143 	bool is_vlan_tx_insert = !!(features & NETIF_F_HW_VLAN_CTAG_TX);
144 	bool is_vlan_rx_strip = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
145 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
146 	bool need_ndev_restart = false;
147 	struct aq_nic_cfg_s *aq_cfg;
148 	bool is_lro = false;
149 	int err = 0;
150 
151 	aq_cfg = aq_nic_get_cfg(aq_nic);
152 
153 	if (!(features & NETIF_F_NTUPLE)) {
154 		if (aq_nic->ndev->features & NETIF_F_NTUPLE) {
155 			err = aq_clear_rxnfc_all_rules(aq_nic);
156 			if (unlikely(err))
157 				goto err_exit;
158 		}
159 	}
160 	if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER)) {
161 		if (aq_nic->ndev->features & NETIF_F_HW_VLAN_CTAG_FILTER) {
162 			err = aq_filters_vlan_offload_off(aq_nic);
163 			if (unlikely(err))
164 				goto err_exit;
165 		}
166 	}
167 
168 	aq_cfg->features = features;
169 
170 	if (aq_cfg->aq_hw_caps->hw_features & NETIF_F_LRO) {
171 		is_lro = features & NETIF_F_LRO;
172 
173 		if (aq_cfg->is_lro != is_lro) {
174 			aq_cfg->is_lro = is_lro;
175 			need_ndev_restart = true;
176 		}
177 	}
178 
179 	if ((aq_nic->ndev->features ^ features) & NETIF_F_RXCSUM) {
180 		err = aq_nic->aq_hw_ops->hw_set_offload(aq_nic->aq_hw,
181 							aq_cfg);
182 
183 		if (unlikely(err))
184 			goto err_exit;
185 	}
186 
187 	if (aq_cfg->is_vlan_rx_strip != is_vlan_rx_strip) {
188 		aq_cfg->is_vlan_rx_strip = is_vlan_rx_strip;
189 		need_ndev_restart = true;
190 	}
191 	if (aq_cfg->is_vlan_tx_insert != is_vlan_tx_insert) {
192 		aq_cfg->is_vlan_tx_insert = is_vlan_tx_insert;
193 		need_ndev_restart = true;
194 	}
195 
196 	if (need_ndev_restart && netif_running(ndev)) {
197 		aq_ndev_close(ndev);
198 		aq_ndev_open(ndev);
199 	}
200 
201 err_exit:
202 	return err;
203 }
204 
205 static int aq_ndev_set_mac_address(struct net_device *ndev, void *addr)
206 {
207 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
208 	int err = 0;
209 
210 	err = eth_mac_addr(ndev, addr);
211 	if (err < 0)
212 		goto err_exit;
213 	err = aq_nic_set_mac(aq_nic, ndev);
214 	if (err < 0)
215 		goto err_exit;
216 
217 err_exit:
218 	return err;
219 }
220 
221 static void aq_ndev_set_multicast_settings(struct net_device *ndev)
222 {
223 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
224 
225 	(void)aq_nic_set_multicast_list(aq_nic, ndev);
226 }
227 
228 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
229 static int aq_ndev_config_hwtstamp(struct aq_nic_s *aq_nic,
230 				   struct hwtstamp_config *config)
231 {
232 	if (config->flags)
233 		return -EINVAL;
234 
235 	switch (config->tx_type) {
236 	case HWTSTAMP_TX_OFF:
237 	case HWTSTAMP_TX_ON:
238 		break;
239 	default:
240 		return -ERANGE;
241 	}
242 
243 	switch (config->rx_filter) {
244 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
245 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
246 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
247 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
248 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
249 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
250 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
251 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
252 		config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
253 		break;
254 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
255 	case HWTSTAMP_FILTER_NONE:
256 		break;
257 	default:
258 		return -ERANGE;
259 	}
260 
261 	return aq_ptp_hwtstamp_config_set(aq_nic->aq_ptp, config);
262 }
263 #endif
264 
265 static int aq_ndev_hwtstamp_set(struct aq_nic_s *aq_nic, struct ifreq *ifr)
266 {
267 	struct hwtstamp_config config;
268 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
269 	int ret_val;
270 #endif
271 
272 	if (!aq_nic->aq_ptp)
273 		return -EOPNOTSUPP;
274 
275 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
276 		return -EFAULT;
277 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
278 	ret_val = aq_ndev_config_hwtstamp(aq_nic, &config);
279 	if (ret_val)
280 		return ret_val;
281 #endif
282 
283 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
284 	       -EFAULT : 0;
285 }
286 
287 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
288 static int aq_ndev_hwtstamp_get(struct aq_nic_s *aq_nic, struct ifreq *ifr)
289 {
290 	struct hwtstamp_config config;
291 
292 	if (!aq_nic->aq_ptp)
293 		return -EOPNOTSUPP;
294 
295 	aq_ptp_hwtstamp_config_get(aq_nic->aq_ptp, &config);
296 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
297 	       -EFAULT : 0;
298 }
299 #endif
300 
301 static int aq_ndev_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
302 {
303 	struct aq_nic_s *aq_nic = netdev_priv(netdev);
304 
305 	switch (cmd) {
306 	case SIOCSHWTSTAMP:
307 		return aq_ndev_hwtstamp_set(aq_nic, ifr);
308 
309 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
310 	case SIOCGHWTSTAMP:
311 		return aq_ndev_hwtstamp_get(aq_nic, ifr);
312 #endif
313 	}
314 
315 	return -EOPNOTSUPP;
316 }
317 
318 static int aq_ndo_vlan_rx_add_vid(struct net_device *ndev, __be16 proto,
319 				  u16 vid)
320 {
321 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
322 
323 	if (!aq_nic->aq_hw_ops->hw_filter_vlan_set)
324 		return -EOPNOTSUPP;
325 
326 	set_bit(vid, aq_nic->active_vlans);
327 
328 	return aq_filters_vlans_update(aq_nic);
329 }
330 
331 static int aq_ndo_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto,
332 				   u16 vid)
333 {
334 	struct aq_nic_s *aq_nic = netdev_priv(ndev);
335 
336 	if (!aq_nic->aq_hw_ops->hw_filter_vlan_set)
337 		return -EOPNOTSUPP;
338 
339 	clear_bit(vid, aq_nic->active_vlans);
340 
341 	if (-ENOENT == aq_del_fvlan_by_vlan(aq_nic, vid))
342 		return aq_filters_vlans_update(aq_nic);
343 
344 	return 0;
345 }
346 
347 static int aq_validate_mqprio_opt(struct aq_nic_s *self,
348 				  struct tc_mqprio_qopt_offload *mqprio,
349 				  const unsigned int num_tc)
350 {
351 	const bool has_min_rate = !!(mqprio->flags & TC_MQPRIO_F_MIN_RATE);
352 	struct aq_nic_cfg_s *aq_nic_cfg = aq_nic_get_cfg(self);
353 	const unsigned int tcs_max = min_t(u8, aq_nic_cfg->aq_hw_caps->tcs_max,
354 					   AQ_CFG_TCS_MAX);
355 
356 	if (num_tc > tcs_max) {
357 		netdev_err(self->ndev, "Too many TCs requested\n");
358 		return -EOPNOTSUPP;
359 	}
360 
361 	if (num_tc != 0 && !is_power_of_2(num_tc)) {
362 		netdev_err(self->ndev, "TC count should be power of 2\n");
363 		return -EOPNOTSUPP;
364 	}
365 
366 	if (has_min_rate && !ATL_HW_IS_CHIP_FEATURE(self->aq_hw, ANTIGUA)) {
367 		netdev_err(self->ndev, "Min tx rate is not supported\n");
368 		return -EOPNOTSUPP;
369 	}
370 
371 	return 0;
372 }
373 
374 static int aq_ndo_setup_tc(struct net_device *dev, enum tc_setup_type type,
375 			   void *type_data)
376 {
377 	struct tc_mqprio_qopt_offload *mqprio = type_data;
378 	struct aq_nic_s *aq_nic = netdev_priv(dev);
379 	bool has_min_rate;
380 	bool has_max_rate;
381 	int err;
382 	int i;
383 
384 	if (type != TC_SETUP_QDISC_MQPRIO)
385 		return -EOPNOTSUPP;
386 
387 	has_min_rate = !!(mqprio->flags & TC_MQPRIO_F_MIN_RATE);
388 	has_max_rate = !!(mqprio->flags & TC_MQPRIO_F_MAX_RATE);
389 
390 	err = aq_validate_mqprio_opt(aq_nic, mqprio, mqprio->qopt.num_tc);
391 	if (err)
392 		return err;
393 
394 	for (i = 0; i < mqprio->qopt.num_tc; i++) {
395 		if (has_max_rate) {
396 			u64 max_rate = mqprio->max_rate[i];
397 
398 			do_div(max_rate, AQ_MBPS_DIVISOR);
399 			aq_nic_setup_tc_max_rate(aq_nic, i, (u32)max_rate);
400 		}
401 
402 		if (has_min_rate) {
403 			u64 min_rate = mqprio->min_rate[i];
404 
405 			do_div(min_rate, AQ_MBPS_DIVISOR);
406 			aq_nic_setup_tc_min_rate(aq_nic, i, (u32)min_rate);
407 		}
408 	}
409 
410 	return aq_nic_setup_tc_mqprio(aq_nic, mqprio->qopt.num_tc,
411 				      mqprio->qopt.prio_tc_map);
412 }
413 
414 static const struct net_device_ops aq_ndev_ops = {
415 	.ndo_open = aq_ndev_open,
416 	.ndo_stop = aq_ndev_close,
417 	.ndo_start_xmit = aq_ndev_start_xmit,
418 	.ndo_set_rx_mode = aq_ndev_set_multicast_settings,
419 	.ndo_change_mtu = aq_ndev_change_mtu,
420 	.ndo_set_mac_address = aq_ndev_set_mac_address,
421 	.ndo_set_features = aq_ndev_set_features,
422 	.ndo_do_ioctl = aq_ndev_ioctl,
423 	.ndo_vlan_rx_add_vid = aq_ndo_vlan_rx_add_vid,
424 	.ndo_vlan_rx_kill_vid = aq_ndo_vlan_rx_kill_vid,
425 	.ndo_setup_tc = aq_ndo_setup_tc,
426 };
427 
428 static int __init aq_ndev_init_module(void)
429 {
430 	int ret;
431 
432 	aq_ndev_wq = create_singlethread_workqueue(aq_ndev_driver_name);
433 	if (!aq_ndev_wq) {
434 		pr_err("Failed to create workqueue\n");
435 		return -ENOMEM;
436 	}
437 
438 	ret = aq_pci_func_register_driver();
439 	if (ret) {
440 		destroy_workqueue(aq_ndev_wq);
441 		return ret;
442 	}
443 
444 	return 0;
445 }
446 
447 static void __exit aq_ndev_exit_module(void)
448 {
449 	aq_pci_func_unregister_driver();
450 
451 	if (aq_ndev_wq) {
452 		destroy_workqueue(aq_ndev_wq);
453 		aq_ndev_wq = NULL;
454 	}
455 }
456 
457 module_init(aq_ndev_init_module);
458 module_exit(aq_ndev_exit_module);
459