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_nic.c: Definition of common code for NIC. */
8 
9 #include "aq_nic.h"
10 #include "aq_ring.h"
11 #include "aq_vec.h"
12 #include "aq_hw.h"
13 #include "aq_pci_func.h"
14 #include "aq_main.h"
15 #include "aq_phy.h"
16 #include "aq_ptp.h"
17 #include "aq_filters.h"
18 
19 #include <linux/moduleparam.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/timer.h>
23 #include <linux/cpu.h>
24 #include <linux/ip.h>
25 #include <linux/tcp.h>
26 #include <net/ip.h>
27 
28 static unsigned int aq_itr = AQ_CFG_INTERRUPT_MODERATION_AUTO;
29 module_param_named(aq_itr, aq_itr, uint, 0644);
30 MODULE_PARM_DESC(aq_itr, "Interrupt throttling mode");
31 
32 static unsigned int aq_itr_tx;
33 module_param_named(aq_itr_tx, aq_itr_tx, uint, 0644);
34 MODULE_PARM_DESC(aq_itr_tx, "TX interrupt throttle rate");
35 
36 static unsigned int aq_itr_rx;
37 module_param_named(aq_itr_rx, aq_itr_rx, uint, 0644);
38 MODULE_PARM_DESC(aq_itr_rx, "RX interrupt throttle rate");
39 
40 static void aq_nic_update_ndev_stats(struct aq_nic_s *self);
41 
42 static void aq_nic_rss_init(struct aq_nic_s *self, unsigned int num_rss_queues)
43 {
44 	static u8 rss_key[AQ_CFG_RSS_HASHKEY_SIZE] = {
45 		0x1e, 0xad, 0x71, 0x87, 0x65, 0xfc, 0x26, 0x7d,
46 		0x0d, 0x45, 0x67, 0x74, 0xcd, 0x06, 0x1a, 0x18,
47 		0xb6, 0xc1, 0xf0, 0xc7, 0xbb, 0x18, 0xbe, 0xf8,
48 		0x19, 0x13, 0x4b, 0xa9, 0xd0, 0x3e, 0xfe, 0x70,
49 		0x25, 0x03, 0xab, 0x50, 0x6a, 0x8b, 0x82, 0x0c
50 	};
51 	struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;
52 	struct aq_rss_parameters *rss_params;
53 	int i = 0;
54 
55 	rss_params = &cfg->aq_rss;
56 
57 	rss_params->hash_secret_key_size = sizeof(rss_key);
58 	memcpy(rss_params->hash_secret_key, rss_key, sizeof(rss_key));
59 	rss_params->indirection_table_size = AQ_CFG_RSS_INDIRECTION_TABLE_MAX;
60 
61 	for (i = rss_params->indirection_table_size; i--;)
62 		rss_params->indirection_table[i] = i & (num_rss_queues - 1);
63 }
64 
65 /* Checks hw_caps and 'corrects' aq_nic_cfg in runtime */
66 void aq_nic_cfg_start(struct aq_nic_s *self)
67 {
68 	struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;
69 
70 	cfg->tcs = AQ_CFG_TCS_DEF;
71 
72 	cfg->is_polling = AQ_CFG_IS_POLLING_DEF;
73 
74 	cfg->itr = aq_itr;
75 	cfg->tx_itr = aq_itr_tx;
76 	cfg->rx_itr = aq_itr_rx;
77 
78 	cfg->rxpageorder = AQ_CFG_RX_PAGEORDER;
79 	cfg->is_rss = AQ_CFG_IS_RSS_DEF;
80 	cfg->num_rss_queues = AQ_CFG_NUM_RSS_QUEUES_DEF;
81 	cfg->aq_rss.base_cpu_number = AQ_CFG_RSS_BASE_CPU_NUM_DEF;
82 	cfg->fc.req = AQ_CFG_FC_MODE;
83 	cfg->wol = AQ_CFG_WOL_MODES;
84 
85 	cfg->mtu = AQ_CFG_MTU_DEF;
86 	cfg->link_speed_msk = AQ_CFG_SPEED_MSK;
87 	cfg->is_autoneg = AQ_CFG_IS_AUTONEG_DEF;
88 
89 	cfg->is_lro = AQ_CFG_IS_LRO_DEF;
90 
91 	/*descriptors */
92 	cfg->rxds = min(cfg->aq_hw_caps->rxds_max, AQ_CFG_RXDS_DEF);
93 	cfg->txds = min(cfg->aq_hw_caps->txds_max, AQ_CFG_TXDS_DEF);
94 
95 	/*rss rings */
96 	cfg->vecs = min(cfg->aq_hw_caps->vecs, AQ_CFG_VECS_DEF);
97 	cfg->vecs = min(cfg->vecs, num_online_cpus());
98 	if (self->irqvecs > AQ_HW_SERVICE_IRQS)
99 		cfg->vecs = min(cfg->vecs, self->irqvecs - AQ_HW_SERVICE_IRQS);
100 	/* cfg->vecs should be power of 2 for RSS */
101 	if (cfg->vecs >= 8U)
102 		cfg->vecs = 8U;
103 	else if (cfg->vecs >= 4U)
104 		cfg->vecs = 4U;
105 	else if (cfg->vecs >= 2U)
106 		cfg->vecs = 2U;
107 	else
108 		cfg->vecs = 1U;
109 
110 	cfg->num_rss_queues = min(cfg->vecs, AQ_CFG_NUM_RSS_QUEUES_DEF);
111 
112 	aq_nic_rss_init(self, cfg->num_rss_queues);
113 
114 	cfg->irq_type = aq_pci_func_get_irq_type(self);
115 
116 	if ((cfg->irq_type == AQ_HW_IRQ_LEGACY) ||
117 	    (cfg->aq_hw_caps->vecs == 1U) ||
118 	    (cfg->vecs == 1U)) {
119 		cfg->is_rss = 0U;
120 		cfg->vecs = 1U;
121 	}
122 
123 	/* Check if we have enough vectors allocated for
124 	 * link status IRQ. If no - we'll know link state from
125 	 * slower service task.
126 	 */
127 	if (AQ_HW_SERVICE_IRQS > 0 && cfg->vecs + 1 <= self->irqvecs)
128 		cfg->link_irq_vec = cfg->vecs;
129 	else
130 		cfg->link_irq_vec = 0;
131 
132 	cfg->link_speed_msk &= cfg->aq_hw_caps->link_speed_msk;
133 	cfg->features = cfg->aq_hw_caps->hw_features;
134 	cfg->is_vlan_rx_strip = !!(cfg->features & NETIF_F_HW_VLAN_CTAG_RX);
135 	cfg->is_vlan_tx_insert = !!(cfg->features & NETIF_F_HW_VLAN_CTAG_TX);
136 	cfg->is_vlan_force_promisc = true;
137 }
138 
139 static int aq_nic_update_link_status(struct aq_nic_s *self)
140 {
141 	int err = self->aq_fw_ops->update_link_status(self->aq_hw);
142 	u32 fc = 0;
143 
144 	if (err)
145 		return err;
146 
147 	if (self->aq_fw_ops->get_flow_control)
148 		self->aq_fw_ops->get_flow_control(self->aq_hw, &fc);
149 	self->aq_nic_cfg.fc.cur = fc;
150 
151 	if (self->link_status.mbps != self->aq_hw->aq_link_status.mbps) {
152 		netdev_info(self->ndev, "%s: link change old %d new %d\n",
153 			    AQ_CFG_DRV_NAME, self->link_status.mbps,
154 			    self->aq_hw->aq_link_status.mbps);
155 		aq_nic_update_interrupt_moderation_settings(self);
156 
157 		if (self->aq_ptp) {
158 			aq_ptp_clock_init(self);
159 			aq_ptp_tm_offset_set(self,
160 					     self->aq_hw->aq_link_status.mbps);
161 			aq_ptp_link_change(self);
162 		}
163 
164 		/* Driver has to update flow control settings on RX block
165 		 * on any link event.
166 		 * We should query FW whether it negotiated FC.
167 		 */
168 		if (self->aq_hw_ops->hw_set_fc)
169 			self->aq_hw_ops->hw_set_fc(self->aq_hw, fc, 0);
170 	}
171 
172 	self->link_status = self->aq_hw->aq_link_status;
173 	if (!netif_carrier_ok(self->ndev) && self->link_status.mbps) {
174 		aq_utils_obj_set(&self->flags,
175 				 AQ_NIC_FLAG_STARTED);
176 		aq_utils_obj_clear(&self->flags,
177 				   AQ_NIC_LINK_DOWN);
178 		netif_carrier_on(self->ndev);
179 		netif_tx_wake_all_queues(self->ndev);
180 	}
181 	if (netif_carrier_ok(self->ndev) && !self->link_status.mbps) {
182 		netif_carrier_off(self->ndev);
183 		netif_tx_disable(self->ndev);
184 		aq_utils_obj_set(&self->flags, AQ_NIC_LINK_DOWN);
185 	}
186 
187 	return 0;
188 }
189 
190 static irqreturn_t aq_linkstate_threaded_isr(int irq, void *private)
191 {
192 	struct aq_nic_s *self = private;
193 
194 	if (!self)
195 		return IRQ_NONE;
196 
197 	aq_nic_update_link_status(self);
198 
199 	self->aq_hw_ops->hw_irq_enable(self->aq_hw,
200 				       BIT(self->aq_nic_cfg.link_irq_vec));
201 
202 	return IRQ_HANDLED;
203 }
204 
205 static void aq_nic_service_task(struct work_struct *work)
206 {
207 	struct aq_nic_s *self = container_of(work, struct aq_nic_s,
208 					     service_task);
209 	int err;
210 
211 	aq_ptp_service_task(self);
212 
213 	if (aq_utils_obj_test(&self->flags, AQ_NIC_FLAGS_IS_NOT_READY))
214 		return;
215 
216 	err = aq_nic_update_link_status(self);
217 	if (err)
218 		return;
219 
220 	mutex_lock(&self->fwreq_mutex);
221 	if (self->aq_fw_ops->update_stats)
222 		self->aq_fw_ops->update_stats(self->aq_hw);
223 	mutex_unlock(&self->fwreq_mutex);
224 
225 	aq_nic_update_ndev_stats(self);
226 }
227 
228 static void aq_nic_service_timer_cb(struct timer_list *t)
229 {
230 	struct aq_nic_s *self = from_timer(self, t, service_timer);
231 
232 	mod_timer(&self->service_timer,
233 		  jiffies + AQ_CFG_SERVICE_TIMER_INTERVAL);
234 
235 	aq_ndev_schedule_work(&self->service_task);
236 }
237 
238 static void aq_nic_polling_timer_cb(struct timer_list *t)
239 {
240 	struct aq_nic_s *self = from_timer(self, t, polling_timer);
241 	struct aq_vec_s *aq_vec = NULL;
242 	unsigned int i = 0U;
243 
244 	for (i = 0U, aq_vec = self->aq_vec[0];
245 		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
246 		aq_vec_isr(i, (void *)aq_vec);
247 
248 	mod_timer(&self->polling_timer, jiffies +
249 		  AQ_CFG_POLLING_TIMER_INTERVAL);
250 }
251 
252 int aq_nic_ndev_register(struct aq_nic_s *self)
253 {
254 	int err = 0;
255 
256 	if (!self->ndev) {
257 		err = -EINVAL;
258 		goto err_exit;
259 	}
260 
261 	err = hw_atl_utils_initfw(self->aq_hw, &self->aq_fw_ops);
262 	if (err)
263 		goto err_exit;
264 
265 	mutex_lock(&self->fwreq_mutex);
266 	err = self->aq_fw_ops->get_mac_permanent(self->aq_hw,
267 			    self->ndev->dev_addr);
268 	mutex_unlock(&self->fwreq_mutex);
269 	if (err)
270 		goto err_exit;
271 
272 #if defined(AQ_CFG_MAC_ADDR_PERMANENT)
273 	{
274 		static u8 mac_addr_permanent[] = AQ_CFG_MAC_ADDR_PERMANENT;
275 
276 		ether_addr_copy(self->ndev->dev_addr, mac_addr_permanent);
277 	}
278 #endif
279 
280 	for (self->aq_vecs = 0; self->aq_vecs < aq_nic_get_cfg(self)->vecs;
281 	     self->aq_vecs++) {
282 		self->aq_vec[self->aq_vecs] =
283 		    aq_vec_alloc(self, self->aq_vecs, aq_nic_get_cfg(self));
284 		if (!self->aq_vec[self->aq_vecs]) {
285 			err = -ENOMEM;
286 			goto err_exit;
287 		}
288 	}
289 
290 	netif_carrier_off(self->ndev);
291 
292 	netif_tx_disable(self->ndev);
293 
294 	err = register_netdev(self->ndev);
295 	if (err)
296 		goto err_exit;
297 
298 err_exit:
299 	return err;
300 }
301 
302 void aq_nic_ndev_init(struct aq_nic_s *self)
303 {
304 	const struct aq_hw_caps_s *aq_hw_caps = self->aq_nic_cfg.aq_hw_caps;
305 	struct aq_nic_cfg_s *aq_nic_cfg = &self->aq_nic_cfg;
306 
307 	self->ndev->hw_features |= aq_hw_caps->hw_features;
308 	self->ndev->features = aq_hw_caps->hw_features;
309 	self->ndev->vlan_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
310 				     NETIF_F_RXHASH | NETIF_F_SG |
311 				     NETIF_F_LRO | NETIF_F_TSO;
312 	self->ndev->gso_partial_features = NETIF_F_GSO_UDP_L4;
313 	self->ndev->priv_flags = aq_hw_caps->hw_priv_flags;
314 	self->ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
315 
316 	self->msg_enable = NETIF_MSG_DRV | NETIF_MSG_LINK;
317 	self->ndev->mtu = aq_nic_cfg->mtu - ETH_HLEN;
318 	self->ndev->max_mtu = aq_hw_caps->mtu - ETH_FCS_LEN - ETH_HLEN;
319 
320 }
321 
322 void aq_nic_set_tx_ring(struct aq_nic_s *self, unsigned int idx,
323 			struct aq_ring_s *ring)
324 {
325 	self->aq_ring_tx[idx] = ring;
326 }
327 
328 struct net_device *aq_nic_get_ndev(struct aq_nic_s *self)
329 {
330 	return self->ndev;
331 }
332 
333 int aq_nic_init(struct aq_nic_s *self)
334 {
335 	struct aq_vec_s *aq_vec = NULL;
336 	unsigned int i = 0U;
337 	int err = 0;
338 
339 	self->power_state = AQ_HW_POWER_STATE_D0;
340 	mutex_lock(&self->fwreq_mutex);
341 	err = self->aq_hw_ops->hw_reset(self->aq_hw);
342 	mutex_unlock(&self->fwreq_mutex);
343 	if (err < 0)
344 		goto err_exit;
345 
346 	err = self->aq_hw_ops->hw_init(self->aq_hw,
347 				       aq_nic_get_ndev(self)->dev_addr);
348 	if (err < 0)
349 		goto err_exit;
350 
351 	if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_TP) {
352 		self->aq_hw->phy_id = HW_ATL_PHY_ID_MAX;
353 		err = aq_phy_init(self->aq_hw);
354 	}
355 
356 	for (i = 0U, aq_vec = self->aq_vec[0];
357 		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
358 		aq_vec_init(aq_vec, self->aq_hw_ops, self->aq_hw);
359 
360 	err = aq_ptp_init(self, self->irqvecs - 1);
361 	if (err < 0)
362 		goto err_exit;
363 
364 	err = aq_ptp_ring_alloc(self);
365 	if (err < 0)
366 		goto err_exit;
367 
368 	err = aq_ptp_ring_init(self);
369 	if (err < 0)
370 		goto err_exit;
371 
372 	netif_carrier_off(self->ndev);
373 
374 err_exit:
375 	return err;
376 }
377 
378 int aq_nic_start(struct aq_nic_s *self)
379 {
380 	struct aq_vec_s *aq_vec = NULL;
381 	unsigned int i = 0U;
382 	int err = 0;
383 
384 	err = self->aq_hw_ops->hw_multicast_list_set(self->aq_hw,
385 						     self->mc_list.ar,
386 						     self->mc_list.count);
387 	if (err < 0)
388 		goto err_exit;
389 
390 	err = self->aq_hw_ops->hw_packet_filter_set(self->aq_hw,
391 						    self->packet_filter);
392 	if (err < 0)
393 		goto err_exit;
394 
395 	for (i = 0U, aq_vec = self->aq_vec[0];
396 		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i]) {
397 		err = aq_vec_start(aq_vec);
398 		if (err < 0)
399 			goto err_exit;
400 	}
401 
402 	err = aq_ptp_ring_start(self);
403 	if (err < 0)
404 		goto err_exit;
405 
406 	aq_nic_set_loopback(self);
407 
408 	err = self->aq_hw_ops->hw_start(self->aq_hw);
409 	if (err < 0)
410 		goto err_exit;
411 
412 	err = aq_nic_update_interrupt_moderation_settings(self);
413 	if (err)
414 		goto err_exit;
415 
416 	INIT_WORK(&self->service_task, aq_nic_service_task);
417 
418 	timer_setup(&self->service_timer, aq_nic_service_timer_cb, 0);
419 	aq_nic_service_timer_cb(&self->service_timer);
420 
421 	if (self->aq_nic_cfg.is_polling) {
422 		timer_setup(&self->polling_timer, aq_nic_polling_timer_cb, 0);
423 		mod_timer(&self->polling_timer, jiffies +
424 			  AQ_CFG_POLLING_TIMER_INTERVAL);
425 	} else {
426 		for (i = 0U, aq_vec = self->aq_vec[0];
427 			self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i]) {
428 			err = aq_pci_func_alloc_irq(self, i, self->ndev->name,
429 						    aq_vec_isr, aq_vec,
430 						    aq_vec_get_affinity_mask(aq_vec));
431 			if (err < 0)
432 				goto err_exit;
433 		}
434 
435 		err = aq_ptp_irq_alloc(self);
436 		if (err < 0)
437 			goto err_exit;
438 
439 		if (self->aq_nic_cfg.link_irq_vec) {
440 			int irqvec = pci_irq_vector(self->pdev,
441 						   self->aq_nic_cfg.link_irq_vec);
442 			err = request_threaded_irq(irqvec, NULL,
443 						   aq_linkstate_threaded_isr,
444 						   IRQF_SHARED | IRQF_ONESHOT,
445 						   self->ndev->name, self);
446 			if (err < 0)
447 				goto err_exit;
448 			self->msix_entry_mask |= (1 << self->aq_nic_cfg.link_irq_vec);
449 		}
450 
451 		err = self->aq_hw_ops->hw_irq_enable(self->aq_hw,
452 						     AQ_CFG_IRQ_MASK);
453 		if (err < 0)
454 			goto err_exit;
455 	}
456 
457 	err = netif_set_real_num_tx_queues(self->ndev, self->aq_vecs);
458 	if (err < 0)
459 		goto err_exit;
460 
461 	err = netif_set_real_num_rx_queues(self->ndev, self->aq_vecs);
462 	if (err < 0)
463 		goto err_exit;
464 
465 	netif_tx_start_all_queues(self->ndev);
466 
467 err_exit:
468 	return err;
469 }
470 
471 unsigned int aq_nic_map_skb(struct aq_nic_s *self, struct sk_buff *skb,
472 			    struct aq_ring_s *ring)
473 {
474 	unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
475 	struct aq_ring_buff_s *first = NULL;
476 	u8 ipver = ip_hdr(skb)->version;
477 	struct aq_ring_buff_s *dx_buff;
478 	bool need_context_tag = false;
479 	unsigned int frag_count = 0U;
480 	unsigned int ret = 0U;
481 	unsigned int dx;
482 	u8 l4proto = 0;
483 
484 	if (ipver == 4)
485 		l4proto = ip_hdr(skb)->protocol;
486 	else if (ipver == 6)
487 		l4proto = ipv6_hdr(skb)->nexthdr;
488 
489 	dx = ring->sw_tail;
490 	dx_buff = &ring->buff_ring[dx];
491 	dx_buff->flags = 0U;
492 
493 	if (unlikely(skb_is_gso(skb))) {
494 		dx_buff->mss = skb_shinfo(skb)->gso_size;
495 		if (l4proto == IPPROTO_TCP) {
496 			dx_buff->is_gso_tcp = 1U;
497 			dx_buff->len_l4 = tcp_hdrlen(skb);
498 		} else if (l4proto == IPPROTO_UDP) {
499 			dx_buff->is_gso_udp = 1U;
500 			dx_buff->len_l4 = sizeof(struct udphdr);
501 			/* UDP GSO Hardware does not replace packet length. */
502 			udp_hdr(skb)->len = htons(dx_buff->mss +
503 						  dx_buff->len_l4);
504 		} else {
505 			WARN_ONCE(true, "Bad GSO mode");
506 			goto exit;
507 		}
508 		dx_buff->len_pkt = skb->len;
509 		dx_buff->len_l2 = ETH_HLEN;
510 		dx_buff->len_l3 = skb_network_header_len(skb);
511 		dx_buff->eop_index = 0xffffU;
512 		dx_buff->is_ipv6 = (ipver == 6);
513 		need_context_tag = true;
514 	}
515 
516 	if (self->aq_nic_cfg.is_vlan_tx_insert && skb_vlan_tag_present(skb)) {
517 		dx_buff->vlan_tx_tag = skb_vlan_tag_get(skb);
518 		dx_buff->len_pkt = skb->len;
519 		dx_buff->is_vlan = 1U;
520 		need_context_tag = true;
521 	}
522 
523 	if (need_context_tag) {
524 		dx = aq_ring_next_dx(ring, dx);
525 		dx_buff = &ring->buff_ring[dx];
526 		dx_buff->flags = 0U;
527 		++ret;
528 	}
529 
530 	dx_buff->len = skb_headlen(skb);
531 	dx_buff->pa = dma_map_single(aq_nic_get_dev(self),
532 				     skb->data,
533 				     dx_buff->len,
534 				     DMA_TO_DEVICE);
535 
536 	if (unlikely(dma_mapping_error(aq_nic_get_dev(self), dx_buff->pa)))
537 		goto exit;
538 
539 	first = dx_buff;
540 	dx_buff->len_pkt = skb->len;
541 	dx_buff->is_sop = 1U;
542 	dx_buff->is_mapped = 1U;
543 	++ret;
544 
545 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
546 		dx_buff->is_ip_cso = (htons(ETH_P_IP) == skb->protocol);
547 		dx_buff->is_tcp_cso = (l4proto == IPPROTO_TCP);
548 		dx_buff->is_udp_cso = (l4proto == IPPROTO_UDP);
549 	}
550 
551 	for (; nr_frags--; ++frag_count) {
552 		unsigned int frag_len = 0U;
553 		unsigned int buff_offset = 0U;
554 		unsigned int buff_size = 0U;
555 		dma_addr_t frag_pa;
556 		skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_count];
557 
558 		frag_len = skb_frag_size(frag);
559 
560 		while (frag_len) {
561 			if (frag_len > AQ_CFG_TX_FRAME_MAX)
562 				buff_size = AQ_CFG_TX_FRAME_MAX;
563 			else
564 				buff_size = frag_len;
565 
566 			frag_pa = skb_frag_dma_map(aq_nic_get_dev(self),
567 						   frag,
568 						   buff_offset,
569 						   buff_size,
570 						   DMA_TO_DEVICE);
571 
572 			if (unlikely(dma_mapping_error(aq_nic_get_dev(self),
573 						       frag_pa)))
574 				goto mapping_error;
575 
576 			dx = aq_ring_next_dx(ring, dx);
577 			dx_buff = &ring->buff_ring[dx];
578 
579 			dx_buff->flags = 0U;
580 			dx_buff->len = buff_size;
581 			dx_buff->pa = frag_pa;
582 			dx_buff->is_mapped = 1U;
583 			dx_buff->eop_index = 0xffffU;
584 
585 			frag_len -= buff_size;
586 			buff_offset += buff_size;
587 
588 			++ret;
589 		}
590 	}
591 
592 	first->eop_index = dx;
593 	dx_buff->is_eop = 1U;
594 	dx_buff->skb = skb;
595 	goto exit;
596 
597 mapping_error:
598 	for (dx = ring->sw_tail;
599 	     ret > 0;
600 	     --ret, dx = aq_ring_next_dx(ring, dx)) {
601 		dx_buff = &ring->buff_ring[dx];
602 
603 		if (!(dx_buff->is_gso_tcp || dx_buff->is_gso_udp) &&
604 		    !dx_buff->is_vlan && dx_buff->pa) {
605 			if (unlikely(dx_buff->is_sop)) {
606 				dma_unmap_single(aq_nic_get_dev(self),
607 						 dx_buff->pa,
608 						 dx_buff->len,
609 						 DMA_TO_DEVICE);
610 			} else {
611 				dma_unmap_page(aq_nic_get_dev(self),
612 					       dx_buff->pa,
613 					       dx_buff->len,
614 					       DMA_TO_DEVICE);
615 			}
616 		}
617 	}
618 
619 exit:
620 	return ret;
621 }
622 
623 int aq_nic_xmit(struct aq_nic_s *self, struct sk_buff *skb)
624 {
625 	unsigned int vec = skb->queue_mapping % self->aq_nic_cfg.vecs;
626 	struct aq_ring_s *ring = NULL;
627 	unsigned int frags = 0U;
628 	int err = NETDEV_TX_OK;
629 	unsigned int tc = 0U;
630 
631 	frags = skb_shinfo(skb)->nr_frags + 1;
632 
633 	ring = self->aq_ring_tx[AQ_NIC_TCVEC2RING(self, tc, vec)];
634 
635 	if (frags > AQ_CFG_SKB_FRAGS_MAX) {
636 		dev_kfree_skb_any(skb);
637 		goto err_exit;
638 	}
639 
640 	aq_ring_update_queue_state(ring);
641 
642 	if (self->aq_nic_cfg.priv_flags & BIT(AQ_HW_LOOPBACK_DMA_NET)) {
643 		err = NETDEV_TX_BUSY;
644 		goto err_exit;
645 	}
646 
647 	/* Above status update may stop the queue. Check this. */
648 	if (__netif_subqueue_stopped(self->ndev, ring->idx)) {
649 		err = NETDEV_TX_BUSY;
650 		goto err_exit;
651 	}
652 
653 	frags = aq_nic_map_skb(self, skb, ring);
654 
655 	if (likely(frags)) {
656 		err = self->aq_hw_ops->hw_ring_tx_xmit(self->aq_hw,
657 						       ring, frags);
658 		if (err >= 0) {
659 			++ring->stats.tx.packets;
660 			ring->stats.tx.bytes += skb->len;
661 		}
662 	} else {
663 		err = NETDEV_TX_BUSY;
664 	}
665 
666 err_exit:
667 	return err;
668 }
669 
670 int aq_nic_update_interrupt_moderation_settings(struct aq_nic_s *self)
671 {
672 	return self->aq_hw_ops->hw_interrupt_moderation_set(self->aq_hw);
673 }
674 
675 int aq_nic_set_packet_filter(struct aq_nic_s *self, unsigned int flags)
676 {
677 	int err = 0;
678 
679 	err = self->aq_hw_ops->hw_packet_filter_set(self->aq_hw, flags);
680 	if (err < 0)
681 		goto err_exit;
682 
683 	self->packet_filter = flags;
684 
685 err_exit:
686 	return err;
687 }
688 
689 int aq_nic_set_multicast_list(struct aq_nic_s *self, struct net_device *ndev)
690 {
691 	const struct aq_hw_ops *hw_ops = self->aq_hw_ops;
692 	struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;
693 	unsigned int packet_filter = ndev->flags;
694 	struct netdev_hw_addr *ha = NULL;
695 	unsigned int i = 0U;
696 	int err = 0;
697 
698 	self->mc_list.count = 0;
699 	if (netdev_uc_count(ndev) > AQ_HW_MULTICAST_ADDRESS_MAX) {
700 		packet_filter |= IFF_PROMISC;
701 	} else {
702 		netdev_for_each_uc_addr(ha, ndev) {
703 			ether_addr_copy(self->mc_list.ar[i++], ha->addr);
704 		}
705 	}
706 
707 	cfg->is_mc_list_enabled = !!(packet_filter & IFF_MULTICAST);
708 	if (cfg->is_mc_list_enabled) {
709 		if (i + netdev_mc_count(ndev) > AQ_HW_MULTICAST_ADDRESS_MAX) {
710 			packet_filter |= IFF_ALLMULTI;
711 		} else {
712 			netdev_for_each_mc_addr(ha, ndev) {
713 				ether_addr_copy(self->mc_list.ar[i++],
714 						ha->addr);
715 			}
716 		}
717 	}
718 
719 	if (i > 0 && i <= AQ_HW_MULTICAST_ADDRESS_MAX) {
720 		self->mc_list.count = i;
721 		err = hw_ops->hw_multicast_list_set(self->aq_hw,
722 						    self->mc_list.ar,
723 						    self->mc_list.count);
724 		if (err < 0)
725 			return err;
726 	}
727 
728 	return aq_nic_set_packet_filter(self, packet_filter);
729 }
730 
731 int aq_nic_set_mtu(struct aq_nic_s *self, int new_mtu)
732 {
733 	self->aq_nic_cfg.mtu = new_mtu;
734 
735 	return 0;
736 }
737 
738 int aq_nic_set_mac(struct aq_nic_s *self, struct net_device *ndev)
739 {
740 	return self->aq_hw_ops->hw_set_mac_address(self->aq_hw, ndev->dev_addr);
741 }
742 
743 unsigned int aq_nic_get_link_speed(struct aq_nic_s *self)
744 {
745 	return self->link_status.mbps;
746 }
747 
748 int aq_nic_get_regs(struct aq_nic_s *self, struct ethtool_regs *regs, void *p)
749 {
750 	u32 *regs_buff = p;
751 	int err = 0;
752 
753 	regs->version = 1;
754 
755 	err = self->aq_hw_ops->hw_get_regs(self->aq_hw,
756 					   self->aq_nic_cfg.aq_hw_caps,
757 					   regs_buff);
758 	if (err < 0)
759 		goto err_exit;
760 
761 err_exit:
762 	return err;
763 }
764 
765 int aq_nic_get_regs_count(struct aq_nic_s *self)
766 {
767 	return self->aq_nic_cfg.aq_hw_caps->mac_regs_count;
768 }
769 
770 void aq_nic_get_stats(struct aq_nic_s *self, u64 *data)
771 {
772 	struct aq_vec_s *aq_vec = NULL;
773 	struct aq_stats_s *stats;
774 	unsigned int count = 0U;
775 	unsigned int i = 0U;
776 
777 	if (self->aq_fw_ops->update_stats) {
778 		mutex_lock(&self->fwreq_mutex);
779 		self->aq_fw_ops->update_stats(self->aq_hw);
780 		mutex_unlock(&self->fwreq_mutex);
781 	}
782 	stats = self->aq_hw_ops->hw_get_hw_stats(self->aq_hw);
783 
784 	if (!stats)
785 		goto err_exit;
786 
787 	data[i] = stats->uprc + stats->mprc + stats->bprc;
788 	data[++i] = stats->uprc;
789 	data[++i] = stats->mprc;
790 	data[++i] = stats->bprc;
791 	data[++i] = stats->erpt;
792 	data[++i] = stats->uptc + stats->mptc + stats->bptc;
793 	data[++i] = stats->uptc;
794 	data[++i] = stats->mptc;
795 	data[++i] = stats->bptc;
796 	data[++i] = stats->ubrc;
797 	data[++i] = stats->ubtc;
798 	data[++i] = stats->mbrc;
799 	data[++i] = stats->mbtc;
800 	data[++i] = stats->bbrc;
801 	data[++i] = stats->bbtc;
802 	data[++i] = stats->ubrc + stats->mbrc + stats->bbrc;
803 	data[++i] = stats->ubtc + stats->mbtc + stats->bbtc;
804 	data[++i] = stats->dma_pkt_rc;
805 	data[++i] = stats->dma_pkt_tc;
806 	data[++i] = stats->dma_oct_rc;
807 	data[++i] = stats->dma_oct_tc;
808 	data[++i] = stats->dpc;
809 
810 	i++;
811 
812 	data += i;
813 
814 	for (i = 0U, aq_vec = self->aq_vec[0];
815 		aq_vec && self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i]) {
816 		data += count;
817 		aq_vec_get_sw_stats(aq_vec, data, &count);
818 	}
819 
820 err_exit:;
821 }
822 
823 static void aq_nic_update_ndev_stats(struct aq_nic_s *self)
824 {
825 	struct aq_stats_s *stats = self->aq_hw_ops->hw_get_hw_stats(self->aq_hw);
826 	struct net_device *ndev = self->ndev;
827 
828 	ndev->stats.rx_packets = stats->dma_pkt_rc;
829 	ndev->stats.rx_bytes = stats->dma_oct_rc;
830 	ndev->stats.rx_errors = stats->erpr;
831 	ndev->stats.rx_dropped = stats->dpc;
832 	ndev->stats.tx_packets = stats->dma_pkt_tc;
833 	ndev->stats.tx_bytes = stats->dma_oct_tc;
834 	ndev->stats.tx_errors = stats->erpt;
835 	ndev->stats.multicast = stats->mprc;
836 }
837 
838 void aq_nic_get_link_ksettings(struct aq_nic_s *self,
839 			       struct ethtool_link_ksettings *cmd)
840 {
841 	if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_FIBRE)
842 		cmd->base.port = PORT_FIBRE;
843 	else
844 		cmd->base.port = PORT_TP;
845 	/* This driver supports only 10G capable adapters, so DUPLEX_FULL */
846 	cmd->base.duplex = DUPLEX_FULL;
847 	cmd->base.autoneg = self->aq_nic_cfg.is_autoneg;
848 
849 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
850 
851 	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_10G)
852 		ethtool_link_ksettings_add_link_mode(cmd, supported,
853 						     10000baseT_Full);
854 
855 	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_5G)
856 		ethtool_link_ksettings_add_link_mode(cmd, supported,
857 						     5000baseT_Full);
858 
859 	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_2GS)
860 		ethtool_link_ksettings_add_link_mode(cmd, supported,
861 						     2500baseT_Full);
862 
863 	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_1G)
864 		ethtool_link_ksettings_add_link_mode(cmd, supported,
865 						     1000baseT_Full);
866 
867 	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_100M)
868 		ethtool_link_ksettings_add_link_mode(cmd, supported,
869 						     100baseT_Full);
870 
871 	if (self->aq_nic_cfg.aq_hw_caps->flow_control) {
872 		ethtool_link_ksettings_add_link_mode(cmd, supported,
873 						     Pause);
874 		ethtool_link_ksettings_add_link_mode(cmd, supported,
875 						     Asym_Pause);
876 	}
877 
878 	ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
879 
880 	if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_FIBRE)
881 		ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
882 	else
883 		ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
884 
885 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
886 
887 	if (self->aq_nic_cfg.is_autoneg)
888 		ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
889 
890 	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_10G)
891 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
892 						     10000baseT_Full);
893 
894 	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_5G)
895 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
896 						     5000baseT_Full);
897 
898 	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_2GS)
899 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
900 						     2500baseT_Full);
901 
902 	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_1G)
903 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
904 						     1000baseT_Full);
905 
906 	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_100M)
907 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
908 						     100baseT_Full);
909 
910 	if (self->aq_nic_cfg.fc.cur & AQ_NIC_FC_RX)
911 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
912 						     Pause);
913 
914 	/* Asym is when either RX or TX, but not both */
915 	if (!!(self->aq_nic_cfg.fc.cur & AQ_NIC_FC_TX) ^
916 	    !!(self->aq_nic_cfg.fc.cur & AQ_NIC_FC_RX))
917 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
918 						     Asym_Pause);
919 
920 	if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_FIBRE)
921 		ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
922 	else
923 		ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
924 }
925 
926 int aq_nic_set_link_ksettings(struct aq_nic_s *self,
927 			      const struct ethtool_link_ksettings *cmd)
928 {
929 	u32 speed = 0U;
930 	u32 rate = 0U;
931 	int err = 0;
932 
933 	if (cmd->base.autoneg == AUTONEG_ENABLE) {
934 		rate = self->aq_nic_cfg.aq_hw_caps->link_speed_msk;
935 		self->aq_nic_cfg.is_autoneg = true;
936 	} else {
937 		speed = cmd->base.speed;
938 
939 		switch (speed) {
940 		case SPEED_100:
941 			rate = AQ_NIC_RATE_100M;
942 			break;
943 
944 		case SPEED_1000:
945 			rate = AQ_NIC_RATE_1G;
946 			break;
947 
948 		case SPEED_2500:
949 			rate = AQ_NIC_RATE_2GS;
950 			break;
951 
952 		case SPEED_5000:
953 			rate = AQ_NIC_RATE_5G;
954 			break;
955 
956 		case SPEED_10000:
957 			rate = AQ_NIC_RATE_10G;
958 			break;
959 
960 		default:
961 			err = -1;
962 			goto err_exit;
963 		break;
964 		}
965 		if (!(self->aq_nic_cfg.aq_hw_caps->link_speed_msk & rate)) {
966 			err = -1;
967 			goto err_exit;
968 		}
969 
970 		self->aq_nic_cfg.is_autoneg = false;
971 	}
972 
973 	mutex_lock(&self->fwreq_mutex);
974 	err = self->aq_fw_ops->set_link_speed(self->aq_hw, rate);
975 	mutex_unlock(&self->fwreq_mutex);
976 	if (err < 0)
977 		goto err_exit;
978 
979 	self->aq_nic_cfg.link_speed_msk = rate;
980 
981 err_exit:
982 	return err;
983 }
984 
985 struct aq_nic_cfg_s *aq_nic_get_cfg(struct aq_nic_s *self)
986 {
987 	return &self->aq_nic_cfg;
988 }
989 
990 u32 aq_nic_get_fw_version(struct aq_nic_s *self)
991 {
992 	u32 fw_version = 0U;
993 
994 	self->aq_hw_ops->hw_get_fw_version(self->aq_hw, &fw_version);
995 
996 	return fw_version;
997 }
998 
999 int aq_nic_set_loopback(struct aq_nic_s *self)
1000 {
1001 	struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;
1002 
1003 	if (!self->aq_hw_ops->hw_set_loopback ||
1004 	    !self->aq_fw_ops->set_phyloopback)
1005 		return -ENOTSUPP;
1006 
1007 	mutex_lock(&self->fwreq_mutex);
1008 	self->aq_hw_ops->hw_set_loopback(self->aq_hw,
1009 					 AQ_HW_LOOPBACK_DMA_SYS,
1010 					 !!(cfg->priv_flags &
1011 					    BIT(AQ_HW_LOOPBACK_DMA_SYS)));
1012 
1013 	self->aq_hw_ops->hw_set_loopback(self->aq_hw,
1014 					 AQ_HW_LOOPBACK_PKT_SYS,
1015 					 !!(cfg->priv_flags &
1016 					    BIT(AQ_HW_LOOPBACK_PKT_SYS)));
1017 
1018 	self->aq_hw_ops->hw_set_loopback(self->aq_hw,
1019 					 AQ_HW_LOOPBACK_DMA_NET,
1020 					 !!(cfg->priv_flags &
1021 					    BIT(AQ_HW_LOOPBACK_DMA_NET)));
1022 
1023 	self->aq_fw_ops->set_phyloopback(self->aq_hw,
1024 					 AQ_HW_LOOPBACK_PHYINT_SYS,
1025 					 !!(cfg->priv_flags &
1026 					    BIT(AQ_HW_LOOPBACK_PHYINT_SYS)));
1027 
1028 	self->aq_fw_ops->set_phyloopback(self->aq_hw,
1029 					 AQ_HW_LOOPBACK_PHYEXT_SYS,
1030 					 !!(cfg->priv_flags &
1031 					    BIT(AQ_HW_LOOPBACK_PHYEXT_SYS)));
1032 	mutex_unlock(&self->fwreq_mutex);
1033 
1034 	return 0;
1035 }
1036 
1037 int aq_nic_stop(struct aq_nic_s *self)
1038 {
1039 	struct aq_vec_s *aq_vec = NULL;
1040 	unsigned int i = 0U;
1041 
1042 	netif_tx_disable(self->ndev);
1043 	netif_carrier_off(self->ndev);
1044 
1045 	del_timer_sync(&self->service_timer);
1046 	cancel_work_sync(&self->service_task);
1047 
1048 	self->aq_hw_ops->hw_irq_disable(self->aq_hw, AQ_CFG_IRQ_MASK);
1049 
1050 	if (self->aq_nic_cfg.is_polling)
1051 		del_timer_sync(&self->polling_timer);
1052 	else
1053 		aq_pci_func_free_irqs(self);
1054 
1055 	aq_ptp_irq_free(self);
1056 
1057 	for (i = 0U, aq_vec = self->aq_vec[0];
1058 		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
1059 		aq_vec_stop(aq_vec);
1060 
1061 	aq_ptp_ring_stop(self);
1062 
1063 	return self->aq_hw_ops->hw_stop(self->aq_hw);
1064 }
1065 
1066 void aq_nic_set_power(struct aq_nic_s *self)
1067 {
1068 	if (self->power_state != AQ_HW_POWER_STATE_D0 ||
1069 	    self->aq_hw->aq_nic_cfg->wol)
1070 		if (likely(self->aq_fw_ops->set_power)) {
1071 			mutex_lock(&self->fwreq_mutex);
1072 			self->aq_fw_ops->set_power(self->aq_hw,
1073 						   self->power_state,
1074 						   self->ndev->dev_addr);
1075 			mutex_unlock(&self->fwreq_mutex);
1076 		}
1077 }
1078 
1079 void aq_nic_deinit(struct aq_nic_s *self, bool link_down)
1080 {
1081 	struct aq_vec_s *aq_vec = NULL;
1082 	unsigned int i = 0U;
1083 
1084 	if (!self)
1085 		goto err_exit;
1086 
1087 	for (i = 0U, aq_vec = self->aq_vec[0];
1088 		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
1089 		aq_vec_deinit(aq_vec);
1090 
1091 	aq_ptp_unregister(self);
1092 	aq_ptp_ring_deinit(self);
1093 	aq_ptp_ring_free(self);
1094 	aq_ptp_free(self);
1095 
1096 	if (likely(self->aq_fw_ops->deinit) && link_down) {
1097 		mutex_lock(&self->fwreq_mutex);
1098 		self->aq_fw_ops->deinit(self->aq_hw);
1099 		mutex_unlock(&self->fwreq_mutex);
1100 	}
1101 
1102 err_exit:;
1103 }
1104 
1105 void aq_nic_free_vectors(struct aq_nic_s *self)
1106 {
1107 	unsigned int i = 0U;
1108 
1109 	if (!self)
1110 		goto err_exit;
1111 
1112 	for (i = ARRAY_SIZE(self->aq_vec); i--;) {
1113 		if (self->aq_vec[i]) {
1114 			aq_vec_free(self->aq_vec[i]);
1115 			self->aq_vec[i] = NULL;
1116 		}
1117 	}
1118 
1119 err_exit:;
1120 }
1121 
1122 void aq_nic_shutdown(struct aq_nic_s *self)
1123 {
1124 	int err = 0;
1125 
1126 	if (!self->ndev)
1127 		return;
1128 
1129 	rtnl_lock();
1130 
1131 	netif_device_detach(self->ndev);
1132 
1133 	if (netif_running(self->ndev)) {
1134 		err = aq_nic_stop(self);
1135 		if (err < 0)
1136 			goto err_exit;
1137 	}
1138 	aq_nic_deinit(self, !self->aq_hw->aq_nic_cfg->wol);
1139 	aq_nic_set_power(self);
1140 
1141 err_exit:
1142 	rtnl_unlock();
1143 }
1144 
1145 u8 aq_nic_reserve_filter(struct aq_nic_s *self, enum aq_rx_filter_type type)
1146 {
1147 	u8 location = 0xFF;
1148 	u32 fltr_cnt;
1149 	u32 n_bit;
1150 
1151 	switch (type) {
1152 	case aq_rx_filter_ethertype:
1153 		location = AQ_RX_LAST_LOC_FETHERT - AQ_RX_FIRST_LOC_FETHERT -
1154 			   self->aq_hw_rx_fltrs.fet_reserved_count;
1155 		self->aq_hw_rx_fltrs.fet_reserved_count++;
1156 		break;
1157 	case aq_rx_filter_l3l4:
1158 		fltr_cnt = AQ_RX_LAST_LOC_FL3L4 - AQ_RX_FIRST_LOC_FL3L4;
1159 		n_bit = fltr_cnt - self->aq_hw_rx_fltrs.fl3l4.reserved_count;
1160 
1161 		self->aq_hw_rx_fltrs.fl3l4.active_ipv4 |= BIT(n_bit);
1162 		self->aq_hw_rx_fltrs.fl3l4.reserved_count++;
1163 		location = n_bit;
1164 		break;
1165 	default:
1166 		break;
1167 	}
1168 
1169 	return location;
1170 }
1171 
1172 void aq_nic_release_filter(struct aq_nic_s *self, enum aq_rx_filter_type type,
1173 			   u32 location)
1174 {
1175 	switch (type) {
1176 	case aq_rx_filter_ethertype:
1177 		self->aq_hw_rx_fltrs.fet_reserved_count--;
1178 		break;
1179 	case aq_rx_filter_l3l4:
1180 		self->aq_hw_rx_fltrs.fl3l4.reserved_count--;
1181 		self->aq_hw_rx_fltrs.fl3l4.active_ipv4 &= ~BIT(location);
1182 		break;
1183 	default:
1184 		break;
1185 	}
1186 }
1187