Home | History | Annotate | Line # | Download | only in dev
if_cnmac.c revision 1.19
      1 /*	$NetBSD: if_cnmac.c,v 1.19 2020/05/31 06:27:06 simonb Exp $	*/
      2 
      3 #include <sys/cdefs.h>
      4 #if 0
      5 __KERNEL_RCSID(0, "$NetBSD: if_cnmac.c,v 1.19 2020/05/31 06:27:06 simonb Exp $");
      6 #endif
      7 
      8 #include "opt_octeon.h"
      9 
     10 #ifdef	CNMAC_DEBUG
     11 
     12 #ifndef DIAGNOSTIC
     13 #define	DIAGNOSTIC
     14 #endif
     15 
     16 #ifndef DEBUG
     17 #define	DEBUG
     18 #endif
     19 
     20 #endif
     21 
     22 /*
     23  * If no free send buffer is available, free all the sent buffers and bail out.
     24  */
     25 #define CNMAC_SEND_QUEUE_CHECK
     26 
     27 /* XXX XXX XXX XXX XXX XXX */
     28 
     29 #include <sys/param.h>
     30 #include <sys/systm.h>
     31 #include <sys/pool.h>
     32 #include <sys/mbuf.h>
     33 #include <sys/malloc.h>
     34 #include <sys/kernel.h>
     35 #include <sys/socket.h>
     36 #include <sys/ioctl.h>
     37 #include <sys/errno.h>
     38 #include <sys/device.h>
     39 #include <sys/queue.h>
     40 #include <sys/conf.h>
     41 #include <sys/sysctl.h>
     42 #include <sys/syslog.h>
     43 
     44 #include <net/if.h>
     45 #include <net/if_dl.h>
     46 #include <net/if_media.h>
     47 #include <net/if_ether.h>
     48 #include <net/route.h>
     49 #include <net/bpf.h>
     50 
     51 #include <netinet/in.h>
     52 #include <netinet/in_systm.h>
     53 #include <netinet/in_var.h>
     54 #include <netinet/ip.h>
     55 
     56 #include <sys/bus.h>
     57 #include <machine/intr.h>
     58 #include <machine/endian.h>
     59 #include <machine/locore.h>
     60 
     61 #include <dev/mii/mii.h>
     62 #include <dev/mii/miivar.h>
     63 
     64 #include <mips/cpuregs.h>
     65 
     66 #include <mips/cavium/dev/octeon_asxreg.h>
     67 #include <mips/cavium/dev/octeon_ciureg.h>
     68 #include <mips/cavium/dev/octeon_npireg.h>
     69 #include <mips/cavium/dev/octeon_gmxreg.h>
     70 #include <mips/cavium/dev/octeon_ipdreg.h>
     71 #include <mips/cavium/dev/octeon_pipreg.h>
     72 #include <mips/cavium/dev/octeon_powreg.h>
     73 #include <mips/cavium/dev/octeon_faureg.h>
     74 #include <mips/cavium/dev/octeon_fpareg.h>
     75 #include <mips/cavium/dev/octeon_bootbusreg.h>
     76 #include <mips/cavium/include/iobusvar.h>
     77 #include <mips/cavium/octeonvar.h>
     78 #include <mips/cavium/dev/octeon_fpavar.h>
     79 #include <mips/cavium/dev/octeon_gmxvar.h>
     80 #include <mips/cavium/dev/octeon_fauvar.h>
     81 #include <mips/cavium/dev/octeon_powvar.h>
     82 #include <mips/cavium/dev/octeon_ipdvar.h>
     83 #include <mips/cavium/dev/octeon_pipvar.h>
     84 #include <mips/cavium/dev/octeon_pkovar.h>
     85 #include <mips/cavium/dev/octeon_asxvar.h>
     86 #include <mips/cavium/dev/octeon_smivar.h>
     87 #include <mips/cavium/dev/if_cnmacvar.h>
     88 
     89 #ifdef CNMAC_DEBUG
     90 #define	CNMAC_KASSERT(x)	KASSERT(x)
     91 #define	CNMAC_KDASSERT(x)	KDASSERT(x)
     92 #else
     93 #define	CNMAC_KASSERT(x)
     94 #define	CNMAC_KDASSERT(x)
     95 #endif
     96 
     97 /*
     98  * Set the PKO to think command buffers are an odd length.  This makes it so we
     99  * never have to divide a comamnd across two buffers.
    100  */
    101 #define OCTEON_POOL_NWORDS_CMD	\
    102 	    (((uint32_t)OCTEON_POOL_SIZE_CMD / sizeof(uint64_t)) - 1)
    103 #define FPA_COMMAND_BUFFER_POOL_NWORDS	OCTEON_POOL_NWORDS_CMD	/* XXX */
    104 
    105 static void	cnmac_buf_init(struct cnmac_softc *);
    106 
    107 static int	cnmac_match(device_t, struct cfdata *, void *);
    108 static void	cnmac_attach(device_t, device_t, void *);
    109 static void	cnmac_pip_init(struct cnmac_softc *);
    110 static void	cnmac_ipd_init(struct cnmac_softc *);
    111 static void	cnmac_pko_init(struct cnmac_softc *);
    112 static void	cnmac_asx_init(struct cnmac_softc *);
    113 static void	cnmac_smi_init(struct cnmac_softc *);
    114 
    115 static void	cnmac_board_mac_addr(uint8_t *, size_t, struct cnmac_softc *);
    116 
    117 static int	cnmac_mii_readreg(device_t, int, int, uint16_t *);
    118 static int	cnmac_mii_writereg(device_t, int, int, uint16_t);
    119 static void	cnmac_mii_statchg(struct ifnet *);
    120 
    121 static int	cnmac_mediainit(struct cnmac_softc *);
    122 static void	cnmac_mediastatus(struct ifnet *, struct ifmediareq *);
    123 
    124 static inline void cnmac_send_queue_flush_prefetch(struct cnmac_softc *);
    125 static inline void cnmac_send_queue_flush_fetch(struct cnmac_softc *);
    126 static inline void cnmac_send_queue_flush(struct cnmac_softc *);
    127 static inline void cnmac_send_queue_flush_sync(struct cnmac_softc *);
    128 static inline int cnmac_send_queue_is_full(struct cnmac_softc *);
    129 static inline void cnmac_send_queue_add(struct cnmac_softc *, struct mbuf *,
    130     uint64_t *);
    131 static inline void cnmac_send_queue_del(struct cnmac_softc *, struct mbuf **,
    132     uint64_t **);
    133 static inline int cnmac_buf_free_work(struct cnmac_softc *, uint64_t *,
    134     uint64_t);
    135 static inline void cnmac_buf_ext_free_m(struct mbuf *, void *, size_t, void *);
    136 static inline void cnmac_buf_ext_free_ext(struct mbuf *, void *, size_t,
    137     void *);
    138 
    139 static int	cnmac_ioctl(struct ifnet *, u_long, void *);
    140 static void	cnmac_watchdog(struct ifnet *);
    141 static int	cnmac_init(struct ifnet *);
    142 static void	cnmac_stop(struct ifnet *, int);
    143 static void	cnmac_start(struct ifnet *);
    144 
    145 static inline int cnmac_send_cmd(struct cnmac_softc *, uint64_t, uint64_t,
    146     int *);
    147 static inline uint64_t	cnmac_send_makecmd_w1(int, paddr_t);
    148 static inline uint64_t	cnmac_send_makecmd_w0(uint64_t, uint64_t, size_t, int);
    149 static inline int cnmac_send_makecmd_gbuf(struct cnmac_softc *, struct mbuf *,
    150     uint64_t *, int *);
    151 static inline int cnmac_send_makecmd(struct cnmac_softc *, struct mbuf *,
    152     uint64_t *, uint64_t *, uint64_t *);
    153 static inline int cnmac_send_buf(struct cnmac_softc *, struct mbuf *,
    154     uint64_t *, int *);
    155 static inline int cnmac_send(struct cnmac_softc *, struct mbuf *, int *);
    156 
    157 static int	cnmac_reset(struct cnmac_softc *);
    158 static int	cnmac_configure(struct cnmac_softc *);
    159 static int	cnmac_configure_common(struct cnmac_softc *);
    160 
    161 static void	cnmac_tick_free(void *);
    162 static void	cnmac_tick_misc(void *);
    163 
    164 static inline int cnmac_recv_mbuf(struct cnmac_softc *, uint64_t *,
    165     struct mbuf **);
    166 static inline int cnmac_recv_check_code(struct cnmac_softc *, uint64_t);
    167 static inline int cnmac_recv_check_jumbo(struct cnmac_softc *, uint64_t);
    168 static inline int cnmac_recv_check_link(struct cnmac_softc *, uint64_t);
    169 static inline int cnmac_recv_check(struct cnmac_softc *, uint64_t);
    170 static inline int cnmac_recv(struct cnmac_softc *, uint64_t *);
    171 static void	cnmac_recv_redir(struct ifnet *, struct mbuf *);
    172 static inline void cnmac_recv_intr(void *, uint64_t *);
    173 
    174 /* Device driver context */
    175 static struct	cnmac_softc *cnmac_gsc[GMX_PORT_NUNITS];
    176 static void	*cnmac_pow_recv_ih;
    177 
    178 /* sysctl'able parameters */
    179 int		cnmac_param_pko_cmd_w0_n2 = 1;
    180 int		cnmac_param_pip_dyn_rs = 1;
    181 int		cnmac_param_redir = 0;
    182 int		cnmac_param_pktbuf = 0;
    183 int		cnmac_param_rate = 0;
    184 int		cnmac_param_intr = 0;
    185 
    186 CFATTACH_DECL_NEW(cnmac, sizeof(struct cnmac_softc),
    187     cnmac_match, cnmac_attach, NULL, NULL);
    188 
    189 #ifdef CNMAC_DEBUG
    190 
    191 static const struct octeon_evcnt_entry octeon_evcnt_entries[] = {
    192 #define	_ENTRY(name, type, parent, descr) \
    193 	OCTEON_EVCNT_ENTRY(struct cnmac_softc, name, type, parent, descr)
    194 	_ENTRY(rx,			MISC, NULL, "rx"),
    195 	_ENTRY(rxint,			INTR, NULL, "rx intr"),
    196 	_ENTRY(rxrs,			MISC, NULL, "rx dynamic short"),
    197 	_ENTRY(rxbufpkalloc,		MISC, NULL, "rx buf pkt alloc"),
    198 	_ENTRY(rxbufpkput,		MISC, NULL, "rx buf pkt put"),
    199 	_ENTRY(rxbufwqalloc,		MISC, NULL, "rx buf wqe alloc"),
    200 	_ENTRY(rxbufwqput,		MISC, NULL, "rx buf wqe put"),
    201 	_ENTRY(rxerrcode,		MISC, NULL, "rx code error"),
    202 	_ENTRY(rxerrfix,		MISC, NULL, "rx fixup error"),
    203 	_ENTRY(rxerrjmb,		MISC, NULL, "rx jmb error"),
    204 	_ENTRY(rxerrlink,		MISC, NULL, "rx link error"),
    205 	_ENTRY(rxerroff,		MISC, NULL, "rx offload error"),
    206 	_ENTRY(rxonperrshort,		MISC, NULL, "rx onp fixup short error"),
    207 	_ENTRY(rxonperrpreamble,	MISC, NULL, "rx onp fixup preamble error"),
    208 	_ENTRY(rxonperrcrc,		MISC, NULL, "rx onp fixup crc error"),
    209 	_ENTRY(rxonperraddress,		MISC, NULL, "rx onp fixup address error"),
    210 	_ENTRY(rxonponp,		MISC, NULL, "rx onp fixup onp packets"),
    211 	_ENTRY(rxonpok,			MISC, NULL, "rx onp fixup success packets"),
    212 	_ENTRY(tx,			MISC, NULL, "tx"),
    213 	_ENTRY(txadd,			MISC, NULL, "tx add"),
    214 	_ENTRY(txbufcballoc,		MISC, NULL, "tx buf cb alloc"),
    215 	_ENTRY(txbufcbget,		MISC, NULL, "tx buf cb get"),
    216 	_ENTRY(txbufgballoc,		MISC, NULL, "tx buf gb alloc"),
    217 	_ENTRY(txbufgbget,		MISC, NULL, "tx buf gb get"),
    218 	_ENTRY(txbufgbput,		MISC, NULL, "tx buf gb put"),
    219 	_ENTRY(txdel,			MISC, NULL, "tx del"),
    220 	_ENTRY(txerr,			MISC, NULL, "tx error"),
    221 	_ENTRY(txerrcmd,		MISC, NULL, "tx cmd error"),
    222 	_ENTRY(txerrgbuf,		MISC, NULL, "tx gbuf error"),
    223 	_ENTRY(txerrlink,		MISC, NULL, "tx link error"),
    224 	_ENTRY(txerrmkcmd,		MISC, NULL, "tx makecmd error"),
    225 #undef	_ENTRY
    226 };
    227 #endif
    228 
    229 /* ---- buffer management */
    230 
    231 static const struct cnmac_pool_param {
    232 	int			poolno;
    233 	size_t			size;
    234 	size_t			nelems;
    235 } cnmac_pool_params[] = {
    236 #define	_ENTRY(x)	{ OCTEON_POOL_NO_##x, OCTEON_POOL_SIZE_##x, OCTEON_POOL_NELEMS_##x }
    237 	_ENTRY(PKT),
    238 	_ENTRY(WQE),
    239 	_ENTRY(CMD),
    240 	_ENTRY(SG)
    241 #undef	_ENTRY
    242 };
    243 struct octfpa_buf	*cnmac_pools[8/* XXX */];
    244 #define	cnmac_fb_pkt	cnmac_pools[OCTEON_POOL_NO_PKT]
    245 #define	cnmac_fb_wqe	cnmac_pools[OCTEON_POOL_NO_WQE]
    246 #define	cnmac_fb_cmd	cnmac_pools[OCTEON_POOL_NO_CMD]
    247 #define	cnmac_fb_sg	cnmac_pools[OCTEON_POOL_NO_SG]
    248 
    249 static void
    250 cnmac_buf_init(struct cnmac_softc *sc)
    251 {
    252 	static int once;
    253 	int i;
    254 	const struct cnmac_pool_param *pp;
    255 	struct octfpa_buf *fb;
    256 
    257 	if (once == 1)
    258 		return;
    259 	once = 1;
    260 
    261 	for (i = 0; i < (int)__arraycount(cnmac_pool_params); i++) {
    262 		pp = &cnmac_pool_params[i];
    263 		octfpa_buf_init(pp->poolno, pp->size, pp->nelems, &fb);
    264 		cnmac_pools[i] = fb;
    265 	}
    266 }
    267 
    268 /* ---- autoconf */
    269 
    270 static int
    271 cnmac_match(device_t parent, struct cfdata *match, void *aux)
    272 {
    273 	struct octgmx_attach_args *ga = aux;
    274 
    275 	if (strcmp(match->cf_name, ga->ga_name) != 0) {
    276 		return 0;
    277 	}
    278 	return 1;
    279 }
    280 
    281 static void
    282 cnmac_attach(device_t parent, device_t self, void *aux)
    283 {
    284 	struct cnmac_softc *sc = device_private(self);
    285 	struct octgmx_attach_args *ga = aux;
    286 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    287 	prop_dictionary_t dict;
    288 	prop_object_t clk;
    289 	uint8_t enaddr[ETHER_ADDR_LEN];
    290 
    291 	sc->sc_dev = self;
    292 	sc->sc_regt = ga->ga_regt;
    293 	sc->sc_port = ga->ga_portno;
    294 	sc->sc_port_type = ga->ga_port_type;
    295 	sc->sc_gmx = ga->ga_gmx;
    296 	sc->sc_gmx_port = ga->ga_gmx_port;
    297 
    298 	sc->sc_init_flag = 0;
    299 	/*
    300 	 * XXXUEBAYASI
    301 	 * Setting PIP_IP_OFFSET[OFFSET] to 8 causes panic ... why???
    302 	 */
    303 	sc->sc_ip_offset = 0/* XXX */;
    304 
    305 	if (MIPS_PRID_IMPL(mips_options.mips_cpu_id) <= MIPS_CN30XX) {
    306 		SET(sc->sc_quirks, CNMAC_QUIRKS_NO_PRE_ALIGN);
    307 		SET(sc->sc_quirks, CNMAC_QUIRKS_NO_RX_INBND);
    308 	}
    309 
    310 	cnmac_board_mac_addr(enaddr, sizeof(enaddr), sc);
    311 	printf("%s: Ethernet address %s\n", device_xname(sc->sc_dev),
    312 	    ether_sprintf(enaddr));
    313 
    314 	cnmac_gsc[sc->sc_port] = sc;
    315 
    316 	SIMPLEQ_INIT(&sc->sc_sendq);
    317 	sc->sc_soft_req_thresh = 15/* XXX */;
    318 	sc->sc_ext_callback_cnt = 0;
    319 
    320 	octgmx_stats_init(sc->sc_gmx_port);
    321 
    322 	callout_init(&sc->sc_tick_misc_ch, 0);
    323 	callout_init(&sc->sc_tick_free_ch, 0);
    324 
    325 	octfau_op_init(&sc->sc_fau_done,
    326 	    OCTEON_CVMSEG_ETHER_OFFSET(sc->sc_port, csm_ether_fau_done),
    327 	    OCT_FAU_REG_ADDR_END - (8 * (sc->sc_port + 1))/* XXX */);
    328 	octfau_op_set_8(&sc->sc_fau_done, 0);
    329 
    330 	cnmac_pip_init(sc);
    331 	cnmac_ipd_init(sc);
    332 	cnmac_pko_init(sc);
    333 	cnmac_asx_init(sc);
    334 	cnmac_smi_init(sc);
    335 
    336 	sc->sc_gmx_port->sc_ipd = sc->sc_ipd;
    337 	sc->sc_gmx_port->sc_port_asx = sc->sc_asx;
    338 	sc->sc_gmx_port->sc_port_mii = &sc->sc_mii;
    339 	sc->sc_gmx_port->sc_port_ec = &sc->sc_ethercom;
    340 	/* XXX */
    341 	sc->sc_gmx_port->sc_quirks = sc->sc_quirks;
    342 
    343 	/* XXX */
    344 	sc->sc_pow = &octpow_softc;
    345 
    346 	cnmac_mediainit(sc);
    347 
    348 	strncpy(ifp->if_xname, device_xname(sc->sc_dev), sizeof(ifp->if_xname));
    349 	ifp->if_softc = sc;
    350 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    351 	ifp->if_ioctl = cnmac_ioctl;
    352 	ifp->if_start = cnmac_start;
    353 	ifp->if_watchdog = cnmac_watchdog;
    354 	ifp->if_init = cnmac_init;
    355 	ifp->if_stop = cnmac_stop;
    356 	IFQ_SET_MAXLEN(&ifp->if_snd, uimax(GATHER_QUEUE_SIZE, IFQ_MAXLEN));
    357 	IFQ_SET_READY(&ifp->if_snd);
    358 
    359 	/* XXX: not yet tx checksum */
    360 	ifp->if_capabilities =
    361 	    IFCAP_CSUM_IPv4_Rx | IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
    362 	    IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
    363 
    364 	/* 802.1Q VLAN-sized frames are supported */
    365 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    366 
    367 	octgmx_set_mac_addr(sc->sc_gmx_port, enaddr);
    368 
    369 	if_attach(ifp);
    370 	ether_ifattach(ifp, enaddr);
    371 	octgmx_set_filter(sc->sc_gmx_port);
    372 
    373 	/* XXX */
    374 	sc->sc_rate_recv_check_link_cap.tv_sec = 1;
    375 	sc->sc_rate_recv_check_jumbo_cap.tv_sec = 1;
    376 	sc->sc_rate_recv_check_code_cap.tv_sec = 1;
    377 	sc->sc_rate_recv_fixup_odd_nibble_short_cap.tv_sec = 1;
    378 	sc->sc_rate_recv_fixup_odd_nibble_preamble_cap.tv_sec = 1;
    379 	sc->sc_rate_recv_fixup_odd_nibble_crc_cap.tv_sec = 1;
    380 #ifdef CNMAC_DEBUG
    381 	sc->sc_rate_recv_fixup_odd_nibble_addr_cap.tv_sec = 1;
    382 #endif
    383 	/* XXX */
    384 
    385 #if 1
    386 	cnmac_buf_init(sc);
    387 #endif
    388 
    389 	if (cnmac_pow_recv_ih == NULL)
    390 		cnmac_pow_recv_ih
    391 		    = octpow_intr_establish(OCTEON_POW_GROUP_PIP,
    392 			IPL_NET, cnmac_recv_intr, NULL, NULL);
    393 
    394 	OCTEON_EVCNT_ATTACH_EVCNTS(sc, octeon_evcnt_entries,
    395 	    device_xname(sc->sc_dev));
    396 
    397 	dict = device_properties(sc->sc_gmx->sc_dev);
    398 
    399 	clk = prop_dictionary_get(dict, "rgmii-tx");
    400 	KASSERT(clk != NULL);
    401 	sc->sc_gmx_port->sc_clk_tx_setting = prop_number_integer_value(clk);
    402 	clk = prop_dictionary_get(dict, "rgmii-rx");
    403 	KASSERT(clk != NULL);
    404 	sc->sc_gmx_port->sc_clk_rx_setting = prop_number_integer_value(clk);
    405 }
    406 
    407 /* ---- submodules */
    408 
    409 /* XXX */
    410 static void
    411 cnmac_pip_init(struct cnmac_softc *sc)
    412 {
    413 	struct octpip_attach_args pip_aa;
    414 
    415 	pip_aa.aa_port = sc->sc_port;
    416 	pip_aa.aa_regt = sc->sc_regt;
    417 	pip_aa.aa_tag_type = POW_TAG_TYPE_ORDERED/* XXX */;
    418 	pip_aa.aa_receive_group = OCTEON_POW_GROUP_PIP;
    419 	pip_aa.aa_ip_offset = sc->sc_ip_offset;
    420 	octpip_init(&pip_aa, &sc->sc_pip);
    421 }
    422 
    423 /* XXX */
    424 static void
    425 cnmac_ipd_init(struct cnmac_softc *sc)
    426 {
    427 	struct octipd_attach_args ipd_aa;
    428 
    429 	ipd_aa.aa_port = sc->sc_port;
    430 	ipd_aa.aa_regt = sc->sc_regt;
    431 	ipd_aa.aa_first_mbuff_skip = 184/* XXX */;
    432 	ipd_aa.aa_not_first_mbuff_skip = 0/* XXX */;
    433 	octipd_init(&ipd_aa, &sc->sc_ipd);
    434 }
    435 
    436 /* XXX */
    437 static void
    438 cnmac_pko_init(struct cnmac_softc *sc)
    439 {
    440 	struct octpko_attach_args pko_aa;
    441 
    442 	pko_aa.aa_port = sc->sc_port;
    443 	pko_aa.aa_regt = sc->sc_regt;
    444 	pko_aa.aa_cmdptr = &sc->sc_cmdptr;
    445 	pko_aa.aa_cmd_buf_pool = OCTEON_POOL_NO_CMD;
    446 	pko_aa.aa_cmd_buf_size = OCTEON_POOL_NWORDS_CMD;
    447 	octpko_init(&pko_aa, &sc->sc_pko);
    448 }
    449 
    450 /* XXX */
    451 static void
    452 cnmac_asx_init(struct cnmac_softc *sc)
    453 {
    454 	struct octasx_attach_args asx_aa;
    455 
    456 	asx_aa.aa_port = sc->sc_port;
    457 	asx_aa.aa_regt = sc->sc_regt;
    458 	octasx_init(&asx_aa, &sc->sc_asx);
    459 }
    460 
    461 static void
    462 cnmac_smi_init(struct cnmac_softc *sc)
    463 {
    464 	struct octsmi_attach_args smi_aa;
    465 
    466 	smi_aa.aa_port = sc->sc_port;
    467 	smi_aa.aa_regt = sc->sc_regt;
    468 	octsmi_init(&smi_aa, &sc->sc_smi);
    469 	octsmi_set_clock(sc->sc_smi, 0x1464ULL); /* XXX */
    470 }
    471 
    472 /* ---- XXX */
    473 
    474 #define	ADDR2UINT64(u, a) \
    475 	do { \
    476 		u = \
    477 		    (((uint64_t)a[0] << 40) | ((uint64_t)a[1] << 32) | \
    478 		     ((uint64_t)a[2] << 24) | ((uint64_t)a[3] << 16) | \
    479 		     ((uint64_t)a[4] <<	 8) | ((uint64_t)a[5] <<  0)); \
    480 	} while (0)
    481 #define	UINT642ADDR(a, u) \
    482 	do { \
    483 		a[0] = (uint8_t)((u) >> 40); a[1] = (uint8_t)((u) >> 32); \
    484 		a[2] = (uint8_t)((u) >> 24); a[3] = (uint8_t)((u) >> 16); \
    485 		a[4] = (uint8_t)((u) >>	 8); a[5] = (uint8_t)((u) >>  0); \
    486 	} while (0)
    487 
    488 static void
    489 cnmac_board_mac_addr(uint8_t *enaddr, size_t size, struct cnmac_softc *sc)
    490 {
    491 	prop_dictionary_t dict;
    492 	prop_data_t ea;
    493 
    494 	dict = device_properties(sc->sc_dev);
    495 	KASSERT(dict != NULL);
    496 	ea = prop_dictionary_get(dict, "mac-address");
    497 	KASSERT(ea != NULL);
    498 	memcpy(enaddr, prop_data_data_nocopy(ea), size);
    499 }
    500 
    501 /* ---- media */
    502 
    503 static int
    504 cnmac_mii_readreg(device_t self, int phy_addr, int reg, uint16_t *val)
    505 {
    506 	struct cnmac_softc *sc = device_private(self);
    507 
    508 	return octsmi_read(sc->sc_smi, phy_addr, reg, val);
    509 }
    510 
    511 static int
    512 cnmac_mii_writereg(device_t self, int phy_addr, int reg, uint16_t val)
    513 {
    514 	struct cnmac_softc *sc = device_private(self);
    515 
    516 	return octsmi_write(sc->sc_smi, phy_addr, reg, val);
    517 }
    518 
    519 static void
    520 cnmac_mii_statchg(struct ifnet *ifp)
    521 {
    522 	struct cnmac_softc *sc = ifp->if_softc;
    523 
    524 	octpko_port_enable(sc->sc_pko, 0);
    525 	octgmx_port_enable(sc->sc_gmx_port, 0);
    526 
    527 	cnmac_reset(sc);
    528 
    529 	if (ISSET(ifp->if_flags, IFF_RUNNING))
    530 		octgmx_set_filter(sc->sc_gmx_port);
    531 
    532 	octpko_port_enable(sc->sc_pko, 1);
    533 	octgmx_port_enable(sc->sc_gmx_port, 1);
    534 }
    535 
    536 static int
    537 cnmac_mediainit(struct cnmac_softc *sc)
    538 {
    539 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    540 	struct mii_data *mii = &sc->sc_mii;
    541 	prop_object_t phy;
    542 
    543 	mii->mii_ifp = ifp;
    544 	mii->mii_readreg = cnmac_mii_readreg;
    545 	mii->mii_writereg = cnmac_mii_writereg;
    546 	mii->mii_statchg = cnmac_mii_statchg;
    547 	sc->sc_ethercom.ec_mii = mii;
    548 
    549 	/* Initialize ifmedia structures. */
    550 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, cnmac_mediastatus);
    551 
    552 	phy = prop_dictionary_get(device_properties(sc->sc_dev), "phy-addr");
    553 	KASSERT(phy != NULL);
    554 
    555 	mii_attach(sc->sc_dev, mii, 0xffffffff, prop_number_integer_value(phy),
    556 	    MII_OFFSET_ANY, MIIF_DOPAUSE);
    557 
    558 	/* XXX XXX XXX */
    559 	if (LIST_FIRST(&mii->mii_phys) != NULL) {
    560 		/* XXX XXX XXX */
    561 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    562 		/* XXX XXX XXX */
    563 	} else {
    564 		/* XXX XXX XXX */
    565 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE,
    566 		    MII_MEDIA_NONE, NULL);
    567 		/* XXX XXX XXX */
    568 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    569 		/* XXX XXX XXX */
    570 	}
    571 	/* XXX XXX XXX */
    572 
    573 	return 0;
    574 }
    575 
    576 static void
    577 cnmac_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    578 {
    579 	struct cnmac_softc *sc = ifp->if_softc;
    580 
    581 	mii_pollstat(&sc->sc_mii);
    582 
    583 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
    584 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    585 	ifmr->ifm_active = (sc->sc_mii.mii_media_active & ~IFM_ETH_FMASK) |
    586 	    sc->sc_gmx_port->sc_port_flowflags;
    587 }
    588 
    589 /* ---- send buffer garbage collection */
    590 
    591 static inline void
    592 cnmac_send_queue_flush_prefetch(struct cnmac_softc *sc)
    593 {
    594 	CNMAC_KASSERT(sc->sc_prefetch == 0);
    595 	octfau_op_inc_fetch_8(&sc->sc_fau_done, 0);
    596 	sc->sc_prefetch = 1;
    597 }
    598 
    599 static inline void
    600 cnmac_send_queue_flush_fetch(struct cnmac_softc *sc)
    601 {
    602 #ifndef	 CNMAC_DEBUG
    603 	if (!sc->sc_prefetch)
    604 		return;
    605 #endif
    606 	CNMAC_KASSERT(sc->sc_prefetch == 1);
    607 	sc->sc_hard_done_cnt = octfau_op_inc_read_8(&sc->sc_fau_done);
    608 	CNMAC_KASSERT(sc->sc_hard_done_cnt <= 0);
    609 	sc->sc_prefetch = 0;
    610 }
    611 
    612 static inline void
    613 cnmac_send_queue_flush(struct cnmac_softc *sc)
    614 {
    615 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    616 	const int64_t sent_count = sc->sc_hard_done_cnt;
    617 	int i;
    618 
    619 	CNMAC_KASSERT(sc->sc_flush == 0);
    620 	CNMAC_KASSERT(sent_count <= 0);
    621 
    622 	for (i = 0; i < 0 - sent_count; i++) {
    623 		struct mbuf *m;
    624 		uint64_t *gbuf;
    625 
    626 		cnmac_send_queue_del(sc, &m, &gbuf);
    627 
    628 		octfpa_buf_put(cnmac_fb_sg, gbuf);
    629 		OCTEON_EVCNT_INC(sc, txbufgbput);
    630 
    631 		m_freem(m);
    632 
    633 		CLR(ifp->if_flags, IFF_OACTIVE);
    634 	}
    635 
    636 	octfau_op_inc_fetch_8(&sc->sc_fau_done, i);
    637 	sc->sc_flush = i;
    638 }
    639 
    640 static inline void
    641 cnmac_send_queue_flush_sync(struct cnmac_softc *sc)
    642 {
    643 	if (sc->sc_flush == 0)
    644 		return;
    645 
    646 	CNMAC_KASSERT(sc->sc_flush > 0);
    647 
    648 	/* XXX XXX XXX */
    649 	octfau_op_inc_read_8(&sc->sc_fau_done);
    650 	sc->sc_soft_req_cnt -= sc->sc_flush;
    651 	CNMAC_KASSERT(sc->sc_soft_req_cnt >= 0);
    652 	/* XXX XXX XXX */
    653 
    654 	sc->sc_flush = 0;
    655 }
    656 
    657 static inline int
    658 cnmac_send_queue_is_full(struct cnmac_softc *sc)
    659 {
    660 #ifdef CNMAC_SEND_QUEUE_CHECK
    661 	int64_t nofree_cnt;
    662 
    663 	nofree_cnt = sc->sc_soft_req_cnt + sc->sc_hard_done_cnt;
    664 
    665 	if (__predict_false(nofree_cnt == GATHER_QUEUE_SIZE - 1)) {
    666 		cnmac_send_queue_flush(sc);
    667 		OCTEON_EVCNT_INC(sc, txerrgbuf);
    668 		cnmac_send_queue_flush_sync(sc);
    669 		return 1;
    670 	}
    671 
    672 #endif
    673 	return 0;
    674 }
    675 
    676 /*
    677  * (Ab)use m_nextpkt and m_paddr to maintain mbuf chain and pointer to gather
    678  * buffer.  Other mbuf members may be used by m_freem(), so don't touch them!
    679  */
    680 
    681 struct _send_queue_entry {
    682 	union {
    683 		struct mbuf _sqe_s_mbuf;
    684 		struct {
    685 			char _sqe_s_entry_pad[offsetof(struct mbuf, m_nextpkt)];
    686 			SIMPLEQ_ENTRY(_send_queue_entry) _sqe_s_entry_entry;
    687 		} _sqe_s_entry;
    688 		struct {
    689 			char _sqe_s_gbuf_pad[offsetof(struct mbuf, m_paddr)];
    690 			uint64_t *_sqe_s_gbuf_gbuf;
    691 		} _sqe_s_gbuf;
    692 	} _sqe_u;
    693 #define	_sqe_entry	_sqe_u._sqe_s_entry._sqe_s_entry_entry
    694 #define	_sqe_gbuf	_sqe_u._sqe_s_gbuf._sqe_s_gbuf_gbuf
    695 };
    696 
    697 static inline void
    698 cnmac_send_queue_add(struct cnmac_softc *sc, struct mbuf *m,
    699     uint64_t *gbuf)
    700 {
    701 	struct _send_queue_entry *sqe = (struct _send_queue_entry *)m;
    702 
    703 	sqe->_sqe_gbuf = gbuf;
    704 	SIMPLEQ_INSERT_TAIL(&sc->sc_sendq, sqe, _sqe_entry);
    705 
    706 	if ((m->m_flags & M_EXT) && m->m_ext.ext_free != NULL)
    707 		sc->sc_ext_callback_cnt++;
    708 
    709 	OCTEON_EVCNT_INC(sc, txadd);
    710 }
    711 
    712 static inline void
    713 cnmac_send_queue_del(struct cnmac_softc *sc, struct mbuf **rm, uint64_t **rgbuf)
    714 {
    715 	struct _send_queue_entry *sqe;
    716 
    717 	sqe = SIMPLEQ_FIRST(&sc->sc_sendq);
    718 	CNMAC_KASSERT(sqe != NULL);
    719 	SIMPLEQ_REMOVE_HEAD(&sc->sc_sendq, _sqe_entry);
    720 
    721 	*rm = (void *)sqe;
    722 	*rgbuf = sqe->_sqe_gbuf;
    723 
    724 	if (((*rm)->m_flags & M_EXT) && (*rm)->m_ext.ext_free != NULL) {
    725 		sc->sc_ext_callback_cnt--;
    726 		CNMAC_KASSERT(sc->sc_ext_callback_cnt >= 0);
    727 	}
    728 
    729 	OCTEON_EVCNT_INC(sc, txdel);
    730 }
    731 
    732 static inline int
    733 cnmac_buf_free_work(struct cnmac_softc *sc, uint64_t *work, uint64_t word2)
    734 {
    735 	/* XXX when jumbo frame */
    736 	if (ISSET(word2, PIP_WQE_WORD2_IP_BUFS)) {
    737 		paddr_t addr;
    738 		paddr_t start_buffer;
    739 
    740 		addr = work[3] & PIP_WQE_WORD3_ADDR;
    741 		start_buffer = addr & ~(2048 - 1);
    742 
    743 		octfpa_buf_put_paddr(cnmac_fb_pkt, start_buffer);
    744 		OCTEON_EVCNT_INC(sc, rxbufpkput);
    745 	}
    746 
    747 	octfpa_buf_put(cnmac_fb_wqe, work);
    748 	OCTEON_EVCNT_INC(sc, rxbufwqput);
    749 
    750 	return 0;
    751 }
    752 
    753 static inline void
    754 cnmac_buf_ext_free_m(struct mbuf *m, void *buf, size_t size, void *arg)
    755 {
    756 	uint64_t *work = (void *)arg;
    757 #ifdef CNMAC_DEBUG
    758 	struct cnmac_softc *sc = (void *)(uintptr_t)work[0];
    759 #endif
    760 	int s = splnet();
    761 
    762 	OCTEON_EVCNT_INC(sc, rxrs);
    763 
    764 	octfpa_buf_put(cnmac_fb_wqe, work);
    765 	OCTEON_EVCNT_INC(sc, rxbufwqput);
    766 
    767 	CNMAC_KASSERT(m != NULL);
    768 
    769 	pool_cache_put(mb_cache, m);
    770 
    771 	splx(s);
    772 }
    773 
    774 static inline void
    775 cnmac_buf_ext_free_ext(struct mbuf *m, void *buf, size_t size, void *arg)
    776 {
    777 	uint64_t *work = (void *)arg;
    778 #ifdef CNMAC_DEBUG
    779 	struct cnmac_softc *sc = (void *)(uintptr_t)work[0];
    780 #endif
    781 	int s = splnet();
    782 
    783 	octfpa_buf_put(cnmac_fb_wqe, work);
    784 	OCTEON_EVCNT_INC(sc, rxbufwqput);
    785 
    786 	octfpa_buf_put(cnmac_fb_pkt, buf);
    787 	OCTEON_EVCNT_INC(sc, rxbufpkput);
    788 
    789 	CNMAC_KASSERT(m != NULL);
    790 
    791 	pool_cache_put(mb_cache, m);
    792 
    793 	splx(s);
    794 }
    795 
    796 /* ---- ifnet interfaces */
    797 
    798 static int
    799 cnmac_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    800 {
    801 	struct cnmac_softc *sc = ifp->if_softc;
    802 	struct ifreq *ifr = (struct ifreq *)data;
    803 	int s, error;
    804 
    805 	s = splnet();
    806 	switch (cmd) {
    807 	case SIOCSIFMEDIA:
    808 		/* Flow control requires full-duplex mode. */
    809 		if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
    810 		    (ifr->ifr_media & IFM_FDX) == 0) {
    811 			ifr->ifr_media &= ~IFM_ETH_FMASK;
    812 		}
    813 		if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
    814 			if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
    815 				ifr->ifr_media |=
    816 				    IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
    817 			}
    818 			sc->sc_gmx_port->sc_port_flowflags =
    819 				ifr->ifr_media & IFM_ETH_FMASK;
    820 		}
    821 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    822 		break;
    823 	default:
    824 		error = ether_ioctl(ifp, cmd, data);
    825 		if (error == ENETRESET) {
    826 			/*
    827 			 * Multicast list has changed; set the hardware filter
    828 			 * accordingly.
    829 			 */
    830 			if (ISSET(ifp->if_flags, IFF_RUNNING))
    831 				octgmx_set_filter(sc->sc_gmx_port);
    832 			error = 0;
    833 		}
    834 		break;
    835 	}
    836 	cnmac_start(ifp);
    837 	splx(s);
    838 
    839 	return error;
    840 }
    841 
    842 /* ---- send (output) */
    843 
    844 static inline uint64_t
    845 cnmac_send_makecmd_w0(uint64_t fau0, uint64_t fau1, size_t len, int segs)
    846 {
    847 	return octpko_cmd_word0(
    848 		OCT_FAU_OP_SIZE_64,		/* sz1 */
    849 		OCT_FAU_OP_SIZE_64,		/* sz0 */
    850 		1, fau1, 1, fau0,		/* s1, reg1, s0, reg0 */
    851 		0,				/* le */
    852 		cnmac_param_pko_cmd_w0_n2,	/* n2 */
    853 		1, 0,				/* q, r */
    854 		(segs == 1) ? 0 : 1,		/* g */
    855 		0, 0, 1,			/* ipoffp1, ii, df */
    856 		segs, (int)len);		/* segs, totalbytes */
    857 }
    858 
    859 static inline uint64_t
    860 cnmac_send_makecmd_w1(int size, paddr_t addr)
    861 {
    862 	return octpko_cmd_word1(
    863 		0, 0,				/* i, back */
    864 		FPA_GATHER_BUFFER_POOL,		/* pool */
    865 		size, addr);			/* size, addr */
    866 }
    867 
    868 static inline int
    869 cnmac_send_makecmd_gbuf(struct cnmac_softc *sc, struct mbuf *m0, uint64_t *gbuf,
    870     int *rsegs)
    871 {
    872 	struct mbuf *m;
    873 	int segs = 0;
    874 	uintptr_t laddr, rlen, nlen;
    875 
    876 	for (m = m0; m != NULL; m = m->m_next) {
    877 
    878 		if (__predict_false(m->m_len == 0))
    879 			continue;
    880 
    881 #if 0
    882 		CNMAC_KASSERT(((uint32_t)m->m_data & (PAGE_SIZE - 1))
    883 		   == (kvtophys((vaddr_t)m->m_data) & (PAGE_SIZE - 1)));
    884 #endif
    885 
    886 		/* Aligned 4k */
    887 		laddr = (uintptr_t)m->m_data & (PAGE_SIZE - 1);
    888 
    889 		if (laddr + m->m_len > PAGE_SIZE) {
    890 			/* XXX XXX XXX */
    891 			rlen = PAGE_SIZE - laddr;
    892 			nlen = m->m_len - rlen;
    893 			*(gbuf + segs) = cnmac_send_makecmd_w1(rlen,
    894 			    kvtophys((vaddr_t)m->m_data));
    895 			segs++;
    896 			if (segs > 63) {
    897 				return 1;
    898 			}
    899 			/* XXX XXX XXX */
    900 		} else {
    901 			rlen = 0;
    902 			nlen = m->m_len;
    903 		}
    904 
    905 		*(gbuf + segs) = cnmac_send_makecmd_w1(nlen,
    906 		    kvtophys((vaddr_t)(m->m_data + rlen)));
    907 		segs++;
    908 		if (segs > 63) {
    909 			return 1;
    910 		}
    911 	}
    912 
    913 	CNMAC_KASSERT(m == NULL);
    914 
    915 	*rsegs = segs;
    916 
    917 	return 0;
    918 }
    919 
    920 static inline int
    921 cnmac_send_makecmd(struct cnmac_softc *sc, struct mbuf *m,
    922     uint64_t *gbuf, uint64_t *rpko_cmd_w0, uint64_t *rpko_cmd_w1)
    923 {
    924 	uint64_t pko_cmd_w0, pko_cmd_w1;
    925 	int segs;
    926 	int result = 0;
    927 
    928 	if (cnmac_send_makecmd_gbuf(sc, m, gbuf, &segs)) {
    929 		log(LOG_WARNING, "%s: there are a lot of number of segments"
    930 		    " of transmission data", device_xname(sc->sc_dev));
    931 		result = 1;
    932 		goto done;
    933 	}
    934 
    935 	/*
    936 	 * segs == 1	-> link mode (single continuous buffer)
    937 	 *		   WORD1[size] is number of bytes pointed by segment
    938 	 *
    939 	 * segs > 1	-> gather mode (scatter-gather buffer)
    940 	 *		   WORD1[size] is number of segments
    941 	 */
    942 	pko_cmd_w0 = cnmac_send_makecmd_w0(sc->sc_fau_done.fd_regno,
    943 	    0, m->m_pkthdr.len, segs);
    944 	if (segs == 1) {
    945 		pko_cmd_w1 = cnmac_send_makecmd_w1(
    946 		    m->m_pkthdr.len, kvtophys((vaddr_t)m->m_data));
    947 	} else {
    948 #ifdef __mips_n32
    949 		KASSERT(MIPS_KSEG0_P(gbuf));
    950 		pko_cmd_w1 = cnmac_send_makecmd_w1(segs,
    951 		    MIPS_KSEG0_TO_PHYS(gbuf));
    952 #else
    953 		pko_cmd_w1 = cnmac_send_makecmd_w1(segs,
    954 		    MIPS_XKPHYS_TO_PHYS(gbuf));
    955 #endif
    956 	}
    957 
    958 	*rpko_cmd_w0 = pko_cmd_w0;
    959 	*rpko_cmd_w1 = pko_cmd_w1;
    960 
    961 done:
    962 	return result;
    963 }
    964 
    965 static inline int
    966 cnmac_send_cmd(struct cnmac_softc *sc, uint64_t pko_cmd_w0,
    967     uint64_t pko_cmd_w1, int *pwdc)
    968 {
    969 	uint64_t *cmdptr;
    970 	int result = 0;
    971 
    972 #ifdef __mips_n32
    973 	KASSERT((sc->sc_cmdptr.cmdptr & ~MIPS_PHYS_MASK) == 0);
    974 	cmdptr = (uint64_t *)MIPS_PHYS_TO_KSEG0(sc->sc_cmdptr.cmdptr);
    975 #else
    976 	cmdptr = (uint64_t *)MIPS_PHYS_TO_XKPHYS_CACHED(sc->sc_cmdptr.cmdptr);
    977 #endif
    978 	cmdptr += sc->sc_cmdptr.cmdptr_idx;
    979 
    980 	CNMAC_KASSERT(cmdptr != NULL);
    981 
    982 	*cmdptr++ = pko_cmd_w0;
    983 	*cmdptr++ = pko_cmd_w1;
    984 
    985 	CNMAC_KASSERT(sc->sc_cmdptr.cmdptr_idx + 2 <= FPA_COMMAND_BUFFER_POOL_NWORDS - 1);
    986 
    987 	if (sc->sc_cmdptr.cmdptr_idx + 2 == FPA_COMMAND_BUFFER_POOL_NWORDS - 1) {
    988 		paddr_t buf;
    989 
    990 		buf = octfpa_buf_get_paddr(cnmac_fb_cmd);
    991 		if (buf == 0) {
    992 			log(LOG_WARNING,
    993 			    "%s: can not allocate command buffer from free pool allocator\n",
    994 			    device_xname(sc->sc_dev));
    995 			result = 1;
    996 			goto done;
    997 		}
    998 		OCTEON_EVCNT_INC(sc, txbufcbget);
    999 		*cmdptr++ = buf;
   1000 		sc->sc_cmdptr.cmdptr = (uint64_t)buf;
   1001 		sc->sc_cmdptr.cmdptr_idx = 0;
   1002 	} else {
   1003 		sc->sc_cmdptr.cmdptr_idx += 2;
   1004 	}
   1005 
   1006 	*pwdc += 2;
   1007 
   1008 done:
   1009 	return result;
   1010 }
   1011 
   1012 static inline int
   1013 cnmac_send_buf(struct cnmac_softc *sc, struct mbuf *m, uint64_t *gbuf,
   1014     int *pwdc)
   1015 {
   1016 	int result = 0, error;
   1017 	uint64_t pko_cmd_w0, pko_cmd_w1;
   1018 
   1019 	error = cnmac_send_makecmd(sc, m, gbuf, &pko_cmd_w0, &pko_cmd_w1);
   1020 	if (error != 0) {
   1021 		/* Already logging */
   1022 		OCTEON_EVCNT_INC(sc, txerrmkcmd);
   1023 		result = error;
   1024 		goto done;
   1025 	}
   1026 
   1027 	error = cnmac_send_cmd(sc, pko_cmd_w0, pko_cmd_w1, pwdc);
   1028 	if (error != 0) {
   1029 		/* Already logging */
   1030 		OCTEON_EVCNT_INC(sc, txerrcmd);
   1031 		result = error;
   1032 	}
   1033 
   1034 done:
   1035 	return result;
   1036 }
   1037 
   1038 static inline int
   1039 cnmac_send(struct cnmac_softc *sc, struct mbuf *m, int *pwdc)
   1040 {
   1041 	paddr_t gaddr = 0;
   1042 	uint64_t *gbuf = NULL;
   1043 	int result = 0, error;
   1044 
   1045 	OCTEON_EVCNT_INC(sc, tx);
   1046 
   1047 	gaddr = octfpa_buf_get_paddr(cnmac_fb_sg);
   1048 	if (gaddr == 0) {
   1049 		log(LOG_WARNING, "%s: can not allocate gather buffer from "
   1050 		    "free pool allocator\n", device_xname(sc->sc_dev));
   1051 		OCTEON_EVCNT_INC(sc, txerrgbuf);
   1052 		result = 1;
   1053 		goto done;
   1054 	}
   1055 	OCTEON_EVCNT_INC(sc, txbufgbget);
   1056 
   1057 #ifdef __mips_n32
   1058 	KASSERT((gaddr & ~MIPS_PHYS_MASK) == 0);
   1059 	gbuf = (uint64_t *)(uintptr_t)MIPS_PHYS_TO_KSEG0(gaddr);
   1060 #else
   1061 	gbuf = (uint64_t *)(uintptr_t)MIPS_PHYS_TO_XKPHYS_CACHED(gaddr);
   1062 #endif
   1063 
   1064 	CNMAC_KASSERT(gbuf != NULL);
   1065 
   1066 	error = cnmac_send_buf(sc, m, gbuf, pwdc);
   1067 	if (error != 0) {
   1068 		/* Already logging */
   1069 		octfpa_buf_put_paddr(cnmac_fb_sg, gaddr);
   1070 		OCTEON_EVCNT_INC(sc, txbufgbput);
   1071 		result = error;
   1072 		goto done;
   1073 	}
   1074 
   1075 	cnmac_send_queue_add(sc, m, gbuf);
   1076 
   1077 done:
   1078 	return result;
   1079 }
   1080 
   1081 static void
   1082 cnmac_start(struct ifnet *ifp)
   1083 {
   1084 	struct cnmac_softc *sc = ifp->if_softc;
   1085 	struct mbuf *m;
   1086 	int wdc = 0;
   1087 
   1088 	/*
   1089 	 * Performance tuning
   1090 	 * pre-send iobdma request
   1091 	 */
   1092 	cnmac_send_queue_flush_prefetch(sc);
   1093 
   1094 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1095 		goto last;
   1096 
   1097 	/* XXX assume that OCTEON doesn't buffer packets */
   1098 	if (__predict_false(!octgmx_link_status(sc->sc_gmx_port))) {
   1099 		/* Dequeue and drop them */
   1100 		while (1) {
   1101 			IFQ_DEQUEUE(&ifp->if_snd, m);
   1102 			if (m == NULL)
   1103 				break;
   1104 
   1105 			m_freem(m);
   1106 			IF_DROP(&ifp->if_snd);
   1107 			OCTEON_EVCNT_INC(sc, txerrlink);
   1108 		}
   1109 		goto last;
   1110 	}
   1111 
   1112 	for (;;) {
   1113 		IFQ_POLL(&ifp->if_snd, m);
   1114 		if (__predict_false(m == NULL))
   1115 			break;
   1116 
   1117 		/* XXX XXX XXX */
   1118 		cnmac_send_queue_flush_fetch(sc);
   1119 
   1120 		/*
   1121 		 * If no free send buffer is available, free all the sent
   1122 		 * buffers and bail out.
   1123 		 */
   1124 		if (cnmac_send_queue_is_full(sc)) {
   1125 			SET(ifp->if_flags, IFF_OACTIVE);
   1126 			if (wdc > 0)
   1127 				octpko_op_doorbell_write(sc->sc_port,
   1128 				    sc->sc_port, wdc);
   1129 			return;
   1130 		}
   1131 		/* XXX XXX XXX */
   1132 
   1133 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1134 
   1135 		bpf_mtap(ifp, m, BPF_D_OUT);
   1136 
   1137 		/* XXX XXX XXX */
   1138 		if (sc->sc_soft_req_cnt > sc->sc_soft_req_thresh)
   1139 			cnmac_send_queue_flush(sc);
   1140 		if (cnmac_send(sc, m, &wdc)) {
   1141 			IF_DROP(&ifp->if_snd);
   1142 			m_freem(m);
   1143 			log(LOG_WARNING,
   1144 			  "%s: failed in the transmission of the packet\n",
   1145 			  device_xname(sc->sc_dev));
   1146 			OCTEON_EVCNT_INC(sc, txerr);
   1147 		} else
   1148 			sc->sc_soft_req_cnt++;
   1149 
   1150 		if (sc->sc_flush)
   1151 			cnmac_send_queue_flush_sync(sc);
   1152 		/* XXX XXX XXX */
   1153 
   1154 		/* Send next iobdma request */
   1155 		cnmac_send_queue_flush_prefetch(sc);
   1156 	}
   1157 
   1158 	if (wdc > 0)
   1159 		octpko_op_doorbell_write(sc->sc_port, sc->sc_port, wdc);
   1160 
   1161 /*
   1162  * Don't schedule send-buffer-free callout every time - those buffers are freed
   1163  * by "free tick".  This makes some packets like NFS slower.
   1164  */
   1165 #ifdef CNMAC_USENFS
   1166 	if (__predict_false(sc->sc_ext_callback_cnt > 0)) {
   1167 		int timo;
   1168 
   1169 		/* ??? */
   1170 		timo = hz - (100 * sc->sc_ext_callback_cnt);
   1171 		if (timo < 10)
   1172 			timo = 10;
   1173 		callout_schedule(&sc->sc_tick_free_ch, timo);
   1174 	}
   1175 #endif
   1176 
   1177 last:
   1178 	cnmac_send_queue_flush_fetch(sc);
   1179 }
   1180 
   1181 static void
   1182 cnmac_watchdog(struct ifnet *ifp)
   1183 {
   1184 	struct cnmac_softc *sc = ifp->if_softc;
   1185 
   1186 	printf("%s: device timeout\n", device_xname(sc->sc_dev));
   1187 
   1188 	cnmac_configure(sc);
   1189 
   1190 	SET(ifp->if_flags, IFF_RUNNING);
   1191 	CLR(ifp->if_flags, IFF_OACTIVE);
   1192 	ifp->if_timer = 0;
   1193 
   1194 	cnmac_start(ifp);
   1195 }
   1196 
   1197 static int
   1198 cnmac_init(struct ifnet *ifp)
   1199 {
   1200 	struct cnmac_softc *sc = ifp->if_softc;
   1201 
   1202 	/* XXX don't disable commonly used parts!!! XXX */
   1203 	if (sc->sc_init_flag == 0) {
   1204 		/* Cancel any pending I/O. */
   1205 		cnmac_stop(ifp, 0);
   1206 
   1207 		/* Initialize the device */
   1208 		cnmac_configure(sc);
   1209 
   1210 		octpko_enable(sc->sc_pko);
   1211 		octipd_enable(sc->sc_ipd);
   1212 
   1213 		sc->sc_init_flag = 1;
   1214 	} else {
   1215 		octgmx_port_enable(sc->sc_gmx_port, 1);
   1216 	}
   1217 	mii_ifmedia_change(&sc->sc_mii);
   1218 
   1219 	octgmx_set_filter(sc->sc_gmx_port);
   1220 
   1221 	callout_reset(&sc->sc_tick_misc_ch, hz, cnmac_tick_misc, sc);
   1222 	callout_reset(&sc->sc_tick_free_ch, hz, cnmac_tick_free, sc);
   1223 
   1224 	SET(ifp->if_flags, IFF_RUNNING);
   1225 	CLR(ifp->if_flags, IFF_OACTIVE);
   1226 
   1227 	return 0;
   1228 }
   1229 
   1230 static void
   1231 cnmac_stop(struct ifnet *ifp, int disable)
   1232 {
   1233 	struct cnmac_softc *sc = ifp->if_softc;
   1234 
   1235 	callout_stop(&sc->sc_tick_misc_ch);
   1236 	callout_stop(&sc->sc_tick_free_ch);
   1237 
   1238 	mii_down(&sc->sc_mii);
   1239 
   1240 	octgmx_port_enable(sc->sc_gmx_port, 0);
   1241 
   1242 	/* Mark the interface as down and cancel the watchdog timer. */
   1243 	CLR(ifp->if_flags, IFF_RUNNING | IFF_OACTIVE);
   1244 	ifp->if_timer = 0;
   1245 }
   1246 
   1247 /* ---- misc */
   1248 
   1249 #define PKO_INDEX_MASK	((1ULL << 12/* XXX */) - 1)
   1250 
   1251 static int
   1252 cnmac_reset(struct cnmac_softc *sc)
   1253 {
   1254 	octgmx_reset_speed(sc->sc_gmx_port);
   1255 	octgmx_reset_flowctl(sc->sc_gmx_port);
   1256 	octgmx_reset_timing(sc->sc_gmx_port);
   1257 
   1258 	return 0;
   1259 }
   1260 
   1261 static int
   1262 cnmac_configure(struct cnmac_softc *sc)
   1263 {
   1264 	octgmx_port_enable(sc->sc_gmx_port, 0);
   1265 
   1266 	cnmac_reset(sc);
   1267 
   1268 	cnmac_configure_common(sc);
   1269 
   1270 	octpko_port_config(sc->sc_pko);
   1271 	octpko_port_enable(sc->sc_pko, 1);
   1272 	octpip_port_config(sc->sc_pip);
   1273 
   1274 	octgmx_tx_stats_rd_clr(sc->sc_gmx_port, 1);
   1275 	octgmx_rx_stats_rd_clr(sc->sc_gmx_port, 1);
   1276 
   1277 	octgmx_port_enable(sc->sc_gmx_port, 1);
   1278 
   1279 	return 0;
   1280 }
   1281 
   1282 static int
   1283 cnmac_configure_common(struct cnmac_softc *sc)
   1284 {
   1285 	static int once;
   1286 
   1287 	if (once == 1)
   1288 		return 0;
   1289 	once = 1;
   1290 
   1291 	octipd_config(sc->sc_ipd);
   1292 #ifdef CNMAC_IPD_RED
   1293 	octipd_red(sc->sc_ipd, RECV_QUEUE_SIZE >> 2, RECV_QUEUE_SIZE >> 3);
   1294 #endif
   1295 	octpko_config(sc->sc_pko);
   1296 
   1297 	octpow_config(sc->sc_pow, OCTEON_POW_GROUP_PIP);
   1298 
   1299 	return 0;
   1300 }
   1301 
   1302 /* ---- receive (input) */
   1303 
   1304 static inline int
   1305 cnmac_recv_mbuf(struct cnmac_softc *sc, uint64_t *work, struct mbuf **rm)
   1306 {
   1307 	struct mbuf *m;
   1308 	void (*ext_free)(struct mbuf *, void *, size_t, void *);
   1309 	void *ext_buf;
   1310 	size_t ext_size;
   1311 	void *data;
   1312 	uint64_t word1 = work[1];
   1313 	uint64_t word2 = work[2];
   1314 	uint64_t word3 = work[3];
   1315 
   1316 	MGETHDR(m, M_NOWAIT, MT_DATA);
   1317 	if (m == NULL)
   1318 		return 1;
   1319 	CNMAC_KASSERT(m != NULL);
   1320 
   1321 	if ((word2 & PIP_WQE_WORD2_IP_BUFS) == 0) {
   1322 		/* Dynamic short */
   1323 		ext_free = cnmac_buf_ext_free_m;
   1324 		ext_buf = &work[4];
   1325 		ext_size = 96;
   1326 
   1327 		data = &work[4 + sc->sc_ip_offset / sizeof(uint64_t)];
   1328 	} else {
   1329 		vaddr_t addr;
   1330 		vaddr_t start_buffer;
   1331 
   1332 #ifdef __mips_n32
   1333 		KASSERT((word3 & ~MIPS_PHYS_MASK) == 0);
   1334 		addr = MIPS_PHYS_TO_KSEG0(word3 & PIP_WQE_WORD3_ADDR);
   1335 #else
   1336 		addr = MIPS_PHYS_TO_XKPHYS_CACHED(word3 & PIP_WQE_WORD3_ADDR);
   1337 #endif
   1338 		start_buffer = addr & ~(2048 - 1);
   1339 
   1340 		ext_free = cnmac_buf_ext_free_ext;
   1341 		ext_buf = (void *)start_buffer;
   1342 		ext_size = 2048;
   1343 
   1344 		data = (void *)addr;
   1345 	}
   1346 
   1347 	/* Embed sc pointer into work[0] for _ext_free evcnt */
   1348 	work[0] = (uintptr_t)sc;
   1349 
   1350 	MEXTADD(m, ext_buf, ext_size, 0, ext_free, work);
   1351 	CNMAC_KASSERT(ISSET(m->m_flags, M_EXT));
   1352 
   1353 	m->m_data = data;
   1354 	m->m_len = m->m_pkthdr.len = (word1 & PIP_WQE_WORD1_LEN) >> 48;
   1355 	m_set_rcvif(m, &sc->sc_ethercom.ec_if);
   1356 
   1357 	/* Not readonly buffer */
   1358 	m->m_flags |= M_EXT_RW;
   1359 
   1360 	*rm = m;
   1361 
   1362 	CNMAC_KASSERT(*rm != NULL);
   1363 
   1364 	return 0;
   1365 }
   1366 
   1367 static inline int
   1368 cnmac_recv_check_code(struct cnmac_softc *sc, uint64_t word2)
   1369 {
   1370 	uint64_t opecode = word2 & PIP_WQE_WORD2_NOIP_OPECODE;
   1371 
   1372 	if (__predict_true(!ISSET(word2, PIP_WQE_WORD2_NOIP_RE)))
   1373 		return 0;
   1374 
   1375 	/* This error is harmless */
   1376 	if (opecode == PIP_OVER_ERR)
   1377 		return 0;
   1378 
   1379 	return 1;
   1380 }
   1381 
   1382 static inline int
   1383 cnmac_recv_check_jumbo(struct cnmac_softc *sc, uint64_t word2)
   1384 {
   1385 	if (__predict_false((word2 & PIP_WQE_WORD2_IP_BUFS) > (1ULL << 56)))
   1386 		return 1;
   1387 	return 0;
   1388 }
   1389 
   1390 static inline int
   1391 cnmac_recv_check_link(struct cnmac_softc *sc, uint64_t word2)
   1392 {
   1393 	if (__predict_false(!octgmx_link_status(sc->sc_gmx_port)))
   1394 		return 1;
   1395 	return 0;
   1396 }
   1397 
   1398 static inline int
   1399 cnmac_recv_check(struct cnmac_softc *sc, uint64_t word2)
   1400 {
   1401 	if (__predict_false(cnmac_recv_check_link(sc, word2)) != 0) {
   1402 		if (ratecheck(&sc->sc_rate_recv_check_link_last,
   1403 		    &sc->sc_rate_recv_check_link_cap))
   1404 			log(LOG_DEBUG,
   1405 			    "%s: link is not up, the packet was dropped\n",
   1406 			    device_xname(sc->sc_dev));
   1407 		OCTEON_EVCNT_INC(sc, rxerrlink);
   1408 		return 1;
   1409 	}
   1410 
   1411 #if 0 /* XXX Performance tuning (Jumbo-frame is not supported yet!) */
   1412 	if (__predict_false(cnmac_recv_check_jumbo(sc, word2)) != 0) {
   1413 		/* XXX jumbo frame */
   1414 		if (ratecheck(&sc->sc_rate_recv_check_jumbo_last,
   1415 		    &sc->sc_rate_recv_check_jumbo_cap))
   1416 			log(LOG_DEBUG,
   1417 			    "jumbo frame was received\n");
   1418 		OCTEON_EVCNT_INC(sc, rxerrjmb);
   1419 		return 1;
   1420 	}
   1421 #endif
   1422 
   1423 	if (__predict_false(cnmac_recv_check_code(sc, word2)) != 0) {
   1424 
   1425 		if ((word2 & PIP_WQE_WORD2_NOIP_OPECODE) ==
   1426 				PIP_WQE_WORD2_RE_OPCODE_LENGTH) {
   1427 			/* No logging */
   1428 			/* XXX increment special error count */
   1429 		} else if ((word2 & PIP_WQE_WORD2_NOIP_OPECODE) ==
   1430 				PIP_WQE_WORD2_RE_OPCODE_PARTIAL) {
   1431 			/* Not an error, it's because of overload */
   1432 		} else {
   1433 
   1434 			if (ratecheck(&sc->sc_rate_recv_check_code_last,
   1435 			    &sc->sc_rate_recv_check_code_cap))
   1436 				log(LOG_WARNING,
   1437 				    "%s: reception error, packet dropped "
   1438 				    "(error code = %" PRId64 ")\n",
   1439 				    device_xname(sc->sc_dev), word2 & PIP_WQE_WORD2_NOIP_OPECODE);
   1440 		}
   1441 		OCTEON_EVCNT_INC(sc, rxerrcode);
   1442 		return 1;
   1443 	}
   1444 
   1445 	return 0;
   1446 }
   1447 
   1448 static inline int
   1449 cnmac_recv(struct cnmac_softc *sc, uint64_t *work)
   1450 {
   1451 	int result = 0;
   1452 	struct ifnet *ifp;
   1453 	struct mbuf *m;
   1454 	uint64_t word2;
   1455 
   1456 	/* XXX XXX XXX */
   1457 	/*
   1458 	 * Performance tuning
   1459 	 * pre-send iobdma request
   1460 	 */
   1461 	if (sc->sc_soft_req_cnt > sc->sc_soft_req_thresh) {
   1462 		cnmac_send_queue_flush_prefetch(sc);
   1463 	}
   1464 	/* XXX XXX XXX */
   1465 
   1466 	CNMAC_KASSERT(sc != NULL);
   1467 	CNMAC_KASSERT(work != NULL);
   1468 
   1469 	OCTEON_EVCNT_INC(sc, rx);
   1470 
   1471 	word2 = work[2];
   1472 	ifp = &sc->sc_ethercom.ec_if;
   1473 
   1474 	CNMAC_KASSERT(ifp != NULL);
   1475 
   1476 	if (__predict_false(cnmac_recv_check(sc, word2) != 0)) {
   1477 		if_statinc(ifp, if_ierrors);
   1478 		result = 1;
   1479 		cnmac_buf_free_work(sc, work, word2);
   1480 		goto drop;
   1481 	}
   1482 
   1483 	if (__predict_false(cnmac_recv_mbuf(sc, work, &m) != 0)) {
   1484 		if_statinc(ifp, if_ierrors);
   1485 		result = 1;
   1486 		cnmac_buf_free_work(sc, work, word2);
   1487 		goto drop;
   1488 	}
   1489 
   1490 	/* work[0] .. work[3] may not be valid any more */
   1491 
   1492 	CNMAC_KASSERT(m != NULL);
   1493 
   1494 	octipd_offload(word2, m->m_data, &m->m_pkthdr.csum_flags);
   1495 
   1496 	/* XXX XXX XXX */
   1497 	if (sc->sc_soft_req_cnt > sc->sc_soft_req_thresh) {
   1498 		cnmac_send_queue_flush_fetch(sc);
   1499 		cnmac_send_queue_flush(sc);
   1500 	}
   1501 
   1502 	/* XXX XXX XXX */
   1503 	if (sc->sc_flush)
   1504 		cnmac_send_queue_flush_sync(sc);
   1505 	/* XXX XXX XXX */
   1506 
   1507 	if_percpuq_enqueue(ifp->if_percpuq, m);
   1508 
   1509 	return 0;
   1510 
   1511 drop:
   1512 	/* XXX XXX XXX */
   1513 	if (sc->sc_soft_req_cnt > sc->sc_soft_req_thresh) {
   1514 		cnmac_send_queue_flush_fetch(sc);
   1515 	}
   1516 	/* XXX XXX XXX */
   1517 
   1518 	return result;
   1519 }
   1520 
   1521 static void
   1522 cnmac_recv_redir(struct ifnet *ifp, struct mbuf *m)
   1523 {
   1524 	struct cnmac_softc *rsc = ifp->if_softc;
   1525 	struct cnmac_softc *sc = NULL;
   1526 	int i, wdc = 0;
   1527 
   1528 	for (i = 0; i < 3 /* XXX */; i++) {
   1529 		if (rsc->sc_redir & (1 << i))
   1530 			sc = cnmac_gsc[i];
   1531 	}
   1532 
   1533 	if (sc == NULL) {
   1534 		m_freem(m);
   1535 		return;
   1536 	}
   1537 	cnmac_send_queue_flush_prefetch(sc);
   1538 
   1539 	cnmac_send_queue_flush_fetch(sc);
   1540 
   1541 	if (cnmac_send_queue_is_full(sc)) {
   1542 		m_freem(m);
   1543 		return;
   1544 	}
   1545 	if (sc->sc_soft_req_cnt > sc->sc_soft_req_thresh)
   1546 		cnmac_send_queue_flush(sc);
   1547 
   1548 	if (cnmac_send(sc, m, &wdc)) {
   1549 		IF_DROP(&ifp->if_snd);
   1550 		m_freem(m);
   1551 	} else {
   1552 		octpko_op_doorbell_write(sc->sc_port, sc->sc_port, wdc);
   1553 		sc->sc_soft_req_cnt++;
   1554 	}
   1555 
   1556 	if (sc->sc_flush)
   1557 		cnmac_send_queue_flush_sync(sc);
   1558 }
   1559 
   1560 static inline void
   1561 cnmac_recv_intr(void *data, uint64_t *work)
   1562 {
   1563 	struct cnmac_softc *sc;
   1564 	int port;
   1565 
   1566 	CNMAC_KASSERT(work != NULL);
   1567 
   1568 	port = (work[1] & PIP_WQE_WORD1_IPRT) >> 42;
   1569 
   1570 	CNMAC_KASSERT(port < GMX_PORT_NUNITS);
   1571 
   1572 	sc = cnmac_gsc[port];
   1573 
   1574 	CNMAC_KASSERT(sc != NULL);
   1575 	CNMAC_KASSERT(port == sc->sc_port);
   1576 
   1577 	/* XXX process all work queue entries anyway */
   1578 
   1579 	(void)cnmac_recv(sc, work);
   1580 }
   1581 
   1582 /* ---- tick */
   1583 
   1584 /*
   1585  * cnmac_tick_free
   1586  *
   1587  * => garbage collect send gather buffer / mbuf
   1588  * => called at softclock
   1589  */
   1590 static void
   1591 cnmac_tick_free(void *arg)
   1592 {
   1593 	struct cnmac_softc *sc = arg;
   1594 	int timo;
   1595 	int s;
   1596 
   1597 	s = splnet();
   1598 	/* XXX XXX XXX */
   1599 	if (sc->sc_soft_req_cnt > 0) {
   1600 		cnmac_send_queue_flush_prefetch(sc);
   1601 		cnmac_send_queue_flush_fetch(sc);
   1602 		cnmac_send_queue_flush(sc);
   1603 		cnmac_send_queue_flush_sync(sc);
   1604 	}
   1605 	/* XXX XXX XXX */
   1606 
   1607 	/* XXX XXX XXX */
   1608 	/* ??? */
   1609 	timo = hz - (100 * sc->sc_ext_callback_cnt);
   1610 	if (timo < 10)
   1611 		 timo = 10;
   1612 	callout_schedule(&sc->sc_tick_free_ch, timo);
   1613 	/* XXX XXX XXX */
   1614 	splx(s);
   1615 }
   1616 
   1617 /*
   1618  * cnmac_tick_misc
   1619  *
   1620  * => collect statistics
   1621  * => check link status
   1622  * => called at softclock
   1623  */
   1624 static void
   1625 cnmac_tick_misc(void *arg)
   1626 {
   1627 	struct cnmac_softc *sc = arg;
   1628 	struct ifnet *ifp;
   1629 	int s;
   1630 
   1631 	s = splnet();
   1632 
   1633 	ifp = &sc->sc_ethercom.ec_if;
   1634 
   1635 	octgmx_stats(sc->sc_gmx_port);
   1636 	octpip_stats(sc->sc_pip, ifp, sc->sc_port);
   1637 	mii_tick(&sc->sc_mii);
   1638 
   1639 	splx(s);
   1640 
   1641 	callout_schedule(&sc->sc_tick_misc_ch, hz);
   1642 }
   1643 
   1644 /* ---- Odd nibble preamble workaround (software CRC processing) */
   1645 
   1646 /* ---- sysctl */
   1647 
   1648 static int	cnmac_sysctl_verify(SYSCTLFN_ARGS);
   1649 static int	cnmac_sysctl_pool(SYSCTLFN_ARGS);
   1650 static int	cnmac_sysctl_rd(SYSCTLFN_ARGS);
   1651 
   1652 static int	cnmac_sysctl_pkocmdw0n2_num;
   1653 static int	cnmac_sysctl_pipdynrs_num;
   1654 static int	cnmac_sysctl_redir_num;
   1655 static int	cnmac_sysctl_pkt_pool_num;
   1656 static int	cnmac_sysctl_wqe_pool_num;
   1657 static int	cnmac_sysctl_cmd_pool_num;
   1658 static int	cnmac_sysctl_sg_pool_num;
   1659 static int	cnmac_sysctl_pktbuf_num;
   1660 
   1661 /*
   1662  * Set up sysctl(3) MIB, hw.cnmac.*.
   1663  */
   1664 SYSCTL_SETUP(sysctl_octeon_eth, "sysctl cnmac subtree setup")
   1665 {
   1666 	int rc;
   1667 	int cnmac_sysctl_root_num;
   1668 	const struct sysctlnode *node;
   1669 
   1670 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
   1671 		    0, CTLTYPE_NODE, "hw", NULL,
   1672 		    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
   1673 		goto err;
   1674 	}
   1675 
   1676 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   1677 		    0, CTLTYPE_NODE, "cnmac",
   1678 		    SYSCTL_DESCR("cnmac interface controls"),
   1679 		    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
   1680 		goto err;
   1681 	}
   1682 
   1683 	cnmac_sysctl_root_num = node->sysctl_num;
   1684 
   1685 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   1686 		    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1687 		    CTLTYPE_INT, "pko_cmd_w0_n2",
   1688 		    SYSCTL_DESCR("PKO command WORD0 N2 bit"),
   1689 		    cnmac_sysctl_verify, 0,
   1690 		    &cnmac_param_pko_cmd_w0_n2,
   1691 		    0, CTL_HW, cnmac_sysctl_root_num, CTL_CREATE,
   1692 		    CTL_EOL)) != 0) {
   1693 		goto err;
   1694 	}
   1695 
   1696 	cnmac_sysctl_pkocmdw0n2_num = node->sysctl_num;
   1697 
   1698 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   1699 		    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1700 		    CTLTYPE_INT, "pip_dyn_rs",
   1701 		    SYSCTL_DESCR("PIP dynamic short in WQE"),
   1702 		    cnmac_sysctl_verify, 0,
   1703 		    &cnmac_param_pip_dyn_rs,
   1704 		    0, CTL_HW, cnmac_sysctl_root_num, CTL_CREATE,
   1705 		    CTL_EOL)) != 0) {
   1706 		goto err;
   1707 	}
   1708 
   1709 	cnmac_sysctl_pipdynrs_num = node->sysctl_num;
   1710 
   1711 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   1712 		    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1713 		    CTLTYPE_INT, "redir",
   1714 		    SYSCTL_DESCR("input port redirection"),
   1715 		    cnmac_sysctl_verify, 0,
   1716 		    &cnmac_param_redir,
   1717 		    0, CTL_HW, cnmac_sysctl_root_num, CTL_CREATE,
   1718 		    CTL_EOL)) != 0) {
   1719 		goto err;
   1720 	}
   1721 
   1722 	cnmac_sysctl_redir_num = node->sysctl_num;
   1723 
   1724 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   1725 		    CTLFLAG_PERMANENT,
   1726 		    CTLTYPE_INT, "pkt_pool",
   1727 		    SYSCTL_DESCR("packet pool available"),
   1728 		    cnmac_sysctl_pool, 0, NULL,
   1729 		    0, CTL_HW, cnmac_sysctl_root_num, CTL_CREATE,
   1730 		    CTL_EOL)) != 0) {
   1731 		goto err;
   1732 	}
   1733 
   1734 	cnmac_sysctl_pkt_pool_num = node->sysctl_num;
   1735 
   1736 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   1737 		    CTLFLAG_PERMANENT,
   1738 		    CTLTYPE_INT, "wqe_pool",
   1739 		    SYSCTL_DESCR("wqe pool available"),
   1740 		    cnmac_sysctl_pool, 0, NULL,
   1741 		    0, CTL_HW, cnmac_sysctl_root_num, CTL_CREATE,
   1742 		    CTL_EOL)) != 0) {
   1743 		goto err;
   1744 	}
   1745 
   1746 	cnmac_sysctl_wqe_pool_num = node->sysctl_num;
   1747 
   1748 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   1749 		    CTLFLAG_PERMANENT,
   1750 		    CTLTYPE_INT, "cmd_pool",
   1751 		    SYSCTL_DESCR("cmd pool available"),
   1752 		    cnmac_sysctl_pool, 0, NULL,
   1753 		    0, CTL_HW, cnmac_sysctl_root_num, CTL_CREATE,
   1754 		    CTL_EOL)) != 0) {
   1755 		goto err;
   1756 	}
   1757 
   1758 	cnmac_sysctl_cmd_pool_num = node->sysctl_num;
   1759 
   1760 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   1761 		    CTLFLAG_PERMANENT,
   1762 		    CTLTYPE_INT, "sg_pool",
   1763 		    SYSCTL_DESCR("sg pool available"),
   1764 		    cnmac_sysctl_pool, 0, NULL,
   1765 		    0, CTL_HW, cnmac_sysctl_root_num, CTL_CREATE,
   1766 		    CTL_EOL)) != 0) {
   1767 		goto err;
   1768 	}
   1769 
   1770 	cnmac_sysctl_sg_pool_num = node->sysctl_num;
   1771 
   1772 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
   1773 		    CTLFLAG_PERMANENT | CTLFLAG_READONLY,
   1774 		    CTLTYPE_INT, "pktbuf",
   1775 		    SYSCTL_DESCR("input packet buffer size on POW"),
   1776 		    cnmac_sysctl_rd, 0,
   1777 		    &cnmac_param_pktbuf,
   1778 		    0, CTL_HW, cnmac_sysctl_root_num, CTL_CREATE,
   1779 		    CTL_EOL)) != 0) {
   1780 		goto err;
   1781 	}
   1782 
   1783 	cnmac_sysctl_pktbuf_num = node->sysctl_num;
   1784 
   1785 	return;
   1786 
   1787 err:
   1788 	aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc);
   1789 }
   1790 
   1791 static int
   1792 cnmac_sysctl_verify(SYSCTLFN_ARGS)
   1793 {
   1794 	int error, v;
   1795 	struct sysctlnode node;
   1796 	struct cnmac_softc *sc;
   1797 	int i;
   1798 	int s;
   1799 
   1800 	node = *rnode;
   1801 	v = *(int *)rnode->sysctl_data;
   1802 	node.sysctl_data = &v;
   1803 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1804 	if (error || newp == NULL)
   1805 		return error;
   1806 
   1807 	if (node.sysctl_num == cnmac_sysctl_pkocmdw0n2_num) {
   1808 		if (v < 0 || v > 1)
   1809 			return EINVAL;
   1810 		*(int *)rnode->sysctl_data = v;
   1811 		return 0;
   1812 	}
   1813 
   1814 	if (node.sysctl_num == cnmac_sysctl_pipdynrs_num) {
   1815 		if (v < 0 || v > 1)
   1816 			return EINVAL;
   1817 		*(int *)rnode->sysctl_data = v;
   1818 		s = splnet();
   1819 		for (i = 0; i < 3/* XXX */; i++) {
   1820 			sc = cnmac_gsc[i];	/* XXX */
   1821 			octpip_prt_cfg_enable(sc->sc_pip,
   1822 			    PIP_PRT_CFGN_DYN_RS, v);
   1823 		}
   1824 		splx(s);
   1825 		return 0;
   1826 	}
   1827 
   1828 	if (node.sysctl_num == cnmac_sysctl_redir_num) {
   1829 		if (v & ~((0x7 << (4 * 0)) | (0x7 << (4 * 1)) | (0x7 << (4 * 2))))
   1830 			return EINVAL;
   1831 		*(int *)rnode->sysctl_data = v;
   1832 		s = splnet();
   1833 		for (i = 0; i < 3/* XXX */; i++) {
   1834 			struct ifnet *ifp;
   1835 
   1836 			sc = cnmac_gsc[i];	/* XXX */
   1837 			ifp = &sc->sc_ethercom.ec_if;
   1838 
   1839 			sc->sc_redir
   1840 			    = (cnmac_param_redir >> (4 * i)) & 0x7;
   1841 			if (sc->sc_redir == 0) {
   1842 				if (ISSET(ifp->if_flags, IFF_PROMISC)) {
   1843 					CLR(ifp->if_flags, IFF_PROMISC);
   1844 					cnmac_mii_statchg(ifp);
   1845 					/* octgmx_set_filter(sc->sc_gmx_port); */
   1846 				}
   1847 				ifp->_if_input = ether_input;
   1848 			}
   1849 			else {
   1850 				if (!ISSET(ifp->if_flags, IFF_PROMISC)) {
   1851 					SET(ifp->if_flags, IFF_PROMISC);
   1852 					cnmac_mii_statchg(ifp);
   1853 					/* octgmx_set_filter(sc->sc_gmx_port); */
   1854 				}
   1855 				ifp->_if_input = cnmac_recv_redir;
   1856 			}
   1857 		}
   1858 		splx(s);
   1859 		return 0;
   1860 	}
   1861 
   1862 	return EINVAL;
   1863 }
   1864 
   1865 static int
   1866 cnmac_sysctl_pool(SYSCTLFN_ARGS)
   1867 {
   1868 	int error, newval = 0;
   1869 	struct sysctlnode node;
   1870 	int s;
   1871 
   1872 	node = *rnode;
   1873 	node.sysctl_data = &newval;
   1874 	s = splnet();
   1875 	if (node.sysctl_num == cnmac_sysctl_pkt_pool_num) {
   1876 		error = octfpa_available_fpa_pool(&newval,
   1877 		    OCTEON_POOL_NO_PKT);
   1878 	} else if (node.sysctl_num == cnmac_sysctl_wqe_pool_num) {
   1879 		error = octfpa_available_fpa_pool(&newval,
   1880 		    OCTEON_POOL_NO_WQE);
   1881 	} else if (node.sysctl_num == cnmac_sysctl_cmd_pool_num) {
   1882 		error = octfpa_available_fpa_pool(&newval,
   1883 		    OCTEON_POOL_NO_CMD);
   1884 	} else if (node.sysctl_num == cnmac_sysctl_sg_pool_num) {
   1885 		error = octfpa_available_fpa_pool(&newval,
   1886 		    OCTEON_POOL_NO_SG);
   1887 	} else {
   1888 		splx(s);
   1889 		return EINVAL;
   1890 	}
   1891 	splx(s);
   1892 	if (error)
   1893 		return error;
   1894 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1895 	if (error || newp == NULL)
   1896 		return error;
   1897 
   1898 	return 0;
   1899 }
   1900 
   1901 static int
   1902 cnmac_sysctl_rd(SYSCTLFN_ARGS)
   1903 {
   1904 	int error, v;
   1905 	struct sysctlnode node;
   1906 	int s;
   1907 
   1908 	node = *rnode;
   1909 	v = *(int *)rnode->sysctl_data;
   1910 	node.sysctl_data = &v;
   1911 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1912 	if (error || newp != NULL)
   1913 		return error;
   1914 
   1915 	if (node.sysctl_num == cnmac_sysctl_pktbuf_num) {
   1916 		uint64_t tmp;
   1917 		int n;
   1918 
   1919 		s = splnet();
   1920 		tmp = octfpa_query(0);
   1921 		n = (int)tmp;
   1922 		splx(s);
   1923 		*(int *)rnode->sysctl_data = n;
   1924 		cnmac_param_pktbuf = n;
   1925 		*(int *)oldp = n;
   1926 		return 0;
   1927 	}
   1928 
   1929 	return EINVAL;
   1930 }
   1931