]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.h
f9149efc8b031daa59fa01d1910cbe18f50ba885
[linux.git] / drivers / net / ethernet / marvell / octeontx2 / nic / otx2_common.h
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 #ifndef OTX2_COMMON_H
12 #define OTX2_COMMON_H
13
14 #include <linux/pci.h>
15 #include <linux/iommu.h>
16
17 #include <mbox.h>
18 #include "otx2_reg.h"
19 #include "otx2_txrx.h"
20
21 /* PCI device IDs */
22 #define PCI_DEVID_OCTEONTX2_RVU_PF              0xA063
23
24 #define PCI_SUBSYS_DEVID_96XX_RVU_PFVF          0xB200
25
26 /* PCI BAR nos */
27 #define PCI_CFG_REG_BAR_NUM                     2
28 #define PCI_MBOX_BAR_NUM                        4
29
30 #define NAME_SIZE                               32
31
32 enum arua_mapped_qtypes {
33         AURA_NIX_RQ,
34         AURA_NIX_SQ,
35 };
36
37 /* NIX LF interrupts range*/
38 #define NIX_LF_QINT_VEC_START                   0x00
39 #define NIX_LF_CINT_VEC_START                   0x40
40 #define NIX_LF_GINT_VEC                         0x80
41 #define NIX_LF_ERR_VEC                          0x81
42 #define NIX_LF_POISON_VEC                       0x82
43
44 /* RSS configuration */
45 struct otx2_rss_info {
46         u8 enable;
47         u32 flowkey_cfg;
48         u16 rss_size;
49         u8  ind_tbl[MAX_RSS_INDIR_TBL_SIZE];
50 #define RSS_HASH_KEY_SIZE       44   /* 352 bit key */
51         u8  key[RSS_HASH_KEY_SIZE];
52 };
53
54 /* NIX (or NPC) RX errors */
55 enum otx2_errlvl {
56         NPC_ERRLVL_RE,
57         NPC_ERRLVL_LID_LA,
58         NPC_ERRLVL_LID_LB,
59         NPC_ERRLVL_LID_LC,
60         NPC_ERRLVL_LID_LD,
61         NPC_ERRLVL_LID_LE,
62         NPC_ERRLVL_LID_LF,
63         NPC_ERRLVL_LID_LG,
64         NPC_ERRLVL_LID_LH,
65         NPC_ERRLVL_NIX = 0x0F,
66 };
67
68 enum otx2_errcodes_re {
69         /* NPC_ERRLVL_RE errcodes */
70         ERRCODE_FCS = 0x7,
71         ERRCODE_FCS_RCV = 0x8,
72         ERRCODE_UNDERSIZE = 0x10,
73         ERRCODE_OVERSIZE = 0x11,
74         ERRCODE_OL2_LEN_MISMATCH = 0x12,
75         /* NPC_ERRLVL_NIX errcodes */
76         ERRCODE_OL3_LEN = 0x10,
77         ERRCODE_OL4_LEN = 0x11,
78         ERRCODE_OL4_CSUM = 0x12,
79         ERRCODE_IL3_LEN = 0x20,
80         ERRCODE_IL4_LEN = 0x21,
81         ERRCODE_IL4_CSUM = 0x22,
82 };
83
84 /* Driver counted stats */
85 struct otx2_drv_stats {
86         atomic_t rx_fcs_errs;
87         atomic_t rx_oversize_errs;
88         atomic_t rx_undersize_errs;
89         atomic_t rx_csum_errs;
90         atomic_t rx_len_errs;
91         atomic_t rx_other_errs;
92 };
93
94 struct mbox {
95         struct otx2_mbox        mbox;
96         struct work_struct      mbox_wrk;
97         struct otx2_mbox        mbox_up;
98         struct work_struct      mbox_up_wrk;
99         struct otx2_nic         *pfvf;
100         void                    *bbuf_base; /* Bounce buffer for mbox memory */
101         struct mutex            lock;   /* serialize mailbox access */
102         int                     num_msgs; /* mbox number of messages */
103         int                     up_num_msgs; /* mbox_up number of messages */
104 };
105
106 struct otx2_hw {
107         struct pci_dev          *pdev;
108         struct otx2_rss_info    rss_info;
109         u16                     rx_queues;
110         u16                     tx_queues;
111         u16                     max_queues;
112         u16                     pool_cnt;
113         u16                     rqpool_cnt;
114         u16                     sqpool_cnt;
115
116         /* NPA */
117         u32                     stack_pg_ptrs;  /* No of ptrs per stack page */
118         u32                     stack_pg_bytes; /* Size of stack page */
119         u16                     sqb_size;
120
121         /* NIX */
122         u16             txschq_list[NIX_TXSCH_LVL_CNT][MAX_TXSCHQ_PER_FUNC];
123
124         /* HW settings, coalescing etc */
125         u16                     rx_chan_base;
126         u16                     tx_chan_base;
127         u16                     cq_qcount_wait;
128         u16                     cq_ecount_wait;
129         u16                     rq_skid;
130         u8                      cq_time_wait;
131
132         /* MSI-X */
133         u8                      cint_cnt; /* CQ interrupt count */
134         u16                     npa_msixoff; /* Offset of NPA vectors */
135         u16                     nix_msixoff; /* Offset of NIX vectors */
136         char                    *irq_name;
137         cpumask_var_t           *affinity_mask;
138
139         /* Stats */
140         struct otx2_drv_stats   drv_stats;
141 };
142
143 struct refill_work {
144         struct delayed_work pool_refill_work;
145         struct otx2_nic *pf;
146 };
147
148 struct otx2_nic {
149         void __iomem            *reg_base;
150         struct net_device       *netdev;
151         void                    *iommu_domain;
152         u16                     max_frs;
153         u16                     rbsize; /* Receive buffer size */
154
155 #define OTX2_FLAG_INTF_DOWN                     BIT_ULL(2)
156         u64                     flags;
157
158         struct otx2_qset        qset;
159         struct otx2_hw          hw;
160         struct pci_dev          *pdev;
161         struct device           *dev;
162
163         /* Mbox */
164         struct mbox             mbox;
165         struct workqueue_struct *mbox_wq;
166
167         u16                     pcifunc; /* RVU PF_FUNC */
168         struct cgx_link_user_info linfo;
169
170         u64                     reset_count;
171         struct work_struct      reset_task;
172         struct refill_work      *refill_wrk;
173
174         /* Block address of NIX either BLKADDR_NIX0 or BLKADDR_NIX1 */
175         int                     nix_blkaddr;
176 };
177
178 static inline bool is_96xx_A0(struct pci_dev *pdev)
179 {
180         return (pdev->revision == 0x00) &&
181                 (pdev->subsystem_device == PCI_SUBSYS_DEVID_96XX_RVU_PFVF);
182 }
183
184 static inline bool is_96xx_B0(struct pci_dev *pdev)
185 {
186         return (pdev->revision == 0x01) &&
187                 (pdev->subsystem_device == PCI_SUBSYS_DEVID_96XX_RVU_PFVF);
188 }
189
190 static inline void otx2_setup_dev_hw_settings(struct otx2_nic *pfvf)
191 {
192         pfvf->hw.cq_time_wait = CQ_TIMER_THRESH_DEFAULT;
193         pfvf->hw.cq_ecount_wait = CQ_CQE_THRESH_DEFAULT;
194         pfvf->hw.cq_qcount_wait = CQ_QCOUNT_DEFAULT;
195
196         if (is_96xx_A0(pfvf->pdev)) {
197                 /* Time based irq coalescing is not supported */
198                 pfvf->hw.cq_qcount_wait = 0x0;
199
200                 /* Due to HW issue previous silicons required minimum
201                  * 600 unused CQE to avoid CQ overflow.
202                  */
203                 pfvf->hw.rq_skid = 600;
204                 pfvf->qset.rqe_cnt = Q_COUNT(Q_SIZE_1K);
205         }
206 }
207
208 /* Register read/write APIs */
209 static inline void __iomem *otx2_get_regaddr(struct otx2_nic *nic, u64 offset)
210 {
211         u64 blkaddr;
212
213         switch ((offset >> RVU_FUNC_BLKADDR_SHIFT) & RVU_FUNC_BLKADDR_MASK) {
214         case BLKTYPE_NIX:
215                 blkaddr = nic->nix_blkaddr;
216                 break;
217         case BLKTYPE_NPA:
218                 blkaddr = BLKADDR_NPA;
219                 break;
220         default:
221                 blkaddr = BLKADDR_RVUM;
222                 break;
223         };
224
225         offset &= ~(RVU_FUNC_BLKADDR_MASK << RVU_FUNC_BLKADDR_SHIFT);
226         offset |= (blkaddr << RVU_FUNC_BLKADDR_SHIFT);
227
228         return nic->reg_base + offset;
229 }
230
231 static inline void otx2_write64(struct otx2_nic *nic, u64 offset, u64 val)
232 {
233         void __iomem *addr = otx2_get_regaddr(nic, offset);
234
235         writeq(val, addr);
236 }
237
238 static inline u64 otx2_read64(struct otx2_nic *nic, u64 offset)
239 {
240         void __iomem *addr = otx2_get_regaddr(nic, offset);
241
242         return readq(addr);
243 }
244
245 /* Mbox bounce buffer APIs */
246 static inline int otx2_mbox_bbuf_init(struct mbox *mbox, struct pci_dev *pdev)
247 {
248         struct otx2_mbox *otx2_mbox;
249         struct otx2_mbox_dev *mdev;
250
251         mbox->bbuf_base = devm_kmalloc(&pdev->dev, MBOX_SIZE, GFP_KERNEL);
252         if (!mbox->bbuf_base)
253                 return -ENOMEM;
254
255         /* Overwrite mbox mbase to point to bounce buffer, so that PF/VF
256          * prepare all mbox messages in bounce buffer instead of directly
257          * in hw mbox memory.
258          */
259         otx2_mbox = &mbox->mbox;
260         mdev = &otx2_mbox->dev[0];
261         mdev->mbase = mbox->bbuf_base;
262
263         otx2_mbox = &mbox->mbox_up;
264         mdev = &otx2_mbox->dev[0];
265         mdev->mbase = mbox->bbuf_base;
266         return 0;
267 }
268
269 static inline void otx2_sync_mbox_bbuf(struct otx2_mbox *mbox, int devid)
270 {
271         u16 msgs_offset = ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
272         void *hw_mbase = mbox->hwbase + (devid * MBOX_SIZE);
273         struct otx2_mbox_dev *mdev = &mbox->dev[devid];
274         struct mbox_hdr *hdr;
275         u64 msg_size;
276
277         if (mdev->mbase == hw_mbase)
278                 return;
279
280         hdr = hw_mbase + mbox->rx_start;
281         msg_size = hdr->msg_size;
282
283         if (msg_size > mbox->rx_size - msgs_offset)
284                 msg_size = mbox->rx_size - msgs_offset;
285
286         /* Copy mbox messages from mbox memory to bounce buffer */
287         memcpy(mdev->mbase + mbox->rx_start,
288                hw_mbase + mbox->rx_start, msg_size + msgs_offset);
289 }
290
291 static inline void otx2_mbox_lock_init(struct mbox *mbox)
292 {
293         mutex_init(&mbox->lock);
294 }
295
296 static inline void otx2_mbox_lock(struct mbox *mbox)
297 {
298         mutex_lock(&mbox->lock);
299 }
300
301 static inline void otx2_mbox_unlock(struct mbox *mbox)
302 {
303         mutex_unlock(&mbox->lock);
304 }
305
306 /* With the absence of API for 128-bit IO memory access for arm64,
307  * implement required operations at place.
308  */
309 #if defined(CONFIG_ARM64)
310 static inline void otx2_write128(u64 lo, u64 hi, void __iomem *addr)
311 {
312         __asm__ volatile("stp %x[x0], %x[x1], [%x[p1],#0]!"
313                          ::[x0]"r"(lo), [x1]"r"(hi), [p1]"r"(addr));
314 }
315
316 static inline u64 otx2_atomic64_add(u64 incr, u64 *ptr)
317 {
318         u64 result;
319
320         __asm__ volatile(".cpu   generic+lse\n"
321                          "ldadd %x[i], %x[r], [%[b]]"
322                          : [r]"=r"(result), "+m"(*ptr)
323                          : [i]"r"(incr), [b]"r"(ptr)
324                          : "memory");
325         return result;
326 }
327
328 static inline u64 otx2_lmt_flush(uint64_t addr)
329 {
330         u64 result = 0;
331
332         __asm__ volatile(".cpu  generic+lse\n"
333                          "ldeor xzr,%x[rf],[%[rs]]"
334                          : [rf]"=r"(result)
335                          : [rs]"r"(addr));
336         return result;
337 }
338
339 #else
340 #define otx2_write128(lo, hi, addr)
341 #define otx2_atomic64_add(incr, ptr)            ({ *ptr += incr; })
342 #define otx2_lmt_flush(addr)                    ({ 0; })
343 #endif
344
345 /* Alloc pointer from pool/aura */
346 static inline u64 otx2_aura_allocptr(struct otx2_nic *pfvf, int aura)
347 {
348         u64 *ptr = (u64 *)otx2_get_regaddr(pfvf,
349                            NPA_LF_AURA_OP_ALLOCX(0));
350         u64 incr = (u64)aura | BIT_ULL(63);
351
352         return otx2_atomic64_add(incr, ptr);
353 }
354
355 /* Free pointer to a pool/aura */
356 static inline void otx2_aura_freeptr(struct otx2_nic *pfvf,
357                                      int aura, s64 buf)
358 {
359         otx2_write128((u64)buf, (u64)aura | BIT_ULL(63),
360                       otx2_get_regaddr(pfvf, NPA_LF_AURA_OP_FREE0));
361 }
362
363 /* Update page ref count */
364 static inline void otx2_get_page(struct otx2_pool *pool)
365 {
366         if (!pool->page)
367                 return;
368
369         if (pool->pageref)
370                 page_ref_add(pool->page, pool->pageref);
371         pool->pageref = 0;
372         pool->page = NULL;
373 }
374
375 static inline int otx2_get_pool_idx(struct otx2_nic *pfvf, int type, int idx)
376 {
377         if (type == AURA_NIX_SQ)
378                 return pfvf->hw.rqpool_cnt + idx;
379
380          /* AURA_NIX_RQ */
381         return idx;
382 }
383
384 /* Mbox APIs */
385 static inline int otx2_sync_mbox_msg(struct mbox *mbox)
386 {
387         int err;
388
389         if (!otx2_mbox_nonempty(&mbox->mbox, 0))
390                 return 0;
391         otx2_mbox_msg_send(&mbox->mbox, 0);
392         err = otx2_mbox_wait_for_rsp(&mbox->mbox, 0);
393         if (err)
394                 return err;
395
396         return otx2_mbox_check_rsp_msgs(&mbox->mbox, 0);
397 }
398
399 static inline int otx2_sync_mbox_up_msg(struct mbox *mbox, int devid)
400 {
401         int err;
402
403         if (!otx2_mbox_nonempty(&mbox->mbox_up, devid))
404                 return 0;
405         otx2_mbox_msg_send(&mbox->mbox_up, devid);
406         err = otx2_mbox_wait_for_rsp(&mbox->mbox_up, devid);
407         if (err)
408                 return err;
409
410         return otx2_mbox_check_rsp_msgs(&mbox->mbox_up, devid);
411 }
412
413 /* Use this API to send mbox msgs in atomic context
414  * where sleeping is not allowed
415  */
416 static inline int otx2_sync_mbox_msg_busy_poll(struct mbox *mbox)
417 {
418         int err;
419
420         if (!otx2_mbox_nonempty(&mbox->mbox, 0))
421                 return 0;
422         otx2_mbox_msg_send(&mbox->mbox, 0);
423         err = otx2_mbox_busy_poll_for_rsp(&mbox->mbox, 0);
424         if (err)
425                 return err;
426
427         return otx2_mbox_check_rsp_msgs(&mbox->mbox, 0);
428 }
429
430 #define M(_name, _id, _fn_name, _req_type, _rsp_type)                   \
431 static struct _req_type __maybe_unused                                  \
432 *otx2_mbox_alloc_msg_ ## _fn_name(struct mbox *mbox)                    \
433 {                                                                       \
434         struct _req_type *req;                                          \
435                                                                         \
436         req = (struct _req_type *)otx2_mbox_alloc_msg_rsp(              \
437                 &mbox->mbox, 0, sizeof(struct _req_type),               \
438                 sizeof(struct _rsp_type));                              \
439         if (!req)                                                       \
440                 return NULL;                                            \
441         req->hdr.sig = OTX2_MBOX_REQ_SIG;                               \
442         req->hdr.id = _id;                                              \
443         return req;                                                     \
444 }
445
446 MBOX_MESSAGES
447 #undef M
448
449 #define M(_name, _id, _fn_name, _req_type, _rsp_type)                   \
450 int                                                                     \
451 otx2_mbox_up_handler_ ## _fn_name(struct otx2_nic *pfvf,                \
452                                 struct _req_type *req,                  \
453                                 struct _rsp_type *rsp);                 \
454
455 MBOX_UP_CGX_MESSAGES
456 #undef M
457
458 /* Time to wait before watchdog kicks off */
459 #define OTX2_TX_TIMEOUT         (100 * HZ)
460
461 #define RVU_PFVF_PF_SHIFT       10
462 #define RVU_PFVF_PF_MASK        0x3F
463 #define RVU_PFVF_FUNC_SHIFT     0
464 #define RVU_PFVF_FUNC_MASK      0x3FF
465
466 static inline int rvu_get_pf(u16 pcifunc)
467 {
468         return (pcifunc >> RVU_PFVF_PF_SHIFT) & RVU_PFVF_PF_MASK;
469 }
470
471 static inline dma_addr_t otx2_dma_map_page(struct otx2_nic *pfvf,
472                                            struct page *page,
473                                            size_t offset, size_t size,
474                                            enum dma_data_direction dir)
475 {
476         dma_addr_t iova;
477
478         iova = dma_map_page_attrs(pfvf->dev, page,
479                                   offset, size, dir, DMA_ATTR_SKIP_CPU_SYNC);
480         if (unlikely(dma_mapping_error(pfvf->dev, iova)))
481                 return (dma_addr_t)NULL;
482         return iova;
483 }
484
485 static inline void otx2_dma_unmap_page(struct otx2_nic *pfvf,
486                                        dma_addr_t addr, size_t size,
487                                        enum dma_data_direction dir)
488 {
489         dma_unmap_page_attrs(pfvf->dev, addr, size,
490                              dir, DMA_ATTR_SKIP_CPU_SYNC);
491 }
492
493 /* MSI-X APIs */
494 void otx2_free_cints(struct otx2_nic *pfvf, int n);
495 void otx2_set_cints_affinity(struct otx2_nic *pfvf);
496 int otx2_set_mac_address(struct net_device *netdev, void *p);
497 int otx2_hw_set_mtu(struct otx2_nic *pfvf, int mtu);
498 void otx2_tx_timeout(struct net_device *netdev, unsigned int txq);
499 void otx2_get_mac_from_af(struct net_device *netdev);
500 void otx2_config_irq_coalescing(struct otx2_nic *pfvf, int qidx);
501
502 /* RVU block related APIs */
503 int otx2_attach_npa_nix(struct otx2_nic *pfvf);
504 int otx2_detach_resources(struct mbox *mbox);
505 int otx2_config_npa(struct otx2_nic *pfvf);
506 int otx2_sq_aura_pool_init(struct otx2_nic *pfvf);
507 int otx2_rq_aura_pool_init(struct otx2_nic *pfvf);
508 void otx2_aura_pool_free(struct otx2_nic *pfvf);
509 void otx2_free_aura_ptr(struct otx2_nic *pfvf, int type);
510 void otx2_sq_free_sqbs(struct otx2_nic *pfvf);
511 int otx2_config_nix(struct otx2_nic *pfvf);
512 int otx2_config_nix_queues(struct otx2_nic *pfvf);
513 int otx2_txschq_config(struct otx2_nic *pfvf, int lvl);
514 int otx2_txsch_alloc(struct otx2_nic *pfvf);
515 int otx2_txschq_stop(struct otx2_nic *pfvf);
516 void otx2_sqb_flush(struct otx2_nic *pfvf);
517 dma_addr_t otx2_alloc_rbuf(struct otx2_nic *pfvf, struct otx2_pool *pool,
518                            gfp_t gfp);
519 int otx2_rxtx_enable(struct otx2_nic *pfvf, bool enable);
520 void otx2_ctx_disable(struct mbox *mbox, int type, bool npa);
521 void otx2_cleanup_rx_cqes(struct otx2_nic *pfvf, struct otx2_cq_queue *cq);
522 void otx2_cleanup_tx_cqes(struct otx2_nic *pfvf, struct otx2_cq_queue *cq);
523
524 /* RSS configuration APIs*/
525 int otx2_rss_init(struct otx2_nic *pfvf);
526
527 /* Mbox handlers */
528 void mbox_handler_msix_offset(struct otx2_nic *pfvf,
529                               struct msix_offset_rsp *rsp);
530 void mbox_handler_npa_lf_alloc(struct otx2_nic *pfvf,
531                                struct npa_lf_alloc_rsp *rsp);
532 void mbox_handler_nix_lf_alloc(struct otx2_nic *pfvf,
533                                struct nix_lf_alloc_rsp *rsp);
534 void mbox_handler_nix_txsch_alloc(struct otx2_nic *pf,
535                                   struct nix_txsch_alloc_rsp *rsp);
536
537 int otx2_open(struct net_device *netdev);
538 int otx2_stop(struct net_device *netdev);
539 #endif /* OTX2_COMMON_H */