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