1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2021, Microsoft Corporation. */
4 #include <uapi/linux/bpf.h>
6 #include <linux/inetdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/ethtool.h>
9 #include <linux/filter.h>
11 #include <linux/pci.h>
13 #include <net/checksum.h>
14 #include <net/ip6_checksum.h>
15 #include <net/page_pool/helpers.h>
18 #include <net/mana/mana.h>
19 #include <net/mana/mana_auxiliary.h>
21 static DEFINE_IDA(mana_adev_ida);
23 static int mana_adev_idx_alloc(void)
25 return ida_alloc(&mana_adev_ida, GFP_KERNEL);
28 static void mana_adev_idx_free(int idx)
30 ida_free(&mana_adev_ida, idx);
33 /* Microsoft Azure Network Adapter (MANA) functions */
35 static int mana_open(struct net_device *ndev)
37 struct mana_port_context *apc = netdev_priv(ndev);
40 err = mana_alloc_queues(ndev);
44 apc->port_is_up = true;
46 /* Ensure port state updated before txq state */
49 netif_carrier_on(ndev);
50 netif_tx_wake_all_queues(ndev);
55 static int mana_close(struct net_device *ndev)
57 struct mana_port_context *apc = netdev_priv(ndev);
62 return mana_detach(ndev, true);
65 static bool mana_can_tx(struct gdma_queue *wq)
67 return mana_gd_wq_avail_space(wq) >= MAX_TX_WQE_SIZE;
70 static unsigned int mana_checksum_info(struct sk_buff *skb)
72 if (skb->protocol == htons(ETH_P_IP)) {
73 struct iphdr *ip = ip_hdr(skb);
75 if (ip->protocol == IPPROTO_TCP)
78 if (ip->protocol == IPPROTO_UDP)
80 } else if (skb->protocol == htons(ETH_P_IPV6)) {
81 struct ipv6hdr *ip6 = ipv6_hdr(skb);
83 if (ip6->nexthdr == IPPROTO_TCP)
86 if (ip6->nexthdr == IPPROTO_UDP)
90 /* No csum offloading */
94 static int mana_map_skb(struct sk_buff *skb, struct mana_port_context *apc,
95 struct mana_tx_package *tp)
97 struct mana_skb_head *ash = (struct mana_skb_head *)skb->head;
98 struct gdma_dev *gd = apc->ac->gdma_dev;
99 struct gdma_context *gc;
105 gc = gd->gdma_context;
107 da = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
109 if (dma_mapping_error(dev, da))
112 ash->dma_handle[0] = da;
113 ash->size[0] = skb_headlen(skb);
115 tp->wqe_req.sgl[0].address = ash->dma_handle[0];
116 tp->wqe_req.sgl[0].mem_key = gd->gpa_mkey;
117 tp->wqe_req.sgl[0].size = ash->size[0];
119 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
120 frag = &skb_shinfo(skb)->frags[i];
121 da = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
124 if (dma_mapping_error(dev, da))
127 ash->dma_handle[i + 1] = da;
128 ash->size[i + 1] = skb_frag_size(frag);
130 tp->wqe_req.sgl[i + 1].address = ash->dma_handle[i + 1];
131 tp->wqe_req.sgl[i + 1].mem_key = gd->gpa_mkey;
132 tp->wqe_req.sgl[i + 1].size = ash->size[i + 1];
138 for (i = i - 1; i >= 0; i--)
139 dma_unmap_page(dev, ash->dma_handle[i + 1], ash->size[i + 1],
142 dma_unmap_single(dev, ash->dma_handle[0], ash->size[0], DMA_TO_DEVICE);
147 netdev_tx_t mana_start_xmit(struct sk_buff *skb, struct net_device *ndev)
149 enum mana_tx_pkt_format pkt_fmt = MANA_SHORT_PKT_FMT;
150 struct mana_port_context *apc = netdev_priv(ndev);
151 u16 txq_idx = skb_get_queue_mapping(skb);
152 struct gdma_dev *gd = apc->ac->gdma_dev;
153 bool ipv4 = false, ipv6 = false;
154 struct mana_tx_package pkg = {};
155 struct netdev_queue *net_txq;
156 struct mana_stats_tx *tx_stats;
157 struct gdma_queue *gdma_sq;
158 unsigned int csum_type;
159 struct mana_txq *txq;
164 if (unlikely(!apc->port_is_up))
167 if (skb_cow_head(skb, MANA_HEADROOM))
170 txq = &apc->tx_qp[txq_idx].txq;
171 gdma_sq = txq->gdma_sq;
172 cq = &apc->tx_qp[txq_idx].tx_cq;
173 tx_stats = &txq->stats;
175 pkg.tx_oob.s_oob.vcq_num = cq->gdma_id;
176 pkg.tx_oob.s_oob.vsq_frame = txq->vsq_frame;
178 if (txq->vp_offset > MANA_SHORT_VPORT_OFFSET_MAX) {
179 pkg.tx_oob.l_oob.long_vp_offset = txq->vp_offset;
180 pkt_fmt = MANA_LONG_PKT_FMT;
182 pkg.tx_oob.s_oob.short_vp_offset = txq->vp_offset;
185 if (skb_vlan_tag_present(skb)) {
186 pkt_fmt = MANA_LONG_PKT_FMT;
187 pkg.tx_oob.l_oob.inject_vlan_pri_tag = 1;
188 pkg.tx_oob.l_oob.pcp = skb_vlan_tag_get_prio(skb);
189 pkg.tx_oob.l_oob.dei = skb_vlan_tag_get_cfi(skb);
190 pkg.tx_oob.l_oob.vlan_id = skb_vlan_tag_get_id(skb);
193 pkg.tx_oob.s_oob.pkt_fmt = pkt_fmt;
195 if (pkt_fmt == MANA_SHORT_PKT_FMT) {
196 pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_short_oob);
197 u64_stats_update_begin(&tx_stats->syncp);
198 tx_stats->short_pkt_fmt++;
199 u64_stats_update_end(&tx_stats->syncp);
201 pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_oob);
202 u64_stats_update_begin(&tx_stats->syncp);
203 tx_stats->long_pkt_fmt++;
204 u64_stats_update_end(&tx_stats->syncp);
207 pkg.wqe_req.inline_oob_data = &pkg.tx_oob;
208 pkg.wqe_req.flags = 0;
209 pkg.wqe_req.client_data_unit = 0;
211 pkg.wqe_req.num_sge = 1 + skb_shinfo(skb)->nr_frags;
212 WARN_ON_ONCE(pkg.wqe_req.num_sge > MAX_TX_WQE_SGL_ENTRIES);
214 if (pkg.wqe_req.num_sge <= ARRAY_SIZE(pkg.sgl_array)) {
215 pkg.wqe_req.sgl = pkg.sgl_array;
217 pkg.sgl_ptr = kmalloc_array(pkg.wqe_req.num_sge,
218 sizeof(struct gdma_sge),
223 pkg.wqe_req.sgl = pkg.sgl_ptr;
226 if (skb->protocol == htons(ETH_P_IP))
228 else if (skb->protocol == htons(ETH_P_IPV6))
231 if (skb_is_gso(skb)) {
232 pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
233 pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
235 pkg.tx_oob.s_oob.comp_iphdr_csum = 1;
236 pkg.tx_oob.s_oob.comp_tcp_csum = 1;
237 pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb);
239 pkg.wqe_req.client_data_unit = skb_shinfo(skb)->gso_size;
240 pkg.wqe_req.flags = GDMA_WR_OOB_IN_SGL | GDMA_WR_PAD_BY_SGE0;
242 ip_hdr(skb)->tot_len = 0;
243 ip_hdr(skb)->check = 0;
244 tcp_hdr(skb)->check =
245 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
246 ip_hdr(skb)->daddr, 0,
249 ipv6_hdr(skb)->payload_len = 0;
250 tcp_hdr(skb)->check =
251 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
252 &ipv6_hdr(skb)->daddr, 0,
256 if (skb->encapsulation) {
257 ihs = skb_inner_tcp_all_headers(skb);
258 u64_stats_update_begin(&tx_stats->syncp);
259 tx_stats->tso_inner_packets++;
260 tx_stats->tso_inner_bytes += skb->len - ihs;
261 u64_stats_update_end(&tx_stats->syncp);
263 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
264 ihs = skb_transport_offset(skb) + sizeof(struct udphdr);
266 ihs = skb_tcp_all_headers(skb);
267 if (ipv6_has_hopopt_jumbo(skb))
268 ihs -= sizeof(struct hop_jumbo_hdr);
271 u64_stats_update_begin(&tx_stats->syncp);
272 tx_stats->tso_packets++;
273 tx_stats->tso_bytes += skb->len - ihs;
274 u64_stats_update_end(&tx_stats->syncp);
277 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
278 csum_type = mana_checksum_info(skb);
280 u64_stats_update_begin(&tx_stats->syncp);
281 tx_stats->csum_partial++;
282 u64_stats_update_end(&tx_stats->syncp);
284 if (csum_type == IPPROTO_TCP) {
285 pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
286 pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
288 pkg.tx_oob.s_oob.comp_tcp_csum = 1;
289 pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb);
291 } else if (csum_type == IPPROTO_UDP) {
292 pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
293 pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
295 pkg.tx_oob.s_oob.comp_udp_csum = 1;
297 /* Can't do offload of this type of checksum */
298 if (skb_checksum_help(skb))
303 if (mana_map_skb(skb, apc, &pkg)) {
304 u64_stats_update_begin(&tx_stats->syncp);
305 tx_stats->mana_map_err++;
306 u64_stats_update_end(&tx_stats->syncp);
310 skb_queue_tail(&txq->pending_skbs, skb);
313 net_txq = netdev_get_tx_queue(ndev, txq_idx);
315 err = mana_gd_post_work_request(gdma_sq, &pkg.wqe_req,
316 (struct gdma_posted_wqe_info *)skb->cb);
317 if (!mana_can_tx(gdma_sq)) {
318 netif_tx_stop_queue(net_txq);
319 apc->eth_stats.stop_queue++;
323 (void)skb_dequeue_tail(&txq->pending_skbs);
324 netdev_warn(ndev, "Failed to post TX OOB: %d\n", err);
325 err = NETDEV_TX_BUSY;
330 atomic_inc(&txq->pending_sends);
332 mana_gd_wq_ring_doorbell(gd->gdma_context, gdma_sq);
334 /* skb may be freed after mana_gd_post_work_request. Do not use it. */
337 tx_stats = &txq->stats;
338 u64_stats_update_begin(&tx_stats->syncp);
340 tx_stats->bytes += len;
341 u64_stats_update_end(&tx_stats->syncp);
344 if (netif_tx_queue_stopped(net_txq) && mana_can_tx(gdma_sq)) {
345 netif_tx_wake_queue(net_txq);
346 apc->eth_stats.wake_queue++;
355 ndev->stats.tx_dropped++;
357 dev_kfree_skb_any(skb);
361 static void mana_get_stats64(struct net_device *ndev,
362 struct rtnl_link_stats64 *st)
364 struct mana_port_context *apc = netdev_priv(ndev);
365 unsigned int num_queues = apc->num_queues;
366 struct mana_stats_rx *rx_stats;
367 struct mana_stats_tx *tx_stats;
372 if (!apc->port_is_up)
375 netdev_stats_to_stats64(st, &ndev->stats);
377 for (q = 0; q < num_queues; q++) {
378 rx_stats = &apc->rxqs[q]->stats;
381 start = u64_stats_fetch_begin(&rx_stats->syncp);
382 packets = rx_stats->packets;
383 bytes = rx_stats->bytes;
384 } while (u64_stats_fetch_retry(&rx_stats->syncp, start));
386 st->rx_packets += packets;
387 st->rx_bytes += bytes;
390 for (q = 0; q < num_queues; q++) {
391 tx_stats = &apc->tx_qp[q].txq.stats;
394 start = u64_stats_fetch_begin(&tx_stats->syncp);
395 packets = tx_stats->packets;
396 bytes = tx_stats->bytes;
397 } while (u64_stats_fetch_retry(&tx_stats->syncp, start));
399 st->tx_packets += packets;
400 st->tx_bytes += bytes;
404 static int mana_get_tx_queue(struct net_device *ndev, struct sk_buff *skb,
407 struct mana_port_context *apc = netdev_priv(ndev);
408 u32 hash = skb_get_hash(skb);
409 struct sock *sk = skb->sk;
412 txq = apc->indir_table[hash & MANA_INDIRECT_TABLE_MASK];
414 if (txq != old_q && sk && sk_fullsock(sk) &&
415 rcu_access_pointer(sk->sk_dst_cache))
416 sk_tx_queue_set(sk, txq);
421 static u16 mana_select_queue(struct net_device *ndev, struct sk_buff *skb,
422 struct net_device *sb_dev)
426 if (ndev->real_num_tx_queues == 1)
429 txq = sk_tx_queue_get(skb->sk);
431 if (txq < 0 || skb->ooo_okay || txq >= ndev->real_num_tx_queues) {
432 if (skb_rx_queue_recorded(skb))
433 txq = skb_get_rx_queue(skb);
435 txq = mana_get_tx_queue(ndev, skb, txq);
441 /* Release pre-allocated RX buffers */
442 static void mana_pre_dealloc_rxbufs(struct mana_port_context *mpc)
447 dev = mpc->ac->gdma_dev->gdma_context->dev;
449 if (!mpc->rxbufs_pre)
455 while (mpc->rxbpre_total) {
456 i = --mpc->rxbpre_total;
457 dma_unmap_single(dev, mpc->das_pre[i], mpc->rxbpre_datasize,
459 put_page(virt_to_head_page(mpc->rxbufs_pre[i]));
466 kfree(mpc->rxbufs_pre);
467 mpc->rxbufs_pre = NULL;
470 mpc->rxbpre_datasize = 0;
471 mpc->rxbpre_alloc_size = 0;
472 mpc->rxbpre_headroom = 0;
475 /* Get a buffer from the pre-allocated RX buffers */
476 static void *mana_get_rxbuf_pre(struct mana_rxq *rxq, dma_addr_t *da)
478 struct net_device *ndev = rxq->ndev;
479 struct mana_port_context *mpc;
482 mpc = netdev_priv(ndev);
484 if (!mpc->rxbufs_pre || !mpc->das_pre || !mpc->rxbpre_total) {
485 netdev_err(ndev, "No RX pre-allocated bufs\n");
489 /* Check sizes to catch unexpected coding error */
490 if (mpc->rxbpre_datasize != rxq->datasize) {
491 netdev_err(ndev, "rxbpre_datasize mismatch: %u: %u\n",
492 mpc->rxbpre_datasize, rxq->datasize);
496 if (mpc->rxbpre_alloc_size != rxq->alloc_size) {
497 netdev_err(ndev, "rxbpre_alloc_size mismatch: %u: %u\n",
498 mpc->rxbpre_alloc_size, rxq->alloc_size);
502 if (mpc->rxbpre_headroom != rxq->headroom) {
503 netdev_err(ndev, "rxbpre_headroom mismatch: %u: %u\n",
504 mpc->rxbpre_headroom, rxq->headroom);
510 *da = mpc->das_pre[mpc->rxbpre_total];
511 va = mpc->rxbufs_pre[mpc->rxbpre_total];
512 mpc->rxbufs_pre[mpc->rxbpre_total] = NULL;
514 /* Deallocate the array after all buffers are gone */
515 if (!mpc->rxbpre_total)
516 mana_pre_dealloc_rxbufs(mpc);
521 /* Get RX buffer's data size, alloc size, XDP headroom based on MTU */
522 static void mana_get_rxbuf_cfg(int mtu, u32 *datasize, u32 *alloc_size,
525 if (mtu > MANA_XDP_MTU_MAX)
526 *headroom = 0; /* no support for XDP */
528 *headroom = XDP_PACKET_HEADROOM;
530 *alloc_size = mtu + MANA_RXBUF_PAD + *headroom;
532 *datasize = ALIGN(mtu + ETH_HLEN, MANA_RX_DATA_ALIGN);
535 static int mana_pre_alloc_rxbufs(struct mana_port_context *mpc, int new_mtu)
544 mana_get_rxbuf_cfg(new_mtu, &mpc->rxbpre_datasize,
545 &mpc->rxbpre_alloc_size, &mpc->rxbpre_headroom);
547 dev = mpc->ac->gdma_dev->gdma_context->dev;
549 num_rxb = mpc->num_queues * RX_BUFFERS_PER_QUEUE;
551 WARN(mpc->rxbufs_pre, "mana rxbufs_pre exists\n");
552 mpc->rxbufs_pre = kmalloc_array(num_rxb, sizeof(void *), GFP_KERNEL);
553 if (!mpc->rxbufs_pre)
556 mpc->das_pre = kmalloc_array(num_rxb, sizeof(dma_addr_t), GFP_KERNEL);
560 mpc->rxbpre_total = 0;
562 for (i = 0; i < num_rxb; i++) {
563 if (mpc->rxbpre_alloc_size > PAGE_SIZE) {
564 va = netdev_alloc_frag(mpc->rxbpre_alloc_size);
568 page = virt_to_head_page(va);
569 /* Check if the frag falls back to single page */
570 if (compound_order(page) <
571 get_order(mpc->rxbpre_alloc_size)) {
576 page = dev_alloc_page();
580 va = page_to_virt(page);
583 da = dma_map_single(dev, va + mpc->rxbpre_headroom,
584 mpc->rxbpre_datasize, DMA_FROM_DEVICE);
585 if (dma_mapping_error(dev, da)) {
586 put_page(virt_to_head_page(va));
590 mpc->rxbufs_pre[i] = va;
591 mpc->das_pre[i] = da;
592 mpc->rxbpre_total = i + 1;
598 mana_pre_dealloc_rxbufs(mpc);
602 static int mana_change_mtu(struct net_device *ndev, int new_mtu)
604 struct mana_port_context *mpc = netdev_priv(ndev);
605 unsigned int old_mtu = ndev->mtu;
608 /* Pre-allocate buffers to prevent failure in mana_attach later */
609 err = mana_pre_alloc_rxbufs(mpc, new_mtu);
611 netdev_err(ndev, "Insufficient memory for new MTU\n");
615 err = mana_detach(ndev, false);
617 netdev_err(ndev, "mana_detach failed: %d\n", err);
623 err = mana_attach(ndev);
625 netdev_err(ndev, "mana_attach failed: %d\n", err);
630 mana_pre_dealloc_rxbufs(mpc);
634 static const struct net_device_ops mana_devops = {
635 .ndo_open = mana_open,
636 .ndo_stop = mana_close,
637 .ndo_select_queue = mana_select_queue,
638 .ndo_start_xmit = mana_start_xmit,
639 .ndo_validate_addr = eth_validate_addr,
640 .ndo_get_stats64 = mana_get_stats64,
642 .ndo_xdp_xmit = mana_xdp_xmit,
643 .ndo_change_mtu = mana_change_mtu,
646 static void mana_cleanup_port_context(struct mana_port_context *apc)
652 static int mana_init_port_context(struct mana_port_context *apc)
654 apc->rxqs = kcalloc(apc->num_queues, sizeof(struct mana_rxq *),
657 return !apc->rxqs ? -ENOMEM : 0;
660 static int mana_send_request(struct mana_context *ac, void *in_buf,
661 u32 in_len, void *out_buf, u32 out_len)
663 struct gdma_context *gc = ac->gdma_dev->gdma_context;
664 struct gdma_resp_hdr *resp = out_buf;
665 struct gdma_req_hdr *req = in_buf;
666 struct device *dev = gc->dev;
667 static atomic_t activity_id;
670 req->dev_id = gc->mana.dev_id;
671 req->activity_id = atomic_inc_return(&activity_id);
673 err = mana_gd_send_request(gc, in_len, in_buf, out_len,
675 if (err || resp->status) {
676 dev_err(dev, "Failed to send mana message: %d, 0x%x\n",
678 return err ? err : -EPROTO;
681 if (req->dev_id.as_uint32 != resp->dev_id.as_uint32 ||
682 req->activity_id != resp->activity_id) {
683 dev_err(dev, "Unexpected mana message response: %x,%x,%x,%x\n",
684 req->dev_id.as_uint32, resp->dev_id.as_uint32,
685 req->activity_id, resp->activity_id);
692 static int mana_verify_resp_hdr(const struct gdma_resp_hdr *resp_hdr,
693 const enum mana_command_code expected_code,
696 if (resp_hdr->response.msg_type != expected_code)
699 if (resp_hdr->response.msg_version < GDMA_MESSAGE_V1)
702 if (resp_hdr->response.msg_size < min_size)
708 static int mana_pf_register_hw_vport(struct mana_port_context *apc)
710 struct mana_register_hw_vport_resp resp = {};
711 struct mana_register_hw_vport_req req = {};
714 mana_gd_init_req_hdr(&req.hdr, MANA_REGISTER_HW_PORT,
715 sizeof(req), sizeof(resp));
716 req.attached_gfid = 1;
717 req.is_pf_default_vport = 1;
718 req.allow_all_ether_types = 1;
720 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
723 netdev_err(apc->ndev, "Failed to register hw vPort: %d\n", err);
727 err = mana_verify_resp_hdr(&resp.hdr, MANA_REGISTER_HW_PORT,
729 if (err || resp.hdr.status) {
730 netdev_err(apc->ndev, "Failed to register hw vPort: %d, 0x%x\n",
731 err, resp.hdr.status);
732 return err ? err : -EPROTO;
735 apc->port_handle = resp.hw_vport_handle;
739 static void mana_pf_deregister_hw_vport(struct mana_port_context *apc)
741 struct mana_deregister_hw_vport_resp resp = {};
742 struct mana_deregister_hw_vport_req req = {};
745 mana_gd_init_req_hdr(&req.hdr, MANA_DEREGISTER_HW_PORT,
746 sizeof(req), sizeof(resp));
747 req.hw_vport_handle = apc->port_handle;
749 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
752 netdev_err(apc->ndev, "Failed to unregister hw vPort: %d\n",
757 err = mana_verify_resp_hdr(&resp.hdr, MANA_DEREGISTER_HW_PORT,
759 if (err || resp.hdr.status)
760 netdev_err(apc->ndev,
761 "Failed to deregister hw vPort: %d, 0x%x\n",
762 err, resp.hdr.status);
765 static int mana_pf_register_filter(struct mana_port_context *apc)
767 struct mana_register_filter_resp resp = {};
768 struct mana_register_filter_req req = {};
771 mana_gd_init_req_hdr(&req.hdr, MANA_REGISTER_FILTER,
772 sizeof(req), sizeof(resp));
773 req.vport = apc->port_handle;
774 memcpy(req.mac_addr, apc->mac_addr, ETH_ALEN);
776 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
779 netdev_err(apc->ndev, "Failed to register filter: %d\n", err);
783 err = mana_verify_resp_hdr(&resp.hdr, MANA_REGISTER_FILTER,
785 if (err || resp.hdr.status) {
786 netdev_err(apc->ndev, "Failed to register filter: %d, 0x%x\n",
787 err, resp.hdr.status);
788 return err ? err : -EPROTO;
791 apc->pf_filter_handle = resp.filter_handle;
795 static void mana_pf_deregister_filter(struct mana_port_context *apc)
797 struct mana_deregister_filter_resp resp = {};
798 struct mana_deregister_filter_req req = {};
801 mana_gd_init_req_hdr(&req.hdr, MANA_DEREGISTER_FILTER,
802 sizeof(req), sizeof(resp));
803 req.filter_handle = apc->pf_filter_handle;
805 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
808 netdev_err(apc->ndev, "Failed to unregister filter: %d\n",
813 err = mana_verify_resp_hdr(&resp.hdr, MANA_DEREGISTER_FILTER,
815 if (err || resp.hdr.status)
816 netdev_err(apc->ndev,
817 "Failed to deregister filter: %d, 0x%x\n",
818 err, resp.hdr.status);
821 static int mana_query_device_cfg(struct mana_context *ac, u32 proto_major_ver,
822 u32 proto_minor_ver, u32 proto_micro_ver,
825 struct gdma_context *gc = ac->gdma_dev->gdma_context;
826 struct mana_query_device_cfg_resp resp = {};
827 struct mana_query_device_cfg_req req = {};
828 struct device *dev = gc->dev;
831 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_DEV_CONFIG,
832 sizeof(req), sizeof(resp));
834 req.hdr.resp.msg_version = GDMA_MESSAGE_V2;
836 req.proto_major_ver = proto_major_ver;
837 req.proto_minor_ver = proto_minor_ver;
838 req.proto_micro_ver = proto_micro_ver;
840 err = mana_send_request(ac, &req, sizeof(req), &resp, sizeof(resp));
842 dev_err(dev, "Failed to query config: %d", err);
846 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_DEV_CONFIG,
848 if (err || resp.hdr.status) {
849 dev_err(dev, "Invalid query result: %d, 0x%x\n", err,
856 *max_num_vports = resp.max_num_vports;
858 if (resp.hdr.response.msg_version == GDMA_MESSAGE_V2)
859 gc->adapter_mtu = resp.adapter_mtu;
861 gc->adapter_mtu = ETH_FRAME_LEN;
866 static int mana_query_vport_cfg(struct mana_port_context *apc, u32 vport_index,
867 u32 *max_sq, u32 *max_rq, u32 *num_indir_entry)
869 struct mana_query_vport_cfg_resp resp = {};
870 struct mana_query_vport_cfg_req req = {};
873 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_VPORT_CONFIG,
874 sizeof(req), sizeof(resp));
876 req.vport_index = vport_index;
878 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
883 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_VPORT_CONFIG,
891 *max_sq = resp.max_num_sq;
892 *max_rq = resp.max_num_rq;
893 *num_indir_entry = resp.num_indirection_ent;
895 apc->port_handle = resp.vport;
896 ether_addr_copy(apc->mac_addr, resp.mac_addr);
901 void mana_uncfg_vport(struct mana_port_context *apc)
903 mutex_lock(&apc->vport_mutex);
904 apc->vport_use_count--;
905 WARN_ON(apc->vport_use_count < 0);
906 mutex_unlock(&apc->vport_mutex);
908 EXPORT_SYMBOL_NS(mana_uncfg_vport, NET_MANA);
910 int mana_cfg_vport(struct mana_port_context *apc, u32 protection_dom_id,
913 struct mana_config_vport_resp resp = {};
914 struct mana_config_vport_req req = {};
917 /* This function is used to program the Ethernet port in the hardware
918 * table. It can be called from the Ethernet driver or the RDMA driver.
920 * For Ethernet usage, the hardware supports only one active user on a
921 * physical port. The driver checks on the port usage before programming
922 * the hardware when creating the RAW QP (RDMA driver) or exposing the
923 * device to kernel NET layer (Ethernet driver).
925 * Because the RDMA driver doesn't know in advance which QP type the
926 * user will create, it exposes the device with all its ports. The user
927 * may not be able to create RAW QP on a port if this port is already
928 * in used by the Ethernet driver from the kernel.
930 * This physical port limitation only applies to the RAW QP. For RC QP,
931 * the hardware doesn't have this limitation. The user can create RC
932 * QPs on a physical port up to the hardware limits independent of the
933 * Ethernet usage on the same port.
935 mutex_lock(&apc->vport_mutex);
936 if (apc->vport_use_count > 0) {
937 mutex_unlock(&apc->vport_mutex);
940 apc->vport_use_count++;
941 mutex_unlock(&apc->vport_mutex);
943 mana_gd_init_req_hdr(&req.hdr, MANA_CONFIG_VPORT_TX,
944 sizeof(req), sizeof(resp));
945 req.vport = apc->port_handle;
946 req.pdid = protection_dom_id;
947 req.doorbell_pageid = doorbell_pg_id;
949 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
952 netdev_err(apc->ndev, "Failed to configure vPort: %d\n", err);
956 err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_TX,
958 if (err || resp.hdr.status) {
959 netdev_err(apc->ndev, "Failed to configure vPort: %d, 0x%x\n",
960 err, resp.hdr.status);
967 apc->tx_shortform_allowed = resp.short_form_allowed;
968 apc->tx_vp_offset = resp.tx_vport_offset;
970 netdev_info(apc->ndev, "Configured vPort %llu PD %u DB %u\n",
971 apc->port_handle, protection_dom_id, doorbell_pg_id);
974 mana_uncfg_vport(apc);
978 EXPORT_SYMBOL_NS(mana_cfg_vport, NET_MANA);
980 static int mana_cfg_vport_steering(struct mana_port_context *apc,
982 bool update_default_rxobj, bool update_key,
985 u16 num_entries = MANA_INDIRECT_TABLE_SIZE;
986 struct mana_cfg_rx_steer_req_v2 *req;
987 struct mana_cfg_rx_steer_resp resp = {};
988 struct net_device *ndev = apc->ndev;
989 mana_handle_t *req_indir_tab;
993 req_buf_size = sizeof(*req) + sizeof(mana_handle_t) * num_entries;
994 req = kzalloc(req_buf_size, GFP_KERNEL);
998 mana_gd_init_req_hdr(&req->hdr, MANA_CONFIG_VPORT_RX, req_buf_size,
1001 req->hdr.req.msg_version = GDMA_MESSAGE_V2;
1003 req->vport = apc->port_handle;
1004 req->num_indir_entries = num_entries;
1005 req->indir_tab_offset = sizeof(*req);
1006 req->rx_enable = rx;
1007 req->rss_enable = apc->rss_state;
1008 req->update_default_rxobj = update_default_rxobj;
1009 req->update_hashkey = update_key;
1010 req->update_indir_tab = update_tab;
1011 req->default_rxobj = apc->default_rxobj;
1012 req->cqe_coalescing_enable = 0;
1015 memcpy(&req->hashkey, apc->hashkey, MANA_HASH_KEY_SIZE);
1018 req_indir_tab = (mana_handle_t *)(req + 1);
1019 memcpy(req_indir_tab, apc->rxobj_table,
1020 req->num_indir_entries * sizeof(mana_handle_t));
1023 err = mana_send_request(apc->ac, req, req_buf_size, &resp,
1026 netdev_err(ndev, "Failed to configure vPort RX: %d\n", err);
1030 err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_RX,
1033 netdev_err(ndev, "vPort RX configuration failed: %d\n", err);
1037 if (resp.hdr.status) {
1038 netdev_err(ndev, "vPort RX configuration failed: 0x%x\n",
1043 netdev_info(ndev, "Configured steering vPort %llu entries %u\n",
1044 apc->port_handle, num_entries);
1050 int mana_create_wq_obj(struct mana_port_context *apc,
1051 mana_handle_t vport,
1052 u32 wq_type, struct mana_obj_spec *wq_spec,
1053 struct mana_obj_spec *cq_spec,
1054 mana_handle_t *wq_obj)
1056 struct mana_create_wqobj_resp resp = {};
1057 struct mana_create_wqobj_req req = {};
1058 struct net_device *ndev = apc->ndev;
1061 mana_gd_init_req_hdr(&req.hdr, MANA_CREATE_WQ_OBJ,
1062 sizeof(req), sizeof(resp));
1064 req.wq_type = wq_type;
1065 req.wq_gdma_region = wq_spec->gdma_region;
1066 req.cq_gdma_region = cq_spec->gdma_region;
1067 req.wq_size = wq_spec->queue_size;
1068 req.cq_size = cq_spec->queue_size;
1069 req.cq_moderation_ctx_id = cq_spec->modr_ctx_id;
1070 req.cq_parent_qid = cq_spec->attached_eq;
1072 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1075 netdev_err(ndev, "Failed to create WQ object: %d\n", err);
1079 err = mana_verify_resp_hdr(&resp.hdr, MANA_CREATE_WQ_OBJ,
1081 if (err || resp.hdr.status) {
1082 netdev_err(ndev, "Failed to create WQ object: %d, 0x%x\n", err,
1089 if (resp.wq_obj == INVALID_MANA_HANDLE) {
1090 netdev_err(ndev, "Got an invalid WQ object handle\n");
1095 *wq_obj = resp.wq_obj;
1096 wq_spec->queue_index = resp.wq_id;
1097 cq_spec->queue_index = resp.cq_id;
1103 EXPORT_SYMBOL_NS(mana_create_wq_obj, NET_MANA);
1105 void mana_destroy_wq_obj(struct mana_port_context *apc, u32 wq_type,
1106 mana_handle_t wq_obj)
1108 struct mana_destroy_wqobj_resp resp = {};
1109 struct mana_destroy_wqobj_req req = {};
1110 struct net_device *ndev = apc->ndev;
1113 mana_gd_init_req_hdr(&req.hdr, MANA_DESTROY_WQ_OBJ,
1114 sizeof(req), sizeof(resp));
1115 req.wq_type = wq_type;
1116 req.wq_obj_handle = wq_obj;
1118 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1121 netdev_err(ndev, "Failed to destroy WQ object: %d\n", err);
1125 err = mana_verify_resp_hdr(&resp.hdr, MANA_DESTROY_WQ_OBJ,
1127 if (err || resp.hdr.status)
1128 netdev_err(ndev, "Failed to destroy WQ object: %d, 0x%x\n", err,
1131 EXPORT_SYMBOL_NS(mana_destroy_wq_obj, NET_MANA);
1133 static void mana_destroy_eq(struct mana_context *ac)
1135 struct gdma_context *gc = ac->gdma_dev->gdma_context;
1136 struct gdma_queue *eq;
1142 for (i = 0; i < gc->max_num_queues; i++) {
1147 mana_gd_destroy_queue(gc, eq);
1154 static int mana_create_eq(struct mana_context *ac)
1156 struct gdma_dev *gd = ac->gdma_dev;
1157 struct gdma_context *gc = gd->gdma_context;
1158 struct gdma_queue_spec spec = {};
1162 ac->eqs = kcalloc(gc->max_num_queues, sizeof(struct mana_eq),
1167 spec.type = GDMA_EQ;
1168 spec.monitor_avl_buf = false;
1169 spec.queue_size = EQ_SIZE;
1170 spec.eq.callback = NULL;
1171 spec.eq.context = ac->eqs;
1172 spec.eq.log2_throttle_limit = LOG2_EQ_THROTTLE;
1174 for (i = 0; i < gc->max_num_queues; i++) {
1175 err = mana_gd_create_mana_eq(gd, &spec, &ac->eqs[i].eq);
1182 mana_destroy_eq(ac);
1186 static int mana_fence_rq(struct mana_port_context *apc, struct mana_rxq *rxq)
1188 struct mana_fence_rq_resp resp = {};
1189 struct mana_fence_rq_req req = {};
1192 init_completion(&rxq->fence_event);
1194 mana_gd_init_req_hdr(&req.hdr, MANA_FENCE_RQ,
1195 sizeof(req), sizeof(resp));
1196 req.wq_obj_handle = rxq->rxobj;
1198 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1201 netdev_err(apc->ndev, "Failed to fence RQ %u: %d\n",
1206 err = mana_verify_resp_hdr(&resp.hdr, MANA_FENCE_RQ, sizeof(resp));
1207 if (err || resp.hdr.status) {
1208 netdev_err(apc->ndev, "Failed to fence RQ %u: %d, 0x%x\n",
1209 rxq->rxq_idx, err, resp.hdr.status);
1216 if (wait_for_completion_timeout(&rxq->fence_event, 10 * HZ) == 0) {
1217 netdev_err(apc->ndev, "Failed to fence RQ %u: timed out\n",
1225 static void mana_fence_rqs(struct mana_port_context *apc)
1227 unsigned int rxq_idx;
1228 struct mana_rxq *rxq;
1231 for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) {
1232 rxq = apc->rxqs[rxq_idx];
1233 err = mana_fence_rq(apc, rxq);
1235 /* In case of any error, use sleep instead. */
1241 static int mana_move_wq_tail(struct gdma_queue *wq, u32 num_units)
1246 used_space_old = wq->head - wq->tail;
1247 used_space_new = wq->head - (wq->tail + num_units);
1249 if (WARN_ON_ONCE(used_space_new > used_space_old))
1252 wq->tail += num_units;
1256 static void mana_unmap_skb(struct sk_buff *skb, struct mana_port_context *apc)
1258 struct mana_skb_head *ash = (struct mana_skb_head *)skb->head;
1259 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1260 struct device *dev = gc->dev;
1263 dma_unmap_single(dev, ash->dma_handle[0], ash->size[0], DMA_TO_DEVICE);
1265 for (i = 1; i < skb_shinfo(skb)->nr_frags + 1; i++)
1266 dma_unmap_page(dev, ash->dma_handle[i], ash->size[i],
1270 static void mana_poll_tx_cq(struct mana_cq *cq)
1272 struct gdma_comp *completions = cq->gdma_comp_buf;
1273 struct gdma_posted_wqe_info *wqe_info;
1274 unsigned int pkt_transmitted = 0;
1275 unsigned int wqe_unit_cnt = 0;
1276 struct mana_txq *txq = cq->txq;
1277 struct mana_port_context *apc;
1278 struct netdev_queue *net_txq;
1279 struct gdma_queue *gdma_wq;
1280 unsigned int avail_space;
1281 struct net_device *ndev;
1282 struct sk_buff *skb;
1288 apc = netdev_priv(ndev);
1290 comp_read = mana_gd_poll_cq(cq->gdma_cq, completions,
1291 CQE_POLLING_BUFFER);
1296 for (i = 0; i < comp_read; i++) {
1297 struct mana_tx_comp_oob *cqe_oob;
1299 if (WARN_ON_ONCE(!completions[i].is_sq))
1302 cqe_oob = (struct mana_tx_comp_oob *)completions[i].cqe_data;
1303 if (WARN_ON_ONCE(cqe_oob->cqe_hdr.client_type !=
1304 MANA_CQE_COMPLETION))
1307 switch (cqe_oob->cqe_hdr.cqe_type) {
1311 case CQE_TX_SA_DROP:
1312 case CQE_TX_MTU_DROP:
1313 case CQE_TX_INVALID_OOB:
1314 case CQE_TX_INVALID_ETH_TYPE:
1315 case CQE_TX_HDR_PROCESSING_ERROR:
1316 case CQE_TX_VF_DISABLED:
1317 case CQE_TX_VPORT_IDX_OUT_OF_RANGE:
1318 case CQE_TX_VPORT_DISABLED:
1319 case CQE_TX_VLAN_TAGGING_VIOLATION:
1320 WARN_ONCE(1, "TX: CQE error %d: ignored.\n",
1321 cqe_oob->cqe_hdr.cqe_type);
1322 apc->eth_stats.tx_cqe_err++;
1326 /* If the CQE type is unexpected, log an error, assert,
1327 * and go through the error path.
1329 WARN_ONCE(1, "TX: Unexpected CQE type %d: HW BUG?\n",
1330 cqe_oob->cqe_hdr.cqe_type);
1331 apc->eth_stats.tx_cqe_unknown_type++;
1335 if (WARN_ON_ONCE(txq->gdma_txq_id != completions[i].wq_num))
1338 skb = skb_dequeue(&txq->pending_skbs);
1339 if (WARN_ON_ONCE(!skb))
1342 wqe_info = (struct gdma_posted_wqe_info *)skb->cb;
1343 wqe_unit_cnt += wqe_info->wqe_size_in_bu;
1345 mana_unmap_skb(skb, apc);
1347 napi_consume_skb(skb, cq->budget);
1352 if (WARN_ON_ONCE(wqe_unit_cnt == 0))
1355 mana_move_wq_tail(txq->gdma_sq, wqe_unit_cnt);
1357 gdma_wq = txq->gdma_sq;
1358 avail_space = mana_gd_wq_avail_space(gdma_wq);
1360 /* Ensure tail updated before checking q stop */
1363 net_txq = txq->net_txq;
1364 txq_stopped = netif_tx_queue_stopped(net_txq);
1366 /* Ensure checking txq_stopped before apc->port_is_up. */
1369 if (txq_stopped && apc->port_is_up && avail_space >= MAX_TX_WQE_SIZE) {
1370 netif_tx_wake_queue(net_txq);
1371 apc->eth_stats.wake_queue++;
1374 if (atomic_sub_return(pkt_transmitted, &txq->pending_sends) < 0)
1377 cq->work_done = pkt_transmitted;
1380 static void mana_post_pkt_rxq(struct mana_rxq *rxq)
1382 struct mana_recv_buf_oob *recv_buf_oob;
1386 curr_index = rxq->buf_index++;
1387 if (rxq->buf_index == rxq->num_rx_buf)
1390 recv_buf_oob = &rxq->rx_oobs[curr_index];
1392 err = mana_gd_post_work_request(rxq->gdma_rq, &recv_buf_oob->wqe_req,
1393 &recv_buf_oob->wqe_inf);
1394 if (WARN_ON_ONCE(err))
1397 WARN_ON_ONCE(recv_buf_oob->wqe_inf.wqe_size_in_bu != 1);
1400 static struct sk_buff *mana_build_skb(struct mana_rxq *rxq, void *buf_va,
1401 uint pkt_len, struct xdp_buff *xdp)
1403 struct sk_buff *skb = napi_build_skb(buf_va, rxq->alloc_size);
1408 if (xdp->data_hard_start) {
1409 skb_reserve(skb, xdp->data - xdp->data_hard_start);
1410 skb_put(skb, xdp->data_end - xdp->data);
1414 skb_reserve(skb, rxq->headroom);
1415 skb_put(skb, pkt_len);
1420 static void mana_rx_skb(void *buf_va, bool from_pool,
1421 struct mana_rxcomp_oob *cqe, struct mana_rxq *rxq)
1423 struct mana_stats_rx *rx_stats = &rxq->stats;
1424 struct net_device *ndev = rxq->ndev;
1425 uint pkt_len = cqe->ppi[0].pkt_len;
1426 u16 rxq_idx = rxq->rxq_idx;
1427 struct napi_struct *napi;
1428 struct xdp_buff xdp = {};
1429 struct sk_buff *skb;
1433 rxq->rx_cq.work_done++;
1434 napi = &rxq->rx_cq.napi;
1437 ++ndev->stats.rx_dropped;
1441 act = mana_run_xdp(ndev, rxq, &xdp, buf_va, pkt_len);
1443 if (act == XDP_REDIRECT && !rxq->xdp_rc)
1446 if (act != XDP_PASS && act != XDP_TX)
1449 skb = mana_build_skb(rxq, buf_va, pkt_len, &xdp);
1455 skb_mark_for_recycle(skb);
1457 skb->dev = napi->dev;
1459 skb->protocol = eth_type_trans(skb, ndev);
1460 skb_checksum_none_assert(skb);
1461 skb_record_rx_queue(skb, rxq_idx);
1463 if ((ndev->features & NETIF_F_RXCSUM) && cqe->rx_iphdr_csum_succeed) {
1464 if (cqe->rx_tcp_csum_succeed || cqe->rx_udp_csum_succeed)
1465 skb->ip_summed = CHECKSUM_UNNECESSARY;
1468 if (cqe->rx_hashtype != 0 && (ndev->features & NETIF_F_RXHASH)) {
1469 hash_value = cqe->ppi[0].pkt_hash;
1471 if (cqe->rx_hashtype & MANA_HASH_L4)
1472 skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L4);
1474 skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L3);
1477 if (cqe->rx_vlantag_present) {
1478 u16 vlan_tci = cqe->rx_vlan_id;
1480 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
1483 u64_stats_update_begin(&rx_stats->syncp);
1484 rx_stats->packets++;
1485 rx_stats->bytes += pkt_len;
1489 u64_stats_update_end(&rx_stats->syncp);
1491 if (act == XDP_TX) {
1492 skb_set_queue_mapping(skb, rxq_idx);
1493 mana_xdp_tx(skb, ndev);
1497 napi_gro_receive(napi, skb);
1502 u64_stats_update_begin(&rx_stats->syncp);
1503 rx_stats->xdp_drop++;
1504 u64_stats_update_end(&rx_stats->syncp);
1508 page_pool_recycle_direct(rxq->page_pool,
1509 virt_to_head_page(buf_va));
1511 WARN_ON_ONCE(rxq->xdp_save_va);
1512 /* Save for reuse */
1513 rxq->xdp_save_va = buf_va;
1516 ++ndev->stats.rx_dropped;
1521 static void *mana_get_rxfrag(struct mana_rxq *rxq, struct device *dev,
1522 dma_addr_t *da, bool *from_pool, bool is_napi)
1529 /* Reuse XDP dropped page if available */
1530 if (rxq->xdp_save_va) {
1531 va = rxq->xdp_save_va;
1532 rxq->xdp_save_va = NULL;
1533 } else if (rxq->alloc_size > PAGE_SIZE) {
1535 va = napi_alloc_frag(rxq->alloc_size);
1537 va = netdev_alloc_frag(rxq->alloc_size);
1542 page = virt_to_head_page(va);
1543 /* Check if the frag falls back to single page */
1544 if (compound_order(page) < get_order(rxq->alloc_size)) {
1549 page = page_pool_dev_alloc_pages(rxq->page_pool);
1554 va = page_to_virt(page);
1557 *da = dma_map_single(dev, va + rxq->headroom, rxq->datasize,
1559 if (dma_mapping_error(dev, *da)) {
1561 page_pool_put_full_page(rxq->page_pool, page, false);
1563 put_page(virt_to_head_page(va));
1571 /* Allocate frag for rx buffer, and save the old buf */
1572 static void mana_refill_rx_oob(struct device *dev, struct mana_rxq *rxq,
1573 struct mana_recv_buf_oob *rxoob, void **old_buf,
1580 va = mana_get_rxfrag(rxq, dev, &da, &from_pool, true);
1584 dma_unmap_single(dev, rxoob->sgl[0].address, rxq->datasize,
1586 *old_buf = rxoob->buf_va;
1587 *old_fp = rxoob->from_pool;
1590 rxoob->sgl[0].address = da;
1591 rxoob->from_pool = from_pool;
1594 static void mana_process_rx_cqe(struct mana_rxq *rxq, struct mana_cq *cq,
1595 struct gdma_comp *cqe)
1597 struct mana_rxcomp_oob *oob = (struct mana_rxcomp_oob *)cqe->cqe_data;
1598 struct gdma_context *gc = rxq->gdma_rq->gdma_dev->gdma_context;
1599 struct net_device *ndev = rxq->ndev;
1600 struct mana_recv_buf_oob *rxbuf_oob;
1601 struct mana_port_context *apc;
1602 struct device *dev = gc->dev;
1603 void *old_buf = NULL;
1607 apc = netdev_priv(ndev);
1609 switch (oob->cqe_hdr.cqe_type) {
1613 case CQE_RX_TRUNCATED:
1614 ++ndev->stats.rx_dropped;
1615 rxbuf_oob = &rxq->rx_oobs[rxq->buf_index];
1616 netdev_warn_once(ndev, "Dropped a truncated packet\n");
1619 case CQE_RX_COALESCED_4:
1620 netdev_err(ndev, "RX coalescing is unsupported\n");
1621 apc->eth_stats.rx_coalesced_err++;
1624 case CQE_RX_OBJECT_FENCE:
1625 complete(&rxq->fence_event);
1629 netdev_err(ndev, "Unknown RX CQE type = %d\n",
1630 oob->cqe_hdr.cqe_type);
1631 apc->eth_stats.rx_cqe_unknown_type++;
1635 pktlen = oob->ppi[0].pkt_len;
1638 /* data packets should never have packetlength of zero */
1639 netdev_err(ndev, "RX pkt len=0, rq=%u, cq=%u, rxobj=0x%llx\n",
1640 rxq->gdma_id, cq->gdma_id, rxq->rxobj);
1644 curr = rxq->buf_index;
1645 rxbuf_oob = &rxq->rx_oobs[curr];
1646 WARN_ON_ONCE(rxbuf_oob->wqe_inf.wqe_size_in_bu != 1);
1648 mana_refill_rx_oob(dev, rxq, rxbuf_oob, &old_buf, &old_fp);
1650 /* Unsuccessful refill will have old_buf == NULL.
1651 * In this case, mana_rx_skb() will drop the packet.
1653 mana_rx_skb(old_buf, old_fp, oob, rxq);
1656 mana_move_wq_tail(rxq->gdma_rq, rxbuf_oob->wqe_inf.wqe_size_in_bu);
1658 mana_post_pkt_rxq(rxq);
1661 static void mana_poll_rx_cq(struct mana_cq *cq)
1663 struct gdma_comp *comp = cq->gdma_comp_buf;
1664 struct mana_rxq *rxq = cq->rxq;
1667 comp_read = mana_gd_poll_cq(cq->gdma_cq, comp, CQE_POLLING_BUFFER);
1668 WARN_ON_ONCE(comp_read > CQE_POLLING_BUFFER);
1670 rxq->xdp_flush = false;
1672 for (i = 0; i < comp_read; i++) {
1673 if (WARN_ON_ONCE(comp[i].is_sq))
1676 /* verify recv cqe references the right rxq */
1677 if (WARN_ON_ONCE(comp[i].wq_num != cq->rxq->gdma_id))
1680 mana_process_rx_cqe(rxq, cq, &comp[i]);
1683 if (comp_read > 0) {
1684 struct gdma_context *gc = rxq->gdma_rq->gdma_dev->gdma_context;
1686 mana_gd_wq_ring_doorbell(gc, rxq->gdma_rq);
1693 static int mana_cq_handler(void *context, struct gdma_queue *gdma_queue)
1695 struct mana_cq *cq = context;
1699 WARN_ON_ONCE(cq->gdma_cq != gdma_queue);
1701 if (cq->type == MANA_CQ_TYPE_RX)
1702 mana_poll_rx_cq(cq);
1704 mana_poll_tx_cq(cq);
1708 if (w < cq->budget &&
1709 napi_complete_done(&cq->napi, w)) {
1710 arm_bit = SET_ARM_BIT;
1715 mana_gd_ring_cq(gdma_queue, arm_bit);
1720 static int mana_poll(struct napi_struct *napi, int budget)
1722 struct mana_cq *cq = container_of(napi, struct mana_cq, napi);
1726 cq->budget = budget;
1728 w = mana_cq_handler(cq, cq->gdma_cq);
1730 return min(w, budget);
1733 static void mana_schedule_napi(void *context, struct gdma_queue *gdma_queue)
1735 struct mana_cq *cq = context;
1737 napi_schedule_irqoff(&cq->napi);
1740 static void mana_deinit_cq(struct mana_port_context *apc, struct mana_cq *cq)
1742 struct gdma_dev *gd = apc->ac->gdma_dev;
1747 mana_gd_destroy_queue(gd->gdma_context, cq->gdma_cq);
1750 static void mana_deinit_txq(struct mana_port_context *apc, struct mana_txq *txq)
1752 struct gdma_dev *gd = apc->ac->gdma_dev;
1757 mana_gd_destroy_queue(gd->gdma_context, txq->gdma_sq);
1760 static void mana_destroy_txq(struct mana_port_context *apc)
1762 struct napi_struct *napi;
1768 for (i = 0; i < apc->num_queues; i++) {
1769 napi = &apc->tx_qp[i].tx_cq.napi;
1770 napi_synchronize(napi);
1772 netif_napi_del(napi);
1774 mana_destroy_wq_obj(apc, GDMA_SQ, apc->tx_qp[i].tx_object);
1776 mana_deinit_cq(apc, &apc->tx_qp[i].tx_cq);
1778 mana_deinit_txq(apc, &apc->tx_qp[i].txq);
1785 static int mana_create_txq(struct mana_port_context *apc,
1786 struct net_device *net)
1788 struct mana_context *ac = apc->ac;
1789 struct gdma_dev *gd = ac->gdma_dev;
1790 struct mana_obj_spec wq_spec;
1791 struct mana_obj_spec cq_spec;
1792 struct gdma_queue_spec spec;
1793 struct gdma_context *gc;
1794 struct mana_txq *txq;
1801 apc->tx_qp = kcalloc(apc->num_queues, sizeof(struct mana_tx_qp),
1806 /* The minimum size of the WQE is 32 bytes, hence
1807 * MAX_SEND_BUFFERS_PER_QUEUE represents the maximum number of WQEs
1808 * the SQ can store. This value is then used to size other queues
1809 * to prevent overflow.
1811 txq_size = MAX_SEND_BUFFERS_PER_QUEUE * 32;
1812 BUILD_BUG_ON(!PAGE_ALIGNED(txq_size));
1814 cq_size = MAX_SEND_BUFFERS_PER_QUEUE * COMP_ENTRY_SIZE;
1815 cq_size = PAGE_ALIGN(cq_size);
1817 gc = gd->gdma_context;
1819 for (i = 0; i < apc->num_queues; i++) {
1820 apc->tx_qp[i].tx_object = INVALID_MANA_HANDLE;
1823 txq = &apc->tx_qp[i].txq;
1825 u64_stats_init(&txq->stats.syncp);
1827 txq->net_txq = netdev_get_tx_queue(net, i);
1828 txq->vp_offset = apc->tx_vp_offset;
1829 skb_queue_head_init(&txq->pending_skbs);
1831 memset(&spec, 0, sizeof(spec));
1832 spec.type = GDMA_SQ;
1833 spec.monitor_avl_buf = true;
1834 spec.queue_size = txq_size;
1835 err = mana_gd_create_mana_wq_cq(gd, &spec, &txq->gdma_sq);
1839 /* Create SQ's CQ */
1840 cq = &apc->tx_qp[i].tx_cq;
1841 cq->type = MANA_CQ_TYPE_TX;
1845 memset(&spec, 0, sizeof(spec));
1846 spec.type = GDMA_CQ;
1847 spec.monitor_avl_buf = false;
1848 spec.queue_size = cq_size;
1849 spec.cq.callback = mana_schedule_napi;
1850 spec.cq.parent_eq = ac->eqs[i].eq;
1851 spec.cq.context = cq;
1852 err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq);
1856 memset(&wq_spec, 0, sizeof(wq_spec));
1857 memset(&cq_spec, 0, sizeof(cq_spec));
1859 wq_spec.gdma_region = txq->gdma_sq->mem_info.dma_region_handle;
1860 wq_spec.queue_size = txq->gdma_sq->queue_size;
1862 cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle;
1863 cq_spec.queue_size = cq->gdma_cq->queue_size;
1864 cq_spec.modr_ctx_id = 0;
1865 cq_spec.attached_eq = cq->gdma_cq->cq.parent->id;
1867 err = mana_create_wq_obj(apc, apc->port_handle, GDMA_SQ,
1869 &apc->tx_qp[i].tx_object);
1874 txq->gdma_sq->id = wq_spec.queue_index;
1875 cq->gdma_cq->id = cq_spec.queue_index;
1877 txq->gdma_sq->mem_info.dma_region_handle =
1878 GDMA_INVALID_DMA_REGION;
1879 cq->gdma_cq->mem_info.dma_region_handle =
1880 GDMA_INVALID_DMA_REGION;
1882 txq->gdma_txq_id = txq->gdma_sq->id;
1884 cq->gdma_id = cq->gdma_cq->id;
1886 if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) {
1891 gc->cq_table[cq->gdma_id] = cq->gdma_cq;
1893 netif_napi_add_tx(net, &cq->napi, mana_poll);
1894 napi_enable(&cq->napi);
1896 mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT);
1901 mana_destroy_txq(apc);
1905 static void mana_destroy_rxq(struct mana_port_context *apc,
1906 struct mana_rxq *rxq, bool validate_state)
1909 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1910 struct mana_recv_buf_oob *rx_oob;
1911 struct device *dev = gc->dev;
1912 struct napi_struct *napi;
1919 napi = &rxq->rx_cq.napi;
1922 napi_synchronize(napi);
1926 xdp_rxq_info_unreg(&rxq->xdp_rxq);
1928 netif_napi_del(napi);
1930 mana_destroy_wq_obj(apc, GDMA_RQ, rxq->rxobj);
1932 mana_deinit_cq(apc, &rxq->rx_cq);
1934 if (rxq->xdp_save_va)
1935 put_page(virt_to_head_page(rxq->xdp_save_va));
1937 for (i = 0; i < rxq->num_rx_buf; i++) {
1938 rx_oob = &rxq->rx_oobs[i];
1940 if (!rx_oob->buf_va)
1943 dma_unmap_single(dev, rx_oob->sgl[0].address,
1944 rx_oob->sgl[0].size, DMA_FROM_DEVICE);
1946 page = virt_to_head_page(rx_oob->buf_va);
1948 if (rx_oob->from_pool)
1949 page_pool_put_full_page(rxq->page_pool, page, false);
1953 rx_oob->buf_va = NULL;
1956 page_pool_destroy(rxq->page_pool);
1959 mana_gd_destroy_queue(gc, rxq->gdma_rq);
1964 static int mana_fill_rx_oob(struct mana_recv_buf_oob *rx_oob, u32 mem_key,
1965 struct mana_rxq *rxq, struct device *dev)
1967 struct mana_port_context *mpc = netdev_priv(rxq->ndev);
1968 bool from_pool = false;
1972 if (mpc->rxbufs_pre)
1973 va = mana_get_rxbuf_pre(rxq, &da);
1975 va = mana_get_rxfrag(rxq, dev, &da, &from_pool, false);
1980 rx_oob->buf_va = va;
1981 rx_oob->from_pool = from_pool;
1983 rx_oob->sgl[0].address = da;
1984 rx_oob->sgl[0].size = rxq->datasize;
1985 rx_oob->sgl[0].mem_key = mem_key;
1990 #define MANA_WQE_HEADER_SIZE 16
1991 #define MANA_WQE_SGE_SIZE 16
1993 static int mana_alloc_rx_wqe(struct mana_port_context *apc,
1994 struct mana_rxq *rxq, u32 *rxq_size, u32 *cq_size)
1996 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1997 struct mana_recv_buf_oob *rx_oob;
1998 struct device *dev = gc->dev;
2002 WARN_ON(rxq->datasize == 0);
2007 for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) {
2008 rx_oob = &rxq->rx_oobs[buf_idx];
2009 memset(rx_oob, 0, sizeof(*rx_oob));
2011 rx_oob->num_sge = 1;
2013 ret = mana_fill_rx_oob(rx_oob, apc->ac->gdma_dev->gpa_mkey, rxq,
2018 rx_oob->wqe_req.sgl = rx_oob->sgl;
2019 rx_oob->wqe_req.num_sge = rx_oob->num_sge;
2020 rx_oob->wqe_req.inline_oob_size = 0;
2021 rx_oob->wqe_req.inline_oob_data = NULL;
2022 rx_oob->wqe_req.flags = 0;
2023 rx_oob->wqe_req.client_data_unit = 0;
2025 *rxq_size += ALIGN(MANA_WQE_HEADER_SIZE +
2026 MANA_WQE_SGE_SIZE * rx_oob->num_sge, 32);
2027 *cq_size += COMP_ENTRY_SIZE;
2033 static int mana_push_wqe(struct mana_rxq *rxq)
2035 struct mana_recv_buf_oob *rx_oob;
2039 for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) {
2040 rx_oob = &rxq->rx_oobs[buf_idx];
2042 err = mana_gd_post_and_ring(rxq->gdma_rq, &rx_oob->wqe_req,
2051 static int mana_create_page_pool(struct mana_rxq *rxq, struct gdma_context *gc)
2053 struct page_pool_params pprm = {};
2056 pprm.pool_size = RX_BUFFERS_PER_QUEUE;
2057 pprm.nid = gc->numa_node;
2058 pprm.napi = &rxq->rx_cq.napi;
2060 rxq->page_pool = page_pool_create(&pprm);
2062 if (IS_ERR(rxq->page_pool)) {
2063 ret = PTR_ERR(rxq->page_pool);
2064 rxq->page_pool = NULL;
2071 static struct mana_rxq *mana_create_rxq(struct mana_port_context *apc,
2072 u32 rxq_idx, struct mana_eq *eq,
2073 struct net_device *ndev)
2075 struct gdma_dev *gd = apc->ac->gdma_dev;
2076 struct mana_obj_spec wq_spec;
2077 struct mana_obj_spec cq_spec;
2078 struct gdma_queue_spec spec;
2079 struct mana_cq *cq = NULL;
2080 struct gdma_context *gc;
2081 u32 cq_size, rq_size;
2082 struct mana_rxq *rxq;
2085 gc = gd->gdma_context;
2087 rxq = kzalloc(struct_size(rxq, rx_oobs, RX_BUFFERS_PER_QUEUE),
2093 rxq->num_rx_buf = RX_BUFFERS_PER_QUEUE;
2094 rxq->rxq_idx = rxq_idx;
2095 rxq->rxobj = INVALID_MANA_HANDLE;
2097 mana_get_rxbuf_cfg(ndev->mtu, &rxq->datasize, &rxq->alloc_size,
2100 /* Create page pool for RX queue */
2101 err = mana_create_page_pool(rxq, gc);
2103 netdev_err(ndev, "Create page pool err:%d\n", err);
2107 err = mana_alloc_rx_wqe(apc, rxq, &rq_size, &cq_size);
2111 rq_size = PAGE_ALIGN(rq_size);
2112 cq_size = PAGE_ALIGN(cq_size);
2115 memset(&spec, 0, sizeof(spec));
2116 spec.type = GDMA_RQ;
2117 spec.monitor_avl_buf = true;
2118 spec.queue_size = rq_size;
2119 err = mana_gd_create_mana_wq_cq(gd, &spec, &rxq->gdma_rq);
2123 /* Create RQ's CQ */
2125 cq->type = MANA_CQ_TYPE_RX;
2128 memset(&spec, 0, sizeof(spec));
2129 spec.type = GDMA_CQ;
2130 spec.monitor_avl_buf = false;
2131 spec.queue_size = cq_size;
2132 spec.cq.callback = mana_schedule_napi;
2133 spec.cq.parent_eq = eq->eq;
2134 spec.cq.context = cq;
2135 err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq);
2139 memset(&wq_spec, 0, sizeof(wq_spec));
2140 memset(&cq_spec, 0, sizeof(cq_spec));
2141 wq_spec.gdma_region = rxq->gdma_rq->mem_info.dma_region_handle;
2142 wq_spec.queue_size = rxq->gdma_rq->queue_size;
2144 cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle;
2145 cq_spec.queue_size = cq->gdma_cq->queue_size;
2146 cq_spec.modr_ctx_id = 0;
2147 cq_spec.attached_eq = cq->gdma_cq->cq.parent->id;
2149 err = mana_create_wq_obj(apc, apc->port_handle, GDMA_RQ,
2150 &wq_spec, &cq_spec, &rxq->rxobj);
2154 rxq->gdma_rq->id = wq_spec.queue_index;
2155 cq->gdma_cq->id = cq_spec.queue_index;
2157 rxq->gdma_rq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION;
2158 cq->gdma_cq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION;
2160 rxq->gdma_id = rxq->gdma_rq->id;
2161 cq->gdma_id = cq->gdma_cq->id;
2163 err = mana_push_wqe(rxq);
2167 if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) {
2172 gc->cq_table[cq->gdma_id] = cq->gdma_cq;
2174 netif_napi_add_weight(ndev, &cq->napi, mana_poll, 1);
2176 WARN_ON(xdp_rxq_info_reg(&rxq->xdp_rxq, ndev, rxq_idx,
2178 WARN_ON(xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL,
2181 napi_enable(&cq->napi);
2183 mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT);
2188 netdev_err(ndev, "Failed to create RXQ: err = %d\n", err);
2190 mana_destroy_rxq(apc, rxq, false);
2193 mana_deinit_cq(apc, cq);
2198 static int mana_add_rx_queues(struct mana_port_context *apc,
2199 struct net_device *ndev)
2201 struct mana_context *ac = apc->ac;
2202 struct mana_rxq *rxq;
2206 for (i = 0; i < apc->num_queues; i++) {
2207 rxq = mana_create_rxq(apc, i, &ac->eqs[i], ndev);
2213 u64_stats_init(&rxq->stats.syncp);
2218 apc->default_rxobj = apc->rxqs[0]->rxobj;
2223 static void mana_destroy_vport(struct mana_port_context *apc)
2225 struct gdma_dev *gd = apc->ac->gdma_dev;
2226 struct mana_rxq *rxq;
2229 for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) {
2230 rxq = apc->rxqs[rxq_idx];
2234 mana_destroy_rxq(apc, rxq, true);
2235 apc->rxqs[rxq_idx] = NULL;
2238 mana_destroy_txq(apc);
2239 mana_uncfg_vport(apc);
2241 if (gd->gdma_context->is_pf)
2242 mana_pf_deregister_hw_vport(apc);
2245 static int mana_create_vport(struct mana_port_context *apc,
2246 struct net_device *net)
2248 struct gdma_dev *gd = apc->ac->gdma_dev;
2251 apc->default_rxobj = INVALID_MANA_HANDLE;
2253 if (gd->gdma_context->is_pf) {
2254 err = mana_pf_register_hw_vport(apc);
2259 err = mana_cfg_vport(apc, gd->pdid, gd->doorbell);
2263 return mana_create_txq(apc, net);
2266 static void mana_rss_table_init(struct mana_port_context *apc)
2270 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
2271 apc->indir_table[i] =
2272 ethtool_rxfh_indir_default(i, apc->num_queues);
2275 int mana_config_rss(struct mana_port_context *apc, enum TRI_STATE rx,
2276 bool update_hash, bool update_tab)
2283 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) {
2284 queue_idx = apc->indir_table[i];
2285 apc->rxobj_table[i] = apc->rxqs[queue_idx]->rxobj;
2289 err = mana_cfg_vport_steering(apc, rx, true, update_hash, update_tab);
2293 mana_fence_rqs(apc);
2298 void mana_query_gf_stats(struct mana_port_context *apc)
2300 struct mana_query_gf_stat_resp resp = {};
2301 struct mana_query_gf_stat_req req = {};
2302 struct net_device *ndev = apc->ndev;
2305 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_GF_STAT,
2306 sizeof(req), sizeof(resp));
2307 req.req_stats = STATISTICS_FLAGS_HC_TX_BYTES |
2308 STATISTICS_FLAGS_HC_TX_UCAST_PACKETS |
2309 STATISTICS_FLAGS_HC_TX_UCAST_BYTES |
2310 STATISTICS_FLAGS_HC_TX_MCAST_PACKETS |
2311 STATISTICS_FLAGS_HC_TX_MCAST_BYTES |
2312 STATISTICS_FLAGS_HC_TX_BCAST_PACKETS |
2313 STATISTICS_FLAGS_HC_TX_BCAST_BYTES;
2315 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
2318 netdev_err(ndev, "Failed to query GF stats: %d\n", err);
2321 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_GF_STAT,
2323 if (err || resp.hdr.status) {
2324 netdev_err(ndev, "Failed to query GF stats: %d, 0x%x\n", err,
2329 apc->eth_stats.hc_tx_bytes = resp.hc_tx_bytes;
2330 apc->eth_stats.hc_tx_ucast_pkts = resp.hc_tx_ucast_pkts;
2331 apc->eth_stats.hc_tx_ucast_bytes = resp.hc_tx_ucast_bytes;
2332 apc->eth_stats.hc_tx_bcast_pkts = resp.hc_tx_bcast_pkts;
2333 apc->eth_stats.hc_tx_bcast_bytes = resp.hc_tx_bcast_bytes;
2334 apc->eth_stats.hc_tx_mcast_pkts = resp.hc_tx_mcast_pkts;
2335 apc->eth_stats.hc_tx_mcast_bytes = resp.hc_tx_mcast_bytes;
2338 static int mana_init_port(struct net_device *ndev)
2340 struct mana_port_context *apc = netdev_priv(ndev);
2341 u32 max_txq, max_rxq, max_queues;
2342 int port_idx = apc->port_idx;
2343 u32 num_indirect_entries;
2346 err = mana_init_port_context(apc);
2350 err = mana_query_vport_cfg(apc, port_idx, &max_txq, &max_rxq,
2351 &num_indirect_entries);
2353 netdev_err(ndev, "Failed to query info for vPort %d\n",
2358 max_queues = min_t(u32, max_txq, max_rxq);
2359 if (apc->max_queues > max_queues)
2360 apc->max_queues = max_queues;
2362 if (apc->num_queues > apc->max_queues)
2363 apc->num_queues = apc->max_queues;
2365 eth_hw_addr_set(ndev, apc->mac_addr);
2375 int mana_alloc_queues(struct net_device *ndev)
2377 struct mana_port_context *apc = netdev_priv(ndev);
2378 struct gdma_dev *gd = apc->ac->gdma_dev;
2381 err = mana_create_vport(apc, ndev);
2385 err = netif_set_real_num_tx_queues(ndev, apc->num_queues);
2389 err = mana_add_rx_queues(apc, ndev);
2393 apc->rss_state = apc->num_queues > 1 ? TRI_STATE_TRUE : TRI_STATE_FALSE;
2395 err = netif_set_real_num_rx_queues(ndev, apc->num_queues);
2399 mana_rss_table_init(apc);
2401 err = mana_config_rss(apc, TRI_STATE_TRUE, true, true);
2405 if (gd->gdma_context->is_pf) {
2406 err = mana_pf_register_filter(apc);
2411 mana_chn_setxdp(apc, mana_xdp_get(apc));
2416 mana_destroy_vport(apc);
2420 int mana_attach(struct net_device *ndev)
2422 struct mana_port_context *apc = netdev_priv(ndev);
2427 err = mana_init_port(ndev);
2431 if (apc->port_st_save) {
2432 err = mana_alloc_queues(ndev);
2434 mana_cleanup_port_context(apc);
2439 apc->port_is_up = apc->port_st_save;
2441 /* Ensure port state updated before txq state */
2444 if (apc->port_is_up)
2445 netif_carrier_on(ndev);
2447 netif_device_attach(ndev);
2452 static int mana_dealloc_queues(struct net_device *ndev)
2454 struct mana_port_context *apc = netdev_priv(ndev);
2455 unsigned long timeout = jiffies + 120 * HZ;
2456 struct gdma_dev *gd = apc->ac->gdma_dev;
2457 struct mana_txq *txq;
2458 struct sk_buff *skb;
2462 if (apc->port_is_up)
2465 mana_chn_setxdp(apc, NULL);
2467 if (gd->gdma_context->is_pf)
2468 mana_pf_deregister_filter(apc);
2470 /* No packet can be transmitted now since apc->port_is_up is false.
2471 * There is still a tiny chance that mana_poll_tx_cq() can re-enable
2472 * a txq because it may not timely see apc->port_is_up being cleared
2473 * to false, but it doesn't matter since mana_start_xmit() drops any
2474 * new packets due to apc->port_is_up being false.
2476 * Drain all the in-flight TX packets.
2477 * A timeout of 120 seconds for all the queues is used.
2478 * This will break the while loop when h/w is not responding.
2479 * This value of 120 has been decided here considering max
2483 for (i = 0; i < apc->num_queues; i++) {
2484 txq = &apc->tx_qp[i].txq;
2486 while (atomic_read(&txq->pending_sends) > 0 &&
2487 time_before(jiffies, timeout)) {
2488 usleep_range(tsleep, tsleep + 1000);
2491 if (atomic_read(&txq->pending_sends)) {
2492 err = pcie_flr(to_pci_dev(gd->gdma_context->dev));
2494 netdev_err(ndev, "flr failed %d with %d pkts pending in txq %u\n",
2495 err, atomic_read(&txq->pending_sends),
2502 for (i = 0; i < apc->num_queues; i++) {
2503 txq = &apc->tx_qp[i].txq;
2504 while ((skb = skb_dequeue(&txq->pending_skbs))) {
2505 mana_unmap_skb(skb, apc);
2506 dev_kfree_skb_any(skb);
2508 atomic_set(&txq->pending_sends, 0);
2510 /* We're 100% sure the queues can no longer be woken up, because
2511 * we're sure now mana_poll_tx_cq() can't be running.
2514 apc->rss_state = TRI_STATE_FALSE;
2515 err = mana_config_rss(apc, TRI_STATE_FALSE, false, false);
2517 netdev_err(ndev, "Failed to disable vPort: %d\n", err);
2521 mana_destroy_vport(apc);
2526 int mana_detach(struct net_device *ndev, bool from_close)
2528 struct mana_port_context *apc = netdev_priv(ndev);
2533 apc->port_st_save = apc->port_is_up;
2534 apc->port_is_up = false;
2536 /* Ensure port state updated before txq state */
2539 netif_tx_disable(ndev);
2540 netif_carrier_off(ndev);
2542 if (apc->port_st_save) {
2543 err = mana_dealloc_queues(ndev);
2549 netif_device_detach(ndev);
2550 mana_cleanup_port_context(apc);
2556 static int mana_probe_port(struct mana_context *ac, int port_idx,
2557 struct net_device **ndev_storage)
2559 struct gdma_context *gc = ac->gdma_dev->gdma_context;
2560 struct mana_port_context *apc;
2561 struct net_device *ndev;
2564 ndev = alloc_etherdev_mq(sizeof(struct mana_port_context),
2565 gc->max_num_queues);
2569 *ndev_storage = ndev;
2571 apc = netdev_priv(ndev);
2574 apc->max_queues = gc->max_num_queues;
2575 apc->num_queues = gc->max_num_queues;
2576 apc->port_handle = INVALID_MANA_HANDLE;
2577 apc->pf_filter_handle = INVALID_MANA_HANDLE;
2578 apc->port_idx = port_idx;
2580 mutex_init(&apc->vport_mutex);
2581 apc->vport_use_count = 0;
2583 ndev->netdev_ops = &mana_devops;
2584 ndev->ethtool_ops = &mana_ethtool_ops;
2585 ndev->mtu = ETH_DATA_LEN;
2586 ndev->max_mtu = gc->adapter_mtu - ETH_HLEN;
2587 ndev->min_mtu = ETH_MIN_MTU;
2588 ndev->needed_headroom = MANA_HEADROOM;
2589 ndev->dev_port = port_idx;
2590 SET_NETDEV_DEV(ndev, gc->dev);
2592 netif_carrier_off(ndev);
2594 netdev_rss_key_fill(apc->hashkey, MANA_HASH_KEY_SIZE);
2596 err = mana_init_port(ndev);
2600 netdev_lockdep_set_classes(ndev);
2602 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2603 ndev->hw_features |= NETIF_F_RXCSUM;
2604 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2605 ndev->hw_features |= NETIF_F_RXHASH;
2606 ndev->features = ndev->hw_features | NETIF_F_HW_VLAN_CTAG_TX |
2607 NETIF_F_HW_VLAN_CTAG_RX;
2608 ndev->vlan_features = ndev->features;
2609 ndev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
2610 NETDEV_XDP_ACT_NDO_XMIT;
2612 err = register_netdev(ndev);
2614 netdev_err(ndev, "Unable to register netdev.\n");
2624 *ndev_storage = NULL;
2625 netdev_err(ndev, "Failed to probe vPort %d: %d\n", port_idx, err);
2630 static void adev_release(struct device *dev)
2632 struct mana_adev *madev = container_of(dev, struct mana_adev, adev.dev);
2637 static void remove_adev(struct gdma_dev *gd)
2639 struct auxiliary_device *adev = gd->adev;
2642 auxiliary_device_delete(adev);
2643 auxiliary_device_uninit(adev);
2645 mana_adev_idx_free(id);
2649 static int add_adev(struct gdma_dev *gd)
2651 struct auxiliary_device *adev;
2652 struct mana_adev *madev;
2655 madev = kzalloc(sizeof(*madev), GFP_KERNEL);
2659 adev = &madev->adev;
2660 ret = mana_adev_idx_alloc();
2665 adev->name = "rdma";
2666 adev->dev.parent = gd->gdma_context->dev;
2667 adev->dev.release = adev_release;
2670 ret = auxiliary_device_init(adev);
2674 ret = auxiliary_device_add(adev);
2682 auxiliary_device_uninit(adev);
2685 mana_adev_idx_free(adev->id);
2693 int mana_probe(struct gdma_dev *gd, bool resuming)
2695 struct gdma_context *gc = gd->gdma_context;
2696 struct mana_context *ac = gd->driver_data;
2697 struct device *dev = gc->dev;
2703 "Microsoft Azure Network Adapter protocol version: %d.%d.%d\n",
2704 MANA_MAJOR_VERSION, MANA_MINOR_VERSION, MANA_MICRO_VERSION);
2706 err = mana_gd_register_device(gd);
2711 ac = kzalloc(sizeof(*ac), GFP_KERNEL);
2716 gd->driver_data = ac;
2719 err = mana_create_eq(ac);
2723 err = mana_query_device_cfg(ac, MANA_MAJOR_VERSION, MANA_MINOR_VERSION,
2724 MANA_MICRO_VERSION, &num_ports);
2729 ac->num_ports = num_ports;
2731 if (ac->num_ports != num_ports) {
2732 dev_err(dev, "The number of vPorts changed: %d->%d\n",
2733 ac->num_ports, num_ports);
2739 if (ac->num_ports == 0)
2740 dev_err(dev, "Failed to detect any vPort\n");
2742 if (ac->num_ports > MAX_PORTS_IN_MANA_DEV)
2743 ac->num_ports = MAX_PORTS_IN_MANA_DEV;
2746 for (i = 0; i < ac->num_ports; i++) {
2747 err = mana_probe_port(ac, i, &ac->ports[i]);
2752 for (i = 0; i < ac->num_ports; i++) {
2754 err = mana_attach(ac->ports[i]);
2764 mana_remove(gd, false);
2769 void mana_remove(struct gdma_dev *gd, bool suspending)
2771 struct gdma_context *gc = gd->gdma_context;
2772 struct mana_context *ac = gd->driver_data;
2773 struct device *dev = gc->dev;
2774 struct net_device *ndev;
2778 /* adev currently doesn't support suspending, always remove it */
2782 for (i = 0; i < ac->num_ports; i++) {
2783 ndev = ac->ports[i];
2786 dev_err(dev, "No net device to remove\n");
2790 /* All cleanup actions should stay after rtnl_lock(), otherwise
2791 * other functions may access partially cleaned up data.
2795 err = mana_detach(ndev, false);
2797 netdev_err(ndev, "Failed to detach vPort %d: %d\n",
2801 /* No need to unregister the ndev. */
2806 unregister_netdevice(ndev);
2813 mana_destroy_eq(ac);
2815 mana_gd_deregister_device(gd);
2820 gd->driver_data = NULL;
2821 gd->gdma_context = NULL;