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