Home | History | Annotate | Line # | Download | only in dev
octeon_pip.c revision 1.6
      1 /*	$NetBSD: octeon_pip.c,v 1.6 2020/06/22 02:26:20 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.6 2020/06/22 02:26:20 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 /*
     46  * register definitions (for debug and statics)
     47  */
     48 #define	_ENTRY(x)	{ #x, x##_BITS, x##_OFFSET }
     49 #define	_ENTRY_0_3(x) \
     50 	_ENTRY(x## 0), _ENTRY(x## 1), _ENTRY(x## 2), _ENTRY(x## 3)
     51 #define	_ENTRY_0_7(x) \
     52 	_ENTRY(x## 0), _ENTRY(x## 1), _ENTRY(x## 2), _ENTRY(x## 3), \
     53 	_ENTRY(x## 4), _ENTRY(x## 5), _ENTRY(x## 6), _ENTRY(x## 7)
     54 #define	_ENTRY_0_1_2_32(x) \
     55 	_ENTRY(x## 0), _ENTRY(x## 1), _ENTRY(x## 2), _ENTRY(x##32)
     56 
     57 struct octpip_dump_reg_ {
     58 	const char *name;
     59 	const char *format;
     60 	size_t	offset;
     61 };
     62 
     63 static const struct octpip_dump_reg_ octpip_dump_stats_[] = {
     64 /* PIP_QOS_DIFF[0-63] */
     65 	_ENTRY_0_1_2_32	(PIP_STAT0_PRT),
     66 	_ENTRY_0_1_2_32	(PIP_STAT1_PRT),
     67 	_ENTRY_0_1_2_32	(PIP_STAT2_PRT),
     68 	_ENTRY_0_1_2_32	(PIP_STAT3_PRT),
     69 	_ENTRY_0_1_2_32	(PIP_STAT4_PRT),
     70 	_ENTRY_0_1_2_32	(PIP_STAT5_PRT),
     71 	_ENTRY_0_1_2_32	(PIP_STAT6_PRT),
     72 	_ENTRY_0_1_2_32	(PIP_STAT7_PRT),
     73 	_ENTRY_0_1_2_32	(PIP_STAT8_PRT),
     74 	_ENTRY_0_1_2_32	(PIP_STAT9_PRT),
     75 /* PIP_TAG_INC[0-63] */
     76 	_ENTRY_0_1_2_32	(PIP_STAT_INB_PKTS),
     77 	_ENTRY_0_1_2_32	(PIP_STAT_INB_OCTS),
     78 	_ENTRY_0_1_2_32	(PIP_STAT_INB_ERRS),
     79 };
     80 
     81 #undef	_ENTRY
     82 #undef	_ENTRY_0_3
     83 #undef	_ENTRY_0_7
     84 #undef	_ENTRY_0_1_2_32
     85 
     86 /* XXX */
     87 void
     88 octpip_init(struct octpip_attach_args *aa, struct octpip_softc **rsc)
     89 {
     90 	struct octpip_softc *sc;
     91 	int status;
     92 
     93 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
     94 	if (sc == NULL)
     95 		panic("can't allocate memory: %s", __func__);
     96 
     97 	sc->sc_port = aa->aa_port;
     98 	sc->sc_regt = aa->aa_regt;
     99 	sc->sc_tag_type = aa->aa_tag_type;
    100 	sc->sc_receive_group = aa->aa_receive_group;
    101 	sc->sc_ip_offset = aa->aa_ip_offset;
    102 
    103 	status = bus_space_map(sc->sc_regt, PIP_BASE, PIP_SIZE, 0,
    104 	    &sc->sc_regh);
    105 	if (status != 0)
    106 		panic("can't map %s space", "pip register");
    107 
    108 	*rsc = sc;
    109 }
    110 
    111 #define	_PIP_RD8(sc, off) \
    112 	bus_space_read_8((sc)->sc_regt, (sc)->sc_regh, (off))
    113 #define	_PIP_WR8(sc, off, v) \
    114 	bus_space_write_8((sc)->sc_regt, (sc)->sc_regh, (off), (v))
    115 
    116 int
    117 octpip_port_config(struct octpip_softc *sc)
    118 {
    119 	uint64_t prt_cfg;
    120 	uint64_t prt_tag;
    121 	uint64_t ip_offset;
    122 
    123 	/*
    124 	 * Process the headers and place the IP header in the work queue
    125 	 */
    126 	prt_cfg = 0;
    127 	if (MIPS_PRID_IMPL(mips_options.mips_cpu_id) == MIPS_CN50XX) {
    128 		SET(prt_cfg, PIP_PRT_CFGN_LENERR_EN);
    129 		SET(prt_cfg, PIP_PRT_CFGN_MAXERR_EN);
    130 		SET(prt_cfg, PIP_PRT_CFGN_MINERR_EN);
    131 	}
    132 	/* RAWDRP=0; don't allow raw packet drop */
    133 	/* TAGINC=0 */
    134 	SET(prt_cfg, PIP_PRT_CFGN_DYN_RS);
    135 	/* INST_HDR=0 */
    136 	/* GRP_WAT=0 */
    137 	SET(prt_cfg, (sc->sc_port << 24) & PIP_PRT_CFGN_QOS);
    138 	/* QOS_WAT=0 */
    139 	/* SPARE=0 */
    140 	/* QOS_DIFF=0 */
    141 	/* QOS_VLAN=0 */
    142 	SET(prt_cfg, PIP_PRT_CFGN_CRC_EN);
    143 	/* SKIP=0 */
    144 
    145 	prt_tag = 0;
    146 	SET(prt_tag, PIP_PRT_TAGN_INC_PRT);
    147 	CLR(prt_tag, PIP_PRT_TAGN_IP6_DPRT);
    148 	CLR(prt_tag, PIP_PRT_TAGN_IP4_DPRT);
    149 	CLR(prt_tag, PIP_PRT_TAGN_IP6_SPRT);
    150 	CLR(prt_tag, PIP_PRT_TAGN_IP4_SPRT);
    151 	CLR(prt_tag, PIP_PRT_TAGN_IP6_NXTH);
    152 	CLR(prt_tag, PIP_PRT_TAGN_IP4_PCTL);
    153 	CLR(prt_tag, PIP_PRT_TAGN_IP6_DST);
    154 	CLR(prt_tag, PIP_PRT_TAGN_IP4_SRC);
    155 	CLR(prt_tag, PIP_PRT_TAGN_IP6_SRC);
    156 	CLR(prt_tag, PIP_PRT_TAGN_IP4_DST);
    157 	SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_TCP6_TAG_ORDERED, PIP_PRT_TAGN_TCP6_TAG));
    158 	SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_TCP4_TAG_ORDERED, PIP_PRT_TAGN_TCP4_TAG));
    159 	SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_IP6_TAG_ORDERED, PIP_PRT_TAGN_IP6_TAG));
    160 	SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_IP4_TAG_ORDERED, PIP_PRT_TAGN_IP4_TAG));
    161 	SET(prt_tag, __SHIFTIN(PIP_PRT_TAGN_NON_TAG_ORDERED, PIP_PRT_TAGN_NON_TAG));
    162 	SET(prt_tag, sc->sc_receive_group & PIP_PRT_TAGN_GRP);
    163 
    164 	ip_offset = 0;
    165 	SET(ip_offset, (sc->sc_ip_offset / 8) & PIP_IP_OFFSET_MASK_OFFSET);
    166 
    167 	_PIP_WR8(sc, PIP_PRT_CFG0_OFFSET + (8 * sc->sc_port), prt_cfg);
    168 	_PIP_WR8(sc, PIP_PRT_TAG0_OFFSET + (8 * sc->sc_port), prt_tag);
    169 	_PIP_WR8(sc, PIP_IP_OFFSET_OFFSET, ip_offset);
    170 
    171 	return 0;
    172 }
    173 
    174 void
    175 octpip_prt_cfg_enable(struct octpip_softc *sc, uint64_t prt_cfg, int enable)
    176 {
    177 	uint64_t tmp;
    178 
    179 	tmp = _PIP_RD8(sc, PIP_PRT_CFG0_OFFSET + (8 * sc->sc_port));
    180 	if (enable)
    181 		tmp |= prt_cfg;
    182 	else
    183 		tmp &= ~prt_cfg;
    184 	_PIP_WR8(sc, PIP_PRT_CFG0_OFFSET + (8 * sc->sc_port), tmp);
    185 }
    186 
    187 void
    188 octpip_stats(struct octpip_softc *sc, struct ifnet *ifp, int gmx_port)
    189 {
    190 	const struct octpip_dump_reg_ *reg;
    191 	uint64_t tmp, pkts;
    192 	uint64_t pip_stat_ctl;
    193 
    194 	if (sc == NULL || ifp == NULL)
    195 		panic("%s: invalid argument. sc=%p, ifp=%p\n", __func__,
    196 			sc, ifp);
    197 
    198 	if (gmx_port < 0 || gmx_port > 2) {
    199 		printf("%s: invalid gmx_port %d\n", __func__, gmx_port);
    200 		return;
    201 	}
    202 
    203 	pip_stat_ctl = _PIP_RD8(sc, PIP_STAT_CTL_OFFSET);
    204 	_PIP_WR8(sc, PIP_STAT_CTL_OFFSET, pip_stat_ctl | PIP_STAT_CTL_RDCLR);
    205 	reg = &octpip_dump_stats_[gmx_port];
    206 	tmp = _PIP_RD8(sc, reg->offset);
    207 	pkts = __SHIFTOUT(tmp, PIP_STAT0_PRTN_DRP_PKTS);
    208 	if_statadd(ifp, if_iqdrops, pkts);
    209 
    210 	_PIP_WR8(sc, PIP_STAT_CTL_OFFSET, pip_stat_ctl);
    211 }
    212