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