1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell OcteonTx2 RVU Ethernet driver
3 *
4 * Copyright (C) 2020 Marvell International Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11 #include <linux/interrupt.h>
12 #include <linux/pci.h>
13 #include <net/tso.h>
14
15 #include "otx2_reg.h"
16 #include "otx2_common.h"
17 #include "otx2_struct.h"
18
otx2_nix_rq_op_stats(struct queue_stats * stats,struct otx2_nic * pfvf,int qidx)19 static void otx2_nix_rq_op_stats(struct queue_stats *stats,
20 struct otx2_nic *pfvf, int qidx)
21 {
22 u64 incr = (u64)qidx << 32;
23 u64 *ptr;
24
25 ptr = (u64 *)otx2_get_regaddr(pfvf, NIX_LF_RQ_OP_OCTS);
26 stats->bytes = otx2_atomic64_add(incr, ptr);
27
28 ptr = (u64 *)otx2_get_regaddr(pfvf, NIX_LF_RQ_OP_PKTS);
29 stats->pkts = otx2_atomic64_add(incr, ptr);
30 }
31
otx2_nix_sq_op_stats(struct queue_stats * stats,struct otx2_nic * pfvf,int qidx)32 static void otx2_nix_sq_op_stats(struct queue_stats *stats,
33 struct otx2_nic *pfvf, int qidx)
34 {
35 u64 incr = (u64)qidx << 32;
36 u64 *ptr;
37
38 ptr = (u64 *)otx2_get_regaddr(pfvf, NIX_LF_SQ_OP_OCTS);
39 stats->bytes = otx2_atomic64_add(incr, ptr);
40
41 ptr = (u64 *)otx2_get_regaddr(pfvf, NIX_LF_SQ_OP_PKTS);
42 stats->pkts = otx2_atomic64_add(incr, ptr);
43 }
44
otx2_update_lmac_stats(struct otx2_nic * pfvf)45 void otx2_update_lmac_stats(struct otx2_nic *pfvf)
46 {
47 struct msg_req *req;
48
49 if (!netif_running(pfvf->netdev))
50 return;
51
52 mutex_lock(&pfvf->mbox.lock);
53 req = otx2_mbox_alloc_msg_cgx_stats(&pfvf->mbox);
54 if (!req) {
55 mutex_unlock(&pfvf->mbox.lock);
56 return;
57 }
58
59 otx2_sync_mbox_msg(&pfvf->mbox);
60 mutex_unlock(&pfvf->mbox.lock);
61 }
62
otx2_update_rq_stats(struct otx2_nic * pfvf,int qidx)63 int otx2_update_rq_stats(struct otx2_nic *pfvf, int qidx)
64 {
65 struct otx2_rcv_queue *rq = &pfvf->qset.rq[qidx];
66
67 if (!pfvf->qset.rq)
68 return 0;
69
70 otx2_nix_rq_op_stats(&rq->stats, pfvf, qidx);
71 return 1;
72 }
73
otx2_update_sq_stats(struct otx2_nic * pfvf,int qidx)74 int otx2_update_sq_stats(struct otx2_nic *pfvf, int qidx)
75 {
76 struct otx2_snd_queue *sq = &pfvf->qset.sq[qidx];
77
78 if (!pfvf->qset.sq)
79 return 0;
80
81 otx2_nix_sq_op_stats(&sq->stats, pfvf, qidx);
82 return 1;
83 }
84
otx2_get_dev_stats(struct otx2_nic * pfvf)85 void otx2_get_dev_stats(struct otx2_nic *pfvf)
86 {
87 struct otx2_dev_stats *dev_stats = &pfvf->hw.dev_stats;
88
89 #define OTX2_GET_RX_STATS(reg) \
90 otx2_read64(pfvf, NIX_LF_RX_STATX(reg))
91 #define OTX2_GET_TX_STATS(reg) \
92 otx2_read64(pfvf, NIX_LF_TX_STATX(reg))
93
94 dev_stats->rx_bytes = OTX2_GET_RX_STATS(RX_OCTS);
95 dev_stats->rx_drops = OTX2_GET_RX_STATS(RX_DROP);
96 dev_stats->rx_bcast_frames = OTX2_GET_RX_STATS(RX_BCAST);
97 dev_stats->rx_mcast_frames = OTX2_GET_RX_STATS(RX_MCAST);
98 dev_stats->rx_ucast_frames = OTX2_GET_RX_STATS(RX_UCAST);
99 dev_stats->rx_frames = dev_stats->rx_bcast_frames +
100 dev_stats->rx_mcast_frames +
101 dev_stats->rx_ucast_frames;
102
103 dev_stats->tx_bytes = OTX2_GET_TX_STATS(TX_OCTS);
104 dev_stats->tx_drops = OTX2_GET_TX_STATS(TX_DROP);
105 dev_stats->tx_bcast_frames = OTX2_GET_TX_STATS(TX_BCAST);
106 dev_stats->tx_mcast_frames = OTX2_GET_TX_STATS(TX_MCAST);
107 dev_stats->tx_ucast_frames = OTX2_GET_TX_STATS(TX_UCAST);
108 dev_stats->tx_frames = dev_stats->tx_bcast_frames +
109 dev_stats->tx_mcast_frames +
110 dev_stats->tx_ucast_frames;
111 }
112
otx2_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)113 void otx2_get_stats64(struct net_device *netdev,
114 struct rtnl_link_stats64 *stats)
115 {
116 struct otx2_nic *pfvf = netdev_priv(netdev);
117 struct otx2_dev_stats *dev_stats;
118
119 otx2_get_dev_stats(pfvf);
120
121 dev_stats = &pfvf->hw.dev_stats;
122 stats->rx_bytes = dev_stats->rx_bytes;
123 stats->rx_packets = dev_stats->rx_frames;
124 stats->rx_dropped = dev_stats->rx_drops;
125 stats->multicast = dev_stats->rx_mcast_frames;
126
127 stats->tx_bytes = dev_stats->tx_bytes;
128 stats->tx_packets = dev_stats->tx_frames;
129 stats->tx_dropped = dev_stats->tx_drops;
130 }
131 EXPORT_SYMBOL(otx2_get_stats64);
132
133 /* Sync MAC address with RVU AF */
otx2_hw_set_mac_addr(struct otx2_nic * pfvf,u8 * mac)134 static int otx2_hw_set_mac_addr(struct otx2_nic *pfvf, u8 *mac)
135 {
136 struct nix_set_mac_addr *req;
137 int err;
138
139 mutex_lock(&pfvf->mbox.lock);
140 req = otx2_mbox_alloc_msg_nix_set_mac_addr(&pfvf->mbox);
141 if (!req) {
142 mutex_unlock(&pfvf->mbox.lock);
143 return -ENOMEM;
144 }
145
146 ether_addr_copy(req->mac_addr, mac);
147
148 err = otx2_sync_mbox_msg(&pfvf->mbox);
149 mutex_unlock(&pfvf->mbox.lock);
150 return err;
151 }
152
otx2_hw_get_mac_addr(struct otx2_nic * pfvf,struct net_device * netdev)153 static int otx2_hw_get_mac_addr(struct otx2_nic *pfvf,
154 struct net_device *netdev)
155 {
156 struct nix_get_mac_addr_rsp *rsp;
157 struct mbox_msghdr *msghdr;
158 struct msg_req *req;
159 int err;
160
161 mutex_lock(&pfvf->mbox.lock);
162 req = otx2_mbox_alloc_msg_nix_get_mac_addr(&pfvf->mbox);
163 if (!req) {
164 mutex_unlock(&pfvf->mbox.lock);
165 return -ENOMEM;
166 }
167
168 err = otx2_sync_mbox_msg(&pfvf->mbox);
169 if (err) {
170 mutex_unlock(&pfvf->mbox.lock);
171 return err;
172 }
173
174 msghdr = otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
175 if (IS_ERR(msghdr)) {
176 mutex_unlock(&pfvf->mbox.lock);
177 return PTR_ERR(msghdr);
178 }
179 rsp = (struct nix_get_mac_addr_rsp *)msghdr;
180 ether_addr_copy(netdev->dev_addr, rsp->mac_addr);
181 mutex_unlock(&pfvf->mbox.lock);
182
183 return 0;
184 }
185
otx2_set_mac_address(struct net_device * netdev,void * p)186 int otx2_set_mac_address(struct net_device *netdev, void *p)
187 {
188 struct otx2_nic *pfvf = netdev_priv(netdev);
189 struct sockaddr *addr = p;
190
191 if (!is_valid_ether_addr(addr->sa_data))
192 return -EADDRNOTAVAIL;
193
194 if (!otx2_hw_set_mac_addr(pfvf, addr->sa_data))
195 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
196 else
197 return -EPERM;
198
199 return 0;
200 }
201 EXPORT_SYMBOL(otx2_set_mac_address);
202
otx2_hw_set_mtu(struct otx2_nic * pfvf,int mtu)203 int otx2_hw_set_mtu(struct otx2_nic *pfvf, int mtu)
204 {
205 struct nix_frs_cfg *req;
206 int err;
207
208 mutex_lock(&pfvf->mbox.lock);
209 req = otx2_mbox_alloc_msg_nix_set_hw_frs(&pfvf->mbox);
210 if (!req) {
211 mutex_unlock(&pfvf->mbox.lock);
212 return -ENOMEM;
213 }
214
215 pfvf->max_frs = mtu + OTX2_ETH_HLEN;
216 req->maxlen = pfvf->max_frs;
217
218 err = otx2_sync_mbox_msg(&pfvf->mbox);
219 mutex_unlock(&pfvf->mbox.lock);
220 return err;
221 }
222
otx2_config_pause_frm(struct otx2_nic * pfvf)223 int otx2_config_pause_frm(struct otx2_nic *pfvf)
224 {
225 struct cgx_pause_frm_cfg *req;
226 int err;
227
228 if (is_otx2_lbkvf(pfvf->pdev))
229 return 0;
230
231 mutex_lock(&pfvf->mbox.lock);
232 req = otx2_mbox_alloc_msg_cgx_cfg_pause_frm(&pfvf->mbox);
233 if (!req) {
234 err = -ENOMEM;
235 goto unlock;
236 }
237
238 req->rx_pause = !!(pfvf->flags & OTX2_FLAG_RX_PAUSE_ENABLED);
239 req->tx_pause = !!(pfvf->flags & OTX2_FLAG_TX_PAUSE_ENABLED);
240 req->set = 1;
241
242 err = otx2_sync_mbox_msg(&pfvf->mbox);
243 unlock:
244 mutex_unlock(&pfvf->mbox.lock);
245 return err;
246 }
247
otx2_set_flowkey_cfg(struct otx2_nic * pfvf)248 int otx2_set_flowkey_cfg(struct otx2_nic *pfvf)
249 {
250 struct otx2_rss_info *rss = &pfvf->hw.rss_info;
251 struct nix_rss_flowkey_cfg *req;
252 int err;
253
254 mutex_lock(&pfvf->mbox.lock);
255 req = otx2_mbox_alloc_msg_nix_rss_flowkey_cfg(&pfvf->mbox);
256 if (!req) {
257 mutex_unlock(&pfvf->mbox.lock);
258 return -ENOMEM;
259 }
260 req->mcam_index = -1; /* Default or reserved index */
261 req->flowkey_cfg = rss->flowkey_cfg;
262 req->group = DEFAULT_RSS_CONTEXT_GROUP;
263
264 err = otx2_sync_mbox_msg(&pfvf->mbox);
265 mutex_unlock(&pfvf->mbox.lock);
266 return err;
267 }
268
otx2_set_rss_table(struct otx2_nic * pfvf)269 int otx2_set_rss_table(struct otx2_nic *pfvf)
270 {
271 struct otx2_rss_info *rss = &pfvf->hw.rss_info;
272 struct mbox *mbox = &pfvf->mbox;
273 struct nix_aq_enq_req *aq;
274 int idx, err;
275
276 mutex_lock(&mbox->lock);
277 /* Get memory to put this msg */
278 for (idx = 0; idx < rss->rss_size; idx++) {
279 aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
280 if (!aq) {
281 /* The shared memory buffer can be full.
282 * Flush it and retry
283 */
284 err = otx2_sync_mbox_msg(mbox);
285 if (err) {
286 mutex_unlock(&mbox->lock);
287 return err;
288 }
289 aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
290 if (!aq) {
291 mutex_unlock(&mbox->lock);
292 return -ENOMEM;
293 }
294 }
295
296 aq->rss.rq = rss->ind_tbl[idx];
297
298 /* Fill AQ info */
299 aq->qidx = idx;
300 aq->ctype = NIX_AQ_CTYPE_RSS;
301 aq->op = NIX_AQ_INSTOP_INIT;
302 }
303 err = otx2_sync_mbox_msg(mbox);
304 mutex_unlock(&mbox->lock);
305 return err;
306 }
307
otx2_set_rss_key(struct otx2_nic * pfvf)308 void otx2_set_rss_key(struct otx2_nic *pfvf)
309 {
310 struct otx2_rss_info *rss = &pfvf->hw.rss_info;
311 u64 *key = (u64 *)&rss->key[4];
312 int idx;
313
314 /* 352bit or 44byte key needs to be configured as below
315 * NIX_LF_RX_SECRETX0 = key<351:288>
316 * NIX_LF_RX_SECRETX1 = key<287:224>
317 * NIX_LF_RX_SECRETX2 = key<223:160>
318 * NIX_LF_RX_SECRETX3 = key<159:96>
319 * NIX_LF_RX_SECRETX4 = key<95:32>
320 * NIX_LF_RX_SECRETX5<63:32> = key<31:0>
321 */
322 otx2_write64(pfvf, NIX_LF_RX_SECRETX(5),
323 (u64)(*((u32 *)&rss->key)) << 32);
324 idx = sizeof(rss->key) / sizeof(u64);
325 while (idx > 0) {
326 idx--;
327 otx2_write64(pfvf, NIX_LF_RX_SECRETX(idx), *key++);
328 }
329 }
330
otx2_rss_init(struct otx2_nic * pfvf)331 int otx2_rss_init(struct otx2_nic *pfvf)
332 {
333 struct otx2_rss_info *rss = &pfvf->hw.rss_info;
334 int idx, ret = 0;
335
336 rss->rss_size = sizeof(rss->ind_tbl);
337
338 /* Init RSS key if it is not setup already */
339 if (!rss->enable)
340 netdev_rss_key_fill(rss->key, sizeof(rss->key));
341 otx2_set_rss_key(pfvf);
342
343 if (!netif_is_rxfh_configured(pfvf->netdev)) {
344 /* Default indirection table */
345 for (idx = 0; idx < rss->rss_size; idx++)
346 rss->ind_tbl[idx] =
347 ethtool_rxfh_indir_default(idx,
348 pfvf->hw.rx_queues);
349 }
350 ret = otx2_set_rss_table(pfvf);
351 if (ret)
352 return ret;
353
354 /* Flowkey or hash config to be used for generating flow tag */
355 rss->flowkey_cfg = rss->enable ? rss->flowkey_cfg :
356 NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6 |
357 NIX_FLOW_KEY_TYPE_TCP | NIX_FLOW_KEY_TYPE_UDP |
358 NIX_FLOW_KEY_TYPE_SCTP | NIX_FLOW_KEY_TYPE_VLAN;
359
360 ret = otx2_set_flowkey_cfg(pfvf);
361 if (ret)
362 return ret;
363
364 rss->enable = true;
365 return 0;
366 }
367
368 /* Setup UDP segmentation algorithm in HW */
otx2_setup_udp_segmentation(struct nix_lso_format_cfg * lso,bool v4)369 static void otx2_setup_udp_segmentation(struct nix_lso_format_cfg *lso, bool v4)
370 {
371 struct nix_lso_format *field;
372
373 field = (struct nix_lso_format *)&lso->fields[0];
374 lso->field_mask = GENMASK(18, 0);
375
376 /* IP's Length field */
377 field->layer = NIX_TXLAYER_OL3;
378 /* In ipv4, length field is at offset 2 bytes, for ipv6 it's 4 */
379 field->offset = v4 ? 2 : 4;
380 field->sizem1 = 1; /* i.e 2 bytes */
381 field->alg = NIX_LSOALG_ADD_PAYLEN;
382 field++;
383
384 /* No ID field in IPv6 header */
385 if (v4) {
386 /* Increment IPID */
387 field->layer = NIX_TXLAYER_OL3;
388 field->offset = 4;
389 field->sizem1 = 1; /* i.e 2 bytes */
390 field->alg = NIX_LSOALG_ADD_SEGNUM;
391 field++;
392 }
393
394 /* Update length in UDP header */
395 field->layer = NIX_TXLAYER_OL4;
396 field->offset = 4;
397 field->sizem1 = 1;
398 field->alg = NIX_LSOALG_ADD_PAYLEN;
399 }
400
401 /* Setup segmentation algorithms in HW and retrieve algorithm index */
otx2_setup_segmentation(struct otx2_nic * pfvf)402 void otx2_setup_segmentation(struct otx2_nic *pfvf)
403 {
404 struct nix_lso_format_cfg_rsp *rsp;
405 struct nix_lso_format_cfg *lso;
406 struct otx2_hw *hw = &pfvf->hw;
407 int err;
408
409 mutex_lock(&pfvf->mbox.lock);
410
411 /* UDPv4 segmentation */
412 lso = otx2_mbox_alloc_msg_nix_lso_format_cfg(&pfvf->mbox);
413 if (!lso)
414 goto fail;
415
416 /* Setup UDP/IP header fields that HW should update per segment */
417 otx2_setup_udp_segmentation(lso, true);
418
419 err = otx2_sync_mbox_msg(&pfvf->mbox);
420 if (err)
421 goto fail;
422
423 rsp = (struct nix_lso_format_cfg_rsp *)
424 otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &lso->hdr);
425 if (IS_ERR(rsp))
426 goto fail;
427
428 hw->lso_udpv4_idx = rsp->lso_format_idx;
429
430 /* UDPv6 segmentation */
431 lso = otx2_mbox_alloc_msg_nix_lso_format_cfg(&pfvf->mbox);
432 if (!lso)
433 goto fail;
434
435 /* Setup UDP/IP header fields that HW should update per segment */
436 otx2_setup_udp_segmentation(lso, false);
437
438 err = otx2_sync_mbox_msg(&pfvf->mbox);
439 if (err)
440 goto fail;
441
442 rsp = (struct nix_lso_format_cfg_rsp *)
443 otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &lso->hdr);
444 if (IS_ERR(rsp))
445 goto fail;
446
447 hw->lso_udpv6_idx = rsp->lso_format_idx;
448 mutex_unlock(&pfvf->mbox.lock);
449 return;
450 fail:
451 mutex_unlock(&pfvf->mbox.lock);
452 netdev_info(pfvf->netdev,
453 "Failed to get LSO index for UDP GSO offload, disabling\n");
454 pfvf->netdev->hw_features &= ~NETIF_F_GSO_UDP_L4;
455 }
456
otx2_config_irq_coalescing(struct otx2_nic * pfvf,int qidx)457 void otx2_config_irq_coalescing(struct otx2_nic *pfvf, int qidx)
458 {
459 /* Configure CQE interrupt coalescing parameters
460 *
461 * HW triggers an irq when ECOUNT > cq_ecount_wait, hence
462 * set 1 less than cq_ecount_wait. And cq_time_wait is in
463 * usecs, convert that to 100ns count.
464 */
465 otx2_write64(pfvf, NIX_LF_CINTX_WAIT(qidx),
466 ((u64)(pfvf->hw.cq_time_wait * 10) << 48) |
467 ((u64)pfvf->hw.cq_qcount_wait << 32) |
468 (pfvf->hw.cq_ecount_wait - 1));
469 }
470
__otx2_alloc_rbuf(struct otx2_nic * pfvf,struct otx2_pool * pool)471 dma_addr_t __otx2_alloc_rbuf(struct otx2_nic *pfvf, struct otx2_pool *pool)
472 {
473 dma_addr_t iova;
474 u8 *buf;
475
476 buf = napi_alloc_frag(pool->rbsize + OTX2_ALIGN);
477 if (unlikely(!buf))
478 return -ENOMEM;
479
480 buf = PTR_ALIGN(buf, OTX2_ALIGN);
481 iova = dma_map_single_attrs(pfvf->dev, buf, pool->rbsize,
482 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
483 if (unlikely(dma_mapping_error(pfvf->dev, iova))) {
484 page_frag_free(buf);
485 return -ENOMEM;
486 }
487
488 return iova;
489 }
490
otx2_alloc_rbuf(struct otx2_nic * pfvf,struct otx2_pool * pool)491 static dma_addr_t otx2_alloc_rbuf(struct otx2_nic *pfvf, struct otx2_pool *pool)
492 {
493 dma_addr_t addr;
494
495 local_bh_disable();
496 addr = __otx2_alloc_rbuf(pfvf, pool);
497 local_bh_enable();
498 return addr;
499 }
500
otx2_tx_timeout(struct net_device * netdev,unsigned int txq)501 void otx2_tx_timeout(struct net_device *netdev, unsigned int txq)
502 {
503 struct otx2_nic *pfvf = netdev_priv(netdev);
504
505 schedule_work(&pfvf->reset_task);
506 }
507 EXPORT_SYMBOL(otx2_tx_timeout);
508
otx2_get_mac_from_af(struct net_device * netdev)509 void otx2_get_mac_from_af(struct net_device *netdev)
510 {
511 struct otx2_nic *pfvf = netdev_priv(netdev);
512 int err;
513
514 err = otx2_hw_get_mac_addr(pfvf, netdev);
515 if (err)
516 dev_warn(pfvf->dev, "Failed to read mac from hardware\n");
517
518 /* If AF doesn't provide a valid MAC, generate a random one */
519 if (!is_valid_ether_addr(netdev->dev_addr))
520 eth_hw_addr_random(netdev);
521 }
522 EXPORT_SYMBOL(otx2_get_mac_from_af);
523
otx2_get_link(struct otx2_nic * pfvf)524 static int otx2_get_link(struct otx2_nic *pfvf)
525 {
526 int link = 0;
527 u16 map;
528
529 /* cgx lmac link */
530 if (pfvf->hw.tx_chan_base >= CGX_CHAN_BASE) {
531 map = pfvf->hw.tx_chan_base & 0x7FF;
532 link = 4 * ((map >> 8) & 0xF) + ((map >> 4) & 0xF);
533 }
534 /* LBK channel */
535 if (pfvf->hw.tx_chan_base < SDP_CHAN_BASE)
536 link = 12;
537
538 return link;
539 }
540
otx2_txschq_config(struct otx2_nic * pfvf,int lvl)541 int otx2_txschq_config(struct otx2_nic *pfvf, int lvl)
542 {
543 struct otx2_hw *hw = &pfvf->hw;
544 struct nix_txschq_config *req;
545 u64 schq, parent;
546
547 req = otx2_mbox_alloc_msg_nix_txschq_cfg(&pfvf->mbox);
548 if (!req)
549 return -ENOMEM;
550
551 req->lvl = lvl;
552 req->num_regs = 1;
553
554 schq = hw->txschq_list[lvl][0];
555 /* Set topology e.t.c configuration */
556 if (lvl == NIX_TXSCH_LVL_SMQ) {
557 req->reg[0] = NIX_AF_SMQX_CFG(schq);
558 req->regval[0] = ((OTX2_MAX_MTU + OTX2_ETH_HLEN) << 8) |
559 OTX2_MIN_MTU;
560
561 req->regval[0] |= (0x20ULL << 51) | (0x80ULL << 39) |
562 (0x2ULL << 36);
563 req->num_regs++;
564 /* MDQ config */
565 parent = hw->txschq_list[NIX_TXSCH_LVL_TL4][0];
566 req->reg[1] = NIX_AF_MDQX_PARENT(schq);
567 req->regval[1] = parent << 16;
568 req->num_regs++;
569 /* Set DWRR quantum */
570 req->reg[2] = NIX_AF_MDQX_SCHEDULE(schq);
571 req->regval[2] = DFLT_RR_QTM;
572 } else if (lvl == NIX_TXSCH_LVL_TL4) {
573 parent = hw->txschq_list[NIX_TXSCH_LVL_TL3][0];
574 req->reg[0] = NIX_AF_TL4X_PARENT(schq);
575 req->regval[0] = parent << 16;
576 req->num_regs++;
577 req->reg[1] = NIX_AF_TL4X_SCHEDULE(schq);
578 req->regval[1] = DFLT_RR_QTM;
579 } else if (lvl == NIX_TXSCH_LVL_TL3) {
580 parent = hw->txschq_list[NIX_TXSCH_LVL_TL2][0];
581 req->reg[0] = NIX_AF_TL3X_PARENT(schq);
582 req->regval[0] = parent << 16;
583 req->num_regs++;
584 req->reg[1] = NIX_AF_TL3X_SCHEDULE(schq);
585 req->regval[1] = DFLT_RR_QTM;
586 } else if (lvl == NIX_TXSCH_LVL_TL2) {
587 parent = hw->txschq_list[NIX_TXSCH_LVL_TL1][0];
588 req->reg[0] = NIX_AF_TL2X_PARENT(schq);
589 req->regval[0] = parent << 16;
590
591 req->num_regs++;
592 req->reg[1] = NIX_AF_TL2X_SCHEDULE(schq);
593 req->regval[1] = TXSCH_TL1_DFLT_RR_PRIO << 24 | DFLT_RR_QTM;
594
595 req->num_regs++;
596 req->reg[2] = NIX_AF_TL3_TL2X_LINKX_CFG(schq,
597 otx2_get_link(pfvf));
598 /* Enable this queue and backpressure */
599 req->regval[2] = BIT_ULL(13) | BIT_ULL(12);
600
601 } else if (lvl == NIX_TXSCH_LVL_TL1) {
602 /* Default config for TL1.
603 * For VF this is always ignored.
604 */
605
606 /* Set DWRR quantum */
607 req->reg[0] = NIX_AF_TL1X_SCHEDULE(schq);
608 req->regval[0] = TXSCH_TL1_DFLT_RR_QTM;
609
610 req->num_regs++;
611 req->reg[1] = NIX_AF_TL1X_TOPOLOGY(schq);
612 req->regval[1] = (TXSCH_TL1_DFLT_RR_PRIO << 1);
613
614 req->num_regs++;
615 req->reg[2] = NIX_AF_TL1X_CIR(schq);
616 req->regval[2] = 0;
617 }
618
619 return otx2_sync_mbox_msg(&pfvf->mbox);
620 }
621
otx2_txsch_alloc(struct otx2_nic * pfvf)622 int otx2_txsch_alloc(struct otx2_nic *pfvf)
623 {
624 struct nix_txsch_alloc_req *req;
625 int lvl;
626
627 /* Get memory to put this msg */
628 req = otx2_mbox_alloc_msg_nix_txsch_alloc(&pfvf->mbox);
629 if (!req)
630 return -ENOMEM;
631
632 /* Request one schq per level */
633 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++)
634 req->schq[lvl] = 1;
635
636 return otx2_sync_mbox_msg(&pfvf->mbox);
637 }
638
otx2_txschq_stop(struct otx2_nic * pfvf)639 int otx2_txschq_stop(struct otx2_nic *pfvf)
640 {
641 struct nix_txsch_free_req *free_req;
642 int lvl, schq, err;
643
644 mutex_lock(&pfvf->mbox.lock);
645 /* Free the transmit schedulers */
646 free_req = otx2_mbox_alloc_msg_nix_txsch_free(&pfvf->mbox);
647 if (!free_req) {
648 mutex_unlock(&pfvf->mbox.lock);
649 return -ENOMEM;
650 }
651
652 free_req->flags = TXSCHQ_FREE_ALL;
653 err = otx2_sync_mbox_msg(&pfvf->mbox);
654 mutex_unlock(&pfvf->mbox.lock);
655
656 /* Clear the txschq list */
657 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
658 for (schq = 0; schq < MAX_TXSCHQ_PER_FUNC; schq++)
659 pfvf->hw.txschq_list[lvl][schq] = 0;
660 }
661 return err;
662 }
663
otx2_sqb_flush(struct otx2_nic * pfvf)664 void otx2_sqb_flush(struct otx2_nic *pfvf)
665 {
666 int qidx, sqe_tail, sqe_head;
667 u64 incr, *ptr, val;
668 int timeout = 1000;
669
670 ptr = (u64 *)otx2_get_regaddr(pfvf, NIX_LF_SQ_OP_STATUS);
671 for (qidx = 0; qidx < pfvf->hw.tx_queues; qidx++) {
672 incr = (u64)qidx << 32;
673 while (timeout) {
674 val = otx2_atomic64_add(incr, ptr);
675 sqe_head = (val >> 20) & 0x3F;
676 sqe_tail = (val >> 28) & 0x3F;
677 if (sqe_head == sqe_tail)
678 break;
679 usleep_range(1, 3);
680 timeout--;
681 }
682 }
683 }
684
685 /* RED and drop levels of CQ on packet reception.
686 * For CQ level is measure of emptiness ( 0x0 = full, 255 = empty).
687 */
688 #define RQ_PASS_LVL_CQ(skid, qsize) ((((skid) + 16) * 256) / (qsize))
689 #define RQ_DROP_LVL_CQ(skid, qsize) (((skid) * 256) / (qsize))
690
691 /* RED and drop levels of AURA for packet reception.
692 * For AURA level is measure of fullness (0x0 = empty, 255 = full).
693 * Eg: For RQ length 1K, for pass/drop level 204/230.
694 * RED accepts pkts if free pointers > 102 & <= 205.
695 * Drops pkts if free pointers < 102.
696 */
697 #define RQ_BP_LVL_AURA (255 - ((85 * 256) / 100)) /* BP when 85% is full */
698 #define RQ_PASS_LVL_AURA (255 - ((95 * 256) / 100)) /* RED when 95% is full */
699 #define RQ_DROP_LVL_AURA (255 - ((99 * 256) / 100)) /* Drop when 99% is full */
700
701 /* Send skid of 2000 packets required for CQ size of 4K CQEs. */
702 #define SEND_CQ_SKID 2000
703
otx2_rq_init(struct otx2_nic * pfvf,u16 qidx,u16 lpb_aura)704 static int otx2_rq_init(struct otx2_nic *pfvf, u16 qidx, u16 lpb_aura)
705 {
706 struct otx2_qset *qset = &pfvf->qset;
707 struct nix_aq_enq_req *aq;
708
709 /* Get memory to put this msg */
710 aq = otx2_mbox_alloc_msg_nix_aq_enq(&pfvf->mbox);
711 if (!aq)
712 return -ENOMEM;
713
714 aq->rq.cq = qidx;
715 aq->rq.ena = 1;
716 aq->rq.pb_caching = 1;
717 aq->rq.lpb_aura = lpb_aura; /* Use large packet buffer aura */
718 aq->rq.lpb_sizem1 = (DMA_BUFFER_LEN(pfvf->rbsize) / 8) - 1;
719 aq->rq.xqe_imm_size = 0; /* Copying of packet to CQE not needed */
720 aq->rq.flow_tagw = 32; /* Copy full 32bit flow_tag to CQE header */
721 aq->rq.qint_idx = 0;
722 aq->rq.lpb_drop_ena = 1; /* Enable RED dropping for AURA */
723 aq->rq.xqe_drop_ena = 1; /* Enable RED dropping for CQ/SSO */
724 aq->rq.xqe_pass = RQ_PASS_LVL_CQ(pfvf->hw.rq_skid, qset->rqe_cnt);
725 aq->rq.xqe_drop = RQ_DROP_LVL_CQ(pfvf->hw.rq_skid, qset->rqe_cnt);
726 aq->rq.lpb_aura_pass = RQ_PASS_LVL_AURA;
727 aq->rq.lpb_aura_drop = RQ_DROP_LVL_AURA;
728
729 /* Fill AQ info */
730 aq->qidx = qidx;
731 aq->ctype = NIX_AQ_CTYPE_RQ;
732 aq->op = NIX_AQ_INSTOP_INIT;
733
734 return otx2_sync_mbox_msg(&pfvf->mbox);
735 }
736
otx2_sq_init(struct otx2_nic * pfvf,u16 qidx,u16 sqb_aura)737 static int otx2_sq_init(struct otx2_nic *pfvf, u16 qidx, u16 sqb_aura)
738 {
739 struct otx2_qset *qset = &pfvf->qset;
740 struct otx2_snd_queue *sq;
741 struct nix_aq_enq_req *aq;
742 struct otx2_pool *pool;
743 int err;
744
745 pool = &pfvf->qset.pool[sqb_aura];
746 sq = &qset->sq[qidx];
747 sq->sqe_size = NIX_SQESZ_W16 ? 64 : 128;
748 sq->sqe_cnt = qset->sqe_cnt;
749
750 err = qmem_alloc(pfvf->dev, &sq->sqe, 1, sq->sqe_size);
751 if (err)
752 return err;
753
754 err = qmem_alloc(pfvf->dev, &sq->tso_hdrs, qset->sqe_cnt,
755 TSO_HEADER_SIZE);
756 if (err)
757 return err;
758
759 sq->sqe_base = sq->sqe->base;
760 sq->sg = kcalloc(qset->sqe_cnt, sizeof(struct sg_list), GFP_KERNEL);
761 if (!sq->sg)
762 return -ENOMEM;
763
764 if (pfvf->ptp) {
765 err = qmem_alloc(pfvf->dev, &sq->timestamps, qset->sqe_cnt,
766 sizeof(*sq->timestamps));
767 if (err)
768 return err;
769 }
770
771 sq->head = 0;
772 sq->sqe_per_sqb = (pfvf->hw.sqb_size / sq->sqe_size) - 1;
773 sq->num_sqbs = (qset->sqe_cnt + sq->sqe_per_sqb) / sq->sqe_per_sqb;
774 /* Set SQE threshold to 10% of total SQEs */
775 sq->sqe_thresh = ((sq->num_sqbs * sq->sqe_per_sqb) * 10) / 100;
776 sq->aura_id = sqb_aura;
777 sq->aura_fc_addr = pool->fc_addr->base;
778 sq->lmt_addr = (__force u64 *)(pfvf->reg_base + LMT_LF_LMTLINEX(qidx));
779 sq->io_addr = (__force u64)otx2_get_regaddr(pfvf, NIX_LF_OP_SENDX(0));
780
781 sq->stats.bytes = 0;
782 sq->stats.pkts = 0;
783
784 /* Get memory to put this msg */
785 aq = otx2_mbox_alloc_msg_nix_aq_enq(&pfvf->mbox);
786 if (!aq)
787 return -ENOMEM;
788
789 aq->sq.cq = pfvf->hw.rx_queues + qidx;
790 aq->sq.max_sqe_size = NIX_MAXSQESZ_W16; /* 128 byte */
791 aq->sq.cq_ena = 1;
792 aq->sq.ena = 1;
793 /* Only one SMQ is allocated, map all SQ's to that SMQ */
794 aq->sq.smq = pfvf->hw.txschq_list[NIX_TXSCH_LVL_SMQ][0];
795 aq->sq.smq_rr_quantum = DFLT_RR_QTM;
796 aq->sq.default_chan = pfvf->hw.tx_chan_base;
797 aq->sq.sqe_stype = NIX_STYPE_STF; /* Cache SQB */
798 aq->sq.sqb_aura = sqb_aura;
799 aq->sq.sq_int_ena = NIX_SQINT_BITS;
800 aq->sq.qint_idx = 0;
801 /* Due pipelining impact minimum 2000 unused SQ CQE's
802 * need to maintain to avoid CQ overflow.
803 */
804 aq->sq.cq_limit = ((SEND_CQ_SKID * 256) / (sq->sqe_cnt));
805
806 /* Fill AQ info */
807 aq->qidx = qidx;
808 aq->ctype = NIX_AQ_CTYPE_SQ;
809 aq->op = NIX_AQ_INSTOP_INIT;
810
811 return otx2_sync_mbox_msg(&pfvf->mbox);
812 }
813
otx2_cq_init(struct otx2_nic * pfvf,u16 qidx)814 static int otx2_cq_init(struct otx2_nic *pfvf, u16 qidx)
815 {
816 struct otx2_qset *qset = &pfvf->qset;
817 struct nix_aq_enq_req *aq;
818 struct otx2_cq_queue *cq;
819 int err, pool_id;
820
821 cq = &qset->cq[qidx];
822 cq->cq_idx = qidx;
823 if (qidx < pfvf->hw.rx_queues) {
824 cq->cq_type = CQ_RX;
825 cq->cint_idx = qidx;
826 cq->cqe_cnt = qset->rqe_cnt;
827 } else {
828 cq->cq_type = CQ_TX;
829 cq->cint_idx = qidx - pfvf->hw.rx_queues;
830 cq->cqe_cnt = qset->sqe_cnt;
831 }
832 cq->cqe_size = pfvf->qset.xqe_size;
833
834 /* Allocate memory for CQEs */
835 err = qmem_alloc(pfvf->dev, &cq->cqe, cq->cqe_cnt, cq->cqe_size);
836 if (err)
837 return err;
838
839 /* Save CQE CPU base for faster reference */
840 cq->cqe_base = cq->cqe->base;
841 /* In case where all RQs auras point to single pool,
842 * all CQs receive buffer pool also point to same pool.
843 */
844 pool_id = ((cq->cq_type == CQ_RX) &&
845 (pfvf->hw.rqpool_cnt != pfvf->hw.rx_queues)) ? 0 : qidx;
846 cq->rbpool = &qset->pool[pool_id];
847 cq->refill_task_sched = false;
848
849 /* Get memory to put this msg */
850 aq = otx2_mbox_alloc_msg_nix_aq_enq(&pfvf->mbox);
851 if (!aq)
852 return -ENOMEM;
853
854 aq->cq.ena = 1;
855 aq->cq.qsize = Q_SIZE(cq->cqe_cnt, 4);
856 aq->cq.caching = 1;
857 aq->cq.base = cq->cqe->iova;
858 aq->cq.cint_idx = cq->cint_idx;
859 aq->cq.cq_err_int_ena = NIX_CQERRINT_BITS;
860 aq->cq.qint_idx = 0;
861 aq->cq.avg_level = 255;
862
863 if (qidx < pfvf->hw.rx_queues) {
864 aq->cq.drop = RQ_DROP_LVL_CQ(pfvf->hw.rq_skid, cq->cqe_cnt);
865 aq->cq.drop_ena = 1;
866
867 /* Enable receive CQ backpressure */
868 aq->cq.bp_ena = 1;
869 aq->cq.bpid = pfvf->bpid[0];
870
871 /* Set backpressure level is same as cq pass level */
872 aq->cq.bp = RQ_PASS_LVL_CQ(pfvf->hw.rq_skid, qset->rqe_cnt);
873 }
874
875 /* Fill AQ info */
876 aq->qidx = qidx;
877 aq->ctype = NIX_AQ_CTYPE_CQ;
878 aq->op = NIX_AQ_INSTOP_INIT;
879
880 return otx2_sync_mbox_msg(&pfvf->mbox);
881 }
882
otx2_pool_refill_task(struct work_struct * work)883 static void otx2_pool_refill_task(struct work_struct *work)
884 {
885 struct otx2_cq_queue *cq;
886 struct otx2_pool *rbpool;
887 struct refill_work *wrk;
888 int qidx, free_ptrs = 0;
889 struct otx2_nic *pfvf;
890 s64 bufptr;
891
892 wrk = container_of(work, struct refill_work, pool_refill_work.work);
893 pfvf = wrk->pf;
894 qidx = wrk - pfvf->refill_wrk;
895 cq = &pfvf->qset.cq[qidx];
896 rbpool = cq->rbpool;
897 free_ptrs = cq->pool_ptrs;
898
899 while (cq->pool_ptrs) {
900 bufptr = otx2_alloc_rbuf(pfvf, rbpool);
901 if (bufptr <= 0) {
902 /* Schedule a WQ if we fails to free atleast half of the
903 * pointers else enable napi for this RQ.
904 */
905 if (!((free_ptrs - cq->pool_ptrs) > free_ptrs / 2)) {
906 struct delayed_work *dwork;
907
908 dwork = &wrk->pool_refill_work;
909 schedule_delayed_work(dwork,
910 msecs_to_jiffies(100));
911 } else {
912 cq->refill_task_sched = false;
913 }
914 return;
915 }
916 otx2_aura_freeptr(pfvf, qidx, bufptr + OTX2_HEAD_ROOM);
917 cq->pool_ptrs--;
918 }
919 cq->refill_task_sched = false;
920 }
921
otx2_config_nix_queues(struct otx2_nic * pfvf)922 int otx2_config_nix_queues(struct otx2_nic *pfvf)
923 {
924 int qidx, err;
925
926 /* Initialize RX queues */
927 for (qidx = 0; qidx < pfvf->hw.rx_queues; qidx++) {
928 u16 lpb_aura = otx2_get_pool_idx(pfvf, AURA_NIX_RQ, qidx);
929
930 err = otx2_rq_init(pfvf, qidx, lpb_aura);
931 if (err)
932 return err;
933 }
934
935 /* Initialize TX queues */
936 for (qidx = 0; qidx < pfvf->hw.tx_queues; qidx++) {
937 u16 sqb_aura = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
938
939 err = otx2_sq_init(pfvf, qidx, sqb_aura);
940 if (err)
941 return err;
942 }
943
944 /* Initialize completion queues */
945 for (qidx = 0; qidx < pfvf->qset.cq_cnt; qidx++) {
946 err = otx2_cq_init(pfvf, qidx);
947 if (err)
948 return err;
949 }
950
951 /* Initialize work queue for receive buffer refill */
952 pfvf->refill_wrk = devm_kcalloc(pfvf->dev, pfvf->qset.cq_cnt,
953 sizeof(struct refill_work), GFP_KERNEL);
954 if (!pfvf->refill_wrk)
955 return -ENOMEM;
956
957 for (qidx = 0; qidx < pfvf->qset.cq_cnt; qidx++) {
958 pfvf->refill_wrk[qidx].pf = pfvf;
959 INIT_DELAYED_WORK(&pfvf->refill_wrk[qidx].pool_refill_work,
960 otx2_pool_refill_task);
961 }
962 return 0;
963 }
964
otx2_config_nix(struct otx2_nic * pfvf)965 int otx2_config_nix(struct otx2_nic *pfvf)
966 {
967 struct nix_lf_alloc_req *nixlf;
968 struct nix_lf_alloc_rsp *rsp;
969 int err;
970
971 pfvf->qset.xqe_size = NIX_XQESZ_W16 ? 128 : 512;
972
973 /* Get memory to put this msg */
974 nixlf = otx2_mbox_alloc_msg_nix_lf_alloc(&pfvf->mbox);
975 if (!nixlf)
976 return -ENOMEM;
977
978 /* Set RQ/SQ/CQ counts */
979 nixlf->rq_cnt = pfvf->hw.rx_queues;
980 nixlf->sq_cnt = pfvf->hw.tx_queues;
981 nixlf->cq_cnt = pfvf->qset.cq_cnt;
982 nixlf->rss_sz = MAX_RSS_INDIR_TBL_SIZE;
983 nixlf->rss_grps = 1; /* Single RSS indir table supported, for now */
984 nixlf->xqe_sz = NIX_XQESZ_W16;
985 /* We don't know absolute NPA LF idx attached.
986 * AF will replace 'RVU_DEFAULT_PF_FUNC' with
987 * NPA LF attached to this RVU PF/VF.
988 */
989 nixlf->npa_func = RVU_DEFAULT_PF_FUNC;
990 /* Disable alignment pad, enable L2 length check,
991 * enable L4 TCP/UDP checksum verification.
992 */
993 nixlf->rx_cfg = BIT_ULL(33) | BIT_ULL(35) | BIT_ULL(37);
994
995 err = otx2_sync_mbox_msg(&pfvf->mbox);
996 if (err)
997 return err;
998
999 rsp = (struct nix_lf_alloc_rsp *)otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0,
1000 &nixlf->hdr);
1001 if (IS_ERR(rsp))
1002 return PTR_ERR(rsp);
1003
1004 if (rsp->qints < 1)
1005 return -ENXIO;
1006
1007 return rsp->hdr.rc;
1008 }
1009
otx2_sq_free_sqbs(struct otx2_nic * pfvf)1010 void otx2_sq_free_sqbs(struct otx2_nic *pfvf)
1011 {
1012 struct otx2_qset *qset = &pfvf->qset;
1013 struct otx2_hw *hw = &pfvf->hw;
1014 struct otx2_snd_queue *sq;
1015 int sqb, qidx;
1016 u64 iova, pa;
1017
1018 for (qidx = 0; qidx < hw->tx_queues; qidx++) {
1019 sq = &qset->sq[qidx];
1020 if (!sq->sqb_ptrs)
1021 continue;
1022 for (sqb = 0; sqb < sq->sqb_count; sqb++) {
1023 if (!sq->sqb_ptrs[sqb])
1024 continue;
1025 iova = sq->sqb_ptrs[sqb];
1026 pa = otx2_iova_to_phys(pfvf->iommu_domain, iova);
1027 dma_unmap_page_attrs(pfvf->dev, iova, hw->sqb_size,
1028 DMA_FROM_DEVICE,
1029 DMA_ATTR_SKIP_CPU_SYNC);
1030 put_page(virt_to_page(phys_to_virt(pa)));
1031 }
1032 sq->sqb_count = 0;
1033 }
1034 }
1035
otx2_free_aura_ptr(struct otx2_nic * pfvf,int type)1036 void otx2_free_aura_ptr(struct otx2_nic *pfvf, int type)
1037 {
1038 int pool_id, pool_start = 0, pool_end = 0, size = 0;
1039 u64 iova, pa;
1040
1041 if (type == AURA_NIX_SQ) {
1042 pool_start = otx2_get_pool_idx(pfvf, type, 0);
1043 pool_end = pool_start + pfvf->hw.sqpool_cnt;
1044 size = pfvf->hw.sqb_size;
1045 }
1046 if (type == AURA_NIX_RQ) {
1047 pool_start = otx2_get_pool_idx(pfvf, type, 0);
1048 pool_end = pfvf->hw.rqpool_cnt;
1049 size = pfvf->rbsize;
1050 }
1051
1052 /* Free SQB and RQB pointers from the aura pool */
1053 for (pool_id = pool_start; pool_id < pool_end; pool_id++) {
1054 iova = otx2_aura_allocptr(pfvf, pool_id);
1055 while (iova) {
1056 if (type == AURA_NIX_RQ)
1057 iova -= OTX2_HEAD_ROOM;
1058
1059 pa = otx2_iova_to_phys(pfvf->iommu_domain, iova);
1060 dma_unmap_page_attrs(pfvf->dev, iova, size,
1061 DMA_FROM_DEVICE,
1062 DMA_ATTR_SKIP_CPU_SYNC);
1063 put_page(virt_to_page(phys_to_virt(pa)));
1064 iova = otx2_aura_allocptr(pfvf, pool_id);
1065 }
1066 }
1067 }
1068
otx2_aura_pool_free(struct otx2_nic * pfvf)1069 void otx2_aura_pool_free(struct otx2_nic *pfvf)
1070 {
1071 struct otx2_pool *pool;
1072 int pool_id;
1073
1074 if (!pfvf->qset.pool)
1075 return;
1076
1077 for (pool_id = 0; pool_id < pfvf->hw.pool_cnt; pool_id++) {
1078 pool = &pfvf->qset.pool[pool_id];
1079 qmem_free(pfvf->dev, pool->stack);
1080 qmem_free(pfvf->dev, pool->fc_addr);
1081 }
1082 devm_kfree(pfvf->dev, pfvf->qset.pool);
1083 pfvf->qset.pool = NULL;
1084 }
1085
otx2_aura_init(struct otx2_nic * pfvf,int aura_id,int pool_id,int numptrs)1086 static int otx2_aura_init(struct otx2_nic *pfvf, int aura_id,
1087 int pool_id, int numptrs)
1088 {
1089 struct npa_aq_enq_req *aq;
1090 struct otx2_pool *pool;
1091 int err;
1092
1093 pool = &pfvf->qset.pool[pool_id];
1094
1095 /* Allocate memory for HW to update Aura count.
1096 * Alloc one cache line, so that it fits all FC_STYPE modes.
1097 */
1098 if (!pool->fc_addr) {
1099 err = qmem_alloc(pfvf->dev, &pool->fc_addr, 1, OTX2_ALIGN);
1100 if (err)
1101 return err;
1102 }
1103
1104 /* Initialize this aura's context via AF */
1105 aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1106 if (!aq) {
1107 /* Shared mbox memory buffer is full, flush it and retry */
1108 err = otx2_sync_mbox_msg(&pfvf->mbox);
1109 if (err)
1110 return err;
1111 aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1112 if (!aq)
1113 return -ENOMEM;
1114 }
1115
1116 aq->aura_id = aura_id;
1117 /* Will be filled by AF with correct pool context address */
1118 aq->aura.pool_addr = pool_id;
1119 aq->aura.pool_caching = 1;
1120 aq->aura.shift = ilog2(numptrs) - 8;
1121 aq->aura.count = numptrs;
1122 aq->aura.limit = numptrs;
1123 aq->aura.avg_level = 255;
1124 aq->aura.ena = 1;
1125 aq->aura.fc_ena = 1;
1126 aq->aura.fc_addr = pool->fc_addr->iova;
1127 aq->aura.fc_hyst_bits = 0; /* Store count on all updates */
1128
1129 /* Enable backpressure for RQ aura */
1130 if (aura_id < pfvf->hw.rqpool_cnt) {
1131 aq->aura.bp_ena = 0;
1132 /* If NIX1 LF is attached then specify NIX1_RX.
1133 *
1134 * Below NPA_AURA_S[BP_ENA] is set according to the
1135 * NPA_BPINTF_E enumeration given as:
1136 * 0x0 + a*0x1 where 'a' is 0 for NIX0_RX and 1 for NIX1_RX so
1137 * NIX0_RX is 0x0 + 0*0x1 = 0
1138 * NIX1_RX is 0x0 + 1*0x1 = 1
1139 * But in HRM it is given that
1140 * "NPA_AURA_S[BP_ENA](w1[33:32]) - Enable aura backpressure to
1141 * NIX-RX based on [BP] level. One bit per NIX-RX; index
1142 * enumerated by NPA_BPINTF_E."
1143 */
1144 if (pfvf->nix_blkaddr == BLKADDR_NIX1)
1145 aq->aura.bp_ena = 1;
1146 aq->aura.nix0_bpid = pfvf->bpid[0];
1147
1148 /* Set backpressure level for RQ's Aura */
1149 aq->aura.bp = RQ_BP_LVL_AURA;
1150 }
1151
1152 /* Fill AQ info */
1153 aq->ctype = NPA_AQ_CTYPE_AURA;
1154 aq->op = NPA_AQ_INSTOP_INIT;
1155
1156 return 0;
1157 }
1158
otx2_pool_init(struct otx2_nic * pfvf,u16 pool_id,int stack_pages,int numptrs,int buf_size)1159 static int otx2_pool_init(struct otx2_nic *pfvf, u16 pool_id,
1160 int stack_pages, int numptrs, int buf_size)
1161 {
1162 struct npa_aq_enq_req *aq;
1163 struct otx2_pool *pool;
1164 int err;
1165
1166 pool = &pfvf->qset.pool[pool_id];
1167 /* Alloc memory for stack which is used to store buffer pointers */
1168 err = qmem_alloc(pfvf->dev, &pool->stack,
1169 stack_pages, pfvf->hw.stack_pg_bytes);
1170 if (err)
1171 return err;
1172
1173 pool->rbsize = buf_size;
1174
1175 /* Initialize this pool's context via AF */
1176 aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1177 if (!aq) {
1178 /* Shared mbox memory buffer is full, flush it and retry */
1179 err = otx2_sync_mbox_msg(&pfvf->mbox);
1180 if (err) {
1181 qmem_free(pfvf->dev, pool->stack);
1182 return err;
1183 }
1184 aq = otx2_mbox_alloc_msg_npa_aq_enq(&pfvf->mbox);
1185 if (!aq) {
1186 qmem_free(pfvf->dev, pool->stack);
1187 return -ENOMEM;
1188 }
1189 }
1190
1191 aq->aura_id = pool_id;
1192 aq->pool.stack_base = pool->stack->iova;
1193 aq->pool.stack_caching = 1;
1194 aq->pool.ena = 1;
1195 aq->pool.buf_size = buf_size / 128;
1196 aq->pool.stack_max_pages = stack_pages;
1197 aq->pool.shift = ilog2(numptrs) - 8;
1198 aq->pool.ptr_start = 0;
1199 aq->pool.ptr_end = ~0ULL;
1200
1201 /* Fill AQ info */
1202 aq->ctype = NPA_AQ_CTYPE_POOL;
1203 aq->op = NPA_AQ_INSTOP_INIT;
1204
1205 return 0;
1206 }
1207
otx2_sq_aura_pool_init(struct otx2_nic * pfvf)1208 int otx2_sq_aura_pool_init(struct otx2_nic *pfvf)
1209 {
1210 int qidx, pool_id, stack_pages, num_sqbs;
1211 struct otx2_qset *qset = &pfvf->qset;
1212 struct otx2_hw *hw = &pfvf->hw;
1213 struct otx2_snd_queue *sq;
1214 struct otx2_pool *pool;
1215 int err, ptr;
1216 s64 bufptr;
1217
1218 /* Calculate number of SQBs needed.
1219 *
1220 * For a 128byte SQE, and 4K size SQB, 31 SQEs will fit in one SQB.
1221 * Last SQE is used for pointing to next SQB.
1222 */
1223 num_sqbs = (hw->sqb_size / 128) - 1;
1224 num_sqbs = (qset->sqe_cnt + num_sqbs) / num_sqbs;
1225
1226 /* Get no of stack pages needed */
1227 stack_pages =
1228 (num_sqbs + hw->stack_pg_ptrs - 1) / hw->stack_pg_ptrs;
1229
1230 for (qidx = 0; qidx < hw->tx_queues; qidx++) {
1231 pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1232 /* Initialize aura context */
1233 err = otx2_aura_init(pfvf, pool_id, pool_id, num_sqbs);
1234 if (err)
1235 goto fail;
1236
1237 /* Initialize pool context */
1238 err = otx2_pool_init(pfvf, pool_id, stack_pages,
1239 num_sqbs, hw->sqb_size);
1240 if (err)
1241 goto fail;
1242 }
1243
1244 /* Flush accumulated messages */
1245 err = otx2_sync_mbox_msg(&pfvf->mbox);
1246 if (err)
1247 goto fail;
1248
1249 /* Allocate pointers and free them to aura/pool */
1250 for (qidx = 0; qidx < hw->tx_queues; qidx++) {
1251 pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_SQ, qidx);
1252 pool = &pfvf->qset.pool[pool_id];
1253
1254 sq = &qset->sq[qidx];
1255 sq->sqb_count = 0;
1256 sq->sqb_ptrs = kcalloc(num_sqbs, sizeof(u64 *), GFP_KERNEL);
1257 if (!sq->sqb_ptrs)
1258 return -ENOMEM;
1259
1260 for (ptr = 0; ptr < num_sqbs; ptr++) {
1261 bufptr = otx2_alloc_rbuf(pfvf, pool);
1262 if (bufptr <= 0)
1263 return bufptr;
1264 otx2_aura_freeptr(pfvf, pool_id, bufptr);
1265 sq->sqb_ptrs[sq->sqb_count++] = (u64)bufptr;
1266 }
1267 }
1268
1269 return 0;
1270 fail:
1271 otx2_mbox_reset(&pfvf->mbox.mbox, 0);
1272 otx2_aura_pool_free(pfvf);
1273 return err;
1274 }
1275
otx2_rq_aura_pool_init(struct otx2_nic * pfvf)1276 int otx2_rq_aura_pool_init(struct otx2_nic *pfvf)
1277 {
1278 struct otx2_hw *hw = &pfvf->hw;
1279 int stack_pages, pool_id, rq;
1280 struct otx2_pool *pool;
1281 int err, ptr, num_ptrs;
1282 s64 bufptr;
1283
1284 num_ptrs = pfvf->qset.rqe_cnt;
1285
1286 stack_pages =
1287 (num_ptrs + hw->stack_pg_ptrs - 1) / hw->stack_pg_ptrs;
1288
1289 for (rq = 0; rq < hw->rx_queues; rq++) {
1290 pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_RQ, rq);
1291 /* Initialize aura context */
1292 err = otx2_aura_init(pfvf, pool_id, pool_id, num_ptrs);
1293 if (err)
1294 goto fail;
1295 }
1296 for (pool_id = 0; pool_id < hw->rqpool_cnt; pool_id++) {
1297 err = otx2_pool_init(pfvf, pool_id, stack_pages,
1298 num_ptrs, pfvf->rbsize);
1299 if (err)
1300 goto fail;
1301 }
1302
1303 /* Flush accumulated messages */
1304 err = otx2_sync_mbox_msg(&pfvf->mbox);
1305 if (err)
1306 goto fail;
1307
1308 /* Allocate pointers and free them to aura/pool */
1309 for (pool_id = 0; pool_id < hw->rqpool_cnt; pool_id++) {
1310 pool = &pfvf->qset.pool[pool_id];
1311 for (ptr = 0; ptr < num_ptrs; ptr++) {
1312 bufptr = otx2_alloc_rbuf(pfvf, pool);
1313 if (bufptr <= 0)
1314 return bufptr;
1315 otx2_aura_freeptr(pfvf, pool_id,
1316 bufptr + OTX2_HEAD_ROOM);
1317 }
1318 }
1319
1320 return 0;
1321 fail:
1322 otx2_mbox_reset(&pfvf->mbox.mbox, 0);
1323 otx2_aura_pool_free(pfvf);
1324 return err;
1325 }
1326
otx2_config_npa(struct otx2_nic * pfvf)1327 int otx2_config_npa(struct otx2_nic *pfvf)
1328 {
1329 struct otx2_qset *qset = &pfvf->qset;
1330 struct npa_lf_alloc_req *npalf;
1331 struct otx2_hw *hw = &pfvf->hw;
1332 int aura_cnt;
1333
1334 /* Pool - Stack of free buffer pointers
1335 * Aura - Alloc/frees pointers from/to pool for NIX DMA.
1336 */
1337
1338 if (!hw->pool_cnt)
1339 return -EINVAL;
1340
1341 qset->pool = devm_kcalloc(pfvf->dev, hw->pool_cnt,
1342 sizeof(struct otx2_pool), GFP_KERNEL);
1343 if (!qset->pool)
1344 return -ENOMEM;
1345
1346 /* Get memory to put this msg */
1347 npalf = otx2_mbox_alloc_msg_npa_lf_alloc(&pfvf->mbox);
1348 if (!npalf)
1349 return -ENOMEM;
1350
1351 /* Set aura and pool counts */
1352 npalf->nr_pools = hw->pool_cnt;
1353 aura_cnt = ilog2(roundup_pow_of_two(hw->pool_cnt));
1354 npalf->aura_sz = (aura_cnt >= ilog2(128)) ? (aura_cnt - 6) : 1;
1355
1356 return otx2_sync_mbox_msg(&pfvf->mbox);
1357 }
1358
otx2_detach_resources(struct mbox * mbox)1359 int otx2_detach_resources(struct mbox *mbox)
1360 {
1361 struct rsrc_detach *detach;
1362
1363 mutex_lock(&mbox->lock);
1364 detach = otx2_mbox_alloc_msg_detach_resources(mbox);
1365 if (!detach) {
1366 mutex_unlock(&mbox->lock);
1367 return -ENOMEM;
1368 }
1369
1370 /* detach all */
1371 detach->partial = false;
1372
1373 /* Send detach request to AF */
1374 otx2_mbox_msg_send(&mbox->mbox, 0);
1375 mutex_unlock(&mbox->lock);
1376 return 0;
1377 }
1378 EXPORT_SYMBOL(otx2_detach_resources);
1379
otx2_attach_npa_nix(struct otx2_nic * pfvf)1380 int otx2_attach_npa_nix(struct otx2_nic *pfvf)
1381 {
1382 struct rsrc_attach *attach;
1383 struct msg_req *msix;
1384 int err;
1385
1386 mutex_lock(&pfvf->mbox.lock);
1387 /* Get memory to put this msg */
1388 attach = otx2_mbox_alloc_msg_attach_resources(&pfvf->mbox);
1389 if (!attach) {
1390 mutex_unlock(&pfvf->mbox.lock);
1391 return -ENOMEM;
1392 }
1393
1394 attach->npalf = true;
1395 attach->nixlf = true;
1396
1397 /* Send attach request to AF */
1398 err = otx2_sync_mbox_msg(&pfvf->mbox);
1399 if (err) {
1400 mutex_unlock(&pfvf->mbox.lock);
1401 return err;
1402 }
1403
1404 pfvf->nix_blkaddr = BLKADDR_NIX0;
1405
1406 /* If the platform has two NIX blocks then LF may be
1407 * allocated from NIX1.
1408 */
1409 if (otx2_read64(pfvf, RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_NIX1)) & 0x1FFULL)
1410 pfvf->nix_blkaddr = BLKADDR_NIX1;
1411
1412 /* Get NPA and NIX MSIX vector offsets */
1413 msix = otx2_mbox_alloc_msg_msix_offset(&pfvf->mbox);
1414 if (!msix) {
1415 mutex_unlock(&pfvf->mbox.lock);
1416 return -ENOMEM;
1417 }
1418
1419 err = otx2_sync_mbox_msg(&pfvf->mbox);
1420 if (err) {
1421 mutex_unlock(&pfvf->mbox.lock);
1422 return err;
1423 }
1424 mutex_unlock(&pfvf->mbox.lock);
1425
1426 if (pfvf->hw.npa_msixoff == MSIX_VECTOR_INVALID ||
1427 pfvf->hw.nix_msixoff == MSIX_VECTOR_INVALID) {
1428 dev_err(pfvf->dev,
1429 "RVUPF: Invalid MSIX vector offset for NPA/NIX\n");
1430 return -EINVAL;
1431 }
1432
1433 return 0;
1434 }
1435 EXPORT_SYMBOL(otx2_attach_npa_nix);
1436
otx2_ctx_disable(struct mbox * mbox,int type,bool npa)1437 void otx2_ctx_disable(struct mbox *mbox, int type, bool npa)
1438 {
1439 struct hwctx_disable_req *req;
1440
1441 mutex_lock(&mbox->lock);
1442 /* Request AQ to disable this context */
1443 if (npa)
1444 req = otx2_mbox_alloc_msg_npa_hwctx_disable(mbox);
1445 else
1446 req = otx2_mbox_alloc_msg_nix_hwctx_disable(mbox);
1447
1448 if (!req) {
1449 mutex_unlock(&mbox->lock);
1450 return;
1451 }
1452
1453 req->ctype = type;
1454
1455 if (otx2_sync_mbox_msg(mbox))
1456 dev_err(mbox->pfvf->dev, "%s failed to disable context\n",
1457 __func__);
1458
1459 mutex_unlock(&mbox->lock);
1460 }
1461
otx2_nix_config_bp(struct otx2_nic * pfvf,bool enable)1462 int otx2_nix_config_bp(struct otx2_nic *pfvf, bool enable)
1463 {
1464 struct nix_bp_cfg_req *req;
1465
1466 if (enable)
1467 req = otx2_mbox_alloc_msg_nix_bp_enable(&pfvf->mbox);
1468 else
1469 req = otx2_mbox_alloc_msg_nix_bp_disable(&pfvf->mbox);
1470
1471 if (!req)
1472 return -ENOMEM;
1473
1474 req->chan_base = 0;
1475 req->chan_cnt = 1;
1476 req->bpid_per_chan = 0;
1477
1478 return otx2_sync_mbox_msg(&pfvf->mbox);
1479 }
1480
1481 /* Mbox message handlers */
mbox_handler_cgx_stats(struct otx2_nic * pfvf,struct cgx_stats_rsp * rsp)1482 void mbox_handler_cgx_stats(struct otx2_nic *pfvf,
1483 struct cgx_stats_rsp *rsp)
1484 {
1485 int id;
1486
1487 for (id = 0; id < CGX_RX_STATS_COUNT; id++)
1488 pfvf->hw.cgx_rx_stats[id] = rsp->rx_stats[id];
1489 for (id = 0; id < CGX_TX_STATS_COUNT; id++)
1490 pfvf->hw.cgx_tx_stats[id] = rsp->tx_stats[id];
1491 }
1492
mbox_handler_nix_txsch_alloc(struct otx2_nic * pf,struct nix_txsch_alloc_rsp * rsp)1493 void mbox_handler_nix_txsch_alloc(struct otx2_nic *pf,
1494 struct nix_txsch_alloc_rsp *rsp)
1495 {
1496 int lvl, schq;
1497
1498 /* Setup transmit scheduler list */
1499 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++)
1500 for (schq = 0; schq < rsp->schq[lvl]; schq++)
1501 pf->hw.txschq_list[lvl][schq] =
1502 rsp->schq_list[lvl][schq];
1503 }
1504 EXPORT_SYMBOL(mbox_handler_nix_txsch_alloc);
1505
mbox_handler_npa_lf_alloc(struct otx2_nic * pfvf,struct npa_lf_alloc_rsp * rsp)1506 void mbox_handler_npa_lf_alloc(struct otx2_nic *pfvf,
1507 struct npa_lf_alloc_rsp *rsp)
1508 {
1509 pfvf->hw.stack_pg_ptrs = rsp->stack_pg_ptrs;
1510 pfvf->hw.stack_pg_bytes = rsp->stack_pg_bytes;
1511 }
1512 EXPORT_SYMBOL(mbox_handler_npa_lf_alloc);
1513
mbox_handler_nix_lf_alloc(struct otx2_nic * pfvf,struct nix_lf_alloc_rsp * rsp)1514 void mbox_handler_nix_lf_alloc(struct otx2_nic *pfvf,
1515 struct nix_lf_alloc_rsp *rsp)
1516 {
1517 pfvf->hw.sqb_size = rsp->sqb_size;
1518 pfvf->hw.rx_chan_base = rsp->rx_chan_base;
1519 pfvf->hw.tx_chan_base = rsp->tx_chan_base;
1520 pfvf->hw.lso_tsov4_idx = rsp->lso_tsov4_idx;
1521 pfvf->hw.lso_tsov6_idx = rsp->lso_tsov6_idx;
1522 }
1523 EXPORT_SYMBOL(mbox_handler_nix_lf_alloc);
1524
mbox_handler_msix_offset(struct otx2_nic * pfvf,struct msix_offset_rsp * rsp)1525 void mbox_handler_msix_offset(struct otx2_nic *pfvf,
1526 struct msix_offset_rsp *rsp)
1527 {
1528 pfvf->hw.npa_msixoff = rsp->npa_msixoff;
1529 pfvf->hw.nix_msixoff = rsp->nix_msixoff;
1530 }
1531 EXPORT_SYMBOL(mbox_handler_msix_offset);
1532
mbox_handler_nix_bp_enable(struct otx2_nic * pfvf,struct nix_bp_cfg_rsp * rsp)1533 void mbox_handler_nix_bp_enable(struct otx2_nic *pfvf,
1534 struct nix_bp_cfg_rsp *rsp)
1535 {
1536 int chan, chan_id;
1537
1538 for (chan = 0; chan < rsp->chan_cnt; chan++) {
1539 chan_id = ((rsp->chan_bpid[chan] >> 10) & 0x7F);
1540 pfvf->bpid[chan_id] = rsp->chan_bpid[chan] & 0x3FF;
1541 }
1542 }
1543 EXPORT_SYMBOL(mbox_handler_nix_bp_enable);
1544
otx2_free_cints(struct otx2_nic * pfvf,int n)1545 void otx2_free_cints(struct otx2_nic *pfvf, int n)
1546 {
1547 struct otx2_qset *qset = &pfvf->qset;
1548 struct otx2_hw *hw = &pfvf->hw;
1549 int irq, qidx;
1550
1551 for (qidx = 0, irq = hw->nix_msixoff + NIX_LF_CINT_VEC_START;
1552 qidx < n;
1553 qidx++, irq++) {
1554 int vector = pci_irq_vector(pfvf->pdev, irq);
1555
1556 irq_set_affinity_hint(vector, NULL);
1557 free_cpumask_var(hw->affinity_mask[irq]);
1558 free_irq(vector, &qset->napi[qidx]);
1559 }
1560 }
1561
otx2_set_cints_affinity(struct otx2_nic * pfvf)1562 void otx2_set_cints_affinity(struct otx2_nic *pfvf)
1563 {
1564 struct otx2_hw *hw = &pfvf->hw;
1565 int vec, cpu, irq, cint;
1566
1567 vec = hw->nix_msixoff + NIX_LF_CINT_VEC_START;
1568 cpu = cpumask_first(cpu_online_mask);
1569
1570 /* CQ interrupts */
1571 for (cint = 0; cint < pfvf->hw.cint_cnt; cint++, vec++) {
1572 if (!alloc_cpumask_var(&hw->affinity_mask[vec], GFP_KERNEL))
1573 return;
1574
1575 cpumask_set_cpu(cpu, hw->affinity_mask[vec]);
1576
1577 irq = pci_irq_vector(pfvf->pdev, vec);
1578 irq_set_affinity_hint(irq, hw->affinity_mask[vec]);
1579
1580 cpu = cpumask_next(cpu, cpu_online_mask);
1581 if (unlikely(cpu >= nr_cpu_ids))
1582 cpu = 0;
1583 }
1584 }
1585
1586 #define M(_name, _id, _fn_name, _req_type, _rsp_type) \
1587 int __weak \
1588 otx2_mbox_up_handler_ ## _fn_name(struct otx2_nic *pfvf, \
1589 struct _req_type *req, \
1590 struct _rsp_type *rsp) \
1591 { \
1592 /* Nothing to do here */ \
1593 return 0; \
1594 } \
1595 EXPORT_SYMBOL(otx2_mbox_up_handler_ ## _fn_name);
1596 MBOX_UP_CGX_MESSAGES
1597 #undef M
1598