Home | History | Annotate | Line # | Download | only in dev
octeon_pip.c revision 1.5
      1 /*	$NetBSD: octeon_pip.c,v 1.5 2020/06/18 13:52:08 simonb Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007 Internet Initiative Japan, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: octeon_pip.c,v 1.5 2020/06/18 13:52:08 simonb Exp $");
     31 
     32 #include "opt_octeon.h"
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/malloc.h>
     37 #include <sys/syslog.h>
     38 #include <sys/time.h>
     39 #include <net/if.h>
     40 #include <mips/locore.h>
     41 #include <mips/cavium/octeonvar.h>
     42 #include <mips/cavium/dev/octeon_pipreg.h>
     43 #include <mips/cavium/dev/octeon_pipvar.h>
     44 
     45 #ifdef CNMAC_DEBUG
     46 struct octpip_softc *__octpip_softc;
     47 
     48 void			octpip_intr_evcnt_attach(struct octpip_softc *);
     49 void			octpip_intr_rml(void *);
     50 
     51 void			octpip_dump(void);
     52 void			octpip_int_enable(struct octpip_softc *, int);
     53 #endif
     54 
     55 /*
     56  * register definitions (for debug and statics)
     57  */
     58 #define	_ENTRY(x)	{ #x, x##_BITS, x##_OFFSET }
     59 #define	_ENTRY_0_3(x) \
     60 	_ENTRY(x## 0), _ENTRY(x## 1), _ENTRY(x## 2), _ENTRY(x## 3)
     61 #define	_ENTRY_0_7(x) \
     62 	_ENTRY(x## 0), _ENTRY(x## 1), _ENTRY(x## 2), _ENTRY(x## 3), \
     63 	_ENTRY(x## 4), _ENTRY(x## 5), _ENTRY(x## 6), _ENTRY(x## 7)
     64 #define	_ENTRY_0_1_2_32(x) \
     65 	_ENTRY(x## 0), _ENTRY(x## 1), _ENTRY(x## 2), _ENTRY(x##32)
     66 
     67 struct octpip_dump_reg_ {
     68 	const char *name;
     69 	const char *format;
     70 	size_t	offset;
     71 };
     72 
     73 static const struct octpip_dump_reg_ octpip_dump_stats_[] = {
     74 /* PIP_QOS_DIFF[0-63] */
     75 	_ENTRY_0_1_2_32	(PIP_STAT0_PRT),
     76 	_ENTRY_0_1_2_32	(PIP_STAT1_PRT),
     77 	_ENTRY_0_1_2_32	(PIP_STAT2_PRT),
     78 	_ENTRY_0_1_2_32	(PIP_STAT3_PRT),
     79 	_ENTRY_0_1_2_32	(PIP_STAT4_PRT),
     80 	_ENTRY_0_1_2_32	(PIP_STAT5_PRT),
     81 	_ENTRY_0_1_2_32	(PIP_STAT6_PRT),
     82 	_ENTRY_0_1_2_32	(PIP_STAT7_PRT),
     83 	_ENTRY_0_1_2_32	(PIP_STAT8_PRT),
     84 	_ENTRY_0_1_2_32	(PIP_STAT9_PRT),
     85 /* PIP_TAG_INC[0-63] */
     86 	_ENTRY_0_1_2_32	(PIP_STAT_INB_PKTS),
     87 	_ENTRY_0_1_2_32	(PIP_STAT_INB_OCTS),
     88 	_ENTRY_0_1_2_32	(PIP_STAT_INB_ERRS),
     89 };
     90 
     91 #ifdef CNMAC_DEBUG
     92 static const struct octpip_dump_reg_ octpip_dump_regs_[] = {
     93 	_ENTRY		(PIP_BIST_STATUS),
     94 	_ENTRY		(PIP_INT_REG),
     95 	_ENTRY		(PIP_INT_EN),
     96 	_ENTRY		(PIP_STAT_CTL),
     97 	_ENTRY		(PIP_GBL_CTL),
     98 	_ENTRY		(PIP_GBL_CFG),
     99 	_ENTRY		(PIP_SOFT_RST),
    100 	_ENTRY		(PIP_IP_OFFSET),
    101 	_ENTRY		(PIP_TAG_SECRET),
    102 	_ENTRY		(PIP_TAG_MASK),
    103 	_ENTRY_0_3	(PIP_DEC_IPSEC),
    104 	_ENTRY		(PIP_RAW_WORD),
    105 	_ENTRY_0_7	(PIP_QOS_VLAN),
    106 	_ENTRY_0_3	(PIP_QOS_WATCH),
    107 	_ENTRY_0_1_2_32	(PIP_PRT_CFG),
    108 	_ENTRY_0_1_2_32	(PIP_PRT_TAG),
    109 };
    110 #endif
    111 
    112 #undef	_ENTRY
    113 #undef	_ENTRY_0_3
    114 #undef	_ENTRY_0_7
    115 #undef	_ENTRY_0_1_2_32
    116 
    117 /* XXX */
    118 void
    119 octpip_init(struct octpip_attach_args *aa, struct octpip_softc **rsc)
    120 {
    121 	struct octpip_softc *sc;
    122 	int status;
    123 
    124 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
    125 	if (sc == NULL)
    126 		panic("can't allocate memory: %s", __func__);
    127 
    128 	sc->sc_port = aa->aa_port;
    129 	sc->sc_regt = aa->aa_regt;
    130 	sc->sc_tag_type = aa->aa_tag_type;
    131 	sc->sc_receive_group = aa->aa_receive_group;
    132 	sc->sc_ip_offset = aa->aa_ip_offset;
    133 
    134 	status = bus_space_map(sc->sc_regt, PIP_BASE, PIP_SIZE, 0,
    135 	    &sc->sc_regh);
    136 	if (status != 0)
    137 		panic("can't map %s space", "pip register");
    138 
    139 	*rsc = sc;
    140 
    141 #ifdef CNMAC_DEBUG
    142 	octpip_int_enable(sc, 1);
    143 	octpip_intr_evcnt_attach(sc);
    144 	__octpip_softc = sc;
    145 	printf("PIP Code initialized.\n");
    146 #endif
    147 }
    148 
    149 #define	_PIP_RD8(sc, off) \
    150 	bus_space_read_8((sc)->sc_regt, (sc)->sc_regh, (off))
    151 #define	_PIP_WR8(sc, off, v) \
    152 	bus_space_write_8((sc)->sc_regt, (sc)->sc_regh, (off), (v))
    153 
    154 int
    155 octpip_port_config(struct octpip_softc *sc)
    156 {
    157 	uint64_t prt_cfg;
    158 	uint64_t prt_tag;
    159 	uint64_t ip_offset;
    160 
    161 	/*
    162 	 * Process the headers and place the IP header in the work queue
    163 	 */
    164 	prt_cfg = 0;
    165 	if (MIPS_PRID_IMPL(mips_options.mips_cpu_id) == MIPS_CN50XX) {
    166 		SET(prt_cfg, PIP_PRT_CFGN_LENERR_EN);
    167 		SET(prt_cfg, PIP_PRT_CFGN_MAXERR_EN);
    168 		SET(prt_cfg, PIP_PRT_CFGN_MINERR_EN);
    169 	}
    170 	/* RAWDRP=0; don't allow raw packet drop */
    171 	/* TAGINC=0 */
    172 	SET(prt_cfg, PIP_PRT_CFGN_DYN_RS);
    173 	/* INST_HDR=0 */
    174 	/* GRP_WAT=0 */
    175 	SET(prt_cfg, (sc->sc_port << 24) & PIP_PRT_CFGN_QOS);
    176 	/* QOS_WAT=0 */
    177 	/* SPARE=0 */
    178 	/* QOS_DIFF=0 */
    179 	/* QOS_VLAN=0 */
    180 	SET(prt_cfg, PIP_PRT_CFGN_CRC_EN);
    181 	/* SKIP=0 */
    182 
    183 	prt_tag = 0;
    184 	SET(prt_tag, PIP_PRT_TAGN_INC_PRT);
    185 	CLR(prt_tag, PIP_PRT_TAGN_IP6_DPRT);
    186 	CLR(prt_tag, PIP_PRT_TAGN_IP4_DPRT);
    187 	CLR(prt_tag, PIP_PRT_TAGN_IP6_SPRT);
    188 	CLR(prt_tag, PIP_PRT_TAGN_IP4_SPRT);
    189 	CLR(prt_tag, PIP_PRT_TAGN_IP6_NXTH);
    190 	CLR(prt_tag, PIP_PRT_TAGN_IP4_PCTL);
    191 	CLR(prt_tag, PIP_PRT_TAGN_IP6_DST);
    192 	CLR(prt_tag, PIP_PRT_TAGN_IP4_SRC);
    193 	CLR(prt_tag, PIP_PRT_TAGN_IP6_SRC);
    194 	CLR(prt_tag, PIP_PRT_TAGN_IP4_DST);
    195 	SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_TCP6_TAG_ORDERED, PIP_PRT_TAGN_TCP6_TAG));
    196 	SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_TCP4_TAG_ORDERED, PIP_PRT_TAGN_TCP4_TAG));
    197 	SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_IP6_TAG_ORDERED, PIP_PRT_TAGN_IP6_TAG));
    198 	SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_IP4_TAG_ORDERED, PIP_PRT_TAGN_IP4_TAG));
    199 	SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_NON_TAG_ORDERED, PIP_PRT_TAGN_NON_TAG));
    200 	SET(prt_tag, sc->sc_receive_group & PIP_PRT_TAGN_GRP);
    201 
    202 	ip_offset = 0;
    203 	SET(ip_offset, (sc->sc_ip_offset / 8) & PIP_IP_OFFSET_MASK_OFFSET);
    204 
    205 	_PIP_WR8(sc, PIP_PRT_CFG0_OFFSET + (8 * sc->sc_port), prt_cfg);
    206 	_PIP_WR8(sc, PIP_PRT_TAG0_OFFSET + (8 * sc->sc_port), prt_tag);
    207 	_PIP_WR8(sc, PIP_IP_OFFSET_OFFSET, ip_offset);
    208 
    209 	return 0;
    210 }
    211 
    212 void
    213 octpip_prt_cfg_enable(struct octpip_softc *sc, uint64_t prt_cfg, int enable)
    214 {
    215 	uint64_t tmp;
    216 
    217 	tmp = _PIP_RD8(sc, PIP_PRT_CFG0_OFFSET + (8 * sc->sc_port));
    218 	if (enable)
    219 		tmp |= prt_cfg;
    220 	else
    221 		tmp &= ~prt_cfg;
    222 	_PIP_WR8(sc, PIP_PRT_CFG0_OFFSET + (8 * sc->sc_port), tmp);
    223 }
    224 
    225 void
    226 octpip_stats(struct octpip_softc *sc, struct ifnet *ifp, int gmx_port)
    227 {
    228 	const struct octpip_dump_reg_ *reg;
    229 	uint64_t tmp, pkts;
    230 	uint64_t pip_stat_ctl;
    231 
    232 	if (sc == NULL || ifp == NULL)
    233 		panic("%s: invalid argument. sc=%p, ifp=%p\n", __func__,
    234 			sc, ifp);
    235 
    236 	if (gmx_port < 0 || gmx_port > 2) {
    237 		printf("%s: invalid gmx_port %d\n", __func__, gmx_port);
    238 		return;
    239 	}
    240 
    241 	pip_stat_ctl = _PIP_RD8(sc, PIP_STAT_CTL_OFFSET);
    242 	_PIP_WR8(sc, PIP_STAT_CTL_OFFSET, pip_stat_ctl | PIP_STAT_CTL_RDCLR);
    243 	reg = &octpip_dump_stats_[gmx_port];
    244 	tmp = _PIP_RD8(sc, reg->offset);
    245 	pkts = __SHIFTOUT(tmp, PIP_STAT0_PRTN_DRP_PKTS);
    246 	if_statadd(ifp, if_iqdrops, pkts);
    247 
    248 	_PIP_WR8(sc, PIP_STAT_CTL_OFFSET, pip_stat_ctl);
    249 }
    250 
    251 
    252 #ifdef CNMAC_DEBUG
    253 int			octpip_intr_rml_verbose;
    254 struct evcnt		octpip_intr_evcnt;
    255 
    256 static const struct octeon_evcnt_entry octpip_intr_evcnt_entries[] = {
    257 #define	_ENTRY(name, type, parent, descr) \
    258 	OCTEON_EVCNT_ENTRY(struct octpip_softc, name, type, parent, descr)
    259 	_ENTRY(pipbeperr,	MISC, NULL, "pip parity error backend"),
    260 	_ENTRY(pipfeperr,	MISC, NULL, "pip parity error frontend"),
    261 	_ENTRY(pipskprunt,	MISC, NULL, "pip skiper"),
    262 	_ENTRY(pipbadtag,	MISC, NULL, "pip bad tag"),
    263 	_ENTRY(pipprtnxa,	MISC, NULL, "pip nonexistent port"),
    264 	_ENTRY(pippktdrp,	MISC, NULL, "pip qos drop"),
    265 #undef	_ENTRY
    266 };
    267 
    268 void
    269 octpip_intr_evcnt_attach(struct octpip_softc *sc)
    270 {
    271 	OCTEON_EVCNT_ATTACH_EVCNTS(sc, octpip_intr_evcnt_entries, "pip0");
    272 }
    273 
    274 void
    275 octpip_intr_rml(void *arg)
    276 {
    277 	struct octpip_softc *sc;
    278 	uint64_t reg;
    279 
    280 	octpip_intr_evcnt.ev_count++;
    281 	sc = __octpip_softc;
    282 	KASSERT(sc != NULL);
    283 	reg = octpip_int_summary(sc);
    284 	if (octpip_intr_rml_verbose)
    285 		printf("%s: PIP_INT_REG=0x%016" PRIx64 "\n", __func__, reg);
    286 	if (reg & PIP_INT_REG_BEPERR)
    287 		OCTEON_EVCNT_INC(sc, pipbeperr);
    288 	if (reg & PIP_INT_REG_FEPERR)
    289 		OCTEON_EVCNT_INC(sc, pipfeperr);
    290 	if (reg & PIP_INT_REG_SKPRUNT)
    291 		OCTEON_EVCNT_INC(sc, pipskprunt);
    292 	if (reg & PIP_INT_REG_BADTAG)
    293 		OCTEON_EVCNT_INC(sc, pipbadtag);
    294 	if (reg & PIP_INT_REG_PRTNXA)
    295 		OCTEON_EVCNT_INC(sc, pipprtnxa);
    296 	if (reg & PIP_INT_REG_PKTDRP)
    297 		OCTEON_EVCNT_INC(sc, pippktdrp);
    298 }
    299 
    300 void		octpip_dump_regs(void);
    301 void		octpip_dump_stats(void);
    302 
    303 void
    304 octpip_dump(void)
    305 {
    306 	octpip_dump_regs();
    307 	octpip_dump_stats();
    308 }
    309 
    310 void
    311 octpip_dump_regs(void)
    312 {
    313 	struct octpip_softc *sc = __octpip_softc;
    314 	const struct octpip_dump_reg_ *reg;
    315 	uint64_t tmp;
    316 	char buf[512];
    317 	int i;
    318 
    319 	for (i = 0; i < (int)__arraycount(octpip_dump_regs_); i++) {
    320 		reg = &octpip_dump_regs_[i];
    321 		tmp = _PIP_RD8(sc, reg->offset);
    322 		if (reg->format == NULL) {
    323 			snprintf(buf, sizeof(buf), "%16" PRIx64, tmp);
    324 		} else {
    325 			snprintb(buf, sizeof(buf), reg->format, tmp);
    326 		}
    327 		printf("\t%-24s: %s\n", reg->name, buf);
    328 	}
    329 }
    330 
    331 void
    332 octpip_dump_stats(void)
    333 {
    334 	struct octpip_softc *sc = __octpip_softc;
    335 	const struct octpip_dump_reg_ *reg;
    336 	uint64_t tmp;
    337 	char buf[512];
    338 	int i;
    339 	uint64_t pip_stat_ctl;
    340 
    341 	pip_stat_ctl = _PIP_RD8(sc, PIP_STAT_CTL_OFFSET);
    342 	_PIP_WR8(sc, PIP_STAT_CTL_OFFSET, pip_stat_ctl & ~PIP_STAT_CTL_RDCLR);
    343 	for (i = 0; i < (int)__arraycount(octpip_dump_stats_); i++) {
    344 		reg = &octpip_dump_stats_[i];
    345 		tmp = _PIP_RD8(sc, reg->offset);
    346 		if (reg->format == NULL) {
    347 			snprintf(buf, sizeof(buf), "%16" PRIx64, tmp);
    348 		} else {
    349 			snprintb(buf, sizeof(buf), reg->format, tmp);
    350 		}
    351 		printf("\t%-24s: %s\n", reg->name, buf);
    352 	}
    353 	printf("\t%-24s:\n", "PIP_QOS_DIFF[0-63]");
    354 	for (i = 0; i < 64; i++) {
    355 		tmp = _PIP_RD8(sc, PIP_QOS_DIFF0_OFFSET + sizeof(uint64_t) * i);
    356 		snprintf(buf, sizeof(buf), "%16" PRIx64, tmp);
    357 		printf("%s\t%s%s",
    358 		    ((i % 4) == 0) ? "\t" : "",
    359 		    buf,
    360 		    ((i % 4) == 3) ? "\n" : "");
    361 	}
    362 	printf("\t%-24s:\n", "PIP_TAG_INC[0-63]");
    363 	for (i = 0; i < 64; i++) {
    364 		tmp = _PIP_RD8(sc, PIP_TAG_INC0_OFFSET + sizeof(uint64_t) * i);
    365 		snprintf(buf, sizeof(buf), "%16" PRIx64, tmp);
    366 		printf("%s\t%s%s",
    367 		    ((i % 4) == 0) ? "\t" : "",
    368 		    buf,
    369 		    ((i % 4) == 3) ? "\n" : "");
    370 	}
    371 	_PIP_WR8(sc, PIP_STAT_CTL_OFFSET, pip_stat_ctl);
    372 }
    373 
    374 void
    375 octpip_int_enable(struct octpip_softc *sc, int enable)
    376 {
    377 	uint64_t pip_int_xxx = 0;
    378 
    379 	SET(pip_int_xxx,
    380 	    PIP_INT_EN_BEPERR |
    381 	    PIP_INT_EN_FEPERR |
    382 	    PIP_INT_EN_SKPRUNT |
    383 	    PIP_INT_EN_BADTAG |
    384 	    PIP_INT_EN_PRTNXA |
    385 	    PIP_INT_EN_PKTDRP);
    386 	_PIP_WR8(sc, PIP_INT_REG_OFFSET, pip_int_xxx);
    387 	_PIP_WR8(sc, PIP_INT_EN_OFFSET, enable ? pip_int_xxx : 0);
    388 }
    389 uint64_t
    390 octpip_int_summary(struct octpip_softc *sc)
    391 {
    392 	uint64_t summary;
    393 
    394 	summary = _PIP_RD8(sc, PIP_INT_REG_OFFSET);
    395 	_PIP_WR8(sc, PIP_INT_REG_OFFSET, summary);
    396 	return summary;
    397 }
    398 #endif /* CNMAC_DEBUG */
    399