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_v2 *req;
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->hdr.req.msg_version = GDMA_MESSAGE_V2;
999 
1000 	req->vport = apc->port_handle;
1001 	req->num_indir_entries = num_entries;
1002 	req->indir_tab_offset = sizeof(*req);
1003 	req->rx_enable = rx;
1004 	req->rss_enable = apc->rss_state;
1005 	req->update_default_rxobj = update_default_rxobj;
1006 	req->update_hashkey = update_key;
1007 	req->update_indir_tab = update_tab;
1008 	req->default_rxobj = apc->default_rxobj;
1009 	req->cqe_coalescing_enable = 0;
1010 
1011 	if (update_key)
1012 		memcpy(&req->hashkey, apc->hashkey, MANA_HASH_KEY_SIZE);
1013 
1014 	if (update_tab) {
1015 		req_indir_tab = (mana_handle_t *)(req + 1);
1016 		memcpy(req_indir_tab, apc->rxobj_table,
1017 		       req->num_indir_entries * sizeof(mana_handle_t));
1018 	}
1019 
1020 	err = mana_send_request(apc->ac, req, req_buf_size, &resp,
1021 				sizeof(resp));
1022 	if (err) {
1023 		netdev_err(ndev, "Failed to configure vPort RX: %d\n", err);
1024 		goto out;
1025 	}
1026 
1027 	err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_RX,
1028 				   sizeof(resp));
1029 	if (err) {
1030 		netdev_err(ndev, "vPort RX configuration failed: %d\n", err);
1031 		goto out;
1032 	}
1033 
1034 	if (resp.hdr.status) {
1035 		netdev_err(ndev, "vPort RX configuration failed: 0x%x\n",
1036 			   resp.hdr.status);
1037 		err = -EPROTO;
1038 	}
1039 
1040 	netdev_info(ndev, "Configured steering vPort %llu entries %u\n",
1041 		    apc->port_handle, num_entries);
1042 out:
1043 	kfree(req);
1044 	return err;
1045 }
1046 
1047 int mana_create_wq_obj(struct mana_port_context *apc,
1048 		       mana_handle_t vport,
1049 		       u32 wq_type, struct mana_obj_spec *wq_spec,
1050 		       struct mana_obj_spec *cq_spec,
1051 		       mana_handle_t *wq_obj)
1052 {
1053 	struct mana_create_wqobj_resp resp = {};
1054 	struct mana_create_wqobj_req req = {};
1055 	struct net_device *ndev = apc->ndev;
1056 	int err;
1057 
1058 	mana_gd_init_req_hdr(&req.hdr, MANA_CREATE_WQ_OBJ,
1059 			     sizeof(req), sizeof(resp));
1060 	req.vport = vport;
1061 	req.wq_type = wq_type;
1062 	req.wq_gdma_region = wq_spec->gdma_region;
1063 	req.cq_gdma_region = cq_spec->gdma_region;
1064 	req.wq_size = wq_spec->queue_size;
1065 	req.cq_size = cq_spec->queue_size;
1066 	req.cq_moderation_ctx_id = cq_spec->modr_ctx_id;
1067 	req.cq_parent_qid = cq_spec->attached_eq;
1068 
1069 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1070 				sizeof(resp));
1071 	if (err) {
1072 		netdev_err(ndev, "Failed to create WQ object: %d\n", err);
1073 		goto out;
1074 	}
1075 
1076 	err = mana_verify_resp_hdr(&resp.hdr, MANA_CREATE_WQ_OBJ,
1077 				   sizeof(resp));
1078 	if (err || resp.hdr.status) {
1079 		netdev_err(ndev, "Failed to create WQ object: %d, 0x%x\n", err,
1080 			   resp.hdr.status);
1081 		if (!err)
1082 			err = -EPROTO;
1083 		goto out;
1084 	}
1085 
1086 	if (resp.wq_obj == INVALID_MANA_HANDLE) {
1087 		netdev_err(ndev, "Got an invalid WQ object handle\n");
1088 		err = -EPROTO;
1089 		goto out;
1090 	}
1091 
1092 	*wq_obj = resp.wq_obj;
1093 	wq_spec->queue_index = resp.wq_id;
1094 	cq_spec->queue_index = resp.cq_id;
1095 
1096 	return 0;
1097 out:
1098 	return err;
1099 }
1100 EXPORT_SYMBOL_NS(mana_create_wq_obj, NET_MANA);
1101 
1102 void mana_destroy_wq_obj(struct mana_port_context *apc, u32 wq_type,
1103 			 mana_handle_t wq_obj)
1104 {
1105 	struct mana_destroy_wqobj_resp resp = {};
1106 	struct mana_destroy_wqobj_req req = {};
1107 	struct net_device *ndev = apc->ndev;
1108 	int err;
1109 
1110 	mana_gd_init_req_hdr(&req.hdr, MANA_DESTROY_WQ_OBJ,
1111 			     sizeof(req), sizeof(resp));
1112 	req.wq_type = wq_type;
1113 	req.wq_obj_handle = wq_obj;
1114 
1115 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1116 				sizeof(resp));
1117 	if (err) {
1118 		netdev_err(ndev, "Failed to destroy WQ object: %d\n", err);
1119 		return;
1120 	}
1121 
1122 	err = mana_verify_resp_hdr(&resp.hdr, MANA_DESTROY_WQ_OBJ,
1123 				   sizeof(resp));
1124 	if (err || resp.hdr.status)
1125 		netdev_err(ndev, "Failed to destroy WQ object: %d, 0x%x\n", err,
1126 			   resp.hdr.status);
1127 }
1128 EXPORT_SYMBOL_NS(mana_destroy_wq_obj, NET_MANA);
1129 
1130 static void mana_destroy_eq(struct mana_context *ac)
1131 {
1132 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
1133 	struct gdma_queue *eq;
1134 	int i;
1135 
1136 	if (!ac->eqs)
1137 		return;
1138 
1139 	for (i = 0; i < gc->max_num_queues; i++) {
1140 		eq = ac->eqs[i].eq;
1141 		if (!eq)
1142 			continue;
1143 
1144 		mana_gd_destroy_queue(gc, eq);
1145 	}
1146 
1147 	kfree(ac->eqs);
1148 	ac->eqs = NULL;
1149 }
1150 
1151 static int mana_create_eq(struct mana_context *ac)
1152 {
1153 	struct gdma_dev *gd = ac->gdma_dev;
1154 	struct gdma_context *gc = gd->gdma_context;
1155 	struct gdma_queue_spec spec = {};
1156 	int err;
1157 	int i;
1158 
1159 	ac->eqs = kcalloc(gc->max_num_queues, sizeof(struct mana_eq),
1160 			  GFP_KERNEL);
1161 	if (!ac->eqs)
1162 		return -ENOMEM;
1163 
1164 	spec.type = GDMA_EQ;
1165 	spec.monitor_avl_buf = false;
1166 	spec.queue_size = EQ_SIZE;
1167 	spec.eq.callback = NULL;
1168 	spec.eq.context = ac->eqs;
1169 	spec.eq.log2_throttle_limit = LOG2_EQ_THROTTLE;
1170 
1171 	for (i = 0; i < gc->max_num_queues; i++) {
1172 		err = mana_gd_create_mana_eq(gd, &spec, &ac->eqs[i].eq);
1173 		if (err)
1174 			goto out;
1175 	}
1176 
1177 	return 0;
1178 out:
1179 	mana_destroy_eq(ac);
1180 	return err;
1181 }
1182 
1183 static int mana_fence_rq(struct mana_port_context *apc, struct mana_rxq *rxq)
1184 {
1185 	struct mana_fence_rq_resp resp = {};
1186 	struct mana_fence_rq_req req = {};
1187 	int err;
1188 
1189 	init_completion(&rxq->fence_event);
1190 
1191 	mana_gd_init_req_hdr(&req.hdr, MANA_FENCE_RQ,
1192 			     sizeof(req), sizeof(resp));
1193 	req.wq_obj_handle =  rxq->rxobj;
1194 
1195 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1196 				sizeof(resp));
1197 	if (err) {
1198 		netdev_err(apc->ndev, "Failed to fence RQ %u: %d\n",
1199 			   rxq->rxq_idx, err);
1200 		return err;
1201 	}
1202 
1203 	err = mana_verify_resp_hdr(&resp.hdr, MANA_FENCE_RQ, sizeof(resp));
1204 	if (err || resp.hdr.status) {
1205 		netdev_err(apc->ndev, "Failed to fence RQ %u: %d, 0x%x\n",
1206 			   rxq->rxq_idx, err, resp.hdr.status);
1207 		if (!err)
1208 			err = -EPROTO;
1209 
1210 		return err;
1211 	}
1212 
1213 	if (wait_for_completion_timeout(&rxq->fence_event, 10 * HZ) == 0) {
1214 		netdev_err(apc->ndev, "Failed to fence RQ %u: timed out\n",
1215 			   rxq->rxq_idx);
1216 		return -ETIMEDOUT;
1217 	}
1218 
1219 	return 0;
1220 }
1221 
1222 static void mana_fence_rqs(struct mana_port_context *apc)
1223 {
1224 	unsigned int rxq_idx;
1225 	struct mana_rxq *rxq;
1226 	int err;
1227 
1228 	for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) {
1229 		rxq = apc->rxqs[rxq_idx];
1230 		err = mana_fence_rq(apc, rxq);
1231 
1232 		/* In case of any error, use sleep instead. */
1233 		if (err)
1234 			msleep(100);
1235 	}
1236 }
1237 
1238 static int mana_move_wq_tail(struct gdma_queue *wq, u32 num_units)
1239 {
1240 	u32 used_space_old;
1241 	u32 used_space_new;
1242 
1243 	used_space_old = wq->head - wq->tail;
1244 	used_space_new = wq->head - (wq->tail + num_units);
1245 
1246 	if (WARN_ON_ONCE(used_space_new > used_space_old))
1247 		return -ERANGE;
1248 
1249 	wq->tail += num_units;
1250 	return 0;
1251 }
1252 
1253 static void mana_unmap_skb(struct sk_buff *skb, struct mana_port_context *apc)
1254 {
1255 	struct mana_skb_head *ash = (struct mana_skb_head *)skb->head;
1256 	struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1257 	struct device *dev = gc->dev;
1258 	int i;
1259 
1260 	dma_unmap_single(dev, ash->dma_handle[0], ash->size[0], DMA_TO_DEVICE);
1261 
1262 	for (i = 1; i < skb_shinfo(skb)->nr_frags + 1; i++)
1263 		dma_unmap_page(dev, ash->dma_handle[i], ash->size[i],
1264 			       DMA_TO_DEVICE);
1265 }
1266 
1267 static void mana_poll_tx_cq(struct mana_cq *cq)
1268 {
1269 	struct gdma_comp *completions = cq->gdma_comp_buf;
1270 	struct gdma_posted_wqe_info *wqe_info;
1271 	unsigned int pkt_transmitted = 0;
1272 	unsigned int wqe_unit_cnt = 0;
1273 	struct mana_txq *txq = cq->txq;
1274 	struct mana_port_context *apc;
1275 	struct netdev_queue *net_txq;
1276 	struct gdma_queue *gdma_wq;
1277 	unsigned int avail_space;
1278 	struct net_device *ndev;
1279 	struct sk_buff *skb;
1280 	bool txq_stopped;
1281 	int comp_read;
1282 	int i;
1283 
1284 	ndev = txq->ndev;
1285 	apc = netdev_priv(ndev);
1286 
1287 	comp_read = mana_gd_poll_cq(cq->gdma_cq, completions,
1288 				    CQE_POLLING_BUFFER);
1289 
1290 	if (comp_read < 1)
1291 		return;
1292 
1293 	for (i = 0; i < comp_read; i++) {
1294 		struct mana_tx_comp_oob *cqe_oob;
1295 
1296 		if (WARN_ON_ONCE(!completions[i].is_sq))
1297 			return;
1298 
1299 		cqe_oob = (struct mana_tx_comp_oob *)completions[i].cqe_data;
1300 		if (WARN_ON_ONCE(cqe_oob->cqe_hdr.client_type !=
1301 				 MANA_CQE_COMPLETION))
1302 			return;
1303 
1304 		switch (cqe_oob->cqe_hdr.cqe_type) {
1305 		case CQE_TX_OKAY:
1306 			break;
1307 
1308 		case CQE_TX_SA_DROP:
1309 		case CQE_TX_MTU_DROP:
1310 		case CQE_TX_INVALID_OOB:
1311 		case CQE_TX_INVALID_ETH_TYPE:
1312 		case CQE_TX_HDR_PROCESSING_ERROR:
1313 		case CQE_TX_VF_DISABLED:
1314 		case CQE_TX_VPORT_IDX_OUT_OF_RANGE:
1315 		case CQE_TX_VPORT_DISABLED:
1316 		case CQE_TX_VLAN_TAGGING_VIOLATION:
1317 			WARN_ONCE(1, "TX: CQE error %d: ignored.\n",
1318 				  cqe_oob->cqe_hdr.cqe_type);
1319 			apc->eth_stats.tx_cqe_err++;
1320 			break;
1321 
1322 		default:
1323 			/* If the CQE type is unexpected, log an error, assert,
1324 			 * and go through the error path.
1325 			 */
1326 			WARN_ONCE(1, "TX: Unexpected CQE type %d: HW BUG?\n",
1327 				  cqe_oob->cqe_hdr.cqe_type);
1328 			apc->eth_stats.tx_cqe_unknown_type++;
1329 			return;
1330 		}
1331 
1332 		if (WARN_ON_ONCE(txq->gdma_txq_id != completions[i].wq_num))
1333 			return;
1334 
1335 		skb = skb_dequeue(&txq->pending_skbs);
1336 		if (WARN_ON_ONCE(!skb))
1337 			return;
1338 
1339 		wqe_info = (struct gdma_posted_wqe_info *)skb->cb;
1340 		wqe_unit_cnt += wqe_info->wqe_size_in_bu;
1341 
1342 		mana_unmap_skb(skb, apc);
1343 
1344 		napi_consume_skb(skb, cq->budget);
1345 
1346 		pkt_transmitted++;
1347 	}
1348 
1349 	if (WARN_ON_ONCE(wqe_unit_cnt == 0))
1350 		return;
1351 
1352 	mana_move_wq_tail(txq->gdma_sq, wqe_unit_cnt);
1353 
1354 	gdma_wq = txq->gdma_sq;
1355 	avail_space = mana_gd_wq_avail_space(gdma_wq);
1356 
1357 	/* Ensure tail updated before checking q stop */
1358 	smp_mb();
1359 
1360 	net_txq = txq->net_txq;
1361 	txq_stopped = netif_tx_queue_stopped(net_txq);
1362 
1363 	/* Ensure checking txq_stopped before apc->port_is_up. */
1364 	smp_rmb();
1365 
1366 	if (txq_stopped && apc->port_is_up && avail_space >= MAX_TX_WQE_SIZE) {
1367 		netif_tx_wake_queue(net_txq);
1368 		apc->eth_stats.wake_queue++;
1369 	}
1370 
1371 	if (atomic_sub_return(pkt_transmitted, &txq->pending_sends) < 0)
1372 		WARN_ON_ONCE(1);
1373 
1374 	cq->work_done = pkt_transmitted;
1375 }
1376 
1377 static void mana_post_pkt_rxq(struct mana_rxq *rxq)
1378 {
1379 	struct mana_recv_buf_oob *recv_buf_oob;
1380 	u32 curr_index;
1381 	int err;
1382 
1383 	curr_index = rxq->buf_index++;
1384 	if (rxq->buf_index == rxq->num_rx_buf)
1385 		rxq->buf_index = 0;
1386 
1387 	recv_buf_oob = &rxq->rx_oobs[curr_index];
1388 
1389 	err = mana_gd_post_and_ring(rxq->gdma_rq, &recv_buf_oob->wqe_req,
1390 				    &recv_buf_oob->wqe_inf);
1391 	if (WARN_ON_ONCE(err))
1392 		return;
1393 
1394 	WARN_ON_ONCE(recv_buf_oob->wqe_inf.wqe_size_in_bu != 1);
1395 }
1396 
1397 static struct sk_buff *mana_build_skb(struct mana_rxq *rxq, void *buf_va,
1398 				      uint pkt_len, struct xdp_buff *xdp)
1399 {
1400 	struct sk_buff *skb = napi_build_skb(buf_va, rxq->alloc_size);
1401 
1402 	if (!skb)
1403 		return NULL;
1404 
1405 	if (xdp->data_hard_start) {
1406 		skb_reserve(skb, xdp->data - xdp->data_hard_start);
1407 		skb_put(skb, xdp->data_end - xdp->data);
1408 		return skb;
1409 	}
1410 
1411 	skb_reserve(skb, rxq->headroom);
1412 	skb_put(skb, pkt_len);
1413 
1414 	return skb;
1415 }
1416 
1417 static void mana_rx_skb(void *buf_va, struct mana_rxcomp_oob *cqe,
1418 			struct mana_rxq *rxq)
1419 {
1420 	struct mana_stats_rx *rx_stats = &rxq->stats;
1421 	struct net_device *ndev = rxq->ndev;
1422 	uint pkt_len = cqe->ppi[0].pkt_len;
1423 	u16 rxq_idx = rxq->rxq_idx;
1424 	struct napi_struct *napi;
1425 	struct xdp_buff xdp = {};
1426 	struct sk_buff *skb;
1427 	u32 hash_value;
1428 	u32 act;
1429 
1430 	rxq->rx_cq.work_done++;
1431 	napi = &rxq->rx_cq.napi;
1432 
1433 	if (!buf_va) {
1434 		++ndev->stats.rx_dropped;
1435 		return;
1436 	}
1437 
1438 	act = mana_run_xdp(ndev, rxq, &xdp, buf_va, pkt_len);
1439 
1440 	if (act == XDP_REDIRECT && !rxq->xdp_rc)
1441 		return;
1442 
1443 	if (act != XDP_PASS && act != XDP_TX)
1444 		goto drop_xdp;
1445 
1446 	skb = mana_build_skb(rxq, buf_va, pkt_len, &xdp);
1447 
1448 	if (!skb)
1449 		goto drop;
1450 
1451 	skb->dev = napi->dev;
1452 
1453 	skb->protocol = eth_type_trans(skb, ndev);
1454 	skb_checksum_none_assert(skb);
1455 	skb_record_rx_queue(skb, rxq_idx);
1456 
1457 	if ((ndev->features & NETIF_F_RXCSUM) && cqe->rx_iphdr_csum_succeed) {
1458 		if (cqe->rx_tcp_csum_succeed || cqe->rx_udp_csum_succeed)
1459 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1460 	}
1461 
1462 	if (cqe->rx_hashtype != 0 && (ndev->features & NETIF_F_RXHASH)) {
1463 		hash_value = cqe->ppi[0].pkt_hash;
1464 
1465 		if (cqe->rx_hashtype & MANA_HASH_L4)
1466 			skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L4);
1467 		else
1468 			skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L3);
1469 	}
1470 
1471 	if (cqe->rx_vlantag_present) {
1472 		u16 vlan_tci = cqe->rx_vlan_id;
1473 
1474 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
1475 	}
1476 
1477 	u64_stats_update_begin(&rx_stats->syncp);
1478 	rx_stats->packets++;
1479 	rx_stats->bytes += pkt_len;
1480 
1481 	if (act == XDP_TX)
1482 		rx_stats->xdp_tx++;
1483 	u64_stats_update_end(&rx_stats->syncp);
1484 
1485 	if (act == XDP_TX) {
1486 		skb_set_queue_mapping(skb, rxq_idx);
1487 		mana_xdp_tx(skb, ndev);
1488 		return;
1489 	}
1490 
1491 	napi_gro_receive(napi, skb);
1492 
1493 	return;
1494 
1495 drop_xdp:
1496 	u64_stats_update_begin(&rx_stats->syncp);
1497 	rx_stats->xdp_drop++;
1498 	u64_stats_update_end(&rx_stats->syncp);
1499 
1500 drop:
1501 	WARN_ON_ONCE(rxq->xdp_save_va);
1502 	/* Save for reuse */
1503 	rxq->xdp_save_va = buf_va;
1504 
1505 	++ndev->stats.rx_dropped;
1506 
1507 	return;
1508 }
1509 
1510 static void *mana_get_rxfrag(struct mana_rxq *rxq, struct device *dev,
1511 			     dma_addr_t *da, bool is_napi)
1512 {
1513 	struct page *page;
1514 	void *va;
1515 
1516 	/* Reuse XDP dropped page if available */
1517 	if (rxq->xdp_save_va) {
1518 		va = rxq->xdp_save_va;
1519 		rxq->xdp_save_va = NULL;
1520 	} else if (rxq->alloc_size > PAGE_SIZE) {
1521 		if (is_napi)
1522 			va = napi_alloc_frag(rxq->alloc_size);
1523 		else
1524 			va = netdev_alloc_frag(rxq->alloc_size);
1525 
1526 		if (!va)
1527 			return NULL;
1528 
1529 		page = virt_to_head_page(va);
1530 		/* Check if the frag falls back to single page */
1531 		if (compound_order(page) < get_order(rxq->alloc_size)) {
1532 			put_page(page);
1533 			return NULL;
1534 		}
1535 	} else {
1536 		page = dev_alloc_page();
1537 		if (!page)
1538 			return NULL;
1539 
1540 		va = page_to_virt(page);
1541 	}
1542 
1543 	*da = dma_map_single(dev, va + rxq->headroom, rxq->datasize,
1544 			     DMA_FROM_DEVICE);
1545 	if (dma_mapping_error(dev, *da)) {
1546 		put_page(virt_to_head_page(va));
1547 		return NULL;
1548 	}
1549 
1550 	return va;
1551 }
1552 
1553 /* Allocate frag for rx buffer, and save the old buf */
1554 static void mana_refill_rx_oob(struct device *dev, struct mana_rxq *rxq,
1555 			       struct mana_recv_buf_oob *rxoob, void **old_buf)
1556 {
1557 	dma_addr_t da;
1558 	void *va;
1559 
1560 	va = mana_get_rxfrag(rxq, dev, &da, true);
1561 	if (!va)
1562 		return;
1563 
1564 	dma_unmap_single(dev, rxoob->sgl[0].address, rxq->datasize,
1565 			 DMA_FROM_DEVICE);
1566 	*old_buf = rxoob->buf_va;
1567 
1568 	rxoob->buf_va = va;
1569 	rxoob->sgl[0].address = da;
1570 }
1571 
1572 static void mana_process_rx_cqe(struct mana_rxq *rxq, struct mana_cq *cq,
1573 				struct gdma_comp *cqe)
1574 {
1575 	struct mana_rxcomp_oob *oob = (struct mana_rxcomp_oob *)cqe->cqe_data;
1576 	struct gdma_context *gc = rxq->gdma_rq->gdma_dev->gdma_context;
1577 	struct net_device *ndev = rxq->ndev;
1578 	struct mana_recv_buf_oob *rxbuf_oob;
1579 	struct mana_port_context *apc;
1580 	struct device *dev = gc->dev;
1581 	void *old_buf = NULL;
1582 	u32 curr, pktlen;
1583 
1584 	apc = netdev_priv(ndev);
1585 
1586 	switch (oob->cqe_hdr.cqe_type) {
1587 	case CQE_RX_OKAY:
1588 		break;
1589 
1590 	case CQE_RX_TRUNCATED:
1591 		++ndev->stats.rx_dropped;
1592 		rxbuf_oob = &rxq->rx_oobs[rxq->buf_index];
1593 		netdev_warn_once(ndev, "Dropped a truncated packet\n");
1594 		goto drop;
1595 
1596 	case CQE_RX_COALESCED_4:
1597 		netdev_err(ndev, "RX coalescing is unsupported\n");
1598 		apc->eth_stats.rx_coalesced_err++;
1599 		return;
1600 
1601 	case CQE_RX_OBJECT_FENCE:
1602 		complete(&rxq->fence_event);
1603 		return;
1604 
1605 	default:
1606 		netdev_err(ndev, "Unknown RX CQE type = %d\n",
1607 			   oob->cqe_hdr.cqe_type);
1608 		apc->eth_stats.rx_cqe_unknown_type++;
1609 		return;
1610 	}
1611 
1612 	pktlen = oob->ppi[0].pkt_len;
1613 
1614 	if (pktlen == 0) {
1615 		/* data packets should never have packetlength of zero */
1616 		netdev_err(ndev, "RX pkt len=0, rq=%u, cq=%u, rxobj=0x%llx\n",
1617 			   rxq->gdma_id, cq->gdma_id, rxq->rxobj);
1618 		return;
1619 	}
1620 
1621 	curr = rxq->buf_index;
1622 	rxbuf_oob = &rxq->rx_oobs[curr];
1623 	WARN_ON_ONCE(rxbuf_oob->wqe_inf.wqe_size_in_bu != 1);
1624 
1625 	mana_refill_rx_oob(dev, rxq, rxbuf_oob, &old_buf);
1626 
1627 	/* Unsuccessful refill will have old_buf == NULL.
1628 	 * In this case, mana_rx_skb() will drop the packet.
1629 	 */
1630 	mana_rx_skb(old_buf, oob, rxq);
1631 
1632 drop:
1633 	mana_move_wq_tail(rxq->gdma_rq, rxbuf_oob->wqe_inf.wqe_size_in_bu);
1634 
1635 	mana_post_pkt_rxq(rxq);
1636 }
1637 
1638 static void mana_poll_rx_cq(struct mana_cq *cq)
1639 {
1640 	struct gdma_comp *comp = cq->gdma_comp_buf;
1641 	struct mana_rxq *rxq = cq->rxq;
1642 	int comp_read, i;
1643 
1644 	comp_read = mana_gd_poll_cq(cq->gdma_cq, comp, CQE_POLLING_BUFFER);
1645 	WARN_ON_ONCE(comp_read > CQE_POLLING_BUFFER);
1646 
1647 	rxq->xdp_flush = false;
1648 
1649 	for (i = 0; i < comp_read; i++) {
1650 		if (WARN_ON_ONCE(comp[i].is_sq))
1651 			return;
1652 
1653 		/* verify recv cqe references the right rxq */
1654 		if (WARN_ON_ONCE(comp[i].wq_num != cq->rxq->gdma_id))
1655 			return;
1656 
1657 		mana_process_rx_cqe(rxq, cq, &comp[i]);
1658 	}
1659 
1660 	if (rxq->xdp_flush)
1661 		xdp_do_flush();
1662 }
1663 
1664 static int mana_cq_handler(void *context, struct gdma_queue *gdma_queue)
1665 {
1666 	struct mana_cq *cq = context;
1667 	u8 arm_bit;
1668 	int w;
1669 
1670 	WARN_ON_ONCE(cq->gdma_cq != gdma_queue);
1671 
1672 	if (cq->type == MANA_CQ_TYPE_RX)
1673 		mana_poll_rx_cq(cq);
1674 	else
1675 		mana_poll_tx_cq(cq);
1676 
1677 	w = cq->work_done;
1678 
1679 	if (w < cq->budget &&
1680 	    napi_complete_done(&cq->napi, w)) {
1681 		arm_bit = SET_ARM_BIT;
1682 	} else {
1683 		arm_bit = 0;
1684 	}
1685 
1686 	mana_gd_ring_cq(gdma_queue, arm_bit);
1687 
1688 	return w;
1689 }
1690 
1691 static int mana_poll(struct napi_struct *napi, int budget)
1692 {
1693 	struct mana_cq *cq = container_of(napi, struct mana_cq, napi);
1694 	int w;
1695 
1696 	cq->work_done = 0;
1697 	cq->budget = budget;
1698 
1699 	w = mana_cq_handler(cq, cq->gdma_cq);
1700 
1701 	return min(w, budget);
1702 }
1703 
1704 static void mana_schedule_napi(void *context, struct gdma_queue *gdma_queue)
1705 {
1706 	struct mana_cq *cq = context;
1707 
1708 	napi_schedule_irqoff(&cq->napi);
1709 }
1710 
1711 static void mana_deinit_cq(struct mana_port_context *apc, struct mana_cq *cq)
1712 {
1713 	struct gdma_dev *gd = apc->ac->gdma_dev;
1714 
1715 	if (!cq->gdma_cq)
1716 		return;
1717 
1718 	mana_gd_destroy_queue(gd->gdma_context, cq->gdma_cq);
1719 }
1720 
1721 static void mana_deinit_txq(struct mana_port_context *apc, struct mana_txq *txq)
1722 {
1723 	struct gdma_dev *gd = apc->ac->gdma_dev;
1724 
1725 	if (!txq->gdma_sq)
1726 		return;
1727 
1728 	mana_gd_destroy_queue(gd->gdma_context, txq->gdma_sq);
1729 }
1730 
1731 static void mana_destroy_txq(struct mana_port_context *apc)
1732 {
1733 	struct napi_struct *napi;
1734 	int i;
1735 
1736 	if (!apc->tx_qp)
1737 		return;
1738 
1739 	for (i = 0; i < apc->num_queues; i++) {
1740 		napi = &apc->tx_qp[i].tx_cq.napi;
1741 		napi_synchronize(napi);
1742 		napi_disable(napi);
1743 		netif_napi_del(napi);
1744 
1745 		mana_destroy_wq_obj(apc, GDMA_SQ, apc->tx_qp[i].tx_object);
1746 
1747 		mana_deinit_cq(apc, &apc->tx_qp[i].tx_cq);
1748 
1749 		mana_deinit_txq(apc, &apc->tx_qp[i].txq);
1750 	}
1751 
1752 	kfree(apc->tx_qp);
1753 	apc->tx_qp = NULL;
1754 }
1755 
1756 static int mana_create_txq(struct mana_port_context *apc,
1757 			   struct net_device *net)
1758 {
1759 	struct mana_context *ac = apc->ac;
1760 	struct gdma_dev *gd = ac->gdma_dev;
1761 	struct mana_obj_spec wq_spec;
1762 	struct mana_obj_spec cq_spec;
1763 	struct gdma_queue_spec spec;
1764 	struct gdma_context *gc;
1765 	struct mana_txq *txq;
1766 	struct mana_cq *cq;
1767 	u32 txq_size;
1768 	u32 cq_size;
1769 	int err;
1770 	int i;
1771 
1772 	apc->tx_qp = kcalloc(apc->num_queues, sizeof(struct mana_tx_qp),
1773 			     GFP_KERNEL);
1774 	if (!apc->tx_qp)
1775 		return -ENOMEM;
1776 
1777 	/*  The minimum size of the WQE is 32 bytes, hence
1778 	 *  MAX_SEND_BUFFERS_PER_QUEUE represents the maximum number of WQEs
1779 	 *  the SQ can store. This value is then used to size other queues
1780 	 *  to prevent overflow.
1781 	 */
1782 	txq_size = MAX_SEND_BUFFERS_PER_QUEUE * 32;
1783 	BUILD_BUG_ON(!PAGE_ALIGNED(txq_size));
1784 
1785 	cq_size = MAX_SEND_BUFFERS_PER_QUEUE * COMP_ENTRY_SIZE;
1786 	cq_size = PAGE_ALIGN(cq_size);
1787 
1788 	gc = gd->gdma_context;
1789 
1790 	for (i = 0; i < apc->num_queues; i++) {
1791 		apc->tx_qp[i].tx_object = INVALID_MANA_HANDLE;
1792 
1793 		/* Create SQ */
1794 		txq = &apc->tx_qp[i].txq;
1795 
1796 		u64_stats_init(&txq->stats.syncp);
1797 		txq->ndev = net;
1798 		txq->net_txq = netdev_get_tx_queue(net, i);
1799 		txq->vp_offset = apc->tx_vp_offset;
1800 		skb_queue_head_init(&txq->pending_skbs);
1801 
1802 		memset(&spec, 0, sizeof(spec));
1803 		spec.type = GDMA_SQ;
1804 		spec.monitor_avl_buf = true;
1805 		spec.queue_size = txq_size;
1806 		err = mana_gd_create_mana_wq_cq(gd, &spec, &txq->gdma_sq);
1807 		if (err)
1808 			goto out;
1809 
1810 		/* Create SQ's CQ */
1811 		cq = &apc->tx_qp[i].tx_cq;
1812 		cq->type = MANA_CQ_TYPE_TX;
1813 
1814 		cq->txq = txq;
1815 
1816 		memset(&spec, 0, sizeof(spec));
1817 		spec.type = GDMA_CQ;
1818 		spec.monitor_avl_buf = false;
1819 		spec.queue_size = cq_size;
1820 		spec.cq.callback = mana_schedule_napi;
1821 		spec.cq.parent_eq = ac->eqs[i].eq;
1822 		spec.cq.context = cq;
1823 		err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq);
1824 		if (err)
1825 			goto out;
1826 
1827 		memset(&wq_spec, 0, sizeof(wq_spec));
1828 		memset(&cq_spec, 0, sizeof(cq_spec));
1829 
1830 		wq_spec.gdma_region = txq->gdma_sq->mem_info.dma_region_handle;
1831 		wq_spec.queue_size = txq->gdma_sq->queue_size;
1832 
1833 		cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle;
1834 		cq_spec.queue_size = cq->gdma_cq->queue_size;
1835 		cq_spec.modr_ctx_id = 0;
1836 		cq_spec.attached_eq = cq->gdma_cq->cq.parent->id;
1837 
1838 		err = mana_create_wq_obj(apc, apc->port_handle, GDMA_SQ,
1839 					 &wq_spec, &cq_spec,
1840 					 &apc->tx_qp[i].tx_object);
1841 
1842 		if (err)
1843 			goto out;
1844 
1845 		txq->gdma_sq->id = wq_spec.queue_index;
1846 		cq->gdma_cq->id = cq_spec.queue_index;
1847 
1848 		txq->gdma_sq->mem_info.dma_region_handle =
1849 			GDMA_INVALID_DMA_REGION;
1850 		cq->gdma_cq->mem_info.dma_region_handle =
1851 			GDMA_INVALID_DMA_REGION;
1852 
1853 		txq->gdma_txq_id = txq->gdma_sq->id;
1854 
1855 		cq->gdma_id = cq->gdma_cq->id;
1856 
1857 		if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) {
1858 			err = -EINVAL;
1859 			goto out;
1860 		}
1861 
1862 		gc->cq_table[cq->gdma_id] = cq->gdma_cq;
1863 
1864 		netif_napi_add_tx(net, &cq->napi, mana_poll);
1865 		napi_enable(&cq->napi);
1866 
1867 		mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT);
1868 	}
1869 
1870 	return 0;
1871 out:
1872 	mana_destroy_txq(apc);
1873 	return err;
1874 }
1875 
1876 static void mana_destroy_rxq(struct mana_port_context *apc,
1877 			     struct mana_rxq *rxq, bool validate_state)
1878 
1879 {
1880 	struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1881 	struct mana_recv_buf_oob *rx_oob;
1882 	struct device *dev = gc->dev;
1883 	struct napi_struct *napi;
1884 	int i;
1885 
1886 	if (!rxq)
1887 		return;
1888 
1889 	napi = &rxq->rx_cq.napi;
1890 
1891 	if (validate_state)
1892 		napi_synchronize(napi);
1893 
1894 	napi_disable(napi);
1895 
1896 	xdp_rxq_info_unreg(&rxq->xdp_rxq);
1897 
1898 	netif_napi_del(napi);
1899 
1900 	mana_destroy_wq_obj(apc, GDMA_RQ, rxq->rxobj);
1901 
1902 	mana_deinit_cq(apc, &rxq->rx_cq);
1903 
1904 	if (rxq->xdp_save_va)
1905 		put_page(virt_to_head_page(rxq->xdp_save_va));
1906 
1907 	for (i = 0; i < rxq->num_rx_buf; i++) {
1908 		rx_oob = &rxq->rx_oobs[i];
1909 
1910 		if (!rx_oob->buf_va)
1911 			continue;
1912 
1913 		dma_unmap_single(dev, rx_oob->sgl[0].address,
1914 				 rx_oob->sgl[0].size, DMA_FROM_DEVICE);
1915 
1916 		put_page(virt_to_head_page(rx_oob->buf_va));
1917 		rx_oob->buf_va = NULL;
1918 	}
1919 
1920 	if (rxq->gdma_rq)
1921 		mana_gd_destroy_queue(gc, rxq->gdma_rq);
1922 
1923 	kfree(rxq);
1924 }
1925 
1926 static int mana_fill_rx_oob(struct mana_recv_buf_oob *rx_oob, u32 mem_key,
1927 			    struct mana_rxq *rxq, struct device *dev)
1928 {
1929 	struct mana_port_context *mpc = netdev_priv(rxq->ndev);
1930 	dma_addr_t da;
1931 	void *va;
1932 
1933 	if (mpc->rxbufs_pre)
1934 		va = mana_get_rxbuf_pre(rxq, &da);
1935 	else
1936 		va = mana_get_rxfrag(rxq, dev, &da, false);
1937 
1938 	if (!va)
1939 		return -ENOMEM;
1940 
1941 	rx_oob->buf_va = va;
1942 
1943 	rx_oob->sgl[0].address = da;
1944 	rx_oob->sgl[0].size = rxq->datasize;
1945 	rx_oob->sgl[0].mem_key = mem_key;
1946 
1947 	return 0;
1948 }
1949 
1950 #define MANA_WQE_HEADER_SIZE 16
1951 #define MANA_WQE_SGE_SIZE 16
1952 
1953 static int mana_alloc_rx_wqe(struct mana_port_context *apc,
1954 			     struct mana_rxq *rxq, u32 *rxq_size, u32 *cq_size)
1955 {
1956 	struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1957 	struct mana_recv_buf_oob *rx_oob;
1958 	struct device *dev = gc->dev;
1959 	u32 buf_idx;
1960 	int ret;
1961 
1962 	WARN_ON(rxq->datasize == 0);
1963 
1964 	*rxq_size = 0;
1965 	*cq_size = 0;
1966 
1967 	for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) {
1968 		rx_oob = &rxq->rx_oobs[buf_idx];
1969 		memset(rx_oob, 0, sizeof(*rx_oob));
1970 
1971 		rx_oob->num_sge = 1;
1972 
1973 		ret = mana_fill_rx_oob(rx_oob, apc->ac->gdma_dev->gpa_mkey, rxq,
1974 				       dev);
1975 		if (ret)
1976 			return ret;
1977 
1978 		rx_oob->wqe_req.sgl = rx_oob->sgl;
1979 		rx_oob->wqe_req.num_sge = rx_oob->num_sge;
1980 		rx_oob->wqe_req.inline_oob_size = 0;
1981 		rx_oob->wqe_req.inline_oob_data = NULL;
1982 		rx_oob->wqe_req.flags = 0;
1983 		rx_oob->wqe_req.client_data_unit = 0;
1984 
1985 		*rxq_size += ALIGN(MANA_WQE_HEADER_SIZE +
1986 				   MANA_WQE_SGE_SIZE * rx_oob->num_sge, 32);
1987 		*cq_size += COMP_ENTRY_SIZE;
1988 	}
1989 
1990 	return 0;
1991 }
1992 
1993 static int mana_push_wqe(struct mana_rxq *rxq)
1994 {
1995 	struct mana_recv_buf_oob *rx_oob;
1996 	u32 buf_idx;
1997 	int err;
1998 
1999 	for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) {
2000 		rx_oob = &rxq->rx_oobs[buf_idx];
2001 
2002 		err = mana_gd_post_and_ring(rxq->gdma_rq, &rx_oob->wqe_req,
2003 					    &rx_oob->wqe_inf);
2004 		if (err)
2005 			return -ENOSPC;
2006 	}
2007 
2008 	return 0;
2009 }
2010 
2011 static struct mana_rxq *mana_create_rxq(struct mana_port_context *apc,
2012 					u32 rxq_idx, struct mana_eq *eq,
2013 					struct net_device *ndev)
2014 {
2015 	struct gdma_dev *gd = apc->ac->gdma_dev;
2016 	struct mana_obj_spec wq_spec;
2017 	struct mana_obj_spec cq_spec;
2018 	struct gdma_queue_spec spec;
2019 	struct mana_cq *cq = NULL;
2020 	struct gdma_context *gc;
2021 	u32 cq_size, rq_size;
2022 	struct mana_rxq *rxq;
2023 	int err;
2024 
2025 	gc = gd->gdma_context;
2026 
2027 	rxq = kzalloc(struct_size(rxq, rx_oobs, RX_BUFFERS_PER_QUEUE),
2028 		      GFP_KERNEL);
2029 	if (!rxq)
2030 		return NULL;
2031 
2032 	rxq->ndev = ndev;
2033 	rxq->num_rx_buf = RX_BUFFERS_PER_QUEUE;
2034 	rxq->rxq_idx = rxq_idx;
2035 	rxq->rxobj = INVALID_MANA_HANDLE;
2036 
2037 	mana_get_rxbuf_cfg(ndev->mtu, &rxq->datasize, &rxq->alloc_size,
2038 			   &rxq->headroom);
2039 
2040 	err = mana_alloc_rx_wqe(apc, rxq, &rq_size, &cq_size);
2041 	if (err)
2042 		goto out;
2043 
2044 	rq_size = PAGE_ALIGN(rq_size);
2045 	cq_size = PAGE_ALIGN(cq_size);
2046 
2047 	/* Create RQ */
2048 	memset(&spec, 0, sizeof(spec));
2049 	spec.type = GDMA_RQ;
2050 	spec.monitor_avl_buf = true;
2051 	spec.queue_size = rq_size;
2052 	err = mana_gd_create_mana_wq_cq(gd, &spec, &rxq->gdma_rq);
2053 	if (err)
2054 		goto out;
2055 
2056 	/* Create RQ's CQ */
2057 	cq = &rxq->rx_cq;
2058 	cq->type = MANA_CQ_TYPE_RX;
2059 	cq->rxq = rxq;
2060 
2061 	memset(&spec, 0, sizeof(spec));
2062 	spec.type = GDMA_CQ;
2063 	spec.monitor_avl_buf = false;
2064 	spec.queue_size = cq_size;
2065 	spec.cq.callback = mana_schedule_napi;
2066 	spec.cq.parent_eq = eq->eq;
2067 	spec.cq.context = cq;
2068 	err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq);
2069 	if (err)
2070 		goto out;
2071 
2072 	memset(&wq_spec, 0, sizeof(wq_spec));
2073 	memset(&cq_spec, 0, sizeof(cq_spec));
2074 	wq_spec.gdma_region = rxq->gdma_rq->mem_info.dma_region_handle;
2075 	wq_spec.queue_size = rxq->gdma_rq->queue_size;
2076 
2077 	cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle;
2078 	cq_spec.queue_size = cq->gdma_cq->queue_size;
2079 	cq_spec.modr_ctx_id = 0;
2080 	cq_spec.attached_eq = cq->gdma_cq->cq.parent->id;
2081 
2082 	err = mana_create_wq_obj(apc, apc->port_handle, GDMA_RQ,
2083 				 &wq_spec, &cq_spec, &rxq->rxobj);
2084 	if (err)
2085 		goto out;
2086 
2087 	rxq->gdma_rq->id = wq_spec.queue_index;
2088 	cq->gdma_cq->id = cq_spec.queue_index;
2089 
2090 	rxq->gdma_rq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION;
2091 	cq->gdma_cq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION;
2092 
2093 	rxq->gdma_id = rxq->gdma_rq->id;
2094 	cq->gdma_id = cq->gdma_cq->id;
2095 
2096 	err = mana_push_wqe(rxq);
2097 	if (err)
2098 		goto out;
2099 
2100 	if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) {
2101 		err = -EINVAL;
2102 		goto out;
2103 	}
2104 
2105 	gc->cq_table[cq->gdma_id] = cq->gdma_cq;
2106 
2107 	netif_napi_add_weight(ndev, &cq->napi, mana_poll, 1);
2108 
2109 	WARN_ON(xdp_rxq_info_reg(&rxq->xdp_rxq, ndev, rxq_idx,
2110 				 cq->napi.napi_id));
2111 	WARN_ON(xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq,
2112 					   MEM_TYPE_PAGE_SHARED, NULL));
2113 
2114 	napi_enable(&cq->napi);
2115 
2116 	mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT);
2117 out:
2118 	if (!err)
2119 		return rxq;
2120 
2121 	netdev_err(ndev, "Failed to create RXQ: err = %d\n", err);
2122 
2123 	mana_destroy_rxq(apc, rxq, false);
2124 
2125 	if (cq)
2126 		mana_deinit_cq(apc, cq);
2127 
2128 	return NULL;
2129 }
2130 
2131 static int mana_add_rx_queues(struct mana_port_context *apc,
2132 			      struct net_device *ndev)
2133 {
2134 	struct mana_context *ac = apc->ac;
2135 	struct mana_rxq *rxq;
2136 	int err = 0;
2137 	int i;
2138 
2139 	for (i = 0; i < apc->num_queues; i++) {
2140 		rxq = mana_create_rxq(apc, i, &ac->eqs[i], ndev);
2141 		if (!rxq) {
2142 			err = -ENOMEM;
2143 			goto out;
2144 		}
2145 
2146 		u64_stats_init(&rxq->stats.syncp);
2147 
2148 		apc->rxqs[i] = rxq;
2149 	}
2150 
2151 	apc->default_rxobj = apc->rxqs[0]->rxobj;
2152 out:
2153 	return err;
2154 }
2155 
2156 static void mana_destroy_vport(struct mana_port_context *apc)
2157 {
2158 	struct gdma_dev *gd = apc->ac->gdma_dev;
2159 	struct mana_rxq *rxq;
2160 	u32 rxq_idx;
2161 
2162 	for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) {
2163 		rxq = apc->rxqs[rxq_idx];
2164 		if (!rxq)
2165 			continue;
2166 
2167 		mana_destroy_rxq(apc, rxq, true);
2168 		apc->rxqs[rxq_idx] = NULL;
2169 	}
2170 
2171 	mana_destroy_txq(apc);
2172 	mana_uncfg_vport(apc);
2173 
2174 	if (gd->gdma_context->is_pf)
2175 		mana_pf_deregister_hw_vport(apc);
2176 }
2177 
2178 static int mana_create_vport(struct mana_port_context *apc,
2179 			     struct net_device *net)
2180 {
2181 	struct gdma_dev *gd = apc->ac->gdma_dev;
2182 	int err;
2183 
2184 	apc->default_rxobj = INVALID_MANA_HANDLE;
2185 
2186 	if (gd->gdma_context->is_pf) {
2187 		err = mana_pf_register_hw_vport(apc);
2188 		if (err)
2189 			return err;
2190 	}
2191 
2192 	err = mana_cfg_vport(apc, gd->pdid, gd->doorbell);
2193 	if (err)
2194 		return err;
2195 
2196 	return mana_create_txq(apc, net);
2197 }
2198 
2199 static void mana_rss_table_init(struct mana_port_context *apc)
2200 {
2201 	int i;
2202 
2203 	for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
2204 		apc->indir_table[i] =
2205 			ethtool_rxfh_indir_default(i, apc->num_queues);
2206 }
2207 
2208 int mana_config_rss(struct mana_port_context *apc, enum TRI_STATE rx,
2209 		    bool update_hash, bool update_tab)
2210 {
2211 	u32 queue_idx;
2212 	int err;
2213 	int i;
2214 
2215 	if (update_tab) {
2216 		for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) {
2217 			queue_idx = apc->indir_table[i];
2218 			apc->rxobj_table[i] = apc->rxqs[queue_idx]->rxobj;
2219 		}
2220 	}
2221 
2222 	err = mana_cfg_vport_steering(apc, rx, true, update_hash, update_tab);
2223 	if (err)
2224 		return err;
2225 
2226 	mana_fence_rqs(apc);
2227 
2228 	return 0;
2229 }
2230 
2231 static int mana_init_port(struct net_device *ndev)
2232 {
2233 	struct mana_port_context *apc = netdev_priv(ndev);
2234 	u32 max_txq, max_rxq, max_queues;
2235 	int port_idx = apc->port_idx;
2236 	u32 num_indirect_entries;
2237 	int err;
2238 
2239 	err = mana_init_port_context(apc);
2240 	if (err)
2241 		return err;
2242 
2243 	err = mana_query_vport_cfg(apc, port_idx, &max_txq, &max_rxq,
2244 				   &num_indirect_entries);
2245 	if (err) {
2246 		netdev_err(ndev, "Failed to query info for vPort %d\n",
2247 			   port_idx);
2248 		goto reset_apc;
2249 	}
2250 
2251 	max_queues = min_t(u32, max_txq, max_rxq);
2252 	if (apc->max_queues > max_queues)
2253 		apc->max_queues = max_queues;
2254 
2255 	if (apc->num_queues > apc->max_queues)
2256 		apc->num_queues = apc->max_queues;
2257 
2258 	eth_hw_addr_set(ndev, apc->mac_addr);
2259 
2260 	return 0;
2261 
2262 reset_apc:
2263 	kfree(apc->rxqs);
2264 	apc->rxqs = NULL;
2265 	return err;
2266 }
2267 
2268 int mana_alloc_queues(struct net_device *ndev)
2269 {
2270 	struct mana_port_context *apc = netdev_priv(ndev);
2271 	struct gdma_dev *gd = apc->ac->gdma_dev;
2272 	int err;
2273 
2274 	err = mana_create_vport(apc, ndev);
2275 	if (err)
2276 		return err;
2277 
2278 	err = netif_set_real_num_tx_queues(ndev, apc->num_queues);
2279 	if (err)
2280 		goto destroy_vport;
2281 
2282 	err = mana_add_rx_queues(apc, ndev);
2283 	if (err)
2284 		goto destroy_vport;
2285 
2286 	apc->rss_state = apc->num_queues > 1 ? TRI_STATE_TRUE : TRI_STATE_FALSE;
2287 
2288 	err = netif_set_real_num_rx_queues(ndev, apc->num_queues);
2289 	if (err)
2290 		goto destroy_vport;
2291 
2292 	mana_rss_table_init(apc);
2293 
2294 	err = mana_config_rss(apc, TRI_STATE_TRUE, true, true);
2295 	if (err)
2296 		goto destroy_vport;
2297 
2298 	if (gd->gdma_context->is_pf) {
2299 		err = mana_pf_register_filter(apc);
2300 		if (err)
2301 			goto destroy_vport;
2302 	}
2303 
2304 	mana_chn_setxdp(apc, mana_xdp_get(apc));
2305 
2306 	return 0;
2307 
2308 destroy_vport:
2309 	mana_destroy_vport(apc);
2310 	return err;
2311 }
2312 
2313 int mana_attach(struct net_device *ndev)
2314 {
2315 	struct mana_port_context *apc = netdev_priv(ndev);
2316 	int err;
2317 
2318 	ASSERT_RTNL();
2319 
2320 	err = mana_init_port(ndev);
2321 	if (err)
2322 		return err;
2323 
2324 	if (apc->port_st_save) {
2325 		err = mana_alloc_queues(ndev);
2326 		if (err) {
2327 			mana_cleanup_port_context(apc);
2328 			return err;
2329 		}
2330 	}
2331 
2332 	apc->port_is_up = apc->port_st_save;
2333 
2334 	/* Ensure port state updated before txq state */
2335 	smp_wmb();
2336 
2337 	if (apc->port_is_up)
2338 		netif_carrier_on(ndev);
2339 
2340 	netif_device_attach(ndev);
2341 
2342 	return 0;
2343 }
2344 
2345 static int mana_dealloc_queues(struct net_device *ndev)
2346 {
2347 	struct mana_port_context *apc = netdev_priv(ndev);
2348 	struct gdma_dev *gd = apc->ac->gdma_dev;
2349 	struct mana_txq *txq;
2350 	int i, err;
2351 
2352 	if (apc->port_is_up)
2353 		return -EINVAL;
2354 
2355 	mana_chn_setxdp(apc, NULL);
2356 
2357 	if (gd->gdma_context->is_pf)
2358 		mana_pf_deregister_filter(apc);
2359 
2360 	/* No packet can be transmitted now since apc->port_is_up is false.
2361 	 * There is still a tiny chance that mana_poll_tx_cq() can re-enable
2362 	 * a txq because it may not timely see apc->port_is_up being cleared
2363 	 * to false, but it doesn't matter since mana_start_xmit() drops any
2364 	 * new packets due to apc->port_is_up being false.
2365 	 *
2366 	 * Drain all the in-flight TX packets
2367 	 */
2368 	for (i = 0; i < apc->num_queues; i++) {
2369 		txq = &apc->tx_qp[i].txq;
2370 
2371 		while (atomic_read(&txq->pending_sends) > 0)
2372 			usleep_range(1000, 2000);
2373 	}
2374 
2375 	/* We're 100% sure the queues can no longer be woken up, because
2376 	 * we're sure now mana_poll_tx_cq() can't be running.
2377 	 */
2378 
2379 	apc->rss_state = TRI_STATE_FALSE;
2380 	err = mana_config_rss(apc, TRI_STATE_FALSE, false, false);
2381 	if (err) {
2382 		netdev_err(ndev, "Failed to disable vPort: %d\n", err);
2383 		return err;
2384 	}
2385 
2386 	mana_destroy_vport(apc);
2387 
2388 	return 0;
2389 }
2390 
2391 int mana_detach(struct net_device *ndev, bool from_close)
2392 {
2393 	struct mana_port_context *apc = netdev_priv(ndev);
2394 	int err;
2395 
2396 	ASSERT_RTNL();
2397 
2398 	apc->port_st_save = apc->port_is_up;
2399 	apc->port_is_up = false;
2400 
2401 	/* Ensure port state updated before txq state */
2402 	smp_wmb();
2403 
2404 	netif_tx_disable(ndev);
2405 	netif_carrier_off(ndev);
2406 
2407 	if (apc->port_st_save) {
2408 		err = mana_dealloc_queues(ndev);
2409 		if (err)
2410 			return err;
2411 	}
2412 
2413 	if (!from_close) {
2414 		netif_device_detach(ndev);
2415 		mana_cleanup_port_context(apc);
2416 	}
2417 
2418 	return 0;
2419 }
2420 
2421 static int mana_probe_port(struct mana_context *ac, int port_idx,
2422 			   struct net_device **ndev_storage)
2423 {
2424 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
2425 	struct mana_port_context *apc;
2426 	struct net_device *ndev;
2427 	int err;
2428 
2429 	ndev = alloc_etherdev_mq(sizeof(struct mana_port_context),
2430 				 gc->max_num_queues);
2431 	if (!ndev)
2432 		return -ENOMEM;
2433 
2434 	*ndev_storage = ndev;
2435 
2436 	apc = netdev_priv(ndev);
2437 	apc->ac = ac;
2438 	apc->ndev = ndev;
2439 	apc->max_queues = gc->max_num_queues;
2440 	apc->num_queues = gc->max_num_queues;
2441 	apc->port_handle = INVALID_MANA_HANDLE;
2442 	apc->pf_filter_handle = INVALID_MANA_HANDLE;
2443 	apc->port_idx = port_idx;
2444 
2445 	mutex_init(&apc->vport_mutex);
2446 	apc->vport_use_count = 0;
2447 
2448 	ndev->netdev_ops = &mana_devops;
2449 	ndev->ethtool_ops = &mana_ethtool_ops;
2450 	ndev->mtu = ETH_DATA_LEN;
2451 	ndev->max_mtu = gc->adapter_mtu - ETH_HLEN;
2452 	ndev->min_mtu = ETH_MIN_MTU;
2453 	ndev->needed_headroom = MANA_HEADROOM;
2454 	ndev->dev_port = port_idx;
2455 	SET_NETDEV_DEV(ndev, gc->dev);
2456 
2457 	netif_carrier_off(ndev);
2458 
2459 	netdev_rss_key_fill(apc->hashkey, MANA_HASH_KEY_SIZE);
2460 
2461 	err = mana_init_port(ndev);
2462 	if (err)
2463 		goto free_net;
2464 
2465 	netdev_lockdep_set_classes(ndev);
2466 
2467 	ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2468 	ndev->hw_features |= NETIF_F_RXCSUM;
2469 	ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2470 	ndev->hw_features |= NETIF_F_RXHASH;
2471 	ndev->features = ndev->hw_features | NETIF_F_HW_VLAN_CTAG_TX |
2472 			 NETIF_F_HW_VLAN_CTAG_RX;
2473 	ndev->vlan_features = ndev->features;
2474 	ndev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
2475 			     NETDEV_XDP_ACT_NDO_XMIT;
2476 
2477 	err = register_netdev(ndev);
2478 	if (err) {
2479 		netdev_err(ndev, "Unable to register netdev.\n");
2480 		goto reset_apc;
2481 	}
2482 
2483 	return 0;
2484 
2485 reset_apc:
2486 	kfree(apc->rxqs);
2487 	apc->rxqs = NULL;
2488 free_net:
2489 	*ndev_storage = NULL;
2490 	netdev_err(ndev, "Failed to probe vPort %d: %d\n", port_idx, err);
2491 	free_netdev(ndev);
2492 	return err;
2493 }
2494 
2495 static void adev_release(struct device *dev)
2496 {
2497 	struct mana_adev *madev = container_of(dev, struct mana_adev, adev.dev);
2498 
2499 	kfree(madev);
2500 }
2501 
2502 static void remove_adev(struct gdma_dev *gd)
2503 {
2504 	struct auxiliary_device *adev = gd->adev;
2505 	int id = adev->id;
2506 
2507 	auxiliary_device_delete(adev);
2508 	auxiliary_device_uninit(adev);
2509 
2510 	mana_adev_idx_free(id);
2511 	gd->adev = NULL;
2512 }
2513 
2514 static int add_adev(struct gdma_dev *gd)
2515 {
2516 	struct auxiliary_device *adev;
2517 	struct mana_adev *madev;
2518 	int ret;
2519 
2520 	madev = kzalloc(sizeof(*madev), GFP_KERNEL);
2521 	if (!madev)
2522 		return -ENOMEM;
2523 
2524 	adev = &madev->adev;
2525 	ret = mana_adev_idx_alloc();
2526 	if (ret < 0)
2527 		goto idx_fail;
2528 	adev->id = ret;
2529 
2530 	adev->name = "rdma";
2531 	adev->dev.parent = gd->gdma_context->dev;
2532 	adev->dev.release = adev_release;
2533 	madev->mdev = gd;
2534 
2535 	ret = auxiliary_device_init(adev);
2536 	if (ret)
2537 		goto init_fail;
2538 
2539 	ret = auxiliary_device_add(adev);
2540 	if (ret)
2541 		goto add_fail;
2542 
2543 	gd->adev = adev;
2544 	return 0;
2545 
2546 add_fail:
2547 	auxiliary_device_uninit(adev);
2548 
2549 init_fail:
2550 	mana_adev_idx_free(adev->id);
2551 
2552 idx_fail:
2553 	kfree(madev);
2554 
2555 	return ret;
2556 }
2557 
2558 int mana_probe(struct gdma_dev *gd, bool resuming)
2559 {
2560 	struct gdma_context *gc = gd->gdma_context;
2561 	struct mana_context *ac = gd->driver_data;
2562 	struct device *dev = gc->dev;
2563 	u16 num_ports = 0;
2564 	int err;
2565 	int i;
2566 
2567 	dev_info(dev,
2568 		 "Microsoft Azure Network Adapter protocol version: %d.%d.%d\n",
2569 		 MANA_MAJOR_VERSION, MANA_MINOR_VERSION, MANA_MICRO_VERSION);
2570 
2571 	err = mana_gd_register_device(gd);
2572 	if (err)
2573 		return err;
2574 
2575 	if (!resuming) {
2576 		ac = kzalloc(sizeof(*ac), GFP_KERNEL);
2577 		if (!ac)
2578 			return -ENOMEM;
2579 
2580 		ac->gdma_dev = gd;
2581 		gd->driver_data = ac;
2582 	}
2583 
2584 	err = mana_create_eq(ac);
2585 	if (err)
2586 		goto out;
2587 
2588 	err = mana_query_device_cfg(ac, MANA_MAJOR_VERSION, MANA_MINOR_VERSION,
2589 				    MANA_MICRO_VERSION, &num_ports);
2590 	if (err)
2591 		goto out;
2592 
2593 	if (!resuming) {
2594 		ac->num_ports = num_ports;
2595 	} else {
2596 		if (ac->num_ports != num_ports) {
2597 			dev_err(dev, "The number of vPorts changed: %d->%d\n",
2598 				ac->num_ports, num_ports);
2599 			err = -EPROTO;
2600 			goto out;
2601 		}
2602 	}
2603 
2604 	if (ac->num_ports == 0)
2605 		dev_err(dev, "Failed to detect any vPort\n");
2606 
2607 	if (ac->num_ports > MAX_PORTS_IN_MANA_DEV)
2608 		ac->num_ports = MAX_PORTS_IN_MANA_DEV;
2609 
2610 	if (!resuming) {
2611 		for (i = 0; i < ac->num_ports; i++) {
2612 			err = mana_probe_port(ac, i, &ac->ports[i]);
2613 			if (err)
2614 				break;
2615 		}
2616 	} else {
2617 		for (i = 0; i < ac->num_ports; i++) {
2618 			rtnl_lock();
2619 			err = mana_attach(ac->ports[i]);
2620 			rtnl_unlock();
2621 			if (err)
2622 				break;
2623 		}
2624 	}
2625 
2626 	err = add_adev(gd);
2627 out:
2628 	if (err)
2629 		mana_remove(gd, false);
2630 
2631 	return err;
2632 }
2633 
2634 void mana_remove(struct gdma_dev *gd, bool suspending)
2635 {
2636 	struct gdma_context *gc = gd->gdma_context;
2637 	struct mana_context *ac = gd->driver_data;
2638 	struct device *dev = gc->dev;
2639 	struct net_device *ndev;
2640 	int err;
2641 	int i;
2642 
2643 	/* adev currently doesn't support suspending, always remove it */
2644 	if (gd->adev)
2645 		remove_adev(gd);
2646 
2647 	for (i = 0; i < ac->num_ports; i++) {
2648 		ndev = ac->ports[i];
2649 		if (!ndev) {
2650 			if (i == 0)
2651 				dev_err(dev, "No net device to remove\n");
2652 			goto out;
2653 		}
2654 
2655 		/* All cleanup actions should stay after rtnl_lock(), otherwise
2656 		 * other functions may access partially cleaned up data.
2657 		 */
2658 		rtnl_lock();
2659 
2660 		err = mana_detach(ndev, false);
2661 		if (err)
2662 			netdev_err(ndev, "Failed to detach vPort %d: %d\n",
2663 				   i, err);
2664 
2665 		if (suspending) {
2666 			/* No need to unregister the ndev. */
2667 			rtnl_unlock();
2668 			continue;
2669 		}
2670 
2671 		unregister_netdevice(ndev);
2672 
2673 		rtnl_unlock();
2674 
2675 		free_netdev(ndev);
2676 	}
2677 
2678 	mana_destroy_eq(ac);
2679 out:
2680 	mana_gd_deregister_device(gd);
2681 
2682 	if (suspending)
2683 		return;
2684 
2685 	gd->driver_data = NULL;
2686 	gd->gdma_context = NULL;
2687 	kfree(ac);
2688 }
2689