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 #define FMAN_SP_EXT_BUF_MARG_START_SHIFT 16
44 /* Instantiate the global variable that the inline CRC64 implementation (in
45 * <fsl_fman.h>) depends on.
47 DECLARE_FMAN_CRC64_TABLE();
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]))
58 fman_if_set_mcast_filter_table(struct fman_if *p)
60 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
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);
70 fman_if_reset_mcast_filter_table(struct fman_if *p)
72 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
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);
82 uint32_t get_mac_hash_code(uint64_t eth_addr)
84 uint64_t mask1, mask2;
88 for (i = 0; i < 6; i++) {
89 mask1 = eth_addr & (uint64_t)0x01;
92 for (j = 0; j < 7; j++) {
93 mask2 = eth_addr & (uint64_t)0x01;
98 xorVal |= (mask1 << (5 - i));
105 fman_if_add_hash_mac_addr(struct fman_if *p, uint8_t *eth)
108 void *hashtable_ctrl;
111 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
113 eth_addr = ETH_ADDR_TO_UINT64(eth);
115 if (!(eth_addr & GROUP_ADDRESS))
118 hash = get_mac_hash_code(eth_addr) & HASH_CTRL_ADDR_MASK;
119 hash = hash | HASH_CTRL_MCAST_EN;
121 hashtable_ctrl = &((struct memac_regs *)__if->ccsr_map)->hashtable_ctrl;
122 out_be32(hashtable_ctrl, hash);
128 fman_if_get_primary_mac_addr(struct fman_if *p, uint8_t *eth)
130 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
132 &((struct memac_regs *)__if->ccsr_map)->mac_addr0.mac_addr_l;
133 u32 val = in_be32(mac_reg);
135 eth[0] = (val & 0x000000ff) >> 0;
136 eth[1] = (val & 0x0000ff00) >> 8;
137 eth[2] = (val & 0x00ff0000) >> 16;
138 eth[3] = (val & 0xff000000) >> 24;
140 mac_reg = &((struct memac_regs *)__if->ccsr_map)->mac_addr0.mac_addr_u;
141 val = in_be32(mac_reg);
143 eth[4] = (val & 0x000000ff) >> 0;
144 eth[5] = (val & 0x0000ff00) >> 8;
150 fman_if_clear_mac_addr(struct fman_if *p, uint8_t addr_num)
152 struct __fman_if *m = container_of(p, struct __fman_if, __if);
156 reg = &((struct memac_regs *)m->ccsr_map)->
157 mac_addr[addr_num-1].mac_addr_l;
159 reg = &((struct memac_regs *)m->ccsr_map)->
160 mac_addr[addr_num-1].mac_addr_u;
163 reg = &((struct memac_regs *)m->ccsr_map)->mac_addr0.mac_addr_l;
165 reg = &((struct memac_regs *)m->ccsr_map)->mac_addr0.mac_addr_u;
171 fman_if_add_mac_addr(struct fman_if *p, uint8_t *eth, uint8_t addr_num)
173 struct __fman_if *m = container_of(p, struct __fman_if, __if);
178 memcpy(&m->__if.mac_addr, eth, ETHER_ADDR_LEN);
181 reg = &((struct memac_regs *)m->ccsr_map)->
182 mac_addr[addr_num-1].mac_addr_l;
184 reg = &((struct memac_regs *)m->ccsr_map)->mac_addr0.mac_addr_l;
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));
193 reg = &((struct memac_regs *)m->ccsr_map)->
194 mac_addr[addr_num-1].mac_addr_u;
196 reg = &((struct memac_regs *)m->ccsr_map)->mac_addr0.mac_addr_u;
198 val = ((m->__if.mac_addr.addr_bytes[4] << 0) |
199 (m->__if.mac_addr.addr_bytes[5] << 8));
206 fman_if_set_rx_ignore_pause_frames(struct fman_if *p, bool enable)
208 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
212 assert(fman_ccsr_map_fd != -1);
214 /* Set Rx Ignore Pause Frames */
215 cmdcfg = &((struct memac_regs *)__if->ccsr_map)->command_config;
217 value = in_be32(cmdcfg) | CMD_CFG_PAUSE_IGNORE;
219 value = in_be32(cmdcfg) & ~CMD_CFG_PAUSE_IGNORE;
221 out_be32(cmdcfg, value);
225 fman_if_conf_max_frame_len(struct fman_if *p, unsigned int max_frame_len)
227 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
228 unsigned int *maxfrm;
230 assert(fman_ccsr_map_fd != -1);
232 /* Set Max frame length */
233 maxfrm = &((struct memac_regs *)__if->ccsr_map)->maxfrm;
234 out_be32(maxfrm, (MAXFRM_RX_MASK & max_frame_len));
238 fman_if_stats_get(struct fman_if *p, struct rte_eth_stats *stats)
240 struct __fman_if *m = container_of(p, struct __fman_if, __if);
241 struct memac_regs *regs = m->ccsr_map;
243 /* read recved packet count */
244 stats->ipackets = ((u64)in_be32(®s->rfrm_u)) << 32 |
245 in_be32(®s->rfrm_l);
246 stats->ibytes = ((u64)in_be32(®s->roct_u)) << 32 |
247 in_be32(®s->roct_l);
248 stats->ierrors = ((u64)in_be32(®s->rerr_u)) << 32 |
249 in_be32(®s->rerr_l);
251 /* read xmited packet count */
252 stats->opackets = ((u64)in_be32(®s->tfrm_u)) << 32 |
253 in_be32(®s->tfrm_l);
254 stats->obytes = ((u64)in_be32(®s->toct_u)) << 32 |
255 in_be32(®s->toct_l);
256 stats->oerrors = ((u64)in_be32(®s->terr_u)) << 32 |
257 in_be32(®s->terr_l);
261 fman_if_stats_get_all(struct fman_if *p, uint64_t *value, int n)
263 struct __fman_if *m = container_of(p, struct __fman_if, __if);
264 struct memac_regs *regs = m->ccsr_map;
266 uint64_t base_offset = offsetof(struct memac_regs, reoct_l);
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));
276 fman_if_stats_reset(struct fman_if *p)
278 struct __fman_if *m = container_of(p, struct __fman_if, __if);
279 struct memac_regs *regs = m->ccsr_map;
282 tmp = in_be32(®s->statn_config);
284 tmp |= STATS_CFG_CLR;
286 out_be32(®s->statn_config, tmp);
288 while (in_be32(®s->statn_config) & STATS_CFG_CLR)
293 fman_if_promiscuous_enable(struct fman_if *p)
295 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
298 assert(fman_ccsr_map_fd != -1);
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);
306 fman_if_promiscuous_disable(struct fman_if *p)
308 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
311 assert(fman_ccsr_map_fd != -1);
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));
319 fman_if_enable_rx(struct fman_if *p)
321 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
323 assert(fman_ccsr_map_fd != -1);
325 /* enable Rx and Tx */
326 out_be32(__if->ccsr_map + 8, in_be32(__if->ccsr_map + 8) | 3);
330 fman_if_disable_rx(struct fman_if *p)
332 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
334 assert(fman_ccsr_map_fd != -1);
336 /* only disable Rx, not Tx */
337 out_be32(__if->ccsr_map + 8, in_be32(__if->ccsr_map + 8) & ~(u32)2);
341 fman_if_loopback_enable(struct fman_if *p)
343 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
345 assert(fman_ccsr_map_fd != -1);
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);
353 unsigned int *cmdcfg =
354 &((struct memac_regs *)__if->ccsr_map)->command_config;
355 out_be32(cmdcfg, in_be32(cmdcfg) | CMD_CFG_LOOPBACK_EN);
360 fman_if_loopback_disable(struct fman_if *p)
362 struct __fman_if *__if = container_of(p, struct __fman_if, __if);
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);
371 unsigned int *cmdcfg =
372 &((struct memac_regs *)__if->ccsr_map)->command_config;
373 out_be32(cmdcfg, in_be32(cmdcfg) & ~CMD_CFG_LOOPBACK_EN);
378 fman_if_set_bp(struct fman_if *fm_if, unsigned num __always_unused,
379 int bpid, size_t bufsize)
382 u32 ebmpi_val_ace = 0xc0000000;
383 u32 ebmpi_mask = 0xffc00000;
385 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
387 assert(fman_ccsr_map_fd != -1);
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) |
394 out_be32(&((struct rx_bmi_regs *)__if->bmi_map)->fmbm_ebmpi[0],
399 fman_if_get_fc_threshold(struct fman_if *fm_if)
401 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
402 unsigned int *fmbm_mpd;
404 assert(fman_ccsr_map_fd != -1);
406 fmbm_mpd = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_mpd;
407 return in_be32(fmbm_mpd);
411 fman_if_set_fc_threshold(struct fman_if *fm_if, u32 high_water,
412 u32 low_water, u32 bpid)
414 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
415 unsigned int *fmbm_mpd;
417 assert(fman_ccsr_map_fd != -1);
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);
426 fman_if_get_fc_quanta(struct fman_if *fm_if)
428 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
430 assert(fman_ccsr_map_fd != -1);
432 return in_be32(&((struct memac_regs *)__if->ccsr_map)->pause_quanta[0]);
436 fman_if_set_fc_quanta(struct fman_if *fm_if, u16 pause_quanta)
438 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
440 assert(fman_ccsr_map_fd != -1);
442 out_be32(&((struct memac_regs *)__if->ccsr_map)->pause_quanta[0],
448 fman_if_get_fdoff(struct fman_if *fm_if)
453 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
455 assert(fman_ccsr_map_fd != -1);
457 fmbm_rebm = in_be32(&((struct rx_bmi_regs *)__if->bmi_map)->fmbm_rebm);
459 fdoff = (fmbm_rebm >> FMAN_SP_EXT_BUF_MARG_START_SHIFT) & 0x1ff;
465 fman_if_set_err_fqid(struct fman_if *fm_if, uint32_t err_fqid)
467 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
469 assert(fman_ccsr_map_fd != -1);
471 unsigned int *fmbm_refqid =
472 &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_refqid;
473 out_be32(fmbm_refqid, err_fqid);
477 fman_if_get_ic_params(struct fman_if *fm_if, struct fman_if_ic_params *icp)
479 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
481 int iceof_mask = 0x001f0000;
482 int icsz_mask = 0x0000001f;
483 int iciof_mask = 0x00000f00;
485 assert(fman_ccsr_map_fd != -1);
487 unsigned int *fmbm_ricp =
488 &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_ricp;
489 val = in_be32(fmbm_ricp);
491 icp->iceof = (val & iceof_mask) >> 12;
492 icp->iciof = (val & iciof_mask) >> 4;
493 icp->icsz = (val & icsz_mask) << 4;
499 fman_if_set_ic_params(struct fman_if *fm_if,
500 const struct fman_if_ic_params *icp)
502 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
504 int iceof_mask = 0x001f0000;
505 int icsz_mask = 0x0000001f;
506 int iciof_mask = 0x00000f00;
508 assert(fman_ccsr_map_fd != -1);
510 val |= (icp->iceof << 12) & iceof_mask;
511 val |= (icp->iciof << 4) & iciof_mask;
512 val |= (icp->icsz >> 4) & icsz_mask;
514 unsigned int *fmbm_ricp =
515 &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_ricp;
516 out_be32(fmbm_ricp, val);
522 fman_if_set_fdoff(struct fman_if *fm_if, uint32_t fd_offset)
524 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
525 unsigned int *fmbm_rebm;
527 int fmbm_mask = 0x01ff0000;
529 val = fd_offset << FMAN_SP_EXT_BUF_MARG_START_SHIFT;
531 assert(fman_ccsr_map_fd != -1);
533 fmbm_rebm = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_rebm;
535 out_be32(fmbm_rebm, (in_be32(fmbm_rebm) & ~fmbm_mask) | val);
539 fman_if_set_maxfrm(struct fman_if *fm_if, uint16_t max_frm)
541 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
542 unsigned int *reg_maxfrm;
544 assert(fman_ccsr_map_fd != -1);
546 reg_maxfrm = &((struct memac_regs *)__if->ccsr_map)->maxfrm;
548 out_be32(reg_maxfrm, (in_be32(reg_maxfrm) & 0xFFFF0000) | max_frm);
552 fman_if_get_maxfrm(struct fman_if *fm_if)
554 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
555 unsigned int *reg_maxfrm;
557 assert(fman_ccsr_map_fd != -1);
559 reg_maxfrm = &((struct memac_regs *)__if->ccsr_map)->maxfrm;
561 return (in_be32(reg_maxfrm) | 0x0000FFFF);
565 fman_if_set_dnia(struct fman_if *fm_if, uint32_t nia)
567 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
568 unsigned int *fmqm_pndn;
570 assert(fman_ccsr_map_fd != -1);
572 fmqm_pndn = &((struct fman_port_qmi_regs *)__if->qmi_map)->fmqm_pndn;
574 out_be32(fmqm_pndn, nia);
578 fman_if_discard_rx_errors(struct fman_if *fm_if)
580 struct __fman_if *__if = container_of(fm_if, struct __fman_if, __if);
581 unsigned int *fmbm_rfsdm, *fmbm_rfsem;
583 fmbm_rfsem = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_rfsem;
584 out_be32(fmbm_rfsem, 0);
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]
590 fmbm_rfsdm = &((struct rx_bmi_regs *)__if->bmi_map)->fmbm_rfsdm;
591 out_be32(fmbm_rfsdm, 0x010CE3F0);