16716431Robert Mustacchi/*
26716431Robert Mustacchi * udmfE_usbgem.c : Davicom DM9601E USB to Fast Ethernet Driver for Solaris
36716431Robert Mustacchi *
46716431Robert Mustacchi * Copyright (c) 2009-2012 Masayuki Murayama.  All rights reserved.
56716431Robert Mustacchi *
66716431Robert Mustacchi * Redistribution and use in source and binary forms, with or without
76716431Robert Mustacchi * modification, are permitted provided that the following conditions are met:
86716431Robert Mustacchi *
96716431Robert Mustacchi * 1. Redistributions of source code must retain the above copyright notice,
106716431Robert Mustacchi *    this list of conditions and the following disclaimer.
116716431Robert Mustacchi *
126716431Robert Mustacchi * 2. Redistributions in binary form must reproduce the above copyright notice,
136716431Robert Mustacchi *    this list of conditions and the following disclaimer in the documentation
146716431Robert Mustacchi *    and/or other materials provided with the distribution.
156716431Robert Mustacchi *
166716431Robert Mustacchi * 3. Neither the name of the author nor the names of its contributors may be
176716431Robert Mustacchi *    used to endorse or promote products derived from this software without
186716431Robert Mustacchi *    specific prior written permission.
196716431Robert Mustacchi *
206716431Robert Mustacchi * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
216716431Robert Mustacchi * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
226716431Robert Mustacchi * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
236716431Robert Mustacchi * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
246716431Robert Mustacchi * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
256716431Robert Mustacchi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
266716431Robert Mustacchi * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
276716431Robert Mustacchi * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
286716431Robert Mustacchi * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
296716431Robert Mustacchi * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
306716431Robert Mustacchi * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
316716431Robert Mustacchi * DAMAGE.
326716431Robert Mustacchi */
336716431Robert Mustacchi
346716431Robert Mustacchi/*
356716431Robert Mustacchi *  Changelog:
366716431Robert Mustacchi */
376716431Robert Mustacchi
386716431Robert Mustacchi/*
396716431Robert Mustacchi * TODO
406716431Robert Mustacchi */
416716431Robert Mustacchi/* ======================================================= */
426716431Robert Mustacchi
436716431Robert Mustacchi/*
446716431Robert Mustacchi * Solaris system header files and macros
456716431Robert Mustacchi */
466716431Robert Mustacchi
476716431Robert Mustacchi/* minimum kernel headers for drivers */
486716431Robert Mustacchi#include <sys/types.h>
496716431Robert Mustacchi#include <sys/conf.h>
506716431Robert Mustacchi#include <sys/debug.h>
516716431Robert Mustacchi#include <sys/kmem.h>
526716431Robert Mustacchi#include <sys/modctl.h>
536716431Robert Mustacchi#include <sys/errno.h>
546716431Robert Mustacchi#include <sys/ddi.h>
556716431Robert Mustacchi#include <sys/sunddi.h>
566716431Robert Mustacchi#include <sys/byteorder.h>
576716431Robert Mustacchi
586716431Robert Mustacchi/* ethernet stuff */
596716431Robert Mustacchi#include <sys/ethernet.h>
606716431Robert Mustacchi
616716431Robert Mustacchi/* interface card depend stuff */
626716431Robert Mustacchi#include <sys/stropts.h>
636716431Robert Mustacchi#include <sys/stream.h>
646716431Robert Mustacchi#include <sys/strlog.h>
656716431Robert Mustacchi#include <sys/strsun.h>
666716431Robert Mustacchi#include <sys/usb/usba.h>
676716431Robert Mustacchi#include "usbgem.h"
686716431Robert Mustacchi
696716431Robert Mustacchi/* hardware stuff */
706716431Robert Mustacchi#include "usbgem_mii.h"
716716431Robert Mustacchi#include "dm9601reg.h"
726716431Robert Mustacchi
736716431Robert Mustacchichar	ident[] = "dm9601 usbnic driver v" VERSION;
746716431Robert Mustacchi
756716431Robert Mustacchi/*
766716431Robert Mustacchi * Useful macros
776716431Robert Mustacchi */
786716431Robert Mustacchi#define	CHECK_AND_JUMP(err, label)	if (err != USB_SUCCESS) goto label
796716431Robert Mustacchi#define	LE16P(p)	((((uint8_t *)(p))[1] << 8) | ((uint8_t *)(p))[0])
806716431Robert Mustacchi
816716431Robert Mustacchi/*
826716431Robert Mustacchi * Debugging
836716431Robert Mustacchi */
846716431Robert Mustacchi#ifdef DEBUG_LEVEL
856716431Robert Mustacchistatic int udmf_debug = DEBUG_LEVEL;
866716431Robert Mustacchi#define	DPRINTF(n, args)	if (udmf_debug > (n)) cmn_err args
876716431Robert Mustacchi#else
886716431Robert Mustacchi#define	DPRINTF(n, args)
896716431Robert Mustacchi#endif
906716431Robert Mustacchi
916716431Robert Mustacchi/*
926716431Robert Mustacchi * Our configration for dm9601
936716431Robert Mustacchi */
946716431Robert Mustacchi/* timeouts */
956716431Robert Mustacchi#define	ONESEC	(drv_usectohz(1*1000000))
966716431Robert Mustacchi
976716431Robert Mustacchi/*
986716431Robert Mustacchi * Local device definitions
996716431Robert Mustacchi */
1006716431Robert Mustacchistruct udmf_dev {
1016716431Robert Mustacchi	/*
1026716431Robert Mustacchi	 * Misc HW information
1036716431Robert Mustacchi	 */
1046716431Robert Mustacchi	uint8_t	rcr;
1056716431Robert Mustacchi	uint8_t	last_nsr;
1066716431Robert Mustacchi	uint8_t	mac_addr[ETHERADDRL];
1076716431Robert Mustacchi};
1086716431Robert Mustacchi
1096716431Robert Mustacchi/*
1106716431Robert Mustacchi * private functions
1116716431Robert Mustacchi */
1126716431Robert Mustacchi
1136716431Robert Mustacchi/* mii operations */
1146716431Robert Mustacchistatic uint16_t udmf_mii_read(struct usbgem_dev *, uint_t, int *errp);
1156716431Robert Mustacchistatic void udmf_mii_write(struct usbgem_dev *, uint_t, uint16_t, int *errp);
1166716431Robert Mustacchi
1176716431Robert Mustacchi/* nic operations */
1186716431Robert Mustacchistatic int udmf_reset_chip(struct usbgem_dev *);
1196716431Robert Mustacchistatic int udmf_init_chip(struct usbgem_dev *);
1206716431Robert Mustacchistatic int udmf_start_chip(struct usbgem_dev *);
1216716431Robert Mustacchistatic int udmf_stop_chip(struct usbgem_dev *);
1226716431Robert Mustacchistatic int udmf_set_media(struct usbgem_dev *);
1236716431Robert Mustacchistatic int udmf_set_rx_filter(struct usbgem_dev *);
1246716431Robert Mustacchistatic int udmf_get_stats(struct usbgem_dev *);
1256716431Robert Mustacchistatic void udmf_interrupt(struct usbgem_dev *, mblk_t *);
1266716431Robert Mustacchi
1276716431Robert Mustacchi/* packet operations */
1286716431Robert Mustacchistatic mblk_t *udmf_tx_make_packet(struct usbgem_dev *, mblk_t *);
1296716431Robert Mustacchistatic mblk_t *udmf_rx_make_packet(struct usbgem_dev *, mblk_t *);
1306716431Robert Mustacchi
1316716431Robert Mustacchi/* =============================================================== */
1326716431Robert Mustacchi/*
1336716431Robert Mustacchi * I/O functions
1346716431Robert Mustacchi */
1356716431Robert Mustacchi/* =============================================================== */
1366716431Robert Mustacchi#define	OUT(dp, ix, len, buf, errp, label)	\
1376716431Robert Mustacchi	if ((*(errp) = usbgem_ctrl_out((dp), 	\
1386716431Robert Mustacchi	/* bmRequestType */ USB_DEV_REQ_HOST_TO_DEV	\
1396716431Robert Mustacchi		    | USB_DEV_REQ_TYPE_VENDOR | USB_DEV_REQ_RCPT_DEV,	\
1406716431Robert Mustacchi	/* bRequest */	1,	\
1416716431Robert Mustacchi	/* wValue */	0,	\
1426716431Robert Mustacchi	/* wIndex */	(ix),	\
1436716431Robert Mustacchi	/* wLength */	(len),	\
1446716431Robert Mustacchi	/* value */	(buf),	\
1456716431Robert Mustacchi	/* size */	(len))) != USB_SUCCESS) goto label
1466716431Robert Mustacchi
1476716431Robert Mustacchi#define	OUTB(dp, ix, val, errp, label)	\
1486716431Robert Mustacchi	if ((*(errp) = usbgem_ctrl_out((dp), 	\
1496716431Robert Mustacchi	/* bmRequestType */ USB_DEV_REQ_HOST_TO_DEV	\
1506716431Robert Mustacchi		    | USB_DEV_REQ_TYPE_VENDOR | USB_DEV_REQ_RCPT_DEV,	\
1516716431Robert Mustacchi	/* bRequest */	3,	\
1526716431Robert Mustacchi	/* wValue */	(val),	\
1536716431Robert Mustacchi	/* wIndex */	(ix),	\
1546716431Robert Mustacchi	/* wLength */	0,	\
1556716431Robert Mustacchi	/* value */	NULL,	\
1566716431Robert Mustacchi	/* size */	0)) != USB_SUCCESS) goto label
1576716431Robert Mustacchi
1586716431Robert Mustacchi#define	IN(dp, ix, len, buf, errp, label)	\
1596716431Robert Mustacchi	if ((*(errp) = usbgem_ctrl_in((dp), 	\
1606716431Robert Mustacchi	/* bmRequestType */ USB_DEV_REQ_DEV_TO_HOST	\
1616716431Robert Mustacchi		    | USB_DEV_REQ_TYPE_VENDOR | USB_DEV_REQ_RCPT_DEV,	\
1626716431Robert Mustacchi	/* bRequest */	0,	\
1636716431Robert Mustacchi	/* wValue */	0,	\
1646716431Robert Mustacchi	/* wIndex */	(ix),	\
1656716431Robert Mustacchi	/* wLength */	(len),	\
1666716431Robert Mustacchi	/* valuep */	(buf),	\
1676716431Robert Mustacchi	/* size */	(len))) != USB_SUCCESS) goto label
1686716431Robert Mustacchi
1696716431Robert Mustacchi/* =============================================================== */
1706716431Robert Mustacchi/*
1716716431Robert Mustacchi * Hardware manupilation
1726716431Robert Mustacchi */
1736716431Robert Mustacchi/* =============================================================== */
1746716431Robert Mustacchistatic void
1756716431Robert Mustacchiudmf_enable_phy(struct usbgem_dev *dp)
1766716431Robert Mustacchi{
1776716431Robert Mustacchi	int	err = USB_SUCCESS;
1786716431Robert Mustacchi
1796716431Robert Mustacchi	/* de-assert reset signal to phy */
1806716431Robert Mustacchi	OUTB(dp, GPCR, GPCR_OUT(0), &err, usberr);
1816716431Robert Mustacchi	OUTB(dp, GPR, 0, &err, usberr);
1826716431Robert Mustacchiusberr:
1836716431Robert Mustacchi	;
1846716431Robert Mustacchi}
1856716431Robert Mustacchi
1866716431Robert Mustacchistatic int
1876716431Robert Mustacchiudmf_reset_chip(struct usbgem_dev *dp)
1886716431Robert Mustacchi{
1896716431Robert Mustacchi	int	err = USB_SUCCESS;
1906716431Robert Mustacchi
1916716431Robert Mustacchi	DPRINTF(2, (CE_CONT, "!%s: %s: called", dp->name, __func__));
1926716431Robert Mustacchi
1936716431Robert Mustacchi	OUTB(dp, NCR, NCR_LBK_NORMAL | NCR_RST, &err, usberr);
1946716431Robert Mustacchi	drv_usecwait(100);
1956716431Robert Mustacchiusberr:
1966716431Robert Mustacchi	return (err);
1976716431Robert Mustacchi}
1986716431Robert Mustacchi
1996716431Robert Mustacchi/*
2006716431Robert Mustacchi * Setup dm9601
2016716431Robert Mustacchi */
2026716431Robert Mustacchistatic int
2036716431Robert Mustacchiudmf_init_chip(struct usbgem_dev *dp)
2046716431Robert Mustacchi{
2056716431Robert Mustacchi	int		i;
2066716431Robert Mustacchi	uint32_t	val;
2076716431Robert Mustacchi	int		err = USB_SUCCESS;
2086716431Robert Mustacchi	uint16_t	reg;
2096716431Robert Mustacchi	uint8_t		buf[2];
2106716431Robert Mustacchi	struct udmf_dev	*lp = dp->private;
2116716431Robert Mustacchi
2126716431Robert Mustacchi	DPRINTF(2, (CE_CONT, "!%s: %s: called", dp->name, __func__));
2136716431Robert Mustacchi
2146716431Robert Mustacchi	OUTB(dp, NCR, NCR_LBK_NORMAL, &err, usberr);
2156716431Robert Mustacchi
2166716431Robert Mustacchi	/* tx control regiser: enable padding and crc generation */
2176716431Robert Mustacchi	OUTB(dp, TCR, 0, &err, usberr);
2186716431Robert Mustacchi
2196716431Robert Mustacchi	/* rx control register: will be set later by udmf_set_rx_filer() */
2206716431Robert Mustacchi	lp->rcr = RCR_RUNT;
2216716431Robert Mustacchi
2226716431Robert Mustacchi	/* back pressure threshold: */
2236716431Robert Mustacchi	OUTB(dp, BPTR, (2 << BPTR_BPHW_SHIFT) | BPTR_JPT_200us,
2246716431Robert Mustacchi	    &err, usberr);
2256716431Robert Mustacchi
2266716431Robert Mustacchi	/* flow control threshold: same as default */
2276716431Robert Mustacchi	OUTB(dp, FCTR, (3 << FCTR_HWOT_SHIFT) | (8 << FCTR_LWOT_SHIFT),
2286716431Robert Mustacchi	    &err, usberr);
2296716431Robert Mustacchi
2306716431Robert Mustacchi	/* usb control register */
2316716431Robert Mustacchi	OUTB(dp, USBC, USBC_EP3ACK | 0x06, &err, usberr);
2326716431Robert Mustacchi
2336716431Robert Mustacchi	/* flow control: will be set later by udmf_set_media() */
2346716431Robert Mustacchi
2356716431Robert Mustacchi	/* wake up control register: */
2366716431Robert Mustacchi	OUTB(dp, WCR, 0, &err, usberr);
2376716431Robert Mustacchi
2386716431Robert Mustacchiusberr:
2396716431Robert Mustacchi	DPRINTF(2, (CE_CONT, "!%s: %s: end err:%d(%s)",
2406716431Robert Mustacchi	    dp->name, __func__,
2416716431Robert Mustacchi	    err, err == USB_SUCCESS ? "success" : "error"));
2426716431Robert Mustacchi	return (err);
2436716431Robert Mustacchi}
2446716431Robert Mustacchi
2456716431Robert Mustacchistatic int
2466716431Robert Mustacchiudmf_start_chip(struct usbgem_dev *dp)
2476716431Robert Mustacchi{
2486716431Robert Mustacchi	int	err = USB_SUCCESS;
2496716431Robert Mustacchi	struct udmf_dev	*lp = dp->private;
2506716431Robert Mustacchi
2516716431Robert Mustacchi	/* enable Rx */
2526716431Robert Mustacchi	lp->rcr |= RCR_RXEN;
2536716431Robert Mustacchi	OUTB(dp, RCR, lp->rcr, &err, usberr);
2546716431Robert Mustacchi
2556716431Robert Mustacchiusberr:
2566716431Robert Mustacchi	DPRINTF(2, (CE_CONT, "!%s: %s: end err:%d(%s)",
2576716431Robert Mustacchi	    dp->name, __func__,
2586716431Robert Mustacchi	    err, err == USB_SUCCESS ? "success" : "error"));
2596716431Robert Mustacchi	return (err);
2606716431Robert Mustacchi}
2616716431Robert Mustacchi
2626716431Robert Mustacchistatic int
2636716431Robert Mustacchiudmf_stop_chip(struct usbgem_dev *dp)
2646716431Robert Mustacchi{
2656716431Robert Mustacchi	int	err = USB_SUCCESS;
2666716431Robert Mustacchi	struct udmf_dev	*lp = dp->private;
2676716431Robert Mustacchi
2686716431Robert Mustacchi	/* disable rx */
2696716431Robert Mustacchi	lp->rcr &= ~RCR_RXEN;
2706716431Robert Mustacchi	OUTB(dp, RCR, lp->rcr, &err, usberr);
2716716431Robert Mustacchi
2726716431Robert Mustacchiusberr:
2736716431Robert Mustacchi	DPRINTF(2, (CE_CONT, "!%s: %s: end err:%d(%s)",
2746716431Robert Mustacchi	    dp->name, __func__,
2756716431Robert Mustacchi	    err, err == USB_SUCCESS ? "success" : "error"));
2766716431Robert Mustacchi	return (err);
2776716431Robert Mustacchi}
2786716431Robert Mustacchi
2796716431Robert Mustacchistatic int
2806716431Robert Mustacchiudmf_get_stats(struct usbgem_dev *dp)
2816716431Robert Mustacchi{
2826716431Robert Mustacchi	/* empty */
2836716431Robert Mustacchi	return (USB_SUCCESS);
2846716431Robert Mustacchi}
2856716431Robert Mustacchi
2866716431Robert Mustacchistatic uint_t
2876716431Robert Mustacchiudmf_mcast_hash(struct usbgem_dev *dp, const uint8_t *addr)
2886716431Robert Mustacchi{
2896716431Robert Mustacchi	return (usbgem_ether_crc_le(addr) & 0x3f);
2906716431Robert Mustacchi}
2916716431Robert Mustacchi
2926716431Robert Mustacchistatic int
2936716431Robert Mustacchiudmf_set_rx_filter(struct usbgem_dev *dp)
2946716431Robert Mustacchi{
2956716431Robert Mustacchi	int		i;
2966716431Robert Mustacchi	uint8_t		rcr;
2976716431Robert Mustacchi	uint8_t		mode;
2986716431Robert Mustacchi	uint8_t		mhash[8];
2996716431Robert Mustacchi	uint8_t		*mac;
3006716431Robert Mustacchi	uint_t		h;
3016716431Robert Mustacchi	int		err = USB_SUCCESS;
3026716431Robert Mustacchi	struct udmf_dev	*lp = dp->private;
3036716431Robert Mustacchi	static uint8_t	invalid_mac[ETHERADDRL] = {0, 0, 0, 0, 0, 0};
3046716431Robert Mustacchi
3056716431Robert Mustacchi	DPRINTF(2, (CE_CONT, "!%s: %s: called, rxmode:%x",
3066716431Robert Mustacchi	    dp->name, __func__, dp->rxmode));
3076716431Robert Mustacchi
3086716431Robert Mustacchi	if (lp->rcr & RCR_RXEN) {
3096716431Robert Mustacchi		/* set promiscuous mode before changing rx filter mode */
3106716431Robert Mustacchi		OUTB(dp, RCR, lp->rcr | RCR_PRMSC, &err, usberr);
3116716431Robert Mustacchi	}
3126716431Robert Mustacchi
3136716431Robert Mustacchi	lp->rcr &= ~(RCR_ALL | RCR_PRMSC);
3146716431Robert Mustacchi	mode = 0;
3156716431Robert Mustacchi	bzero(mhash, sizeof (mhash));
3166716431Robert Mustacchi	mac = dp->cur_addr.ether_addr_octet;
3176716431Robert Mustacchi
3186716431Robert Mustacchi	if ((dp->rxmode & RXMODE_ENABLE) == 0) {
3196716431Robert Mustacchi		mac = invalid_mac;
3206716431Robert Mustacchi	} else if (dp->rxmode & RXMODE_PROMISC) {
3216716431Robert Mustacchi		/* promiscious mode implies all multicast and all physical */
3226716431Robert Mustacchi		mode |= RCR_PRMSC;
3236716431Robert Mustacchi	} else if ((dp->rxmode & RXMODE_ALLMULTI) || dp->mc_count > 32) {
3246716431Robert Mustacchi		/* accept all multicast packets */
3256716431Robert Mustacchi		mode |= RCR_ALL;
3266716431Robert Mustacchi	} else if (dp->mc_count > 0) {
3276716431Robert Mustacchi		/*
3286716431Robert Mustacchi		 * make hash table to select interresting
3296716431Robert Mustacchi		 * multicast address only.
3306716431Robert Mustacchi		 */
3316716431Robert Mustacchi		for (i = 0; i < dp->mc_count; i++) {
3326716431Robert Mustacchi			/* hash table is 64 = 2^6 bit width */
3336716431Robert Mustacchi			h = dp->mc_list[i].hash;
3346716431Robert Mustacchi			mhash[h / 8] |= 1 << (h % 8);
3356716431Robert Mustacchi		}
3366716431Robert Mustacchi	}
3376716431Robert Mustacchi
3386716431Robert Mustacchi	/* set node address */
3396716431Robert Mustacchi	if (bcmp(mac, lp->mac_addr, ETHERADDRL) != 0) {
3406716431Robert Mustacchi		OUT(dp, PAR, ETHERADDRL, dp->cur_addr.ether_addr_octet,
3416716431Robert Mustacchi		    &err, usberr);
3426716431Robert Mustacchi		bcopy(mac, lp->mac_addr, ETHERADDRL);
3436716431Robert Mustacchi	}
3446716431Robert Mustacchi
3456716431Robert Mustacchi	/* set multicast hash table */
3466716431Robert Mustacchi	OUT(dp, MAR, sizeof (mhash), &mhash[0], &err, usberr);
3476716431Robert Mustacchi
3486716431Robert Mustacchi	/* update rcr */
3496716431Robert Mustacchi	lp->rcr |= mode;
3506716431Robert Mustacchi	OUTB(dp, RCR, lp->rcr, &err, usberr);
3516716431Robert Mustacchi
3526716431Robert Mustacchi#if DEBUG_LEVEL > 1
3536716431Robert Mustacchi	/* verify rcr */
3546716431Robert Mustacchi	IN(dp, RCR, 1, &rcr, &err, usberr);
3556716431Robert Mustacchi	cmn_err(CE_CONT, "!%s: %s: rcr:%b returned",
3566716431Robert Mustacchi	    dp->name, __func__, rcr, RCR_BITS);
3576716431Robert Mustacchi#endif
3586716431Robert Mustacchiusberr:
3596716431Robert Mustacchi	DPRINTF(2, (CE_CONT, "!%s: %s: end err:%d(%s)",
3606716431Robert Mustacchi	    dp->name, __func__,
3616716431Robert Mustacchi	    err, err == USB_SUCCESS ? "success" : "error"));
3626716431Robert Mustacchi	return (err);
3636716431Robert Mustacchi}
3646716431Robert Mustacchi
3656716431Robert Mustacchistatic int
3666716431Robert Mustacchiudmf_set_media(struct usbgem_dev *dp)
3676716431Robert Mustacchi{
3686716431Robert Mustacchi	int	err = USB_SUCCESS;
3696716431Robert Mustacchi	uint8_t	fcr;
3706716431Robert Mustacchi	struct udmf_dev	*lp = dp->private;
3716716431Robert Mustacchi
3726716431Robert Mustacchi	DPRINTF(0, (CE_CONT, "!%s: %s: called", dp->name, __func__));
3736716431Robert Mustacchi
3746716431Robert Mustacchi	/* setup flow control */
3756716431Robert Mustacchi	fcr = 0;
3766716431Robert Mustacchi	if (dp->full_duplex) {
3776716431Robert Mustacchi		/* select flow control */
3786716431Robert Mustacchi		switch (dp->flow_control) {
3796716431Robert Mustacchi		case FLOW_CONTROL_RX_PAUSE:
3806716431Robert Mustacchi			fcr |= FCR_FLCE;
3816716431Robert Mustacchi			break;
3826716431Robert Mustacchi
3836716431Robert Mustacchi		case FLOW_CONTROL_TX_PAUSE:
3846716431Robert Mustacchi			fcr |= FCR_TXPEN;
3856716431Robert Mustacchi			break;
3866716431Robert Mustacchi
3876716431Robert Mustacchi		case FLOW_CONTROL_SYMMETRIC:
3886716431Robert Mustacchi			fcr |= FCR_FLCE | FCR_TXPEN;
3896716431Robert Mustacchi			break;
3906716431Robert Mustacchi		}
3916716431Robert Mustacchi	}
3926716431Robert Mustacchi
3936716431Robert Mustacchi	/* update flow control register */
3946716431Robert Mustacchi	OUTB(dp, FCR, fcr, &err, usberr);
3956716431Robert Mustacchi
3966716431Robert Mustacchiusberr:
3976716431Robert Mustacchi	DPRINTF(2, (CE_CONT, "!%s: %s: end err:%d(%s)",
3986716431Robert Mustacchi	    dp->name, __func__,
3996716431Robert Mustacchi	    err, err == USB_SUCCESS ? "success" : "error"));
4006716431Robert Mustacchi	return (err);
4016716431Robert Mustacchi}
4026716431Robert Mustacchi
4036716431Robert Mustacchi/*
4046716431Robert Mustacchi * send/receive packet check
4056716431Robert Mustacchi */
4066716431Robert Mustacchistatic mblk_t *
4076716431Robert Mustacchiudmf_tx_make_packet(struct usbgem_dev *dp, mblk_t *mp)
4086716431Robert Mustacchi{
4096716431Robert Mustacchi	int		n;
4106716431Robert Mustacchi	size_t		pkt_size;
4116716431Robert Mustacchi	mblk_t		*new;
4126716431Robert Mustacchi	mblk_t		*tp;
4136716431Robert Mustacchi	uint8_t		*bp;
4146716431Robert Mustacchi	uint8_t		*last_pos;
4156716431Robert Mustacchi	uint_t		align_mask;
4166716431Robert Mustacchi
4176716431Robert Mustacchi	pkt_size = msgdsize(mp);
4186716431Robert Mustacchi	align_mask = 63;
4196716431Robert Mustacchi
4206716431Robert Mustacchi	/*
4216716431Robert Mustacchi	 * re-allocate the mp
4226716431Robert Mustacchi	 */
4236716431Robert Mustacchi
4246716431Robert Mustacchi	/* minimum ethernet packet size of ETHERMIN */
4256716431Robert Mustacchi	pkt_size = max(pkt_size, ETHERMIN);
4266716431Robert Mustacchi
4276716431Robert Mustacchi#if 0 /* CONFIG_ADD_TX_DELIMITOR_ALWAYS */
4286716431Robert Mustacchi	pkt_size += TX_HEADER_SIZE;
4296716431Robert Mustacchi#endif
4306716431Robert Mustacchi	if (((pkt_size + TX_HEADER_SIZE) & align_mask) == 0) {
4316716431Robert Mustacchi		/* padding is required in usb communication */
4326716431Robert Mustacchi		pkt_size += TX_HEADER_SIZE;
4336716431Robert Mustacchi	}
4346716431Robert Mustacchi
4356716431Robert Mustacchi	if ((new = allocb(TX_HEADER_SIZE + pkt_size, 0)) == NULL) {
4366716431Robert Mustacchi		return (NULL);
4376716431Robert Mustacchi	}
4386716431Robert Mustacchi	new->b_wptr = new->b_rptr + TX_HEADER_SIZE + pkt_size;
4396716431Robert Mustacchi
4406716431Robert Mustacchi	/* add a header */
4416716431Robert Mustacchi	bp = new->b_rptr;
4426716431Robert Mustacchi	bp[0] = (uint8_t)pkt_size;
4436716431Robert Mustacchi	bp[1] = (uint8_t)(pkt_size >> 8);
4446716431Robert Mustacchi	bp += TX_HEADER_SIZE;
4456716431Robert Mustacchi
4466716431Robert Mustacchi	/* copy contents of the buffer */
4476716431Robert Mustacchi	for (tp = mp; tp; tp = tp->b_cont) {
4486716431Robert Mustacchi		n = MBLKL(tp);
4496716431Robert Mustacchi		bcopy(tp->b_rptr, bp, n);
4506716431Robert Mustacchi		bp += n;
4516716431Robert Mustacchi	}
4526716431Robert Mustacchi
4536716431Robert Mustacchi	/* clear the rest including the next zero length header */
4546716431Robert Mustacchi	last_pos = new->b_wptr;
4556716431Robert Mustacchi	while (bp < last_pos) {
4566716431Robert Mustacchi		*bp++ = 0;
4576716431Robert Mustacchi	}
4586716431Robert Mustacchi
4596716431Robert Mustacchi	return (new);
4606716431Robert Mustacchi}
4616716431Robert Mustacchi
4626716431Robert Mustacchistatic void
4636716431Robert Mustacchiudmf_dump_packet(struct usbgem_dev *dp, uint8_t *bp, int n)
4646716431Robert Mustacchi{
4656716431Robert Mustacchi	int	i;
4666716431Robert Mustacchi
4676716431Robert Mustacchi	for (i = 0; i < n; i += 8, bp += 8) {
4686716431Robert Mustacchi		cmn_err(CE_CONT, "%02x %02x %02x %02x %02x %02x %02x %02x",
4696716431Robert Mustacchi		    bp[0], bp[1], bp[2], bp[3], bp[4], bp[5], bp[6], bp[7]);
4706716431Robert Mustacchi	}
4716716431Robert Mustacchi}
4726716431Robert Mustacchi
4736716431Robert Mustacchistatic mblk_t *
4746716431Robert Mustacchiudmf_rx_make_packet(struct usbgem_dev *dp, mblk_t *mp)
4756716431Robert Mustacchi{
4766716431Robert Mustacchi	size_t len;
4776716431Robert Mustacchi	uint8_t	rx_stat;
4786716431Robert Mustacchi
4796716431Robert Mustacchi	len = MBLKL(mp);
4806716431Robert Mustacchi
4816716431Robert Mustacchi	if (len <= RX_HEADER_SIZE) {
4826716431Robert Mustacchi		/*
4836716431Robert Mustacchi		 * the usb bulk-in frame doesn't include a valid
4846716431Robert Mustacchi		 * ethernet packet.
4856716431Robert Mustacchi		 */
4866716431Robert Mustacchi		return (NULL);
4876716431Robert Mustacchi	}
4886716431Robert Mustacchi
4896716431Robert Mustacchi	/* remove rx header */
4906716431Robert Mustacchi	rx_stat = mp->b_rptr[0];
4916716431Robert Mustacchi	if (rx_stat & (RSR_RF |  RSR_LCS | RSR_RWTO |
4926716431Robert Mustacchi	    RSR_PLE | RSR_AE | RSR_CE |  RSR_FOE)) {
4936716431Robert Mustacchi		if (rx_stat & RSR_RF) {
4946716431Robert Mustacchi			dp->stats.runt++;
4956716431Robert Mustacchi		}
4966716431Robert Mustacchi		if (rx_stat & RSR_LCS) {
4976716431Robert Mustacchi			/* late collision */
4986716431Robert Mustacchi			dp->stats.rcv_internal_err++;
4996716431Robert Mustacchi		}
5006716431Robert Mustacchi		if (rx_stat & RSR_RWTO) {
5016716431Robert Mustacchi			/* rx timeout */
5026716431Robert Mustacchi			dp->stats.rcv_internal_err++;
5036716431Robert Mustacchi		}
5046716431Robert Mustacchi		if (rx_stat & RSR_PLE) {
5056716431Robert Mustacchi			/* physical layer error */
5066716431Robert Mustacchi			dp->stats.rcv_internal_err++;
5076716431Robert Mustacchi		}
5086716431Robert Mustacchi		if (rx_stat & RSR_AE) {
5096716431Robert Mustacchi			/* alignment error */
5106716431Robert Mustacchi			dp->stats.frame++;
5116716431Robert Mustacchi		}
5126716431Robert Mustacchi		if (rx_stat & RSR_CE) {
5136716431Robert Mustacchi			/* crc error */
5146716431Robert Mustacchi			dp->stats.crc++;
5156716431Robert Mustacchi		}
5166716431Robert Mustacchi		if (rx_stat & RSR_FOE) {
5176716431Robert Mustacchi			/* fifo overflow error */
5186716431Robert Mustacchi			dp->stats.overflow++;
5196716431Robert Mustacchi		}
5206716431Robert Mustacchi		dp->stats.errrcv++;
5216716431Robert Mustacchi	}
5226716431Robert Mustacchi	len = LE16P(&mp->b_rptr[1]);
5236716431Robert Mustacchi	if (len >= ETHERFCSL) {
5246716431Robert Mustacchi		len -= ETHERFCSL;
5256716431Robert Mustacchi	}
5266716431Robert Mustacchi	mp->b_rptr += RX_HEADER_SIZE;
5276716431Robert Mustacchi	mp->b_wptr = mp->b_rptr + len;
5286716431Robert Mustacchi
5296716431Robert Mustacchi	return (mp);
5306716431Robert Mustacchi}
5316716431Robert Mustacchi
5326716431Robert Mustacchi/*
5336716431Robert Mustacchi * MII Interfaces
5346716431Robert Mustacchi */
5356716431Robert Mustacchistatic uint16_t
5366716431Robert Mustacchiudmf_ep_read(struct usbgem_dev *dp, uint_t which, uint_t addr, int *errp)
5376716431Robert Mustacchi{
5386716431Robert Mustacchi	int	i;
5396716431Robert Mustacchi	uint8_t	epcr;
5406716431Robert Mustacchi	uint16_t	val;
5416716431Robert Mustacchi
5426716431Robert Mustacchi	DPRINTF(4, (CE_CONT, "!%s: %s: called, ix:%d",
5436716431Robert Mustacchi	    dp->name, __func__, addr));
5446716431Robert Mustacchi
5456716431Robert Mustacchi	OUTB(dp, EPAR, addr, errp, usberr);
5466716431Robert Mustacchi	OUTB(dp, EPCR, which | EPCR_ERPRR, errp, usberr);
5476716431Robert Mustacchi
5486716431Robert Mustacchi	for (i = 0; i < 100; i++) {
5496716431Robert Mustacchi		IN(dp, EPCR, sizeof (epcr), &epcr, errp, usberr);
5506716431Robert Mustacchi		if ((epcr & EPCR_ERRE) == 0) {
5516716431Robert Mustacchi			/* done */
5526716431Robert Mustacchi			IN(dp, EPDR, sizeof (val), &val, errp, usberr);
5536716431Robert Mustacchi			val = LE_16(val);
5546716431Robert Mustacchi			goto done;
5556716431Robert Mustacchi		}
5566716431Robert Mustacchi		drv_usecwait(10);
5576716431Robert Mustacchi	}
5586716431Robert Mustacchi	/* timeout */
5596716431Robert Mustacchi	cmn_err(CE_WARN, "!%s: %s: timeout", dp->name, __func__);
5606716431Robert Mustacchi	val = 0;
5616716431Robert Mustacchidone:
5626716431Robert Mustacchi	OUTB(dp, EPCR, 0, errp, usberr);
5636716431Robert Mustacchi	return (val);
5646716431Robert Mustacchi
5656716431Robert Mustacchiusberr:
5666716431Robert Mustacchi	DPRINTF(2, (CE_CONT, "!%s: %s: end err:%d(%s)",
5676716431Robert Mustacchi	    dp->name, __func__,
5686716431Robert Mustacchi	    *errp, *errp == USB_SUCCESS ? "success" : "error"));
5696716431Robert Mustacchi	return (0);
5706716431Robert Mustacchi}
5716716431Robert Mustacchi
5726716431Robert Mustacchistatic void
5736716431Robert Mustacchiudmf_ep_write(struct usbgem_dev *dp, uint_t which, uint_t addr,
5746716431Robert Mustacchi    uint16_t val, int *errp)
5756716431Robert Mustacchi{
5766716431Robert Mustacchi	int	i;
5776716431Robert Mustacchi	uint8_t	epcr;
5786716431Robert Mustacchi
5796716431Robert Mustacchi	DPRINTF(5, (CE_CONT, "!%s: %s called", dp->name, __func__));
5806716431Robert Mustacchi
5816716431Robert Mustacchi	val = LE_16(val);
5826716431Robert Mustacchi	OUT(dp, EPDR, sizeof (val), &val, errp, usberr);
5836716431Robert Mustacchi
5846716431Robert Mustacchi	OUTB(dp, EPAR, addr, errp, usberr);
5856716431Robert Mustacchi
5866716431Robert Mustacchi	OUTB(dp, EPCR, which | EPCR_WEP | EPCR_ERPRW, errp, usberr);
5876716431Robert Mustacchi
5886716431Robert Mustacchi	for (i = 0; i < 100; i++) {
5896716431Robert Mustacchi		IN(dp, EPCR, 1, &epcr, errp, usberr);
5906716431Robert Mustacchi		if ((epcr & EPCR_ERRE) == 0) {
5916716431Robert Mustacchi			/* done */
5926716431Robert Mustacchi			goto done;
5936716431Robert Mustacchi		}
5946716431Robert Mustacchi		drv_usecwait(10);
5956716431Robert Mustacchi	}
5966716431Robert Mustacchi	/* timeout */
5976716431Robert Mustacchi	cmn_err(CE_WARN, "!%s: %s: timeout", dp->name, __func__);
5986716431Robert Mustacchidone:
5996716431Robert Mustacchi	OUTB(dp, EPCR, 0, errp, usberr);
6006716431Robert Mustacchi	return;
6016716431Robert Mustacchi
6026716431Robert Mustacchiusberr:
6036716431Robert Mustacchi	DPRINTF(2, (CE_CONT, "!%s: %s: end err:%d(%s)",
6046716431Robert Mustacchi	    dp->name, __func__,
6056716431Robert Mustacchi	    *errp, *errp == USB_SUCCESS ? "success" : "error"));
6066716431Robert Mustacchi}
6076716431Robert Mustacchi
6086716431Robert Mustacchistatic uint16_t
6096716431Robert Mustacchiudmf_mii_read(struct usbgem_dev *dp, uint_t index, int *errp)
6106716431Robert Mustacchi{
6116716431Robert Mustacchi	uint16_t	val;
6126716431Robert Mustacchi
6136716431Robert Mustacchi	val = udmf_ep_read(dp, EPCR_EPOS,
6146716431Robert Mustacchi	    (dp->mii_phy_addr << EPAR_PHYADR_SHIFT) | index, errp);
6156716431Robert Mustacchi
6166716431Robert Mustacchi	return (val);
6176716431Robert Mustacchi}
6186716431Robert Mustacchi
6196716431Robert Mustacchistatic void
6206716431Robert Mustacchiudmf_mii_write(struct usbgem_dev *dp, uint_t index, uint16_t val, int *errp)
6216716431Robert Mustacchi{
6226716431Robert Mustacchi	udmf_ep_write(dp, EPCR_EPOS,
6236716431Robert Mustacchi	    (dp->mii_phy_addr << EPAR_PHYADR_SHIFT) | index, val, errp);
6246716431Robert Mustacchi}
6256716431Robert Mustacchi
6266716431Robert Mustacchistatic void
6276716431Robert Mustacchiudmf_interrupt(struct usbgem_dev *dp, mblk_t *mp)
6286716431Robert Mustacchi{
6296716431Robert Mustacchi	struct intr_msg	*imp;
6306716431Robert Mustacchi	struct udmf_dev	*lp = dp->private;
6316716431Robert Mustacchi
6326716431Robert Mustacchi	imp = (struct intr_msg *)&mp->b_rptr[0];
6336716431Robert Mustacchi
6346716431Robert Mustacchi	DPRINTF(4, (CE_CONT,
6356716431Robert Mustacchi	    "!%s: %s: size:%d, nsr:%b tsr1:%b tsr2:%b"
6366716431Robert Mustacchi	    " rsr:%b rocr:%b rxc:%02x txc:%b gpr:%b",
6376716431Robert Mustacchi	    dp->name, __func__, mp->b_wptr - mp->b_rptr,
6386716431Robert Mustacchi	    imp->im_nsr, NSR_BITS,
6396716431Robert Mustacchi	    imp->im_tsr1, TSR_BITS,
6406716431Robert Mustacchi	    imp->im_tsr2, TSR_BITS,
6416716431Robert Mustacchi	    imp->im_rsr, RSR_BITS,
6426716431Robert Mustacchi	    imp->im_rocr, ROCR_BITS,
6436716431Robert Mustacchi	    imp->im_rxc,
6446716431Robert Mustacchi	    imp->im_txc, TUSR_BITS,
6456716431Robert Mustacchi	    imp->im_gpr, GPR_BITS));
6466716431Robert Mustacchi
6476716431Robert Mustacchi	if ((lp->last_nsr ^ imp->im_nsr) & NSR_LINKST) {
6486716431Robert Mustacchi		usbgem_mii_update_link(dp);
6496716431Robert Mustacchi	}
6506716431Robert Mustacchi
6516716431Robert Mustacchi	lp->last_nsr = imp->im_nsr;
6526716431Robert Mustacchi}
6536716431Robert Mustacchi
6546716431Robert Mustacchi/* ======================================================== */
6556716431Robert Mustacchi/*
6566716431Robert Mustacchi * OS depend (device driver DKI) routine
6576716431Robert Mustacchi */
6586716431Robert Mustacchi/* ======================================================== */
6596716431Robert Mustacchistatic uint16_t
6606716431Robert Mustacchiudmf_eeprom_read(struct usbgem_dev *dp, uint_t index, int *errp)
6616716431Robert Mustacchi{
6626716431Robert Mustacchi	uint16_t	val;
6636716431Robert Mustacchi
6646716431Robert Mustacchi	val = udmf_ep_read(dp, 0, index, errp);
6656716431Robert Mustacchi
6666716431Robert Mustacchi	return (val);
6676716431Robert Mustacchi}
6686716431Robert Mustacchi
6696716431Robert Mustacchi#ifdef DEBUG_LEVEL
6706716431Robert Mustacchistatic void
6716716431Robert Mustacchiudmf_eeprom_dump(struct usbgem_dev *dp, int size)
6726716431Robert Mustacchi{
6736716431Robert Mustacchi	int	i;
6746716431Robert Mustacchi	int	err;
6756716431Robert Mustacchi	uint16_t	w0, w1, w2, w3;
6766716431Robert Mustacchi
6776716431Robert Mustacchi	cmn_err(CE_CONT, "!%s: eeprom dump:", dp->name);
6786716431Robert Mustacchi
6796716431Robert Mustacchi	err = USB_SUCCESS;
6806716431Robert Mustacchi
6816716431Robert Mustacchi	for (i = 0; i < size; i += 4) {
6826716431Robert Mustacchi		w0 = udmf_eeprom_read(dp, i + 0, &err);
6836716431Robert Mustacchi		w1 = udmf_eeprom_read(dp, i + 1, &err);
6846716431Robert Mustacchi		w2 = udmf_eeprom_read(dp, i + 2, &err);
6856716431Robert Mustacchi		w3 = udmf_eeprom_read(dp, i + 3, &err);
6866716431Robert Mustacchi		cmn_err(CE_CONT, "!0x%02x: 0x%04x 0x%04x 0x%04x 0x%04x",
6876716431Robert Mustacchi		    i, w0, w1, w2, w3);
6886716431Robert Mustacchi	}
6896716431Robert Mustacchiusberr:
6906716431Robert Mustacchi	;
6916716431Robert Mustacchi}
6926716431Robert Mustacchi#endif
6936716431Robert Mustacchi
6946716431Robert Mustacchistatic int
6956716431Robert Mustacchiudmf_attach_chip(struct usbgem_dev *dp)
6966716431Robert Mustacchi{
6976716431Robert Mustacchi	int	i;
6986716431Robert Mustacchi	uint_t	val;
6996716431Robert Mustacchi	uint8_t	*m;
7006716431Robert Mustacchi	int	err;
7016716431Robert Mustacchi	struct udmf_dev	*lp = dp->private;
7026716431Robert Mustacchi
7036716431Robert Mustacchi	DPRINTF(0, (CE_CONT, "!%s: %s enter", dp->name, __func__));
7046716431Robert Mustacchi
7056716431Robert Mustacchi	/*
7066716431Robert Mustacchi	 * get mac address from EEPROM
7076716431Robert Mustacchi	 */
7086716431Robert Mustacchi	m = dp->dev_addr.ether_addr_octet;
7096716431Robert Mustacchi	for (i = 0; i < ETHERADDRL; i += 2)  {
7106716431Robert Mustacchi		val = udmf_eeprom_read(dp, i/2, &err);
7116716431Robert Mustacchi		m[i + 0] = (uint8_t)val;
7126716431Robert Mustacchi		m[i + 1] = (uint8_t)(val >> 8);
7136716431Robert Mustacchi	}
7146716431Robert Mustacchi
7156716431Robert Mustacchi	/* invalidate a private cache for mac addr */
7166716431Robert Mustacchi	bzero(lp->mac_addr, sizeof (lp->mac_addr));
7176716431Robert Mustacchi#ifdef CONFIG_VLAN
7186716431Robert Mustacchi	dp->misc_flag = USBGEM_VLAN;
7196716431Robert Mustacchi#endif
7206716431Robert Mustacchi#if DEBUG_LEVEL > 0
7216716431Robert Mustacchi	udmf_eeprom_dump(dp, /* 0x3f + 1 */ 128);
7226716431Robert Mustacchi#endif
7236716431Robert Mustacchi{
7246716431Robert Mustacchi	static uint8_t bcst[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
7256716431Robert Mustacchi	DPRINTF(0, (CE_CONT, "!%s: %s: hash of bcast:%x",
7266716431Robert Mustacchi	    dp->name, __func__, usbgem_ether_crc_be(bcst)));
7276716431Robert Mustacchi}
7286716431Robert Mustacchi	return (USB_SUCCESS);
7296716431Robert Mustacchi
7306716431Robert Mustacchiusberr:
7316716431Robert Mustacchi	cmn_err(CE_WARN, "%s: %s: usb error detected (%d)",
7326716431Robert Mustacchi	    dp->name, __func__, err);
7336716431Robert Mustacchi	return (USB_FAILURE);
7346716431Robert Mustacchi}
7356716431Robert Mustacchi
7366716431Robert Mustacchistatic int
7376716431Robert Mustacchiudmf_mii_probe(struct usbgem_dev *dp)
7386716431Robert Mustacchi{
7396716431Robert Mustacchi	DPRINTF(2, (CE_CONT, "!%s: %s: called", dp->name, __func__));
7406716431Robert Mustacchi
7416716431Robert Mustacchi	udmf_enable_phy(dp);
7426716431Robert Mustacchi	return (usbgem_mii_probe_default(dp));
7436716431Robert Mustacchi}
7446716431Robert Mustacchi
7456716431Robert Mustacchistatic int
7466716431Robert Mustacchiudmf_mii_init(struct usbgem_dev *dp)
7476716431Robert Mustacchi{
7486716431Robert Mustacchi	DPRINTF(2, (CE_CONT, "!%s: %s: called", dp->name, __func__));
7496716431Robert Mustacchi	udmf_enable_phy(dp);
7506716431Robert Mustacchi	return (USB_SUCCESS);
7516716431Robert Mustacchi}
7526716431Robert Mustacchi
7536716431Robert Mustacchistatic int
7546716431Robert Mustacchiudmfattach(dev_info_t *dip, ddi_attach_cmd_t cmd)
7556716431Robert Mustacchi{
7566716431Robert Mustacchi	int			i;
7576716431Robert Mustacchi	ddi_iblock_cookie_t	c;
7586716431Robert Mustacchi	int			ret;
7596716431Robert Mustacchi	int			revid;
7606716431Robert Mustacchi	int			unit;
7616716431Robert Mustacchi	int			len;
7626716431Robert Mustacchi	const char		*drv_name;
7636716431Robert Mustacchi	struct usbgem_dev	*dp;
7646716431Robert Mustacchi	void			*base;
7656716431Robert Mustacchi	struct usbgem_conf	*ugcp;
7666716431Robert Mustacchi	struct udmf_dev		*lp;
7676716431Robert Mustacchi
7686716431Robert Mustacchi	unit = ddi_get_instance(dip);
7696716431Robert Mustacchi	drv_name = ddi_driver_name(dip);
7706716431Robert Mustacchi
7716716431Robert Mustacchi	DPRINTF(3, (CE_CONT, "!%s%d: %s: called, cmd:%d",
7726716431Robert Mustacchi	    drv_name, unit, __func__, cmd));
7736716431Robert Mustacchi
7746716431Robert Mustacchi	if (cmd == DDI_ATTACH) {
7756716431Robert Mustacchi		/*
7766716431Robert Mustacchi		 * construct usbgem configration
7776716431Robert Mustacchi		 */
7786716431Robert Mustacchi		ugcp = kmem_zalloc(sizeof (*ugcp), KM_SLEEP);
7796716431Robert Mustacchi
7806716431Robert Mustacchi		/* name */
7816716431Robert Mustacchi		/*
7826716431Robert Mustacchi		 * softmac requires that ppa is the instance number
7836716431Robert Mustacchi		 * of the device, otherwise it hangs in seaching the device.
7846716431Robert Mustacchi		 */
7856716431Robert Mustacchi		(void) sprintf(ugcp->usbgc_name, "%s%d", drv_name, unit);
7866716431Robert Mustacchi		ugcp->usbgc_ppa = unit;
7876716431Robert Mustacchi
7886716431Robert Mustacchi		ugcp->usbgc_ifnum = 0;
7896716431Robert Mustacchi		ugcp->usbgc_alt = 0;
7906716431Robert Mustacchi
7916716431Robert Mustacchi		ugcp->usbgc_tx_list_max = 64;
7926716431Robert Mustacchi
7936716431Robert Mustacchi		ugcp->usbgc_rx_header_len = RX_HEADER_SIZE;
7946716431Robert Mustacchi		ugcp->usbgc_rx_list_max = 64;
7956716431Robert Mustacchi
7966716431Robert Mustacchi		/* time out parameters */
7976716431Robert Mustacchi		ugcp->usbgc_tx_timeout = USBGEM_TX_TIMEOUT;
7986716431Robert Mustacchi		ugcp->usbgc_tx_timeout_interval = USBGEM_TX_TIMEOUT_INTERVAL;
7996716431Robert Mustacchi#if 1
8006716431Robert Mustacchi		/* flow control */
8016716431Robert Mustacchi		ugcp->usbgc_flow_control = FLOW_CONTROL_RX_PAUSE;
8026716431Robert Mustacchi#else
8036716431Robert Mustacchi		/*
8046716431Robert Mustacchi		 * XXX - flow control caused link down frequently under
8056716431Robert Mustacchi		 * heavy traffic
8066716431Robert Mustacchi		 */
8076716431Robert Mustacchi		ugcp->usbgc_flow_control = FLOW_CONTROL_NONE;
8086716431Robert Mustacchi#endif
8096716431Robert Mustacchi		/* MII timeout parameters */
8106716431Robert Mustacchi		ugcp->usbgc_mii_link_watch_interval =
8116716431Robert Mustacchi		    USBGEM_LINK_WATCH_INTERVAL;
8126716431Robert Mustacchi		ugcp->usbgc_mii_an_watch_interval =
8136716431Robert Mustacchi		    USBGEM_LINK_WATCH_INTERVAL/5;
8146716431Robert Mustacchi		ugcp->usbgc_mii_reset_timeout = MII_RESET_TIMEOUT; /* 1 sec */
8156716431Robert Mustacchi		ugcp->usbgc_mii_an_timeout = MII_AN_TIMEOUT;	/* 5 sec */
8166716431Robert Mustacchi		ugcp->usbgc_mii_an_wait = (25*ONESEC)/10;
8176716431Robert Mustacchi		ugcp->usbgc_mii_linkdown_timeout = MII_LINKDOWN_TIMEOUT;
8186716431Robert Mustacchi
8196716431Robert Mustacchi		ugcp->usbgc_mii_an_delay = ONESEC/10;
8206716431Robert Mustacchi		ugcp->usbgc_mii_linkdown_action = MII_ACTION_RSA;
8216716431Robert Mustacchi		ugcp->usbgc_mii_linkdown_timeout_action = MII_ACTION_RESET;
8226716431Robert Mustacchi		ugcp->usbgc_mii_dont_reset = B_FALSE;
8236716431Robert Mustacchi		ugcp->usbgc_mii_hw_link_detection = B_TRUE;
8246716431Robert Mustacchi
8256716431Robert Mustacchi		/* I/O methods */
8266716431Robert Mustacchi
8276716431Robert Mustacchi		/* mac operation */
8286716431Robert Mustacchi		ugcp->usbgc_attach_chip = &udmf_attach_chip;
8296716431Robert Mustacchi		ugcp->usbgc_reset_chip = &udmf_reset_chip;
8306716431Robert Mustacchi		ugcp->usbgc_init_chip = &udmf_init_chip;
8316716431Robert Mustacchi		ugcp->usbgc_start_chip = &udmf_start_chip;
8326716431Robert Mustacchi		ugcp->usbgc_stop_chip = &udmf_stop_chip;
8336716431Robert Mustacchi		ugcp->usbgc_multicast_hash = &udmf_mcast_hash;
8346716431Robert Mustacchi
8356716431Robert Mustacchi		ugcp->usbgc_set_rx_filter = &udmf_set_rx_filter;
8366716431Robert Mustacchi		ugcp->usbgc_set_media = &udmf_set_media;
8376716431Robert Mustacchi		ugcp->usbgc_get_stats = &udmf_get_stats;
8386716431Robert Mustacchi		ugcp->usbgc_interrupt = &udmf_interrupt;
8396716431Robert Mustacchi
8406716431Robert Mustacchi		/* packet operation */
8416716431Robert Mustacchi		ugcp->usbgc_tx_make_packet = &udmf_tx_make_packet;
8426716431Robert Mustacchi		ugcp->usbgc_rx_make_packet = &udmf_rx_make_packet;
8436716431Robert Mustacchi
8446716431Robert Mustacchi		/* mii operations */
8456716431Robert Mustacchi		ugcp->usbgc_mii_probe = &udmf_mii_probe;
8466716431Robert Mustacchi		ugcp->usbgc_mii_init = &udmf_mii_init;
8476716431Robert Mustacchi		ugcp->usbgc_mii_config = &usbgem_mii_config_default;
8486716431Robert Mustacchi		ugcp->usbgc_mii_read = &udmf_mii_read;
8496716431Robert Mustacchi		ugcp->usbgc_mii_write = &udmf_mii_write;
8506716431Robert Mustacchi		ugcp->usbgc_mii_addr_min = 1;
8516716431Robert Mustacchi
8526716431Robert Mustacchi		/* mtu */
8536716431Robert Mustacchi		ugcp->usbgc_min_mtu = ETHERMTU;
8546716431Robert Mustacchi		ugcp->usbgc_max_mtu = ETHERMTU;
8556716431Robert Mustacchi		ugcp->usbgc_default_mtu = ETHERMTU;
8566716431Robert Mustacchi
8576716431Robert Mustacchi		lp = kmem_zalloc(sizeof (struct udmf_dev), KM_SLEEP);
8586716431Robert Mustacchi
8596716431Robert Mustacchi		ddi_set_driver_private(dip, NULL);
8606716431Robert Mustacchi
8616716431Robert Mustacchi		dp = usbgem_do_attach(dip, ugcp, lp, sizeof (struct udmf_dev));
8626716431Robert Mustacchi
8636716431Robert Mustacchi		kmem_free(ugcp, sizeof (*ugcp));
8646716431Robert Mustacchi
8656716431Robert Mustacchi		if (dp != NULL) {
8666716431Robert Mustacchi			return (DDI_SUCCESS);
8676716431Robert Mustacchi		}
8686716431Robert Mustacchi
8696716431Robert Mustacchierr_free_mem:
8706716431Robert Mustacchi		kmem_free(lp, sizeof (struct udmf_dev));
8716716431Robert Mustacchierr_close_pipe:
8726716431Robert Mustacchierr:
8736716431Robert Mustacchi		return (DDI_FAILURE);
8746716431Robert Mustacchi	}
8756716431Robert Mustacchi
8766716431Robert Mustacchi	if (cmd == DDI_RESUME) {
8776716431Robert Mustacchi		return (usbgem_resume(dip));
8786716431Robert Mustacchi	}
8796716431Robert Mustacchi
8806716431Robert Mustacchi	return (DDI_FAILURE);
8816716431Robert Mustacchi}
8826716431Robert Mustacchi
8836716431Robert Mustacchistatic int
8846716431Robert Mustacchiudmfdetach(dev_info_t *dip, ddi_detach_cmd_t cmd)
8856716431Robert Mustacchi{
8866716431Robert Mustacchi	int	ret;
8876716431Robert Mustacchi
8886716431Robert Mustacchi	if (cmd == DDI_DETACH) {
8896716431Robert Mustacchi		ret = usbgem_do_detach(dip);
8906716431Robert Mustacchi		if (ret != DDI_SUCCESS) {
8916716431Robert Mustacchi			return (DDI_FAILURE);
8926716431Robert Mustacchi		}
8936716431Robert Mustacchi		return (DDI_SUCCESS);
8946716431Robert Mustacchi	}
8956716431Robert Mustacchi	if (cmd == DDI_SUSPEND) {
8966716431Robert Mustacchi		return (usbgem_suspend(dip));
8976716431Robert Mustacchi	}
8986716431Robert Mustacchi	return (DDI_FAILURE);
8996716431Robert Mustacchi}
9006716431Robert Mustacchi
9016716431Robert Mustacchi/* ======================================================== */
9026716431Robert Mustacchi/*
9036716431Robert Mustacchi * OS depend (loadable streams driver) routine
9046716431Robert Mustacchi */
9056716431Robert Mustacchi/* ======================================================== */
9066716431Robert Mustacchi#ifdef USBGEM_CONFIG_GLDv3
9076716431Robert MustacchiUSBGEM_STREAM_OPS(udmf_ops, udmfattach, udmfdetach);
9086716431Robert Mustacchi#else
9096716431Robert Mustacchistatic	struct module_info udmfminfo = {
9106716431Robert Mustacchi	0,			/* mi_idnum */
9116716431Robert Mustacchi	"udmf",			/* mi_idname */
9126716431Robert Mustacchi	0,			/* mi_minpsz */
9136716431Robert Mustacchi	ETHERMTU,		/* mi_maxpsz */
9146716431Robert Mustacchi	ETHERMTU*128,		/* mi_hiwat */
9156716431Robert Mustacchi	1,			/* mi_lowat */
9166716431Robert Mustacchi};
9176716431Robert Mustacchi
9186716431Robert Mustacchistatic	struct qinit udmfrinit = {
9196716431Robert Mustacchi	(int (*)()) NULL,	/* qi_putp */
9206716431Robert Mustacchi	usbgem_rsrv,		/* qi_srvp */
9216716431Robert Mustacchi	usbgem_open,		/* qi_qopen */
9226716431Robert Mustacchi	usbgem_close,		/* qi_qclose */
9236716431Robert Mustacchi	(int (*)()) NULL,	/* qi_qadmin */
9246716431Robert Mustacchi	&udmfminfo,		/* qi_minfo */
9256716431Robert Mustacchi	NULL			/* qi_mstat */
9266716431Robert Mustacchi};
9276716431Robert Mustacchi
9286716431Robert Mustacchistatic	struct qinit udmfwinit = {
9296716431Robert Mustacchi	usbgem_wput,		/* qi_putp */
9306716431Robert Mustacchi	usbgem_wsrv,		/* qi_srvp */
9316716431Robert Mustacchi	(int (*)()) NULL,	/* qi_qopen */
9326716431Robert Mustacchi	(int (*)()) NULL,	/* qi_qclose */
9336716431Robert Mustacchi	(int (*)()) NULL,	/* qi_qadmin */
9346716431Robert Mustacchi	&udmfminfo,		/* qi_minfo */
9356716431Robert Mustacchi	NULL			/* qi_mstat */
9366716431Robert Mustacchi};
9376716431Robert Mustacchi
9386716431Robert Mustacchistatic struct streamtab	udmf_info = {
9396716431Robert Mustacchi	&udmfrinit,	/* st_rdinit */
9406716431Robert Mustacchi	&udmfwinit,	/* st_wrinit */
9416716431Robert Mustacchi	NULL,		/* st_muxrinit */
9426716431Robert Mustacchi	NULL		/* st_muxwrinit */
9436716431Robert Mustacchi};
9446716431Robert Mustacchi
9456716431Robert Mustacchistatic	struct cb_ops cb_udmf_ops = {
9466716431Robert Mustacchi	nulldev,	/* cb_open */
9476716431Robert Mustacchi	nulldev,	/* cb_close */
9486716431Robert Mustacchi	nodev,		/* cb_strategy */
9496716431Robert Mustacchi	nodev,		/* cb_print */
9506716431Robert Mustacchi	nodev,		/* cb_dump */
9516716431Robert Mustacchi	nodev,		/* cb_read */
9526716431Robert Mustacchi	nodev,		/* cb_write */
9536716431Robert Mustacchi	nodev,		/* cb_ioctl */
9546716431Robert Mustacchi	nodev,		/* cb_devmap */
9556716431Robert Mustacchi	nodev,		/* cb_mmap */
9566716431Robert Mustacchi	nodev,		/* cb_segmap */
9576716431Robert Mustacchi	nochpoll,	/* cb_chpoll */
9586716431Robert Mustacchi	ddi_prop_op,	/* cb_prop_op */
9596716431Robert Mustacchi	&udmf_info,	/* cb_stream */
9606716431Robert Mustacchi	D_NEW|D_MP	/* cb_flag */
9616716431Robert Mustacchi};
9626716431Robert Mustacchi
9636716431Robert Mustacchistatic	struct dev_ops udmf_ops = {
9646716431Robert Mustacchi	DEVO_REV,	/* devo_rev */
9656716431Robert Mustacchi	0,		/* devo_refcnt */
9666716431Robert Mustacchi	usbgem_getinfo,	/* devo_getinfo */
9676716431Robert Mustacchi	nulldev,	/* devo_identify */
9686716431Robert Mustacchi	nulldev,	/* devo_probe */
9696716431Robert Mustacchi	udmfattach,	/* devo_attach */
9706716431Robert Mustacchi	udmfdetach,	/* devo_detach */
9716716431Robert Mustacchi	nodev,		/* devo_reset */
9726716431Robert Mustacchi	&cb_udmf_ops,	/* devo_cb_ops */
9736716431Robert Mustacchi	NULL,		/* devo_bus_ops */
9746716431Robert Mustacchi	usbgem_power,   /* devo_power */
9756716431Robert Mustacchi#if DEVO_REV >= 4
9766716431Robert Mustacchi	usbgem_quiesce, /* devo_quiesce */
9776716431Robert Mustacchi#endif
9786716431Robert Mustacchi};
9796716431Robert Mustacchi#endif
9806716431Robert Mustacchi
9816716431Robert Mustacchistatic struct modldrv modldrv = {
9826716431Robert Mustacchi	&mod_driverops,	/* Type of module.  This one is a driver */
9836716431Robert Mustacchi	ident,
9846716431Robert Mustacchi	&udmf_ops,	/* driver ops */
9856716431Robert Mustacchi};
9866716431Robert Mustacchi
9876716431Robert Mustacchistatic struct modlinkage modlinkage = {
988