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