1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2021, Microsoft Corporation. */
4 #include <linux/inetdevice.h>
5 #include <linux/etherdevice.h>
6 #include <linux/ethtool.h>
9 #include <net/checksum.h>
10 #include <net/ip6_checksum.h>
14 /* Microsoft Azure Network Adapter (MANA) functions */
16 static int mana_open(struct net_device *ndev)
18 struct mana_port_context *apc = netdev_priv(ndev);
21 err = mana_alloc_queues(ndev);
25 apc->port_is_up = true;
27 /* Ensure port state updated before txq state */
30 netif_carrier_on(ndev);
31 netif_tx_wake_all_queues(ndev);
36 static int mana_close(struct net_device *ndev)
38 struct mana_port_context *apc = netdev_priv(ndev);
43 return mana_detach(ndev, true);
46 static bool mana_can_tx(struct gdma_queue *wq)
48 return mana_gd_wq_avail_space(wq) >= MAX_TX_WQE_SIZE;
51 static unsigned int mana_checksum_info(struct sk_buff *skb)
53 if (skb->protocol == htons(ETH_P_IP)) {
54 struct iphdr *ip = ip_hdr(skb);
56 if (ip->protocol == IPPROTO_TCP)
59 if (ip->protocol == IPPROTO_UDP)
61 } else if (skb->protocol == htons(ETH_P_IPV6)) {
62 struct ipv6hdr *ip6 = ipv6_hdr(skb);
64 if (ip6->nexthdr == IPPROTO_TCP)
67 if (ip6->nexthdr == IPPROTO_UDP)
71 /* No csum offloading */
75 static int mana_map_skb(struct sk_buff *skb, struct mana_port_context *apc,
76 struct mana_tx_package *tp)
78 struct mana_skb_head *ash = (struct mana_skb_head *)skb->head;
79 struct gdma_dev *gd = apc->ac->gdma_dev;
80 struct gdma_context *gc;
86 gc = gd->gdma_context;
88 da = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
90 if (dma_mapping_error(dev, da))
93 ash->dma_handle[0] = da;
94 ash->size[0] = skb_headlen(skb);
96 tp->wqe_req.sgl[0].address = ash->dma_handle[0];
97 tp->wqe_req.sgl[0].mem_key = gd->gpa_mkey;
98 tp->wqe_req.sgl[0].size = ash->size[0];
100 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
101 frag = &skb_shinfo(skb)->frags[i];
102 da = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
105 if (dma_mapping_error(dev, da))
108 ash->dma_handle[i + 1] = da;
109 ash->size[i + 1] = skb_frag_size(frag);
111 tp->wqe_req.sgl[i + 1].address = ash->dma_handle[i + 1];
112 tp->wqe_req.sgl[i + 1].mem_key = gd->gpa_mkey;
113 tp->wqe_req.sgl[i + 1].size = ash->size[i + 1];
119 for (i = i - 1; i >= 0; i--)
120 dma_unmap_page(dev, ash->dma_handle[i + 1], ash->size[i + 1],
123 dma_unmap_single(dev, ash->dma_handle[0], ash->size[0], DMA_TO_DEVICE);
128 static int mana_start_xmit(struct sk_buff *skb, struct net_device *ndev)
130 enum mana_tx_pkt_format pkt_fmt = MANA_SHORT_PKT_FMT;
131 struct mana_port_context *apc = netdev_priv(ndev);
132 u16 txq_idx = skb_get_queue_mapping(skb);
133 struct gdma_dev *gd = apc->ac->gdma_dev;
134 bool ipv4 = false, ipv6 = false;
135 struct mana_tx_package pkg = {};
136 struct netdev_queue *net_txq;
137 struct mana_stats *tx_stats;
138 struct gdma_queue *gdma_sq;
139 unsigned int csum_type;
140 struct mana_txq *txq;
144 if (unlikely(!apc->port_is_up))
147 if (skb_cow_head(skb, MANA_HEADROOM))
150 txq = &apc->tx_qp[txq_idx].txq;
151 gdma_sq = txq->gdma_sq;
152 cq = &apc->tx_qp[txq_idx].tx_cq;
154 pkg.tx_oob.s_oob.vcq_num = cq->gdma_id;
155 pkg.tx_oob.s_oob.vsq_frame = txq->vsq_frame;
157 if (txq->vp_offset > MANA_SHORT_VPORT_OFFSET_MAX) {
158 pkg.tx_oob.l_oob.long_vp_offset = txq->vp_offset;
159 pkt_fmt = MANA_LONG_PKT_FMT;
161 pkg.tx_oob.s_oob.short_vp_offset = txq->vp_offset;
164 pkg.tx_oob.s_oob.pkt_fmt = pkt_fmt;
166 if (pkt_fmt == MANA_SHORT_PKT_FMT)
167 pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_short_oob);
169 pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_oob);
171 pkg.wqe_req.inline_oob_data = &pkg.tx_oob;
172 pkg.wqe_req.flags = 0;
173 pkg.wqe_req.client_data_unit = 0;
175 pkg.wqe_req.num_sge = 1 + skb_shinfo(skb)->nr_frags;
176 WARN_ON_ONCE(pkg.wqe_req.num_sge > 30);
178 if (pkg.wqe_req.num_sge <= ARRAY_SIZE(pkg.sgl_array)) {
179 pkg.wqe_req.sgl = pkg.sgl_array;
181 pkg.sgl_ptr = kmalloc_array(pkg.wqe_req.num_sge,
182 sizeof(struct gdma_sge),
187 pkg.wqe_req.sgl = pkg.sgl_ptr;
190 if (skb->protocol == htons(ETH_P_IP))
192 else if (skb->protocol == htons(ETH_P_IPV6))
195 if (skb_is_gso(skb)) {
196 pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
197 pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
199 pkg.tx_oob.s_oob.comp_iphdr_csum = 1;
200 pkg.tx_oob.s_oob.comp_tcp_csum = 1;
201 pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb);
203 pkg.wqe_req.client_data_unit = skb_shinfo(skb)->gso_size;
204 pkg.wqe_req.flags = GDMA_WR_OOB_IN_SGL | GDMA_WR_PAD_BY_SGE0;
206 ip_hdr(skb)->tot_len = 0;
207 ip_hdr(skb)->check = 0;
208 tcp_hdr(skb)->check =
209 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
210 ip_hdr(skb)->daddr, 0,
213 ipv6_hdr(skb)->payload_len = 0;
214 tcp_hdr(skb)->check =
215 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
216 &ipv6_hdr(skb)->daddr, 0,
219 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
220 csum_type = mana_checksum_info(skb);
222 if (csum_type == IPPROTO_TCP) {
223 pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
224 pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
226 pkg.tx_oob.s_oob.comp_tcp_csum = 1;
227 pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb);
229 } else if (csum_type == IPPROTO_UDP) {
230 pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
231 pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
233 pkg.tx_oob.s_oob.comp_udp_csum = 1;
235 /* Can't do offload of this type of checksum */
236 if (skb_checksum_help(skb))
241 if (mana_map_skb(skb, apc, &pkg))
244 skb_queue_tail(&txq->pending_skbs, skb);
247 net_txq = netdev_get_tx_queue(ndev, txq_idx);
249 err = mana_gd_post_work_request(gdma_sq, &pkg.wqe_req,
250 (struct gdma_posted_wqe_info *)skb->cb);
251 if (!mana_can_tx(gdma_sq)) {
252 netif_tx_stop_queue(net_txq);
253 apc->eth_stats.stop_queue++;
257 (void)skb_dequeue_tail(&txq->pending_skbs);
258 netdev_warn(ndev, "Failed to post TX OOB: %d\n", err);
259 err = NETDEV_TX_BUSY;
264 atomic_inc(&txq->pending_sends);
266 mana_gd_wq_ring_doorbell(gd->gdma_context, gdma_sq);
268 /* skb may be freed after mana_gd_post_work_request. Do not use it. */
271 tx_stats = &txq->stats;
272 u64_stats_update_begin(&tx_stats->syncp);
274 tx_stats->bytes += len;
275 u64_stats_update_end(&tx_stats->syncp);
278 if (netif_tx_queue_stopped(net_txq) && mana_can_tx(gdma_sq)) {
279 netif_tx_wake_queue(net_txq);
280 apc->eth_stats.wake_queue++;
289 ndev->stats.tx_dropped++;
291 dev_kfree_skb_any(skb);
295 static void mana_get_stats64(struct net_device *ndev,
296 struct rtnl_link_stats64 *st)
298 struct mana_port_context *apc = netdev_priv(ndev);
299 unsigned int num_queues = apc->num_queues;
300 struct mana_stats *stats;
305 if (!apc->port_is_up)
308 netdev_stats_to_stats64(st, &ndev->stats);
310 for (q = 0; q < num_queues; q++) {
311 stats = &apc->rxqs[q]->stats;
314 start = u64_stats_fetch_begin_irq(&stats->syncp);
315 packets = stats->packets;
316 bytes = stats->bytes;
317 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
319 st->rx_packets += packets;
320 st->rx_bytes += bytes;
323 for (q = 0; q < num_queues; q++) {
324 stats = &apc->tx_qp[q].txq.stats;
327 start = u64_stats_fetch_begin_irq(&stats->syncp);
328 packets = stats->packets;
329 bytes = stats->bytes;
330 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
332 st->tx_packets += packets;
333 st->tx_bytes += bytes;
337 static int mana_get_tx_queue(struct net_device *ndev, struct sk_buff *skb,
340 struct mana_port_context *apc = netdev_priv(ndev);
341 u32 hash = skb_get_hash(skb);
342 struct sock *sk = skb->sk;
345 txq = apc->indir_table[hash & MANA_INDIRECT_TABLE_MASK];
347 if (txq != old_q && sk && sk_fullsock(sk) &&
348 rcu_access_pointer(sk->sk_dst_cache))
349 sk_tx_queue_set(sk, txq);
354 static u16 mana_select_queue(struct net_device *ndev, struct sk_buff *skb,
355 struct net_device *sb_dev)
359 if (ndev->real_num_tx_queues == 1)
362 txq = sk_tx_queue_get(skb->sk);
364 if (txq < 0 || skb->ooo_okay || txq >= ndev->real_num_tx_queues) {
365 if (skb_rx_queue_recorded(skb))
366 txq = skb_get_rx_queue(skb);
368 txq = mana_get_tx_queue(ndev, skb, txq);
374 static const struct net_device_ops mana_devops = {
375 .ndo_open = mana_open,
376 .ndo_stop = mana_close,
377 .ndo_select_queue = mana_select_queue,
378 .ndo_start_xmit = mana_start_xmit,
379 .ndo_validate_addr = eth_validate_addr,
380 .ndo_get_stats64 = mana_get_stats64,
383 static void mana_cleanup_port_context(struct mana_port_context *apc)
389 static int mana_init_port_context(struct mana_port_context *apc)
391 apc->rxqs = kcalloc(apc->num_queues, sizeof(struct mana_rxq *),
394 return !apc->rxqs ? -ENOMEM : 0;
397 static int mana_send_request(struct mana_context *ac, void *in_buf,
398 u32 in_len, void *out_buf, u32 out_len)
400 struct gdma_context *gc = ac->gdma_dev->gdma_context;
401 struct gdma_resp_hdr *resp = out_buf;
402 struct gdma_req_hdr *req = in_buf;
403 struct device *dev = gc->dev;
404 static atomic_t activity_id;
407 req->dev_id = gc->mana.dev_id;
408 req->activity_id = atomic_inc_return(&activity_id);
410 err = mana_gd_send_request(gc, in_len, in_buf, out_len,
412 if (err || resp->status) {
413 dev_err(dev, "Failed to send mana message: %d, 0x%x\n",
415 return err ? err : -EPROTO;
418 if (req->dev_id.as_uint32 != resp->dev_id.as_uint32 ||
419 req->activity_id != resp->activity_id) {
420 dev_err(dev, "Unexpected mana message response: %x,%x,%x,%x\n",
421 req->dev_id.as_uint32, resp->dev_id.as_uint32,
422 req->activity_id, resp->activity_id);
429 static int mana_verify_resp_hdr(const struct gdma_resp_hdr *resp_hdr,
430 const enum mana_command_code expected_code,
433 if (resp_hdr->response.msg_type != expected_code)
436 if (resp_hdr->response.msg_version < GDMA_MESSAGE_V1)
439 if (resp_hdr->response.msg_size < min_size)
445 static int mana_query_device_cfg(struct mana_context *ac, u32 proto_major_ver,
446 u32 proto_minor_ver, u32 proto_micro_ver,
449 struct gdma_context *gc = ac->gdma_dev->gdma_context;
450 struct mana_query_device_cfg_resp resp = {};
451 struct mana_query_device_cfg_req req = {};
452 struct device *dev = gc->dev;
455 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_DEV_CONFIG,
456 sizeof(req), sizeof(resp));
457 req.proto_major_ver = proto_major_ver;
458 req.proto_minor_ver = proto_minor_ver;
459 req.proto_micro_ver = proto_micro_ver;
461 err = mana_send_request(ac, &req, sizeof(req), &resp, sizeof(resp));
463 dev_err(dev, "Failed to query config: %d", err);
467 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_DEV_CONFIG,
469 if (err || resp.hdr.status) {
470 dev_err(dev, "Invalid query result: %d, 0x%x\n", err,
477 *max_num_vports = resp.max_num_vports;
482 static int mana_query_vport_cfg(struct mana_port_context *apc, u32 vport_index,
483 u32 *max_sq, u32 *max_rq, u32 *num_indir_entry)
485 struct mana_query_vport_cfg_resp resp = {};
486 struct mana_query_vport_cfg_req req = {};
489 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_VPORT_CONFIG,
490 sizeof(req), sizeof(resp));
492 req.vport_index = vport_index;
494 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
499 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_VPORT_CONFIG,
507 *max_sq = resp.max_num_sq;
508 *max_rq = resp.max_num_rq;
509 *num_indir_entry = resp.num_indirection_ent;
511 apc->port_handle = resp.vport;
512 ether_addr_copy(apc->mac_addr, resp.mac_addr);
517 static int mana_cfg_vport(struct mana_port_context *apc, u32 protection_dom_id,
520 struct mana_config_vport_resp resp = {};
521 struct mana_config_vport_req req = {};
524 mana_gd_init_req_hdr(&req.hdr, MANA_CONFIG_VPORT_TX,
525 sizeof(req), sizeof(resp));
526 req.vport = apc->port_handle;
527 req.pdid = protection_dom_id;
528 req.doorbell_pageid = doorbell_pg_id;
530 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
533 netdev_err(apc->ndev, "Failed to configure vPort: %d\n", err);
537 err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_TX,
539 if (err || resp.hdr.status) {
540 netdev_err(apc->ndev, "Failed to configure vPort: %d, 0x%x\n",
541 err, resp.hdr.status);
548 apc->tx_shortform_allowed = resp.short_form_allowed;
549 apc->tx_vp_offset = resp.tx_vport_offset;
554 static int mana_cfg_vport_steering(struct mana_port_context *apc,
556 bool update_default_rxobj, bool update_key,
559 u16 num_entries = MANA_INDIRECT_TABLE_SIZE;
560 struct mana_cfg_rx_steer_req *req = NULL;
561 struct mana_cfg_rx_steer_resp resp = {};
562 struct net_device *ndev = apc->ndev;
563 mana_handle_t *req_indir_tab;
567 req_buf_size = sizeof(*req) + sizeof(mana_handle_t) * num_entries;
568 req = kzalloc(req_buf_size, GFP_KERNEL);
572 mana_gd_init_req_hdr(&req->hdr, MANA_CONFIG_VPORT_RX, req_buf_size,
575 req->vport = apc->port_handle;
576 req->num_indir_entries = num_entries;
577 req->indir_tab_offset = sizeof(*req);
579 req->rss_enable = apc->rss_state;
580 req->update_default_rxobj = update_default_rxobj;
581 req->update_hashkey = update_key;
582 req->update_indir_tab = update_tab;
583 req->default_rxobj = apc->default_rxobj;
586 memcpy(&req->hashkey, apc->hashkey, MANA_HASH_KEY_SIZE);
589 req_indir_tab = (mana_handle_t *)(req + 1);
590 memcpy(req_indir_tab, apc->rxobj_table,
591 req->num_indir_entries * sizeof(mana_handle_t));
594 err = mana_send_request(apc->ac, req, req_buf_size, &resp,
597 netdev_err(ndev, "Failed to configure vPort RX: %d\n", err);
601 err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_RX,
604 netdev_err(ndev, "vPort RX configuration failed: %d\n", err);
608 if (resp.hdr.status) {
609 netdev_err(ndev, "vPort RX configuration failed: 0x%x\n",
618 static int mana_create_wq_obj(struct mana_port_context *apc,
620 u32 wq_type, struct mana_obj_spec *wq_spec,
621 struct mana_obj_spec *cq_spec,
622 mana_handle_t *wq_obj)
624 struct mana_create_wqobj_resp resp = {};
625 struct mana_create_wqobj_req req = {};
626 struct net_device *ndev = apc->ndev;
629 mana_gd_init_req_hdr(&req.hdr, MANA_CREATE_WQ_OBJ,
630 sizeof(req), sizeof(resp));
632 req.wq_type = wq_type;
633 req.wq_gdma_region = wq_spec->gdma_region;
634 req.cq_gdma_region = cq_spec->gdma_region;
635 req.wq_size = wq_spec->queue_size;
636 req.cq_size = cq_spec->queue_size;
637 req.cq_moderation_ctx_id = cq_spec->modr_ctx_id;
638 req.cq_parent_qid = cq_spec->attached_eq;
640 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
643 netdev_err(ndev, "Failed to create WQ object: %d\n", err);
647 err = mana_verify_resp_hdr(&resp.hdr, MANA_CREATE_WQ_OBJ,
649 if (err || resp.hdr.status) {
650 netdev_err(ndev, "Failed to create WQ object: %d, 0x%x\n", err,
657 if (resp.wq_obj == INVALID_MANA_HANDLE) {
658 netdev_err(ndev, "Got an invalid WQ object handle\n");
663 *wq_obj = resp.wq_obj;
664 wq_spec->queue_index = resp.wq_id;
665 cq_spec->queue_index = resp.cq_id;
672 static void mana_destroy_wq_obj(struct mana_port_context *apc, u32 wq_type,
673 mana_handle_t wq_obj)
675 struct mana_destroy_wqobj_resp resp = {};
676 struct mana_destroy_wqobj_req req = {};
677 struct net_device *ndev = apc->ndev;
680 mana_gd_init_req_hdr(&req.hdr, MANA_DESTROY_WQ_OBJ,
681 sizeof(req), sizeof(resp));
682 req.wq_type = wq_type;
683 req.wq_obj_handle = wq_obj;
685 err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
688 netdev_err(ndev, "Failed to destroy WQ object: %d\n", err);
692 err = mana_verify_resp_hdr(&resp.hdr, MANA_DESTROY_WQ_OBJ,
694 if (err || resp.hdr.status)
695 netdev_err(ndev, "Failed to destroy WQ object: %d, 0x%x\n", err,
699 static void mana_init_cqe_poll_buf(struct gdma_comp *cqe_poll_buf)
703 for (i = 0; i < CQE_POLLING_BUFFER; i++)
704 memset(&cqe_poll_buf[i], 0, sizeof(struct gdma_comp));
707 static void mana_destroy_eq(struct gdma_context *gc,
708 struct mana_port_context *apc)
710 struct gdma_queue *eq;
716 for (i = 0; i < apc->num_queues; i++) {
721 mana_gd_destroy_queue(gc, eq);
728 static int mana_create_eq(struct mana_port_context *apc)
730 struct gdma_dev *gd = apc->ac->gdma_dev;
731 struct gdma_queue_spec spec = {};
735 apc->eqs = kcalloc(apc->num_queues, sizeof(struct mana_eq),
741 spec.monitor_avl_buf = false;
742 spec.queue_size = EQ_SIZE;
743 spec.eq.callback = NULL;
744 spec.eq.context = apc->eqs;
745 spec.eq.log2_throttle_limit = LOG2_EQ_THROTTLE;
746 spec.eq.ndev = apc->ndev;
748 for (i = 0; i < apc->num_queues; i++) {
749 mana_init_cqe_poll_buf(apc->eqs[i].cqe_poll);
751 err = mana_gd_create_mana_eq(gd, &spec, &apc->eqs[i].eq);
758 mana_destroy_eq(gd->gdma_context, apc);
762 static int mana_move_wq_tail(struct gdma_queue *wq, u32 num_units)
767 used_space_old = wq->head - wq->tail;
768 used_space_new = wq->head - (wq->tail + num_units);
770 if (WARN_ON_ONCE(used_space_new > used_space_old))
773 wq->tail += num_units;
777 static void mana_unmap_skb(struct sk_buff *skb, struct mana_port_context *apc)
779 struct mana_skb_head *ash = (struct mana_skb_head *)skb->head;
780 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
781 struct device *dev = gc->dev;
784 dma_unmap_single(dev, ash->dma_handle[0], ash->size[0], DMA_TO_DEVICE);
786 for (i = 1; i < skb_shinfo(skb)->nr_frags + 1; i++)
787 dma_unmap_page(dev, ash->dma_handle[i], ash->size[i],
791 static void mana_poll_tx_cq(struct mana_cq *cq)
793 struct gdma_queue *gdma_eq = cq->gdma_cq->cq.parent;
794 struct gdma_comp *completions = cq->gdma_comp_buf;
795 struct gdma_posted_wqe_info *wqe_info;
796 unsigned int pkt_transmitted = 0;
797 unsigned int wqe_unit_cnt = 0;
798 struct mana_txq *txq = cq->txq;
799 struct mana_port_context *apc;
800 struct netdev_queue *net_txq;
801 struct gdma_queue *gdma_wq;
802 unsigned int avail_space;
803 struct net_device *ndev;
810 apc = netdev_priv(ndev);
812 comp_read = mana_gd_poll_cq(cq->gdma_cq, completions,
815 for (i = 0; i < comp_read; i++) {
816 struct mana_tx_comp_oob *cqe_oob;
818 if (WARN_ON_ONCE(!completions[i].is_sq))
821 cqe_oob = (struct mana_tx_comp_oob *)completions[i].cqe_data;
822 if (WARN_ON_ONCE(cqe_oob->cqe_hdr.client_type !=
823 MANA_CQE_COMPLETION))
826 switch (cqe_oob->cqe_hdr.cqe_type) {
831 case CQE_TX_MTU_DROP:
832 case CQE_TX_INVALID_OOB:
833 case CQE_TX_INVALID_ETH_TYPE:
834 case CQE_TX_HDR_PROCESSING_ERROR:
835 case CQE_TX_VF_DISABLED:
836 case CQE_TX_VPORT_IDX_OUT_OF_RANGE:
837 case CQE_TX_VPORT_DISABLED:
838 case CQE_TX_VLAN_TAGGING_VIOLATION:
839 WARN_ONCE(1, "TX: CQE error %d: ignored.\n",
840 cqe_oob->cqe_hdr.cqe_type);
844 /* If the CQE type is unexpected, log an error, assert,
845 * and go through the error path.
847 WARN_ONCE(1, "TX: Unexpected CQE type %d: HW BUG?\n",
848 cqe_oob->cqe_hdr.cqe_type);
852 if (WARN_ON_ONCE(txq->gdma_txq_id != completions[i].wq_num))
855 skb = skb_dequeue(&txq->pending_skbs);
856 if (WARN_ON_ONCE(!skb))
859 wqe_info = (struct gdma_posted_wqe_info *)skb->cb;
860 wqe_unit_cnt += wqe_info->wqe_size_in_bu;
862 mana_unmap_skb(skb, apc);
864 napi_consume_skb(skb, gdma_eq->eq.budget);
869 if (WARN_ON_ONCE(wqe_unit_cnt == 0))
872 mana_move_wq_tail(txq->gdma_sq, wqe_unit_cnt);
874 gdma_wq = txq->gdma_sq;
875 avail_space = mana_gd_wq_avail_space(gdma_wq);
877 /* Ensure tail updated before checking q stop */
880 net_txq = txq->net_txq;
881 txq_stopped = netif_tx_queue_stopped(net_txq);
883 /* Ensure checking txq_stopped before apc->port_is_up. */
886 if (txq_stopped && apc->port_is_up && avail_space >= MAX_TX_WQE_SIZE) {
887 netif_tx_wake_queue(net_txq);
888 apc->eth_stats.wake_queue++;
891 if (atomic_sub_return(pkt_transmitted, &txq->pending_sends) < 0)
895 static void mana_post_pkt_rxq(struct mana_rxq *rxq)
897 struct mana_recv_buf_oob *recv_buf_oob;
901 curr_index = rxq->buf_index++;
902 if (rxq->buf_index == rxq->num_rx_buf)
905 recv_buf_oob = &rxq->rx_oobs[curr_index];
907 err = mana_gd_post_and_ring(rxq->gdma_rq, &recv_buf_oob->wqe_req,
908 &recv_buf_oob->wqe_inf);
909 if (WARN_ON_ONCE(err))
912 WARN_ON_ONCE(recv_buf_oob->wqe_inf.wqe_size_in_bu != 1);
915 static void mana_rx_skb(void *buf_va, struct mana_rxcomp_oob *cqe,
916 struct mana_rxq *rxq)
918 struct mana_stats *rx_stats = &rxq->stats;
919 struct net_device *ndev = rxq->ndev;
920 uint pkt_len = cqe->ppi[0].pkt_len;
921 struct mana_port_context *apc;
922 u16 rxq_idx = rxq->rxq_idx;
923 struct napi_struct *napi;
924 struct gdma_queue *eq;
928 apc = netdev_priv(ndev);
929 eq = apc->eqs[rxq_idx].eq;
934 ++ndev->stats.rx_dropped;
938 skb = build_skb(buf_va, PAGE_SIZE);
941 free_page((unsigned long)buf_va);
942 ++ndev->stats.rx_dropped;
946 skb_put(skb, pkt_len);
947 skb->dev = napi->dev;
949 skb->protocol = eth_type_trans(skb, ndev);
950 skb_checksum_none_assert(skb);
951 skb_record_rx_queue(skb, rxq_idx);
953 if ((ndev->features & NETIF_F_RXCSUM) && cqe->rx_iphdr_csum_succeed) {
954 if (cqe->rx_tcp_csum_succeed || cqe->rx_udp_csum_succeed)
955 skb->ip_summed = CHECKSUM_UNNECESSARY;
958 if (cqe->rx_hashtype != 0 && (ndev->features & NETIF_F_RXHASH)) {
959 hash_value = cqe->ppi[0].pkt_hash;
961 if (cqe->rx_hashtype & MANA_HASH_L4)
962 skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L4);
964 skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L3);
967 napi_gro_receive(napi, skb);
969 u64_stats_update_begin(&rx_stats->syncp);
971 rx_stats->bytes += pkt_len;
972 u64_stats_update_end(&rx_stats->syncp);
975 static void mana_process_rx_cqe(struct mana_rxq *rxq, struct mana_cq *cq,
976 struct gdma_comp *cqe)
978 struct mana_rxcomp_oob *oob = (struct mana_rxcomp_oob *)cqe->cqe_data;
979 struct gdma_context *gc = rxq->gdma_rq->gdma_dev->gdma_context;
980 struct net_device *ndev = rxq->ndev;
981 struct mana_recv_buf_oob *rxbuf_oob;
982 struct device *dev = gc->dev;
983 void *new_buf, *old_buf;
984 struct page *new_page;
988 switch (oob->cqe_hdr.cqe_type) {
992 case CQE_RX_TRUNCATED:
993 netdev_err(ndev, "Dropped a truncated packet\n");
996 case CQE_RX_COALESCED_4:
997 netdev_err(ndev, "RX coalescing is unsupported\n");
1000 case CQE_RX_OBJECT_FENCE:
1001 netdev_err(ndev, "RX Fencing is unsupported\n");
1005 netdev_err(ndev, "Unknown RX CQE type = %d\n",
1006 oob->cqe_hdr.cqe_type);
1010 if (oob->cqe_hdr.cqe_type != CQE_RX_OKAY)
1013 pktlen = oob->ppi[0].pkt_len;
1016 /* data packets should never have packetlength of zero */
1017 netdev_err(ndev, "RX pkt len=0, rq=%u, cq=%u, rxobj=0x%llx\n",
1018 rxq->gdma_id, cq->gdma_id, rxq->rxobj);
1022 curr = rxq->buf_index;
1023 rxbuf_oob = &rxq->rx_oobs[curr];
1024 WARN_ON_ONCE(rxbuf_oob->wqe_inf.wqe_size_in_bu != 1);
1026 new_page = alloc_page(GFP_ATOMIC);
1029 da = dma_map_page(dev, new_page, 0, rxq->datasize,
1032 if (dma_mapping_error(dev, da)) {
1033 __free_page(new_page);
1038 new_buf = new_page ? page_to_virt(new_page) : NULL;
1041 dma_unmap_page(dev, rxbuf_oob->buf_dma_addr, rxq->datasize,
1044 old_buf = rxbuf_oob->buf_va;
1046 /* refresh the rxbuf_oob with the new page */
1047 rxbuf_oob->buf_va = new_buf;
1048 rxbuf_oob->buf_dma_addr = da;
1049 rxbuf_oob->sgl[0].address = rxbuf_oob->buf_dma_addr;
1051 old_buf = NULL; /* drop the packet if no memory */
1054 mana_rx_skb(old_buf, oob, rxq);
1056 mana_move_wq_tail(rxq->gdma_rq, rxbuf_oob->wqe_inf.wqe_size_in_bu);
1058 mana_post_pkt_rxq(rxq);
1061 static void mana_poll_rx_cq(struct mana_cq *cq)
1063 struct gdma_comp *comp = cq->gdma_comp_buf;
1066 comp_read = mana_gd_poll_cq(cq->gdma_cq, comp, CQE_POLLING_BUFFER);
1067 WARN_ON_ONCE(comp_read > CQE_POLLING_BUFFER);
1069 for (i = 0; i < comp_read; i++) {
1070 if (WARN_ON_ONCE(comp[i].is_sq))
1073 /* verify recv cqe references the right rxq */
1074 if (WARN_ON_ONCE(comp[i].wq_num != cq->rxq->gdma_id))
1077 mana_process_rx_cqe(cq->rxq, cq, &comp[i]);
1081 static void mana_cq_handler(void *context, struct gdma_queue *gdma_queue)
1083 struct mana_cq *cq = context;
1085 WARN_ON_ONCE(cq->gdma_cq != gdma_queue);
1087 if (cq->type == MANA_CQ_TYPE_RX)
1088 mana_poll_rx_cq(cq);
1090 mana_poll_tx_cq(cq);
1092 mana_gd_arm_cq(gdma_queue);
1095 static void mana_deinit_cq(struct mana_port_context *apc, struct mana_cq *cq)
1097 struct gdma_dev *gd = apc->ac->gdma_dev;
1102 mana_gd_destroy_queue(gd->gdma_context, cq->gdma_cq);
1105 static void mana_deinit_txq(struct mana_port_context *apc, struct mana_txq *txq)
1107 struct gdma_dev *gd = apc->ac->gdma_dev;
1112 mana_gd_destroy_queue(gd->gdma_context, txq->gdma_sq);
1115 static void mana_destroy_txq(struct mana_port_context *apc)
1122 for (i = 0; i < apc->num_queues; i++) {
1123 mana_destroy_wq_obj(apc, GDMA_SQ, apc->tx_qp[i].tx_object);
1125 mana_deinit_cq(apc, &apc->tx_qp[i].tx_cq);
1127 mana_deinit_txq(apc, &apc->tx_qp[i].txq);
1134 static int mana_create_txq(struct mana_port_context *apc,
1135 struct net_device *net)
1137 struct gdma_dev *gd = apc->ac->gdma_dev;
1138 struct mana_obj_spec wq_spec;
1139 struct mana_obj_spec cq_spec;
1140 struct gdma_queue_spec spec;
1141 struct gdma_context *gc;
1142 struct mana_txq *txq;
1149 apc->tx_qp = kcalloc(apc->num_queues, sizeof(struct mana_tx_qp),
1154 /* The minimum size of the WQE is 32 bytes, hence
1155 * MAX_SEND_BUFFERS_PER_QUEUE represents the maximum number of WQEs
1156 * the SQ can store. This value is then used to size other queues
1157 * to prevent overflow.
1159 txq_size = MAX_SEND_BUFFERS_PER_QUEUE * 32;
1160 BUILD_BUG_ON(!PAGE_ALIGNED(txq_size));
1162 cq_size = MAX_SEND_BUFFERS_PER_QUEUE * COMP_ENTRY_SIZE;
1163 cq_size = PAGE_ALIGN(cq_size);
1165 gc = gd->gdma_context;
1167 for (i = 0; i < apc->num_queues; i++) {
1168 apc->tx_qp[i].tx_object = INVALID_MANA_HANDLE;
1171 txq = &apc->tx_qp[i].txq;
1173 u64_stats_init(&txq->stats.syncp);
1175 txq->net_txq = netdev_get_tx_queue(net, i);
1176 txq->vp_offset = apc->tx_vp_offset;
1177 skb_queue_head_init(&txq->pending_skbs);
1179 memset(&spec, 0, sizeof(spec));
1180 spec.type = GDMA_SQ;
1181 spec.monitor_avl_buf = true;
1182 spec.queue_size = txq_size;
1183 err = mana_gd_create_mana_wq_cq(gd, &spec, &txq->gdma_sq);
1187 /* Create SQ's CQ */
1188 cq = &apc->tx_qp[i].tx_cq;
1189 cq->gdma_comp_buf = apc->eqs[i].cqe_poll;
1190 cq->type = MANA_CQ_TYPE_TX;
1194 memset(&spec, 0, sizeof(spec));
1195 spec.type = GDMA_CQ;
1196 spec.monitor_avl_buf = false;
1197 spec.queue_size = cq_size;
1198 spec.cq.callback = mana_cq_handler;
1199 spec.cq.parent_eq = apc->eqs[i].eq;
1200 spec.cq.context = cq;
1201 err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq);
1205 memset(&wq_spec, 0, sizeof(wq_spec));
1206 memset(&cq_spec, 0, sizeof(cq_spec));
1208 wq_spec.gdma_region = txq->gdma_sq->mem_info.gdma_region;
1209 wq_spec.queue_size = txq->gdma_sq->queue_size;
1211 cq_spec.gdma_region = cq->gdma_cq->mem_info.gdma_region;
1212 cq_spec.queue_size = cq->gdma_cq->queue_size;
1213 cq_spec.modr_ctx_id = 0;
1214 cq_spec.attached_eq = cq->gdma_cq->cq.parent->id;
1216 err = mana_create_wq_obj(apc, apc->port_handle, GDMA_SQ,
1218 &apc->tx_qp[i].tx_object);
1223 txq->gdma_sq->id = wq_spec.queue_index;
1224 cq->gdma_cq->id = cq_spec.queue_index;
1226 txq->gdma_sq->mem_info.gdma_region = GDMA_INVALID_DMA_REGION;
1227 cq->gdma_cq->mem_info.gdma_region = GDMA_INVALID_DMA_REGION;
1229 txq->gdma_txq_id = txq->gdma_sq->id;
1231 cq->gdma_id = cq->gdma_cq->id;
1233 if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) {
1238 gc->cq_table[cq->gdma_id] = cq->gdma_cq;
1240 mana_gd_arm_cq(cq->gdma_cq);
1245 mana_destroy_txq(apc);
1249 static void mana_napi_sync_for_rx(struct mana_rxq *rxq)
1251 struct net_device *ndev = rxq->ndev;
1252 struct mana_port_context *apc;
1253 u16 rxq_idx = rxq->rxq_idx;
1254 struct napi_struct *napi;
1255 struct gdma_queue *eq;
1257 apc = netdev_priv(ndev);
1258 eq = apc->eqs[rxq_idx].eq;
1259 napi = &eq->eq.napi;
1261 napi_synchronize(napi);
1264 static void mana_destroy_rxq(struct mana_port_context *apc,
1265 struct mana_rxq *rxq, bool validate_state)
1268 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1269 struct mana_recv_buf_oob *rx_oob;
1270 struct device *dev = gc->dev;
1277 mana_napi_sync_for_rx(rxq);
1279 mana_destroy_wq_obj(apc, GDMA_RQ, rxq->rxobj);
1281 mana_deinit_cq(apc, &rxq->rx_cq);
1283 for (i = 0; i < rxq->num_rx_buf; i++) {
1284 rx_oob = &rxq->rx_oobs[i];
1286 if (!rx_oob->buf_va)
1289 dma_unmap_page(dev, rx_oob->buf_dma_addr, rxq->datasize,
1292 free_page((unsigned long)rx_oob->buf_va);
1293 rx_oob->buf_va = NULL;
1297 mana_gd_destroy_queue(gc, rxq->gdma_rq);
1302 #define MANA_WQE_HEADER_SIZE 16
1303 #define MANA_WQE_SGE_SIZE 16
1305 static int mana_alloc_rx_wqe(struct mana_port_context *apc,
1306 struct mana_rxq *rxq, u32 *rxq_size, u32 *cq_size)
1308 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1309 struct mana_recv_buf_oob *rx_oob;
1310 struct device *dev = gc->dev;
1315 WARN_ON(rxq->datasize == 0 || rxq->datasize > PAGE_SIZE);
1320 for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) {
1321 rx_oob = &rxq->rx_oobs[buf_idx];
1322 memset(rx_oob, 0, sizeof(*rx_oob));
1324 page = alloc_page(GFP_KERNEL);
1328 da = dma_map_page(dev, page, 0, rxq->datasize, DMA_FROM_DEVICE);
1330 if (dma_mapping_error(dev, da)) {
1335 rx_oob->buf_va = page_to_virt(page);
1336 rx_oob->buf_dma_addr = da;
1338 rx_oob->num_sge = 1;
1339 rx_oob->sgl[0].address = rx_oob->buf_dma_addr;
1340 rx_oob->sgl[0].size = rxq->datasize;
1341 rx_oob->sgl[0].mem_key = apc->ac->gdma_dev->gpa_mkey;
1343 rx_oob->wqe_req.sgl = rx_oob->sgl;
1344 rx_oob->wqe_req.num_sge = rx_oob->num_sge;
1345 rx_oob->wqe_req.inline_oob_size = 0;
1346 rx_oob->wqe_req.inline_oob_data = NULL;
1347 rx_oob->wqe_req.flags = 0;
1348 rx_oob->wqe_req.client_data_unit = 0;
1350 *rxq_size += ALIGN(MANA_WQE_HEADER_SIZE +
1351 MANA_WQE_SGE_SIZE * rx_oob->num_sge, 32);
1352 *cq_size += COMP_ENTRY_SIZE;
1358 static int mana_push_wqe(struct mana_rxq *rxq)
1360 struct mana_recv_buf_oob *rx_oob;
1364 for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) {
1365 rx_oob = &rxq->rx_oobs[buf_idx];
1367 err = mana_gd_post_and_ring(rxq->gdma_rq, &rx_oob->wqe_req,
1376 static struct mana_rxq *mana_create_rxq(struct mana_port_context *apc,
1377 u32 rxq_idx, struct mana_eq *eq,
1378 struct net_device *ndev)
1380 struct gdma_dev *gd = apc->ac->gdma_dev;
1381 struct mana_obj_spec wq_spec;
1382 struct mana_obj_spec cq_spec;
1383 struct gdma_queue_spec spec;
1384 struct mana_cq *cq = NULL;
1385 struct gdma_context *gc;
1386 u32 cq_size, rq_size;
1387 struct mana_rxq *rxq;
1390 gc = gd->gdma_context;
1392 rxq = kzalloc(struct_size(rxq, rx_oobs, RX_BUFFERS_PER_QUEUE),
1398 rxq->num_rx_buf = RX_BUFFERS_PER_QUEUE;
1399 rxq->rxq_idx = rxq_idx;
1400 rxq->datasize = ALIGN(MAX_FRAME_SIZE, 64);
1401 rxq->rxobj = INVALID_MANA_HANDLE;
1403 err = mana_alloc_rx_wqe(apc, rxq, &rq_size, &cq_size);
1407 rq_size = PAGE_ALIGN(rq_size);
1408 cq_size = PAGE_ALIGN(cq_size);
1411 memset(&spec, 0, sizeof(spec));
1412 spec.type = GDMA_RQ;
1413 spec.monitor_avl_buf = true;
1414 spec.queue_size = rq_size;
1415 err = mana_gd_create_mana_wq_cq(gd, &spec, &rxq->gdma_rq);
1419 /* Create RQ's CQ */
1421 cq->gdma_comp_buf = eq->cqe_poll;
1422 cq->type = MANA_CQ_TYPE_RX;
1425 memset(&spec, 0, sizeof(spec));
1426 spec.type = GDMA_CQ;
1427 spec.monitor_avl_buf = false;
1428 spec.queue_size = cq_size;
1429 spec.cq.callback = mana_cq_handler;
1430 spec.cq.parent_eq = eq->eq;
1431 spec.cq.context = cq;
1432 err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq);
1436 memset(&wq_spec, 0, sizeof(wq_spec));
1437 memset(&cq_spec, 0, sizeof(cq_spec));
1438 wq_spec.gdma_region = rxq->gdma_rq->mem_info.gdma_region;
1439 wq_spec.queue_size = rxq->gdma_rq->queue_size;
1441 cq_spec.gdma_region = cq->gdma_cq->mem_info.gdma_region;
1442 cq_spec.queue_size = cq->gdma_cq->queue_size;
1443 cq_spec.modr_ctx_id = 0;
1444 cq_spec.attached_eq = cq->gdma_cq->cq.parent->id;
1446 err = mana_create_wq_obj(apc, apc->port_handle, GDMA_RQ,
1447 &wq_spec, &cq_spec, &rxq->rxobj);
1451 rxq->gdma_rq->id = wq_spec.queue_index;
1452 cq->gdma_cq->id = cq_spec.queue_index;
1454 rxq->gdma_rq->mem_info.gdma_region = GDMA_INVALID_DMA_REGION;
1455 cq->gdma_cq->mem_info.gdma_region = GDMA_INVALID_DMA_REGION;
1457 rxq->gdma_id = rxq->gdma_rq->id;
1458 cq->gdma_id = cq->gdma_cq->id;
1460 err = mana_push_wqe(rxq);
1464 if (cq->gdma_id >= gc->max_num_cqs)
1467 gc->cq_table[cq->gdma_id] = cq->gdma_cq;
1469 mana_gd_arm_cq(cq->gdma_cq);
1474 netdev_err(ndev, "Failed to create RXQ: err = %d\n", err);
1476 mana_destroy_rxq(apc, rxq, false);
1479 mana_deinit_cq(apc, cq);
1484 static int mana_add_rx_queues(struct mana_port_context *apc,
1485 struct net_device *ndev)
1487 struct mana_rxq *rxq;
1491 for (i = 0; i < apc->num_queues; i++) {
1492 rxq = mana_create_rxq(apc, i, &apc->eqs[i], ndev);
1498 u64_stats_init(&rxq->stats.syncp);
1503 apc->default_rxobj = apc->rxqs[0]->rxobj;
1508 static void mana_destroy_vport(struct mana_port_context *apc)
1510 struct mana_rxq *rxq;
1513 for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) {
1514 rxq = apc->rxqs[rxq_idx];
1518 mana_destroy_rxq(apc, rxq, true);
1519 apc->rxqs[rxq_idx] = NULL;
1522 mana_destroy_txq(apc);
1525 static int mana_create_vport(struct mana_port_context *apc,
1526 struct net_device *net)
1528 struct gdma_dev *gd = apc->ac->gdma_dev;
1531 apc->default_rxobj = INVALID_MANA_HANDLE;
1533 err = mana_cfg_vport(apc, gd->pdid, gd->doorbell);
1537 return mana_create_txq(apc, net);
1540 static void mana_rss_table_init(struct mana_port_context *apc)
1544 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
1545 apc->indir_table[i] =
1546 ethtool_rxfh_indir_default(i, apc->num_queues);
1549 int mana_config_rss(struct mana_port_context *apc, enum TRI_STATE rx,
1550 bool update_hash, bool update_tab)
1556 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) {
1557 queue_idx = apc->indir_table[i];
1558 apc->rxobj_table[i] = apc->rxqs[queue_idx]->rxobj;
1562 return mana_cfg_vport_steering(apc, rx, true, update_hash, update_tab);
1565 static int mana_init_port(struct net_device *ndev)
1567 struct mana_port_context *apc = netdev_priv(ndev);
1568 u32 max_txq, max_rxq, max_queues;
1569 int port_idx = apc->port_idx;
1570 u32 num_indirect_entries;
1573 err = mana_init_port_context(apc);
1577 err = mana_query_vport_cfg(apc, port_idx, &max_txq, &max_rxq,
1578 &num_indirect_entries);
1580 netdev_err(ndev, "Failed to query info for vPort 0\n");
1584 max_queues = min_t(u32, max_txq, max_rxq);
1585 if (apc->max_queues > max_queues)
1586 apc->max_queues = max_queues;
1588 if (apc->num_queues > apc->max_queues)
1589 apc->num_queues = apc->max_queues;
1591 ether_addr_copy(ndev->dev_addr, apc->mac_addr);
1601 int mana_alloc_queues(struct net_device *ndev)
1603 struct mana_port_context *apc = netdev_priv(ndev);
1604 struct gdma_dev *gd = apc->ac->gdma_dev;
1607 err = mana_create_eq(apc);
1611 err = mana_create_vport(apc, ndev);
1615 err = netif_set_real_num_tx_queues(ndev, apc->num_queues);
1619 err = mana_add_rx_queues(apc, ndev);
1623 apc->rss_state = apc->num_queues > 1 ? TRI_STATE_TRUE : TRI_STATE_FALSE;
1625 err = netif_set_real_num_rx_queues(ndev, apc->num_queues);
1629 mana_rss_table_init(apc);
1631 err = mana_config_rss(apc, TRI_STATE_TRUE, true, true);
1638 mana_destroy_vport(apc);
1640 mana_destroy_eq(gd->gdma_context, apc);
1644 int mana_attach(struct net_device *ndev)
1646 struct mana_port_context *apc = netdev_priv(ndev);
1651 err = mana_init_port(ndev);
1655 err = mana_alloc_queues(ndev);
1662 netif_device_attach(ndev);
1664 apc->port_is_up = apc->port_st_save;
1666 /* Ensure port state updated before txq state */
1669 if (apc->port_is_up) {
1670 netif_carrier_on(ndev);
1671 netif_tx_wake_all_queues(ndev);
1677 static int mana_dealloc_queues(struct net_device *ndev)
1679 struct mana_port_context *apc = netdev_priv(ndev);
1680 struct mana_txq *txq;
1683 if (apc->port_is_up)
1686 /* No packet can be transmitted now since apc->port_is_up is false.
1687 * There is still a tiny chance that mana_poll_tx_cq() can re-enable
1688 * a txq because it may not timely see apc->port_is_up being cleared
1689 * to false, but it doesn't matter since mana_start_xmit() drops any
1690 * new packets due to apc->port_is_up being false.
1692 * Drain all the in-flight TX packets
1694 for (i = 0; i < apc->num_queues; i++) {
1695 txq = &apc->tx_qp[i].txq;
1697 while (atomic_read(&txq->pending_sends) > 0)
1698 usleep_range(1000, 2000);
1701 /* We're 100% sure the queues can no longer be woken up, because
1702 * we're sure now mana_poll_tx_cq() can't be running.
1705 apc->rss_state = TRI_STATE_FALSE;
1706 err = mana_config_rss(apc, TRI_STATE_FALSE, false, false);
1708 netdev_err(ndev, "Failed to disable vPort: %d\n", err);
1712 /* TODO: Implement RX fencing */
1715 mana_destroy_vport(apc);
1717 mana_destroy_eq(apc->ac->gdma_dev->gdma_context, apc);
1722 int mana_detach(struct net_device *ndev, bool from_close)
1724 struct mana_port_context *apc = netdev_priv(ndev);
1729 apc->port_st_save = apc->port_is_up;
1730 apc->port_is_up = false;
1732 /* Ensure port state updated before txq state */
1735 netif_tx_disable(ndev);
1736 netif_carrier_off(ndev);
1738 if (apc->port_st_save) {
1739 err = mana_dealloc_queues(ndev);
1745 netif_device_detach(ndev);
1746 mana_cleanup_port_context(apc);
1752 static int mana_probe_port(struct mana_context *ac, int port_idx,
1753 struct net_device **ndev_storage)
1755 struct gdma_context *gc = ac->gdma_dev->gdma_context;
1756 struct mana_port_context *apc;
1757 struct net_device *ndev;
1760 ndev = alloc_etherdev_mq(sizeof(struct mana_port_context),
1761 gc->max_num_queues);
1765 *ndev_storage = ndev;
1767 apc = netdev_priv(ndev);
1770 apc->max_queues = gc->max_num_queues;
1771 apc->num_queues = min_t(uint, gc->max_num_queues, MANA_MAX_NUM_QUEUES);
1772 apc->port_handle = INVALID_MANA_HANDLE;
1773 apc->port_idx = port_idx;
1775 ndev->netdev_ops = &mana_devops;
1776 ndev->ethtool_ops = &mana_ethtool_ops;
1777 ndev->mtu = ETH_DATA_LEN;
1778 ndev->max_mtu = ndev->mtu;
1779 ndev->min_mtu = ndev->mtu;
1780 ndev->needed_headroom = MANA_HEADROOM;
1781 SET_NETDEV_DEV(ndev, gc->dev);
1783 netif_carrier_off(ndev);
1785 netdev_rss_key_fill(apc->hashkey, MANA_HASH_KEY_SIZE);
1787 err = mana_init_port(ndev);
1791 netdev_lockdep_set_classes(ndev);
1793 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1794 ndev->hw_features |= NETIF_F_RXCSUM;
1795 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1796 ndev->hw_features |= NETIF_F_RXHASH;
1797 ndev->features = ndev->hw_features;
1798 ndev->vlan_features = 0;
1800 err = register_netdev(ndev);
1802 netdev_err(ndev, "Unable to register netdev.\n");
1812 *ndev_storage = NULL;
1813 netdev_err(ndev, "Failed to probe vPort %d: %d\n", port_idx, err);
1818 int mana_probe(struct gdma_dev *gd)
1820 struct gdma_context *gc = gd->gdma_context;
1821 struct device *dev = gc->dev;
1822 struct mana_context *ac;
1827 "Microsoft Azure Network Adapter protocol version: %d.%d.%d\n",
1828 MANA_MAJOR_VERSION, MANA_MINOR_VERSION, MANA_MICRO_VERSION);
1830 err = mana_gd_register_device(gd);
1834 ac = kzalloc(sizeof(*ac), GFP_KERNEL);
1840 gd->driver_data = ac;
1842 err = mana_query_device_cfg(ac, MANA_MAJOR_VERSION, MANA_MINOR_VERSION,
1843 MANA_MICRO_VERSION, &ac->num_ports);
1847 if (ac->num_ports > MAX_PORTS_IN_MANA_DEV)
1848 ac->num_ports = MAX_PORTS_IN_MANA_DEV;
1850 for (i = 0; i < ac->num_ports; i++) {
1851 err = mana_probe_port(ac, i, &ac->ports[i]);
1862 void mana_remove(struct gdma_dev *gd)
1864 struct gdma_context *gc = gd->gdma_context;
1865 struct mana_context *ac = gd->driver_data;
1866 struct device *dev = gc->dev;
1867 struct net_device *ndev;
1870 for (i = 0; i < ac->num_ports; i++) {
1871 ndev = ac->ports[i];
1874 dev_err(dev, "No net device to remove\n");
1878 /* All cleanup actions should stay after rtnl_lock(), otherwise
1879 * other functions may access partially cleaned up data.
1883 mana_detach(ndev, false);
1885 unregister_netdevice(ndev);
1892 mana_gd_deregister_device(gd);
1893 gd->driver_data = NULL;
1894 gd->gdma_context = NULL;