1 /* Applied Micro X-Gene SoC Ethernet Classifier structures
3 * Copyright (c) 2016, Applied Micro Circuits Corporation
4 * Authors: Khuong Dinh <kdinh@apm.com>
5 * Tanmay Inamdar <tinamdar@apm.com>
6 * Iyappan Subramanian <isubramanian@apm.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "xgene_enet_main.h"
24 /* interfaces to convert structures to HW recognized bit formats */
25 static void xgene_cle_sband_to_hw(u8 frag, enum xgene_cle_prot_version ver,
26 enum xgene_cle_prot_type type, u32 len,
29 *reg = SET_VAL(SB_IPFRAG, frag) |
30 SET_VAL(SB_IPPROT, type) |
31 SET_VAL(SB_IPVER, ver) |
32 SET_VAL(SB_HDRLEN, len);
35 static void xgene_cle_idt_to_hw(struct xgene_enet_pdata *pdata,
36 u32 dstqid, u32 fpsel,
37 u32 nfpsel, u32 *idt_reg)
39 if (pdata->enet_id == XGENE_ENET1) {
40 *idt_reg = SET_VAL(IDT_DSTQID, dstqid) |
41 SET_VAL(IDT_FPSEL1, fpsel) |
42 SET_VAL(IDT_NFPSEL1, nfpsel);
44 *idt_reg = SET_VAL(IDT_DSTQID, dstqid) |
45 SET_VAL(IDT_FPSEL, fpsel) |
46 SET_VAL(IDT_NFPSEL, nfpsel);
50 static void xgene_cle_dbptr_to_hw(struct xgene_enet_pdata *pdata,
51 struct xgene_cle_dbptr *dbptr, u32 *buf)
53 buf[0] = SET_VAL(CLE_DROP, dbptr->drop);
54 buf[4] = SET_VAL(CLE_FPSEL, dbptr->fpsel) |
55 SET_VAL(CLE_DSTQIDL, dbptr->dstqid);
57 buf[5] = SET_VAL(CLE_DSTQIDH, (u32)dbptr->dstqid >> CLE_DSTQIDL_LEN) |
58 SET_VAL(CLE_PRIORITY, dbptr->cle_priority);
61 static void xgene_cle_kn_to_hw(struct xgene_cle_ptree_kn *kn, u32 *buf)
66 buf[j++] = SET_VAL(CLE_TYPE, kn->node_type);
67 for (i = 0; i < kn->num_keys; i++) {
68 struct xgene_cle_ptree_key *key = &kn->key[i];
71 buf[j] = SET_VAL(CLE_KN_PRIO, key->priority) |
72 SET_VAL(CLE_KN_RPTR, key->result_pointer);
74 data = SET_VAL(CLE_KN_PRIO, key->priority) |
75 SET_VAL(CLE_KN_RPTR, key->result_pointer);
76 buf[j++] |= (data << 16);
81 static void xgene_cle_dn_to_hw(struct xgene_cle_ptree_ewdn *dn,
84 struct xgene_cle_ptree_branch *br;
88 buf[j++] = SET_VAL(CLE_DN_TYPE, dn->node_type) |
89 SET_VAL(CLE_DN_LASTN, dn->last_node) |
90 SET_VAL(CLE_DN_HLS, dn->hdr_len_store) |
91 SET_VAL(CLE_DN_EXT, dn->hdr_extn) |
92 SET_VAL(CLE_DN_BSTOR, dn->byte_store) |
93 SET_VAL(CLE_DN_SBSTOR, dn->search_byte_store) |
94 SET_VAL(CLE_DN_RPTR, dn->result_pointer);
96 for (i = 0; i < dn->num_branches; i++) {
98 npp = br->next_packet_pointer;
100 if ((br->jump_rel == JMP_ABS) && (npp < CLE_PKTRAM_SIZE))
103 buf[j++] = SET_VAL(CLE_BR_VALID, br->valid) |
104 SET_VAL(CLE_BR_NPPTR, npp) |
105 SET_VAL(CLE_BR_JB, br->jump_bw) |
106 SET_VAL(CLE_BR_JR, br->jump_rel) |
107 SET_VAL(CLE_BR_OP, br->operation) |
108 SET_VAL(CLE_BR_NNODE, br->next_node) |
109 SET_VAL(CLE_BR_NBR, br->next_branch);
111 buf[j++] = SET_VAL(CLE_BR_DATA, br->data) |
112 SET_VAL(CLE_BR_MASK, br->mask);
116 static int xgene_cle_poll_cmd_done(void __iomem *base,
117 enum xgene_cle_cmd_type cmd)
119 u32 status, loop = 10;
123 status = ioread32(base + INDCMD_STATUS);
128 usleep_range(1000, 2000);
134 static int xgene_cle_dram_wr(struct xgene_enet_cle *cle, u32 *data, u8 nregs,
135 u32 index, enum xgene_cle_dram_type type,
136 enum xgene_cle_cmd_type cmd)
138 enum xgene_cle_parser parser = cle->active_parser;
139 void __iomem *base = cle->base;
144 /* PTREE_RAM onwards, DRAM regions are common for all parsers */
145 nparsers = (type >= PTREE_RAM) ? 1 : cle->parsers;
147 for (i = 0; i < nparsers; i++) {
149 if ((type < PTREE_RAM) && (parser != PARSER_ALL))
152 ind_addr = XGENE_CLE_DRAM(type + (port * 4)) | index;
153 iowrite32(ind_addr, base + INDADDR);
154 for (j = 0; j < nregs; j++)
155 iowrite32(data[j], base + DATA_RAM0 + (j * 4));
156 iowrite32(cmd, base + INDCMD);
158 ret = xgene_cle_poll_cmd_done(base, cmd);
166 static void xgene_cle_enable_ptree(struct xgene_enet_pdata *pdata,
167 struct xgene_enet_cle *cle)
169 struct xgene_cle_ptree *ptree = &cle->ptree;
170 void __iomem *addr, *base = cle->base;
171 u32 offset = CLE_PORT_OFFSET;
174 /* 1G port has to advance 4 bytes and 10G has to advance 8 bytes */
175 ptree->start_pkt += cle->jump_bytes;
176 for (i = 0; i < cle->parsers; i++) {
177 if (cle->active_parser != PARSER_ALL)
178 addr = base + cle->active_parser * offset;
180 addr = base + (i * offset);
182 iowrite32(ptree->start_node & 0x3fff, addr + SNPTR0);
183 iowrite32(ptree->start_pkt & 0x1ff, addr + SPPTR0);
187 static int xgene_cle_setup_dbptr(struct xgene_enet_pdata *pdata,
188 struct xgene_enet_cle *cle)
190 struct xgene_cle_ptree *ptree = &cle->ptree;
191 u32 buf[CLE_DRAM_REGS];
195 memset(buf, 0, sizeof(buf));
196 for (i = 0; i < ptree->num_dbptr; i++) {
197 xgene_cle_dbptr_to_hw(pdata, &ptree->dbptr[i], buf);
198 ret = xgene_cle_dram_wr(cle, buf, 6, i + ptree->start_dbptr,
207 static int xgene_cle_setup_node(struct xgene_enet_pdata *pdata,
208 struct xgene_enet_cle *cle)
210 struct xgene_cle_ptree *ptree = &cle->ptree;
211 struct xgene_cle_ptree_ewdn *dn = ptree->dn;
212 struct xgene_cle_ptree_kn *kn = ptree->kn;
213 u32 buf[CLE_DRAM_REGS];
216 memset(buf, 0, sizeof(buf));
217 for (i = 0; i < ptree->num_dn; i++) {
218 xgene_cle_dn_to_hw(&dn[i], buf, cle->jump_bytes);
219 ret = xgene_cle_dram_wr(cle, buf, 17, i + ptree->start_node,
220 PTREE_RAM, CLE_CMD_WR);
225 /* continue node index for key node */
226 memset(buf, 0, sizeof(buf));
227 for (j = i; j < (ptree->num_kn + ptree->num_dn); j++) {
228 xgene_cle_kn_to_hw(&kn[j - ptree->num_dn], buf);
229 ret = xgene_cle_dram_wr(cle, buf, 17, j + ptree->start_node,
230 PTREE_RAM, CLE_CMD_WR);
238 static int xgene_cle_setup_ptree(struct xgene_enet_pdata *pdata,
239 struct xgene_enet_cle *cle)
243 ret = xgene_cle_setup_node(pdata, cle);
247 ret = xgene_cle_setup_dbptr(pdata, cle);
251 xgene_cle_enable_ptree(pdata, cle);
256 static void xgene_cle_setup_def_dbptr(struct xgene_enet_pdata *pdata,
257 struct xgene_enet_cle *enet_cle,
258 struct xgene_cle_dbptr *dbptr,
259 u32 index, u8 priority)
261 void __iomem *base = enet_cle->base;
262 void __iomem *base_addr;
263 u32 buf[CLE_DRAM_REGS];
267 memset(buf, 0, sizeof(buf));
268 xgene_cle_dbptr_to_hw(pdata, dbptr, buf);
270 for (i = 0; i < enet_cle->parsers; i++) {
271 if (enet_cle->active_parser != PARSER_ALL) {
272 offset = enet_cle->active_parser *
275 offset = i * CLE_PORT_OFFSET;
278 base_addr = base + DFCLSRESDB00 + offset;
279 for (j = 0; j < 6; j++)
280 iowrite32(buf[j], base_addr + (j * 4));
282 def_cls = ((priority & 0x7) << 10) | (index & 0x3ff);
283 iowrite32(def_cls, base + DFCLSRESDBPTR0 + offset);
287 static int xgene_cle_set_rss_sband(struct xgene_enet_cle *cle)
289 u32 idx = CLE_PKTRAM_SIZE / sizeof(u32);
290 u32 mac_hdr_len = ETH_HLEN;
296 /* Sideband: IPV4/TCP packets */
297 hdr_len = (mac_hdr_len << 5) | ipv4_ihl;
298 xgene_cle_sband_to_hw(0, XGENE_CLE_IPV4, XGENE_CLE_TCP, hdr_len, ®);
301 /* Sideband: IPv4/UDP packets */
302 hdr_len = (mac_hdr_len << 5) | ipv4_ihl;
303 xgene_cle_sband_to_hw(1, XGENE_CLE_IPV4, XGENE_CLE_UDP, hdr_len, ®);
304 sband |= (reg << 16);
306 ret = xgene_cle_dram_wr(cle, &sband, 1, idx, PKT_RAM, CLE_CMD_WR);
310 /* Sideband: IPv4/RAW packets */
311 hdr_len = (mac_hdr_len << 5) | ipv4_ihl;
312 xgene_cle_sband_to_hw(0, XGENE_CLE_IPV4, XGENE_CLE_OTHER,
316 /* Sideband: Ethernet II/RAW packets */
317 hdr_len = (mac_hdr_len << 5);
318 xgene_cle_sband_to_hw(0, XGENE_CLE_IPV4, XGENE_CLE_OTHER,
320 sband |= (reg << 16);
322 ret = xgene_cle_dram_wr(cle, &sband, 1, idx + 1, PKT_RAM, CLE_CMD_WR);
329 static int xgene_cle_set_rss_skeys(struct xgene_enet_cle *cle)
331 u32 secret_key_ipv4[4]; /* 16 Bytes*/
334 get_random_bytes(secret_key_ipv4, 16);
335 ret = xgene_cle_dram_wr(cle, secret_key_ipv4, 4, 0,
336 RSS_IPV4_HASH_SKEY, CLE_CMD_WR);
340 static int xgene_cle_set_rss_idt(struct xgene_enet_pdata *pdata)
342 u32 fpsel, dstqid, nfpsel, idt_reg, idx;
346 for (i = 0; i < XGENE_CLE_IDT_ENTRIES; i++) {
347 idx = i % pdata->rxq_cnt;
348 pool_id = pdata->rx_ring[idx]->buf_pool->id;
349 fpsel = xgene_enet_ring_bufnum(pool_id) - 0x20;
350 dstqid = xgene_enet_dst_ring_num(pdata->rx_ring[idx]);
354 xgene_cle_idt_to_hw(pdata, dstqid, fpsel, nfpsel, &idt_reg);
355 ret = xgene_cle_dram_wr(&pdata->cle, &idt_reg, 1, i,
356 RSS_IDT, CLE_CMD_WR);
361 ret = xgene_cle_set_rss_skeys(&pdata->cle);
368 static int xgene_cle_setup_rss(struct xgene_enet_pdata *pdata)
370 struct xgene_enet_cle *cle = &pdata->cle;
371 void __iomem *base = cle->base;
375 offset = CLE_PORT_OFFSET;
376 for (i = 0; i < cle->parsers; i++) {
377 if (cle->active_parser != PARSER_ALL)
378 offset = cle->active_parser * CLE_PORT_OFFSET;
380 offset = i * CLE_PORT_OFFSET;
383 val = (RSS_IPV4_12B << 1) | 0x1;
384 writel(val, base + RSS_CTRL0 + offset);
387 /* setup sideband data */
388 ret = xgene_cle_set_rss_sband(cle);
392 /* setup indirection table */
393 ret = xgene_cle_set_rss_idt(pdata);
400 static int xgene_enet_cle_init(struct xgene_enet_pdata *pdata)
402 struct xgene_enet_cle *enet_cle = &pdata->cle;
403 struct xgene_cle_dbptr dbptr[DB_MAX_PTRS];
404 struct xgene_cle_ptree_branch *br;
405 u32 def_qid, def_fpsel, pool_id;
406 struct xgene_cle_ptree *ptree;
407 struct xgene_cle_ptree_kn kn;
409 struct xgene_cle_ptree_ewdn ptree_dn[] = {
416 .byte_store = NO_BYTE,
417 .search_byte_store = NO_BYTE,
418 .result_pointer = DB_RES_DROP,
424 .next_packet_pointer = 22,
428 .next_node = PKT_PROT_NODE,
435 .next_packet_pointer = 262,
439 .next_node = LAST_NODE,
452 .byte_store = NO_BYTE,
453 .search_byte_store = NO_BYTE,
454 .result_pointer = DB_RES_DROP,
460 .next_packet_pointer = 26,
464 .next_node = RSS_IPV4_TCP_NODE,
472 .next_packet_pointer = 26,
476 .next_node = RSS_IPV4_UDP_NODE,
483 .next_packet_pointer = 260,
487 .next_node = LAST_NODE,
495 /* RSS_IPV4_TCP_NODE */
500 .byte_store = NO_BYTE,
501 .search_byte_store = BOTH_BYTES,
502 .result_pointer = DB_RES_DROP,
508 .next_packet_pointer = 28,
512 .next_node = RSS_IPV4_TCP_NODE,
520 .next_packet_pointer = 30,
524 .next_node = RSS_IPV4_TCP_NODE,
532 .next_packet_pointer = 32,
536 .next_node = RSS_IPV4_TCP_NODE,
544 .next_packet_pointer = 34,
548 .next_node = RSS_IPV4_TCP_NODE,
556 .next_packet_pointer = 36,
560 .next_node = RSS_IPV4_TCP_NODE,
568 .next_packet_pointer = 256,
572 .next_node = LAST_NODE,
580 /* RSS_IPV4_UDP_NODE */
585 .byte_store = NO_BYTE,
586 .search_byte_store = BOTH_BYTES,
587 .result_pointer = DB_RES_DROP,
593 .next_packet_pointer = 28,
597 .next_node = RSS_IPV4_UDP_NODE,
605 .next_packet_pointer = 30,
609 .next_node = RSS_IPV4_UDP_NODE,
617 .next_packet_pointer = 32,
621 .next_node = RSS_IPV4_UDP_NODE,
629 .next_packet_pointer = 34,
633 .next_node = RSS_IPV4_UDP_NODE,
641 .next_packet_pointer = 36,
645 .next_node = RSS_IPV4_UDP_NODE,
653 .next_packet_pointer = 258,
657 .next_node = LAST_NODE,
670 .byte_store = NO_BYTE,
671 .search_byte_store = NO_BYTE,
672 .result_pointer = DB_RES_DROP,
677 .next_packet_pointer = 0,
681 .next_node = MAX_NODES,
690 ptree = &enet_cle->ptree;
691 ptree->start_pkt = 12; /* Ethertype */
692 if (pdata->phy_mode == PHY_INTERFACE_MODE_XGMII) {
693 ret = xgene_cle_setup_rss(pdata);
695 netdev_err(pdata->ndev, "RSS initialization failed\n");
699 br = &ptree_dn[PKT_PROT_NODE].branch[0];
701 br->next_packet_pointer = 260;
702 br->next_node = LAST_NODE;
707 def_qid = xgene_enet_dst_ring_num(pdata->rx_ring[0]);
708 pool_id = pdata->rx_ring[0]->buf_pool->id;
709 def_fpsel = xgene_enet_ring_bufnum(pool_id) - 0x20;
711 memset(dbptr, 0, sizeof(struct xgene_cle_dbptr) * DB_MAX_PTRS);
712 dbptr[DB_RES_ACCEPT].fpsel = def_fpsel;
713 dbptr[DB_RES_ACCEPT].dstqid = def_qid;
714 dbptr[DB_RES_ACCEPT].cle_priority = 1;
716 dbptr[DB_RES_DEF].fpsel = def_fpsel;
717 dbptr[DB_RES_DEF].dstqid = def_qid;
718 dbptr[DB_RES_DEF].cle_priority = 7;
719 xgene_cle_setup_def_dbptr(pdata, enet_cle, &dbptr[DB_RES_DEF],
722 dbptr[DB_RES_DROP].drop = 1;
724 memset(&kn, 0, sizeof(kn));
727 kn.key[0].priority = 0;
728 kn.key[0].result_pointer = DB_RES_ACCEPT;
730 ptree->dn = ptree_dn;
732 ptree->dbptr = dbptr;
733 ptree->num_dn = MAX_NODES;
735 ptree->num_dbptr = DB_MAX_PTRS;
737 return xgene_cle_setup_ptree(pdata, enet_cle);
740 const struct xgene_cle_ops xgene_cle3in_ops = {
741 .cle_init = xgene_enet_cle_init,