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