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