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