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