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