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