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