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