Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / ethernet / aquantia / atlantic / aq_nic.c
blobadac5df0d6b47e159d7bf5fe78453b7af0a505cb
1 /*
2 * aQuantia Corporation Network Driver
3 * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
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 */
10 /* File aq_nic.c: Definition of common code for NIC. */
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"
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>
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");
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");
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");
39 static void aq_nic_update_ndev_stats(struct aq_nic_s *self);
41 static void aq_nic_rss_init(struct aq_nic_s *self, unsigned int num_rss_queues)
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;
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
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;
59 for (i = rss_params->indirection_table_size; i--;)
60 rss_params->indirection_table[i] = i & (num_rss_queues - 1);
63 /* Checks hw_caps and 'corrects' aq_nic_cfg in runtime */
64 void aq_nic_cfg_start(struct aq_nic_s *self)
66 struct aq_nic_cfg_s *cfg = &self->aq_nic_cfg;
68 cfg->tcs = AQ_CFG_TCS_DEF;
70 cfg->is_polling = AQ_CFG_IS_POLLING_DEF;
72 cfg->itr = aq_itr;
73 cfg->tx_itr = aq_itr_tx;
74 cfg->rx_itr = aq_itr_rx;
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;
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;
85 cfg->is_lro = AQ_CFG_IS_LRO_DEF;
87 cfg->vlan_id = 0U;
89 aq_nic_rss_init(self, cfg->num_rss_queues);
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);
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;
109 cfg->num_rss_queues = min(cfg->vecs, AQ_CFG_NUM_RSS_QUEUES_DEF);
111 cfg->irq_type = aq_pci_func_get_irq_type(self);
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;
120 cfg->link_speed_msk &= cfg->aq_hw_caps->link_speed_msk;
121 cfg->hw_features = cfg->aq_hw_caps->hw_features;
124 static int aq_nic_update_link_status(struct aq_nic_s *self)
126 int err = self->aq_fw_ops->update_link_status(self->aq_hw);
128 if (err)
129 return err;
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);
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);
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);
152 return 0;
155 static void aq_nic_service_timer_cb(struct timer_list *t)
157 struct aq_nic_s *self = from_timer(self, t, service_timer);
158 int ctimer = AQ_CFG_SERVICE_TIMER_INTERVAL;
159 int err = 0;
161 if (aq_utils_obj_test(&self->flags, AQ_NIC_FLAGS_IS_NOT_READY))
162 goto err_exit;
164 err = aq_nic_update_link_status(self);
165 if (err)
166 goto err_exit;
168 if (self->aq_fw_ops->update_stats)
169 self->aq_fw_ops->update_stats(self->aq_hw);
171 aq_nic_update_ndev_stats(self);
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);
177 err_exit:
178 mod_timer(&self->service_timer, jiffies + ctimer);
181 static void aq_nic_polling_timer_cb(struct timer_list *t)
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;
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);
191 mod_timer(&self->polling_timer, jiffies +
192 AQ_CFG_POLLING_TIMER_INTERVAL);
195 int aq_nic_ndev_register(struct aq_nic_s *self)
197 int err = 0;
199 if (!self->ndev) {
200 err = -EINVAL;
201 goto err_exit;
204 err = hw_atl_utils_initfw(self->aq_hw, &self->aq_fw_ops);
205 if (err)
206 goto err_exit;
208 err = self->aq_fw_ops->get_mac_permanent(self->aq_hw,
209 self->ndev->dev_addr);
210 if (err)
211 goto err_exit;
213 #if defined(AQ_CFG_MAC_ADDR_PERMANENT)
215 static u8 mac_addr_permanent[] = AQ_CFG_MAC_ADDR_PERMANENT;
217 ether_addr_copy(self->ndev->dev_addr, mac_addr_permanent);
219 #endif
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;
231 netif_carrier_off(self->ndev);
233 netif_tx_disable(self->ndev);
235 err = register_netdev(self->ndev);
236 if (err)
237 goto err_exit;
239 err_exit:
240 return err;
243 void aq_nic_ndev_init(struct aq_nic_s *self)
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;
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;
255 self->ndev->mtu = aq_nic_cfg->mtu - ETH_HLEN;
256 self->ndev->max_mtu = aq_hw_caps->mtu - ETH_FCS_LEN - ETH_HLEN;
260 void aq_nic_set_tx_ring(struct aq_nic_s *self, unsigned int idx,
261 struct aq_ring_s *ring)
263 self->aq_ring_tx[idx] = ring;
266 struct net_device *aq_nic_get_ndev(struct aq_nic_s *self)
268 return self->ndev;
271 int aq_nic_init(struct aq_nic_s *self)
273 struct aq_vec_s *aq_vec = NULL;
274 int err = 0;
275 unsigned int i = 0U;
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;
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;
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);
291 netif_carrier_off(self->ndev);
293 err_exit:
294 return err;
297 int aq_nic_start(struct aq_nic_s *self)
299 struct aq_vec_s *aq_vec = NULL;
300 int err = 0;
301 unsigned int i = 0U;
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;
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;
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;
321 err = self->aq_hw_ops->hw_start(self->aq_hw);
322 if (err < 0)
323 goto err_exit;
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);
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;
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;
352 err = netif_set_real_num_tx_queues(self->ndev, self->aq_vecs);
353 if (err < 0)
354 goto err_exit;
356 err = netif_set_real_num_rx_queues(self->ndev, self->aq_vecs);
357 if (err < 0)
358 goto err_exit;
360 netif_tx_start_all_queues(self->ndev);
362 err_exit:
363 return err;
366 static unsigned int aq_nic_map_skb(struct aq_nic_s *self,
367 struct sk_buff *skb,
368 struct aq_ring_s *ring)
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];
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;
387 dx_buff->is_ipv6 =
388 (ip_hdr(skb)->version == 6) ? 1U : 0U;
390 dx = aq_ring_next_dx(ring, dx);
391 dx_buff = &ring->buff_ring[dx];
392 ++ret;
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);
402 if (unlikely(dma_mapping_error(aq_nic_get_dev(self), dx_buff->pa))) {
403 ret = 0;
404 goto exit;
407 first = dx_buff;
408 dx_buff->len_pkt = skb->len;
409 dx_buff->is_sop = 1U;
410 dx_buff->is_mapped = 1U;
411 ++ret;
413 if (skb->ip_summed == CHECKSUM_PARTIAL) {
414 dx_buff->is_ip_cso = (htons(ETH_P_IP) == skb->protocol) ?
415 1U : 0U;
417 if (ip_hdr(skb)->version == 4) {
418 dx_buff->is_tcp_cso =
419 (ip_hdr(skb)->protocol == IPPROTO_TCP) ?
420 1U : 0U;
421 dx_buff->is_udp_cso =
422 (ip_hdr(skb)->protocol == IPPROTO_UDP) ?
423 1U : 0U;
424 } else if (ip_hdr(skb)->version == 6) {
425 dx_buff->is_tcp_cso =
426 (ipv6_hdr(skb)->nexthdr == NEXTHDR_TCP) ?
427 1U : 0U;
428 dx_buff->is_udp_cso =
429 (ipv6_hdr(skb)->nexthdr == NEXTHDR_UDP) ?
430 1U : 0U;
434 for (; nr_frags--; ++frag_count) {
435 unsigned int frag_len = 0U;
436 unsigned int buff_offset = 0U;
437 unsigned int buff_size = 0U;
438 dma_addr_t frag_pa;
439 skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_count];
441 frag_len = skb_frag_size(frag);
443 while (frag_len) {
444 if (frag_len > AQ_CFG_TX_FRAME_MAX)
445 buff_size = AQ_CFG_TX_FRAME_MAX;
446 else
447 buff_size = frag_len;
449 frag_pa = skb_frag_dma_map(aq_nic_get_dev(self),
450 frag,
451 buff_offset,
452 buff_size,
453 DMA_TO_DEVICE);
455 if (unlikely(dma_mapping_error(aq_nic_get_dev(self),
456 frag_pa)))
457 goto mapping_error;
459 dx = aq_ring_next_dx(ring, dx);
460 dx_buff = &ring->buff_ring[dx];
462 dx_buff->flags = 0U;
463 dx_buff->len = buff_size;
464 dx_buff->pa = frag_pa;
465 dx_buff->is_mapped = 1U;
466 dx_buff->eop_index = 0xffffU;
468 frag_len -= buff_size;
469 buff_offset += buff_size;
471 ++ret;
475 first->eop_index = dx;
476 dx_buff->is_eop = 1U;
477 dx_buff->skb = skb;
478 goto exit;
480 mapping_error:
481 for (dx = ring->sw_tail;
482 ret > 0;
483 --ret, dx = aq_ring_next_dx(ring, dx)) {
484 dx_buff = &ring->buff_ring[dx];
486 if (!dx_buff->is_txc && dx_buff->pa) {
487 if (unlikely(dx_buff->is_sop)) {
488 dma_unmap_single(aq_nic_get_dev(self),
489 dx_buff->pa,
490 dx_buff->len,
491 DMA_TO_DEVICE);
492 } else {
493 dma_unmap_page(aq_nic_get_dev(self),
494 dx_buff->pa,
495 dx_buff->len,
496 DMA_TO_DEVICE);
501 exit:
502 return ret;
505 int aq_nic_xmit(struct aq_nic_s *self, struct sk_buff *skb)
507 struct aq_ring_s *ring = NULL;
508 unsigned int frags = 0U;
509 unsigned int vec = skb->queue_mapping % self->aq_nic_cfg.vecs;
510 unsigned int tc = 0U;
511 int err = NETDEV_TX_OK;
513 frags = skb_shinfo(skb)->nr_frags + 1;
515 ring = self->aq_ring_tx[AQ_NIC_TCVEC2RING(self, tc, vec)];
517 if (frags > AQ_CFG_SKB_FRAGS_MAX) {
518 dev_kfree_skb_any(skb);
519 goto err_exit;
522 aq_ring_update_queue_state(ring);
524 /* Above status update may stop the queue. Check this. */
525 if (__netif_subqueue_stopped(self->ndev, ring->idx)) {
526 err = NETDEV_TX_BUSY;
527 goto err_exit;
530 frags = aq_nic_map_skb(self, skb, ring);
532 if (likely(frags)) {
533 err = self->aq_hw_ops->hw_ring_tx_xmit(self->aq_hw,
534 ring, frags);
535 } else {
536 err = NETDEV_TX_BUSY;
539 err_exit:
540 return err;
543 int aq_nic_update_interrupt_moderation_settings(struct aq_nic_s *self)
545 return self->aq_hw_ops->hw_interrupt_moderation_set(self->aq_hw);
548 int aq_nic_set_packet_filter(struct aq_nic_s *self, unsigned int flags)
550 int err = 0;
552 err = self->aq_hw_ops->hw_packet_filter_set(self->aq_hw, flags);
553 if (err < 0)
554 goto err_exit;
556 self->packet_filter = flags;
558 err_exit:
559 return err;
562 int aq_nic_set_multicast_list(struct aq_nic_s *self, struct net_device *ndev)
564 unsigned int packet_filter = self->packet_filter;
565 struct netdev_hw_addr *ha = NULL;
566 unsigned int i = 0U;
568 self->mc_list.count = 0;
569 if (netdev_uc_count(ndev) > AQ_HW_MULTICAST_ADDRESS_MAX) {
570 packet_filter |= IFF_PROMISC;
571 } else {
572 netdev_for_each_uc_addr(ha, ndev) {
573 ether_addr_copy(self->mc_list.ar[i++], ha->addr);
575 if (i >= AQ_HW_MULTICAST_ADDRESS_MAX)
576 break;
580 if (i + netdev_mc_count(ndev) > AQ_HW_MULTICAST_ADDRESS_MAX) {
581 packet_filter |= IFF_ALLMULTI;
582 } else {
583 netdev_for_each_mc_addr(ha, ndev) {
584 ether_addr_copy(self->mc_list.ar[i++], ha->addr);
586 if (i >= AQ_HW_MULTICAST_ADDRESS_MAX)
587 break;
591 if (i > 0 && i <= AQ_HW_MULTICAST_ADDRESS_MAX) {
592 packet_filter |= IFF_MULTICAST;
593 self->mc_list.count = i;
594 self->aq_hw_ops->hw_multicast_list_set(self->aq_hw,
595 self->mc_list.ar,
596 self->mc_list.count);
598 return aq_nic_set_packet_filter(self, packet_filter);
601 int aq_nic_set_mtu(struct aq_nic_s *self, int new_mtu)
603 self->aq_nic_cfg.mtu = new_mtu;
605 return 0;
608 int aq_nic_set_mac(struct aq_nic_s *self, struct net_device *ndev)
610 return self->aq_hw_ops->hw_set_mac_address(self->aq_hw, ndev->dev_addr);
613 unsigned int aq_nic_get_link_speed(struct aq_nic_s *self)
615 return self->link_status.mbps;
618 int aq_nic_get_regs(struct aq_nic_s *self, struct ethtool_regs *regs, void *p)
620 u32 *regs_buff = p;
621 int err = 0;
623 if (unlikely(!self->aq_hw_ops->hw_get_regs))
624 return -EOPNOTSUPP;
626 regs->version = 1;
628 err = self->aq_hw_ops->hw_get_regs(self->aq_hw,
629 self->aq_nic_cfg.aq_hw_caps,
630 regs_buff);
631 if (err < 0)
632 goto err_exit;
634 err_exit:
635 return err;
638 int aq_nic_get_regs_count(struct aq_nic_s *self)
640 if (unlikely(!self->aq_hw_ops->hw_get_regs))
641 return 0;
643 return self->aq_nic_cfg.aq_hw_caps->mac_regs_count;
646 void aq_nic_get_stats(struct aq_nic_s *self, u64 *data)
648 unsigned int i = 0U;
649 unsigned int count = 0U;
650 struct aq_vec_s *aq_vec = NULL;
651 struct aq_stats_s *stats = self->aq_hw_ops->hw_get_hw_stats(self->aq_hw);
653 if (!stats)
654 goto err_exit;
656 data[i] = stats->uprc + stats->mprc + stats->bprc;
657 data[++i] = stats->uprc;
658 data[++i] = stats->mprc;
659 data[++i] = stats->bprc;
660 data[++i] = stats->erpt;
661 data[++i] = stats->uptc + stats->mptc + stats->bptc;
662 data[++i] = stats->uptc;
663 data[++i] = stats->mptc;
664 data[++i] = stats->bptc;
665 data[++i] = stats->ubrc;
666 data[++i] = stats->ubtc;
667 data[++i] = stats->mbrc;
668 data[++i] = stats->mbtc;
669 data[++i] = stats->bbrc;
670 data[++i] = stats->bbtc;
671 data[++i] = stats->ubrc + stats->mbrc + stats->bbrc;
672 data[++i] = stats->ubtc + stats->mbtc + stats->bbtc;
673 data[++i] = stats->dma_pkt_rc;
674 data[++i] = stats->dma_pkt_tc;
675 data[++i] = stats->dma_oct_rc;
676 data[++i] = stats->dma_oct_tc;
677 data[++i] = stats->dpc;
679 i++;
681 data += i;
683 for (i = 0U, aq_vec = self->aq_vec[0];
684 aq_vec && self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i]) {
685 data += count;
686 aq_vec_get_sw_stats(aq_vec, data, &count);
689 err_exit:;
692 static void aq_nic_update_ndev_stats(struct aq_nic_s *self)
694 struct net_device *ndev = self->ndev;
695 struct aq_stats_s *stats = self->aq_hw_ops->hw_get_hw_stats(self->aq_hw);
697 ndev->stats.rx_packets = stats->uprc + stats->mprc + stats->bprc;
698 ndev->stats.rx_bytes = stats->ubrc + stats->mbrc + stats->bbrc;
699 ndev->stats.rx_errors = stats->erpr;
700 ndev->stats.tx_packets = stats->uptc + stats->mptc + stats->bptc;
701 ndev->stats.tx_bytes = stats->ubtc + stats->mbtc + stats->bbtc;
702 ndev->stats.tx_errors = stats->erpt;
703 ndev->stats.multicast = stats->mprc;
706 void aq_nic_get_link_ksettings(struct aq_nic_s *self,
707 struct ethtool_link_ksettings *cmd)
709 if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_FIBRE)
710 cmd->base.port = PORT_FIBRE;
711 else
712 cmd->base.port = PORT_TP;
713 /* This driver supports only 10G capable adapters, so DUPLEX_FULL */
714 cmd->base.duplex = DUPLEX_FULL;
715 cmd->base.autoneg = self->aq_nic_cfg.is_autoneg;
717 ethtool_link_ksettings_zero_link_mode(cmd, supported);
719 if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_10G)
720 ethtool_link_ksettings_add_link_mode(cmd, supported,
721 10000baseT_Full);
723 if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_5G)
724 ethtool_link_ksettings_add_link_mode(cmd, supported,
725 5000baseT_Full);
727 if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_2GS)
728 ethtool_link_ksettings_add_link_mode(cmd, supported,
729 2500baseT_Full);
731 if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_1G)
732 ethtool_link_ksettings_add_link_mode(cmd, supported,
733 1000baseT_Full);
735 if (self->aq_nic_cfg.aq_hw_caps->link_speed_msk & AQ_NIC_RATE_100M)
736 ethtool_link_ksettings_add_link_mode(cmd, supported,
737 100baseT_Full);
739 if (self->aq_nic_cfg.aq_hw_caps->flow_control)
740 ethtool_link_ksettings_add_link_mode(cmd, supported,
741 Pause);
743 ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
745 if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_FIBRE)
746 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
747 else
748 ethtool_link_ksettings_add_link_mode(cmd, supported, TP);
750 ethtool_link_ksettings_zero_link_mode(cmd, advertising);
752 if (self->aq_nic_cfg.is_autoneg)
753 ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
755 if (self->aq_nic_cfg.link_speed_msk & AQ_NIC_RATE_10G)
756 ethtool_link_ksettings_add_link_mode(cmd, advertising,
757 10000baseT_Full);
759 if (self->aq_nic_cfg.link_speed_msk & AQ_NIC_RATE_5G)
760 ethtool_link_ksettings_add_link_mode(cmd, advertising,
761 5000baseT_Full);
763 if (self->aq_nic_cfg.link_speed_msk & AQ_NIC_RATE_2GS)
764 ethtool_link_ksettings_add_link_mode(cmd, advertising,
765 2500baseT_Full);
767 if (self->aq_nic_cfg.link_speed_msk & AQ_NIC_RATE_1G)
768 ethtool_link_ksettings_add_link_mode(cmd, advertising,
769 1000baseT_Full);
771 if (self->aq_nic_cfg.link_speed_msk & AQ_NIC_RATE_100M)
772 ethtool_link_ksettings_add_link_mode(cmd, advertising,
773 100baseT_Full);
775 if (self->aq_nic_cfg.flow_control & AQ_NIC_FC_RX)
776 ethtool_link_ksettings_add_link_mode(cmd, advertising,
777 Pause);
779 if (self->aq_nic_cfg.flow_control & AQ_NIC_FC_TX)
780 ethtool_link_ksettings_add_link_mode(cmd, advertising,
781 Asym_Pause);
783 if (self->aq_nic_cfg.aq_hw_caps->media_type == AQ_HW_MEDIA_TYPE_FIBRE)
784 ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
785 else
786 ethtool_link_ksettings_add_link_mode(cmd, advertising, TP);
789 int aq_nic_set_link_ksettings(struct aq_nic_s *self,
790 const struct ethtool_link_ksettings *cmd)
792 u32 speed = 0U;
793 u32 rate = 0U;
794 int err = 0;
796 if (cmd->base.autoneg == AUTONEG_ENABLE) {
797 rate = self->aq_nic_cfg.aq_hw_caps->link_speed_msk;
798 self->aq_nic_cfg.is_autoneg = true;
799 } else {
800 speed = cmd->base.speed;
802 switch (speed) {
803 case SPEED_100:
804 rate = AQ_NIC_RATE_100M;
805 break;
807 case SPEED_1000:
808 rate = AQ_NIC_RATE_1G;
809 break;
811 case SPEED_2500:
812 rate = AQ_NIC_RATE_2GS;
813 break;
815 case SPEED_5000:
816 rate = AQ_NIC_RATE_5G;
817 break;
819 case SPEED_10000:
820 rate = AQ_NIC_RATE_10G;
821 break;
823 default:
824 err = -1;
825 goto err_exit;
826 break;
828 if (!(self->aq_nic_cfg.aq_hw_caps->link_speed_msk & rate)) {
829 err = -1;
830 goto err_exit;
833 self->aq_nic_cfg.is_autoneg = false;
836 err = self->aq_fw_ops->set_link_speed(self->aq_hw, rate);
837 if (err < 0)
838 goto err_exit;
840 self->aq_nic_cfg.link_speed_msk = rate;
842 err_exit:
843 return err;
846 struct aq_nic_cfg_s *aq_nic_get_cfg(struct aq_nic_s *self)
848 return &self->aq_nic_cfg;
851 u32 aq_nic_get_fw_version(struct aq_nic_s *self)
853 u32 fw_version = 0U;
855 self->aq_hw_ops->hw_get_fw_version(self->aq_hw, &fw_version);
857 return fw_version;
860 int aq_nic_stop(struct aq_nic_s *self)
862 struct aq_vec_s *aq_vec = NULL;
863 unsigned int i = 0U;
865 netif_tx_disable(self->ndev);
866 netif_carrier_off(self->ndev);
868 del_timer_sync(&self->service_timer);
870 self->aq_hw_ops->hw_irq_disable(self->aq_hw, AQ_CFG_IRQ_MASK);
872 if (self->aq_nic_cfg.is_polling)
873 del_timer_sync(&self->polling_timer);
874 else
875 aq_pci_func_free_irqs(self);
877 for (i = 0U, aq_vec = self->aq_vec[0];
878 self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
879 aq_vec_stop(aq_vec);
881 return self->aq_hw_ops->hw_stop(self->aq_hw);
884 void aq_nic_deinit(struct aq_nic_s *self)
886 struct aq_vec_s *aq_vec = NULL;
887 unsigned int i = 0U;
889 if (!self)
890 goto err_exit;
892 for (i = 0U, aq_vec = self->aq_vec[0];
893 self->aq_vecs > i; ++i, aq_vec = self->aq_vec[i])
894 aq_vec_deinit(aq_vec);
896 if (self->power_state == AQ_HW_POWER_STATE_D0) {
897 (void)self->aq_fw_ops->deinit(self->aq_hw);
898 } else {
899 (void)self->aq_hw_ops->hw_set_power(self->aq_hw,
900 self->power_state);
903 err_exit:;
906 void aq_nic_free_vectors(struct aq_nic_s *self)
908 unsigned int i = 0U;
910 if (!self)
911 goto err_exit;
913 for (i = ARRAY_SIZE(self->aq_vec); i--;) {
914 if (self->aq_vec[i]) {
915 aq_vec_free(self->aq_vec[i]);
916 self->aq_vec[i] = NULL;
920 err_exit:;
923 int aq_nic_change_pm_state(struct aq_nic_s *self, pm_message_t *pm_msg)
925 int err = 0;
927 if (!netif_running(self->ndev)) {
928 err = 0;
929 goto out;
931 rtnl_lock();
932 if (pm_msg->event & PM_EVENT_SLEEP || pm_msg->event & PM_EVENT_FREEZE) {
933 self->power_state = AQ_HW_POWER_STATE_D3;
934 netif_device_detach(self->ndev);
935 netif_tx_stop_all_queues(self->ndev);
937 err = aq_nic_stop(self);
938 if (err < 0)
939 goto err_exit;
941 aq_nic_deinit(self);
942 } else {
943 err = aq_nic_init(self);
944 if (err < 0)
945 goto err_exit;
947 err = aq_nic_start(self);
948 if (err < 0)
949 goto err_exit;
951 netif_device_attach(self->ndev);
952 netif_tx_start_all_queues(self->ndev);
955 err_exit:
956 rtnl_unlock();
957 out:
958 return err;
961 void aq_nic_shutdown(struct aq_nic_s *self)
963 int err = 0;
965 if (!self->ndev)
966 return;
968 rtnl_lock();
970 netif_device_detach(self->ndev);
972 if (netif_running(self->ndev)) {
973 err = aq_nic_stop(self);
974 if (err < 0)
975 goto err_exit;
977 aq_nic_deinit(self);
979 err_exit:
980 rtnl_unlock();