xref: /linux/drivers/net/ethernet/microsoft/mana/mana_en.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
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/debugfs.h>
7 #include <linux/inetdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/ethtool.h>
10 #include <linux/filter.h>
11 #include <linux/mm.h>
12 #include <linux/pci.h>
13 #include <linux/export.h>
14 #include <linux/skbuff.h>
15 
16 #include <net/checksum.h>
17 #include <net/ip6_checksum.h>
18 #include <net/netdev_lock.h>
19 #include <net/page_pool/helpers.h>
20 #include <net/xdp.h>
21 
22 #include <net/mana/mana.h>
23 #include <net/mana/mana_auxiliary.h>
24 #include <net/mana/hw_channel.h>
25 
26 static DEFINE_IDA(mana_adev_ida);
27 
mana_adev_idx_alloc(void)28 static int mana_adev_idx_alloc(void)
29 {
30 	return ida_alloc(&mana_adev_ida, GFP_KERNEL);
31 }
32 
mana_adev_idx_free(int idx)33 static void mana_adev_idx_free(int idx)
34 {
35 	ida_free(&mana_adev_ida, idx);
36 }
37 
mana_dbg_q_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)38 static ssize_t mana_dbg_q_read(struct file *filp, char __user *buf, size_t count,
39 			       loff_t *pos)
40 {
41 	struct gdma_queue *gdma_q = filp->private_data;
42 
43 	if (gdma_q->mem_info.nr_pages)
44 		return mana_gd_read_ring(gdma_q, buf, count, pos);
45 
46 	return simple_read_from_buffer(buf, count, pos, gdma_q->queue_mem_ptr,
47 				       gdma_q->queue_size);
48 }
49 
50 static const struct file_operations mana_dbg_q_fops = {
51 	.owner  = THIS_MODULE,
52 	.open   = simple_open,
53 	.read   = mana_dbg_q_read,
54 };
55 
mana_en_need_log(struct mana_port_context * apc,int err)56 static bool mana_en_need_log(struct mana_port_context *apc, int err)
57 {
58 	if (apc && apc->ac && apc->ac->gdma_dev &&
59 	    apc->ac->gdma_dev->gdma_context)
60 		return mana_need_log(apc->ac->gdma_dev->gdma_context, err);
61 	else
62 		return true;
63 }
64 
mana_put_rx_page(struct mana_rxq * rxq,struct page * page,bool from_pool)65 static void mana_put_rx_page(struct mana_rxq *rxq, struct page *page,
66 			     bool from_pool)
67 {
68 	if (from_pool)
69 		page_pool_put_full_page(rxq->page_pool, page, false);
70 	else
71 		put_page(page);
72 }
73 
74 /* Microsoft Azure Network Adapter (MANA) functions */
75 
mana_open(struct net_device * ndev)76 static int mana_open(struct net_device *ndev)
77 {
78 	struct mana_port_context *apc = netdev_priv(ndev);
79 	int err;
80 	err = mana_alloc_queues(ndev);
81 
82 	if (err) {
83 		netdev_err(ndev, "%s failed to allocate queues: %d\n", __func__, err);
84 		return err;
85 	}
86 
87 	apc->port_is_up = true;
88 
89 	/* Ensure port state updated before txq state */
90 	smp_wmb();
91 
92 	netif_tx_wake_all_queues(ndev);
93 	netdev_dbg(ndev, "%s successful\n", __func__);
94 	return 0;
95 }
96 
mana_close(struct net_device * ndev)97 static int mana_close(struct net_device *ndev)
98 {
99 	struct mana_port_context *apc = netdev_priv(ndev);
100 
101 	if (!apc->port_is_up)
102 		return 0;
103 
104 	return mana_detach(ndev, true);
105 }
106 
mana_link_state_handle(struct work_struct * w)107 static void mana_link_state_handle(struct work_struct *w)
108 {
109 	struct mana_context *ac;
110 	struct net_device *ndev;
111 	u32 link_event;
112 	bool link_up;
113 	int i;
114 
115 	ac = container_of(w, struct mana_context, link_change_work);
116 
117 	rtnl_lock();
118 
119 	link_event = READ_ONCE(ac->link_event);
120 
121 	if (link_event == HWC_DATA_HW_LINK_CONNECT)
122 		link_up = true;
123 	else if (link_event == HWC_DATA_HW_LINK_DISCONNECT)
124 		link_up = false;
125 	else
126 		goto out;
127 
128 	/* Process all ports */
129 	for (i = 0; i < ac->num_ports; i++) {
130 		ndev = ac->ports[i];
131 		if (!ndev)
132 			continue;
133 
134 		if (link_up) {
135 			netif_carrier_on(ndev);
136 
137 			__netdev_notify_peers(ndev);
138 		} else {
139 			netif_carrier_off(ndev);
140 		}
141 	}
142 
143 out:
144 	rtnl_unlock();
145 }
146 
mana_can_tx(struct gdma_queue * wq)147 static bool mana_can_tx(struct gdma_queue *wq)
148 {
149 	return mana_gd_wq_avail_space(wq) >= MAX_TX_WQE_SIZE;
150 }
151 
mana_checksum_info(struct sk_buff * skb)152 static unsigned int mana_checksum_info(struct sk_buff *skb)
153 {
154 	if (skb->protocol == htons(ETH_P_IP)) {
155 		struct iphdr *ip = ip_hdr(skb);
156 
157 		if (ip->protocol == IPPROTO_TCP)
158 			return IPPROTO_TCP;
159 
160 		if (ip->protocol == IPPROTO_UDP)
161 			return IPPROTO_UDP;
162 	} else if (skb->protocol == htons(ETH_P_IPV6)) {
163 		struct ipv6hdr *ip6 = ipv6_hdr(skb);
164 
165 		if (ip6->nexthdr == IPPROTO_TCP)
166 			return IPPROTO_TCP;
167 
168 		if (ip6->nexthdr == IPPROTO_UDP)
169 			return IPPROTO_UDP;
170 	}
171 
172 	/* No csum offloading */
173 	return 0;
174 }
175 
mana_add_sge(struct mana_tx_package * tp,struct mana_skb_head * ash,int sg_i,dma_addr_t da,int sge_len,u32 gpa_mkey)176 static void mana_add_sge(struct mana_tx_package *tp, struct mana_skb_head *ash,
177 			 int sg_i, dma_addr_t da, int sge_len, u32 gpa_mkey)
178 {
179 	ash->dma_handle[sg_i] = da;
180 	ash->size[sg_i] = sge_len;
181 
182 	tp->wqe_req.sgl[sg_i].address = da;
183 	tp->wqe_req.sgl[sg_i].mem_key = gpa_mkey;
184 	tp->wqe_req.sgl[sg_i].size = sge_len;
185 }
186 
mana_map_skb(struct sk_buff * skb,struct mana_port_context * apc,struct mana_tx_package * tp,int gso_hs)187 static int mana_map_skb(struct sk_buff *skb, struct mana_port_context *apc,
188 			struct mana_tx_package *tp, int gso_hs)
189 {
190 	struct mana_skb_head *ash = (struct mana_skb_head *)skb->head;
191 	int hsg = 1; /* num of SGEs of linear part */
192 	struct gdma_dev *gd = apc->ac->gdma_dev;
193 	int skb_hlen = skb_headlen(skb);
194 	int sge0_len, sge1_len = 0;
195 	struct gdma_context *gc;
196 	struct device *dev;
197 	skb_frag_t *frag;
198 	dma_addr_t da;
199 	int sg_i;
200 	int i;
201 
202 	gc = gd->gdma_context;
203 	dev = gc->dev;
204 
205 	if (gso_hs && gso_hs < skb_hlen) {
206 		sge0_len = gso_hs;
207 		sge1_len = skb_hlen - gso_hs;
208 	} else {
209 		sge0_len = skb_hlen;
210 	}
211 
212 	da = dma_map_single(dev, skb->data, sge0_len, DMA_TO_DEVICE);
213 	if (dma_mapping_error(dev, da))
214 		return -ENOMEM;
215 
216 	mana_add_sge(tp, ash, 0, da, sge0_len, gd->gpa_mkey);
217 
218 	if (sge1_len) {
219 		sg_i = 1;
220 		da = dma_map_single(dev, skb->data + sge0_len, sge1_len,
221 				    DMA_TO_DEVICE);
222 		if (dma_mapping_error(dev, da))
223 			goto frag_err;
224 
225 		mana_add_sge(tp, ash, sg_i, da, sge1_len, gd->gpa_mkey);
226 		hsg = 2;
227 	}
228 
229 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
230 		sg_i = hsg + i;
231 
232 		frag = &skb_shinfo(skb)->frags[i];
233 		da = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
234 				      DMA_TO_DEVICE);
235 		if (dma_mapping_error(dev, da))
236 			goto frag_err;
237 
238 		mana_add_sge(tp, ash, sg_i, da, skb_frag_size(frag),
239 			     gd->gpa_mkey);
240 	}
241 
242 	return 0;
243 
244 frag_err:
245 	if (net_ratelimit())
246 		netdev_err(apc->ndev, "Failed to map skb of size %u to DMA\n",
247 			   skb->len);
248 	for (i = sg_i - 1; i >= hsg; i--)
249 		dma_unmap_page(dev, ash->dma_handle[i], ash->size[i],
250 			       DMA_TO_DEVICE);
251 
252 	for (i = hsg - 1; i >= 0; i--)
253 		dma_unmap_single(dev, ash->dma_handle[i], ash->size[i],
254 				 DMA_TO_DEVICE);
255 
256 	return -ENOMEM;
257 }
258 
259 /* Handle the case when GSO SKB linear length is too large.
260  * MANA NIC requires GSO packets to put only the packet header to SGE0.
261  * So, we need 2 SGEs for the skb linear part which contains more than the
262  * header.
263  * Return a positive value for the number of SGEs, or a negative value
264  * for an error.
265  */
mana_fix_skb_head(struct net_device * ndev,struct sk_buff * skb,int gso_hs)266 static int mana_fix_skb_head(struct net_device *ndev, struct sk_buff *skb,
267 			     int gso_hs)
268 {
269 	int num_sge = 1 + skb_shinfo(skb)->nr_frags;
270 	int skb_hlen = skb_headlen(skb);
271 
272 	if (gso_hs < skb_hlen) {
273 		num_sge++;
274 	} else if (gso_hs > skb_hlen) {
275 		if (net_ratelimit())
276 			netdev_err(ndev,
277 				   "TX nonlinear head: hs:%d, skb_hlen:%d\n",
278 				   gso_hs, skb_hlen);
279 
280 		return -EINVAL;
281 	}
282 
283 	return num_sge;
284 }
285 
286 /* Get the GSO packet's header size */
mana_get_gso_hs(struct sk_buff * skb)287 static int mana_get_gso_hs(struct sk_buff *skb)
288 {
289 	int gso_hs;
290 
291 	if (skb->encapsulation) {
292 		gso_hs = skb_inner_tcp_all_headers(skb);
293 	} else {
294 		if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
295 			gso_hs = skb_transport_offset(skb) +
296 				 sizeof(struct udphdr);
297 		} else {
298 			gso_hs = skb_tcp_all_headers(skb);
299 		}
300 	}
301 
302 	return gso_hs;
303 }
304 
mana_per_port_queue_reset_work_handler(struct work_struct * work)305 static void mana_per_port_queue_reset_work_handler(struct work_struct *work)
306 {
307 	struct mana_port_context *apc = container_of(work,
308 						     struct mana_port_context,
309 						     queue_reset_work);
310 	struct net_device *ndev = apc->ndev;
311 	int err;
312 
313 	rtnl_lock();
314 
315 	/* Block RDMA from grabbing the vport during the detach/attach
316 	 * window, same as mana_set_channels().
317 	 */
318 	mutex_lock(&apc->vport_mutex);
319 	apc->channel_changing = true;
320 	mutex_unlock(&apc->vport_mutex);
321 
322 	/* Pre-allocate buffers to prevent failure in mana_attach later */
323 	err = mana_pre_alloc_rxbufs(apc, ndev->mtu, apc->num_queues);
324 	if (err) {
325 		netdev_err(ndev, "Insufficient memory for reset post tx stall detection\n");
326 		goto clear_flag;
327 	}
328 
329 	err = mana_detach(ndev, false);
330 	if (err) {
331 		netdev_err(ndev, "mana_detach failed: %d\n", err);
332 		goto dealloc_pre_rxbufs;
333 	}
334 
335 	err = mana_attach(ndev);
336 	if (err)
337 		netdev_err(ndev, "mana_attach failed: %d\n", err);
338 
339 dealloc_pre_rxbufs:
340 	mana_pre_dealloc_rxbufs(apc);
341 clear_flag:
342 	mutex_lock(&apc->vport_mutex);
343 	apc->channel_changing = false;
344 	mutex_unlock(&apc->vport_mutex);
345 
346 	rtnl_unlock();
347 }
348 
mana_start_xmit(struct sk_buff * skb,struct net_device * ndev)349 netdev_tx_t mana_start_xmit(struct sk_buff *skb, struct net_device *ndev)
350 {
351 	enum mana_tx_pkt_format pkt_fmt = MANA_SHORT_PKT_FMT;
352 	struct mana_port_context *apc = netdev_priv(ndev);
353 	int gso_hs = 0; /* zero for non-GSO pkts */
354 	u16 txq_idx = skb_get_queue_mapping(skb);
355 	struct gdma_dev *gd = apc->ac->gdma_dev;
356 	bool ipv4 = false, ipv6 = false;
357 	struct mana_tx_package pkg = {};
358 	struct netdev_queue *net_txq;
359 	struct mana_stats_tx *tx_stats;
360 	struct gdma_queue *gdma_sq;
361 	int err, len, num_gso_seg;
362 	unsigned int csum_type;
363 	struct mana_txq *txq;
364 	struct mana_cq *cq;
365 
366 	if (unlikely(!apc->port_is_up))
367 		goto tx_drop;
368 
369 	if (skb_cow_head(skb, MANA_HEADROOM))
370 		goto tx_drop_count;
371 
372 	txq = &apc->tx_qp[txq_idx]->txq;
373 	gdma_sq = txq->gdma_sq;
374 	cq = &apc->tx_qp[txq_idx]->tx_cq;
375 	tx_stats = &txq->stats;
376 
377 	BUILD_BUG_ON(MAX_TX_WQE_SGL_ENTRIES != MANA_MAX_TX_WQE_SGL_ENTRIES);
378 	if (MAX_SKB_FRAGS + 2 > MAX_TX_WQE_SGL_ENTRIES &&
379 	    skb_shinfo(skb)->nr_frags + 2 > MAX_TX_WQE_SGL_ENTRIES) {
380 		/* GSO skb with Hardware SGE limit exceeded is not expected here
381 		 * as they are handled in mana_features_check() callback
382 		 */
383 		if (skb_linearize(skb)) {
384 			netdev_warn_once(ndev, "Failed to linearize skb with nr_frags=%d and is_gso=%d\n",
385 					 skb_shinfo(skb)->nr_frags,
386 					 skb_is_gso(skb));
387 			goto tx_drop_count;
388 		}
389 		apc->eth_stats.tx_linear_pkt_cnt++;
390 	}
391 
392 	pkg.tx_oob.s_oob.vcq_num = cq->gdma_id;
393 	pkg.tx_oob.s_oob.vsq_frame = txq->vsq_frame;
394 
395 	if (txq->vp_offset > MANA_SHORT_VPORT_OFFSET_MAX) {
396 		pkg.tx_oob.l_oob.long_vp_offset = txq->vp_offset;
397 		pkt_fmt = MANA_LONG_PKT_FMT;
398 	} else {
399 		pkg.tx_oob.s_oob.short_vp_offset = txq->vp_offset;
400 	}
401 
402 	if (skb_vlan_tag_present(skb)) {
403 		pkt_fmt = MANA_LONG_PKT_FMT;
404 		pkg.tx_oob.l_oob.inject_vlan_pri_tag = 1;
405 		pkg.tx_oob.l_oob.pcp = skb_vlan_tag_get_prio(skb);
406 		pkg.tx_oob.l_oob.dei = skb_vlan_tag_get_cfi(skb);
407 		pkg.tx_oob.l_oob.vlan_id = skb_vlan_tag_get_id(skb);
408 	}
409 
410 	pkg.tx_oob.s_oob.pkt_fmt = pkt_fmt;
411 
412 	if (pkt_fmt == MANA_SHORT_PKT_FMT) {
413 		pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_short_oob);
414 		u64_stats_update_begin(&tx_stats->syncp);
415 		tx_stats->short_pkt_fmt++;
416 		u64_stats_update_end(&tx_stats->syncp);
417 	} else {
418 		pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_oob);
419 		u64_stats_update_begin(&tx_stats->syncp);
420 		tx_stats->long_pkt_fmt++;
421 		u64_stats_update_end(&tx_stats->syncp);
422 	}
423 
424 	pkg.wqe_req.inline_oob_data = &pkg.tx_oob;
425 	pkg.wqe_req.flags = 0;
426 	pkg.wqe_req.client_data_unit = 0;
427 
428 	pkg.wqe_req.num_sge = 1 + skb_shinfo(skb)->nr_frags;
429 
430 	if (skb->protocol == htons(ETH_P_IP))
431 		ipv4 = true;
432 	else if (skb->protocol == htons(ETH_P_IPV6))
433 		ipv6 = true;
434 
435 	if (skb_is_gso(skb)) {
436 		int num_sge;
437 
438 		gso_hs = mana_get_gso_hs(skb);
439 
440 		num_sge = mana_fix_skb_head(ndev, skb, gso_hs);
441 		if (num_sge > 0)
442 			pkg.wqe_req.num_sge = num_sge;
443 		else
444 			goto tx_drop_count;
445 
446 		u64_stats_update_begin(&tx_stats->syncp);
447 		if (skb->encapsulation) {
448 			tx_stats->tso_inner_packets++;
449 			tx_stats->tso_inner_bytes += skb->len - gso_hs;
450 		} else {
451 			tx_stats->tso_packets++;
452 			tx_stats->tso_bytes += skb->len - gso_hs;
453 		}
454 		u64_stats_update_end(&tx_stats->syncp);
455 
456 		pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
457 		pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
458 
459 		pkg.tx_oob.s_oob.comp_iphdr_csum = 1;
460 		pkg.tx_oob.s_oob.comp_tcp_csum = 1;
461 		pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb);
462 
463 		pkg.wqe_req.client_data_unit = skb_shinfo(skb)->gso_size;
464 		pkg.wqe_req.flags = GDMA_WR_OOB_IN_SGL | GDMA_WR_PAD_BY_SGE0;
465 		if (ipv4) {
466 			ip_hdr(skb)->tot_len = 0;
467 			ip_hdr(skb)->check = 0;
468 			tcp_hdr(skb)->check =
469 				~csum_tcpudp_magic(ip_hdr(skb)->saddr,
470 						   ip_hdr(skb)->daddr, 0,
471 						   IPPROTO_TCP, 0);
472 		} else {
473 			ipv6_hdr(skb)->payload_len = 0;
474 			tcp_hdr(skb)->check =
475 				~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
476 						 &ipv6_hdr(skb)->daddr, 0,
477 						 IPPROTO_TCP, 0);
478 		}
479 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
480 		csum_type = mana_checksum_info(skb);
481 
482 		u64_stats_update_begin(&tx_stats->syncp);
483 		tx_stats->csum_partial++;
484 		u64_stats_update_end(&tx_stats->syncp);
485 
486 		if (csum_type == IPPROTO_TCP) {
487 			pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
488 			pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
489 
490 			pkg.tx_oob.s_oob.comp_tcp_csum = 1;
491 			pkg.tx_oob.s_oob.trans_off = skb_transport_offset(skb);
492 
493 		} else if (csum_type == IPPROTO_UDP) {
494 			pkg.tx_oob.s_oob.is_outer_ipv4 = ipv4;
495 			pkg.tx_oob.s_oob.is_outer_ipv6 = ipv6;
496 
497 			pkg.tx_oob.s_oob.comp_udp_csum = 1;
498 		} else {
499 			/* Can't do offload of this type of checksum */
500 			if (skb_checksum_help(skb))
501 				goto tx_drop_count;
502 		}
503 	}
504 
505 	if (pkg.wqe_req.num_sge <= ARRAY_SIZE(pkg.sgl_array)) {
506 		pkg.wqe_req.sgl = pkg.sgl_array;
507 	} else {
508 		pkg.sgl_ptr = kmalloc_objs(struct gdma_sge, pkg.wqe_req.num_sge,
509 					   GFP_ATOMIC);
510 		if (!pkg.sgl_ptr)
511 			goto tx_drop_count;
512 
513 		pkg.wqe_req.sgl = pkg.sgl_ptr;
514 	}
515 
516 	if (mana_map_skb(skb, apc, &pkg, gso_hs)) {
517 		u64_stats_update_begin(&tx_stats->syncp);
518 		tx_stats->mana_map_err++;
519 		u64_stats_update_end(&tx_stats->syncp);
520 		goto free_sgl_ptr;
521 	}
522 
523 	skb_queue_tail(&txq->pending_skbs, skb);
524 
525 	len = skb->len;
526 	num_gso_seg = skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1;
527 	net_txq = netdev_get_tx_queue(ndev, txq_idx);
528 
529 	err = mana_gd_post_work_request(gdma_sq, &pkg.wqe_req,
530 					(struct gdma_posted_wqe_info *)skb->cb);
531 	if (!mana_can_tx(gdma_sq)) {
532 		netif_tx_stop_queue(net_txq);
533 		apc->eth_stats.stop_queue++;
534 	}
535 
536 	if (err) {
537 		(void)skb_dequeue_tail(&txq->pending_skbs);
538 		mana_unmap_skb(skb, apc);
539 		netdev_warn(ndev, "Failed to post TX OOB: %d\n", err);
540 		goto free_sgl_ptr;
541 	}
542 
543 	err = NETDEV_TX_OK;
544 	atomic_inc(&txq->pending_sends);
545 
546 	mana_gd_wq_ring_doorbell(gd->gdma_context, gdma_sq);
547 
548 	/* skb may be freed after mana_gd_post_work_request. Do not use it. */
549 	skb = NULL;
550 
551 	/* Populated the packet and bytes counters based on post GSO packet
552 	 * calculations
553 	 */
554 	tx_stats = &txq->stats;
555 	u64_stats_update_begin(&tx_stats->syncp);
556 	tx_stats->packets += num_gso_seg;
557 	tx_stats->bytes += len + ((num_gso_seg - 1) * gso_hs);
558 	u64_stats_update_end(&tx_stats->syncp);
559 
560 	if (netif_tx_queue_stopped(net_txq) && mana_can_tx(gdma_sq)) {
561 		netif_tx_wake_queue(net_txq);
562 		apc->eth_stats.wake_queue++;
563 	}
564 
565 	kfree(pkg.sgl_ptr);
566 	return err;
567 
568 free_sgl_ptr:
569 	kfree(pkg.sgl_ptr);
570 tx_drop_count:
571 	ndev->stats.tx_dropped++;
572 tx_drop:
573 	dev_kfree_skb_any(skb);
574 	return NETDEV_TX_OK;
575 }
576 
577 #if (MAX_SKB_FRAGS + 2 > MANA_MAX_TX_WQE_SGL_ENTRIES)
mana_features_check(struct sk_buff * skb,struct net_device * ndev,netdev_features_t features)578 static netdev_features_t mana_features_check(struct sk_buff *skb,
579 					     struct net_device *ndev,
580 					     netdev_features_t features)
581 {
582 	if (skb_shinfo(skb)->nr_frags + 2 > MAX_TX_WQE_SGL_ENTRIES) {
583 		/* Exceeds HW SGE limit.
584 		 * GSO case:
585 		 *   Disable GSO so the stack will software-segment the skb
586 		 *   into smaller skbs that fit the SGE budget.
587 		 * Non-GSO case:
588 		 *   The xmit path will attempt skb_linearize() as a fallback.
589 		 */
590 		features &= ~NETIF_F_GSO_MASK;
591 	}
592 	return features;
593 }
594 #endif
595 
mana_get_stats64(struct net_device * ndev,struct rtnl_link_stats64 * st)596 static void mana_get_stats64(struct net_device *ndev,
597 			     struct rtnl_link_stats64 *st)
598 {
599 	struct mana_port_context *apc = netdev_priv(ndev);
600 	unsigned int num_queues = apc->num_queues;
601 	struct mana_stats_rx *rx_stats;
602 	struct mana_stats_tx *tx_stats;
603 	unsigned int start;
604 	u64 packets, bytes;
605 	int q;
606 
607 	if (!apc->port_is_up)
608 		return;
609 
610 	netdev_stats_to_stats64(st, &ndev->stats);
611 
612 	if (apc->ac->hwc_timeout_occurred)
613 		netdev_warn_once(ndev, "HWC timeout occurred\n");
614 
615 	st->rx_missed_errors = apc->ac->hc_stats.hc_rx_discards_no_wqe;
616 
617 	for (q = 0; q < num_queues; q++) {
618 		rx_stats = &apc->rxqs[q]->stats;
619 
620 		do {
621 			start = u64_stats_fetch_begin(&rx_stats->syncp);
622 			packets = rx_stats->packets;
623 			bytes = rx_stats->bytes;
624 		} while (u64_stats_fetch_retry(&rx_stats->syncp, start));
625 
626 		st->rx_packets += packets;
627 		st->rx_bytes += bytes;
628 	}
629 
630 	for (q = 0; q < num_queues; q++) {
631 		tx_stats = &apc->tx_qp[q]->txq.stats;
632 
633 		do {
634 			start = u64_stats_fetch_begin(&tx_stats->syncp);
635 			packets = tx_stats->packets;
636 			bytes = tx_stats->bytes;
637 		} while (u64_stats_fetch_retry(&tx_stats->syncp, start));
638 
639 		st->tx_packets += packets;
640 		st->tx_bytes += bytes;
641 	}
642 }
643 
mana_get_tx_queue(struct net_device * ndev,struct sk_buff * skb,int old_q)644 static int mana_get_tx_queue(struct net_device *ndev, struct sk_buff *skb,
645 			     int old_q)
646 {
647 	struct mana_port_context *apc = netdev_priv(ndev);
648 	u32 hash = skb_get_hash(skb);
649 	struct sock *sk = skb->sk;
650 	int txq;
651 
652 	txq = apc->indir_table[hash & (apc->indir_table_sz - 1)];
653 
654 	if (txq != old_q && sk && sk_fullsock(sk) &&
655 	    rcu_access_pointer(sk->sk_dst_cache))
656 		sk_tx_queue_set(sk, txq);
657 
658 	return txq;
659 }
660 
mana_select_queue(struct net_device * ndev,struct sk_buff * skb,struct net_device * sb_dev)661 static u16 mana_select_queue(struct net_device *ndev, struct sk_buff *skb,
662 			     struct net_device *sb_dev)
663 {
664 	int txq;
665 
666 	if (ndev->real_num_tx_queues == 1)
667 		return 0;
668 
669 	txq = sk_tx_queue_get(skb->sk);
670 
671 	if (txq < 0 || skb->ooo_okay || txq >= ndev->real_num_tx_queues) {
672 		if (skb_rx_queue_recorded(skb))
673 			txq = skb_get_rx_queue(skb);
674 		else
675 			txq = mana_get_tx_queue(ndev, skb, txq);
676 	}
677 
678 	return txq;
679 }
680 
681 /* Release pre-allocated RX buffers */
mana_pre_dealloc_rxbufs(struct mana_port_context * mpc)682 void mana_pre_dealloc_rxbufs(struct mana_port_context *mpc)
683 {
684 	struct device *dev;
685 	int i;
686 
687 	dev = mpc->ac->gdma_dev->gdma_context->dev;
688 
689 	if (!mpc->rxbufs_pre)
690 		goto out1;
691 
692 	if (!mpc->das_pre)
693 		goto out2;
694 
695 	while (mpc->rxbpre_total) {
696 		i = --mpc->rxbpre_total;
697 		dma_unmap_single(dev, mpc->das_pre[i], mpc->rxbpre_datasize,
698 				 DMA_FROM_DEVICE);
699 		put_page(virt_to_head_page(mpc->rxbufs_pre[i]));
700 	}
701 
702 	kvfree(mpc->das_pre);
703 	mpc->das_pre = NULL;
704 
705 out2:
706 	kvfree(mpc->rxbufs_pre);
707 	mpc->rxbufs_pre = NULL;
708 
709 out1:
710 	mpc->rxbpre_datasize = 0;
711 	mpc->rxbpre_alloc_size = 0;
712 	mpc->rxbpre_headroom = 0;
713 }
714 
715 /* Get a buffer from the pre-allocated RX buffers */
mana_get_rxbuf_pre(struct mana_rxq * rxq,dma_addr_t * da)716 static void *mana_get_rxbuf_pre(struct mana_rxq *rxq, dma_addr_t *da)
717 {
718 	struct net_device *ndev = rxq->ndev;
719 	struct mana_port_context *mpc;
720 	void *va;
721 
722 	mpc = netdev_priv(ndev);
723 
724 	if (!mpc->rxbufs_pre || !mpc->das_pre || !mpc->rxbpre_total) {
725 		netdev_err(ndev, "No RX pre-allocated bufs\n");
726 		return NULL;
727 	}
728 
729 	/* Check sizes to catch unexpected coding error */
730 	if (mpc->rxbpre_datasize != rxq->datasize) {
731 		netdev_err(ndev, "rxbpre_datasize mismatch: %u: %u\n",
732 			   mpc->rxbpre_datasize, rxq->datasize);
733 		return NULL;
734 	}
735 
736 	if (mpc->rxbpre_alloc_size != rxq->alloc_size) {
737 		netdev_err(ndev, "rxbpre_alloc_size mismatch: %u: %u\n",
738 			   mpc->rxbpre_alloc_size, rxq->alloc_size);
739 		return NULL;
740 	}
741 
742 	if (mpc->rxbpre_headroom != rxq->headroom) {
743 		netdev_err(ndev, "rxbpre_headroom mismatch: %u: %u\n",
744 			   mpc->rxbpre_headroom, rxq->headroom);
745 		return NULL;
746 	}
747 
748 	mpc->rxbpre_total--;
749 
750 	*da = mpc->das_pre[mpc->rxbpre_total];
751 	va = mpc->rxbufs_pre[mpc->rxbpre_total];
752 	mpc->rxbufs_pre[mpc->rxbpre_total] = NULL;
753 
754 	/* Deallocate the array after all buffers are gone */
755 	if (!mpc->rxbpre_total)
756 		mana_pre_dealloc_rxbufs(mpc);
757 
758 	return va;
759 }
760 
761 static bool
mana_use_single_rxbuf_per_page(struct mana_port_context * apc,u32 mtu)762 mana_use_single_rxbuf_per_page(struct mana_port_context *apc, u32 mtu)
763 {
764 	/* On some platforms with 4K PAGE_SIZE, page_pool fragment allocation
765 	 * in the RX refill path (~2kB buffer) can cause significant throughput
766 	 * regression under high connection counts. Allow user to force one RX
767 	 * buffer per page via ethtool private flag to bypass the fragment
768 	 * path.
769 	 */
770 	if (apc->priv_flags & BIT(MANA_PRIV_FLAG_USE_FULL_PAGE_RXBUF))
771 		return true;
772 
773 	/* For xdp and jumbo frames make sure only one packet fits per page. */
774 	if (mtu + MANA_RXBUF_PAD > PAGE_SIZE / 2 || mana_xdp_get(apc))
775 		return true;
776 
777 	return false;
778 }
779 
780 /* Get RX buffer's data size, alloc size, XDP headroom based on MTU */
mana_get_rxbuf_cfg(struct mana_port_context * apc,int mtu,u32 * datasize,u32 * alloc_size,u32 * headroom,u32 * frag_count)781 static void mana_get_rxbuf_cfg(struct mana_port_context *apc,
782 			       int mtu, u32 *datasize, u32 *alloc_size,
783 			       u32 *headroom, u32 *frag_count)
784 {
785 	u32 len, buf_size;
786 
787 	/* Calculate datasize first (consistent across all cases) */
788 	*datasize = mtu + ETH_HLEN;
789 
790 	if (mana_use_single_rxbuf_per_page(apc, mtu)) {
791 		if (mana_xdp_get(apc)) {
792 			*headroom = XDP_PACKET_HEADROOM;
793 			*alloc_size = PAGE_SIZE;
794 		} else {
795 			*headroom = 0; /* no support for XDP */
796 			*alloc_size = SKB_DATA_ALIGN(mtu + MANA_RXBUF_PAD +
797 						     *headroom);
798 		}
799 
800 		*frag_count = 1;
801 
802 		/* In the single-buffer path, napi_build_skb() must see the
803 		 * actual backing allocation size so skb->truesize reflects
804 		 * the full page (or higher-order page), not just the usable
805 		 * packet area.
806 		 */
807 		*alloc_size = PAGE_SIZE << get_order(*alloc_size);
808 		return;
809 	}
810 
811 	/* Standard MTU case - optimize for multiple packets per page */
812 	*headroom = 0;
813 
814 	/* Calculate base buffer size needed */
815 	len = SKB_DATA_ALIGN(mtu + MANA_RXBUF_PAD + *headroom);
816 	buf_size = ALIGN(len, MANA_RX_FRAG_ALIGNMENT);
817 
818 	/* Calculate how many packets can fit in a page */
819 	*frag_count = PAGE_SIZE / buf_size;
820 	*alloc_size = buf_size;
821 }
822 
mana_pre_alloc_rxbufs(struct mana_port_context * mpc,int new_mtu,int num_queues)823 int mana_pre_alloc_rxbufs(struct mana_port_context *mpc, int new_mtu, int num_queues)
824 {
825 	struct device *dev;
826 	struct page *page;
827 	dma_addr_t da;
828 	int num_rxb;
829 	void *va;
830 	int i;
831 
832 	mana_get_rxbuf_cfg(mpc, new_mtu, &mpc->rxbpre_datasize,
833 			   &mpc->rxbpre_alloc_size, &mpc->rxbpre_headroom,
834 			   &mpc->rxbpre_frag_count);
835 
836 	dev = mpc->ac->gdma_dev->gdma_context->dev;
837 
838 	num_rxb = num_queues * mpc->rx_queue_size;
839 
840 	WARN(mpc->rxbufs_pre, "mana rxbufs_pre exists\n");
841 	mpc->rxbufs_pre = kvmalloc_array(num_rxb, sizeof(void *), GFP_KERNEL);
842 	if (!mpc->rxbufs_pre)
843 		goto error;
844 
845 	mpc->das_pre = kvmalloc_objs(dma_addr_t, num_rxb);
846 	if (!mpc->das_pre)
847 		goto error;
848 
849 	mpc->rxbpre_total = 0;
850 
851 	for (i = 0; i < num_rxb; i++) {
852 		page = dev_alloc_pages(get_order(mpc->rxbpre_alloc_size));
853 		if (!page)
854 			goto error;
855 
856 		va = page_to_virt(page);
857 
858 		da = dma_map_single(dev, va + mpc->rxbpre_headroom,
859 				    mpc->rxbpre_datasize, DMA_FROM_DEVICE);
860 		if (dma_mapping_error(dev, da)) {
861 			put_page(page);
862 			goto error;
863 		}
864 
865 		mpc->rxbufs_pre[i] = va;
866 		mpc->das_pre[i] = da;
867 		mpc->rxbpre_total = i + 1;
868 	}
869 
870 	return 0;
871 
872 error:
873 	netdev_err(mpc->ndev, "Failed to pre-allocate RX buffers for %d queues\n", num_queues);
874 	mana_pre_dealloc_rxbufs(mpc);
875 	return -ENOMEM;
876 }
877 
mana_change_mtu(struct net_device * ndev,int new_mtu)878 static int mana_change_mtu(struct net_device *ndev, int new_mtu)
879 {
880 	struct mana_port_context *mpc = netdev_priv(ndev);
881 	unsigned int old_mtu = ndev->mtu;
882 	int err;
883 
884 	/* Pre-allocate buffers to prevent failure in mana_attach later */
885 	err = mana_pre_alloc_rxbufs(mpc, new_mtu, mpc->num_queues);
886 	if (err) {
887 		netdev_err(ndev, "Insufficient memory for new MTU\n");
888 		return err;
889 	}
890 
891 	err = mana_detach(ndev, false);
892 	if (err) {
893 		netdev_err(ndev, "mana_detach failed: %d\n", err);
894 		goto out;
895 	}
896 
897 	WRITE_ONCE(ndev->mtu, new_mtu);
898 
899 	err = mana_attach(ndev);
900 	if (err) {
901 		netdev_err(ndev, "mana_attach failed: %d\n", err);
902 		WRITE_ONCE(ndev->mtu, old_mtu);
903 	}
904 
905 out:
906 	mana_pre_dealloc_rxbufs(mpc);
907 	return err;
908 }
909 
mana_tx_timeout(struct net_device * netdev,unsigned int txqueue)910 static void mana_tx_timeout(struct net_device *netdev, unsigned int txqueue)
911 {
912 	struct mana_port_context *apc = netdev_priv(netdev);
913 	struct mana_context *ac = apc->ac;
914 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
915 
916 	/* Debug knob for bringup/qualification: when set, log the timeout and
917 	 * skip the reset so the failing state is preserved for telemetry.
918 	 * Disabled by default; production behaviour is unchanged.
919 	 */
920 	if (READ_ONCE(apc->tx_timeout_skip_reset)) {
921 		netdev_warn(netdev,
922 			    "TX timeout on queue %u: reset skipped (tx_timeout_skip_reset enabled)\n",
923 			    txqueue);
924 		return;
925 	}
926 
927 	/* Already in service, hence tx queue reset is not required.*/
928 	if (test_bit(GC_IN_SERVICE, &gc->flags))
929 		return;
930 
931 	/* Note: If there are pending queue reset work for this port(apc),
932 	 * subsequent request queued up from here are ignored. This is because
933 	 * we are using the same work instance per port(apc).
934 	 */
935 	queue_work(ac->per_port_queue_reset_wq, &apc->queue_reset_work);
936 }
937 
mana_shaper_set(struct net_shaper_binding * binding,const struct net_shaper * shaper,struct netlink_ext_ack * extack)938 static int mana_shaper_set(struct net_shaper_binding *binding,
939 			   const struct net_shaper *shaper,
940 			   struct netlink_ext_ack *extack)
941 {
942 	struct mana_port_context *apc = netdev_priv(binding->netdev);
943 	u32 old_speed, rate;
944 	int err;
945 
946 	if (shaper->handle.scope != NET_SHAPER_SCOPE_NETDEV) {
947 		NL_SET_ERR_MSG_MOD(extack, "net shaper scope should be netdev");
948 		return -EINVAL;
949 	}
950 
951 	if (apc->handle.id && shaper->handle.id != apc->handle.id) {
952 		NL_SET_ERR_MSG_MOD(extack, "Cannot create multiple shapers");
953 		return -EOPNOTSUPP;
954 	}
955 
956 	if (!shaper->bw_max || (shaper->bw_max % 100000000)) {
957 		NL_SET_ERR_MSG_MOD(extack, "Please use multiples of 100Mbps for bandwidth");
958 		return -EINVAL;
959 	}
960 
961 	rate = div_u64(shaper->bw_max, 1000); /* Convert bps to Kbps */
962 	rate = div_u64(rate, 1000);	      /* Convert Kbps to Mbps */
963 
964 	/* Get current speed */
965 	err = mana_query_link_cfg(apc);
966 	old_speed = (err) ? SPEED_UNKNOWN : apc->speed;
967 
968 	if (!err) {
969 		err = mana_set_bw_clamp(apc, rate, TRI_STATE_TRUE);
970 		apc->speed = (err) ? old_speed : rate;
971 		apc->handle = (err) ? apc->handle : shaper->handle;
972 	}
973 
974 	return err;
975 }
976 
mana_shaper_del(struct net_shaper_binding * binding,const struct net_shaper_handle * handle,struct netlink_ext_ack * extack)977 static int mana_shaper_del(struct net_shaper_binding *binding,
978 			   const struct net_shaper_handle *handle,
979 			   struct netlink_ext_ack *extack)
980 {
981 	struct mana_port_context *apc = netdev_priv(binding->netdev);
982 	int err;
983 
984 	err = mana_set_bw_clamp(apc, 0, TRI_STATE_FALSE);
985 
986 	if (!err) {
987 		/* Reset mana port context parameters */
988 		apc->handle.id = 0;
989 		apc->handle.scope = NET_SHAPER_SCOPE_UNSPEC;
990 		apc->speed = apc->max_speed;
991 	}
992 
993 	return err;
994 }
995 
mana_shaper_cap(struct net_shaper_binding * binding,enum net_shaper_scope scope,unsigned long * flags)996 static void mana_shaper_cap(struct net_shaper_binding *binding,
997 			    enum net_shaper_scope scope,
998 			    unsigned long *flags)
999 {
1000 	*flags = BIT(NET_SHAPER_A_CAPS_SUPPORT_BW_MAX) |
1001 		 BIT(NET_SHAPER_A_CAPS_SUPPORT_METRIC_BPS);
1002 }
1003 
1004 static const struct net_shaper_ops mana_shaper_ops = {
1005 	.set = mana_shaper_set,
1006 	.delete = mana_shaper_del,
1007 	.capabilities = mana_shaper_cap,
1008 };
1009 
1010 static const struct net_device_ops mana_devops = {
1011 	.ndo_open		= mana_open,
1012 	.ndo_stop		= mana_close,
1013 	.ndo_select_queue	= mana_select_queue,
1014 #if (MAX_SKB_FRAGS + 2 > MANA_MAX_TX_WQE_SGL_ENTRIES)
1015 	.ndo_features_check	= mana_features_check,
1016 #endif
1017 	.ndo_start_xmit		= mana_start_xmit,
1018 	.ndo_validate_addr	= eth_validate_addr,
1019 	.ndo_get_stats64	= mana_get_stats64,
1020 	.ndo_bpf		= mana_bpf,
1021 	.ndo_xdp_xmit		= mana_xdp_xmit,
1022 	.ndo_change_mtu		= mana_change_mtu,
1023 	.ndo_tx_timeout		= mana_tx_timeout,
1024 	.net_shaper_ops         = &mana_shaper_ops,
1025 };
1026 
mana_cleanup_port_context(struct mana_port_context * apc)1027 static void mana_cleanup_port_context(struct mana_port_context *apc)
1028 {
1029 	/*
1030 	 * make sure subsequent cleanup attempts don't end up removing already
1031 	 * cleaned dentry pointer
1032 	 */
1033 	debugfs_remove(apc->mana_port_debugfs);
1034 	apc->mana_port_debugfs = NULL;
1035 	kfree(apc->rxqs);
1036 	apc->rxqs = NULL;
1037 }
1038 
mana_cleanup_indir_table(struct mana_port_context * apc)1039 static void mana_cleanup_indir_table(struct mana_port_context *apc)
1040 {
1041 	apc->indir_table_sz = 0;
1042 	kfree(apc->indir_table);
1043 	kfree(apc->rxobj_table);
1044 }
1045 
mana_init_port_context(struct mana_port_context * apc)1046 static int mana_init_port_context(struct mana_port_context *apc)
1047 {
1048 	apc->rxqs = kzalloc_objs(struct mana_rxq *, apc->num_queues);
1049 
1050 	return !apc->rxqs ? -ENOMEM : 0;
1051 }
1052 
gdma_mana_send_request(struct gdma_context * gc,void * in_buf,u32 in_len,void * out_buf,u32 out_len)1053 static int gdma_mana_send_request(struct gdma_context *gc, void *in_buf,
1054 				  u32 in_len, void *out_buf, u32 out_len)
1055 {
1056 	struct gdma_resp_hdr *resp = out_buf;
1057 	struct gdma_req_hdr *req = in_buf;
1058 	struct device *dev = gc->dev;
1059 	static atomic_t activity_id;
1060 	int err;
1061 
1062 	req->dev_id = gc->mana.dev_id;
1063 	req->activity_id = atomic_inc_return(&activity_id);
1064 
1065 	err = mana_gd_send_request(gc, in_len, in_buf, out_len,
1066 				   out_buf);
1067 	if (err || resp->status) {
1068 		if (err == -EOPNOTSUPP)
1069 			return err;
1070 
1071 		if (req->req.msg_type != MANA_QUERY_PHY_STAT &&
1072 		    mana_need_log(gc, err))
1073 			dev_err(dev, "Command 0x%x failed with status: 0x%x, err: %d\n",
1074 				req->req.msg_type, resp->status, err);
1075 		return err ? err : -EPROTO;
1076 	}
1077 
1078 	if (req->dev_id.as_uint32 != resp->dev_id.as_uint32 ||
1079 	    req->activity_id != resp->activity_id) {
1080 		dev_err(dev, "Unexpected mana message response: %x,%x,%x,%x\n",
1081 			req->dev_id.as_uint32, resp->dev_id.as_uint32,
1082 			req->activity_id, resp->activity_id);
1083 		return -EPROTO;
1084 	}
1085 
1086 	return 0;
1087 }
1088 
mana_send_request(struct mana_context * ac,void * in_buf,u32 in_len,void * out_buf,u32 out_len)1089 static int mana_send_request(struct mana_context *ac, void *in_buf,
1090 			     u32 in_len, void *out_buf, u32 out_len)
1091 {
1092 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
1093 
1094 	return gdma_mana_send_request(gc, in_buf, in_len, out_buf, out_len);
1095 }
1096 
mana_verify_resp_hdr(const struct gdma_resp_hdr * resp_hdr,const enum mana_command_code expected_code,const u32 min_size)1097 static int mana_verify_resp_hdr(const struct gdma_resp_hdr *resp_hdr,
1098 				const enum mana_command_code expected_code,
1099 				const u32 min_size)
1100 {
1101 	if (resp_hdr->response.msg_type != expected_code)
1102 		return -EPROTO;
1103 
1104 	if (resp_hdr->response.msg_version < GDMA_MESSAGE_V1)
1105 		return -EPROTO;
1106 
1107 	if (resp_hdr->response.msg_size < min_size)
1108 		return -EPROTO;
1109 
1110 	return 0;
1111 }
1112 
mana_pf_register_hw_vport(struct mana_port_context * apc)1113 static int mana_pf_register_hw_vport(struct mana_port_context *apc)
1114 {
1115 	struct mana_register_hw_vport_resp resp = {};
1116 	struct mana_register_hw_vport_req req = {};
1117 	int err;
1118 
1119 	mana_gd_init_req_hdr(&req.hdr, MANA_REGISTER_HW_PORT,
1120 			     sizeof(req), sizeof(resp));
1121 	req.attached_gfid = 1;
1122 	req.is_pf_default_vport = 1;
1123 	req.allow_all_ether_types = 1;
1124 
1125 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1126 				sizeof(resp));
1127 	if (err) {
1128 		netdev_err(apc->ndev, "Failed to register hw vPort: %d\n", err);
1129 		return err;
1130 	}
1131 
1132 	err = mana_verify_resp_hdr(&resp.hdr, MANA_REGISTER_HW_PORT,
1133 				   sizeof(resp));
1134 	if (err || resp.hdr.status) {
1135 		netdev_err(apc->ndev, "Failed to register hw vPort: %d, 0x%x\n",
1136 			   err, resp.hdr.status);
1137 		return err ? err : -EPROTO;
1138 	}
1139 
1140 	apc->port_handle = resp.hw_vport_handle;
1141 	return 0;
1142 }
1143 
mana_pf_deregister_hw_vport(struct mana_port_context * apc)1144 static void mana_pf_deregister_hw_vport(struct mana_port_context *apc)
1145 {
1146 	struct mana_deregister_hw_vport_resp resp = {};
1147 	struct mana_deregister_hw_vport_req req = {};
1148 	int err;
1149 
1150 	mana_gd_init_req_hdr(&req.hdr, MANA_DEREGISTER_HW_PORT,
1151 			     sizeof(req), sizeof(resp));
1152 	req.hw_vport_handle = apc->port_handle;
1153 
1154 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1155 				sizeof(resp));
1156 	if (err) {
1157 		if (mana_en_need_log(apc, err))
1158 			netdev_err(apc->ndev, "Failed to unregister hw vPort: %d\n",
1159 				   err);
1160 
1161 		return;
1162 	}
1163 
1164 	err = mana_verify_resp_hdr(&resp.hdr, MANA_DEREGISTER_HW_PORT,
1165 				   sizeof(resp));
1166 	if (err || resp.hdr.status)
1167 		netdev_err(apc->ndev,
1168 			   "Failed to deregister hw vPort: %d, 0x%x\n",
1169 			   err, resp.hdr.status);
1170 }
1171 
mana_pf_register_filter(struct mana_port_context * apc)1172 static int mana_pf_register_filter(struct mana_port_context *apc)
1173 {
1174 	struct mana_register_filter_resp resp = {};
1175 	struct mana_register_filter_req req = {};
1176 	int err;
1177 
1178 	mana_gd_init_req_hdr(&req.hdr, MANA_REGISTER_FILTER,
1179 			     sizeof(req), sizeof(resp));
1180 	req.vport = apc->port_handle;
1181 	memcpy(req.mac_addr, apc->mac_addr, ETH_ALEN);
1182 
1183 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1184 				sizeof(resp));
1185 	if (err) {
1186 		netdev_err(apc->ndev, "Failed to register filter: %d\n", err);
1187 		return err;
1188 	}
1189 
1190 	err = mana_verify_resp_hdr(&resp.hdr, MANA_REGISTER_FILTER,
1191 				   sizeof(resp));
1192 	if (err || resp.hdr.status) {
1193 		netdev_err(apc->ndev, "Failed to register filter: %d, 0x%x\n",
1194 			   err, resp.hdr.status);
1195 		return err ? err : -EPROTO;
1196 	}
1197 
1198 	apc->pf_filter_handle = resp.filter_handle;
1199 	return 0;
1200 }
1201 
mana_pf_deregister_filter(struct mana_port_context * apc)1202 static void mana_pf_deregister_filter(struct mana_port_context *apc)
1203 {
1204 	struct mana_deregister_filter_resp resp = {};
1205 	struct mana_deregister_filter_req req = {};
1206 	int err;
1207 
1208 	mana_gd_init_req_hdr(&req.hdr, MANA_DEREGISTER_FILTER,
1209 			     sizeof(req), sizeof(resp));
1210 	req.filter_handle = apc->pf_filter_handle;
1211 
1212 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1213 				sizeof(resp));
1214 	if (err) {
1215 		if (mana_en_need_log(apc, err))
1216 			netdev_err(apc->ndev, "Failed to unregister filter: %d\n",
1217 				   err);
1218 
1219 		return;
1220 	}
1221 
1222 	err = mana_verify_resp_hdr(&resp.hdr, MANA_DEREGISTER_FILTER,
1223 				   sizeof(resp));
1224 	if (err || resp.hdr.status)
1225 		netdev_err(apc->ndev,
1226 			   "Failed to deregister filter: %d, 0x%x\n",
1227 			   err, resp.hdr.status);
1228 }
1229 
mana_gd_query_device_cfg(struct gdma_context * gc,u32 proto_major_ver,u32 proto_minor_ver,u32 proto_micro_ver,u16 * max_num_vports,u8 * bm_hostmode)1230 int mana_gd_query_device_cfg(struct gdma_context *gc, u32 proto_major_ver,
1231 			     u32 proto_minor_ver, u32 proto_micro_ver,
1232 			     u16 *max_num_vports, u8 *bm_hostmode)
1233 {
1234 	struct mana_query_device_cfg_resp resp = {};
1235 	struct mana_query_device_cfg_req req = {};
1236 	struct device *dev = gc->dev;
1237 	int err = 0;
1238 
1239 	mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_DEV_CONFIG,
1240 			     sizeof(req), sizeof(resp));
1241 
1242 	req.hdr.resp.msg_version = GDMA_MESSAGE_V3;
1243 
1244 	req.proto_major_ver = proto_major_ver;
1245 	req.proto_minor_ver = proto_minor_ver;
1246 	req.proto_micro_ver = proto_micro_ver;
1247 
1248 	err = gdma_mana_send_request(gc, &req, sizeof(req),
1249 				     &resp, sizeof(resp));
1250 	if (err) {
1251 		dev_err(dev, "Failed to query config: %d", err);
1252 		return err;
1253 	}
1254 
1255 	err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_DEV_CONFIG,
1256 				   sizeof(resp));
1257 	if (err || resp.hdr.status) {
1258 		dev_err(dev, "Invalid query result: %d, 0x%x\n", err,
1259 			resp.hdr.status);
1260 		if (!err)
1261 			err = -EPROTO;
1262 		return err;
1263 	}
1264 
1265 	gc->cqe8_coalescing_sup = !!(resp.pf_cap_flags1 &
1266 				     MANA_PF_FLAG_1_CQE_8_COALESCING_SUPPORTED);
1267 
1268 	*max_num_vports = resp.max_num_vports;
1269 
1270 	if (resp.hdr.response.msg_version >= GDMA_MESSAGE_V2) {
1271 		if (resp.adapter_mtu == 0) {
1272 			/*
1273 			 * Some older PF firmware versions report an
1274 			 * adapter_mtu of 0. MANA hardware always supports the
1275 			 * standard Ethernet MTU, so fall back to ETH_FRAME_LEN.
1276 			 * Jumbo frames will not be available in this case.
1277 			 */
1278 			dev_info(dev,
1279 				 "PF reported adapter_mtu of 0, falling back to %u (jumbo frames disabled)\n",
1280 				 ETH_FRAME_LEN);
1281 			gc->adapter_mtu = ETH_FRAME_LEN;
1282 		} else if (resp.adapter_mtu < ETH_MIN_MTU + ETH_HLEN) {
1283 			dev_err(dev, "Adapter MTU too small: %u\n",
1284 				resp.adapter_mtu);
1285 			return -EPROTO;
1286 		} else {
1287 			gc->adapter_mtu = resp.adapter_mtu;
1288 		}
1289 	} else {
1290 		gc->adapter_mtu = ETH_FRAME_LEN;
1291 	}
1292 
1293 	if (resp.hdr.response.msg_version >= GDMA_MESSAGE_V3)
1294 		*bm_hostmode = resp.bm_hostmode;
1295 	else
1296 		*bm_hostmode = 0;
1297 
1298 	return 0;
1299 }
1300 
mana_query_vport_cfg(struct mana_port_context * apc,u32 vport_index,u32 * max_sq,u32 * max_rq,u32 * num_indir_entry)1301 static int mana_query_vport_cfg(struct mana_port_context *apc, u32 vport_index,
1302 				u32 *max_sq, u32 *max_rq, u32 *num_indir_entry)
1303 {
1304 	struct mana_query_vport_cfg_resp resp = {};
1305 	struct mana_query_vport_cfg_req req = {};
1306 	int err;
1307 
1308 	mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_VPORT_CONFIG,
1309 			     sizeof(req), sizeof(resp));
1310 
1311 	req.vport_index = vport_index;
1312 
1313 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1314 				sizeof(resp));
1315 	if (err)
1316 		return err;
1317 
1318 	err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_VPORT_CONFIG,
1319 				   sizeof(resp));
1320 	if (err)
1321 		return err;
1322 
1323 	if (resp.hdr.status)
1324 		return -EPROTO;
1325 
1326 	*max_sq = resp.max_num_sq;
1327 	*max_rq = resp.max_num_rq;
1328 
1329 	if (*max_sq == 0 || *max_rq == 0) {
1330 		netdev_err(apc->ndev, "Invalid max queues from vPort config\n");
1331 		return -EPROTO;
1332 	}
1333 
1334 	if (resp.num_indirection_ent > 0 &&
1335 	    resp.num_indirection_ent <= MANA_INDIRECT_TABLE_MAX_SIZE &&
1336 	    is_power_of_2(resp.num_indirection_ent)) {
1337 		*num_indir_entry = resp.num_indirection_ent;
1338 	} else {
1339 		netdev_warn(apc->ndev,
1340 			    "Setting indirection table size to default %d for vPort %d\n",
1341 			    MANA_INDIRECT_TABLE_DEF_SIZE, apc->port_idx);
1342 		*num_indir_entry = MANA_INDIRECT_TABLE_DEF_SIZE;
1343 	}
1344 
1345 	apc->port_handle = resp.vport;
1346 	ether_addr_copy(apc->mac_addr, resp.mac_addr);
1347 
1348 	apc->vport_max_sq = *max_sq;
1349 	apc->vport_max_rq = *max_rq;
1350 
1351 	return 0;
1352 }
1353 
mana_uncfg_vport(struct mana_port_context * apc)1354 void mana_uncfg_vport(struct mana_port_context *apc)
1355 {
1356 	mutex_lock(&apc->vport_mutex);
1357 	apc->vport_use_count--;
1358 	WARN_ON(apc->vport_use_count < 0);
1359 	mutex_unlock(&apc->vport_mutex);
1360 }
1361 EXPORT_SYMBOL_NS(mana_uncfg_vport, "NET_MANA");
1362 
mana_cfg_vport(struct mana_port_context * apc,u32 protection_dom_id,u32 doorbell_pg_id,bool check_channel_changing)1363 int mana_cfg_vport(struct mana_port_context *apc, u32 protection_dom_id,
1364 		   u32 doorbell_pg_id, bool check_channel_changing)
1365 {
1366 	struct mana_config_vport_resp resp = {};
1367 	struct mana_config_vport_req req = {};
1368 	int err;
1369 
1370 	/* This function is used to program the Ethernet port in the hardware
1371 	 * table. It can be called from the Ethernet driver or the RDMA driver.
1372 	 *
1373 	 * For Ethernet usage, the hardware supports only one active user on a
1374 	 * physical port. The driver checks on the port usage before programming
1375 	 * the hardware when creating the RAW QP (RDMA driver) or exposing the
1376 	 * device to kernel NET layer (Ethernet driver).
1377 	 *
1378 	 * Because the RDMA driver doesn't know in advance which QP type the
1379 	 * user will create, it exposes the device with all its ports. The user
1380 	 * may not be able to create RAW QP on a port if this port is already
1381 	 * in used by the Ethernet driver from the kernel.
1382 	 *
1383 	 * This physical port limitation only applies to the RAW QP. For RC QP,
1384 	 * the hardware doesn't have this limitation. The user can create RC
1385 	 * QPs on a physical port up to the hardware limits independent of the
1386 	 * Ethernet usage on the same port.
1387 	 */
1388 	mutex_lock(&apc->vport_mutex);
1389 	if (apc->vport_use_count > 0 ||
1390 	    (check_channel_changing && apc->channel_changing)) {
1391 		mutex_unlock(&apc->vport_mutex);
1392 		return -EBUSY;
1393 	}
1394 	apc->vport_use_count++;
1395 	mutex_unlock(&apc->vport_mutex);
1396 
1397 	mana_gd_init_req_hdr(&req.hdr, MANA_CONFIG_VPORT_TX,
1398 			     sizeof(req), sizeof(resp));
1399 	req.vport = apc->port_handle;
1400 	req.pdid = protection_dom_id;
1401 	req.doorbell_pageid = doorbell_pg_id;
1402 
1403 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1404 				sizeof(resp));
1405 	if (err) {
1406 		netdev_err(apc->ndev, "Failed to configure vPort: %d\n", err);
1407 		goto out;
1408 	}
1409 
1410 	err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_TX,
1411 				   sizeof(resp));
1412 	if (err || resp.hdr.status) {
1413 		netdev_err(apc->ndev, "Failed to configure vPort: %d, 0x%x\n",
1414 			   err, resp.hdr.status);
1415 		if (!err)
1416 			err = -EPROTO;
1417 
1418 		goto out;
1419 	}
1420 
1421 	apc->tx_shortform_allowed = resp.short_form_allowed;
1422 	apc->tx_vp_offset = resp.tx_vport_offset;
1423 
1424 	netdev_info(apc->ndev, "Enabled vPort %llu PD %u DB %u MAC %pM\n",
1425 		    apc->port_handle, protection_dom_id, doorbell_pg_id, apc->mac_addr);
1426 out:
1427 	if (err)
1428 		mana_uncfg_vport(apc);
1429 
1430 	return err;
1431 }
1432 EXPORT_SYMBOL_NS(mana_cfg_vport, "NET_MANA");
1433 
mana_cfg_vport_steering(struct mana_port_context * apc,enum TRI_STATE rx,bool update_default_rxobj,bool update_key,bool update_tab)1434 static int mana_cfg_vport_steering(struct mana_port_context *apc,
1435 				   enum TRI_STATE rx,
1436 				   bool update_default_rxobj, bool update_key,
1437 				   bool update_tab)
1438 {
1439 	struct mana_cfg_rx_steer_req_v2 *req;
1440 	struct mana_cfg_rx_steer_resp resp = {};
1441 	struct net_device *ndev = apc->ndev;
1442 	u32 req_buf_size;
1443 	int err;
1444 
1445 	req_buf_size = struct_size(req, indir_tab, apc->indir_table_sz);
1446 	req = kzalloc(req_buf_size, GFP_KERNEL);
1447 	if (!req)
1448 		return -ENOMEM;
1449 
1450 	mana_gd_init_req_hdr(&req->hdr, MANA_CONFIG_VPORT_RX, req_buf_size,
1451 			     sizeof(resp));
1452 
1453 	/* Request & response versions can be different.
1454 	 * HW can handle newer msg versions, by skipping
1455 	 * new fields.
1456 	 */
1457 	req->hdr.req.msg_version = GDMA_MESSAGE_V5;
1458 	req->hdr.resp.msg_version = GDMA_MESSAGE_V2;
1459 
1460 	req->vport = apc->port_handle;
1461 	req->num_indir_entries = apc->indir_table_sz;
1462 	req->indir_tab_offset = offsetof(struct mana_cfg_rx_steer_req_v2,
1463 					 indir_tab);
1464 	req->rx_enable = rx;
1465 	req->rss_enable = apc->rss_state;
1466 	req->update_default_rxobj = update_default_rxobj;
1467 	req->update_hashkey = update_key;
1468 	req->update_indir_tab = update_tab;
1469 	req->default_rxobj = apc->default_rxobj;
1470 
1471 	/* Request-msg v5 requires this field */
1472 	req->rss_hash_types = MANA_HASH_ENABLE_SUPPORTED;
1473 
1474 	if (rx != TRI_STATE_FALSE) {
1475 		req->cqe_coalescing_enable = apc->cqe_coalescing_enable;
1476 		req->cqe8_coalescing_enable = apc->cqe8_coalescing_enable;
1477 	}
1478 
1479 	if (update_key)
1480 		memcpy(&req->hashkey, apc->hashkey, MANA_HASH_KEY_SIZE);
1481 
1482 	if (update_tab)
1483 		memcpy(req->indir_tab, apc->rxobj_table,
1484 		       flex_array_size(req, indir_tab, req->num_indir_entries));
1485 
1486 	err = mana_send_request(apc->ac, req, req_buf_size, &resp,
1487 				sizeof(resp));
1488 	if (err) {
1489 		if (mana_en_need_log(apc, err))
1490 			netdev_err(ndev, "Failed to configure vPort RX: %d\n", err);
1491 
1492 		goto out;
1493 	}
1494 
1495 	err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_RX,
1496 				   sizeof(resp));
1497 	if (err) {
1498 		netdev_err(ndev, "vPort RX configuration failed: %d\n", err);
1499 		goto out;
1500 	}
1501 
1502 	if (resp.hdr.status) {
1503 		netdev_err(ndev, "vPort RX configuration failed: 0x%x\n",
1504 			   resp.hdr.status);
1505 		err = -EPROTO;
1506 		goto out;
1507 	}
1508 
1509 	if (resp.hdr.response.msg_version >= GDMA_MESSAGE_V2)
1510 		apc->cqe_coalescing_timeout_ns =
1511 			resp.cqe_coalescing_timeout_ns;
1512 
1513 	netdev_info(ndev, "Configured steering vPort %llu entries %u\n",
1514 		    apc->port_handle, apc->indir_table_sz);
1515 
1516 	apc->steer_rx = rx;
1517 	apc->steer_rss = apc->rss_state;
1518 	apc->steer_update_tab = update_tab;
1519 	apc->steer_cqe_coalescing = req->cqe_coalescing_enable;
1520 out:
1521 	kfree(req);
1522 	return err;
1523 }
1524 
mana_query_link_cfg(struct mana_port_context * apc)1525 int mana_query_link_cfg(struct mana_port_context *apc)
1526 {
1527 	struct net_device *ndev = apc->ndev;
1528 	struct mana_query_link_config_resp resp = {};
1529 	struct mana_query_link_config_req req = {};
1530 	int err;
1531 
1532 	netdev_assert_locked(ndev);
1533 
1534 	err = apc->link_cfg_error;
1535 	if (err <= 0)
1536 		return err;
1537 
1538 	mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_LINK_CONFIG,
1539 			     sizeof(req), sizeof(resp));
1540 
1541 	req.vport = apc->port_handle;
1542 	req.hdr.resp.msg_version = GDMA_MESSAGE_V2;
1543 
1544 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1545 				sizeof(resp));
1546 
1547 	if (err) {
1548 		if (err == -EOPNOTSUPP) {
1549 			netdev_info_once(ndev, "MANA_QUERY_LINK_CONFIG not supported\n");
1550 			apc->link_cfg_error = err;
1551 			return err;
1552 		}
1553 		netdev_err(ndev, "Failed to query link config: %d\n", err);
1554 		return err;
1555 	}
1556 
1557 	err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_LINK_CONFIG,
1558 				   sizeof(resp));
1559 
1560 	if (err || resp.hdr.status) {
1561 		netdev_err(ndev, "Failed to query link config: %d, 0x%x\n", err,
1562 			   resp.hdr.status);
1563 		if (!err)
1564 			err = -EOPNOTSUPP;
1565 		return err;
1566 	}
1567 
1568 	if (resp.qos_unconfigured)
1569 		return -EINVAL;
1570 
1571 	apc->speed = resp.link_speed_mbps;
1572 	apc->max_speed = resp.qos_speed_mbps;
1573 	apc->link_cfg_error = 0;
1574 	return 0;
1575 }
1576 
mana_set_bw_clamp(struct mana_port_context * apc,u32 speed,int enable_clamping)1577 int mana_set_bw_clamp(struct mana_port_context *apc, u32 speed,
1578 		      int enable_clamping)
1579 {
1580 	struct mana_set_bw_clamp_resp resp = {};
1581 	struct mana_set_bw_clamp_req req = {};
1582 	struct net_device *ndev = apc->ndev;
1583 	int err;
1584 
1585 	netdev_assert_locked(ndev);
1586 
1587 	mana_gd_init_req_hdr(&req.hdr, MANA_SET_BW_CLAMP,
1588 			     sizeof(req), sizeof(resp));
1589 	req.vport = apc->port_handle;
1590 	req.link_speed_mbps = speed;
1591 	req.enable_clamping = enable_clamping;
1592 
1593 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1594 				sizeof(resp));
1595 
1596 	if (err) {
1597 		if (err == -EOPNOTSUPP) {
1598 			netdev_info_once(ndev, "MANA_SET_BW_CLAMP not supported\n");
1599 			return err;
1600 		}
1601 		netdev_err(ndev, "Failed to set bandwidth clamp for speed %u, err = %d",
1602 			   speed, err);
1603 		return err;
1604 	}
1605 
1606 	err = mana_verify_resp_hdr(&resp.hdr, MANA_SET_BW_CLAMP,
1607 				   sizeof(resp));
1608 
1609 	if (err || resp.hdr.status) {
1610 		netdev_err(ndev, "Failed to set bandwidth clamp: %d, 0x%x\n", err,
1611 			   resp.hdr.status);
1612 		if (!err)
1613 			err = -EOPNOTSUPP;
1614 		return err;
1615 	}
1616 
1617 	if (resp.qos_unconfigured)
1618 		netdev_info(ndev, "QoS is unconfigured\n");
1619 
1620 	/* Invalidate the cache; next query will re-fetch from firmware. */
1621 	apc->link_cfg_error = 1;
1622 	return 0;
1623 }
1624 
mana_create_wq_obj(struct mana_port_context * apc,mana_handle_t vport,u32 wq_type,struct mana_obj_spec * wq_spec,struct mana_obj_spec * cq_spec,mana_handle_t * wq_obj)1625 int mana_create_wq_obj(struct mana_port_context *apc,
1626 		       mana_handle_t vport,
1627 		       u32 wq_type, struct mana_obj_spec *wq_spec,
1628 		       struct mana_obj_spec *cq_spec,
1629 		       mana_handle_t *wq_obj)
1630 {
1631 	struct mana_create_wqobj_resp resp = {};
1632 	struct mana_create_wqobj_req req = {};
1633 	struct net_device *ndev = apc->ndev;
1634 	int err;
1635 
1636 	mana_gd_init_req_hdr(&req.hdr, MANA_CREATE_WQ_OBJ,
1637 			     sizeof(req), sizeof(resp));
1638 
1639 	/* Our driver uses different message versions for request and
1640 	 * response in this case.
1641 	 * Our firmware is forward compatible with newer message versions, so
1642 	 * the old firmware still properly handles this message, just the new
1643 	 * feature fields are ignored, and queue creation will be successful.
1644 	 */
1645 	req.hdr.req.msg_version = GDMA_MESSAGE_V3;
1646 	req.hdr.resp.msg_version = GDMA_MESSAGE_V2;
1647 	req.vport = vport;
1648 	req.wq_type = wq_type;
1649 	req.wq_gdma_region = wq_spec->gdma_region;
1650 	req.cq_gdma_region = cq_spec->gdma_region;
1651 	req.wq_size = wq_spec->queue_size;
1652 	req.cq_size = cq_spec->queue_size;
1653 	req.cq_moderation_ctx_id = cq_spec->modr_ctx_id;
1654 	req.cq_parent_qid = cq_spec->attached_eq;
1655 	req.req_cq_moderation = cq_spec->req_cq_moderation;
1656 	req.cq_moderation_comp = cq_spec->cq_moderation_comp;
1657 	req.cq_moderation_usec = cq_spec->cq_moderation_usec;
1658 
1659 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1660 				sizeof(resp));
1661 	if (err) {
1662 		netdev_err(ndev, "Failed to create WQ object: %d\n", err);
1663 		goto out;
1664 	}
1665 
1666 	err = mana_verify_resp_hdr(&resp.hdr, MANA_CREATE_WQ_OBJ,
1667 				   sizeof(resp));
1668 	if (err || resp.hdr.status) {
1669 		netdev_err(ndev, "Failed to create WQ object: %d, 0x%x\n", err,
1670 			   resp.hdr.status);
1671 		if (!err)
1672 			err = -EPROTO;
1673 		goto out;
1674 	}
1675 
1676 	if (resp.wq_obj == INVALID_MANA_HANDLE) {
1677 		netdev_err(ndev, "Got an invalid WQ object handle\n");
1678 		err = -EPROTO;
1679 		goto out;
1680 	}
1681 
1682 	*wq_obj = resp.wq_obj;
1683 	wq_spec->queue_index = resp.wq_id;
1684 	cq_spec->queue_index = resp.cq_id;
1685 
1686 	return 0;
1687 out:
1688 	return err;
1689 }
1690 EXPORT_SYMBOL_NS(mana_create_wq_obj, "NET_MANA");
1691 
mana_destroy_wq_obj(struct mana_port_context * apc,u32 wq_type,mana_handle_t wq_obj)1692 void mana_destroy_wq_obj(struct mana_port_context *apc, u32 wq_type,
1693 			 mana_handle_t wq_obj)
1694 {
1695 	struct mana_destroy_wqobj_resp resp = {};
1696 	struct mana_destroy_wqobj_req req = {};
1697 	struct net_device *ndev = apc->ndev;
1698 	int err;
1699 
1700 	mana_gd_init_req_hdr(&req.hdr, MANA_DESTROY_WQ_OBJ,
1701 			     sizeof(req), sizeof(resp));
1702 	req.wq_type = wq_type;
1703 	req.wq_obj_handle = wq_obj;
1704 
1705 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1706 				sizeof(resp));
1707 	if (err) {
1708 		if (mana_en_need_log(apc, err))
1709 			netdev_err(ndev, "Failed to destroy WQ object: %d\n", err);
1710 
1711 		return;
1712 	}
1713 
1714 	err = mana_verify_resp_hdr(&resp.hdr, MANA_DESTROY_WQ_OBJ,
1715 				   sizeof(resp));
1716 	if (err || resp.hdr.status)
1717 		netdev_err(ndev, "Failed to destroy WQ object: %d, 0x%x\n", err,
1718 			   resp.hdr.status);
1719 }
1720 EXPORT_SYMBOL_NS(mana_destroy_wq_obj, "NET_MANA");
1721 
mana_destroy_eq(struct mana_port_context * apc)1722 void mana_destroy_eq(struct mana_port_context *apc)
1723 {
1724 	struct mana_context *ac = apc->ac;
1725 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
1726 	struct gdma_queue *eq;
1727 	unsigned int msi;
1728 	int i;
1729 
1730 	if (!apc->eqs)
1731 		return;
1732 
1733 	debugfs_remove_recursive(apc->mana_eqs_debugfs);
1734 	apc->mana_eqs_debugfs = NULL;
1735 
1736 	for (i = 0; i < apc->num_queues; i++) {
1737 		eq = apc->eqs[i].eq;
1738 		if (!eq)
1739 			continue;
1740 
1741 		msi = eq->eq.msix_index;
1742 		mana_gd_destroy_queue(gc, eq);
1743 		mana_gd_put_gic(gc, !gc->msi_sharing, msi);
1744 	}
1745 
1746 	kfree(apc->eqs);
1747 	apc->eqs = NULL;
1748 }
1749 EXPORT_SYMBOL_NS(mana_destroy_eq, "NET_MANA");
1750 
mana_create_eq_debugfs(struct mana_port_context * apc,int i)1751 static void mana_create_eq_debugfs(struct mana_port_context *apc, int i)
1752 {
1753 	struct mana_eq eq = apc->eqs[i];
1754 	char eqnum[32];
1755 
1756 	sprintf(eqnum, "eq%d", i);
1757 	eq.mana_eq_debugfs = debugfs_create_dir(eqnum, apc->mana_eqs_debugfs);
1758 	debugfs_create_u32("head", 0400, eq.mana_eq_debugfs, &eq.eq->head);
1759 	debugfs_create_u32("tail", 0400, eq.mana_eq_debugfs, &eq.eq->tail);
1760 	debugfs_create_u32("irq", 0400, eq.mana_eq_debugfs, &eq.eq->eq.irq);
1761 	debugfs_create_file("eq_dump", 0400, eq.mana_eq_debugfs, eq.eq, &mana_dbg_q_fops);
1762 }
1763 
mana_create_eq(struct mana_port_context * apc)1764 int mana_create_eq(struct mana_port_context *apc)
1765 {
1766 	struct gdma_dev *gd = apc->ac->gdma_dev;
1767 	struct gdma_context *gc = gd->gdma_context;
1768 	struct gdma_queue_spec spec = {};
1769 	struct gdma_irq_context *gic;
1770 	int err;
1771 	int msi;
1772 	int i;
1773 
1774 	if (WARN_ON(apc->eqs))
1775 		return -EEXIST;
1776 	apc->eqs = kzalloc_objs(struct mana_eq, apc->num_queues);
1777 	if (!apc->eqs)
1778 		return -ENOMEM;
1779 
1780 	spec.type = GDMA_EQ;
1781 	spec.monitor_avl_buf = false;
1782 	spec.queue_size = EQ_SIZE;
1783 	spec.eq.callback = NULL;
1784 	spec.eq.context = apc->eqs;
1785 	spec.eq.log2_throttle_limit = LOG2_EQ_THROTTLE;
1786 
1787 	apc->mana_eqs_debugfs =
1788 		debugfs_create_dir("EQs", apc->mana_port_debugfs);
1789 
1790 	for (i = 0; i < apc->num_queues; i++) {
1791 		msi = (i + 1) % gc->num_msix_usable;
1792 
1793 		gic = mana_gd_get_gic(gc, !gc->msi_sharing, &msi);
1794 		if (IS_ERR(gic)) {
1795 			err = PTR_ERR(gic);
1796 			goto out;
1797 		}
1798 		spec.eq.msix_index = msi;
1799 
1800 		err = mana_gd_create_mana_eq(gd, &spec, &apc->eqs[i].eq);
1801 		if (err) {
1802 			dev_err(gc->dev, "Failed to create EQ %d : %d\n", i, err);
1803 			mana_gd_put_gic(gc, !gc->msi_sharing, msi);
1804 			goto out;
1805 		}
1806 		apc->eqs[i].eq->eq.irq = gic->irq;
1807 		mana_create_eq_debugfs(apc, i);
1808 	}
1809 
1810 	return 0;
1811 out:
1812 	mana_destroy_eq(apc);
1813 	return err;
1814 }
1815 EXPORT_SYMBOL_NS(mana_create_eq, "NET_MANA");
1816 
mana_fence_rq(struct mana_port_context * apc,struct mana_rxq * rxq)1817 static int mana_fence_rq(struct mana_port_context *apc, struct mana_rxq *rxq)
1818 {
1819 	struct mana_fence_rq_resp resp = {};
1820 	struct mana_fence_rq_req req = {};
1821 	int err;
1822 
1823 	init_completion(&rxq->fence_event);
1824 
1825 	mana_gd_init_req_hdr(&req.hdr, MANA_FENCE_RQ,
1826 			     sizeof(req), sizeof(resp));
1827 	req.wq_obj_handle =  rxq->rxobj;
1828 
1829 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
1830 				sizeof(resp));
1831 	if (err) {
1832 		netdev_err(apc->ndev, "Failed to fence RQ %u: %d\n",
1833 			   rxq->rxq_idx, err);
1834 		return err;
1835 	}
1836 
1837 	err = mana_verify_resp_hdr(&resp.hdr, MANA_FENCE_RQ, sizeof(resp));
1838 	if (err || resp.hdr.status) {
1839 		netdev_err(apc->ndev, "Failed to fence RQ %u: %d, 0x%x\n",
1840 			   rxq->rxq_idx, err, resp.hdr.status);
1841 		if (!err)
1842 			err = -EPROTO;
1843 
1844 		return err;
1845 	}
1846 
1847 	if (wait_for_completion_timeout(&rxq->fence_event, 10 * HZ) == 0) {
1848 		netdev_err(apc->ndev, "Failed to fence RQ %u: timed out\n",
1849 			   rxq->rxq_idx);
1850 		return -ETIMEDOUT;
1851 	}
1852 
1853 	return 0;
1854 }
1855 
mana_fence_rqs(struct mana_port_context * apc)1856 static void mana_fence_rqs(struct mana_port_context *apc)
1857 {
1858 	unsigned int rxq_idx;
1859 	struct mana_rxq *rxq;
1860 	int err;
1861 
1862 	if (!apc->rxqs)
1863 		return;
1864 
1865 	for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) {
1866 		rxq = apc->rxqs[rxq_idx];
1867 		err = mana_fence_rq(apc, rxq);
1868 
1869 		/* In case of any error, use sleep instead. */
1870 		if (err)
1871 			msleep(100);
1872 	}
1873 }
1874 
mana_move_wq_tail(struct gdma_queue * wq,u32 num_units)1875 static int mana_move_wq_tail(struct gdma_queue *wq, u32 num_units)
1876 {
1877 	u32 used_space_old;
1878 	u32 used_space_new;
1879 
1880 	used_space_old = wq->head - wq->tail;
1881 	used_space_new = wq->head - (wq->tail + num_units);
1882 
1883 	if (WARN_ON_ONCE(used_space_new > used_space_old))
1884 		return -ERANGE;
1885 
1886 	wq->tail += num_units;
1887 	return 0;
1888 }
1889 
mana_unmap_skb(struct sk_buff * skb,struct mana_port_context * apc)1890 void mana_unmap_skb(struct sk_buff *skb, struct mana_port_context *apc)
1891 {
1892 	struct mana_skb_head *ash = (struct mana_skb_head *)skb->head;
1893 	struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
1894 	struct device *dev = gc->dev;
1895 	int hsg, i;
1896 
1897 	/* Number of SGEs of linear part */
1898 	hsg = (skb_is_gso(skb) && skb_headlen(skb) > ash->size[0]) ? 2 : 1;
1899 
1900 	for (i = 0; i < hsg; i++)
1901 		dma_unmap_single(dev, ash->dma_handle[i], ash->size[i],
1902 				 DMA_TO_DEVICE);
1903 
1904 	for (i = hsg; i < skb_shinfo(skb)->nr_frags + hsg; i++)
1905 		dma_unmap_page(dev, ash->dma_handle[i], ash->size[i],
1906 			       DMA_TO_DEVICE);
1907 }
1908 
mana_poll_tx_cq(struct mana_cq * cq)1909 static void mana_poll_tx_cq(struct mana_cq *cq)
1910 {
1911 	struct gdma_comp *completions = cq->gdma_comp_buf;
1912 	struct gdma_posted_wqe_info *wqe_info;
1913 	unsigned int pkt_transmitted = 0;
1914 	unsigned int wqe_unit_cnt = 0;
1915 	unsigned int tx_bytes = 0;
1916 	struct mana_txq *txq = cq->txq;
1917 	struct mana_port_context *apc;
1918 	struct netdev_queue *net_txq;
1919 	struct gdma_queue *gdma_wq;
1920 	unsigned int avail_space;
1921 	struct net_device *ndev;
1922 	struct sk_buff *skb;
1923 	bool txq_stopped;
1924 	int comp_read;
1925 	int i;
1926 
1927 	ndev = txq->ndev;
1928 	apc = netdev_priv(ndev);
1929 
1930 	/* Limit CQEs polled to 4 wraparounds of the CQ to ensure the
1931 	 * doorbell can be rung in time for the hardware's requirement
1932 	 * of at least one doorbell ring every 8 wraparounds.
1933 	 */
1934 	comp_read = mana_gd_poll_cq(cq->gdma_cq, completions,
1935 				    min((cq->gdma_cq->queue_size /
1936 					  COMP_ENTRY_SIZE) * 4,
1937 					 CQE_POLLING_BUFFER));
1938 
1939 	if (comp_read < 1)
1940 		return;
1941 
1942 	for (i = 0; i < comp_read; i++) {
1943 		struct mana_tx_comp_oob *cqe_oob;
1944 
1945 		if (WARN_ON_ONCE(!completions[i].is_sq))
1946 			return;
1947 
1948 		cqe_oob = (struct mana_tx_comp_oob *)completions[i].cqe_data;
1949 		if (WARN_ON_ONCE(cqe_oob->cqe_hdr.client_type !=
1950 				 MANA_CQE_COMPLETION))
1951 			return;
1952 
1953 		switch (cqe_oob->cqe_hdr.cqe_type) {
1954 		case CQE_TX_OKAY:
1955 			break;
1956 
1957 		case CQE_TX_SA_DROP:
1958 		case CQE_TX_MTU_DROP:
1959 		case CQE_TX_INVALID_OOB:
1960 		case CQE_TX_INVALID_ETH_TYPE:
1961 		case CQE_TX_HDR_PROCESSING_ERROR:
1962 		case CQE_TX_VF_DISABLED:
1963 		case CQE_TX_VPORT_IDX_OUT_OF_RANGE:
1964 		case CQE_TX_VPORT_DISABLED:
1965 		case CQE_TX_VLAN_TAGGING_VIOLATION:
1966 			if (net_ratelimit())
1967 				netdev_err(ndev, "TX: CQE error %d\n",
1968 					   cqe_oob->cqe_hdr.cqe_type);
1969 
1970 			apc->eth_stats.tx_cqe_err++;
1971 			break;
1972 
1973 		default:
1974 			/* If the CQE type is unknown, log an error,
1975 			 * and still free the SKB, update tail, etc.
1976 			 */
1977 			if (net_ratelimit())
1978 				netdev_err(ndev, "TX: unknown CQE type %d\n",
1979 					   cqe_oob->cqe_hdr.cqe_type);
1980 
1981 			apc->eth_stats.tx_cqe_unknown_type++;
1982 			break;
1983 		}
1984 
1985 		if (WARN_ON_ONCE(txq->gdma_txq_id != completions[i].wq_num))
1986 			return;
1987 
1988 		skb = skb_dequeue(&txq->pending_skbs);
1989 		if (WARN_ON_ONCE(!skb))
1990 			return;
1991 
1992 		wqe_info = (struct gdma_posted_wqe_info *)skb->cb;
1993 		wqe_unit_cnt += wqe_info->wqe_size_in_bu;
1994 
1995 		mana_unmap_skb(skb, apc);
1996 
1997 		tx_bytes += skb->len;
1998 
1999 		napi_consume_skb(skb, cq->budget);
2000 
2001 		pkt_transmitted++;
2002 	}
2003 
2004 	if (WARN_ON_ONCE(wqe_unit_cnt == 0))
2005 		return;
2006 
2007 	mana_move_wq_tail(txq->gdma_sq, wqe_unit_cnt);
2008 
2009 	gdma_wq = txq->gdma_sq;
2010 	avail_space = mana_gd_wq_avail_space(gdma_wq);
2011 
2012 	/* Ensure tail updated before checking q stop */
2013 	smp_mb();
2014 
2015 	net_txq = txq->net_txq;
2016 	txq_stopped = netif_tx_queue_stopped(net_txq);
2017 
2018 	/* Ensure checking txq_stopped before apc->port_is_up. */
2019 	smp_rmb();
2020 
2021 	if (txq_stopped && apc->port_is_up && avail_space >= MAX_TX_WQE_SIZE) {
2022 		netif_tx_wake_queue(net_txq);
2023 		apc->eth_stats.wake_queue++;
2024 	}
2025 
2026 	if (atomic_sub_return(pkt_transmitted, &txq->pending_sends) < 0)
2027 		WARN_ON_ONCE(1);
2028 
2029 	/* Feed DIM with the completion rate observed here, in NAPI context. */
2030 	cq->tx_dim_pkts += pkt_transmitted;
2031 	cq->tx_dim_bytes += tx_bytes;
2032 
2033 	cq->work_done = pkt_transmitted;
2034 }
2035 
mana_post_pkt_rxq(struct mana_rxq * rxq)2036 static void mana_post_pkt_rxq(struct mana_rxq *rxq)
2037 {
2038 	struct mana_recv_buf_oob *recv_buf_oob;
2039 	u32 curr_index;
2040 	int err;
2041 
2042 	curr_index = rxq->buf_index++;
2043 	if (rxq->buf_index == rxq->num_rx_buf)
2044 		rxq->buf_index = 0;
2045 
2046 	recv_buf_oob = &rxq->rx_oobs[curr_index];
2047 
2048 	err = mana_gd_post_work_request(rxq->gdma_rq, &recv_buf_oob->wqe_req,
2049 					&recv_buf_oob->wqe_inf);
2050 	if (WARN_ON_ONCE(err))
2051 		return;
2052 
2053 	WARN_ON_ONCE(recv_buf_oob->wqe_inf.wqe_size_in_bu != 1);
2054 }
2055 
mana_build_skb(struct mana_rxq * rxq,void * buf_va,uint pkt_len,struct xdp_buff * xdp)2056 static struct sk_buff *mana_build_skb(struct mana_rxq *rxq, void *buf_va,
2057 				      uint pkt_len, struct xdp_buff *xdp)
2058 {
2059 	struct sk_buff *skb = napi_build_skb(buf_va, rxq->alloc_size);
2060 
2061 	if (!skb)
2062 		return NULL;
2063 
2064 	if (xdp->data_hard_start) {
2065 		u32 metasize = xdp->data - xdp->data_meta;
2066 
2067 		skb_reserve(skb, xdp->data - xdp->data_hard_start);
2068 		skb_put(skb, xdp->data_end - xdp->data);
2069 		if (metasize)
2070 			skb_metadata_set(skb, metasize);
2071 		return skb;
2072 	}
2073 
2074 	skb_reserve(skb, rxq->headroom);
2075 	skb_put(skb, pkt_len);
2076 
2077 	return skb;
2078 }
2079 
mana_rx_skb(void * buf_va,bool from_pool,struct mana_rxcomp_oob * cqe,struct mana_rxq * rxq,u32 pkt_len,u32 pkt_hash)2080 static void mana_rx_skb(void *buf_va, bool from_pool,
2081 			struct mana_rxcomp_oob *cqe, struct mana_rxq *rxq,
2082 			u32 pkt_len, u32 pkt_hash)
2083 {
2084 	struct mana_stats_rx *rx_stats = &rxq->stats;
2085 	struct net_device *ndev = rxq->ndev;
2086 	u16 rxq_idx = rxq->rxq_idx;
2087 	struct napi_struct *napi;
2088 	struct xdp_buff xdp = {};
2089 	struct sk_buff *skb;
2090 	u32 act;
2091 
2092 	rxq->rx_cq.work_done++;
2093 	napi = &rxq->rx_cq.napi;
2094 
2095 	if (!buf_va) {
2096 		++ndev->stats.rx_dropped;
2097 		return;
2098 	}
2099 
2100 	act = mana_run_xdp(ndev, rxq, &xdp, buf_va, pkt_len);
2101 
2102 	if (act == XDP_REDIRECT && !rxq->xdp_rc)
2103 		return;
2104 
2105 	if (act != XDP_PASS && act != XDP_TX)
2106 		goto drop_xdp;
2107 
2108 	skb = mana_build_skb(rxq, buf_va, pkt_len, &xdp);
2109 
2110 	if (!skb)
2111 		goto drop;
2112 
2113 	if (from_pool)
2114 		skb_mark_for_recycle(skb);
2115 
2116 	skb->dev = napi->dev;
2117 
2118 	skb->protocol = eth_type_trans(skb, ndev);
2119 	skb_checksum_none_assert(skb);
2120 	skb_record_rx_queue(skb, rxq_idx);
2121 
2122 	if ((ndev->features & NETIF_F_RXCSUM) && cqe->rx_iphdr_csum_succeed) {
2123 		if (cqe->rx_tcp_csum_succeed || cqe->rx_udp_csum_succeed)
2124 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2125 	}
2126 
2127 	if (cqe->rx_hashtype != 0 && (ndev->features & NETIF_F_RXHASH)) {
2128 		if (cqe->rx_hashtype & MANA_HASH_L4)
2129 			skb_set_hash(skb, pkt_hash, PKT_HASH_TYPE_L4);
2130 		else
2131 			skb_set_hash(skb, pkt_hash, PKT_HASH_TYPE_L3);
2132 	}
2133 
2134 	if (cqe->rx_vlantag_present) {
2135 		u16 vlan_tci = cqe->rx_vlan_id;
2136 
2137 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
2138 	}
2139 
2140 	u64_stats_update_begin(&rx_stats->syncp);
2141 	rx_stats->packets++;
2142 	rx_stats->bytes += pkt_len;
2143 
2144 	if (act == XDP_TX)
2145 		rx_stats->xdp_tx++;
2146 	u64_stats_update_end(&rx_stats->syncp);
2147 
2148 	if (act == XDP_TX) {
2149 		skb_set_queue_mapping(skb, rxq_idx);
2150 		mana_xdp_tx(skb, ndev);
2151 		return;
2152 	}
2153 
2154 	napi_gro_receive(napi, skb);
2155 
2156 	return;
2157 
2158 drop_xdp:
2159 	u64_stats_update_begin(&rx_stats->syncp);
2160 	rx_stats->xdp_drop++;
2161 	u64_stats_update_end(&rx_stats->syncp);
2162 
2163 drop:
2164 	if (from_pool) {
2165 		if (rxq->frag_count == 1)
2166 			page_pool_recycle_direct(rxq->page_pool,
2167 						 virt_to_head_page(buf_va));
2168 		else
2169 			page_pool_free_va(rxq->page_pool, buf_va, true);
2170 	} else {
2171 		WARN_ON_ONCE(rxq->xdp_save_va);
2172 		/* Save for reuse */
2173 		rxq->xdp_save_va = buf_va;
2174 	}
2175 
2176 	++ndev->stats.rx_dropped;
2177 
2178 	return;
2179 }
2180 
mana_get_rxfrag(struct mana_rxq * rxq,struct device * dev,dma_addr_t * da,bool * from_pool,struct page ** pp_page,u32 * dma_sync_offset)2181 static void *mana_get_rxfrag(struct mana_rxq *rxq, struct device *dev,
2182 			     dma_addr_t *da, bool *from_pool,
2183 			     struct page **pp_page, u32 *dma_sync_offset)
2184 {
2185 	struct page *page;
2186 	u32 offset;
2187 	void *va;
2188 
2189 	*from_pool = false;
2190 	*pp_page = NULL;
2191 	*dma_sync_offset = 0;
2192 
2193 	/* Don't use fragments for jumbo frames or XDP where it's 1 fragment
2194 	 * per page.
2195 	 */
2196 	if (rxq->frag_count == 1) {
2197 		/* Reuse XDP dropped page if available */
2198 		if (rxq->xdp_save_va) {
2199 			va = rxq->xdp_save_va;
2200 			page = virt_to_head_page(va);
2201 			rxq->xdp_save_va = NULL;
2202 		} else {
2203 			page = page_pool_dev_alloc_pages(rxq->page_pool);
2204 			if (!page)
2205 				return NULL;
2206 
2207 			*from_pool = true;
2208 			va = page_to_virt(page);
2209 		}
2210 
2211 		*da = dma_map_single(dev, va + rxq->headroom, rxq->datasize,
2212 				     DMA_FROM_DEVICE);
2213 		if (dma_mapping_error(dev, *da)) {
2214 			mana_put_rx_page(rxq, page, *from_pool);
2215 			return NULL;
2216 		}
2217 
2218 		return va;
2219 	}
2220 
2221 	page =  page_pool_dev_alloc_frag(rxq->page_pool, &offset,
2222 					 rxq->alloc_size);
2223 	if (!page)
2224 		return NULL;
2225 
2226 	va  = page_to_virt(page) + offset;
2227 	*da = page_pool_get_dma_addr(page) + offset + rxq->headroom;
2228 	*from_pool = true;
2229 	*pp_page = page;
2230 	*dma_sync_offset = offset + rxq->headroom;
2231 
2232 	return va;
2233 }
2234 
2235 /* Allocate frag for rx buffer, and save the old buf */
mana_refill_rx_oob(struct device * dev,struct mana_rxq * rxq,struct mana_recv_buf_oob * rxoob,u32 pktlen,void ** old_buf,bool * old_fp)2236 static void mana_refill_rx_oob(struct device *dev, struct mana_rxq *rxq,
2237 			       struct mana_recv_buf_oob *rxoob, u32 pktlen,
2238 			       void **old_buf, bool *old_fp)
2239 {
2240 	struct page *pp_page;
2241 	u32 dma_sync_offset;
2242 	bool from_pool;
2243 	dma_addr_t da;
2244 	void *va;
2245 
2246 	va = mana_get_rxfrag(rxq, dev, &da, &from_pool, &pp_page,
2247 			     &dma_sync_offset);
2248 	if (!va)
2249 		return;
2250 	if (!rxoob->from_pool || rxq->frag_count == 1) {
2251 		dma_unmap_single(dev, rxoob->sgl[0].address, rxq->datasize,
2252 				 DMA_FROM_DEVICE);
2253 	} else {
2254 		/* The page pool maps the whole page and only syncs for device
2255 		 * automatically (PP_FLAG_DMA_SYNC_DEV). Sync the received bytes
2256 		 * for the CPU before they are read: this is required if DMA
2257 		 * is incoherent or bounce buffers are used.
2258 		 */
2259 		page_pool_dma_sync_for_cpu(rxq->page_pool, rxoob->pp_page,
2260 					   rxoob->dma_sync_offset, pktlen);
2261 	}
2262 	*old_buf = rxoob->buf_va;
2263 	*old_fp = rxoob->from_pool;
2264 
2265 	rxoob->buf_va = va;
2266 	rxoob->sgl[0].address = da;
2267 	rxoob->from_pool = from_pool;
2268 	rxoob->pp_page = pp_page;
2269 	rxoob->dma_sync_offset = dma_sync_offset;
2270 }
2271 
mana_process_one_rx_pkt(struct device * dev,struct mana_rxq * rxq,struct mana_rxcomp_oob * oob,u32 pktlen,u32 pkt_hash)2272 static void mana_process_one_rx_pkt(struct device *dev, struct mana_rxq *rxq,
2273 				    struct mana_rxcomp_oob *oob,
2274 				    u32 pktlen, u32 pkt_hash)
2275 {
2276 	struct mana_recv_buf_oob *rxbuf_oob;
2277 	struct net_device *ndev = rxq->ndev;
2278 	void *old_buf = NULL;
2279 	bool old_fp;
2280 
2281 	rxbuf_oob = &rxq->rx_oobs[rxq->buf_index];
2282 	WARN_ON_ONCE(rxbuf_oob->wqe_inf.wqe_size_in_bu != 1);
2283 
2284 	if (unlikely(pktlen > rxq->datasize)) {
2285 		/* Increase it even if mana_rx_skb() isn't called. */
2286 		rxq->rx_cq.work_done++;
2287 
2288 		++ndev->stats.rx_dropped;
2289 		netdev_warn_once(ndev,
2290 				 "Dropped oversized RX packet: len=%u, datasize=%u\n",
2291 				 pktlen, rxq->datasize);
2292 
2293 		/* Reuse the RX buffer since rxbuf_oob is unchanged. */
2294 	} else {
2295 		mana_refill_rx_oob(dev, rxq, rxbuf_oob, pktlen,
2296 				   &old_buf, &old_fp);
2297 
2298 		/* Unsuccessful refill will have old_buf == NULL.
2299 		 * In this case, mana_rx_skb() will drop the packet.
2300 		 */
2301 		mana_rx_skb(old_buf, old_fp, oob, rxq, pktlen, pkt_hash);
2302 	}
2303 
2304 	mana_move_wq_tail(rxq->gdma_rq, rxbuf_oob->wqe_inf.wqe_size_in_bu);
2305 
2306 	mana_post_pkt_rxq(rxq);
2307 }
2308 
mana_process_rx_cqe(struct mana_rxq * rxq,struct mana_cq * cq,struct gdma_comp * cqe)2309 static void mana_process_rx_cqe(struct mana_rxq *rxq, struct mana_cq *cq,
2310 				struct gdma_comp *cqe)
2311 {
2312 	struct mana_rxcomp_oob *oob = (struct mana_rxcomp_oob *)cqe->cqe_data;
2313 	struct gdma_context *gc = rxq->gdma_rq->gdma_dev->gdma_context;
2314 	struct net_device *ndev = rxq->ndev;
2315 	struct mana_recv_buf_oob *rxbuf_oob;
2316 	struct mana_port_context *apc;
2317 	struct device *dev = gc->dev;
2318 	bool coalesced_8 = false;
2319 	bool coalesced = false;
2320 	u32 pktlen;
2321 	int pkt_i;
2322 	int i;
2323 
2324 	apc = netdev_priv(ndev);
2325 
2326 	switch (oob->cqe_hdr.cqe_type) {
2327 	case CQE_RX_OKAY:
2328 		break;
2329 
2330 	case CQE_RX_TRUNCATED:
2331 		++ndev->stats.rx_dropped;
2332 		rxbuf_oob = &rxq->rx_oobs[rxq->buf_index];
2333 		netdev_warn_once(ndev, "Dropped a truncated packet\n");
2334 
2335 		mana_move_wq_tail(rxq->gdma_rq,
2336 				  rxbuf_oob->wqe_inf.wqe_size_in_bu);
2337 		mana_post_pkt_rxq(rxq);
2338 		return;
2339 
2340 	case CQE_RX_COALESCED_4:
2341 		coalesced = true;
2342 		break;
2343 
2344 	case CQE_RX_COALESCED_8:
2345 		coalesced = true;
2346 		coalesced_8 = true;
2347 		break;
2348 
2349 	case CQE_RX_OBJECT_FENCE:
2350 		complete(&rxq->fence_event);
2351 		return;
2352 
2353 	default:
2354 		netdev_err(ndev, "Unknown RX CQE type = %d\n",
2355 			   oob->cqe_hdr.cqe_type);
2356 		apc->eth_stats.rx_cqe_unknown_type++;
2357 		return;
2358 	}
2359 
2360 	pkt_i = 0;
2361 	for (i = 0; i < MANA_RXCOMP_OOB_NUM_PPI; i++) {
2362 		u32 pkt_hash;
2363 
2364 		if (coalesced_8) {
2365 			/* 8-pkt mode: 2 packets per PPI entry */
2366 			pktlen = oob->ppi[i].pkt_len0;
2367 			pkt_hash = oob->ppi[i].pkt_hash0;
2368 		} else {
2369 			pktlen = oob->ppi[i].pkt_len;
2370 			pkt_hash = oob->ppi[i].pkt_hash;
2371 		}
2372 		if (pktlen == 0)
2373 			break;
2374 
2375 		mana_process_one_rx_pkt(dev, rxq, oob, pktlen, pkt_hash);
2376 		pkt_i++;
2377 
2378 		if (!coalesced)
2379 			break;
2380 
2381 		/* Process 2nd packet from the same PPI in 8-pkt mode */
2382 		if (coalesced_8) {
2383 			pktlen = oob->ppi[i].pkt_len1;
2384 			pkt_hash = oob->ppi[i].pkt_hash1;
2385 			if (pktlen == 0)
2386 				break;
2387 
2388 			mana_process_one_rx_pkt(dev, rxq, oob, pktlen,
2389 						pkt_hash);
2390 			pkt_i++;
2391 		}
2392 	}
2393 
2394 	/* Collect coalesced CQE count based on packets processed.
2395 	 * Coalesced CQEs have at least 2 packets, so index is pkt_i - 2.
2396 	 */
2397 	if (pkt_i > 1) {
2398 		u64_stats_update_begin(&rxq->stats.syncp);
2399 		rxq->stats.coalesced_cqe[pkt_i - 2]++;
2400 		u64_stats_update_end(&rxq->stats.syncp);
2401 	} else if (!pkt_i && !pktlen) {
2402 		u64_stats_update_begin(&rxq->stats.syncp);
2403 		rxq->stats.pkt_len0_err++;
2404 		u64_stats_update_end(&rxq->stats.syncp);
2405 		netdev_err_once(ndev,
2406 				"RX pkt len=0, rq=%u, cq=%u, rxobj=0x%llx\n",
2407 				rxq->gdma_id, cq->gdma_id, rxq->rxobj);
2408 	}
2409 }
2410 
mana_poll_rx_cq(struct mana_cq * cq)2411 static void mana_poll_rx_cq(struct mana_cq *cq)
2412 {
2413 	struct gdma_comp *comp = cq->gdma_comp_buf;
2414 	struct mana_rxq *rxq = cq->rxq;
2415 	int comp_read, i;
2416 
2417 	/* Limit CQEs polled to 4 wraparounds of the CQ to ensure the
2418 	 * doorbell can be rung in time for the hardware's requirement
2419 	 * of at least one doorbell ring every 8 wraparounds.
2420 	 */
2421 	comp_read = mana_gd_poll_cq(cq->gdma_cq, comp,
2422 				    min((cq->gdma_cq->queue_size /
2423 					  COMP_ENTRY_SIZE) * 4,
2424 					 CQE_POLLING_BUFFER));
2425 	WARN_ON_ONCE(comp_read > CQE_POLLING_BUFFER);
2426 
2427 	rxq->xdp_flush = false;
2428 
2429 	for (i = 0; i < comp_read; i++) {
2430 		if (WARN_ON_ONCE(comp[i].is_sq))
2431 			return;
2432 
2433 		/* verify recv cqe references the right rxq */
2434 		if (WARN_ON_ONCE(comp[i].wq_num != cq->rxq->gdma_id))
2435 			return;
2436 
2437 		mana_process_rx_cqe(rxq, cq, &comp[i]);
2438 	}
2439 
2440 	if (comp_read > 0) {
2441 		struct gdma_context *gc = rxq->gdma_rq->gdma_dev->gdma_context;
2442 
2443 		mana_gd_wq_ring_doorbell(gc, rxq->gdma_rq);
2444 	}
2445 
2446 	if (rxq->xdp_flush)
2447 		xdp_do_flush();
2448 }
2449 
mana_rx_dim_work(struct work_struct * work)2450 static void mana_rx_dim_work(struct work_struct *work)
2451 {
2452 	struct dim *dim = container_of(work, struct dim, work);
2453 	struct dim_cq_moder cur_moder;
2454 	struct mana_cq *cq;
2455 
2456 	cur_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
2457 	cq = container_of(dim, struct mana_cq, dim);
2458 
2459 	cur_moder.usec = min_t(u16, cur_moder.usec, MANA_INTR_MODR_USEC_MAX);
2460 	cur_moder.pkts = min_t(u16, cur_moder.pkts, MANA_INTR_MODR_COMP_MAX);
2461 
2462 	mana_gd_ring_dim(cq->gdma_cq, cur_moder.usec, true,
2463 			 cur_moder.pkts, true);
2464 
2465 	dim->state = DIM_START_MEASURE;
2466 }
2467 
mana_tx_dim_work(struct work_struct * work)2468 static void mana_tx_dim_work(struct work_struct *work)
2469 {
2470 	struct dim *dim = container_of(work, struct dim, work);
2471 	struct dim_cq_moder cur_moder;
2472 	struct mana_cq *cq;
2473 
2474 	cur_moder = net_dim_get_tx_moderation(dim->mode, dim->profile_ix);
2475 	cq = container_of(dim, struct mana_cq, dim);
2476 
2477 	cur_moder.usec = min_t(u16, cur_moder.usec, MANA_INTR_MODR_USEC_MAX);
2478 	cur_moder.pkts = min_t(u16, cur_moder.pkts, MANA_INTR_MODR_COMP_MAX);
2479 
2480 	mana_gd_ring_dim(cq->gdma_cq, cur_moder.usec, true,
2481 			 cur_moder.pkts, true);
2482 
2483 	dim->state = DIM_START_MEASURE;
2484 }
2485 
2486 /* The caller must update apc->rx/tx_dim_enabled before disabling and
2487  * after enabling. And synchronize_net() before draining the DIM work,
2488  * so that NAPI cannot observe a stale flag.
2489  */
mana_dim_change(struct mana_cq * cq,bool enable)2490 void mana_dim_change(struct mana_cq *cq, bool enable)
2491 {
2492 	bool is_rx = cq->type == MANA_CQ_TYPE_RX;
2493 	struct mana_port_context *apc;
2494 	work_func_t work_func;
2495 	u32 usec, comp;
2496 
2497 	if (is_rx) {
2498 		apc = netdev_priv(cq->rxq->ndev);
2499 		usec = apc->intr_modr_rx_usec;
2500 		comp = apc->intr_modr_rx_comp;
2501 		work_func = mana_rx_dim_work;
2502 	} else {
2503 		apc = netdev_priv(cq->txq->ndev);
2504 		usec = apc->intr_modr_tx_usec;
2505 		comp = apc->intr_modr_tx_comp;
2506 		work_func = mana_tx_dim_work;
2507 	}
2508 
2509 	/* On enable, zero the DIM state so net_dim() starts measuring from
2510 	 * scratch.
2511 	 * On disable, drain any pending DIM work and restore the static
2512 	 * moderation values.
2513 	 */
2514 	if (enable) {
2515 		memset(&cq->dim, 0, sizeof(cq->dim));
2516 		cq->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2517 		INIT_WORK(&cq->dim.work, work_func);
2518 	} else {
2519 		cancel_work_sync(&cq->dim.work);
2520 		mana_gd_ring_dim(cq->gdma_cq, usec, true, comp, true);
2521 	}
2522 }
2523 
mana_update_rx_dim(struct mana_cq * cq)2524 static void mana_update_rx_dim(struct mana_cq *cq)
2525 {
2526 	struct mana_port_context *apc = netdev_priv(cq->rxq->ndev);
2527 	struct dim_sample dim_sample = {};
2528 	struct mana_rxq *rxq = cq->rxq;
2529 
2530 	/* Pairs with smp_store_release() in mana_set_coalesce(): observing the
2531 	 * enable flag set guarantees the DIM (re)initialization is visible.
2532 	 */
2533 	if (!smp_load_acquire(&apc->rx_dim_enabled))
2534 		return;
2535 
2536 	dim_update_sample(READ_ONCE(cq->dim_event_ctr), rxq->stats.packets,
2537 			  rxq->stats.bytes, &dim_sample);
2538 	net_dim(&cq->dim, &dim_sample);
2539 }
2540 
mana_update_tx_dim(struct mana_cq * cq)2541 static void mana_update_tx_dim(struct mana_cq *cq)
2542 {
2543 	struct mana_port_context *apc = netdev_priv(cq->txq->ndev);
2544 	struct dim_sample dim_sample = {};
2545 
2546 	/* Pairs with smp_store_release() in mana_set_coalesce(): observing the
2547 	 * enable flag set guarantees the DIM (re)initialization is visible.
2548 	 */
2549 	if (!smp_load_acquire(&apc->tx_dim_enabled))
2550 		return;
2551 
2552 	/* cq->tx_dim_pkts/bytes are accumulated in mana_poll_tx_cq(), in the
2553 	 * same NAPI context as this read, so they track the hardware
2554 	 * completion rate and need no u64_stats_sync protection.
2555 	 */
2556 	dim_update_sample(READ_ONCE(cq->dim_event_ctr), cq->tx_dim_pkts,
2557 			  cq->tx_dim_bytes, &dim_sample);
2558 	net_dim(&cq->dim, &dim_sample);
2559 }
2560 
mana_cq_handler(void * context,struct gdma_queue * gdma_queue)2561 static int mana_cq_handler(void *context, struct gdma_queue *gdma_queue)
2562 {
2563 	struct mana_cq *cq = context;
2564 	int w;
2565 
2566 	WARN_ON_ONCE(cq->gdma_cq != gdma_queue);
2567 
2568 	if (cq->type == MANA_CQ_TYPE_RX)
2569 		mana_poll_rx_cq(cq);
2570 	else
2571 		mana_poll_tx_cq(cq);
2572 
2573 	w = cq->work_done;
2574 	cq->work_done_since_doorbell += w;
2575 
2576 	if (w < cq->budget) {
2577 		mana_gd_ring_cq(gdma_queue, SET_ARM_BIT);
2578 		cq->work_done_since_doorbell = 0;
2579 
2580 		/* Update DIM before napi_complete_done() to prevent running
2581 		 * net_dim() concurrently.
2582 		 */
2583 		if (cq->type == MANA_CQ_TYPE_RX)
2584 			mana_update_rx_dim(cq);
2585 		else
2586 			mana_update_tx_dim(cq);
2587 
2588 		napi_complete_done(&cq->napi, w);
2589 	} else if (cq->work_done_since_doorbell >=
2590 		   (cq->gdma_cq->queue_size / COMP_ENTRY_SIZE) * 4) {
2591 		/* MANA hardware requires at least one doorbell ring every 8
2592 		 * wraparounds of CQ even if there is no need to arm the CQ.
2593 		 * This driver rings the doorbell as soon as it has processed
2594 		 * 4 wraparounds.
2595 		 */
2596 		mana_gd_ring_cq(gdma_queue, 0);
2597 		cq->work_done_since_doorbell = 0;
2598 	}
2599 
2600 	return w;
2601 }
2602 
mana_poll(struct napi_struct * napi,int budget)2603 static int mana_poll(struct napi_struct *napi, int budget)
2604 {
2605 	struct mana_cq *cq = container_of(napi, struct mana_cq, napi);
2606 	int w;
2607 
2608 	cq->work_done = 0;
2609 	cq->budget = budget;
2610 
2611 	w = mana_cq_handler(cq, cq->gdma_cq);
2612 
2613 	return min(w, budget);
2614 }
2615 
mana_schedule_napi(void * context,struct gdma_queue * gdma_queue)2616 static void mana_schedule_napi(void *context, struct gdma_queue *gdma_queue)
2617 {
2618 	struct mana_cq *cq = context;
2619 
2620 	WRITE_ONCE(cq->dim_event_ctr, cq->dim_event_ctr + 1);
2621 	napi_schedule_irqoff(&cq->napi);
2622 }
2623 
mana_deinit_cq(struct mana_port_context * apc,struct mana_cq * cq)2624 static void mana_deinit_cq(struct mana_port_context *apc, struct mana_cq *cq)
2625 {
2626 	struct gdma_dev *gd = apc->ac->gdma_dev;
2627 
2628 	if (!cq->gdma_cq)
2629 		return;
2630 
2631 	mana_gd_destroy_queue(gd->gdma_context, cq->gdma_cq);
2632 }
2633 
mana_deinit_txq(struct mana_port_context * apc,struct mana_txq * txq)2634 static void mana_deinit_txq(struct mana_port_context *apc, struct mana_txq *txq)
2635 {
2636 	struct gdma_dev *gd = apc->ac->gdma_dev;
2637 
2638 	if (!txq->gdma_sq)
2639 		return;
2640 
2641 	mana_gd_destroy_queue(gd->gdma_context, txq->gdma_sq);
2642 }
2643 
mana_destroy_txq(struct mana_port_context * apc)2644 static void mana_destroy_txq(struct mana_port_context *apc)
2645 {
2646 	struct napi_struct *napi;
2647 	int i;
2648 
2649 	if (!apc->tx_qp)
2650 		return;
2651 
2652 	for (i = 0; i < apc->num_queues; i++) {
2653 		if (!apc->tx_qp[i])
2654 			continue;
2655 
2656 		debugfs_remove_recursive(apc->tx_qp[i]->mana_tx_debugfs);
2657 		apc->tx_qp[i]->mana_tx_debugfs = NULL;
2658 
2659 		napi = &apc->tx_qp[i]->tx_cq.napi;
2660 		if (apc->tx_qp[i]->txq.napi_initialized) {
2661 			napi_synchronize(napi);
2662 			napi_disable_locked(napi);
2663 			cancel_work_sync(&apc->tx_qp[i]->tx_cq.dim.work);
2664 			netif_napi_del_locked(napi);
2665 			apc->tx_qp[i]->txq.napi_initialized = false;
2666 		}
2667 
2668 		if (apc->tx_qp[i]->tx_object != INVALID_MANA_HANDLE)
2669 			mana_destroy_wq_obj(apc, GDMA_SQ, apc->tx_qp[i]->tx_object);
2670 
2671 		mana_deinit_cq(apc, &apc->tx_qp[i]->tx_cq);
2672 
2673 		mana_deinit_txq(apc, &apc->tx_qp[i]->txq);
2674 
2675 		kvfree(apc->tx_qp[i]);
2676 	}
2677 
2678 	kfree(apc->tx_qp);
2679 	apc->tx_qp = NULL;
2680 }
2681 
mana_create_txq_debugfs(struct mana_port_context * apc,int idx)2682 static void mana_create_txq_debugfs(struct mana_port_context *apc, int idx)
2683 {
2684 	struct mana_tx_qp *tx_qp = apc->tx_qp[idx];
2685 	char qnum[32];
2686 
2687 	sprintf(qnum, "TX-%d", idx);
2688 	tx_qp->mana_tx_debugfs = debugfs_create_dir(qnum, apc->mana_port_debugfs);
2689 	debugfs_create_u32("sq_head", 0400, tx_qp->mana_tx_debugfs,
2690 			   &tx_qp->txq.gdma_sq->head);
2691 	debugfs_create_u32("sq_tail", 0400, tx_qp->mana_tx_debugfs,
2692 			   &tx_qp->txq.gdma_sq->tail);
2693 	debugfs_create_u32("sq_pend_skb_qlen", 0400, tx_qp->mana_tx_debugfs,
2694 			   &tx_qp->txq.pending_skbs.qlen);
2695 	debugfs_create_u32("cq_head", 0400, tx_qp->mana_tx_debugfs,
2696 			   &tx_qp->tx_cq.gdma_cq->head);
2697 	debugfs_create_u32("cq_tail", 0400, tx_qp->mana_tx_debugfs,
2698 			   &tx_qp->tx_cq.gdma_cq->tail);
2699 	debugfs_create_u32("cq_budget", 0400, tx_qp->mana_tx_debugfs,
2700 			   &tx_qp->tx_cq.budget);
2701 	debugfs_create_file("txq_dump", 0400, tx_qp->mana_tx_debugfs,
2702 			    tx_qp->txq.gdma_sq, &mana_dbg_q_fops);
2703 	debugfs_create_file("cq_dump", 0400, tx_qp->mana_tx_debugfs,
2704 			    tx_qp->tx_cq.gdma_cq, &mana_dbg_q_fops);
2705 }
2706 
mana_create_txq(struct mana_port_context * apc,struct net_device * net)2707 static int mana_create_txq(struct mana_port_context *apc,
2708 			   struct net_device *net)
2709 {
2710 	struct mana_context *ac = apc->ac;
2711 	struct gdma_dev *gd = ac->gdma_dev;
2712 	struct mana_obj_spec wq_spec;
2713 	struct mana_obj_spec cq_spec;
2714 	struct gdma_queue_spec spec;
2715 	struct gdma_context *gc;
2716 	struct mana_txq *txq;
2717 	struct mana_cq *cq;
2718 	u32 txq_size;
2719 	u32 cq_size;
2720 	int err;
2721 	int i;
2722 
2723 	apc->tx_qp = kzalloc_objs(struct mana_tx_qp *, apc->num_queues);
2724 	if (!apc->tx_qp)
2725 		return -ENOMEM;
2726 
2727 	/*  The minimum size of the WQE is 32 bytes, hence
2728 	 *  apc->tx_queue_size represents the maximum number of WQEs
2729 	 *  the SQ can store. This value is then used to size other queues
2730 	 *  to prevent overflow.
2731 	 *  Also note that the txq_size is always going to be MANA_PAGE_ALIGNED,
2732 	 *  as min val of apc->tx_queue_size is 128 and that would make
2733 	 *  txq_size 128*32 = 4096 and the other higher values of apc->tx_queue_size
2734 	 *  are always power of two
2735 	 */
2736 	txq_size = apc->tx_queue_size * 32;
2737 
2738 	cq_size = apc->tx_queue_size * COMP_ENTRY_SIZE;
2739 
2740 	gc = gd->gdma_context;
2741 
2742 	for (i = 0; i < apc->num_queues; i++) {
2743 		apc->tx_qp[i] = kvzalloc_obj(*apc->tx_qp[i]);
2744 		if (!apc->tx_qp[i]) {
2745 			err = -ENOMEM;
2746 			goto out;
2747 		}
2748 
2749 		apc->tx_qp[i]->tx_object = INVALID_MANA_HANDLE;
2750 
2751 		/* Create SQ */
2752 		txq = &apc->tx_qp[i]->txq;
2753 
2754 		u64_stats_init(&txq->stats.syncp);
2755 		txq->ndev = net;
2756 		txq->net_txq = netdev_get_tx_queue(net, i);
2757 		txq->vp_offset = apc->tx_vp_offset;
2758 		txq->napi_initialized = false;
2759 		skb_queue_head_init(&txq->pending_skbs);
2760 
2761 		memset(&spec, 0, sizeof(spec));
2762 		spec.type = GDMA_SQ;
2763 		spec.monitor_avl_buf = true;
2764 		spec.queue_size = txq_size;
2765 		err = mana_gd_create_mana_wq_cq(gd, &spec, &txq->gdma_sq);
2766 		if (err)
2767 			goto out;
2768 
2769 		/* Create SQ's CQ */
2770 		cq = &apc->tx_qp[i]->tx_cq;
2771 		cq->type = MANA_CQ_TYPE_TX;
2772 
2773 		cq->txq = txq;
2774 
2775 		memset(&spec, 0, sizeof(spec));
2776 		spec.type = GDMA_CQ;
2777 		spec.monitor_avl_buf = false;
2778 		spec.queue_size = cq_size;
2779 		spec.cq.callback = mana_schedule_napi;
2780 		spec.cq.parent_eq = apc->eqs[i].eq;
2781 		spec.cq.context = cq;
2782 		err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq);
2783 		if (err)
2784 			goto out;
2785 
2786 		memset(&wq_spec, 0, sizeof(wq_spec));
2787 		memset(&cq_spec, 0, sizeof(cq_spec));
2788 
2789 		wq_spec.gdma_region = txq->gdma_sq->mem_info.dma_region_handle;
2790 		wq_spec.queue_size = txq->gdma_sq->queue_size;
2791 
2792 		cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle;
2793 		cq_spec.queue_size = cq->gdma_cq->queue_size;
2794 		cq_spec.modr_ctx_id = 0;
2795 		cq_spec.attached_eq = cq->gdma_cq->cq.parent->id;
2796 
2797 		/* DIM setting can be changed at runtime */
2798 		cq_spec.req_cq_moderation = true;
2799 		cq_spec.cq_moderation_usec = apc->intr_modr_tx_usec;
2800 		cq_spec.cq_moderation_comp = apc->intr_modr_tx_comp;
2801 
2802 		err = mana_create_wq_obj(apc, apc->port_handle, GDMA_SQ,
2803 					 &wq_spec, &cq_spec,
2804 					 &apc->tx_qp[i]->tx_object);
2805 
2806 		if (err)
2807 			goto out;
2808 
2809 		txq->gdma_sq->id = wq_spec.queue_index;
2810 		cq->gdma_cq->id = cq_spec.queue_index;
2811 
2812 		txq->gdma_sq->mem_info.dma_region_handle =
2813 			GDMA_INVALID_DMA_REGION;
2814 		cq->gdma_cq->mem_info.dma_region_handle =
2815 			GDMA_INVALID_DMA_REGION;
2816 
2817 		txq->gdma_txq_id = txq->gdma_sq->id;
2818 
2819 		cq->gdma_id = cq->gdma_cq->id;
2820 
2821 		if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) {
2822 			err = -EINVAL;
2823 			goto out;
2824 		}
2825 
2826 		gc->cq_table[cq->gdma_id] = cq->gdma_cq;
2827 
2828 		mana_create_txq_debugfs(apc, i);
2829 
2830 		set_bit(NAPI_STATE_NO_BUSY_POLL, &cq->napi.state);
2831 		netif_napi_add_locked(net, &cq->napi, mana_poll);
2832 
2833 		/* Initialize the DIM work before enabling NAPI, so that a poll
2834 		 * cannot reach net_dim() with an uninitialized cq->dim.work.
2835 		 */
2836 		INIT_WORK(&cq->dim.work, mana_tx_dim_work);
2837 		cq->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2838 
2839 		napi_enable_locked(&cq->napi);
2840 		txq->napi_initialized = true;
2841 
2842 		mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT);
2843 	}
2844 
2845 	return 0;
2846 out:
2847 	netdev_err(net, "Failed to create %d TX queues, %d\n",
2848 		   apc->num_queues, err);
2849 	mana_destroy_txq(apc);
2850 	return err;
2851 }
2852 
mana_destroy_rxq(struct mana_port_context * apc,struct mana_rxq * rxq,bool napi_initialized)2853 static void mana_destroy_rxq(struct mana_port_context *apc,
2854 			     struct mana_rxq *rxq, bool napi_initialized)
2855 
2856 {
2857 	struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
2858 	struct mana_recv_buf_oob *rx_oob;
2859 	struct device *dev = gc->dev;
2860 	struct napi_struct *napi;
2861 	struct page *page;
2862 	int i;
2863 
2864 	if (!rxq)
2865 		return;
2866 
2867 	debugfs_remove_recursive(rxq->mana_rx_debugfs);
2868 	rxq->mana_rx_debugfs = NULL;
2869 
2870 	napi = &rxq->rx_cq.napi;
2871 
2872 	if (napi_initialized) {
2873 		napi_synchronize(napi);
2874 
2875 		napi_disable_locked(napi);
2876 		cancel_work_sync(&rxq->rx_cq.dim.work);
2877 		netif_napi_del_locked(napi);
2878 	}
2879 
2880 	if (xdp_rxq_info_is_reg(&rxq->xdp_rxq))
2881 		xdp_rxq_info_unreg(&rxq->xdp_rxq);
2882 
2883 	if (rxq->rxobj != INVALID_MANA_HANDLE)
2884 		mana_destroy_wq_obj(apc, GDMA_RQ, rxq->rxobj);
2885 
2886 	mana_deinit_cq(apc, &rxq->rx_cq);
2887 
2888 	if (rxq->xdp_save_va)
2889 		put_page(virt_to_head_page(rxq->xdp_save_va));
2890 
2891 	for (i = 0; i < rxq->num_rx_buf; i++) {
2892 		rx_oob = &rxq->rx_oobs[i];
2893 
2894 		if (!rx_oob->buf_va)
2895 			continue;
2896 
2897 		page = virt_to_head_page(rx_oob->buf_va);
2898 
2899 		if (rxq->frag_count == 1 || !rx_oob->from_pool) {
2900 			dma_unmap_single(dev, rx_oob->sgl[0].address,
2901 					 rx_oob->sgl[0].size, DMA_FROM_DEVICE);
2902 			mana_put_rx_page(rxq, page, rx_oob->from_pool);
2903 		} else {
2904 			page_pool_free_va(rxq->page_pool, rx_oob->buf_va, true);
2905 		}
2906 
2907 		rx_oob->buf_va = NULL;
2908 	}
2909 
2910 	page_pool_destroy(rxq->page_pool);
2911 
2912 	if (rxq->gdma_rq)
2913 		mana_gd_destroy_queue(gc, rxq->gdma_rq);
2914 
2915 	kvfree(rxq);
2916 }
2917 
mana_fill_rx_oob(struct mana_recv_buf_oob * rx_oob,u32 mem_key,struct mana_rxq * rxq,struct device * dev)2918 static int mana_fill_rx_oob(struct mana_recv_buf_oob *rx_oob, u32 mem_key,
2919 			    struct mana_rxq *rxq, struct device *dev)
2920 {
2921 	struct mana_port_context *mpc = netdev_priv(rxq->ndev);
2922 	struct page *pp_page = NULL;
2923 	u32 dma_sync_offset = 0;
2924 	bool from_pool = false;
2925 	dma_addr_t da;
2926 	void *va;
2927 
2928 	if (mpc->rxbufs_pre)
2929 		va = mana_get_rxbuf_pre(rxq, &da);
2930 	else
2931 		va = mana_get_rxfrag(rxq, dev, &da, &from_pool, &pp_page,
2932 				     &dma_sync_offset);
2933 
2934 	if (!va)
2935 		return -ENOMEM;
2936 
2937 	rx_oob->buf_va = va;
2938 	rx_oob->from_pool = from_pool;
2939 	rx_oob->pp_page = pp_page;
2940 	rx_oob->dma_sync_offset = dma_sync_offset;
2941 
2942 	rx_oob->sgl[0].address = da;
2943 	rx_oob->sgl[0].size = rxq->datasize;
2944 	rx_oob->sgl[0].mem_key = mem_key;
2945 
2946 	return 0;
2947 }
2948 
2949 #define MANA_WQE_HEADER_SIZE 16
2950 #define MANA_WQE_SGE_SIZE 16
2951 
mana_alloc_rx_wqe(struct mana_port_context * apc,struct mana_rxq * rxq,u32 * rxq_size,u32 * cq_size)2952 static int mana_alloc_rx_wqe(struct mana_port_context *apc,
2953 			     struct mana_rxq *rxq, u32 *rxq_size, u32 *cq_size)
2954 {
2955 	struct gdma_context *gc = apc->ac->gdma_dev->gdma_context;
2956 	struct mana_recv_buf_oob *rx_oob;
2957 	struct device *dev = gc->dev;
2958 	u32 buf_idx;
2959 	int ret;
2960 
2961 	WARN_ON(rxq->datasize == 0);
2962 
2963 	*rxq_size = 0;
2964 	*cq_size = 0;
2965 
2966 	for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) {
2967 		rx_oob = &rxq->rx_oobs[buf_idx];
2968 		memset(rx_oob, 0, sizeof(*rx_oob));
2969 
2970 		rx_oob->num_sge = 1;
2971 
2972 		ret = mana_fill_rx_oob(rx_oob, apc->ac->gdma_dev->gpa_mkey, rxq,
2973 				       dev);
2974 		if (ret)
2975 			return ret;
2976 
2977 		rx_oob->wqe_req.sgl = rx_oob->sgl;
2978 		rx_oob->wqe_req.num_sge = rx_oob->num_sge;
2979 		rx_oob->wqe_req.inline_oob_size = 0;
2980 		rx_oob->wqe_req.inline_oob_data = NULL;
2981 		rx_oob->wqe_req.flags = 0;
2982 		rx_oob->wqe_req.client_data_unit = 0;
2983 
2984 		*rxq_size += ALIGN(MANA_WQE_HEADER_SIZE +
2985 				   MANA_WQE_SGE_SIZE * rx_oob->num_sge, 32);
2986 		*cq_size += COMP_ENTRY_SIZE;
2987 	}
2988 
2989 	return 0;
2990 }
2991 
mana_push_wqe(struct mana_rxq * rxq)2992 static int mana_push_wqe(struct mana_rxq *rxq)
2993 {
2994 	struct mana_recv_buf_oob *rx_oob;
2995 	u32 buf_idx;
2996 	int err;
2997 
2998 	for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) {
2999 		rx_oob = &rxq->rx_oobs[buf_idx];
3000 
3001 		err = mana_gd_post_and_ring(rxq->gdma_rq, &rx_oob->wqe_req,
3002 					    &rx_oob->wqe_inf);
3003 		if (err)
3004 			return -ENOSPC;
3005 	}
3006 
3007 	return 0;
3008 }
3009 
mana_create_page_pool(struct mana_rxq * rxq,struct gdma_context * gc)3010 static int mana_create_page_pool(struct mana_rxq *rxq, struct gdma_context *gc)
3011 {
3012 	struct mana_port_context *mpc = netdev_priv(rxq->ndev);
3013 	struct page_pool_params pprm = {};
3014 	int ret;
3015 
3016 	pprm.pool_size = mpc->rx_queue_size / rxq->frag_count + 1;
3017 	pprm.nid = gc->numa_node;
3018 	pprm.napi = &rxq->rx_cq.napi;
3019 	pprm.netdev = rxq->ndev;
3020 	pprm.order = get_order(rxq->alloc_size);
3021 	pprm.queue_idx = rxq->rxq_idx;
3022 	pprm.dev = gc->dev;
3023 
3024 	/* Let the page pool do the dma map when page sharing with multiple
3025 	 * fragments enabled for rx buffers.
3026 	 */
3027 	if (rxq->frag_count > 1) {
3028 		pprm.flags =  PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3029 		pprm.max_len = PAGE_SIZE;
3030 		pprm.dma_dir = DMA_FROM_DEVICE;
3031 	}
3032 
3033 	rxq->page_pool = page_pool_create(&pprm);
3034 
3035 	if (IS_ERR(rxq->page_pool)) {
3036 		ret = PTR_ERR(rxq->page_pool);
3037 		rxq->page_pool = NULL;
3038 		return ret;
3039 	}
3040 
3041 	return 0;
3042 }
3043 
mana_create_rxq(struct mana_port_context * apc,u32 rxq_idx,struct mana_eq * eq,struct net_device * ndev)3044 static struct mana_rxq *mana_create_rxq(struct mana_port_context *apc,
3045 					u32 rxq_idx, struct mana_eq *eq,
3046 					struct net_device *ndev)
3047 {
3048 	struct gdma_dev *gd = apc->ac->gdma_dev;
3049 	struct mana_obj_spec wq_spec;
3050 	struct mana_obj_spec cq_spec;
3051 	struct gdma_queue_spec spec;
3052 	struct mana_cq *cq = NULL;
3053 	struct gdma_context *gc;
3054 	u32 cq_size, rq_size;
3055 	struct mana_rxq *rxq;
3056 	int err;
3057 
3058 	gc = gd->gdma_context;
3059 
3060 	rxq = kvzalloc_flex(*rxq, rx_oobs, apc->rx_queue_size);
3061 	if (!rxq)
3062 		return ERR_PTR(-ENOMEM);
3063 
3064 	rxq->ndev = ndev;
3065 	rxq->num_rx_buf = apc->rx_queue_size;
3066 	rxq->rxq_idx = rxq_idx;
3067 	rxq->rxobj = INVALID_MANA_HANDLE;
3068 
3069 	mana_get_rxbuf_cfg(apc, ndev->mtu, &rxq->datasize, &rxq->alloc_size,
3070 			   &rxq->headroom, &rxq->frag_count);
3071 	/* Create page pool for RX queue */
3072 	err = mana_create_page_pool(rxq, gc);
3073 	if (err) {
3074 		netdev_err(ndev, "Create page pool err:%d\n", err);
3075 		goto out;
3076 	}
3077 
3078 	err = mana_alloc_rx_wqe(apc, rxq, &rq_size, &cq_size);
3079 	if (err)
3080 		goto out;
3081 
3082 	rq_size = MANA_PAGE_ALIGN(rq_size);
3083 	cq_size = MANA_PAGE_ALIGN(cq_size);
3084 
3085 	/* Create RQ */
3086 	memset(&spec, 0, sizeof(spec));
3087 	spec.type = GDMA_RQ;
3088 	spec.monitor_avl_buf = true;
3089 	spec.queue_size = rq_size;
3090 	err = mana_gd_create_mana_wq_cq(gd, &spec, &rxq->gdma_rq);
3091 	if (err)
3092 		goto out;
3093 
3094 	/* Create RQ's CQ */
3095 	cq = &rxq->rx_cq;
3096 	cq->type = MANA_CQ_TYPE_RX;
3097 	cq->rxq = rxq;
3098 
3099 	memset(&spec, 0, sizeof(spec));
3100 	spec.type = GDMA_CQ;
3101 	spec.monitor_avl_buf = false;
3102 	spec.queue_size = cq_size;
3103 	spec.cq.callback = mana_schedule_napi;
3104 	spec.cq.parent_eq = eq->eq;
3105 	spec.cq.context = cq;
3106 	err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq);
3107 	if (err)
3108 		goto out;
3109 
3110 	memset(&wq_spec, 0, sizeof(wq_spec));
3111 	memset(&cq_spec, 0, sizeof(cq_spec));
3112 	wq_spec.gdma_region = rxq->gdma_rq->mem_info.dma_region_handle;
3113 	wq_spec.queue_size = rxq->gdma_rq->queue_size;
3114 
3115 	cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle;
3116 	cq_spec.queue_size = cq->gdma_cq->queue_size;
3117 	cq_spec.modr_ctx_id = 0;
3118 	cq_spec.attached_eq = cq->gdma_cq->cq.parent->id;
3119 
3120 	/* DIM setting can be changed at runtime */
3121 	cq_spec.req_cq_moderation = true;
3122 	cq_spec.cq_moderation_usec = apc->intr_modr_rx_usec;
3123 	cq_spec.cq_moderation_comp = apc->intr_modr_rx_comp;
3124 
3125 	err = mana_create_wq_obj(apc, apc->port_handle, GDMA_RQ,
3126 				 &wq_spec, &cq_spec, &rxq->rxobj);
3127 	if (err)
3128 		goto out;
3129 
3130 	rxq->gdma_rq->id = wq_spec.queue_index;
3131 	cq->gdma_cq->id = cq_spec.queue_index;
3132 
3133 	rxq->gdma_rq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION;
3134 	cq->gdma_cq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION;
3135 
3136 	rxq->gdma_id = rxq->gdma_rq->id;
3137 	cq->gdma_id = cq->gdma_cq->id;
3138 
3139 	err = mana_push_wqe(rxq);
3140 	if (err)
3141 		goto out;
3142 
3143 	if (WARN_ON(cq->gdma_id >= gc->max_num_cqs)) {
3144 		err = -EINVAL;
3145 		goto out;
3146 	}
3147 
3148 	gc->cq_table[cq->gdma_id] = cq->gdma_cq;
3149 
3150 	netif_napi_add_weight_locked(ndev, &cq->napi, mana_poll, 1);
3151 
3152 	WARN_ON(xdp_rxq_info_reg(&rxq->xdp_rxq, ndev, rxq_idx,
3153 				 cq->napi.napi_id));
3154 	WARN_ON(xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL,
3155 					   rxq->page_pool));
3156 
3157 	/* Initialize the DIM work before enabling NAPI, so that a poll
3158 	 * cannot reach net_dim() with an uninitialized cq->dim.work.
3159 	 */
3160 	INIT_WORK(&cq->dim.work, mana_rx_dim_work);
3161 	cq->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
3162 
3163 	napi_enable_locked(&cq->napi);
3164 
3165 	mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT);
3166 out:
3167 	if (!err)
3168 		return rxq;
3169 
3170 	netdev_err(ndev, "Failed to create RXQ: err = %d\n", err);
3171 
3172 	mana_destroy_rxq(apc, rxq, false);
3173 
3174 	return ERR_PTR(err);
3175 }
3176 
mana_create_rxq_debugfs(struct mana_port_context * apc,int idx)3177 static void mana_create_rxq_debugfs(struct mana_port_context *apc, int idx)
3178 {
3179 	struct mana_rxq *rxq;
3180 	char qnum[32];
3181 
3182 	rxq = apc->rxqs[idx];
3183 
3184 	sprintf(qnum, "RX-%d", idx);
3185 	rxq->mana_rx_debugfs = debugfs_create_dir(qnum, apc->mana_port_debugfs);
3186 	debugfs_create_u32("rq_head", 0400, rxq->mana_rx_debugfs, &rxq->gdma_rq->head);
3187 	debugfs_create_u32("rq_tail", 0400, rxq->mana_rx_debugfs, &rxq->gdma_rq->tail);
3188 	debugfs_create_u32("rq_nbuf", 0400, rxq->mana_rx_debugfs, &rxq->num_rx_buf);
3189 	debugfs_create_u32("cq_head", 0400, rxq->mana_rx_debugfs,
3190 			   &rxq->rx_cq.gdma_cq->head);
3191 	debugfs_create_u32("cq_tail", 0400, rxq->mana_rx_debugfs,
3192 			   &rxq->rx_cq.gdma_cq->tail);
3193 	debugfs_create_u32("cq_budget", 0400, rxq->mana_rx_debugfs, &rxq->rx_cq.budget);
3194 	debugfs_create_file("rxq_dump", 0400, rxq->mana_rx_debugfs, rxq->gdma_rq, &mana_dbg_q_fops);
3195 	debugfs_create_file("cq_dump", 0400, rxq->mana_rx_debugfs, rxq->rx_cq.gdma_cq,
3196 			    &mana_dbg_q_fops);
3197 }
3198 
mana_add_rx_queues(struct mana_port_context * apc,struct net_device * ndev)3199 static int mana_add_rx_queues(struct mana_port_context *apc,
3200 			      struct net_device *ndev)
3201 {
3202 	struct mana_rxq *rxq;
3203 	int err = 0;
3204 	int i;
3205 
3206 	for (i = 0; i < apc->num_queues; i++) {
3207 		rxq = mana_create_rxq(apc, i, &apc->eqs[i], ndev);
3208 		if (IS_ERR(rxq)) {
3209 			err = PTR_ERR(rxq);
3210 			netdev_err(ndev, "Failed to create rxq %d : %d\n", i, err);
3211 			goto out;
3212 		}
3213 
3214 		u64_stats_init(&rxq->stats.syncp);
3215 
3216 		apc->rxqs[i] = rxq;
3217 
3218 		mana_create_rxq_debugfs(apc, i);
3219 	}
3220 
3221 	apc->default_rxobj = apc->rxqs[0]->rxobj;
3222 out:
3223 	return err;
3224 }
3225 
mana_destroy_rxqs(struct mana_port_context * apc)3226 static void mana_destroy_rxqs(struct mana_port_context *apc)
3227 {
3228 	struct mana_rxq *rxq;
3229 	u32 rxq_idx;
3230 
3231 	if (apc->rxqs) {
3232 
3233 		for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) {
3234 			rxq = apc->rxqs[rxq_idx];
3235 			if (!rxq)
3236 				continue;
3237 
3238 			mana_destroy_rxq(apc, rxq, true);
3239 			apc->rxqs[rxq_idx] = NULL;
3240 		}
3241 	}
3242 }
3243 
mana_destroy_vport(struct mana_port_context * apc)3244 static void mana_destroy_vport(struct mana_port_context *apc)
3245 {
3246 	struct gdma_dev *gd = apc->ac->gdma_dev;
3247 
3248 	mana_uncfg_vport(apc);
3249 
3250 	if (gd->gdma_context->is_pf && !apc->ac->bm_hostmode)
3251 		mana_pf_deregister_hw_vport(apc);
3252 }
3253 
mana_create_vport(struct mana_port_context * apc,struct net_device * net)3254 static int mana_create_vport(struct mana_port_context *apc,
3255 			     struct net_device *net)
3256 {
3257 	struct gdma_dev *gd = apc->ac->gdma_dev;
3258 	int err;
3259 
3260 	apc->default_rxobj = INVALID_MANA_HANDLE;
3261 
3262 	if (gd->gdma_context->is_pf && !apc->ac->bm_hostmode) {
3263 		err = mana_pf_register_hw_vport(apc);
3264 		if (err)
3265 			return err;
3266 	}
3267 
3268 	err = mana_cfg_vport(apc, gd->pdid, gd->doorbell, false);
3269 	if (err) {
3270 		if (gd->gdma_context->is_pf && !apc->ac->bm_hostmode)
3271 			mana_pf_deregister_hw_vport(apc);
3272 		return err;
3273 	}
3274 
3275 	return 0;
3276 }
3277 
mana_rss_table_alloc(struct mana_port_context * apc)3278 static int mana_rss_table_alloc(struct mana_port_context *apc)
3279 {
3280 	if (!apc->indir_table_sz) {
3281 		netdev_err(apc->ndev,
3282 			   "Indirection table size not set for vPort %d\n",
3283 			   apc->port_idx);
3284 		return -EINVAL;
3285 	}
3286 
3287 	apc->indir_table = kcalloc(apc->indir_table_sz, sizeof(u32), GFP_KERNEL);
3288 	if (!apc->indir_table)
3289 		return -ENOMEM;
3290 
3291 	apc->rxobj_table = kzalloc_objs(mana_handle_t, apc->indir_table_sz);
3292 	if (!apc->rxobj_table) {
3293 		kfree(apc->indir_table);
3294 		return -ENOMEM;
3295 	}
3296 
3297 	return 0;
3298 }
3299 
mana_rss_table_init(struct mana_port_context * apc)3300 static void mana_rss_table_init(struct mana_port_context *apc)
3301 {
3302 	int i;
3303 
3304 	for (i = 0; i < apc->indir_table_sz; i++)
3305 		apc->indir_table[i] =
3306 			ethtool_rxfh_indir_default(i, apc->num_queues);
3307 }
3308 
mana_disable_vport_rx(struct mana_port_context * apc)3309 int mana_disable_vport_rx(struct mana_port_context *apc)
3310 {
3311 	return mana_cfg_vport_steering(apc, TRI_STATE_FALSE, false, false,
3312 				       false);
3313 }
3314 EXPORT_SYMBOL_NS(mana_disable_vport_rx, "NET_MANA");
3315 
mana_config_rss(struct mana_port_context * apc,enum TRI_STATE rx,bool update_hash,bool update_tab)3316 int mana_config_rss(struct mana_port_context *apc, enum TRI_STATE rx,
3317 		    bool update_hash, bool update_tab)
3318 {
3319 	u32 queue_idx;
3320 	int err;
3321 	int i;
3322 
3323 	if (update_tab) {
3324 		for (i = 0; i < apc->indir_table_sz; i++) {
3325 			queue_idx = apc->indir_table[i];
3326 			apc->rxobj_table[i] = apc->rxqs[queue_idx]->rxobj;
3327 		}
3328 	}
3329 
3330 	err = mana_cfg_vport_steering(apc, rx, true, update_hash, update_tab);
3331 	if (err)
3332 		return err;
3333 
3334 	mana_fence_rqs(apc);
3335 
3336 	return 0;
3337 }
3338 
mana_query_gf_stats(struct mana_context * ac)3339 int mana_query_gf_stats(struct mana_context *ac)
3340 {
3341 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
3342 	struct mana_query_gf_stat_resp resp = {};
3343 	struct mana_query_gf_stat_req req = {};
3344 	struct device *dev = gc->dev;
3345 	int err;
3346 
3347 	mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_GF_STAT,
3348 			     sizeof(req), sizeof(resp));
3349 	req.hdr.resp.msg_version = GDMA_MESSAGE_V2;
3350 	req.req_stats = STATISTICS_FLAGS_RX_DISCARDS_NO_WQE |
3351 			STATISTICS_FLAGS_RX_ERRORS_VPORT_DISABLED |
3352 			STATISTICS_FLAGS_HC_RX_BYTES |
3353 			STATISTICS_FLAGS_HC_RX_UCAST_PACKETS |
3354 			STATISTICS_FLAGS_HC_RX_UCAST_BYTES |
3355 			STATISTICS_FLAGS_HC_RX_MCAST_PACKETS |
3356 			STATISTICS_FLAGS_HC_RX_MCAST_BYTES |
3357 			STATISTICS_FLAGS_HC_RX_BCAST_PACKETS |
3358 			STATISTICS_FLAGS_HC_RX_BCAST_BYTES |
3359 			STATISTICS_FLAGS_TX_ERRORS_GF_DISABLED |
3360 			STATISTICS_FLAGS_TX_ERRORS_VPORT_DISABLED |
3361 			STATISTICS_FLAGS_TX_ERRORS_INVAL_VPORT_OFFSET_PACKETS |
3362 			STATISTICS_FLAGS_TX_ERRORS_VLAN_ENFORCEMENT |
3363 			STATISTICS_FLAGS_TX_ERRORS_ETH_TYPE_ENFORCEMENT |
3364 			STATISTICS_FLAGS_TX_ERRORS_SA_ENFORCEMENT |
3365 			STATISTICS_FLAGS_TX_ERRORS_SQPDID_ENFORCEMENT |
3366 			STATISTICS_FLAGS_TX_ERRORS_CQPDID_ENFORCEMENT |
3367 			STATISTICS_FLAGS_TX_ERRORS_MTU_VIOLATION |
3368 			STATISTICS_FLAGS_TX_ERRORS_INVALID_OOB |
3369 			STATISTICS_FLAGS_HC_TX_BYTES |
3370 			STATISTICS_FLAGS_HC_TX_UCAST_PACKETS |
3371 			STATISTICS_FLAGS_HC_TX_UCAST_BYTES |
3372 			STATISTICS_FLAGS_HC_TX_MCAST_PACKETS |
3373 			STATISTICS_FLAGS_HC_TX_MCAST_BYTES |
3374 			STATISTICS_FLAGS_HC_TX_BCAST_PACKETS |
3375 			STATISTICS_FLAGS_HC_TX_BCAST_BYTES |
3376 			STATISTICS_FLAGS_TX_ERRORS_GDMA_ERROR;
3377 
3378 	err = mana_send_request(ac, &req, sizeof(req), &resp,
3379 				sizeof(resp));
3380 	if (err) {
3381 		dev_err(dev, "Failed to query GF stats: %d\n", err);
3382 		return err;
3383 	}
3384 	err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_GF_STAT,
3385 				   sizeof(resp));
3386 	if (err || resp.hdr.status) {
3387 		dev_err(dev, "Failed to query GF stats: %d, 0x%x\n", err,
3388 			resp.hdr.status);
3389 		return err;
3390 	}
3391 
3392 	ac->hc_stats.hc_rx_discards_no_wqe = resp.rx_discards_nowqe;
3393 	ac->hc_stats.hc_rx_err_vport_disabled = resp.rx_err_vport_disabled;
3394 	ac->hc_stats.hc_rx_bytes = resp.hc_rx_bytes;
3395 	ac->hc_stats.hc_rx_ucast_pkts = resp.hc_rx_ucast_pkts;
3396 	ac->hc_stats.hc_rx_ucast_bytes = resp.hc_rx_ucast_bytes;
3397 	ac->hc_stats.hc_rx_bcast_pkts = resp.hc_rx_bcast_pkts;
3398 	ac->hc_stats.hc_rx_bcast_bytes = resp.hc_rx_bcast_bytes;
3399 	ac->hc_stats.hc_rx_mcast_pkts = resp.hc_rx_mcast_pkts;
3400 	ac->hc_stats.hc_rx_mcast_bytes = resp.hc_rx_mcast_bytes;
3401 	ac->hc_stats.hc_tx_err_gf_disabled = resp.tx_err_gf_disabled;
3402 	ac->hc_stats.hc_tx_err_vport_disabled = resp.tx_err_vport_disabled;
3403 	ac->hc_stats.hc_tx_err_inval_vportoffset_pkt =
3404 					     resp.tx_err_inval_vport_offset_pkt;
3405 	ac->hc_stats.hc_tx_err_vlan_enforcement =
3406 					     resp.tx_err_vlan_enforcement;
3407 	ac->hc_stats.hc_tx_err_eth_type_enforcement =
3408 					     resp.tx_err_ethtype_enforcement;
3409 	ac->hc_stats.hc_tx_err_sa_enforcement = resp.tx_err_SA_enforcement;
3410 	ac->hc_stats.hc_tx_err_sqpdid_enforcement =
3411 					     resp.tx_err_SQPDID_enforcement;
3412 	ac->hc_stats.hc_tx_err_cqpdid_enforcement =
3413 					     resp.tx_err_CQPDID_enforcement;
3414 	ac->hc_stats.hc_tx_err_mtu_violation = resp.tx_err_mtu_violation;
3415 	ac->hc_stats.hc_tx_err_inval_oob = resp.tx_err_inval_oob;
3416 	ac->hc_stats.hc_tx_bytes = resp.hc_tx_bytes;
3417 	ac->hc_stats.hc_tx_ucast_pkts = resp.hc_tx_ucast_pkts;
3418 	ac->hc_stats.hc_tx_ucast_bytes = resp.hc_tx_ucast_bytes;
3419 	ac->hc_stats.hc_tx_bcast_pkts = resp.hc_tx_bcast_pkts;
3420 	ac->hc_stats.hc_tx_bcast_bytes = resp.hc_tx_bcast_bytes;
3421 	ac->hc_stats.hc_tx_mcast_pkts = resp.hc_tx_mcast_pkts;
3422 	ac->hc_stats.hc_tx_mcast_bytes = resp.hc_tx_mcast_bytes;
3423 	ac->hc_stats.hc_tx_err_gdma = resp.tx_err_gdma;
3424 
3425 	return 0;
3426 }
3427 
mana_query_phy_stats(struct mana_port_context * apc)3428 void mana_query_phy_stats(struct mana_port_context *apc)
3429 {
3430 	struct mana_query_phy_stat_resp resp = {};
3431 	struct mana_query_phy_stat_req req = {};
3432 	struct net_device *ndev = apc->ndev;
3433 	int err;
3434 
3435 	mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_PHY_STAT,
3436 			     sizeof(req), sizeof(resp));
3437 	err = mana_send_request(apc->ac, &req, sizeof(req), &resp,
3438 				sizeof(resp));
3439 	if (err)
3440 		return;
3441 
3442 	err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_PHY_STAT,
3443 				   sizeof(resp));
3444 	if (err || resp.hdr.status) {
3445 		netdev_err(ndev,
3446 			   "Failed to query PHY stats: %d, resp:0x%x\n",
3447 				err, resp.hdr.status);
3448 		return;
3449 	}
3450 
3451 	/* Aggregate drop counters */
3452 	apc->phy_stats.rx_pkt_drop_phy = resp.rx_pkt_drop_phy;
3453 	apc->phy_stats.tx_pkt_drop_phy = resp.tx_pkt_drop_phy;
3454 
3455 	/* Per TC traffic Counters */
3456 	apc->phy_stats.rx_pkt_tc0_phy = resp.rx_pkt_tc0_phy;
3457 	apc->phy_stats.tx_pkt_tc0_phy = resp.tx_pkt_tc0_phy;
3458 	apc->phy_stats.rx_pkt_tc1_phy = resp.rx_pkt_tc1_phy;
3459 	apc->phy_stats.tx_pkt_tc1_phy = resp.tx_pkt_tc1_phy;
3460 	apc->phy_stats.rx_pkt_tc2_phy = resp.rx_pkt_tc2_phy;
3461 	apc->phy_stats.tx_pkt_tc2_phy = resp.tx_pkt_tc2_phy;
3462 	apc->phy_stats.rx_pkt_tc3_phy = resp.rx_pkt_tc3_phy;
3463 	apc->phy_stats.tx_pkt_tc3_phy = resp.tx_pkt_tc3_phy;
3464 	apc->phy_stats.rx_pkt_tc4_phy = resp.rx_pkt_tc4_phy;
3465 	apc->phy_stats.tx_pkt_tc4_phy = resp.tx_pkt_tc4_phy;
3466 	apc->phy_stats.rx_pkt_tc5_phy = resp.rx_pkt_tc5_phy;
3467 	apc->phy_stats.tx_pkt_tc5_phy = resp.tx_pkt_tc5_phy;
3468 	apc->phy_stats.rx_pkt_tc6_phy = resp.rx_pkt_tc6_phy;
3469 	apc->phy_stats.tx_pkt_tc6_phy = resp.tx_pkt_tc6_phy;
3470 	apc->phy_stats.rx_pkt_tc7_phy = resp.rx_pkt_tc7_phy;
3471 	apc->phy_stats.tx_pkt_tc7_phy = resp.tx_pkt_tc7_phy;
3472 
3473 	/* Per TC byte Counters */
3474 	apc->phy_stats.rx_byte_tc0_phy = resp.rx_byte_tc0_phy;
3475 	apc->phy_stats.tx_byte_tc0_phy = resp.tx_byte_tc0_phy;
3476 	apc->phy_stats.rx_byte_tc1_phy = resp.rx_byte_tc1_phy;
3477 	apc->phy_stats.tx_byte_tc1_phy = resp.tx_byte_tc1_phy;
3478 	apc->phy_stats.rx_byte_tc2_phy = resp.rx_byte_tc2_phy;
3479 	apc->phy_stats.tx_byte_tc2_phy = resp.tx_byte_tc2_phy;
3480 	apc->phy_stats.rx_byte_tc3_phy = resp.rx_byte_tc3_phy;
3481 	apc->phy_stats.tx_byte_tc3_phy = resp.tx_byte_tc3_phy;
3482 	apc->phy_stats.rx_byte_tc4_phy = resp.rx_byte_tc4_phy;
3483 	apc->phy_stats.tx_byte_tc4_phy = resp.tx_byte_tc4_phy;
3484 	apc->phy_stats.rx_byte_tc5_phy = resp.rx_byte_tc5_phy;
3485 	apc->phy_stats.tx_byte_tc5_phy = resp.tx_byte_tc5_phy;
3486 	apc->phy_stats.rx_byte_tc6_phy = resp.rx_byte_tc6_phy;
3487 	apc->phy_stats.tx_byte_tc6_phy = resp.tx_byte_tc6_phy;
3488 	apc->phy_stats.rx_byte_tc7_phy = resp.rx_byte_tc7_phy;
3489 	apc->phy_stats.tx_byte_tc7_phy = resp.tx_byte_tc7_phy;
3490 
3491 	/* Per TC pause Counters */
3492 	apc->phy_stats.rx_pause_tc0_phy = resp.rx_pause_tc0_phy;
3493 	apc->phy_stats.tx_pause_tc0_phy = resp.tx_pause_tc0_phy;
3494 	apc->phy_stats.rx_pause_tc1_phy = resp.rx_pause_tc1_phy;
3495 	apc->phy_stats.tx_pause_tc1_phy = resp.tx_pause_tc1_phy;
3496 	apc->phy_stats.rx_pause_tc2_phy = resp.rx_pause_tc2_phy;
3497 	apc->phy_stats.tx_pause_tc2_phy = resp.tx_pause_tc2_phy;
3498 	apc->phy_stats.rx_pause_tc3_phy = resp.rx_pause_tc3_phy;
3499 	apc->phy_stats.tx_pause_tc3_phy = resp.tx_pause_tc3_phy;
3500 	apc->phy_stats.rx_pause_tc4_phy = resp.rx_pause_tc4_phy;
3501 	apc->phy_stats.tx_pause_tc4_phy = resp.tx_pause_tc4_phy;
3502 	apc->phy_stats.rx_pause_tc5_phy = resp.rx_pause_tc5_phy;
3503 	apc->phy_stats.tx_pause_tc5_phy = resp.tx_pause_tc5_phy;
3504 	apc->phy_stats.rx_pause_tc6_phy = resp.rx_pause_tc6_phy;
3505 	apc->phy_stats.tx_pause_tc6_phy = resp.tx_pause_tc6_phy;
3506 	apc->phy_stats.rx_pause_tc7_phy = resp.rx_pause_tc7_phy;
3507 	apc->phy_stats.tx_pause_tc7_phy = resp.tx_pause_tc7_phy;
3508 }
3509 
mana_init_port(struct net_device * ndev)3510 static int mana_init_port(struct net_device *ndev)
3511 {
3512 	struct mana_port_context *apc = netdev_priv(ndev);
3513 	struct gdma_dev *gd = apc->ac->gdma_dev;
3514 	u32 max_txq, max_rxq, max_queues;
3515 	int port_idx = apc->port_idx;
3516 	struct gdma_context *gc;
3517 	char vport[32];
3518 	int err;
3519 
3520 	err = mana_init_port_context(apc);
3521 	if (err)
3522 		return err;
3523 
3524 	gc = gd->gdma_context;
3525 
3526 	err = mana_query_vport_cfg(apc, port_idx, &max_txq, &max_rxq,
3527 				   &apc->indir_table_sz);
3528 	if (err) {
3529 		netdev_err(ndev, "Failed to query info for vPort %d\n",
3530 			   port_idx);
3531 		goto reset_apc;
3532 	}
3533 
3534 	max_queues = min_t(u32, max_txq, max_rxq);
3535 	if (apc->max_queues > max_queues)
3536 		apc->max_queues = max_queues;
3537 	if (apc->max_queues > gc->max_num_queues_vport)
3538 		apc->max_queues = gc->max_num_queues_vport;
3539 
3540 	if (apc->num_queues > apc->max_queues)
3541 		apc->num_queues = apc->max_queues;
3542 
3543 	eth_hw_addr_set(ndev, apc->mac_addr);
3544 	sprintf(vport, "vport%d", port_idx);
3545 	apc->mana_port_debugfs = debugfs_create_dir(vport, gc->mana_pci_debugfs);
3546 
3547 	debugfs_create_u64("port_handle", 0400, apc->mana_port_debugfs,
3548 			   &apc->port_handle);
3549 	debugfs_create_u32("max_sq", 0400, apc->mana_port_debugfs,
3550 			   &apc->vport_max_sq);
3551 	debugfs_create_u32("max_rq", 0400, apc->mana_port_debugfs,
3552 			   &apc->vport_max_rq);
3553 	debugfs_create_u32("indir_table_sz", 0400, apc->mana_port_debugfs,
3554 			   &apc->indir_table_sz);
3555 	debugfs_create_u32("steer_rx", 0400, apc->mana_port_debugfs,
3556 			   &apc->steer_rx);
3557 	debugfs_create_u32("steer_rss", 0400, apc->mana_port_debugfs,
3558 			   &apc->steer_rss);
3559 	debugfs_create_bool("steer_update_tab", 0400, apc->mana_port_debugfs,
3560 			    &apc->steer_update_tab);
3561 	debugfs_create_u32("steer_cqe_coalescing", 0400, apc->mana_port_debugfs,
3562 			   &apc->steer_cqe_coalescing);
3563 	debugfs_create_u32("current_speed", 0400, apc->mana_port_debugfs,
3564 			   &apc->speed);
3565 	debugfs_create_bool("tx_timeout_skip_reset", 0600,
3566 			    apc->mana_port_debugfs,
3567 			    &apc->tx_timeout_skip_reset);
3568 	return 0;
3569 
3570 reset_apc:
3571 	mana_cleanup_port_context(apc);
3572 	return err;
3573 }
3574 
mana_alloc_queues(struct net_device * ndev)3575 int mana_alloc_queues(struct net_device *ndev)
3576 {
3577 	struct mana_port_context *apc = netdev_priv(ndev);
3578 	struct gdma_dev *gd = apc->ac->gdma_dev;
3579 	int err;
3580 
3581 	err = mana_create_vport(apc, ndev);
3582 	if (err) {
3583 		netdev_err(ndev, "Failed to create vPort %u : %d\n",
3584 			   apc->port_idx, err);
3585 		return err;
3586 	}
3587 
3588 	err = mana_create_eq(apc);
3589 	if (err) {
3590 		netdev_err(ndev, "Failed to create EQ on vPort %u: %d\n",
3591 			   apc->port_idx, err);
3592 		goto destroy_vport;
3593 	}
3594 
3595 	err = mana_create_txq(apc, ndev);
3596 	if (err) {
3597 		netdev_err(ndev, "Failed to create TXQ on vPort %u: %d\n",
3598 			   apc->port_idx, err);
3599 		goto destroy_eq;
3600 	}
3601 
3602 	err = netif_set_real_num_tx_queues(ndev, apc->num_queues);
3603 	if (err) {
3604 		netdev_err(ndev,
3605 			   "netif_set_real_num_tx_queues () failed for ndev with num_queues %u : %d\n",
3606 			   apc->num_queues, err);
3607 		goto destroy_txq;
3608 	}
3609 
3610 	err = mana_add_rx_queues(apc, ndev);
3611 	if (err)
3612 		goto destroy_rxq;
3613 
3614 	apc->rss_state = apc->num_queues > 1 ? TRI_STATE_TRUE : TRI_STATE_FALSE;
3615 
3616 	err = netif_set_real_num_rx_queues(ndev, apc->num_queues);
3617 	if (err) {
3618 		netdev_err(ndev,
3619 			   "netif_set_real_num_rx_queues () failed for ndev with num_queues %u : %d\n",
3620 			   apc->num_queues, err);
3621 		goto destroy_rxq;
3622 	}
3623 
3624 	mana_rss_table_init(apc);
3625 
3626 	err = mana_config_rss(apc, TRI_STATE_TRUE, true, true);
3627 	if (err) {
3628 		netdev_err(ndev, "Failed to configure RSS table: %d\n", err);
3629 		goto destroy_rxq;
3630 	}
3631 
3632 	if (gd->gdma_context->is_pf && !apc->ac->bm_hostmode) {
3633 		err = mana_pf_register_filter(apc);
3634 		if (err)
3635 			goto destroy_rxq;
3636 	}
3637 
3638 	mana_chn_setxdp(apc, mana_xdp_get(apc));
3639 
3640 	return 0;
3641 
3642 destroy_rxq:
3643 	mana_destroy_rxqs(apc);
3644 destroy_txq:
3645 	mana_destroy_txq(apc);
3646 destroy_eq:
3647 	mana_destroy_eq(apc);
3648 destroy_vport:
3649 	mana_destroy_vport(apc);
3650 	return err;
3651 }
3652 
mana_attach(struct net_device * ndev)3653 int mana_attach(struct net_device *ndev)
3654 {
3655 	struct mana_port_context *apc = netdev_priv(ndev);
3656 	int err;
3657 
3658 	ASSERT_RTNL();
3659 
3660 	err = mana_init_port(ndev);
3661 	if (err)
3662 		return err;
3663 
3664 	if (apc->port_st_save) {
3665 		err = mana_alloc_queues(ndev);
3666 		if (err) {
3667 			mana_cleanup_port_context(apc);
3668 			return err;
3669 		}
3670 	}
3671 
3672 	apc->port_is_up = apc->port_st_save;
3673 
3674 	/* Ensure port state updated before txq state */
3675 	smp_wmb();
3676 
3677 	netif_device_attach(ndev);
3678 
3679 	return 0;
3680 }
3681 
mana_dealloc_queues(struct net_device * ndev)3682 static int mana_dealloc_queues(struct net_device *ndev)
3683 {
3684 	struct mana_port_context *apc = netdev_priv(ndev);
3685 	unsigned long timeout = jiffies + 120 * HZ;
3686 	struct gdma_dev *gd = apc->ac->gdma_dev;
3687 	struct mana_txq *txq;
3688 	struct sk_buff *skb;
3689 	int i, err;
3690 	u32 tsleep;
3691 
3692 	if (apc->port_is_up)
3693 		return -EINVAL;
3694 
3695 	if (apc->rxqs)
3696 		mana_chn_setxdp(apc, NULL);
3697 
3698 	if (gd->gdma_context->is_pf && !apc->ac->bm_hostmode)
3699 		mana_pf_deregister_filter(apc);
3700 
3701 	/* No packet can be transmitted now since apc->port_is_up is false.
3702 	 * There is still a tiny chance that mana_poll_tx_cq() can re-enable
3703 	 * a txq because it may not timely see apc->port_is_up being cleared
3704 	 * to false, but it doesn't matter since mana_start_xmit() drops any
3705 	 * new packets due to apc->port_is_up being false.
3706 	 *
3707 	 * Drain all the in-flight TX packets.
3708 	 * A timeout of 120 seconds for all the queues is used.
3709 	 * This will break the while loop when h/w is not responding.
3710 	 * This value of 120 has been decided here considering max
3711 	 * number of queues.
3712 	 */
3713 
3714 	if (apc->tx_qp) {
3715 		for (i = 0; i < apc->num_queues; i++) {
3716 			txq = &apc->tx_qp[i]->txq;
3717 			tsleep = 1000;
3718 			while (atomic_read(&txq->pending_sends) > 0 &&
3719 			       time_before(jiffies, timeout)) {
3720 				usleep_range(tsleep, tsleep + 1000);
3721 				tsleep <<= 1;
3722 			}
3723 			if (atomic_read(&txq->pending_sends)) {
3724 				err =
3725 				    pcie_flr(to_pci_dev(gd->gdma_context->dev));
3726 				if (err) {
3727 					netdev_err(ndev, "flr failed %d with %d pkts pending in txq %u\n",
3728 						   err,
3729 					    atomic_read(&txq->pending_sends),
3730 					    txq->gdma_txq_id);
3731 				}
3732 				break;
3733 			}
3734 		}
3735 
3736 		for (i = 0; i < apc->num_queues; i++) {
3737 			txq = &apc->tx_qp[i]->txq;
3738 			while ((skb = skb_dequeue(&txq->pending_skbs))) {
3739 				mana_unmap_skb(skb, apc);
3740 				dev_kfree_skb_any(skb);
3741 			}
3742 			atomic_set(&txq->pending_sends, 0);
3743 		}
3744 	}
3745 
3746 	/* We're 100% sure the queues can no longer be woken up, because
3747 	 * we're sure now mana_poll_tx_cq() can't be running.
3748 	 */
3749 
3750 	apc->rss_state = TRI_STATE_FALSE;
3751 	err = mana_disable_vport_rx(apc);
3752 	if (err && mana_en_need_log(apc, err))
3753 		netdev_err(ndev, "Failed to disable vPort: %d\n", err);
3754 
3755 	mana_fence_rqs(apc);
3756 
3757 	/* Even in err case, still need to cleanup the vPort */
3758 	mana_destroy_rxqs(apc);
3759 	mana_destroy_txq(apc);
3760 	mana_destroy_eq(apc);
3761 	mana_destroy_vport(apc);
3762 
3763 	return 0;
3764 }
3765 
mana_detach(struct net_device * ndev,bool from_close)3766 int mana_detach(struct net_device *ndev, bool from_close)
3767 {
3768 	struct mana_port_context *apc = netdev_priv(ndev);
3769 	int err;
3770 
3771 	ASSERT_RTNL();
3772 
3773 	/* If already detached (indicates detach succeeded but attach failed
3774 	 * previously). Now skip mana detach and just retry mana_attach.
3775 	 */
3776 	if (!from_close && !netif_device_present(ndev))
3777 		return 0;
3778 
3779 	apc->port_st_save = apc->port_is_up;
3780 	apc->port_is_up = false;
3781 
3782 	/* Ensure port state updated before txq state */
3783 	smp_wmb();
3784 
3785 	netif_tx_disable(ndev);
3786 
3787 	if (apc->port_st_save) {
3788 		err = mana_dealloc_queues(ndev);
3789 		if (err) {
3790 			netdev_err(ndev, "%s failed to deallocate queues: %d\n", __func__, err);
3791 			return err;
3792 		}
3793 	}
3794 
3795 	if (!from_close) {
3796 		netif_device_detach(ndev);
3797 		mana_cleanup_port_context(apc);
3798 	}
3799 
3800 	return 0;
3801 }
3802 
mana_probe_port(struct mana_context * ac,int port_idx,struct net_device ** ndev_storage)3803 static int mana_probe_port(struct mana_context *ac, int port_idx,
3804 			   struct net_device **ndev_storage)
3805 {
3806 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
3807 	struct mana_port_context *apc;
3808 	struct net_device *ndev;
3809 	int err;
3810 
3811 	ndev = alloc_etherdev_mq(sizeof(struct mana_port_context),
3812 				 gc->max_num_queues_vport);
3813 	if (!ndev)
3814 		return -ENOMEM;
3815 
3816 	*ndev_storage = ndev;
3817 
3818 	apc = netdev_priv(ndev);
3819 	apc->ac = ac;
3820 	apc->ndev = ndev;
3821 	apc->max_queues = gc->max_num_queues_vport;
3822 	/* Use MANA_DEF_NUM_QUEUES as default, still honoring the HW limit */
3823 	apc->num_queues = min(gc->max_num_queues_vport, MANA_DEF_NUM_QUEUES);
3824 	apc->tx_queue_size = DEF_TX_BUFFERS_PER_QUEUE;
3825 	apc->rx_queue_size = DEF_RX_BUFFERS_PER_QUEUE;
3826 	apc->port_handle = INVALID_MANA_HANDLE;
3827 	apc->pf_filter_handle = INVALID_MANA_HANDLE;
3828 	apc->port_idx = port_idx;
3829 	apc->link_cfg_error = 1;
3830 	apc->cqe_coalescing_enable = 0;
3831 	apc->cqe8_coalescing_enable = 0;
3832 
3833 	/* Initialize interrupt moderation settings if supported by HW */
3834 	if (gc->pf_cap_flags1 & GDMA_PF_CAP_FLAG_1_DYN_INTERRUPT_MODERATION) {
3835 		apc->intr_modr_rx_usec = MANA_INTR_MODR_USEC_DEF;
3836 		apc->intr_modr_rx_comp = MANA_INTR_MODR_COMP_DEF;
3837 		apc->intr_modr_tx_usec = MANA_INTR_MODR_USEC_DEF;
3838 		apc->intr_modr_tx_comp = MANA_INTR_MODR_COMP_DEF;
3839 		apc->rx_dim_enabled = MANA_ADAPTIVE_RX_DEF;
3840 		apc->tx_dim_enabled = MANA_ADAPTIVE_TX_DEF;
3841 	}
3842 
3843 	mutex_init(&apc->vport_mutex);
3844 	apc->vport_use_count = 0;
3845 
3846 	ndev->netdev_ops = &mana_devops;
3847 	ndev->ethtool_ops = &mana_ethtool_ops;
3848 	ndev->mtu = ETH_DATA_LEN;
3849 	ndev->max_mtu = gc->adapter_mtu - ETH_HLEN;
3850 	ndev->min_mtu = ETH_MIN_MTU;
3851 	ndev->needed_headroom = MANA_HEADROOM;
3852 	ndev->dev_port = port_idx;
3853 	/* Recommended timeout based on HW FPGA re-config scenario. */
3854 	ndev->watchdog_timeo = 15 * HZ;
3855 	SET_NETDEV_DEV(ndev, gc->dev);
3856 
3857 	netif_set_tso_max_size(ndev, GSO_MAX_SIZE);
3858 
3859 	netif_carrier_off(ndev);
3860 
3861 	netdev_rss_key_fill(apc->hashkey, MANA_HASH_KEY_SIZE);
3862 
3863 	err = mana_init_port(ndev);
3864 	if (err)
3865 		goto free_net;
3866 
3867 	err = mana_rss_table_alloc(apc);
3868 	if (err)
3869 		goto reset_apc;
3870 
3871 	/* Initialize the per port queue reset work.*/
3872 	INIT_WORK(&apc->queue_reset_work,
3873 		  mana_per_port_queue_reset_work_handler);
3874 
3875 	netdev_lockdep_set_classes(ndev);
3876 
3877 	ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3878 	ndev->hw_features |= NETIF_F_RXCSUM;
3879 	ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3880 	ndev->hw_features |= NETIF_F_RXHASH;
3881 	ndev->features = ndev->hw_features | NETIF_F_HW_VLAN_CTAG_TX |
3882 			 NETIF_F_HW_VLAN_CTAG_RX;
3883 	ndev->vlan_features = ndev->features;
3884 	xdp_set_features_flag(ndev, NETDEV_XDP_ACT_BASIC |
3885 			      NETDEV_XDP_ACT_REDIRECT |
3886 			      NETDEV_XDP_ACT_NDO_XMIT);
3887 
3888 	err = register_netdev(ndev);
3889 	if (err) {
3890 		netdev_err(ndev, "Unable to register netdev.\n");
3891 		goto free_indir;
3892 	}
3893 
3894 	netif_carrier_on(ndev);
3895 
3896 	return 0;
3897 
3898 free_indir:
3899 	mana_cleanup_indir_table(apc);
3900 reset_apc:
3901 	mana_cleanup_port_context(apc);
3902 free_net:
3903 	*ndev_storage = NULL;
3904 	netdev_err(ndev, "Failed to probe vPort %d: %d\n", port_idx, err);
3905 	free_netdev(ndev);
3906 	return err;
3907 }
3908 
adev_release(struct device * dev)3909 static void adev_release(struct device *dev)
3910 {
3911 	struct mana_adev *madev = container_of(dev, struct mana_adev, adev.dev);
3912 
3913 	kfree(madev);
3914 }
3915 
remove_adev(struct gdma_dev * gd)3916 static void remove_adev(struct gdma_dev *gd)
3917 {
3918 	struct auxiliary_device *adev = gd->adev;
3919 	int id = adev->id;
3920 
3921 	auxiliary_device_delete(adev);
3922 	auxiliary_device_uninit(adev);
3923 
3924 	mana_adev_idx_free(id);
3925 	gd->adev = NULL;
3926 }
3927 
add_adev(struct gdma_dev * gd,const char * name)3928 static int add_adev(struct gdma_dev *gd, const char *name)
3929 {
3930 	struct auxiliary_device *adev;
3931 	struct mana_adev *madev;
3932 	int ret;
3933 	int id;
3934 
3935 	madev = kzalloc_obj(*madev);
3936 	if (!madev)
3937 		return -ENOMEM;
3938 
3939 	adev = &madev->adev;
3940 	ret = mana_adev_idx_alloc();
3941 	if (ret < 0)
3942 		goto idx_fail;
3943 	id = ret;
3944 	adev->id = id;
3945 
3946 	adev->name = name;
3947 	adev->dev.parent = gd->gdma_context->dev;
3948 	adev->dev.release = adev_release;
3949 	madev->mdev = gd;
3950 
3951 	ret = auxiliary_device_init(adev);
3952 	if (ret)
3953 		goto init_fail;
3954 
3955 	/* madev is owned by the auxiliary device */
3956 	madev = NULL;
3957 	ret = auxiliary_device_add(adev);
3958 	if (ret)
3959 		goto add_fail;
3960 
3961 	gd->adev = adev;
3962 	dev_dbg(gd->gdma_context->dev,
3963 		"Auxiliary device added successfully\n");
3964 	return 0;
3965 
3966 add_fail:
3967 	auxiliary_device_uninit(adev);
3968 
3969 init_fail:
3970 	mana_adev_idx_free(id);
3971 
3972 idx_fail:
3973 	kfree(madev);
3974 
3975 	return ret;
3976 }
3977 
mana_rdma_service_handle(struct work_struct * work)3978 static void mana_rdma_service_handle(struct work_struct *work)
3979 {
3980 	struct mana_service_work *serv_work =
3981 		container_of(work, struct mana_service_work, work);
3982 	struct gdma_dev *gd = serv_work->gdma_dev;
3983 	struct device *dev = gd->gdma_context->dev;
3984 	int ret;
3985 
3986 	if (READ_ONCE(gd->rdma_teardown))
3987 		goto out;
3988 
3989 	switch (serv_work->event) {
3990 	case GDMA_SERVICE_TYPE_RDMA_SUSPEND:
3991 		if (!gd->adev || gd->is_suspended)
3992 			break;
3993 
3994 		remove_adev(gd);
3995 		gd->is_suspended = true;
3996 		break;
3997 
3998 	case GDMA_SERVICE_TYPE_RDMA_RESUME:
3999 		if (!gd->is_suspended)
4000 			break;
4001 
4002 		ret = add_adev(gd, "rdma");
4003 		if (ret)
4004 			dev_err(dev, "Failed to add adev on resume: %d\n", ret);
4005 		else
4006 			gd->is_suspended = false;
4007 		break;
4008 
4009 	default:
4010 		dev_warn(dev, "unknown adev service event %u\n",
4011 			 serv_work->event);
4012 		break;
4013 	}
4014 
4015 out:
4016 	kfree(serv_work);
4017 }
4018 
mana_rdma_service_event(struct gdma_context * gc,enum gdma_service_type event)4019 int mana_rdma_service_event(struct gdma_context *gc, enum gdma_service_type event)
4020 {
4021 	struct gdma_dev *gd = &gc->mana_ib;
4022 	struct mana_service_work *serv_work;
4023 
4024 	if (gd->dev_id.type != GDMA_DEVICE_MANA_IB) {
4025 		/* RDMA device is not detected on pci */
4026 		return 0;
4027 	}
4028 
4029 	serv_work = kzalloc_obj(*serv_work, GFP_ATOMIC);
4030 	if (!serv_work)
4031 		return -ENOMEM;
4032 
4033 	serv_work->event = event;
4034 	serv_work->gdma_dev = gd;
4035 
4036 	INIT_WORK(&serv_work->work, mana_rdma_service_handle);
4037 	queue_work(gc->service_wq, &serv_work->work);
4038 
4039 	return 0;
4040 }
4041 
4042 #define MANA_GF_STATS_PERIOD (2 * HZ)
4043 
mana_gf_stats_work_handler(struct work_struct * work)4044 static void mana_gf_stats_work_handler(struct work_struct *work)
4045 {
4046 	struct mana_context *ac =
4047 		container_of(to_delayed_work(work), struct mana_context, gf_stats_work);
4048 	struct gdma_context *gc = ac->gdma_dev->gdma_context;
4049 	int err;
4050 
4051 	err = mana_query_gf_stats(ac);
4052 	if (err == -ETIMEDOUT) {
4053 		/* HWC timeout detected - reset stats and stop rescheduling */
4054 		ac->hwc_timeout_occurred = true;
4055 		memset(&ac->hc_stats, 0, sizeof(ac->hc_stats));
4056 		dev_warn(gc->dev,
4057 			 "Gf stats wk handler: gf stats query timed out.\n");
4058 		/* As HWC timed out, indicating a faulty HW state and needs a
4059 		 * reset.
4060 		 */
4061 		mana_schedule_serv_work(gc, GDMA_EQE_HWC_RESET_REQUEST);
4062 		return;
4063 	}
4064 	schedule_delayed_work(&ac->gf_stats_work, MANA_GF_STATS_PERIOD);
4065 }
4066 
mana_probe(struct gdma_dev * gd,bool resuming)4067 int mana_probe(struct gdma_dev *gd, bool resuming)
4068 {
4069 	struct gdma_context *gc = gd->gdma_context;
4070 	struct mana_context *ac = gd->driver_data;
4071 	struct mana_port_context *apc = NULL;
4072 	struct device *dev = gc->dev;
4073 	u8 bm_hostmode = 0;
4074 	u16 num_ports = 0;
4075 	int err;
4076 	int i;
4077 
4078 	dev_info(dev,
4079 		 "Microsoft Azure Network Adapter protocol version: %d.%d.%d\n",
4080 		 MANA_MAJOR_VERSION, MANA_MINOR_VERSION, MANA_MICRO_VERSION);
4081 
4082 	err = mana_gd_register_device(gd);
4083 	if (err)
4084 		return err;
4085 
4086 	if (!resuming) {
4087 		ac = kzalloc_obj(*ac);
4088 		if (!ac)
4089 			return -ENOMEM;
4090 
4091 		ac->gdma_dev = gd;
4092 		gd->driver_data = ac;
4093 
4094 		INIT_WORK(&ac->link_change_work, mana_link_state_handle);
4095 	}
4096 
4097 	INIT_DELAYED_WORK(&ac->gf_stats_work, mana_gf_stats_work_handler);
4098 
4099 	err = mana_gd_query_device_cfg(gc, MANA_MAJOR_VERSION,
4100 				       MANA_MINOR_VERSION,
4101 				       MANA_MICRO_VERSION,
4102 				       &num_ports, &bm_hostmode);
4103 	if (err)
4104 		goto out;
4105 
4106 	ac->bm_hostmode = bm_hostmode;
4107 
4108 	debugfs_create_u16("adapter-MTU", 0400,
4109 			   gc->mana_pci_debugfs, &gc->adapter_mtu);
4110 
4111 	if (!resuming) {
4112 		ac->num_ports = num_ports;
4113 	} else {
4114 		if (ac->num_ports != num_ports) {
4115 			dev_err(dev, "The number of vPorts changed: %d->%d\n",
4116 				ac->num_ports, num_ports);
4117 			err = -EPROTO;
4118 			goto out;
4119 		}
4120 
4121 		enable_work(&ac->link_change_work);
4122 	}
4123 
4124 	if (ac->num_ports > MAX_PORTS_IN_MANA_DEV)
4125 		ac->num_ports = MAX_PORTS_IN_MANA_DEV;
4126 
4127 	debugfs_create_u16("num_vports", 0400, gc->mana_pci_debugfs,
4128 			   &ac->num_ports);
4129 	debugfs_create_u8("bm_hostmode", 0400, gc->mana_pci_debugfs,
4130 			  &ac->bm_hostmode);
4131 
4132 	ac->per_port_queue_reset_wq =
4133 		create_singlethread_workqueue("mana_per_port_queue_reset_wq");
4134 	if (!ac->per_port_queue_reset_wq) {
4135 		dev_err(dev, "Failed to allocate per port queue reset workqueue\n");
4136 		err = -ENOMEM;
4137 		goto out;
4138 	}
4139 
4140 	if (!resuming) {
4141 		for (i = 0; i < ac->num_ports; i++) {
4142 			err = mana_probe_port(ac, i, &ac->ports[i]);
4143 			/* Log the port for which the probe failed, stop probing
4144 			 * subsequent ports, and skip add_adev.
4145 			 * mana_remove() will clean up already-probed ports.
4146 			 */
4147 			if (err) {
4148 				dev_err(dev, "Probe Failed for port %d\n", i);
4149 				break;
4150 			}
4151 		}
4152 	} else {
4153 		for (i = 0; i < ac->num_ports; i++) {
4154 			rtnl_lock();
4155 			apc = netdev_priv(ac->ports[i]);
4156 			enable_work(&apc->queue_reset_work);
4157 			netdev_lock(ac->ports[i]);
4158 			apc->link_cfg_error = 1;
4159 			netdev_unlock(ac->ports[i]);
4160 			err = mana_attach(ac->ports[i]);
4161 			rtnl_unlock();
4162 			/* Log the port for which the attach failed, stop
4163 			 * attaching subsequent ports, and skip add_adev.
4164 			 * mana_remove() will clean up already-attached ports.
4165 			 */
4166 			if (err) {
4167 				dev_err(dev, "Attach Failed for port %d\n", i);
4168 				break;
4169 			}
4170 		}
4171 	}
4172 
4173 	if (!err)
4174 		err = add_adev(gd, "eth");
4175 
4176 	schedule_delayed_work(&ac->gf_stats_work, MANA_GF_STATS_PERIOD);
4177 
4178 out:
4179 	if (err) {
4180 		mana_remove(gd, false);
4181 	} else {
4182 		dev_dbg(dev, "gd=%p, id=%u, num_ports=%d, type=%u, instance=%u\n",
4183 			gd, gd->dev_id.as_uint32, ac->num_ports,
4184 			gd->dev_id.type, gd->dev_id.instance);
4185 		dev_dbg(dev, "%s succeeded\n", __func__);
4186 	}
4187 
4188 	return err;
4189 }
4190 
mana_remove(struct gdma_dev * gd,bool suspending)4191 void mana_remove(struct gdma_dev *gd, bool suspending)
4192 {
4193 	struct gdma_context *gc = gd->gdma_context;
4194 	struct mana_context *ac = gd->driver_data;
4195 	struct mana_port_context *apc;
4196 	struct device *dev;
4197 	struct net_device *ndev;
4198 	int err;
4199 	int i;
4200 
4201 	if (!gc || !ac)
4202 		return;
4203 
4204 	dev = gc->dev;
4205 
4206 	disable_work_sync(&ac->link_change_work);
4207 	cancel_delayed_work_sync(&ac->gf_stats_work);
4208 
4209 	/* adev currently doesn't support suspending, always remove it */
4210 	if (gd->adev)
4211 		remove_adev(gd);
4212 
4213 	for (i = 0; i < ac->num_ports; i++) {
4214 		ndev = ac->ports[i];
4215 		if (!ndev) {
4216 			if (i == 0)
4217 				dev_err(dev, "No net device to remove\n");
4218 			break;
4219 		}
4220 
4221 		apc = netdev_priv(ndev);
4222 		disable_work_sync(&apc->queue_reset_work);
4223 
4224 		/* All cleanup actions should stay after rtnl_lock(), otherwise
4225 		 * other functions may access partially cleaned up data.
4226 		 */
4227 		rtnl_lock();
4228 
4229 		err = mana_detach(ndev, false);
4230 		if (err)
4231 			netdev_err(ndev, "Failed to detach vPort %d: %d\n",
4232 				   i, err);
4233 
4234 		if (suspending) {
4235 			/* No need to unregister the ndev. */
4236 			rtnl_unlock();
4237 			continue;
4238 		}
4239 
4240 		unregister_netdevice(ndev);
4241 		mana_cleanup_indir_table(apc);
4242 
4243 		rtnl_unlock();
4244 
4245 		free_netdev(ndev);
4246 	}
4247 
4248 	if (ac->per_port_queue_reset_wq) {
4249 		destroy_workqueue(ac->per_port_queue_reset_wq);
4250 		ac->per_port_queue_reset_wq = NULL;
4251 	}
4252 
4253 	mana_gd_deregister_device(gd);
4254 
4255 	if (gc->mana_pci_debugfs) {
4256 		debugfs_lookup_and_remove("bm_hostmode", gc->mana_pci_debugfs);
4257 		debugfs_lookup_and_remove("num_vports", gc->mana_pci_debugfs);
4258 	}
4259 
4260 	if (suspending)
4261 		return;
4262 
4263 	gd->driver_data = NULL;
4264 	gd->gdma_context = NULL;
4265 	kfree(ac);
4266 	dev_dbg(dev, "%s succeeded\n", __func__);
4267 }
4268 
mana_rdma_probe(struct gdma_dev * gd)4269 int mana_rdma_probe(struct gdma_dev *gd)
4270 {
4271 	int err = 0;
4272 
4273 	if (gd->dev_id.type != GDMA_DEVICE_MANA_IB) {
4274 		/* RDMA device is not detected on pci */
4275 		return err;
4276 	}
4277 
4278 	err = mana_gd_register_device(gd);
4279 	if (err)
4280 		return err;
4281 
4282 	err = add_adev(gd, "rdma");
4283 	if (err)
4284 		mana_gd_deregister_device(gd);
4285 
4286 	return err;
4287 }
4288 
mana_rdma_remove(struct gdma_dev * gd)4289 void mana_rdma_remove(struct gdma_dev *gd)
4290 {
4291 	struct gdma_context *gc = gd->gdma_context;
4292 
4293 	if (gd->dev_id.type != GDMA_DEVICE_MANA_IB) {
4294 		/* RDMA device is not detected on pci */
4295 		return;
4296 	}
4297 
4298 	WRITE_ONCE(gd->rdma_teardown, true);
4299 
4300 	if (gc->service_wq)
4301 		flush_workqueue(gc->service_wq);
4302 
4303 	if (gd->adev)
4304 		remove_adev(gd);
4305 
4306 	mana_gd_deregister_device(gd);
4307 }
4308 
mana_get_primary_netdev(struct mana_context * ac,u32 port_index,netdevice_tracker * tracker)4309 struct net_device *mana_get_primary_netdev(struct mana_context *ac,
4310 					   u32 port_index,
4311 					   netdevice_tracker *tracker)
4312 {
4313 	struct net_device *ndev;
4314 
4315 	if (port_index >= ac->num_ports)
4316 		return NULL;
4317 
4318 	rcu_read_lock();
4319 
4320 	/* If mana is used in netvsc, the upper netdevice should be returned. */
4321 	ndev = netdev_master_upper_dev_get_rcu(ac->ports[port_index]);
4322 
4323 	/* If there is no upper device, use the parent Ethernet device */
4324 	if (!ndev)
4325 		ndev = ac->ports[port_index];
4326 
4327 	netdev_hold(ndev, tracker, GFP_ATOMIC);
4328 	rcu_read_unlock();
4329 
4330 	return ndev;
4331 }
4332 EXPORT_SYMBOL_NS(mana_get_primary_netdev, "NET_MANA");
4333