7ada7fa477505b38f8cd3c4bd05b6b336fd616bb
[dpdk.git] / drivers / bus / dpaa / base / fman / fman_hw.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright 2017 NXP
4  *
5  */
6
7 #include <sys/types.h>
8 #include <sys/ioctl.h>
9 #include <ifaddrs.h>
10 #include <fman.h>
11 /* This header declares things about Fman hardware itself (the format of status
12  * words and an inline implementation of CRC64). We include it only in order to
13  * instantiate the one global variable it depends on.
14  */
15 #include <fsl_fman.h>
16 #include <fsl_fman_crc64.h>
17 #include <fsl_bman.h>
18
19 #define FMAN_SP_EXT_BUF_MARG_START_SHIFT            16
20
21 /* Instantiate the global variable that the inline CRC64 implementation (in
22  * <fsl_fman.h>) depends on.
23  */
24 DECLARE_FMAN_CRC64_TABLE();
25
26 #define ETH_ADDR_TO_UINT64(eth_addr)                  \
27         (uint64_t)(((uint64_t)(eth_addr)[0] << 40) |   \
28         ((uint64_t)(eth_addr)[1] << 32) |   \
29         ((uint64_t)(eth_addr)[2] << 24) |   \
30         ((uint64_t)(eth_addr)[3] << 16) |   \
31         ((uint64_t)(eth_addr)[4] << 8) |    \
32         ((uint64_t)(eth_addr)[5]))
33
34 void
35 fman_if_set_mcast_filter_table(struct fman_if *p)
36 {
37         struct __fman_if *__if = container_of(p, struct __fman_if, __if);
38         void *hashtable_ctrl;
39         uint32_t i;
40
41         hashtable_ctrl = &((struct memac_regs *)__if->ccsr_map)->hashtable_ctrl;
42         for (i = 0; i < 64; i++)
43                 out_be32(hashtable_ctrl, i|HASH_CTRL_MCAST_EN);
44 }
45
46 void
47 fman_if_reset_mcast_filter_table(struct fman_if *p)
48 {
49         struct __fman_if *__if = container_of(p, struct __fman_if, __if);
50         void *hashtable_ctrl;
51         uint32_t i;
52
53         hashtable_ctrl = &((struct memac_regs *)__if->ccsr_map)->hashtable_ctrl;
54         for (i = 0; i < 64; i++)
55                 out_be32(hashtable_ctrl, i & ~HASH_CTRL_MCAST_EN);
56 }
57
58 static
59 uint32_t get_mac_hash_code(uint64_t eth_addr)
60 {
61         uint64_t        mask1, mask2;
62         uint32_t        xorVal = 0;
63         uint8_t         i, j;
64
65         for (i = 0; i < 6; i++) {
66                 mask1 = eth_addr & (uint64_t)0x01;
67                 eth_addr >>= 1;
68
69                 for (j = 0; j < 7; j++) {
70                         mask2 = eth_addr & (uint64_t)0x01;
71                         mask1 ^= mask2;
72                         eth_addr >>= 1;
73                 }
74
75                 xorVal |= (mask1 << (5 - i));
76         }
77
78         return xorVal;
79 }
80
81 int
82 fman_if_add_hash_mac_addr(struct fman_if *p, uint8_t *eth)
83 {
84         uint64_t eth_addr;
85         void *hashtable_ctrl;
86         uint32_t hash;
87
88         struct __fman_if *__if = container_of(p, struct __fman_if, __if);
89
90         eth_addr = ETH_ADDR_TO_UINT64(eth);
91
92         if (!(eth_addr & GROUP_ADDRESS))
93                 return -1;
94
95         hash = get_mac_hash_code(eth_addr) & HASH_CTRL_ADDR_MASK;
96         hash = hash | HASH_CTRL_MCAST_EN;
97
98         hashtable_ctrl = &((struct memac_regs *)__if->ccsr_map)->hashtable_ctrl;
99         out_be32(hashtable_ctrl, hash);
100
101         return 0;
102 }
103
104 int
105 fman_if_get_primary_mac_addr(struct fman_if *p, uint8_t *eth)
106 {
107         struct __fman_if *__if = container_of(p, struct __fman_if, __if);
108         void *mac_reg =
109                 &((struct memac_regs *)__if->ccsr_map)->mac_addr0.mac_addr_l;
110         u32 val = in_be32(mac_reg);
111
112         eth[0] = (val & 0x000000ff) >> 0;
113         eth[1] = (val & 0x0000ff00) >> 8;
114         eth[2] = (val & 0x00ff0000) >> 16;
115         eth[3] = (val & 0xff000000) >> 24;
116
117         mac_reg =  &((struct memac_regs *)__if->ccsr_map)->mac_addr0.mac_addr_u;
118         val = in_be32(mac_reg);
119
120         eth[4] = (val & 0x000000ff) >> 0;
121         eth[5] = (val & 0x0000ff00) >> 8;
122
123         return 0;
124 }
125
126 void
127 fman_if_clear_mac_addr(struct fman_if *p, uint8_t addr_num)
128 {
129         struct __fman_if *m = container_of(p, struct __fman_if, __if);
130         void *reg;
131
132         if (addr_num) {
133                 reg = &((struct memac_regs *)m->ccsr_map)->
134                                 mac_addr[addr_num-1].mac_addr_l;
135                 out_be32(reg, 0x0);
136                 reg = &((struct memac_regs *)m->ccsr_map)->
137                                         mac_addr[addr_num-1].mac_addr_u;
138                 out_be32(reg, 0x0);
139         } else {
140                 reg = &((struct memac_regs *)m->ccsr_map)->mac_addr0.mac_addr_l;
141                 out_be32(reg, 0x0);
142                 reg = &((struct memac_regs *)m->ccsr_map)->mac_addr0.mac_addr_u;
143                 out_be32(reg, 0x0);
144         }
145 }
146
147 int
148 fman_if_add_mac_addr(struct fman_if *p, uint8_t *eth, uint8_t addr_num)
149 {
150         struct __fman_if *m = container_of(p, struct __fman_if, __if);
151
152         void *reg;
153         u32 val;
154
155         memcpy(&m->__if.mac_addr, eth, ETHER_ADDR_LEN);
156
157         if (addr_num)
158                 reg = &((struct memac_regs *)m->ccsr_map)->
159                                         mac_addr[addr_num-1].mac_addr_l;
160         else
161                 reg = &((struct memac_regs *)m->ccsr_map)->mac_addr0.mac_addr_l;
162
163         val = (m->__if.mac_addr.addr_bytes[0] |
164                (m->__if.mac_addr.addr_bytes[1] << 8) |
165                (m->__if.mac_addr.addr_bytes[2] << 16) |
166                (m->__if.mac_addr.addr_bytes[3] << 24));
167         out_be32(reg, val);
168
169         if (addr_num)
170                 reg = &((struct memac_regs *)m->ccsr_map)->
171                                         mac_addr[addr_num-1].mac_addr_u;
172         else
173                 reg = &((struct memac_regs *)m->ccsr_map)->mac_addr0.mac_addr_u;
174
175         val = ((m->__if.mac_addr.addr_bytes[4] << 0) |
176                (m->__if.mac_addr.addr_bytes[5] << 8));
177         out_be32(reg, val);
178
179         return 0;
180 }
181
182 void
183 fman_if_set_rx_ignore_pause_frames(struct fman_if *p, bool enable)
184 {
185         struct __fman_if *__if = container_of(p, struct __fman_if, __if);
186         u32 value = 0;
187         void *cmdcfg;
188
189         assert(fman_ccsr_map_fd != -1);
190
191         /* Set Rx Ignore Pause Frames */
192         cmdcfg = &((struct memac_regs *)__if->ccsr_map)->command_config;
193         if (enable)
194                 value = in_be32(cmdcfg) | CMD_CFG_PAUSE_IGNORE;
195         else
196                 value = in_be32(cmdcfg) & ~CMD_CFG_PAUSE_IGNORE;
197
198         out_be32(cmdcfg, value);
199 }
200
201 void
202 fman_if_conf_max_frame_len(struct fman_if *p, unsigned int max_frame_len)
203 {
204         struct __fman_if *__if = container_of(p, struct __fman_if, __if);
205         unsigned int *maxfrm;
206
207         assert(fman_ccsr_map_fd != -1);
208
209         /* Set Max frame length */
210         maxfrm = &((struct memac_regs *)__if->ccsr_map)->maxfrm;
211         out_be32(maxfrm, (MAXFRM_RX_MASK & max_frame_len));
212 }
213
214 void
215 fman_if_stats_get(struct fman_if *p, struct rte_eth_stats *stats)
216 {
217         struct __fman_if *m = container_of(p, struct __fman_if, __if);
218         struct memac_regs *regs = m->ccsr_map;
219
220         /* read recved packet count */
221         stats->ipackets = ((u64)in_be32(&regs->rfrm_u)) << 32 |
222                         in_be32(&regs->rfrm_l);
223         stats->ibytes = ((u64)in_be32(&regs->roct_u)) << 32 |
224                         in_be32(&regs->roct_l);
225         stats->ierrors = ((u64)in_be32(&regs->rerr_u)) << 32 |
226                         in_be32(&regs->rerr_l);
227
228         /* read xmited packet count */
229         stats->opackets = ((u64)in_be32(&regs->tfrm_u)) << 32 |
230                         in_be32(&regs->tfrm_l);
231         stats->obytes = ((u64)in_be32(&regs->toct_u)) << 32 |
232                         in_be32(&regs->toct_l);
233         stats->oerrors = ((u64)in_be32(&regs->terr_u)) << 32 |
234                         in_be32(&regs->terr_l);
235 }
236
237 void
238 fman_if_stats_get_all(struct fman_if *p, uint64_t *value, int n)
239 {
240         struct __fman_if *m = container_of(p, struct __fman_if, __if);
241         struct memac_regs *regs = m->ccsr_map;
242         int i;
243         uint64_t base_offset = offsetof(struct memac_regs, reoct_l);
244
245         for (i = 0; i < n; i++)
246                 value[i] = ((u64)in_be32((char *)regs
247                                 + base_offset + 8 * i + 4)) << 32 |
248                                 ((u64)in_be32((char *)regs
249                                 + base_offset + 8 * i));
250 }
251
252 void
253 fman_if_stats_reset(struct fman_if *p)
254 {
255         struct __fman_if *m = container_of(p, struct __fman_if, __if);
256         struct memac_regs *regs = m->ccsr_map;
257         uint32_t tmp;
258
259         tmp = in_be32(&regs->statn_config);
260
261         tmp |= STATS_CFG_CLR;
262
263         out_be32(&regs->statn_config, tmp);
264
265         while (in_be32(&regs->statn_config) & STATS_CFG_CLR)
266                 ;
267 }
268
269 void
270 fman_if_promiscuous_enable(struct fman_if *p)
271 {
272         struct __fman_if *__if = container_of(p, struct __fman_if, __if);
273         void *cmdcfg;
274
275         assert(fman_ccsr_map_fd != -1);
276
277         /* Enable Rx promiscuous mode */
278         cmdcfg = &((struct memac_regs *)__if->ccsr_map)->command_config;
279         out_be32(cmdcfg, in_be32(cmdcfg) | CMD_CFG_PROMIS_EN);
280 }
281
282 void
283 fman_if_promiscuous_disable(struct fman_if *p)
284 {
285         struct __fman_if *__if = container_of(p, struct __fman_if, __if);
286         void *cmdcfg;
287
288         assert(fman_ccsr_map_fd != -1);
289
290         /* Disable Rx promiscuous mode */
291         cmdcfg = &((struct memac_regs *)__if->ccsr_map)->command_config;
292         out_be32(cmdcfg, in_be32(cmdcfg) & (~CMD_CFG_PROMIS_EN));
293 }
294
295 void
296 fman_if_enable_rx(struct fman_if *p)
297 {
298         struct __fman_if *__if = container_of(p, struct __fman_if, __if);
299
300         assert(fman_ccsr_map_fd != -1);
301
302         /* enable Rx and Tx */
303         out_be32(__if->ccsr_map + 8, in_be32(__if->ccsr_map + 8) | 3);
304 }
305
306 void
307 fman_if_disable_rx(struct fman_if *p)
308 {
309         struct __fman_if *__if = container_of(p, struct __fman_if, __if);
310
311         assert(fman_ccsr_map_fd != -1);
312
313         /* only disable Rx, not Tx */
314         out_be32(__if->ccsr_map + 8, in_be32(__if->ccsr_map + 8) & ~(u32)2);
315 }
316
317 void
318 fman_if_loopback_enable(struct fman_if *p)
319 {
320         struct __fman_if *__if = container_of(p, struct __fman_if, __if);
321
322         assert(fman_ccsr_map_fd != -1);
323
324         /* Enable loopback mode */
325         if ((__if->__if.is_memac) && (__if->__if.is_rgmii)) {
326                 unsigned int *ifmode =
327                         &((struct memac_regs *)__if->ccsr_map)->if_mode;
328                 out_be32(ifmode, in_be32(ifmode) | IF_MODE_RLP);
329         } else{
330                 unsigned int *cmdcfg =
331                         &((struct memac_regs *)__if->ccsr_map)->command_config;
332                 out_be32(cmdcfg, in_be32(cmdcfg) | CMD_CFG_LOOPBACK_EN);
333         }
334 }
335
336 void
337 fman_if_loopback_disable(struct fman_if *p)
338 {
339         struct __fman_if *__if = container_of(p, struct __fman_if, __if);
340
341         assert(fman_ccsr_map_fd != -1);
342         /* Disable loopback mode */
343         if ((__if->__if.is_memac) && (__if->__if.is_rgmii)) {
344                 unsigned int *ifmode =
345                         &((struct memac_regs *)__if->ccsr_map)->if_mode;
346                 out_be32(ifmode, in_be32(ifmode) & ~IF_MODE_RLP);
347         } else {
348                 unsigned int *cmdcfg =
349                         &((struct memac_regs *)__if->ccsr_map)->command_config;
350                 out_be32(cmdcfg, in_be32(cmdcfg) & ~CMD_CFG_LOOPBACK_EN);
351         }
352 }
353
354 void
355 fman_if_set_bp(struct fman_if *fm_if, unsigned num __always_unused,
356                     int bpid, size_t bufsize)
357 {
358         u32 fmbm_ebmpi;
359         u32 ebmpi_val_ace = 0xc0000000;
360         u32 ebmpi_mask = 0xffc00000;
361
362         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
363
364         assert(fman_ccsr_map_fd != -1);
365
366         fmbm_ebmpi =
367                in_be32(&((struct rx_bmi_regs *)__if->bmi_map)->fmbm_ebmpi[0]);
368         fmbm_ebmpi = ebmpi_val_ace | (fmbm_ebmpi & ebmpi_mask) | (bpid << 16) |
369                      (bufsize);
370
371         out_be32(&((struct rx_bmi_regs *)__if->bmi_map)->fmbm_ebmpi[0],
372                  fmbm_ebmpi);
373 }
374
375 int
376 fman_if_get_fc_threshold(struct fman_if *fm_if)
377 {
378         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
379         unsigned int *fmbm_mpd;
380
381         assert(fman_ccsr_map_fd != -1);
382
383         fmbm_mpd = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_mpd;
384         return in_be32(fmbm_mpd);
385 }
386
387 int
388 fman_if_set_fc_threshold(struct fman_if *fm_if, u32 high_water,
389                          u32 low_water, u32 bpid)
390 {
391         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
392         unsigned int *fmbm_mpd;
393
394         assert(fman_ccsr_map_fd != -1);
395
396         fmbm_mpd = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_mpd;
397         out_be32(fmbm_mpd, FMAN_ENABLE_BPOOL_DEPLETION);
398         return bm_pool_set_hw_threshold(bpid, low_water, high_water);
399
400 }
401
402 int
403 fman_if_get_fc_quanta(struct fman_if *fm_if)
404 {
405         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
406
407         assert(fman_ccsr_map_fd != -1);
408
409         return in_be32(&((struct memac_regs *)__if->ccsr_map)->pause_quanta[0]);
410 }
411
412 int
413 fman_if_set_fc_quanta(struct fman_if *fm_if, u16 pause_quanta)
414 {
415         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
416
417         assert(fman_ccsr_map_fd != -1);
418
419         out_be32(&((struct memac_regs *)__if->ccsr_map)->pause_quanta[0],
420                  pause_quanta);
421         return 0;
422 }
423
424 int
425 fman_if_get_fdoff(struct fman_if *fm_if)
426 {
427         u32 fmbm_rebm;
428         int fdoff;
429
430         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
431
432         assert(fman_ccsr_map_fd != -1);
433
434         fmbm_rebm = in_be32(&((struct rx_bmi_regs *)__if->bmi_map)->fmbm_rebm);
435
436         fdoff = (fmbm_rebm >> FMAN_SP_EXT_BUF_MARG_START_SHIFT) & 0x1ff;
437
438         return fdoff;
439 }
440
441 void
442 fman_if_set_err_fqid(struct fman_if *fm_if, uint32_t err_fqid)
443 {
444         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
445
446         assert(fman_ccsr_map_fd != -1);
447
448         unsigned int *fmbm_refqid =
449                         &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_refqid;
450         out_be32(fmbm_refqid, err_fqid);
451 }
452
453 int
454 fman_if_get_ic_params(struct fman_if *fm_if, struct fman_if_ic_params *icp)
455 {
456         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
457         int val = 0;
458         int iceof_mask = 0x001f0000;
459         int icsz_mask = 0x0000001f;
460         int iciof_mask = 0x00000f00;
461
462         assert(fman_ccsr_map_fd != -1);
463
464         unsigned int *fmbm_ricp =
465                 &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_ricp;
466         val = in_be32(fmbm_ricp);
467
468         icp->iceof = (val & iceof_mask) >> 12;
469         icp->iciof = (val & iciof_mask) >> 4;
470         icp->icsz = (val & icsz_mask) << 4;
471
472         return 0;
473 }
474
475 int
476 fman_if_set_ic_params(struct fman_if *fm_if,
477                           const struct fman_if_ic_params *icp)
478 {
479         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
480         int val = 0;
481         int iceof_mask = 0x001f0000;
482         int icsz_mask = 0x0000001f;
483         int iciof_mask = 0x00000f00;
484
485         assert(fman_ccsr_map_fd != -1);
486
487         val |= (icp->iceof << 12) & iceof_mask;
488         val |= (icp->iciof << 4) & iciof_mask;
489         val |= (icp->icsz >> 4) & icsz_mask;
490
491         unsigned int *fmbm_ricp =
492                 &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_ricp;
493         out_be32(fmbm_ricp, val);
494
495         return 0;
496 }
497
498 void
499 fman_if_set_fdoff(struct fman_if *fm_if, uint32_t fd_offset)
500 {
501         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
502         unsigned int *fmbm_rebm;
503         int val = 0;
504         int fmbm_mask = 0x01ff0000;
505
506         val = fd_offset << FMAN_SP_EXT_BUF_MARG_START_SHIFT;
507
508         assert(fman_ccsr_map_fd != -1);
509
510         fmbm_rebm = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_rebm;
511
512         out_be32(fmbm_rebm, (in_be32(fmbm_rebm) & ~fmbm_mask) | val);
513 }
514
515 void
516 fman_if_set_maxfrm(struct fman_if *fm_if, uint16_t max_frm)
517 {
518         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
519         unsigned int *reg_maxfrm;
520
521         assert(fman_ccsr_map_fd != -1);
522
523         reg_maxfrm = &((struct memac_regs *)__if->ccsr_map)->maxfrm;
524
525         out_be32(reg_maxfrm, (in_be32(reg_maxfrm) & 0xFFFF0000) | max_frm);
526 }
527
528 uint16_t
529 fman_if_get_maxfrm(struct fman_if *fm_if)
530 {
531         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
532         unsigned int *reg_maxfrm;
533
534         assert(fman_ccsr_map_fd != -1);
535
536         reg_maxfrm = &((struct memac_regs *)__if->ccsr_map)->maxfrm;
537
538         return (in_be32(reg_maxfrm) | 0x0000FFFF);
539 }
540
541 void
542 fman_if_set_dnia(struct fman_if *fm_if, uint32_t nia)
543 {
544         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
545         unsigned int *fmqm_pndn;
546
547         assert(fman_ccsr_map_fd != -1);
548
549         fmqm_pndn = &((struct fman_port_qmi_regs *)__if->qmi_map)->fmqm_pndn;
550
551         out_be32(fmqm_pndn, nia);
552 }
553
554 void
555 fman_if_discard_rx_errors(struct fman_if *fm_if)
556 {
557         struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
558         unsigned int *fmbm_rfsdm, *fmbm_rfsem;
559
560         fmbm_rfsem = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_rfsem;
561         out_be32(fmbm_rfsem, 0);
562
563         /* Configure the discard mask to discard the error packets which have
564          * DMA errors, Frame size error, Header error etc. The mask 0x010CE3F0
565          * is to configured discard all the errors which come in the FD[STATUS]
566          */
567         fmbm_rfsdm = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_rfsdm;
568         out_be32(fmbm_rfsdm, 0x010CE3F0);
569 }