1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* Copyright (c) 2021, Microsoft Corporation. */ 3 4 #include <uapi/linux/bpf.h> 5 6 #include <linux/inetdevice.h> 7 #include <linux/etherdevice.h> 8 #include <linux/ethtool.h> 9 #include <linux/filter.h> 10 #include <linux/mm.h> 11 #include <linux/pci.h> 12 13 #include <net/checksum.h> 14 #include <net/ip6_checksum.h> 15 #include <net/page_pool/helpers.h> 16 #include <net/xdp.h> 17 18 #include <net/mana/mana.h> 19 #include <net/mana/mana_auxiliary.h> 20 21 static DEFINE_IDA(mana_adev_ida); 22 23 static int mana_adev_idx_alloc(void) 24 { 25 return ida_alloc(&mana_adev_ida, GFP_KERNEL); 26 } 27 28 static void mana_adev_idx_free(int idx) 29 { 30 ida_free(&mana_adev_ida, idx); 31 } 32 33 /* Microsoft Azure Network Adapter (MANA) functions */ 34 35 static int mana_open(struct net_device *ndev) 36 { 37 struct mana_port_context *apc = netdev_priv(ndev); 38 int err; 39 40 err = mana_alloc_queues(ndev); 41 if (err) 42 return err; 43 44 apc->port_is_up = true; 45 46 /* Ensure port state updated before txq state */ 47 smp_wmb(); 48 49 netif_carrier_on(ndev); 50 netif_tx_wake_all_queues(ndev); 51 52 return 0; 53 } 54 55 static int mana_close(struct net_device *ndev) 56 { 57 struct mana_port_context *apc = netdev_priv(ndev); 58 59 if (!apc->port_is_up) 60 return 0; 61 62 return mana_detach(ndev, true); 63 } 64 65 static bool mana_can_tx(struct gdma_queue *wq) 66 { 67 return mana_gd_wq_avail_space(wq) >= MAX_TX_WQE_SIZE; 68 } 69 70 static unsigned int mana_checksum_info(struct sk_buff *skb) 71 { 72 if (skb->protocol == htons(ETH_P_IP)) { 73 struct iphdr *ip = ip_hdr(skb); 74 75 if (ip->protocol == IPPROTO_TCP) 76 return IPPROTO_TCP; 77 78 if (ip->protocol == IPPROTO_UDP) 79 return IPPROTO_UDP; 80 } else if (skb->protocol == htons(ETH_P_IPV6)) { 81 struct ipv6hdr *ip6 = ipv6_hdr(skb); 82 83 if (ip6->nexthdr == IPPROTO_TCP) 84 return IPPROTO_TCP; 85 86 if (ip6->nexthdr == IPPROTO_UDP) 87 return IPPROTO_UDP; 88 } 89 90 /* No csum offloading */ 91 return 0; 92 } 93 94 static void mana_add_sge(struct mana_tx_package *tp, struct mana_skb_head *ash, 95 int sg_i, dma_addr_t da, int sge_len, u32 gpa_mkey) 96 { 97 ash->dma_handle[sg_i] = da; 98 ash->size[sg_i] = sge_len; 99 100 tp->wqe_req.sgl[sg_i].address = da; 101 tp->wqe_req.sgl[sg_i].mem_key = gpa_mkey; 102 tp->wqe_req.sgl[sg_i].size = sge_len; 103 } 104 105 static int mana_map_skb(struct sk_buff *skb, struct mana_port_context *apc, 106 struct mana_tx_package *tp, int gso_hs) 107 { 108 struct mana_skb_head *ash = (struct mana_skb_head *)skb->head; 109 int hsg = 1; /* num of SGEs of linear part */ 110 struct gdma_dev *gd = apc->ac->gdma_dev; 111 int skb_hlen = skb_headlen(skb); 112 int sge0_len, sge1_len = 0; 113 struct gdma_context *gc; 114 struct device *dev; 115 skb_frag_t *frag; 116 dma_addr_t da; 117 int sg_i; 118 int i; 119 120 gc = gd->gdma_context; 121 dev = gc->dev; 122 123 if (gso_hs && gso_hs < skb_hlen) { 124 sge0_len = gso_hs; 125 sge1_len = skb_hlen - gso_hs; 126 } else { 127 sge0_len = skb_hlen; 128 } 129 130 da = dma_map_single(dev, skb->data, sge0_len, DMA_TO_DEVICE); 131 if (dma_mapping_error(dev, da)) 132 return -ENOMEM; 133 134 mana_add_sge(tp, ash, 0, da, sge0_len, gd->gpa_mkey); 135 136 if (sge1_len) { 137 sg_i = 1; 138 da = dma_map_single(dev, skb->data + sge0_len, sge1_len, 139 DMA_TO_DEVICE); 140 if (dma_mapping_error(dev, da)) 141 goto frag_err; 142 143 mana_add_sge(tp, ash, sg_i, da, sge1_len, gd->gpa_mkey); 144 hsg = 2; 145 } 146 147 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 148 sg_i = hsg + i; 149 150 frag = &skb_shinfo(skb)->frags[i]; 151 da = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag), 152 DMA_TO_DEVICE); 153 if (dma_mapping_error(dev, da)) 154 goto frag_err; 155 156 mana_add_sge(tp, ash, sg_i, da, skb_frag_size(frag), 157 gd->gpa_mkey); 158 } 159 160 return 0; 161 162 frag_err: 163 for (i = sg_i - 1; i >= hsg; i--) 164 dma_unmap_page(dev, ash->dma_handle[i], ash->size[i], 165 DMA_TO_DEVICE); 166 167 for (i = hsg - 1; i >= 0; i--) 168 dma_unmap_single(dev, ash->dma_handle[i], ash->size[i], 169 DMA_TO_DEVICE); 170 171 return -ENOMEM; 172 } 173 174 /* Handle the case when GSO SKB linear length is too large. 175 * MANA NIC requires GSO packets to put only the packet header to SGE0. 176 * So, we need 2 SGEs for the skb linear part which contains more than the 177 * header. 178 * Return a positive value for the number of SGEs, or a negative value 179 * for an error. 180 */ 181 static int mana_fix_skb_head(struct net_device *ndev, struct sk_buff *skb, 182 int gso_hs) 183 { 184 int num_sge = 1 + skb_shinfo(skb)->nr_frags; 185 int skb_hlen = skb_headlen(skb); 186 187 if (gso_hs < skb_hlen) { 188 num_sge++; 189 } else if (gso_hs > skb_hlen) { 190 if (net_ratelimit()) 191 netdev_err(ndev, 192 "TX nonlinear head: hs:%d, skb_hlen:%d\n", 193 gso_hs, skb_hlen); 194 195 return -EINVAL; 196 } 197 198 return num_sge; 199 } 200 201 /* Get the GSO packet's header size */ 202 static int mana_get_gso_hs(struct sk_buff *skb) 203 { 204 int gso_hs; 205 206 if (skb->encapsulation) { 207 gso_hs = skb_inner_tcp_all_headers(skb); 208 } else { 209 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) { 210 gso_hs = skb_transport_offset(skb) + 211 sizeof(struct udphdr); 212 } else { 213 gso_hs = skb_tcp_all_headers(skb); 214 } 215 } 216 217 return gso_hs; 218 } 219 220 netdev_tx_t mana_start_xmit(struct sk_buff *skb, struct net_device *ndev) 221 { 222 enum mana_tx_pkt_format pkt_fmt = MANA_SHORT_PKT_FMT; 223 struct mana_port_context *apc = netdev_priv(ndev); 224 int gso_hs = 0; /* zero for non-GSO pkts */ 225 u16 txq_idx = skb_get_queue_mapping(skb); 226 struct gdma_dev *gd = apc->ac->gdma_dev; 227 bool ipv4 = false, ipv6 = false; 228 struct mana_tx_package pkg = {}; 229 struct netdev_queue *net_txq; 230 struct mana_stats_tx *tx_stats; 231 struct gdma_queue *gdma_sq; 232 unsigned int csum_type; 233 struct mana_txq *txq; 234 struct mana_cq *cq; 235 int err, len; 236 237 if (unlikely(!apc->port_is_up)) 238 goto tx_drop; 239 240 if (skb_cow_head(skb, MANA_HEADROOM)) 241 goto tx_drop_count; 242 243 txq = &apc->tx_qp[txq_idx].txq; 244 gdma_sq = txq->gdma_sq; 245 cq = &apc->tx_qp[txq_idx].tx_cq; 246 tx_stats = &txq->stats; 247 248 pkg.tx_oob.s_oob.vcq_num = cq->gdma_id; 249 pkg.tx_oob.s_oob.vsq_frame = txq->vsq_frame; 250 251 if (txq->vp_offset > MANA_SHORT_VPORT_OFFSET_MAX) { 252 pkg.tx_oob.l_oob.long_vp_offset = txq->vp_offset; 253 pkt_fmt = MANA_LONG_PKT_FMT; 254 } else { 255 pkg.tx_oob.s_oob.short_vp_offset = txq->vp_offset; 256 } 257 258 if (skb_vlan_tag_present(skb)) { 259 pkt_fmt = MANA_LONG_PKT_FMT; 260 pkg.tx_oob.l_oob.inject_vlan_pri_tag = 1; 261 pkg.tx_oob.l_oob.pcp = skb_vlan_tag_get_prio(skb); 262 pkg.tx_oob.l_oob.dei = skb_vlan_tag_get_cfi(skb); 263 pkg.tx_oob.l_oob.vlan_id = skb_vlan_tag_get_id(skb); 264 } 265 266 pkg.tx_oob.s_oob.pkt_fmt = pkt_fmt; 267 268 if (pkt_fmt == MANA_SHORT_PKT_FMT) { 269 pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_short_oob); 270 u64_stats_update_begin(&tx_stats->syncp); 271 tx_stats->short_pkt_fmt++; 272 u64_stats_update_end(&tx_stats->syncp); 273 } else { 274 pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_oob); 275 u64_stats_update_begin(&tx_stats->syncp); 276 tx_stats->long_pkt_fmt++; 277 u64_stats_update_end(&tx_stats->syncp); 278 } 279 280 pkg.wqe_req.inline_oob_data = &pkg.tx_oob; 281 pkg.wqe_req.flags = 0; 282 pkg.wqe_req.client_data_unit = 0; 283 284 pkg.wqe_req.num_sge = 1 + skb_shinfo(skb)->nr_frags; 285 286 if (skb->protocol == htons(ETH_P_IP)) 287 ipv4 = true; 288 else if (skb->protocol == htons(ETH_P_IPV6)) 289 ipv6 = true; 290 291 if (skb_is_gso(skb)) { 292 int num_sge; 293 294 gso_hs = mana_get_gso_hs(skb); 295 296 num_sge = mana_fix_skb_head(ndev, skb, gso_hs); 297 if (num_sge > 0) 298 pkg.wqe_req.num_sge = num_sge; 299 else 300 goto tx_drop_count; 301 302 u64_stats_update_begin(&tx_stats->syncp); 303 if (skb->encapsulation) { 304 tx_stats->tso_inner_packets++; 305 tx_stats->tso_inner_bytes += skb->len - gso_hs; 306 } else { 307 tx_stats->tso_packets++; 308 tx_stats->tso_bytes += skb->len - gso_hs; 309 } 310 u64_stats_update_end(&tx_stats->syncp); 311 312 pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4; 313 pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6; 314 315 pkg.tx_oob.s_oob.comp_iphdr_csum = 1; 316 pkg.tx_oob.s_oob.comp_tcp_csum = 1; 317 pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb); 318 319 pkg.wqe_req.client_data_unit = skb_shinfo(skb)->gso_size; 320 pkg.wqe_req.flags = GDMA_WR_OOB_IN_SGL | GDMA_WR_PAD_BY_SGE0; 321 if (ipv4) { 322 ip_hdr(skb)->tot_len = 0; 323 ip_hdr(skb)->check = 0; 324 tcp_hdr(skb)->check = 325 ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 326 ip_hdr(skb)->daddr, 0, 327 IPPROTO_TCP, 0); 328 } else { 329 ipv6_hdr(skb)->payload_len = 0; 330 tcp_hdr(skb)->check = 331 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 332 &ipv6_hdr(skb)->daddr, 0, 333 IPPROTO_TCP, 0); 334 } 335 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 336 csum_type = mana_checksum_info(skb); 337 338 u64_stats_update_begin(&tx_stats->syncp); 339 tx_stats->csum_partial++; 340 u64_stats_update_end(&tx_stats->syncp); 341 342 if (csum_type == IPPROTO_TCP) { 343 pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4; 344 pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6; 345 346 pkg.tx_oob.s_oob.comp_tcp_csum = 1; 347 pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb); 348 349 } else if (csum_type == IPPROTO_UDP) { 350 pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4; 351 pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6; 352 353 pkg.tx_oob.s_oob.comp_udp_csum = 1; 354 } else { 355 /* Can't do offload of this type of checksum */ 356 if (skb_checksum_help(skb)) 357 goto tx_drop_count; 358 } 359 } 360 361 WARN_ON_ONCE(pkg.wqe_req.num_sge > MAX_TX_WQE_SGL_ENTRIES); 362 363 if (pkg.wqe_req.num_sge <= ARRAY_SIZE(pkg.sgl_array)) { 364 pkg.wqe_req.sgl = pkg.sgl_array; 365 } else { 366 pkg.sgl_ptr = kmalloc_array(pkg.wqe_req.num_sge, 367 sizeof(struct gdma_sge), 368 GFP_ATOMIC); 369 if (!pkg.sgl_ptr) 370 goto tx_drop_count; 371 372 pkg.wqe_req.sgl = pkg.sgl_ptr; 373 } 374 375 if (mana_map_skb(skb, apc, &pkg, gso_hs)) { 376 u64_stats_update_begin(&tx_stats->syncp); 377 tx_stats->mana_map_err++; 378 u64_stats_update_end(&tx_stats->syncp); 379 goto free_sgl_ptr; 380 } 381 382 skb_queue_tail(&txq->pending_skbs, skb); 383 384 len = skb->len; 385 net_txq = netdev_get_tx_queue(ndev, txq_idx); 386 387 err = mana_gd_post_work_request(gdma_sq, &pkg.wqe_req, 388 (struct gdma_posted_wqe_info *)skb->cb); 389 if (!mana_can_tx(gdma_sq)) { 390 netif_tx_stop_queue(net_txq); 391 apc->eth_stats.stop_queue++; 392 } 393 394 if (err) { 395 (void)skb_dequeue_tail(&txq->pending_skbs); 396 netdev_warn(ndev, "Failed to post TX OOB: %d\n", err); 397 err = NETDEV_TX_BUSY; 398 goto tx_busy; 399 } 400 401 err = NETDEV_TX_OK; 402 atomic_inc(&txq->pending_sends); 403 404 mana_gd_wq_ring_doorbell(gd->gdma_context, gdma_sq); 405 406 /* skb may be freed after mana_gd_post_work_request. Do not use it. */ 407 skb = NULL; 408 409 tx_stats = &txq->stats; 410 u64_stats_update_begin(&tx_stats->syncp); 411 tx_stats->packets++; 412 tx_stats->bytes += len; 413 u64_stats_update_end(&tx_stats->syncp); 414 415 tx_busy: 416 if (netif_tx_queue_stopped(net_txq) && mana_can_tx(gdma_sq)) { 417 netif_tx_wake_queue(net_txq); 418 apc->eth_stats.wake_queue++; 419 } 420 421 kfree(pkg.sgl_ptr); 422 return err; 423 424 free_sgl_ptr: 425 kfree(pkg.sgl_ptr); 426 tx_drop_count: 427 ndev->stats.tx_dropped++; 428 tx_drop: 429 dev_kfree_skb_any(skb); 430 return NETDEV_TX_OK; 431 } 432 433 static void mana_get_stats64(struct net_device *ndev, 434 struct rtnl_link_stats64 *st) 435 { 436 struct mana_port_context *apc = netdev_priv(ndev); 437 unsigned int num_queues = apc->num_queues; 438 struct mana_stats_rx *rx_stats; 439 struct mana_stats_tx *tx_stats; 440 unsigned int start; 441 u64 packets, bytes; 442 int q; 443 444 if (!apc->port_is_up) 445 return; 446 447 netdev_stats_to_stats64(st, &ndev->stats); 448 449 for (q = 0; q < num_queues; q++) { 450 rx_stats = &apc->rxqs[q]->stats; 451 452 do { 453 start = u64_stats_fetch_begin(&rx_stats->syncp); 454 packets = rx_stats->packets; 455 bytes = rx_stats->bytes; 456 } while (u64_stats_fetch_retry(&rx_stats->syncp, start)); 457 458 st->rx_packets += packets; 459 st->rx_bytes += bytes; 460 } 461 462 for (q = 0; q < num_queues; q++) { 463 tx_stats = &apc->tx_qp[q].txq.stats; 464 465 do { 466 start = u64_stats_fetch_begin(&tx_stats->syncp); 467 packets = tx_stats->packets; 468 bytes = tx_stats->bytes; 469 } while (u64_stats_fetch_retry(&tx_stats->syncp, start)); 470 471 st->tx_packets += packets; 472 st->tx_bytes += bytes; 473 } 474 } 475 476 static int mana_get_tx_queue(struct net_device *ndev, struct sk_buff *skb, 477 int old_q) 478 { 479 struct mana_port_context *apc = netdev_priv(ndev); 480 u32 hash = skb_get_hash(skb); 481 struct sock *sk = skb->sk; 482 int txq; 483 484 txq = apc->indir_table[hash & MANA_INDIRECT_TABLE_MASK]; 485 486 if (txq != old_q && sk && sk_fullsock(sk) && 487 rcu_access_pointer(sk->sk_dst_cache)) 488 sk_tx_queue_set(sk, txq); 489 490 return txq; 491 } 492 493 static u16 mana_select_queue(struct net_device *ndev, struct sk_buff *skb, 494 struct net_device *sb_dev) 495 { 496 int txq; 497 498 if (ndev->real_num_tx_queues == 1) 499 return 0; 500 501 txq = sk_tx_queue_get(skb->sk); 502 503 if (txq < 0 || skb->ooo_okay || txq >= ndev->real_num_tx_queues) { 504 if (skb_rx_queue_recorded(skb)) 505 txq = skb_get_rx_queue(skb); 506 else 507 txq = mana_get_tx_queue(ndev, skb, txq); 508 } 509 510 return txq; 511 } 512 513 /* Release pre-allocated RX buffers */ 514 static void mana_pre_dealloc_rxbufs(struct mana_port_context *mpc) 515 { 516 struct device *dev; 517 int i; 518 519 dev = mpc->ac->gdma_dev->gdma_context->dev; 520 521 if (!mpc->rxbufs_pre) 522 goto out1; 523 524 if (!mpc->das_pre) 525 goto out2; 526 527 while (mpc->rxbpre_total) { 528 i = --mpc->rxbpre_total; 529 dma_unmap_single(dev, mpc->das_pre[i], mpc->rxbpre_datasize, 530 DMA_FROM_DEVICE); 531 put_page(virt_to_head_page(mpc->rxbufs_pre[i])); 532 } 533 534 kfree(mpc->das_pre); 535 mpc->das_pre = NULL; 536 537 out2: 538 kfree(mpc->rxbufs_pre); 539 mpc->rxbufs_pre = NULL; 540 541 out1: 542 mpc->rxbpre_datasize = 0; 543 mpc->rxbpre_alloc_size = 0; 544 mpc->rxbpre_headroom = 0; 545 } 546 547 /* Get a buffer from the pre-allocated RX buffers */ 548 static void *mana_get_rxbuf_pre(struct mana_rxq *rxq, dma_addr_t *da) 549 { 550 struct net_device *ndev = rxq->ndev; 551 struct mana_port_context *mpc; 552 void *va; 553 554 mpc = netdev_priv(ndev); 555 556 if (!mpc->rxbufs_pre || !mpc->das_pre || !mpc->rxbpre_total) { 557 netdev_err(ndev, "No RX pre-allocated bufs\n"); 558 return NULL; 559 } 560 561 /* Check sizes to catch unexpected coding error */ 562 if (mpc->rxbpre_datasize != rxq->datasize) { 563 netdev_err(ndev, "rxbpre_datasize mismatch: %u: %u\n", 564 mpc->rxbpre_datasize, rxq->datasize); 565 return NULL; 566 } 567 568 if (mpc->rxbpre_alloc_size != rxq->alloc_size) { 569 netdev_err(ndev, "rxbpre_alloc_size mismatch: %u: %u\n", 570 mpc->rxbpre_alloc_size, rxq->alloc_size); 571 return NULL; 572 } 573 574 if (mpc->rxbpre_headroom != rxq->headroom) { 575 netdev_err(ndev, "rxbpre_headroom mismatch: %u: %u\n", 576 mpc->rxbpre_headroom, rxq->headroom); 577 return NULL; 578 } 579 580 mpc->rxbpre_total--; 581 582 *da = mpc->das_pre[mpc->rxbpre_total]; 583 va = mpc->rxbufs_pre[mpc->rxbpre_total]; 584 mpc->rxbufs_pre[mpc->rxbpre_total] = NULL; 585 586 /* Deallocate the array after all buffers are gone */ 587 if (!mpc->rxbpre_total) 588 mana_pre_dealloc_rxbufs(mpc); 589 590 return va; 591 } 592 593 /* Get RX buffer's data size, alloc size, XDP headroom based on MTU */ 594 static void mana_get_rxbuf_cfg(int mtu, u32 *datasize, u32 *alloc_size, 595 u32 *headroom) 596 { 597 if (mtu > MANA_XDP_MTU_MAX) 598 *headroom = 0; /* no support for XDP */ 599 else 600 *headroom = XDP_PACKET_HEADROOM; 601 602 *alloc_size = mtu + MANA_RXBUF_PAD + *headroom; 603 604 *datasize = mtu + ETH_HLEN; 605 } 606 607 static int mana_pre_alloc_rxbufs(struct mana_port_context *mpc, int new_mtu) 608 { 609 struct device *dev; 610 struct page *page; 611 dma_addr_t da; 612 int num_rxb; 613 void *va; 614 int i; 615 616 mana_get_rxbuf_cfg(new_mtu, &mpc->rxbpre_datasize, 617 &mpc->rxbpre_alloc_size, &mpc->rxbpre_headroom); 618 619 dev = mpc->ac->gdma_dev->gdma_context->dev; 620 621 num_rxb = mpc->num_queues * RX_BUFFERS_PER_QUEUE; 622 623 WARN(mpc->rxbufs_pre, "mana rxbufs_pre exists\n"); 624 mpc->rxbufs_pre = kmalloc_array(num_rxb, sizeof(void *), GFP_KERNEL); 625 if (!mpc->rxbufs_pre) 626 goto error; 627 628 mpc->das_pre = kmalloc_array(num_rxb, sizeof(dma_addr_t), GFP_KERNEL); 629 if (!mpc->das_pre) 630 goto error; 631 632 mpc->rxbpre_total = 0; 633 634 for (i = 0; i < num_rxb; i++) { 635 if (mpc->rxbpre_alloc_size > PAGE_SIZE) { 636 va = netdev_alloc_frag(mpc->rxbpre_alloc_size); 637 if (!va) 638 goto error; 639 640 page = virt_to_head_page(va); 641 /* Check if the frag falls back to single page */ 642 if (compound_order(page) < 643 get_order(mpc->rxbpre_alloc_size)) { 644 put_page(page); 645 goto error; 646 } 647 } else { 648 page = dev_alloc_page(); 649 if (!page) 650 goto error; 651 652 va = page_to_virt(page); 653 } 654 655 da = dma_map_single(dev, va + mpc->rxbpre_headroom, 656 mpc->rxbpre_datasize, DMA_FROM_DEVICE); 657 if (dma_mapping_error(dev, da)) { 658 put_page(virt_to_head_page(va)); 659 goto error; 660 } 661 662 mpc->rxbufs_pre[i] = va; 663 mpc->das_pre[i] = da; 664 mpc->rxbpre_total = i + 1; 665 } 666 667 return 0; 668 669 error: 670 mana_pre_dealloc_rxbufs(mpc); 671 return -ENOMEM; 672 } 673 674 static int mana_change_mtu(struct net_device *ndev, int new_mtu) 675 { 676 struct mana_port_context *mpc = netdev_priv(ndev); 677 unsigned int old_mtu = ndev->mtu; 678 int err; 679 680 /* Pre-allocate buffers to prevent failure in mana_attach later */ 681 err = mana_pre_alloc_rxbufs(mpc, new_mtu); 682 if (err) { 683 netdev_err(ndev, "Insufficient memory for new MTU\n"); 684 return err; 685 } 686 687 err = mana_detach(ndev, false); 688 if (err) { 689 netdev_err(ndev, "mana_detach failed: %d\n", err); 690 goto out; 691 } 692 693 WRITE_ONCE(ndev->mtu, new_mtu); 694 695 err = mana_attach(ndev); 696 if (err) { 697 netdev_err(ndev, "mana_attach failed: %d\n", err); 698 WRITE_ONCE(ndev->mtu, old_mtu); 699 } 700 701 out: 702 mana_pre_dealloc_rxbufs(mpc); 703 return err; 704 } 705 706 static const struct net_device_ops mana_devops = { 707 .ndo_open = mana_open, 708 .ndo_stop = mana_close, 709 .ndo_select_queue = mana_select_queue, 710 .ndo_start_xmit = mana_start_xmit, 711 .ndo_validate_addr = eth_validate_addr, 712 .ndo_get_stats64 = mana_get_stats64, 713 .ndo_bpf = mana_bpf, 714 .ndo_xdp_xmit = mana_xdp_xmit, 715 .ndo_change_mtu = mana_change_mtu, 716 }; 717 718 static void mana_cleanup_port_context(struct mana_port_context *apc) 719 { 720 kfree(apc->rxqs); 721 apc->rxqs = NULL; 722 } 723 724 static int mana_init_port_context(struct mana_port_context *apc) 725 { 726 apc->rxqs = kcalloc(apc->num_queues, sizeof(struct mana_rxq *), 727 GFP_KERNEL); 728 729 return !apc->rxqs ? -ENOMEM : 0; 730 } 731 732 static int mana_send_request(struct mana_context *ac, void *in_buf, 733 u32 in_len, void *out_buf, u32 out_len) 734 { 735 struct gdma_context *gc = ac->gdma_dev->gdma_context; 736 struct gdma_resp_hdr *resp = out_buf; 737 struct gdma_req_hdr *req = in_buf; 738 struct device *dev = gc->dev; 739 static atomic_t activity_id; 740 int err; 741 742 req->dev_id = gc->mana.dev_id; 743 req->activity_id = atomic_inc_return(&activity_id); 744 745 err = mana_gd_send_request(gc, in_len, in_buf, out_len, 746 out_buf); 747 if (err || resp->status) { 748 dev_err(dev, "Failed to send mana message: %d, 0x%x\n", 749 err, resp->status); 750 return err ? err : -EPROTO; 751 } 752 753 if (req->dev_id.as_uint32 != resp->dev_id.as_uint32 || 754 req->activity_id != resp->activity_id) { 755 dev_err(dev, "Unexpected mana message response: %x,%x,%x,%x\n", 756 req->dev_id.as_uint32, resp->dev_id.as_uint32, 757 req->activity_id, resp->activity_id); 758 return -EPROTO; 759 } 760 761 return 0; 762 } 763 764 static int mana_verify_resp_hdr(const struct gdma_resp_hdr *resp_hdr, 765 const enum mana_command_code expected_code, 766 const u32 min_size) 767 { 768 if (resp_hdr->response.msg_type != expected_code) 769 return -EPROTO; 770 771 if (resp_hdr->response.msg_version < GDMA_MESSAGE_V1) 772 return -EPROTO; 773 774 if (resp_hdr->response.msg_size < min_size) 775 return -EPROTO; 776 777 return 0; 778 } 779 780 static int mana_pf_register_hw_vport(struct mana_port_context *apc) 781 { 782 struct mana_register_hw_vport_resp resp = {}; 783 struct mana_register_hw_vport_req req = {}; 784 int err; 785 786 mana_gd_init_req_hdr(&req.hdr, MANA_REGISTER_HW_PORT, 787 sizeof(req), sizeof(resp)); 788 req.attached_gfid = 1; 789 req.is_pf_default_vport = 1; 790 req.allow_all_ether_types = 1; 791 792 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 793 sizeof(resp)); 794 if (err) { 795 netdev_err(apc->ndev, "Failed to register hw vPort: %d\n", err); 796 return err; 797 } 798 799 err = mana_verify_resp_hdr(&resp.hdr, MANA_REGISTER_HW_PORT, 800 sizeof(resp)); 801 if (err || resp.hdr.status) { 802 netdev_err(apc->ndev, "Failed to register hw vPort: %d, 0x%x\n", 803 err, resp.hdr.status); 804 return err ? err : -EPROTO; 805 } 806 807 apc->port_handle = resp.hw_vport_handle; 808 return 0; 809 } 810 811 static void mana_pf_deregister_hw_vport(struct mana_port_context *apc) 812 { 813 struct mana_deregister_hw_vport_resp resp = {}; 814 struct mana_deregister_hw_vport_req req = {}; 815 int err; 816 817 mana_gd_init_req_hdr(&req.hdr, MANA_DEREGISTER_HW_PORT, 818 sizeof(req), sizeof(resp)); 819 req.hw_vport_handle = apc->port_handle; 820 821 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 822 sizeof(resp)); 823 if (err) { 824 netdev_err(apc->ndev, "Failed to unregister hw vPort: %d\n", 825 err); 826 return; 827 } 828 829 err = mana_verify_resp_hdr(&resp.hdr, MANA_DEREGISTER_HW_PORT, 830 sizeof(resp)); 831 if (err || resp.hdr.status) 832 netdev_err(apc->ndev, 833 "Failed to deregister hw vPort: %d, 0x%x\n", 834 err, resp.hdr.status); 835 } 836 837 static int mana_pf_register_filter(struct mana_port_context *apc) 838 { 839 struct mana_register_filter_resp resp = {}; 840 struct mana_register_filter_req req = {}; 841 int err; 842 843 mana_gd_init_req_hdr(&req.hdr, MANA_REGISTER_FILTER, 844 sizeof(req), sizeof(resp)); 845 req.vport = apc->port_handle; 846 memcpy(req.mac_addr, apc->mac_addr, ETH_ALEN); 847 848 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 849 sizeof(resp)); 850 if (err) { 851 netdev_err(apc->ndev, "Failed to register filter: %d\n", err); 852 return err; 853 } 854 855 err = mana_verify_resp_hdr(&resp.hdr, MANA_REGISTER_FILTER, 856 sizeof(resp)); 857 if (err || resp.hdr.status) { 858 netdev_err(apc->ndev, "Failed to register filter: %d, 0x%x\n", 859 err, resp.hdr.status); 860 return err ? err : -EPROTO; 861 } 862 863 apc->pf_filter_handle = resp.filter_handle; 864 return 0; 865 } 866 867 static void mana_pf_deregister_filter(struct mana_port_context *apc) 868 { 869 struct mana_deregister_filter_resp resp = {}; 870 struct mana_deregister_filter_req req = {}; 871 int err; 872 873 mana_gd_init_req_hdr(&req.hdr, MANA_DEREGISTER_FILTER, 874 sizeof(req), sizeof(resp)); 875 req.filter_handle = apc->pf_filter_handle; 876 877 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 878 sizeof(resp)); 879 if (err) { 880 netdev_err(apc->ndev, "Failed to unregister filter: %d\n", 881 err); 882 return; 883 } 884 885 err = mana_verify_resp_hdr(&resp.hdr, MANA_DEREGISTER_FILTER, 886 sizeof(resp)); 887 if (err || resp.hdr.status) 888 netdev_err(apc->ndev, 889 "Failed to deregister filter: %d, 0x%x\n", 890 err, resp.hdr.status); 891 } 892 893 static int mana_query_device_cfg(struct mana_context *ac, u32 proto_major_ver, 894 u32 proto_minor_ver, u32 proto_micro_ver, 895 u16 *max_num_vports) 896 { 897 struct gdma_context *gc = ac->gdma_dev->gdma_context; 898 struct mana_query_device_cfg_resp resp = {}; 899 struct mana_query_device_cfg_req req = {}; 900 struct device *dev = gc->dev; 901 int err = 0; 902 903 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_DEV_CONFIG, 904 sizeof(req), sizeof(resp)); 905 906 req.hdr.resp.msg_version = GDMA_MESSAGE_V2; 907 908 req.proto_major_ver = proto_major_ver; 909 req.proto_minor_ver = proto_minor_ver; 910 req.proto_micro_ver = proto_micro_ver; 911 912 err = mana_send_request(ac, &req, sizeof(req), &resp, sizeof(resp)); 913 if (err) { 914 dev_err(dev, "Failed to query config: %d", err); 915 return err; 916 } 917 918 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_DEV_CONFIG, 919 sizeof(resp)); 920 if (err || resp.hdr.status) { 921 dev_err(dev, "Invalid query result: %d, 0x%x\n", err, 922 resp.hdr.status); 923 if (!err) 924 err = -EPROTO; 925 return err; 926 } 927 928 *max_num_vports = resp.max_num_vports; 929 930 if (resp.hdr.response.msg_version == GDMA_MESSAGE_V2) 931 gc->adapter_mtu = resp.adapter_mtu; 932 else 933 gc->adapter_mtu = ETH_FRAME_LEN; 934 935 return 0; 936 } 937 938 static int mana_query_vport_cfg(struct mana_port_context *apc, u32 vport_index, 939 u32 *max_sq, u32 *max_rq, u32 *num_indir_entry) 940 { 941 struct mana_query_vport_cfg_resp resp = {}; 942 struct mana_query_vport_cfg_req req = {}; 943 int err; 944 945 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_VPORT_CONFIG, 946 sizeof(req), sizeof(resp)); 947 948 req.vport_index = vport_index; 949 950 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 951 sizeof(resp)); 952 if (err) 953 return err; 954 955 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_VPORT_CONFIG, 956 sizeof(resp)); 957 if (err) 958 return err; 959 960 if (resp.hdr.status) 961 return -EPROTO; 962 963 *max_sq = resp.max_num_sq; 964 *max_rq = resp.max_num_rq; 965 *num_indir_entry = resp.num_indirection_ent; 966 967 apc->port_handle = resp.vport; 968 ether_addr_copy(apc->mac_addr, resp.mac_addr); 969 970 return 0; 971 } 972 973 void mana_uncfg_vport(struct mana_port_context *apc) 974 { 975 mutex_lock(&apc->vport_mutex); 976 apc->vport_use_count--; 977 WARN_ON(apc->vport_use_count < 0); 978 mutex_unlock(&apc->vport_mutex); 979 } 980 EXPORT_SYMBOL_NS(mana_uncfg_vport, NET_MANA); 981 982 int mana_cfg_vport(struct mana_port_context *apc, u32 protection_dom_id, 983 u32 doorbell_pg_id) 984 { 985 struct mana_config_vport_resp resp = {}; 986 struct mana_config_vport_req req = {}; 987 int err; 988 989 /* This function is used to program the Ethernet port in the hardware 990 * table. It can be called from the Ethernet driver or the RDMA driver. 991 * 992 * For Ethernet usage, the hardware supports only one active user on a 993 * physical port. The driver checks on the port usage before programming 994 * the hardware when creating the RAW QP (RDMA driver) or exposing the 995 * device to kernel NET layer (Ethernet driver). 996 * 997 * Because the RDMA driver doesn't know in advance which QP type the 998 * user will create, it exposes the device with all its ports. The user 999 * may not be able to create RAW QP on a port if this port is already 1000 * in used by the Ethernet driver from the kernel. 1001 * 1002 * This physical port limitation only applies to the RAW QP. For RC QP, 1003 * the hardware doesn't have this limitation. The user can create RC 1004 * QPs on a physical port up to the hardware limits independent of the 1005 * Ethernet usage on the same port. 1006 */ 1007 mutex_lock(&apc->vport_mutex); 1008 if (apc->vport_use_count > 0) { 1009 mutex_unlock(&apc->vport_mutex); 1010 return -EBUSY; 1011 } 1012 apc->vport_use_count++; 1013 mutex_unlock(&apc->vport_mutex); 1014 1015 mana_gd_init_req_hdr(&req.hdr, MANA_CONFIG_VPORT_TX, 1016 sizeof(req), sizeof(resp)); 1017 req.vport = apc->port_handle; 1018 req.pdid = protection_dom_id; 1019 req.doorbell_pageid = doorbell_pg_id; 1020 1021 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1022 sizeof(resp)); 1023 if (err) { 1024 netdev_err(apc->ndev, "Failed to configure vPort: %d\n", err); 1025 goto out; 1026 } 1027 1028 err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_TX, 1029 sizeof(resp)); 1030 if (err || resp.hdr.status) { 1031 netdev_err(apc->ndev, "Failed to configure vPort: %d, 0x%x\n", 1032 err, resp.hdr.status); 1033 if (!err) 1034 err = -EPROTO; 1035 1036 goto out; 1037 } 1038 1039 apc->tx_shortform_allowed = resp.short_form_allowed; 1040 apc->tx_vp_offset = resp.tx_vport_offset; 1041 1042 netdev_info(apc->ndev, "Configured vPort %llu PD %u DB %u\n", 1043 apc->port_handle, protection_dom_id, doorbell_pg_id); 1044 out: 1045 if (err) 1046 mana_uncfg_vport(apc); 1047 1048 return err; 1049 } 1050 EXPORT_SYMBOL_NS(mana_cfg_vport, NET_MANA); 1051 1052 static int mana_cfg_vport_steering(struct mana_port_context *apc, 1053 enum TRI_STATE rx, 1054 bool update_default_rxobj, bool update_key, 1055 bool update_tab) 1056 { 1057 u16 num_entries = MANA_INDIRECT_TABLE_SIZE; 1058 struct mana_cfg_rx_steer_req_v2 *req; 1059 struct mana_cfg_rx_steer_resp resp = {}; 1060 struct net_device *ndev = apc->ndev; 1061 u32 req_buf_size; 1062 int err; 1063 1064 req_buf_size = struct_size(req, indir_tab, num_entries); 1065 req = kzalloc(req_buf_size, GFP_KERNEL); 1066 if (!req) 1067 return -ENOMEM; 1068 1069 mana_gd_init_req_hdr(&req->hdr, MANA_CONFIG_VPORT_RX, req_buf_size, 1070 sizeof(resp)); 1071 1072 req->hdr.req.msg_version = GDMA_MESSAGE_V2; 1073 1074 req->vport = apc->port_handle; 1075 req->num_indir_entries = num_entries; 1076 req->indir_tab_offset = offsetof(struct mana_cfg_rx_steer_req_v2, 1077 indir_tab); 1078 req->rx_enable = rx; 1079 req->rss_enable = apc->rss_state; 1080 req->update_default_rxobj = update_default_rxobj; 1081 req->update_hashkey = update_key; 1082 req->update_indir_tab = update_tab; 1083 req->default_rxobj = apc->default_rxobj; 1084 req->cqe_coalescing_enable = 0; 1085 1086 if (update_key) 1087 memcpy(&req->hashkey, apc->hashkey, MANA_HASH_KEY_SIZE); 1088 1089 if (update_tab) 1090 memcpy(req->indir_tab, apc->rxobj_table, 1091 flex_array_size(req, indir_tab, req->num_indir_entries)); 1092 1093 err = mana_send_request(apc->ac, req, req_buf_size, &resp, 1094 sizeof(resp)); 1095 if (err) { 1096 netdev_err(ndev, "Failed to configure vPort RX: %d\n", err); 1097 goto out; 1098 } 1099 1100 err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_RX, 1101 sizeof(resp)); 1102 if (err) { 1103 netdev_err(ndev, "vPort RX configuration failed: %d\n", err); 1104 goto out; 1105 } 1106 1107 if (resp.hdr.status) { 1108 netdev_err(ndev, "vPort RX configuration failed: 0x%x\n", 1109 resp.hdr.status); 1110 err = -EPROTO; 1111 } 1112 1113 netdev_info(ndev, "Configured steering vPort %llu entries %u\n", 1114 apc->port_handle, num_entries); 1115 out: 1116 kfree(req); 1117 return err; 1118 } 1119 1120 int mana_create_wq_obj(struct mana_port_context *apc, 1121 mana_handle_t vport, 1122 u32 wq_type, struct mana_obj_spec *wq_spec, 1123 struct mana_obj_spec *cq_spec, 1124 mana_handle_t *wq_obj) 1125 { 1126 struct mana_create_wqobj_resp resp = {}; 1127 struct mana_create_wqobj_req req = {}; 1128 struct net_device *ndev = apc->ndev; 1129 int err; 1130 1131 mana_gd_init_req_hdr(&req.hdr, MANA_CREATE_WQ_OBJ, 1132 sizeof(req), sizeof(resp)); 1133 req.vport = vport; 1134 req.wq_type = wq_type; 1135 req.wq_gdma_region = wq_spec->gdma_region; 1136 req.cq_gdma_region = cq_spec->gdma_region; 1137 req.wq_size = wq_spec->queue_size; 1138 req.cq_size = cq_spec->queue_size; 1139 req.cq_moderation_ctx_id = cq_spec->modr_ctx_id; 1140 req.cq_parent_qid = cq_spec->attached_eq; 1141 1142 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1143 sizeof(resp)); 1144 if (err) { 1145 netdev_err(ndev, "Failed to create WQ object: %d\n", err); 1146 goto out; 1147 } 1148 1149 err = mana_verify_resp_hdr(&resp.hdr, MANA_CREATE_WQ_OBJ, 1150 sizeof(resp)); 1151 if (err || resp.hdr.status) { 1152 netdev_err(ndev, "Failed to create WQ object: %d, 0x%x\n", err, 1153 resp.hdr.status); 1154 if (!err) 1155 err = -EPROTO; 1156 goto out; 1157 } 1158 1159 if (resp.wq_obj == INVALID_MANA_HANDLE) { 1160 netdev_err(ndev, "Got an invalid WQ object handle\n"); 1161 err = -EPROTO; 1162 goto out; 1163 } 1164 1165 *wq_obj = resp.wq_obj; 1166 wq_spec->queue_index = resp.wq_id; 1167 cq_spec->queue_index = resp.cq_id; 1168 1169 return 0; 1170 out: 1171 return err; 1172 } 1173 EXPORT_SYMBOL_NS(mana_create_wq_obj, NET_MANA); 1174 1175 void mana_destroy_wq_obj(struct mana_port_context *apc, u32 wq_type, 1176 mana_handle_t wq_obj) 1177 { 1178 struct mana_destroy_wqobj_resp resp = {}; 1179 struct mana_destroy_wqobj_req req = {}; 1180 struct net_device *ndev = apc->ndev; 1181 int err; 1182 1183 mana_gd_init_req_hdr(&req.hdr, MANA_DESTROY_WQ_OBJ, 1184 sizeof(req), sizeof(resp)); 1185 req.wq_type = wq_type; 1186 req.wq_obj_handle = wq_obj; 1187 1188 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1189 sizeof(resp)); 1190 if (err) { 1191 netdev_err(ndev, "Failed to destroy WQ object: %d\n", err); 1192 return; 1193 } 1194 1195 err = mana_verify_resp_hdr(&resp.hdr, MANA_DESTROY_WQ_OBJ, 1196 sizeof(resp)); 1197 if (err || resp.hdr.status) 1198 netdev_err(ndev, "Failed to destroy WQ object: %d, 0x%x\n", err, 1199 resp.hdr.status); 1200 } 1201 EXPORT_SYMBOL_NS(mana_destroy_wq_obj, NET_MANA); 1202 1203 static void mana_destroy_eq(struct mana_context *ac) 1204 { 1205 struct gdma_context *gc = ac->gdma_dev->gdma_context; 1206 struct gdma_queue *eq; 1207 int i; 1208 1209 if (!ac->eqs) 1210 return; 1211 1212 for (i = 0; i < gc->max_num_queues; i++) { 1213 eq = ac->eqs[i].eq; 1214 if (!eq) 1215 continue; 1216 1217 mana_gd_destroy_queue(gc, eq); 1218 } 1219 1220 kfree(ac->eqs); 1221 ac->eqs = NULL; 1222 } 1223 1224 static int mana_create_eq(struct mana_context *ac) 1225 { 1226 struct gdma_dev *gd = ac->gdma_dev; 1227 struct gdma_context *gc = gd->gdma_context; 1228 struct gdma_queue_spec spec = {}; 1229 int err; 1230 int i; 1231 1232 ac->eqs = kcalloc(gc->max_num_queues, sizeof(struct mana_eq), 1233 GFP_KERNEL); 1234 if (!ac->eqs) 1235 return -ENOMEM; 1236 1237 spec.type = GDMA_EQ; 1238 spec.monitor_avl_buf = false; 1239 spec.queue_size = EQ_SIZE; 1240 spec.eq.callback = NULL; 1241 spec.eq.context = ac->eqs; 1242 spec.eq.log2_throttle_limit = LOG2_EQ_THROTTLE; 1243 1244 for (i = 0; i < gc->max_num_queues; i++) { 1245 spec.eq.msix_index = (i + 1) % gc->num_msix_usable; 1246 err = mana_gd_create_mana_eq(gd, &spec, &ac->eqs[i].eq); 1247 if (err) 1248 goto out; 1249 } 1250 1251 return 0; 1252 out: 1253 mana_destroy_eq(ac); 1254 return err; 1255 } 1256 1257 static int mana_fence_rq(struct mana_port_context *apc, struct mana_rxq *rxq) 1258 { 1259 struct mana_fence_rq_resp resp = {}; 1260 struct mana_fence_rq_req req = {}; 1261 int err; 1262 1263 init_completion(&rxq->fence_event); 1264 1265 mana_gd_init_req_hdr(&req.hdr, MANA_FENCE_RQ, 1266 sizeof(req), sizeof(resp)); 1267 req.wq_obj_handle = rxq->rxobj; 1268 1269 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1270 sizeof(resp)); 1271 if (err) { 1272 netdev_err(apc->ndev, "Failed to fence RQ %u: %d\n", 1273 rxq->rxq_idx, err); 1274 return err; 1275 } 1276 1277 err = mana_verify_resp_hdr(&resp.hdr, MANA_FENCE_RQ, sizeof(resp)); 1278 if (err || resp.hdr.status) { 1279 netdev_err(apc->ndev, "Failed to fence RQ %u: %d, 0x%x\n", 1280 rxq->rxq_idx, err, resp.hdr.status); 1281 if (!err) 1282 err = -EPROTO; 1283 1284 return err; 1285 } 1286 1287 if (wait_for_completion_timeout(&rxq->fence_event, 10 * HZ) == 0) { 1288 netdev_err(apc->ndev, "Failed to fence RQ %u: timed out\n", 1289 rxq->rxq_idx); 1290 return -ETIMEDOUT; 1291 } 1292 1293 return 0; 1294 } 1295 1296 static void mana_fence_rqs(struct mana_port_context *apc) 1297 { 1298 unsigned int rxq_idx; 1299 struct mana_rxq *rxq; 1300 int err; 1301 1302 for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) { 1303 rxq = apc->rxqs[rxq_idx]; 1304 err = mana_fence_rq(apc, rxq); 1305 1306 /* In case of any error, use sleep instead. */ 1307 if (err) 1308 msleep(100); 1309 } 1310 } 1311 1312 static int mana_move_wq_tail(struct gdma_queue *wq, u32 num_units) 1313 { 1314 u32 used_space_old; 1315 u32 used_space_new; 1316 1317 used_space_old = wq->head - wq->tail; 1318 used_space_new = wq->head - (wq->tail + num_units); 1319 1320 if (WARN_ON_ONCE(used_space_new > used_space_old)) 1321 return -ERANGE; 1322 1323 wq->tail += num_units; 1324 return 0; 1325 } 1326 1327 static void mana_unmap_skb(struct sk_buff *skb, struct mana_port_context *apc) 1328 { 1329 struct mana_skb_head *ash = (struct mana_skb_head *)skb->head; 1330 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context; 1331 struct device *dev = gc->dev; 1332 int hsg, i; 1333 1334 /* Number of SGEs of linear part */ 1335 hsg = (skb_is_gso(skb) && skb_headlen(skb) > ash->size[0]) ? 2 : 1; 1336 1337 for (i = 0; i < hsg; i++) 1338 dma_unmap_single(dev, ash->dma_handle[i], ash->size[i], 1339 DMA_TO_DEVICE); 1340 1341 for (i = hsg; i < skb_shinfo(skb)->nr_frags + hsg; i++) 1342 dma_unmap_page(dev, ash->dma_handle[i], ash->size[i], 1343 DMA_TO_DEVICE); 1344 } 1345 1346 static void mana_poll_tx_cq(struct mana_cq *cq) 1347 { 1348 struct gdma_comp *completions = cq->gdma_comp_buf; 1349 struct gdma_posted_wqe_info *wqe_info; 1350 unsigned int pkt_transmitted = 0; 1351 unsigned int wqe_unit_cnt = 0; 1352 struct mana_txq *txq = cq->txq; 1353 struct mana_port_context *apc; 1354 struct netdev_queue *net_txq; 1355 struct gdma_queue *gdma_wq; 1356 unsigned int avail_space; 1357 struct net_device *ndev; 1358 struct sk_buff *skb; 1359 bool txq_stopped; 1360 int comp_read; 1361 int i; 1362 1363 ndev = txq->ndev; 1364 apc = netdev_priv(ndev); 1365 1366 comp_read = mana_gd_poll_cq(cq->gdma_cq, completions, 1367 CQE_POLLING_BUFFER); 1368 1369 if (comp_read < 1) 1370 return; 1371 1372 for (i = 0; i < comp_read; i++) { 1373 struct mana_tx_comp_oob *cqe_oob; 1374 1375 if (WARN_ON_ONCE(!completions[i].is_sq)) 1376 return; 1377 1378 cqe_oob = (struct mana_tx_comp_oob *)completions[i].cqe_data; 1379 if (WARN_ON_ONCE(cqe_oob->cqe_hdr.client_type != 1380 MANA_CQE_COMPLETION)) 1381 return; 1382 1383 switch (cqe_oob->cqe_hdr.cqe_type) { 1384 case CQE_TX_OKAY: 1385 break; 1386 1387 case CQE_TX_SA_DROP: 1388 case CQE_TX_MTU_DROP: 1389 case CQE_TX_INVALID_OOB: 1390 case CQE_TX_INVALID_ETH_TYPE: 1391 case CQE_TX_HDR_PROCESSING_ERROR: 1392 case CQE_TX_VF_DISABLED: 1393 case CQE_TX_VPORT_IDX_OUT_OF_RANGE: 1394 case CQE_TX_VPORT_DISABLED: 1395 case CQE_TX_VLAN_TAGGING_VIOLATION: 1396 if (net_ratelimit()) 1397 netdev_err(ndev, "TX: CQE error %d\n", 1398 cqe_oob->cqe_hdr.cqe_type); 1399 1400 apc->eth_stats.tx_cqe_err++; 1401 break; 1402 1403 default: 1404 /* If the CQE type is unknown, log an error, 1405 * and still free the SKB, update tail, etc. 1406 */ 1407 if (net_ratelimit()) 1408 netdev_err(ndev, "TX: unknown CQE type %d\n", 1409 cqe_oob->cqe_hdr.cqe_type); 1410 1411 apc->eth_stats.tx_cqe_unknown_type++; 1412 break; 1413 } 1414 1415 if (WARN_ON_ONCE(txq->gdma_txq_id != completions[i].wq_num)) 1416 return; 1417 1418 skb = skb_dequeue(&txq->pending_skbs); 1419 if (WARN_ON_ONCE(!skb)) 1420 return; 1421 1422 wqe_info = (struct gdma_posted_wqe_info *)skb->cb; 1423 wqe_unit_cnt += wqe_info->wqe_size_in_bu; 1424 1425 mana_unmap_skb(skb, apc); 1426 1427 napi_consume_skb(skb, cq->budget); 1428 1429 pkt_transmitted++; 1430 } 1431 1432 if (WARN_ON_ONCE(wqe_unit_cnt == 0)) 1433 return; 1434 1435 mana_move_wq_tail(txq->gdma_sq, wqe_unit_cnt); 1436 1437 gdma_wq = txq->gdma_sq; 1438 avail_space = mana_gd_wq_avail_space(gdma_wq); 1439 1440 /* Ensure tail updated before checking q stop */ 1441 smp_mb(); 1442 1443 net_txq = txq->net_txq; 1444 txq_stopped = netif_tx_queue_stopped(net_txq); 1445 1446 /* Ensure checking txq_stopped before apc->port_is_up. */ 1447 smp_rmb(); 1448 1449 if (txq_stopped && apc->port_is_up && avail_space >= MAX_TX_WQE_SIZE) { 1450 netif_tx_wake_queue(net_txq); 1451 apc->eth_stats.wake_queue++; 1452 } 1453 1454 if (atomic_sub_return(pkt_transmitted, &txq->pending_sends) < 0) 1455 WARN_ON_ONCE(1); 1456 1457 cq->work_done = pkt_transmitted; 1458 } 1459 1460 static void mana_post_pkt_rxq(struct mana_rxq *rxq) 1461 { 1462 struct mana_recv_buf_oob *recv_buf_oob; 1463 u32 curr_index; 1464 int err; 1465 1466 curr_index = rxq->buf_index++; 1467 if (rxq->buf_index == rxq->num_rx_buf) 1468 rxq->buf_index = 0; 1469 1470 recv_buf_oob = &rxq->rx_oobs[curr_index]; 1471 1472 err = mana_gd_post_work_request(rxq->gdma_rq, &recv_buf_oob->wqe_req, 1473 &recv_buf_oob->wqe_inf); 1474 if (WARN_ON_ONCE(err)) 1475 return; 1476 1477 WARN_ON_ONCE(recv_buf_oob->wqe_inf.wqe_size_in_bu != 1); 1478 } 1479 1480 static struct sk_buff *mana_build_skb(struct mana_rxq *rxq, void *buf_va, 1481 uint pkt_len, struct xdp_buff *xdp) 1482 { 1483 struct sk_buff *skb = napi_build_skb(buf_va, rxq->alloc_size); 1484 1485 if (!skb) 1486 return NULL; 1487 1488 if (xdp->data_hard_start) { 1489 skb_reserve(skb, xdp->data - xdp->data_hard_start); 1490 skb_put(skb, xdp->data_end - xdp->data); 1491 return skb; 1492 } 1493 1494 skb_reserve(skb, rxq->headroom); 1495 skb_put(skb, pkt_len); 1496 1497 return skb; 1498 } 1499 1500 static void mana_rx_skb(void *buf_va, bool from_pool, 1501 struct mana_rxcomp_oob *cqe, struct mana_rxq *rxq) 1502 { 1503 struct mana_stats_rx *rx_stats = &rxq->stats; 1504 struct net_device *ndev = rxq->ndev; 1505 uint pkt_len = cqe->ppi[0].pkt_len; 1506 u16 rxq_idx = rxq->rxq_idx; 1507 struct napi_struct *napi; 1508 struct xdp_buff xdp = {}; 1509 struct sk_buff *skb; 1510 u32 hash_value; 1511 u32 act; 1512 1513 rxq->rx_cq.work_done++; 1514 napi = &rxq->rx_cq.napi; 1515 1516 if (!buf_va) { 1517 ++ndev->stats.rx_dropped; 1518 return; 1519 } 1520 1521 act = mana_run_xdp(ndev, rxq, &xdp, buf_va, pkt_len); 1522 1523 if (act == XDP_REDIRECT && !rxq->xdp_rc) 1524 return; 1525 1526 if (act != XDP_PASS && act != XDP_TX) 1527 goto drop_xdp; 1528 1529 skb = mana_build_skb(rxq, buf_va, pkt_len, &xdp); 1530 1531 if (!skb) 1532 goto drop; 1533 1534 if (from_pool) 1535 skb_mark_for_recycle(skb); 1536 1537 skb->dev = napi->dev; 1538 1539 skb->protocol = eth_type_trans(skb, ndev); 1540 skb_checksum_none_assert(skb); 1541 skb_record_rx_queue(skb, rxq_idx); 1542 1543 if ((ndev->features & NETIF_F_RXCSUM) && cqe->rx_iphdr_csum_succeed) { 1544 if (cqe->rx_tcp_csum_succeed || cqe->rx_udp_csum_succeed) 1545 skb->ip_summed = CHECKSUM_UNNECESSARY; 1546 } 1547 1548 if (cqe->rx_hashtype != 0 && (ndev->features & NETIF_F_RXHASH)) { 1549 hash_value = cqe->ppi[0].pkt_hash; 1550 1551 if (cqe->rx_hashtype & MANA_HASH_L4) 1552 skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L4); 1553 else 1554 skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L3); 1555 } 1556 1557 if (cqe->rx_vlantag_present) { 1558 u16 vlan_tci = cqe->rx_vlan_id; 1559 1560 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci); 1561 } 1562 1563 u64_stats_update_begin(&rx_stats->syncp); 1564 rx_stats->packets++; 1565 rx_stats->bytes += pkt_len; 1566 1567 if (act == XDP_TX) 1568 rx_stats->xdp_tx++; 1569 u64_stats_update_end(&rx_stats->syncp); 1570 1571 if (act == XDP_TX) { 1572 skb_set_queue_mapping(skb, rxq_idx); 1573 mana_xdp_tx(skb, ndev); 1574 return; 1575 } 1576 1577 napi_gro_receive(napi, skb); 1578 1579 return; 1580 1581 drop_xdp: 1582 u64_stats_update_begin(&rx_stats->syncp); 1583 rx_stats->xdp_drop++; 1584 u64_stats_update_end(&rx_stats->syncp); 1585 1586 drop: 1587 if (from_pool) { 1588 page_pool_recycle_direct(rxq->page_pool, 1589 virt_to_head_page(buf_va)); 1590 } else { 1591 WARN_ON_ONCE(rxq->xdp_save_va); 1592 /* Save for reuse */ 1593 rxq->xdp_save_va = buf_va; 1594 } 1595 1596 ++ndev->stats.rx_dropped; 1597 1598 return; 1599 } 1600 1601 static void *mana_get_rxfrag(struct mana_rxq *rxq, struct device *dev, 1602 dma_addr_t *da, bool *from_pool, bool is_napi) 1603 { 1604 struct page *page; 1605 void *va; 1606 1607 *from_pool = false; 1608 1609 /* Reuse XDP dropped page if available */ 1610 if (rxq->xdp_save_va) { 1611 va = rxq->xdp_save_va; 1612 rxq->xdp_save_va = NULL; 1613 } else if (rxq->alloc_size > PAGE_SIZE) { 1614 if (is_napi) 1615 va = napi_alloc_frag(rxq->alloc_size); 1616 else 1617 va = netdev_alloc_frag(rxq->alloc_size); 1618 1619 if (!va) 1620 return NULL; 1621 1622 page = virt_to_head_page(va); 1623 /* Check if the frag falls back to single page */ 1624 if (compound_order(page) < get_order(rxq->alloc_size)) { 1625 put_page(page); 1626 return NULL; 1627 } 1628 } else { 1629 page = page_pool_dev_alloc_pages(rxq->page_pool); 1630 if (!page) 1631 return NULL; 1632 1633 *from_pool = true; 1634 va = page_to_virt(page); 1635 } 1636 1637 *da = dma_map_single(dev, va + rxq->headroom, rxq->datasize, 1638 DMA_FROM_DEVICE); 1639 if (dma_mapping_error(dev, *da)) { 1640 if (*from_pool) 1641 page_pool_put_full_page(rxq->page_pool, page, false); 1642 else 1643 put_page(virt_to_head_page(va)); 1644 1645 return NULL; 1646 } 1647 1648 return va; 1649 } 1650 1651 /* Allocate frag for rx buffer, and save the old buf */ 1652 static void mana_refill_rx_oob(struct device *dev, struct mana_rxq *rxq, 1653 struct mana_recv_buf_oob *rxoob, void **old_buf, 1654 bool *old_fp) 1655 { 1656 bool from_pool; 1657 dma_addr_t da; 1658 void *va; 1659 1660 va = mana_get_rxfrag(rxq, dev, &da, &from_pool, true); 1661 if (!va) 1662 return; 1663 1664 dma_unmap_single(dev, rxoob->sgl[0].address, rxq->datasize, 1665 DMA_FROM_DEVICE); 1666 *old_buf = rxoob->buf_va; 1667 *old_fp = rxoob->from_pool; 1668 1669 rxoob->buf_va = va; 1670 rxoob->sgl[0].address = da; 1671 rxoob->from_pool = from_pool; 1672 } 1673 1674 static void mana_process_rx_cqe(struct mana_rxq *rxq, struct mana_cq *cq, 1675 struct gdma_comp *cqe) 1676 { 1677 struct mana_rxcomp_oob *oob = (struct mana_rxcomp_oob *)cqe->cqe_data; 1678 struct gdma_context *gc = rxq->gdma_rq->gdma_dev->gdma_context; 1679 struct net_device *ndev = rxq->ndev; 1680 struct mana_recv_buf_oob *rxbuf_oob; 1681 struct mana_port_context *apc; 1682 struct device *dev = gc->dev; 1683 void *old_buf = NULL; 1684 u32 curr, pktlen; 1685 bool old_fp; 1686 1687 apc = netdev_priv(ndev); 1688 1689 switch (oob->cqe_hdr.cqe_type) { 1690 case CQE_RX_OKAY: 1691 break; 1692 1693 case CQE_RX_TRUNCATED: 1694 ++ndev->stats.rx_dropped; 1695 rxbuf_oob = &rxq->rx_oobs[rxq->buf_index]; 1696 netdev_warn_once(ndev, "Dropped a truncated packet\n"); 1697 goto drop; 1698 1699 case CQE_RX_COALESCED_4: 1700 netdev_err(ndev, "RX coalescing is unsupported\n"); 1701 apc->eth_stats.rx_coalesced_err++; 1702 return; 1703 1704 case CQE_RX_OBJECT_FENCE: 1705 complete(&rxq->fence_event); 1706 return; 1707 1708 default: 1709 netdev_err(ndev, "Unknown RX CQE type = %d\n", 1710 oob->cqe_hdr.cqe_type); 1711 apc->eth_stats.rx_cqe_unknown_type++; 1712 return; 1713 } 1714 1715 pktlen = oob->ppi[0].pkt_len; 1716 1717 if (pktlen == 0) { 1718 /* data packets should never have packetlength of zero */ 1719 netdev_err(ndev, "RX pkt len=0, rq=%u, cq=%u, rxobj=0x%llx\n", 1720 rxq->gdma_id, cq->gdma_id, rxq->rxobj); 1721 return; 1722 } 1723 1724 curr = rxq->buf_index; 1725 rxbuf_oob = &rxq->rx_oobs[curr]; 1726 WARN_ON_ONCE(rxbuf_oob->wqe_inf.wqe_size_in_bu != 1); 1727 1728 mana_refill_rx_oob(dev, rxq, rxbuf_oob, &old_buf, &old_fp); 1729 1730 /* Unsuccessful refill will have old_buf == NULL. 1731 * In this case, mana_rx_skb() will drop the packet. 1732 */ 1733 mana_rx_skb(old_buf, old_fp, oob, rxq); 1734 1735 drop: 1736 mana_move_wq_tail(rxq->gdma_rq, rxbuf_oob->wqe_inf.wqe_size_in_bu); 1737 1738 mana_post_pkt_rxq(rxq); 1739 } 1740 1741 static void mana_poll_rx_cq(struct mana_cq *cq) 1742 { 1743 struct gdma_comp *comp = cq->gdma_comp_buf; 1744 struct mana_rxq *rxq = cq->rxq; 1745 int comp_read, i; 1746 1747 comp_read = mana_gd_poll_cq(cq->gdma_cq, comp, CQE_POLLING_BUFFER); 1748 WARN_ON_ONCE(comp_read > CQE_POLLING_BUFFER); 1749 1750 rxq->xdp_flush = false; 1751 1752 for (i = 0; i < comp_read; i++) { 1753 if (WARN_ON_ONCE(comp[i].is_sq)) 1754 return; 1755 1756 /* verify recv cqe references the right rxq */ 1757 if (WARN_ON_ONCE(comp[i].wq_num != cq->rxq->gdma_id)) 1758 return; 1759 1760 mana_process_rx_cqe(rxq, cq, &comp[i]); 1761 } 1762 1763 if (comp_read > 0) { 1764 struct gdma_context *gc = rxq->gdma_rq->gdma_dev->gdma_context; 1765 1766 mana_gd_wq_ring_doorbell(gc, rxq->gdma_rq); 1767 } 1768 1769 if (rxq->xdp_flush) 1770 xdp_do_flush(); 1771 } 1772 1773 static int mana_cq_handler(void *context, struct gdma_queue *gdma_queue) 1774 { 1775 struct mana_cq *cq = context; 1776 u8 arm_bit; 1777 int w; 1778 1779 WARN_ON_ONCE(cq->gdma_cq != gdma_queue); 1780 1781 if (cq->type == MANA_CQ_TYPE_RX) 1782 mana_poll_rx_cq(cq); 1783 else 1784 mana_poll_tx_cq(cq); 1785 1786 w = cq->work_done; 1787 1788 if (w < cq->budget && 1789 napi_complete_done(&cq->napi, w)) { 1790 arm_bit = SET_ARM_BIT; 1791 } else { 1792 arm_bit = 0; 1793 } 1794 1795 mana_gd_ring_cq(gdma_queue, arm_bit); 1796 1797 return w; 1798 } 1799 1800 static int mana_poll(struct napi_struct *napi, int budget) 1801 { 1802 struct mana_cq *cq = container_of(napi, struct mana_cq, napi); 1803 int w; 1804 1805 cq->work_done = 0; 1806 cq->budget = budget; 1807 1808 w = mana_cq_handler(cq, cq->gdma_cq); 1809 1810 return min(w, budget); 1811 } 1812 1813 static void mana_schedule_napi(void *context, struct gdma_queue *gdma_queue) 1814 { 1815 struct mana_cq *cq = context; 1816 1817 napi_schedule_irqoff(&cq->napi); 1818 } 1819 1820 static void mana_deinit_cq(struct mana_port_context *apc, struct mana_cq *cq) 1821 { 1822 struct gdma_dev *gd = apc->ac->gdma_dev; 1823 1824 if (!cq->gdma_cq) 1825 return; 1826 1827 mana_gd_destroy_queue(gd->gdma_context, cq->gdma_cq); 1828 } 1829 1830 static void mana_deinit_txq(struct mana_port_context *apc, struct mana_txq *txq) 1831 { 1832 struct gdma_dev *gd = apc->ac->gdma_dev; 1833 1834 if (!txq->gdma_sq) 1835 return; 1836 1837 mana_gd_destroy_queue(gd->gdma_context, txq->gdma_sq); 1838 } 1839 1840 static void mana_destroy_txq(struct mana_port_context *apc) 1841 { 1842 struct napi_struct *napi; 1843 int i; 1844 1845 if (!apc->tx_qp) 1846 return; 1847 1848 for (i = 0; i < apc->num_queues; i++) { 1849 napi = &apc->tx_qp[i].tx_cq.napi; 1850 napi_synchronize(napi); 1851 napi_disable(napi); 1852 netif_napi_del(napi); 1853 1854 mana_destroy_wq_obj(apc, GDMA_SQ, apc->tx_qp[i].tx_object); 1855 1856 mana_deinit_cq(apc, &apc->tx_qp[i].tx_cq); 1857 1858 mana_deinit_txq(apc, &apc->tx_qp[i].txq); 1859 } 1860 1861 kfree(apc->tx_qp); 1862 apc->tx_qp = NULL; 1863 } 1864 1865 static int mana_create_txq(struct mana_port_context *apc, 1866 struct net_device *net) 1867 { 1868 struct mana_context *ac = apc->ac; 1869 struct gdma_dev *gd = ac->gdma_dev; 1870 struct mana_obj_spec wq_spec; 1871 struct mana_obj_spec cq_spec; 1872 struct gdma_queue_spec spec; 1873 struct gdma_context *gc; 1874 struct mana_txq *txq; 1875 struct mana_cq *cq; 1876 u32 txq_size; 1877 u32 cq_size; 1878 int err; 1879 int i; 1880 1881 apc->tx_qp = kcalloc(apc->num_queues, sizeof(struct mana_tx_qp), 1882 GFP_KERNEL); 1883 if (!apc->tx_qp) 1884 return -ENOMEM; 1885 1886 /* The minimum size of the WQE is 32 bytes, hence 1887 * MAX_SEND_BUFFERS_PER_QUEUE represents the maximum number of WQEs 1888 * the SQ can store. This value is then used to size other queues 1889 * to prevent overflow. 1890 */ 1891 txq_size = MAX_SEND_BUFFERS_PER_QUEUE * 32; 1892 BUILD_BUG_ON(!PAGE_ALIGNED(txq_size)); 1893 1894 cq_size = MAX_SEND_BUFFERS_PER_QUEUE * COMP_ENTRY_SIZE; 1895 cq_size = PAGE_ALIGN(cq_size); 1896 1897 gc = gd->gdma_context; 1898 1899 for (i = 0; i < apc->num_queues; i++) { 1900 apc->tx_qp[i].tx_object = INVALID_MANA_HANDLE; 1901 1902 /* Create SQ */ 1903 txq = &apc->tx_qp[i].txq; 1904 1905 u64_stats_init(&txq->stats.syncp); 1906 txq->ndev = net; 1907 txq->net_txq = netdev_get_tx_queue(net, i); 1908 txq->vp_offset = apc->tx_vp_offset; 1909 skb_queue_head_init(&txq->pending_skbs); 1910 1911 memset(&spec, 0, sizeof(spec)); 1912 spec.type = GDMA_SQ; 1913 spec.monitor_avl_buf = true; 1914 spec.queue_size = txq_size; 1915 err = mana_gd_create_mana_wq_cq(gd, &spec, &txq->gdma_sq); 1916 if (err) 1917 goto out; 1918 1919 /* Create SQ's CQ */ 1920 cq = &apc->tx_qp[i].tx_cq; 1921 cq->type = MANA_CQ_TYPE_TX; 1922 1923 cq->txq = txq; 1924 1925 memset(&spec, 0, sizeof(spec)); 1926 spec.type = GDMA_CQ; 1927 spec.monitor_avl_buf = false; 1928 spec.queue_size = cq_size; 1929 spec.cq.callback = mana_schedule_napi; 1930 spec.cq.parent_eq = ac->eqs[i].eq; 1931 spec.cq.context = cq; 1932 err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq); 1933 if (err) 1934 goto out; 1935 1936 memset(&wq_spec, 0, sizeof(wq_spec)); 1937 memset(&cq_spec, 0, sizeof(cq_spec)); 1938 1939 wq_spec.gdma_region = txq->gdma_sq->mem_info.dma_region_handle; 1940 wq_spec.queue_size = txq->gdma_sq->queue_size; 1941 1942 cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle; 1943 cq_spec.queue_size = cq->gdma_cq->queue_size; 1944 cq_spec.modr_ctx_id = 0; 1945 cq_spec.attached_eq = cq->gdma_cq->cq.parent->id; 1946 1947 err = mana_create_wq_obj(apc, apc->port_handle, GDMA_SQ, 1948 &wq_spec, &cq_spec, 1949 &apc->tx_qp[i].tx_object); 1950 1951 if (err) 1952 goto out; 1953 1954 txq->gdma_sq->id = wq_spec.queue_index; 1955 cq->gdma_cq->id = cq_spec.queue_index; 1956 1957 txq->gdma_sq->mem_info.dma_region_handle = 1958 GDMA_INVALID_DMA_REGION; 1959 cq->gdma_cq->mem_info.dma_region_handle = 1960 GDMA_INVALID_DMA_REGION; 1961 1962 txq->gdma_txq_id = txq->gdma_sq->id; 1963 1964 cq->gdma_id = cq->gdma_cq->id; 1965 1966 if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) { 1967 err = -EINVAL; 1968 goto out; 1969 } 1970 1971 gc->cq_table[cq->gdma_id] = cq->gdma_cq; 1972 1973 netif_napi_add_tx(net, &cq->napi, mana_poll); 1974 napi_enable(&cq->napi); 1975 1976 mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT); 1977 } 1978 1979 return 0; 1980 out: 1981 mana_destroy_txq(apc); 1982 return err; 1983 } 1984 1985 static void mana_destroy_rxq(struct mana_port_context *apc, 1986 struct mana_rxq *rxq, bool validate_state) 1987 1988 { 1989 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context; 1990 struct mana_recv_buf_oob *rx_oob; 1991 struct device *dev = gc->dev; 1992 struct napi_struct *napi; 1993 struct page *page; 1994 int i; 1995 1996 if (!rxq) 1997 return; 1998 1999 napi = &rxq->rx_cq.napi; 2000 2001 if (validate_state) 2002 napi_synchronize(napi); 2003 2004 napi_disable(napi); 2005 2006 xdp_rxq_info_unreg(&rxq->xdp_rxq); 2007 2008 netif_napi_del(napi); 2009 2010 mana_destroy_wq_obj(apc, GDMA_RQ, rxq->rxobj); 2011 2012 mana_deinit_cq(apc, &rxq->rx_cq); 2013 2014 if (rxq->xdp_save_va) 2015 put_page(virt_to_head_page(rxq->xdp_save_va)); 2016 2017 for (i = 0; i < rxq->num_rx_buf; i++) { 2018 rx_oob = &rxq->rx_oobs[i]; 2019 2020 if (!rx_oob->buf_va) 2021 continue; 2022 2023 dma_unmap_single(dev, rx_oob->sgl[0].address, 2024 rx_oob->sgl[0].size, DMA_FROM_DEVICE); 2025 2026 page = virt_to_head_page(rx_oob->buf_va); 2027 2028 if (rx_oob->from_pool) 2029 page_pool_put_full_page(rxq->page_pool, page, false); 2030 else 2031 put_page(page); 2032 2033 rx_oob->buf_va = NULL; 2034 } 2035 2036 page_pool_destroy(rxq->page_pool); 2037 2038 if (rxq->gdma_rq) 2039 mana_gd_destroy_queue(gc, rxq->gdma_rq); 2040 2041 kfree(rxq); 2042 } 2043 2044 static int mana_fill_rx_oob(struct mana_recv_buf_oob *rx_oob, u32 mem_key, 2045 struct mana_rxq *rxq, struct device *dev) 2046 { 2047 struct mana_port_context *mpc = netdev_priv(rxq->ndev); 2048 bool from_pool = false; 2049 dma_addr_t da; 2050 void *va; 2051 2052 if (mpc->rxbufs_pre) 2053 va = mana_get_rxbuf_pre(rxq, &da); 2054 else 2055 va = mana_get_rxfrag(rxq, dev, &da, &from_pool, false); 2056 2057 if (!va) 2058 return -ENOMEM; 2059 2060 rx_oob->buf_va = va; 2061 rx_oob->from_pool = from_pool; 2062 2063 rx_oob->sgl[0].address = da; 2064 rx_oob->sgl[0].size = rxq->datasize; 2065 rx_oob->sgl[0].mem_key = mem_key; 2066 2067 return 0; 2068 } 2069 2070 #define MANA_WQE_HEADER_SIZE 16 2071 #define MANA_WQE_SGE_SIZE 16 2072 2073 static int mana_alloc_rx_wqe(struct mana_port_context *apc, 2074 struct mana_rxq *rxq, u32 *rxq_size, u32 *cq_size) 2075 { 2076 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context; 2077 struct mana_recv_buf_oob *rx_oob; 2078 struct device *dev = gc->dev; 2079 u32 buf_idx; 2080 int ret; 2081 2082 WARN_ON(rxq->datasize == 0); 2083 2084 *rxq_size = 0; 2085 *cq_size = 0; 2086 2087 for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) { 2088 rx_oob = &rxq->rx_oobs[buf_idx]; 2089 memset(rx_oob, 0, sizeof(*rx_oob)); 2090 2091 rx_oob->num_sge = 1; 2092 2093 ret = mana_fill_rx_oob(rx_oob, apc->ac->gdma_dev->gpa_mkey, rxq, 2094 dev); 2095 if (ret) 2096 return ret; 2097 2098 rx_oob->wqe_req.sgl = rx_oob->sgl; 2099 rx_oob->wqe_req.num_sge = rx_oob->num_sge; 2100 rx_oob->wqe_req.inline_oob_size = 0; 2101 rx_oob->wqe_req.inline_oob_data = NULL; 2102 rx_oob->wqe_req.flags = 0; 2103 rx_oob->wqe_req.client_data_unit = 0; 2104 2105 *rxq_size += ALIGN(MANA_WQE_HEADER_SIZE + 2106 MANA_WQE_SGE_SIZE * rx_oob->num_sge, 32); 2107 *cq_size += COMP_ENTRY_SIZE; 2108 } 2109 2110 return 0; 2111 } 2112 2113 static int mana_push_wqe(struct mana_rxq *rxq) 2114 { 2115 struct mana_recv_buf_oob *rx_oob; 2116 u32 buf_idx; 2117 int err; 2118 2119 for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) { 2120 rx_oob = &rxq->rx_oobs[buf_idx]; 2121 2122 err = mana_gd_post_and_ring(rxq->gdma_rq, &rx_oob->wqe_req, 2123 &rx_oob->wqe_inf); 2124 if (err) 2125 return -ENOSPC; 2126 } 2127 2128 return 0; 2129 } 2130 2131 static int mana_create_page_pool(struct mana_rxq *rxq, struct gdma_context *gc) 2132 { 2133 struct page_pool_params pprm = {}; 2134 int ret; 2135 2136 pprm.pool_size = RX_BUFFERS_PER_QUEUE; 2137 pprm.nid = gc->numa_node; 2138 pprm.napi = &rxq->rx_cq.napi; 2139 pprm.netdev = rxq->ndev; 2140 2141 rxq->page_pool = page_pool_create(&pprm); 2142 2143 if (IS_ERR(rxq->page_pool)) { 2144 ret = PTR_ERR(rxq->page_pool); 2145 rxq->page_pool = NULL; 2146 return ret; 2147 } 2148 2149 return 0; 2150 } 2151 2152 static struct mana_rxq *mana_create_rxq(struct mana_port_context *apc, 2153 u32 rxq_idx, struct mana_eq *eq, 2154 struct net_device *ndev) 2155 { 2156 struct gdma_dev *gd = apc->ac->gdma_dev; 2157 struct mana_obj_spec wq_spec; 2158 struct mana_obj_spec cq_spec; 2159 struct gdma_queue_spec spec; 2160 struct mana_cq *cq = NULL; 2161 struct gdma_context *gc; 2162 u32 cq_size, rq_size; 2163 struct mana_rxq *rxq; 2164 int err; 2165 2166 gc = gd->gdma_context; 2167 2168 rxq = kzalloc(struct_size(rxq, rx_oobs, RX_BUFFERS_PER_QUEUE), 2169 GFP_KERNEL); 2170 if (!rxq) 2171 return NULL; 2172 2173 rxq->ndev = ndev; 2174 rxq->num_rx_buf = RX_BUFFERS_PER_QUEUE; 2175 rxq->rxq_idx = rxq_idx; 2176 rxq->rxobj = INVALID_MANA_HANDLE; 2177 2178 mana_get_rxbuf_cfg(ndev->mtu, &rxq->datasize, &rxq->alloc_size, 2179 &rxq->headroom); 2180 2181 /* Create page pool for RX queue */ 2182 err = mana_create_page_pool(rxq, gc); 2183 if (err) { 2184 netdev_err(ndev, "Create page pool err:%d\n", err); 2185 goto out; 2186 } 2187 2188 err = mana_alloc_rx_wqe(apc, rxq, &rq_size, &cq_size); 2189 if (err) 2190 goto out; 2191 2192 rq_size = PAGE_ALIGN(rq_size); 2193 cq_size = PAGE_ALIGN(cq_size); 2194 2195 /* Create RQ */ 2196 memset(&spec, 0, sizeof(spec)); 2197 spec.type = GDMA_RQ; 2198 spec.monitor_avl_buf = true; 2199 spec.queue_size = rq_size; 2200 err = mana_gd_create_mana_wq_cq(gd, &spec, &rxq->gdma_rq); 2201 if (err) 2202 goto out; 2203 2204 /* Create RQ's CQ */ 2205 cq = &rxq->rx_cq; 2206 cq->type = MANA_CQ_TYPE_RX; 2207 cq->rxq = rxq; 2208 2209 memset(&spec, 0, sizeof(spec)); 2210 spec.type = GDMA_CQ; 2211 spec.monitor_avl_buf = false; 2212 spec.queue_size = cq_size; 2213 spec.cq.callback = mana_schedule_napi; 2214 spec.cq.parent_eq = eq->eq; 2215 spec.cq.context = cq; 2216 err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq); 2217 if (err) 2218 goto out; 2219 2220 memset(&wq_spec, 0, sizeof(wq_spec)); 2221 memset(&cq_spec, 0, sizeof(cq_spec)); 2222 wq_spec.gdma_region = rxq->gdma_rq->mem_info.dma_region_handle; 2223 wq_spec.queue_size = rxq->gdma_rq->queue_size; 2224 2225 cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle; 2226 cq_spec.queue_size = cq->gdma_cq->queue_size; 2227 cq_spec.modr_ctx_id = 0; 2228 cq_spec.attached_eq = cq->gdma_cq->cq.parent->id; 2229 2230 err = mana_create_wq_obj(apc, apc->port_handle, GDMA_RQ, 2231 &wq_spec, &cq_spec, &rxq->rxobj); 2232 if (err) 2233 goto out; 2234 2235 rxq->gdma_rq->id = wq_spec.queue_index; 2236 cq->gdma_cq->id = cq_spec.queue_index; 2237 2238 rxq->gdma_rq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION; 2239 cq->gdma_cq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION; 2240 2241 rxq->gdma_id = rxq->gdma_rq->id; 2242 cq->gdma_id = cq->gdma_cq->id; 2243 2244 err = mana_push_wqe(rxq); 2245 if (err) 2246 goto out; 2247 2248 if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) { 2249 err = -EINVAL; 2250 goto out; 2251 } 2252 2253 gc->cq_table[cq->gdma_id] = cq->gdma_cq; 2254 2255 netif_napi_add_weight(ndev, &cq->napi, mana_poll, 1); 2256 2257 WARN_ON(xdp_rxq_info_reg(&rxq->xdp_rxq, ndev, rxq_idx, 2258 cq->napi.napi_id)); 2259 WARN_ON(xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL, 2260 rxq->page_pool)); 2261 2262 napi_enable(&cq->napi); 2263 2264 mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT); 2265 out: 2266 if (!err) 2267 return rxq; 2268 2269 netdev_err(ndev, "Failed to create RXQ: err = %d\n", err); 2270 2271 mana_destroy_rxq(apc, rxq, false); 2272 2273 if (cq) 2274 mana_deinit_cq(apc, cq); 2275 2276 return NULL; 2277 } 2278 2279 static int mana_add_rx_queues(struct mana_port_context *apc, 2280 struct net_device *ndev) 2281 { 2282 struct mana_context *ac = apc->ac; 2283 struct mana_rxq *rxq; 2284 int err = 0; 2285 int i; 2286 2287 for (i = 0; i < apc->num_queues; i++) { 2288 rxq = mana_create_rxq(apc, i, &ac->eqs[i], ndev); 2289 if (!rxq) { 2290 err = -ENOMEM; 2291 goto out; 2292 } 2293 2294 u64_stats_init(&rxq->stats.syncp); 2295 2296 apc->rxqs[i] = rxq; 2297 } 2298 2299 apc->default_rxobj = apc->rxqs[0]->rxobj; 2300 out: 2301 return err; 2302 } 2303 2304 static void mana_destroy_vport(struct mana_port_context *apc) 2305 { 2306 struct gdma_dev *gd = apc->ac->gdma_dev; 2307 struct mana_rxq *rxq; 2308 u32 rxq_idx; 2309 2310 for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) { 2311 rxq = apc->rxqs[rxq_idx]; 2312 if (!rxq) 2313 continue; 2314 2315 mana_destroy_rxq(apc, rxq, true); 2316 apc->rxqs[rxq_idx] = NULL; 2317 } 2318 2319 mana_destroy_txq(apc); 2320 mana_uncfg_vport(apc); 2321 2322 if (gd->gdma_context->is_pf) 2323 mana_pf_deregister_hw_vport(apc); 2324 } 2325 2326 static int mana_create_vport(struct mana_port_context *apc, 2327 struct net_device *net) 2328 { 2329 struct gdma_dev *gd = apc->ac->gdma_dev; 2330 int err; 2331 2332 apc->default_rxobj = INVALID_MANA_HANDLE; 2333 2334 if (gd->gdma_context->is_pf) { 2335 err = mana_pf_register_hw_vport(apc); 2336 if (err) 2337 return err; 2338 } 2339 2340 err = mana_cfg_vport(apc, gd->pdid, gd->doorbell); 2341 if (err) 2342 return err; 2343 2344 return mana_create_txq(apc, net); 2345 } 2346 2347 static void mana_rss_table_init(struct mana_port_context *apc) 2348 { 2349 int i; 2350 2351 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) 2352 apc->indir_table[i] = 2353 ethtool_rxfh_indir_default(i, apc->num_queues); 2354 } 2355 2356 int mana_config_rss(struct mana_port_context *apc, enum TRI_STATE rx, 2357 bool update_hash, bool update_tab) 2358 { 2359 u32 queue_idx; 2360 int err; 2361 int i; 2362 2363 if (update_tab) { 2364 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) { 2365 queue_idx = apc->indir_table[i]; 2366 apc->rxobj_table[i] = apc->rxqs[queue_idx]->rxobj; 2367 } 2368 } 2369 2370 err = mana_cfg_vport_steering(apc, rx, true, update_hash, update_tab); 2371 if (err) 2372 return err; 2373 2374 mana_fence_rqs(apc); 2375 2376 return 0; 2377 } 2378 2379 void mana_query_gf_stats(struct mana_port_context *apc) 2380 { 2381 struct mana_query_gf_stat_resp resp = {}; 2382 struct mana_query_gf_stat_req req = {}; 2383 struct net_device *ndev = apc->ndev; 2384 int err; 2385 2386 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_GF_STAT, 2387 sizeof(req), sizeof(resp)); 2388 req.req_stats = STATISTICS_FLAGS_RX_DISCARDS_NO_WQE | 2389 STATISTICS_FLAGS_RX_ERRORS_VPORT_DISABLED | 2390 STATISTICS_FLAGS_HC_RX_BYTES | 2391 STATISTICS_FLAGS_HC_RX_UCAST_PACKETS | 2392 STATISTICS_FLAGS_HC_RX_UCAST_BYTES | 2393 STATISTICS_FLAGS_HC_RX_MCAST_PACKETS | 2394 STATISTICS_FLAGS_HC_RX_MCAST_BYTES | 2395 STATISTICS_FLAGS_HC_RX_BCAST_PACKETS | 2396 STATISTICS_FLAGS_HC_RX_BCAST_BYTES | 2397 STATISTICS_FLAGS_TX_ERRORS_GF_DISABLED | 2398 STATISTICS_FLAGS_TX_ERRORS_VPORT_DISABLED | 2399 STATISTICS_FLAGS_TX_ERRORS_INVAL_VPORT_OFFSET_PACKETS | 2400 STATISTICS_FLAGS_TX_ERRORS_VLAN_ENFORCEMENT | 2401 STATISTICS_FLAGS_TX_ERRORS_ETH_TYPE_ENFORCEMENT | 2402 STATISTICS_FLAGS_TX_ERRORS_SA_ENFORCEMENT | 2403 STATISTICS_FLAGS_TX_ERRORS_SQPDID_ENFORCEMENT | 2404 STATISTICS_FLAGS_TX_ERRORS_CQPDID_ENFORCEMENT | 2405 STATISTICS_FLAGS_TX_ERRORS_MTU_VIOLATION | 2406 STATISTICS_FLAGS_TX_ERRORS_INVALID_OOB | 2407 STATISTICS_FLAGS_HC_TX_BYTES | 2408 STATISTICS_FLAGS_HC_TX_UCAST_PACKETS | 2409 STATISTICS_FLAGS_HC_TX_UCAST_BYTES | 2410 STATISTICS_FLAGS_HC_TX_MCAST_PACKETS | 2411 STATISTICS_FLAGS_HC_TX_MCAST_BYTES | 2412 STATISTICS_FLAGS_HC_TX_BCAST_PACKETS | 2413 STATISTICS_FLAGS_HC_TX_BCAST_BYTES | 2414 STATISTICS_FLAGS_TX_ERRORS_GDMA_ERROR; 2415 2416 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 2417 sizeof(resp)); 2418 if (err) { 2419 netdev_err(ndev, "Failed to query GF stats: %d\n", err); 2420 return; 2421 } 2422 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_GF_STAT, 2423 sizeof(resp)); 2424 if (err || resp.hdr.status) { 2425 netdev_err(ndev, "Failed to query GF stats: %d, 0x%x\n", err, 2426 resp.hdr.status); 2427 return; 2428 } 2429 2430 apc->eth_stats.hc_rx_discards_no_wqe = resp.rx_discards_nowqe; 2431 apc->eth_stats.hc_rx_err_vport_disabled = resp.rx_err_vport_disabled; 2432 apc->eth_stats.hc_rx_bytes = resp.hc_rx_bytes; 2433 apc->eth_stats.hc_rx_ucast_pkts = resp.hc_rx_ucast_pkts; 2434 apc->eth_stats.hc_rx_ucast_bytes = resp.hc_rx_ucast_bytes; 2435 apc->eth_stats.hc_rx_bcast_pkts = resp.hc_rx_bcast_pkts; 2436 apc->eth_stats.hc_rx_bcast_bytes = resp.hc_rx_bcast_bytes; 2437 apc->eth_stats.hc_rx_mcast_pkts = resp.hc_rx_mcast_pkts; 2438 apc->eth_stats.hc_rx_mcast_bytes = resp.hc_rx_mcast_bytes; 2439 apc->eth_stats.hc_tx_err_gf_disabled = resp.tx_err_gf_disabled; 2440 apc->eth_stats.hc_tx_err_vport_disabled = resp.tx_err_vport_disabled; 2441 apc->eth_stats.hc_tx_err_inval_vportoffset_pkt = 2442 resp.tx_err_inval_vport_offset_pkt; 2443 apc->eth_stats.hc_tx_err_vlan_enforcement = 2444 resp.tx_err_vlan_enforcement; 2445 apc->eth_stats.hc_tx_err_eth_type_enforcement = 2446 resp.tx_err_ethtype_enforcement; 2447 apc->eth_stats.hc_tx_err_sa_enforcement = resp.tx_err_SA_enforcement; 2448 apc->eth_stats.hc_tx_err_sqpdid_enforcement = 2449 resp.tx_err_SQPDID_enforcement; 2450 apc->eth_stats.hc_tx_err_cqpdid_enforcement = 2451 resp.tx_err_CQPDID_enforcement; 2452 apc->eth_stats.hc_tx_err_mtu_violation = resp.tx_err_mtu_violation; 2453 apc->eth_stats.hc_tx_err_inval_oob = resp.tx_err_inval_oob; 2454 apc->eth_stats.hc_tx_bytes = resp.hc_tx_bytes; 2455 apc->eth_stats.hc_tx_ucast_pkts = resp.hc_tx_ucast_pkts; 2456 apc->eth_stats.hc_tx_ucast_bytes = resp.hc_tx_ucast_bytes; 2457 apc->eth_stats.hc_tx_bcast_pkts = resp.hc_tx_bcast_pkts; 2458 apc->eth_stats.hc_tx_bcast_bytes = resp.hc_tx_bcast_bytes; 2459 apc->eth_stats.hc_tx_mcast_pkts = resp.hc_tx_mcast_pkts; 2460 apc->eth_stats.hc_tx_mcast_bytes = resp.hc_tx_mcast_bytes; 2461 apc->eth_stats.hc_tx_err_gdma = resp.tx_err_gdma; 2462 } 2463 2464 static int mana_init_port(struct net_device *ndev) 2465 { 2466 struct mana_port_context *apc = netdev_priv(ndev); 2467 u32 max_txq, max_rxq, max_queues; 2468 int port_idx = apc->port_idx; 2469 u32 num_indirect_entries; 2470 int err; 2471 2472 err = mana_init_port_context(apc); 2473 if (err) 2474 return err; 2475 2476 err = mana_query_vport_cfg(apc, port_idx, &max_txq, &max_rxq, 2477 &num_indirect_entries); 2478 if (err) { 2479 netdev_err(ndev, "Failed to query info for vPort %d\n", 2480 port_idx); 2481 goto reset_apc; 2482 } 2483 2484 max_queues = min_t(u32, max_txq, max_rxq); 2485 if (apc->max_queues > max_queues) 2486 apc->max_queues = max_queues; 2487 2488 if (apc->num_queues > apc->max_queues) 2489 apc->num_queues = apc->max_queues; 2490 2491 eth_hw_addr_set(ndev, apc->mac_addr); 2492 2493 return 0; 2494 2495 reset_apc: 2496 kfree(apc->rxqs); 2497 apc->rxqs = NULL; 2498 return err; 2499 } 2500 2501 int mana_alloc_queues(struct net_device *ndev) 2502 { 2503 struct mana_port_context *apc = netdev_priv(ndev); 2504 struct gdma_dev *gd = apc->ac->gdma_dev; 2505 int err; 2506 2507 err = mana_create_vport(apc, ndev); 2508 if (err) 2509 return err; 2510 2511 err = netif_set_real_num_tx_queues(ndev, apc->num_queues); 2512 if (err) 2513 goto destroy_vport; 2514 2515 err = mana_add_rx_queues(apc, ndev); 2516 if (err) 2517 goto destroy_vport; 2518 2519 apc->rss_state = apc->num_queues > 1 ? TRI_STATE_TRUE : TRI_STATE_FALSE; 2520 2521 err = netif_set_real_num_rx_queues(ndev, apc->num_queues); 2522 if (err) 2523 goto destroy_vport; 2524 2525 mana_rss_table_init(apc); 2526 2527 err = mana_config_rss(apc, TRI_STATE_TRUE, true, true); 2528 if (err) 2529 goto destroy_vport; 2530 2531 if (gd->gdma_context->is_pf) { 2532 err = mana_pf_register_filter(apc); 2533 if (err) 2534 goto destroy_vport; 2535 } 2536 2537 mana_chn_setxdp(apc, mana_xdp_get(apc)); 2538 2539 return 0; 2540 2541 destroy_vport: 2542 mana_destroy_vport(apc); 2543 return err; 2544 } 2545 2546 int mana_attach(struct net_device *ndev) 2547 { 2548 struct mana_port_context *apc = netdev_priv(ndev); 2549 int err; 2550 2551 ASSERT_RTNL(); 2552 2553 err = mana_init_port(ndev); 2554 if (err) 2555 return err; 2556 2557 if (apc->port_st_save) { 2558 err = mana_alloc_queues(ndev); 2559 if (err) { 2560 mana_cleanup_port_context(apc); 2561 return err; 2562 } 2563 } 2564 2565 apc->port_is_up = apc->port_st_save; 2566 2567 /* Ensure port state updated before txq state */ 2568 smp_wmb(); 2569 2570 if (apc->port_is_up) 2571 netif_carrier_on(ndev); 2572 2573 netif_device_attach(ndev); 2574 2575 return 0; 2576 } 2577 2578 static int mana_dealloc_queues(struct net_device *ndev) 2579 { 2580 struct mana_port_context *apc = netdev_priv(ndev); 2581 unsigned long timeout = jiffies + 120 * HZ; 2582 struct gdma_dev *gd = apc->ac->gdma_dev; 2583 struct mana_txq *txq; 2584 struct sk_buff *skb; 2585 int i, err; 2586 u32 tsleep; 2587 2588 if (apc->port_is_up) 2589 return -EINVAL; 2590 2591 mana_chn_setxdp(apc, NULL); 2592 2593 if (gd->gdma_context->is_pf) 2594 mana_pf_deregister_filter(apc); 2595 2596 /* No packet can be transmitted now since apc->port_is_up is false. 2597 * There is still a tiny chance that mana_poll_tx_cq() can re-enable 2598 * a txq because it may not timely see apc->port_is_up being cleared 2599 * to false, but it doesn't matter since mana_start_xmit() drops any 2600 * new packets due to apc->port_is_up being false. 2601 * 2602 * Drain all the in-flight TX packets. 2603 * A timeout of 120 seconds for all the queues is used. 2604 * This will break the while loop when h/w is not responding. 2605 * This value of 120 has been decided here considering max 2606 * number of queues. 2607 */ 2608 2609 for (i = 0; i < apc->num_queues; i++) { 2610 txq = &apc->tx_qp[i].txq; 2611 tsleep = 1000; 2612 while (atomic_read(&txq->pending_sends) > 0 && 2613 time_before(jiffies, timeout)) { 2614 usleep_range(tsleep, tsleep + 1000); 2615 tsleep <<= 1; 2616 } 2617 if (atomic_read(&txq->pending_sends)) { 2618 err = pcie_flr(to_pci_dev(gd->gdma_context->dev)); 2619 if (err) { 2620 netdev_err(ndev, "flr failed %d with %d pkts pending in txq %u\n", 2621 err, atomic_read(&txq->pending_sends), 2622 txq->gdma_txq_id); 2623 } 2624 break; 2625 } 2626 } 2627 2628 for (i = 0; i < apc->num_queues; i++) { 2629 txq = &apc->tx_qp[i].txq; 2630 while ((skb = skb_dequeue(&txq->pending_skbs))) { 2631 mana_unmap_skb(skb, apc); 2632 dev_kfree_skb_any(skb); 2633 } 2634 atomic_set(&txq->pending_sends, 0); 2635 } 2636 /* We're 100% sure the queues can no longer be woken up, because 2637 * we're sure now mana_poll_tx_cq() can't be running. 2638 */ 2639 2640 apc->rss_state = TRI_STATE_FALSE; 2641 err = mana_config_rss(apc, TRI_STATE_FALSE, false, false); 2642 if (err) { 2643 netdev_err(ndev, "Failed to disable vPort: %d\n", err); 2644 return err; 2645 } 2646 2647 mana_destroy_vport(apc); 2648 2649 return 0; 2650 } 2651 2652 int mana_detach(struct net_device *ndev, bool from_close) 2653 { 2654 struct mana_port_context *apc = netdev_priv(ndev); 2655 int err; 2656 2657 ASSERT_RTNL(); 2658 2659 apc->port_st_save = apc->port_is_up; 2660 apc->port_is_up = false; 2661 2662 /* Ensure port state updated before txq state */ 2663 smp_wmb(); 2664 2665 netif_tx_disable(ndev); 2666 netif_carrier_off(ndev); 2667 2668 if (apc->port_st_save) { 2669 err = mana_dealloc_queues(ndev); 2670 if (err) 2671 return err; 2672 } 2673 2674 if (!from_close) { 2675 netif_device_detach(ndev); 2676 mana_cleanup_port_context(apc); 2677 } 2678 2679 return 0; 2680 } 2681 2682 static int mana_probe_port(struct mana_context *ac, int port_idx, 2683 struct net_device **ndev_storage) 2684 { 2685 struct gdma_context *gc = ac->gdma_dev->gdma_context; 2686 struct mana_port_context *apc; 2687 struct net_device *ndev; 2688 int err; 2689 2690 ndev = alloc_etherdev_mq(sizeof(struct mana_port_context), 2691 gc->max_num_queues); 2692 if (!ndev) 2693 return -ENOMEM; 2694 2695 *ndev_storage = ndev; 2696 2697 apc = netdev_priv(ndev); 2698 apc->ac = ac; 2699 apc->ndev = ndev; 2700 apc->max_queues = gc->max_num_queues; 2701 apc->num_queues = gc->max_num_queues; 2702 apc->port_handle = INVALID_MANA_HANDLE; 2703 apc->pf_filter_handle = INVALID_MANA_HANDLE; 2704 apc->port_idx = port_idx; 2705 2706 mutex_init(&apc->vport_mutex); 2707 apc->vport_use_count = 0; 2708 2709 ndev->netdev_ops = &mana_devops; 2710 ndev->ethtool_ops = &mana_ethtool_ops; 2711 ndev->mtu = ETH_DATA_LEN; 2712 ndev->max_mtu = gc->adapter_mtu - ETH_HLEN; 2713 ndev->min_mtu = ETH_MIN_MTU; 2714 ndev->needed_headroom = MANA_HEADROOM; 2715 ndev->dev_port = port_idx; 2716 SET_NETDEV_DEV(ndev, gc->dev); 2717 2718 netif_carrier_off(ndev); 2719 2720 netdev_rss_key_fill(apc->hashkey, MANA_HASH_KEY_SIZE); 2721 2722 err = mana_init_port(ndev); 2723 if (err) 2724 goto free_net; 2725 2726 netdev_lockdep_set_classes(ndev); 2727 2728 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM; 2729 ndev->hw_features |= NETIF_F_RXCSUM; 2730 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6; 2731 ndev->hw_features |= NETIF_F_RXHASH; 2732 ndev->features = ndev->hw_features | NETIF_F_HW_VLAN_CTAG_TX | 2733 NETIF_F_HW_VLAN_CTAG_RX; 2734 ndev->vlan_features = ndev->features; 2735 xdp_set_features_flag(ndev, NETDEV_XDP_ACT_BASIC | 2736 NETDEV_XDP_ACT_REDIRECT | 2737 NETDEV_XDP_ACT_NDO_XMIT); 2738 2739 err = register_netdev(ndev); 2740 if (err) { 2741 netdev_err(ndev, "Unable to register netdev.\n"); 2742 goto reset_apc; 2743 } 2744 2745 return 0; 2746 2747 reset_apc: 2748 kfree(apc->rxqs); 2749 apc->rxqs = NULL; 2750 free_net: 2751 *ndev_storage = NULL; 2752 netdev_err(ndev, "Failed to probe vPort %d: %d\n", port_idx, err); 2753 free_netdev(ndev); 2754 return err; 2755 } 2756 2757 static void adev_release(struct device *dev) 2758 { 2759 struct mana_adev *madev = container_of(dev, struct mana_adev, adev.dev); 2760 2761 kfree(madev); 2762 } 2763 2764 static void remove_adev(struct gdma_dev *gd) 2765 { 2766 struct auxiliary_device *adev = gd->adev; 2767 int id = adev->id; 2768 2769 auxiliary_device_delete(adev); 2770 auxiliary_device_uninit(adev); 2771 2772 mana_adev_idx_free(id); 2773 gd->adev = NULL; 2774 } 2775 2776 static int add_adev(struct gdma_dev *gd) 2777 { 2778 struct auxiliary_device *adev; 2779 struct mana_adev *madev; 2780 int ret; 2781 2782 madev = kzalloc(sizeof(*madev), GFP_KERNEL); 2783 if (!madev) 2784 return -ENOMEM; 2785 2786 adev = &madev->adev; 2787 ret = mana_adev_idx_alloc(); 2788 if (ret < 0) 2789 goto idx_fail; 2790 adev->id = ret; 2791 2792 adev->name = "rdma"; 2793 adev->dev.parent = gd->gdma_context->dev; 2794 adev->dev.release = adev_release; 2795 madev->mdev = gd; 2796 2797 ret = auxiliary_device_init(adev); 2798 if (ret) 2799 goto init_fail; 2800 2801 ret = auxiliary_device_add(adev); 2802 if (ret) 2803 goto add_fail; 2804 2805 gd->adev = adev; 2806 return 0; 2807 2808 add_fail: 2809 auxiliary_device_uninit(adev); 2810 2811 init_fail: 2812 mana_adev_idx_free(adev->id); 2813 2814 idx_fail: 2815 kfree(madev); 2816 2817 return ret; 2818 } 2819 2820 int mana_probe(struct gdma_dev *gd, bool resuming) 2821 { 2822 struct gdma_context *gc = gd->gdma_context; 2823 struct mana_context *ac = gd->driver_data; 2824 struct device *dev = gc->dev; 2825 u16 num_ports = 0; 2826 int err; 2827 int i; 2828 2829 dev_info(dev, 2830 "Microsoft Azure Network Adapter protocol version: %d.%d.%d\n", 2831 MANA_MAJOR_VERSION, MANA_MINOR_VERSION, MANA_MICRO_VERSION); 2832 2833 err = mana_gd_register_device(gd); 2834 if (err) 2835 return err; 2836 2837 if (!resuming) { 2838 ac = kzalloc(sizeof(*ac), GFP_KERNEL); 2839 if (!ac) 2840 return -ENOMEM; 2841 2842 ac->gdma_dev = gd; 2843 gd->driver_data = ac; 2844 } 2845 2846 err = mana_create_eq(ac); 2847 if (err) 2848 goto out; 2849 2850 err = mana_query_device_cfg(ac, MANA_MAJOR_VERSION, MANA_MINOR_VERSION, 2851 MANA_MICRO_VERSION, &num_ports); 2852 if (err) 2853 goto out; 2854 2855 if (!resuming) { 2856 ac->num_ports = num_ports; 2857 } else { 2858 if (ac->num_ports != num_ports) { 2859 dev_err(dev, "The number of vPorts changed: %d->%d\n", 2860 ac->num_ports, num_ports); 2861 err = -EPROTO; 2862 goto out; 2863 } 2864 } 2865 2866 if (ac->num_ports == 0) 2867 dev_err(dev, "Failed to detect any vPort\n"); 2868 2869 if (ac->num_ports > MAX_PORTS_IN_MANA_DEV) 2870 ac->num_ports = MAX_PORTS_IN_MANA_DEV; 2871 2872 if (!resuming) { 2873 for (i = 0; i < ac->num_ports; i++) { 2874 err = mana_probe_port(ac, i, &ac->ports[i]); 2875 if (err) 2876 break; 2877 } 2878 } else { 2879 for (i = 0; i < ac->num_ports; i++) { 2880 rtnl_lock(); 2881 err = mana_attach(ac->ports[i]); 2882 rtnl_unlock(); 2883 if (err) 2884 break; 2885 } 2886 } 2887 2888 err = add_adev(gd); 2889 out: 2890 if (err) 2891 mana_remove(gd, false); 2892 2893 return err; 2894 } 2895 2896 void mana_remove(struct gdma_dev *gd, bool suspending) 2897 { 2898 struct gdma_context *gc = gd->gdma_context; 2899 struct mana_context *ac = gd->driver_data; 2900 struct device *dev = gc->dev; 2901 struct net_device *ndev; 2902 int err; 2903 int i; 2904 2905 /* adev currently doesn't support suspending, always remove it */ 2906 if (gd->adev) 2907 remove_adev(gd); 2908 2909 for (i = 0; i < ac->num_ports; i++) { 2910 ndev = ac->ports[i]; 2911 if (!ndev) { 2912 if (i == 0) 2913 dev_err(dev, "No net device to remove\n"); 2914 goto out; 2915 } 2916 2917 /* All cleanup actions should stay after rtnl_lock(), otherwise 2918 * other functions may access partially cleaned up data. 2919 */ 2920 rtnl_lock(); 2921 2922 err = mana_detach(ndev, false); 2923 if (err) 2924 netdev_err(ndev, "Failed to detach vPort %d: %d\n", 2925 i, err); 2926 2927 if (suspending) { 2928 /* No need to unregister the ndev. */ 2929 rtnl_unlock(); 2930 continue; 2931 } 2932 2933 unregister_netdevice(ndev); 2934 2935 rtnl_unlock(); 2936 2937 free_netdev(ndev); 2938 } 2939 2940 mana_destroy_eq(ac); 2941 out: 2942 mana_gd_deregister_device(gd); 2943 2944 if (suspending) 2945 return; 2946 2947 gd->driver_data = NULL; 2948 gd->gdma_context = NULL; 2949 kfree(ac); 2950 } 2951