1// SPDX-License-Identifier: GPL-2.0+ 2// Copyright (c) 2016-2017 Hisilicon Limited. 3 4#include <linux/dma-mapping.h> 5#include <linux/etherdevice.h> 6#include <linux/interrupt.h> 7#ifdef CONFIG_RFS_ACCEL 8#include <linux/cpu_rmap.h> 9#endif 10#include <linux/if_vlan.h> 11#include <linux/irq.h> 12#include <linux/ip.h> 13#include <linux/ipv6.h> 14#include <linux/module.h> 15#include <linux/pci.h> 16#include <linux/aer.h> 17#include <linux/skbuff.h> 18#include <linux/sctp.h> 19#include <net/gre.h> 20#include <net/ip6_checksum.h> 21#include <net/pkt_cls.h> 22#include <net/tcp.h> 23#include <net/vxlan.h> 24#include <net/geneve.h> 25 26#include "hnae3.h" 27#include "hns3_enet.h" 28/* All hns3 tracepoints are defined by the include below, which 29 * must be included exactly once across the whole kernel with 30 * CREATE_TRACE_POINTS defined 31 */ 32#define CREATE_TRACE_POINTS 33#include "hns3_trace.h" 34 35#define hns3_set_field(origin, shift, val) ((origin) |= ((val) << (shift))) 36#define hns3_tx_bd_count(S) DIV_ROUND_UP(S, HNS3_MAX_BD_SIZE) 37 38#define hns3_rl_err(fmt, ...) \ 39 do { \ 40 if (net_ratelimit()) \ 41 netdev_err(fmt, ##__VA_ARGS__); \ 42 } while (0) 43 44static void hns3_clear_all_ring(struct hnae3_handle *h, bool force); 45 46static const char hns3_driver_name[] = "hns3"; 47static const char hns3_driver_string[] = 48 "Hisilicon Ethernet Network Driver for Hip08 Family"; 49static const char hns3_copyright[] = "Copyright (c) 2017 Huawei Corporation."; 50static struct hnae3_client client; 51 52static int debug = -1; 53module_param(debug, int, 0); 54MODULE_PARM_DESC(debug, " Network interface message level setting"); 55 56#define DEFAULT_MSG_LEVEL (NETIF_MSG_PROBE | NETIF_MSG_LINK | \ 57 NETIF_MSG_IFDOWN | NETIF_MSG_IFUP) 58 59#define HNS3_INNER_VLAN_TAG 1 60#define HNS3_OUTER_VLAN_TAG 2 61 62#define HNS3_MIN_TX_LEN 33U 63#define HNS3_MIN_TUN_PKT_LEN 65U 64 65/* hns3_pci_tbl - PCI Device ID Table 66 * 67 * Last entry must be all 0s 68 * 69 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, 70 * Class, Class Mask, private data (not used) } 71 */ 72static const struct pci_device_id hns3_pci_tbl[] = { 73 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_GE), 0}, 74 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE), 0}, 75 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA), 76 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS}, 77 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA_MACSEC), 78 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS}, 79 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA), 80 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS}, 81 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA_MACSEC), 82 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS}, 83 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_MACSEC), 84 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS}, 85 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_200G_RDMA), 86 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS}, 87 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_VF), 0}, 88 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_RDMA_DCB_PFC_VF), 89 HNAE3_DEV_SUPPORT_ROCE_DCB_BITS}, 90 /* required last entry */ 91 {0, } 92}; 93MODULE_DEVICE_TABLE(pci, hns3_pci_tbl); 94 95static irqreturn_t hns3_irq_handle(int irq, void *vector) 96{ 97 struct hns3_enet_tqp_vector *tqp_vector = vector; 98 99 napi_schedule_irqoff(&tqp_vector->napi); 100 101 return IRQ_HANDLED; 102} 103 104static void hns3_nic_uninit_irq(struct hns3_nic_priv *priv) 105{ 106 struct hns3_enet_tqp_vector *tqp_vectors; 107 unsigned int i; 108 109 for (i = 0; i < priv->vector_num; i++) { 110 tqp_vectors = &priv->tqp_vector[i]; 111 112 if (tqp_vectors->irq_init_flag != HNS3_VECTOR_INITED) 113 continue; 114 115 /* clear the affinity mask */ 116 irq_set_affinity_hint(tqp_vectors->vector_irq, NULL); 117 118 /* release the irq resource */ 119 free_irq(tqp_vectors->vector_irq, tqp_vectors); 120 tqp_vectors->irq_init_flag = HNS3_VECTOR_NOT_INITED; 121 } 122} 123 124static int hns3_nic_init_irq(struct hns3_nic_priv *priv) 125{ 126 struct hns3_enet_tqp_vector *tqp_vectors; 127 int txrx_int_idx = 0; 128 int rx_int_idx = 0; 129 int tx_int_idx = 0; 130 unsigned int i; 131 int ret; 132 133 for (i = 0; i < priv->vector_num; i++) { 134 tqp_vectors = &priv->tqp_vector[i]; 135 136 if (tqp_vectors->irq_init_flag == HNS3_VECTOR_INITED) 137 continue; 138 139 if (tqp_vectors->tx_group.ring && tqp_vectors->rx_group.ring) { 140 snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN, 141 "%s-%s-%s-%d", hns3_driver_name, 142 pci_name(priv->ae_handle->pdev), 143 "TxRx", txrx_int_idx++); 144 txrx_int_idx++; 145 } else if (tqp_vectors->rx_group.ring) { 146 snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN, 147 "%s-%s-%s-%d", hns3_driver_name, 148 pci_name(priv->ae_handle->pdev), 149 "Rx", rx_int_idx++); 150 } else if (tqp_vectors->tx_group.ring) { 151 snprintf(tqp_vectors->name, HNAE3_INT_NAME_LEN, 152 "%s-%s-%s-%d", hns3_driver_name, 153 pci_name(priv->ae_handle->pdev), 154 "Tx", tx_int_idx++); 155 } else { 156 /* Skip this unused q_vector */ 157 continue; 158 } 159 160 tqp_vectors->name[HNAE3_INT_NAME_LEN - 1] = '\0'; 161 162 irq_set_status_flags(tqp_vectors->vector_irq, IRQ_NOAUTOEN); 163 ret = request_irq(tqp_vectors->vector_irq, hns3_irq_handle, 0, 164 tqp_vectors->name, tqp_vectors); 165 if (ret) { 166 netdev_err(priv->netdev, "request irq(%d) fail\n", 167 tqp_vectors->vector_irq); 168 hns3_nic_uninit_irq(priv); 169 return ret; 170 } 171 172 irq_set_affinity_hint(tqp_vectors->vector_irq, 173 &tqp_vectors->affinity_mask); 174 175 tqp_vectors->irq_init_flag = HNS3_VECTOR_INITED; 176 } 177 178 return 0; 179} 180 181static void hns3_mask_vector_irq(struct hns3_enet_tqp_vector *tqp_vector, 182 u32 mask_en) 183{ 184 writel(mask_en, tqp_vector->mask_addr); 185} 186 187static void hns3_vector_enable(struct hns3_enet_tqp_vector *tqp_vector) 188{ 189 napi_enable(&tqp_vector->napi); 190 enable_irq(tqp_vector->vector_irq); 191 192 /* enable vector */ 193 hns3_mask_vector_irq(tqp_vector, 1); 194} 195 196static void hns3_vector_disable(struct hns3_enet_tqp_vector *tqp_vector) 197{ 198 /* disable vector */ 199 hns3_mask_vector_irq(tqp_vector, 0); 200 201 disable_irq(tqp_vector->vector_irq); 202 napi_disable(&tqp_vector->napi); 203} 204 205void hns3_set_vector_coalesce_rl(struct hns3_enet_tqp_vector *tqp_vector, 206 u32 rl_value) 207{ 208 u32 rl_reg = hns3_rl_usec_to_reg(rl_value); 209 210 /* this defines the configuration for RL (Interrupt Rate Limiter). 211 * Rl defines rate of interrupts i.e. number of interrupts-per-second 212 * GL and RL(Rate Limiter) are 2 ways to acheive interrupt coalescing 213 */ 214 215 if (rl_reg > 0 && !tqp_vector->tx_group.coal.gl_adapt_enable && 216 !tqp_vector->rx_group.coal.gl_adapt_enable) 217 /* According to the hardware, the range of rl_reg is 218 * 0-59 and the unit is 4. 219 */ 220 rl_reg |= HNS3_INT_RL_ENABLE_MASK; 221 222 writel(rl_reg, tqp_vector->mask_addr + HNS3_VECTOR_RL_OFFSET); 223} 224 225void hns3_set_vector_coalesce_rx_gl(struct hns3_enet_tqp_vector *tqp_vector, 226 u32 gl_value) 227{ 228 u32 rx_gl_reg = hns3_gl_usec_to_reg(gl_value); 229 230 writel(rx_gl_reg, tqp_vector->mask_addr + HNS3_VECTOR_GL0_OFFSET); 231} 232 233void hns3_set_vector_coalesce_tx_gl(struct hns3_enet_tqp_vector *tqp_vector, 234 u32 gl_value) 235{ 236 u32 tx_gl_reg = hns3_gl_usec_to_reg(gl_value); 237 238 writel(tx_gl_reg, tqp_vector->mask_addr + HNS3_VECTOR_GL1_OFFSET); 239} 240 241static void hns3_vector_gl_rl_init(struct hns3_enet_tqp_vector *tqp_vector, 242 struct hns3_nic_priv *priv) 243{ 244 /* initialize the configuration for interrupt coalescing. 245 * 1. GL (Interrupt Gap Limiter) 246 * 2. RL (Interrupt Rate Limiter) 247 * 248 * Default: enable interrupt coalescing self-adaptive and GL 249 */ 250 tqp_vector->tx_group.coal.gl_adapt_enable = 1; 251 tqp_vector->rx_group.coal.gl_adapt_enable = 1; 252 253 tqp_vector->tx_group.coal.int_gl = HNS3_INT_GL_50K; 254 tqp_vector->rx_group.coal.int_gl = HNS3_INT_GL_50K; 255 256 tqp_vector->rx_group.coal.flow_level = HNS3_FLOW_LOW; 257 tqp_vector->tx_group.coal.flow_level = HNS3_FLOW_LOW; 258} 259 260static void hns3_vector_gl_rl_init_hw(struct hns3_enet_tqp_vector *tqp_vector, 261 struct hns3_nic_priv *priv) 262{ 263 struct hnae3_handle *h = priv->ae_handle; 264 265 hns3_set_vector_coalesce_tx_gl(tqp_vector, 266 tqp_vector->tx_group.coal.int_gl); 267 hns3_set_vector_coalesce_rx_gl(tqp_vector, 268 tqp_vector->rx_group.coal.int_gl); 269 hns3_set_vector_coalesce_rl(tqp_vector, h->kinfo.int_rl_setting); 270} 271 272static int hns3_nic_set_real_num_queue(struct net_device *netdev) 273{ 274 struct hnae3_handle *h = hns3_get_handle(netdev); 275 struct hnae3_knic_private_info *kinfo = &h->kinfo; 276 unsigned int queue_size = kinfo->rss_size * kinfo->num_tc; 277 int i, ret; 278 279 if (kinfo->num_tc <= 1) { 280 netdev_reset_tc(netdev); 281 } else { 282 ret = netdev_set_num_tc(netdev, kinfo->num_tc); 283 if (ret) { 284 netdev_err(netdev, 285 "netdev_set_num_tc fail, ret=%d!\n", ret); 286 return ret; 287 } 288 289 for (i = 0; i < HNAE3_MAX_TC; i++) { 290 if (!kinfo->tc_info[i].enable) 291 continue; 292 293 netdev_set_tc_queue(netdev, 294 kinfo->tc_info[i].tc, 295 kinfo->tc_info[i].tqp_count, 296 kinfo->tc_info[i].tqp_offset); 297 } 298 } 299 300 ret = netif_set_real_num_tx_queues(netdev, queue_size); 301 if (ret) { 302 netdev_err(netdev, 303 "netif_set_real_num_tx_queues fail, ret=%d!\n", ret); 304 return ret; 305 } 306 307 ret = netif_set_real_num_rx_queues(netdev, queue_size); 308 if (ret) { 309 netdev_err(netdev, 310 "netif_set_real_num_rx_queues fail, ret=%d!\n", ret); 311 return ret; 312 } 313 314 return 0; 315} 316 317static u16 hns3_get_max_available_channels(struct hnae3_handle *h) 318{ 319 u16 alloc_tqps, max_rss_size, rss_size; 320 321 h->ae_algo->ops->get_tqps_and_rss_info(h, &alloc_tqps, &max_rss_size); 322 rss_size = alloc_tqps / h->kinfo.num_tc; 323 324 return min_t(u16, rss_size, max_rss_size); 325} 326 327static void hns3_tqp_enable(struct hnae3_queue *tqp) 328{ 329 u32 rcb_reg; 330 331 rcb_reg = hns3_read_dev(tqp, HNS3_RING_EN_REG); 332 rcb_reg |= BIT(HNS3_RING_EN_B); 333 hns3_write_dev(tqp, HNS3_RING_EN_REG, rcb_reg); 334} 335 336static void hns3_tqp_disable(struct hnae3_queue *tqp) 337{ 338 u32 rcb_reg; 339 340 rcb_reg = hns3_read_dev(tqp, HNS3_RING_EN_REG); 341 rcb_reg &= ~BIT(HNS3_RING_EN_B); 342 hns3_write_dev(tqp, HNS3_RING_EN_REG, rcb_reg); 343} 344 345static void hns3_free_rx_cpu_rmap(struct net_device *netdev) 346{ 347#ifdef CONFIG_RFS_ACCEL 348 free_irq_cpu_rmap(netdev->rx_cpu_rmap); 349 netdev->rx_cpu_rmap = NULL; 350#endif 351} 352 353static int hns3_set_rx_cpu_rmap(struct net_device *netdev) 354{ 355#ifdef CONFIG_RFS_ACCEL 356 struct hns3_nic_priv *priv = netdev_priv(netdev); 357 struct hns3_enet_tqp_vector *tqp_vector; 358 int i, ret; 359 360 if (!netdev->rx_cpu_rmap) { 361 netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(priv->vector_num); 362 if (!netdev->rx_cpu_rmap) 363 return -ENOMEM; 364 } 365 366 for (i = 0; i < priv->vector_num; i++) { 367 tqp_vector = &priv->tqp_vector[i]; 368 ret = irq_cpu_rmap_add(netdev->rx_cpu_rmap, 369 tqp_vector->vector_irq); 370 if (ret) { 371 hns3_free_rx_cpu_rmap(netdev); 372 return ret; 373 } 374 } 375#endif 376 return 0; 377} 378 379static int hns3_nic_net_up(struct net_device *netdev) 380{ 381 struct hns3_nic_priv *priv = netdev_priv(netdev); 382 struct hnae3_handle *h = priv->ae_handle; 383 int i, j; 384 int ret; 385 386 ret = hns3_nic_reset_all_ring(h); 387 if (ret) 388 return ret; 389 390 clear_bit(HNS3_NIC_STATE_DOWN, &priv->state); 391 392 /* enable the vectors */ 393 for (i = 0; i < priv->vector_num; i++) 394 hns3_vector_enable(&priv->tqp_vector[i]); 395 396 /* enable rcb */ 397 for (j = 0; j < h->kinfo.num_tqps; j++) 398 hns3_tqp_enable(h->kinfo.tqp[j]); 399 400 /* start the ae_dev */ 401 ret = h->ae_algo->ops->start ? h->ae_algo->ops->start(h) : 0; 402 if (ret) { 403 set_bit(HNS3_NIC_STATE_DOWN, &priv->state); 404 while (j--) 405 hns3_tqp_disable(h->kinfo.tqp[j]); 406 407 for (j = i - 1; j >= 0; j--) 408 hns3_vector_disable(&priv->tqp_vector[j]); 409 } 410 411 return ret; 412} 413 414static void hns3_config_xps(struct hns3_nic_priv *priv) 415{ 416 int i; 417 418 for (i = 0; i < priv->vector_num; i++) { 419 struct hns3_enet_tqp_vector *tqp_vector = &priv->tqp_vector[i]; 420 struct hns3_enet_ring *ring = tqp_vector->tx_group.ring; 421 422 while (ring) { 423 int ret; 424 425 ret = netif_set_xps_queue(priv->netdev, 426 &tqp_vector->affinity_mask, 427 ring->tqp->tqp_index); 428 if (ret) 429 netdev_warn(priv->netdev, 430 "set xps queue failed: %d", ret); 431 432 ring = ring->next; 433 } 434 } 435} 436 437static int hns3_nic_net_open(struct net_device *netdev) 438{ 439 struct hns3_nic_priv *priv = netdev_priv(netdev); 440 struct hnae3_handle *h = hns3_get_handle(netdev); 441 struct hnae3_knic_private_info *kinfo; 442 int i, ret; 443 444 if (hns3_nic_resetting(netdev)) 445 return -EBUSY; 446 447 if (!test_bit(HNS3_NIC_STATE_DOWN, &priv->state)) { 448 netdev_warn(netdev, "net open repeatedly!\n"); 449 return 0; 450 } 451 452 netif_carrier_off(netdev); 453 454 ret = hns3_nic_set_real_num_queue(netdev); 455 if (ret) 456 return ret; 457 458 ret = hns3_nic_net_up(netdev); 459 if (ret) { 460 netdev_err(netdev, "net up fail, ret=%d!\n", ret); 461 return ret; 462 } 463 464 kinfo = &h->kinfo; 465 for (i = 0; i < HNAE3_MAX_USER_PRIO; i++) 466 netdev_set_prio_tc_map(netdev, i, kinfo->prio_tc[i]); 467 468 if (h->ae_algo->ops->set_timer_task) 469 h->ae_algo->ops->set_timer_task(priv->ae_handle, true); 470 471 hns3_config_xps(priv); 472 473 netif_dbg(h, drv, netdev, "net open\n"); 474 475 return 0; 476} 477 478static void hns3_reset_tx_queue(struct hnae3_handle *h) 479{ 480 struct net_device *ndev = h->kinfo.netdev; 481 struct hns3_nic_priv *priv = netdev_priv(ndev); 482 struct netdev_queue *dev_queue; 483 u32 i; 484 485 for (i = 0; i < h->kinfo.num_tqps; i++) { 486 dev_queue = netdev_get_tx_queue(ndev, 487 priv->ring[i].queue_index); 488 netdev_tx_reset_queue(dev_queue); 489 } 490} 491 492static void hns3_nic_net_down(struct net_device *netdev) 493{ 494 struct hns3_nic_priv *priv = netdev_priv(netdev); 495 struct hnae3_handle *h = hns3_get_handle(netdev); 496 const struct hnae3_ae_ops *ops; 497 int i; 498 499 /* disable vectors */ 500 for (i = 0; i < priv->vector_num; i++) 501 hns3_vector_disable(&priv->tqp_vector[i]); 502 503 /* disable rcb */ 504 for (i = 0; i < h->kinfo.num_tqps; i++) 505 hns3_tqp_disable(h->kinfo.tqp[i]); 506 507 /* stop ae_dev */ 508 ops = priv->ae_handle->ae_algo->ops; 509 if (ops->stop) 510 ops->stop(priv->ae_handle); 511 512 /* delay ring buffer clearing to hns3_reset_notify_uninit_enet 513 * during reset process, because driver may not be able 514 * to disable the ring through firmware when downing the netdev. 515 */ 516 if (!hns3_nic_resetting(netdev)) 517 hns3_clear_all_ring(priv->ae_handle, false); 518 519 hns3_reset_tx_queue(priv->ae_handle); 520} 521 522static int hns3_nic_net_stop(struct net_device *netdev) 523{ 524 struct hns3_nic_priv *priv = netdev_priv(netdev); 525 struct hnae3_handle *h = hns3_get_handle(netdev); 526 527 if (test_and_set_bit(HNS3_NIC_STATE_DOWN, &priv->state)) 528 return 0; 529 530 netif_dbg(h, drv, netdev, "net stop\n"); 531 532 if (h->ae_algo->ops->set_timer_task) 533 h->ae_algo->ops->set_timer_task(priv->ae_handle, false); 534 535 netif_carrier_off(netdev); 536 netif_tx_disable(netdev); 537 538 hns3_nic_net_down(netdev); 539 540 return 0; 541} 542 543static int hns3_nic_uc_sync(struct net_device *netdev, 544 const unsigned char *addr) 545{ 546 struct hnae3_handle *h = hns3_get_handle(netdev); 547 548 if (h->ae_algo->ops->add_uc_addr) 549 return h->ae_algo->ops->add_uc_addr(h, addr); 550 551 return 0; 552} 553 554static int hns3_nic_uc_unsync(struct net_device *netdev, 555 const unsigned char *addr) 556{ 557 struct hnae3_handle *h = hns3_get_handle(netdev); 558 559 /* need ignore the request of removing device address, because 560 * we store the device address and other addresses of uc list 561 * in the function's mac filter list. 562 */ 563 if (ether_addr_equal(addr, netdev->dev_addr)) 564 return 0; 565 566 if (h->ae_algo->ops->rm_uc_addr) 567 return h->ae_algo->ops->rm_uc_addr(h, addr); 568 569 return 0; 570} 571 572static int hns3_nic_mc_sync(struct net_device *netdev, 573 const unsigned char *addr) 574{ 575 struct hnae3_handle *h = hns3_get_handle(netdev); 576 577 if (h->ae_algo->ops->add_mc_addr) 578 return h->ae_algo->ops->add_mc_addr(h, addr); 579 580 return 0; 581} 582 583static int hns3_nic_mc_unsync(struct net_device *netdev, 584 const unsigned char *addr) 585{ 586 struct hnae3_handle *h = hns3_get_handle(netdev); 587 588 if (h->ae_algo->ops->rm_mc_addr) 589 return h->ae_algo->ops->rm_mc_addr(h, addr); 590 591 return 0; 592} 593 594static u8 hns3_get_netdev_flags(struct net_device *netdev) 595{ 596 u8 flags = 0; 597 598 if (netdev->flags & IFF_PROMISC) { 599 flags = HNAE3_USER_UPE | HNAE3_USER_MPE | HNAE3_BPE; 600 } else { 601 flags |= HNAE3_VLAN_FLTR; 602 if (netdev->flags & IFF_ALLMULTI) 603 flags |= HNAE3_USER_MPE; 604 } 605 606 return flags; 607} 608 609static void hns3_nic_set_rx_mode(struct net_device *netdev) 610{ 611 struct hnae3_handle *h = hns3_get_handle(netdev); 612 u8 new_flags; 613 614 new_flags = hns3_get_netdev_flags(netdev); 615 616 __dev_uc_sync(netdev, hns3_nic_uc_sync, hns3_nic_uc_unsync); 617 __dev_mc_sync(netdev, hns3_nic_mc_sync, hns3_nic_mc_unsync); 618 619 /* User mode Promisc mode enable and vlan filtering is disabled to 620 * let all packets in. 621 */ 622 h->netdev_flags = new_flags; 623 hns3_request_update_promisc_mode(h); 624} 625 626void hns3_request_update_promisc_mode(struct hnae3_handle *handle) 627{ 628 const struct hnae3_ae_ops *ops = handle->ae_algo->ops; 629 630 if (ops->request_update_promisc_mode) 631 ops->request_update_promisc_mode(handle); 632} 633 634void hns3_enable_vlan_filter(struct net_device *netdev, bool enable) 635{ 636 struct hns3_nic_priv *priv = netdev_priv(netdev); 637 struct hnae3_handle *h = priv->ae_handle; 638 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(h->pdev); 639 bool last_state; 640 641 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2 && 642 h->ae_algo->ops->enable_vlan_filter) { 643 last_state = h->netdev_flags & HNAE3_VLAN_FLTR ? true : false; 644 if (enable != last_state) { 645 netdev_info(netdev, 646 "%s vlan filter\n", 647 enable ? "enable" : "disable"); 648 h->ae_algo->ops->enable_vlan_filter(h, enable); 649 } 650 } 651} 652 653static int hns3_set_tso(struct sk_buff *skb, u32 *paylen, 654 u16 *mss, u32 *type_cs_vlan_tso) 655{ 656 u32 l4_offset, hdr_len; 657 union l3_hdr_info l3; 658 union l4_hdr_info l4; 659 u32 l4_paylen; 660 int ret; 661 662 if (!skb_is_gso(skb)) 663 return 0; 664 665 ret = skb_cow_head(skb, 0); 666 if (unlikely(ret < 0)) 667 return ret; 668 669 l3.hdr = skb_network_header(skb); 670 l4.hdr = skb_transport_header(skb); 671 672 /* Software should clear the IPv4's checksum field when tso is 673 * needed. 674 */ 675 if (l3.v4->version == 4) 676 l3.v4->check = 0; 677 678 /* tunnel packet */ 679 if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE | 680 SKB_GSO_GRE_CSUM | 681 SKB_GSO_UDP_TUNNEL | 682 SKB_GSO_UDP_TUNNEL_CSUM)) { 683 if ((!(skb_shinfo(skb)->gso_type & 684 SKB_GSO_PARTIAL)) && 685 (skb_shinfo(skb)->gso_type & 686 SKB_GSO_UDP_TUNNEL_CSUM)) { 687 /* Software should clear the udp's checksum 688 * field when tso is needed. 689 */ 690 l4.udp->check = 0; 691 } 692 /* reset l3&l4 pointers from outer to inner headers */ 693 l3.hdr = skb_inner_network_header(skb); 694 l4.hdr = skb_inner_transport_header(skb); 695 696 /* Software should clear the IPv4's checksum field when 697 * tso is needed. 698 */ 699 if (l3.v4->version == 4) 700 l3.v4->check = 0; 701 } 702 703 /* normal or tunnel packet */ 704 l4_offset = l4.hdr - skb->data; 705 706 /* remove payload length from inner pseudo checksum when tso */ 707 l4_paylen = skb->len - l4_offset; 708 709 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) { 710 hdr_len = sizeof(*l4.udp) + l4_offset; 711 csum_replace_by_diff(&l4.udp->check, 712 (__force __wsum)htonl(l4_paylen)); 713 } else { 714 hdr_len = (l4.tcp->doff << 2) + l4_offset; 715 csum_replace_by_diff(&l4.tcp->check, 716 (__force __wsum)htonl(l4_paylen)); 717 } 718 719 /* find the txbd field values */ 720 *paylen = skb->len - hdr_len; 721 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_TSO_B, 1); 722 723 /* get MSS for TSO */ 724 *mss = skb_shinfo(skb)->gso_size; 725 726 trace_hns3_tso(skb); 727 728 return 0; 729} 730 731static int hns3_get_l4_protocol(struct sk_buff *skb, u8 *ol4_proto, 732 u8 *il4_proto) 733{ 734 union l3_hdr_info l3; 735 unsigned char *l4_hdr; 736 unsigned char *exthdr; 737 u8 l4_proto_tmp; 738 __be16 frag_off; 739 740 /* find outer header point */ 741 l3.hdr = skb_network_header(skb); 742 l4_hdr = skb_transport_header(skb); 743 744 if (skb->protocol == htons(ETH_P_IPV6)) { 745 exthdr = l3.hdr + sizeof(*l3.v6); 746 l4_proto_tmp = l3.v6->nexthdr; 747 if (l4_hdr != exthdr) 748 ipv6_skip_exthdr(skb, exthdr - skb->data, 749 &l4_proto_tmp, &frag_off); 750 } else if (skb->protocol == htons(ETH_P_IP)) { 751 l4_proto_tmp = l3.v4->protocol; 752 } else { 753 return -EINVAL; 754 } 755 756 *ol4_proto = l4_proto_tmp; 757 758 /* tunnel packet */ 759 if (!skb->encapsulation) { 760 *il4_proto = 0; 761 return 0; 762 } 763 764 /* find inner header point */ 765 l3.hdr = skb_inner_network_header(skb); 766 l4_hdr = skb_inner_transport_header(skb); 767 768 if (l3.v6->version == 6) { 769 exthdr = l3.hdr + sizeof(*l3.v6); 770 l4_proto_tmp = l3.v6->nexthdr; 771 if (l4_hdr != exthdr) 772 ipv6_skip_exthdr(skb, exthdr - skb->data, 773 &l4_proto_tmp, &frag_off); 774 } else if (l3.v4->version == 4) { 775 l4_proto_tmp = l3.v4->protocol; 776 } 777 778 *il4_proto = l4_proto_tmp; 779 780 return 0; 781} 782 783/* when skb->encapsulation is 0, skb->ip_summed is CHECKSUM_PARTIAL 784 * and it is udp packet, which has a dest port as the IANA assigned. 785 * the hardware is expected to do the checksum offload, but the 786 * hardware will not do the checksum offload when udp dest port is 787 * 4789, 4790 or 6081. 788 */ 789static bool hns3_tunnel_csum_bug(struct sk_buff *skb) 790{ 791 union l4_hdr_info l4; 792 793 l4.hdr = skb_transport_header(skb); 794 795 if (!(!skb->encapsulation && 796 (l4.udp->dest == htons(IANA_VXLAN_UDP_PORT) || 797 l4.udp->dest == htons(GENEVE_UDP_PORT) || 798 l4.udp->dest == htons(4790)))) 799 return false; 800 801 return true; 802} 803 804static void hns3_set_outer_l2l3l4(struct sk_buff *skb, u8 ol4_proto, 805 u32 *ol_type_vlan_len_msec) 806{ 807 u32 l2_len, l3_len, l4_len; 808 unsigned char *il2_hdr; 809 union l3_hdr_info l3; 810 union l4_hdr_info l4; 811 812 l3.hdr = skb_network_header(skb); 813 l4.hdr = skb_transport_header(skb); 814 815 /* compute OL2 header size, defined in 2 Bytes */ 816 l2_len = l3.hdr - skb->data; 817 hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L2LEN_S, l2_len >> 1); 818 819 /* compute OL3 header size, defined in 4 Bytes */ 820 l3_len = l4.hdr - l3.hdr; 821 hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L3LEN_S, l3_len >> 2); 822 823 il2_hdr = skb_inner_mac_header(skb); 824 /* compute OL4 header size, defined in 4 Bytes */ 825 l4_len = il2_hdr - l4.hdr; 826 hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_L4LEN_S, l4_len >> 2); 827 828 /* define outer network header type */ 829 if (skb->protocol == htons(ETH_P_IP)) { 830 if (skb_is_gso(skb)) 831 hns3_set_field(*ol_type_vlan_len_msec, 832 HNS3_TXD_OL3T_S, 833 HNS3_OL3T_IPV4_CSUM); 834 else 835 hns3_set_field(*ol_type_vlan_len_msec, 836 HNS3_TXD_OL3T_S, 837 HNS3_OL3T_IPV4_NO_CSUM); 838 839 } else if (skb->protocol == htons(ETH_P_IPV6)) { 840 hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_OL3T_S, 841 HNS3_OL3T_IPV6); 842 } 843 844 if (ol4_proto == IPPROTO_UDP) 845 hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_TUNTYPE_S, 846 HNS3_TUN_MAC_IN_UDP); 847 else if (ol4_proto == IPPROTO_GRE) 848 hns3_set_field(*ol_type_vlan_len_msec, HNS3_TXD_TUNTYPE_S, 849 HNS3_TUN_NVGRE); 850} 851 852static int hns3_set_l2l3l4(struct sk_buff *skb, u8 ol4_proto, 853 u8 il4_proto, u32 *type_cs_vlan_tso, 854 u32 *ol_type_vlan_len_msec) 855{ 856 unsigned char *l2_hdr = skb->data; 857 u32 l4_proto = ol4_proto; 858 union l4_hdr_info l4; 859 union l3_hdr_info l3; 860 u32 l2_len, l3_len; 861 862 l4.hdr = skb_transport_header(skb); 863 l3.hdr = skb_network_header(skb); 864 865 /* handle encapsulation skb */ 866 if (skb->encapsulation) { 867 /* If this is a not UDP/GRE encapsulation skb */ 868 if (!(ol4_proto == IPPROTO_UDP || ol4_proto == IPPROTO_GRE)) { 869 /* drop the skb tunnel packet if hardware don't support, 870 * because hardware can't calculate csum when TSO. 871 */ 872 if (skb_is_gso(skb)) 873 return -EDOM; 874 875 /* the stack computes the IP header already, 876 * driver calculate l4 checksum when not TSO. 877 */ 878 return skb_checksum_help(skb); 879 } 880 881 hns3_set_outer_l2l3l4(skb, ol4_proto, ol_type_vlan_len_msec); 882 883 /* switch to inner header */ 884 l2_hdr = skb_inner_mac_header(skb); 885 l3.hdr = skb_inner_network_header(skb); 886 l4.hdr = skb_inner_transport_header(skb); 887 l4_proto = il4_proto; 888 } 889 890 if (l3.v4->version == 4) { 891 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L3T_S, 892 HNS3_L3T_IPV4); 893 894 /* the stack computes the IP header already, the only time we 895 * need the hardware to recompute it is in the case of TSO. 896 */ 897 if (skb_is_gso(skb)) 898 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L3CS_B, 1); 899 } else if (l3.v6->version == 6) { 900 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L3T_S, 901 HNS3_L3T_IPV6); 902 } 903 904 /* compute inner(/normal) L2 header size, defined in 2 Bytes */ 905 l2_len = l3.hdr - l2_hdr; 906 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L2LEN_S, l2_len >> 1); 907 908 /* compute inner(/normal) L3 header size, defined in 4 Bytes */ 909 l3_len = l4.hdr - l3.hdr; 910 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L3LEN_S, l3_len >> 2); 911 912 /* compute inner(/normal) L4 header size, defined in 4 Bytes */ 913 switch (l4_proto) { 914 case IPPROTO_TCP: 915 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1); 916 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4T_S, 917 HNS3_L4T_TCP); 918 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_S, 919 l4.tcp->doff); 920 break; 921 case IPPROTO_UDP: 922 if (hns3_tunnel_csum_bug(skb)) { 923 int ret = skb_put_padto(skb, HNS3_MIN_TUN_PKT_LEN); 924 925 return ret ? ret : skb_checksum_help(skb); 926 } 927 928 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1); 929 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4T_S, 930 HNS3_L4T_UDP); 931 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_S, 932 (sizeof(struct udphdr) >> 2)); 933 break; 934 case IPPROTO_SCTP: 935 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4CS_B, 1); 936 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4T_S, 937 HNS3_L4T_SCTP); 938 hns3_set_field(*type_cs_vlan_tso, HNS3_TXD_L4LEN_S, 939 (sizeof(struct sctphdr) >> 2)); 940 break; 941 default: 942 /* drop the skb tunnel packet if hardware don't support, 943 * because hardware can't calculate csum when TSO. 944 */ 945 if (skb_is_gso(skb)) 946 return -EDOM; 947 948 /* the stack computes the IP header already, 949 * driver calculate l4 checksum when not TSO. 950 */ 951 return skb_checksum_help(skb); 952 } 953 954 return 0; 955} 956 957static int hns3_handle_vtags(struct hns3_enet_ring *tx_ring, 958 struct sk_buff *skb) 959{ 960 struct hnae3_handle *handle = tx_ring->tqp->handle; 961 struct vlan_ethhdr *vhdr; 962 int rc; 963 964 if (!(skb->protocol == htons(ETH_P_8021Q) || 965 skb_vlan_tag_present(skb))) 966 return 0; 967 968 /* Since HW limitation, if port based insert VLAN enabled, only one VLAN 969 * header is allowed in skb, otherwise it will cause RAS error. 970 */ 971 if (unlikely(skb_vlan_tagged_multi(skb) && 972 handle->port_base_vlan_state == 973 HNAE3_PORT_BASE_VLAN_ENABLE)) 974 return -EINVAL; 975 976 if (skb->protocol == htons(ETH_P_8021Q) && 977 !(handle->kinfo.netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) { 978 /* When HW VLAN acceleration is turned off, and the stack 979 * sets the protocol to 802.1q, the driver just need to 980 * set the protocol to the encapsulated ethertype. 981 */ 982 skb->protocol = vlan_get_protocol(skb); 983 return 0; 984 } 985 986 if (skb_vlan_tag_present(skb)) { 987 /* Based on hw strategy, use out_vtag in two layer tag case, 988 * and use inner_vtag in one tag case. 989 */ 990 if (skb->protocol == htons(ETH_P_8021Q) && 991 handle->port_base_vlan_state == 992 HNAE3_PORT_BASE_VLAN_DISABLE) 993 rc = HNS3_OUTER_VLAN_TAG; 994 else 995 rc = HNS3_INNER_VLAN_TAG; 996 997 skb->protocol = vlan_get_protocol(skb); 998 return rc; 999 } 1000 1001 rc = skb_cow_head(skb, 0); 1002 if (unlikely(rc < 0)) 1003 return rc; 1004 1005 vhdr = skb_vlan_eth_hdr(skb); 1006 vhdr->h_vlan_TCI |= cpu_to_be16((skb->priority << VLAN_PRIO_SHIFT) 1007 & VLAN_PRIO_MASK); 1008 1009 skb->protocol = vlan_get_protocol(skb); 1010 return 0; 1011} 1012 1013static int hns3_fill_skb_desc(struct hns3_enet_ring *ring, 1014 struct sk_buff *skb, struct hns3_desc *desc) 1015{ 1016 u32 ol_type_vlan_len_msec = 0; 1017 u32 type_cs_vlan_tso = 0; 1018 u32 paylen = skb->len; 1019 u16 inner_vtag = 0; 1020 u16 out_vtag = 0; 1021 u16 mss = 0; 1022 int ret; 1023 1024 ret = hns3_handle_vtags(ring, skb); 1025 if (unlikely(ret < 0)) { 1026 u64_stats_update_begin(&ring->syncp); 1027 ring->stats.tx_vlan_err++; 1028 u64_stats_update_end(&ring->syncp); 1029 return ret; 1030 } else if (ret == HNS3_INNER_VLAN_TAG) { 1031 inner_vtag = skb_vlan_tag_get(skb); 1032 inner_vtag |= (skb->priority << VLAN_PRIO_SHIFT) & 1033 VLAN_PRIO_MASK; 1034 hns3_set_field(type_cs_vlan_tso, HNS3_TXD_VLAN_B, 1); 1035 } else if (ret == HNS3_OUTER_VLAN_TAG) { 1036 out_vtag = skb_vlan_tag_get(skb); 1037 out_vtag |= (skb->priority << VLAN_PRIO_SHIFT) & 1038 VLAN_PRIO_MASK; 1039 hns3_set_field(ol_type_vlan_len_msec, HNS3_TXD_OVLAN_B, 1040 1); 1041 } 1042 1043 if (skb->ip_summed == CHECKSUM_PARTIAL) { 1044 u8 ol4_proto, il4_proto; 1045 1046 skb_reset_mac_len(skb); 1047 1048 ret = hns3_get_l4_protocol(skb, &ol4_proto, &il4_proto); 1049 if (unlikely(ret < 0)) { 1050 u64_stats_update_begin(&ring->syncp); 1051 ring->stats.tx_l4_proto_err++; 1052 u64_stats_update_end(&ring->syncp); 1053 return ret; 1054 } 1055 1056 ret = hns3_set_l2l3l4(skb, ol4_proto, il4_proto, 1057 &type_cs_vlan_tso, 1058 &ol_type_vlan_len_msec); 1059 if (unlikely(ret < 0)) { 1060 u64_stats_update_begin(&ring->syncp); 1061 ring->stats.tx_l2l3l4_err++; 1062 u64_stats_update_end(&ring->syncp); 1063 return ret; 1064 } 1065 1066 ret = hns3_set_tso(skb, &paylen, &mss, 1067 &type_cs_vlan_tso); 1068 if (unlikely(ret < 0)) { 1069 u64_stats_update_begin(&ring->syncp); 1070 ring->stats.tx_tso_err++; 1071 u64_stats_update_end(&ring->syncp); 1072 return ret; 1073 } 1074 } 1075 1076 /* Set txbd */ 1077 desc->tx.ol_type_vlan_len_msec = 1078 cpu_to_le32(ol_type_vlan_len_msec); 1079 desc->tx.type_cs_vlan_tso_len = cpu_to_le32(type_cs_vlan_tso); 1080 desc->tx.paylen = cpu_to_le32(paylen); 1081 desc->tx.mss = cpu_to_le16(mss); 1082 desc->tx.vlan_tag = cpu_to_le16(inner_vtag); 1083 desc->tx.outer_vlan_tag = cpu_to_le16(out_vtag); 1084 1085 return 0; 1086} 1087 1088static int hns3_fill_desc(struct hns3_enet_ring *ring, void *priv, 1089 unsigned int size, enum hns_desc_type type) 1090{ 1091#define HNS3_LIKELY_BD_NUM 1 1092 1093 struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_use]; 1094 struct hns3_desc *desc = &ring->desc[ring->next_to_use]; 1095 struct device *dev = ring_to_dev(ring); 1096 skb_frag_t *frag; 1097 unsigned int frag_buf_num; 1098 int k, sizeoflast; 1099 dma_addr_t dma; 1100 1101 if (type == DESC_TYPE_FRAGLIST_SKB || 1102 type == DESC_TYPE_SKB) { 1103 struct sk_buff *skb = (struct sk_buff *)priv; 1104 1105 dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE); 1106 } else { 1107 frag = (skb_frag_t *)priv; 1108 dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE); 1109 } 1110 1111 if (unlikely(dma_mapping_error(dev, dma))) { 1112 u64_stats_update_begin(&ring->syncp); 1113 ring->stats.sw_err_cnt++; 1114 u64_stats_update_end(&ring->syncp); 1115 return -ENOMEM; 1116 } 1117 1118 desc_cb->priv = priv; 1119 desc_cb->length = size; 1120 desc_cb->dma = dma; 1121 desc_cb->type = type; 1122 1123 if (likely(size <= HNS3_MAX_BD_SIZE)) { 1124 desc->addr = cpu_to_le64(dma); 1125 desc->tx.send_size = cpu_to_le16(size); 1126 desc->tx.bdtp_fe_sc_vld_ra_ri = 1127 cpu_to_le16(BIT(HNS3_TXD_VLD_B)); 1128 1129 trace_hns3_tx_desc(ring, ring->next_to_use); 1130 ring_ptr_move_fw(ring, next_to_use); 1131 return HNS3_LIKELY_BD_NUM; 1132 } 1133 1134 frag_buf_num = hns3_tx_bd_count(size); 1135 sizeoflast = size % HNS3_MAX_BD_SIZE; 1136 sizeoflast = sizeoflast ? sizeoflast : HNS3_MAX_BD_SIZE; 1137 1138 /* When frag size is bigger than hardware limit, split this frag */ 1139 for (k = 0; k < frag_buf_num; k++) { 1140 /* now, fill the descriptor */ 1141 desc->addr = cpu_to_le64(dma + HNS3_MAX_BD_SIZE * k); 1142 desc->tx.send_size = cpu_to_le16((k == frag_buf_num - 1) ? 1143 (u16)sizeoflast : (u16)HNS3_MAX_BD_SIZE); 1144 desc->tx.bdtp_fe_sc_vld_ra_ri = 1145 cpu_to_le16(BIT(HNS3_TXD_VLD_B)); 1146 1147 trace_hns3_tx_desc(ring, ring->next_to_use); 1148 /* move ring pointer to next */ 1149 ring_ptr_move_fw(ring, next_to_use); 1150 1151 desc = &ring->desc[ring->next_to_use]; 1152 } 1153 1154 return frag_buf_num; 1155} 1156 1157static unsigned int hns3_skb_bd_num(struct sk_buff *skb, unsigned int *bd_size, 1158 unsigned int bd_num) 1159{ 1160 unsigned int size; 1161 int i; 1162 1163 size = skb_headlen(skb); 1164 while (size > HNS3_MAX_BD_SIZE) { 1165 bd_size[bd_num++] = HNS3_MAX_BD_SIZE; 1166 size -= HNS3_MAX_BD_SIZE; 1167 1168 if (bd_num > HNS3_MAX_TSO_BD_NUM) 1169 return bd_num; 1170 } 1171 1172 if (size) { 1173 bd_size[bd_num++] = size; 1174 if (bd_num > HNS3_MAX_TSO_BD_NUM) 1175 return bd_num; 1176 } 1177 1178 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1179 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1180 size = skb_frag_size(frag); 1181 if (!size) 1182 continue; 1183 1184 while (size > HNS3_MAX_BD_SIZE) { 1185 bd_size[bd_num++] = HNS3_MAX_BD_SIZE; 1186 size -= HNS3_MAX_BD_SIZE; 1187 1188 if (bd_num > HNS3_MAX_TSO_BD_NUM) 1189 return bd_num; 1190 } 1191 1192 bd_size[bd_num++] = size; 1193 if (bd_num > HNS3_MAX_TSO_BD_NUM) 1194 return bd_num; 1195 } 1196 1197 return bd_num; 1198} 1199 1200static unsigned int hns3_tx_bd_num(struct sk_buff *skb, unsigned int *bd_size, 1201 u8 max_non_tso_bd_num, unsigned int bd_num, 1202 unsigned int recursion_level) 1203{ 1204#define HNS3_MAX_RECURSION_LEVEL 24 1205 1206 struct sk_buff *frag_skb; 1207 1208 /* If the total len is within the max bd limit */ 1209 if (likely(skb->len <= HNS3_MAX_BD_SIZE && !recursion_level && 1210 !skb_has_frag_list(skb) && 1211 skb_shinfo(skb)->nr_frags < max_non_tso_bd_num)) 1212 return skb_shinfo(skb)->nr_frags + 1U; 1213 1214 if (unlikely(recursion_level >= HNS3_MAX_RECURSION_LEVEL)) 1215 return UINT_MAX; 1216 1217 bd_num = hns3_skb_bd_num(skb, bd_size, bd_num); 1218 1219 if (!skb_has_frag_list(skb) || bd_num > HNS3_MAX_TSO_BD_NUM) 1220 return bd_num; 1221 1222 skb_walk_frags(skb, frag_skb) { 1223 bd_num = hns3_tx_bd_num(frag_skb, bd_size, max_non_tso_bd_num, 1224 bd_num, recursion_level + 1); 1225 if (bd_num > HNS3_MAX_TSO_BD_NUM) 1226 return bd_num; 1227 } 1228 1229 return bd_num; 1230} 1231 1232static unsigned int hns3_gso_hdr_len(struct sk_buff *skb) 1233{ 1234 if (!skb->encapsulation) 1235 return skb_transport_offset(skb) + tcp_hdrlen(skb); 1236 1237 return skb_inner_transport_offset(skb) + inner_tcp_hdrlen(skb); 1238} 1239 1240/* HW need every continuous max_non_tso_bd_num buffer data to be larger 1241 * than MSS, we simplify it by ensuring skb_headlen + the first continuous 1242 * max_non_tso_bd_num - 1 frags to be larger than gso header len + mss, 1243 * and the remaining continuous max_non_tso_bd_num - 1 frags to be larger 1244 * than MSS except the last max_non_tso_bd_num - 1 frags. 1245 */ 1246static bool hns3_skb_need_linearized(struct sk_buff *skb, unsigned int *bd_size, 1247 unsigned int bd_num, u8 max_non_tso_bd_num) 1248{ 1249 unsigned int tot_len = 0; 1250 int i; 1251 1252 for (i = 0; i < max_non_tso_bd_num - 1U; i++) 1253 tot_len += bd_size[i]; 1254 1255 /* ensure the first max_non_tso_bd_num frags is greater than 1256 * mss + header 1257 */ 1258 if (tot_len + bd_size[max_non_tso_bd_num - 1U] < 1259 skb_shinfo(skb)->gso_size + hns3_gso_hdr_len(skb)) 1260 return true; 1261 1262 /* ensure every continuous max_non_tso_bd_num - 1 buffer is greater 1263 * than mss except the last one. 1264 */ 1265 for (i = 0; i < bd_num - max_non_tso_bd_num; i++) { 1266 tot_len -= bd_size[i]; 1267 tot_len += bd_size[i + max_non_tso_bd_num - 1U]; 1268 1269 if (tot_len < skb_shinfo(skb)->gso_size) 1270 return true; 1271 } 1272 1273 return false; 1274} 1275 1276void hns3_shinfo_pack(struct skb_shared_info *shinfo, __u32 *size) 1277{ 1278 int i; 1279 1280 for (i = 0; i < MAX_SKB_FRAGS; i++) 1281 size[i] = skb_frag_size(&shinfo->frags[i]); 1282} 1283 1284static int hns3_skb_linearize(struct hns3_enet_ring *ring, 1285 struct sk_buff *skb, 1286 unsigned int bd_num) 1287{ 1288 /* 'bd_num == UINT_MAX' means the skb' fraglist has a 1289 * recursion level of over HNS3_MAX_RECURSION_LEVEL. 1290 */ 1291 if (bd_num == UINT_MAX) { 1292 u64_stats_update_begin(&ring->syncp); 1293 ring->stats.over_max_recursion++; 1294 u64_stats_update_end(&ring->syncp); 1295 return -ENOMEM; 1296 } 1297 1298 /* The skb->len has exceeded the hw limitation, linearization 1299 * will not help. 1300 */ 1301 if (skb->len > HNS3_MAX_TSO_SIZE || 1302 (!skb_is_gso(skb) && skb->len > HNS3_MAX_NON_TSO_SIZE)) { 1303 u64_stats_update_begin(&ring->syncp); 1304 ring->stats.hw_limitation++; 1305 u64_stats_update_end(&ring->syncp); 1306 return -ENOMEM; 1307 } 1308 1309 if (__skb_linearize(skb)) { 1310 u64_stats_update_begin(&ring->syncp); 1311 ring->stats.sw_err_cnt++; 1312 u64_stats_update_end(&ring->syncp); 1313 return -ENOMEM; 1314 } 1315 1316 return 0; 1317} 1318 1319static int hns3_nic_maybe_stop_tx(struct hns3_enet_ring *ring, 1320 struct net_device *netdev, 1321 struct sk_buff *skb) 1322{ 1323 struct hns3_nic_priv *priv = netdev_priv(netdev); 1324 u8 max_non_tso_bd_num = priv->max_non_tso_bd_num; 1325 unsigned int bd_size[HNS3_MAX_TSO_BD_NUM + 1U]; 1326 unsigned int bd_num; 1327 1328 bd_num = hns3_tx_bd_num(skb, bd_size, max_non_tso_bd_num, 0, 0); 1329 if (unlikely(bd_num > max_non_tso_bd_num)) { 1330 if (bd_num <= HNS3_MAX_TSO_BD_NUM && skb_is_gso(skb) && 1331 !hns3_skb_need_linearized(skb, bd_size, bd_num, 1332 max_non_tso_bd_num)) { 1333 trace_hns3_over_max_bd(skb); 1334 goto out; 1335 } 1336 1337 if (hns3_skb_linearize(ring, skb, bd_num)) 1338 return -ENOMEM; 1339 1340 bd_num = hns3_tx_bd_count(skb->len); 1341 1342 u64_stats_update_begin(&ring->syncp); 1343 ring->stats.tx_copy++; 1344 u64_stats_update_end(&ring->syncp); 1345 } 1346 1347out: 1348 if (likely(ring_space(ring) >= bd_num)) 1349 return bd_num; 1350 1351 netif_stop_subqueue(netdev, ring->queue_index); 1352 smp_mb(); /* Memory barrier before checking ring_space */ 1353 1354 /* Start queue in case hns3_clean_tx_ring has just made room 1355 * available and has not seen the queue stopped state performed 1356 * by netif_stop_subqueue above. 1357 */ 1358 if (ring_space(ring) >= bd_num && netif_carrier_ok(netdev) && 1359 !test_bit(HNS3_NIC_STATE_DOWN, &priv->state)) { 1360 netif_start_subqueue(netdev, ring->queue_index); 1361 return bd_num; 1362 } 1363 1364 u64_stats_update_begin(&ring->syncp); 1365 ring->stats.tx_busy++; 1366 u64_stats_update_end(&ring->syncp); 1367 1368 return -EBUSY; 1369} 1370 1371static void hns3_clear_desc(struct hns3_enet_ring *ring, int next_to_use_orig) 1372{ 1373 struct device *dev = ring_to_dev(ring); 1374 unsigned int i; 1375 1376 for (i = 0; i < ring->desc_num; i++) { 1377 struct hns3_desc *desc = &ring->desc[ring->next_to_use]; 1378 1379 memset(desc, 0, sizeof(*desc)); 1380 1381 /* check if this is where we started */ 1382 if (ring->next_to_use == next_to_use_orig) 1383 break; 1384 1385 /* rollback one */ 1386 ring_ptr_move_bw(ring, next_to_use); 1387 1388 if (!ring->desc_cb[ring->next_to_use].dma) 1389 continue; 1390 1391 /* unmap the descriptor dma address */ 1392 if (ring->desc_cb[ring->next_to_use].type == DESC_TYPE_SKB || 1393 ring->desc_cb[ring->next_to_use].type == 1394 DESC_TYPE_FRAGLIST_SKB) 1395 dma_unmap_single(dev, 1396 ring->desc_cb[ring->next_to_use].dma, 1397 ring->desc_cb[ring->next_to_use].length, 1398 DMA_TO_DEVICE); 1399 else if (ring->desc_cb[ring->next_to_use].length) 1400 dma_unmap_page(dev, 1401 ring->desc_cb[ring->next_to_use].dma, 1402 ring->desc_cb[ring->next_to_use].length, 1403 DMA_TO_DEVICE); 1404 1405 ring->desc_cb[ring->next_to_use].length = 0; 1406 ring->desc_cb[ring->next_to_use].dma = 0; 1407 ring->desc_cb[ring->next_to_use].type = DESC_TYPE_UNKNOWN; 1408 } 1409} 1410 1411static int hns3_fill_skb_to_desc(struct hns3_enet_ring *ring, 1412 struct sk_buff *skb, enum hns_desc_type type) 1413{ 1414 unsigned int size = skb_headlen(skb); 1415 struct sk_buff *frag_skb; 1416 int i, ret, bd_num = 0; 1417 1418 if (size) { 1419 ret = hns3_fill_desc(ring, skb, size, type); 1420 if (unlikely(ret < 0)) 1421 return ret; 1422 1423 bd_num += ret; 1424 } 1425 1426 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1427 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1428 1429 size = skb_frag_size(frag); 1430 if (!size) 1431 continue; 1432 1433 ret = hns3_fill_desc(ring, frag, size, DESC_TYPE_PAGE); 1434 if (unlikely(ret < 0)) 1435 return ret; 1436 1437 bd_num += ret; 1438 } 1439 1440 skb_walk_frags(skb, frag_skb) { 1441 ret = hns3_fill_skb_to_desc(ring, frag_skb, 1442 DESC_TYPE_FRAGLIST_SKB); 1443 if (unlikely(ret < 0)) 1444 return ret; 1445 1446 bd_num += ret; 1447 } 1448 1449 return bd_num; 1450} 1451 1452static void hns3_tx_doorbell(struct hns3_enet_ring *ring, int num, 1453 bool doorbell) 1454{ 1455 ring->pending_buf += num; 1456 1457 if (!doorbell) { 1458 u64_stats_update_begin(&ring->syncp); 1459 ring->stats.tx_more++; 1460 u64_stats_update_end(&ring->syncp); 1461 return; 1462 } 1463 1464 if (!ring->pending_buf) 1465 return; 1466 1467 writel(ring->pending_buf, 1468 ring->tqp->io_base + HNS3_RING_TX_RING_TAIL_REG); 1469 ring->pending_buf = 0; 1470 WRITE_ONCE(ring->last_to_use, ring->next_to_use); 1471} 1472 1473netdev_tx_t hns3_nic_net_xmit(struct sk_buff *skb, struct net_device *netdev) 1474{ 1475 struct hns3_nic_priv *priv = netdev_priv(netdev); 1476 struct hns3_enet_ring *ring = &priv->ring[skb->queue_mapping]; 1477 struct netdev_queue *dev_queue; 1478 int pre_ntu, next_to_use_head; 1479 bool doorbell; 1480 int ret; 1481 1482 /* Hardware can only handle short frames above 32 bytes */ 1483 if (skb_put_padto(skb, HNS3_MIN_TX_LEN)) { 1484 hns3_tx_doorbell(ring, 0, !netdev_xmit_more()); 1485 return NETDEV_TX_OK; 1486 } 1487 1488 /* Prefetch the data used later */ 1489 prefetch(skb->data); 1490 1491 ret = hns3_nic_maybe_stop_tx(ring, netdev, skb); 1492 if (unlikely(ret <= 0)) { 1493 if (ret == -EBUSY) { 1494 hns3_tx_doorbell(ring, 0, true); 1495 return NETDEV_TX_BUSY; 1496 } 1497 1498 hns3_rl_err(netdev, "xmit error: %d!\n", ret); 1499 goto out_err_tx_ok; 1500 } 1501 1502 next_to_use_head = ring->next_to_use; 1503 1504 ret = hns3_fill_skb_desc(ring, skb, &ring->desc[ring->next_to_use]); 1505 if (unlikely(ret < 0)) 1506 goto fill_err; 1507 1508 /* 'ret < 0' means filling error, 'ret == 0' means skb->len is 1509 * zero, which is unlikely, and 'ret > 0' means how many tx desc 1510 * need to be notified to the hw. 1511 */ 1512 ret = hns3_fill_skb_to_desc(ring, skb, DESC_TYPE_SKB); 1513 if (unlikely(ret <= 0)) 1514 goto fill_err; 1515 1516 pre_ntu = ring->next_to_use ? (ring->next_to_use - 1) : 1517 (ring->desc_num - 1); 1518 ring->desc[pre_ntu].tx.bdtp_fe_sc_vld_ra_ri |= 1519 cpu_to_le16(BIT(HNS3_TXD_FE_B)); 1520 trace_hns3_tx_desc(ring, pre_ntu); 1521 1522 /* Complete translate all packets */ 1523 dev_queue = netdev_get_tx_queue(netdev, ring->queue_index); 1524 doorbell = __netdev_tx_sent_queue(dev_queue, skb->len, 1525 netdev_xmit_more()); 1526 hns3_tx_doorbell(ring, ret, doorbell); 1527 1528 return NETDEV_TX_OK; 1529 1530fill_err: 1531 hns3_clear_desc(ring, next_to_use_head); 1532 1533out_err_tx_ok: 1534 dev_kfree_skb_any(skb); 1535 hns3_tx_doorbell(ring, 0, !netdev_xmit_more()); 1536 return NETDEV_TX_OK; 1537} 1538 1539static int hns3_nic_net_set_mac_address(struct net_device *netdev, void *p) 1540{ 1541 struct hnae3_handle *h = hns3_get_handle(netdev); 1542 struct sockaddr *mac_addr = p; 1543 int ret; 1544 1545 if (!mac_addr || !is_valid_ether_addr((const u8 *)mac_addr->sa_data)) 1546 return -EADDRNOTAVAIL; 1547 1548 if (ether_addr_equal(netdev->dev_addr, mac_addr->sa_data)) { 1549 netdev_info(netdev, "already using mac address %pM\n", 1550 mac_addr->sa_data); 1551 return 0; 1552 } 1553 1554 /* For VF device, if there is a perm_addr, then the user will not 1555 * be allowed to change the address. 1556 */ 1557 if (!hns3_is_phys_func(h->pdev) && 1558 !is_zero_ether_addr(netdev->perm_addr)) { 1559 netdev_err(netdev, "has permanent MAC %pM, user MAC %pM not allow\n", 1560 netdev->perm_addr, mac_addr->sa_data); 1561 return -EPERM; 1562 } 1563 1564 ret = h->ae_algo->ops->set_mac_addr(h, mac_addr->sa_data, false); 1565 if (ret) { 1566 netdev_err(netdev, "set_mac_address fail, ret=%d!\n", ret); 1567 return ret; 1568 } 1569 1570 ether_addr_copy(netdev->dev_addr, mac_addr->sa_data); 1571 1572 return 0; 1573} 1574 1575static int hns3_nic_do_ioctl(struct net_device *netdev, 1576 struct ifreq *ifr, int cmd) 1577{ 1578 struct hnae3_handle *h = hns3_get_handle(netdev); 1579 1580 if (!netif_running(netdev)) 1581 return -EINVAL; 1582 1583 if (!h->ae_algo->ops->do_ioctl) 1584 return -EOPNOTSUPP; 1585 1586 return h->ae_algo->ops->do_ioctl(h, ifr, cmd); 1587} 1588 1589static int hns3_nic_set_features(struct net_device *netdev, 1590 netdev_features_t features) 1591{ 1592 netdev_features_t changed = netdev->features ^ features; 1593 struct hns3_nic_priv *priv = netdev_priv(netdev); 1594 struct hnae3_handle *h = priv->ae_handle; 1595 bool enable; 1596 int ret; 1597 1598 if (changed & (NETIF_F_GRO_HW) && h->ae_algo->ops->set_gro_en) { 1599 enable = !!(features & NETIF_F_GRO_HW); 1600 ret = h->ae_algo->ops->set_gro_en(h, enable); 1601 if (ret) 1602 return ret; 1603 } 1604 1605 if ((changed & NETIF_F_HW_VLAN_CTAG_RX) && 1606 h->ae_algo->ops->enable_hw_strip_rxvtag) { 1607 enable = !!(features & NETIF_F_HW_VLAN_CTAG_RX); 1608 ret = h->ae_algo->ops->enable_hw_strip_rxvtag(h, enable); 1609 if (ret) 1610 return ret; 1611 } 1612 1613 if ((changed & NETIF_F_NTUPLE) && h->ae_algo->ops->enable_fd) { 1614 enable = !!(features & NETIF_F_NTUPLE); 1615 h->ae_algo->ops->enable_fd(h, enable); 1616 } 1617 1618 netdev->features = features; 1619 return 0; 1620} 1621 1622static netdev_features_t hns3_features_check(struct sk_buff *skb, 1623 struct net_device *dev, 1624 netdev_features_t features) 1625{ 1626#define HNS3_MAX_HDR_LEN 480U 1627#define HNS3_MAX_L4_HDR_LEN 60U 1628 1629 size_t len; 1630 1631 if (skb->ip_summed != CHECKSUM_PARTIAL) 1632 return features; 1633 1634 if (skb->encapsulation) 1635 len = skb_inner_transport_header(skb) - skb->data; 1636 else 1637 len = skb_transport_header(skb) - skb->data; 1638 1639 /* Assume L4 is 60 byte as TCP is the only protocol with a 1640 * a flexible value, and it's max len is 60 bytes. 1641 */ 1642 len += HNS3_MAX_L4_HDR_LEN; 1643 1644 /* Hardware only supports checksum on the skb with a max header 1645 * len of 480 bytes. 1646 */ 1647 if (len > HNS3_MAX_HDR_LEN) 1648 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 1649 1650 return features; 1651} 1652 1653static void hns3_nic_get_stats64(struct net_device *netdev, 1654 struct rtnl_link_stats64 *stats) 1655{ 1656 struct hns3_nic_priv *priv = netdev_priv(netdev); 1657 int queue_num = priv->ae_handle->kinfo.num_tqps; 1658 struct hnae3_handle *handle = priv->ae_handle; 1659 struct hns3_enet_ring *ring; 1660 u64 rx_length_errors = 0; 1661 u64 rx_crc_errors = 0; 1662 u64 rx_multicast = 0; 1663 unsigned int start; 1664 u64 tx_errors = 0; 1665 u64 rx_errors = 0; 1666 unsigned int idx; 1667 u64 tx_bytes = 0; 1668 u64 rx_bytes = 0; 1669 u64 tx_pkts = 0; 1670 u64 rx_pkts = 0; 1671 u64 tx_drop = 0; 1672 u64 rx_drop = 0; 1673 1674 if (test_bit(HNS3_NIC_STATE_DOWN, &priv->state)) 1675 return; 1676 1677 handle->ae_algo->ops->update_stats(handle, &netdev->stats); 1678 1679 for (idx = 0; idx < queue_num; idx++) { 1680 /* fetch the tx stats */ 1681 ring = &priv->ring[idx]; 1682 do { 1683 start = u64_stats_fetch_begin_irq(&ring->syncp); 1684 tx_bytes += ring->stats.tx_bytes; 1685 tx_pkts += ring->stats.tx_pkts; 1686 tx_drop += ring->stats.sw_err_cnt; 1687 tx_drop += ring->stats.tx_vlan_err; 1688 tx_drop += ring->stats.tx_l4_proto_err; 1689 tx_drop += ring->stats.tx_l2l3l4_err; 1690 tx_drop += ring->stats.tx_tso_err; 1691 tx_drop += ring->stats.over_max_recursion; 1692 tx_drop += ring->stats.hw_limitation; 1693 tx_errors += ring->stats.sw_err_cnt; 1694 tx_errors += ring->stats.tx_vlan_err; 1695 tx_errors += ring->stats.tx_l4_proto_err; 1696 tx_errors += ring->stats.tx_l2l3l4_err; 1697 tx_errors += ring->stats.tx_tso_err; 1698 tx_errors += ring->stats.over_max_recursion; 1699 tx_errors += ring->stats.hw_limitation; 1700 } while (u64_stats_fetch_retry_irq(&ring->syncp, start)); 1701 1702 /* fetch the rx stats */ 1703 ring = &priv->ring[idx + queue_num]; 1704 do { 1705 start = u64_stats_fetch_begin_irq(&ring->syncp); 1706 rx_bytes += ring->stats.rx_bytes; 1707 rx_pkts += ring->stats.rx_pkts; 1708 rx_drop += ring->stats.l2_err; 1709 rx_errors += ring->stats.l2_err; 1710 rx_errors += ring->stats.l3l4_csum_err; 1711 rx_crc_errors += ring->stats.l2_err; 1712 rx_multicast += ring->stats.rx_multicast; 1713 rx_length_errors += ring->stats.err_pkt_len; 1714 } while (u64_stats_fetch_retry_irq(&ring->syncp, start)); 1715 } 1716 1717 stats->tx_bytes = tx_bytes; 1718 stats->tx_packets = tx_pkts; 1719 stats->rx_bytes = rx_bytes; 1720 stats->rx_packets = rx_pkts; 1721 1722 stats->rx_errors = rx_errors; 1723 stats->multicast = rx_multicast; 1724 stats->rx_length_errors = rx_length_errors; 1725 stats->rx_crc_errors = rx_crc_errors; 1726 stats->rx_missed_errors = netdev->stats.rx_missed_errors; 1727 1728 stats->tx_errors = tx_errors; 1729 stats->rx_dropped = rx_drop; 1730 stats->tx_dropped = tx_drop; 1731 stats->collisions = netdev->stats.collisions; 1732 stats->rx_over_errors = netdev->stats.rx_over_errors; 1733 stats->rx_frame_errors = netdev->stats.rx_frame_errors; 1734 stats->rx_fifo_errors = netdev->stats.rx_fifo_errors; 1735 stats->tx_aborted_errors = netdev->stats.tx_aborted_errors; 1736 stats->tx_carrier_errors = netdev->stats.tx_carrier_errors; 1737 stats->tx_fifo_errors = netdev->stats.tx_fifo_errors; 1738 stats->tx_heartbeat_errors = netdev->stats.tx_heartbeat_errors; 1739 stats->tx_window_errors = netdev->stats.tx_window_errors; 1740 stats->rx_compressed = netdev->stats.rx_compressed; 1741 stats->tx_compressed = netdev->stats.tx_compressed; 1742} 1743 1744static int hns3_setup_tc(struct net_device *netdev, void *type_data) 1745{ 1746 struct tc_mqprio_qopt_offload *mqprio_qopt = type_data; 1747 u8 *prio_tc = mqprio_qopt->qopt.prio_tc_map; 1748 struct hnae3_knic_private_info *kinfo; 1749 u8 tc = mqprio_qopt->qopt.num_tc; 1750 u16 mode = mqprio_qopt->mode; 1751 u8 hw = mqprio_qopt->qopt.hw; 1752 struct hnae3_handle *h; 1753 1754 if (!((hw == TC_MQPRIO_HW_OFFLOAD_TCS && 1755 mode == TC_MQPRIO_MODE_CHANNEL) || (!hw && tc == 0))) 1756 return -EOPNOTSUPP; 1757 1758 if (tc > HNAE3_MAX_TC) 1759 return -EINVAL; 1760 1761 if (!netdev) 1762 return -EINVAL; 1763 1764 h = hns3_get_handle(netdev); 1765 kinfo = &h->kinfo; 1766 1767 netif_dbg(h, drv, netdev, "setup tc: num_tc=%u\n", tc); 1768 1769 return (kinfo->dcb_ops && kinfo->dcb_ops->setup_tc) ? 1770 kinfo->dcb_ops->setup_tc(h, tc ? tc : 1, prio_tc) : -EOPNOTSUPP; 1771} 1772 1773static int hns3_nic_setup_tc(struct net_device *dev, enum tc_setup_type type, 1774 void *type_data) 1775{ 1776 if (type != TC_SETUP_QDISC_MQPRIO) 1777 return -EOPNOTSUPP; 1778 1779 return hns3_setup_tc(dev, type_data); 1780} 1781 1782static int hns3_vlan_rx_add_vid(struct net_device *netdev, 1783 __be16 proto, u16 vid) 1784{ 1785 struct hnae3_handle *h = hns3_get_handle(netdev); 1786 int ret = -EIO; 1787 1788 if (h->ae_algo->ops->set_vlan_filter) 1789 ret = h->ae_algo->ops->set_vlan_filter(h, proto, vid, false); 1790 1791 return ret; 1792} 1793 1794static int hns3_vlan_rx_kill_vid(struct net_device *netdev, 1795 __be16 proto, u16 vid) 1796{ 1797 struct hnae3_handle *h = hns3_get_handle(netdev); 1798 int ret = -EIO; 1799 1800 if (h->ae_algo->ops->set_vlan_filter) 1801 ret = h->ae_algo->ops->set_vlan_filter(h, proto, vid, true); 1802 1803 return ret; 1804} 1805 1806static int hns3_ndo_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, 1807 u8 qos, __be16 vlan_proto) 1808{ 1809 struct hnae3_handle *h = hns3_get_handle(netdev); 1810 int ret = -EIO; 1811 1812 netif_dbg(h, drv, netdev, 1813 "set vf vlan: vf=%d, vlan=%u, qos=%u, vlan_proto=0x%x\n", 1814 vf, vlan, qos, ntohs(vlan_proto)); 1815 1816 if (h->ae_algo->ops->set_vf_vlan_filter) 1817 ret = h->ae_algo->ops->set_vf_vlan_filter(h, vf, vlan, 1818 qos, vlan_proto); 1819 1820 return ret; 1821} 1822 1823static int hns3_set_vf_spoofchk(struct net_device *netdev, int vf, bool enable) 1824{ 1825 struct hnae3_handle *handle = hns3_get_handle(netdev); 1826 1827 if (hns3_nic_resetting(netdev)) 1828 return -EBUSY; 1829 1830 if (!handle->ae_algo->ops->set_vf_spoofchk) 1831 return -EOPNOTSUPP; 1832 1833 return handle->ae_algo->ops->set_vf_spoofchk(handle, vf, enable); 1834} 1835 1836static int hns3_set_vf_trust(struct net_device *netdev, int vf, bool enable) 1837{ 1838 struct hnae3_handle *handle = hns3_get_handle(netdev); 1839 1840 if (!handle->ae_algo->ops->set_vf_trust) 1841 return -EOPNOTSUPP; 1842 1843 return handle->ae_algo->ops->set_vf_trust(handle, vf, enable); 1844} 1845 1846static int hns3_nic_change_mtu(struct net_device *netdev, int new_mtu) 1847{ 1848 struct hnae3_handle *h = hns3_get_handle(netdev); 1849 int ret; 1850 1851 if (hns3_nic_resetting(netdev)) 1852 return -EBUSY; 1853 1854 if (!h->ae_algo->ops->set_mtu) 1855 return -EOPNOTSUPP; 1856 1857 netif_dbg(h, drv, netdev, 1858 "change mtu from %u to %d\n", netdev->mtu, new_mtu); 1859 1860 ret = h->ae_algo->ops->set_mtu(h, new_mtu); 1861 if (ret) 1862 netdev_err(netdev, "failed to change MTU in hardware %d\n", 1863 ret); 1864 else 1865 netdev->mtu = new_mtu; 1866 1867 return ret; 1868} 1869 1870static bool hns3_get_tx_timeo_queue_info(struct net_device *ndev) 1871{ 1872 struct hns3_nic_priv *priv = netdev_priv(ndev); 1873 struct hnae3_handle *h = hns3_get_handle(ndev); 1874 struct hns3_enet_ring *tx_ring; 1875 struct napi_struct *napi; 1876 int timeout_queue = 0; 1877 int hw_head, hw_tail; 1878 int fbd_num, fbd_oft; 1879 int ebd_num, ebd_oft; 1880 int bd_num, bd_err; 1881 int ring_en, tc; 1882 int i; 1883 1884 /* Find the stopped queue the same way the stack does */ 1885 for (i = 0; i < ndev->num_tx_queues; i++) { 1886 struct netdev_queue *q; 1887 unsigned long trans_start; 1888 1889 q = netdev_get_tx_queue(ndev, i); 1890 trans_start = q->trans_start; 1891 if (netif_xmit_stopped(q) && 1892 time_after(jiffies, 1893 (trans_start + ndev->watchdog_timeo))) { 1894 timeout_queue = i; 1895 netdev_info(ndev, "queue state: 0x%lx, delta msecs: %u\n", 1896 q->state, 1897 jiffies_to_msecs(jiffies - trans_start)); 1898 break; 1899 } 1900 } 1901 1902 if (i == ndev->num_tx_queues) { 1903 netdev_info(ndev, 1904 "no netdev TX timeout queue found, timeout count: %llu\n", 1905 priv->tx_timeout_count); 1906 return false; 1907 } 1908 1909 priv->tx_timeout_count++; 1910 1911 tx_ring = &priv->ring[timeout_queue]; 1912 napi = &tx_ring->tqp_vector->napi; 1913 1914 netdev_info(ndev, 1915 "tx_timeout count: %llu, queue id: %d, SW_NTU: 0x%x, SW_NTC: 0x%x, napi state: %lu\n", 1916 priv->tx_timeout_count, timeout_queue, tx_ring->next_to_use, 1917 tx_ring->next_to_clean, napi->state); 1918 1919 netdev_info(ndev, 1920 "tx_pkts: %llu, tx_bytes: %llu, sw_err_cnt: %llu, tx_pending: %d\n", 1921 tx_ring->stats.tx_pkts, tx_ring->stats.tx_bytes, 1922 tx_ring->stats.sw_err_cnt, tx_ring->pending_buf); 1923 1924 netdev_info(ndev, 1925 "seg_pkt_cnt: %llu, tx_more: %llu, restart_queue: %llu, tx_busy: %llu\n", 1926 tx_ring->stats.seg_pkt_cnt, tx_ring->stats.tx_more, 1927 tx_ring->stats.restart_queue, tx_ring->stats.tx_busy); 1928 1929 /* When mac received many pause frames continuous, it's unable to send 1930 * packets, which may cause tx timeout 1931 */ 1932 if (h->ae_algo->ops->get_mac_stats) { 1933 struct hns3_mac_stats mac_stats; 1934 1935 h->ae_algo->ops->get_mac_stats(h, &mac_stats); 1936 netdev_info(ndev, "tx_pause_cnt: %llu, rx_pause_cnt: %llu\n", 1937 mac_stats.tx_pause_cnt, mac_stats.rx_pause_cnt); 1938 } 1939 1940 hw_head = readl_relaxed(tx_ring->tqp->io_base + 1941 HNS3_RING_TX_RING_HEAD_REG); 1942 hw_tail = readl_relaxed(tx_ring->tqp->io_base + 1943 HNS3_RING_TX_RING_TAIL_REG); 1944 fbd_num = readl_relaxed(tx_ring->tqp->io_base + 1945 HNS3_RING_TX_RING_FBDNUM_REG); 1946 fbd_oft = readl_relaxed(tx_ring->tqp->io_base + 1947 HNS3_RING_TX_RING_OFFSET_REG); 1948 ebd_num = readl_relaxed(tx_ring->tqp->io_base + 1949 HNS3_RING_TX_RING_EBDNUM_REG); 1950 ebd_oft = readl_relaxed(tx_ring->tqp->io_base + 1951 HNS3_RING_TX_RING_EBD_OFFSET_REG); 1952 bd_num = readl_relaxed(tx_ring->tqp->io_base + 1953 HNS3_RING_TX_RING_BD_NUM_REG); 1954 bd_err = readl_relaxed(tx_ring->tqp->io_base + 1955 HNS3_RING_TX_RING_BD_ERR_REG); 1956 ring_en = readl_relaxed(tx_ring->tqp->io_base + HNS3_RING_EN_REG); 1957 tc = readl_relaxed(tx_ring->tqp->io_base + HNS3_RING_TX_RING_TC_REG); 1958 1959 netdev_info(ndev, 1960 "BD_NUM: 0x%x HW_HEAD: 0x%x, HW_TAIL: 0x%x, BD_ERR: 0x%x, INT: 0x%x\n", 1961 bd_num, hw_head, hw_tail, bd_err, 1962 readl(tx_ring->tqp_vector->mask_addr)); 1963 netdev_info(ndev, 1964 "RING_EN: 0x%x, TC: 0x%x, FBD_NUM: 0x%x FBD_OFT: 0x%x, EBD_NUM: 0x%x, EBD_OFT: 0x%x\n", 1965 ring_en, tc, fbd_num, fbd_oft, ebd_num, ebd_oft); 1966 1967 return true; 1968} 1969 1970static void hns3_nic_net_timeout(struct net_device *ndev, unsigned int txqueue) 1971{ 1972 struct hns3_nic_priv *priv = netdev_priv(ndev); 1973 struct hnae3_handle *h = priv->ae_handle; 1974 1975 if (!hns3_get_tx_timeo_queue_info(ndev)) 1976 return; 1977 1978 /* request the reset, and let the hclge to determine 1979 * which reset level should be done 1980 */ 1981 if (h->ae_algo->ops->reset_event) 1982 h->ae_algo->ops->reset_event(h->pdev, h); 1983} 1984 1985#ifdef CONFIG_RFS_ACCEL 1986static int hns3_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb, 1987 u16 rxq_index, u32 flow_id) 1988{ 1989 struct hnae3_handle *h = hns3_get_handle(dev); 1990 struct flow_keys fkeys; 1991 1992 if (!h->ae_algo->ops->add_arfs_entry) 1993 return -EOPNOTSUPP; 1994 1995 if (skb->encapsulation) 1996 return -EPROTONOSUPPORT; 1997 1998 if (!skb_flow_dissect_flow_keys(skb, &fkeys, 0)) 1999 return -EPROTONOSUPPORT; 2000 2001 if ((fkeys.basic.n_proto != htons(ETH_P_IP) && 2002 fkeys.basic.n_proto != htons(ETH_P_IPV6)) || 2003 (fkeys.basic.ip_proto != IPPROTO_TCP && 2004 fkeys.basic.ip_proto != IPPROTO_UDP)) 2005 return -EPROTONOSUPPORT; 2006 2007 return h->ae_algo->ops->add_arfs_entry(h, rxq_index, flow_id, &fkeys); 2008} 2009#endif 2010 2011static int hns3_nic_get_vf_config(struct net_device *ndev, int vf, 2012 struct ifla_vf_info *ivf) 2013{ 2014 struct hnae3_handle *h = hns3_get_handle(ndev); 2015 2016 if (!h->ae_algo->ops->get_vf_config) 2017 return -EOPNOTSUPP; 2018 2019 return h->ae_algo->ops->get_vf_config(h, vf, ivf); 2020} 2021 2022static int hns3_nic_set_vf_link_state(struct net_device *ndev, int vf, 2023 int link_state) 2024{ 2025 struct hnae3_handle *h = hns3_get_handle(ndev); 2026 2027 if (!h->ae_algo->ops->set_vf_link_state) 2028 return -EOPNOTSUPP; 2029 2030 return h->ae_algo->ops->set_vf_link_state(h, vf, link_state); 2031} 2032 2033static int hns3_nic_set_vf_rate(struct net_device *ndev, int vf, 2034 int min_tx_rate, int max_tx_rate) 2035{ 2036 struct hnae3_handle *h = hns3_get_handle(ndev); 2037 2038 if (!h->ae_algo->ops->set_vf_rate) 2039 return -EOPNOTSUPP; 2040 2041 return h->ae_algo->ops->set_vf_rate(h, vf, min_tx_rate, max_tx_rate, 2042 false); 2043} 2044 2045static int hns3_nic_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac) 2046{ 2047 struct hnae3_handle *h = hns3_get_handle(netdev); 2048 2049 if (!h->ae_algo->ops->set_vf_mac) 2050 return -EOPNOTSUPP; 2051 2052 if (is_multicast_ether_addr(mac)) { 2053 netdev_err(netdev, 2054 "Invalid MAC:%pM specified. Could not set MAC\n", 2055 mac); 2056 return -EINVAL; 2057 } 2058 2059 return h->ae_algo->ops->set_vf_mac(h, vf_id, mac); 2060} 2061 2062static const struct net_device_ops hns3_nic_netdev_ops = { 2063 .ndo_open = hns3_nic_net_open, 2064 .ndo_stop = hns3_nic_net_stop, 2065 .ndo_start_xmit = hns3_nic_net_xmit, 2066 .ndo_tx_timeout = hns3_nic_net_timeout, 2067 .ndo_set_mac_address = hns3_nic_net_set_mac_address, 2068 .ndo_do_ioctl = hns3_nic_do_ioctl, 2069 .ndo_change_mtu = hns3_nic_change_mtu, 2070 .ndo_set_features = hns3_nic_set_features, 2071 .ndo_features_check = hns3_features_check, 2072 .ndo_get_stats64 = hns3_nic_get_stats64, 2073 .ndo_setup_tc = hns3_nic_setup_tc, 2074 .ndo_set_rx_mode = hns3_nic_set_rx_mode, 2075 .ndo_vlan_rx_add_vid = hns3_vlan_rx_add_vid, 2076 .ndo_vlan_rx_kill_vid = hns3_vlan_rx_kill_vid, 2077 .ndo_set_vf_vlan = hns3_ndo_set_vf_vlan, 2078 .ndo_set_vf_spoofchk = hns3_set_vf_spoofchk, 2079 .ndo_set_vf_trust = hns3_set_vf_trust, 2080#ifdef CONFIG_RFS_ACCEL 2081 .ndo_rx_flow_steer = hns3_rx_flow_steer, 2082#endif 2083 .ndo_get_vf_config = hns3_nic_get_vf_config, 2084 .ndo_set_vf_link_state = hns3_nic_set_vf_link_state, 2085 .ndo_set_vf_rate = hns3_nic_set_vf_rate, 2086 .ndo_set_vf_mac = hns3_nic_set_vf_mac, 2087}; 2088 2089bool hns3_is_phys_func(struct pci_dev *pdev) 2090{ 2091 u32 dev_id = pdev->device; 2092 2093 switch (dev_id) { 2094 case HNAE3_DEV_ID_GE: 2095 case HNAE3_DEV_ID_25GE: 2096 case HNAE3_DEV_ID_25GE_RDMA: 2097 case HNAE3_DEV_ID_25GE_RDMA_MACSEC: 2098 case HNAE3_DEV_ID_50GE_RDMA: 2099 case HNAE3_DEV_ID_50GE_RDMA_MACSEC: 2100 case HNAE3_DEV_ID_100G_RDMA_MACSEC: 2101 case HNAE3_DEV_ID_200G_RDMA: 2102 return true; 2103 case HNAE3_DEV_ID_VF: 2104 case HNAE3_DEV_ID_RDMA_DCB_PFC_VF: 2105 return false; 2106 default: 2107 dev_warn(&pdev->dev, "un-recognized pci device-id %u", 2108 dev_id); 2109 } 2110 2111 return false; 2112} 2113 2114static void hns3_disable_sriov(struct pci_dev *pdev) 2115{ 2116 /* If our VFs are assigned we cannot shut down SR-IOV 2117 * without causing issues, so just leave the hardware 2118 * available but disabled 2119 */ 2120 if (pci_vfs_assigned(pdev)) { 2121 dev_warn(&pdev->dev, 2122 "disabling driver while VFs are assigned\n"); 2123 return; 2124 } 2125 2126 pci_disable_sriov(pdev); 2127} 2128 2129/* hns3_probe - Device initialization routine 2130 * @pdev: PCI device information struct 2131 * @ent: entry in hns3_pci_tbl 2132 * 2133 * hns3_probe initializes a PF identified by a pci_dev structure. 2134 * The OS initialization, configuring of the PF private structure, 2135 * and a hardware reset occur. 2136 * 2137 * Returns 0 on success, negative on failure 2138 */ 2139static int hns3_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 2140{ 2141 struct hnae3_ae_dev *ae_dev; 2142 int ret; 2143 2144 ae_dev = devm_kzalloc(&pdev->dev, sizeof(*ae_dev), GFP_KERNEL); 2145 if (!ae_dev) 2146 return -ENOMEM; 2147 2148 ae_dev->pdev = pdev; 2149 ae_dev->flag = ent->driver_data; 2150 pci_set_drvdata(pdev, ae_dev); 2151 2152 ret = hnae3_register_ae_dev(ae_dev); 2153 if (ret) 2154 pci_set_drvdata(pdev, NULL); 2155 2156 return ret; 2157} 2158 2159/* hns3_remove - Device removal routine 2160 * @pdev: PCI device information struct 2161 */ 2162static void hns3_remove(struct pci_dev *pdev) 2163{ 2164 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev); 2165 2166 if (hns3_is_phys_func(pdev) && IS_ENABLED(CONFIG_PCI_IOV)) 2167 hns3_disable_sriov(pdev); 2168 2169 hnae3_unregister_ae_dev(ae_dev); 2170 pci_set_drvdata(pdev, NULL); 2171} 2172 2173/** 2174 * hns3_pci_sriov_configure 2175 * @pdev: pointer to a pci_dev structure 2176 * @num_vfs: number of VFs to allocate 2177 * 2178 * Enable or change the number of VFs. Called when the user updates the number 2179 * of VFs in sysfs. 2180 **/ 2181static int hns3_pci_sriov_configure(struct pci_dev *pdev, int num_vfs) 2182{ 2183 int ret; 2184 2185 if (!(hns3_is_phys_func(pdev) && IS_ENABLED(CONFIG_PCI_IOV))) { 2186 dev_warn(&pdev->dev, "Can not config SRIOV\n"); 2187 return -EINVAL; 2188 } 2189 2190 if (num_vfs) { 2191 ret = pci_enable_sriov(pdev, num_vfs); 2192 if (ret) 2193 dev_err(&pdev->dev, "SRIOV enable failed %d\n", ret); 2194 else 2195 return num_vfs; 2196 } else if (!pci_vfs_assigned(pdev)) { 2197 pci_disable_sriov(pdev); 2198 } else { 2199 dev_warn(&pdev->dev, 2200 "Unable to free VFs because some are assigned to VMs.\n"); 2201 } 2202 2203 return 0; 2204} 2205 2206static void hns3_shutdown(struct pci_dev *pdev) 2207{ 2208 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev); 2209 2210 hnae3_unregister_ae_dev(ae_dev); 2211 pci_set_drvdata(pdev, NULL); 2212 2213 if (system_state == SYSTEM_POWER_OFF) 2214 pci_set_power_state(pdev, PCI_D3hot); 2215} 2216 2217static pci_ers_result_t hns3_error_detected(struct pci_dev *pdev, 2218 pci_channel_state_t state) 2219{ 2220 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev); 2221 pci_ers_result_t ret; 2222 2223 dev_info(&pdev->dev, "PCI error detected, state(=%d)!!\n", state); 2224 2225 if (state == pci_channel_io_perm_failure) 2226 return PCI_ERS_RESULT_DISCONNECT; 2227 2228 if (!ae_dev || !ae_dev->ops) { 2229 dev_err(&pdev->dev, 2230 "Can't recover - error happened before device initialized\n"); 2231 return PCI_ERS_RESULT_NONE; 2232 } 2233 2234 if (ae_dev->ops->handle_hw_ras_error) 2235 ret = ae_dev->ops->handle_hw_ras_error(ae_dev); 2236 else 2237 return PCI_ERS_RESULT_NONE; 2238 2239 return ret; 2240} 2241 2242static pci_ers_result_t hns3_slot_reset(struct pci_dev *pdev) 2243{ 2244 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev); 2245 const struct hnae3_ae_ops *ops; 2246 enum hnae3_reset_type reset_type; 2247 struct device *dev = &pdev->dev; 2248 2249 if (!ae_dev || !ae_dev->ops) 2250 return PCI_ERS_RESULT_NONE; 2251 2252 ops = ae_dev->ops; 2253 /* request the reset */ 2254 if (ops->reset_event && ops->get_reset_level && 2255 ops->set_default_reset_request) { 2256 if (ae_dev->hw_err_reset_req) { 2257 reset_type = ops->get_reset_level(ae_dev, 2258 &ae_dev->hw_err_reset_req); 2259 ops->set_default_reset_request(ae_dev, reset_type); 2260 dev_info(dev, "requesting reset due to PCI error\n"); 2261 ops->reset_event(pdev, NULL); 2262 } 2263 2264 return PCI_ERS_RESULT_RECOVERED; 2265 } 2266 2267 return PCI_ERS_RESULT_DISCONNECT; 2268} 2269 2270static void hns3_reset_prepare(struct pci_dev *pdev) 2271{ 2272 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev); 2273 2274 dev_info(&pdev->dev, "FLR prepare\n"); 2275 if (ae_dev && ae_dev->ops && ae_dev->ops->flr_prepare) 2276 ae_dev->ops->flr_prepare(ae_dev); 2277} 2278 2279static void hns3_reset_done(struct pci_dev *pdev) 2280{ 2281 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev); 2282 2283 dev_info(&pdev->dev, "FLR done\n"); 2284 if (ae_dev && ae_dev->ops && ae_dev->ops->flr_done) 2285 ae_dev->ops->flr_done(ae_dev); 2286} 2287 2288static const struct pci_error_handlers hns3_err_handler = { 2289 .error_detected = hns3_error_detected, 2290 .slot_reset = hns3_slot_reset, 2291 .reset_prepare = hns3_reset_prepare, 2292 .reset_done = hns3_reset_done, 2293}; 2294 2295static struct pci_driver hns3_driver = { 2296 .name = hns3_driver_name, 2297 .id_table = hns3_pci_tbl, 2298 .probe = hns3_probe, 2299 .remove = hns3_remove, 2300 .shutdown = hns3_shutdown, 2301 .sriov_configure = hns3_pci_sriov_configure, 2302 .err_handler = &hns3_err_handler, 2303}; 2304 2305/* set default feature to hns3 */ 2306static void hns3_set_default_feature(struct net_device *netdev) 2307{ 2308 struct hnae3_handle *h = hns3_get_handle(netdev); 2309 struct pci_dev *pdev = h->pdev; 2310 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev); 2311 2312 netdev->priv_flags |= IFF_UNICAST_FLT; 2313 2314 netdev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | 2315 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO | 2316 NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE | 2317 NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL | 2318 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_SCTP_CRC | 2319 NETIF_F_TSO_MANGLEID | NETIF_F_FRAGLIST; 2320 2321 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM; 2322 2323 netdev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | 2324 NETIF_F_HW_VLAN_CTAG_FILTER | 2325 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | 2326 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO | 2327 NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE | 2328 NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL | 2329 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_SCTP_CRC | 2330 NETIF_F_FRAGLIST; 2331 2332 netdev->vlan_features |= 2333 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM | 2334 NETIF_F_SG | NETIF_F_GSO | NETIF_F_GRO | 2335 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE | 2336 NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL | 2337 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_SCTP_CRC | 2338 NETIF_F_FRAGLIST; 2339 2340 netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | 2341 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | 2342 NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_GSO | 2343 NETIF_F_GRO | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_GSO_GRE | 2344 NETIF_F_GSO_GRE_CSUM | NETIF_F_GSO_UDP_TUNNEL | 2345 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_SCTP_CRC | 2346 NETIF_F_FRAGLIST; 2347 2348 if (ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) { 2349 netdev->hw_features |= NETIF_F_GRO_HW; 2350 netdev->features |= NETIF_F_GRO_HW; 2351 2352 if (!(h->flags & HNAE3_SUPPORT_VF)) { 2353 netdev->hw_features |= NETIF_F_NTUPLE; 2354 netdev->features |= NETIF_F_NTUPLE; 2355 } 2356 } 2357 2358 if (test_bit(HNAE3_DEV_SUPPORT_UDP_GSO_B, ae_dev->caps)) { 2359 netdev->hw_features |= NETIF_F_GSO_UDP_L4; 2360 netdev->features |= NETIF_F_GSO_UDP_L4; 2361 netdev->vlan_features |= NETIF_F_GSO_UDP_L4; 2362 netdev->hw_enc_features |= NETIF_F_GSO_UDP_L4; 2363 } 2364} 2365 2366static int hns3_alloc_buffer(struct hns3_enet_ring *ring, 2367 struct hns3_desc_cb *cb) 2368{ 2369 unsigned int order = hns3_page_order(ring); 2370 struct page *p; 2371 2372 p = dev_alloc_pages(order); 2373 if (!p) 2374 return -ENOMEM; 2375 2376 cb->priv = p; 2377 cb->page_offset = 0; 2378 cb->reuse_flag = 0; 2379 cb->buf = page_address(p); 2380 cb->length = hns3_page_size(ring); 2381 cb->type = DESC_TYPE_PAGE; 2382 page_ref_add(p, USHRT_MAX - 1); 2383 cb->pagecnt_bias = USHRT_MAX; 2384 2385 return 0; 2386} 2387 2388static void hns3_free_buffer(struct hns3_enet_ring *ring, 2389 struct hns3_desc_cb *cb, int budget) 2390{ 2391 if (cb->type == DESC_TYPE_SKB) 2392 napi_consume_skb(cb->priv, budget); 2393 else if (!HNAE3_IS_TX_RING(ring) && cb->pagecnt_bias) 2394 __page_frag_cache_drain(cb->priv, cb->pagecnt_bias); 2395 memset(cb, 0, sizeof(*cb)); 2396} 2397 2398static int hns3_map_buffer(struct hns3_enet_ring *ring, struct hns3_desc_cb *cb) 2399{ 2400 cb->dma = dma_map_page(ring_to_dev(ring), cb->priv, 0, 2401 cb->length, ring_to_dma_dir(ring)); 2402 2403 if (unlikely(dma_mapping_error(ring_to_dev(ring), cb->dma))) 2404 return -EIO; 2405 2406 return 0; 2407} 2408 2409static void hns3_unmap_buffer(struct hns3_enet_ring *ring, 2410 struct hns3_desc_cb *cb) 2411{ 2412 if (cb->type == DESC_TYPE_SKB || cb->type == DESC_TYPE_FRAGLIST_SKB) 2413 dma_unmap_single(ring_to_dev(ring), cb->dma, cb->length, 2414 ring_to_dma_dir(ring)); 2415 else if (cb->length) 2416 dma_unmap_page(ring_to_dev(ring), cb->dma, cb->length, 2417 ring_to_dma_dir(ring)); 2418} 2419 2420static void hns3_buffer_detach(struct hns3_enet_ring *ring, int i) 2421{ 2422 hns3_unmap_buffer(ring, &ring->desc_cb[i]); 2423 ring->desc[i].addr = 0; 2424 ring->desc_cb[i].refill = 0; 2425} 2426 2427static void hns3_free_buffer_detach(struct hns3_enet_ring *ring, int i, 2428 int budget) 2429{ 2430 struct hns3_desc_cb *cb = &ring->desc_cb[i]; 2431 2432 if (!ring->desc_cb[i].dma) 2433 return; 2434 2435 hns3_buffer_detach(ring, i); 2436 hns3_free_buffer(ring, cb, budget); 2437} 2438 2439static void hns3_free_buffers(struct hns3_enet_ring *ring) 2440{ 2441 int i; 2442 2443 for (i = 0; i < ring->desc_num; i++) 2444 hns3_free_buffer_detach(ring, i, 0); 2445} 2446 2447/* free desc along with its attached buffer */ 2448static void hns3_free_desc(struct hns3_enet_ring *ring) 2449{ 2450 int size = ring->desc_num * sizeof(ring->desc[0]); 2451 2452 hns3_free_buffers(ring); 2453 2454 if (ring->desc) { 2455 dma_free_coherent(ring_to_dev(ring), size, 2456 ring->desc, ring->desc_dma_addr); 2457 ring->desc = NULL; 2458 } 2459} 2460 2461static int hns3_alloc_desc(struct hns3_enet_ring *ring) 2462{ 2463 int size = ring->desc_num * sizeof(ring->desc[0]); 2464 2465 ring->desc = dma_alloc_coherent(ring_to_dev(ring), size, 2466 &ring->desc_dma_addr, GFP_KERNEL); 2467 if (!ring->desc) 2468 return -ENOMEM; 2469 2470 return 0; 2471} 2472 2473static int hns3_alloc_and_map_buffer(struct hns3_enet_ring *ring, 2474 struct hns3_desc_cb *cb) 2475{ 2476 int ret; 2477 2478 ret = hns3_alloc_buffer(ring, cb); 2479 if (ret) 2480 goto out; 2481 2482 ret = hns3_map_buffer(ring, cb); 2483 if (ret) 2484 goto out_with_buf; 2485 2486 return 0; 2487 2488out_with_buf: 2489 hns3_free_buffer(ring, cb, 0); 2490out: 2491 return ret; 2492} 2493 2494static int hns3_alloc_and_attach_buffer(struct hns3_enet_ring *ring, int i) 2495{ 2496 int ret = hns3_alloc_and_map_buffer(ring, &ring->desc_cb[i]); 2497 2498 if (ret) 2499 return ret; 2500 2501 ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma); 2502 ring->desc_cb[i].refill = 1; 2503 2504 return 0; 2505} 2506 2507/* Allocate memory for raw pkg, and map with dma */ 2508static int hns3_alloc_ring_buffers(struct hns3_enet_ring *ring) 2509{ 2510 int i, j, ret; 2511 2512 for (i = 0; i < ring->desc_num; i++) { 2513 ret = hns3_alloc_and_attach_buffer(ring, i); 2514 if (ret) 2515 goto out_buffer_fail; 2516 } 2517 2518 return 0; 2519 2520out_buffer_fail: 2521 for (j = i - 1; j >= 0; j--) 2522 hns3_free_buffer_detach(ring, j, 0); 2523 return ret; 2524} 2525 2526/* detach a in-used buffer and replace with a reserved one */ 2527static void hns3_replace_buffer(struct hns3_enet_ring *ring, int i, 2528 struct hns3_desc_cb *res_cb) 2529{ 2530 hns3_unmap_buffer(ring, &ring->desc_cb[i]); 2531 ring->desc_cb[i] = *res_cb; 2532 ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma); 2533 ring->desc_cb[i].refill = 1; 2534 ring->desc[i].rx.bd_base_info = 0; 2535} 2536 2537static void hns3_reuse_buffer(struct hns3_enet_ring *ring, int i) 2538{ 2539 ring->desc_cb[i].reuse_flag = 0; 2540 ring->desc_cb[i].refill = 1; 2541 ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma + 2542 ring->desc_cb[i].page_offset); 2543 ring->desc[i].rx.bd_base_info = 0; 2544 2545 dma_sync_single_for_device(ring_to_dev(ring), 2546 ring->desc_cb[i].dma + ring->desc_cb[i].page_offset, 2547 hns3_buf_size(ring), 2548 DMA_FROM_DEVICE); 2549} 2550 2551static bool hns3_nic_reclaim_desc(struct hns3_enet_ring *ring, 2552 int *bytes, int *pkts, int budget) 2553{ 2554 /* pair with ring->last_to_use update in hns3_tx_doorbell(), 2555 * smp_store_release() is not used in hns3_tx_doorbell() because 2556 * the doorbell operation already have the needed barrier operation. 2557 */ 2558 int ltu = smp_load_acquire(&ring->last_to_use); 2559 int ntc = ring->next_to_clean; 2560 struct hns3_desc_cb *desc_cb; 2561 bool reclaimed = false; 2562 struct hns3_desc *desc; 2563 2564 while (ltu != ntc) { 2565 desc = &ring->desc[ntc]; 2566 2567 if (le16_to_cpu(desc->tx.bdtp_fe_sc_vld_ra_ri) & 2568 BIT(HNS3_TXD_VLD_B)) 2569 break; 2570 2571 desc_cb = &ring->desc_cb[ntc]; 2572 (*pkts) += (desc_cb->type == DESC_TYPE_SKB); 2573 (*bytes) += desc_cb->length; 2574 /* desc_cb will be cleaned, after hnae3_free_buffer_detach */ 2575 hns3_free_buffer_detach(ring, ntc, budget); 2576 2577 if (++ntc == ring->desc_num) 2578 ntc = 0; 2579 2580 /* Issue prefetch for next Tx descriptor */ 2581 prefetch(&ring->desc_cb[ntc]); 2582 reclaimed = true; 2583 } 2584 2585 if (unlikely(!reclaimed)) 2586 return false; 2587 2588 /* This smp_store_release() pairs with smp_load_acquire() in 2589 * ring_space called by hns3_nic_net_xmit. 2590 */ 2591 smp_store_release(&ring->next_to_clean, ntc); 2592 return true; 2593} 2594 2595void hns3_clean_tx_ring(struct hns3_enet_ring *ring, int budget) 2596{ 2597 struct net_device *netdev = ring_to_netdev(ring); 2598 struct hns3_nic_priv *priv = netdev_priv(netdev); 2599 struct netdev_queue *dev_queue; 2600 int bytes, pkts; 2601 2602 bytes = 0; 2603 pkts = 0; 2604 2605 if (unlikely(!hns3_nic_reclaim_desc(ring, &bytes, &pkts, budget))) 2606 return; 2607 2608 ring->tqp_vector->tx_group.total_bytes += bytes; 2609 ring->tqp_vector->tx_group.total_packets += pkts; 2610 2611 u64_stats_update_begin(&ring->syncp); 2612 ring->stats.tx_bytes += bytes; 2613 ring->stats.tx_pkts += pkts; 2614 u64_stats_update_end(&ring->syncp); 2615 2616 dev_queue = netdev_get_tx_queue(netdev, ring->tqp->tqp_index); 2617 netdev_tx_completed_queue(dev_queue, pkts, bytes); 2618 2619 if (unlikely(netif_carrier_ok(netdev) && 2620 ring_space(ring) > HNS3_MAX_TSO_BD_NUM)) { 2621 /* Make sure that anybody stopping the queue after this 2622 * sees the new next_to_clean. 2623 */ 2624 smp_mb(); 2625 if (netif_tx_queue_stopped(dev_queue) && 2626 !test_bit(HNS3_NIC_STATE_DOWN, &priv->state)) { 2627 netif_tx_wake_queue(dev_queue); 2628 ring->stats.restart_queue++; 2629 } 2630 } 2631} 2632 2633static int hns3_desc_unused(struct hns3_enet_ring *ring) 2634{ 2635 int ntc = ring->next_to_clean; 2636 int ntu = ring->next_to_use; 2637 2638 if (unlikely(ntc == ntu && !ring->desc_cb[ntc].refill)) 2639 return ring->desc_num; 2640 2641 return ((ntc >= ntu) ? 0 : ring->desc_num) + ntc - ntu; 2642} 2643 2644/* Return true if there is any allocation failure */ 2645static bool hns3_nic_alloc_rx_buffers(struct hns3_enet_ring *ring, 2646 int cleand_count) 2647{ 2648 struct hns3_desc_cb *desc_cb; 2649 struct hns3_desc_cb res_cbs; 2650 int i, ret; 2651 2652 for (i = 0; i < cleand_count; i++) { 2653 desc_cb = &ring->desc_cb[ring->next_to_use]; 2654 if (desc_cb->reuse_flag) { 2655 u64_stats_update_begin(&ring->syncp); 2656 ring->stats.reuse_pg_cnt++; 2657 u64_stats_update_end(&ring->syncp); 2658 2659 hns3_reuse_buffer(ring, ring->next_to_use); 2660 } else { 2661 ret = hns3_alloc_and_map_buffer(ring, &res_cbs); 2662 if (ret) { 2663 u64_stats_update_begin(&ring->syncp); 2664 ring->stats.sw_err_cnt++; 2665 u64_stats_update_end(&ring->syncp); 2666 2667 hns3_rl_err(ring_to_netdev(ring), 2668 "alloc rx buffer failed: %d\n", 2669 ret); 2670 2671 writel(i, ring->tqp->io_base + 2672 HNS3_RING_RX_RING_HEAD_REG); 2673 return true; 2674 } 2675 hns3_replace_buffer(ring, ring->next_to_use, &res_cbs); 2676 2677 u64_stats_update_begin(&ring->syncp); 2678 ring->stats.non_reuse_pg++; 2679 u64_stats_update_end(&ring->syncp); 2680 } 2681 2682 ring_ptr_move_fw(ring, next_to_use); 2683 } 2684 2685 writel(i, ring->tqp->io_base + HNS3_RING_RX_RING_HEAD_REG); 2686 return false; 2687} 2688 2689static bool hns3_page_is_reusable(struct page *page) 2690{ 2691 return page_to_nid(page) == numa_mem_id() && 2692 !page_is_pfmemalloc(page); 2693} 2694 2695static bool hns3_can_reuse_page(struct hns3_desc_cb *cb) 2696{ 2697 return (page_count(cb->priv) - cb->pagecnt_bias) == 1; 2698} 2699 2700static void hns3_nic_reuse_page(struct sk_buff *skb, int i, 2701 struct hns3_enet_ring *ring, int pull_len, 2702 struct hns3_desc_cb *desc_cb) 2703{ 2704 struct hns3_desc *desc = &ring->desc[ring->next_to_clean]; 2705 int size = le16_to_cpu(desc->rx.size); 2706 u32 truesize = hns3_buf_size(ring); 2707 2708 desc_cb->pagecnt_bias--; 2709 skb_add_rx_frag(skb, i, desc_cb->priv, desc_cb->page_offset + pull_len, 2710 size - pull_len, truesize); 2711 2712 /* Avoid re-using remote pages, or the stack is still using the page 2713 * when page_offset rollback to zero, flag default unreuse 2714 */ 2715 if (unlikely(!hns3_page_is_reusable(desc_cb->priv)) || 2716 (!desc_cb->page_offset && !hns3_can_reuse_page(desc_cb))) { 2717 __page_frag_cache_drain(desc_cb->priv, desc_cb->pagecnt_bias); 2718 return; 2719 } 2720 2721 /* Move offset up to the next cache line */ 2722 desc_cb->page_offset += truesize; 2723 2724 if (desc_cb->page_offset + truesize <= hns3_page_size(ring)) { 2725 desc_cb->reuse_flag = 1; 2726 } else if (hns3_can_reuse_page(desc_cb)) { 2727 desc_cb->reuse_flag = 1; 2728 desc_cb->page_offset = 0; 2729 } else if (desc_cb->pagecnt_bias) { 2730 __page_frag_cache_drain(desc_cb->priv, desc_cb->pagecnt_bias); 2731 return; 2732 } 2733 2734 if (unlikely(!desc_cb->pagecnt_bias)) { 2735 page_ref_add(desc_cb->priv, USHRT_MAX); 2736 desc_cb->pagecnt_bias = USHRT_MAX; 2737 } 2738} 2739 2740static int hns3_gro_complete(struct sk_buff *skb, u32 l234info) 2741{ 2742 __be16 type = skb->protocol; 2743 struct tcphdr *th; 2744 int depth = 0; 2745 2746 while (eth_type_vlan(type)) { 2747 struct vlan_hdr *vh; 2748 2749 if ((depth + VLAN_HLEN) > skb_headlen(skb)) 2750 return -EFAULT; 2751 2752 vh = (struct vlan_hdr *)(skb->data + depth); 2753 type = vh->h_vlan_encapsulated_proto; 2754 depth += VLAN_HLEN; 2755 } 2756 2757 skb_set_network_header(skb, depth); 2758 2759 if (type == htons(ETH_P_IP)) { 2760 const struct iphdr *iph = ip_hdr(skb); 2761 2762 depth += sizeof(struct iphdr); 2763 skb_set_transport_header(skb, depth); 2764 th = tcp_hdr(skb); 2765 th->check = ~tcp_v4_check(skb->len - depth, iph->saddr, 2766 iph->daddr, 0); 2767 } else if (type == htons(ETH_P_IPV6)) { 2768 const struct ipv6hdr *iph = ipv6_hdr(skb); 2769 2770 depth += sizeof(struct ipv6hdr); 2771 skb_set_transport_header(skb, depth); 2772 th = tcp_hdr(skb); 2773 th->check = ~tcp_v6_check(skb->len - depth, &iph->saddr, 2774 &iph->daddr, 0); 2775 } else { 2776 hns3_rl_err(skb->dev, 2777 "Error: FW GRO supports only IPv4/IPv6, not 0x%04x, depth: %d\n", 2778 be16_to_cpu(type), depth); 2779 return -EFAULT; 2780 } 2781 2782 skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count; 2783 if (th->cwr) 2784 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 2785 2786 if (l234info & BIT(HNS3_RXD_GRO_FIXID_B)) 2787 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_FIXEDID; 2788 2789 skb->csum_start = (unsigned char *)th - skb->head; 2790 skb->csum_offset = offsetof(struct tcphdr, check); 2791 skb->ip_summed = CHECKSUM_PARTIAL; 2792 2793 trace_hns3_gro(skb); 2794 2795 return 0; 2796} 2797 2798static void hns3_rx_checksum(struct hns3_enet_ring *ring, struct sk_buff *skb, 2799 u32 l234info, u32 bd_base_info, u32 ol_info) 2800{ 2801 struct net_device *netdev = ring_to_netdev(ring); 2802 int l3_type, l4_type; 2803 int ol4_type; 2804 2805 skb->ip_summed = CHECKSUM_NONE; 2806 2807 skb_checksum_none_assert(skb); 2808 2809 if (!(netdev->features & NETIF_F_RXCSUM)) 2810 return; 2811 2812 /* check if hardware has done checksum */ 2813 if (!(bd_base_info & BIT(HNS3_RXD_L3L4P_B))) 2814 return; 2815 2816 if (unlikely(l234info & (BIT(HNS3_RXD_L3E_B) | BIT(HNS3_RXD_L4E_B) | 2817 BIT(HNS3_RXD_OL3E_B) | 2818 BIT(HNS3_RXD_OL4E_B)))) { 2819 u64_stats_update_begin(&ring->syncp); 2820 ring->stats.l3l4_csum_err++; 2821 u64_stats_update_end(&ring->syncp); 2822 2823 return; 2824 } 2825 2826 ol4_type = hnae3_get_field(ol_info, HNS3_RXD_OL4ID_M, 2827 HNS3_RXD_OL4ID_S); 2828 switch (ol4_type) { 2829 case HNS3_OL4_TYPE_MAC_IN_UDP: 2830 case HNS3_OL4_TYPE_NVGRE: 2831 skb->csum_level = 1; 2832 fallthrough; 2833 case HNS3_OL4_TYPE_NO_TUN: 2834 l3_type = hnae3_get_field(l234info, HNS3_RXD_L3ID_M, 2835 HNS3_RXD_L3ID_S); 2836 l4_type = hnae3_get_field(l234info, HNS3_RXD_L4ID_M, 2837 HNS3_RXD_L4ID_S); 2838 2839 /* Can checksum ipv4 or ipv6 + UDP/TCP/SCTP packets */ 2840 if ((l3_type == HNS3_L3_TYPE_IPV4 || 2841 l3_type == HNS3_L3_TYPE_IPV6) && 2842 (l4_type == HNS3_L4_TYPE_UDP || 2843 l4_type == HNS3_L4_TYPE_TCP || 2844 l4_type == HNS3_L4_TYPE_SCTP)) 2845 skb->ip_summed = CHECKSUM_UNNECESSARY; 2846 break; 2847 default: 2848 break; 2849 } 2850} 2851 2852static void hns3_rx_skb(struct hns3_enet_ring *ring, struct sk_buff *skb) 2853{ 2854 if (skb_has_frag_list(skb)) 2855 napi_gro_flush(&ring->tqp_vector->napi, false); 2856 2857 napi_gro_receive(&ring->tqp_vector->napi, skb); 2858} 2859 2860static bool hns3_parse_vlan_tag(struct hns3_enet_ring *ring, 2861 struct hns3_desc *desc, u32 l234info, 2862 u16 *vlan_tag) 2863{ 2864 struct hnae3_handle *handle = ring->tqp->handle; 2865 struct pci_dev *pdev = ring->tqp->handle->pdev; 2866 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev); 2867 2868 if (unlikely(ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)) { 2869 *vlan_tag = le16_to_cpu(desc->rx.ot_vlan_tag); 2870 if (!(*vlan_tag & VLAN_VID_MASK)) 2871 *vlan_tag = le16_to_cpu(desc->rx.vlan_tag); 2872 2873 return (*vlan_tag != 0); 2874 } 2875 2876#define HNS3_STRP_OUTER_VLAN 0x1 2877#define HNS3_STRP_INNER_VLAN 0x2 2878#define HNS3_STRP_BOTH 0x3 2879 2880 /* Hardware always insert VLAN tag into RX descriptor when 2881 * remove the tag from packet, driver needs to determine 2882 * reporting which tag to stack. 2883 */ 2884 switch (hnae3_get_field(l234info, HNS3_RXD_STRP_TAGP_M, 2885 HNS3_RXD_STRP_TAGP_S)) { 2886 case HNS3_STRP_OUTER_VLAN: 2887 if (handle->port_base_vlan_state != 2888 HNAE3_PORT_BASE_VLAN_DISABLE) 2889 return false; 2890 2891 *vlan_tag = le16_to_cpu(desc->rx.ot_vlan_tag); 2892 return true; 2893 case HNS3_STRP_INNER_VLAN: 2894 if (handle->port_base_vlan_state != 2895 HNAE3_PORT_BASE_VLAN_DISABLE) 2896 return false; 2897 2898 *vlan_tag = le16_to_cpu(desc->rx.vlan_tag); 2899 return true; 2900 case HNS3_STRP_BOTH: 2901 if (handle->port_base_vlan_state == 2902 HNAE3_PORT_BASE_VLAN_DISABLE) 2903 *vlan_tag = le16_to_cpu(desc->rx.ot_vlan_tag); 2904 else 2905 *vlan_tag = le16_to_cpu(desc->rx.vlan_tag); 2906 2907 return true; 2908 default: 2909 return false; 2910 } 2911} 2912 2913static void hns3_rx_ring_move_fw(struct hns3_enet_ring *ring) 2914{ 2915 ring->desc[ring->next_to_clean].rx.bd_base_info &= 2916 cpu_to_le32(~BIT(HNS3_RXD_VLD_B)); 2917 ring->desc_cb[ring->next_to_clean].refill = 0; 2918 ring->next_to_clean += 1; 2919 2920 if (unlikely(ring->next_to_clean == ring->desc_num)) 2921 ring->next_to_clean = 0; 2922} 2923 2924static int hns3_alloc_skb(struct hns3_enet_ring *ring, unsigned int length, 2925 unsigned char *va) 2926{ 2927 struct hns3_desc_cb *desc_cb = &ring->desc_cb[ring->next_to_clean]; 2928 struct net_device *netdev = ring_to_netdev(ring); 2929 struct sk_buff *skb; 2930 2931 ring->skb = napi_alloc_skb(&ring->tqp_vector->napi, HNS3_RX_HEAD_SIZE); 2932 skb = ring->skb; 2933 if (unlikely(!skb)) { 2934 hns3_rl_err(netdev, "alloc rx skb fail\n"); 2935 2936 u64_stats_update_begin(&ring->syncp); 2937 ring->stats.sw_err_cnt++; 2938 u64_stats_update_end(&ring->syncp); 2939 2940 return -ENOMEM; 2941 } 2942 2943 trace_hns3_rx_desc(ring); 2944 prefetchw(skb->data); 2945 2946 ring->pending_buf = 1; 2947 ring->frag_num = 0; 2948 ring->tail_skb = NULL; 2949 if (length <= HNS3_RX_HEAD_SIZE) { 2950 memcpy(__skb_put(skb, length), va, ALIGN(length, sizeof(long))); 2951 2952 /* We can reuse buffer as-is, just make sure it is local */ 2953 if (likely(hns3_page_is_reusable(desc_cb->priv))) 2954 desc_cb->reuse_flag = 1; 2955 else /* This page cannot be reused so discard it */ 2956 __page_frag_cache_drain(desc_cb->priv, 2957 desc_cb->pagecnt_bias); 2958 2959 hns3_rx_ring_move_fw(ring); 2960 return 0; 2961 } 2962 u64_stats_update_begin(&ring->syncp); 2963 ring->stats.seg_pkt_cnt++; 2964 u64_stats_update_end(&ring->syncp); 2965 2966 ring->pull_len = eth_get_headlen(netdev, va, HNS3_RX_HEAD_SIZE); 2967 __skb_put(skb, ring->pull_len); 2968 hns3_nic_reuse_page(skb, ring->frag_num++, ring, ring->pull_len, 2969 desc_cb); 2970 hns3_rx_ring_move_fw(ring); 2971 2972 return 0; 2973} 2974 2975static int hns3_add_frag(struct hns3_enet_ring *ring) 2976{ 2977 struct sk_buff *skb = ring->skb; 2978 struct sk_buff *head_skb = skb; 2979 struct sk_buff *new_skb; 2980 struct hns3_desc_cb *desc_cb; 2981 struct hns3_desc *desc; 2982 u32 bd_base_info; 2983 2984 do { 2985 desc = &ring->desc[ring->next_to_clean]; 2986 desc_cb = &ring->desc_cb[ring->next_to_clean]; 2987 bd_base_info = le32_to_cpu(desc->rx.bd_base_info); 2988 /* make sure HW write desc complete */ 2989 dma_rmb(); 2990 if (!(bd_base_info & BIT(HNS3_RXD_VLD_B))) 2991 return -ENXIO; 2992 2993 if (unlikely(ring->frag_num >= MAX_SKB_FRAGS)) { 2994 new_skb = napi_alloc_skb(&ring->tqp_vector->napi, 0); 2995 if (unlikely(!new_skb)) { 2996 hns3_rl_err(ring_to_netdev(ring), 2997 "alloc rx fraglist skb fail\n"); 2998 return -ENXIO; 2999 } 3000 ring->frag_num = 0; 3001 3002 if (ring->tail_skb) { 3003 ring->tail_skb->next = new_skb; 3004 ring->tail_skb = new_skb; 3005 } else { 3006 skb_shinfo(skb)->frag_list = new_skb; 3007 ring->tail_skb = new_skb; 3008 } 3009 } 3010 3011 if (ring->tail_skb) { 3012 head_skb->truesize += hns3_buf_size(ring); 3013 head_skb->data_len += le16_to_cpu(desc->rx.size); 3014 head_skb->len += le16_to_cpu(desc->rx.size); 3015 skb = ring->tail_skb; 3016 } 3017 3018 dma_sync_single_for_cpu(ring_to_dev(ring), 3019 desc_cb->dma + desc_cb->page_offset, 3020 hns3_buf_size(ring), 3021 DMA_FROM_DEVICE); 3022 3023 hns3_nic_reuse_page(skb, ring->frag_num++, ring, 0, desc_cb); 3024 trace_hns3_rx_desc(ring); 3025 hns3_rx_ring_move_fw(ring); 3026 ring->pending_buf++; 3027 } while (!(bd_base_info & BIT(HNS3_RXD_FE_B))); 3028 3029 return 0; 3030} 3031 3032static int hns3_set_gro_and_checksum(struct hns3_enet_ring *ring, 3033 struct sk_buff *skb, u32 l234info, 3034 u32 bd_base_info, u32 ol_info) 3035{ 3036 u32 l3_type; 3037 3038 skb_shinfo(skb)->gso_size = hnae3_get_field(bd_base_info, 3039 HNS3_RXD_GRO_SIZE_M, 3040 HNS3_RXD_GRO_SIZE_S); 3041 /* if there is no HW GRO, do not set gro params */ 3042 if (!skb_shinfo(skb)->gso_size) { 3043 hns3_rx_checksum(ring, skb, l234info, bd_base_info, ol_info); 3044 return 0; 3045 } 3046 3047 NAPI_GRO_CB(skb)->count = hnae3_get_field(l234info, 3048 HNS3_RXD_GRO_COUNT_M, 3049 HNS3_RXD_GRO_COUNT_S); 3050 3051 l3_type = hnae3_get_field(l234info, HNS3_RXD_L3ID_M, HNS3_RXD_L3ID_S); 3052 if (l3_type == HNS3_L3_TYPE_IPV4) 3053 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; 3054 else if (l3_type == HNS3_L3_TYPE_IPV6) 3055 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; 3056 else 3057 return -EFAULT; 3058 3059 return hns3_gro_complete(skb, l234info); 3060} 3061 3062static void hns3_set_rx_skb_rss_type(struct hns3_enet_ring *ring, 3063 struct sk_buff *skb, u32 rss_hash) 3064{ 3065 struct hnae3_handle *handle = ring->tqp->handle; 3066 enum pkt_hash_types rss_type; 3067 3068 if (rss_hash) 3069 rss_type = handle->kinfo.rss_type; 3070 else 3071 rss_type = PKT_HASH_TYPE_NONE; 3072 3073 skb_set_hash(skb, rss_hash, rss_type); 3074} 3075 3076static int hns3_handle_bdinfo(struct hns3_enet_ring *ring, struct sk_buff *skb) 3077{ 3078 struct net_device *netdev = ring_to_netdev(ring); 3079 enum hns3_pkt_l2t_type l2_frame_type; 3080 u32 bd_base_info, l234info, ol_info; 3081 struct hns3_desc *desc; 3082 unsigned int len; 3083 int pre_ntc, ret; 3084 3085 /* bdinfo handled below is only valid on the last BD of the 3086 * current packet, and ring->next_to_clean indicates the first 3087 * descriptor of next packet, so need - 1 below. 3088 */ 3089 pre_ntc = ring->next_to_clean ? (ring->next_to_clean - 1) : 3090 (ring->desc_num - 1); 3091 desc = &ring->desc[pre_ntc]; 3092 bd_base_info = le32_to_cpu(desc->rx.bd_base_info); 3093 l234info = le32_to_cpu(desc->rx.l234_info); 3094 ol_info = le32_to_cpu(desc->rx.ol_info); 3095 3096 /* Based on hw strategy, the tag offloaded will be stored at 3097 * ot_vlan_tag in two layer tag case, and stored at vlan_tag 3098 * in one layer tag case. 3099 */ 3100 if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX) { 3101 u16 vlan_tag; 3102 3103 if (hns3_parse_vlan_tag(ring, desc, l234info, &vlan_tag)) 3104 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 3105 vlan_tag); 3106 } 3107 3108 if (unlikely(!desc->rx.pkt_len || (l234info & (BIT(HNS3_RXD_TRUNCAT_B) | 3109 BIT(HNS3_RXD_L2E_B))))) { 3110 u64_stats_update_begin(&ring->syncp); 3111 if (l234info & BIT(HNS3_RXD_L2E_B)) 3112 ring->stats.l2_err++; 3113 else 3114 ring->stats.err_pkt_len++; 3115 u64_stats_update_end(&ring->syncp); 3116 3117 return -EFAULT; 3118 } 3119 3120 len = skb->len; 3121 3122 /* Do update ip stack process */ 3123 skb->protocol = eth_type_trans(skb, netdev); 3124 3125 /* This is needed in order to enable forwarding support */ 3126 ret = hns3_set_gro_and_checksum(ring, skb, l234info, 3127 bd_base_info, ol_info); 3128 if (unlikely(ret)) { 3129 u64_stats_update_begin(&ring->syncp); 3130 ring->stats.rx_err_cnt++; 3131 u64_stats_update_end(&ring->syncp); 3132 return ret; 3133 } 3134 3135 l2_frame_type = hnae3_get_field(l234info, HNS3_RXD_DMAC_M, 3136 HNS3_RXD_DMAC_S); 3137 3138 u64_stats_update_begin(&ring->syncp); 3139 ring->stats.rx_pkts++; 3140 ring->stats.rx_bytes += len; 3141 3142 if (l2_frame_type == HNS3_L2_TYPE_MULTICAST) 3143 ring->stats.rx_multicast++; 3144 3145 u64_stats_update_end(&ring->syncp); 3146 3147 ring->tqp_vector->rx_group.total_bytes += len; 3148 3149 hns3_set_rx_skb_rss_type(ring, skb, le32_to_cpu(desc->rx.rss_hash)); 3150 return 0; 3151} 3152 3153static int hns3_handle_rx_bd(struct hns3_enet_ring *ring) 3154{ 3155 struct sk_buff *skb = ring->skb; 3156 struct hns3_desc_cb *desc_cb; 3157 struct hns3_desc *desc; 3158 unsigned int length; 3159 u32 bd_base_info; 3160 int ret; 3161 3162 desc = &ring->desc[ring->next_to_clean]; 3163 desc_cb = &ring->desc_cb[ring->next_to_clean]; 3164 3165 prefetch(desc); 3166 3167 if (!skb) { 3168 bd_base_info = le32_to_cpu(desc->rx.bd_base_info); 3169 3170 /* Check valid BD */ 3171 if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B)))) 3172 return -ENXIO; 3173 3174 dma_rmb(); 3175 length = le16_to_cpu(desc->rx.size); 3176 3177 ring->va = desc_cb->buf + desc_cb->page_offset; 3178 3179 dma_sync_single_for_cpu(ring_to_dev(ring), 3180 desc_cb->dma + desc_cb->page_offset, 3181 hns3_buf_size(ring), 3182 DMA_FROM_DEVICE); 3183 3184 /* Prefetch first cache line of first page. 3185 * Idea is to cache few bytes of the header of the packet. 3186 * Our L1 Cache line size is 64B so need to prefetch twice to make 3187 * it 128B. But in actual we can have greater size of caches with 3188 * 128B Level 1 cache lines. In such a case, single fetch would 3189 * suffice to cache in the relevant part of the header. 3190 */ 3191 net_prefetch(ring->va); 3192 3193 ret = hns3_alloc_skb(ring, length, ring->va); 3194 skb = ring->skb; 3195 3196 if (ret < 0) /* alloc buffer fail */ 3197 return ret; 3198 if (!(bd_base_info & BIT(HNS3_RXD_FE_B))) { /* need add frag */ 3199 ret = hns3_add_frag(ring); 3200 if (ret) 3201 return ret; 3202 } 3203 } else { 3204 ret = hns3_add_frag(ring); 3205 if (ret) 3206 return ret; 3207 } 3208 3209 /* As the head data may be changed when GRO enable, copy 3210 * the head data in after other data rx completed 3211 */ 3212 if (skb->len > HNS3_RX_HEAD_SIZE) 3213 memcpy(skb->data, ring->va, 3214 ALIGN(ring->pull_len, sizeof(long))); 3215 3216 ret = hns3_handle_bdinfo(ring, skb); 3217 if (unlikely(ret)) { 3218 dev_kfree_skb_any(skb); 3219 return ret; 3220 } 3221 3222 skb_record_rx_queue(skb, ring->tqp->tqp_index); 3223 return 0; 3224} 3225 3226int hns3_clean_rx_ring(struct hns3_enet_ring *ring, int budget, 3227 void (*rx_fn)(struct hns3_enet_ring *, struct sk_buff *)) 3228{ 3229#define RCB_NOF_ALLOC_RX_BUFF_ONCE 16 3230 int unused_count = hns3_desc_unused(ring); 3231 bool failure = false; 3232 int recv_pkts = 0; 3233 int err; 3234 3235 unused_count -= ring->pending_buf; 3236 3237 while (recv_pkts < budget) { 3238 /* Reuse or realloc buffers */ 3239 if (unused_count >= RCB_NOF_ALLOC_RX_BUFF_ONCE) { 3240 failure = failure || 3241 hns3_nic_alloc_rx_buffers(ring, unused_count); 3242 unused_count = 0; 3243 } 3244 3245 /* Poll one pkt */ 3246 err = hns3_handle_rx_bd(ring); 3247 /* Do not get FE for the packet or failed to alloc skb */ 3248 if (unlikely(!ring->skb || err == -ENXIO)) { 3249 goto out; 3250 } else if (likely(!err)) { 3251 rx_fn(ring, ring->skb); 3252 recv_pkts++; 3253 } 3254 3255 unused_count += ring->pending_buf; 3256 ring->skb = NULL; 3257 ring->pending_buf = 0; 3258 } 3259 3260out: 3261 return failure ? budget : recv_pkts; 3262} 3263 3264static bool hns3_get_new_flow_lvl(struct hns3_enet_ring_group *ring_group) 3265{ 3266#define HNS3_RX_LOW_BYTE_RATE 10000 3267#define HNS3_RX_MID_BYTE_RATE 20000 3268#define HNS3_RX_ULTRA_PACKET_RATE 40 3269 3270 enum hns3_flow_level_range new_flow_level; 3271 struct hns3_enet_tqp_vector *tqp_vector; 3272 int packets_per_msecs, bytes_per_msecs; 3273 u32 time_passed_ms; 3274 3275 tqp_vector = ring_group->ring->tqp_vector; 3276 time_passed_ms = 3277 jiffies_to_msecs(jiffies - tqp_vector->last_jiffies); 3278 if (!time_passed_ms) 3279 return false; 3280 3281 do_div(ring_group->total_packets, time_passed_ms); 3282 packets_per_msecs = ring_group->total_packets; 3283 3284 do_div(ring_group->total_bytes, time_passed_ms); 3285 bytes_per_msecs = ring_group->total_bytes; 3286 3287 new_flow_level = ring_group->coal.flow_level; 3288 3289 /* Simple throttlerate management 3290 * 0-10MB/s lower (50000 ints/s) 3291 * 10-20MB/s middle (20000 ints/s) 3292 * 20-1249MB/s high (18000 ints/s) 3293 * > 40000pps ultra (8000 ints/s) 3294 */ 3295 switch (new_flow_level) { 3296 case HNS3_FLOW_LOW: 3297 if (bytes_per_msecs > HNS3_RX_LOW_BYTE_RATE) 3298 new_flow_level = HNS3_FLOW_MID; 3299 break; 3300 case HNS3_FLOW_MID: 3301 if (bytes_per_msecs > HNS3_RX_MID_BYTE_RATE) 3302 new_flow_level = HNS3_FLOW_HIGH; 3303 else if (bytes_per_msecs <= HNS3_RX_LOW_BYTE_RATE) 3304 new_flow_level = HNS3_FLOW_LOW; 3305 break; 3306 case HNS3_FLOW_HIGH: 3307 case HNS3_FLOW_ULTRA: 3308 default: 3309 if (bytes_per_msecs <= HNS3_RX_MID_BYTE_RATE) 3310 new_flow_level = HNS3_FLOW_MID; 3311 break; 3312 } 3313 3314 if (packets_per_msecs > HNS3_RX_ULTRA_PACKET_RATE && 3315 &tqp_vector->rx_group == ring_group) 3316 new_flow_level = HNS3_FLOW_ULTRA; 3317 3318 ring_group->total_bytes = 0; 3319 ring_group->total_packets = 0; 3320 ring_group->coal.flow_level = new_flow_level; 3321 3322 return true; 3323} 3324 3325static bool hns3_get_new_int_gl(struct hns3_enet_ring_group *ring_group) 3326{ 3327 struct hns3_enet_tqp_vector *tqp_vector; 3328 u16 new_int_gl; 3329 3330 if (!ring_group->ring) 3331 return false; 3332 3333 tqp_vector = ring_group->ring->tqp_vector; 3334 if (!tqp_vector->last_jiffies) 3335 return false; 3336 3337 if (ring_group->total_packets == 0) { 3338 ring_group->coal.int_gl = HNS3_INT_GL_50K; 3339 ring_group->coal.flow_level = HNS3_FLOW_LOW; 3340 return true; 3341 } 3342 3343 if (!hns3_get_new_flow_lvl(ring_group)) 3344 return false; 3345 3346 new_int_gl = ring_group->coal.int_gl; 3347 switch (ring_group->coal.flow_level) { 3348 case HNS3_FLOW_LOW: 3349 new_int_gl = HNS3_INT_GL_50K; 3350 break; 3351 case HNS3_FLOW_MID: 3352 new_int_gl = HNS3_INT_GL_20K; 3353 break; 3354 case HNS3_FLOW_HIGH: 3355 new_int_gl = HNS3_INT_GL_18K; 3356 break; 3357 case HNS3_FLOW_ULTRA: 3358 new_int_gl = HNS3_INT_GL_8K; 3359 break; 3360 default: 3361 break; 3362 } 3363 3364 if (new_int_gl != ring_group->coal.int_gl) { 3365 ring_group->coal.int_gl = new_int_gl; 3366 return true; 3367 } 3368 return false; 3369} 3370 3371static void hns3_update_new_int_gl(struct hns3_enet_tqp_vector *tqp_vector) 3372{ 3373 struct hns3_enet_ring_group *rx_group = &tqp_vector->rx_group; 3374 struct hns3_enet_ring_group *tx_group = &tqp_vector->tx_group; 3375 bool rx_update, tx_update; 3376 3377 /* update param every 1000ms */ 3378 if (time_before(jiffies, 3379 tqp_vector->last_jiffies + msecs_to_jiffies(1000))) 3380 return; 3381 3382 if (rx_group->coal.gl_adapt_enable) { 3383 rx_update = hns3_get_new_int_gl(rx_group); 3384 if (rx_update) 3385 hns3_set_vector_coalesce_rx_gl(tqp_vector, 3386 rx_group->coal.int_gl); 3387 } 3388 3389 if (tx_group->coal.gl_adapt_enable) { 3390 tx_update = hns3_get_new_int_gl(tx_group); 3391 if (tx_update) 3392 hns3_set_vector_coalesce_tx_gl(tqp_vector, 3393 tx_group->coal.int_gl); 3394 } 3395 3396 tqp_vector->last_jiffies = jiffies; 3397} 3398 3399static int hns3_nic_common_poll(struct napi_struct *napi, int budget) 3400{ 3401 struct hns3_nic_priv *priv = netdev_priv(napi->dev); 3402 struct hns3_enet_ring *ring; 3403 int rx_pkt_total = 0; 3404 3405 struct hns3_enet_tqp_vector *tqp_vector = 3406 container_of(napi, struct hns3_enet_tqp_vector, napi); 3407 bool clean_complete = true; 3408 int rx_budget = budget; 3409 3410 if (unlikely(test_bit(HNS3_NIC_STATE_DOWN, &priv->state))) { 3411 napi_complete(napi); 3412 return 0; 3413 } 3414 3415 /* Since the actual Tx work is minimal, we can give the Tx a larger 3416 * budget and be more aggressive about cleaning up the Tx descriptors. 3417 */ 3418 hns3_for_each_ring(ring, tqp_vector->tx_group) 3419 hns3_clean_tx_ring(ring, budget); 3420 3421 /* make sure rx ring budget not smaller than 1 */ 3422 if (tqp_vector->num_tqps > 1) 3423 rx_budget = max(budget / tqp_vector->num_tqps, 1); 3424 3425 hns3_for_each_ring(ring, tqp_vector->rx_group) { 3426 int rx_cleaned = hns3_clean_rx_ring(ring, rx_budget, 3427 hns3_rx_skb); 3428 3429 if (rx_cleaned >= rx_budget) 3430 clean_complete = false; 3431 3432 rx_pkt_total += rx_cleaned; 3433 } 3434 3435 tqp_vector->rx_group.total_packets += rx_pkt_total; 3436 3437 if (!clean_complete) 3438 return budget; 3439 3440 if (napi_complete(napi) && 3441 likely(!test_bit(HNS3_NIC_STATE_DOWN, &priv->state))) { 3442 hns3_update_new_int_gl(tqp_vector); 3443 hns3_mask_vector_irq(tqp_vector, 1); 3444 } 3445 3446 return rx_pkt_total; 3447} 3448 3449static int hns3_get_vector_ring_chain(struct hns3_enet_tqp_vector *tqp_vector, 3450 struct hnae3_ring_chain_node *head) 3451{ 3452 struct pci_dev *pdev = tqp_vector->handle->pdev; 3453 struct hnae3_ring_chain_node *cur_chain = head; 3454 struct hnae3_ring_chain_node *chain; 3455 struct hns3_enet_ring *tx_ring; 3456 struct hns3_enet_ring *rx_ring; 3457 3458 tx_ring = tqp_vector->tx_group.ring; 3459 if (tx_ring) { 3460 cur_chain->tqp_index = tx_ring->tqp->tqp_index; 3461 hnae3_set_bit(cur_chain->flag, HNAE3_RING_TYPE_B, 3462 HNAE3_RING_TYPE_TX); 3463 hnae3_set_field(cur_chain->int_gl_idx, HNAE3_RING_GL_IDX_M, 3464 HNAE3_RING_GL_IDX_S, HNAE3_RING_GL_TX); 3465 3466 cur_chain->next = NULL; 3467 3468 while (tx_ring->next) { 3469 tx_ring = tx_ring->next; 3470 3471 chain = devm_kzalloc(&pdev->dev, sizeof(*chain), 3472 GFP_KERNEL); 3473 if (!chain) 3474 goto err_free_chain; 3475 3476 cur_chain->next = chain; 3477 chain->tqp_index = tx_ring->tqp->tqp_index; 3478 hnae3_set_bit(chain->flag, HNAE3_RING_TYPE_B, 3479 HNAE3_RING_TYPE_TX); 3480 hnae3_set_field(chain->int_gl_idx, 3481 HNAE3_RING_GL_IDX_M, 3482 HNAE3_RING_GL_IDX_S, 3483 HNAE3_RING_GL_TX); 3484 3485 cur_chain = chain; 3486 } 3487 } 3488 3489 rx_ring = tqp_vector->rx_group.ring; 3490 if (!tx_ring && rx_ring) { 3491 cur_chain->next = NULL; 3492 cur_chain->tqp_index = rx_ring->tqp->tqp_index; 3493 hnae3_set_bit(cur_chain->flag, HNAE3_RING_TYPE_B, 3494 HNAE3_RING_TYPE_RX); 3495 hnae3_set_field(cur_chain->int_gl_idx, HNAE3_RING_GL_IDX_M, 3496 HNAE3_RING_GL_IDX_S, HNAE3_RING_GL_RX); 3497 3498 rx_ring = rx_ring->next; 3499 } 3500 3501 while (rx_ring) { 3502 chain = devm_kzalloc(&pdev->dev, sizeof(*chain), GFP_KERNEL); 3503 if (!chain) 3504 goto err_free_chain; 3505 3506 cur_chain->next = chain; 3507 chain->tqp_index = rx_ring->tqp->tqp_index; 3508 hnae3_set_bit(chain->flag, HNAE3_RING_TYPE_B, 3509 HNAE3_RING_TYPE_RX); 3510 hnae3_set_field(chain->int_gl_idx, HNAE3_RING_GL_IDX_M, 3511 HNAE3_RING_GL_IDX_S, HNAE3_RING_GL_RX); 3512 3513 cur_chain = chain; 3514 3515 rx_ring = rx_ring->next; 3516 } 3517 3518 return 0; 3519 3520err_free_chain: 3521 cur_chain = head->next; 3522 while (cur_chain) { 3523 chain = cur_chain->next; 3524 devm_kfree(&pdev->dev, cur_chain); 3525 cur_chain = chain; 3526 } 3527 head->next = NULL; 3528 3529 return -ENOMEM; 3530} 3531 3532static void hns3_free_vector_ring_chain(struct hns3_enet_tqp_vector *tqp_vector, 3533 struct hnae3_ring_chain_node *head) 3534{ 3535 struct pci_dev *pdev = tqp_vector->handle->pdev; 3536 struct hnae3_ring_chain_node *chain_tmp, *chain; 3537 3538 chain = head->next; 3539 3540 while (chain) { 3541 chain_tmp = chain->next; 3542 devm_kfree(&pdev->dev, chain); 3543 chain = chain_tmp; 3544 } 3545} 3546 3547static void hns3_add_ring_to_group(struct hns3_enet_ring_group *group, 3548 struct hns3_enet_ring *ring) 3549{ 3550 ring->next = group->ring; 3551 group->ring = ring; 3552 3553 group->count++; 3554} 3555 3556static void hns3_nic_set_cpumask(struct hns3_nic_priv *priv) 3557{ 3558 struct pci_dev *pdev = priv->ae_handle->pdev; 3559 struct hns3_enet_tqp_vector *tqp_vector; 3560 int num_vectors = priv->vector_num; 3561 int numa_node; 3562 int vector_i; 3563 3564 numa_node = dev_to_node(&pdev->dev); 3565 3566 for (vector_i = 0; vector_i < num_vectors; vector_i++) { 3567 tqp_vector = &priv->tqp_vector[vector_i]; 3568 cpumask_set_cpu(cpumask_local_spread(vector_i, numa_node), 3569 &tqp_vector->affinity_mask); 3570 } 3571} 3572 3573static int hns3_nic_init_vector_data(struct hns3_nic_priv *priv) 3574{ 3575 struct hnae3_handle *h = priv->ae_handle; 3576 struct hns3_enet_tqp_vector *tqp_vector; 3577 int ret; 3578 int i; 3579 3580 hns3_nic_set_cpumask(priv); 3581 3582 for (i = 0; i < priv->vector_num; i++) { 3583 tqp_vector = &priv->tqp_vector[i]; 3584 hns3_vector_gl_rl_init_hw(tqp_vector, priv); 3585 tqp_vector->num_tqps = 0; 3586 } 3587 3588 for (i = 0; i < h->kinfo.num_tqps; i++) { 3589 u16 vector_i = i % priv->vector_num; 3590 u16 tqp_num = h->kinfo.num_tqps; 3591 3592 tqp_vector = &priv->tqp_vector[vector_i]; 3593 3594 hns3_add_ring_to_group(&tqp_vector->tx_group, 3595 &priv->ring[i]); 3596 3597 hns3_add_ring_to_group(&tqp_vector->rx_group, 3598 &priv->ring[i + tqp_num]); 3599 3600 priv->ring[i].tqp_vector = tqp_vector; 3601 priv->ring[i + tqp_num].tqp_vector = tqp_vector; 3602 tqp_vector->num_tqps++; 3603 } 3604 3605 for (i = 0; i < priv->vector_num; i++) { 3606 struct hnae3_ring_chain_node vector_ring_chain; 3607 3608 tqp_vector = &priv->tqp_vector[i]; 3609 3610 tqp_vector->rx_group.total_bytes = 0; 3611 tqp_vector->rx_group.total_packets = 0; 3612 tqp_vector->tx_group.total_bytes = 0; 3613 tqp_vector->tx_group.total_packets = 0; 3614 tqp_vector->handle = h; 3615 3616 ret = hns3_get_vector_ring_chain(tqp_vector, 3617 &vector_ring_chain); 3618 if (ret) 3619 goto map_ring_fail; 3620 3621 ret = h->ae_algo->ops->map_ring_to_vector(h, 3622 tqp_vector->vector_irq, &vector_ring_chain); 3623 3624 hns3_free_vector_ring_chain(tqp_vector, &vector_ring_chain); 3625 3626 if (ret) 3627 goto map_ring_fail; 3628 3629 netif_napi_add(priv->netdev, &tqp_vector->napi, 3630 hns3_nic_common_poll, NAPI_POLL_WEIGHT); 3631 } 3632 3633 return 0; 3634 3635map_ring_fail: 3636 while (i--) 3637 netif_napi_del(&priv->tqp_vector[i].napi); 3638 3639 return ret; 3640} 3641 3642static int hns3_nic_alloc_vector_data(struct hns3_nic_priv *priv) 3643{ 3644#define HNS3_VECTOR_PF_MAX_NUM 64 3645 3646 struct hnae3_handle *h = priv->ae_handle; 3647 struct hns3_enet_tqp_vector *tqp_vector; 3648 struct hnae3_vector_info *vector; 3649 struct pci_dev *pdev = h->pdev; 3650 u16 tqp_num = h->kinfo.num_tqps; 3651 u16 vector_num; 3652 int ret = 0; 3653 u16 i; 3654 3655 /* RSS size, cpu online and vector_num should be the same */ 3656 /* Should consider 2p/4p later */ 3657 vector_num = min_t(u16, num_online_cpus(), tqp_num); 3658 vector_num = min_t(u16, vector_num, HNS3_VECTOR_PF_MAX_NUM); 3659 3660 vector = devm_kcalloc(&pdev->dev, vector_num, sizeof(*vector), 3661 GFP_KERNEL); 3662 if (!vector) 3663 return -ENOMEM; 3664 3665 /* save the actual available vector number */ 3666 vector_num = h->ae_algo->ops->get_vector(h, vector_num, vector); 3667 3668 priv->vector_num = vector_num; 3669 priv->tqp_vector = (struct hns3_enet_tqp_vector *) 3670 devm_kcalloc(&pdev->dev, vector_num, sizeof(*priv->tqp_vector), 3671 GFP_KERNEL); 3672 if (!priv->tqp_vector) { 3673 ret = -ENOMEM; 3674 goto out; 3675 } 3676 3677 for (i = 0; i < priv->vector_num; i++) { 3678 tqp_vector = &priv->tqp_vector[i]; 3679 tqp_vector->idx = i; 3680 tqp_vector->mask_addr = vector[i].io_addr; 3681 tqp_vector->vector_irq = vector[i].vector; 3682 hns3_vector_gl_rl_init(tqp_vector, priv); 3683 } 3684 3685out: 3686 devm_kfree(&pdev->dev, vector); 3687 return ret; 3688} 3689 3690static void hns3_clear_ring_group(struct hns3_enet_ring_group *group) 3691{ 3692 group->ring = NULL; 3693 group->count = 0; 3694} 3695 3696static void hns3_nic_uninit_vector_data(struct hns3_nic_priv *priv) 3697{ 3698 struct hnae3_ring_chain_node vector_ring_chain; 3699 struct hnae3_handle *h = priv->ae_handle; 3700 struct hns3_enet_tqp_vector *tqp_vector; 3701 int i; 3702 3703 for (i = 0; i < priv->vector_num; i++) { 3704 tqp_vector = &priv->tqp_vector[i]; 3705 3706 if (!tqp_vector->rx_group.ring && !tqp_vector->tx_group.ring) 3707 continue; 3708 3709 /* Since the mapping can be overwritten, when fail to get the 3710 * chain between vector and ring, we should go on to deal with 3711 * the remaining options. 3712 */ 3713 if (hns3_get_vector_ring_chain(tqp_vector, &vector_ring_chain)) 3714 dev_warn(priv->dev, "failed to get ring chain\n"); 3715 3716 h->ae_algo->ops->unmap_ring_from_vector(h, 3717 tqp_vector->vector_irq, &vector_ring_chain); 3718 3719 hns3_free_vector_ring_chain(tqp_vector, &vector_ring_chain); 3720 3721 hns3_clear_ring_group(&tqp_vector->rx_group); 3722 hns3_clear_ring_group(&tqp_vector->tx_group); 3723 netif_napi_del(&priv->tqp_vector[i].napi); 3724 } 3725} 3726 3727static void hns3_nic_dealloc_vector_data(struct hns3_nic_priv *priv) 3728{ 3729 struct hnae3_handle *h = priv->ae_handle; 3730 struct pci_dev *pdev = h->pdev; 3731 int i, ret; 3732 3733 for (i = 0; i < priv->vector_num; i++) { 3734 struct hns3_enet_tqp_vector *tqp_vector; 3735 3736 tqp_vector = &priv->tqp_vector[i]; 3737 ret = h->ae_algo->ops->put_vector(h, tqp_vector->vector_irq); 3738 if (ret) 3739 return; 3740 } 3741 3742 devm_kfree(&pdev->dev, priv->tqp_vector); 3743} 3744 3745static void hns3_ring_get_cfg(struct hnae3_queue *q, struct hns3_nic_priv *priv, 3746 unsigned int ring_type) 3747{ 3748 int queue_num = priv->ae_handle->kinfo.num_tqps; 3749 struct hns3_enet_ring *ring; 3750 int desc_num; 3751 3752 if (ring_type == HNAE3_RING_TYPE_TX) { 3753 ring = &priv->ring[q->tqp_index]; 3754 desc_num = priv->ae_handle->kinfo.num_tx_desc; 3755 ring->queue_index = q->tqp_index; 3756 } else { 3757 ring = &priv->ring[q->tqp_index + queue_num]; 3758 desc_num = priv->ae_handle->kinfo.num_rx_desc; 3759 ring->queue_index = q->tqp_index; 3760 } 3761 3762 hnae3_set_bit(ring->flag, HNAE3_RING_TYPE_B, ring_type); 3763 3764 ring->tqp = q; 3765 ring->desc = NULL; 3766 ring->desc_cb = NULL; 3767 ring->dev = priv->dev; 3768 ring->desc_dma_addr = 0; 3769 ring->buf_size = q->buf_size; 3770 ring->desc_num = desc_num; 3771 ring->next_to_use = 0; 3772 ring->next_to_clean = 0; 3773 ring->last_to_use = 0; 3774} 3775 3776static void hns3_queue_to_ring(struct hnae3_queue *tqp, 3777 struct hns3_nic_priv *priv) 3778{ 3779 hns3_ring_get_cfg(tqp, priv, HNAE3_RING_TYPE_TX); 3780 hns3_ring_get_cfg(tqp, priv, HNAE3_RING_TYPE_RX); 3781} 3782 3783static int hns3_get_ring_config(struct hns3_nic_priv *priv) 3784{ 3785 struct hnae3_handle *h = priv->ae_handle; 3786 struct pci_dev *pdev = h->pdev; 3787 int i; 3788 3789 priv->ring = devm_kzalloc(&pdev->dev, 3790 array3_size(h->kinfo.num_tqps, 3791 sizeof(*priv->ring), 2), 3792 GFP_KERNEL); 3793 if (!priv->ring) 3794 return -ENOMEM; 3795 3796 for (i = 0; i < h->kinfo.num_tqps; i++) 3797 hns3_queue_to_ring(h->kinfo.tqp[i], priv); 3798 3799 return 0; 3800} 3801 3802static void hns3_put_ring_config(struct hns3_nic_priv *priv) 3803{ 3804 if (!priv->ring) 3805 return; 3806 3807 devm_kfree(priv->dev, priv->ring); 3808 priv->ring = NULL; 3809} 3810 3811static int hns3_alloc_ring_memory(struct hns3_enet_ring *ring) 3812{ 3813 int ret; 3814 3815 if (ring->desc_num <= 0 || ring->buf_size <= 0) 3816 return -EINVAL; 3817 3818 ring->desc_cb = devm_kcalloc(ring_to_dev(ring), ring->desc_num, 3819 sizeof(ring->desc_cb[0]), GFP_KERNEL); 3820 if (!ring->desc_cb) { 3821 ret = -ENOMEM; 3822 goto out; 3823 } 3824 3825 ret = hns3_alloc_desc(ring); 3826 if (ret) 3827 goto out_with_desc_cb; 3828 3829 if (!HNAE3_IS_TX_RING(ring)) { 3830 ret = hns3_alloc_ring_buffers(ring); 3831 if (ret) 3832 goto out_with_desc; 3833 } 3834 3835 return 0; 3836 3837out_with_desc: 3838 hns3_free_desc(ring); 3839out_with_desc_cb: 3840 devm_kfree(ring_to_dev(ring), ring->desc_cb); 3841 ring->desc_cb = NULL; 3842out: 3843 return ret; 3844} 3845 3846void hns3_fini_ring(struct hns3_enet_ring *ring) 3847{ 3848 hns3_free_desc(ring); 3849 devm_kfree(ring_to_dev(ring), ring->desc_cb); 3850 ring->desc_cb = NULL; 3851 ring->next_to_clean = 0; 3852 ring->next_to_use = 0; 3853 ring->last_to_use = 0; 3854 ring->pending_buf = 0; 3855 if (ring->skb) { 3856 dev_kfree_skb_any(ring->skb); 3857 ring->skb = NULL; 3858 } 3859} 3860 3861static int hns3_buf_size2type(u32 buf_size) 3862{ 3863 int bd_size_type; 3864 3865 switch (buf_size) { 3866 case 512: 3867 bd_size_type = HNS3_BD_SIZE_512_TYPE; 3868 break; 3869 case 1024: 3870 bd_size_type = HNS3_BD_SIZE_1024_TYPE; 3871 break; 3872 case 2048: 3873 bd_size_type = HNS3_BD_SIZE_2048_TYPE; 3874 break; 3875 case 4096: 3876 bd_size_type = HNS3_BD_SIZE_4096_TYPE; 3877 break; 3878 default: 3879 bd_size_type = HNS3_BD_SIZE_2048_TYPE; 3880 } 3881 3882 return bd_size_type; 3883} 3884 3885static void hns3_init_ring_hw(struct hns3_enet_ring *ring) 3886{ 3887 dma_addr_t dma = ring->desc_dma_addr; 3888 struct hnae3_queue *q = ring->tqp; 3889 3890 if (!HNAE3_IS_TX_RING(ring)) { 3891 hns3_write_dev(q, HNS3_RING_RX_RING_BASEADDR_L_REG, (u32)dma); 3892 hns3_write_dev(q, HNS3_RING_RX_RING_BASEADDR_H_REG, 3893 (u32)((dma >> 31) >> 1)); 3894 3895 hns3_write_dev(q, HNS3_RING_RX_RING_BD_LEN_REG, 3896 hns3_buf_size2type(ring->buf_size)); 3897 hns3_write_dev(q, HNS3_RING_RX_RING_BD_NUM_REG, 3898 ring->desc_num / 8 - 1); 3899 3900 } else { 3901 hns3_write_dev(q, HNS3_RING_TX_RING_BASEADDR_L_REG, 3902 (u32)dma); 3903 hns3_write_dev(q, HNS3_RING_TX_RING_BASEADDR_H_REG, 3904 (u32)((dma >> 31) >> 1)); 3905 3906 hns3_write_dev(q, HNS3_RING_TX_RING_BD_NUM_REG, 3907 ring->desc_num / 8 - 1); 3908 } 3909} 3910 3911static void hns3_init_tx_ring_tc(struct hns3_nic_priv *priv) 3912{ 3913 struct hnae3_knic_private_info *kinfo = &priv->ae_handle->kinfo; 3914 int i; 3915 3916 for (i = 0; i < HNAE3_MAX_TC; i++) { 3917 struct hnae3_tc_info *tc_info = &kinfo->tc_info[i]; 3918 int j; 3919 3920 if (!tc_info->enable) 3921 continue; 3922 3923 for (j = 0; j < tc_info->tqp_count; j++) { 3924 struct hnae3_queue *q; 3925 3926 q = priv->ring[tc_info->tqp_offset + j].tqp; 3927 hns3_write_dev(q, HNS3_RING_TX_RING_TC_REG, 3928 tc_info->tc); 3929 } 3930 } 3931} 3932 3933int hns3_init_all_ring(struct hns3_nic_priv *priv) 3934{ 3935 struct hnae3_handle *h = priv->ae_handle; 3936 int ring_num = h->kinfo.num_tqps * 2; 3937 int i, j; 3938 int ret; 3939 3940 for (i = 0; i < ring_num; i++) { 3941 ret = hns3_alloc_ring_memory(&priv->ring[i]); 3942 if (ret) { 3943 dev_err(priv->dev, 3944 "Alloc ring memory fail! ret=%d\n", ret); 3945 goto out_when_alloc_ring_memory; 3946 } 3947 3948 u64_stats_init(&priv->ring[i].syncp); 3949 } 3950 3951 return 0; 3952 3953out_when_alloc_ring_memory: 3954 for (j = i - 1; j >= 0; j--) 3955 hns3_fini_ring(&priv->ring[j]); 3956 3957 return -ENOMEM; 3958} 3959 3960int hns3_uninit_all_ring(struct hns3_nic_priv *priv) 3961{ 3962 struct hnae3_handle *h = priv->ae_handle; 3963 int i; 3964 3965 for (i = 0; i < h->kinfo.num_tqps; i++) { 3966 hns3_fini_ring(&priv->ring[i]); 3967 hns3_fini_ring(&priv->ring[i + h->kinfo.num_tqps]); 3968 } 3969 return 0; 3970} 3971 3972/* Set mac addr if it is configured. or leave it to the AE driver */ 3973static int hns3_init_mac_addr(struct net_device *netdev) 3974{ 3975 struct hns3_nic_priv *priv = netdev_priv(netdev); 3976 struct hnae3_handle *h = priv->ae_handle; 3977 u8 mac_addr_temp[ETH_ALEN] = {0}; 3978 int ret = 0; 3979 3980 if (h->ae_algo->ops->get_mac_addr) 3981 h->ae_algo->ops->get_mac_addr(h, mac_addr_temp); 3982 3983 /* Check if the MAC address is valid, if not get a random one */ 3984 if (!is_valid_ether_addr(mac_addr_temp)) { 3985 eth_hw_addr_random(netdev); 3986 dev_warn(priv->dev, "using random MAC address %pM\n", 3987 netdev->dev_addr); 3988 } else if (!ether_addr_equal(netdev->dev_addr, mac_addr_temp)) { 3989 ether_addr_copy(netdev->dev_addr, mac_addr_temp); 3990 ether_addr_copy(netdev->perm_addr, mac_addr_temp); 3991 } else { 3992 return 0; 3993 } 3994 3995 if (h->ae_algo->ops->set_mac_addr) 3996 ret = h->ae_algo->ops->set_mac_addr(h, netdev->dev_addr, true); 3997 3998 return ret; 3999} 4000 4001static int hns3_init_phy(struct net_device *netdev) 4002{ 4003 struct hnae3_handle *h = hns3_get_handle(netdev); 4004 int ret = 0; 4005 4006 if (h->ae_algo->ops->mac_connect_phy) 4007 ret = h->ae_algo->ops->mac_connect_phy(h); 4008 4009 return ret; 4010} 4011 4012static void hns3_uninit_phy(struct net_device *netdev) 4013{ 4014 struct hnae3_handle *h = hns3_get_handle(netdev); 4015 4016 if (h->ae_algo->ops->mac_disconnect_phy) 4017 h->ae_algo->ops->mac_disconnect_phy(h); 4018} 4019 4020static void hns3_del_all_fd_rules(struct net_device *netdev, bool clear_list) 4021{ 4022 struct hnae3_handle *h = hns3_get_handle(netdev); 4023 4024 if (h->ae_algo->ops->del_all_fd_entries) 4025 h->ae_algo->ops->del_all_fd_entries(h, clear_list); 4026} 4027 4028static int hns3_client_start(struct hnae3_handle *handle) 4029{ 4030 if (!handle->ae_algo->ops->client_start) 4031 return 0; 4032 4033 return handle->ae_algo->ops->client_start(handle); 4034} 4035 4036static void hns3_client_stop(struct hnae3_handle *handle) 4037{ 4038 if (!handle->ae_algo->ops->client_stop) 4039 return; 4040 4041 handle->ae_algo->ops->client_stop(handle); 4042} 4043 4044static void hns3_info_show(struct hns3_nic_priv *priv) 4045{ 4046 struct hnae3_knic_private_info *kinfo = &priv->ae_handle->kinfo; 4047 4048 dev_info(priv->dev, "MAC address: %pM\n", priv->netdev->dev_addr); 4049 dev_info(priv->dev, "Task queue pairs numbers: %u\n", kinfo->num_tqps); 4050 dev_info(priv->dev, "RSS size: %u\n", kinfo->rss_size); 4051 dev_info(priv->dev, "Allocated RSS size: %u\n", kinfo->req_rss_size); 4052 dev_info(priv->dev, "RX buffer length: %u\n", kinfo->rx_buf_len); 4053 dev_info(priv->dev, "Desc num per TX queue: %u\n", kinfo->num_tx_desc); 4054 dev_info(priv->dev, "Desc num per RX queue: %u\n", kinfo->num_rx_desc); 4055 dev_info(priv->dev, "Total number of enabled TCs: %u\n", kinfo->num_tc); 4056 dev_info(priv->dev, "Max mtu size: %u\n", priv->netdev->max_mtu); 4057} 4058 4059static int hns3_client_init(struct hnae3_handle *handle) 4060{ 4061 struct pci_dev *pdev = handle->pdev; 4062 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev); 4063 u16 alloc_tqps, max_rss_size; 4064 struct hns3_nic_priv *priv; 4065 struct net_device *netdev; 4066 int ret; 4067 4068 handle->ae_algo->ops->get_tqps_and_rss_info(handle, &alloc_tqps, 4069 &max_rss_size); 4070 netdev = alloc_etherdev_mq(sizeof(struct hns3_nic_priv), alloc_tqps); 4071 if (!netdev) 4072 return -ENOMEM; 4073 4074 priv = netdev_priv(netdev); 4075 priv->dev = &pdev->dev; 4076 priv->netdev = netdev; 4077 priv->ae_handle = handle; 4078 priv->tx_timeout_count = 0; 4079 priv->max_non_tso_bd_num = ae_dev->dev_specs.max_non_tso_bd_num; 4080 set_bit(HNS3_NIC_STATE_DOWN, &priv->state); 4081 4082 handle->msg_enable = netif_msg_init(debug, DEFAULT_MSG_LEVEL); 4083 4084 handle->kinfo.netdev = netdev; 4085 handle->priv = (void *)priv; 4086 4087 hns3_init_mac_addr(netdev); 4088 4089 hns3_set_default_feature(netdev); 4090 4091 netdev->watchdog_timeo = HNS3_TX_TIMEOUT; 4092 netdev->priv_flags |= IFF_UNICAST_FLT; 4093 netdev->netdev_ops = &hns3_nic_netdev_ops; 4094 SET_NETDEV_DEV(netdev, &pdev->dev); 4095 hns3_ethtool_set_ops(netdev); 4096 4097 /* Carrier off reporting is important to ethtool even BEFORE open */ 4098 netif_carrier_off(netdev); 4099 4100 ret = hns3_get_ring_config(priv); 4101 if (ret) { 4102 ret = -ENOMEM; 4103 goto out_get_ring_cfg; 4104 } 4105 4106 ret = hns3_nic_alloc_vector_data(priv); 4107 if (ret) { 4108 ret = -ENOMEM; 4109 goto out_alloc_vector_data; 4110 } 4111 4112 ret = hns3_nic_init_vector_data(priv); 4113 if (ret) { 4114 ret = -ENOMEM; 4115 goto out_init_vector_data; 4116 } 4117 4118 ret = hns3_init_all_ring(priv); 4119 if (ret) { 4120 ret = -ENOMEM; 4121 goto out_init_ring; 4122 } 4123 4124 ret = hns3_init_phy(netdev); 4125 if (ret) 4126 goto out_init_phy; 4127 4128 /* the device can work without cpu rmap, only aRFS needs it */ 4129 ret = hns3_set_rx_cpu_rmap(netdev); 4130 if (ret) 4131 dev_warn(priv->dev, "set rx cpu rmap fail, ret=%d\n", ret); 4132 4133 ret = hns3_nic_init_irq(priv); 4134 if (ret) { 4135 dev_err(priv->dev, "init irq failed! ret=%d\n", ret); 4136 hns3_free_rx_cpu_rmap(netdev); 4137 goto out_init_irq_fail; 4138 } 4139 4140 ret = hns3_client_start(handle); 4141 if (ret) { 4142 dev_err(priv->dev, "hns3_client_start fail! ret=%d\n", ret); 4143 goto out_client_start; 4144 } 4145 4146 hns3_dcbnl_setup(handle); 4147 4148 hns3_dbg_init(handle); 4149 4150 /* MTU range: (ETH_MIN_MTU(kernel default) - 9702) */ 4151 netdev->max_mtu = HNS3_MAX_MTU; 4152 4153 set_bit(HNS3_NIC_STATE_INITED, &priv->state); 4154 4155 ret = register_netdev(netdev); 4156 if (ret) { 4157 dev_err(priv->dev, "probe register netdev fail!\n"); 4158 goto out_reg_netdev_fail; 4159 } 4160 4161 if (netif_msg_drv(handle)) 4162 hns3_info_show(priv); 4163 4164 return ret; 4165 4166out_reg_netdev_fail: 4167 hns3_dbg_uninit(handle); 4168out_client_start: 4169 hns3_free_rx_cpu_rmap(netdev); 4170 hns3_nic_uninit_irq(priv); 4171out_init_irq_fail: 4172 hns3_uninit_phy(netdev); 4173out_init_phy: 4174 hns3_uninit_all_ring(priv); 4175out_init_ring: 4176 hns3_nic_uninit_vector_data(priv); 4177out_init_vector_data: 4178 hns3_nic_dealloc_vector_data(priv); 4179out_alloc_vector_data: 4180 priv->ring = NULL; 4181out_get_ring_cfg: 4182 priv->ae_handle = NULL; 4183 free_netdev(netdev); 4184 return ret; 4185} 4186 4187static void hns3_client_uninit(struct hnae3_handle *handle, bool reset) 4188{ 4189 struct net_device *netdev = handle->kinfo.netdev; 4190 struct hns3_nic_priv *priv = netdev_priv(netdev); 4191 int ret; 4192 4193 if (netdev->reg_state != NETREG_UNINITIALIZED) 4194 unregister_netdev(netdev); 4195 4196 hns3_client_stop(handle); 4197 4198 hns3_uninit_phy(netdev); 4199 4200 if (!test_and_clear_bit(HNS3_NIC_STATE_INITED, &priv->state)) { 4201 netdev_warn(netdev, "already uninitialized\n"); 4202 goto out_netdev_free; 4203 } 4204 4205 hns3_free_rx_cpu_rmap(netdev); 4206 4207 hns3_nic_uninit_irq(priv); 4208 4209 hns3_del_all_fd_rules(netdev, true); 4210 4211 hns3_clear_all_ring(handle, true); 4212 4213 hns3_nic_uninit_vector_data(priv); 4214 4215 hns3_nic_dealloc_vector_data(priv); 4216 4217 ret = hns3_uninit_all_ring(priv); 4218 if (ret) 4219 netdev_err(netdev, "uninit ring error\n"); 4220 4221 hns3_put_ring_config(priv); 4222 4223out_netdev_free: 4224 hns3_dbg_uninit(handle); 4225 free_netdev(netdev); 4226} 4227 4228static void hns3_link_status_change(struct hnae3_handle *handle, bool linkup) 4229{ 4230 struct net_device *netdev = handle->kinfo.netdev; 4231 4232 if (!netdev) 4233 return; 4234 4235 if (linkup) { 4236 netif_tx_wake_all_queues(netdev); 4237 netif_carrier_on(netdev); 4238 if (netif_msg_link(handle)) 4239 netdev_info(netdev, "link up\n"); 4240 } else { 4241 netif_carrier_off(netdev); 4242 netif_tx_stop_all_queues(netdev); 4243 if (netif_msg_link(handle)) 4244 netdev_info(netdev, "link down\n"); 4245 } 4246} 4247 4248static int hns3_client_setup_tc(struct hnae3_handle *handle, u8 tc) 4249{ 4250 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 4251 struct net_device *ndev = kinfo->netdev; 4252 4253 if (tc > HNAE3_MAX_TC) 4254 return -EINVAL; 4255 4256 if (!ndev) 4257 return -ENODEV; 4258 4259 return hns3_nic_set_real_num_queue(ndev); 4260} 4261 4262static void hns3_clear_tx_ring(struct hns3_enet_ring *ring) 4263{ 4264 while (ring->next_to_clean != ring->next_to_use) { 4265 ring->desc[ring->next_to_clean].tx.bdtp_fe_sc_vld_ra_ri = 0; 4266 hns3_free_buffer_detach(ring, ring->next_to_clean, 0); 4267 ring_ptr_move_fw(ring, next_to_clean); 4268 } 4269 4270 ring->pending_buf = 0; 4271} 4272 4273static int hns3_clear_rx_ring(struct hns3_enet_ring *ring) 4274{ 4275 struct hns3_desc_cb res_cbs; 4276 int ret; 4277 4278 while (ring->next_to_use != ring->next_to_clean) { 4279 /* When a buffer is not reused, it's memory has been 4280 * freed in hns3_handle_rx_bd or will be freed by 4281 * stack, so we need to replace the buffer here. 4282 */ 4283 if (!ring->desc_cb[ring->next_to_use].reuse_flag) { 4284 ret = hns3_alloc_and_map_buffer(ring, &res_cbs); 4285 if (ret) { 4286 u64_stats_update_begin(&ring->syncp); 4287 ring->stats.sw_err_cnt++; 4288 u64_stats_update_end(&ring->syncp); 4289 /* if alloc new buffer fail, exit directly 4290 * and reclear in up flow. 4291 */ 4292 netdev_warn(ring_to_netdev(ring), 4293 "reserve buffer map failed, ret = %d\n", 4294 ret); 4295 return ret; 4296 } 4297 hns3_replace_buffer(ring, ring->next_to_use, &res_cbs); 4298 } 4299 ring_ptr_move_fw(ring, next_to_use); 4300 } 4301 4302 /* Free the pending skb in rx ring */ 4303 if (ring->skb) { 4304 dev_kfree_skb_any(ring->skb); 4305 ring->skb = NULL; 4306 ring->pending_buf = 0; 4307 } 4308 4309 return 0; 4310} 4311 4312static void hns3_force_clear_rx_ring(struct hns3_enet_ring *ring) 4313{ 4314 while (ring->next_to_use != ring->next_to_clean) { 4315 /* When a buffer is not reused, it's memory has been 4316 * freed in hns3_handle_rx_bd or will be freed by 4317 * stack, so only need to unmap the buffer here. 4318 */ 4319 if (!ring->desc_cb[ring->next_to_use].reuse_flag) { 4320 hns3_unmap_buffer(ring, 4321 &ring->desc_cb[ring->next_to_use]); 4322 ring->desc_cb[ring->next_to_use].dma = 0; 4323 } 4324 4325 ring_ptr_move_fw(ring, next_to_use); 4326 } 4327} 4328 4329static void hns3_clear_all_ring(struct hnae3_handle *h, bool force) 4330{ 4331 struct net_device *ndev = h->kinfo.netdev; 4332 struct hns3_nic_priv *priv = netdev_priv(ndev); 4333 u32 i; 4334 4335 for (i = 0; i < h->kinfo.num_tqps; i++) { 4336 struct hns3_enet_ring *ring; 4337 4338 ring = &priv->ring[i]; 4339 hns3_clear_tx_ring(ring); 4340 4341 ring = &priv->ring[i + h->kinfo.num_tqps]; 4342 /* Continue to clear other rings even if clearing some 4343 * rings failed. 4344 */ 4345 if (force) 4346 hns3_force_clear_rx_ring(ring); 4347 else 4348 hns3_clear_rx_ring(ring); 4349 } 4350} 4351 4352int hns3_nic_reset_all_ring(struct hnae3_handle *h) 4353{ 4354 struct net_device *ndev = h->kinfo.netdev; 4355 struct hns3_nic_priv *priv = netdev_priv(ndev); 4356 struct hns3_enet_ring *rx_ring; 4357 int i, j; 4358 int ret; 4359 4360 for (i = 0; i < h->kinfo.num_tqps; i++) { 4361 ret = h->ae_algo->ops->reset_queue(h, i); 4362 if (ret) 4363 return ret; 4364 4365 hns3_init_ring_hw(&priv->ring[i]); 4366 4367 /* We need to clear tx ring here because self test will 4368 * use the ring and will not run down before up 4369 */ 4370 hns3_clear_tx_ring(&priv->ring[i]); 4371 priv->ring[i].next_to_clean = 0; 4372 priv->ring[i].next_to_use = 0; 4373 priv->ring[i].last_to_use = 0; 4374 4375 rx_ring = &priv->ring[i + h->kinfo.num_tqps]; 4376 hns3_init_ring_hw(rx_ring); 4377 ret = hns3_clear_rx_ring(rx_ring); 4378 if (ret) 4379 return ret; 4380 4381 /* We can not know the hardware head and tail when this 4382 * function is called in reset flow, so we reuse all desc. 4383 */ 4384 for (j = 0; j < rx_ring->desc_num; j++) 4385 hns3_reuse_buffer(rx_ring, j); 4386 4387 rx_ring->next_to_clean = 0; 4388 rx_ring->next_to_use = 0; 4389 } 4390 4391 hns3_init_tx_ring_tc(priv); 4392 4393 return 0; 4394} 4395 4396static void hns3_store_coal(struct hns3_nic_priv *priv) 4397{ 4398 /* ethtool only support setting and querying one coal 4399 * configuration for now, so save the vector 0' coal 4400 * configuration here in order to restore it. 4401 */ 4402 memcpy(&priv->tx_coal, &priv->tqp_vector[0].tx_group.coal, 4403 sizeof(struct hns3_enet_coalesce)); 4404 memcpy(&priv->rx_coal, &priv->tqp_vector[0].rx_group.coal, 4405 sizeof(struct hns3_enet_coalesce)); 4406} 4407 4408static void hns3_restore_coal(struct hns3_nic_priv *priv) 4409{ 4410 u16 vector_num = priv->vector_num; 4411 int i; 4412 4413 for (i = 0; i < vector_num; i++) { 4414 memcpy(&priv->tqp_vector[i].tx_group.coal, &priv->tx_coal, 4415 sizeof(struct hns3_enet_coalesce)); 4416 memcpy(&priv->tqp_vector[i].rx_group.coal, &priv->rx_coal, 4417 sizeof(struct hns3_enet_coalesce)); 4418 } 4419} 4420 4421static int hns3_reset_notify_down_enet(struct hnae3_handle *handle) 4422{ 4423 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 4424 struct net_device *ndev = kinfo->netdev; 4425 struct hns3_nic_priv *priv = netdev_priv(ndev); 4426 4427 if (test_and_set_bit(HNS3_NIC_STATE_RESETTING, &priv->state)) 4428 return 0; 4429 4430 if (!netif_running(ndev)) 4431 return 0; 4432 4433 return hns3_nic_net_stop(ndev); 4434} 4435 4436static int hns3_reset_notify_up_enet(struct hnae3_handle *handle) 4437{ 4438 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 4439 struct hns3_nic_priv *priv = netdev_priv(kinfo->netdev); 4440 int ret = 0; 4441 4442 if (!test_bit(HNS3_NIC_STATE_INITED, &priv->state)) { 4443 netdev_err(kinfo->netdev, "device is not initialized yet\n"); 4444 return -EFAULT; 4445 } 4446 4447 clear_bit(HNS3_NIC_STATE_RESETTING, &priv->state); 4448 4449 if (netif_running(kinfo->netdev)) { 4450 ret = hns3_nic_net_open(kinfo->netdev); 4451 if (ret) { 4452 set_bit(HNS3_NIC_STATE_RESETTING, &priv->state); 4453 netdev_err(kinfo->netdev, 4454 "net up fail, ret=%d!\n", ret); 4455 return ret; 4456 } 4457 } 4458 4459 return ret; 4460} 4461 4462static int hns3_reset_notify_init_enet(struct hnae3_handle *handle) 4463{ 4464 struct net_device *netdev = handle->kinfo.netdev; 4465 struct hns3_nic_priv *priv = netdev_priv(netdev); 4466 int ret; 4467 4468 /* Carrier off reporting is important to ethtool even BEFORE open */ 4469 netif_carrier_off(netdev); 4470 4471 ret = hns3_get_ring_config(priv); 4472 if (ret) 4473 return ret; 4474 4475 ret = hns3_nic_alloc_vector_data(priv); 4476 if (ret) 4477 goto err_put_ring; 4478 4479 hns3_restore_coal(priv); 4480 4481 ret = hns3_nic_init_vector_data(priv); 4482 if (ret) 4483 goto err_dealloc_vector; 4484 4485 ret = hns3_init_all_ring(priv); 4486 if (ret) 4487 goto err_uninit_vector; 4488 4489 /* the device can work without cpu rmap, only aRFS needs it */ 4490 ret = hns3_set_rx_cpu_rmap(netdev); 4491 if (ret) 4492 dev_warn(priv->dev, "set rx cpu rmap fail, ret=%d\n", ret); 4493 4494 ret = hns3_nic_init_irq(priv); 4495 if (ret) { 4496 dev_err(priv->dev, "init irq failed! ret=%d\n", ret); 4497 hns3_free_rx_cpu_rmap(netdev); 4498 goto err_init_irq_fail; 4499 } 4500 4501 if (!hns3_is_phys_func(handle->pdev)) 4502 hns3_init_mac_addr(netdev); 4503 4504 ret = hns3_client_start(handle); 4505 if (ret) { 4506 dev_err(priv->dev, "hns3_client_start fail! ret=%d\n", ret); 4507 goto err_client_start_fail; 4508 } 4509 4510 set_bit(HNS3_NIC_STATE_INITED, &priv->state); 4511 4512 return ret; 4513 4514err_client_start_fail: 4515 hns3_free_rx_cpu_rmap(netdev); 4516 hns3_nic_uninit_irq(priv); 4517err_init_irq_fail: 4518 hns3_uninit_all_ring(priv); 4519err_uninit_vector: 4520 hns3_nic_uninit_vector_data(priv); 4521err_dealloc_vector: 4522 hns3_nic_dealloc_vector_data(priv); 4523err_put_ring: 4524 hns3_put_ring_config(priv); 4525 4526 return ret; 4527} 4528 4529static int hns3_reset_notify_uninit_enet(struct hnae3_handle *handle) 4530{ 4531 struct net_device *netdev = handle->kinfo.netdev; 4532 struct hns3_nic_priv *priv = netdev_priv(netdev); 4533 int ret; 4534 4535 if (!test_and_clear_bit(HNS3_NIC_STATE_INITED, &priv->state)) { 4536 netdev_warn(netdev, "already uninitialized\n"); 4537 return 0; 4538 } 4539 4540 hns3_free_rx_cpu_rmap(netdev); 4541 hns3_nic_uninit_irq(priv); 4542 hns3_clear_all_ring(handle, true); 4543 hns3_reset_tx_queue(priv->ae_handle); 4544 4545 hns3_nic_uninit_vector_data(priv); 4546 4547 hns3_store_coal(priv); 4548 4549 hns3_nic_dealloc_vector_data(priv); 4550 4551 ret = hns3_uninit_all_ring(priv); 4552 if (ret) 4553 netdev_err(netdev, "uninit ring error\n"); 4554 4555 hns3_put_ring_config(priv); 4556 4557 return ret; 4558} 4559 4560static int hns3_reset_notify(struct hnae3_handle *handle, 4561 enum hnae3_reset_notify_type type) 4562{ 4563 int ret = 0; 4564 4565 switch (type) { 4566 case HNAE3_UP_CLIENT: 4567 ret = hns3_reset_notify_up_enet(handle); 4568 break; 4569 case HNAE3_DOWN_CLIENT: 4570 ret = hns3_reset_notify_down_enet(handle); 4571 break; 4572 case HNAE3_INIT_CLIENT: 4573 ret = hns3_reset_notify_init_enet(handle); 4574 break; 4575 case HNAE3_UNINIT_CLIENT: 4576 ret = hns3_reset_notify_uninit_enet(handle); 4577 break; 4578 default: 4579 break; 4580 } 4581 4582 return ret; 4583} 4584 4585static int hns3_change_channels(struct hnae3_handle *handle, u32 new_tqp_num, 4586 bool rxfh_configured) 4587{ 4588 int ret; 4589 4590 ret = handle->ae_algo->ops->set_channels(handle, new_tqp_num, 4591 rxfh_configured); 4592 if (ret) { 4593 dev_err(&handle->pdev->dev, 4594 "Change tqp num(%u) fail.\n", new_tqp_num); 4595 return ret; 4596 } 4597 4598 ret = hns3_reset_notify(handle, HNAE3_INIT_CLIENT); 4599 if (ret) 4600 return ret; 4601 4602 ret = hns3_reset_notify(handle, HNAE3_UP_CLIENT); 4603 if (ret) 4604 hns3_reset_notify(handle, HNAE3_UNINIT_CLIENT); 4605 4606 return ret; 4607} 4608 4609int hns3_set_channels(struct net_device *netdev, 4610 struct ethtool_channels *ch) 4611{ 4612 struct hnae3_handle *h = hns3_get_handle(netdev); 4613 struct hnae3_knic_private_info *kinfo = &h->kinfo; 4614 bool rxfh_configured = netif_is_rxfh_configured(netdev); 4615 u32 new_tqp_num = ch->combined_count; 4616 u16 org_tqp_num; 4617 int ret; 4618 4619 if (hns3_nic_resetting(netdev)) 4620 return -EBUSY; 4621 4622 if (ch->rx_count || ch->tx_count) 4623 return -EINVAL; 4624 4625 if (new_tqp_num > hns3_get_max_available_channels(h) || 4626 new_tqp_num < 1) { 4627 dev_err(&netdev->dev, 4628 "Change tqps fail, the tqp range is from 1 to %u", 4629 hns3_get_max_available_channels(h)); 4630 return -EINVAL; 4631 } 4632 4633 if (kinfo->rss_size == new_tqp_num) 4634 return 0; 4635 4636 netif_dbg(h, drv, netdev, 4637 "set channels: tqp_num=%u, rxfh=%d\n", 4638 new_tqp_num, rxfh_configured); 4639 4640 ret = hns3_reset_notify(h, HNAE3_DOWN_CLIENT); 4641 if (ret) 4642 return ret; 4643 4644 ret = hns3_reset_notify(h, HNAE3_UNINIT_CLIENT); 4645 if (ret) 4646 return ret; 4647 4648 org_tqp_num = h->kinfo.num_tqps; 4649 ret = hns3_change_channels(h, new_tqp_num, rxfh_configured); 4650 if (ret) { 4651 int ret1; 4652 4653 netdev_warn(netdev, 4654 "Change channels fail, revert to old value\n"); 4655 ret1 = hns3_change_channels(h, org_tqp_num, rxfh_configured); 4656 if (ret1) { 4657 netdev_err(netdev, 4658 "revert to old channel fail\n"); 4659 return ret1; 4660 } 4661 4662 return ret; 4663 } 4664 4665 return 0; 4666} 4667 4668static const struct hns3_hw_error_info hns3_hw_err[] = { 4669 { .type = HNAE3_PPU_POISON_ERROR, 4670 .msg = "PPU poison" }, 4671 { .type = HNAE3_CMDQ_ECC_ERROR, 4672 .msg = "IMP CMDQ error" }, 4673 { .type = HNAE3_IMP_RD_POISON_ERROR, 4674 .msg = "IMP RD poison" }, 4675 { .type = HNAE3_ROCEE_AXI_RESP_ERROR, 4676 .msg = "ROCEE AXI RESP error" }, 4677}; 4678 4679static void hns3_process_hw_error(struct hnae3_handle *handle, 4680 enum hnae3_hw_error_type type) 4681{ 4682 int i; 4683 4684 for (i = 0; i < ARRAY_SIZE(hns3_hw_err); i++) { 4685 if (hns3_hw_err[i].type == type) { 4686 dev_err(&handle->pdev->dev, "Detected %s!\n", 4687 hns3_hw_err[i].msg); 4688 break; 4689 } 4690 } 4691} 4692 4693static const struct hnae3_client_ops client_ops = { 4694 .init_instance = hns3_client_init, 4695 .uninit_instance = hns3_client_uninit, 4696 .link_status_change = hns3_link_status_change, 4697 .setup_tc = hns3_client_setup_tc, 4698 .reset_notify = hns3_reset_notify, 4699 .process_hw_error = hns3_process_hw_error, 4700}; 4701 4702/* hns3_init_module - Driver registration routine 4703 * hns3_init_module is the first routine called when the driver is 4704 * loaded. All it does is register with the PCI subsystem. 4705 */ 4706static int __init hns3_init_module(void) 4707{ 4708 int ret; 4709 4710 pr_info("%s: %s - version\n", hns3_driver_name, hns3_driver_string); 4711 pr_info("%s: %s\n", hns3_driver_name, hns3_copyright); 4712 4713 client.type = HNAE3_CLIENT_KNIC; 4714 snprintf(client.name, HNAE3_CLIENT_NAME_LENGTH, "%s", 4715 hns3_driver_name); 4716 4717 client.ops = &client_ops; 4718 4719 INIT_LIST_HEAD(&client.node); 4720 4721 hns3_dbg_register_debugfs(hns3_driver_name); 4722 4723 ret = hnae3_register_client(&client); 4724 if (ret) 4725 goto err_reg_client; 4726 4727 ret = pci_register_driver(&hns3_driver); 4728 if (ret) 4729 goto err_reg_driver; 4730 4731 return ret; 4732 4733err_reg_driver: 4734 hnae3_unregister_client(&client); 4735err_reg_client: 4736 hns3_dbg_unregister_debugfs(); 4737 return ret; 4738} 4739module_init(hns3_init_module); 4740 4741/* hns3_exit_module - Driver exit cleanup routine 4742 * hns3_exit_module is called just before the driver is removed 4743 * from memory. 4744 */ 4745static void __exit hns3_exit_module(void) 4746{ 4747 pci_unregister_driver(&hns3_driver); 4748 hnae3_unregister_client(&client); 4749 hns3_dbg_unregister_debugfs(); 4750} 4751module_exit(hns3_exit_module); 4752 4753MODULE_DESCRIPTION("HNS3: Hisilicon Ethernet Driver"); 4754MODULE_AUTHOR("Huawei Tech. Co., Ltd."); 4755MODULE_LICENSE("GPL"); 4756MODULE_ALIAS("pci:hns-nic"); 4757