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