xref: /linux/drivers/net/ethernet/aquantia/atlantic/aq_nic.c (revision 1f2367a39f17bd553a75e179a747f9b257bc9478)
1 /*
2  * aQuantia Corporation Network Driver
3  * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  */
9 
10 /* File aq_nic.c: Definition of common code for NIC. */
11 
12 #include "aq_nic.h"
13 #include "aq_ring.h"
14 #include "aq_vec.h"
15 #include "aq_hw.h"
16 #include "aq_pci_func.h"
17 
18 #include <linux/moduleparam.h>
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/timer.h>
22 #include <linux/cpu.h>
23 #include <linux/ip.h>
24 #include <linux/tcp.h>
25 #include <net/ip.h>
26 
27 static unsigned int aq_itr = AQ_CFG_INTERRUPT_MODERATION_AUTO;
28 module_param_named(aq_itr, aq_itr, uint, 0644);
29 MODULE_PARM_DESC(aq_itr, "Interrupt throttling mode");
30 
31 static unsigned int aq_itr_tx;
32 module_param_named(aq_itr_tx, aq_itr_tx, uint, 0644);
33 MODULE_PARM_DESC(aq_itr_tx, "TX interrupt throttle rate");
34 
35 static unsigned int aq_itr_rx;
36 module_param_named(aq_itr_rx, aq_itr_rx, uint, 0644);
37 MODULE_PARM_DESC(aq_itr_rx, "RX interrupt throttle rate");
38 
39 static void aq_nic_update_ndev_stats(struct aq_nic_s *self);
40 
41 static void aq_nic_rss_init(struct aq_nic_s *self, unsigned int num_rss_queues)
42 {
43 	struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;
44 	struct aq_rss_parameters *rss_params = &cfg->aq_rss;
45 	int i = 0;
46 
47 	static u8 rss_key[AQ_CFG_RSS_HASHKEY_SIZE] = {
48 		0x1e, 0xad, 0x71, 0x87, 0x65, 0xfc, 0x26, 0x7d,
49 		0x0d, 0x45, 0x67, 0x74, 0xcd, 0x06, 0x1a, 0x18,
50 		0xb6, 0xc1, 0xf0, 0xc7, 0xbb, 0x18, 0xbe, 0xf8,
51 		0x19, 0x13, 0x4b, 0xa9, 0xd0, 0x3e, 0xfe, 0x70,
52 		0x25, 0x03, 0xab, 0x50, 0x6a, 0x8b, 0x82, 0x0c
53 	};
54 
55 	rss_params->hash_secret_key_size = sizeof(rss_key);
56 	memcpy(rss_params->hash_secret_key, rss_key, sizeof(rss_key));
57 	rss_params->indirection_table_size = AQ_CFG_RSS_INDIRECTION_TABLE_MAX;
58 
59 	for (i = rss_params->indirection_table_size; i--;)
60 		rss_params->indirection_table[i] = i & (num_rss_queues - 1);
61 }
62 
63 /* Checks hw_caps and 'corrects' aq_nic_cfg in runtime */
64 void aq_nic_cfg_start(struct aq_nic_s *self)
65 {
66 	struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;
67 
68 	cfg->tcs = AQ_CFG_TCS_DEF;
69 
70 	cfg->is_polling = AQ_CFG_IS_POLLING_DEF;
71 
72 	cfg->itr = aq_itr;
73 	cfg->tx_itr = aq_itr_tx;
74 	cfg->rx_itr = aq_itr_rx;
75 
76 	cfg->is_rss = AQ_CFG_IS_RSS_DEF;
77 	cfg->num_rss_queues = AQ_CFG_NUM_RSS_QUEUES_DEF;
78 	cfg->aq_rss.base_cpu_number = AQ_CFG_RSS_BASE_CPU_NUM_DEF;
79 	cfg->flow_control = AQ_CFG_FC_MODE;
80 
81 	cfg->mtu = AQ_CFG_MTU_DEF;
82 	cfg->link_speed_msk = AQ_CFG_SPEED_MSK;
83 	cfg->is_autoneg = AQ_CFG_IS_AUTONEG_DEF;
84 
85 	cfg->is_lro = AQ_CFG_IS_LRO_DEF;
86 
87 	/*descriptors */
88 	cfg->rxds = min(cfg->aq_hw_caps->rxds_max, AQ_CFG_RXDS_DEF);
89 	cfg->txds = min(cfg->aq_hw_caps->txds_max, AQ_CFG_TXDS_DEF);
90 
91 	/*rss rings */
92 	cfg->vecs = min(cfg->aq_hw_caps->vecs, AQ_CFG_VECS_DEF);
93 	cfg->vecs = min(cfg->vecs, num_online_cpus());
94 	cfg->vecs = min(cfg->vecs, self->irqvecs);
95 	/* cfg->vecs should be power of 2 for RSS */
96 	if (cfg->vecs >= 8U)
97 		cfg->vecs = 8U;
98 	else if (cfg->vecs >= 4U)
99 		cfg->vecs = 4U;
100 	else if (cfg->vecs >= 2U)
101 		cfg->vecs = 2U;
102 	else
103 		cfg->vecs = 1U;
104 
105 	cfg->num_rss_queues = min(cfg->vecs, AQ_CFG_NUM_RSS_QUEUES_DEF);
106 
107 	aq_nic_rss_init(self, cfg->num_rss_queues);
108 
109 	cfg->irq_type = aq_pci_func_get_irq_type(self);
110 
111 	if ((cfg->irq_type == AQ_HW_IRQ_LEGACY) ||
112 	    (cfg->aq_hw_caps->vecs == 1U) ||
113 	    (cfg->vecs == 1U)) {
114 		cfg->is_rss = 0U;
115 		cfg->vecs = 1U;
116 	}
117 
118 	cfg->link_speed_msk &= cfg->aq_hw_caps->link_speed_msk;
119 	cfg->features = cfg->aq_hw_caps->hw_features;
120 }
121 
122 static int aq_nic_update_link_status(struct aq_nic_s *self)
123 {
124 	int err = self->aq_fw_ops->update_link_status(self->aq_hw);
125 	u32 fc = 0;
126 
127 	if (err)
128 		return err;
129 
130 	if (self->link_status.mbps != self->aq_hw->aq_link_status.mbps) {
131 		pr_info("%s: link change old %d new %d\n",
132 			AQ_CFG_DRV_NAME, self->link_status.mbps,
133 			self->aq_hw->aq_link_status.mbps);
134 		aq_nic_update_interrupt_moderation_settings(self);
135 
136 		/* Driver has to update flow control settings on RX block
137 		 * on any link event.
138 		 * We should query FW whether it negotiated FC.
139 		 */
140 		if (self->aq_fw_ops->get_flow_control)
141 			self->aq_fw_ops->get_flow_control(self->aq_hw, &fc);
142 		if (self->aq_hw_ops->hw_set_fc)
143 			self->aq_hw_ops->hw_set_fc(self->aq_hw, fc, 0);
144 	}
145 
146 	self->link_status = self->aq_hw->aq_link_status;
147 	if (!netif_carrier_ok(self->ndev) && self->link_status.mbps) {
148 		aq_utils_obj_set(&self->flags,
149 				 AQ_NIC_FLAG_STARTED);
150 		aq_utils_obj_clear(&self->flags,
151 				   AQ_NIC_LINK_DOWN);
152 		netif_carrier_on(self->ndev);
153 		netif_tx_wake_all_queues(self->ndev);
154 	}
155 	if (netif_carrier_ok(self->ndev) && !self->link_status.mbps) {
156 		netif_carrier_off(self->ndev);
157 		netif_tx_disable(self->ndev);
158 		aq_utils_obj_set(&self->flags, AQ_NIC_LINK_DOWN);
159 	}
160 	return 0;
161 }
162 
163 static void aq_nic_service_timer_cb(struct timer_list *t)
164 {
165 	struct aq_nic_s *self = from_timer(self, t, service_timer);
166 	int ctimer = AQ_CFG_SERVICE_TIMER_INTERVAL;
167 	int err = 0;
168 
169 	if (aq_utils_obj_test(&self->flags, AQ_NIC_FLAGS_IS_NOT_READY))
170 		goto err_exit;
171 
172 	err = aq_nic_update_link_status(self);
173 	if (err)
174 		goto err_exit;
175 
176 	if (self->aq_fw_ops->update_stats)
177 		self->aq_fw_ops->update_stats(self->aq_hw);
178 
179 	aq_nic_update_ndev_stats(self);
180 
181 	/* If no link - use faster timer rate to detect link up asap */
182 	if (!netif_carrier_ok(self->ndev))
183 		ctimer = max(ctimer / 2, 1);
184 
185 err_exit:
186 	mod_timer(&self->service_timer, jiffies + ctimer);
187 }
188 
189 static void aq_nic_polling_timer_cb(struct timer_list *t)
190 {
191 	struct aq_nic_s *self = from_timer(self, t, polling_timer);
192 	struct aq_vec_s *aq_vec = NULL;
193 	unsigned int i = 0U;
194 
195 	for (i = 0U, aq_vec = self->aq_vec[0];
196 		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
197 		aq_vec_isr(i, (void *)aq_vec);
198 
199 	mod_timer(&self->polling_timer, jiffies +
200 		  AQ_CFG_POLLING_TIMER_INTERVAL);
201 }
202 
203 int aq_nic_ndev_register(struct aq_nic_s *self)
204 {
205 	int err = 0;
206 
207 	if (!self->ndev) {
208 		err = -EINVAL;
209 		goto err_exit;
210 	}
211 
212 	err = hw_atl_utils_initfw(self->aq_hw, &self->aq_fw_ops);
213 	if (err)
214 		goto err_exit;
215 
216 	err = self->aq_fw_ops->get_mac_permanent(self->aq_hw,
217 			    self->ndev->dev_addr);
218 	if (err)
219 		goto err_exit;
220 
221 #if defined(AQ_CFG_MAC_ADDR_PERMANENT)
222 	{
223 		static u8 mac_addr_permanent[] = AQ_CFG_MAC_ADDR_PERMANENT;
224 
225 		ether_addr_copy(self->ndev->dev_addr, mac_addr_permanent);
226 	}
227 #endif
228 
229 	for (self->aq_vecs = 0; self->aq_vecs < aq_nic_get_cfg(self)->vecs;
230 	     self->aq_vecs++) {
231 		self->aq_vec[self->aq_vecs] =
232 		    aq_vec_alloc(self, self->aq_vecs, aq_nic_get_cfg(self));
233 		if (!self->aq_vec[self->aq_vecs]) {
234 			err = -ENOMEM;
235 			goto err_exit;
236 		}
237 	}
238 
239 	netif_carrier_off(self->ndev);
240 
241 	netif_tx_disable(self->ndev);
242 
243 	err = register_netdev(self->ndev);
244 	if (err)
245 		goto err_exit;
246 
247 err_exit:
248 	return err;
249 }
250 
251 void aq_nic_ndev_init(struct aq_nic_s *self)
252 {
253 	const struct aq_hw_caps_s *aq_hw_caps = self->aq_nic_cfg.aq_hw_caps;
254 	struct aq_nic_cfg_s *aq_nic_cfg = &self->aq_nic_cfg;
255 
256 	self->ndev->hw_features |= aq_hw_caps->hw_features;
257 	self->ndev->features = aq_hw_caps->hw_features;
258 	self->ndev->vlan_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
259 				     NETIF_F_RXHASH | NETIF_F_SG | NETIF_F_LRO;
260 	self->ndev->priv_flags = aq_hw_caps->hw_priv_flags;
261 	self->ndev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
262 
263 	self->ndev->mtu = aq_nic_cfg->mtu - ETH_HLEN;
264 	self->ndev->max_mtu = aq_hw_caps->mtu - ETH_FCS_LEN - ETH_HLEN;
265 
266 }
267 
268 void aq_nic_set_tx_ring(struct aq_nic_s *self, unsigned int idx,
269 			struct aq_ring_s *ring)
270 {
271 	self->aq_ring_tx[idx] = ring;
272 }
273 
274 struct net_device *aq_nic_get_ndev(struct aq_nic_s *self)
275 {
276 	return self->ndev;
277 }
278 
279 int aq_nic_init(struct aq_nic_s *self)
280 {
281 	struct aq_vec_s *aq_vec = NULL;
282 	int err = 0;
283 	unsigned int i = 0U;
284 
285 	self->power_state = AQ_HW_POWER_STATE_D0;
286 	err = self->aq_hw_ops->hw_reset(self->aq_hw);
287 	if (err < 0)
288 		goto err_exit;
289 
290 	err = self->aq_hw_ops->hw_init(self->aq_hw,
291 				       aq_nic_get_ndev(self)->dev_addr);
292 	if (err < 0)
293 		goto err_exit;
294 
295 	for (i = 0U, aq_vec = self->aq_vec[0];
296 		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
297 		aq_vec_init(aq_vec, self->aq_hw_ops, self->aq_hw);
298 
299 	netif_carrier_off(self->ndev);
300 
301 err_exit:
302 	return err;
303 }
304 
305 int aq_nic_start(struct aq_nic_s *self)
306 {
307 	struct aq_vec_s *aq_vec = NULL;
308 	int err = 0;
309 	unsigned int i = 0U;
310 
311 	err = self->aq_hw_ops->hw_multicast_list_set(self->aq_hw,
312 						     self->mc_list.ar,
313 						     self->mc_list.count);
314 	if (err < 0)
315 		goto err_exit;
316 
317 	err = self->aq_hw_ops->hw_packet_filter_set(self->aq_hw,
318 						    self->packet_filter);
319 	if (err < 0)
320 		goto err_exit;
321 
322 	for (i = 0U, aq_vec = self->aq_vec[0];
323 		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i]) {
324 		err = aq_vec_start(aq_vec);
325 		if (err < 0)
326 			goto err_exit;
327 	}
328 
329 	err = self->aq_hw_ops->hw_start(self->aq_hw);
330 	if (err < 0)
331 		goto err_exit;
332 
333 	err = aq_nic_update_interrupt_moderation_settings(self);
334 	if (err)
335 		goto err_exit;
336 	timer_setup(&self->service_timer, aq_nic_service_timer_cb, 0);
337 	mod_timer(&self->service_timer, jiffies +
338 		  AQ_CFG_SERVICE_TIMER_INTERVAL);
339 
340 	if (self->aq_nic_cfg.is_polling) {
341 		timer_setup(&self->polling_timer, aq_nic_polling_timer_cb, 0);
342 		mod_timer(&self->polling_timer, jiffies +
343 			  AQ_CFG_POLLING_TIMER_INTERVAL);
344 	} else {
345 		for (i = 0U, aq_vec = self->aq_vec[0];
346 			self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i]) {
347 			err = aq_pci_func_alloc_irq(self, i,
348 						    self->ndev->name, aq_vec,
349 						    aq_vec_get_affinity_mask(aq_vec));
350 			if (err < 0)
351 				goto err_exit;
352 		}
353 
354 		err = self->aq_hw_ops->hw_irq_enable(self->aq_hw,
355 						     AQ_CFG_IRQ_MASK);
356 		if (err < 0)
357 			goto err_exit;
358 	}
359 
360 	err = netif_set_real_num_tx_queues(self->ndev, self->aq_vecs);
361 	if (err < 0)
362 		goto err_exit;
363 
364 	err = netif_set_real_num_rx_queues(self->ndev, self->aq_vecs);
365 	if (err < 0)
366 		goto err_exit;
367 
368 	netif_tx_start_all_queues(self->ndev);
369 
370 err_exit:
371 	return err;
372 }
373 
374 static unsigned int aq_nic_map_skb(struct aq_nic_s *self,
375 				   struct sk_buff *skb,
376 				   struct aq_ring_s *ring)
377 {
378 	unsigned int ret = 0U;
379 	unsigned int nr_frags = skb_shinfo(skb)->nr_frags;
380 	unsigned int frag_count = 0U;
381 	unsigned int dx = ring->sw_tail;
382 	struct aq_ring_buff_s *first = NULL;
383 	struct aq_ring_buff_s *dx_buff = &ring->buff_ring[dx];
384 
385 	if (unlikely(skb_is_gso(skb))) {
386 		dx_buff->flags = 0U;
387 		dx_buff->len_pkt = skb->len;
388 		dx_buff->len_l2 = ETH_HLEN;
389 		dx_buff->len_l3 = ip_hdrlen(skb);
390 		dx_buff->len_l4 = tcp_hdrlen(skb);
391 		dx_buff->mss = skb_shinfo(skb)->gso_size;
392 		dx_buff->is_txc = 1U;
393 		dx_buff->eop_index = 0xffffU;
394 
395 		dx_buff->is_ipv6 =
396 			(ip_hdr(skb)->version == 6) ? 1U : 0U;
397 
398 		dx = aq_ring_next_dx(ring, dx);
399 		dx_buff = &ring->buff_ring[dx];
400 		++ret;
401 	}
402 
403 	dx_buff->flags = 0U;
404 	dx_buff->len = skb_headlen(skb);
405 	dx_buff->pa = dma_map_single(aq_nic_get_dev(self),
406 				     skb->data,
407 				     dx_buff->len,
408 				     DMA_TO_DEVICE);
409 
410 	if (unlikely(dma_mapping_error(aq_nic_get_dev(self), dx_buff->pa)))
411 		goto exit;
412 
413 	first = dx_buff;
414 	dx_buff->len_pkt = skb->len;
415 	dx_buff->is_sop = 1U;
416 	dx_buff->is_mapped = 1U;
417 	++ret;
418 
419 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
420 		dx_buff->is_ip_cso = (htons(ETH_P_IP) == skb->protocol) ?
421 			1U : 0U;
422 
423 		if (ip_hdr(skb)->version == 4) {
424 			dx_buff->is_tcp_cso =
425 				(ip_hdr(skb)->protocol == IPPROTO_TCP) ?
426 					1U : 0U;
427 			dx_buff->is_udp_cso =
428 				(ip_hdr(skb)->protocol == IPPROTO_UDP) ?
429 					1U : 0U;
430 		} else if (ip_hdr(skb)->version == 6) {
431 			dx_buff->is_tcp_cso =
432 				(ipv6_hdr(skb)->nexthdr == NEXTHDR_TCP) ?
433 					1U : 0U;
434 			dx_buff->is_udp_cso =
435 				(ipv6_hdr(skb)->nexthdr == NEXTHDR_UDP) ?
436 					1U : 0U;
437 		}
438 	}
439 
440 	for (; nr_frags--; ++frag_count) {
441 		unsigned int frag_len = 0U;
442 		unsigned int buff_offset = 0U;
443 		unsigned int buff_size = 0U;
444 		dma_addr_t frag_pa;
445 		skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_count];
446 
447 		frag_len = skb_frag_size(frag);
448 
449 		while (frag_len) {
450 			if (frag_len > AQ_CFG_TX_FRAME_MAX)
451 				buff_size = AQ_CFG_TX_FRAME_MAX;
452 			else
453 				buff_size = frag_len;
454 
455 			frag_pa = skb_frag_dma_map(aq_nic_get_dev(self),
456 						   frag,
457 						   buff_offset,
458 						   buff_size,
459 						   DMA_TO_DEVICE);
460 
461 			if (unlikely(dma_mapping_error(aq_nic_get_dev(self),
462 						       frag_pa)))
463 				goto mapping_error;
464 
465 			dx = aq_ring_next_dx(ring, dx);
466 			dx_buff = &ring->buff_ring[dx];
467 
468 			dx_buff->flags = 0U;
469 			dx_buff->len = buff_size;
470 			dx_buff->pa = frag_pa;
471 			dx_buff->is_mapped = 1U;
472 			dx_buff->eop_index = 0xffffU;
473 
474 			frag_len -= buff_size;
475 			buff_offset += buff_size;
476 
477 			++ret;
478 		}
479 	}
480 
481 	first->eop_index = dx;
482 	dx_buff->is_eop = 1U;
483 	dx_buff->skb = skb;
484 	goto exit;
485 
486 mapping_error:
487 	for (dx = ring->sw_tail;
488 	     ret > 0;
489 	     --ret, dx = aq_ring_next_dx(ring, dx)) {
490 		dx_buff = &ring->buff_ring[dx];
491 
492 		if (!dx_buff->is_txc && dx_buff->pa) {
493 			if (unlikely(dx_buff->is_sop)) {
494 				dma_unmap_single(aq_nic_get_dev(self),
495 						 dx_buff->pa,
496 						 dx_buff->len,
497 						 DMA_TO_DEVICE);
498 			} else {
499 				dma_unmap_page(aq_nic_get_dev(self),
500 					       dx_buff->pa,
501 					       dx_buff->len,
502 					       DMA_TO_DEVICE);
503 			}
504 		}
505 	}
506 
507 exit:
508 	return ret;
509 }
510 
511 int aq_nic_xmit(struct aq_nic_s *self, struct sk_buff *skb)
512 {
513 	struct aq_ring_s *ring = NULL;
514 	unsigned int frags = 0U;
515 	unsigned int vec = skb->queue_mapping % self->aq_nic_cfg.vecs;
516 	unsigned int tc = 0U;
517 	int err = NETDEV_TX_OK;
518 
519 	frags = skb_shinfo(skb)->nr_frags + 1;
520 
521 	ring = self->aq_ring_tx[AQ_NIC_TCVEC2RING(self, tc, vec)];
522 
523 	if (frags > AQ_CFG_SKB_FRAGS_MAX) {
524 		dev_kfree_skb_any(skb);
525 		goto err_exit;
526 	}
527 
528 	aq_ring_update_queue_state(ring);
529 
530 	/* Above status update may stop the queue. Check this. */
531 	if (__netif_subqueue_stopped(self->ndev, ring->idx)) {
532 		err = NETDEV_TX_BUSY;
533 		goto err_exit;
534 	}
535 
536 	frags = aq_nic_map_skb(self, skb, ring);
537 
538 	if (likely(frags)) {
539 		err = self->aq_hw_ops->hw_ring_tx_xmit(self->aq_hw,
540 						       ring, frags);
541 		if (err >= 0) {
542 			++ring->stats.tx.packets;
543 			ring->stats.tx.bytes += skb->len;
544 		}
545 	} else {
546 		err = NETDEV_TX_BUSY;
547 	}
548 
549 err_exit:
550 	return err;
551 }
552 
553 int aq_nic_update_interrupt_moderation_settings(struct aq_nic_s *self)
554 {
555 	return self->aq_hw_ops->hw_interrupt_moderation_set(self->aq_hw);
556 }
557 
558 int aq_nic_set_packet_filter(struct aq_nic_s *self, unsigned int flags)
559 {
560 	int err = 0;
561 
562 	err = self->aq_hw_ops->hw_packet_filter_set(self->aq_hw, flags);
563 	if (err < 0)
564 		goto err_exit;
565 
566 	self->packet_filter = flags;
567 
568 err_exit:
569 	return err;
570 }
571 
572 int aq_nic_set_multicast_list(struct aq_nic_s *self, struct net_device *ndev)
573 {
574 	unsigned int packet_filter = self->packet_filter;
575 	struct netdev_hw_addr *ha = NULL;
576 	unsigned int i = 0U;
577 
578 	self->mc_list.count = 0;
579 	if (netdev_uc_count(ndev) > AQ_HW_MULTICAST_ADDRESS_MAX) {
580 		packet_filter |= IFF_PROMISC;
581 	} else {
582 		netdev_for_each_uc_addr(ha, ndev) {
583 			ether_addr_copy(self->mc_list.ar[i++], ha->addr);
584 
585 			if (i >= AQ_HW_MULTICAST_ADDRESS_MAX)
586 				break;
587 		}
588 	}
589 
590 	if (i + netdev_mc_count(ndev) > AQ_HW_MULTICAST_ADDRESS_MAX) {
591 		packet_filter |= IFF_ALLMULTI;
592 	} else {
593 		netdev_for_each_mc_addr(ha, ndev) {
594 			ether_addr_copy(self->mc_list.ar[i++], ha->addr);
595 
596 			if (i >= AQ_HW_MULTICAST_ADDRESS_MAX)
597 				break;
598 		}
599 	}
600 
601 	if (i > 0 && i <= AQ_HW_MULTICAST_ADDRESS_MAX) {
602 		packet_filter |= IFF_MULTICAST;
603 		self->mc_list.count = i;
604 		self->aq_hw_ops->hw_multicast_list_set(self->aq_hw,
605 						       self->mc_list.ar,
606 						       self->mc_list.count);
607 	}
608 	return aq_nic_set_packet_filter(self, packet_filter);
609 }
610 
611 int aq_nic_set_mtu(struct aq_nic_s *self, int new_mtu)
612 {
613 	self->aq_nic_cfg.mtu = new_mtu;
614 
615 	return 0;
616 }
617 
618 int aq_nic_set_mac(struct aq_nic_s *self, struct net_device *ndev)
619 {
620 	return self->aq_hw_ops->hw_set_mac_address(self->aq_hw, ndev->dev_addr);
621 }
622 
623 unsigned int aq_nic_get_link_speed(struct aq_nic_s *self)
624 {
625 	return self->link_status.mbps;
626 }
627 
628 int aq_nic_get_regs(struct aq_nic_s *self, struct ethtool_regs *regs, void *p)
629 {
630 	u32 *regs_buff = p;
631 	int err = 0;
632 
633 	regs->version = 1;
634 
635 	err = self->aq_hw_ops->hw_get_regs(self->aq_hw,
636 					   self->aq_nic_cfg.aq_hw_caps,
637 					   regs_buff);
638 	if (err < 0)
639 		goto err_exit;
640 
641 err_exit:
642 	return err;
643 }
644 
645 int aq_nic_get_regs_count(struct aq_nic_s *self)
646 {
647 	return self->aq_nic_cfg.aq_hw_caps->mac_regs_count;
648 }
649 
650 void aq_nic_get_stats(struct aq_nic_s *self, u64 *data)
651 {
652 	unsigned int i = 0U;
653 	unsigned int count = 0U;
654 	struct aq_vec_s *aq_vec = NULL;
655 	struct aq_stats_s *stats = self->aq_hw_ops->hw_get_hw_stats(self->aq_hw);
656 
657 	if (!stats)
658 		goto err_exit;
659 
660 	data[i] = stats->uprc + stats->mprc + stats->bprc;
661 	data[++i] = stats->uprc;
662 	data[++i] = stats->mprc;
663 	data[++i] = stats->bprc;
664 	data[++i] = stats->erpt;
665 	data[++i] = stats->uptc + stats->mptc + stats->bptc;
666 	data[++i] = stats->uptc;
667 	data[++i] = stats->mptc;
668 	data[++i] = stats->bptc;
669 	data[++i] = stats->ubrc;
670 	data[++i] = stats->ubtc;
671 	data[++i] = stats->mbrc;
672 	data[++i] = stats->mbtc;
673 	data[++i] = stats->bbrc;
674 	data[++i] = stats->bbtc;
675 	data[++i] = stats->ubrc + stats->mbrc + stats->bbrc;
676 	data[++i] = stats->ubtc + stats->mbtc + stats->bbtc;
677 	data[++i] = stats->dma_pkt_rc;
678 	data[++i] = stats->dma_pkt_tc;
679 	data[++i] = stats->dma_oct_rc;
680 	data[++i] = stats->dma_oct_tc;
681 	data[++i] = stats->dpc;
682 
683 	i++;
684 
685 	data += i;
686 
687 	for (i = 0U, aq_vec = self->aq_vec[0];
688 		aq_vec && self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i]) {
689 		data += count;
690 		aq_vec_get_sw_stats(aq_vec, data, &count);
691 	}
692 
693 err_exit:;
694 }
695 
696 static void aq_nic_update_ndev_stats(struct aq_nic_s *self)
697 {
698 	struct net_device *ndev = self->ndev;
699 	struct aq_stats_s *stats = self->aq_hw_ops->hw_get_hw_stats(self->aq_hw);
700 
701 	ndev->stats.rx_packets = stats->uprc + stats->mprc + stats->bprc;
702 	ndev->stats.rx_bytes = stats->ubrc + stats->mbrc + stats->bbrc;
703 	ndev->stats.rx_errors = stats->erpr;
704 	ndev->stats.tx_packets = stats->uptc + stats->mptc + stats->bptc;
705 	ndev->stats.tx_bytes = stats->ubtc + stats->mbtc + stats->bbtc;
706 	ndev->stats.tx_errors = stats->erpt;
707 	ndev->stats.multicast = stats->mprc;
708 }
709 
710 void aq_nic_get_link_ksettings(struct aq_nic_s *self,
711 			       struct ethtool_link_ksettings *cmd)
712 {
713 	if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_FIBRE)
714 		cmd->base.port = PORT_FIBRE;
715 	else
716 		cmd->base.port = PORT_TP;
717 	/* This driver supports only 10G capable adapters, so DUPLEX_FULL */
718 	cmd->base.duplex = DUPLEX_FULL;
719 	cmd->base.autoneg = self->aq_nic_cfg.is_autoneg;
720 
721 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
722 
723 	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_10G)
724 		ethtool_link_ksettings_add_link_mode(cmd, supported,
725 						     10000baseT_Full);
726 
727 	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_5G)
728 		ethtool_link_ksettings_add_link_mode(cmd, supported,
729 						     5000baseT_Full);
730 
731 	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_2GS)
732 		ethtool_link_ksettings_add_link_mode(cmd, supported,
733 						     2500baseT_Full);
734 
735 	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_1G)
736 		ethtool_link_ksettings_add_link_mode(cmd, supported,
737 						     1000baseT_Full);
738 
739 	if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_100M)
740 		ethtool_link_ksettings_add_link_mode(cmd, supported,
741 						     100baseT_Full);
742 
743 	if (self->aq_nic_cfg.aq_hw_caps->flow_control)
744 		ethtool_link_ksettings_add_link_mode(cmd, supported,
745 						     Pause);
746 
747 	ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
748 
749 	if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_FIBRE)
750 		ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
751 	else
752 		ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
753 
754 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
755 
756 	if (self->aq_nic_cfg.is_autoneg)
757 		ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
758 
759 	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_10G)
760 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
761 						     10000baseT_Full);
762 
763 	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_5G)
764 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
765 						     5000baseT_Full);
766 
767 	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_2GS)
768 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
769 						     2500baseT_Full);
770 
771 	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_1G)
772 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
773 						     1000baseT_Full);
774 
775 	if (self->aq_nic_cfg.link_speed_msk  & AQ_NIC_RATE_100M)
776 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
777 						     100baseT_Full);
778 
779 	if (self->aq_nic_cfg.flow_control & AQ_NIC_FC_RX)
780 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
781 						     Pause);
782 
783 	/* Asym is when either RX or TX, but not both */
784 	if (!!(self->aq_nic_cfg.flow_control & AQ_NIC_FC_TX) ^
785 	    !!(self->aq_nic_cfg.flow_control & AQ_NIC_FC_RX))
786 		ethtool_link_ksettings_add_link_mode(cmd, advertising,
787 						     Asym_Pause);
788 
789 	if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_FIBRE)
790 		ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
791 	else
792 		ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
793 }
794 
795 int aq_nic_set_link_ksettings(struct aq_nic_s *self,
796 			      const struct ethtool_link_ksettings *cmd)
797 {
798 	u32 speed = 0U;
799 	u32 rate = 0U;
800 	int err = 0;
801 
802 	if (cmd->base.autoneg == AUTONEG_ENABLE) {
803 		rate = self->aq_nic_cfg.aq_hw_caps->link_speed_msk;
804 		self->aq_nic_cfg.is_autoneg = true;
805 	} else {
806 		speed = cmd->base.speed;
807 
808 		switch (speed) {
809 		case SPEED_100:
810 			rate = AQ_NIC_RATE_100M;
811 			break;
812 
813 		case SPEED_1000:
814 			rate = AQ_NIC_RATE_1G;
815 			break;
816 
817 		case SPEED_2500:
818 			rate = AQ_NIC_RATE_2GS;
819 			break;
820 
821 		case SPEED_5000:
822 			rate = AQ_NIC_RATE_5G;
823 			break;
824 
825 		case SPEED_10000:
826 			rate = AQ_NIC_RATE_10G;
827 			break;
828 
829 		default:
830 			err = -1;
831 			goto err_exit;
832 		break;
833 		}
834 		if (!(self->aq_nic_cfg.aq_hw_caps->link_speed_msk & rate)) {
835 			err = -1;
836 			goto err_exit;
837 		}
838 
839 		self->aq_nic_cfg.is_autoneg = false;
840 	}
841 
842 	err = self->aq_fw_ops->set_link_speed(self->aq_hw, rate);
843 	if (err < 0)
844 		goto err_exit;
845 
846 	self->aq_nic_cfg.link_speed_msk = rate;
847 
848 err_exit:
849 	return err;
850 }
851 
852 struct aq_nic_cfg_s *aq_nic_get_cfg(struct aq_nic_s *self)
853 {
854 	return &self->aq_nic_cfg;
855 }
856 
857 u32 aq_nic_get_fw_version(struct aq_nic_s *self)
858 {
859 	u32 fw_version = 0U;
860 
861 	self->aq_hw_ops->hw_get_fw_version(self->aq_hw, &fw_version);
862 
863 	return fw_version;
864 }
865 
866 int aq_nic_stop(struct aq_nic_s *self)
867 {
868 	struct aq_vec_s *aq_vec = NULL;
869 	unsigned int i = 0U;
870 
871 	netif_tx_disable(self->ndev);
872 	netif_carrier_off(self->ndev);
873 
874 	del_timer_sync(&self->service_timer);
875 
876 	self->aq_hw_ops->hw_irq_disable(self->aq_hw, AQ_CFG_IRQ_MASK);
877 
878 	if (self->aq_nic_cfg.is_polling)
879 		del_timer_sync(&self->polling_timer);
880 	else
881 		aq_pci_func_free_irqs(self);
882 
883 	for (i = 0U, aq_vec = self->aq_vec[0];
884 		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
885 		aq_vec_stop(aq_vec);
886 
887 	return self->aq_hw_ops->hw_stop(self->aq_hw);
888 }
889 
890 void aq_nic_deinit(struct aq_nic_s *self)
891 {
892 	struct aq_vec_s *aq_vec = NULL;
893 	unsigned int i = 0U;
894 
895 	if (!self)
896 		goto err_exit;
897 
898 	for (i = 0U, aq_vec = self->aq_vec[0];
899 		self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
900 		aq_vec_deinit(aq_vec);
901 
902 	self->aq_fw_ops->deinit(self->aq_hw);
903 
904 	if (self->power_state != AQ_HW_POWER_STATE_D0 ||
905 	    self->aq_hw->aq_nic_cfg->wol) {
906 		self->aq_fw_ops->set_power(self->aq_hw,
907 					   self->power_state,
908 					   self->ndev->dev_addr);
909 	}
910 
911 err_exit:;
912 }
913 
914 void aq_nic_free_vectors(struct aq_nic_s *self)
915 {
916 	unsigned int i = 0U;
917 
918 	if (!self)
919 		goto err_exit;
920 
921 	for (i = ARRAY_SIZE(self->aq_vec); i--;) {
922 		if (self->aq_vec[i]) {
923 			aq_vec_free(self->aq_vec[i]);
924 			self->aq_vec[i] = NULL;
925 		}
926 	}
927 
928 err_exit:;
929 }
930 
931 int aq_nic_change_pm_state(struct aq_nic_s *self, pm_message_t *pm_msg)
932 {
933 	int err = 0;
934 
935 	if (!netif_running(self->ndev)) {
936 		err = 0;
937 		goto out;
938 	}
939 	rtnl_lock();
940 	if (pm_msg->event & PM_EVENT_SLEEP || pm_msg->event & PM_EVENT_FREEZE) {
941 		self->power_state = AQ_HW_POWER_STATE_D3;
942 		netif_device_detach(self->ndev);
943 		netif_tx_stop_all_queues(self->ndev);
944 
945 		err = aq_nic_stop(self);
946 		if (err < 0)
947 			goto err_exit;
948 
949 		aq_nic_deinit(self);
950 	} else {
951 		err = aq_nic_init(self);
952 		if (err < 0)
953 			goto err_exit;
954 
955 		err = aq_nic_start(self);
956 		if (err < 0)
957 			goto err_exit;
958 
959 		netif_device_attach(self->ndev);
960 		netif_tx_start_all_queues(self->ndev);
961 	}
962 
963 err_exit:
964 	rtnl_unlock();
965 out:
966 	return err;
967 }
968 
969 void aq_nic_shutdown(struct aq_nic_s *self)
970 {
971 	int err = 0;
972 
973 	if (!self->ndev)
974 		return;
975 
976 	rtnl_lock();
977 
978 	netif_device_detach(self->ndev);
979 
980 	if (netif_running(self->ndev)) {
981 		err = aq_nic_stop(self);
982 		if (err < 0)
983 			goto err_exit;
984 	}
985 	aq_nic_deinit(self);
986 
987 err_exit:
988 	rtnl_unlock();
989 }
990