octeon_pip.c revision 1.7 1 1.7 simonb /* $NetBSD: octeon_pip.c,v 1.7 2020/06/22 03:05:07 simonb Exp $ */
2 1.1 hikaru
3 1.1 hikaru /*
4 1.1 hikaru * Copyright (c) 2007 Internet Initiative Japan, Inc.
5 1.1 hikaru * All rights reserved.
6 1.1 hikaru *
7 1.1 hikaru * Redistribution and use in source and binary forms, with or without
8 1.1 hikaru * modification, are permitted provided that the following conditions
9 1.1 hikaru * are met:
10 1.1 hikaru * 1. Redistributions of source code must retain the above copyright
11 1.1 hikaru * notice, this list of conditions and the following disclaimer.
12 1.1 hikaru * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 hikaru * notice, this list of conditions and the following disclaimer in the
14 1.1 hikaru * documentation and/or other materials provided with the distribution.
15 1.1 hikaru *
16 1.1 hikaru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 1.1 hikaru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 hikaru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 hikaru * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 1.1 hikaru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 hikaru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 hikaru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 hikaru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 hikaru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 hikaru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 hikaru * SUCH DAMAGE.
27 1.1 hikaru */
28 1.1 hikaru
29 1.1 hikaru #include <sys/cdefs.h>
30 1.7 simonb __KERNEL_RCSID(0, "$NetBSD: octeon_pip.c,v 1.7 2020/06/22 03:05:07 simonb Exp $");
31 1.1 hikaru
32 1.1 hikaru #include "opt_octeon.h"
33 1.1 hikaru
34 1.1 hikaru #include <sys/param.h>
35 1.1 hikaru #include <sys/systm.h>
36 1.1 hikaru #include <sys/malloc.h>
37 1.1 hikaru #include <sys/syslog.h>
38 1.1 hikaru #include <sys/time.h>
39 1.1 hikaru #include <net/if.h>
40 1.1 hikaru #include <mips/locore.h>
41 1.1 hikaru #include <mips/cavium/octeonvar.h>
42 1.1 hikaru #include <mips/cavium/dev/octeon_pipreg.h>
43 1.1 hikaru #include <mips/cavium/dev/octeon_pipvar.h>
44 1.1 hikaru
45 1.1 hikaru /* XXX */
46 1.1 hikaru void
47 1.4 simonb octpip_init(struct octpip_attach_args *aa, struct octpip_softc **rsc)
48 1.1 hikaru {
49 1.4 simonb struct octpip_softc *sc;
50 1.1 hikaru int status;
51 1.1 hikaru
52 1.1 hikaru sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
53 1.1 hikaru if (sc == NULL)
54 1.1 hikaru panic("can't allocate memory: %s", __func__);
55 1.1 hikaru
56 1.1 hikaru sc->sc_port = aa->aa_port;
57 1.1 hikaru sc->sc_regt = aa->aa_regt;
58 1.1 hikaru sc->sc_tag_type = aa->aa_tag_type;
59 1.1 hikaru sc->sc_receive_group = aa->aa_receive_group;
60 1.1 hikaru sc->sc_ip_offset = aa->aa_ip_offset;
61 1.1 hikaru
62 1.1 hikaru status = bus_space_map(sc->sc_regt, PIP_BASE, PIP_SIZE, 0,
63 1.1 hikaru &sc->sc_regh);
64 1.1 hikaru if (status != 0)
65 1.1 hikaru panic("can't map %s space", "pip register");
66 1.1 hikaru
67 1.1 hikaru *rsc = sc;
68 1.1 hikaru }
69 1.1 hikaru
70 1.1 hikaru #define _PIP_RD8(sc, off) \
71 1.1 hikaru bus_space_read_8((sc)->sc_regt, (sc)->sc_regh, (off))
72 1.1 hikaru #define _PIP_WR8(sc, off, v) \
73 1.1 hikaru bus_space_write_8((sc)->sc_regt, (sc)->sc_regh, (off), (v))
74 1.1 hikaru
75 1.1 hikaru int
76 1.4 simonb octpip_port_config(struct octpip_softc *sc)
77 1.1 hikaru {
78 1.1 hikaru uint64_t prt_cfg;
79 1.1 hikaru uint64_t prt_tag;
80 1.1 hikaru uint64_t ip_offset;
81 1.1 hikaru
82 1.1 hikaru /*
83 1.1 hikaru * Process the headers and place the IP header in the work queue
84 1.1 hikaru */
85 1.1 hikaru prt_cfg = 0;
86 1.1 hikaru if (MIPS_PRID_IMPL(mips_options.mips_cpu_id) == MIPS_CN50XX) {
87 1.1 hikaru SET(prt_cfg, PIP_PRT_CFGN_LENERR_EN);
88 1.1 hikaru SET(prt_cfg, PIP_PRT_CFGN_MAXERR_EN);
89 1.1 hikaru SET(prt_cfg, PIP_PRT_CFGN_MINERR_EN);
90 1.1 hikaru }
91 1.1 hikaru /* RAWDRP=0; don't allow raw packet drop */
92 1.1 hikaru /* TAGINC=0 */
93 1.1 hikaru SET(prt_cfg, PIP_PRT_CFGN_DYN_RS);
94 1.1 hikaru /* INST_HDR=0 */
95 1.1 hikaru /* GRP_WAT=0 */
96 1.1 hikaru SET(prt_cfg, (sc->sc_port << 24) & PIP_PRT_CFGN_QOS);
97 1.1 hikaru /* QOS_WAT=0 */
98 1.1 hikaru /* SPARE=0 */
99 1.1 hikaru /* QOS_DIFF=0 */
100 1.1 hikaru /* QOS_VLAN=0 */
101 1.1 hikaru SET(prt_cfg, PIP_PRT_CFGN_CRC_EN);
102 1.1 hikaru /* SKIP=0 */
103 1.1 hikaru
104 1.1 hikaru prt_tag = 0;
105 1.1 hikaru SET(prt_tag, PIP_PRT_TAGN_INC_PRT);
106 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP6_DPRT);
107 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP4_DPRT);
108 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP6_SPRT);
109 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP4_SPRT);
110 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP6_NXTH);
111 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP4_PCTL);
112 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP6_DST);
113 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP4_SRC);
114 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP6_SRC);
115 1.1 hikaru CLR(prt_tag, PIP_PRT_TAGN_IP4_DST);
116 1.5 simonb SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_TCP6_TAG_ORDERED, PIP_PRT_TAGN_TCP6_TAG));
117 1.5 simonb SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_TCP4_TAG_ORDERED, PIP_PRT_TAGN_TCP4_TAG));
118 1.5 simonb SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_IP6_TAG_ORDERED, PIP_PRT_TAGN_IP6_TAG));
119 1.5 simonb SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_IP4_TAG_ORDERED, PIP_PRT_TAGN_IP4_TAG));
120 1.5 simonb SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_NON_TAG_ORDERED, PIP_PRT_TAGN_NON_TAG));
121 1.1 hikaru SET(prt_tag, sc->sc_receive_group & PIP_PRT_TAGN_GRP);
122 1.1 hikaru
123 1.1 hikaru ip_offset = 0;
124 1.1 hikaru SET(ip_offset, (sc->sc_ip_offset / 8) & PIP_IP_OFFSET_MASK_OFFSET);
125 1.1 hikaru
126 1.1 hikaru _PIP_WR8(sc, PIP_PRT_CFG0_OFFSET + (8 * sc->sc_port), prt_cfg);
127 1.1 hikaru _PIP_WR8(sc, PIP_PRT_TAG0_OFFSET + (8 * sc->sc_port), prt_tag);
128 1.1 hikaru _PIP_WR8(sc, PIP_IP_OFFSET_OFFSET, ip_offset);
129 1.1 hikaru
130 1.1 hikaru return 0;
131 1.1 hikaru }
132 1.1 hikaru
133 1.1 hikaru void
134 1.4 simonb octpip_prt_cfg_enable(struct octpip_softc *sc, uint64_t prt_cfg, int enable)
135 1.1 hikaru {
136 1.1 hikaru uint64_t tmp;
137 1.1 hikaru
138 1.1 hikaru tmp = _PIP_RD8(sc, PIP_PRT_CFG0_OFFSET + (8 * sc->sc_port));
139 1.1 hikaru if (enable)
140 1.1 hikaru tmp |= prt_cfg;
141 1.1 hikaru else
142 1.1 hikaru tmp &= ~prt_cfg;
143 1.1 hikaru _PIP_WR8(sc, PIP_PRT_CFG0_OFFSET + (8 * sc->sc_port), tmp);
144 1.1 hikaru }
145 1.1 hikaru
146 1.1 hikaru void
147 1.4 simonb octpip_stats(struct octpip_softc *sc, struct ifnet *ifp, int gmx_port)
148 1.1 hikaru {
149 1.1 hikaru uint64_t tmp, pkts;
150 1.1 hikaru uint64_t pip_stat_ctl;
151 1.1 hikaru
152 1.1 hikaru if (sc == NULL || ifp == NULL)
153 1.1 hikaru panic("%s: invalid argument. sc=%p, ifp=%p\n", __func__,
154 1.1 hikaru sc, ifp);
155 1.1 hikaru
156 1.1 hikaru if (gmx_port < 0 || gmx_port > 2) {
157 1.1 hikaru printf("%s: invalid gmx_port %d\n", __func__, gmx_port);
158 1.1 hikaru return;
159 1.1 hikaru }
160 1.1 hikaru
161 1.1 hikaru pip_stat_ctl = _PIP_RD8(sc, PIP_STAT_CTL_OFFSET);
162 1.1 hikaru _PIP_WR8(sc, PIP_STAT_CTL_OFFSET, pip_stat_ctl | PIP_STAT_CTL_RDCLR);
163 1.7 simonb tmp = _PIP_RD8(sc, PIP_STAT0_PRT_OFFSET(gmx_port));
164 1.5 simonb pkts = __SHIFTOUT(tmp, PIP_STAT0_PRTN_DRP_PKTS);
165 1.3 thorpej if_statadd(ifp, if_iqdrops, pkts);
166 1.1 hikaru
167 1.1 hikaru _PIP_WR8(sc, PIP_STAT_CTL_OFFSET, pip_stat_ctl);
168 1.1 hikaru }
169