xref: /linux/drivers/net/ethernet/microsoft/mana/mana_en.c (revision 6beeaf48db6c548fcfc2ad32739d33af2fef3a5b)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2021, Microsoft Corporation. */
3 
4 #include <linux/inetdevice.h>
5 #include <linux/etherdevice.h>
6 #include <linux/ethtool.h>
7 #include <linux/mm.h>
8 
9 #include <net/checksum.h>
10 #include <net/ip6_checksum.h>
11 
12 #include "mana.h"
13 
14 /* Microsoft Azure Network Adapter (MANA) functions */
15 
16 static int mana_open(struct net_device *ndev)
17 {
18 	struct mana_port_context *apc = netdev_priv(ndev);
19 	int err;
20 
21 	err = mana_alloc_queues(ndev);
22 	if (err)
23 		return err;
24 
25 	apc->port_is_up = true;
26 
27 	/* Ensure port state updated before txq state */
28 	smp_wmb();
29 
30 	netif_carrier_on(ndev);
31 	netif_tx_wake_all_queues(ndev);
32 
33 	return 0;
34 }
35 
36 static int mana_close(struct net_device *ndev)
37 {
38 	struct mana_port_context *apc = netdev_priv(ndev);
39 
40 	if (!apc->port_is_up)
41 		return 0;
42 
43 	return mana_detach(ndev, true);
44 }
45 
46 static bool mana_can_tx(struct gdma_queue *wq)
47 {
48 	return mana_gd_wq_avail_space(wq) >= MAX_TX_WQE_SIZE;
49 }
50 
51 static unsigned int mana_checksum_info(struct sk_buff *skb)
52 {
53 	if (skb->protocol == htons(ETH_P_IP)) {
54 		struct iphdr *ip = ip_hdr(skb);
55 
56 		if (ip->protocol == IPPROTO_TCP)
57 			return IPPROTO_TCP;
58 
59 		if (ip->protocol == IPPROTO_UDP)
60 			return IPPROTO_UDP;
61 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
62 		struct ipv6hdr *ip6 = ipv6_hdr(skb);
63 
64 		if (ip6->nexthdr == IPPROTO_TCP)
65 			return IPPROTO_TCP;
66 
67 		if (ip6->nexthdr == IPPROTO_UDP)
68 			return IPPROTO_UDP;
69 	}
70 
71 	/* No csum offloading */
72 	return 0;
73 }
74 
75 static int mana_map_skb(struct sk_buff *skb, struct mana_port_context *apc,
76 			struct mana_tx_package *tp)
77 {
78 	struct mana_skb_head *ash = (struct mana_skb_head *)skb->head;
79 	struct gdma_dev *gd = apc->ac->gdma_dev;
80 	struct gdma_context *gc;
81 	struct device *dev;
82 	skb_frag_t *frag;
83 	dma_addr_t da;
84 	int i;
85 
86 	gc = gd->gdma_context;
87 	dev = gc->dev;
88 	da = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
89 
90 	if (dma_mapping_error(dev, da))
91 		return -ENOMEM;
92 
93 	ash->dma_handle[0] = da;
94 	ash->size[0] = skb_headlen(skb);
95 
96 	tp->wqe_req.sgl[0].address = ash->dma_handle[0];
97 	tp->wqe_req.sgl[0].mem_key = gd->gpa_mkey;
98 	tp->wqe_req.sgl[0].size = ash->size[0];
99 
100 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
101 		frag = &skb_shinfo(skb)->frags[i];
102 		da = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
103 				      DMA_TO_DEVICE);
104 
105 		if (dma_mapping_error(dev, da))
106 			goto frag_err;
107 
108 		ash->dma_handle[i + 1] = da;
109 		ash->size[i + 1] = skb_frag_size(frag);
110 
111 		tp->wqe_req.sgl[i + 1].address = ash->dma_handle[i + 1];
112 		tp->wqe_req.sgl[i + 1].mem_key = gd->gpa_mkey;
113 		tp->wqe_req.sgl[i + 1].size = ash->size[i + 1];
114 	}
115 
116 	return 0;
117 
118 frag_err:
119 	for (i = i - 1; i >= 0; i--)
120 		dma_unmap_page(dev, ash->dma_handle[i + 1], ash->size[i + 1],
121 			       DMA_TO_DEVICE);
122 
123 	dma_unmap_single(dev, ash->dma_handle[0], ash->size[0], DMA_TO_DEVICE);
124 
125 	return -ENOMEM;
126 }
127 
128 static int mana_start_xmit(struct sk_buff *skb, struct net_device *ndev)
129 {
130 	enum mana_tx_pkt_format pkt_fmt = MANA_SHORT_PKT_FMT;
131 	struct mana_port_context *apc = netdev_priv(ndev);
132 	u16 txq_idx = skb_get_queue_mapping(skb);
133 	struct gdma_dev *gd = apc->ac->gdma_dev;
134 	bool ipv4 = false, ipv6 = false;
135 	struct mana_tx_package pkg = {};
136 	struct netdev_queue *net_txq;
137 	struct mana_stats *tx_stats;
138 	struct gdma_queue *gdma_sq;
139 	unsigned int csum_type;
140 	struct mana_txq *txq;
141 	struct mana_cq *cq;
142 	int err, len;
143 
144 	if (unlikely(!apc->port_is_up))
145 		goto tx_drop;
146 
147 	if (skb_cow_head(skb, MANA_HEADROOM))
148 		goto tx_drop_count;
149 
150 	txq = &apc->tx_qp[txq_idx].txq;
151 	gdma_sq = txq->gdma_sq;
152 	cq = &apc->tx_qp[txq_idx].tx_cq;
153 
154 	pkg.tx_oob.s_oob.vcq_num = cq->gdma_id;
155 	pkg.tx_oob.s_oob.vsq_frame = txq->vsq_frame;
156 
157 	if (txq->vp_offset > MANA_SHORT_VPORT_OFFSET_MAX) {
158 		pkg.tx_oob.l_oob.long_vp_offset = txq->vp_offset;
159 		pkt_fmt = MANA_LONG_PKT_FMT;
160 	} else {
161 		pkg.tx_oob.s_oob.short_vp_offset = txq->vp_offset;
162 	}
163 
164 	pkg.tx_oob.s_oob.pkt_fmt = pkt_fmt;
165 
166 	if (pkt_fmt == MANA_SHORT_PKT_FMT)
167 		pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_short_oob);
168 	else
169 		pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_oob);
170 
171 	pkg.wqe_req.inline_oob_data = &pkg.tx_oob;
172 	pkg.wqe_req.flags = 0;
173 	pkg.wqe_req.client_data_unit = 0;
174 
175 	pkg.wqe_req.num_sge = 1 + skb_shinfo(skb)->nr_frags;
176 	WARN_ON_ONCE(pkg.wqe_req.num_sge > 30);
177 
178 	if (pkg.wqe_req.num_sge <= ARRAY_SIZE(pkg.sgl_array)) {
179 		pkg.wqe_req.sgl = pkg.sgl_array;
180 	} else {
181 		pkg.sgl_ptr = kmalloc_array(pkg.wqe_req.num_sge,
182 					    sizeof(struct gdma_sge),
183 					    GFP_ATOMIC);
184 		if (!pkg.sgl_ptr)
185 			goto tx_drop_count;
186 
187 		pkg.wqe_req.sgl = pkg.sgl_ptr;
188 	}
189 
190 	if (skb->protocol == htons(ETH_P_IP))
191 		ipv4 = true;
192 	else if (skb->protocol == htons(ETH_P_IPV6))
193 		ipv6 = true;
194 
195 	if (skb_is_gso(skb)) {
196 		pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
197 		pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
198 
199 		pkg.tx_oob.s_oob.comp_iphdr_csum = 1;
200 		pkg.tx_oob.s_oob.comp_tcp_csum = 1;
201 		pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb);
202 
203 		pkg.wqe_req.client_data_unit = skb_shinfo(skb)->gso_size;
204 		pkg.wqe_req.flags = GDMA_WR_OOB_IN_SGL | GDMA_WR_PAD_BY_SGE0;
205 		if (ipv4) {
206 			ip_hdr(skb)->tot_len = 0;
207 			ip_hdr(skb)->check = 0;
208 			tcp_hdr(skb)->check =
209 				~csum_tcpudp_magic(ip_hdr(skb)->saddr,
210 						   ip_hdr(skb)->daddr, 0,
211 						   IPPROTO_TCP, 0);
212 		} else {
213 			ipv6_hdr(skb)->payload_len = 0;
214 			tcp_hdr(skb)->check =
215 				~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
216 						 &ipv6_hdr(skb)->daddr, 0,
217 						 IPPROTO_TCP, 0);
218 		}
219 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
220 		csum_type = mana_checksum_info(skb);
221 
222 		if (csum_type == IPPROTO_TCP) {
223 			pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
224 			pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
225 
226 			pkg.tx_oob.s_oob.comp_tcp_csum = 1;
227 			pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb);
228 
229 		} else if (csum_type == IPPROTO_UDP) {
230 			pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
231 			pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
232 
233 			pkg.tx_oob.s_oob.comp_udp_csum = 1;
234 		} else {
235 			/* Can't do offload of this type of checksum */
236 			if (skb_checksum_help(skb))
237 				goto free_sgl_ptr;
238 		}
239 	}
240 
241 	if (mana_map_skb(skb, apc, &pkg))
242 		goto free_sgl_ptr;
243 
244 	skb_queue_tail(&txq->pending_skbs, skb);
245 
246 	len = skb->len;
247 	net_txq = netdev_get_tx_queue(ndev, txq_idx);
248 
249 	err = mana_gd_post_work_request(gdma_sq, &pkg.wqe_req,
250 					(struct gdma_posted_wqe_info *)skb->cb);
251 	if (!mana_can_tx(gdma_sq)) {
252 		netif_tx_stop_queue(net_txq);
253 		apc->eth_stats.stop_queue++;
254 	}
255 
256 	if (err) {
257 		(void)skb_dequeue_tail(&txq->pending_skbs);
258 		netdev_warn(ndev, "Failed to post TX OOB: %d\n", err);
259 		err = NETDEV_TX_BUSY;
260 		goto tx_busy;
261 	}
262 
263 	err = NETDEV_TX_OK;
264 	atomic_inc(&txq->pending_sends);
265 
266 	mana_gd_wq_ring_doorbell(gd->gdma_context, gdma_sq);
267 
268 	/* skb may be freed after mana_gd_post_work_request. Do not use it. */
269 	skb = NULL;
270 
271 	tx_stats = &txq->stats;
272 	u64_stats_update_begin(&tx_stats->syncp);
273 	tx_stats->packets++;
274 	tx_stats->bytes += len;
275 	u64_stats_update_end(&tx_stats->syncp);
276 
277 tx_busy:
278 	if (netif_tx_queue_stopped(net_txq) && mana_can_tx(gdma_sq)) {
279 		netif_tx_wake_queue(net_txq);
280 		apc->eth_stats.wake_queue++;
281 	}
282 
283 	kfree(pkg.sgl_ptr);
284 	return err;
285 
286 free_sgl_ptr:
287 	kfree(pkg.sgl_ptr);
288 tx_drop_count:
289 	ndev->stats.tx_dropped++;
290 tx_drop:
291 	dev_kfree_skb_any(skb);
292 	return NETDEV_TX_OK;
293 }
294 
295 static void mana_get_stats64(struct net_device *ndev,
296 			     struct rtnl_link_stats64 *st)
297 {
298 	struct mana_port_context *apc = netdev_priv(ndev);
299 	unsigned int num_queues = apc->num_queues;
300 	struct mana_stats *stats;
301 	unsigned int start;
302 	u64 packets, bytes;
303 	int q;
304 
305 	if (!apc->port_is_up)
306 		return;
307 
308 	netdev_stats_to_stats64(st, &ndev->stats);
309 
310 	for (q = 0; q < num_queues; q++) {
311 		stats = &apc->rxqs[q]->stats;
312 
313 		do {
314 			start = u64_stats_fetch_begin_irq(&stats->syncp);
315 			packets = stats->packets;
316 			bytes = stats->bytes;
317 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
318 
319 		st->rx_packets += packets;
320 		st->rx_bytes += bytes;
321 	}
322 
323 	for (q = 0; q < num_queues; q++) {
324 		stats = &apc->tx_qp[q].txq.stats;
325 
326 		do {
327 			start = u64_stats_fetch_begin_irq(&stats->syncp);
328 			packets = stats->packets;
329 			bytes = stats->bytes;
330 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
331 
332 		st->tx_packets += packets;
333 		st->tx_bytes += bytes;
334 	}
335 }
336 
337 static int mana_get_tx_queue(struct net_device *ndev, struct sk_buff *skb,
338 			     int old_q)
339 {
340 	struct mana_port_context *apc = netdev_priv(ndev);
341 	u32 hash = skb_get_hash(skb);
342 	struct sock *sk = skb->sk;
343 	int txq;
344 
345 	txq = apc->indir_table[hash & MANA_INDIRECT_TABLE_MASK];
346 
347 	if (txq != old_q && sk && sk_fullsock(sk) &&
348 	    rcu_access_pointer(sk->sk_dst_cache))
349 		sk_tx_queue_set(sk, txq);
350 
351 	return txq;
352 }
353 
354 static u16 mana_select_queue(struct net_device *ndev, struct sk_buff *skb,
355 			     struct net_device *sb_dev)
356 {
357 	int txq;
358 
359 	if (ndev->real_num_tx_queues == 1)
360 		return 0;
361 
362 	txq = sk_tx_queue_get(skb->sk);
363 
364 	if (txq < 0 || skb->ooo_okay || txq >= ndev->real_num_tx_queues) {
365 		if (skb_rx_queue_recorded(skb))
366 			txq = skb_get_rx_queue(skb);
367 		else
368 			txq = mana_get_tx_queue(ndev, skb, txq);
369 	}
370 
371 	return txq;
372 }
373 
374 static const struct net_device_ops mana_devops = {
375 	.ndo_open		= mana_open,
376 	.ndo_stop		= mana_close,
377 	.ndo_select_queue	= mana_select_queue,
378 	.ndo_start_xmit		= mana_start_xmit,
379 	.ndo_validate_addr	= eth_validate_addr,
380 	.ndo_get_stats64	= mana_get_stats64,
381 };
382 
383 static void mana_cleanup_port_context(struct mana_port_context *apc)
384 {
385 	kfree(apc->rxqs);
386 	apc->rxqs = NULL;
387 }
388 
389 static int mana_init_port_context(struct mana_port_context *apc)
390 {
391 	apc->rxqs = kcalloc(apc->num_queues, sizeof(struct mana_rxq *),
392 			    GFP_KERNEL);
393 
394 	return !apc->rxqs ? -ENOMEM : 0;
395 }
396 
397 static int mana_send_request(struct mana_context *ac, void *in_buf,
398 			     u32 in_len, void *out_buf, u32 out_len)
399 {
400 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
401 	struct gdma_resp_hdr *resp = out_buf;
402 	struct gdma_req_hdr *req = in_buf;
403 	struct device *dev = gc->dev;
404 	static atomic_t activity_id;
405 	int err;
406 
407 	req->dev_id = gc->mana.dev_id;
408 	req->activity_id = atomic_inc_return(&activity_id);
409 
410 	err = mana_gd_send_request(gc, in_len, in_buf, out_len,
411 				   out_buf);
412 	if (err || resp->status) {
413 		dev_err(dev, "Failed to send mana message: %d, 0x%x\n",
414 			err, resp->status);
415 		return err ? err : -EPROTO;
416 	}
417 
418 	if (req->dev_id.as_uint32 != resp->dev_id.as_uint32 ||
419 	    req->activity_id != resp->activity_id) {
420 		dev_err(dev, "Unexpected mana message response: %x,%x,%x,%x\n",
421 			req->dev_id.as_uint32, resp->dev_id.as_uint32,
422 			req->activity_id, resp->activity_id);
423 		return -EPROTO;
424 	}
425 
426 	return 0;
427 }
428 
429 static int mana_verify_resp_hdr(const struct gdma_resp_hdr *resp_hdr,
430 				const enum mana_command_code expected_code,
431 				const u32 min_size)
432 {
433 	if (resp_hdr->response.msg_type != expected_code)
434 		return -EPROTO;
435 
436 	if (resp_hdr->response.msg_version < GDMA_MESSAGE_V1)
437 		return -EPROTO;
438 
439 	if (resp_hdr->response.msg_size < min_size)
440 		return -EPROTO;
441 
442 	return 0;
443 }
444 
445 static int mana_query_device_cfg(struct mana_context *ac, u32 proto_major_ver,
446 				 u32 proto_minor_ver, u32 proto_micro_ver,
447 				 u16 *max_num_vports)
448 {
449 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
450 	struct mana_query_device_cfg_resp resp = {};
451 	struct mana_query_device_cfg_req req = {};
452 	struct device *dev = gc->dev;
453 	int err = 0;
454 
455 	mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_DEV_CONFIG,
456 			     sizeof(req), sizeof(resp));
457 	req.proto_major_ver = proto_major_ver;
458 	req.proto_minor_ver = proto_minor_ver;
459 	req.proto_micro_ver = proto_micro_ver;
460 
461 	err = mana_send_request(ac, &req, sizeof(req), &resp, sizeof(resp));
462 	if (err) {
463 		dev_err(dev, "Failed to query config: %d", err);
464 		return err;
465 	}
466 
467 	err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_DEV_CONFIG,
468 				   sizeof(resp));
469 	if (err || resp.hdr.status) {
470 		dev_err(dev, "Invalid query result: %d, 0x%x\n", err,
471 			resp.hdr.status);
472 		if (!err)
473 			err = -EPROTO;
474 		return err;
475 	}
476 
477 	*max_num_vports = resp.max_num_vports;
478 
479 	return 0;
480 }
481 
482 static int mana_query_vport_cfg(struct mana_port_context *apc, u32 vport_index,
483 				u32 *max_sq, u32 *max_rq, u32 *num_indir_entry)
484 {
485 	struct mana_query_vport_cfg_resp resp = {};
486 	struct mana_query_vport_cfg_req req = {};
487 	int err;
488 
489 	mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_VPORT_CONFIG,
490 			     sizeof(req), sizeof(resp));
491 
492 	req.vport_index = vport_index;
493 
494 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
495 				sizeof(resp));
496 	if (err)
497 		return err;
498 
499 	err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_VPORT_CONFIG,
500 				   sizeof(resp));
501 	if (err)
502 		return err;
503 
504 	if (resp.hdr.status)
505 		return -EPROTO;
506 
507 	*max_sq = resp.max_num_sq;
508 	*max_rq = resp.max_num_rq;
509 	*num_indir_entry = resp.num_indirection_ent;
510 
511 	apc->port_handle = resp.vport;
512 	ether_addr_copy(apc->mac_addr, resp.mac_addr);
513 
514 	return 0;
515 }
516 
517 static int mana_cfg_vport(struct mana_port_context *apc, u32 protection_dom_id,
518 			  u32 doorbell_pg_id)
519 {
520 	struct mana_config_vport_resp resp = {};
521 	struct mana_config_vport_req req = {};
522 	int err;
523 
524 	mana_gd_init_req_hdr(&req.hdr, MANA_CONFIG_VPORT_TX,
525 			     sizeof(req), sizeof(resp));
526 	req.vport = apc->port_handle;
527 	req.pdid = protection_dom_id;
528 	req.doorbell_pageid = doorbell_pg_id;
529 
530 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
531 				sizeof(resp));
532 	if (err) {
533 		netdev_err(apc->ndev, "Failed to configure vPort: %d\n", err);
534 		goto out;
535 	}
536 
537 	err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_TX,
538 				   sizeof(resp));
539 	if (err || resp.hdr.status) {
540 		netdev_err(apc->ndev, "Failed to configure vPort: %d, 0x%x\n",
541 			   err, resp.hdr.status);
542 		if (!err)
543 			err = -EPROTO;
544 
545 		goto out;
546 	}
547 
548 	apc->tx_shortform_allowed = resp.short_form_allowed;
549 	apc->tx_vp_offset = resp.tx_vport_offset;
550 out:
551 	return err;
552 }
553 
554 static int mana_cfg_vport_steering(struct mana_port_context *apc,
555 				   enum TRI_STATE rx,
556 				   bool update_default_rxobj, bool update_key,
557 				   bool update_tab)
558 {
559 	u16 num_entries = MANA_INDIRECT_TABLE_SIZE;
560 	struct mana_cfg_rx_steer_req *req = NULL;
561 	struct mana_cfg_rx_steer_resp resp = {};
562 	struct net_device *ndev = apc->ndev;
563 	mana_handle_t *req_indir_tab;
564 	u32 req_buf_size;
565 	int err;
566 
567 	req_buf_size = sizeof(*req) + sizeof(mana_handle_t) * num_entries;
568 	req = kzalloc(req_buf_size, GFP_KERNEL);
569 	if (!req)
570 		return -ENOMEM;
571 
572 	mana_gd_init_req_hdr(&req->hdr, MANA_CONFIG_VPORT_RX, req_buf_size,
573 			     sizeof(resp));
574 
575 	req->vport = apc->port_handle;
576 	req->num_indir_entries = num_entries;
577 	req->indir_tab_offset = sizeof(*req);
578 	req->rx_enable = rx;
579 	req->rss_enable = apc->rss_state;
580 	req->update_default_rxobj = update_default_rxobj;
581 	req->update_hashkey = update_key;
582 	req->update_indir_tab = update_tab;
583 	req->default_rxobj = apc->default_rxobj;
584 
585 	if (update_key)
586 		memcpy(&req->hashkey, apc->hashkey, MANA_HASH_KEY_SIZE);
587 
588 	if (update_tab) {
589 		req_indir_tab = (mana_handle_t *)(req + 1);
590 		memcpy(req_indir_tab, apc->rxobj_table,
591 		       req->num_indir_entries * sizeof(mana_handle_t));
592 	}
593 
594 	err = mana_send_request(apc->ac, req, req_buf_size, &resp,
595 				sizeof(resp));
596 	if (err) {
597 		netdev_err(ndev, "Failed to configure vPort RX: %d\n", err);
598 		goto out;
599 	}
600 
601 	err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_RX,
602 				   sizeof(resp));
603 	if (err) {
604 		netdev_err(ndev, "vPort RX configuration failed: %d\n", err);
605 		goto out;
606 	}
607 
608 	if (resp.hdr.status) {
609 		netdev_err(ndev, "vPort RX configuration failed: 0x%x\n",
610 			   resp.hdr.status);
611 		err = -EPROTO;
612 	}
613 out:
614 	kfree(req);
615 	return err;
616 }
617 
618 static int mana_create_wq_obj(struct mana_port_context *apc,
619 			      mana_handle_t vport,
620 			      u32 wq_type, struct mana_obj_spec *wq_spec,
621 			      struct mana_obj_spec *cq_spec,
622 			      mana_handle_t *wq_obj)
623 {
624 	struct mana_create_wqobj_resp resp = {};
625 	struct mana_create_wqobj_req req = {};
626 	struct net_device *ndev = apc->ndev;
627 	int err;
628 
629 	mana_gd_init_req_hdr(&req.hdr, MANA_CREATE_WQ_OBJ,
630 			     sizeof(req), sizeof(resp));
631 	req.vport = vport;
632 	req.wq_type = wq_type;
633 	req.wq_gdma_region = wq_spec->gdma_region;
634 	req.cq_gdma_region = cq_spec->gdma_region;
635 	req.wq_size = wq_spec->queue_size;
636 	req.cq_size = cq_spec->queue_size;
637 	req.cq_moderation_ctx_id = cq_spec->modr_ctx_id;
638 	req.cq_parent_qid = cq_spec->attached_eq;
639 
640 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
641 				sizeof(resp));
642 	if (err) {
643 		netdev_err(ndev, "Failed to create WQ object: %d\n", err);
644 		goto out;
645 	}
646 
647 	err = mana_verify_resp_hdr(&resp.hdr, MANA_CREATE_WQ_OBJ,
648 				   sizeof(resp));
649 	if (err || resp.hdr.status) {
650 		netdev_err(ndev, "Failed to create WQ object: %d, 0x%x\n", err,
651 			   resp.hdr.status);
652 		if (!err)
653 			err = -EPROTO;
654 		goto out;
655 	}
656 
657 	if (resp.wq_obj == INVALID_MANA_HANDLE) {
658 		netdev_err(ndev, "Got an invalid WQ object handle\n");
659 		err = -EPROTO;
660 		goto out;
661 	}
662 
663 	*wq_obj = resp.wq_obj;
664 	wq_spec->queue_index = resp.wq_id;
665 	cq_spec->queue_index = resp.cq_id;
666 
667 	return 0;
668 out:
669 	return err;
670 }
671 
672 static void mana_destroy_wq_obj(struct mana_port_context *apc, u32 wq_type,
673 				mana_handle_t wq_obj)
674 {
675 	struct mana_destroy_wqobj_resp resp = {};
676 	struct mana_destroy_wqobj_req req = {};
677 	struct net_device *ndev = apc->ndev;
678 	int err;
679 
680 	mana_gd_init_req_hdr(&req.hdr, MANA_DESTROY_WQ_OBJ,
681 			     sizeof(req), sizeof(resp));
682 	req.wq_type = wq_type;
683 	req.wq_obj_handle = wq_obj;
684 
685 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
686 				sizeof(resp));
687 	if (err) {
688 		netdev_err(ndev, "Failed to destroy WQ object: %d\n", err);
689 		return;
690 	}
691 
692 	err = mana_verify_resp_hdr(&resp.hdr, MANA_DESTROY_WQ_OBJ,
693 				   sizeof(resp));
694 	if (err || resp.hdr.status)
695 		netdev_err(ndev, "Failed to destroy WQ object: %d, 0x%x\n", err,
696 			   resp.hdr.status);
697 }
698 
699 static void mana_destroy_eq(struct mana_context *ac)
700 {
701 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
702 	struct gdma_queue *eq;
703 	int i;
704 
705 	if (!ac->eqs)
706 		return;
707 
708 	for (i = 0; i < gc->max_num_queues; i++) {
709 		eq = ac->eqs[i].eq;
710 		if (!eq)
711 			continue;
712 
713 		mana_gd_destroy_queue(gc, eq);
714 	}
715 
716 	kfree(ac->eqs);
717 	ac->eqs = NULL;
718 }
719 
720 static int mana_create_eq(struct mana_context *ac)
721 {
722 	struct gdma_dev *gd = ac->gdma_dev;
723 	struct gdma_context *gc = gd->gdma_context;
724 	struct gdma_queue_spec spec = {};
725 	int err;
726 	int i;
727 
728 	ac->eqs = kcalloc(gc->max_num_queues, sizeof(struct mana_eq),
729 			  GFP_KERNEL);
730 	if (!ac->eqs)
731 		return -ENOMEM;
732 
733 	spec.type = GDMA_EQ;
734 	spec.monitor_avl_buf = false;
735 	spec.queue_size = EQ_SIZE;
736 	spec.eq.callback = NULL;
737 	spec.eq.context = ac->eqs;
738 	spec.eq.log2_throttle_limit = LOG2_EQ_THROTTLE;
739 
740 	for (i = 0; i < gc->max_num_queues; i++) {
741 		err = mana_gd_create_mana_eq(gd, &spec, &ac->eqs[i].eq);
742 		if (err)
743 			goto out;
744 	}
745 
746 	return 0;
747 out:
748 	mana_destroy_eq(ac);
749 	return err;
750 }
751 
752 static int mana_move_wq_tail(struct gdma_queue *wq, u32 num_units)
753 {
754 	u32 used_space_old;
755 	u32 used_space_new;
756 
757 	used_space_old = wq->head - wq->tail;
758 	used_space_new = wq->head - (wq->tail + num_units);
759 
760 	if (WARN_ON_ONCE(used_space_new > used_space_old))
761 		return -ERANGE;
762 
763 	wq->tail += num_units;
764 	return 0;
765 }
766 
767 static void mana_unmap_skb(struct sk_buff *skb, struct mana_port_context *apc)
768 {
769 	struct mana_skb_head *ash = (struct mana_skb_head *)skb->head;
770 	struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
771 	struct device *dev = gc->dev;
772 	int i;
773 
774 	dma_unmap_single(dev, ash->dma_handle[0], ash->size[0], DMA_TO_DEVICE);
775 
776 	for (i = 1; i < skb_shinfo(skb)->nr_frags + 1; i++)
777 		dma_unmap_page(dev, ash->dma_handle[i], ash->size[i],
778 			       DMA_TO_DEVICE);
779 }
780 
781 static void mana_poll_tx_cq(struct mana_cq *cq)
782 {
783 	struct gdma_comp *completions = cq->gdma_comp_buf;
784 	struct gdma_posted_wqe_info *wqe_info;
785 	unsigned int pkt_transmitted = 0;
786 	unsigned int wqe_unit_cnt = 0;
787 	struct mana_txq *txq = cq->txq;
788 	struct mana_port_context *apc;
789 	struct netdev_queue *net_txq;
790 	struct gdma_queue *gdma_wq;
791 	unsigned int avail_space;
792 	struct net_device *ndev;
793 	struct sk_buff *skb;
794 	bool txq_stopped;
795 	int comp_read;
796 	int i;
797 
798 	ndev = txq->ndev;
799 	apc = netdev_priv(ndev);
800 
801 	comp_read = mana_gd_poll_cq(cq->gdma_cq, completions,
802 				    CQE_POLLING_BUFFER);
803 
804 	if (comp_read < 1)
805 		return;
806 
807 	for (i = 0; i < comp_read; i++) {
808 		struct mana_tx_comp_oob *cqe_oob;
809 
810 		if (WARN_ON_ONCE(!completions[i].is_sq))
811 			return;
812 
813 		cqe_oob = (struct mana_tx_comp_oob *)completions[i].cqe_data;
814 		if (WARN_ON_ONCE(cqe_oob->cqe_hdr.client_type !=
815 				 MANA_CQE_COMPLETION))
816 			return;
817 
818 		switch (cqe_oob->cqe_hdr.cqe_type) {
819 		case CQE_TX_OKAY:
820 			break;
821 
822 		case CQE_TX_SA_DROP:
823 		case CQE_TX_MTU_DROP:
824 		case CQE_TX_INVALID_OOB:
825 		case CQE_TX_INVALID_ETH_TYPE:
826 		case CQE_TX_HDR_PROCESSING_ERROR:
827 		case CQE_TX_VF_DISABLED:
828 		case CQE_TX_VPORT_IDX_OUT_OF_RANGE:
829 		case CQE_TX_VPORT_DISABLED:
830 		case CQE_TX_VLAN_TAGGING_VIOLATION:
831 			WARN_ONCE(1, "TX: CQE error %d: ignored.\n",
832 				  cqe_oob->cqe_hdr.cqe_type);
833 			break;
834 
835 		default:
836 			/* If the CQE type is unexpected, log an error, assert,
837 			 * and go through the error path.
838 			 */
839 			WARN_ONCE(1, "TX: Unexpected CQE type %d: HW BUG?\n",
840 				  cqe_oob->cqe_hdr.cqe_type);
841 			return;
842 		}
843 
844 		if (WARN_ON_ONCE(txq->gdma_txq_id != completions[i].wq_num))
845 			return;
846 
847 		skb = skb_dequeue(&txq->pending_skbs);
848 		if (WARN_ON_ONCE(!skb))
849 			return;
850 
851 		wqe_info = (struct gdma_posted_wqe_info *)skb->cb;
852 		wqe_unit_cnt += wqe_info->wqe_size_in_bu;
853 
854 		mana_unmap_skb(skb, apc);
855 
856 		napi_consume_skb(skb, cq->budget);
857 
858 		pkt_transmitted++;
859 	}
860 
861 	if (WARN_ON_ONCE(wqe_unit_cnt == 0))
862 		return;
863 
864 	mana_move_wq_tail(txq->gdma_sq, wqe_unit_cnt);
865 
866 	gdma_wq = txq->gdma_sq;
867 	avail_space = mana_gd_wq_avail_space(gdma_wq);
868 
869 	/* Ensure tail updated before checking q stop */
870 	smp_mb();
871 
872 	net_txq = txq->net_txq;
873 	txq_stopped = netif_tx_queue_stopped(net_txq);
874 
875 	/* Ensure checking txq_stopped before apc->port_is_up. */
876 	smp_rmb();
877 
878 	if (txq_stopped && apc->port_is_up && avail_space >= MAX_TX_WQE_SIZE) {
879 		netif_tx_wake_queue(net_txq);
880 		apc->eth_stats.wake_queue++;
881 	}
882 
883 	if (atomic_sub_return(pkt_transmitted, &txq->pending_sends) < 0)
884 		WARN_ON_ONCE(1);
885 
886 	cq->work_done = pkt_transmitted;
887 }
888 
889 static void mana_post_pkt_rxq(struct mana_rxq *rxq)
890 {
891 	struct mana_recv_buf_oob *recv_buf_oob;
892 	u32 curr_index;
893 	int err;
894 
895 	curr_index = rxq->buf_index++;
896 	if (rxq->buf_index == rxq->num_rx_buf)
897 		rxq->buf_index = 0;
898 
899 	recv_buf_oob = &rxq->rx_oobs[curr_index];
900 
901 	err = mana_gd_post_and_ring(rxq->gdma_rq, &recv_buf_oob->wqe_req,
902 				    &recv_buf_oob->wqe_inf);
903 	if (WARN_ON_ONCE(err))
904 		return;
905 
906 	WARN_ON_ONCE(recv_buf_oob->wqe_inf.wqe_size_in_bu != 1);
907 }
908 
909 static void mana_rx_skb(void *buf_va, struct mana_rxcomp_oob *cqe,
910 			struct mana_rxq *rxq)
911 {
912 	struct mana_stats *rx_stats = &rxq->stats;
913 	struct net_device *ndev = rxq->ndev;
914 	uint pkt_len = cqe->ppi[0].pkt_len;
915 	u16 rxq_idx = rxq->rxq_idx;
916 	struct napi_struct *napi;
917 	struct sk_buff *skb;
918 	u32 hash_value;
919 
920 	rxq->rx_cq.work_done++;
921 	napi = &rxq->rx_cq.napi;
922 
923 	if (!buf_va) {
924 		++ndev->stats.rx_dropped;
925 		return;
926 	}
927 
928 	skb = build_skb(buf_va, PAGE_SIZE);
929 
930 	if (!skb) {
931 		free_page((unsigned long)buf_va);
932 		++ndev->stats.rx_dropped;
933 		return;
934 	}
935 
936 	skb_put(skb, pkt_len);
937 	skb->dev = napi->dev;
938 
939 	skb->protocol = eth_type_trans(skb, ndev);
940 	skb_checksum_none_assert(skb);
941 	skb_record_rx_queue(skb, rxq_idx);
942 
943 	if ((ndev->features & NETIF_F_RXCSUM) && cqe->rx_iphdr_csum_succeed) {
944 		if (cqe->rx_tcp_csum_succeed || cqe->rx_udp_csum_succeed)
945 			skb->ip_summed = CHECKSUM_UNNECESSARY;
946 	}
947 
948 	if (cqe->rx_hashtype != 0 && (ndev->features & NETIF_F_RXHASH)) {
949 		hash_value = cqe->ppi[0].pkt_hash;
950 
951 		if (cqe->rx_hashtype & MANA_HASH_L4)
952 			skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L4);
953 		else
954 			skb_set_hash(skb, hash_value, PKT_HASH_TYPE_L3);
955 	}
956 
957 	napi_gro_receive(napi, skb);
958 
959 	u64_stats_update_begin(&rx_stats->syncp);
960 	rx_stats->packets++;
961 	rx_stats->bytes += pkt_len;
962 	u64_stats_update_end(&rx_stats->syncp);
963 }
964 
965 static void mana_process_rx_cqe(struct mana_rxq *rxq, struct mana_cq *cq,
966 				struct gdma_comp *cqe)
967 {
968 	struct mana_rxcomp_oob *oob = (struct mana_rxcomp_oob *)cqe->cqe_data;
969 	struct gdma_context *gc = rxq->gdma_rq->gdma_dev->gdma_context;
970 	struct net_device *ndev = rxq->ndev;
971 	struct mana_recv_buf_oob *rxbuf_oob;
972 	struct device *dev = gc->dev;
973 	void *new_buf, *old_buf;
974 	struct page *new_page;
975 	u32 curr, pktlen;
976 	dma_addr_t da;
977 
978 	switch (oob->cqe_hdr.cqe_type) {
979 	case CQE_RX_OKAY:
980 		break;
981 
982 	case CQE_RX_TRUNCATED:
983 		netdev_err(ndev, "Dropped a truncated packet\n");
984 		return;
985 
986 	case CQE_RX_COALESCED_4:
987 		netdev_err(ndev, "RX coalescing is unsupported\n");
988 		return;
989 
990 	case CQE_RX_OBJECT_FENCE:
991 		netdev_err(ndev, "RX Fencing is unsupported\n");
992 		return;
993 
994 	default:
995 		netdev_err(ndev, "Unknown RX CQE type = %d\n",
996 			   oob->cqe_hdr.cqe_type);
997 		return;
998 	}
999 
1000 	if (oob->cqe_hdr.cqe_type != CQE_RX_OKAY)
1001 		return;
1002 
1003 	pktlen = oob->ppi[0].pkt_len;
1004 
1005 	if (pktlen == 0) {
1006 		/* data packets should never have packetlength of zero */
1007 		netdev_err(ndev, "RX pkt len=0, rq=%u, cq=%u, rxobj=0x%llx\n",
1008 			   rxq->gdma_id, cq->gdma_id, rxq->rxobj);
1009 		return;
1010 	}
1011 
1012 	curr = rxq->buf_index;
1013 	rxbuf_oob = &rxq->rx_oobs[curr];
1014 	WARN_ON_ONCE(rxbuf_oob->wqe_inf.wqe_size_in_bu != 1);
1015 
1016 	new_page = alloc_page(GFP_ATOMIC);
1017 
1018 	if (new_page) {
1019 		da = dma_map_page(dev, new_page, 0, rxq->datasize,
1020 				  DMA_FROM_DEVICE);
1021 
1022 		if (dma_mapping_error(dev, da)) {
1023 			__free_page(new_page);
1024 			new_page = NULL;
1025 		}
1026 	}
1027 
1028 	new_buf = new_page ? page_to_virt(new_page) : NULL;
1029 
1030 	if (new_buf) {
1031 		dma_unmap_page(dev, rxbuf_oob->buf_dma_addr, rxq->datasize,
1032 			       DMA_FROM_DEVICE);
1033 
1034 		old_buf = rxbuf_oob->buf_va;
1035 
1036 		/* refresh the rxbuf_oob with the new page */
1037 		rxbuf_oob->buf_va = new_buf;
1038 		rxbuf_oob->buf_dma_addr = da;
1039 		rxbuf_oob->sgl[0].address = rxbuf_oob->buf_dma_addr;
1040 	} else {
1041 		old_buf = NULL; /* drop the packet if no memory */
1042 	}
1043 
1044 	mana_rx_skb(old_buf, oob, rxq);
1045 
1046 	mana_move_wq_tail(rxq->gdma_rq, rxbuf_oob->wqe_inf.wqe_size_in_bu);
1047 
1048 	mana_post_pkt_rxq(rxq);
1049 }
1050 
1051 static void mana_poll_rx_cq(struct mana_cq *cq)
1052 {
1053 	struct gdma_comp *comp = cq->gdma_comp_buf;
1054 	int comp_read, i;
1055 
1056 	comp_read = mana_gd_poll_cq(cq->gdma_cq, comp, CQE_POLLING_BUFFER);
1057 	WARN_ON_ONCE(comp_read > CQE_POLLING_BUFFER);
1058 
1059 	for (i = 0; i < comp_read; i++) {
1060 		if (WARN_ON_ONCE(comp[i].is_sq))
1061 			return;
1062 
1063 		/* verify recv cqe references the right rxq */
1064 		if (WARN_ON_ONCE(comp[i].wq_num != cq->rxq->gdma_id))
1065 			return;
1066 
1067 		mana_process_rx_cqe(cq->rxq, cq, &comp[i]);
1068 	}
1069 }
1070 
1071 static void mana_cq_handler(void *context, struct gdma_queue *gdma_queue)
1072 {
1073 	struct mana_cq *cq = context;
1074 	u8 arm_bit;
1075 
1076 	WARN_ON_ONCE(cq->gdma_cq != gdma_queue);
1077 
1078 	if (cq->type == MANA_CQ_TYPE_RX)
1079 		mana_poll_rx_cq(cq);
1080 	else
1081 		mana_poll_tx_cq(cq);
1082 
1083 	if (cq->work_done < cq->budget &&
1084 	    napi_complete_done(&cq->napi, cq->work_done)) {
1085 		arm_bit = SET_ARM_BIT;
1086 	} else {
1087 		arm_bit = 0;
1088 	}
1089 
1090 	mana_gd_ring_cq(gdma_queue, arm_bit);
1091 }
1092 
1093 static int mana_poll(struct napi_struct *napi, int budget)
1094 {
1095 	struct mana_cq *cq = container_of(napi, struct mana_cq, napi);
1096 
1097 	cq->work_done = 0;
1098 	cq->budget = budget;
1099 
1100 	mana_cq_handler(cq, cq->gdma_cq);
1101 
1102 	return min(cq->work_done, budget);
1103 }
1104 
1105 static void mana_schedule_napi(void *context, struct gdma_queue *gdma_queue)
1106 {
1107 	struct mana_cq *cq = context;
1108 
1109 	napi_schedule_irqoff(&cq->napi);
1110 }
1111 
1112 static void mana_deinit_cq(struct mana_port_context *apc, struct mana_cq *cq)
1113 {
1114 	struct gdma_dev *gd = apc->ac->gdma_dev;
1115 
1116 	if (!cq->gdma_cq)
1117 		return;
1118 
1119 	mana_gd_destroy_queue(gd->gdma_context, cq->gdma_cq);
1120 }
1121 
1122 static void mana_deinit_txq(struct mana_port_context *apc, struct mana_txq *txq)
1123 {
1124 	struct gdma_dev *gd = apc->ac->gdma_dev;
1125 
1126 	if (!txq->gdma_sq)
1127 		return;
1128 
1129 	mana_gd_destroy_queue(gd->gdma_context, txq->gdma_sq);
1130 }
1131 
1132 static void mana_destroy_txq(struct mana_port_context *apc)
1133 {
1134 	struct napi_struct *napi;
1135 	int i;
1136 
1137 	if (!apc->tx_qp)
1138 		return;
1139 
1140 	for (i = 0; i < apc->num_queues; i++) {
1141 		napi = &apc->tx_qp[i].tx_cq.napi;
1142 		napi_synchronize(napi);
1143 		napi_disable(napi);
1144 		netif_napi_del(napi);
1145 
1146 		mana_destroy_wq_obj(apc, GDMA_SQ, apc->tx_qp[i].tx_object);
1147 
1148 		mana_deinit_cq(apc, &apc->tx_qp[i].tx_cq);
1149 
1150 		mana_deinit_txq(apc, &apc->tx_qp[i].txq);
1151 	}
1152 
1153 	kfree(apc->tx_qp);
1154 	apc->tx_qp = NULL;
1155 }
1156 
1157 static int mana_create_txq(struct mana_port_context *apc,
1158 			   struct net_device *net)
1159 {
1160 	struct mana_context *ac = apc->ac;
1161 	struct gdma_dev *gd = ac->gdma_dev;
1162 	struct mana_obj_spec wq_spec;
1163 	struct mana_obj_spec cq_spec;
1164 	struct gdma_queue_spec spec;
1165 	struct gdma_context *gc;
1166 	struct mana_txq *txq;
1167 	struct mana_cq *cq;
1168 	u32 txq_size;
1169 	u32 cq_size;
1170 	int err;
1171 	int i;
1172 
1173 	apc->tx_qp = kcalloc(apc->num_queues, sizeof(struct mana_tx_qp),
1174 			     GFP_KERNEL);
1175 	if (!apc->tx_qp)
1176 		return -ENOMEM;
1177 
1178 	/*  The minimum size of the WQE is 32 bytes, hence
1179 	 *  MAX_SEND_BUFFERS_PER_QUEUE represents the maximum number of WQEs
1180 	 *  the SQ can store. This value is then used to size other queues
1181 	 *  to prevent overflow.
1182 	 */
1183 	txq_size = MAX_SEND_BUFFERS_PER_QUEUE * 32;
1184 	BUILD_BUG_ON(!PAGE_ALIGNED(txq_size));
1185 
1186 	cq_size = MAX_SEND_BUFFERS_PER_QUEUE * COMP_ENTRY_SIZE;
1187 	cq_size = PAGE_ALIGN(cq_size);
1188 
1189 	gc = gd->gdma_context;
1190 
1191 	for (i = 0; i < apc->num_queues; i++) {
1192 		apc->tx_qp[i].tx_object = INVALID_MANA_HANDLE;
1193 
1194 		/* Create SQ */
1195 		txq = &apc->tx_qp[i].txq;
1196 
1197 		u64_stats_init(&txq->stats.syncp);
1198 		txq->ndev = net;
1199 		txq->net_txq = netdev_get_tx_queue(net, i);
1200 		txq->vp_offset = apc->tx_vp_offset;
1201 		skb_queue_head_init(&txq->pending_skbs);
1202 
1203 		memset(&spec, 0, sizeof(spec));
1204 		spec.type = GDMA_SQ;
1205 		spec.monitor_avl_buf = true;
1206 		spec.queue_size = txq_size;
1207 		err = mana_gd_create_mana_wq_cq(gd, &spec, &txq->gdma_sq);
1208 		if (err)
1209 			goto out;
1210 
1211 		/* Create SQ's CQ */
1212 		cq = &apc->tx_qp[i].tx_cq;
1213 		cq->type = MANA_CQ_TYPE_TX;
1214 
1215 		cq->txq = txq;
1216 
1217 		memset(&spec, 0, sizeof(spec));
1218 		spec.type = GDMA_CQ;
1219 		spec.monitor_avl_buf = false;
1220 		spec.queue_size = cq_size;
1221 		spec.cq.callback = mana_schedule_napi;
1222 		spec.cq.parent_eq = ac->eqs[i].eq;
1223 		spec.cq.context = cq;
1224 		err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq);
1225 		if (err)
1226 			goto out;
1227 
1228 		memset(&wq_spec, 0, sizeof(wq_spec));
1229 		memset(&cq_spec, 0, sizeof(cq_spec));
1230 
1231 		wq_spec.gdma_region = txq->gdma_sq->mem_info.gdma_region;
1232 		wq_spec.queue_size = txq->gdma_sq->queue_size;
1233 
1234 		cq_spec.gdma_region = cq->gdma_cq->mem_info.gdma_region;
1235 		cq_spec.queue_size = cq->gdma_cq->queue_size;
1236 		cq_spec.modr_ctx_id = 0;
1237 		cq_spec.attached_eq = cq->gdma_cq->cq.parent->id;
1238 
1239 		err = mana_create_wq_obj(apc, apc->port_handle, GDMA_SQ,
1240 					 &wq_spec, &cq_spec,
1241 					 &apc->tx_qp[i].tx_object);
1242 
1243 		if (err)
1244 			goto out;
1245 
1246 		txq->gdma_sq->id = wq_spec.queue_index;
1247 		cq->gdma_cq->id = cq_spec.queue_index;
1248 
1249 		txq->gdma_sq->mem_info.gdma_region = GDMA_INVALID_DMA_REGION;
1250 		cq->gdma_cq->mem_info.gdma_region = GDMA_INVALID_DMA_REGION;
1251 
1252 		txq->gdma_txq_id = txq->gdma_sq->id;
1253 
1254 		cq->gdma_id = cq->gdma_cq->id;
1255 
1256 		if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) {
1257 			err = -EINVAL;
1258 			goto out;
1259 		}
1260 
1261 		gc->cq_table[cq->gdma_id] = cq->gdma_cq;
1262 
1263 		netif_tx_napi_add(net, &cq->napi, mana_poll, NAPI_POLL_WEIGHT);
1264 		napi_enable(&cq->napi);
1265 
1266 		mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT);
1267 	}
1268 
1269 	return 0;
1270 out:
1271 	mana_destroy_txq(apc);
1272 	return err;
1273 }
1274 
1275 static void mana_destroy_rxq(struct mana_port_context *apc,
1276 			     struct mana_rxq *rxq, bool validate_state)
1277 
1278 {
1279 	struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1280 	struct mana_recv_buf_oob *rx_oob;
1281 	struct device *dev = gc->dev;
1282 	struct napi_struct *napi;
1283 	int i;
1284 
1285 	if (!rxq)
1286 		return;
1287 
1288 	napi = &rxq->rx_cq.napi;
1289 
1290 	if (validate_state)
1291 		napi_synchronize(napi);
1292 
1293 	napi_disable(napi);
1294 	netif_napi_del(napi);
1295 
1296 	mana_destroy_wq_obj(apc, GDMA_RQ, rxq->rxobj);
1297 
1298 	mana_deinit_cq(apc, &rxq->rx_cq);
1299 
1300 	for (i = 0; i < rxq->num_rx_buf; i++) {
1301 		rx_oob = &rxq->rx_oobs[i];
1302 
1303 		if (!rx_oob->buf_va)
1304 			continue;
1305 
1306 		dma_unmap_page(dev, rx_oob->buf_dma_addr, rxq->datasize,
1307 			       DMA_FROM_DEVICE);
1308 
1309 		free_page((unsigned long)rx_oob->buf_va);
1310 		rx_oob->buf_va = NULL;
1311 	}
1312 
1313 	if (rxq->gdma_rq)
1314 		mana_gd_destroy_queue(gc, rxq->gdma_rq);
1315 
1316 	kfree(rxq);
1317 }
1318 
1319 #define MANA_WQE_HEADER_SIZE 16
1320 #define MANA_WQE_SGE_SIZE 16
1321 
1322 static int mana_alloc_rx_wqe(struct mana_port_context *apc,
1323 			     struct mana_rxq *rxq, u32 *rxq_size, u32 *cq_size)
1324 {
1325 	struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1326 	struct mana_recv_buf_oob *rx_oob;
1327 	struct device *dev = gc->dev;
1328 	struct page *page;
1329 	dma_addr_t da;
1330 	u32 buf_idx;
1331 
1332 	WARN_ON(rxq->datasize == 0 || rxq->datasize > PAGE_SIZE);
1333 
1334 	*rxq_size = 0;
1335 	*cq_size = 0;
1336 
1337 	for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) {
1338 		rx_oob = &rxq->rx_oobs[buf_idx];
1339 		memset(rx_oob, 0, sizeof(*rx_oob));
1340 
1341 		page = alloc_page(GFP_KERNEL);
1342 		if (!page)
1343 			return -ENOMEM;
1344 
1345 		da = dma_map_page(dev, page, 0, rxq->datasize, DMA_FROM_DEVICE);
1346 
1347 		if (dma_mapping_error(dev, da)) {
1348 			__free_page(page);
1349 			return -ENOMEM;
1350 		}
1351 
1352 		rx_oob->buf_va = page_to_virt(page);
1353 		rx_oob->buf_dma_addr = da;
1354 
1355 		rx_oob->num_sge = 1;
1356 		rx_oob->sgl[0].address = rx_oob->buf_dma_addr;
1357 		rx_oob->sgl[0].size = rxq->datasize;
1358 		rx_oob->sgl[0].mem_key = apc->ac->gdma_dev->gpa_mkey;
1359 
1360 		rx_oob->wqe_req.sgl = rx_oob->sgl;
1361 		rx_oob->wqe_req.num_sge = rx_oob->num_sge;
1362 		rx_oob->wqe_req.inline_oob_size = 0;
1363 		rx_oob->wqe_req.inline_oob_data = NULL;
1364 		rx_oob->wqe_req.flags = 0;
1365 		rx_oob->wqe_req.client_data_unit = 0;
1366 
1367 		*rxq_size += ALIGN(MANA_WQE_HEADER_SIZE +
1368 				   MANA_WQE_SGE_SIZE * rx_oob->num_sge, 32);
1369 		*cq_size += COMP_ENTRY_SIZE;
1370 	}
1371 
1372 	return 0;
1373 }
1374 
1375 static int mana_push_wqe(struct mana_rxq *rxq)
1376 {
1377 	struct mana_recv_buf_oob *rx_oob;
1378 	u32 buf_idx;
1379 	int err;
1380 
1381 	for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) {
1382 		rx_oob = &rxq->rx_oobs[buf_idx];
1383 
1384 		err = mana_gd_post_and_ring(rxq->gdma_rq, &rx_oob->wqe_req,
1385 					    &rx_oob->wqe_inf);
1386 		if (err)
1387 			return -ENOSPC;
1388 	}
1389 
1390 	return 0;
1391 }
1392 
1393 static struct mana_rxq *mana_create_rxq(struct mana_port_context *apc,
1394 					u32 rxq_idx, struct mana_eq *eq,
1395 					struct net_device *ndev)
1396 {
1397 	struct gdma_dev *gd = apc->ac->gdma_dev;
1398 	struct mana_obj_spec wq_spec;
1399 	struct mana_obj_spec cq_spec;
1400 	struct gdma_queue_spec spec;
1401 	struct mana_cq *cq = NULL;
1402 	struct gdma_context *gc;
1403 	u32 cq_size, rq_size;
1404 	struct mana_rxq *rxq;
1405 	int err;
1406 
1407 	gc = gd->gdma_context;
1408 
1409 	rxq = kzalloc(struct_size(rxq, rx_oobs, RX_BUFFERS_PER_QUEUE),
1410 		      GFP_KERNEL);
1411 	if (!rxq)
1412 		return NULL;
1413 
1414 	rxq->ndev = ndev;
1415 	rxq->num_rx_buf = RX_BUFFERS_PER_QUEUE;
1416 	rxq->rxq_idx = rxq_idx;
1417 	rxq->datasize = ALIGN(MAX_FRAME_SIZE, 64);
1418 	rxq->rxobj = INVALID_MANA_HANDLE;
1419 
1420 	err = mana_alloc_rx_wqe(apc, rxq, &rq_size, &cq_size);
1421 	if (err)
1422 		goto out;
1423 
1424 	rq_size = PAGE_ALIGN(rq_size);
1425 	cq_size = PAGE_ALIGN(cq_size);
1426 
1427 	/* Create RQ */
1428 	memset(&spec, 0, sizeof(spec));
1429 	spec.type = GDMA_RQ;
1430 	spec.monitor_avl_buf = true;
1431 	spec.queue_size = rq_size;
1432 	err = mana_gd_create_mana_wq_cq(gd, &spec, &rxq->gdma_rq);
1433 	if (err)
1434 		goto out;
1435 
1436 	/* Create RQ's CQ */
1437 	cq = &rxq->rx_cq;
1438 	cq->type = MANA_CQ_TYPE_RX;
1439 	cq->rxq = rxq;
1440 
1441 	memset(&spec, 0, sizeof(spec));
1442 	spec.type = GDMA_CQ;
1443 	spec.monitor_avl_buf = false;
1444 	spec.queue_size = cq_size;
1445 	spec.cq.callback = mana_schedule_napi;
1446 	spec.cq.parent_eq = eq->eq;
1447 	spec.cq.context = cq;
1448 	err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq);
1449 	if (err)
1450 		goto out;
1451 
1452 	memset(&wq_spec, 0, sizeof(wq_spec));
1453 	memset(&cq_spec, 0, sizeof(cq_spec));
1454 	wq_spec.gdma_region = rxq->gdma_rq->mem_info.gdma_region;
1455 	wq_spec.queue_size = rxq->gdma_rq->queue_size;
1456 
1457 	cq_spec.gdma_region = cq->gdma_cq->mem_info.gdma_region;
1458 	cq_spec.queue_size = cq->gdma_cq->queue_size;
1459 	cq_spec.modr_ctx_id = 0;
1460 	cq_spec.attached_eq = cq->gdma_cq->cq.parent->id;
1461 
1462 	err = mana_create_wq_obj(apc, apc->port_handle, GDMA_RQ,
1463 				 &wq_spec, &cq_spec, &rxq->rxobj);
1464 	if (err)
1465 		goto out;
1466 
1467 	rxq->gdma_rq->id = wq_spec.queue_index;
1468 	cq->gdma_cq->id = cq_spec.queue_index;
1469 
1470 	rxq->gdma_rq->mem_info.gdma_region = GDMA_INVALID_DMA_REGION;
1471 	cq->gdma_cq->mem_info.gdma_region = GDMA_INVALID_DMA_REGION;
1472 
1473 	rxq->gdma_id = rxq->gdma_rq->id;
1474 	cq->gdma_id = cq->gdma_cq->id;
1475 
1476 	err = mana_push_wqe(rxq);
1477 	if (err)
1478 		goto out;
1479 
1480 	if (cq->gdma_id >= gc->max_num_cqs)
1481 		goto out;
1482 
1483 	gc->cq_table[cq->gdma_id] = cq->gdma_cq;
1484 
1485 	netif_napi_add(ndev, &cq->napi, mana_poll, 1);
1486 	napi_enable(&cq->napi);
1487 
1488 	mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT);
1489 out:
1490 	if (!err)
1491 		return rxq;
1492 
1493 	netdev_err(ndev, "Failed to create RXQ: err = %d\n", err);
1494 
1495 	mana_destroy_rxq(apc, rxq, false);
1496 
1497 	if (cq)
1498 		mana_deinit_cq(apc, cq);
1499 
1500 	return NULL;
1501 }
1502 
1503 static int mana_add_rx_queues(struct mana_port_context *apc,
1504 			      struct net_device *ndev)
1505 {
1506 	struct mana_context *ac = apc->ac;
1507 	struct mana_rxq *rxq;
1508 	int err = 0;
1509 	int i;
1510 
1511 	for (i = 0; i < apc->num_queues; i++) {
1512 		rxq = mana_create_rxq(apc, i, &ac->eqs[i], ndev);
1513 		if (!rxq) {
1514 			err = -ENOMEM;
1515 			goto out;
1516 		}
1517 
1518 		u64_stats_init(&rxq->stats.syncp);
1519 
1520 		apc->rxqs[i] = rxq;
1521 	}
1522 
1523 	apc->default_rxobj = apc->rxqs[0]->rxobj;
1524 out:
1525 	return err;
1526 }
1527 
1528 static void mana_destroy_vport(struct mana_port_context *apc)
1529 {
1530 	struct mana_rxq *rxq;
1531 	u32 rxq_idx;
1532 
1533 	for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) {
1534 		rxq = apc->rxqs[rxq_idx];
1535 		if (!rxq)
1536 			continue;
1537 
1538 		mana_destroy_rxq(apc, rxq, true);
1539 		apc->rxqs[rxq_idx] = NULL;
1540 	}
1541 
1542 	mana_destroy_txq(apc);
1543 }
1544 
1545 static int mana_create_vport(struct mana_port_context *apc,
1546 			     struct net_device *net)
1547 {
1548 	struct gdma_dev *gd = apc->ac->gdma_dev;
1549 	int err;
1550 
1551 	apc->default_rxobj = INVALID_MANA_HANDLE;
1552 
1553 	err = mana_cfg_vport(apc, gd->pdid, gd->doorbell);
1554 	if (err)
1555 		return err;
1556 
1557 	return mana_create_txq(apc, net);
1558 }
1559 
1560 static void mana_rss_table_init(struct mana_port_context *apc)
1561 {
1562 	int i;
1563 
1564 	for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++)
1565 		apc->indir_table[i] =
1566 			ethtool_rxfh_indir_default(i, apc->num_queues);
1567 }
1568 
1569 int mana_config_rss(struct mana_port_context *apc, enum TRI_STATE rx,
1570 		    bool update_hash, bool update_tab)
1571 {
1572 	u32 queue_idx;
1573 	int i;
1574 
1575 	if (update_tab) {
1576 		for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) {
1577 			queue_idx = apc->indir_table[i];
1578 			apc->rxobj_table[i] = apc->rxqs[queue_idx]->rxobj;
1579 		}
1580 	}
1581 
1582 	return mana_cfg_vport_steering(apc, rx, true, update_hash, update_tab);
1583 }
1584 
1585 static int mana_init_port(struct net_device *ndev)
1586 {
1587 	struct mana_port_context *apc = netdev_priv(ndev);
1588 	u32 max_txq, max_rxq, max_queues;
1589 	int port_idx = apc->port_idx;
1590 	u32 num_indirect_entries;
1591 	int err;
1592 
1593 	err = mana_init_port_context(apc);
1594 	if (err)
1595 		return err;
1596 
1597 	err = mana_query_vport_cfg(apc, port_idx, &max_txq, &max_rxq,
1598 				   &num_indirect_entries);
1599 	if (err) {
1600 		netdev_err(ndev, "Failed to query info for vPort 0\n");
1601 		goto reset_apc;
1602 	}
1603 
1604 	max_queues = min_t(u32, max_txq, max_rxq);
1605 	if (apc->max_queues > max_queues)
1606 		apc->max_queues = max_queues;
1607 
1608 	if (apc->num_queues > apc->max_queues)
1609 		apc->num_queues = apc->max_queues;
1610 
1611 	ether_addr_copy(ndev->dev_addr, apc->mac_addr);
1612 
1613 	return 0;
1614 
1615 reset_apc:
1616 	kfree(apc->rxqs);
1617 	apc->rxqs = NULL;
1618 	return err;
1619 }
1620 
1621 int mana_alloc_queues(struct net_device *ndev)
1622 {
1623 	struct mana_port_context *apc = netdev_priv(ndev);
1624 	int err;
1625 
1626 	err = mana_create_vport(apc, ndev);
1627 	if (err)
1628 		return err;
1629 
1630 	err = netif_set_real_num_tx_queues(ndev, apc->num_queues);
1631 	if (err)
1632 		goto destroy_vport;
1633 
1634 	err = mana_add_rx_queues(apc, ndev);
1635 	if (err)
1636 		goto destroy_vport;
1637 
1638 	apc->rss_state = apc->num_queues > 1 ? TRI_STATE_TRUE : TRI_STATE_FALSE;
1639 
1640 	err = netif_set_real_num_rx_queues(ndev, apc->num_queues);
1641 	if (err)
1642 		goto destroy_vport;
1643 
1644 	mana_rss_table_init(apc);
1645 
1646 	err = mana_config_rss(apc, TRI_STATE_TRUE, true, true);
1647 	if (err)
1648 		goto destroy_vport;
1649 
1650 	return 0;
1651 
1652 destroy_vport:
1653 	mana_destroy_vport(apc);
1654 	return err;
1655 }
1656 
1657 int mana_attach(struct net_device *ndev)
1658 {
1659 	struct mana_port_context *apc = netdev_priv(ndev);
1660 	int err;
1661 
1662 	ASSERT_RTNL();
1663 
1664 	err = mana_init_port(ndev);
1665 	if (err)
1666 		return err;
1667 
1668 	err = mana_alloc_queues(ndev);
1669 	if (err) {
1670 		kfree(apc->rxqs);
1671 		apc->rxqs = NULL;
1672 		return err;
1673 	}
1674 
1675 	netif_device_attach(ndev);
1676 
1677 	apc->port_is_up = apc->port_st_save;
1678 
1679 	/* Ensure port state updated before txq state */
1680 	smp_wmb();
1681 
1682 	if (apc->port_is_up) {
1683 		netif_carrier_on(ndev);
1684 		netif_tx_wake_all_queues(ndev);
1685 	}
1686 
1687 	return 0;
1688 }
1689 
1690 static int mana_dealloc_queues(struct net_device *ndev)
1691 {
1692 	struct mana_port_context *apc = netdev_priv(ndev);
1693 	struct mana_txq *txq;
1694 	int i, err;
1695 
1696 	if (apc->port_is_up)
1697 		return -EINVAL;
1698 
1699 	/* No packet can be transmitted now since apc->port_is_up is false.
1700 	 * There is still a tiny chance that mana_poll_tx_cq() can re-enable
1701 	 * a txq because it may not timely see apc->port_is_up being cleared
1702 	 * to false, but it doesn't matter since mana_start_xmit() drops any
1703 	 * new packets due to apc->port_is_up being false.
1704 	 *
1705 	 * Drain all the in-flight TX packets
1706 	 */
1707 	for (i = 0; i < apc->num_queues; i++) {
1708 		txq = &apc->tx_qp[i].txq;
1709 
1710 		while (atomic_read(&txq->pending_sends) > 0)
1711 			usleep_range(1000, 2000);
1712 	}
1713 
1714 	/* We're 100% sure the queues can no longer be woken up, because
1715 	 * we're sure now mana_poll_tx_cq() can't be running.
1716 	 */
1717 
1718 	apc->rss_state = TRI_STATE_FALSE;
1719 	err = mana_config_rss(apc, TRI_STATE_FALSE, false, false);
1720 	if (err) {
1721 		netdev_err(ndev, "Failed to disable vPort: %d\n", err);
1722 		return err;
1723 	}
1724 
1725 	/* TODO: Implement RX fencing */
1726 	ssleep(1);
1727 
1728 	mana_destroy_vport(apc);
1729 
1730 	return 0;
1731 }
1732 
1733 int mana_detach(struct net_device *ndev, bool from_close)
1734 {
1735 	struct mana_port_context *apc = netdev_priv(ndev);
1736 	int err;
1737 
1738 	ASSERT_RTNL();
1739 
1740 	apc->port_st_save = apc->port_is_up;
1741 	apc->port_is_up = false;
1742 
1743 	/* Ensure port state updated before txq state */
1744 	smp_wmb();
1745 
1746 	netif_tx_disable(ndev);
1747 	netif_carrier_off(ndev);
1748 
1749 	if (apc->port_st_save) {
1750 		err = mana_dealloc_queues(ndev);
1751 		if (err)
1752 			return err;
1753 	}
1754 
1755 	if (!from_close) {
1756 		netif_device_detach(ndev);
1757 		mana_cleanup_port_context(apc);
1758 	}
1759 
1760 	return 0;
1761 }
1762 
1763 static int mana_probe_port(struct mana_context *ac, int port_idx,
1764 			   struct net_device **ndev_storage)
1765 {
1766 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
1767 	struct mana_port_context *apc;
1768 	struct net_device *ndev;
1769 	int err;
1770 
1771 	ndev = alloc_etherdev_mq(sizeof(struct mana_port_context),
1772 				 gc->max_num_queues);
1773 	if (!ndev)
1774 		return -ENOMEM;
1775 
1776 	*ndev_storage = ndev;
1777 
1778 	apc = netdev_priv(ndev);
1779 	apc->ac = ac;
1780 	apc->ndev = ndev;
1781 	apc->max_queues = gc->max_num_queues;
1782 	apc->num_queues = gc->max_num_queues;
1783 	apc->port_handle = INVALID_MANA_HANDLE;
1784 	apc->port_idx = port_idx;
1785 
1786 	ndev->netdev_ops = &mana_devops;
1787 	ndev->ethtool_ops = &mana_ethtool_ops;
1788 	ndev->mtu = ETH_DATA_LEN;
1789 	ndev->max_mtu = ndev->mtu;
1790 	ndev->min_mtu = ndev->mtu;
1791 	ndev->needed_headroom = MANA_HEADROOM;
1792 	SET_NETDEV_DEV(ndev, gc->dev);
1793 
1794 	netif_carrier_off(ndev);
1795 
1796 	netdev_rss_key_fill(apc->hashkey, MANA_HASH_KEY_SIZE);
1797 
1798 	err = mana_init_port(ndev);
1799 	if (err)
1800 		goto free_net;
1801 
1802 	netdev_lockdep_set_classes(ndev);
1803 
1804 	ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1805 	ndev->hw_features |= NETIF_F_RXCSUM;
1806 	ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
1807 	ndev->hw_features |= NETIF_F_RXHASH;
1808 	ndev->features = ndev->hw_features;
1809 	ndev->vlan_features = 0;
1810 
1811 	err = register_netdev(ndev);
1812 	if (err) {
1813 		netdev_err(ndev, "Unable to register netdev.\n");
1814 		goto reset_apc;
1815 	}
1816 
1817 	return 0;
1818 
1819 reset_apc:
1820 	kfree(apc->rxqs);
1821 	apc->rxqs = NULL;
1822 free_net:
1823 	*ndev_storage = NULL;
1824 	netdev_err(ndev, "Failed to probe vPort %d: %d\n", port_idx, err);
1825 	free_netdev(ndev);
1826 	return err;
1827 }
1828 
1829 int mana_probe(struct gdma_dev *gd)
1830 {
1831 	struct gdma_context *gc = gd->gdma_context;
1832 	struct device *dev = gc->dev;
1833 	struct mana_context *ac;
1834 	int err;
1835 	int i;
1836 
1837 	dev_info(dev,
1838 		 "Microsoft Azure Network Adapter protocol version: %d.%d.%d\n",
1839 		 MANA_MAJOR_VERSION, MANA_MINOR_VERSION, MANA_MICRO_VERSION);
1840 
1841 	err = mana_gd_register_device(gd);
1842 	if (err)
1843 		return err;
1844 
1845 	ac = kzalloc(sizeof(*ac), GFP_KERNEL);
1846 	if (!ac)
1847 		return -ENOMEM;
1848 
1849 	ac->gdma_dev = gd;
1850 	ac->num_ports = 1;
1851 	gd->driver_data = ac;
1852 
1853 	err = mana_create_eq(ac);
1854 	if (err)
1855 		goto out;
1856 
1857 	err = mana_query_device_cfg(ac, MANA_MAJOR_VERSION, MANA_MINOR_VERSION,
1858 				    MANA_MICRO_VERSION, &ac->num_ports);
1859 	if (err)
1860 		goto out;
1861 
1862 	if (ac->num_ports > MAX_PORTS_IN_MANA_DEV)
1863 		ac->num_ports = MAX_PORTS_IN_MANA_DEV;
1864 
1865 	for (i = 0; i < ac->num_ports; i++) {
1866 		err = mana_probe_port(ac, i, &ac->ports[i]);
1867 		if (err)
1868 			break;
1869 	}
1870 out:
1871 	if (err)
1872 		mana_remove(gd);
1873 
1874 	return err;
1875 }
1876 
1877 void mana_remove(struct gdma_dev *gd)
1878 {
1879 	struct gdma_context *gc = gd->gdma_context;
1880 	struct mana_context *ac = gd->driver_data;
1881 	struct device *dev = gc->dev;
1882 	struct net_device *ndev;
1883 	int i;
1884 
1885 	for (i = 0; i < ac->num_ports; i++) {
1886 		ndev = ac->ports[i];
1887 		if (!ndev) {
1888 			if (i == 0)
1889 				dev_err(dev, "No net device to remove\n");
1890 			goto out;
1891 		}
1892 
1893 		/* All cleanup actions should stay after rtnl_lock(), otherwise
1894 		 * other functions may access partially cleaned up data.
1895 		 */
1896 		rtnl_lock();
1897 
1898 		mana_detach(ndev, false);
1899 
1900 		unregister_netdevice(ndev);
1901 
1902 		rtnl_unlock();
1903 
1904 		free_netdev(ndev);
1905 	}
1906 
1907 	mana_destroy_eq(ac);
1908 
1909 out:
1910 	mana_gd_deregister_device(gd);
1911 	gd->driver_data = NULL;
1912 	gd->gdma_context = NULL;
1913 	kfree(ac);
1914 }
1915