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