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