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