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.
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.
30 #include <sys/types.h>
31 #include <sys/ioctl.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.
39 #include <fsl_fman_crc64.h>
42 /* Instantiate the global variable that the inline CRC64 implementation (in
43 * <fsl_fman.h>) depends on.
45 DECLARE_FMAN_CRC64_TABLE();
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]))
56 fman_if_set_mcast_filter_table(struct fman_if *p)
58 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
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);
68 fman_if_reset_mcast_filter_table(struct fman_if *p)
70 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
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);
80 uint32_t get_mac_hash_code(uint64_t eth_addr)
82 uint64_t mask1, mask2;
86 for (i = 0; i < 6; i++) {
87 mask1 = eth_addr & (uint64_t)0x01;
90 for (j = 0; j < 7; j++) {
91 mask2 = eth_addr & (uint64_t)0x01;
96 xorVal |= (mask1 << (5 - i));
103 fman_if_add_hash_mac_addr(struct fman_if *p, uint8_t *eth)
106 void *hashtable_ctrl;
109 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
111 eth_addr = ETH_ADDR_TO_UINT64(eth);
113 if (!(eth_addr & GROUP_ADDRESS))
116 hash = get_mac_hash_code(eth_addr) & HASH_CTRL_ADDR_MASK;
117 hash = hash | HASH_CTRL_MCAST_EN;
119 hashtable_ctrl = &((struct memac_regs *)__if->ccsr_map)->hashtable_ctrl;
120 out_be32(hashtable_ctrl, hash);
126 fman_if_get_primary_mac_addr(struct fman_if *p, uint8_t *eth)
128 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
130 &((struct memac_regs *)__if->ccsr_map)->mac_addr0.mac_addr_l;
131 u32 val = in_be32(mac_reg);
133 eth[0] = (val & 0x000000ff) >> 0;
134 eth[1] = (val & 0x0000ff00) >> 8;
135 eth[2] = (val & 0x00ff0000) >> 16;
136 eth[3] = (val & 0xff000000) >> 24;
138 mac_reg = &((struct memac_regs *)__if->ccsr_map)->mac_addr0.mac_addr_u;
139 val = in_be32(mac_reg);
141 eth[4] = (val & 0x000000ff) >> 0;
142 eth[5] = (val & 0x0000ff00) >> 8;
148 fman_if_clear_mac_addr(struct fman_if *p, uint8_t addr_num)
150 struct __fman_if *m = container_of(p, struct __fman_if, __if);
154 reg = &((struct memac_regs *)m->ccsr_map)->
155 mac_addr[addr_num-1].mac_addr_l;
157 reg = &((struct memac_regs *)m->ccsr_map)->
158 mac_addr[addr_num-1].mac_addr_u;
161 reg = &((struct memac_regs *)m->ccsr_map)->mac_addr0.mac_addr_l;
163 reg = &((struct memac_regs *)m->ccsr_map)->mac_addr0.mac_addr_u;
169 fman_if_add_mac_addr(struct fman_if *p, uint8_t *eth, uint8_t addr_num)
171 struct __fman_if *m = container_of(p, struct __fman_if, __if);
176 memcpy(&m->__if.mac_addr, eth, ETHER_ADDR_LEN);
179 reg = &((struct memac_regs *)m->ccsr_map)->
180 mac_addr[addr_num-1].mac_addr_l;
182 reg = &((struct memac_regs *)m->ccsr_map)->mac_addr0.mac_addr_l;
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));
191 reg = &((struct memac_regs *)m->ccsr_map)->
192 mac_addr[addr_num-1].mac_addr_u;
194 reg = &((struct memac_regs *)m->ccsr_map)->mac_addr0.mac_addr_u;
196 val = ((m->__if.mac_addr.addr_bytes[4] << 0) |
197 (m->__if.mac_addr.addr_bytes[5] << 8));
204 fman_if_set_rx_ignore_pause_frames(struct fman_if *p, bool enable)
206 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
210 assert(fman_ccsr_map_fd != -1);
212 /* Set Rx Ignore Pause Frames */
213 cmdcfg = &((struct memac_regs *)__if->ccsr_map)->command_config;
215 value = in_be32(cmdcfg) | CMD_CFG_PAUSE_IGNORE;
217 value = in_be32(cmdcfg) & ~CMD_CFG_PAUSE_IGNORE;
219 out_be32(cmdcfg, value);
223 fman_if_conf_max_frame_len(struct fman_if *p, unsigned int max_frame_len)
225 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
226 unsigned int *maxfrm;
228 assert(fman_ccsr_map_fd != -1);
230 /* Set Max frame length */
231 maxfrm = &((struct memac_regs *)__if->ccsr_map)->maxfrm;
232 out_be32(maxfrm, (MAXFRM_RX_MASK & max_frame_len));
236 fman_if_stats_get(struct fman_if *p, struct rte_eth_stats *stats)
238 struct __fman_if *m = container_of(p, struct __fman_if, __if);
239 struct memac_regs *regs = m->ccsr_map;
241 /* read recved packet count */
242 stats->ipackets = ((u64)in_be32(®s->rfrm_u)) << 32 |
243 in_be32(®s->rfrm_l);
244 stats->ibytes = ((u64)in_be32(®s->roct_u)) << 32 |
245 in_be32(®s->roct_l);
246 stats->ierrors = ((u64)in_be32(®s->rerr_u)) << 32 |
247 in_be32(®s->rerr_l);
249 /* read xmited packet count */
250 stats->opackets = ((u64)in_be32(®s->tfrm_u)) << 32 |
251 in_be32(®s->tfrm_l);
252 stats->obytes = ((u64)in_be32(®s->toct_u)) << 32 |
253 in_be32(®s->toct_l);
254 stats->oerrors = ((u64)in_be32(®s->terr_u)) << 32 |
255 in_be32(®s->terr_l);
259 fman_if_stats_get_all(struct fman_if *p, uint64_t *value, int n)
261 struct __fman_if *m = container_of(p, struct __fman_if, __if);
262 struct memac_regs *regs = m->ccsr_map;
264 uint64_t base_offset = offsetof(struct memac_regs, reoct_l);
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));
274 fman_if_stats_reset(struct fman_if *p)
276 struct __fman_if *m = container_of(p, struct __fman_if, __if);
277 struct memac_regs *regs = m->ccsr_map;
280 tmp = in_be32(®s->statn_config);
282 tmp |= STATS_CFG_CLR;
284 out_be32(®s->statn_config, tmp);
286 while (in_be32(®s->statn_config) & STATS_CFG_CLR)
291 fman_if_promiscuous_enable(struct fman_if *p)
293 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
296 assert(fman_ccsr_map_fd != -1);
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);
304 fman_if_promiscuous_disable(struct fman_if *p)
306 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
309 assert(fman_ccsr_map_fd != -1);
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));
317 fman_if_enable_rx(struct fman_if *p)
319 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
321 assert(fman_ccsr_map_fd != -1);
323 /* enable Rx and Tx */
324 out_be32(__if->ccsr_map + 8, in_be32(__if->ccsr_map + 8) | 3);
328 fman_if_disable_rx(struct fman_if *p)
330 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
332 assert(fman_ccsr_map_fd != -1);
334 /* only disable Rx, not Tx */
335 out_be32(__if->ccsr_map + 8, in_be32(__if->ccsr_map + 8) & ~(u32)2);
339 fman_if_loopback_enable(struct fman_if *p)
341 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
343 assert(fman_ccsr_map_fd != -1);
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);
351 unsigned int *cmdcfg =
352 &((struct memac_regs *)__if->ccsr_map)->command_config;
353 out_be32(cmdcfg, in_be32(cmdcfg) | CMD_CFG_LOOPBACK_EN);
358 fman_if_loopback_disable(struct fman_if *p)
360 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
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);
369 unsigned int *cmdcfg =
370 &((struct memac_regs *)__if->ccsr_map)->command_config;
371 out_be32(cmdcfg, in_be32(cmdcfg) & ~CMD_CFG_LOOPBACK_EN);
376 fman_if_set_bp(struct fman_if *fm_if, unsigned num __always_unused,
377 int bpid, size_t bufsize)
380 u32 ebmpi_val_ace = 0xc0000000;
381 u32 ebmpi_mask = 0xffc00000;
383 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
385 assert(fman_ccsr_map_fd != -1);
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) |
392 out_be32(&((struct rx_bmi_regs *)__if->bmi_map)->fmbm_ebmpi[0],
397 fman_if_get_fc_threshold(struct fman_if *fm_if)
399 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
400 unsigned int *fmbm_mpd;
402 assert(fman_ccsr_map_fd != -1);
404 fmbm_mpd = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_mpd;
405 return in_be32(fmbm_mpd);
409 fman_if_set_fc_threshold(struct fman_if *fm_if, u32 high_water,
410 u32 low_water, u32 bpid)
412 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
413 unsigned int *fmbm_mpd;
415 assert(fman_ccsr_map_fd != -1);
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);
424 fman_if_get_fc_quanta(struct fman_if *fm_if)
426 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
428 assert(fman_ccsr_map_fd != -1);
430 return in_be32(&((struct memac_regs *)__if->ccsr_map)->pause_quanta[0]);
434 fman_if_set_fc_quanta(struct fman_if *fm_if, u16 pause_quanta)
436 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
438 assert(fman_ccsr_map_fd != -1);
440 out_be32(&((struct memac_regs *)__if->ccsr_map)->pause_quanta[0],
446 fman_if_get_fdoff(struct fman_if *fm_if)
450 int iceof_mask = 0x001f0000;
451 int icsz_mask = 0x0000001f;
453 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
455 assert(fman_ccsr_map_fd != -1);
458 in_be32(&((struct rx_bmi_regs *)__if->bmi_map)->fmbm_ricp);
460 fdoff = ((fmbm_ricp & iceof_mask) >> 16) * 16 +
461 (fmbm_ricp & icsz_mask) * 16;
467 fman_if_set_err_fqid(struct fman_if *fm_if, uint32_t err_fqid)
469 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
471 assert(fman_ccsr_map_fd != -1);
473 unsigned int *fmbm_refqid =
474 &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_refqid;
475 out_be32(fmbm_refqid, err_fqid);
479 fman_if_get_ic_params(struct fman_if *fm_if, struct fman_if_ic_params *icp)
481 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
483 int iceof_mask = 0x001f0000;
484 int icsz_mask = 0x0000001f;
485 int iciof_mask = 0x00000f00;
487 assert(fman_ccsr_map_fd != -1);
489 unsigned int *fmbm_ricp =
490 &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_ricp;
491 val = in_be32(fmbm_ricp);
493 icp->iceof = (val & iceof_mask) >> 12;
494 icp->iciof = (val & iciof_mask) >> 4;
495 icp->icsz = (val & icsz_mask) << 4;
501 fman_if_set_ic_params(struct fman_if *fm_if,
502 const struct fman_if_ic_params *icp)
504 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
506 int iceof_mask = 0x001f0000;
507 int icsz_mask = 0x0000001f;
508 int iciof_mask = 0x00000f00;
510 assert(fman_ccsr_map_fd != -1);
512 val |= (icp->iceof << 12) & iceof_mask;
513 val |= (icp->iciof << 4) & iciof_mask;
514 val |= (icp->icsz >> 4) & icsz_mask;
516 unsigned int *fmbm_ricp =
517 &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_ricp;
518 out_be32(fmbm_ricp, val);
524 fman_if_set_fdoff(struct fman_if *fm_if, uint32_t fd_offset)
526 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
527 unsigned int *fmbm_rebm;
529 assert(fman_ccsr_map_fd != -1);
531 fmbm_rebm = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_rebm;
533 out_be32(fmbm_rebm, in_be32(fmbm_rebm) | (fd_offset << 16));
537 fman_if_set_maxfrm(struct fman_if *fm_if, uint16_t max_frm)
539 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
540 unsigned int *reg_maxfrm;
542 assert(fman_ccsr_map_fd != -1);
544 reg_maxfrm = &((struct memac_regs *)__if->ccsr_map)->maxfrm;
546 out_be32(reg_maxfrm, (in_be32(reg_maxfrm) & 0xFFFF0000) | max_frm);
550 fman_if_get_maxfrm(struct fman_if *fm_if)
552 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
553 unsigned int *reg_maxfrm;
555 assert(fman_ccsr_map_fd != -1);
557 reg_maxfrm = &((struct memac_regs *)__if->ccsr_map)->maxfrm;
559 return (in_be32(reg_maxfrm) | 0x0000FFFF);
563 fman_if_set_dnia(struct fman_if *fm_if, uint32_t nia)
565 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
566 unsigned int *fmqm_pndn;
568 assert(fman_ccsr_map_fd != -1);
570 fmqm_pndn = &((struct fman_port_qmi_regs *)__if->qmi_map)->fmqm_pndn;
572 out_be32(fmqm_pndn, nia);
576 fman_if_discard_rx_errors(struct fman_if *fm_if)
578 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
579 unsigned int *fmbm_rfsdm, *fmbm_rfsem;
581 fmbm_rfsem = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_rfsem;
582 out_be32(fmbm_rfsem, 0);
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]
588 fmbm_rfsdm = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_rfsdm;
589 out_be32(fmbm_rfsdm, 0x010CE3F0);