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/inetdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/ethtool.h>
9 #include <linux/filter.h>
10 #include <linux/mm.h>
11 
12 #include <net/checksum.h>
13 #include <net/ip6_checksum.h>
14 
15 #include <net/mana/mana.h>
16 #include <net/mana/mana_auxiliary.h>
17 
18 static DEFINE_IDA(mana_adev_ida);
19 
20 static int mana_adev_idx_alloc(void)
21 {
22 	return ida_alloc(&mana_adev_ida, GFP_KERNEL);
23 }
24 
25 static void mana_adev_idx_free(int idx)
26 {
27 	ida_free(&mana_adev_ida, idx);
28 }
29 
30 /* Microsoft Azure Network Adapter (MANA) functions */
31 
32 static int mana_open(struct net_device *ndev)
33 {
34 	struct mana_port_context *apc = netdev_priv(ndev);
35 	int err;
36 
37 	err = mana_alloc_queues(ndev);
38 	if (err)
39 		return err;
40 
41 	apc->port_is_up = true;
42 
43 	/* Ensure port state updated before txq state */
44 	smp_wmb();
45 
46 	netif_carrier_on(ndev);
47 	netif_tx_wake_all_queues(ndev);
48 
49 	return 0;
50 }
51 
52 static int mana_close(struct net_device *ndev)
53 {
54 	struct mana_port_context *apc = netdev_priv(ndev);
55 
56 	if (!apc->port_is_up)
57 		return 0;
58 
59 	return mana_detach(ndev, true);
60 }
61 
62 static bool mana_can_tx(struct gdma_queue *wq)
63 {
64 	return mana_gd_wq_avail_space(wq) >= MAX_TX_WQE_SIZE;
65 }
66 
67 static unsigned int mana_checksum_info(struct sk_buff *skb)
68 {
69 	if (skb->protocol == htons(ETH_P_IP)) {
70 		struct iphdr *ip = ip_hdr(skb);
71 
72 		if (ip->protocol == IPPROTO_TCP)
73 			return IPPROTO_TCP;
74 
75 		if (ip->protocol == IPPROTO_UDP)
76 			return IPPROTO_UDP;
77 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
78 		struct ipv6hdr *ip6 = ipv6_hdr(skb);
79 
80 		if (ip6->nexthdr == IPPROTO_TCP)
81 			return IPPROTO_TCP;
82 
83 		if (ip6->nexthdr == IPPROTO_UDP)
84 			return IPPROTO_UDP;
85 	}
86 
87 	/* No csum offloading */
88 	return 0;
89 }
90 
91 static int mana_map_skb(struct sk_buff *skb, struct mana_port_context *apc,
92 			struct mana_tx_package *tp)
93 {
94 	struct mana_skb_head *ash = (struct mana_skb_head *)skb->head;
95 	struct gdma_dev *gd = apc->ac->gdma_dev;
96 	struct gdma_context *gc;
97 	struct device *dev;
98 	skb_frag_t *frag;
99 	dma_addr_t da;
100 	int i;
101 
102 	gc = gd->gdma_context;
103 	dev = gc->dev;
104 	da = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
105 
106 	if (dma_mapping_error(dev, da))
107 		return -ENOMEM;
108 
109 	ash->dma_handle[0] = da;
110 	ash->size[0] = skb_headlen(skb);
111 
112 	tp->wqe_req.sgl[0].address = ash->dma_handle[0];
113 	tp->wqe_req.sgl[0].mem_key = gd->gpa_mkey;
114 	tp->wqe_req.sgl[0].size = ash->size[0];
115 
116 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
117 		frag = &skb_shinfo(skb)->frags[i];
118 		da = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
119 				      DMA_TO_DEVICE);
120 
121 		if (dma_mapping_error(dev, da))
122 			goto frag_err;
123 
124 		ash->dma_handle[i + 1] = da;
125 		ash->size[i + 1] = skb_frag_size(frag);
126 
127 		tp->wqe_req.sgl[i + 1].address = ash->dma_handle[i + 1];
128 		tp->wqe_req.sgl[i + 1].mem_key = gd->gpa_mkey;
129 		tp->wqe_req.sgl[i + 1].size = ash->size[i + 1];
130 	}
131 
132 	return 0;
133 
134 frag_err:
135 	for (i = i - 1; i >= 0; i--)
136 		dma_unmap_page(dev, ash->dma_handle[i + 1], ash->size[i + 1],
137 			       DMA_TO_DEVICE);
138 
139 	dma_unmap_single(dev, ash->dma_handle[0], ash->size[0], DMA_TO_DEVICE);
140 
141 	return -ENOMEM;
142 }
143 
144 netdev_tx_t mana_start_xmit(struct sk_buff *skb, struct net_device *ndev)
145 {
146 	enum mana_tx_pkt_format pkt_fmt = MANA_SHORT_PKT_FMT;
147 	struct mana_port_context *apc = netdev_priv(ndev);
148 	u16 txq_idx = skb_get_queue_mapping(skb);
149 	struct gdma_dev *gd = apc->ac->gdma_dev;
150 	bool ipv4 = false, ipv6 = false;
151 	struct mana_tx_package pkg = {};
152 	struct netdev_queue *net_txq;
153 	struct mana_stats_tx *tx_stats;
154 	struct gdma_queue *gdma_sq;
155 	unsigned int csum_type;
156 	struct mana_txq *txq;
157 	struct mana_cq *cq;
158 	int err, len;
159 	u16 ihs;
160 
161 	if (unlikely(!apc->port_is_up))
162 		goto tx_drop;
163 
164 	if (skb_cow_head(skb, MANA_HEADROOM))
165 		goto tx_drop_count;
166 
167 	txq = &apc->tx_qp[txq_idx].txq;
168 	gdma_sq = txq->gdma_sq;
169 	cq = &apc->tx_qp[txq_idx].tx_cq;
170 	tx_stats = &txq->stats;
171 
172 	pkg.tx_oob.s_oob.vcq_num = cq->gdma_id;
173 	pkg.tx_oob.s_oob.vsq_frame = txq->vsq_frame;
174 
175 	if (txq->vp_offset > MANA_SHORT_VPORT_OFFSET_MAX) {
176 		pkg.tx_oob.l_oob.long_vp_offset = txq->vp_offset;
177 		pkt_fmt = MANA_LONG_PKT_FMT;
178 	} else {
179 		pkg.tx_oob.s_oob.short_vp_offset = txq->vp_offset;
180 	}
181 
182 	if (skb_vlan_tag_present(skb)) {
183 		pkt_fmt = MANA_LONG_PKT_FMT;
184 		pkg.tx_oob.l_oob.inject_vlan_pri_tag = 1;
185 		pkg.tx_oob.l_oob.pcp = skb_vlan_tag_get_prio(skb);
186 		pkg.tx_oob.l_oob.dei = skb_vlan_tag_get_cfi(skb);
187 		pkg.tx_oob.l_oob.vlan_id = skb_vlan_tag_get_id(skb);
188 	}
189 
190 	pkg.tx_oob.s_oob.pkt_fmt = pkt_fmt;
191 
192 	if (pkt_fmt == MANA_SHORT_PKT_FMT) {
193 		pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_short_oob);
194 		u64_stats_update_begin(&tx_stats->syncp);
195 		tx_stats->short_pkt_fmt++;
196 		u64_stats_update_end(&tx_stats->syncp);
197 	} else {
198 		pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_oob);
199 		u64_stats_update_begin(&tx_stats->syncp);
200 		tx_stats->long_pkt_fmt++;
201 		u64_stats_update_end(&tx_stats->syncp);
202 	}
203 
204 	pkg.wqe_req.inline_oob_data = &pkg.tx_oob;
205 	pkg.wqe_req.flags = 0;
206 	pkg.wqe_req.client_data_unit = 0;
207 
208 	pkg.wqe_req.num_sge = 1 + skb_shinfo(skb)->nr_frags;
209 	WARN_ON_ONCE(pkg.wqe_req.num_sge > MAX_TX_WQE_SGL_ENTRIES);
210 
211 	if (pkg.wqe_req.num_sge <= ARRAY_SIZE(pkg.sgl_array)) {
212 		pkg.wqe_req.sgl = pkg.sgl_array;
213 	} else {
214 		pkg.sgl_ptr = kmalloc_array(pkg.wqe_req.num_sge,
215 					    sizeof(struct gdma_sge),
216 					    GFP_ATOMIC);
217 		if (!pkg.sgl_ptr)
218 			goto tx_drop_count;
219 
220 		pkg.wqe_req.sgl = pkg.sgl_ptr;
221 	}
222 
223 	if (skb->protocol == htons(ETH_P_IP))
224 		ipv4 = true;
225 	else if (skb->protocol == htons(ETH_P_IPV6))
226 		ipv6 = true;
227 
228 	if (skb_is_gso(skb)) {
229 		pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
230 		pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
231 
232 		pkg.tx_oob.s_oob.comp_iphdr_csum = 1;
233 		pkg.tx_oob.s_oob.comp_tcp_csum = 1;
234 		pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb);
235 
236 		pkg.wqe_req.client_data_unit = skb_shinfo(skb)->gso_size;
237 		pkg.wqe_req.flags = GDMA_WR_OOB_IN_SGL | GDMA_WR_PAD_BY_SGE0;
238 		if (ipv4) {
239 			ip_hdr(skb)->tot_len = 0;
240 			ip_hdr(skb)->check = 0;
241 			tcp_hdr(skb)->check =
242 				~csum_tcpudp_magic(ip_hdr(skb)->saddr,
243 						   ip_hdr(skb)->daddr, 0,
244 						   IPPROTO_TCP, 0);
245 		} else {
246 			ipv6_hdr(skb)->payload_len = 0;
247 			tcp_hdr(skb)->check =
248 				~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
249 						 &ipv6_hdr(skb)->daddr, 0,
250 						 IPPROTO_TCP, 0);
251 		}
252 
253 		if (skb->encapsulation) {
254 			ihs = skb_inner_tcp_all_headers(skb);
255 			u64_stats_update_begin(&tx_stats->syncp);
256 			tx_stats->tso_inner_packets++;
257 			tx_stats->tso_inner_bytes += skb->len - ihs;
258 			u64_stats_update_end(&tx_stats->syncp);
259 		} else {
260 			if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
261 				ihs = skb_transport_offset(skb) + sizeof(struct udphdr);
262 			} else {
263 				ihs = skb_tcp_all_headers(skb);
264 				if (ipv6_has_hopopt_jumbo(skb))
265 					ihs -= sizeof(struct hop_jumbo_hdr);
266 			}
267 
268 			u64_stats_update_begin(&tx_stats->syncp);
269 			tx_stats->tso_packets++;
270 			tx_stats->tso_bytes += skb->len - ihs;
271 			u64_stats_update_end(&tx_stats->syncp);
272 		}
273 
274 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
275 		csum_type = mana_checksum_info(skb);
276 
277 		u64_stats_update_begin(&tx_stats->syncp);
278 		tx_stats->csum_partial++;
279 		u64_stats_update_end(&tx_stats->syncp);
280 
281 		if (csum_type == IPPROTO_TCP) {
282 			pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
283 			pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
284 
285 			pkg.tx_oob.s_oob.comp_tcp_csum = 1;
286 			pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb);
287 
288 		} else if (csum_type == IPPROTO_UDP) {
289 			pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
290 			pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
291 
292 			pkg.tx_oob.s_oob.comp_udp_csum = 1;
293 		} else {
294 			/* Can't do offload of this type of checksum */
295 			if (skb_checksum_help(skb))
296 				goto free_sgl_ptr;
297 		}
298 	}
299 
300 	if (mana_map_skb(skb, apc, &pkg)) {
301 		u64_stats_update_begin(&tx_stats->syncp);
302 		tx_stats->mana_map_err++;
303 		u64_stats_update_end(&tx_stats->syncp);
304 		goto free_sgl_ptr;
305 	}
306 
307 	skb_queue_tail(&txq->pending_skbs, skb);
308 
309 	len = skb->len;
310 	net_txq = netdev_get_tx_queue(ndev, txq_idx);
311 
312 	err = mana_gd_post_work_request(gdma_sq, &pkg.wqe_req,
313 					(struct gdma_posted_wqe_info *)skb->cb);
314 	if (!mana_can_tx(gdma_sq)) {
315 		netif_tx_stop_queue(net_txq);
316 		apc->eth_stats.stop_queue++;
317 	}
318 
319 	if (err) {
320 		(void)skb_dequeue_tail(&txq->pending_skbs);
321 		netdev_warn(ndev, "Failed to post TX OOB: %d\n", err);
322 		err = NETDEV_TX_BUSY;
323 		goto tx_busy;
324 	}
325 
326 	err = NETDEV_TX_OK;
327 	atomic_inc(&txq->pending_sends);
328 
329 	mana_gd_wq_ring_doorbell(gd->gdma_context, gdma_sq);
330 
331 	/* skb may be freed after mana_gd_post_work_request. Do not use it. */
332 	skb = NULL;
333 
334 	tx_stats = &txq->stats;
335 	u64_stats_update_begin(&tx_stats->syncp);
336 	tx_stats->packets++;
337 	tx_stats->bytes += len;
338 	u64_stats_update_end(&tx_stats->syncp);
339 
340 tx_busy:
341 	if (netif_tx_queue_stopped(net_txq) && mana_can_tx(gdma_sq)) {
342 		netif_tx_wake_queue(net_txq);
343 		apc->eth_stats.wake_queue++;
344 	}
345 
346 	kfree(pkg.sgl_ptr);
347 	return err;
348 
349 free_sgl_ptr:
350 	kfree(pkg.sgl_ptr);
351 tx_drop_count:
352 	ndev->stats.tx_dropped++;
353 tx_drop:
354 	dev_kfree_skb_any(skb);
355 	return NETDEV_TX_OK;
356 }
357 
358 static void mana_get_stats64(struct net_device *ndev,
359 			     struct rtnl_link_stats64 *st)
360 {
361 	struct mana_port_context *apc = netdev_priv(ndev);
362 	unsigned int num_queues = apc->num_queues;
363 	struct mana_stats_rx *rx_stats;
364 	struct mana_stats_tx *tx_stats;
365 	unsigned int start;
366 	u64 packets, bytes;
367 	int q;
368 
369 	if (!apc->port_is_up)
370 		return;
371 
372 	netdev_stats_to_stats64(st, &ndev->stats);
373 
374 	for (q = 0; q < num_queues; q++) {
375 		rx_stats = &apc->rxqs[q]->stats;
376 
377 		do {
378 			start = u64_stats_fetch_begin(&rx_stats->syncp);
379 			packets = rx_stats->packets;
380 			bytes = rx_stats->bytes;
381 		} while (u64_stats_fetch_retry(&rx_stats->syncp, start));
382 
383 		st->rx_packets += packets;
384 		st->rx_bytes += bytes;
385 	}
386 
387 	for (q = 0; q < num_queues; q++) {
388 		tx_stats = &apc->tx_qp[q].txq.stats;
389 
390 		do {
391 			start = u64_stats_fetch_begin(&tx_stats->syncp);
392 			packets = tx_stats->packets;
393 			bytes = tx_stats->bytes;
394 		} while (u64_stats_fetch_retry(&tx_stats->syncp, start));
395 
396 		st->tx_packets += packets;
397 		st->tx_bytes += bytes;
398 	}
399 }
400 
401 static int mana_get_tx_queue(struct net_device *ndev, struct sk_buff *skb,
402 			     int old_q)
403 {
404 	struct mana_port_context *apc = netdev_priv(ndev);
405 	u32 hash = skb_get_hash(skb);
406 	struct sock *sk = skb->sk;
407 	int txq;
408 
409 	txq = apc->indir_table[hash & MANA_INDIRECT_TABLE_MASK];
410 
411 	if (txq != old_q && sk && sk_fullsock(sk) &&
412 	    rcu_access_pointer(sk->sk_dst_cache))
413 		sk_tx_queue_set(sk, txq);
414 
415 	return txq;
416 }
417 
418 static u16 mana_select_queue(struct net_device *ndev, struct sk_buff *skb,
419 			     struct net_device *sb_dev)
420 {
421 	int txq;
422 
423 	if (ndev->real_num_tx_queues == 1)
424 		return 0;
425 
426 	txq = sk_tx_queue_get(skb->sk);
427 
428 	if (txq < 0 || skb->ooo_okay || txq >= ndev->real_num_tx_queues) {
429 		if (skb_rx_queue_recorded(skb))
430 			txq = skb_get_rx_queue(skb);
431 		else
432 			txq = mana_get_tx_queue(ndev, skb, txq);
433 	}
434 
435 	return txq;
436 }
437 
438 /* Release pre-allocated RX buffers */
439 static void mana_pre_dealloc_rxbufs(struct mana_port_context *mpc)
440 {
441 	struct device *dev;
442 	int i;
443 
444 	dev = mpc->ac->gdma_dev->gdma_context->dev;
445 
446 	if (!mpc->rxbufs_pre)
447 		goto out1;
448 
449 	if (!mpc->das_pre)
450 		goto out2;
451 
452 	while (mpc->rxbpre_total) {
453 		i = --mpc->rxbpre_total;
454 		dma_unmap_single(dev, mpc->das_pre[i], mpc->rxbpre_datasize,
455 				 DMA_FROM_DEVICE);
456 		put_page(virt_to_head_page(mpc->rxbufs_pre[i]));
457 	}
458 
459 	kfree(mpc->das_pre);
460 	mpc->das_pre = NULL;
461 
462 out2:
463 	kfree(mpc->rxbufs_pre);
464 	mpc->rxbufs_pre = NULL;
465 
466 out1:
467 	mpc->rxbpre_datasize = 0;
468 	mpc->rxbpre_alloc_size = 0;
469 	mpc->rxbpre_headroom = 0;
470 }
471 
472 /* Get a buffer from the pre-allocated RX buffers */
473 static void *mana_get_rxbuf_pre(struct mana_rxq *rxq, dma_addr_t *da)
474 {
475 	struct net_device *ndev = rxq->ndev;
476 	struct mana_port_context *mpc;
477 	void *va;
478 
479 	mpc = netdev_priv(ndev);
480 
481 	if (!mpc->rxbufs_pre || !mpc->das_pre || !mpc->rxbpre_total) {
482 		netdev_err(ndev, "No RX pre-allocated bufs\n");
483 		return NULL;
484 	}
485 
486 	/* Check sizes to catch unexpected coding error */
487 	if (mpc->rxbpre_datasize != rxq->datasize) {
488 		netdev_err(ndev, "rxbpre_datasize mismatch: %u: %u\n",
489 			   mpc->rxbpre_datasize, rxq->datasize);
490 		return NULL;
491 	}
492 
493 	if (mpc->rxbpre_alloc_size != rxq->alloc_size) {
494 		netdev_err(ndev, "rxbpre_alloc_size mismatch: %u: %u\n",
495 			   mpc->rxbpre_alloc_size, rxq->alloc_size);
496 		return NULL;
497 	}
498 
499 	if (mpc->rxbpre_headroom != rxq->headroom) {
500 		netdev_err(ndev, "rxbpre_headroom mismatch: %u: %u\n",
501 			   mpc->rxbpre_headroom, rxq->headroom);
502 		return NULL;
503 	}
504 
505 	mpc->rxbpre_total--;
506 
507 	*da = mpc->das_pre[mpc->rxbpre_total];
508 	va = mpc->rxbufs_pre[mpc->rxbpre_total];
509 	mpc->rxbufs_pre[mpc->rxbpre_total] = NULL;
510 
511 	/* Deallocate the array after all buffers are gone */
512 	if (!mpc->rxbpre_total)
513 		mana_pre_dealloc_rxbufs(mpc);
514 
515 	return va;
516 }
517 
518 /* Get RX buffer's data size, alloc size, XDP headroom based on MTU */
519 static void mana_get_rxbuf_cfg(int mtu, u32 *datasize, u32 *alloc_size,
520 			       u32 *headroom)
521 {
522 	if (mtu > MANA_XDP_MTU_MAX)
523 		*headroom = 0; /* no support for XDP */
524 	else
525 		*headroom = XDP_PACKET_HEADROOM;
526 
527 	*alloc_size = mtu + MANA_RXBUF_PAD + *headroom;
528 
529 	*datasize = ALIGN(mtu + ETH_HLEN, MANA_RX_DATA_ALIGN);
530 }
531 
532 static int mana_pre_alloc_rxbufs(struct mana_port_context *mpc, int new_mtu)
533 {
534 	struct device *dev;
535 	struct page *page;
536 	dma_addr_t da;
537 	int num_rxb;
538 	void *va;
539 	int i;
540 
541 	mana_get_rxbuf_cfg(new_mtu, &mpc->rxbpre_datasize,
542 			   &mpc->rxbpre_alloc_size, &mpc->rxbpre_headroom);
543 
544 	dev = mpc->ac->gdma_dev->gdma_context->dev;
545 
546 	num_rxb = mpc->num_queues * RX_BUFFERS_PER_QUEUE;
547 
548 	WARN(mpc->rxbufs_pre, "mana rxbufs_pre exists\n");
549 	mpc->rxbufs_pre = kmalloc_array(num_rxb, sizeof(void *), GFP_KERNEL);
550 	if (!mpc->rxbufs_pre)
551 		goto error;
552 
553 	mpc->das_pre = kmalloc_array(num_rxb, sizeof(dma_addr_t), GFP_KERNEL);
554 	if (!mpc->das_pre)
555 		goto error;
556 
557 	mpc->rxbpre_total = 0;
558 
559 	for (i = 0; i < num_rxb; i++) {
560 		if (mpc->rxbpre_alloc_size > PAGE_SIZE) {
561 			va = netdev_alloc_frag(mpc->rxbpre_alloc_size);
562 			if (!va)
563 				goto error;
564 
565 			page = virt_to_head_page(va);
566 			/* Check if the frag falls back to single page */
567 			if (compound_order(page) <
568 			    get_order(mpc->rxbpre_alloc_size)) {
569 				put_page(page);
570 				goto error;
571 			}
572 		} else {
573 			page = dev_alloc_page();
574 			if (!page)
575 				goto error;
576 
577 			va = page_to_virt(page);
578 		}
579 
580 		da = dma_map_single(dev, va + mpc->rxbpre_headroom,
581 				    mpc->rxbpre_datasize, DMA_FROM_DEVICE);
582 		if (dma_mapping_error(dev, da)) {
583 			put_page(virt_to_head_page(va));
584 			goto error;
585 		}
586 
587 		mpc->rxbufs_pre[i] = va;
588 		mpc->das_pre[i] = da;
589 		mpc->rxbpre_total = i + 1;
590 	}
591 
592 	return 0;
593 
594 error:
595 	mana_pre_dealloc_rxbufs(mpc);
596 	return -ENOMEM;
597 }
598 
599 static int mana_change_mtu(struct net_device *ndev, int new_mtu)
600 {
601 	struct mana_port_context *mpc = netdev_priv(ndev);
602 	unsigned int old_mtu = ndev->mtu;
603 	int err;
604 
605 	/* Pre-allocate buffers to prevent failure in mana_attach later */
606 	err = mana_pre_alloc_rxbufs(mpc, new_mtu);
607 	if (err) {
608 		netdev_err(ndev, "Insufficient memory for new MTU\n");
609 		return err;
610 	}
611 
612 	err = mana_detach(ndev, false);
613 	if (err) {
614 		netdev_err(ndev, "mana_detach failed: %d\n", err);
615 		goto out;
616 	}
617 
618 	ndev->mtu = new_mtu;
619 
620 	err = mana_attach(ndev);
621 	if (err) {
622 		netdev_err(ndev, "mana_attach failed: %d\n", err);
623 		ndev->mtu = old_mtu;
624 	}
625 
626 out:
627 	mana_pre_dealloc_rxbufs(mpc);
628 	return err;
629 }
630 
631 static const struct net_device_ops mana_devops = {
632 	.ndo_open		= mana_open,
633 	.ndo_stop		= mana_close,
634 	.ndo_select_queue	= mana_select_queue,
635 	.ndo_start_xmit		= mana_start_xmit,
636 	.ndo_validate_addr	= eth_validate_addr,
637 	.ndo_get_stats64	= mana_get_stats64,
638 	.ndo_bpf		= mana_bpf,
639 	.ndo_xdp_xmit		= mana_xdp_xmit,
640 	.ndo_change_mtu		= mana_change_mtu,
641 };
642 
643 static void mana_cleanup_port_context(struct mana_port_context *apc)
644 {
645 	kfree(apc->rxqs);
646 	apc->rxqs = NULL;
647 }
648 
649 static int mana_init_port_context(struct mana_port_context *apc)
650 {
651 	apc->rxqs = kcalloc(apc->num_queues, sizeof(struct mana_rxq *),
652 			    GFP_KERNEL);
653 
654 	return !apc->rxqs ? -ENOMEM : 0;
655 }
656 
657 static int mana_send_request(struct mana_context *ac, void *in_buf,
658 			     u32 in_len, void *out_buf, u32 out_len)
659 {
660 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
661 	struct gdma_resp_hdr *resp = out_buf;
662 	struct gdma_req_hdr *req = in_buf;
663 	struct device *dev = gc->dev;
664 	static atomic_t activity_id;
665 	int err;
666 
667 	req->dev_id = gc->mana.dev_id;
668 	req->activity_id = atomic_inc_return(&activity_id);
669 
670 	err = mana_gd_send_request(gc, in_len, in_buf, out_len,
671 				   out_buf);
672 	if (err || resp->status) {
673 		dev_err(dev, "Failed to send mana message: %d, 0x%x\n",
674 			err, resp->status);
675 		return err ? err : -EPROTO;
676 	}
677 
678 	if (req->dev_id.as_uint32 != resp->dev_id.as_uint32 ||
679 	    req->activity_id != resp->activity_id) {
680 		dev_err(dev, "Unexpected mana message response: %x,%x,%x,%x\n",
681 			req->dev_id.as_uint32, resp->dev_id.as_uint32,
682 			req->activity_id, resp->activity_id);
683 		return -EPROTO;
684 	}
685 
686 	return 0;
687 }
688 
689 static int mana_verify_resp_hdr(const struct gdma_resp_hdr *resp_hdr,
690 				const enum mana_command_code expected_code,
691 				const u32 min_size)
692 {
693 	if (resp_hdr->response.msg_type != expected_code)
694 		return -EPROTO;
695 
696 	if (resp_hdr->response.msg_version < GDMA_MESSAGE_V1)
697 		return -EPROTO;
698 
699 	if (resp_hdr->response.msg_size < min_size)
700 		return -EPROTO;
701 
702 	return 0;
703 }
704 
705 static int mana_pf_register_hw_vport(struct mana_port_context *apc)
706 {
707 	struct mana_register_hw_vport_resp resp = {};
708 	struct mana_register_hw_vport_req req = {};
709 	int err;
710 
711 	mana_gd_init_req_hdr(&req.hdr, MANA_REGISTER_HW_PORT,
712 			     sizeof(req), sizeof(resp));
713 	req.attached_gfid = 1;
714 	req.is_pf_default_vport = 1;
715 	req.allow_all_ether_types = 1;
716 
717 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
718 				sizeof(resp));
719 	if (err) {
720 		netdev_err(apc->ndev, "Failed to register hw vPort: %d\n", err);
721 		return err;
722 	}
723 
724 	err = mana_verify_resp_hdr(&resp.hdr, MANA_REGISTER_HW_PORT,
725 				   sizeof(resp));
726 	if (err || resp.hdr.status) {
727 		netdev_err(apc->ndev, "Failed to register hw vPort: %d, 0x%x\n",
728 			   err, resp.hdr.status);
729 		return err ? err : -EPROTO;
730 	}
731 
732 	apc->port_handle = resp.hw_vport_handle;
733 	return 0;
734 }
735 
736 static void mana_pf_deregister_hw_vport(struct mana_port_context *apc)
737 {
738 	struct mana_deregister_hw_vport_resp resp = {};
739 	struct mana_deregister_hw_vport_req req = {};
740 	int err;
741 
742 	mana_gd_init_req_hdr(&req.hdr, MANA_DEREGISTER_HW_PORT,
743 			     sizeof(req), sizeof(resp));
744 	req.hw_vport_handle = apc->port_handle;
745 
746 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
747 				sizeof(resp));
748 	if (err) {
749 		netdev_err(apc->ndev, "Failed to unregister hw vPort: %d\n",
750 			   err);
751 		return;
752 	}
753 
754 	err = mana_verify_resp_hdr(&resp.hdr, MANA_DEREGISTER_HW_PORT,
755 				   sizeof(resp));
756 	if (err || resp.hdr.status)
757 		netdev_err(apc->ndev,
758 			   "Failed to deregister hw vPort: %d, 0x%x\n",
759 			   err, resp.hdr.status);
760 }
761 
762 static int mana_pf_register_filter(struct mana_port_context *apc)
763 {
764 	struct mana_register_filter_resp resp = {};
765 	struct mana_register_filter_req req = {};
766 	int err;
767 
768 	mana_gd_init_req_hdr(&req.hdr, MANA_REGISTER_FILTER,
769 			     sizeof(req), sizeof(resp));
770 	req.vport = apc->port_handle;
771 	memcpy(req.mac_addr, apc->mac_addr, ETH_ALEN);
772 
773 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
774 				sizeof(resp));
775 	if (err) {
776 		netdev_err(apc->ndev, "Failed to register filter: %d\n", err);
777 		return err;
778 	}
779 
780 	err = mana_verify_resp_hdr(&resp.hdr, MANA_REGISTER_FILTER,
781 				   sizeof(resp));
782 	if (err || resp.hdr.status) {
783 		netdev_err(apc->ndev, "Failed to register filter: %d, 0x%x\n",
784 			   err, resp.hdr.status);
785 		return err ? err : -EPROTO;
786 	}
787 
788 	apc->pf_filter_handle = resp.filter_handle;
789 	return 0;
790 }
791 
792 static void mana_pf_deregister_filter(struct mana_port_context *apc)
793 {
794 	struct mana_deregister_filter_resp resp = {};
795 	struct mana_deregister_filter_req req = {};
796 	int err;
797 
798 	mana_gd_init_req_hdr(&req.hdr, MANA_DEREGISTER_FILTER,
799 			     sizeof(req), sizeof(resp));
800 	req.filter_handle = apc->pf_filter_handle;
801 
802 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
803 				sizeof(resp));
804 	if (err) {
805 		netdev_err(apc->ndev, "Failed to unregister filter: %d\n",
806 			   err);
807 		return;
808 	}
809 
810 	err = mana_verify_resp_hdr(&resp.hdr, MANA_DEREGISTER_FILTER,
811 				   sizeof(resp));
812 	if (err || resp.hdr.status)
813 		netdev_err(apc->ndev,
814 			   "Failed to deregister filter: %d, 0x%x\n",
815 			   err, resp.hdr.status);
816 }
817 
818 static int mana_query_device_cfg(struct mana_context *ac, u32 proto_major_ver,
819 				 u32 proto_minor_ver, u32 proto_micro_ver,
820 				 u16 *max_num_vports)
821 {
822 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
823 	struct mana_query_device_cfg_resp resp = {};
824 	struct mana_query_device_cfg_req req = {};
825 	struct device *dev = gc->dev;
826 	int err = 0;
827 
828 	mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_DEV_CONFIG,
829 			     sizeof(req), sizeof(resp));
830 
831 	req.hdr.resp.msg_version = GDMA_MESSAGE_V2;
832 
833 	req.proto_major_ver = proto_major_ver;
834 	req.proto_minor_ver = proto_minor_ver;
835 	req.proto_micro_ver = proto_micro_ver;
836 
837 	err = mana_send_request(ac, &req, sizeof(req), &resp, sizeof(resp));
838 	if (err) {
839 		dev_err(dev, "Failed to query config: %d", err);
840 		return err;
841 	}
842 
843 	err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_DEV_CONFIG,
844 				   sizeof(resp));
845 	if (err || resp.hdr.status) {
846 		dev_err(dev, "Invalid query result: %d, 0x%x\n", err,
847 			resp.hdr.status);
848 		if (!err)
849 			err = -EPROTO;
850 		return err;
851 	}
852 
853 	*max_num_vports = resp.max_num_vports;
854 
855 	if (resp.hdr.response.msg_version == GDMA_MESSAGE_V2)
856 		gc->adapter_mtu = resp.adapter_mtu;
857 	else
858 		gc->adapter_mtu = ETH_FRAME_LEN;
859 
860 	return 0;
861 }
862 
863 static int mana_query_vport_cfg(struct mana_port_context *apc, u32 vport_index,
864 				u32 *max_sq, u32 *max_rq, u32 *num_indir_entry)
865 {
866 	struct mana_query_vport_cfg_resp resp = {};
867 	struct mana_query_vport_cfg_req req = {};
868 	int err;
869 
870 	mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_VPORT_CONFIG,
871 			     sizeof(req), sizeof(resp));
872 
873 	req.vport_index = vport_index;
874 
875 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
876 				sizeof(resp));
877 	if (err)
878 		return err;
879 
880 	err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_VPORT_CONFIG,
881 				   sizeof(resp));
882 	if (err)
883 		return err;
884 
885 	if (resp.hdr.status)
886 		return -EPROTO;
887 
888 	*max_sq = resp.max_num_sq;
889 	*max_rq = resp.max_num_rq;
890 	*num_indir_entry = resp.num_indirection_ent;
891 
892 	apc->port_handle = resp.vport;
893 	ether_addr_copy(apc->mac_addr, resp.mac_addr);
894 
895 	return 0;
896 }
897 
898 void mana_uncfg_vport(struct mana_port_context *apc)
899 {
900 	mutex_lock(&apc->vport_mutex);
901 	apc->vport_use_count--;
902 	WARN_ON(apc->vport_use_count < 0);
903 	mutex_unlock(&apc->vport_mutex);
904 }
905 EXPORT_SYMBOL_NS(mana_uncfg_vport, NET_MANA);
906 
907 int mana_cfg_vport(struct mana_port_context *apc, u32 protection_dom_id,
908 		   u32 doorbell_pg_id)
909 {
910 	struct mana_config_vport_resp resp = {};
911 	struct mana_config_vport_req req = {};
912 	int err;
913 
914 	/* This function is used to program the Ethernet port in the hardware
915 	 * table. It can be called from the Ethernet driver or the RDMA driver.
916 	 *
917 	 * For Ethernet usage, the hardware supports only one active user on a
918 	 * physical port. The driver checks on the port usage before programming
919 	 * the hardware when creating the RAW QP (RDMA driver) or exposing the
920 	 * device to kernel NET layer (Ethernet driver).
921 	 *
922 	 * Because the RDMA driver doesn't know in advance which QP type the
923 	 * user will create, it exposes the device with all its ports. The user
924 	 * may not be able to create RAW QP on a port if this port is already
925 	 * in used by the Ethernet driver from the kernel.
926 	 *
927 	 * This physical port limitation only applies to the RAW QP. For RC QP,
928 	 * the hardware doesn't have this limitation. The user can create RC
929 	 * QPs on a physical port up to the hardware limits independent of the
930 	 * Ethernet usage on the same port.
931 	 */
932 	mutex_lock(&apc->vport_mutex);
933 	if (apc->vport_use_count > 0) {
934 		mutex_unlock(&apc->vport_mutex);
935 		return -EBUSY;
936 	}
937 	apc->vport_use_count++;
938 	mutex_unlock(&apc->vport_mutex);
939 
940 	mana_gd_init_req_hdr(&req.hdr, MANA_CONFIG_VPORT_TX,
941 			     sizeof(req), sizeof(resp));
942 	req.vport = apc->port_handle;
943 	req.pdid = protection_dom_id;
944 	req.doorbell_pageid = doorbell_pg_id;
945 
946 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
947 				sizeof(resp));
948 	if (err) {
949 		netdev_err(apc->ndev, "Failed to configure vPort: %d\n", err);
950 		goto out;
951 	}
952 
953 	err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_TX,
954 				   sizeof(resp));
955 	if (err || resp.hdr.status) {
956 		netdev_err(apc->ndev, "Failed to configure vPort: %d, 0x%x\n",
957 			   err, resp.hdr.status);
958 		if (!err)
959 			err = -EPROTO;
960 
961 		goto out;
962 	}
963 
964 	apc->tx_shortform_allowed = resp.short_form_allowed;
965 	apc->tx_vp_offset = resp.tx_vport_offset;
966 
967 	netdev_info(apc->ndev, "Configured vPort %llu PD %u DB %u\n",
968 		    apc->port_handle, protection_dom_id, doorbell_pg_id);
969 out:
970 	if (err)
971 		mana_uncfg_vport(apc);
972 
973 	return err;
974 }
975 EXPORT_SYMBOL_NS(mana_cfg_vport, NET_MANA);
976 
977 static int mana_cfg_vport_steering(struct mana_port_context *apc,
978 				   enum TRI_STATE rx,
979 				   bool update_default_rxobj, bool update_key,
980 				   bool update_tab)
981 {
982 	u16 num_entries = MANA_INDIRECT_TABLE_SIZE;
983 	struct mana_cfg_rx_steer_req *req = NULL;
984 	struct mana_cfg_rx_steer_resp resp = {};
985 	struct net_device *ndev = apc->ndev;
986 	mana_handle_t *req_indir_tab;
987 	u32 req_buf_size;
988 	int err;
989 
990 	req_buf_size = sizeof(*req) + sizeof(mana_handle_t) * num_entries;
991 	req = kzalloc(req_buf_size, GFP_KERNEL);
992 	if (!req)
993 		return -ENOMEM;
994 
995 	mana_gd_init_req_hdr(&req->hdr, MANA_CONFIG_VPORT_RX, req_buf_size,
996 			     sizeof(resp));
997 
998 	req->vport = apc->port_handle;
999 	req->num_indir_entries = num_entries;
1000 	req->indir_tab_offset = sizeof(*req);
1001 	req->rx_enable = rx;
1002 	req->rss_enable = apc->rss_state;
1003 	req->update_default_rxobj = update_default_rxobj;
1004 	req->update_hashkey = update_key;
1005 	req->update_indir_tab = update_tab;
1006 	req->default_rxobj = apc->default_rxobj;
1007 
1008 	if (update_key)
1009 		memcpy(&req->hashkey, apc->hashkey, MANA_HASH_KEY_SIZE);
1010 
1011 	if (update_tab) {
1012 		req_indir_tab = (mana_handle_t *)(req + 1);
1013 		memcpy(req_indir_tab, apc->rxobj_table,
1014 		       req->num_indir_entries * sizeof(mana_handle_t));
1015 	}
1016 
1017 	err = mana_send_request(apc->ac, req, req_buf_size, &resp,
1018 				sizeof(resp));
1019 	if (err) {
1020 		netdev_err(ndev, "Failed to configure vPort RX: %d\n", err);
1021 		goto out;
1022 	}
1023 
1024 	err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_RX,
1025 				   sizeof(resp));
1026 	if (err) {
1027 		netdev_err(ndev, "vPort RX configuration failed: %d\n", err);
1028 		goto out;
1029 	}
1030 
1031 	if (resp.hdr.status) {
1032 		netdev_err(ndev, "vPort RX configuration failed: 0x%x\n",
1033 			   resp.hdr.status);
1034 		err = -EPROTO;
1035 	}
1036 
1037 	netdev_info(ndev, "Configured steering vPort %llu entries %u\n",
1038 		    apc->port_handle, num_entries);
1039 out:
1040 	kfree(req);
1041 	return err;
1042 }
1043 
1044 int mana_create_wq_obj(struct mana_port_context *apc,
1045 		       mana_handle_t vport,
1046 		       u32 wq_type, struct mana_obj_spec *wq_spec,
1047 		       struct mana_obj_spec *cq_spec,
1048 		       mana_handle_t *wq_obj)
1049 {
1050 	struct mana_create_wqobj_resp resp = {};
1051 	struct mana_create_wqobj_req req = {};
1052 	struct net_device *ndev = apc->ndev;
1053 	int err;
1054 
1055 	mana_gd_init_req_hdr(&req.hdr, MANA_CREATE_WQ_OBJ,
1056 			     sizeof(req), sizeof(resp));
1057 	req.vport = vport;
1058 	req.wq_type = wq_type;
1059 	req.wq_gdma_region = wq_spec->gdma_region;
1060 	req.cq_gdma_region = cq_spec->gdma_region;
1061 	req.wq_size = wq_spec->queue_size;
1062 	req.cq_size = cq_spec->queue_size;
1063 	req.cq_moderation_ctx_id = cq_spec->modr_ctx_id;
1064 	req.cq_parent_qid = cq_spec->attached_eq;
1065 
1066 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1067 				sizeof(resp));
1068 	if (err) {
1069 		netdev_err(ndev, "Failed to create WQ object: %d\n", err);
1070 		goto out;
1071 	}
1072 
1073 	err = mana_verify_resp_hdr(&resp.hdr, MANA_CREATE_WQ_OBJ,
1074 				   sizeof(resp));
1075 	if (err || resp.hdr.status) {
1076 		netdev_err(ndev, "Failed to create WQ object: %d, 0x%x\n", err,
1077 			   resp.hdr.status);
1078 		if (!err)
1079 			err = -EPROTO;
1080 		goto out;
1081 	}
1082 
1083 	if (resp.wq_obj == INVALID_MANA_HANDLE) {
1084 		netdev_err(ndev, "Got an invalid WQ object handle\n");
1085 		err = -EPROTO;
1086 		goto out;
1087 	}
1088 
1089 	*wq_obj = resp.wq_obj;
1090 	wq_spec->queue_index = resp.wq_id;
1091 	cq_spec->queue_index = resp.cq_id;
1092 
1093 	return 0;
1094 out:
1095 	return err;
1096 }
1097 EXPORT_SYMBOL_NS(mana_create_wq_obj, NET_MANA);
1098 
1099 void mana_destroy_wq_obj(struct mana_port_context *apc, u32 wq_type,
1100 			 mana_handle_t wq_obj)
1101 {
1102 	struct mana_destroy_wqobj_resp resp = {};
1103 	struct mana_destroy_wqobj_req req = {};
1104 	struct net_device *ndev = apc->ndev;
1105 	int err;
1106 
1107 	mana_gd_init_req_hdr(&req.hdr, MANA_DESTROY_WQ_OBJ,
1108 			     sizeof(req), sizeof(resp));
1109 	req.wq_type = wq_type;
1110 	req.wq_obj_handle = wq_obj;
1111 
1112 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1113 				sizeof(resp));
1114 	if (err) {
1115 		netdev_err(ndev, "Failed to destroy WQ object: %d\n", err);
1116 		return;
1117 	}
1118 
1119 	err = mana_verify_resp_hdr(&resp.hdr, MANA_DESTROY_WQ_OBJ,
1120 				   sizeof(resp));
1121 	if (err || resp.hdr.status)
1122 		netdev_err(ndev, "Failed to destroy WQ object: %d, 0x%x\n", err,
1123 			   resp.hdr.status);
1124 }
1125 EXPORT_SYMBOL_NS(mana_destroy_wq_obj, NET_MANA);
1126 
1127 static void mana_destroy_eq(struct mana_context *ac)
1128 {
1129 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
1130 	struct gdma_queue *eq;
1131 	int i;
1132 
1133 	if (!ac->eqs)
1134 		return;
1135 
1136 	for (i = 0; i < gc->max_num_queues; i++) {
1137 		eq = ac->eqs[i].eq;
1138 		if (!eq)
1139 			continue;
1140 
1141 		mana_gd_destroy_queue(gc, eq);
1142 	}
1143 
1144 	kfree(ac->eqs);
1145 	ac->eqs = NULL;
1146 }
1147 
1148 static int mana_create_eq(struct mana_context *ac)
1149 {
1150 	struct gdma_dev *gd = ac->gdma_dev;
1151 	struct gdma_context *gc = gd->gdma_context;
1152 	struct gdma_queue_spec spec = {};
1153 	int err;
1154 	int i;
1155 
1156 	ac->eqs = kcalloc(gc->max_num_queues, sizeof(struct mana_eq),
1157 			  GFP_KERNEL);
1158 	if (!ac->eqs)
1159 		return -ENOMEM;
1160 
1161 	spec.type = GDMA_EQ;
1162 	spec.monitor_avl_buf = false;
1163 	spec.queue_size = EQ_SIZE;
1164 	spec.eq.callback = NULL;
1165 	spec.eq.context = ac->eqs;
1166 	spec.eq.log2_throttle_limit = LOG2_EQ_THROTTLE;
1167 
1168 	for (i = 0; i < gc->max_num_queues; i++) {
1169 		err = mana_gd_create_mana_eq(gd, &spec, &ac->eqs[i].eq);
1170 		if (err)
1171 			goto out;
1172 	}
1173 
1174 	return 0;
1175 out:
1176 	mana_destroy_eq(ac);
1177 	return err;
1178 }
1179 
1180 static int mana_fence_rq(struct mana_port_context *apc, struct mana_rxq *rxq)
1181 {
1182 	struct mana_fence_rq_resp resp = {};
1183 	struct mana_fence_rq_req req = {};
1184 	int err;
1185 
1186 	init_completion(&rxq->fence_event);
1187 
1188 	mana_gd_init_req_hdr(&req.hdr, MANA_FENCE_RQ,
1189 			     sizeof(req), sizeof(resp));
1190 	req.wq_obj_handle =  rxq->rxobj;
1191 
1192 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1193 				sizeof(resp));
1194 	if (err) {
1195 		netdev_err(apc->ndev, "Failed to fence RQ %u: %d\n",
1196 			   rxq->rxq_idx, err);
1197 		return err;
1198 	}
1199 
1200 	err = mana_verify_resp_hdr(&resp.hdr, MANA_FENCE_RQ, sizeof(resp));
1201 	if (err || resp.hdr.status) {
1202 		netdev_err(apc->ndev, "Failed to fence RQ %u: %d, 0x%x\n",
1203 			   rxq->rxq_idx, err, resp.hdr.status);
1204 		if (!err)
1205 			err = -EPROTO;
1206 
1207 		return err;
1208 	}
1209 
1210 	if (wait_for_completion_timeout(&rxq->fence_event, 10 * HZ) == 0) {
1211 		netdev_err(apc->ndev, "Failed to fence RQ %u: timed out\n",
1212 			   rxq->rxq_idx);
1213 		return -ETIMEDOUT;
1214 	}
1215 
1216 	return 0;
1217 }
1218 
1219 static void mana_fence_rqs(struct mana_port_context *apc)
1220 {
1221 	unsigned int rxq_idx;
1222 	struct mana_rxq *rxq;
1223 	int err;
1224 
1225 	for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) {
1226 		rxq = apc->rxqs[rxq_idx];
1227 		err = mana_fence_rq(apc, rxq);
1228 
1229 		/* In case of any error, use sleep instead. */
1230 		if (err)
1231 			msleep(100);
1232 	}
1233 }
1234 
1235 static int mana_move_wq_tail(struct gdma_queue *wq, u32 num_units)
1236 {
1237 	u32 used_space_old;
1238 	u32 used_space_new;
1239 
1240 	used_space_old = wq->head - wq->tail;
1241 	used_space_new = wq->head - (wq->tail + num_units);
1242 
1243 	if (WARN_ON_ONCE(used_space_new > used_space_old))
1244 		return -ERANGE;
1245 
1246 	wq->tail += num_units;
1247 	return 0;
1248 }
1249 
1250 static void mana_unmap_skb(struct sk_buff *skb, struct mana_port_context *apc)
1251 {
1252 	struct mana_skb_head *ash = (struct mana_skb_head *)skb->head;
1253 	struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1254 	struct device *dev = gc->dev;
1255 	int i;
1256 
1257 	dma_unmap_single(dev, ash->dma_handle[0], ash->size[0], DMA_TO_DEVICE);
1258 
1259 	for (i = 1; i < skb_shinfo(skb)->nr_frags + 1; i++)
1260 		dma_unmap_page(dev, ash->dma_handle[i], ash->size[i],
1261 			       DMA_TO_DEVICE);
1262 }
1263 
1264 static void mana_poll_tx_cq(struct mana_cq *cq)
1265 {
1266 	struct gdma_comp *completions = cq->gdma_comp_buf;
1267 	struct gdma_posted_wqe_info *wqe_info;
1268 	unsigned int pkt_transmitted = 0;
1269 	unsigned int wqe_unit_cnt = 0;
1270 	struct mana_txq *txq = cq->txq;
1271 	struct mana_port_context *apc;
1272 	struct netdev_queue *net_txq;
1273 	struct gdma_queue *gdma_wq;
1274 	unsigned int avail_space;
1275 	struct net_device *ndev;
1276 	struct sk_buff *skb;
1277 	bool txq_stopped;
1278 	int comp_read;
1279 	int i;
1280 
1281 	ndev = txq->ndev;
1282 	apc = netdev_priv(ndev);
1283 
1284 	comp_read = mana_gd_poll_cq(cq->gdma_cq, completions,
1285 				    CQE_POLLING_BUFFER);
1286 
1287 	if (comp_read < 1)
1288 		return;
1289 
1290 	for (i = 0; i < comp_read; i++) {
1291 		struct mana_tx_comp_oob *cqe_oob;
1292 
1293 		if (WARN_ON_ONCE(!completions[i].is_sq))
1294 			return;
1295 
1296 		cqe_oob = (struct mana_tx_comp_oob *)completions[i].cqe_data;
1297 		if (WARN_ON_ONCE(cqe_oob->cqe_hdr.client_type !=
1298 				 MANA_CQE_COMPLETION))
1299 			return;
1300 
1301 		switch (cqe_oob->cqe_hdr.cqe_type) {
1302 		case CQE_TX_OKAY:
1303 			break;
1304 
1305 		case CQE_TX_SA_DROP:
1306 		case CQE_TX_MTU_DROP:
1307 		case CQE_TX_INVALID_OOB:
1308 		case CQE_TX_INVALID_ETH_TYPE:
1309 		case CQE_TX_HDR_PROCESSING_ERROR:
1310 		case CQE_TX_VF_DISABLED:
1311 		case CQE_TX_VPORT_IDX_OUT_OF_RANGE:
1312 		case CQE_TX_VPORT_DISABLED:
1313 		case CQE_TX_VLAN_TAGGING_VIOLATION:
1314 			WARN_ONCE(1, "TX: CQE error %d: ignored.\n",
1315 				  cqe_oob->cqe_hdr.cqe_type);
1316 			apc->eth_stats.tx_cqe_err++;
1317 			break;
1318 
1319 		default:
1320 			/* If the CQE type is unexpected, log an error, assert,
1321 			 * and go through the error path.
1322 			 */
1323 			WARN_ONCE(1, "TX: Unexpected CQE type %d: HW BUG?\n",
1324 				  cqe_oob->cqe_hdr.cqe_type);
1325 			apc->eth_stats.tx_cqe_unknown_type++;
1326 			return;
1327 		}
1328 
1329 		if (WARN_ON_ONCE(txq->gdma_txq_id != completions[i].wq_num))
1330 			return;
1331 
1332 		skb = skb_dequeue(&txq->pending_skbs);
1333 		if (WARN_ON_ONCE(!skb))
1334 			return;
1335 
1336 		wqe_info = (struct gdma_posted_wqe_info *)skb->cb;
1337 		wqe_unit_cnt += wqe_info->wqe_size_in_bu;
1338 
1339 		mana_unmap_skb(skb, apc);
1340 
1341 		napi_consume_skb(skb, cq->budget);
1342 
1343 		pkt_transmitted++;
1344 	}
1345 
1346 	if (WARN_ON_ONCE(wqe_unit_cnt == 0))
1347 		return;
1348 
1349 	mana_move_wq_tail(txq->gdma_sq, wqe_unit_cnt);
1350 
1351 	gdma_wq = txq->gdma_sq;
1352 	avail_space = mana_gd_wq_avail_space(gdma_wq);
1353 
1354 	/* Ensure tail updated before checking q stop */
1355 	smp_mb();
1356 
1357 	net_txq = txq->net_txq;
1358 	txq_stopped = netif_tx_queue_stopped(net_txq);
1359 
1360 	/* Ensure checking txq_stopped before apc->port_is_up. */
1361 	smp_rmb();
1362 
1363 	if (txq_stopped && apc->port_is_up && avail_space >= MAX_TX_WQE_SIZE) {
1364 		netif_tx_wake_queue(net_txq);
1365 		apc->eth_stats.wake_queue++;
1366 	}
1367 
1368 	if (atomic_sub_return(pkt_transmitted, &txq->pending_sends) < 0)
1369 		WARN_ON_ONCE(1);
1370 
1371 	cq->work_done = pkt_transmitted;
1372 }
1373 
1374 static void mana_post_pkt_rxq(struct mana_rxq *rxq)
1375 {
1376 	struct mana_recv_buf_oob *recv_buf_oob;
1377 	u32 curr_index;
1378 	int err;
1379 
1380 	curr_index = rxq->buf_index++;
1381 	if (rxq->buf_index == rxq->num_rx_buf)
1382 		rxq->buf_index = 0;
1383 
1384 	recv_buf_oob = &rxq->rx_oobs[curr_index];
1385 
1386 	err = mana_gd_post_and_ring(rxq->gdma_rq, &recv_buf_oob->wqe_req,
1387 				    &recv_buf_oob->wqe_inf);
1388 	if (WARN_ON_ONCE(err))
1389 		return;
1390 
1391 	WARN_ON_ONCE(recv_buf_oob->wqe_inf.wqe_size_in_bu != 1);
1392 }
1393 
1394 static struct sk_buff *mana_build_skb(struct mana_rxq *rxq, void *buf_va,
1395 				      uint pkt_len, struct xdp_buff *xdp)
1396 {
1397 	struct sk_buff *skb = napi_build_skb(buf_va, rxq->alloc_size);
1398 
1399 	if (!skb)
1400 		return NULL;
1401 
1402 	if (xdp->data_hard_start) {
1403 		skb_reserve(skb, xdp->data - xdp->data_hard_start);
1404 		skb_put(skb, xdp->data_end - xdp->data);
1405 		return skb;
1406 	}
1407 
1408 	skb_reserve(skb, rxq->headroom);
1409 	skb_put(skb, pkt_len);
1410 
1411 	return skb;
1412 }
1413 
1414 static void mana_rx_skb(void *buf_va, struct mana_rxcomp_oob *cqe,
1415 			struct mana_rxq *rxq)
1416 {
1417 	struct mana_stats_rx *rx_stats = &rxq->stats;
1418 	struct net_device *ndev = rxq->ndev;
1419 	uint pkt_len = cqe->ppi[0].pkt_len;
1420 	u16 rxq_idx = rxq->rxq_idx;
1421 	struct napi_struct *napi;
1422 	struct xdp_buff xdp = {};
1423 	struct sk_buff *skb;
1424 	u32 hash_value;
1425 	u32 act;
1426 
1427 	rxq->rx_cq.work_done++;
1428 	napi = &rxq->rx_cq.napi;
1429 
1430 	if (!buf_va) {
1431 		++ndev->stats.rx_dropped;
1432 		return;
1433 	}
1434 
1435 	act = mana_run_xdp(ndev, rxq, &xdp, buf_va, pkt_len);
1436 
1437 	if (act == XDP_REDIRECT && !rxq->xdp_rc)
1438 		return;
1439 
1440 	if (act != XDP_PASS && act != XDP_TX)
1441 		goto drop_xdp;
1442 
1443 	skb = mana_build_skb(rxq, buf_va, pkt_len, &xdp);
1444 
1445 	if (!skb)
1446 		goto drop;
1447 
1448 	skb->dev = napi->dev;
1449 
1450 	skb->protocol = eth_type_trans(skb, ndev);
1451 	skb_checksum_none_assert(skb);
1452 	skb_record_rx_queue(skb, rxq_idx);
1453 
1454 	if ((ndev->features & NETIF_F_RXCSUM) && cqe->rx_iphdr_csum_succeed) {
1455 		if (cqe->rx_tcp_csum_succeed || cqe->rx_udp_csum_succeed)
1456 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1457 	}
1458 
1459 	if (cqe->rx_hashtype != 0 && (ndev->features & NETIF_F_RXHASH)) {
1460 		hash_value = cqe->ppi[0].pkt_hash;
1461 
1462 		if (cqe->rx_hashtype & MANA_HASH_L4)
1463 			skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L4);
1464 		else
1465 			skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L3);
1466 	}
1467 
1468 	if (cqe->rx_vlantag_present) {
1469 		u16 vlan_tci = cqe->rx_vlan_id;
1470 
1471 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
1472 	}
1473 
1474 	u64_stats_update_begin(&rx_stats->syncp);
1475 	rx_stats->packets++;
1476 	rx_stats->bytes += pkt_len;
1477 
1478 	if (act == XDP_TX)
1479 		rx_stats->xdp_tx++;
1480 	u64_stats_update_end(&rx_stats->syncp);
1481 
1482 	if (act == XDP_TX) {
1483 		skb_set_queue_mapping(skb, rxq_idx);
1484 		mana_xdp_tx(skb, ndev);
1485 		return;
1486 	}
1487 
1488 	napi_gro_receive(napi, skb);
1489 
1490 	return;
1491 
1492 drop_xdp:
1493 	u64_stats_update_begin(&rx_stats->syncp);
1494 	rx_stats->xdp_drop++;
1495 	u64_stats_update_end(&rx_stats->syncp);
1496 
1497 drop:
1498 	WARN_ON_ONCE(rxq->xdp_save_va);
1499 	/* Save for reuse */
1500 	rxq->xdp_save_va = buf_va;
1501 
1502 	++ndev->stats.rx_dropped;
1503 
1504 	return;
1505 }
1506 
1507 static void *mana_get_rxfrag(struct mana_rxq *rxq, struct device *dev,
1508 			     dma_addr_t *da, bool is_napi)
1509 {
1510 	struct page *page;
1511 	void *va;
1512 
1513 	/* Reuse XDP dropped page if available */
1514 	if (rxq->xdp_save_va) {
1515 		va = rxq->xdp_save_va;
1516 		rxq->xdp_save_va = NULL;
1517 	} else if (rxq->alloc_size > PAGE_SIZE) {
1518 		if (is_napi)
1519 			va = napi_alloc_frag(rxq->alloc_size);
1520 		else
1521 			va = netdev_alloc_frag(rxq->alloc_size);
1522 
1523 		if (!va)
1524 			return NULL;
1525 
1526 		page = virt_to_head_page(va);
1527 		/* Check if the frag falls back to single page */
1528 		if (compound_order(page) < get_order(rxq->alloc_size)) {
1529 			put_page(page);
1530 			return NULL;
1531 		}
1532 	} else {
1533 		page = dev_alloc_page();
1534 		if (!page)
1535 			return NULL;
1536 
1537 		va = page_to_virt(page);
1538 	}
1539 
1540 	*da = dma_map_single(dev, va + rxq->headroom, rxq->datasize,
1541 			     DMA_FROM_DEVICE);
1542 	if (dma_mapping_error(dev, *da)) {
1543 		put_page(virt_to_head_page(va));
1544 		return NULL;
1545 	}
1546 
1547 	return va;
1548 }
1549 
1550 /* Allocate frag for rx buffer, and save the old buf */
1551 static void mana_refill_rx_oob(struct device *dev, struct mana_rxq *rxq,
1552 			       struct mana_recv_buf_oob *rxoob, void **old_buf)
1553 {
1554 	dma_addr_t da;
1555 	void *va;
1556 
1557 	va = mana_get_rxfrag(rxq, dev, &da, true);
1558 	if (!va)
1559 		return;
1560 
1561 	dma_unmap_single(dev, rxoob->sgl[0].address, rxq->datasize,
1562 			 DMA_FROM_DEVICE);
1563 	*old_buf = rxoob->buf_va;
1564 
1565 	rxoob->buf_va = va;
1566 	rxoob->sgl[0].address = da;
1567 }
1568 
1569 static void mana_process_rx_cqe(struct mana_rxq *rxq, struct mana_cq *cq,
1570 				struct gdma_comp *cqe)
1571 {
1572 	struct mana_rxcomp_oob *oob = (struct mana_rxcomp_oob *)cqe->cqe_data;
1573 	struct gdma_context *gc = rxq->gdma_rq->gdma_dev->gdma_context;
1574 	struct net_device *ndev = rxq->ndev;
1575 	struct mana_recv_buf_oob *rxbuf_oob;
1576 	struct mana_port_context *apc;
1577 	struct device *dev = gc->dev;
1578 	void *old_buf = NULL;
1579 	u32 curr, pktlen;
1580 
1581 	apc = netdev_priv(ndev);
1582 
1583 	switch (oob->cqe_hdr.cqe_type) {
1584 	case CQE_RX_OKAY:
1585 		break;
1586 
1587 	case CQE_RX_TRUNCATED:
1588 		++ndev->stats.rx_dropped;
1589 		rxbuf_oob = &rxq->rx_oobs[rxq->buf_index];
1590 		netdev_warn_once(ndev, "Dropped a truncated packet\n");
1591 		goto drop;
1592 
1593 	case CQE_RX_COALESCED_4:
1594 		netdev_err(ndev, "RX coalescing is unsupported\n");
1595 		apc->eth_stats.rx_coalesced_err++;
1596 		return;
1597 
1598 	case CQE_RX_OBJECT_FENCE:
1599 		complete(&rxq->fence_event);
1600 		return;
1601 
1602 	default:
1603 		netdev_err(ndev, "Unknown RX CQE type = %d\n",
1604 			   oob->cqe_hdr.cqe_type);
1605 		apc->eth_stats.rx_cqe_unknown_type++;
1606 		return;
1607 	}
1608 
1609 	pktlen = oob->ppi[0].pkt_len;
1610 
1611 	if (pktlen == 0) {
1612 		/* data packets should never have packetlength of zero */
1613 		netdev_err(ndev, "RX pkt len=0, rq=%u, cq=%u, rxobj=0x%llx\n",
1614 			   rxq->gdma_id, cq->gdma_id, rxq->rxobj);
1615 		return;
1616 	}
1617 
1618 	curr = rxq->buf_index;
1619 	rxbuf_oob = &rxq->rx_oobs[curr];
1620 	WARN_ON_ONCE(rxbuf_oob->wqe_inf.wqe_size_in_bu != 1);
1621 
1622 	mana_refill_rx_oob(dev, rxq, rxbuf_oob, &old_buf);
1623 
1624 	/* Unsuccessful refill will have old_buf == NULL.
1625 	 * In this case, mana_rx_skb() will drop the packet.
1626 	 */
1627 	mana_rx_skb(old_buf, oob, rxq);
1628 
1629 drop:
1630 	mana_move_wq_tail(rxq->gdma_rq, rxbuf_oob->wqe_inf.wqe_size_in_bu);
1631 
1632 	mana_post_pkt_rxq(rxq);
1633 }
1634 
1635 static void mana_poll_rx_cq(struct mana_cq *cq)
1636 {
1637 	struct gdma_comp *comp = cq->gdma_comp_buf;
1638 	struct mana_rxq *rxq = cq->rxq;
1639 	int comp_read, i;
1640 
1641 	comp_read = mana_gd_poll_cq(cq->gdma_cq, comp, CQE_POLLING_BUFFER);
1642 	WARN_ON_ONCE(comp_read > CQE_POLLING_BUFFER);
1643 
1644 	rxq->xdp_flush = false;
1645 
1646 	for (i = 0; i < comp_read; i++) {
1647 		if (WARN_ON_ONCE(comp[i].is_sq))
1648 			return;
1649 
1650 		/* verify recv cqe references the right rxq */
1651 		if (WARN_ON_ONCE(comp[i].wq_num != cq->rxq->gdma_id))
1652 			return;
1653 
1654 		mana_process_rx_cqe(rxq, cq, &comp[i]);
1655 	}
1656 
1657 	if (rxq->xdp_flush)
1658 		xdp_do_flush();
1659 }
1660 
1661 static int mana_cq_handler(void *context, struct gdma_queue *gdma_queue)
1662 {
1663 	struct mana_cq *cq = context;
1664 	u8 arm_bit;
1665 	int w;
1666 
1667 	WARN_ON_ONCE(cq->gdma_cq != gdma_queue);
1668 
1669 	if (cq->type == MANA_CQ_TYPE_RX)
1670 		mana_poll_rx_cq(cq);
1671 	else
1672 		mana_poll_tx_cq(cq);
1673 
1674 	w = cq->work_done;
1675 
1676 	if (w < cq->budget &&
1677 	    napi_complete_done(&cq->napi, w)) {
1678 		arm_bit = SET_ARM_BIT;
1679 	} else {
1680 		arm_bit = 0;
1681 	}
1682 
1683 	mana_gd_ring_cq(gdma_queue, arm_bit);
1684 
1685 	return w;
1686 }
1687 
1688 static int mana_poll(struct napi_struct *napi, int budget)
1689 {
1690 	struct mana_cq *cq = container_of(napi, struct mana_cq, napi);
1691 	int w;
1692 
1693 	cq->work_done = 0;
1694 	cq->budget = budget;
1695 
1696 	w = mana_cq_handler(cq, cq->gdma_cq);
1697 
1698 	return min(w, budget);
1699 }
1700 
1701 static void mana_schedule_napi(void *context, struct gdma_queue *gdma_queue)
1702 {
1703 	struct mana_cq *cq = context;
1704 
1705 	napi_schedule_irqoff(&cq->napi);
1706 }
1707 
1708 static void mana_deinit_cq(struct mana_port_context *apc, struct mana_cq *cq)
1709 {
1710 	struct gdma_dev *gd = apc->ac->gdma_dev;
1711 
1712 	if (!cq->gdma_cq)
1713 		return;
1714 
1715 	mana_gd_destroy_queue(gd->gdma_context, cq->gdma_cq);
1716 }
1717 
1718 static void mana_deinit_txq(struct mana_port_context *apc, struct mana_txq *txq)
1719 {
1720 	struct gdma_dev *gd = apc->ac->gdma_dev;
1721 
1722 	if (!txq->gdma_sq)
1723 		return;
1724 
1725 	mana_gd_destroy_queue(gd->gdma_context, txq->gdma_sq);
1726 }
1727 
1728 static void mana_destroy_txq(struct mana_port_context *apc)
1729 {
1730 	struct napi_struct *napi;
1731 	int i;
1732 
1733 	if (!apc->tx_qp)
1734 		return;
1735 
1736 	for (i = 0; i < apc->num_queues; i++) {
1737 		napi = &apc->tx_qp[i].tx_cq.napi;
1738 		napi_synchronize(napi);
1739 		napi_disable(napi);
1740 		netif_napi_del(napi);
1741 
1742 		mana_destroy_wq_obj(apc, GDMA_SQ, apc->tx_qp[i].tx_object);
1743 
1744 		mana_deinit_cq(apc, &apc->tx_qp[i].tx_cq);
1745 
1746 		mana_deinit_txq(apc, &apc->tx_qp[i].txq);
1747 	}
1748 
1749 	kfree(apc->tx_qp);
1750 	apc->tx_qp = NULL;
1751 }
1752 
1753 static int mana_create_txq(struct mana_port_context *apc,
1754 			   struct net_device *net)
1755 {
1756 	struct mana_context *ac = apc->ac;
1757 	struct gdma_dev *gd = ac->gdma_dev;
1758 	struct mana_obj_spec wq_spec;
1759 	struct mana_obj_spec cq_spec;
1760 	struct gdma_queue_spec spec;
1761 	struct gdma_context *gc;
1762 	struct mana_txq *txq;
1763 	struct mana_cq *cq;
1764 	u32 txq_size;
1765 	u32 cq_size;
1766 	int err;
1767 	int i;
1768 
1769 	apc->tx_qp = kcalloc(apc->num_queues, sizeof(struct mana_tx_qp),
1770 			     GFP_KERNEL);
1771 	if (!apc->tx_qp)
1772 		return -ENOMEM;
1773 
1774 	/*  The minimum size of the WQE is 32 bytes, hence
1775 	 *  MAX_SEND_BUFFERS_PER_QUEUE represents the maximum number of WQEs
1776 	 *  the SQ can store. This value is then used to size other queues
1777 	 *  to prevent overflow.
1778 	 */
1779 	txq_size = MAX_SEND_BUFFERS_PER_QUEUE * 32;
1780 	BUILD_BUG_ON(!PAGE_ALIGNED(txq_size));
1781 
1782 	cq_size = MAX_SEND_BUFFERS_PER_QUEUE * COMP_ENTRY_SIZE;
1783 	cq_size = PAGE_ALIGN(cq_size);
1784 
1785 	gc = gd->gdma_context;
1786 
1787 	for (i = 0; i < apc->num_queues; i++) {
1788 		apc->tx_qp[i].tx_object = INVALID_MANA_HANDLE;
1789 
1790 		/* Create SQ */
1791 		txq = &apc->tx_qp[i].txq;
1792 
1793 		u64_stats_init(&txq->stats.syncp);
1794 		txq->ndev = net;
1795 		txq->net_txq = netdev_get_tx_queue(net, i);
1796 		txq->vp_offset = apc->tx_vp_offset;
1797 		skb_queue_head_init(&txq->pending_skbs);
1798 
1799 		memset(&spec, 0, sizeof(spec));
1800 		spec.type = GDMA_SQ;
1801 		spec.monitor_avl_buf = true;
1802 		spec.queue_size = txq_size;
1803 		err = mana_gd_create_mana_wq_cq(gd, &spec, &txq->gdma_sq);
1804 		if (err)
1805 			goto out;
1806 
1807 		/* Create SQ's CQ */
1808 		cq = &apc->tx_qp[i].tx_cq;
1809 		cq->type = MANA_CQ_TYPE_TX;
1810 
1811 		cq->txq = txq;
1812 
1813 		memset(&spec, 0, sizeof(spec));
1814 		spec.type = GDMA_CQ;
1815 		spec.monitor_avl_buf = false;
1816 		spec.queue_size = cq_size;
1817 		spec.cq.callback = mana_schedule_napi;
1818 		spec.cq.parent_eq = ac->eqs[i].eq;
1819 		spec.cq.context = cq;
1820 		err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq);
1821 		if (err)
1822 			goto out;
1823 
1824 		memset(&wq_spec, 0, sizeof(wq_spec));
1825 		memset(&cq_spec, 0, sizeof(cq_spec));
1826 
1827 		wq_spec.gdma_region = txq->gdma_sq->mem_info.dma_region_handle;
1828 		wq_spec.queue_size = txq->gdma_sq->queue_size;
1829 
1830 		cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle;
1831 		cq_spec.queue_size = cq->gdma_cq->queue_size;
1832 		cq_spec.modr_ctx_id = 0;
1833 		cq_spec.attached_eq = cq->gdma_cq->cq.parent->id;
1834 
1835 		err = mana_create_wq_obj(apc, apc->port_handle, GDMA_SQ,
1836 					 &wq_spec, &cq_spec,
1837 					 &apc->tx_qp[i].tx_object);
1838 
1839 		if (err)
1840 			goto out;
1841 
1842 		txq->gdma_sq->id = wq_spec.queue_index;
1843 		cq->gdma_cq->id = cq_spec.queue_index;
1844 
1845 		txq->gdma_sq->mem_info.dma_region_handle =
1846 			GDMA_INVALID_DMA_REGION;
1847 		cq->gdma_cq->mem_info.dma_region_handle =
1848 			GDMA_INVALID_DMA_REGION;
1849 
1850 		txq->gdma_txq_id = txq->gdma_sq->id;
1851 
1852 		cq->gdma_id = cq->gdma_cq->id;
1853 
1854 		if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) {
1855 			err = -EINVAL;
1856 			goto out;
1857 		}
1858 
1859 		gc->cq_table[cq->gdma_id] = cq->gdma_cq;
1860 
1861 		netif_napi_add_tx(net, &cq->napi, mana_poll);
1862 		napi_enable(&cq->napi);
1863 
1864 		mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT);
1865 	}
1866 
1867 	return 0;
1868 out:
1869 	mana_destroy_txq(apc);
1870 	return err;
1871 }
1872 
1873 static void mana_destroy_rxq(struct mana_port_context *apc,
1874 			     struct mana_rxq *rxq, bool validate_state)
1875 
1876 {
1877 	struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1878 	struct mana_recv_buf_oob *rx_oob;
1879 	struct device *dev = gc->dev;
1880 	struct napi_struct *napi;
1881 	int i;
1882 
1883 	if (!rxq)
1884 		return;
1885 
1886 	napi = &rxq->rx_cq.napi;
1887 
1888 	if (validate_state)
1889 		napi_synchronize(napi);
1890 
1891 	napi_disable(napi);
1892 
1893 	xdp_rxq_info_unreg(&rxq->xdp_rxq);
1894 
1895 	netif_napi_del(napi);
1896 
1897 	mana_destroy_wq_obj(apc, GDMA_RQ, rxq->rxobj);
1898 
1899 	mana_deinit_cq(apc, &rxq->rx_cq);
1900 
1901 	if (rxq->xdp_save_va)
1902 		put_page(virt_to_head_page(rxq->xdp_save_va));
1903 
1904 	for (i = 0; i < rxq->num_rx_buf; i++) {
1905 		rx_oob = &rxq->rx_oobs[i];
1906 
1907 		if (!rx_oob->buf_va)
1908 			continue;
1909 
1910 		dma_unmap_single(dev, rx_oob->sgl[0].address,
1911 				 rx_oob->sgl[0].size, DMA_FROM_DEVICE);
1912 
1913 		put_page(virt_to_head_page(rx_oob->buf_va));
1914 		rx_oob->buf_va = NULL;
1915 	}
1916 
1917 	if (rxq->gdma_rq)
1918 		mana_gd_destroy_queue(gc, rxq->gdma_rq);
1919 
1920 	kfree(rxq);
1921 }
1922 
1923 static int mana_fill_rx_oob(struct mana_recv_buf_oob *rx_oob, u32 mem_key,
1924 			    struct mana_rxq *rxq, struct device *dev)
1925 {
1926 	struct mana_port_context *mpc = netdev_priv(rxq->ndev);
1927 	dma_addr_t da;
1928 	void *va;
1929 
1930 	if (mpc->rxbufs_pre)
1931 		va = mana_get_rxbuf_pre(rxq, &da);
1932 	else
1933 		va = mana_get_rxfrag(rxq, dev, &da, false);
1934 
1935 	if (!va)
1936 		return -ENOMEM;
1937 
1938 	rx_oob->buf_va = va;
1939 
1940 	rx_oob->sgl[0].address = da;
1941 	rx_oob->sgl[0].size = rxq->datasize;
1942 	rx_oob->sgl[0].mem_key = mem_key;
1943 
1944 	return 0;
1945 }
1946 
1947 #define MANA_WQE_HEADER_SIZE 16
1948 #define MANA_WQE_SGE_SIZE 16
1949 
1950 static int mana_alloc_rx_wqe(struct mana_port_context *apc,
1951 			     struct mana_rxq *rxq, u32 *rxq_size, u32 *cq_size)
1952 {
1953 	struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1954 	struct mana_recv_buf_oob *rx_oob;
1955 	struct device *dev = gc->dev;
1956 	u32 buf_idx;
1957 	int ret;
1958 
1959 	WARN_ON(rxq->datasize == 0);
1960 
1961 	*rxq_size = 0;
1962 	*cq_size = 0;
1963 
1964 	for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) {
1965 		rx_oob = &rxq->rx_oobs[buf_idx];
1966 		memset(rx_oob, 0, sizeof(*rx_oob));
1967 
1968 		rx_oob->num_sge = 1;
1969 
1970 		ret = mana_fill_rx_oob(rx_oob, apc->ac->gdma_dev->gpa_mkey, rxq,
1971 				       dev);
1972 		if (ret)
1973 			return ret;
1974 
1975 		rx_oob->wqe_req.sgl = rx_oob->sgl;
1976 		rx_oob->wqe_req.num_sge = rx_oob->num_sge;
1977 		rx_oob->wqe_req.inline_oob_size = 0;
1978 		rx_oob->wqe_req.inline_oob_data = NULL;
1979 		rx_oob->wqe_req.flags = 0;
1980 		rx_oob->wqe_req.client_data_unit = 0;
1981 
1982 		*rxq_size += ALIGN(MANA_WQE_HEADER_SIZE +
1983 				   MANA_WQE_SGE_SIZE * rx_oob->num_sge, 32);
1984 		*cq_size += COMP_ENTRY_SIZE;
1985 	}
1986 
1987 	return 0;
1988 }
1989 
1990 static int mana_push_wqe(struct mana_rxq *rxq)
1991 {
1992 	struct mana_recv_buf_oob *rx_oob;
1993 	u32 buf_idx;
1994 	int err;
1995 
1996 	for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) {
1997 		rx_oob = &rxq->rx_oobs[buf_idx];
1998 
1999 		err = mana_gd_post_and_ring(rxq->gdma_rq, &rx_oob->wqe_req,
2000 					    &rx_oob->wqe_inf);
2001 		if (err)
2002 			return -ENOSPC;
2003 	}
2004 
2005 	return 0;
2006 }
2007 
2008 static struct mana_rxq *mana_create_rxq(struct mana_port_context *apc,
2009 					u32 rxq_idx, struct mana_eq *eq,
2010 					struct net_device *ndev)
2011 {
2012 	struct gdma_dev *gd = apc->ac->gdma_dev;
2013 	struct mana_obj_spec wq_spec;
2014 	struct mana_obj_spec cq_spec;
2015 	struct gdma_queue_spec spec;
2016 	struct mana_cq *cq = NULL;
2017 	struct gdma_context *gc;
2018 	u32 cq_size, rq_size;
2019 	struct mana_rxq *rxq;
2020 	int err;
2021 
2022 	gc = gd->gdma_context;
2023 
2024 	rxq = kzalloc(struct_size(rxq, rx_oobs, RX_BUFFERS_PER_QUEUE),
2025 		      GFP_KERNEL);
2026 	if (!rxq)
2027 		return NULL;
2028 
2029 	rxq->ndev = ndev;
2030 	rxq->num_rx_buf = RX_BUFFERS_PER_QUEUE;
2031 	rxq->rxq_idx = rxq_idx;
2032 	rxq->rxobj = INVALID_MANA_HANDLE;
2033 
2034 	mana_get_rxbuf_cfg(ndev->mtu, &rxq->datasize, &rxq->alloc_size,
2035 			   &rxq->headroom);
2036 
2037 	err = mana_alloc_rx_wqe(apc, rxq, &rq_size, &cq_size);
2038 	if (err)
2039 		goto out;
2040 
2041 	rq_size = PAGE_ALIGN(rq_size);
2042 	cq_size = PAGE_ALIGN(cq_size);
2043 
2044 	/* Create RQ */
2045 	memset(&spec, 0, sizeof(spec));
2046 	spec.type = GDMA_RQ;
2047 	spec.monitor_avl_buf = true;
2048 	spec.queue_size = rq_size;
2049 	err = mana_gd_create_mana_wq_cq(gd, &spec, &rxq->gdma_rq);
2050 	if (err)
2051 		goto out;
2052 
2053 	/* Create RQ's CQ */
2054 	cq = &rxq->rx_cq;
2055 	cq->type = MANA_CQ_TYPE_RX;
2056 	cq->rxq = rxq;
2057 
2058 	memset(&spec, 0, sizeof(spec));
2059 	spec.type = GDMA_CQ;
2060 	spec.monitor_avl_buf = false;
2061 	spec.queue_size = cq_size;
2062 	spec.cq.callback = mana_schedule_napi;
2063 	spec.cq.parent_eq = eq->eq;
2064 	spec.cq.context = cq;
2065 	err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq);
2066 	if (err)
2067 		goto out;
2068 
2069 	memset(&wq_spec, 0, sizeof(wq_spec));
2070 	memset(&cq_spec, 0, sizeof(cq_spec));
2071 	wq_spec.gdma_region = rxq->gdma_rq->mem_info.dma_region_handle;
2072 	wq_spec.queue_size = rxq->gdma_rq->queue_size;
2073 
2074 	cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle;
2075 	cq_spec.queue_size = cq->gdma_cq->queue_size;
2076 	cq_spec.modr_ctx_id = 0;
2077 	cq_spec.attached_eq = cq->gdma_cq->cq.parent->id;
2078 
2079 	err = mana_create_wq_obj(apc, apc->port_handle, GDMA_RQ,
2080 				 &wq_spec, &cq_spec, &rxq->rxobj);
2081 	if (err)
2082 		goto out;
2083 
2084 	rxq->gdma_rq->id = wq_spec.queue_index;
2085 	cq->gdma_cq->id = cq_spec.queue_index;
2086 
2087 	rxq->gdma_rq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION;
2088 	cq->gdma_cq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION;
2089 
2090 	rxq->gdma_id = rxq->gdma_rq->id;
2091 	cq->gdma_id = cq->gdma_cq->id;
2092 
2093 	err = mana_push_wqe(rxq);
2094 	if (err)
2095 		goto out;
2096 
2097 	if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) {
2098 		err = -EINVAL;
2099 		goto out;
2100 	}
2101 
2102 	gc->cq_table[cq->gdma_id] = cq->gdma_cq;
2103 
2104 	netif_napi_add_weight(ndev, &cq->napi, mana_poll, 1);
2105 
2106 	WARN_ON(xdp_rxq_info_reg(&rxq->xdp_rxq, ndev, rxq_idx,
2107 				 cq->napi.napi_id));
2108 	WARN_ON(xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq,
2109 					   MEM_TYPE_PAGE_SHARED, NULL));
2110 
2111 	napi_enable(&cq->napi);
2112 
2113 	mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT);
2114 out:
2115 	if (!err)
2116 		return rxq;
2117 
2118 	netdev_err(ndev, "Failed to create RXQ: err = %d\n", err);
2119 
2120 	mana_destroy_rxq(apc, rxq, false);
2121 
2122 	if (cq)
2123 		mana_deinit_cq(apc, cq);
2124 
2125 	return NULL;
2126 }
2127 
2128 static int mana_add_rx_queues(struct mana_port_context *apc,
2129 			      struct net_device *ndev)
2130 {
2131 	struct mana_context *ac = apc->ac;
2132 	struct mana_rxq *rxq;
2133 	int err = 0;
2134 	int i;
2135 
2136 	for (i = 0; i < apc->num_queues; i++) {
2137 		rxq = mana_create_rxq(apc, i, &ac->eqs[i], ndev);
2138 		if (!rxq) {
2139 			err = -ENOMEM;
2140 			goto out;
2141 		}
2142 
2143 		u64_stats_init(&rxq->stats.syncp);
2144 
2145 		apc->rxqs[i] = rxq;
2146 	}
2147 
2148 	apc->default_rxobj = apc->rxqs[0]->rxobj;
2149 out:
2150 	return err;
2151 }
2152 
2153 static void mana_destroy_vport(struct mana_port_context *apc)
2154 {
2155 	struct gdma_dev *gd = apc->ac->gdma_dev;
2156 	struct mana_rxq *rxq;
2157 	u32 rxq_idx;
2158 
2159 	for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) {
2160 		rxq = apc->rxqs[rxq_idx];
2161 		if (!rxq)
2162 			continue;
2163 
2164 		mana_destroy_rxq(apc, rxq, true);
2165 		apc->rxqs[rxq_idx] = NULL;
2166 	}
2167 
2168 	mana_destroy_txq(apc);
2169 	mana_uncfg_vport(apc);
2170 
2171 	if (gd->gdma_context->is_pf)
2172 		mana_pf_deregister_hw_vport(apc);
2173 }
2174 
2175 static int mana_create_vport(struct mana_port_context *apc,
2176 			     struct net_device *net)
2177 {
2178 	struct gdma_dev *gd = apc->ac->gdma_dev;
2179 	int err;
2180 
2181 	apc->default_rxobj = INVALID_MANA_HANDLE;
2182 
2183 	if (gd->gdma_context->is_pf) {
2184 		err = mana_pf_register_hw_vport(apc);
2185 		if (err)
2186 			return err;
2187 	}
2188 
2189 	err = mana_cfg_vport(apc, gd->pdid, gd->doorbell);
2190 	if (err)
2191 		return err;
2192 
2193 	return mana_create_txq(apc, net);
2194 }
2195 
2196 static void mana_rss_table_init(struct mana_port_context *apc)
2197 {
2198 	int i;
2199 
2200 	for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
2201 		apc->indir_table[i] =
2202 			ethtool_rxfh_indir_default(i, apc->num_queues);
2203 }
2204 
2205 int mana_config_rss(struct mana_port_context *apc, enum TRI_STATE rx,
2206 		    bool update_hash, bool update_tab)
2207 {
2208 	u32 queue_idx;
2209 	int err;
2210 	int i;
2211 
2212 	if (update_tab) {
2213 		for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) {
2214 			queue_idx = apc->indir_table[i];
2215 			apc->rxobj_table[i] = apc->rxqs[queue_idx]->rxobj;
2216 		}
2217 	}
2218 
2219 	err = mana_cfg_vport_steering(apc, rx, true, update_hash, update_tab);
2220 	if (err)
2221 		return err;
2222 
2223 	mana_fence_rqs(apc);
2224 
2225 	return 0;
2226 }
2227 
2228 static int mana_init_port(struct net_device *ndev)
2229 {
2230 	struct mana_port_context *apc = netdev_priv(ndev);
2231 	u32 max_txq, max_rxq, max_queues;
2232 	int port_idx = apc->port_idx;
2233 	u32 num_indirect_entries;
2234 	int err;
2235 
2236 	err = mana_init_port_context(apc);
2237 	if (err)
2238 		return err;
2239 
2240 	err = mana_query_vport_cfg(apc, port_idx, &max_txq, &max_rxq,
2241 				   &num_indirect_entries);
2242 	if (err) {
2243 		netdev_err(ndev, "Failed to query info for vPort %d\n",
2244 			   port_idx);
2245 		goto reset_apc;
2246 	}
2247 
2248 	max_queues = min_t(u32, max_txq, max_rxq);
2249 	if (apc->max_queues > max_queues)
2250 		apc->max_queues = max_queues;
2251 
2252 	if (apc->num_queues > apc->max_queues)
2253 		apc->num_queues = apc->max_queues;
2254 
2255 	eth_hw_addr_set(ndev, apc->mac_addr);
2256 
2257 	return 0;
2258 
2259 reset_apc:
2260 	kfree(apc->rxqs);
2261 	apc->rxqs = NULL;
2262 	return err;
2263 }
2264 
2265 int mana_alloc_queues(struct net_device *ndev)
2266 {
2267 	struct mana_port_context *apc = netdev_priv(ndev);
2268 	struct gdma_dev *gd = apc->ac->gdma_dev;
2269 	int err;
2270 
2271 	err = mana_create_vport(apc, ndev);
2272 	if (err)
2273 		return err;
2274 
2275 	err = netif_set_real_num_tx_queues(ndev, apc->num_queues);
2276 	if (err)
2277 		goto destroy_vport;
2278 
2279 	err = mana_add_rx_queues(apc, ndev);
2280 	if (err)
2281 		goto destroy_vport;
2282 
2283 	apc->rss_state = apc->num_queues > 1 ? TRI_STATE_TRUE : TRI_STATE_FALSE;
2284 
2285 	err = netif_set_real_num_rx_queues(ndev, apc->num_queues);
2286 	if (err)
2287 		goto destroy_vport;
2288 
2289 	mana_rss_table_init(apc);
2290 
2291 	err = mana_config_rss(apc, TRI_STATE_TRUE, true, true);
2292 	if (err)
2293 		goto destroy_vport;
2294 
2295 	if (gd->gdma_context->is_pf) {
2296 		err = mana_pf_register_filter(apc);
2297 		if (err)
2298 			goto destroy_vport;
2299 	}
2300 
2301 	mana_chn_setxdp(apc, mana_xdp_get(apc));
2302 
2303 	return 0;
2304 
2305 destroy_vport:
2306 	mana_destroy_vport(apc);
2307 	return err;
2308 }
2309 
2310 int mana_attach(struct net_device *ndev)
2311 {
2312 	struct mana_port_context *apc = netdev_priv(ndev);
2313 	int err;
2314 
2315 	ASSERT_RTNL();
2316 
2317 	err = mana_init_port(ndev);
2318 	if (err)
2319 		return err;
2320 
2321 	if (apc->port_st_save) {
2322 		err = mana_alloc_queues(ndev);
2323 		if (err) {
2324 			mana_cleanup_port_context(apc);
2325 			return err;
2326 		}
2327 	}
2328 
2329 	apc->port_is_up = apc->port_st_save;
2330 
2331 	/* Ensure port state updated before txq state */
2332 	smp_wmb();
2333 
2334 	if (apc->port_is_up)
2335 		netif_carrier_on(ndev);
2336 
2337 	netif_device_attach(ndev);
2338 
2339 	return 0;
2340 }
2341 
2342 static int mana_dealloc_queues(struct net_device *ndev)
2343 {
2344 	struct mana_port_context *apc = netdev_priv(ndev);
2345 	struct gdma_dev *gd = apc->ac->gdma_dev;
2346 	struct mana_txq *txq;
2347 	int i, err;
2348 
2349 	if (apc->port_is_up)
2350 		return -EINVAL;
2351 
2352 	mana_chn_setxdp(apc, NULL);
2353 
2354 	if (gd->gdma_context->is_pf)
2355 		mana_pf_deregister_filter(apc);
2356 
2357 	/* No packet can be transmitted now since apc->port_is_up is false.
2358 	 * There is still a tiny chance that mana_poll_tx_cq() can re-enable
2359 	 * a txq because it may not timely see apc->port_is_up being cleared
2360 	 * to false, but it doesn't matter since mana_start_xmit() drops any
2361 	 * new packets due to apc->port_is_up being false.
2362 	 *
2363 	 * Drain all the in-flight TX packets
2364 	 */
2365 	for (i = 0; i < apc->num_queues; i++) {
2366 		txq = &apc->tx_qp[i].txq;
2367 
2368 		while (atomic_read(&txq->pending_sends) > 0)
2369 			usleep_range(1000, 2000);
2370 	}
2371 
2372 	/* We're 100% sure the queues can no longer be woken up, because
2373 	 * we're sure now mana_poll_tx_cq() can't be running.
2374 	 */
2375 
2376 	apc->rss_state = TRI_STATE_FALSE;
2377 	err = mana_config_rss(apc, TRI_STATE_FALSE, false, false);
2378 	if (err) {
2379 		netdev_err(ndev, "Failed to disable vPort: %d\n", err);
2380 		return err;
2381 	}
2382 
2383 	mana_destroy_vport(apc);
2384 
2385 	return 0;
2386 }
2387 
2388 int mana_detach(struct net_device *ndev, bool from_close)
2389 {
2390 	struct mana_port_context *apc = netdev_priv(ndev);
2391 	int err;
2392 
2393 	ASSERT_RTNL();
2394 
2395 	apc->port_st_save = apc->port_is_up;
2396 	apc->port_is_up = false;
2397 
2398 	/* Ensure port state updated before txq state */
2399 	smp_wmb();
2400 
2401 	netif_tx_disable(ndev);
2402 	netif_carrier_off(ndev);
2403 
2404 	if (apc->port_st_save) {
2405 		err = mana_dealloc_queues(ndev);
2406 		if (err)
2407 			return err;
2408 	}
2409 
2410 	if (!from_close) {
2411 		netif_device_detach(ndev);
2412 		mana_cleanup_port_context(apc);
2413 	}
2414 
2415 	return 0;
2416 }
2417 
2418 static int mana_probe_port(struct mana_context *ac, int port_idx,
2419 			   struct net_device **ndev_storage)
2420 {
2421 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
2422 	struct mana_port_context *apc;
2423 	struct net_device *ndev;
2424 	int err;
2425 
2426 	ndev = alloc_etherdev_mq(sizeof(struct mana_port_context),
2427 				 gc->max_num_queues);
2428 	if (!ndev)
2429 		return -ENOMEM;
2430 
2431 	*ndev_storage = ndev;
2432 
2433 	apc = netdev_priv(ndev);
2434 	apc->ac = ac;
2435 	apc->ndev = ndev;
2436 	apc->max_queues = gc->max_num_queues;
2437 	apc->num_queues = gc->max_num_queues;
2438 	apc->port_handle = INVALID_MANA_HANDLE;
2439 	apc->pf_filter_handle = INVALID_MANA_HANDLE;
2440 	apc->port_idx = port_idx;
2441 
2442 	mutex_init(&apc->vport_mutex);
2443 	apc->vport_use_count = 0;
2444 
2445 	ndev->netdev_ops = &mana_devops;
2446 	ndev->ethtool_ops = &mana_ethtool_ops;
2447 	ndev->mtu = ETH_DATA_LEN;
2448 	ndev->max_mtu = gc->adapter_mtu - ETH_HLEN;
2449 	ndev->min_mtu = ETH_MIN_MTU;
2450 	ndev->needed_headroom = MANA_HEADROOM;
2451 	ndev->dev_port = port_idx;
2452 	SET_NETDEV_DEV(ndev, gc->dev);
2453 
2454 	netif_carrier_off(ndev);
2455 
2456 	netdev_rss_key_fill(apc->hashkey, MANA_HASH_KEY_SIZE);
2457 
2458 	err = mana_init_port(ndev);
2459 	if (err)
2460 		goto free_net;
2461 
2462 	netdev_lockdep_set_classes(ndev);
2463 
2464 	ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2465 	ndev->hw_features |= NETIF_F_RXCSUM;
2466 	ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2467 	ndev->hw_features |= NETIF_F_RXHASH;
2468 	ndev->features = ndev->hw_features | NETIF_F_HW_VLAN_CTAG_TX |
2469 			 NETIF_F_HW_VLAN_CTAG_RX;
2470 	ndev->vlan_features = ndev->features;
2471 	ndev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
2472 			     NETDEV_XDP_ACT_NDO_XMIT;
2473 
2474 	err = register_netdev(ndev);
2475 	if (err) {
2476 		netdev_err(ndev, "Unable to register netdev.\n");
2477 		goto reset_apc;
2478 	}
2479 
2480 	return 0;
2481 
2482 reset_apc:
2483 	kfree(apc->rxqs);
2484 	apc->rxqs = NULL;
2485 free_net:
2486 	*ndev_storage = NULL;
2487 	netdev_err(ndev, "Failed to probe vPort %d: %d\n", port_idx, err);
2488 	free_netdev(ndev);
2489 	return err;
2490 }
2491 
2492 static void adev_release(struct device *dev)
2493 {
2494 	struct mana_adev *madev = container_of(dev, struct mana_adev, adev.dev);
2495 
2496 	kfree(madev);
2497 }
2498 
2499 static void remove_adev(struct gdma_dev *gd)
2500 {
2501 	struct auxiliary_device *adev = gd->adev;
2502 	int id = adev->id;
2503 
2504 	auxiliary_device_delete(adev);
2505 	auxiliary_device_uninit(adev);
2506 
2507 	mana_adev_idx_free(id);
2508 	gd->adev = NULL;
2509 }
2510 
2511 static int add_adev(struct gdma_dev *gd)
2512 {
2513 	struct auxiliary_device *adev;
2514 	struct mana_adev *madev;
2515 	int ret;
2516 
2517 	madev = kzalloc(sizeof(*madev), GFP_KERNEL);
2518 	if (!madev)
2519 		return -ENOMEM;
2520 
2521 	adev = &madev->adev;
2522 	ret = mana_adev_idx_alloc();
2523 	if (ret < 0)
2524 		goto idx_fail;
2525 	adev->id = ret;
2526 
2527 	adev->name = "rdma";
2528 	adev->dev.parent = gd->gdma_context->dev;
2529 	adev->dev.release = adev_release;
2530 	madev->mdev = gd;
2531 
2532 	ret = auxiliary_device_init(adev);
2533 	if (ret)
2534 		goto init_fail;
2535 
2536 	ret = auxiliary_device_add(adev);
2537 	if (ret)
2538 		goto add_fail;
2539 
2540 	gd->adev = adev;
2541 	return 0;
2542 
2543 add_fail:
2544 	auxiliary_device_uninit(adev);
2545 
2546 init_fail:
2547 	mana_adev_idx_free(adev->id);
2548 
2549 idx_fail:
2550 	kfree(madev);
2551 
2552 	return ret;
2553 }
2554 
2555 int mana_probe(struct gdma_dev *gd, bool resuming)
2556 {
2557 	struct gdma_context *gc = gd->gdma_context;
2558 	struct mana_context *ac = gd->driver_data;
2559 	struct device *dev = gc->dev;
2560 	u16 num_ports = 0;
2561 	int err;
2562 	int i;
2563 
2564 	dev_info(dev,
2565 		 "Microsoft Azure Network Adapter protocol version: %d.%d.%d\n",
2566 		 MANA_MAJOR_VERSION, MANA_MINOR_VERSION, MANA_MICRO_VERSION);
2567 
2568 	err = mana_gd_register_device(gd);
2569 	if (err)
2570 		return err;
2571 
2572 	if (!resuming) {
2573 		ac = kzalloc(sizeof(*ac), GFP_KERNEL);
2574 		if (!ac)
2575 			return -ENOMEM;
2576 
2577 		ac->gdma_dev = gd;
2578 		gd->driver_data = ac;
2579 	}
2580 
2581 	err = mana_create_eq(ac);
2582 	if (err)
2583 		goto out;
2584 
2585 	err = mana_query_device_cfg(ac, MANA_MAJOR_VERSION, MANA_MINOR_VERSION,
2586 				    MANA_MICRO_VERSION, &num_ports);
2587 	if (err)
2588 		goto out;
2589 
2590 	if (!resuming) {
2591 		ac->num_ports = num_ports;
2592 	} else {
2593 		if (ac->num_ports != num_ports) {
2594 			dev_err(dev, "The number of vPorts changed: %d->%d\n",
2595 				ac->num_ports, num_ports);
2596 			err = -EPROTO;
2597 			goto out;
2598 		}
2599 	}
2600 
2601 	if (ac->num_ports == 0)
2602 		dev_err(dev, "Failed to detect any vPort\n");
2603 
2604 	if (ac->num_ports > MAX_PORTS_IN_MANA_DEV)
2605 		ac->num_ports = MAX_PORTS_IN_MANA_DEV;
2606 
2607 	if (!resuming) {
2608 		for (i = 0; i < ac->num_ports; i++) {
2609 			err = mana_probe_port(ac, i, &ac->ports[i]);
2610 			if (err)
2611 				break;
2612 		}
2613 	} else {
2614 		for (i = 0; i < ac->num_ports; i++) {
2615 			rtnl_lock();
2616 			err = mana_attach(ac->ports[i]);
2617 			rtnl_unlock();
2618 			if (err)
2619 				break;
2620 		}
2621 	}
2622 
2623 	err = add_adev(gd);
2624 out:
2625 	if (err)
2626 		mana_remove(gd, false);
2627 
2628 	return err;
2629 }
2630 
2631 void mana_remove(struct gdma_dev *gd, bool suspending)
2632 {
2633 	struct gdma_context *gc = gd->gdma_context;
2634 	struct mana_context *ac = gd->driver_data;
2635 	struct device *dev = gc->dev;
2636 	struct net_device *ndev;
2637 	int err;
2638 	int i;
2639 
2640 	/* adev currently doesn't support suspending, always remove it */
2641 	if (gd->adev)
2642 		remove_adev(gd);
2643 
2644 	for (i = 0; i < ac->num_ports; i++) {
2645 		ndev = ac->ports[i];
2646 		if (!ndev) {
2647 			if (i == 0)
2648 				dev_err(dev, "No net device to remove\n");
2649 			goto out;
2650 		}
2651 
2652 		/* All cleanup actions should stay after rtnl_lock(), otherwise
2653 		 * other functions may access partially cleaned up data.
2654 		 */
2655 		rtnl_lock();
2656 
2657 		err = mana_detach(ndev, false);
2658 		if (err)
2659 			netdev_err(ndev, "Failed to detach vPort %d: %d\n",
2660 				   i, err);
2661 
2662 		if (suspending) {
2663 			/* No need to unregister the ndev. */
2664 			rtnl_unlock();
2665 			continue;
2666 		}
2667 
2668 		unregister_netdevice(ndev);
2669 
2670 		rtnl_unlock();
2671 
2672 		free_netdev(ndev);
2673 	}
2674 
2675 	mana_destroy_eq(ac);
2676 out:
2677 	mana_gd_deregister_device(gd);
2678 
2679 	if (suspending)
2680 		return;
2681 
2682 	gd->driver_data = NULL;
2683 	gd->gdma_context = NULL;
2684 	kfree(ac);
2685 }
2686