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/debugfs.h> 7 #include <linux/inetdevice.h> 8 #include <linux/etherdevice.h> 9 #include <linux/ethtool.h> 10 #include <linux/filter.h> 11 #include <linux/mm.h> 12 #include <linux/pci.h> 13 #include <linux/export.h> 14 #include <linux/skbuff.h> 15 16 #include <net/checksum.h> 17 #include <net/ip6_checksum.h> 18 #include <net/netdev_lock.h> 19 #include <net/page_pool/helpers.h> 20 #include <net/xdp.h> 21 22 #include <net/mana/mana.h> 23 #include <net/mana/mana_auxiliary.h> 24 #include <net/mana/hw_channel.h> 25 26 static DEFINE_IDA(mana_adev_ida); 27 28 static int mana_adev_idx_alloc(void) 29 { 30 return ida_alloc(&mana_adev_ida, GFP_KERNEL); 31 } 32 33 static void mana_adev_idx_free(int idx) 34 { 35 ida_free(&mana_adev_ida, idx); 36 } 37 38 static ssize_t mana_dbg_q_read(struct file *filp, char __user *buf, size_t count, 39 loff_t *pos) 40 { 41 struct gdma_queue *gdma_q = filp->private_data; 42 43 return simple_read_from_buffer(buf, count, pos, gdma_q->queue_mem_ptr, 44 gdma_q->queue_size); 45 } 46 47 static const struct file_operations mana_dbg_q_fops = { 48 .owner = THIS_MODULE, 49 .open = simple_open, 50 .read = mana_dbg_q_read, 51 }; 52 53 static bool mana_en_need_log(struct mana_port_context *apc, int err) 54 { 55 if (apc && apc->ac && apc->ac->gdma_dev && 56 apc->ac->gdma_dev->gdma_context) 57 return mana_need_log(apc->ac->gdma_dev->gdma_context, err); 58 else 59 return true; 60 } 61 62 static void mana_put_rx_page(struct mana_rxq *rxq, struct page *page, 63 bool from_pool) 64 { 65 if (from_pool) 66 page_pool_put_full_page(rxq->page_pool, page, false); 67 else 68 put_page(page); 69 } 70 71 /* Microsoft Azure Network Adapter (MANA) functions */ 72 73 static int mana_open(struct net_device *ndev) 74 { 75 struct mana_port_context *apc = netdev_priv(ndev); 76 int err; 77 err = mana_alloc_queues(ndev); 78 79 if (err) { 80 netdev_err(ndev, "%s failed to allocate queues: %d\n", __func__, err); 81 return err; 82 } 83 84 apc->port_is_up = true; 85 86 /* Ensure port state updated before txq state */ 87 smp_wmb(); 88 89 netif_tx_wake_all_queues(ndev); 90 netdev_dbg(ndev, "%s successful\n", __func__); 91 return 0; 92 } 93 94 static int mana_close(struct net_device *ndev) 95 { 96 struct mana_port_context *apc = netdev_priv(ndev); 97 98 if (!apc->port_is_up) 99 return 0; 100 101 return mana_detach(ndev, true); 102 } 103 104 static void mana_link_state_handle(struct work_struct *w) 105 { 106 struct mana_context *ac; 107 struct net_device *ndev; 108 u32 link_event; 109 bool link_up; 110 int i; 111 112 ac = container_of(w, struct mana_context, link_change_work); 113 114 rtnl_lock(); 115 116 link_event = READ_ONCE(ac->link_event); 117 118 if (link_event == HWC_DATA_HW_LINK_CONNECT) 119 link_up = true; 120 else if (link_event == HWC_DATA_HW_LINK_DISCONNECT) 121 link_up = false; 122 else 123 goto out; 124 125 /* Process all ports */ 126 for (i = 0; i < ac->num_ports; i++) { 127 ndev = ac->ports[i]; 128 if (!ndev) 129 continue; 130 131 if (link_up) { 132 netif_carrier_on(ndev); 133 134 __netdev_notify_peers(ndev); 135 } else { 136 netif_carrier_off(ndev); 137 } 138 } 139 140 out: 141 rtnl_unlock(); 142 } 143 144 static bool mana_can_tx(struct gdma_queue *wq) 145 { 146 return mana_gd_wq_avail_space(wq) >= MAX_TX_WQE_SIZE; 147 } 148 149 static unsigned int mana_checksum_info(struct sk_buff *skb) 150 { 151 if (skb->protocol == htons(ETH_P_IP)) { 152 struct iphdr *ip = ip_hdr(skb); 153 154 if (ip->protocol == IPPROTO_TCP) 155 return IPPROTO_TCP; 156 157 if (ip->protocol == IPPROTO_UDP) 158 return IPPROTO_UDP; 159 } else if (skb->protocol == htons(ETH_P_IPV6)) { 160 struct ipv6hdr *ip6 = ipv6_hdr(skb); 161 162 if (ip6->nexthdr == IPPROTO_TCP) 163 return IPPROTO_TCP; 164 165 if (ip6->nexthdr == IPPROTO_UDP) 166 return IPPROTO_UDP; 167 } 168 169 /* No csum offloading */ 170 return 0; 171 } 172 173 static void mana_add_sge(struct mana_tx_package *tp, struct mana_skb_head *ash, 174 int sg_i, dma_addr_t da, int sge_len, u32 gpa_mkey) 175 { 176 ash->dma_handle[sg_i] = da; 177 ash->size[sg_i] = sge_len; 178 179 tp->wqe_req.sgl[sg_i].address = da; 180 tp->wqe_req.sgl[sg_i].mem_key = gpa_mkey; 181 tp->wqe_req.sgl[sg_i].size = sge_len; 182 } 183 184 static int mana_map_skb(struct sk_buff *skb, struct mana_port_context *apc, 185 struct mana_tx_package *tp, int gso_hs) 186 { 187 struct mana_skb_head *ash = (struct mana_skb_head *)skb->head; 188 int hsg = 1; /* num of SGEs of linear part */ 189 struct gdma_dev *gd = apc->ac->gdma_dev; 190 int skb_hlen = skb_headlen(skb); 191 int sge0_len, sge1_len = 0; 192 struct gdma_context *gc; 193 struct device *dev; 194 skb_frag_t *frag; 195 dma_addr_t da; 196 int sg_i; 197 int i; 198 199 gc = gd->gdma_context; 200 dev = gc->dev; 201 202 if (gso_hs && gso_hs < skb_hlen) { 203 sge0_len = gso_hs; 204 sge1_len = skb_hlen - gso_hs; 205 } else { 206 sge0_len = skb_hlen; 207 } 208 209 da = dma_map_single(dev, skb->data, sge0_len, DMA_TO_DEVICE); 210 if (dma_mapping_error(dev, da)) 211 return -ENOMEM; 212 213 mana_add_sge(tp, ash, 0, da, sge0_len, gd->gpa_mkey); 214 215 if (sge1_len) { 216 sg_i = 1; 217 da = dma_map_single(dev, skb->data + sge0_len, sge1_len, 218 DMA_TO_DEVICE); 219 if (dma_mapping_error(dev, da)) 220 goto frag_err; 221 222 mana_add_sge(tp, ash, sg_i, da, sge1_len, gd->gpa_mkey); 223 hsg = 2; 224 } 225 226 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 227 sg_i = hsg + i; 228 229 frag = &skb_shinfo(skb)->frags[i]; 230 da = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag), 231 DMA_TO_DEVICE); 232 if (dma_mapping_error(dev, da)) 233 goto frag_err; 234 235 mana_add_sge(tp, ash, sg_i, da, skb_frag_size(frag), 236 gd->gpa_mkey); 237 } 238 239 return 0; 240 241 frag_err: 242 if (net_ratelimit()) 243 netdev_err(apc->ndev, "Failed to map skb of size %u to DMA\n", 244 skb->len); 245 for (i = sg_i - 1; i >= hsg; i--) 246 dma_unmap_page(dev, ash->dma_handle[i], ash->size[i], 247 DMA_TO_DEVICE); 248 249 for (i = hsg - 1; i >= 0; i--) 250 dma_unmap_single(dev, ash->dma_handle[i], ash->size[i], 251 DMA_TO_DEVICE); 252 253 return -ENOMEM; 254 } 255 256 /* Handle the case when GSO SKB linear length is too large. 257 * MANA NIC requires GSO packets to put only the packet header to SGE0. 258 * So, we need 2 SGEs for the skb linear part which contains more than the 259 * header. 260 * Return a positive value for the number of SGEs, or a negative value 261 * for an error. 262 */ 263 static int mana_fix_skb_head(struct net_device *ndev, struct sk_buff *skb, 264 int gso_hs) 265 { 266 int num_sge = 1 + skb_shinfo(skb)->nr_frags; 267 int skb_hlen = skb_headlen(skb); 268 269 if (gso_hs < skb_hlen) { 270 num_sge++; 271 } else if (gso_hs > skb_hlen) { 272 if (net_ratelimit()) 273 netdev_err(ndev, 274 "TX nonlinear head: hs:%d, skb_hlen:%d\n", 275 gso_hs, skb_hlen); 276 277 return -EINVAL; 278 } 279 280 return num_sge; 281 } 282 283 /* Get the GSO packet's header size */ 284 static int mana_get_gso_hs(struct sk_buff *skb) 285 { 286 int gso_hs; 287 288 if (skb->encapsulation) { 289 gso_hs = skb_inner_tcp_all_headers(skb); 290 } else { 291 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) { 292 gso_hs = skb_transport_offset(skb) + 293 sizeof(struct udphdr); 294 } else { 295 gso_hs = skb_tcp_all_headers(skb); 296 } 297 } 298 299 return gso_hs; 300 } 301 302 static void mana_per_port_queue_reset_work_handler(struct work_struct *work) 303 { 304 struct mana_port_context *apc = container_of(work, 305 struct mana_port_context, 306 queue_reset_work); 307 struct net_device *ndev = apc->ndev; 308 int err; 309 310 rtnl_lock(); 311 312 /* Pre-allocate buffers to prevent failure in mana_attach later */ 313 err = mana_pre_alloc_rxbufs(apc, ndev->mtu, apc->num_queues); 314 if (err) { 315 netdev_err(ndev, "Insufficient memory for reset post tx stall detection\n"); 316 goto out; 317 } 318 319 err = mana_detach(ndev, false); 320 if (err) { 321 netdev_err(ndev, "mana_detach failed: %d\n", err); 322 goto dealloc_pre_rxbufs; 323 } 324 325 err = mana_attach(ndev); 326 if (err) 327 netdev_err(ndev, "mana_attach failed: %d\n", err); 328 329 dealloc_pre_rxbufs: 330 mana_pre_dealloc_rxbufs(apc); 331 out: 332 rtnl_unlock(); 333 } 334 335 netdev_tx_t mana_start_xmit(struct sk_buff *skb, struct net_device *ndev) 336 { 337 enum mana_tx_pkt_format pkt_fmt = MANA_SHORT_PKT_FMT; 338 struct mana_port_context *apc = netdev_priv(ndev); 339 int gso_hs = 0; /* zero for non-GSO pkts */ 340 u16 txq_idx = skb_get_queue_mapping(skb); 341 struct gdma_dev *gd = apc->ac->gdma_dev; 342 bool ipv4 = false, ipv6 = false; 343 struct mana_tx_package pkg = {}; 344 struct netdev_queue *net_txq; 345 struct mana_stats_tx *tx_stats; 346 struct gdma_queue *gdma_sq; 347 int err, len, num_gso_seg; 348 unsigned int csum_type; 349 struct mana_txq *txq; 350 struct mana_cq *cq; 351 352 if (unlikely(!apc->port_is_up)) 353 goto tx_drop; 354 355 if (skb_cow_head(skb, MANA_HEADROOM)) 356 goto tx_drop_count; 357 358 if (unlikely(ipv6_hopopt_jumbo_remove(skb))) 359 goto tx_drop_count; 360 361 txq = &apc->tx_qp[txq_idx].txq; 362 gdma_sq = txq->gdma_sq; 363 cq = &apc->tx_qp[txq_idx].tx_cq; 364 tx_stats = &txq->stats; 365 366 BUILD_BUG_ON(MAX_TX_WQE_SGL_ENTRIES != MANA_MAX_TX_WQE_SGL_ENTRIES); 367 if (MAX_SKB_FRAGS + 2 > MAX_TX_WQE_SGL_ENTRIES && 368 skb_shinfo(skb)->nr_frags + 2 > MAX_TX_WQE_SGL_ENTRIES) { 369 /* GSO skb with Hardware SGE limit exceeded is not expected here 370 * as they are handled in mana_features_check() callback 371 */ 372 if (skb_linearize(skb)) { 373 netdev_warn_once(ndev, "Failed to linearize skb with nr_frags=%d and is_gso=%d\n", 374 skb_shinfo(skb)->nr_frags, 375 skb_is_gso(skb)); 376 goto tx_drop_count; 377 } 378 apc->eth_stats.tx_linear_pkt_cnt++; 379 } 380 381 pkg.tx_oob.s_oob.vcq_num = cq->gdma_id; 382 pkg.tx_oob.s_oob.vsq_frame = txq->vsq_frame; 383 384 if (txq->vp_offset > MANA_SHORT_VPORT_OFFSET_MAX) { 385 pkg.tx_oob.l_oob.long_vp_offset = txq->vp_offset; 386 pkt_fmt = MANA_LONG_PKT_FMT; 387 } else { 388 pkg.tx_oob.s_oob.short_vp_offset = txq->vp_offset; 389 } 390 391 if (skb_vlan_tag_present(skb)) { 392 pkt_fmt = MANA_LONG_PKT_FMT; 393 pkg.tx_oob.l_oob.inject_vlan_pri_tag = 1; 394 pkg.tx_oob.l_oob.pcp = skb_vlan_tag_get_prio(skb); 395 pkg.tx_oob.l_oob.dei = skb_vlan_tag_get_cfi(skb); 396 pkg.tx_oob.l_oob.vlan_id = skb_vlan_tag_get_id(skb); 397 } 398 399 pkg.tx_oob.s_oob.pkt_fmt = pkt_fmt; 400 401 if (pkt_fmt == MANA_SHORT_PKT_FMT) { 402 pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_short_oob); 403 u64_stats_update_begin(&tx_stats->syncp); 404 tx_stats->short_pkt_fmt++; 405 u64_stats_update_end(&tx_stats->syncp); 406 } else { 407 pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_oob); 408 u64_stats_update_begin(&tx_stats->syncp); 409 tx_stats->long_pkt_fmt++; 410 u64_stats_update_end(&tx_stats->syncp); 411 } 412 413 pkg.wqe_req.inline_oob_data = &pkg.tx_oob; 414 pkg.wqe_req.flags = 0; 415 pkg.wqe_req.client_data_unit = 0; 416 417 pkg.wqe_req.num_sge = 1 + skb_shinfo(skb)->nr_frags; 418 419 if (skb->protocol == htons(ETH_P_IP)) 420 ipv4 = true; 421 else if (skb->protocol == htons(ETH_P_IPV6)) 422 ipv6 = true; 423 424 if (skb_is_gso(skb)) { 425 int num_sge; 426 427 gso_hs = mana_get_gso_hs(skb); 428 429 num_sge = mana_fix_skb_head(ndev, skb, gso_hs); 430 if (num_sge > 0) 431 pkg.wqe_req.num_sge = num_sge; 432 else 433 goto tx_drop_count; 434 435 u64_stats_update_begin(&tx_stats->syncp); 436 if (skb->encapsulation) { 437 tx_stats->tso_inner_packets++; 438 tx_stats->tso_inner_bytes += skb->len - gso_hs; 439 } else { 440 tx_stats->tso_packets++; 441 tx_stats->tso_bytes += skb->len - gso_hs; 442 } 443 u64_stats_update_end(&tx_stats->syncp); 444 445 pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4; 446 pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6; 447 448 pkg.tx_oob.s_oob.comp_iphdr_csum = 1; 449 pkg.tx_oob.s_oob.comp_tcp_csum = 1; 450 pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb); 451 452 pkg.wqe_req.client_data_unit = skb_shinfo(skb)->gso_size; 453 pkg.wqe_req.flags = GDMA_WR_OOB_IN_SGL | GDMA_WR_PAD_BY_SGE0; 454 if (ipv4) { 455 ip_hdr(skb)->tot_len = 0; 456 ip_hdr(skb)->check = 0; 457 tcp_hdr(skb)->check = 458 ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 459 ip_hdr(skb)->daddr, 0, 460 IPPROTO_TCP, 0); 461 } else { 462 ipv6_hdr(skb)->payload_len = 0; 463 tcp_hdr(skb)->check = 464 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 465 &ipv6_hdr(skb)->daddr, 0, 466 IPPROTO_TCP, 0); 467 } 468 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 469 csum_type = mana_checksum_info(skb); 470 471 u64_stats_update_begin(&tx_stats->syncp); 472 tx_stats->csum_partial++; 473 u64_stats_update_end(&tx_stats->syncp); 474 475 if (csum_type == IPPROTO_TCP) { 476 pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4; 477 pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6; 478 479 pkg.tx_oob.s_oob.comp_tcp_csum = 1; 480 pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb); 481 482 } else if (csum_type == IPPROTO_UDP) { 483 pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4; 484 pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6; 485 486 pkg.tx_oob.s_oob.comp_udp_csum = 1; 487 } else { 488 /* Can't do offload of this type of checksum */ 489 if (skb_checksum_help(skb)) 490 goto tx_drop_count; 491 } 492 } 493 494 if (pkg.wqe_req.num_sge <= ARRAY_SIZE(pkg.sgl_array)) { 495 pkg.wqe_req.sgl = pkg.sgl_array; 496 } else { 497 pkg.sgl_ptr = kmalloc_array(pkg.wqe_req.num_sge, 498 sizeof(struct gdma_sge), 499 GFP_ATOMIC); 500 if (!pkg.sgl_ptr) 501 goto tx_drop_count; 502 503 pkg.wqe_req.sgl = pkg.sgl_ptr; 504 } 505 506 if (mana_map_skb(skb, apc, &pkg, gso_hs)) { 507 u64_stats_update_begin(&tx_stats->syncp); 508 tx_stats->mana_map_err++; 509 u64_stats_update_end(&tx_stats->syncp); 510 goto free_sgl_ptr; 511 } 512 513 skb_queue_tail(&txq->pending_skbs, skb); 514 515 len = skb->len; 516 num_gso_seg = skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1; 517 net_txq = netdev_get_tx_queue(ndev, txq_idx); 518 519 err = mana_gd_post_work_request(gdma_sq, &pkg.wqe_req, 520 (struct gdma_posted_wqe_info *)skb->cb); 521 if (!mana_can_tx(gdma_sq)) { 522 netif_tx_stop_queue(net_txq); 523 apc->eth_stats.stop_queue++; 524 } 525 526 if (err) { 527 (void)skb_dequeue_tail(&txq->pending_skbs); 528 mana_unmap_skb(skb, apc); 529 netdev_warn(ndev, "Failed to post TX OOB: %d\n", err); 530 goto free_sgl_ptr; 531 } 532 533 err = NETDEV_TX_OK; 534 atomic_inc(&txq->pending_sends); 535 536 mana_gd_wq_ring_doorbell(gd->gdma_context, gdma_sq); 537 538 /* skb may be freed after mana_gd_post_work_request. Do not use it. */ 539 skb = NULL; 540 541 /* Populated the packet and bytes counters based on post GSO packet 542 * calculations 543 */ 544 tx_stats = &txq->stats; 545 u64_stats_update_begin(&tx_stats->syncp); 546 tx_stats->packets += num_gso_seg; 547 tx_stats->bytes += len + ((num_gso_seg - 1) * gso_hs); 548 u64_stats_update_end(&tx_stats->syncp); 549 550 if (netif_tx_queue_stopped(net_txq) && mana_can_tx(gdma_sq)) { 551 netif_tx_wake_queue(net_txq); 552 apc->eth_stats.wake_queue++; 553 } 554 555 kfree(pkg.sgl_ptr); 556 return err; 557 558 free_sgl_ptr: 559 kfree(pkg.sgl_ptr); 560 tx_drop_count: 561 ndev->stats.tx_dropped++; 562 tx_drop: 563 dev_kfree_skb_any(skb); 564 return NETDEV_TX_OK; 565 } 566 567 #if (MAX_SKB_FRAGS + 2 > MANA_MAX_TX_WQE_SGL_ENTRIES) 568 static netdev_features_t mana_features_check(struct sk_buff *skb, 569 struct net_device *ndev, 570 netdev_features_t features) 571 { 572 if (skb_shinfo(skb)->nr_frags + 2 > MAX_TX_WQE_SGL_ENTRIES) { 573 /* Exceeds HW SGE limit. 574 * GSO case: 575 * Disable GSO so the stack will software-segment the skb 576 * into smaller skbs that fit the SGE budget. 577 * Non-GSO case: 578 * The xmit path will attempt skb_linearize() as a fallback. 579 */ 580 features &= ~NETIF_F_GSO_MASK; 581 } 582 return features; 583 } 584 #endif 585 586 static void mana_get_stats64(struct net_device *ndev, 587 struct rtnl_link_stats64 *st) 588 { 589 struct mana_port_context *apc = netdev_priv(ndev); 590 unsigned int num_queues = apc->num_queues; 591 struct mana_stats_rx *rx_stats; 592 struct mana_stats_tx *tx_stats; 593 unsigned int start; 594 u64 packets, bytes; 595 int q; 596 597 if (!apc->port_is_up) 598 return; 599 600 netdev_stats_to_stats64(st, &ndev->stats); 601 602 if (apc->ac->hwc_timeout_occurred) 603 netdev_warn_once(ndev, "HWC timeout occurred\n"); 604 605 st->rx_missed_errors = apc->ac->hc_stats.hc_rx_discards_no_wqe; 606 607 for (q = 0; q < num_queues; q++) { 608 rx_stats = &apc->rxqs[q]->stats; 609 610 do { 611 start = u64_stats_fetch_begin(&rx_stats->syncp); 612 packets = rx_stats->packets; 613 bytes = rx_stats->bytes; 614 } while (u64_stats_fetch_retry(&rx_stats->syncp, start)); 615 616 st->rx_packets += packets; 617 st->rx_bytes += bytes; 618 } 619 620 for (q = 0; q < num_queues; q++) { 621 tx_stats = &apc->tx_qp[q].txq.stats; 622 623 do { 624 start = u64_stats_fetch_begin(&tx_stats->syncp); 625 packets = tx_stats->packets; 626 bytes = tx_stats->bytes; 627 } while (u64_stats_fetch_retry(&tx_stats->syncp, start)); 628 629 st->tx_packets += packets; 630 st->tx_bytes += bytes; 631 } 632 } 633 634 static int mana_get_tx_queue(struct net_device *ndev, struct sk_buff *skb, 635 int old_q) 636 { 637 struct mana_port_context *apc = netdev_priv(ndev); 638 u32 hash = skb_get_hash(skb); 639 struct sock *sk = skb->sk; 640 int txq; 641 642 txq = apc->indir_table[hash & (apc->indir_table_sz - 1)]; 643 644 if (txq != old_q && sk && sk_fullsock(sk) && 645 rcu_access_pointer(sk->sk_dst_cache)) 646 sk_tx_queue_set(sk, txq); 647 648 return txq; 649 } 650 651 static u16 mana_select_queue(struct net_device *ndev, struct sk_buff *skb, 652 struct net_device *sb_dev) 653 { 654 int txq; 655 656 if (ndev->real_num_tx_queues == 1) 657 return 0; 658 659 txq = sk_tx_queue_get(skb->sk); 660 661 if (txq < 0 || skb->ooo_okay || txq >= ndev->real_num_tx_queues) { 662 if (skb_rx_queue_recorded(skb)) 663 txq = skb_get_rx_queue(skb); 664 else 665 txq = mana_get_tx_queue(ndev, skb, txq); 666 } 667 668 return txq; 669 } 670 671 /* Release pre-allocated RX buffers */ 672 void mana_pre_dealloc_rxbufs(struct mana_port_context *mpc) 673 { 674 struct device *dev; 675 int i; 676 677 dev = mpc->ac->gdma_dev->gdma_context->dev; 678 679 if (!mpc->rxbufs_pre) 680 goto out1; 681 682 if (!mpc->das_pre) 683 goto out2; 684 685 while (mpc->rxbpre_total) { 686 i = --mpc->rxbpre_total; 687 dma_unmap_single(dev, mpc->das_pre[i], mpc->rxbpre_datasize, 688 DMA_FROM_DEVICE); 689 put_page(virt_to_head_page(mpc->rxbufs_pre[i])); 690 } 691 692 kfree(mpc->das_pre); 693 mpc->das_pre = NULL; 694 695 out2: 696 kfree(mpc->rxbufs_pre); 697 mpc->rxbufs_pre = NULL; 698 699 out1: 700 mpc->rxbpre_datasize = 0; 701 mpc->rxbpre_alloc_size = 0; 702 mpc->rxbpre_headroom = 0; 703 } 704 705 /* Get a buffer from the pre-allocated RX buffers */ 706 static void *mana_get_rxbuf_pre(struct mana_rxq *rxq, dma_addr_t *da) 707 { 708 struct net_device *ndev = rxq->ndev; 709 struct mana_port_context *mpc; 710 void *va; 711 712 mpc = netdev_priv(ndev); 713 714 if (!mpc->rxbufs_pre || !mpc->das_pre || !mpc->rxbpre_total) { 715 netdev_err(ndev, "No RX pre-allocated bufs\n"); 716 return NULL; 717 } 718 719 /* Check sizes to catch unexpected coding error */ 720 if (mpc->rxbpre_datasize != rxq->datasize) { 721 netdev_err(ndev, "rxbpre_datasize mismatch: %u: %u\n", 722 mpc->rxbpre_datasize, rxq->datasize); 723 return NULL; 724 } 725 726 if (mpc->rxbpre_alloc_size != rxq->alloc_size) { 727 netdev_err(ndev, "rxbpre_alloc_size mismatch: %u: %u\n", 728 mpc->rxbpre_alloc_size, rxq->alloc_size); 729 return NULL; 730 } 731 732 if (mpc->rxbpre_headroom != rxq->headroom) { 733 netdev_err(ndev, "rxbpre_headroom mismatch: %u: %u\n", 734 mpc->rxbpre_headroom, rxq->headroom); 735 return NULL; 736 } 737 738 mpc->rxbpre_total--; 739 740 *da = mpc->das_pre[mpc->rxbpre_total]; 741 va = mpc->rxbufs_pre[mpc->rxbpre_total]; 742 mpc->rxbufs_pre[mpc->rxbpre_total] = NULL; 743 744 /* Deallocate the array after all buffers are gone */ 745 if (!mpc->rxbpre_total) 746 mana_pre_dealloc_rxbufs(mpc); 747 748 return va; 749 } 750 751 /* Get RX buffer's data size, alloc size, XDP headroom based on MTU */ 752 static void mana_get_rxbuf_cfg(struct mana_port_context *apc, 753 int mtu, u32 *datasize, u32 *alloc_size, 754 u32 *headroom, u32 *frag_count) 755 { 756 u32 len, buf_size; 757 758 /* Calculate datasize first (consistent across all cases) */ 759 *datasize = mtu + ETH_HLEN; 760 761 /* For xdp and jumbo frames make sure only one packet fits per page */ 762 if (mtu + MANA_RXBUF_PAD > PAGE_SIZE / 2 || mana_xdp_get(apc)) { 763 if (mana_xdp_get(apc)) { 764 *headroom = XDP_PACKET_HEADROOM; 765 *alloc_size = PAGE_SIZE; 766 } else { 767 *headroom = 0; /* no support for XDP */ 768 *alloc_size = SKB_DATA_ALIGN(mtu + MANA_RXBUF_PAD + 769 *headroom); 770 } 771 772 *frag_count = 1; 773 return; 774 } 775 776 /* Standard MTU case - optimize for multiple packets per page */ 777 *headroom = 0; 778 779 /* Calculate base buffer size needed */ 780 len = SKB_DATA_ALIGN(mtu + MANA_RXBUF_PAD + *headroom); 781 buf_size = ALIGN(len, MANA_RX_FRAG_ALIGNMENT); 782 783 /* Calculate how many packets can fit in a page */ 784 *frag_count = PAGE_SIZE / buf_size; 785 *alloc_size = buf_size; 786 } 787 788 int mana_pre_alloc_rxbufs(struct mana_port_context *mpc, int new_mtu, int num_queues) 789 { 790 struct device *dev; 791 struct page *page; 792 dma_addr_t da; 793 int num_rxb; 794 void *va; 795 int i; 796 797 mana_get_rxbuf_cfg(mpc, new_mtu, &mpc->rxbpre_datasize, 798 &mpc->rxbpre_alloc_size, &mpc->rxbpre_headroom, 799 &mpc->rxbpre_frag_count); 800 801 dev = mpc->ac->gdma_dev->gdma_context->dev; 802 803 num_rxb = num_queues * mpc->rx_queue_size; 804 805 WARN(mpc->rxbufs_pre, "mana rxbufs_pre exists\n"); 806 mpc->rxbufs_pre = kmalloc_array(num_rxb, sizeof(void *), GFP_KERNEL); 807 if (!mpc->rxbufs_pre) 808 goto error; 809 810 mpc->das_pre = kmalloc_array(num_rxb, sizeof(dma_addr_t), GFP_KERNEL); 811 if (!mpc->das_pre) 812 goto error; 813 814 mpc->rxbpre_total = 0; 815 816 for (i = 0; i < num_rxb; i++) { 817 page = dev_alloc_pages(get_order(mpc->rxbpre_alloc_size)); 818 if (!page) 819 goto error; 820 821 va = page_to_virt(page); 822 823 da = dma_map_single(dev, va + mpc->rxbpre_headroom, 824 mpc->rxbpre_datasize, DMA_FROM_DEVICE); 825 if (dma_mapping_error(dev, da)) { 826 put_page(page); 827 goto error; 828 } 829 830 mpc->rxbufs_pre[i] = va; 831 mpc->das_pre[i] = da; 832 mpc->rxbpre_total = i + 1; 833 } 834 835 return 0; 836 837 error: 838 netdev_err(mpc->ndev, "Failed to pre-allocate RX buffers for %d queues\n", num_queues); 839 mana_pre_dealloc_rxbufs(mpc); 840 return -ENOMEM; 841 } 842 843 static int mana_change_mtu(struct net_device *ndev, int new_mtu) 844 { 845 struct mana_port_context *mpc = netdev_priv(ndev); 846 unsigned int old_mtu = ndev->mtu; 847 int err; 848 849 /* Pre-allocate buffers to prevent failure in mana_attach later */ 850 err = mana_pre_alloc_rxbufs(mpc, new_mtu, mpc->num_queues); 851 if (err) { 852 netdev_err(ndev, "Insufficient memory for new MTU\n"); 853 return err; 854 } 855 856 err = mana_detach(ndev, false); 857 if (err) { 858 netdev_err(ndev, "mana_detach failed: %d\n", err); 859 goto out; 860 } 861 862 WRITE_ONCE(ndev->mtu, new_mtu); 863 864 err = mana_attach(ndev); 865 if (err) { 866 netdev_err(ndev, "mana_attach failed: %d\n", err); 867 WRITE_ONCE(ndev->mtu, old_mtu); 868 } 869 870 out: 871 mana_pre_dealloc_rxbufs(mpc); 872 return err; 873 } 874 875 static void mana_tx_timeout(struct net_device *netdev, unsigned int txqueue) 876 { 877 struct mana_port_context *apc = netdev_priv(netdev); 878 struct mana_context *ac = apc->ac; 879 struct gdma_context *gc = ac->gdma_dev->gdma_context; 880 881 /* Already in service, hence tx queue reset is not required.*/ 882 if (gc->in_service) 883 return; 884 885 /* Note: If there are pending queue reset work for this port(apc), 886 * subsequent request queued up from here are ignored. This is because 887 * we are using the same work instance per port(apc). 888 */ 889 queue_work(ac->per_port_queue_reset_wq, &apc->queue_reset_work); 890 } 891 892 static int mana_shaper_set(struct net_shaper_binding *binding, 893 const struct net_shaper *shaper, 894 struct netlink_ext_ack *extack) 895 { 896 struct mana_port_context *apc = netdev_priv(binding->netdev); 897 u32 old_speed, rate; 898 int err; 899 900 if (shaper->handle.scope != NET_SHAPER_SCOPE_NETDEV) { 901 NL_SET_ERR_MSG_MOD(extack, "net shaper scope should be netdev"); 902 return -EINVAL; 903 } 904 905 if (apc->handle.id && shaper->handle.id != apc->handle.id) { 906 NL_SET_ERR_MSG_MOD(extack, "Cannot create multiple shapers"); 907 return -EOPNOTSUPP; 908 } 909 910 if (!shaper->bw_max || (shaper->bw_max % 100000000)) { 911 NL_SET_ERR_MSG_MOD(extack, "Please use multiples of 100Mbps for bandwidth"); 912 return -EINVAL; 913 } 914 915 rate = div_u64(shaper->bw_max, 1000); /* Convert bps to Kbps */ 916 rate = div_u64(rate, 1000); /* Convert Kbps to Mbps */ 917 918 /* Get current speed */ 919 err = mana_query_link_cfg(apc); 920 old_speed = (err) ? SPEED_UNKNOWN : apc->speed; 921 922 if (!err) { 923 err = mana_set_bw_clamp(apc, rate, TRI_STATE_TRUE); 924 apc->speed = (err) ? old_speed : rate; 925 apc->handle = (err) ? apc->handle : shaper->handle; 926 } 927 928 return err; 929 } 930 931 static int mana_shaper_del(struct net_shaper_binding *binding, 932 const struct net_shaper_handle *handle, 933 struct netlink_ext_ack *extack) 934 { 935 struct mana_port_context *apc = netdev_priv(binding->netdev); 936 int err; 937 938 err = mana_set_bw_clamp(apc, 0, TRI_STATE_FALSE); 939 940 if (!err) { 941 /* Reset mana port context parameters */ 942 apc->handle.id = 0; 943 apc->handle.scope = NET_SHAPER_SCOPE_UNSPEC; 944 apc->speed = apc->max_speed; 945 } 946 947 return err; 948 } 949 950 static void mana_shaper_cap(struct net_shaper_binding *binding, 951 enum net_shaper_scope scope, 952 unsigned long *flags) 953 { 954 *flags = BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MAX) | 955 BIT(NET_SHAPER_A_CAPS_SUPPORT_METRIC_BPS); 956 } 957 958 static const struct net_shaper_ops mana_shaper_ops = { 959 .set = mana_shaper_set, 960 .delete = mana_shaper_del, 961 .capabilities = mana_shaper_cap, 962 }; 963 964 static const struct net_device_ops mana_devops = { 965 .ndo_open = mana_open, 966 .ndo_stop = mana_close, 967 .ndo_select_queue = mana_select_queue, 968 #if (MAX_SKB_FRAGS + 2 > MANA_MAX_TX_WQE_SGL_ENTRIES) 969 .ndo_features_check = mana_features_check, 970 #endif 971 .ndo_start_xmit = mana_start_xmit, 972 .ndo_validate_addr = eth_validate_addr, 973 .ndo_get_stats64 = mana_get_stats64, 974 .ndo_bpf = mana_bpf, 975 .ndo_xdp_xmit = mana_xdp_xmit, 976 .ndo_change_mtu = mana_change_mtu, 977 .ndo_tx_timeout = mana_tx_timeout, 978 .net_shaper_ops = &mana_shaper_ops, 979 }; 980 981 static void mana_cleanup_port_context(struct mana_port_context *apc) 982 { 983 /* 984 * make sure subsequent cleanup attempts don't end up removing already 985 * cleaned dentry pointer 986 */ 987 debugfs_remove(apc->mana_port_debugfs); 988 apc->mana_port_debugfs = NULL; 989 kfree(apc->rxqs); 990 apc->rxqs = NULL; 991 } 992 993 static void mana_cleanup_indir_table(struct mana_port_context *apc) 994 { 995 apc->indir_table_sz = 0; 996 kfree(apc->indir_table); 997 kfree(apc->rxobj_table); 998 } 999 1000 static int mana_init_port_context(struct mana_port_context *apc) 1001 { 1002 apc->rxqs = kcalloc(apc->num_queues, sizeof(struct mana_rxq *), 1003 GFP_KERNEL); 1004 1005 return !apc->rxqs ? -ENOMEM : 0; 1006 } 1007 1008 static int mana_send_request(struct mana_context *ac, void *in_buf, 1009 u32 in_len, void *out_buf, u32 out_len) 1010 { 1011 struct gdma_context *gc = ac->gdma_dev->gdma_context; 1012 struct gdma_resp_hdr *resp = out_buf; 1013 struct gdma_req_hdr *req = in_buf; 1014 struct device *dev = gc->dev; 1015 static atomic_t activity_id; 1016 int err; 1017 1018 req->dev_id = gc->mana.dev_id; 1019 req->activity_id = atomic_inc_return(&activity_id); 1020 1021 err = mana_gd_send_request(gc, in_len, in_buf, out_len, 1022 out_buf); 1023 if (err || resp->status) { 1024 if (err == -EOPNOTSUPP) 1025 return err; 1026 1027 if (req->req.msg_type != MANA_QUERY_PHY_STAT && 1028 mana_need_log(gc, err)) 1029 dev_err(dev, "Failed to send mana message: %d, 0x%x\n", 1030 err, resp->status); 1031 return err ? err : -EPROTO; 1032 } 1033 1034 if (req->dev_id.as_uint32 != resp->dev_id.as_uint32 || 1035 req->activity_id != resp->activity_id) { 1036 dev_err(dev, "Unexpected mana message response: %x,%x,%x,%x\n", 1037 req->dev_id.as_uint32, resp->dev_id.as_uint32, 1038 req->activity_id, resp->activity_id); 1039 return -EPROTO; 1040 } 1041 1042 return 0; 1043 } 1044 1045 static int mana_verify_resp_hdr(const struct gdma_resp_hdr *resp_hdr, 1046 const enum mana_command_code expected_code, 1047 const u32 min_size) 1048 { 1049 if (resp_hdr->response.msg_type != expected_code) 1050 return -EPROTO; 1051 1052 if (resp_hdr->response.msg_version < GDMA_MESSAGE_V1) 1053 return -EPROTO; 1054 1055 if (resp_hdr->response.msg_size < min_size) 1056 return -EPROTO; 1057 1058 return 0; 1059 } 1060 1061 static int mana_pf_register_hw_vport(struct mana_port_context *apc) 1062 { 1063 struct mana_register_hw_vport_resp resp = {}; 1064 struct mana_register_hw_vport_req req = {}; 1065 int err; 1066 1067 mana_gd_init_req_hdr(&req.hdr, MANA_REGISTER_HW_PORT, 1068 sizeof(req), sizeof(resp)); 1069 req.attached_gfid = 1; 1070 req.is_pf_default_vport = 1; 1071 req.allow_all_ether_types = 1; 1072 1073 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1074 sizeof(resp)); 1075 if (err) { 1076 netdev_err(apc->ndev, "Failed to register hw vPort: %d\n", err); 1077 return err; 1078 } 1079 1080 err = mana_verify_resp_hdr(&resp.hdr, MANA_REGISTER_HW_PORT, 1081 sizeof(resp)); 1082 if (err || resp.hdr.status) { 1083 netdev_err(apc->ndev, "Failed to register hw vPort: %d, 0x%x\n", 1084 err, resp.hdr.status); 1085 return err ? err : -EPROTO; 1086 } 1087 1088 apc->port_handle = resp.hw_vport_handle; 1089 return 0; 1090 } 1091 1092 static void mana_pf_deregister_hw_vport(struct mana_port_context *apc) 1093 { 1094 struct mana_deregister_hw_vport_resp resp = {}; 1095 struct mana_deregister_hw_vport_req req = {}; 1096 int err; 1097 1098 mana_gd_init_req_hdr(&req.hdr, MANA_DEREGISTER_HW_PORT, 1099 sizeof(req), sizeof(resp)); 1100 req.hw_vport_handle = apc->port_handle; 1101 1102 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1103 sizeof(resp)); 1104 if (err) { 1105 if (mana_en_need_log(apc, err)) 1106 netdev_err(apc->ndev, "Failed to unregister hw vPort: %d\n", 1107 err); 1108 1109 return; 1110 } 1111 1112 err = mana_verify_resp_hdr(&resp.hdr, MANA_DEREGISTER_HW_PORT, 1113 sizeof(resp)); 1114 if (err || resp.hdr.status) 1115 netdev_err(apc->ndev, 1116 "Failed to deregister hw vPort: %d, 0x%x\n", 1117 err, resp.hdr.status); 1118 } 1119 1120 static int mana_pf_register_filter(struct mana_port_context *apc) 1121 { 1122 struct mana_register_filter_resp resp = {}; 1123 struct mana_register_filter_req req = {}; 1124 int err; 1125 1126 mana_gd_init_req_hdr(&req.hdr, MANA_REGISTER_FILTER, 1127 sizeof(req), sizeof(resp)); 1128 req.vport = apc->port_handle; 1129 memcpy(req.mac_addr, apc->mac_addr, ETH_ALEN); 1130 1131 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1132 sizeof(resp)); 1133 if (err) { 1134 netdev_err(apc->ndev, "Failed to register filter: %d\n", err); 1135 return err; 1136 } 1137 1138 err = mana_verify_resp_hdr(&resp.hdr, MANA_REGISTER_FILTER, 1139 sizeof(resp)); 1140 if (err || resp.hdr.status) { 1141 netdev_err(apc->ndev, "Failed to register filter: %d, 0x%x\n", 1142 err, resp.hdr.status); 1143 return err ? err : -EPROTO; 1144 } 1145 1146 apc->pf_filter_handle = resp.filter_handle; 1147 return 0; 1148 } 1149 1150 static void mana_pf_deregister_filter(struct mana_port_context *apc) 1151 { 1152 struct mana_deregister_filter_resp resp = {}; 1153 struct mana_deregister_filter_req req = {}; 1154 int err; 1155 1156 mana_gd_init_req_hdr(&req.hdr, MANA_DEREGISTER_FILTER, 1157 sizeof(req), sizeof(resp)); 1158 req.filter_handle = apc->pf_filter_handle; 1159 1160 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1161 sizeof(resp)); 1162 if (err) { 1163 if (mana_en_need_log(apc, err)) 1164 netdev_err(apc->ndev, "Failed to unregister filter: %d\n", 1165 err); 1166 1167 return; 1168 } 1169 1170 err = mana_verify_resp_hdr(&resp.hdr, MANA_DEREGISTER_FILTER, 1171 sizeof(resp)); 1172 if (err || resp.hdr.status) 1173 netdev_err(apc->ndev, 1174 "Failed to deregister filter: %d, 0x%x\n", 1175 err, resp.hdr.status); 1176 } 1177 1178 static int mana_query_device_cfg(struct mana_context *ac, u32 proto_major_ver, 1179 u32 proto_minor_ver, u32 proto_micro_ver, 1180 u16 *max_num_vports, u8 *bm_hostmode) 1181 { 1182 struct gdma_context *gc = ac->gdma_dev->gdma_context; 1183 struct mana_query_device_cfg_resp resp = {}; 1184 struct mana_query_device_cfg_req req = {}; 1185 struct device *dev = gc->dev; 1186 int err = 0; 1187 1188 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_DEV_CONFIG, 1189 sizeof(req), sizeof(resp)); 1190 1191 req.hdr.resp.msg_version = GDMA_MESSAGE_V3; 1192 1193 req.proto_major_ver = proto_major_ver; 1194 req.proto_minor_ver = proto_minor_ver; 1195 req.proto_micro_ver = proto_micro_ver; 1196 1197 err = mana_send_request(ac, &req, sizeof(req), &resp, sizeof(resp)); 1198 if (err) { 1199 dev_err(dev, "Failed to query config: %d", err); 1200 return err; 1201 } 1202 1203 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_DEV_CONFIG, 1204 sizeof(resp)); 1205 if (err || resp.hdr.status) { 1206 dev_err(dev, "Invalid query result: %d, 0x%x\n", err, 1207 resp.hdr.status); 1208 if (!err) 1209 err = -EPROTO; 1210 return err; 1211 } 1212 1213 *max_num_vports = resp.max_num_vports; 1214 1215 if (resp.hdr.response.msg_version >= GDMA_MESSAGE_V2) 1216 gc->adapter_mtu = resp.adapter_mtu; 1217 else 1218 gc->adapter_mtu = ETH_FRAME_LEN; 1219 1220 if (resp.hdr.response.msg_version >= GDMA_MESSAGE_V3) 1221 *bm_hostmode = resp.bm_hostmode; 1222 else 1223 *bm_hostmode = 0; 1224 1225 debugfs_create_u16("adapter-MTU", 0400, gc->mana_pci_debugfs, &gc->adapter_mtu); 1226 1227 return 0; 1228 } 1229 1230 static int mana_query_vport_cfg(struct mana_port_context *apc, u32 vport_index, 1231 u32 *max_sq, u32 *max_rq, u32 *num_indir_entry) 1232 { 1233 struct mana_query_vport_cfg_resp resp = {}; 1234 struct mana_query_vport_cfg_req req = {}; 1235 int err; 1236 1237 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_VPORT_CONFIG, 1238 sizeof(req), sizeof(resp)); 1239 1240 req.vport_index = vport_index; 1241 1242 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1243 sizeof(resp)); 1244 if (err) 1245 return err; 1246 1247 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_VPORT_CONFIG, 1248 sizeof(resp)); 1249 if (err) 1250 return err; 1251 1252 if (resp.hdr.status) 1253 return -EPROTO; 1254 1255 *max_sq = resp.max_num_sq; 1256 *max_rq = resp.max_num_rq; 1257 if (resp.num_indirection_ent > 0 && 1258 resp.num_indirection_ent <= MANA_INDIRECT_TABLE_MAX_SIZE && 1259 is_power_of_2(resp.num_indirection_ent)) { 1260 *num_indir_entry = resp.num_indirection_ent; 1261 } else { 1262 netdev_warn(apc->ndev, 1263 "Setting indirection table size to default %d for vPort %d\n", 1264 MANA_INDIRECT_TABLE_DEF_SIZE, apc->port_idx); 1265 *num_indir_entry = MANA_INDIRECT_TABLE_DEF_SIZE; 1266 } 1267 1268 apc->port_handle = resp.vport; 1269 ether_addr_copy(apc->mac_addr, resp.mac_addr); 1270 1271 return 0; 1272 } 1273 1274 void mana_uncfg_vport(struct mana_port_context *apc) 1275 { 1276 mutex_lock(&apc->vport_mutex); 1277 apc->vport_use_count--; 1278 WARN_ON(apc->vport_use_count < 0); 1279 mutex_unlock(&apc->vport_mutex); 1280 } 1281 EXPORT_SYMBOL_NS(mana_uncfg_vport, "NET_MANA"); 1282 1283 int mana_cfg_vport(struct mana_port_context *apc, u32 protection_dom_id, 1284 u32 doorbell_pg_id) 1285 { 1286 struct mana_config_vport_resp resp = {}; 1287 struct mana_config_vport_req req = {}; 1288 int err; 1289 1290 /* This function is used to program the Ethernet port in the hardware 1291 * table. It can be called from the Ethernet driver or the RDMA driver. 1292 * 1293 * For Ethernet usage, the hardware supports only one active user on a 1294 * physical port. The driver checks on the port usage before programming 1295 * the hardware when creating the RAW QP (RDMA driver) or exposing the 1296 * device to kernel NET layer (Ethernet driver). 1297 * 1298 * Because the RDMA driver doesn't know in advance which QP type the 1299 * user will create, it exposes the device with all its ports. The user 1300 * may not be able to create RAW QP on a port if this port is already 1301 * in used by the Ethernet driver from the kernel. 1302 * 1303 * This physical port limitation only applies to the RAW QP. For RC QP, 1304 * the hardware doesn't have this limitation. The user can create RC 1305 * QPs on a physical port up to the hardware limits independent of the 1306 * Ethernet usage on the same port. 1307 */ 1308 mutex_lock(&apc->vport_mutex); 1309 if (apc->vport_use_count > 0) { 1310 mutex_unlock(&apc->vport_mutex); 1311 return -EBUSY; 1312 } 1313 apc->vport_use_count++; 1314 mutex_unlock(&apc->vport_mutex); 1315 1316 mana_gd_init_req_hdr(&req.hdr, MANA_CONFIG_VPORT_TX, 1317 sizeof(req), sizeof(resp)); 1318 req.vport = apc->port_handle; 1319 req.pdid = protection_dom_id; 1320 req.doorbell_pageid = doorbell_pg_id; 1321 1322 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1323 sizeof(resp)); 1324 if (err) { 1325 netdev_err(apc->ndev, "Failed to configure vPort: %d\n", err); 1326 goto out; 1327 } 1328 1329 err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_TX, 1330 sizeof(resp)); 1331 if (err || resp.hdr.status) { 1332 netdev_err(apc->ndev, "Failed to configure vPort: %d, 0x%x\n", 1333 err, resp.hdr.status); 1334 if (!err) 1335 err = -EPROTO; 1336 1337 goto out; 1338 } 1339 1340 apc->tx_shortform_allowed = resp.short_form_allowed; 1341 apc->tx_vp_offset = resp.tx_vport_offset; 1342 1343 netdev_info(apc->ndev, "Configured vPort %llu PD %u DB %u\n", 1344 apc->port_handle, protection_dom_id, doorbell_pg_id); 1345 out: 1346 if (err) 1347 mana_uncfg_vport(apc); 1348 1349 return err; 1350 } 1351 EXPORT_SYMBOL_NS(mana_cfg_vport, "NET_MANA"); 1352 1353 static int mana_cfg_vport_steering(struct mana_port_context *apc, 1354 enum TRI_STATE rx, 1355 bool update_default_rxobj, bool update_key, 1356 bool update_tab) 1357 { 1358 struct mana_cfg_rx_steer_req_v2 *req; 1359 struct mana_cfg_rx_steer_resp resp = {}; 1360 struct net_device *ndev = apc->ndev; 1361 u32 req_buf_size; 1362 int err; 1363 1364 req_buf_size = struct_size(req, indir_tab, apc->indir_table_sz); 1365 req = kzalloc(req_buf_size, GFP_KERNEL); 1366 if (!req) 1367 return -ENOMEM; 1368 1369 mana_gd_init_req_hdr(&req->hdr, MANA_CONFIG_VPORT_RX, req_buf_size, 1370 sizeof(resp)); 1371 1372 req->hdr.req.msg_version = GDMA_MESSAGE_V2; 1373 1374 req->vport = apc->port_handle; 1375 req->num_indir_entries = apc->indir_table_sz; 1376 req->indir_tab_offset = offsetof(struct mana_cfg_rx_steer_req_v2, 1377 indir_tab); 1378 req->rx_enable = rx; 1379 req->rss_enable = apc->rss_state; 1380 req->update_default_rxobj = update_default_rxobj; 1381 req->update_hashkey = update_key; 1382 req->update_indir_tab = update_tab; 1383 req->default_rxobj = apc->default_rxobj; 1384 req->cqe_coalescing_enable = 0; 1385 1386 if (update_key) 1387 memcpy(&req->hashkey, apc->hashkey, MANA_HASH_KEY_SIZE); 1388 1389 if (update_tab) 1390 memcpy(req->indir_tab, apc->rxobj_table, 1391 flex_array_size(req, indir_tab, req->num_indir_entries)); 1392 1393 err = mana_send_request(apc->ac, req, req_buf_size, &resp, 1394 sizeof(resp)); 1395 if (err) { 1396 if (mana_en_need_log(apc, err)) 1397 netdev_err(ndev, "Failed to configure vPort RX: %d\n", err); 1398 1399 goto out; 1400 } 1401 1402 err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_RX, 1403 sizeof(resp)); 1404 if (err) { 1405 netdev_err(ndev, "vPort RX configuration failed: %d\n", err); 1406 goto out; 1407 } 1408 1409 if (resp.hdr.status) { 1410 netdev_err(ndev, "vPort RX configuration failed: 0x%x\n", 1411 resp.hdr.status); 1412 err = -EPROTO; 1413 } 1414 1415 netdev_info(ndev, "Configured steering vPort %llu entries %u\n", 1416 apc->port_handle, apc->indir_table_sz); 1417 out: 1418 kfree(req); 1419 return err; 1420 } 1421 1422 int mana_query_link_cfg(struct mana_port_context *apc) 1423 { 1424 struct net_device *ndev = apc->ndev; 1425 struct mana_query_link_config_resp resp = {}; 1426 struct mana_query_link_config_req req = {}; 1427 int err; 1428 1429 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_LINK_CONFIG, 1430 sizeof(req), sizeof(resp)); 1431 1432 req.vport = apc->port_handle; 1433 req.hdr.resp.msg_version = GDMA_MESSAGE_V2; 1434 1435 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1436 sizeof(resp)); 1437 1438 if (err) { 1439 if (err == -EOPNOTSUPP) { 1440 netdev_info_once(ndev, "MANA_QUERY_LINK_CONFIG not supported\n"); 1441 return err; 1442 } 1443 netdev_err(ndev, "Failed to query link config: %d\n", err); 1444 return err; 1445 } 1446 1447 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_LINK_CONFIG, 1448 sizeof(resp)); 1449 1450 if (err || resp.hdr.status) { 1451 netdev_err(ndev, "Failed to query link config: %d, 0x%x\n", err, 1452 resp.hdr.status); 1453 if (!err) 1454 err = -EOPNOTSUPP; 1455 return err; 1456 } 1457 1458 if (resp.qos_unconfigured) { 1459 err = -EINVAL; 1460 return err; 1461 } 1462 apc->speed = resp.link_speed_mbps; 1463 apc->max_speed = resp.qos_speed_mbps; 1464 return 0; 1465 } 1466 1467 int mana_set_bw_clamp(struct mana_port_context *apc, u32 speed, 1468 int enable_clamping) 1469 { 1470 struct mana_set_bw_clamp_resp resp = {}; 1471 struct mana_set_bw_clamp_req req = {}; 1472 struct net_device *ndev = apc->ndev; 1473 int err; 1474 1475 mana_gd_init_req_hdr(&req.hdr, MANA_SET_BW_CLAMP, 1476 sizeof(req), sizeof(resp)); 1477 req.vport = apc->port_handle; 1478 req.link_speed_mbps = speed; 1479 req.enable_clamping = enable_clamping; 1480 1481 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1482 sizeof(resp)); 1483 1484 if (err) { 1485 if (err == -EOPNOTSUPP) { 1486 netdev_info_once(ndev, "MANA_SET_BW_CLAMP not supported\n"); 1487 return err; 1488 } 1489 netdev_err(ndev, "Failed to set bandwidth clamp for speed %u, err = %d", 1490 speed, err); 1491 return err; 1492 } 1493 1494 err = mana_verify_resp_hdr(&resp.hdr, MANA_SET_BW_CLAMP, 1495 sizeof(resp)); 1496 1497 if (err || resp.hdr.status) { 1498 netdev_err(ndev, "Failed to set bandwidth clamp: %d, 0x%x\n", err, 1499 resp.hdr.status); 1500 if (!err) 1501 err = -EOPNOTSUPP; 1502 return err; 1503 } 1504 1505 if (resp.qos_unconfigured) 1506 netdev_info(ndev, "QoS is unconfigured\n"); 1507 1508 return 0; 1509 } 1510 1511 int mana_create_wq_obj(struct mana_port_context *apc, 1512 mana_handle_t vport, 1513 u32 wq_type, struct mana_obj_spec *wq_spec, 1514 struct mana_obj_spec *cq_spec, 1515 mana_handle_t *wq_obj) 1516 { 1517 struct mana_create_wqobj_resp resp = {}; 1518 struct mana_create_wqobj_req req = {}; 1519 struct net_device *ndev = apc->ndev; 1520 int err; 1521 1522 mana_gd_init_req_hdr(&req.hdr, MANA_CREATE_WQ_OBJ, 1523 sizeof(req), sizeof(resp)); 1524 req.vport = vport; 1525 req.wq_type = wq_type; 1526 req.wq_gdma_region = wq_spec->gdma_region; 1527 req.cq_gdma_region = cq_spec->gdma_region; 1528 req.wq_size = wq_spec->queue_size; 1529 req.cq_size = cq_spec->queue_size; 1530 req.cq_moderation_ctx_id = cq_spec->modr_ctx_id; 1531 req.cq_parent_qid = cq_spec->attached_eq; 1532 1533 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1534 sizeof(resp)); 1535 if (err) { 1536 netdev_err(ndev, "Failed to create WQ object: %d\n", err); 1537 goto out; 1538 } 1539 1540 err = mana_verify_resp_hdr(&resp.hdr, MANA_CREATE_WQ_OBJ, 1541 sizeof(resp)); 1542 if (err || resp.hdr.status) { 1543 netdev_err(ndev, "Failed to create WQ object: %d, 0x%x\n", err, 1544 resp.hdr.status); 1545 if (!err) 1546 err = -EPROTO; 1547 goto out; 1548 } 1549 1550 if (resp.wq_obj == INVALID_MANA_HANDLE) { 1551 netdev_err(ndev, "Got an invalid WQ object handle\n"); 1552 err = -EPROTO; 1553 goto out; 1554 } 1555 1556 *wq_obj = resp.wq_obj; 1557 wq_spec->queue_index = resp.wq_id; 1558 cq_spec->queue_index = resp.cq_id; 1559 1560 return 0; 1561 out: 1562 return err; 1563 } 1564 EXPORT_SYMBOL_NS(mana_create_wq_obj, "NET_MANA"); 1565 1566 void mana_destroy_wq_obj(struct mana_port_context *apc, u32 wq_type, 1567 mana_handle_t wq_obj) 1568 { 1569 struct mana_destroy_wqobj_resp resp = {}; 1570 struct mana_destroy_wqobj_req req = {}; 1571 struct net_device *ndev = apc->ndev; 1572 int err; 1573 1574 mana_gd_init_req_hdr(&req.hdr, MANA_DESTROY_WQ_OBJ, 1575 sizeof(req), sizeof(resp)); 1576 req.wq_type = wq_type; 1577 req.wq_obj_handle = wq_obj; 1578 1579 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1580 sizeof(resp)); 1581 if (err) { 1582 if (mana_en_need_log(apc, err)) 1583 netdev_err(ndev, "Failed to destroy WQ object: %d\n", err); 1584 1585 return; 1586 } 1587 1588 err = mana_verify_resp_hdr(&resp.hdr, MANA_DESTROY_WQ_OBJ, 1589 sizeof(resp)); 1590 if (err || resp.hdr.status) 1591 netdev_err(ndev, "Failed to destroy WQ object: %d, 0x%x\n", err, 1592 resp.hdr.status); 1593 } 1594 EXPORT_SYMBOL_NS(mana_destroy_wq_obj, "NET_MANA"); 1595 1596 static void mana_destroy_eq(struct mana_context *ac) 1597 { 1598 struct gdma_context *gc = ac->gdma_dev->gdma_context; 1599 struct gdma_queue *eq; 1600 int i; 1601 1602 if (!ac->eqs) 1603 return; 1604 1605 debugfs_remove_recursive(ac->mana_eqs_debugfs); 1606 ac->mana_eqs_debugfs = NULL; 1607 1608 for (i = 0; i < gc->max_num_queues; i++) { 1609 eq = ac->eqs[i].eq; 1610 if (!eq) 1611 continue; 1612 1613 mana_gd_destroy_queue(gc, eq); 1614 } 1615 1616 kfree(ac->eqs); 1617 ac->eqs = NULL; 1618 } 1619 1620 static void mana_create_eq_debugfs(struct mana_context *ac, int i) 1621 { 1622 struct mana_eq eq = ac->eqs[i]; 1623 char eqnum[32]; 1624 1625 sprintf(eqnum, "eq%d", i); 1626 eq.mana_eq_debugfs = debugfs_create_dir(eqnum, ac->mana_eqs_debugfs); 1627 debugfs_create_u32("head", 0400, eq.mana_eq_debugfs, &eq.eq->head); 1628 debugfs_create_u32("tail", 0400, eq.mana_eq_debugfs, &eq.eq->tail); 1629 debugfs_create_file("eq_dump", 0400, eq.mana_eq_debugfs, eq.eq, &mana_dbg_q_fops); 1630 } 1631 1632 static int mana_create_eq(struct mana_context *ac) 1633 { 1634 struct gdma_dev *gd = ac->gdma_dev; 1635 struct gdma_context *gc = gd->gdma_context; 1636 struct gdma_queue_spec spec = {}; 1637 int err; 1638 int i; 1639 1640 ac->eqs = kcalloc(gc->max_num_queues, sizeof(struct mana_eq), 1641 GFP_KERNEL); 1642 if (!ac->eqs) 1643 return -ENOMEM; 1644 1645 spec.type = GDMA_EQ; 1646 spec.monitor_avl_buf = false; 1647 spec.queue_size = EQ_SIZE; 1648 spec.eq.callback = NULL; 1649 spec.eq.context = ac->eqs; 1650 spec.eq.log2_throttle_limit = LOG2_EQ_THROTTLE; 1651 1652 ac->mana_eqs_debugfs = debugfs_create_dir("EQs", gc->mana_pci_debugfs); 1653 1654 for (i = 0; i < gc->max_num_queues; i++) { 1655 spec.eq.msix_index = (i + 1) % gc->num_msix_usable; 1656 err = mana_gd_create_mana_eq(gd, &spec, &ac->eqs[i].eq); 1657 if (err) { 1658 dev_err(gc->dev, "Failed to create EQ %d : %d\n", i, err); 1659 goto out; 1660 } 1661 mana_create_eq_debugfs(ac, i); 1662 } 1663 1664 return 0; 1665 out: 1666 mana_destroy_eq(ac); 1667 return err; 1668 } 1669 1670 static int mana_fence_rq(struct mana_port_context *apc, struct mana_rxq *rxq) 1671 { 1672 struct mana_fence_rq_resp resp = {}; 1673 struct mana_fence_rq_req req = {}; 1674 int err; 1675 1676 init_completion(&rxq->fence_event); 1677 1678 mana_gd_init_req_hdr(&req.hdr, MANA_FENCE_RQ, 1679 sizeof(req), sizeof(resp)); 1680 req.wq_obj_handle = rxq->rxobj; 1681 1682 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1683 sizeof(resp)); 1684 if (err) { 1685 netdev_err(apc->ndev, "Failed to fence RQ %u: %d\n", 1686 rxq->rxq_idx, err); 1687 return err; 1688 } 1689 1690 err = mana_verify_resp_hdr(&resp.hdr, MANA_FENCE_RQ, sizeof(resp)); 1691 if (err || resp.hdr.status) { 1692 netdev_err(apc->ndev, "Failed to fence RQ %u: %d, 0x%x\n", 1693 rxq->rxq_idx, err, resp.hdr.status); 1694 if (!err) 1695 err = -EPROTO; 1696 1697 return err; 1698 } 1699 1700 if (wait_for_completion_timeout(&rxq->fence_event, 10 * HZ) == 0) { 1701 netdev_err(apc->ndev, "Failed to fence RQ %u: timed out\n", 1702 rxq->rxq_idx); 1703 return -ETIMEDOUT; 1704 } 1705 1706 return 0; 1707 } 1708 1709 static void mana_fence_rqs(struct mana_port_context *apc) 1710 { 1711 unsigned int rxq_idx; 1712 struct mana_rxq *rxq; 1713 int err; 1714 1715 for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) { 1716 rxq = apc->rxqs[rxq_idx]; 1717 err = mana_fence_rq(apc, rxq); 1718 1719 /* In case of any error, use sleep instead. */ 1720 if (err) 1721 msleep(100); 1722 } 1723 } 1724 1725 static int mana_move_wq_tail(struct gdma_queue *wq, u32 num_units) 1726 { 1727 u32 used_space_old; 1728 u32 used_space_new; 1729 1730 used_space_old = wq->head - wq->tail; 1731 used_space_new = wq->head - (wq->tail + num_units); 1732 1733 if (WARN_ON_ONCE(used_space_new > used_space_old)) 1734 return -ERANGE; 1735 1736 wq->tail += num_units; 1737 return 0; 1738 } 1739 1740 void mana_unmap_skb(struct sk_buff *skb, struct mana_port_context *apc) 1741 { 1742 struct mana_skb_head *ash = (struct mana_skb_head *)skb->head; 1743 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context; 1744 struct device *dev = gc->dev; 1745 int hsg, i; 1746 1747 /* Number of SGEs of linear part */ 1748 hsg = (skb_is_gso(skb) && skb_headlen(skb) > ash->size[0]) ? 2 : 1; 1749 1750 for (i = 0; i < hsg; i++) 1751 dma_unmap_single(dev, ash->dma_handle[i], ash->size[i], 1752 DMA_TO_DEVICE); 1753 1754 for (i = hsg; i < skb_shinfo(skb)->nr_frags + hsg; i++) 1755 dma_unmap_page(dev, ash->dma_handle[i], ash->size[i], 1756 DMA_TO_DEVICE); 1757 } 1758 1759 static void mana_poll_tx_cq(struct mana_cq *cq) 1760 { 1761 struct gdma_comp *completions = cq->gdma_comp_buf; 1762 struct gdma_posted_wqe_info *wqe_info; 1763 unsigned int pkt_transmitted = 0; 1764 unsigned int wqe_unit_cnt = 0; 1765 struct mana_txq *txq = cq->txq; 1766 struct mana_port_context *apc; 1767 struct netdev_queue *net_txq; 1768 struct gdma_queue *gdma_wq; 1769 unsigned int avail_space; 1770 struct net_device *ndev; 1771 struct sk_buff *skb; 1772 bool txq_stopped; 1773 int comp_read; 1774 int i; 1775 1776 ndev = txq->ndev; 1777 apc = netdev_priv(ndev); 1778 1779 comp_read = mana_gd_poll_cq(cq->gdma_cq, completions, 1780 CQE_POLLING_BUFFER); 1781 1782 if (comp_read < 1) 1783 return; 1784 1785 for (i = 0; i < comp_read; i++) { 1786 struct mana_tx_comp_oob *cqe_oob; 1787 1788 if (WARN_ON_ONCE(!completions[i].is_sq)) 1789 return; 1790 1791 cqe_oob = (struct mana_tx_comp_oob *)completions[i].cqe_data; 1792 if (WARN_ON_ONCE(cqe_oob->cqe_hdr.client_type != 1793 MANA_CQE_COMPLETION)) 1794 return; 1795 1796 switch (cqe_oob->cqe_hdr.cqe_type) { 1797 case CQE_TX_OKAY: 1798 break; 1799 1800 case CQE_TX_SA_DROP: 1801 case CQE_TX_MTU_DROP: 1802 case CQE_TX_INVALID_OOB: 1803 case CQE_TX_INVALID_ETH_TYPE: 1804 case CQE_TX_HDR_PROCESSING_ERROR: 1805 case CQE_TX_VF_DISABLED: 1806 case CQE_TX_VPORT_IDX_OUT_OF_RANGE: 1807 case CQE_TX_VPORT_DISABLED: 1808 case CQE_TX_VLAN_TAGGING_VIOLATION: 1809 if (net_ratelimit()) 1810 netdev_err(ndev, "TX: CQE error %d\n", 1811 cqe_oob->cqe_hdr.cqe_type); 1812 1813 apc->eth_stats.tx_cqe_err++; 1814 break; 1815 1816 default: 1817 /* If the CQE type is unknown, log an error, 1818 * and still free the SKB, update tail, etc. 1819 */ 1820 if (net_ratelimit()) 1821 netdev_err(ndev, "TX: unknown CQE type %d\n", 1822 cqe_oob->cqe_hdr.cqe_type); 1823 1824 apc->eth_stats.tx_cqe_unknown_type++; 1825 break; 1826 } 1827 1828 if (WARN_ON_ONCE(txq->gdma_txq_id != completions[i].wq_num)) 1829 return; 1830 1831 skb = skb_dequeue(&txq->pending_skbs); 1832 if (WARN_ON_ONCE(!skb)) 1833 return; 1834 1835 wqe_info = (struct gdma_posted_wqe_info *)skb->cb; 1836 wqe_unit_cnt += wqe_info->wqe_size_in_bu; 1837 1838 mana_unmap_skb(skb, apc); 1839 1840 napi_consume_skb(skb, cq->budget); 1841 1842 pkt_transmitted++; 1843 } 1844 1845 if (WARN_ON_ONCE(wqe_unit_cnt == 0)) 1846 return; 1847 1848 mana_move_wq_tail(txq->gdma_sq, wqe_unit_cnt); 1849 1850 gdma_wq = txq->gdma_sq; 1851 avail_space = mana_gd_wq_avail_space(gdma_wq); 1852 1853 /* Ensure tail updated before checking q stop */ 1854 smp_mb(); 1855 1856 net_txq = txq->net_txq; 1857 txq_stopped = netif_tx_queue_stopped(net_txq); 1858 1859 /* Ensure checking txq_stopped before apc->port_is_up. */ 1860 smp_rmb(); 1861 1862 if (txq_stopped && apc->port_is_up && avail_space >= MAX_TX_WQE_SIZE) { 1863 netif_tx_wake_queue(net_txq); 1864 apc->eth_stats.wake_queue++; 1865 } 1866 1867 if (atomic_sub_return(pkt_transmitted, &txq->pending_sends) < 0) 1868 WARN_ON_ONCE(1); 1869 1870 cq->work_done = pkt_transmitted; 1871 } 1872 1873 static void mana_post_pkt_rxq(struct mana_rxq *rxq) 1874 { 1875 struct mana_recv_buf_oob *recv_buf_oob; 1876 u32 curr_index; 1877 int err; 1878 1879 curr_index = rxq->buf_index++; 1880 if (rxq->buf_index == rxq->num_rx_buf) 1881 rxq->buf_index = 0; 1882 1883 recv_buf_oob = &rxq->rx_oobs[curr_index]; 1884 1885 err = mana_gd_post_work_request(rxq->gdma_rq, &recv_buf_oob->wqe_req, 1886 &recv_buf_oob->wqe_inf); 1887 if (WARN_ON_ONCE(err)) 1888 return; 1889 1890 WARN_ON_ONCE(recv_buf_oob->wqe_inf.wqe_size_in_bu != 1); 1891 } 1892 1893 static struct sk_buff *mana_build_skb(struct mana_rxq *rxq, void *buf_va, 1894 uint pkt_len, struct xdp_buff *xdp) 1895 { 1896 struct sk_buff *skb = napi_build_skb(buf_va, rxq->alloc_size); 1897 1898 if (!skb) 1899 return NULL; 1900 1901 if (xdp->data_hard_start) { 1902 u32 metasize = xdp->data - xdp->data_meta; 1903 1904 skb_reserve(skb, xdp->data - xdp->data_hard_start); 1905 skb_put(skb, xdp->data_end - xdp->data); 1906 if (metasize) 1907 skb_metadata_set(skb, metasize); 1908 return skb; 1909 } 1910 1911 skb_reserve(skb, rxq->headroom); 1912 skb_put(skb, pkt_len); 1913 1914 return skb; 1915 } 1916 1917 static void mana_rx_skb(void *buf_va, bool from_pool, 1918 struct mana_rxcomp_oob *cqe, struct mana_rxq *rxq) 1919 { 1920 struct mana_stats_rx *rx_stats = &rxq->stats; 1921 struct net_device *ndev = rxq->ndev; 1922 uint pkt_len = cqe->ppi[0].pkt_len; 1923 u16 rxq_idx = rxq->rxq_idx; 1924 struct napi_struct *napi; 1925 struct xdp_buff xdp = {}; 1926 struct sk_buff *skb; 1927 u32 hash_value; 1928 u32 act; 1929 1930 rxq->rx_cq.work_done++; 1931 napi = &rxq->rx_cq.napi; 1932 1933 if (!buf_va) { 1934 ++ndev->stats.rx_dropped; 1935 return; 1936 } 1937 1938 act = mana_run_xdp(ndev, rxq, &xdp, buf_va, pkt_len); 1939 1940 if (act == XDP_REDIRECT && !rxq->xdp_rc) 1941 return; 1942 1943 if (act != XDP_PASS && act != XDP_TX) 1944 goto drop_xdp; 1945 1946 skb = mana_build_skb(rxq, buf_va, pkt_len, &xdp); 1947 1948 if (!skb) 1949 goto drop; 1950 1951 if (from_pool) 1952 skb_mark_for_recycle(skb); 1953 1954 skb->dev = napi->dev; 1955 1956 skb->protocol = eth_type_trans(skb, ndev); 1957 skb_checksum_none_assert(skb); 1958 skb_record_rx_queue(skb, rxq_idx); 1959 1960 if ((ndev->features & NETIF_F_RXCSUM) && cqe->rx_iphdr_csum_succeed) { 1961 if (cqe->rx_tcp_csum_succeed || cqe->rx_udp_csum_succeed) 1962 skb->ip_summed = CHECKSUM_UNNECESSARY; 1963 } 1964 1965 if (cqe->rx_hashtype != 0 && (ndev->features & NETIF_F_RXHASH)) { 1966 hash_value = cqe->ppi[0].pkt_hash; 1967 1968 if (cqe->rx_hashtype & MANA_HASH_L4) 1969 skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L4); 1970 else 1971 skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L3); 1972 } 1973 1974 if (cqe->rx_vlantag_present) { 1975 u16 vlan_tci = cqe->rx_vlan_id; 1976 1977 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci); 1978 } 1979 1980 u64_stats_update_begin(&rx_stats->syncp); 1981 rx_stats->packets++; 1982 rx_stats->bytes += pkt_len; 1983 1984 if (act == XDP_TX) 1985 rx_stats->xdp_tx++; 1986 u64_stats_update_end(&rx_stats->syncp); 1987 1988 if (act == XDP_TX) { 1989 skb_set_queue_mapping(skb, rxq_idx); 1990 mana_xdp_tx(skb, ndev); 1991 return; 1992 } 1993 1994 napi_gro_receive(napi, skb); 1995 1996 return; 1997 1998 drop_xdp: 1999 u64_stats_update_begin(&rx_stats->syncp); 2000 rx_stats->xdp_drop++; 2001 u64_stats_update_end(&rx_stats->syncp); 2002 2003 drop: 2004 if (from_pool) { 2005 if (rxq->frag_count == 1) 2006 page_pool_recycle_direct(rxq->page_pool, 2007 virt_to_head_page(buf_va)); 2008 else 2009 page_pool_free_va(rxq->page_pool, buf_va, true); 2010 } else { 2011 WARN_ON_ONCE(rxq->xdp_save_va); 2012 /* Save for reuse */ 2013 rxq->xdp_save_va = buf_va; 2014 } 2015 2016 ++ndev->stats.rx_dropped; 2017 2018 return; 2019 } 2020 2021 static void *mana_get_rxfrag(struct mana_rxq *rxq, struct device *dev, 2022 dma_addr_t *da, bool *from_pool) 2023 { 2024 struct page *page; 2025 u32 offset; 2026 void *va; 2027 *from_pool = false; 2028 2029 /* Don't use fragments for jumbo frames or XDP where it's 1 fragment 2030 * per page. 2031 */ 2032 if (rxq->frag_count == 1) { 2033 /* Reuse XDP dropped page if available */ 2034 if (rxq->xdp_save_va) { 2035 va = rxq->xdp_save_va; 2036 page = virt_to_head_page(va); 2037 rxq->xdp_save_va = NULL; 2038 } else { 2039 page = page_pool_dev_alloc_pages(rxq->page_pool); 2040 if (!page) 2041 return NULL; 2042 2043 *from_pool = true; 2044 va = page_to_virt(page); 2045 } 2046 2047 *da = dma_map_single(dev, va + rxq->headroom, rxq->datasize, 2048 DMA_FROM_DEVICE); 2049 if (dma_mapping_error(dev, *da)) { 2050 mana_put_rx_page(rxq, page, *from_pool); 2051 return NULL; 2052 } 2053 2054 return va; 2055 } 2056 2057 page = page_pool_dev_alloc_frag(rxq->page_pool, &offset, 2058 rxq->alloc_size); 2059 if (!page) 2060 return NULL; 2061 2062 va = page_to_virt(page) + offset; 2063 *da = page_pool_get_dma_addr(page) + offset + rxq->headroom; 2064 *from_pool = true; 2065 2066 return va; 2067 } 2068 2069 /* Allocate frag for rx buffer, and save the old buf */ 2070 static void mana_refill_rx_oob(struct device *dev, struct mana_rxq *rxq, 2071 struct mana_recv_buf_oob *rxoob, void **old_buf, 2072 bool *old_fp) 2073 { 2074 bool from_pool; 2075 dma_addr_t da; 2076 void *va; 2077 2078 va = mana_get_rxfrag(rxq, dev, &da, &from_pool); 2079 if (!va) 2080 return; 2081 if (!rxoob->from_pool || rxq->frag_count == 1) 2082 dma_unmap_single(dev, rxoob->sgl[0].address, rxq->datasize, 2083 DMA_FROM_DEVICE); 2084 *old_buf = rxoob->buf_va; 2085 *old_fp = rxoob->from_pool; 2086 2087 rxoob->buf_va = va; 2088 rxoob->sgl[0].address = da; 2089 rxoob->from_pool = from_pool; 2090 } 2091 2092 static void mana_process_rx_cqe(struct mana_rxq *rxq, struct mana_cq *cq, 2093 struct gdma_comp *cqe) 2094 { 2095 struct mana_rxcomp_oob *oob = (struct mana_rxcomp_oob *)cqe->cqe_data; 2096 struct gdma_context *gc = rxq->gdma_rq->gdma_dev->gdma_context; 2097 struct net_device *ndev = rxq->ndev; 2098 struct mana_recv_buf_oob *rxbuf_oob; 2099 struct mana_port_context *apc; 2100 struct device *dev = gc->dev; 2101 void *old_buf = NULL; 2102 u32 curr, pktlen; 2103 bool old_fp; 2104 2105 apc = netdev_priv(ndev); 2106 2107 switch (oob->cqe_hdr.cqe_type) { 2108 case CQE_RX_OKAY: 2109 break; 2110 2111 case CQE_RX_TRUNCATED: 2112 ++ndev->stats.rx_dropped; 2113 rxbuf_oob = &rxq->rx_oobs[rxq->buf_index]; 2114 netdev_warn_once(ndev, "Dropped a truncated packet\n"); 2115 goto drop; 2116 2117 case CQE_RX_COALESCED_4: 2118 netdev_err(ndev, "RX coalescing is unsupported\n"); 2119 apc->eth_stats.rx_coalesced_err++; 2120 return; 2121 2122 case CQE_RX_OBJECT_FENCE: 2123 complete(&rxq->fence_event); 2124 return; 2125 2126 default: 2127 netdev_err(ndev, "Unknown RX CQE type = %d\n", 2128 oob->cqe_hdr.cqe_type); 2129 apc->eth_stats.rx_cqe_unknown_type++; 2130 return; 2131 } 2132 2133 pktlen = oob->ppi[0].pkt_len; 2134 2135 if (pktlen == 0) { 2136 /* data packets should never have packetlength of zero */ 2137 netdev_err(ndev, "RX pkt len=0, rq=%u, cq=%u, rxobj=0x%llx\n", 2138 rxq->gdma_id, cq->gdma_id, rxq->rxobj); 2139 return; 2140 } 2141 2142 curr = rxq->buf_index; 2143 rxbuf_oob = &rxq->rx_oobs[curr]; 2144 WARN_ON_ONCE(rxbuf_oob->wqe_inf.wqe_size_in_bu != 1); 2145 2146 mana_refill_rx_oob(dev, rxq, rxbuf_oob, &old_buf, &old_fp); 2147 2148 /* Unsuccessful refill will have old_buf == NULL. 2149 * In this case, mana_rx_skb() will drop the packet. 2150 */ 2151 mana_rx_skb(old_buf, old_fp, oob, rxq); 2152 2153 drop: 2154 mana_move_wq_tail(rxq->gdma_rq, rxbuf_oob->wqe_inf.wqe_size_in_bu); 2155 2156 mana_post_pkt_rxq(rxq); 2157 } 2158 2159 static void mana_poll_rx_cq(struct mana_cq *cq) 2160 { 2161 struct gdma_comp *comp = cq->gdma_comp_buf; 2162 struct mana_rxq *rxq = cq->rxq; 2163 int comp_read, i; 2164 2165 comp_read = mana_gd_poll_cq(cq->gdma_cq, comp, CQE_POLLING_BUFFER); 2166 WARN_ON_ONCE(comp_read > CQE_POLLING_BUFFER); 2167 2168 rxq->xdp_flush = false; 2169 2170 for (i = 0; i < comp_read; i++) { 2171 if (WARN_ON_ONCE(comp[i].is_sq)) 2172 return; 2173 2174 /* verify recv cqe references the right rxq */ 2175 if (WARN_ON_ONCE(comp[i].wq_num != cq->rxq->gdma_id)) 2176 return; 2177 2178 mana_process_rx_cqe(rxq, cq, &comp[i]); 2179 } 2180 2181 if (comp_read > 0) { 2182 struct gdma_context *gc = rxq->gdma_rq->gdma_dev->gdma_context; 2183 2184 mana_gd_wq_ring_doorbell(gc, rxq->gdma_rq); 2185 } 2186 2187 if (rxq->xdp_flush) 2188 xdp_do_flush(); 2189 } 2190 2191 static int mana_cq_handler(void *context, struct gdma_queue *gdma_queue) 2192 { 2193 struct mana_cq *cq = context; 2194 int w; 2195 2196 WARN_ON_ONCE(cq->gdma_cq != gdma_queue); 2197 2198 if (cq->type == MANA_CQ_TYPE_RX) 2199 mana_poll_rx_cq(cq); 2200 else 2201 mana_poll_tx_cq(cq); 2202 2203 w = cq->work_done; 2204 cq->work_done_since_doorbell += w; 2205 2206 if (w < cq->budget) { 2207 mana_gd_ring_cq(gdma_queue, SET_ARM_BIT); 2208 cq->work_done_since_doorbell = 0; 2209 napi_complete_done(&cq->napi, w); 2210 } else if (cq->work_done_since_doorbell > 2211 cq->gdma_cq->queue_size / COMP_ENTRY_SIZE * 4) { 2212 /* MANA hardware requires at least one doorbell ring every 8 2213 * wraparounds of CQ even if there is no need to arm the CQ. 2214 * This driver rings the doorbell as soon as we have exceeded 2215 * 4 wraparounds. 2216 */ 2217 mana_gd_ring_cq(gdma_queue, 0); 2218 cq->work_done_since_doorbell = 0; 2219 } 2220 2221 return w; 2222 } 2223 2224 static int mana_poll(struct napi_struct *napi, int budget) 2225 { 2226 struct mana_cq *cq = container_of(napi, struct mana_cq, napi); 2227 int w; 2228 2229 cq->work_done = 0; 2230 cq->budget = budget; 2231 2232 w = mana_cq_handler(cq, cq->gdma_cq); 2233 2234 return min(w, budget); 2235 } 2236 2237 static void mana_schedule_napi(void *context, struct gdma_queue *gdma_queue) 2238 { 2239 struct mana_cq *cq = context; 2240 2241 napi_schedule_irqoff(&cq->napi); 2242 } 2243 2244 static void mana_deinit_cq(struct mana_port_context *apc, struct mana_cq *cq) 2245 { 2246 struct gdma_dev *gd = apc->ac->gdma_dev; 2247 2248 if (!cq->gdma_cq) 2249 return; 2250 2251 mana_gd_destroy_queue(gd->gdma_context, cq->gdma_cq); 2252 } 2253 2254 static void mana_deinit_txq(struct mana_port_context *apc, struct mana_txq *txq) 2255 { 2256 struct gdma_dev *gd = apc->ac->gdma_dev; 2257 2258 if (!txq->gdma_sq) 2259 return; 2260 2261 mana_gd_destroy_queue(gd->gdma_context, txq->gdma_sq); 2262 } 2263 2264 static void mana_destroy_txq(struct mana_port_context *apc) 2265 { 2266 struct napi_struct *napi; 2267 int i; 2268 2269 if (!apc->tx_qp) 2270 return; 2271 2272 for (i = 0; i < apc->num_queues; i++) { 2273 debugfs_remove_recursive(apc->tx_qp[i].mana_tx_debugfs); 2274 apc->tx_qp[i].mana_tx_debugfs = NULL; 2275 2276 napi = &apc->tx_qp[i].tx_cq.napi; 2277 if (apc->tx_qp[i].txq.napi_initialized) { 2278 napi_synchronize(napi); 2279 napi_disable_locked(napi); 2280 netif_napi_del_locked(napi); 2281 apc->tx_qp[i].txq.napi_initialized = false; 2282 } 2283 mana_destroy_wq_obj(apc, GDMA_SQ, apc->tx_qp[i].tx_object); 2284 2285 mana_deinit_cq(apc, &apc->tx_qp[i].tx_cq); 2286 2287 mana_deinit_txq(apc, &apc->tx_qp[i].txq); 2288 } 2289 2290 kfree(apc->tx_qp); 2291 apc->tx_qp = NULL; 2292 } 2293 2294 static void mana_create_txq_debugfs(struct mana_port_context *apc, int idx) 2295 { 2296 struct mana_tx_qp *tx_qp = &apc->tx_qp[idx]; 2297 char qnum[32]; 2298 2299 sprintf(qnum, "TX-%d", idx); 2300 tx_qp->mana_tx_debugfs = debugfs_create_dir(qnum, apc->mana_port_debugfs); 2301 debugfs_create_u32("sq_head", 0400, tx_qp->mana_tx_debugfs, 2302 &tx_qp->txq.gdma_sq->head); 2303 debugfs_create_u32("sq_tail", 0400, tx_qp->mana_tx_debugfs, 2304 &tx_qp->txq.gdma_sq->tail); 2305 debugfs_create_u32("sq_pend_skb_qlen", 0400, tx_qp->mana_tx_debugfs, 2306 &tx_qp->txq.pending_skbs.qlen); 2307 debugfs_create_u32("cq_head", 0400, tx_qp->mana_tx_debugfs, 2308 &tx_qp->tx_cq.gdma_cq->head); 2309 debugfs_create_u32("cq_tail", 0400, tx_qp->mana_tx_debugfs, 2310 &tx_qp->tx_cq.gdma_cq->tail); 2311 debugfs_create_u32("cq_budget", 0400, tx_qp->mana_tx_debugfs, 2312 &tx_qp->tx_cq.budget); 2313 debugfs_create_file("txq_dump", 0400, tx_qp->mana_tx_debugfs, 2314 tx_qp->txq.gdma_sq, &mana_dbg_q_fops); 2315 debugfs_create_file("cq_dump", 0400, tx_qp->mana_tx_debugfs, 2316 tx_qp->tx_cq.gdma_cq, &mana_dbg_q_fops); 2317 } 2318 2319 static int mana_create_txq(struct mana_port_context *apc, 2320 struct net_device *net) 2321 { 2322 struct mana_context *ac = apc->ac; 2323 struct gdma_dev *gd = ac->gdma_dev; 2324 struct mana_obj_spec wq_spec; 2325 struct mana_obj_spec cq_spec; 2326 struct gdma_queue_spec spec; 2327 struct gdma_context *gc; 2328 struct mana_txq *txq; 2329 struct mana_cq *cq; 2330 u32 txq_size; 2331 u32 cq_size; 2332 int err; 2333 int i; 2334 2335 apc->tx_qp = kcalloc(apc->num_queues, sizeof(struct mana_tx_qp), 2336 GFP_KERNEL); 2337 if (!apc->tx_qp) 2338 return -ENOMEM; 2339 2340 /* The minimum size of the WQE is 32 bytes, hence 2341 * apc->tx_queue_size represents the maximum number of WQEs 2342 * the SQ can store. This value is then used to size other queues 2343 * to prevent overflow. 2344 * Also note that the txq_size is always going to be MANA_PAGE_ALIGNED, 2345 * as min val of apc->tx_queue_size is 128 and that would make 2346 * txq_size 128*32 = 4096 and the other higher values of apc->tx_queue_size 2347 * are always power of two 2348 */ 2349 txq_size = apc->tx_queue_size * 32; 2350 2351 cq_size = apc->tx_queue_size * COMP_ENTRY_SIZE; 2352 2353 gc = gd->gdma_context; 2354 2355 for (i = 0; i < apc->num_queues; i++) { 2356 apc->tx_qp[i].tx_object = INVALID_MANA_HANDLE; 2357 2358 /* Create SQ */ 2359 txq = &apc->tx_qp[i].txq; 2360 2361 u64_stats_init(&txq->stats.syncp); 2362 txq->ndev = net; 2363 txq->net_txq = netdev_get_tx_queue(net, i); 2364 txq->vp_offset = apc->tx_vp_offset; 2365 txq->napi_initialized = false; 2366 skb_queue_head_init(&txq->pending_skbs); 2367 2368 memset(&spec, 0, sizeof(spec)); 2369 spec.type = GDMA_SQ; 2370 spec.monitor_avl_buf = true; 2371 spec.queue_size = txq_size; 2372 err = mana_gd_create_mana_wq_cq(gd, &spec, &txq->gdma_sq); 2373 if (err) 2374 goto out; 2375 2376 /* Create SQ's CQ */ 2377 cq = &apc->tx_qp[i].tx_cq; 2378 cq->type = MANA_CQ_TYPE_TX; 2379 2380 cq->txq = txq; 2381 2382 memset(&spec, 0, sizeof(spec)); 2383 spec.type = GDMA_CQ; 2384 spec.monitor_avl_buf = false; 2385 spec.queue_size = cq_size; 2386 spec.cq.callback = mana_schedule_napi; 2387 spec.cq.parent_eq = ac->eqs[i].eq; 2388 spec.cq.context = cq; 2389 err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq); 2390 if (err) 2391 goto out; 2392 2393 memset(&wq_spec, 0, sizeof(wq_spec)); 2394 memset(&cq_spec, 0, sizeof(cq_spec)); 2395 2396 wq_spec.gdma_region = txq->gdma_sq->mem_info.dma_region_handle; 2397 wq_spec.queue_size = txq->gdma_sq->queue_size; 2398 2399 cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle; 2400 cq_spec.queue_size = cq->gdma_cq->queue_size; 2401 cq_spec.modr_ctx_id = 0; 2402 cq_spec.attached_eq = cq->gdma_cq->cq.parent->id; 2403 2404 err = mana_create_wq_obj(apc, apc->port_handle, GDMA_SQ, 2405 &wq_spec, &cq_spec, 2406 &apc->tx_qp[i].tx_object); 2407 2408 if (err) 2409 goto out; 2410 2411 txq->gdma_sq->id = wq_spec.queue_index; 2412 cq->gdma_cq->id = cq_spec.queue_index; 2413 2414 txq->gdma_sq->mem_info.dma_region_handle = 2415 GDMA_INVALID_DMA_REGION; 2416 cq->gdma_cq->mem_info.dma_region_handle = 2417 GDMA_INVALID_DMA_REGION; 2418 2419 txq->gdma_txq_id = txq->gdma_sq->id; 2420 2421 cq->gdma_id = cq->gdma_cq->id; 2422 2423 if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) { 2424 err = -EINVAL; 2425 goto out; 2426 } 2427 2428 gc->cq_table[cq->gdma_id] = cq->gdma_cq; 2429 2430 mana_create_txq_debugfs(apc, i); 2431 2432 set_bit(NAPI_STATE_NO_BUSY_POLL, &cq->napi.state); 2433 netif_napi_add_locked(net, &cq->napi, mana_poll); 2434 napi_enable_locked(&cq->napi); 2435 txq->napi_initialized = true; 2436 2437 mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT); 2438 } 2439 2440 return 0; 2441 out: 2442 netdev_err(net, "Failed to create %d TX queues, %d\n", 2443 apc->num_queues, err); 2444 mana_destroy_txq(apc); 2445 return err; 2446 } 2447 2448 static void mana_destroy_rxq(struct mana_port_context *apc, 2449 struct mana_rxq *rxq, bool napi_initialized) 2450 2451 { 2452 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context; 2453 struct mana_recv_buf_oob *rx_oob; 2454 struct device *dev = gc->dev; 2455 struct napi_struct *napi; 2456 struct page *page; 2457 int i; 2458 2459 if (!rxq) 2460 return; 2461 2462 debugfs_remove_recursive(rxq->mana_rx_debugfs); 2463 rxq->mana_rx_debugfs = NULL; 2464 2465 napi = &rxq->rx_cq.napi; 2466 2467 if (napi_initialized) { 2468 napi_synchronize(napi); 2469 2470 napi_disable_locked(napi); 2471 netif_napi_del_locked(napi); 2472 } 2473 xdp_rxq_info_unreg(&rxq->xdp_rxq); 2474 2475 mana_destroy_wq_obj(apc, GDMA_RQ, rxq->rxobj); 2476 2477 mana_deinit_cq(apc, &rxq->rx_cq); 2478 2479 if (rxq->xdp_save_va) 2480 put_page(virt_to_head_page(rxq->xdp_save_va)); 2481 2482 for (i = 0; i < rxq->num_rx_buf; i++) { 2483 rx_oob = &rxq->rx_oobs[i]; 2484 2485 if (!rx_oob->buf_va) 2486 continue; 2487 2488 page = virt_to_head_page(rx_oob->buf_va); 2489 2490 if (rxq->frag_count == 1 || !rx_oob->from_pool) { 2491 dma_unmap_single(dev, rx_oob->sgl[0].address, 2492 rx_oob->sgl[0].size, DMA_FROM_DEVICE); 2493 mana_put_rx_page(rxq, page, rx_oob->from_pool); 2494 } else { 2495 page_pool_free_va(rxq->page_pool, rx_oob->buf_va, true); 2496 } 2497 2498 rx_oob->buf_va = NULL; 2499 } 2500 2501 page_pool_destroy(rxq->page_pool); 2502 2503 if (rxq->gdma_rq) 2504 mana_gd_destroy_queue(gc, rxq->gdma_rq); 2505 2506 kfree(rxq); 2507 } 2508 2509 static int mana_fill_rx_oob(struct mana_recv_buf_oob *rx_oob, u32 mem_key, 2510 struct mana_rxq *rxq, struct device *dev) 2511 { 2512 struct mana_port_context *mpc = netdev_priv(rxq->ndev); 2513 bool from_pool = false; 2514 dma_addr_t da; 2515 void *va; 2516 2517 if (mpc->rxbufs_pre) 2518 va = mana_get_rxbuf_pre(rxq, &da); 2519 else 2520 va = mana_get_rxfrag(rxq, dev, &da, &from_pool); 2521 2522 if (!va) 2523 return -ENOMEM; 2524 2525 rx_oob->buf_va = va; 2526 rx_oob->from_pool = from_pool; 2527 2528 rx_oob->sgl[0].address = da; 2529 rx_oob->sgl[0].size = rxq->datasize; 2530 rx_oob->sgl[0].mem_key = mem_key; 2531 2532 return 0; 2533 } 2534 2535 #define MANA_WQE_HEADER_SIZE 16 2536 #define MANA_WQE_SGE_SIZE 16 2537 2538 static int mana_alloc_rx_wqe(struct mana_port_context *apc, 2539 struct mana_rxq *rxq, u32 *rxq_size, u32 *cq_size) 2540 { 2541 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context; 2542 struct mana_recv_buf_oob *rx_oob; 2543 struct device *dev = gc->dev; 2544 u32 buf_idx; 2545 int ret; 2546 2547 WARN_ON(rxq->datasize == 0); 2548 2549 *rxq_size = 0; 2550 *cq_size = 0; 2551 2552 for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) { 2553 rx_oob = &rxq->rx_oobs[buf_idx]; 2554 memset(rx_oob, 0, sizeof(*rx_oob)); 2555 2556 rx_oob->num_sge = 1; 2557 2558 ret = mana_fill_rx_oob(rx_oob, apc->ac->gdma_dev->gpa_mkey, rxq, 2559 dev); 2560 if (ret) 2561 return ret; 2562 2563 rx_oob->wqe_req.sgl = rx_oob->sgl; 2564 rx_oob->wqe_req.num_sge = rx_oob->num_sge; 2565 rx_oob->wqe_req.inline_oob_size = 0; 2566 rx_oob->wqe_req.inline_oob_data = NULL; 2567 rx_oob->wqe_req.flags = 0; 2568 rx_oob->wqe_req.client_data_unit = 0; 2569 2570 *rxq_size += ALIGN(MANA_WQE_HEADER_SIZE + 2571 MANA_WQE_SGE_SIZE * rx_oob->num_sge, 32); 2572 *cq_size += COMP_ENTRY_SIZE; 2573 } 2574 2575 return 0; 2576 } 2577 2578 static int mana_push_wqe(struct mana_rxq *rxq) 2579 { 2580 struct mana_recv_buf_oob *rx_oob; 2581 u32 buf_idx; 2582 int err; 2583 2584 for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) { 2585 rx_oob = &rxq->rx_oobs[buf_idx]; 2586 2587 err = mana_gd_post_and_ring(rxq->gdma_rq, &rx_oob->wqe_req, 2588 &rx_oob->wqe_inf); 2589 if (err) 2590 return -ENOSPC; 2591 } 2592 2593 return 0; 2594 } 2595 2596 static int mana_create_page_pool(struct mana_rxq *rxq, struct gdma_context *gc) 2597 { 2598 struct mana_port_context *mpc = netdev_priv(rxq->ndev); 2599 struct page_pool_params pprm = {}; 2600 int ret; 2601 2602 pprm.pool_size = mpc->rx_queue_size / rxq->frag_count + 1; 2603 pprm.nid = gc->numa_node; 2604 pprm.napi = &rxq->rx_cq.napi; 2605 pprm.netdev = rxq->ndev; 2606 pprm.order = get_order(rxq->alloc_size); 2607 pprm.queue_idx = rxq->rxq_idx; 2608 pprm.dev = gc->dev; 2609 2610 /* Let the page pool do the dma map when page sharing with multiple 2611 * fragments enabled for rx buffers. 2612 */ 2613 if (rxq->frag_count > 1) { 2614 pprm.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 2615 pprm.max_len = PAGE_SIZE; 2616 pprm.dma_dir = DMA_FROM_DEVICE; 2617 } 2618 2619 rxq->page_pool = page_pool_create(&pprm); 2620 2621 if (IS_ERR(rxq->page_pool)) { 2622 ret = PTR_ERR(rxq->page_pool); 2623 rxq->page_pool = NULL; 2624 return ret; 2625 } 2626 2627 return 0; 2628 } 2629 2630 static struct mana_rxq *mana_create_rxq(struct mana_port_context *apc, 2631 u32 rxq_idx, struct mana_eq *eq, 2632 struct net_device *ndev) 2633 { 2634 struct gdma_dev *gd = apc->ac->gdma_dev; 2635 struct mana_obj_spec wq_spec; 2636 struct mana_obj_spec cq_spec; 2637 struct gdma_queue_spec spec; 2638 struct mana_cq *cq = NULL; 2639 struct gdma_context *gc; 2640 u32 cq_size, rq_size; 2641 struct mana_rxq *rxq; 2642 int err; 2643 2644 gc = gd->gdma_context; 2645 2646 rxq = kzalloc(struct_size(rxq, rx_oobs, apc->rx_queue_size), 2647 GFP_KERNEL); 2648 if (!rxq) 2649 return NULL; 2650 2651 rxq->ndev = ndev; 2652 rxq->num_rx_buf = apc->rx_queue_size; 2653 rxq->rxq_idx = rxq_idx; 2654 rxq->rxobj = INVALID_MANA_HANDLE; 2655 2656 mana_get_rxbuf_cfg(apc, ndev->mtu, &rxq->datasize, &rxq->alloc_size, 2657 &rxq->headroom, &rxq->frag_count); 2658 /* Create page pool for RX queue */ 2659 err = mana_create_page_pool(rxq, gc); 2660 if (err) { 2661 netdev_err(ndev, "Create page pool err:%d\n", err); 2662 goto out; 2663 } 2664 2665 err = mana_alloc_rx_wqe(apc, rxq, &rq_size, &cq_size); 2666 if (err) 2667 goto out; 2668 2669 rq_size = MANA_PAGE_ALIGN(rq_size); 2670 cq_size = MANA_PAGE_ALIGN(cq_size); 2671 2672 /* Create RQ */ 2673 memset(&spec, 0, sizeof(spec)); 2674 spec.type = GDMA_RQ; 2675 spec.monitor_avl_buf = true; 2676 spec.queue_size = rq_size; 2677 err = mana_gd_create_mana_wq_cq(gd, &spec, &rxq->gdma_rq); 2678 if (err) 2679 goto out; 2680 2681 /* Create RQ's CQ */ 2682 cq = &rxq->rx_cq; 2683 cq->type = MANA_CQ_TYPE_RX; 2684 cq->rxq = rxq; 2685 2686 memset(&spec, 0, sizeof(spec)); 2687 spec.type = GDMA_CQ; 2688 spec.monitor_avl_buf = false; 2689 spec.queue_size = cq_size; 2690 spec.cq.callback = mana_schedule_napi; 2691 spec.cq.parent_eq = eq->eq; 2692 spec.cq.context = cq; 2693 err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq); 2694 if (err) 2695 goto out; 2696 2697 memset(&wq_spec, 0, sizeof(wq_spec)); 2698 memset(&cq_spec, 0, sizeof(cq_spec)); 2699 wq_spec.gdma_region = rxq->gdma_rq->mem_info.dma_region_handle; 2700 wq_spec.queue_size = rxq->gdma_rq->queue_size; 2701 2702 cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle; 2703 cq_spec.queue_size = cq->gdma_cq->queue_size; 2704 cq_spec.modr_ctx_id = 0; 2705 cq_spec.attached_eq = cq->gdma_cq->cq.parent->id; 2706 2707 err = mana_create_wq_obj(apc, apc->port_handle, GDMA_RQ, 2708 &wq_spec, &cq_spec, &rxq->rxobj); 2709 if (err) 2710 goto out; 2711 2712 rxq->gdma_rq->id = wq_spec.queue_index; 2713 cq->gdma_cq->id = cq_spec.queue_index; 2714 2715 rxq->gdma_rq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION; 2716 cq->gdma_cq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION; 2717 2718 rxq->gdma_id = rxq->gdma_rq->id; 2719 cq->gdma_id = cq->gdma_cq->id; 2720 2721 err = mana_push_wqe(rxq); 2722 if (err) 2723 goto out; 2724 2725 if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) { 2726 err = -EINVAL; 2727 goto out; 2728 } 2729 2730 gc->cq_table[cq->gdma_id] = cq->gdma_cq; 2731 2732 netif_napi_add_weight_locked(ndev, &cq->napi, mana_poll, 1); 2733 2734 WARN_ON(xdp_rxq_info_reg(&rxq->xdp_rxq, ndev, rxq_idx, 2735 cq->napi.napi_id)); 2736 WARN_ON(xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL, 2737 rxq->page_pool)); 2738 2739 napi_enable_locked(&cq->napi); 2740 2741 mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT); 2742 out: 2743 if (!err) 2744 return rxq; 2745 2746 netdev_err(ndev, "Failed to create RXQ: err = %d\n", err); 2747 2748 mana_destroy_rxq(apc, rxq, false); 2749 2750 if (cq) 2751 mana_deinit_cq(apc, cq); 2752 2753 return NULL; 2754 } 2755 2756 static void mana_create_rxq_debugfs(struct mana_port_context *apc, int idx) 2757 { 2758 struct mana_rxq *rxq; 2759 char qnum[32]; 2760 2761 rxq = apc->rxqs[idx]; 2762 2763 sprintf(qnum, "RX-%d", idx); 2764 rxq->mana_rx_debugfs = debugfs_create_dir(qnum, apc->mana_port_debugfs); 2765 debugfs_create_u32("rq_head", 0400, rxq->mana_rx_debugfs, &rxq->gdma_rq->head); 2766 debugfs_create_u32("rq_tail", 0400, rxq->mana_rx_debugfs, &rxq->gdma_rq->tail); 2767 debugfs_create_u32("rq_nbuf", 0400, rxq->mana_rx_debugfs, &rxq->num_rx_buf); 2768 debugfs_create_u32("cq_head", 0400, rxq->mana_rx_debugfs, 2769 &rxq->rx_cq.gdma_cq->head); 2770 debugfs_create_u32("cq_tail", 0400, rxq->mana_rx_debugfs, 2771 &rxq->rx_cq.gdma_cq->tail); 2772 debugfs_create_u32("cq_budget", 0400, rxq->mana_rx_debugfs, &rxq->rx_cq.budget); 2773 debugfs_create_file("rxq_dump", 0400, rxq->mana_rx_debugfs, rxq->gdma_rq, &mana_dbg_q_fops); 2774 debugfs_create_file("cq_dump", 0400, rxq->mana_rx_debugfs, rxq->rx_cq.gdma_cq, 2775 &mana_dbg_q_fops); 2776 } 2777 2778 static int mana_add_rx_queues(struct mana_port_context *apc, 2779 struct net_device *ndev) 2780 { 2781 struct mana_context *ac = apc->ac; 2782 struct mana_rxq *rxq; 2783 int err = 0; 2784 int i; 2785 2786 for (i = 0; i < apc->num_queues; i++) { 2787 rxq = mana_create_rxq(apc, i, &ac->eqs[i], ndev); 2788 if (!rxq) { 2789 err = -ENOMEM; 2790 netdev_err(ndev, "Failed to create rxq %d : %d\n", i, err); 2791 goto out; 2792 } 2793 2794 u64_stats_init(&rxq->stats.syncp); 2795 2796 apc->rxqs[i] = rxq; 2797 2798 mana_create_rxq_debugfs(apc, i); 2799 } 2800 2801 apc->default_rxobj = apc->rxqs[0]->rxobj; 2802 out: 2803 return err; 2804 } 2805 2806 static void mana_destroy_vport(struct mana_port_context *apc) 2807 { 2808 struct gdma_dev *gd = apc->ac->gdma_dev; 2809 struct mana_rxq *rxq; 2810 u32 rxq_idx; 2811 2812 for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) { 2813 rxq = apc->rxqs[rxq_idx]; 2814 if (!rxq) 2815 continue; 2816 2817 mana_destroy_rxq(apc, rxq, true); 2818 apc->rxqs[rxq_idx] = NULL; 2819 } 2820 2821 mana_destroy_txq(apc); 2822 mana_uncfg_vport(apc); 2823 2824 if (gd->gdma_context->is_pf && !apc->ac->bm_hostmode) 2825 mana_pf_deregister_hw_vport(apc); 2826 } 2827 2828 static int mana_create_vport(struct mana_port_context *apc, 2829 struct net_device *net) 2830 { 2831 struct gdma_dev *gd = apc->ac->gdma_dev; 2832 int err; 2833 2834 apc->default_rxobj = INVALID_MANA_HANDLE; 2835 2836 if (gd->gdma_context->is_pf && !apc->ac->bm_hostmode) { 2837 err = mana_pf_register_hw_vport(apc); 2838 if (err) 2839 return err; 2840 } 2841 2842 err = mana_cfg_vport(apc, gd->pdid, gd->doorbell); 2843 if (err) 2844 return err; 2845 2846 return mana_create_txq(apc, net); 2847 } 2848 2849 static int mana_rss_table_alloc(struct mana_port_context *apc) 2850 { 2851 if (!apc->indir_table_sz) { 2852 netdev_err(apc->ndev, 2853 "Indirection table size not set for vPort %d\n", 2854 apc->port_idx); 2855 return -EINVAL; 2856 } 2857 2858 apc->indir_table = kcalloc(apc->indir_table_sz, sizeof(u32), GFP_KERNEL); 2859 if (!apc->indir_table) 2860 return -ENOMEM; 2861 2862 apc->rxobj_table = kcalloc(apc->indir_table_sz, sizeof(mana_handle_t), GFP_KERNEL); 2863 if (!apc->rxobj_table) { 2864 kfree(apc->indir_table); 2865 return -ENOMEM; 2866 } 2867 2868 return 0; 2869 } 2870 2871 static void mana_rss_table_init(struct mana_port_context *apc) 2872 { 2873 int i; 2874 2875 for (i = 0; i < apc->indir_table_sz; i++) 2876 apc->indir_table[i] = 2877 ethtool_rxfh_indir_default(i, apc->num_queues); 2878 } 2879 2880 int mana_config_rss(struct mana_port_context *apc, enum TRI_STATE rx, 2881 bool update_hash, bool update_tab) 2882 { 2883 u32 queue_idx; 2884 int err; 2885 int i; 2886 2887 if (update_tab) { 2888 for (i = 0; i < apc->indir_table_sz; i++) { 2889 queue_idx = apc->indir_table[i]; 2890 apc->rxobj_table[i] = apc->rxqs[queue_idx]->rxobj; 2891 } 2892 } 2893 2894 err = mana_cfg_vport_steering(apc, rx, true, update_hash, update_tab); 2895 if (err) 2896 return err; 2897 2898 mana_fence_rqs(apc); 2899 2900 return 0; 2901 } 2902 2903 int mana_query_gf_stats(struct mana_context *ac) 2904 { 2905 struct gdma_context *gc = ac->gdma_dev->gdma_context; 2906 struct mana_query_gf_stat_resp resp = {}; 2907 struct mana_query_gf_stat_req req = {}; 2908 struct device *dev = gc->dev; 2909 int err; 2910 2911 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_GF_STAT, 2912 sizeof(req), sizeof(resp)); 2913 req.hdr.resp.msg_version = GDMA_MESSAGE_V2; 2914 req.req_stats = STATISTICS_FLAGS_RX_DISCARDS_NO_WQE | 2915 STATISTICS_FLAGS_RX_ERRORS_VPORT_DISABLED | 2916 STATISTICS_FLAGS_HC_RX_BYTES | 2917 STATISTICS_FLAGS_HC_RX_UCAST_PACKETS | 2918 STATISTICS_FLAGS_HC_RX_UCAST_BYTES | 2919 STATISTICS_FLAGS_HC_RX_MCAST_PACKETS | 2920 STATISTICS_FLAGS_HC_RX_MCAST_BYTES | 2921 STATISTICS_FLAGS_HC_RX_BCAST_PACKETS | 2922 STATISTICS_FLAGS_HC_RX_BCAST_BYTES | 2923 STATISTICS_FLAGS_TX_ERRORS_GF_DISABLED | 2924 STATISTICS_FLAGS_TX_ERRORS_VPORT_DISABLED | 2925 STATISTICS_FLAGS_TX_ERRORS_INVAL_VPORT_OFFSET_PACKETS | 2926 STATISTICS_FLAGS_TX_ERRORS_VLAN_ENFORCEMENT | 2927 STATISTICS_FLAGS_TX_ERRORS_ETH_TYPE_ENFORCEMENT | 2928 STATISTICS_FLAGS_TX_ERRORS_SA_ENFORCEMENT | 2929 STATISTICS_FLAGS_TX_ERRORS_SQPDID_ENFORCEMENT | 2930 STATISTICS_FLAGS_TX_ERRORS_CQPDID_ENFORCEMENT | 2931 STATISTICS_FLAGS_TX_ERRORS_MTU_VIOLATION | 2932 STATISTICS_FLAGS_TX_ERRORS_INVALID_OOB | 2933 STATISTICS_FLAGS_HC_TX_BYTES | 2934 STATISTICS_FLAGS_HC_TX_UCAST_PACKETS | 2935 STATISTICS_FLAGS_HC_TX_UCAST_BYTES | 2936 STATISTICS_FLAGS_HC_TX_MCAST_PACKETS | 2937 STATISTICS_FLAGS_HC_TX_MCAST_BYTES | 2938 STATISTICS_FLAGS_HC_TX_BCAST_PACKETS | 2939 STATISTICS_FLAGS_HC_TX_BCAST_BYTES | 2940 STATISTICS_FLAGS_TX_ERRORS_GDMA_ERROR; 2941 2942 err = mana_send_request(ac, &req, sizeof(req), &resp, 2943 sizeof(resp)); 2944 if (err) { 2945 dev_err(dev, "Failed to query GF stats: %d\n", err); 2946 return err; 2947 } 2948 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_GF_STAT, 2949 sizeof(resp)); 2950 if (err || resp.hdr.status) { 2951 dev_err(dev, "Failed to query GF stats: %d, 0x%x\n", err, 2952 resp.hdr.status); 2953 return err; 2954 } 2955 2956 ac->hc_stats.hc_rx_discards_no_wqe = resp.rx_discards_nowqe; 2957 ac->hc_stats.hc_rx_err_vport_disabled = resp.rx_err_vport_disabled; 2958 ac->hc_stats.hc_rx_bytes = resp.hc_rx_bytes; 2959 ac->hc_stats.hc_rx_ucast_pkts = resp.hc_rx_ucast_pkts; 2960 ac->hc_stats.hc_rx_ucast_bytes = resp.hc_rx_ucast_bytes; 2961 ac->hc_stats.hc_rx_bcast_pkts = resp.hc_rx_bcast_pkts; 2962 ac->hc_stats.hc_rx_bcast_bytes = resp.hc_rx_bcast_bytes; 2963 ac->hc_stats.hc_rx_mcast_pkts = resp.hc_rx_mcast_pkts; 2964 ac->hc_stats.hc_rx_mcast_bytes = resp.hc_rx_mcast_bytes; 2965 ac->hc_stats.hc_tx_err_gf_disabled = resp.tx_err_gf_disabled; 2966 ac->hc_stats.hc_tx_err_vport_disabled = resp.tx_err_vport_disabled; 2967 ac->hc_stats.hc_tx_err_inval_vportoffset_pkt = 2968 resp.tx_err_inval_vport_offset_pkt; 2969 ac->hc_stats.hc_tx_err_vlan_enforcement = 2970 resp.tx_err_vlan_enforcement; 2971 ac->hc_stats.hc_tx_err_eth_type_enforcement = 2972 resp.tx_err_ethtype_enforcement; 2973 ac->hc_stats.hc_tx_err_sa_enforcement = resp.tx_err_SA_enforcement; 2974 ac->hc_stats.hc_tx_err_sqpdid_enforcement = 2975 resp.tx_err_SQPDID_enforcement; 2976 ac->hc_stats.hc_tx_err_cqpdid_enforcement = 2977 resp.tx_err_CQPDID_enforcement; 2978 ac->hc_stats.hc_tx_err_mtu_violation = resp.tx_err_mtu_violation; 2979 ac->hc_stats.hc_tx_err_inval_oob = resp.tx_err_inval_oob; 2980 ac->hc_stats.hc_tx_bytes = resp.hc_tx_bytes; 2981 ac->hc_stats.hc_tx_ucast_pkts = resp.hc_tx_ucast_pkts; 2982 ac->hc_stats.hc_tx_ucast_bytes = resp.hc_tx_ucast_bytes; 2983 ac->hc_stats.hc_tx_bcast_pkts = resp.hc_tx_bcast_pkts; 2984 ac->hc_stats.hc_tx_bcast_bytes = resp.hc_tx_bcast_bytes; 2985 ac->hc_stats.hc_tx_mcast_pkts = resp.hc_tx_mcast_pkts; 2986 ac->hc_stats.hc_tx_mcast_bytes = resp.hc_tx_mcast_bytes; 2987 ac->hc_stats.hc_tx_err_gdma = resp.tx_err_gdma; 2988 2989 return 0; 2990 } 2991 2992 void mana_query_phy_stats(struct mana_port_context *apc) 2993 { 2994 struct mana_query_phy_stat_resp resp = {}; 2995 struct mana_query_phy_stat_req req = {}; 2996 struct net_device *ndev = apc->ndev; 2997 int err; 2998 2999 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_PHY_STAT, 3000 sizeof(req), sizeof(resp)); 3001 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 3002 sizeof(resp)); 3003 if (err) 3004 return; 3005 3006 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_PHY_STAT, 3007 sizeof(resp)); 3008 if (err || resp.hdr.status) { 3009 netdev_err(ndev, 3010 "Failed to query PHY stats: %d, resp:0x%x\n", 3011 err, resp.hdr.status); 3012 return; 3013 } 3014 3015 /* Aggregate drop counters */ 3016 apc->phy_stats.rx_pkt_drop_phy = resp.rx_pkt_drop_phy; 3017 apc->phy_stats.tx_pkt_drop_phy = resp.tx_pkt_drop_phy; 3018 3019 /* Per TC traffic Counters */ 3020 apc->phy_stats.rx_pkt_tc0_phy = resp.rx_pkt_tc0_phy; 3021 apc->phy_stats.tx_pkt_tc0_phy = resp.tx_pkt_tc0_phy; 3022 apc->phy_stats.rx_pkt_tc1_phy = resp.rx_pkt_tc1_phy; 3023 apc->phy_stats.tx_pkt_tc1_phy = resp.tx_pkt_tc1_phy; 3024 apc->phy_stats.rx_pkt_tc2_phy = resp.rx_pkt_tc2_phy; 3025 apc->phy_stats.tx_pkt_tc2_phy = resp.tx_pkt_tc2_phy; 3026 apc->phy_stats.rx_pkt_tc3_phy = resp.rx_pkt_tc3_phy; 3027 apc->phy_stats.tx_pkt_tc3_phy = resp.tx_pkt_tc3_phy; 3028 apc->phy_stats.rx_pkt_tc4_phy = resp.rx_pkt_tc4_phy; 3029 apc->phy_stats.tx_pkt_tc4_phy = resp.tx_pkt_tc4_phy; 3030 apc->phy_stats.rx_pkt_tc5_phy = resp.rx_pkt_tc5_phy; 3031 apc->phy_stats.tx_pkt_tc5_phy = resp.tx_pkt_tc5_phy; 3032 apc->phy_stats.rx_pkt_tc6_phy = resp.rx_pkt_tc6_phy; 3033 apc->phy_stats.tx_pkt_tc6_phy = resp.tx_pkt_tc6_phy; 3034 apc->phy_stats.rx_pkt_tc7_phy = resp.rx_pkt_tc7_phy; 3035 apc->phy_stats.tx_pkt_tc7_phy = resp.tx_pkt_tc7_phy; 3036 3037 /* Per TC byte Counters */ 3038 apc->phy_stats.rx_byte_tc0_phy = resp.rx_byte_tc0_phy; 3039 apc->phy_stats.tx_byte_tc0_phy = resp.tx_byte_tc0_phy; 3040 apc->phy_stats.rx_byte_tc1_phy = resp.rx_byte_tc1_phy; 3041 apc->phy_stats.tx_byte_tc1_phy = resp.tx_byte_tc1_phy; 3042 apc->phy_stats.rx_byte_tc2_phy = resp.rx_byte_tc2_phy; 3043 apc->phy_stats.tx_byte_tc2_phy = resp.tx_byte_tc2_phy; 3044 apc->phy_stats.rx_byte_tc3_phy = resp.rx_byte_tc3_phy; 3045 apc->phy_stats.tx_byte_tc3_phy = resp.tx_byte_tc3_phy; 3046 apc->phy_stats.rx_byte_tc4_phy = resp.rx_byte_tc4_phy; 3047 apc->phy_stats.tx_byte_tc4_phy = resp.tx_byte_tc4_phy; 3048 apc->phy_stats.rx_byte_tc5_phy = resp.rx_byte_tc5_phy; 3049 apc->phy_stats.tx_byte_tc5_phy = resp.tx_byte_tc5_phy; 3050 apc->phy_stats.rx_byte_tc6_phy = resp.rx_byte_tc6_phy; 3051 apc->phy_stats.tx_byte_tc6_phy = resp.tx_byte_tc6_phy; 3052 apc->phy_stats.rx_byte_tc7_phy = resp.rx_byte_tc7_phy; 3053 apc->phy_stats.tx_byte_tc7_phy = resp.tx_byte_tc7_phy; 3054 3055 /* Per TC pause Counters */ 3056 apc->phy_stats.rx_pause_tc0_phy = resp.rx_pause_tc0_phy; 3057 apc->phy_stats.tx_pause_tc0_phy = resp.tx_pause_tc0_phy; 3058 apc->phy_stats.rx_pause_tc1_phy = resp.rx_pause_tc1_phy; 3059 apc->phy_stats.tx_pause_tc1_phy = resp.tx_pause_tc1_phy; 3060 apc->phy_stats.rx_pause_tc2_phy = resp.rx_pause_tc2_phy; 3061 apc->phy_stats.tx_pause_tc2_phy = resp.tx_pause_tc2_phy; 3062 apc->phy_stats.rx_pause_tc3_phy = resp.rx_pause_tc3_phy; 3063 apc->phy_stats.tx_pause_tc3_phy = resp.tx_pause_tc3_phy; 3064 apc->phy_stats.rx_pause_tc4_phy = resp.rx_pause_tc4_phy; 3065 apc->phy_stats.tx_pause_tc4_phy = resp.tx_pause_tc4_phy; 3066 apc->phy_stats.rx_pause_tc5_phy = resp.rx_pause_tc5_phy; 3067 apc->phy_stats.tx_pause_tc5_phy = resp.tx_pause_tc5_phy; 3068 apc->phy_stats.rx_pause_tc6_phy = resp.rx_pause_tc6_phy; 3069 apc->phy_stats.tx_pause_tc6_phy = resp.tx_pause_tc6_phy; 3070 apc->phy_stats.rx_pause_tc7_phy = resp.rx_pause_tc7_phy; 3071 apc->phy_stats.tx_pause_tc7_phy = resp.tx_pause_tc7_phy; 3072 } 3073 3074 static int mana_init_port(struct net_device *ndev) 3075 { 3076 struct mana_port_context *apc = netdev_priv(ndev); 3077 struct gdma_dev *gd = apc->ac->gdma_dev; 3078 u32 max_txq, max_rxq, max_queues; 3079 int port_idx = apc->port_idx; 3080 struct gdma_context *gc; 3081 char vport[32]; 3082 int err; 3083 3084 err = mana_init_port_context(apc); 3085 if (err) 3086 return err; 3087 3088 gc = gd->gdma_context; 3089 3090 err = mana_query_vport_cfg(apc, port_idx, &max_txq, &max_rxq, 3091 &apc->indir_table_sz); 3092 if (err) { 3093 netdev_err(ndev, "Failed to query info for vPort %d\n", 3094 port_idx); 3095 goto reset_apc; 3096 } 3097 3098 max_queues = min_t(u32, max_txq, max_rxq); 3099 if (apc->max_queues > max_queues) 3100 apc->max_queues = max_queues; 3101 3102 if (apc->num_queues > apc->max_queues) 3103 apc->num_queues = apc->max_queues; 3104 3105 eth_hw_addr_set(ndev, apc->mac_addr); 3106 sprintf(vport, "vport%d", port_idx); 3107 apc->mana_port_debugfs = debugfs_create_dir(vport, gc->mana_pci_debugfs); 3108 return 0; 3109 3110 reset_apc: 3111 mana_cleanup_port_context(apc); 3112 return err; 3113 } 3114 3115 int mana_alloc_queues(struct net_device *ndev) 3116 { 3117 struct mana_port_context *apc = netdev_priv(ndev); 3118 struct gdma_dev *gd = apc->ac->gdma_dev; 3119 int err; 3120 3121 err = mana_create_vport(apc, ndev); 3122 if (err) { 3123 netdev_err(ndev, "Failed to create vPort %u : %d\n", apc->port_idx, err); 3124 return err; 3125 } 3126 3127 err = netif_set_real_num_tx_queues(ndev, apc->num_queues); 3128 if (err) { 3129 netdev_err(ndev, 3130 "netif_set_real_num_tx_queues () failed for ndev with num_queues %u : %d\n", 3131 apc->num_queues, err); 3132 goto destroy_vport; 3133 } 3134 3135 err = mana_add_rx_queues(apc, ndev); 3136 if (err) 3137 goto destroy_vport; 3138 3139 apc->rss_state = apc->num_queues > 1 ? TRI_STATE_TRUE : TRI_STATE_FALSE; 3140 3141 err = netif_set_real_num_rx_queues(ndev, apc->num_queues); 3142 if (err) { 3143 netdev_err(ndev, 3144 "netif_set_real_num_rx_queues () failed for ndev with num_queues %u : %d\n", 3145 apc->num_queues, err); 3146 goto destroy_vport; 3147 } 3148 3149 mana_rss_table_init(apc); 3150 3151 err = mana_config_rss(apc, TRI_STATE_TRUE, true, true); 3152 if (err) { 3153 netdev_err(ndev, "Failed to configure RSS table: %d\n", err); 3154 goto destroy_vport; 3155 } 3156 3157 if (gd->gdma_context->is_pf && !apc->ac->bm_hostmode) { 3158 err = mana_pf_register_filter(apc); 3159 if (err) 3160 goto destroy_vport; 3161 } 3162 3163 mana_chn_setxdp(apc, mana_xdp_get(apc)); 3164 3165 return 0; 3166 3167 destroy_vport: 3168 mana_destroy_vport(apc); 3169 return err; 3170 } 3171 3172 int mana_attach(struct net_device *ndev) 3173 { 3174 struct mana_port_context *apc = netdev_priv(ndev); 3175 int err; 3176 3177 ASSERT_RTNL(); 3178 3179 err = mana_init_port(ndev); 3180 if (err) 3181 return err; 3182 3183 if (apc->port_st_save) { 3184 err = mana_alloc_queues(ndev); 3185 if (err) { 3186 mana_cleanup_port_context(apc); 3187 return err; 3188 } 3189 } 3190 3191 apc->port_is_up = apc->port_st_save; 3192 3193 /* Ensure port state updated before txq state */ 3194 smp_wmb(); 3195 3196 netif_device_attach(ndev); 3197 3198 return 0; 3199 } 3200 3201 static int mana_dealloc_queues(struct net_device *ndev) 3202 { 3203 struct mana_port_context *apc = netdev_priv(ndev); 3204 unsigned long timeout = jiffies + 120 * HZ; 3205 struct gdma_dev *gd = apc->ac->gdma_dev; 3206 struct mana_txq *txq; 3207 struct sk_buff *skb; 3208 int i, err; 3209 u32 tsleep; 3210 3211 if (apc->port_is_up) 3212 return -EINVAL; 3213 3214 mana_chn_setxdp(apc, NULL); 3215 3216 if (gd->gdma_context->is_pf && !apc->ac->bm_hostmode) 3217 mana_pf_deregister_filter(apc); 3218 3219 /* No packet can be transmitted now since apc->port_is_up is false. 3220 * There is still a tiny chance that mana_poll_tx_cq() can re-enable 3221 * a txq because it may not timely see apc->port_is_up being cleared 3222 * to false, but it doesn't matter since mana_start_xmit() drops any 3223 * new packets due to apc->port_is_up being false. 3224 * 3225 * Drain all the in-flight TX packets. 3226 * A timeout of 120 seconds for all the queues is used. 3227 * This will break the while loop when h/w is not responding. 3228 * This value of 120 has been decided here considering max 3229 * number of queues. 3230 */ 3231 3232 for (i = 0; i < apc->num_queues; i++) { 3233 txq = &apc->tx_qp[i].txq; 3234 tsleep = 1000; 3235 while (atomic_read(&txq->pending_sends) > 0 && 3236 time_before(jiffies, timeout)) { 3237 usleep_range(tsleep, tsleep + 1000); 3238 tsleep <<= 1; 3239 } 3240 if (atomic_read(&txq->pending_sends)) { 3241 err = pcie_flr(to_pci_dev(gd->gdma_context->dev)); 3242 if (err) { 3243 netdev_err(ndev, "flr failed %d with %d pkts pending in txq %u\n", 3244 err, atomic_read(&txq->pending_sends), 3245 txq->gdma_txq_id); 3246 } 3247 break; 3248 } 3249 } 3250 3251 for (i = 0; i < apc->num_queues; i++) { 3252 txq = &apc->tx_qp[i].txq; 3253 while ((skb = skb_dequeue(&txq->pending_skbs))) { 3254 mana_unmap_skb(skb, apc); 3255 dev_kfree_skb_any(skb); 3256 } 3257 atomic_set(&txq->pending_sends, 0); 3258 } 3259 /* We're 100% sure the queues can no longer be woken up, because 3260 * we're sure now mana_poll_tx_cq() can't be running. 3261 */ 3262 3263 apc->rss_state = TRI_STATE_FALSE; 3264 err = mana_config_rss(apc, TRI_STATE_FALSE, false, false); 3265 if (err && mana_en_need_log(apc, err)) 3266 netdev_err(ndev, "Failed to disable vPort: %d\n", err); 3267 3268 /* Even in err case, still need to cleanup the vPort */ 3269 mana_destroy_vport(apc); 3270 3271 return 0; 3272 } 3273 3274 int mana_detach(struct net_device *ndev, bool from_close) 3275 { 3276 struct mana_port_context *apc = netdev_priv(ndev); 3277 int err; 3278 3279 ASSERT_RTNL(); 3280 3281 apc->port_st_save = apc->port_is_up; 3282 apc->port_is_up = false; 3283 3284 /* Ensure port state updated before txq state */ 3285 smp_wmb(); 3286 3287 netif_tx_disable(ndev); 3288 3289 if (apc->port_st_save) { 3290 err = mana_dealloc_queues(ndev); 3291 if (err) { 3292 netdev_err(ndev, "%s failed to deallocate queues: %d\n", __func__, err); 3293 return err; 3294 } 3295 } 3296 3297 if (!from_close) { 3298 netif_device_detach(ndev); 3299 mana_cleanup_port_context(apc); 3300 } 3301 3302 return 0; 3303 } 3304 3305 static int mana_probe_port(struct mana_context *ac, int port_idx, 3306 struct net_device **ndev_storage) 3307 { 3308 struct gdma_context *gc = ac->gdma_dev->gdma_context; 3309 struct mana_port_context *apc; 3310 struct net_device *ndev; 3311 int err; 3312 3313 ndev = alloc_etherdev_mq(sizeof(struct mana_port_context), 3314 gc->max_num_queues); 3315 if (!ndev) 3316 return -ENOMEM; 3317 3318 *ndev_storage = ndev; 3319 3320 apc = netdev_priv(ndev); 3321 apc->ac = ac; 3322 apc->ndev = ndev; 3323 apc->max_queues = gc->max_num_queues; 3324 apc->num_queues = gc->max_num_queues; 3325 apc->tx_queue_size = DEF_TX_BUFFERS_PER_QUEUE; 3326 apc->rx_queue_size = DEF_RX_BUFFERS_PER_QUEUE; 3327 apc->port_handle = INVALID_MANA_HANDLE; 3328 apc->pf_filter_handle = INVALID_MANA_HANDLE; 3329 apc->port_idx = port_idx; 3330 3331 mutex_init(&apc->vport_mutex); 3332 apc->vport_use_count = 0; 3333 3334 ndev->netdev_ops = &mana_devops; 3335 ndev->ethtool_ops = &mana_ethtool_ops; 3336 ndev->mtu = ETH_DATA_LEN; 3337 ndev->max_mtu = gc->adapter_mtu - ETH_HLEN; 3338 ndev->min_mtu = ETH_MIN_MTU; 3339 ndev->needed_headroom = MANA_HEADROOM; 3340 ndev->dev_port = port_idx; 3341 /* Recommended timeout based on HW FPGA re-config scenario. */ 3342 ndev->watchdog_timeo = 15 * HZ; 3343 SET_NETDEV_DEV(ndev, gc->dev); 3344 3345 netif_set_tso_max_size(ndev, GSO_MAX_SIZE); 3346 3347 netif_carrier_off(ndev); 3348 3349 netdev_rss_key_fill(apc->hashkey, MANA_HASH_KEY_SIZE); 3350 3351 err = mana_init_port(ndev); 3352 if (err) 3353 goto free_net; 3354 3355 err = mana_rss_table_alloc(apc); 3356 if (err) 3357 goto reset_apc; 3358 3359 /* Initialize the per port queue reset work.*/ 3360 INIT_WORK(&apc->queue_reset_work, 3361 mana_per_port_queue_reset_work_handler); 3362 3363 netdev_lockdep_set_classes(ndev); 3364 3365 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM; 3366 ndev->hw_features |= NETIF_F_RXCSUM; 3367 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6; 3368 ndev->hw_features |= NETIF_F_RXHASH; 3369 ndev->features = ndev->hw_features | NETIF_F_HW_VLAN_CTAG_TX | 3370 NETIF_F_HW_VLAN_CTAG_RX; 3371 ndev->vlan_features = ndev->features; 3372 xdp_set_features_flag(ndev, NETDEV_XDP_ACT_BASIC | 3373 NETDEV_XDP_ACT_REDIRECT | 3374 NETDEV_XDP_ACT_NDO_XMIT); 3375 3376 err = register_netdev(ndev); 3377 if (err) { 3378 netdev_err(ndev, "Unable to register netdev.\n"); 3379 goto free_indir; 3380 } 3381 3382 netif_carrier_on(ndev); 3383 3384 debugfs_create_u32("current_speed", 0400, apc->mana_port_debugfs, &apc->speed); 3385 3386 return 0; 3387 3388 free_indir: 3389 mana_cleanup_indir_table(apc); 3390 reset_apc: 3391 mana_cleanup_port_context(apc); 3392 free_net: 3393 *ndev_storage = NULL; 3394 netdev_err(ndev, "Failed to probe vPort %d: %d\n", port_idx, err); 3395 free_netdev(ndev); 3396 return err; 3397 } 3398 3399 static void adev_release(struct device *dev) 3400 { 3401 struct mana_adev *madev = container_of(dev, struct mana_adev, adev.dev); 3402 3403 kfree(madev); 3404 } 3405 3406 static void remove_adev(struct gdma_dev *gd) 3407 { 3408 struct auxiliary_device *adev = gd->adev; 3409 int id = adev->id; 3410 3411 auxiliary_device_delete(adev); 3412 auxiliary_device_uninit(adev); 3413 3414 mana_adev_idx_free(id); 3415 gd->adev = NULL; 3416 } 3417 3418 static int add_adev(struct gdma_dev *gd, const char *name) 3419 { 3420 struct auxiliary_device *adev; 3421 struct mana_adev *madev; 3422 int ret; 3423 3424 madev = kzalloc(sizeof(*madev), GFP_KERNEL); 3425 if (!madev) 3426 return -ENOMEM; 3427 3428 adev = &madev->adev; 3429 ret = mana_adev_idx_alloc(); 3430 if (ret < 0) 3431 goto idx_fail; 3432 adev->id = ret; 3433 3434 adev->name = name; 3435 adev->dev.parent = gd->gdma_context->dev; 3436 adev->dev.release = adev_release; 3437 madev->mdev = gd; 3438 3439 ret = auxiliary_device_init(adev); 3440 if (ret) 3441 goto init_fail; 3442 3443 /* madev is owned by the auxiliary device */ 3444 madev = NULL; 3445 ret = auxiliary_device_add(adev); 3446 if (ret) 3447 goto add_fail; 3448 3449 gd->adev = adev; 3450 dev_dbg(gd->gdma_context->dev, 3451 "Auxiliary device added successfully\n"); 3452 return 0; 3453 3454 add_fail: 3455 auxiliary_device_uninit(adev); 3456 3457 init_fail: 3458 mana_adev_idx_free(adev->id); 3459 3460 idx_fail: 3461 kfree(madev); 3462 3463 return ret; 3464 } 3465 3466 static void mana_rdma_service_handle(struct work_struct *work) 3467 { 3468 struct mana_service_work *serv_work = 3469 container_of(work, struct mana_service_work, work); 3470 struct gdma_dev *gd = serv_work->gdma_dev; 3471 struct device *dev = gd->gdma_context->dev; 3472 int ret; 3473 3474 if (READ_ONCE(gd->rdma_teardown)) 3475 goto out; 3476 3477 switch (serv_work->event) { 3478 case GDMA_SERVICE_TYPE_RDMA_SUSPEND: 3479 if (!gd->adev || gd->is_suspended) 3480 break; 3481 3482 remove_adev(gd); 3483 gd->is_suspended = true; 3484 break; 3485 3486 case GDMA_SERVICE_TYPE_RDMA_RESUME: 3487 if (!gd->is_suspended) 3488 break; 3489 3490 ret = add_adev(gd, "rdma"); 3491 if (ret) 3492 dev_err(dev, "Failed to add adev on resume: %d\n", ret); 3493 else 3494 gd->is_suspended = false; 3495 break; 3496 3497 default: 3498 dev_warn(dev, "unknown adev service event %u\n", 3499 serv_work->event); 3500 break; 3501 } 3502 3503 out: 3504 kfree(serv_work); 3505 } 3506 3507 int mana_rdma_service_event(struct gdma_context *gc, enum gdma_service_type event) 3508 { 3509 struct gdma_dev *gd = &gc->mana_ib; 3510 struct mana_service_work *serv_work; 3511 3512 if (gd->dev_id.type != GDMA_DEVICE_MANA_IB) { 3513 /* RDMA device is not detected on pci */ 3514 return 0; 3515 } 3516 3517 serv_work = kzalloc(sizeof(*serv_work), GFP_ATOMIC); 3518 if (!serv_work) 3519 return -ENOMEM; 3520 3521 serv_work->event = event; 3522 serv_work->gdma_dev = gd; 3523 3524 INIT_WORK(&serv_work->work, mana_rdma_service_handle); 3525 queue_work(gc->service_wq, &serv_work->work); 3526 3527 return 0; 3528 } 3529 3530 #define MANA_GF_STATS_PERIOD (2 * HZ) 3531 3532 static void mana_gf_stats_work_handler(struct work_struct *work) 3533 { 3534 struct mana_context *ac = 3535 container_of(to_delayed_work(work), struct mana_context, gf_stats_work); 3536 int err; 3537 3538 err = mana_query_gf_stats(ac); 3539 if (err == -ETIMEDOUT) { 3540 /* HWC timeout detected - reset stats and stop rescheduling */ 3541 ac->hwc_timeout_occurred = true; 3542 memset(&ac->hc_stats, 0, sizeof(ac->hc_stats)); 3543 return; 3544 } 3545 schedule_delayed_work(&ac->gf_stats_work, MANA_GF_STATS_PERIOD); 3546 } 3547 3548 int mana_probe(struct gdma_dev *gd, bool resuming) 3549 { 3550 struct gdma_context *gc = gd->gdma_context; 3551 struct mana_context *ac = gd->driver_data; 3552 struct mana_port_context *apc = NULL; 3553 struct device *dev = gc->dev; 3554 u8 bm_hostmode = 0; 3555 u16 num_ports = 0; 3556 int err; 3557 int i; 3558 3559 dev_info(dev, 3560 "Microsoft Azure Network Adapter protocol version: %d.%d.%d\n", 3561 MANA_MAJOR_VERSION, MANA_MINOR_VERSION, MANA_MICRO_VERSION); 3562 3563 err = mana_gd_register_device(gd); 3564 if (err) 3565 return err; 3566 3567 if (!resuming) { 3568 ac = kzalloc(sizeof(*ac), GFP_KERNEL); 3569 if (!ac) 3570 return -ENOMEM; 3571 3572 ac->gdma_dev = gd; 3573 gd->driver_data = ac; 3574 } 3575 3576 err = mana_create_eq(ac); 3577 if (err) { 3578 dev_err(dev, "Failed to create EQs: %d\n", err); 3579 goto out; 3580 } 3581 3582 err = mana_query_device_cfg(ac, MANA_MAJOR_VERSION, MANA_MINOR_VERSION, 3583 MANA_MICRO_VERSION, &num_ports, &bm_hostmode); 3584 if (err) 3585 goto out; 3586 3587 ac->bm_hostmode = bm_hostmode; 3588 3589 if (!resuming) { 3590 ac->num_ports = num_ports; 3591 3592 INIT_WORK(&ac->link_change_work, mana_link_state_handle); 3593 } else { 3594 if (ac->num_ports != num_ports) { 3595 dev_err(dev, "The number of vPorts changed: %d->%d\n", 3596 ac->num_ports, num_ports); 3597 err = -EPROTO; 3598 goto out; 3599 } 3600 3601 enable_work(&ac->link_change_work); 3602 } 3603 3604 if (ac->num_ports == 0) 3605 dev_err(dev, "Failed to detect any vPort\n"); 3606 3607 if (ac->num_ports > MAX_PORTS_IN_MANA_DEV) 3608 ac->num_ports = MAX_PORTS_IN_MANA_DEV; 3609 3610 ac->per_port_queue_reset_wq = 3611 create_singlethread_workqueue("mana_per_port_queue_reset_wq"); 3612 if (!ac->per_port_queue_reset_wq) { 3613 dev_err(dev, "Failed to allocate per port queue reset workqueue\n"); 3614 err = -ENOMEM; 3615 goto out; 3616 } 3617 3618 if (!resuming) { 3619 for (i = 0; i < ac->num_ports; i++) { 3620 err = mana_probe_port(ac, i, &ac->ports[i]); 3621 /* we log the port for which the probe failed and stop 3622 * probes for subsequent ports. 3623 * Note that we keep running ports, for which the probes 3624 * were successful, unless add_adev fails too 3625 */ 3626 if (err) { 3627 dev_err(dev, "Probe Failed for port %d\n", i); 3628 break; 3629 } 3630 } 3631 } else { 3632 for (i = 0; i < ac->num_ports; i++) { 3633 rtnl_lock(); 3634 apc = netdev_priv(ac->ports[i]); 3635 enable_work(&apc->queue_reset_work); 3636 err = mana_attach(ac->ports[i]); 3637 rtnl_unlock(); 3638 /* we log the port for which the attach failed and stop 3639 * attach for subsequent ports 3640 * Note that we keep running ports, for which the attach 3641 * were successful, unless add_adev fails too 3642 */ 3643 if (err) { 3644 dev_err(dev, "Attach Failed for port %d\n", i); 3645 break; 3646 } 3647 } 3648 } 3649 3650 err = add_adev(gd, "eth"); 3651 3652 INIT_DELAYED_WORK(&ac->gf_stats_work, mana_gf_stats_work_handler); 3653 schedule_delayed_work(&ac->gf_stats_work, MANA_GF_STATS_PERIOD); 3654 3655 out: 3656 if (err) { 3657 mana_remove(gd, false); 3658 } else { 3659 dev_dbg(dev, "gd=%p, id=%u, num_ports=%d, type=%u, instance=%u\n", 3660 gd, gd->dev_id.as_uint32, ac->num_ports, 3661 gd->dev_id.type, gd->dev_id.instance); 3662 dev_dbg(dev, "%s succeeded\n", __func__); 3663 } 3664 3665 return err; 3666 } 3667 3668 void mana_remove(struct gdma_dev *gd, bool suspending) 3669 { 3670 struct gdma_context *gc = gd->gdma_context; 3671 struct mana_context *ac = gd->driver_data; 3672 struct mana_port_context *apc; 3673 struct device *dev = gc->dev; 3674 struct net_device *ndev; 3675 int err; 3676 int i; 3677 3678 disable_work_sync(&ac->link_change_work); 3679 cancel_delayed_work_sync(&ac->gf_stats_work); 3680 3681 /* adev currently doesn't support suspending, always remove it */ 3682 if (gd->adev) 3683 remove_adev(gd); 3684 3685 for (i = 0; i < ac->num_ports; i++) { 3686 ndev = ac->ports[i]; 3687 if (!ndev) { 3688 if (i == 0) 3689 dev_err(dev, "No net device to remove\n"); 3690 goto out; 3691 } 3692 3693 apc = netdev_priv(ndev); 3694 disable_work_sync(&apc->queue_reset_work); 3695 3696 /* All cleanup actions should stay after rtnl_lock(), otherwise 3697 * other functions may access partially cleaned up data. 3698 */ 3699 rtnl_lock(); 3700 3701 err = mana_detach(ndev, false); 3702 if (err) 3703 netdev_err(ndev, "Failed to detach vPort %d: %d\n", 3704 i, err); 3705 3706 if (suspending) { 3707 /* No need to unregister the ndev. */ 3708 rtnl_unlock(); 3709 continue; 3710 } 3711 3712 unregister_netdevice(ndev); 3713 mana_cleanup_indir_table(apc); 3714 3715 rtnl_unlock(); 3716 3717 free_netdev(ndev); 3718 } 3719 3720 mana_destroy_eq(ac); 3721 out: 3722 if (ac->per_port_queue_reset_wq) { 3723 destroy_workqueue(ac->per_port_queue_reset_wq); 3724 ac->per_port_queue_reset_wq = NULL; 3725 } 3726 3727 mana_gd_deregister_device(gd); 3728 3729 if (suspending) 3730 return; 3731 3732 gd->driver_data = NULL; 3733 gd->gdma_context = NULL; 3734 kfree(ac); 3735 dev_dbg(dev, "%s succeeded\n", __func__); 3736 } 3737 3738 int mana_rdma_probe(struct gdma_dev *gd) 3739 { 3740 int err = 0; 3741 3742 if (gd->dev_id.type != GDMA_DEVICE_MANA_IB) { 3743 /* RDMA device is not detected on pci */ 3744 return err; 3745 } 3746 3747 err = mana_gd_register_device(gd); 3748 if (err) 3749 return err; 3750 3751 err = add_adev(gd, "rdma"); 3752 if (err) 3753 mana_gd_deregister_device(gd); 3754 3755 return err; 3756 } 3757 3758 void mana_rdma_remove(struct gdma_dev *gd) 3759 { 3760 struct gdma_context *gc = gd->gdma_context; 3761 3762 if (gd->dev_id.type != GDMA_DEVICE_MANA_IB) { 3763 /* RDMA device is not detected on pci */ 3764 return; 3765 } 3766 3767 WRITE_ONCE(gd->rdma_teardown, true); 3768 flush_workqueue(gc->service_wq); 3769 3770 if (gd->adev) 3771 remove_adev(gd); 3772 3773 mana_gd_deregister_device(gd); 3774 } 3775 3776 struct net_device *mana_get_primary_netdev(struct mana_context *ac, 3777 u32 port_index, 3778 netdevice_tracker *tracker) 3779 { 3780 struct net_device *ndev; 3781 3782 if (port_index >= ac->num_ports) 3783 return NULL; 3784 3785 rcu_read_lock(); 3786 3787 /* If mana is used in netvsc, the upper netdevice should be returned. */ 3788 ndev = netdev_master_upper_dev_get_rcu(ac->ports[port_index]); 3789 3790 /* If there is no upper device, use the parent Ethernet device */ 3791 if (!ndev) 3792 ndev = ac->ports[port_index]; 3793 3794 netdev_hold(ndev, tracker, GFP_ATOMIC); 3795 rcu_read_unlock(); 3796 3797 return ndev; 3798 } 3799 EXPORT_SYMBOL_NS(mana_get_primary_netdev, "NET_MANA"); 3800