xref: /titanic_53/usr/src/uts/sun4v/io/vsw.c (revision f2b610cf6e03184d9538e3aaec99bcaf65124714)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo 
221ae08745Sheppo /*
23b071742bSsg70180  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
241ae08745Sheppo  * Use is subject to license terms.
251ae08745Sheppo  */
261ae08745Sheppo 
271ae08745Sheppo #pragma ident	"%Z%%M%	%I%	%E% SMI"
281ae08745Sheppo 
291ae08745Sheppo #include <sys/types.h>
301ae08745Sheppo #include <sys/errno.h>
311ae08745Sheppo #include <sys/debug.h>
321ae08745Sheppo #include <sys/time.h>
331ae08745Sheppo #include <sys/sysmacros.h>
341ae08745Sheppo #include <sys/systm.h>
351ae08745Sheppo #include <sys/user.h>
361ae08745Sheppo #include <sys/stropts.h>
371ae08745Sheppo #include <sys/stream.h>
381ae08745Sheppo #include <sys/strlog.h>
391ae08745Sheppo #include <sys/strsubr.h>
401ae08745Sheppo #include <sys/cmn_err.h>
411ae08745Sheppo #include <sys/cpu.h>
421ae08745Sheppo #include <sys/kmem.h>
431ae08745Sheppo #include <sys/conf.h>
441ae08745Sheppo #include <sys/ddi.h>
451ae08745Sheppo #include <sys/sunddi.h>
461ae08745Sheppo #include <sys/ksynch.h>
471ae08745Sheppo #include <sys/stat.h>
481ae08745Sheppo #include <sys/kstat.h>
491ae08745Sheppo #include <sys/vtrace.h>
501ae08745Sheppo #include <sys/strsun.h>
511ae08745Sheppo #include <sys/dlpi.h>
521ae08745Sheppo #include <sys/ethernet.h>
531ae08745Sheppo #include <net/if.h>
541ae08745Sheppo #include <sys/varargs.h>
551ae08745Sheppo #include <sys/machsystm.h>
561ae08745Sheppo #include <sys/modctl.h>
571ae08745Sheppo #include <sys/modhash.h>
581ae08745Sheppo #include <sys/mac.h>
59ba2e4443Sseb #include <sys/mac_ether.h>
601ae08745Sheppo #include <sys/taskq.h>
611ae08745Sheppo #include <sys/note.h>
621ae08745Sheppo #include <sys/mach_descrip.h>
631ae08745Sheppo #include <sys/mac.h>
641ae08745Sheppo #include <sys/mdeg.h>
651ae08745Sheppo #include <sys/ldc.h>
661ae08745Sheppo #include <sys/vsw_fdb.h>
671ae08745Sheppo #include <sys/vsw.h>
681ae08745Sheppo #include <sys/vio_mailbox.h>
691ae08745Sheppo #include <sys/vnet_mailbox.h>
701ae08745Sheppo #include <sys/vnet_common.h>
71d10e4ef2Snarayan #include <sys/vio_util.h>
72d10e4ef2Snarayan #include <sys/sdt.h>
7319b65a69Ssb155480 #include <sys/atomic.h>
7406db247cSraghuram #include <sys/callb.h>
751ae08745Sheppo 
761ae08745Sheppo /*
771ae08745Sheppo  * Function prototypes.
781ae08745Sheppo  */
791ae08745Sheppo static	int vsw_attach(dev_info_t *, ddi_attach_cmd_t);
801ae08745Sheppo static	int vsw_detach(dev_info_t *, ddi_detach_cmd_t);
811ae08745Sheppo static	int vsw_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
8234683adeSsg70180 static	int vsw_get_md_physname(vsw_t *, md_t *, mde_cookie_t, char *);
8334683adeSsg70180 static	int vsw_get_md_smodes(vsw_t *, md_t *, mde_cookie_t, uint8_t *, int *);
841ae08745Sheppo 
851ae08745Sheppo /* MDEG routines */
8634683adeSsg70180 static	int vsw_mdeg_register(vsw_t *vswp);
871ae08745Sheppo static	void vsw_mdeg_unregister(vsw_t *vswp);
881ae08745Sheppo static	int vsw_mdeg_cb(void *cb_argp, mdeg_result_t *);
8934683adeSsg70180 static	int vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *);
9019b65a69Ssb155480 static	int vsw_get_initial_md_properties(vsw_t *vswp, md_t *, mde_cookie_t);
9134683adeSsg70180 static	void vsw_update_md_prop(vsw_t *, md_t *, mde_cookie_t);
9219b65a69Ssb155480 static	int vsw_read_mdprops(vsw_t *vswp);
9319b65a69Ssb155480 static void vsw_save_lmacaddr(vsw_t *vswp, uint64_t macaddr);
941ae08745Sheppo 
9506db247cSraghuram /* Mac driver related routines */
9606db247cSraghuram static int vsw_mac_register(vsw_t *);
9706db247cSraghuram static int vsw_mac_unregister(vsw_t *);
9806db247cSraghuram static int vsw_m_stat(void *, uint_t, uint64_t *);
9906db247cSraghuram static void vsw_m_stop(void *arg);
10006db247cSraghuram static int vsw_m_start(void *arg);
10106db247cSraghuram static int vsw_m_unicst(void *arg, const uint8_t *);
10206db247cSraghuram static int vsw_m_multicst(void *arg, boolean_t, const uint8_t *);
10306db247cSraghuram static int vsw_m_promisc(void *arg, boolean_t);
10406db247cSraghuram static mblk_t *vsw_m_tx(void *arg, mblk_t *);
10506db247cSraghuram static uint_t vsw_rx_softintr(caddr_t arg1, caddr_t arg2);
10606db247cSraghuram void vsw_mac_rx(vsw_t *vswp, int caller, mac_resource_handle_t mrh,
10706db247cSraghuram     mblk_t *mp, mblk_t *mpt, vsw_macrx_flags_t flags);
1081ae08745Sheppo 
10906db247cSraghuram /*
11006db247cSraghuram  * Functions imported from other files.
11106db247cSraghuram  */
11206db247cSraghuram extern void vsw_setup_switching_timeout(void *arg);
11306db247cSraghuram extern void vsw_stop_switching_timeout(vsw_t *vswp);
11406db247cSraghuram extern int vsw_setup_switching(vsw_t *);
11506db247cSraghuram extern int vsw_add_mcst(vsw_t *, uint8_t, uint64_t, void *);
11606db247cSraghuram extern int vsw_del_mcst(vsw_t *, uint8_t, uint64_t, void *);
11706db247cSraghuram extern void vsw_del_mcst_vsw(vsw_t *);
11806db247cSraghuram extern mcst_addr_t *vsw_del_addr(uint8_t devtype, void *arg, uint64_t addr);
11906db247cSraghuram extern int vsw_detach_ports(vsw_t *vswp);
12006db247cSraghuram extern int vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node);
12106db247cSraghuram extern int vsw_port_detach(vsw_t *vswp, int p_instance);
12206db247cSraghuram extern	int vsw_port_attach(vsw_t *vswp, int p_instance,
12306db247cSraghuram 	uint64_t *ldcids, int nids, struct ether_addr *macaddr);
12406db247cSraghuram extern vsw_port_t *vsw_lookup_port(vsw_t *vswp, int p_instance);
12506db247cSraghuram extern int vsw_mac_attach(vsw_t *vswp);
12606db247cSraghuram extern void vsw_mac_detach(vsw_t *vswp);
12706db247cSraghuram extern int vsw_mac_open(vsw_t *vswp);
12806db247cSraghuram extern void vsw_mac_close(vsw_t *vswp);
12906db247cSraghuram extern int vsw_set_hw(vsw_t *, vsw_port_t *, int);
13006db247cSraghuram extern int vsw_unset_hw(vsw_t *, vsw_port_t *, int);
13106db247cSraghuram extern void vsw_reconfig_hw(vsw_t *);
13206db247cSraghuram extern void vsw_unset_addrs(vsw_t *vswp);
13306db247cSraghuram extern void vsw_set_addrs(vsw_t *vswp);
13406db247cSraghuram 
13506db247cSraghuram 
13606db247cSraghuram /*
13706db247cSraghuram  * Internal tunables.
13806db247cSraghuram  */
139445b4c2eSsb155480 int	vsw_num_handshakes = VNET_NUM_HANDSHAKES; /* # of handshake attempts */
1401ae08745Sheppo int	vsw_wretries = 100;		/* # of write attempts */
141d10e4ef2Snarayan int	vsw_desc_delay = 0;		/* delay in us */
142d10e4ef2Snarayan int	vsw_read_attempts = 5;		/* # of reads of descriptor */
14319b65a69Ssb155480 int	vsw_mac_open_retries = 20;	/* max # of mac_open() retries */
14419b65a69Ssb155480 int	vsw_setup_switching_delay = 3;	/* setup sw timeout interval in sec */
14506db247cSraghuram int	vsw_ldc_tx_delay = 5;		/* delay(ticks) for tx retries */
14606db247cSraghuram int	vsw_ldc_tx_retries = 10;	/* # of ldc tx retries */
14706db247cSraghuram int	vsw_ldc_tx_max_failures = 40;	/* Max ldc tx failures */
14806db247cSraghuram boolean_t vsw_ldc_rxthr_enabled = B_TRUE;	/* LDC Rx thread enabled */
14906db247cSraghuram boolean_t vsw_ldc_txthr_enabled = B_TRUE;	/* LDC Tx thread enabled */
150d10e4ef2Snarayan 
151d10e4ef2Snarayan 
15206db247cSraghuram /*
15306db247cSraghuram  * External tunables.
15406db247cSraghuram  */
15506db247cSraghuram /*
15606db247cSraghuram  * Enable/disable thread per ring. This is a mode selection
15706db247cSraghuram  * that is done a vsw driver attach time.
15806db247cSraghuram  */
15906db247cSraghuram boolean_t vsw_multi_ring_enable = B_FALSE;
16006db247cSraghuram int vsw_mac_rx_rings = VSW_MAC_RX_RINGS;
16106db247cSraghuram 
16206db247cSraghuram /*
16306db247cSraghuram  * Max number of mblks received in one receive operation.
16406db247cSraghuram  */
16506db247cSraghuram uint32_t vsw_chain_len = (VSW_NUM_MBLKS * 0.6);
16606db247cSraghuram 
16706db247cSraghuram /*
16806db247cSraghuram  * Tunables for three different pools, that is, the size and
16906db247cSraghuram  * number of mblks for each pool.
17006db247cSraghuram  */
17106db247cSraghuram uint32_t vsw_mblk_size1 = VSW_MBLK_SZ_128;	/* size=128 for pool1 */
17206db247cSraghuram uint32_t vsw_mblk_size2 = VSW_MBLK_SZ_256;	/* size=256 for pool2 */
17306db247cSraghuram uint32_t vsw_mblk_size3 = VSW_MBLK_SZ_2048;	/* size=2048 for pool3 */
17406db247cSraghuram uint32_t vsw_num_mblks1 = VSW_NUM_MBLKS;	/* number of mblks for pool1 */
17506db247cSraghuram uint32_t vsw_num_mblks2 = VSW_NUM_MBLKS;	/* number of mblks for pool2 */
17606db247cSraghuram uint32_t vsw_num_mblks3 = VSW_NUM_MBLKS;	/* number of mblks for pool3 */
17706db247cSraghuram 
17806db247cSraghuram /*
17906db247cSraghuram  * MAC callbacks
18006db247cSraghuram  */
181ba2e4443Sseb static	mac_callbacks_t	vsw_m_callbacks = {
182ba2e4443Sseb 	0,
183ba2e4443Sseb 	vsw_m_stat,
184ba2e4443Sseb 	vsw_m_start,
185ba2e4443Sseb 	vsw_m_stop,
186ba2e4443Sseb 	vsw_m_promisc,
187ba2e4443Sseb 	vsw_m_multicst,
188ba2e4443Sseb 	vsw_m_unicst,
189ba2e4443Sseb 	vsw_m_tx,
190ba2e4443Sseb 	NULL,
191ba2e4443Sseb 	NULL,
192ba2e4443Sseb 	NULL
193ba2e4443Sseb };
194ba2e4443Sseb 
1951ae08745Sheppo static	struct	cb_ops	vsw_cb_ops = {
1961ae08745Sheppo 	nulldev,			/* cb_open */
1971ae08745Sheppo 	nulldev,			/* cb_close */
1981ae08745Sheppo 	nodev,				/* cb_strategy */
1991ae08745Sheppo 	nodev,				/* cb_print */
2001ae08745Sheppo 	nodev,				/* cb_dump */
2011ae08745Sheppo 	nodev,				/* cb_read */
2021ae08745Sheppo 	nodev,				/* cb_write */
2031ae08745Sheppo 	nodev,				/* cb_ioctl */
2041ae08745Sheppo 	nodev,				/* cb_devmap */
2051ae08745Sheppo 	nodev,				/* cb_mmap */
2061ae08745Sheppo 	nodev,				/* cb_segmap */
2071ae08745Sheppo 	nochpoll,			/* cb_chpoll */
2081ae08745Sheppo 	ddi_prop_op,			/* cb_prop_op */
2091ae08745Sheppo 	NULL,				/* cb_stream */
2101ae08745Sheppo 	D_MP,				/* cb_flag */
2111ae08745Sheppo 	CB_REV,				/* rev */
2121ae08745Sheppo 	nodev,				/* int (*cb_aread)() */
2131ae08745Sheppo 	nodev				/* int (*cb_awrite)() */
2141ae08745Sheppo };
2151ae08745Sheppo 
2161ae08745Sheppo static	struct	dev_ops	vsw_ops = {
2171ae08745Sheppo 	DEVO_REV,		/* devo_rev */
2181ae08745Sheppo 	0,			/* devo_refcnt */
2191ae08745Sheppo 	vsw_getinfo,		/* devo_getinfo */
2201ae08745Sheppo 	nulldev,		/* devo_identify */
2211ae08745Sheppo 	nulldev,		/* devo_probe */
2221ae08745Sheppo 	vsw_attach,		/* devo_attach */
2231ae08745Sheppo 	vsw_detach,		/* devo_detach */
2241ae08745Sheppo 	nodev,			/* devo_reset */
2251ae08745Sheppo 	&vsw_cb_ops,		/* devo_cb_ops */
2261ae08745Sheppo 	(struct bus_ops *)NULL,	/* devo_bus_ops */
2271ae08745Sheppo 	ddi_power		/* devo_power */
2281ae08745Sheppo };
2291ae08745Sheppo 
2301ae08745Sheppo extern	struct	mod_ops	mod_driverops;
2311ae08745Sheppo static struct modldrv vswmodldrv = {
2321ae08745Sheppo 	&mod_driverops,
233205eeb1aSlm66018 	"sun4v Virtual Switch",
2341ae08745Sheppo 	&vsw_ops,
2351ae08745Sheppo };
2361ae08745Sheppo 
2371ae08745Sheppo #define	LDC_ENTER_LOCK(ldcp)	\
2381ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_cblock));\
23906db247cSraghuram 				mutex_enter(&((ldcp)->ldc_rxlock));\
2401ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_txlock));
2411ae08745Sheppo #define	LDC_EXIT_LOCK(ldcp)	\
2421ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_txlock));\
24306db247cSraghuram 				mutex_exit(&((ldcp)->ldc_rxlock));\
2441ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_cblock));
2451ae08745Sheppo 
2461ae08745Sheppo /* Driver soft state ptr  */
2471ae08745Sheppo static void	*vsw_state;
2481ae08745Sheppo 
2491ae08745Sheppo /*
2501ae08745Sheppo  * Linked list of "vsw_t" structures - one per instance.
2511ae08745Sheppo  */
2521ae08745Sheppo vsw_t		*vsw_head = NULL;
2531ae08745Sheppo krwlock_t	vsw_rw;
2541ae08745Sheppo 
2551ae08745Sheppo /*
2561ae08745Sheppo  * Property names
2571ae08745Sheppo  */
2581ae08745Sheppo static char vdev_propname[] = "virtual-device";
2591ae08745Sheppo static char vsw_propname[] = "virtual-network-switch";
2601ae08745Sheppo static char physdev_propname[] = "vsw-phys-dev";
2611ae08745Sheppo static char smode_propname[] = "vsw-switch-mode";
2621ae08745Sheppo static char macaddr_propname[] = "local-mac-address";
2631ae08745Sheppo static char remaddr_propname[] = "remote-mac-address";
2641ae08745Sheppo static char ldcids_propname[] = "ldc-ids";
2651ae08745Sheppo static char chan_propname[] = "channel-endpoint";
2661ae08745Sheppo static char id_propname[] = "id";
2671ae08745Sheppo static char reg_propname[] = "reg";
2681ae08745Sheppo 
2691ae08745Sheppo /*
2701ae08745Sheppo  * Matching criteria passed to the MDEG to register interest
2711ae08745Sheppo  * in changes to 'virtual-device-port' nodes identified by their
2721ae08745Sheppo  * 'id' property.
2731ae08745Sheppo  */
2741ae08745Sheppo static md_prop_match_t vport_prop_match[] = {
2751ae08745Sheppo 	{ MDET_PROP_VAL,    "id"   },
2761ae08745Sheppo 	{ MDET_LIST_END,    NULL    }
2771ae08745Sheppo };
2781ae08745Sheppo 
2791ae08745Sheppo static mdeg_node_match_t vport_match = { "virtual-device-port",
2801ae08745Sheppo 						vport_prop_match };
2811ae08745Sheppo 
2821ae08745Sheppo /*
28334683adeSsg70180  * Matching criteria passed to the MDEG to register interest
28434683adeSsg70180  * in changes to 'virtual-device' nodes (i.e. vsw nodes) identified
28534683adeSsg70180  * by their 'name' and 'cfg-handle' properties.
28634683adeSsg70180  */
28734683adeSsg70180 static md_prop_match_t vdev_prop_match[] = {
28834683adeSsg70180 	{ MDET_PROP_STR,    "name"   },
28934683adeSsg70180 	{ MDET_PROP_VAL,    "cfg-handle" },
29034683adeSsg70180 	{ MDET_LIST_END,    NULL    }
29134683adeSsg70180 };
29234683adeSsg70180 
29334683adeSsg70180 static mdeg_node_match_t vdev_match = { "virtual-device",
29434683adeSsg70180 						vdev_prop_match };
29534683adeSsg70180 
29634683adeSsg70180 
29734683adeSsg70180 /*
2981ae08745Sheppo  * Specification of an MD node passed to the MDEG to filter any
2991ae08745Sheppo  * 'vport' nodes that do not belong to the specified node. This
3001ae08745Sheppo  * template is copied for each vsw instance and filled in with
3011ae08745Sheppo  * the appropriate 'cfg-handle' value before being passed to the MDEG.
3021ae08745Sheppo  */
3031ae08745Sheppo static mdeg_prop_spec_t vsw_prop_template[] = {
3041ae08745Sheppo 	{ MDET_PROP_STR,    "name",		vsw_propname },
3051ae08745Sheppo 	{ MDET_PROP_VAL,    "cfg-handle",	NULL	},
3061ae08745Sheppo 	{ MDET_LIST_END,    NULL,		NULL	}
3071ae08745Sheppo };
3081ae08745Sheppo 
3091ae08745Sheppo #define	VSW_SET_MDEG_PROP_INST(specp, val)	(specp)[1].ps_val = (val);
3101ae08745Sheppo 
31106db247cSraghuram #ifdef	DEBUG
3127636cb21Slm66018 /*
3131ae08745Sheppo  * Print debug messages - set to 0x1f to enable all msgs
3141ae08745Sheppo  * or 0x0 to turn all off.
3151ae08745Sheppo  */
3161ae08745Sheppo int vswdbg = 0x0;
3171ae08745Sheppo 
3181ae08745Sheppo /*
3191ae08745Sheppo  * debug levels:
3201ae08745Sheppo  * 0x01:	Function entry/exit tracing
3211ae08745Sheppo  * 0x02:	Internal function messages
3221ae08745Sheppo  * 0x04:	Verbose internal messages
3231ae08745Sheppo  * 0x08:	Warning messages
3241ae08745Sheppo  * 0x10:	Error messages
3251ae08745Sheppo  */
3261ae08745Sheppo 
32706db247cSraghuram void
3281ae08745Sheppo vswdebug(vsw_t *vswp, const char *fmt, ...)
3291ae08745Sheppo {
3301ae08745Sheppo 	char buf[512];
3311ae08745Sheppo 	va_list ap;
3321ae08745Sheppo 
3331ae08745Sheppo 	va_start(ap, fmt);
3341ae08745Sheppo 	(void) vsprintf(buf, fmt, ap);
3351ae08745Sheppo 	va_end(ap);
3361ae08745Sheppo 
3371ae08745Sheppo 	if (vswp == NULL)
3381ae08745Sheppo 		cmn_err(CE_CONT, "%s\n", buf);
3391ae08745Sheppo 	else
3401ae08745Sheppo 		cmn_err(CE_CONT, "vsw%d: %s\n", vswp->instance, buf);
3411ae08745Sheppo }
3421ae08745Sheppo 
3431ae08745Sheppo #endif	/* DEBUG */
3441ae08745Sheppo 
3451ae08745Sheppo static struct modlinkage modlinkage = {
3461ae08745Sheppo 	MODREV_1,
3471ae08745Sheppo 	&vswmodldrv,
3481ae08745Sheppo 	NULL
3491ae08745Sheppo };
3501ae08745Sheppo 
3511ae08745Sheppo int
3521ae08745Sheppo _init(void)
3531ae08745Sheppo {
3541ae08745Sheppo 	int status;
3551ae08745Sheppo 
3561ae08745Sheppo 	rw_init(&vsw_rw, NULL, RW_DRIVER, NULL);
3571ae08745Sheppo 
3581ae08745Sheppo 	status = ddi_soft_state_init(&vsw_state, sizeof (vsw_t), 1);
3591ae08745Sheppo 	if (status != 0) {
3601ae08745Sheppo 		return (status);
3611ae08745Sheppo 	}
3621ae08745Sheppo 
36306db247cSraghuram 	mac_init_ops(&vsw_ops, DRV_NAME);
3641ae08745Sheppo 	status = mod_install(&modlinkage);
3651ae08745Sheppo 	if (status != 0) {
3661ae08745Sheppo 		ddi_soft_state_fini(&vsw_state);
3671ae08745Sheppo 	}
3681ae08745Sheppo 	return (status);
3691ae08745Sheppo }
3701ae08745Sheppo 
3711ae08745Sheppo int
3721ae08745Sheppo _fini(void)
3731ae08745Sheppo {
3741ae08745Sheppo 	int status;
3751ae08745Sheppo 
3761ae08745Sheppo 	status = mod_remove(&modlinkage);
3771ae08745Sheppo 	if (status != 0)
3781ae08745Sheppo 		return (status);
3791ae08745Sheppo 	mac_fini_ops(&vsw_ops);
3801ae08745Sheppo 	ddi_soft_state_fini(&vsw_state);
3811ae08745Sheppo 
3821ae08745Sheppo 	rw_destroy(&vsw_rw);
3831ae08745Sheppo 
3841ae08745Sheppo 	return (status);
3851ae08745Sheppo }
3861ae08745Sheppo 
3871ae08745Sheppo int
3881ae08745Sheppo _info(struct modinfo *modinfop)
3891ae08745Sheppo {
3901ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
3911ae08745Sheppo }
3921ae08745Sheppo 
3931ae08745Sheppo static int
3941ae08745Sheppo vsw_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3951ae08745Sheppo {
3961ae08745Sheppo 	vsw_t		*vswp;
39734683adeSsg70180 	int		instance;
3981ae08745Sheppo 	char		hashname[MAXNAMELEN];
3991ae08745Sheppo 	char		qname[TASKQ_NAMELEN];
4007636cb21Slm66018 	enum		{ PROG_init = 0x00,
40119b65a69Ssb155480 				PROG_locks = 0x01,
40219b65a69Ssb155480 				PROG_readmd = 0x02,
40319b65a69Ssb155480 				PROG_fdb = 0x04,
40419b65a69Ssb155480 				PROG_mfdb = 0x08,
40519b65a69Ssb155480 				PROG_taskq = 0x10,
40606db247cSraghuram 				PROG_rx_softint = 0x20,
40706db247cSraghuram 				PROG_swmode = 0x40,
40806db247cSraghuram 				PROG_macreg = 0x80,
40906db247cSraghuram 				PROG_mdreg = 0x100}
4101ae08745Sheppo 			progress;
4111ae08745Sheppo 
4121ae08745Sheppo 	progress = PROG_init;
41319b65a69Ssb155480 	int		rv;
4141ae08745Sheppo 
4151ae08745Sheppo 	switch (cmd) {
4161ae08745Sheppo 	case DDI_ATTACH:
4171ae08745Sheppo 		break;
4181ae08745Sheppo 	case DDI_RESUME:
4191ae08745Sheppo 		/* nothing to do for this non-device */
4201ae08745Sheppo 		return (DDI_SUCCESS);
4211ae08745Sheppo 	case DDI_PM_RESUME:
4221ae08745Sheppo 	default:
4231ae08745Sheppo 		return (DDI_FAILURE);
4241ae08745Sheppo 	}
4251ae08745Sheppo 
4261ae08745Sheppo 	instance = ddi_get_instance(dip);
4271ae08745Sheppo 	if (ddi_soft_state_zalloc(vsw_state, instance) != DDI_SUCCESS) {
4281ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_soft_state_zalloc failed", instance);
4291ae08745Sheppo 		return (DDI_FAILURE);
4301ae08745Sheppo 	}
4311ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
4321ae08745Sheppo 
4331ae08745Sheppo 	if (vswp == NULL) {
4341ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_get_soft_state failed", instance);
4351ae08745Sheppo 		goto vsw_attach_fail;
4361ae08745Sheppo 	}
4371ae08745Sheppo 
4381ae08745Sheppo 	vswp->dip = dip;
4391ae08745Sheppo 	vswp->instance = instance;
4401ae08745Sheppo 	ddi_set_driver_private(dip, (caddr_t)vswp);
4411ae08745Sheppo 
4425f94e909Ssg70180 	mutex_init(&vswp->hw_lock, NULL, MUTEX_DRIVER, NULL);
44334683adeSsg70180 	mutex_init(&vswp->mac_lock, NULL, MUTEX_DRIVER, NULL);
44419b65a69Ssb155480 	mutex_init(&vswp->mca_lock, NULL, MUTEX_DRIVER, NULL);
44519b65a69Ssb155480 	mutex_init(&vswp->swtmout_lock, NULL, MUTEX_DRIVER, NULL);
4461ae08745Sheppo 	rw_init(&vswp->if_lockrw, NULL, RW_DRIVER, NULL);
44719b65a69Ssb155480 	rw_init(&vswp->mfdbrw, NULL, RW_DRIVER, NULL);
44819b65a69Ssb155480 	rw_init(&vswp->plist.lockrw, NULL, RW_DRIVER, NULL);
44919b65a69Ssb155480 
45019b65a69Ssb155480 	progress |= PROG_locks;
45119b65a69Ssb155480 
45219b65a69Ssb155480 	rv = vsw_read_mdprops(vswp);
45319b65a69Ssb155480 	if (rv != 0)
45419b65a69Ssb155480 		goto vsw_attach_fail;
45519b65a69Ssb155480 
45619b65a69Ssb155480 	progress |= PROG_readmd;
4571ae08745Sheppo 
4581ae08745Sheppo 	/* setup the unicast forwarding database  */
4591ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_unicst_table-%d",
4601ae08745Sheppo 	    vswp->instance);
4611ae08745Sheppo 	D2(vswp, "creating unicast hash table (%s)...", hashname);
4621ae08745Sheppo 	vswp->fdb = mod_hash_create_ptrhash(hashname, VSW_NCHAINS,
4631ae08745Sheppo 	    mod_hash_null_valdtor, sizeof (void *));
4641ae08745Sheppo 
4651ae08745Sheppo 	progress |= PROG_fdb;
4661ae08745Sheppo 
4671ae08745Sheppo 	/* setup the multicast fowarding database */
4681ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_mcst_table-%d",
4691ae08745Sheppo 	    vswp->instance);
4701ae08745Sheppo 	D2(vswp, "creating multicast hash table %s)...", hashname);
4711ae08745Sheppo 	vswp->mfdb = mod_hash_create_ptrhash(hashname, VSW_NCHAINS,
4721ae08745Sheppo 	    mod_hash_null_valdtor, sizeof (void *));
4731ae08745Sheppo 
4741ae08745Sheppo 	progress |= PROG_mfdb;
4751ae08745Sheppo 
4761ae08745Sheppo 	/*
4771ae08745Sheppo 	 * Create the taskq which will process all the VIO
4781ae08745Sheppo 	 * control messages.
4791ae08745Sheppo 	 */
4801ae08745Sheppo 	(void) snprintf(qname, TASKQ_NAMELEN, "vsw_taskq%d", vswp->instance);
4811ae08745Sheppo 	if ((vswp->taskq_p = ddi_taskq_create(vswp->dip, qname, 1,
4821ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
48334683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to create task queue",
48434683adeSsg70180 		    vswp->instance);
4851ae08745Sheppo 		goto vsw_attach_fail;
4861ae08745Sheppo 	}
4871ae08745Sheppo 
4881ae08745Sheppo 	progress |= PROG_taskq;
4891ae08745Sheppo 
49006db247cSraghuram 	/*
49106db247cSraghuram 	 * If LDC receive thread is enabled, then we need a
49206db247cSraghuram 	 * soft-interrupt to deliver the packets to the upper layers.
49306db247cSraghuram 	 * This applies only to the packets that need to be sent up
49406db247cSraghuram 	 * the stack, but not to the packets that are sent out via
49506db247cSraghuram 	 * the physical interface.
49606db247cSraghuram 	 */
49706db247cSraghuram 	if (vsw_ldc_rxthr_enabled) {
49806db247cSraghuram 		vswp->rx_mhead = vswp->rx_mtail = NULL;
49906db247cSraghuram 		vswp->soft_pri = PIL_4;
50006db247cSraghuram 		vswp->rx_softint = B_TRUE;
50106db247cSraghuram 
50206db247cSraghuram 		rv = ddi_intr_add_softint(vswp->dip, &vswp->soft_handle,
50306db247cSraghuram 		    vswp->soft_pri, vsw_rx_softintr, (void *)vswp);
50406db247cSraghuram 		if (rv != DDI_SUCCESS) {
50506db247cSraghuram 			cmn_err(CE_WARN, "!vsw%d: add_softint failed rv(%d)",
50606db247cSraghuram 			    vswp->instance, rv);
50706db247cSraghuram 			goto vsw_attach_fail;
50806db247cSraghuram 		}
50906db247cSraghuram 
51006db247cSraghuram 		/*
51106db247cSraghuram 		 * Initialize the soft_lock with the same priority as
51206db247cSraghuram 		 * the soft interrupt to protect from the soft interrupt.
51306db247cSraghuram 		 */
51406db247cSraghuram 		mutex_init(&vswp->soft_lock, NULL, MUTEX_DRIVER,
51506db247cSraghuram 		    DDI_INTR_PRI(vswp->soft_pri));
51606db247cSraghuram 		progress |= PROG_rx_softint;
51706db247cSraghuram 	} else {
51806db247cSraghuram 		vswp->rx_softint = B_FALSE;
51906db247cSraghuram 	}
52006db247cSraghuram 
521d10e4ef2Snarayan 	/* prevent auto-detaching */
522d10e4ef2Snarayan 	if (ddi_prop_update_int(DDI_DEV_T_NONE, vswp->dip,
523d10e4ef2Snarayan 	    DDI_NO_AUTODETACH, 1) != DDI_SUCCESS) {
52434683adeSsg70180 		cmn_err(CE_NOTE, "!Unable to set \"%s\" property for "
525d10e4ef2Snarayan 		    "instance %u", DDI_NO_AUTODETACH, instance);
526d10e4ef2Snarayan 	}
527d10e4ef2Snarayan 
5281ae08745Sheppo 	/*
52919b65a69Ssb155480 	 * Setup the required switching mode,
53019b65a69Ssb155480 	 * based on the mdprops that we read earlier.
53119b65a69Ssb155480 	 */
53219b65a69Ssb155480 	rv = vsw_setup_switching(vswp);
53319b65a69Ssb155480 	if (rv == EAGAIN) {
53419b65a69Ssb155480 		/*
53519b65a69Ssb155480 		 * Unable to setup switching mode;
53619b65a69Ssb155480 		 * as the error is EAGAIN, schedule a timeout to retry.
53719b65a69Ssb155480 		 */
53819b65a69Ssb155480 		mutex_enter(&vswp->swtmout_lock);
53919b65a69Ssb155480 
54019b65a69Ssb155480 		vswp->swtmout_enabled = B_TRUE;
54119b65a69Ssb155480 		vswp->swtmout_id =
54219b65a69Ssb155480 		    timeout(vsw_setup_switching_timeout, vswp,
54319b65a69Ssb155480 		    (vsw_setup_switching_delay * drv_usectohz(MICROSEC)));
54419b65a69Ssb155480 
54519b65a69Ssb155480 		mutex_exit(&vswp->swtmout_lock);
54619b65a69Ssb155480 	} else if (rv != 0) {
54719b65a69Ssb155480 		goto vsw_attach_fail;
54819b65a69Ssb155480 	}
54919b65a69Ssb155480 
55019b65a69Ssb155480 	progress |= PROG_swmode;
55119b65a69Ssb155480 
55219b65a69Ssb155480 	/* Register with mac layer as a provider */
55319b65a69Ssb155480 	rv = vsw_mac_register(vswp);
55419b65a69Ssb155480 	if (rv != 0)
55519b65a69Ssb155480 		goto vsw_attach_fail;
55619b65a69Ssb155480 
55719b65a69Ssb155480 	progress |= PROG_macreg;
55819b65a69Ssb155480 
55919b65a69Ssb155480 	/*
56034683adeSsg70180 	 * Now we have everything setup, register an interest in
56134683adeSsg70180 	 * specific MD nodes.
56234683adeSsg70180 	 *
56334683adeSsg70180 	 * The callback is invoked in 2 cases, firstly if upon mdeg
56434683adeSsg70180 	 * registration there are existing nodes which match our specified
56534683adeSsg70180 	 * criteria, and secondly if the MD is changed (and again, there
56634683adeSsg70180 	 * are nodes which we are interested in present within it. Note
56734683adeSsg70180 	 * that our callback will be invoked even if our specified nodes
56834683adeSsg70180 	 * have not actually changed).
56934683adeSsg70180 	 *
5701ae08745Sheppo 	 */
57119b65a69Ssb155480 	rv = vsw_mdeg_register(vswp);
57219b65a69Ssb155480 	if (rv != 0)
57334683adeSsg70180 		goto vsw_attach_fail;
5741ae08745Sheppo 
57519b65a69Ssb155480 	progress |= PROG_mdreg;
57619b65a69Ssb155480 
57719b65a69Ssb155480 	WRITE_ENTER(&vsw_rw);
57819b65a69Ssb155480 	vswp->next = vsw_head;
57919b65a69Ssb155480 	vsw_head = vswp;
58019b65a69Ssb155480 	RW_EXIT(&vsw_rw);
58119b65a69Ssb155480 
58219b65a69Ssb155480 	ddi_report_dev(vswp->dip);
5831ae08745Sheppo 	return (DDI_SUCCESS);
5841ae08745Sheppo 
5851ae08745Sheppo vsw_attach_fail:
5861ae08745Sheppo 	DERR(NULL, "vsw_attach: failed");
5871ae08745Sheppo 
58806db247cSraghuram 	if (progress & PROG_rx_softint) {
58906db247cSraghuram 		(void) ddi_intr_remove_softint(vswp->soft_handle);
59006db247cSraghuram 		mutex_destroy(&vswp->soft_lock);
59106db247cSraghuram 	}
59206db247cSraghuram 
59319b65a69Ssb155480 	if (progress & PROG_mdreg) {
59419b65a69Ssb155480 		vsw_mdeg_unregister(vswp);
59519b65a69Ssb155480 		(void) vsw_detach_ports(vswp);
59619b65a69Ssb155480 	}
59719b65a69Ssb155480 
59819b65a69Ssb155480 	if (progress & PROG_macreg)
59919b65a69Ssb155480 		(void) vsw_mac_unregister(vswp);
60019b65a69Ssb155480 
60119b65a69Ssb155480 	if (progress & PROG_swmode) {
60219b65a69Ssb155480 		vsw_stop_switching_timeout(vswp);
60319b65a69Ssb155480 		mutex_enter(&vswp->mac_lock);
60419b65a69Ssb155480 		vsw_mac_detach(vswp);
60519b65a69Ssb155480 		vsw_mac_close(vswp);
60619b65a69Ssb155480 		mutex_exit(&vswp->mac_lock);
60719b65a69Ssb155480 	}
60819b65a69Ssb155480 
6091ae08745Sheppo 	if (progress & PROG_taskq)
6101ae08745Sheppo 		ddi_taskq_destroy(vswp->taskq_p);
6111ae08745Sheppo 
61219b65a69Ssb155480 	if (progress & PROG_mfdb)
6131ae08745Sheppo 		mod_hash_destroy_hash(vswp->mfdb);
6141ae08745Sheppo 
61519b65a69Ssb155480 	if (progress & PROG_fdb)
6161ae08745Sheppo 		mod_hash_destroy_hash(vswp->fdb);
6171ae08745Sheppo 
61819b65a69Ssb155480 	if (progress & PROG_locks) {
61919b65a69Ssb155480 		rw_destroy(&vswp->plist.lockrw);
62019b65a69Ssb155480 		rw_destroy(&vswp->mfdbrw);
6211ae08745Sheppo 		rw_destroy(&vswp->if_lockrw);
62219b65a69Ssb155480 		mutex_destroy(&vswp->swtmout_lock);
62319b65a69Ssb155480 		mutex_destroy(&vswp->mca_lock);
62434683adeSsg70180 		mutex_destroy(&vswp->mac_lock);
6255f94e909Ssg70180 		mutex_destroy(&vswp->hw_lock);
62634683adeSsg70180 	}
6271ae08745Sheppo 
6281ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
6291ae08745Sheppo 	return (DDI_FAILURE);
6301ae08745Sheppo }
6311ae08745Sheppo 
6321ae08745Sheppo static int
6331ae08745Sheppo vsw_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6341ae08745Sheppo {
635d10e4ef2Snarayan 	vio_mblk_pool_t		*poolp, *npoolp;
6361ae08745Sheppo 	vsw_t			**vswpp, *vswp;
6371ae08745Sheppo 	int 			instance;
6381ae08745Sheppo 
6391ae08745Sheppo 	instance = ddi_get_instance(dip);
6401ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
6411ae08745Sheppo 
6421ae08745Sheppo 	if (vswp == NULL) {
6431ae08745Sheppo 		return (DDI_FAILURE);
6441ae08745Sheppo 	}
6451ae08745Sheppo 
6461ae08745Sheppo 	switch (cmd) {
6471ae08745Sheppo 	case DDI_DETACH:
6481ae08745Sheppo 		break;
6491ae08745Sheppo 	case DDI_SUSPEND:
6501ae08745Sheppo 	case DDI_PM_SUSPEND:
6511ae08745Sheppo 	default:
6521ae08745Sheppo 		return (DDI_FAILURE);
6531ae08745Sheppo 	}
6541ae08745Sheppo 
6551ae08745Sheppo 	D2(vswp, "detaching instance %d", instance);
6561ae08745Sheppo 
65719b65a69Ssb155480 	/* Stop any pending timeout to setup switching mode. */
65819b65a69Ssb155480 	vsw_stop_switching_timeout(vswp);
65919b65a69Ssb155480 
66034683adeSsg70180 	if (vswp->if_state & VSW_IF_REG) {
6611ae08745Sheppo 		if (vsw_mac_unregister(vswp) != 0) {
66234683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to detach from "
66334683adeSsg70180 			    "MAC layer", vswp->instance);
6641ae08745Sheppo 			return (DDI_FAILURE);
6651ae08745Sheppo 		}
666d10e4ef2Snarayan 	}
6671ae08745Sheppo 
66806db247cSraghuram 	/*
66906db247cSraghuram 	 * Destroy/free up the receive thread related structures.
67006db247cSraghuram 	 */
67106db247cSraghuram 	if (vswp->rx_softint == B_TRUE) {
67206db247cSraghuram 		(void) ddi_intr_remove_softint(vswp->soft_handle);
67306db247cSraghuram 		mutex_destroy(&vswp->soft_lock);
67406db247cSraghuram 		if (vswp->rx_mhead != NULL) {
67506db247cSraghuram 			freemsgchain(vswp->rx_mhead);
67606db247cSraghuram 			vswp->rx_mhead = vswp->rx_mtail = NULL;
67706db247cSraghuram 		}
67806db247cSraghuram 	}
67906db247cSraghuram 
6801ae08745Sheppo 	vsw_mdeg_unregister(vswp);
6811ae08745Sheppo 
682e1ebb9ecSlm66018 	/* remove mac layer callback */
68334683adeSsg70180 	mutex_enter(&vswp->mac_lock);
684e1ebb9ecSlm66018 	if ((vswp->mh != NULL) && (vswp->mrh != NULL)) {
6851f8aaf0dSethindra 		mac_rx_remove(vswp->mh, vswp->mrh, B_TRUE);
686e1ebb9ecSlm66018 		vswp->mrh = NULL;
6871ae08745Sheppo 	}
68834683adeSsg70180 	mutex_exit(&vswp->mac_lock);
6891ae08745Sheppo 
6901ae08745Sheppo 	if (vsw_detach_ports(vswp) != 0) {
69134683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to detach ports",
69234683adeSsg70180 		    vswp->instance);
6931ae08745Sheppo 		return (DDI_FAILURE);
6941ae08745Sheppo 	}
6951ae08745Sheppo 
69634683adeSsg70180 	rw_destroy(&vswp->if_lockrw);
69734683adeSsg70180 
6985f94e909Ssg70180 	mutex_destroy(&vswp->hw_lock);
6995f94e909Ssg70180 
7001ae08745Sheppo 	/*
701e1ebb9ecSlm66018 	 * Now that the ports have been deleted, stop and close
702e1ebb9ecSlm66018 	 * the physical device.
703e1ebb9ecSlm66018 	 */
70434683adeSsg70180 	mutex_enter(&vswp->mac_lock);
705e1ebb9ecSlm66018 
70619b65a69Ssb155480 	vsw_mac_detach(vswp);
70719b65a69Ssb155480 	vsw_mac_close(vswp);
70819b65a69Ssb155480 
70934683adeSsg70180 	mutex_exit(&vswp->mac_lock);
71019b65a69Ssb155480 
71134683adeSsg70180 	mutex_destroy(&vswp->mac_lock);
71219b65a69Ssb155480 	mutex_destroy(&vswp->swtmout_lock);
713e1ebb9ecSlm66018 
714e1ebb9ecSlm66018 	/*
715d10e4ef2Snarayan 	 * Destroy any free pools that may still exist.
716d10e4ef2Snarayan 	 */
717d10e4ef2Snarayan 	poolp = vswp->rxh;
718d10e4ef2Snarayan 	while (poolp != NULL) {
719d10e4ef2Snarayan 		npoolp = vswp->rxh = poolp->nextp;
720d10e4ef2Snarayan 		if (vio_destroy_mblks(poolp) != 0) {
721d10e4ef2Snarayan 			vswp->rxh = poolp;
722d10e4ef2Snarayan 			return (DDI_FAILURE);
723d10e4ef2Snarayan 		}
724d10e4ef2Snarayan 		poolp = npoolp;
725d10e4ef2Snarayan 	}
726d10e4ef2Snarayan 
727d10e4ef2Snarayan 	/*
7281ae08745Sheppo 	 * Remove this instance from any entries it may be on in
7291ae08745Sheppo 	 * the hash table by using the list of addresses maintained
7301ae08745Sheppo 	 * in the vsw_t structure.
7311ae08745Sheppo 	 */
7321ae08745Sheppo 	vsw_del_mcst_vsw(vswp);
7331ae08745Sheppo 
7341ae08745Sheppo 	vswp->mcap = NULL;
7351ae08745Sheppo 	mutex_destroy(&vswp->mca_lock);
7361ae08745Sheppo 
7371ae08745Sheppo 	/*
7381ae08745Sheppo 	 * By now any pending tasks have finished and the underlying
7391ae08745Sheppo 	 * ldc's have been destroyed, so its safe to delete the control
7401ae08745Sheppo 	 * message taskq.
7411ae08745Sheppo 	 */
7421ae08745Sheppo 	if (vswp->taskq_p != NULL)
7431ae08745Sheppo 		ddi_taskq_destroy(vswp->taskq_p);
7441ae08745Sheppo 
7451ae08745Sheppo 	/*
7461ae08745Sheppo 	 * At this stage all the data pointers in the hash table
7471ae08745Sheppo 	 * should be NULL, as all the ports have been removed and will
7481ae08745Sheppo 	 * have deleted themselves from the port lists which the data
7491ae08745Sheppo 	 * pointers point to. Hence we can destroy the table using the
7501ae08745Sheppo 	 * default destructors.
7511ae08745Sheppo 	 */
7521ae08745Sheppo 	D2(vswp, "vsw_detach: destroying hash tables..");
7531ae08745Sheppo 	mod_hash_destroy_hash(vswp->fdb);
7541ae08745Sheppo 	vswp->fdb = NULL;
7551ae08745Sheppo 
7561ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
7571ae08745Sheppo 	mod_hash_destroy_hash(vswp->mfdb);
7581ae08745Sheppo 	vswp->mfdb = NULL;
7591ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
7601ae08745Sheppo 	rw_destroy(&vswp->mfdbrw);
7611ae08745Sheppo 
7621ae08745Sheppo 	ddi_remove_minor_node(dip, NULL);
7631ae08745Sheppo 
7641ae08745Sheppo 	rw_destroy(&vswp->plist.lockrw);
7651ae08745Sheppo 	WRITE_ENTER(&vsw_rw);
7661ae08745Sheppo 	for (vswpp = &vsw_head; *vswpp; vswpp = &(*vswpp)->next) {
7671ae08745Sheppo 		if (*vswpp == vswp) {
7681ae08745Sheppo 			*vswpp = vswp->next;
7691ae08745Sheppo 			break;
7701ae08745Sheppo 		}
7711ae08745Sheppo 	}
7721ae08745Sheppo 	RW_EXIT(&vsw_rw);
7731ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
7741ae08745Sheppo 
7751ae08745Sheppo 	return (DDI_SUCCESS);
7761ae08745Sheppo }
7771ae08745Sheppo 
7781ae08745Sheppo static int
7791ae08745Sheppo vsw_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
7801ae08745Sheppo {
7811ae08745Sheppo 	_NOTE(ARGUNUSED(dip))
7821ae08745Sheppo 
7831ae08745Sheppo 	vsw_t	*vswp = NULL;
7841ae08745Sheppo 	dev_t	dev = (dev_t)arg;
7851ae08745Sheppo 	int	instance;
7861ae08745Sheppo 
7871ae08745Sheppo 	instance = getminor(dev);
7881ae08745Sheppo 
7891ae08745Sheppo 	switch (infocmd) {
7901ae08745Sheppo 	case DDI_INFO_DEVT2DEVINFO:
7911ae08745Sheppo 		if ((vswp = ddi_get_soft_state(vsw_state, instance)) == NULL) {
7921ae08745Sheppo 			*result = NULL;
7931ae08745Sheppo 			return (DDI_FAILURE);
7941ae08745Sheppo 		}
7951ae08745Sheppo 		*result = vswp->dip;
7961ae08745Sheppo 		return (DDI_SUCCESS);
7971ae08745Sheppo 
7981ae08745Sheppo 	case DDI_INFO_DEVT2INSTANCE:
7991ae08745Sheppo 		*result = (void *)(uintptr_t)instance;
8001ae08745Sheppo 		return (DDI_SUCCESS);
8011ae08745Sheppo 
8021ae08745Sheppo 	default:
8031ae08745Sheppo 		*result = NULL;
8041ae08745Sheppo 		return (DDI_FAILURE);
8051ae08745Sheppo 	}
8061ae08745Sheppo }
8071ae08745Sheppo 
8081ae08745Sheppo /*
80934683adeSsg70180  * Get the value of the "vsw-phys-dev" property in the specified
81034683adeSsg70180  * node. This property is the name of the physical device that
81134683adeSsg70180  * the virtual switch will use to talk to the outside world.
81234683adeSsg70180  *
81334683adeSsg70180  * Note it is valid for this property to be NULL (but the property
81434683adeSsg70180  * itself must exist). Callers of this routine should verify that
81534683adeSsg70180  * the value returned is what they expected (i.e. either NULL or non NULL).
81634683adeSsg70180  *
81734683adeSsg70180  * On success returns value of the property in region pointed to by
81834683adeSsg70180  * the 'name' argument, and with return value of 0. Otherwise returns 1.
8191ae08745Sheppo  */
82034683adeSsg70180 static int
82134683adeSsg70180 vsw_get_md_physname(vsw_t *vswp, md_t *mdp, mde_cookie_t node, char *name)
8221ae08745Sheppo {
82334683adeSsg70180 	int		len = 0;
824*f2b610cfSwentaoy 	int		instance;
8251ae08745Sheppo 	char		*physname = NULL;
8261ae08745Sheppo 	char		*dev;
827*f2b610cfSwentaoy 	const char	*dev_name;
828*f2b610cfSwentaoy 	char		myname[MAXNAMELEN];
829*f2b610cfSwentaoy 
830*f2b610cfSwentaoy 	dev_name = ddi_driver_name(vswp->dip);
831*f2b610cfSwentaoy 	instance = ddi_get_instance(vswp->dip);
832*f2b610cfSwentaoy 	(void) snprintf(myname, MAXNAMELEN, "%s%d", dev_name, instance);
8331ae08745Sheppo 
83434683adeSsg70180 	if (md_get_prop_data(mdp, node, physdev_propname,
8351ae08745Sheppo 	    (uint8_t **)(&physname), &len) != 0) {
83634683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get name(s) of physical "
83734683adeSsg70180 		    "device(s) from MD", vswp->instance);
83834683adeSsg70180 		return (1);
8391ae08745Sheppo 	} else if ((strlen(physname) + 1) > LIFNAMSIZ) {
84034683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: %s is too long a device name",
84134683adeSsg70180 		    vswp->instance, physname);
84234683adeSsg70180 		return (1);
843*f2b610cfSwentaoy 	} else if (strcmp(myname, physname) == 0) {
844*f2b610cfSwentaoy 		/*
845*f2b610cfSwentaoy 		 * Prevent the vswitch from opening itself as the
846*f2b610cfSwentaoy 		 * network device.
847*f2b610cfSwentaoy 		 */
848*f2b610cfSwentaoy 		cmn_err(CE_WARN, "!vsw%d: %s is an invalid device name",
849*f2b610cfSwentaoy 		    vswp->instance, physname);
850*f2b610cfSwentaoy 		return (1);
8511ae08745Sheppo 	} else {
85234683adeSsg70180 		(void) strncpy(name, physname, strlen(physname) + 1);
8531ae08745Sheppo 		D2(vswp, "%s: using first device specified (%s)",
85434683adeSsg70180 		    __func__, physname);
8551ae08745Sheppo 	}
8561ae08745Sheppo 
8571ae08745Sheppo #ifdef DEBUG
8581ae08745Sheppo 	/*
8591ae08745Sheppo 	 * As a temporary measure to aid testing we check to see if there
8601ae08745Sheppo 	 * is a vsw.conf file present. If there is we use the value of the
8611ae08745Sheppo 	 * vsw_physname property in the file as the name of the physical
8621ae08745Sheppo 	 * device, overriding the value from the MD.
8631ae08745Sheppo 	 *
8641ae08745Sheppo 	 * There may be multiple devices listed, but for the moment
8651ae08745Sheppo 	 * we just use the first one.
8661ae08745Sheppo 	 */
8671ae08745Sheppo 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vswp->dip, 0,
8681ae08745Sheppo 	    "vsw_physname", &dev) == DDI_PROP_SUCCESS) {
8691ae08745Sheppo 		if ((strlen(dev) + 1) > LIFNAMSIZ) {
87034683adeSsg70180 			cmn_err(CE_WARN, "vsw%d: %s is too long a device name",
87134683adeSsg70180 			    vswp->instance, dev);
87234683adeSsg70180 			ddi_prop_free(dev);
87334683adeSsg70180 			return (1);
8741ae08745Sheppo 		} else {
87534683adeSsg70180 			cmn_err(CE_NOTE, "vsw%d: Using device name (%s) from "
87634683adeSsg70180 			    "config file", vswp->instance, dev);
8771ae08745Sheppo 
87834683adeSsg70180 			(void) strncpy(name, dev, strlen(dev) + 1);
8791ae08745Sheppo 		}
8801ae08745Sheppo 
8811ae08745Sheppo 		ddi_prop_free(dev);
8821ae08745Sheppo 	}
8831ae08745Sheppo #endif
8841ae08745Sheppo 
88534683adeSsg70180 	return (0);
88634683adeSsg70180 }
887e1ebb9ecSlm66018 
888e1ebb9ecSlm66018 /*
88934683adeSsg70180  * Read the 'vsw-switch-mode' property from the specified MD node.
89034683adeSsg70180  *
89134683adeSsg70180  * Returns 0 on success and the number of modes found in 'found',
89234683adeSsg70180  * otherwise returns 1.
893e1ebb9ecSlm66018  */
89434683adeSsg70180 static int
89534683adeSsg70180 vsw_get_md_smodes(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
89634683adeSsg70180 						uint8_t *modes, int *found)
89734683adeSsg70180 {
89834683adeSsg70180 	int		len = 0;
89934683adeSsg70180 	int		smode_num = 0;
90034683adeSsg70180 	char		*smode = NULL;
90134683adeSsg70180 	char		*curr_mode = NULL;
90234683adeSsg70180 
90334683adeSsg70180 	D1(vswp, "%s: enter", __func__);
9041ae08745Sheppo 
9051ae08745Sheppo 	/*
9061ae08745Sheppo 	 * Get the switch-mode property. The modes are listed in
9071ae08745Sheppo 	 * decreasing order of preference, i.e. prefered mode is
9081ae08745Sheppo 	 * first item in list.
9091ae08745Sheppo 	 */
9101ae08745Sheppo 	len = 0;
91134683adeSsg70180 	smode_num = 0;
91234683adeSsg70180 	if (md_get_prop_data(mdp, node, smode_propname,
9131ae08745Sheppo 	    (uint8_t **)(&smode), &len) != 0) {
9141ae08745Sheppo 		/*
915e1ebb9ecSlm66018 		 * Unable to get switch-mode property from MD, nothing
916e1ebb9ecSlm66018 		 * more we can do.
9171ae08745Sheppo 		 */
91834683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get switch mode property"
91934683adeSsg70180 		    " from the MD", vswp->instance);
92034683adeSsg70180 		*found = 0;
92134683adeSsg70180 		return (1);
922e1ebb9ecSlm66018 	}
923e1ebb9ecSlm66018 
9241ae08745Sheppo 	curr_mode = smode;
9251ae08745Sheppo 	/*
9261ae08745Sheppo 	 * Modes of operation:
9271ae08745Sheppo 	 * 'switched'	 - layer 2 switching, underlying HW in
928e1ebb9ecSlm66018 	 *			programmed mode.
9291ae08745Sheppo 	 * 'promiscuous' - layer 2 switching, underlying HW in
9301ae08745Sheppo 	 *			promiscuous mode.
9311ae08745Sheppo 	 * 'routed'	 - layer 3 (i.e. IP) routing, underlying HW
9321ae08745Sheppo 	 *			in non-promiscuous mode.
9331ae08745Sheppo 	 */
93434683adeSsg70180 	while ((curr_mode < (smode + len)) && (smode_num < NUM_SMODES)) {
9351ae08745Sheppo 		D2(vswp, "%s: curr_mode = [%s]", __func__, curr_mode);
936e1ebb9ecSlm66018 		if (strcmp(curr_mode, "switched") == 0) {
93734683adeSsg70180 			modes[smode_num++] = VSW_LAYER2;
938e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "promiscuous") == 0) {
93934683adeSsg70180 			modes[smode_num++] = VSW_LAYER2_PROMISC;
940e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "routed") == 0) {
94134683adeSsg70180 			modes[smode_num++] = VSW_LAYER3;
942e1ebb9ecSlm66018 		} else {
94334683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unknown switch mode %s, "
94434683adeSsg70180 			    "setting to default switched mode",
94534683adeSsg70180 			    vswp->instance, curr_mode);
94634683adeSsg70180 			modes[smode_num++] = VSW_LAYER2;
9471ae08745Sheppo 		}
9481ae08745Sheppo 		curr_mode += strlen(curr_mode) + 1;
9491ae08745Sheppo 	}
95034683adeSsg70180 	*found = smode_num;
9511ae08745Sheppo 
95234683adeSsg70180 	D2(vswp, "%s: %d modes found", __func__, smode_num);
9531ae08745Sheppo 
9541ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
95534683adeSsg70180 
95634683adeSsg70180 	return (0);
9571ae08745Sheppo }
9581ae08745Sheppo 
959e1ebb9ecSlm66018 /*
9601ae08745Sheppo  * Register with the MAC layer as a network device, so we
9611ae08745Sheppo  * can be plumbed if necessary.
9621ae08745Sheppo  */
9631ae08745Sheppo static int
9641ae08745Sheppo vsw_mac_register(vsw_t *vswp)
9651ae08745Sheppo {
966ba2e4443Sseb 	mac_register_t	*macp;
967ba2e4443Sseb 	int		rv;
9681ae08745Sheppo 
9691ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
9701ae08745Sheppo 
971ba2e4443Sseb 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
972ba2e4443Sseb 		return (EINVAL);
973ba2e4443Sseb 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
9741ae08745Sheppo 	macp->m_driver = vswp;
975ba2e4443Sseb 	macp->m_dip = vswp->dip;
976ba2e4443Sseb 	macp->m_src_addr = (uint8_t *)&vswp->if_addr;
977ba2e4443Sseb 	macp->m_callbacks = &vsw_m_callbacks;
978ba2e4443Sseb 	macp->m_min_sdu = 0;
979ba2e4443Sseb 	macp->m_max_sdu = ETHERMTU;
980ba2e4443Sseb 	rv = mac_register(macp, &vswp->if_mh);
981ba2e4443Sseb 	mac_free(macp);
98219b65a69Ssb155480 	if (rv != 0) {
98319b65a69Ssb155480 		/*
98419b65a69Ssb155480 		 * Treat this as a non-fatal error as we may be
98519b65a69Ssb155480 		 * able to operate in some other mode.
98619b65a69Ssb155480 		 */
98719b65a69Ssb155480 		cmn_err(CE_NOTE, "!vsw%d: Unable to register as "
98819b65a69Ssb155480 		    "a provider with MAC layer", vswp->instance);
98919b65a69Ssb155480 		return (rv);
99019b65a69Ssb155480 	}
99119b65a69Ssb155480 
992ba2e4443Sseb 	vswp->if_state |= VSW_IF_REG;
9931ae08745Sheppo 
9941ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
9951ae08745Sheppo 
9961ae08745Sheppo 	return (rv);
9971ae08745Sheppo }
9981ae08745Sheppo 
9991ae08745Sheppo static int
10001ae08745Sheppo vsw_mac_unregister(vsw_t *vswp)
10011ae08745Sheppo {
10021ae08745Sheppo 	int		rv = 0;
10031ae08745Sheppo 
10041ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10051ae08745Sheppo 
10061ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
10071ae08745Sheppo 
1008ba2e4443Sseb 	if (vswp->if_state & VSW_IF_REG) {
1009ba2e4443Sseb 		rv = mac_unregister(vswp->if_mh);
10101ae08745Sheppo 		if (rv != 0) {
10111ae08745Sheppo 			DWARN(vswp, "%s: unable to unregister from MAC "
10121ae08745Sheppo 			    "framework", __func__);
10131ae08745Sheppo 
10141ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
10151ae08745Sheppo 			D1(vswp, "%s: fail exit", __func__);
10161ae08745Sheppo 			return (rv);
10171ae08745Sheppo 		}
10181ae08745Sheppo 
1019ba2e4443Sseb 		/* mark i/f as down and unregistered */
1020ba2e4443Sseb 		vswp->if_state &= ~(VSW_IF_UP | VSW_IF_REG);
10211ae08745Sheppo 	}
10221ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
10231ae08745Sheppo 
10241ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
10251ae08745Sheppo 
10261ae08745Sheppo 	return (rv);
10271ae08745Sheppo }
10281ae08745Sheppo 
1029ba2e4443Sseb static int
1030ba2e4443Sseb vsw_m_stat(void *arg, uint_t stat, uint64_t *val)
10311ae08745Sheppo {
10321ae08745Sheppo 	vsw_t			*vswp = (vsw_t *)arg;
10331ae08745Sheppo 
10341ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10351ae08745Sheppo 
103634683adeSsg70180 	mutex_enter(&vswp->mac_lock);
103734683adeSsg70180 	if (vswp->mh == NULL) {
103834683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1039ba2e4443Sseb 		return (EINVAL);
104034683adeSsg70180 	}
10411ae08745Sheppo 
10421ae08745Sheppo 	/* return stats from underlying device */
1043ba2e4443Sseb 	*val = mac_stat_get(vswp->mh, stat);
104434683adeSsg70180 
104534683adeSsg70180 	mutex_exit(&vswp->mac_lock);
104634683adeSsg70180 
1047ba2e4443Sseb 	return (0);
10481ae08745Sheppo }
10491ae08745Sheppo 
10501ae08745Sheppo static void
10511ae08745Sheppo vsw_m_stop(void *arg)
10521ae08745Sheppo {
10531ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
10541ae08745Sheppo 
10551ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10561ae08745Sheppo 
10571ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
10581ae08745Sheppo 	vswp->if_state &= ~VSW_IF_UP;
10591ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
10601ae08745Sheppo 
10615f94e909Ssg70180 	mutex_enter(&vswp->hw_lock);
10625f94e909Ssg70180 
10635f94e909Ssg70180 	(void) vsw_unset_hw(vswp, NULL, VSW_LOCALDEV);
10645f94e909Ssg70180 
10655f94e909Ssg70180 	if (vswp->recfg_reqd)
10665f94e909Ssg70180 		vsw_reconfig_hw(vswp);
10675f94e909Ssg70180 
10685f94e909Ssg70180 	mutex_exit(&vswp->hw_lock);
10695f94e909Ssg70180 
10701ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
10711ae08745Sheppo }
10721ae08745Sheppo 
10731ae08745Sheppo static int
10741ae08745Sheppo vsw_m_start(void *arg)
10751ae08745Sheppo {
10761ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
10771ae08745Sheppo 
10781ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10791ae08745Sheppo 
10801ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
10811ae08745Sheppo 
108219b65a69Ssb155480 	vswp->if_state |= VSW_IF_UP;
108319b65a69Ssb155480 
108419b65a69Ssb155480 	if (vswp->switching_setup_done == B_FALSE) {
108519b65a69Ssb155480 		/*
108619b65a69Ssb155480 		 * If the switching mode has not been setup yet, just
108719b65a69Ssb155480 		 * return. The unicast address will be programmed
108819b65a69Ssb155480 		 * after the physical device is successfully setup by the
108919b65a69Ssb155480 		 * timeout handler.
109019b65a69Ssb155480 		 */
109119b65a69Ssb155480 		RW_EXIT(&vswp->if_lockrw);
109219b65a69Ssb155480 		return (0);
109319b65a69Ssb155480 	}
109419b65a69Ssb155480 
109519b65a69Ssb155480 	/* if in layer2 mode, program unicast address. */
109619b65a69Ssb155480 	if (vswp->mh != NULL) {
10975f94e909Ssg70180 		mutex_enter(&vswp->hw_lock);
10985f94e909Ssg70180 		(void) vsw_set_hw(vswp, NULL, VSW_LOCALDEV);
10995f94e909Ssg70180 		mutex_exit(&vswp->hw_lock);
110019b65a69Ssb155480 	}
110119b65a69Ssb155480 
110219b65a69Ssb155480 	RW_EXIT(&vswp->if_lockrw);
11035f94e909Ssg70180 
11041ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
11051ae08745Sheppo 	return (0);
11061ae08745Sheppo }
11071ae08745Sheppo 
11081ae08745Sheppo /*
11091ae08745Sheppo  * Change the local interface address.
11105f94e909Ssg70180  *
11115f94e909Ssg70180  * Note: we don't support this entry point. The local
11125f94e909Ssg70180  * mac address of the switch can only be changed via its
11135f94e909Ssg70180  * MD node properties.
11141ae08745Sheppo  */
11151ae08745Sheppo static int
11161ae08745Sheppo vsw_m_unicst(void *arg, const uint8_t *macaddr)
11171ae08745Sheppo {
11185f94e909Ssg70180 	_NOTE(ARGUNUSED(arg, macaddr))
11191ae08745Sheppo 
11205f94e909Ssg70180 	return (DDI_FAILURE);
11211ae08745Sheppo }
11221ae08745Sheppo 
11231ae08745Sheppo static int
11241ae08745Sheppo vsw_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
11251ae08745Sheppo {
11261ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
11271ae08745Sheppo 	mcst_addr_t	*mcst_p = NULL;
11281ae08745Sheppo 	uint64_t	addr = 0x0;
1129e1ebb9ecSlm66018 	int		i, ret = 0;
11301ae08745Sheppo 
11311ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11321ae08745Sheppo 
11331ae08745Sheppo 	/*
11341ae08745Sheppo 	 * Convert address into form that can be used
11351ae08745Sheppo 	 * as hash table key.
11361ae08745Sheppo 	 */
11371ae08745Sheppo 	for (i = 0; i < ETHERADDRL; i++) {
11381ae08745Sheppo 		addr = (addr << 8) | mca[i];
11391ae08745Sheppo 	}
11401ae08745Sheppo 
11411ae08745Sheppo 	D2(vswp, "%s: addr = 0x%llx", __func__, addr);
11421ae08745Sheppo 
11431ae08745Sheppo 	if (add) {
11441ae08745Sheppo 		D2(vswp, "%s: adding multicast", __func__);
11451ae08745Sheppo 		if (vsw_add_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
11461ae08745Sheppo 			/*
11471ae08745Sheppo 			 * Update the list of multicast addresses
11481ae08745Sheppo 			 * contained within the vsw_t structure to
11491ae08745Sheppo 			 * include this new one.
11501ae08745Sheppo 			 */
11511ae08745Sheppo 			mcst_p = kmem_zalloc(sizeof (mcst_addr_t), KM_NOSLEEP);
11521ae08745Sheppo 			if (mcst_p == NULL) {
11531ae08745Sheppo 				DERR(vswp, "%s unable to alloc mem", __func__);
115419b65a69Ssb155480 				(void) vsw_del_mcst(vswp,
115519b65a69Ssb155480 				    VSW_LOCALDEV, addr, NULL);
11561ae08745Sheppo 				return (1);
11571ae08745Sheppo 			}
11581ae08745Sheppo 			mcst_p->addr = addr;
115919b65a69Ssb155480 			ether_copy(mca, &mcst_p->mca);
11601ae08745Sheppo 
11611ae08745Sheppo 			/*
11621ae08745Sheppo 			 * Call into the underlying driver to program the
11631ae08745Sheppo 			 * address into HW.
11641ae08745Sheppo 			 */
116534683adeSsg70180 			mutex_enter(&vswp->mac_lock);
1166e1ebb9ecSlm66018 			if (vswp->mh != NULL) {
1167e1ebb9ecSlm66018 				ret = mac_multicst_add(vswp->mh, mca);
1168e1ebb9ecSlm66018 				if (ret != 0) {
116934683adeSsg70180 					cmn_err(CE_WARN, "!vsw%d: unable to "
117034683adeSsg70180 					    "add multicast address",
117134683adeSsg70180 					    vswp->instance);
117234683adeSsg70180 					mutex_exit(&vswp->mac_lock);
117319b65a69Ssb155480 					(void) vsw_del_mcst(vswp,
117419b65a69Ssb155480 					    VSW_LOCALDEV, addr, NULL);
117519b65a69Ssb155480 					kmem_free(mcst_p, sizeof (*mcst_p));
117619b65a69Ssb155480 					return (ret);
1177e1ebb9ecSlm66018 				}
117819b65a69Ssb155480 				mcst_p->mac_added = B_TRUE;
11791ae08745Sheppo 			}
118034683adeSsg70180 			mutex_exit(&vswp->mac_lock);
118119b65a69Ssb155480 
118219b65a69Ssb155480 			mutex_enter(&vswp->mca_lock);
118319b65a69Ssb155480 			mcst_p->nextp = vswp->mcap;
118419b65a69Ssb155480 			vswp->mcap = mcst_p;
118519b65a69Ssb155480 			mutex_exit(&vswp->mca_lock);
11861ae08745Sheppo 		} else {
118734683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: unable to add multicast "
118834683adeSsg70180 			    "address", vswp->instance);
1189e1ebb9ecSlm66018 		}
1190e1ebb9ecSlm66018 		return (ret);
1191e1ebb9ecSlm66018 	}
1192e1ebb9ecSlm66018 
11931ae08745Sheppo 	D2(vswp, "%s: removing multicast", __func__);
11941ae08745Sheppo 	/*
11951ae08745Sheppo 	 * Remove the address from the hash table..
11961ae08745Sheppo 	 */
11971ae08745Sheppo 	if (vsw_del_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
11981ae08745Sheppo 
11991ae08745Sheppo 		/*
12001ae08745Sheppo 		 * ..and then from the list maintained in the
12011ae08745Sheppo 		 * vsw_t structure.
12021ae08745Sheppo 		 */
120319b65a69Ssb155480 		mcst_p = vsw_del_addr(VSW_LOCALDEV, vswp, addr);
120419b65a69Ssb155480 		ASSERT(mcst_p != NULL);
12051ae08745Sheppo 
120634683adeSsg70180 		mutex_enter(&vswp->mac_lock);
120719b65a69Ssb155480 		if (vswp->mh != NULL && mcst_p->mac_added) {
12081ae08745Sheppo 			(void) mac_multicst_remove(vswp->mh, mca);
120919b65a69Ssb155480 			mcst_p->mac_added = B_FALSE;
121019b65a69Ssb155480 		}
121134683adeSsg70180 		mutex_exit(&vswp->mac_lock);
121219b65a69Ssb155480 		kmem_free(mcst_p, sizeof (*mcst_p));
12131ae08745Sheppo 	}
12141ae08745Sheppo 
12151ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
12161ae08745Sheppo 
12171ae08745Sheppo 	return (0);
12181ae08745Sheppo }
12191ae08745Sheppo 
12201ae08745Sheppo static int
12211ae08745Sheppo vsw_m_promisc(void *arg, boolean_t on)
12221ae08745Sheppo {
12231ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
12241ae08745Sheppo 
12251ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
12261ae08745Sheppo 
12271ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
12281ae08745Sheppo 	if (on)
12291ae08745Sheppo 		vswp->if_state |= VSW_IF_PROMISC;
12301ae08745Sheppo 	else
12311ae08745Sheppo 		vswp->if_state &= ~VSW_IF_PROMISC;
12321ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
12331ae08745Sheppo 
12341ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
12351ae08745Sheppo 
12361ae08745Sheppo 	return (0);
12371ae08745Sheppo }
12381ae08745Sheppo 
12391ae08745Sheppo static mblk_t *
12401ae08745Sheppo vsw_m_tx(void *arg, mblk_t *mp)
12411ae08745Sheppo {
12421ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
12431ae08745Sheppo 
12441ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
12451ae08745Sheppo 
124634683adeSsg70180 	vswp->vsw_switch_frame(vswp, mp, VSW_LOCALDEV, NULL, NULL);
12471ae08745Sheppo 
12481ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
12491ae08745Sheppo 
12501ae08745Sheppo 	return (NULL);
12511ae08745Sheppo }
12521ae08745Sheppo 
12531ae08745Sheppo /*
12541ae08745Sheppo  * Register for machine description (MD) updates.
125534683adeSsg70180  *
125634683adeSsg70180  * Returns 0 on success, 1 on failure.
12571ae08745Sheppo  */
125834683adeSsg70180 static int
12591ae08745Sheppo vsw_mdeg_register(vsw_t *vswp)
12601ae08745Sheppo {
12611ae08745Sheppo 	mdeg_prop_spec_t	*pspecp;
12621ae08745Sheppo 	mdeg_node_spec_t	*inst_specp;
126334683adeSsg70180 	mdeg_handle_t		mdeg_hdl, mdeg_port_hdl;
12641ae08745Sheppo 	size_t			templatesz;
126519b65a69Ssb155480 	int			rv;
12661ae08745Sheppo 
12671ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
12681ae08745Sheppo 
126934683adeSsg70180 	/*
12701ae08745Sheppo 	 * Allocate and initialize a per-instance copy
12711ae08745Sheppo 	 * of the global property spec array that will
12721ae08745Sheppo 	 * uniquely identify this vsw instance.
12731ae08745Sheppo 	 */
12741ae08745Sheppo 	templatesz = sizeof (vsw_prop_template);
12751ae08745Sheppo 	pspecp = kmem_zalloc(templatesz, KM_SLEEP);
12761ae08745Sheppo 
12771ae08745Sheppo 	bcopy(vsw_prop_template, pspecp, templatesz);
12781ae08745Sheppo 
127919b65a69Ssb155480 	VSW_SET_MDEG_PROP_INST(pspecp, vswp->regprop);
12801ae08745Sheppo 
12811ae08745Sheppo 	/* initialize the complete prop spec structure */
12821ae08745Sheppo 	inst_specp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
12831ae08745Sheppo 	inst_specp->namep = "virtual-device";
12841ae08745Sheppo 	inst_specp->specp = pspecp;
12851ae08745Sheppo 
128619b65a69Ssb155480 	D2(vswp, "%s: instance %d registering with mdeg", __func__,
128719b65a69Ssb155480 	    vswp->regprop);
128834683adeSsg70180 	/*
128934683adeSsg70180 	 * Register an interest in 'virtual-device' nodes with a
129034683adeSsg70180 	 * 'name' property of 'virtual-network-switch'
129134683adeSsg70180 	 */
129234683adeSsg70180 	rv = mdeg_register(inst_specp, &vdev_match, vsw_mdeg_cb,
12931ae08745Sheppo 	    (void *)vswp, &mdeg_hdl);
129434683adeSsg70180 	if (rv != MDEG_SUCCESS) {
129534683adeSsg70180 		DERR(vswp, "%s: mdeg_register failed (%d) for vsw node",
129634683adeSsg70180 		    __func__, rv);
129734683adeSsg70180 		goto mdeg_reg_fail;
129834683adeSsg70180 	}
12991ae08745Sheppo 
130034683adeSsg70180 	/*
130134683adeSsg70180 	 * Register an interest in 'vsw-port' nodes.
130234683adeSsg70180 	 */
130334683adeSsg70180 	rv = mdeg_register(inst_specp, &vport_match, vsw_port_mdeg_cb,
130434683adeSsg70180 	    (void *)vswp, &mdeg_port_hdl);
13051ae08745Sheppo 	if (rv != MDEG_SUCCESS) {
13061ae08745Sheppo 		DERR(vswp, "%s: mdeg_register failed (%d)\n", __func__, rv);
130734683adeSsg70180 		(void) mdeg_unregister(mdeg_hdl);
130834683adeSsg70180 		goto mdeg_reg_fail;
13091ae08745Sheppo 	}
13101ae08745Sheppo 
13111ae08745Sheppo 	/* save off data that will be needed later */
13121ae08745Sheppo 	vswp->inst_spec = inst_specp;
13131ae08745Sheppo 	vswp->mdeg_hdl = mdeg_hdl;
131434683adeSsg70180 	vswp->mdeg_port_hdl = mdeg_port_hdl;
13151ae08745Sheppo 
13161ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
131734683adeSsg70180 	return (0);
131834683adeSsg70180 
131934683adeSsg70180 mdeg_reg_fail:
132034683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: Unable to register MDEG callbacks",
132134683adeSsg70180 	    vswp->instance);
132234683adeSsg70180 	kmem_free(pspecp, templatesz);
132334683adeSsg70180 	kmem_free(inst_specp, sizeof (mdeg_node_spec_t));
132434683adeSsg70180 
132534683adeSsg70180 	vswp->mdeg_hdl = NULL;
132634683adeSsg70180 	vswp->mdeg_port_hdl = NULL;
132734683adeSsg70180 
132834683adeSsg70180 	return (1);
13291ae08745Sheppo }
13301ae08745Sheppo 
13311ae08745Sheppo static void
13321ae08745Sheppo vsw_mdeg_unregister(vsw_t *vswp)
13331ae08745Sheppo {
13341ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: enter");
13351ae08745Sheppo 
133634683adeSsg70180 	if (vswp->mdeg_hdl != NULL)
13371ae08745Sheppo 		(void) mdeg_unregister(vswp->mdeg_hdl);
13381ae08745Sheppo 
133934683adeSsg70180 	if (vswp->mdeg_port_hdl != NULL)
134034683adeSsg70180 		(void) mdeg_unregister(vswp->mdeg_port_hdl);
134134683adeSsg70180 
134234683adeSsg70180 	if (vswp->inst_spec != NULL) {
13431ae08745Sheppo 		if (vswp->inst_spec->specp != NULL) {
13441ae08745Sheppo 			(void) kmem_free(vswp->inst_spec->specp,
13451ae08745Sheppo 			    sizeof (vsw_prop_template));
13461ae08745Sheppo 			vswp->inst_spec->specp = NULL;
13471ae08745Sheppo 		}
13481ae08745Sheppo 
1349205eeb1aSlm66018 		(void) kmem_free(vswp->inst_spec, sizeof (mdeg_node_spec_t));
13501ae08745Sheppo 		vswp->inst_spec = NULL;
13511ae08745Sheppo 	}
13521ae08745Sheppo 
13531ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: exit");
13541ae08745Sheppo }
13551ae08745Sheppo 
135634683adeSsg70180 /*
135734683adeSsg70180  * Mdeg callback invoked for the vsw node itself.
135834683adeSsg70180  */
13591ae08745Sheppo static int
13601ae08745Sheppo vsw_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
13611ae08745Sheppo {
13621ae08745Sheppo 	vsw_t		*vswp;
13631ae08745Sheppo 	md_t		*mdp;
13641ae08745Sheppo 	mde_cookie_t	node;
13651ae08745Sheppo 	uint64_t	inst;
136634683adeSsg70180 	char		*node_name = NULL;
13671ae08745Sheppo 
13681ae08745Sheppo 	if (resp == NULL)
13691ae08745Sheppo 		return (MDEG_FAILURE);
13701ae08745Sheppo 
13711ae08745Sheppo 	vswp = (vsw_t *)cb_argp;
13721ae08745Sheppo 
137334683adeSsg70180 	D1(vswp, "%s: added %d : removed %d : curr matched %d"
137434683adeSsg70180 	    " : prev matched %d", __func__, resp->added.nelem,
137534683adeSsg70180 	    resp->removed.nelem, resp->match_curr.nelem,
137634683adeSsg70180 	    resp->match_prev.nelem);
137734683adeSsg70180 
137834683adeSsg70180 	/*
137919b65a69Ssb155480 	 * We get an initial callback for this node as 'added'
138019b65a69Ssb155480 	 * after registering with mdeg. Note that we would have
138119b65a69Ssb155480 	 * already gathered information about this vsw node by
138219b65a69Ssb155480 	 * walking MD earlier during attach (in vsw_read_mdprops()).
138319b65a69Ssb155480 	 * So, there is a window where the properties of this
138419b65a69Ssb155480 	 * node might have changed when we get this initial 'added'
138519b65a69Ssb155480 	 * callback. We handle this as if an update occured
138619b65a69Ssb155480 	 * and invoke the same function which handles updates to
138719b65a69Ssb155480 	 * the properties of this vsw-node if any.
138819b65a69Ssb155480 	 *
138934683adeSsg70180 	 * A non-zero 'match' value indicates that the MD has been
139019b65a69Ssb155480 	 * updated and that a virtual-network-switch node is
139119b65a69Ssb155480 	 * present which may or may not have been updated. It is
139219b65a69Ssb155480 	 * up to the clients to examine their own nodes and
139319b65a69Ssb155480 	 * determine if they have changed.
139434683adeSsg70180 	 */
139519b65a69Ssb155480 	if (resp->added.nelem != 0) {
139634683adeSsg70180 
139719b65a69Ssb155480 		if (resp->added.nelem != 1) {
139819b65a69Ssb155480 			cmn_err(CE_NOTE, "!vsw%d: number of nodes added "
139919b65a69Ssb155480 			    "invalid: %d\n", vswp->instance, resp->added.nelem);
140019b65a69Ssb155480 			return (MDEG_FAILURE);
140119b65a69Ssb155480 		}
140219b65a69Ssb155480 
140319b65a69Ssb155480 		mdp = resp->added.mdp;
140419b65a69Ssb155480 		node = resp->added.mdep[0];
140519b65a69Ssb155480 
140619b65a69Ssb155480 	} else if (resp->match_curr.nelem != 0) {
140719b65a69Ssb155480 
140819b65a69Ssb155480 		if (resp->match_curr.nelem != 1) {
140919b65a69Ssb155480 			cmn_err(CE_NOTE, "!vsw%d: number of nodes updated "
141019b65a69Ssb155480 			    "invalid: %d\n", vswp->instance,
141119b65a69Ssb155480 			    resp->match_curr.nelem);
141219b65a69Ssb155480 			return (MDEG_FAILURE);
141319b65a69Ssb155480 		}
141419b65a69Ssb155480 
141519b65a69Ssb155480 		mdp = resp->match_curr.mdp;
141619b65a69Ssb155480 		node = resp->match_curr.mdep[0];
141719b65a69Ssb155480 
141819b65a69Ssb155480 	} else {
141919b65a69Ssb155480 		return (MDEG_FAILURE);
142019b65a69Ssb155480 	}
142119b65a69Ssb155480 
142219b65a69Ssb155480 	/* Validate name and instance */
142334683adeSsg70180 	if (md_get_prop_str(mdp, node, "name", &node_name) != 0) {
142419b65a69Ssb155480 		DERR(vswp, "%s: unable to get node name\n",  __func__);
142519b65a69Ssb155480 		return (MDEG_FAILURE);
142619b65a69Ssb155480 	}
142719b65a69Ssb155480 
142819b65a69Ssb155480 	/* is this a virtual-network-switch? */
142919b65a69Ssb155480 	if (strcmp(node_name, vsw_propname) != 0) {
143019b65a69Ssb155480 		DERR(vswp, "%s: Invalid node name: %s\n",
143119b65a69Ssb155480 		    __func__, node_name);
143219b65a69Ssb155480 		return (MDEG_FAILURE);
143334683adeSsg70180 	}
143434683adeSsg70180 
143534683adeSsg70180 	if (md_get_prop_val(mdp, node, "cfg-handle", &inst)) {
143619b65a69Ssb155480 		DERR(vswp, "%s: prop(cfg-handle) not found\n",
143719b65a69Ssb155480 		    __func__);
143819b65a69Ssb155480 		return (MDEG_FAILURE);
143934683adeSsg70180 	}
144034683adeSsg70180 
144119b65a69Ssb155480 	/* is this the right instance of vsw? */
144219b65a69Ssb155480 	if (inst != vswp->regprop) {
144319b65a69Ssb155480 		DERR(vswp, "%s: Invalid cfg-handle: %lx\n",
144419b65a69Ssb155480 		    __func__, inst);
144519b65a69Ssb155480 		return (MDEG_FAILURE);
144619b65a69Ssb155480 	}
144734683adeSsg70180 
144834683adeSsg70180 	vsw_update_md_prop(vswp, mdp, node);
144934683adeSsg70180 
145034683adeSsg70180 	return (MDEG_SUCCESS);
145134683adeSsg70180 }
145234683adeSsg70180 
145334683adeSsg70180 /*
145434683adeSsg70180  * Mdeg callback invoked for changes to the vsw-port nodes
145534683adeSsg70180  * under the vsw node.
145634683adeSsg70180  */
145734683adeSsg70180 static int
145834683adeSsg70180 vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
145934683adeSsg70180 {
146034683adeSsg70180 	vsw_t		*vswp;
146134683adeSsg70180 	int		idx;
146234683adeSsg70180 	md_t		*mdp;
146334683adeSsg70180 	mde_cookie_t	node;
146434683adeSsg70180 	uint64_t	inst;
146534683adeSsg70180 
146634683adeSsg70180 	if ((resp == NULL) || (cb_argp == NULL))
146734683adeSsg70180 		return (MDEG_FAILURE);
146834683adeSsg70180 
146934683adeSsg70180 	vswp = (vsw_t *)cb_argp;
147034683adeSsg70180 
147134683adeSsg70180 	D2(vswp, "%s: added %d : removed %d : curr matched %d"
147234683adeSsg70180 	    " : prev matched %d", __func__, resp->added.nelem,
147334683adeSsg70180 	    resp->removed.nelem, resp->match_curr.nelem,
14741ae08745Sheppo 	    resp->match_prev.nelem);
14751ae08745Sheppo 
14761ae08745Sheppo 	/* process added ports */
14771ae08745Sheppo 	for (idx = 0; idx < resp->added.nelem; idx++) {
14781ae08745Sheppo 		mdp = resp->added.mdp;
14791ae08745Sheppo 		node = resp->added.mdep[idx];
14801ae08745Sheppo 
14811ae08745Sheppo 		D2(vswp, "%s: adding node(%d) 0x%lx", __func__, idx, node);
14821ae08745Sheppo 
14831ae08745Sheppo 		if (vsw_port_add(vswp, mdp, &node) != 0) {
148434683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to add new port "
148534683adeSsg70180 			    "(0x%lx)", vswp->instance, node);
14861ae08745Sheppo 		}
14871ae08745Sheppo 	}
14881ae08745Sheppo 
14891ae08745Sheppo 	/* process removed ports */
14901ae08745Sheppo 	for (idx = 0; idx < resp->removed.nelem; idx++) {
14911ae08745Sheppo 		mdp = resp->removed.mdp;
14921ae08745Sheppo 		node = resp->removed.mdep[idx];
14931ae08745Sheppo 
14941ae08745Sheppo 		if (md_get_prop_val(mdp, node, id_propname, &inst)) {
149534683adeSsg70180 			DERR(vswp, "%s: prop(%s) not found in port(%d)",
14961ae08745Sheppo 			    __func__, id_propname, idx);
14971ae08745Sheppo 			continue;
14981ae08745Sheppo 		}
14991ae08745Sheppo 
15001ae08745Sheppo 		D2(vswp, "%s: removing node(%d) 0x%lx", __func__, idx, node);
15011ae08745Sheppo 
15021ae08745Sheppo 		if (vsw_port_detach(vswp, inst) != 0) {
150334683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to remove port %ld",
150434683adeSsg70180 			    vswp->instance, inst);
15051ae08745Sheppo 		}
15061ae08745Sheppo 	}
15071ae08745Sheppo 
15081ae08745Sheppo 	/*
15091ae08745Sheppo 	 * Currently no support for updating already active ports.
15101ae08745Sheppo 	 * So, ignore the match_curr and match_priv arrays for now.
15111ae08745Sheppo 	 */
15121ae08745Sheppo 
15131ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
15141ae08745Sheppo 
15151ae08745Sheppo 	return (MDEG_SUCCESS);
15161ae08745Sheppo }
15171ae08745Sheppo 
15181ae08745Sheppo /*
151919b65a69Ssb155480  * Scan the machine description for this instance of vsw
152019b65a69Ssb155480  * and read its properties. Called only from vsw_attach().
152119b65a69Ssb155480  * Returns: 0 on success, 1 on failure.
152219b65a69Ssb155480  */
152319b65a69Ssb155480 static int
152419b65a69Ssb155480 vsw_read_mdprops(vsw_t *vswp)
152519b65a69Ssb155480 {
152619b65a69Ssb155480 	md_t		*mdp = NULL;
152719b65a69Ssb155480 	mde_cookie_t	rootnode;
152819b65a69Ssb155480 	mde_cookie_t	*listp = NULL;
152919b65a69Ssb155480 	uint64_t	inst;
153019b65a69Ssb155480 	uint64_t	cfgh;
153119b65a69Ssb155480 	char		*name;
153219b65a69Ssb155480 	int		rv = 1;
153319b65a69Ssb155480 	int		num_nodes = 0;
153419b65a69Ssb155480 	int		num_devs = 0;
153519b65a69Ssb155480 	int		listsz = 0;
153619b65a69Ssb155480 	int		i;
153719b65a69Ssb155480 
153819b65a69Ssb155480 	/*
153919b65a69Ssb155480 	 * In each 'virtual-device' node in the MD there is a
154019b65a69Ssb155480 	 * 'cfg-handle' property which is the MD's concept of
154119b65a69Ssb155480 	 * an instance number (this may be completely different from
154219b65a69Ssb155480 	 * the device drivers instance #). OBP reads that value and
154319b65a69Ssb155480 	 * stores it in the 'reg' property of the appropriate node in
154419b65a69Ssb155480 	 * the device tree. We first read this reg property and use this
154519b65a69Ssb155480 	 * to compare against the 'cfg-handle' property of vsw nodes
154619b65a69Ssb155480 	 * in MD to get to this specific vsw instance and then read
154719b65a69Ssb155480 	 * other properties that we are interested in.
154819b65a69Ssb155480 	 * We also cache the value of 'reg' property and use it later
154919b65a69Ssb155480 	 * to register callbacks with mdeg (see vsw_mdeg_register())
155019b65a69Ssb155480 	 */
155119b65a69Ssb155480 	inst = ddi_prop_get_int(DDI_DEV_T_ANY, vswp->dip,
155219b65a69Ssb155480 	    DDI_PROP_DONTPASS, reg_propname, -1);
155319b65a69Ssb155480 	if (inst == -1) {
155419b65a69Ssb155480 		cmn_err(CE_NOTE, "!vsw%d: Unable to read %s property from "
155519b65a69Ssb155480 		    "OBP device tree", vswp->instance, reg_propname);
155619b65a69Ssb155480 		return (rv);
155719b65a69Ssb155480 	}
155819b65a69Ssb155480 
155919b65a69Ssb155480 	vswp->regprop = inst;
156019b65a69Ssb155480 
156119b65a69Ssb155480 	if ((mdp = md_get_handle()) == NULL) {
156219b65a69Ssb155480 		DWARN(vswp, "%s: cannot init MD\n", __func__);
156319b65a69Ssb155480 		return (rv);
156419b65a69Ssb155480 	}
156519b65a69Ssb155480 
156619b65a69Ssb155480 	num_nodes = md_node_count(mdp);
156719b65a69Ssb155480 	ASSERT(num_nodes > 0);
156819b65a69Ssb155480 
156919b65a69Ssb155480 	listsz = num_nodes * sizeof (mde_cookie_t);
157019b65a69Ssb155480 	listp = (mde_cookie_t *)kmem_zalloc(listsz, KM_SLEEP);
157119b65a69Ssb155480 
157219b65a69Ssb155480 	rootnode = md_root_node(mdp);
157319b65a69Ssb155480 
157419b65a69Ssb155480 	/* search for all "virtual_device" nodes */
157519b65a69Ssb155480 	num_devs = md_scan_dag(mdp, rootnode,
157619b65a69Ssb155480 	    md_find_name(mdp, vdev_propname),
157719b65a69Ssb155480 	    md_find_name(mdp, "fwd"), listp);
157819b65a69Ssb155480 	if (num_devs <= 0) {
157919b65a69Ssb155480 		DWARN(vswp, "%s: invalid num_devs:%d\n", __func__, num_devs);
158019b65a69Ssb155480 		goto vsw_readmd_exit;
158119b65a69Ssb155480 	}
158219b65a69Ssb155480 
158319b65a69Ssb155480 	/*
158419b65a69Ssb155480 	 * Now loop through the list of virtual-devices looking for
158519b65a69Ssb155480 	 * devices with name "virtual-network-switch" and for each
158619b65a69Ssb155480 	 * such device compare its instance with what we have from
158719b65a69Ssb155480 	 * the 'reg' property to find the right node in MD and then
158819b65a69Ssb155480 	 * read all its properties.
158919b65a69Ssb155480 	 */
159019b65a69Ssb155480 	for (i = 0; i < num_devs; i++) {
159119b65a69Ssb155480 
159219b65a69Ssb155480 		if (md_get_prop_str(mdp, listp[i], "name", &name) != 0) {
159319b65a69Ssb155480 			DWARN(vswp, "%s: name property not found\n",
159419b65a69Ssb155480 			    __func__);
159519b65a69Ssb155480 			goto vsw_readmd_exit;
159619b65a69Ssb155480 		}
159719b65a69Ssb155480 
159819b65a69Ssb155480 		/* is this a virtual-network-switch? */
159919b65a69Ssb155480 		if (strcmp(name, vsw_propname) != 0)
160019b65a69Ssb155480 			continue;
160119b65a69Ssb155480 
160219b65a69Ssb155480 		if (md_get_prop_val(mdp, listp[i], "cfg-handle", &cfgh) != 0) {
160319b65a69Ssb155480 			DWARN(vswp, "%s: cfg-handle property not found\n",
160419b65a69Ssb155480 			    __func__);
160519b65a69Ssb155480 			goto vsw_readmd_exit;
160619b65a69Ssb155480 		}
160719b65a69Ssb155480 
160819b65a69Ssb155480 		/* is this the required instance of vsw? */
160919b65a69Ssb155480 		if (inst != cfgh)
161019b65a69Ssb155480 			continue;
161119b65a69Ssb155480 
161219b65a69Ssb155480 		/* now read all properties of this vsw instance */
161319b65a69Ssb155480 		rv = vsw_get_initial_md_properties(vswp, mdp, listp[i]);
161419b65a69Ssb155480 		break;
161519b65a69Ssb155480 	}
161619b65a69Ssb155480 
161719b65a69Ssb155480 vsw_readmd_exit:
161819b65a69Ssb155480 
161919b65a69Ssb155480 	kmem_free(listp, listsz);
162019b65a69Ssb155480 	(void) md_fini_handle(mdp);
162119b65a69Ssb155480 	return (rv);
162219b65a69Ssb155480 }
162319b65a69Ssb155480 
162419b65a69Ssb155480 /*
162534683adeSsg70180  * Read the initial start-of-day values from the specified MD node.
162634683adeSsg70180  */
162719b65a69Ssb155480 static int
162834683adeSsg70180 vsw_get_initial_md_properties(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
162934683adeSsg70180 {
163034683adeSsg70180 	int		i;
163134683adeSsg70180 	uint64_t 	macaddr = 0;
163234683adeSsg70180 
163334683adeSsg70180 	D1(vswp, "%s: enter", __func__);
163434683adeSsg70180 
163519b65a69Ssb155480 	if (vsw_get_md_physname(vswp, mdp, node, vswp->physname) != 0) {
163619b65a69Ssb155480 		return (1);
163734683adeSsg70180 	}
163834683adeSsg70180 
163934683adeSsg70180 	/* mac address for vswitch device itself */
164034683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
164134683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
164234683adeSsg70180 		    vswp->instance);
164319b65a69Ssb155480 		return (1);
164419b65a69Ssb155480 	}
164534683adeSsg70180 
164619b65a69Ssb155480 	vsw_save_lmacaddr(vswp, macaddr);
164734683adeSsg70180 
1648205eeb1aSlm66018 	if (vsw_get_md_smodes(vswp, mdp, node, vswp->smode, &vswp->smode_num)) {
164934683adeSsg70180 		cmn_err(CE_WARN, "vsw%d: Unable to read %s property from "
165034683adeSsg70180 		    "MD, defaulting to programmed mode", vswp->instance,
165134683adeSsg70180 		    smode_propname);
165234683adeSsg70180 
165334683adeSsg70180 		for (i = 0; i < NUM_SMODES; i++)
165434683adeSsg70180 			vswp->smode[i] = VSW_LAYER2;
165534683adeSsg70180 
165634683adeSsg70180 		vswp->smode_num = NUM_SMODES;
165734683adeSsg70180 	} else {
165834683adeSsg70180 		ASSERT(vswp->smode_num != 0);
165934683adeSsg70180 	}
166034683adeSsg70180 
166134683adeSsg70180 	D1(vswp, "%s: exit", __func__);
166219b65a69Ssb155480 	return (0);
166334683adeSsg70180 }
166434683adeSsg70180 
166534683adeSsg70180 /*
166634683adeSsg70180  * Check to see if the relevant properties in the specified node have
166734683adeSsg70180  * changed, and if so take the appropriate action.
166834683adeSsg70180  *
166934683adeSsg70180  * If any of the properties are missing or invalid we don't take
167034683adeSsg70180  * any action, as this function should only be invoked when modifications
167134683adeSsg70180  * have been made to what we assume is a working configuration, which
167234683adeSsg70180  * we leave active.
167334683adeSsg70180  *
167434683adeSsg70180  * Note it is legal for this routine to be invoked even if none of the
167534683adeSsg70180  * properties in the port node within the MD have actually changed.
167634683adeSsg70180  */
167734683adeSsg70180 static void
167834683adeSsg70180 vsw_update_md_prop(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
167934683adeSsg70180 {
168034683adeSsg70180 	char		physname[LIFNAMSIZ];
168134683adeSsg70180 	char		drv[LIFNAMSIZ];
168234683adeSsg70180 	uint_t		ddi_instance;
168334683adeSsg70180 	uint8_t		new_smode[NUM_SMODES];
168434683adeSsg70180 	int		i, smode_num = 0;
168534683adeSsg70180 	uint64_t 	macaddr = 0;
168634683adeSsg70180 	enum		{MD_init = 0x1,
168734683adeSsg70180 				MD_physname = 0x2,
168834683adeSsg70180 				MD_macaddr = 0x4,
168934683adeSsg70180 				MD_smode = 0x8} updated;
169019b65a69Ssb155480 	int		rv;
169134683adeSsg70180 
169234683adeSsg70180 	updated = MD_init;
169334683adeSsg70180 
169434683adeSsg70180 	D1(vswp, "%s: enter", __func__);
169534683adeSsg70180 
169634683adeSsg70180 	/*
169734683adeSsg70180 	 * Check if name of physical device in MD has changed.
169834683adeSsg70180 	 */
169934683adeSsg70180 	if (vsw_get_md_physname(vswp, mdp, node, (char *)&physname) == 0) {
170034683adeSsg70180 		/*
170134683adeSsg70180 		 * Do basic sanity check on new device name/instance,
170234683adeSsg70180 		 * if its non NULL. It is valid for the device name to
170334683adeSsg70180 		 * have changed from a non NULL to a NULL value, i.e.
170434683adeSsg70180 		 * the vsw is being changed to 'routed' mode.
170534683adeSsg70180 		 */
170634683adeSsg70180 		if ((strlen(physname) != 0) &&
170719b65a69Ssb155480 		    (ddi_parse(physname, drv,
170819b65a69Ssb155480 		    &ddi_instance) != DDI_SUCCESS)) {
170934683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: new device name %s is not"
171034683adeSsg70180 			    " a valid device name/instance",
171134683adeSsg70180 			    vswp->instance, physname);
171234683adeSsg70180 			goto fail_reconf;
171334683adeSsg70180 		}
171434683adeSsg70180 
171534683adeSsg70180 		if (strcmp(physname, vswp->physname)) {
171634683adeSsg70180 			D2(vswp, "%s: device name changed from %s to %s",
171734683adeSsg70180 			    __func__, vswp->physname, physname);
171834683adeSsg70180 
171934683adeSsg70180 			updated |= MD_physname;
172034683adeSsg70180 		} else {
172134683adeSsg70180 			D2(vswp, "%s: device name unchanged at %s",
172234683adeSsg70180 			    __func__, vswp->physname);
172334683adeSsg70180 		}
172434683adeSsg70180 	} else {
172534683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read name of physical "
172634683adeSsg70180 		    "device from updated MD.", vswp->instance);
172734683adeSsg70180 		goto fail_reconf;
172834683adeSsg70180 	}
172934683adeSsg70180 
173034683adeSsg70180 	/*
173134683adeSsg70180 	 * Check if MAC address has changed.
173234683adeSsg70180 	 */
173334683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
173434683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
173534683adeSsg70180 		    vswp->instance);
173634683adeSsg70180 		goto fail_reconf;
173734683adeSsg70180 	} else {
173819b65a69Ssb155480 		uint64_t maddr = macaddr;
173934683adeSsg70180 		READ_ENTER(&vswp->if_lockrw);
174034683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
174119b65a69Ssb155480 			if (vswp->if_addr.ether_addr_octet[i]
174219b65a69Ssb155480 			    != (macaddr & 0xFF)) {
174334683adeSsg70180 				D2(vswp, "%s: octet[%d] 0x%x != 0x%x",
174434683adeSsg70180 				    __func__, i,
174534683adeSsg70180 				    vswp->if_addr.ether_addr_octet[i],
174634683adeSsg70180 				    (macaddr & 0xFF));
174734683adeSsg70180 				updated |= MD_macaddr;
174819b65a69Ssb155480 				macaddr = maddr;
174934683adeSsg70180 				break;
175034683adeSsg70180 			}
175134683adeSsg70180 			macaddr >>= 8;
175234683adeSsg70180 		}
175334683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
175419b65a69Ssb155480 		if (updated & MD_macaddr) {
175519b65a69Ssb155480 			vsw_save_lmacaddr(vswp, macaddr);
175619b65a69Ssb155480 		}
175734683adeSsg70180 	}
175834683adeSsg70180 
175934683adeSsg70180 	/*
176034683adeSsg70180 	 * Check if switching modes have changed.
176134683adeSsg70180 	 */
176219b65a69Ssb155480 	if (vsw_get_md_smodes(vswp, mdp, node,
176319b65a69Ssb155480 	    new_smode, &smode_num)) {
176434683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read %s property from MD",
176534683adeSsg70180 		    vswp->instance, smode_propname);
176634683adeSsg70180 		goto fail_reconf;
176734683adeSsg70180 	} else {
176834683adeSsg70180 		ASSERT(smode_num != 0);
176934683adeSsg70180 		if (smode_num != vswp->smode_num) {
177034683adeSsg70180 			D2(vswp, "%s: number of modes changed from %d to %d",
177134683adeSsg70180 			    __func__, vswp->smode_num, smode_num);
177234683adeSsg70180 		}
177334683adeSsg70180 
177434683adeSsg70180 		for (i = 0; i < smode_num; i++) {
177534683adeSsg70180 			if (new_smode[i] != vswp->smode[i]) {
177634683adeSsg70180 				D2(vswp, "%s: mode changed from %d to %d",
177734683adeSsg70180 				    __func__, vswp->smode[i], new_smode[i]);
177834683adeSsg70180 				updated |= MD_smode;
177934683adeSsg70180 				break;
178034683adeSsg70180 			}
178134683adeSsg70180 		}
178234683adeSsg70180 	}
178334683adeSsg70180 
178434683adeSsg70180 	/*
178534683adeSsg70180 	 * Now make any changes which are needed...
178634683adeSsg70180 	 */
178734683adeSsg70180 
178834683adeSsg70180 	if (updated & (MD_physname | MD_smode)) {
178934683adeSsg70180 
179034683adeSsg70180 		/*
179119b65a69Ssb155480 		 * Stop any pending timeout to setup switching mode.
179234683adeSsg70180 		 */
179319b65a69Ssb155480 		vsw_stop_switching_timeout(vswp);
179419b65a69Ssb155480 
179519b65a69Ssb155480 		/*
179619b65a69Ssb155480 		 * Remove unicst, mcst addrs of vsw interface
179719b65a69Ssb155480 		 * and ports from the physdev.
179819b65a69Ssb155480 		 */
179919b65a69Ssb155480 		vsw_unset_addrs(vswp);
180019b65a69Ssb155480 
180119b65a69Ssb155480 		/*
180219b65a69Ssb155480 		 * Stop, detach and close the old device..
180319b65a69Ssb155480 		 */
180419b65a69Ssb155480 		mutex_enter(&vswp->mac_lock);
180519b65a69Ssb155480 
180634683adeSsg70180 		vsw_mac_detach(vswp);
180719b65a69Ssb155480 		vsw_mac_close(vswp);
180819b65a69Ssb155480 
180919b65a69Ssb155480 		mutex_exit(&vswp->mac_lock);
181034683adeSsg70180 
181134683adeSsg70180 		/*
181234683adeSsg70180 		 * Update phys name.
181334683adeSsg70180 		 */
181434683adeSsg70180 		if (updated & MD_physname) {
181534683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: changing from %s to %s",
181634683adeSsg70180 			    vswp->instance, vswp->physname, physname);
181734683adeSsg70180 			(void) strncpy(vswp->physname,
181834683adeSsg70180 			    physname, strlen(physname) + 1);
181934683adeSsg70180 		}
182034683adeSsg70180 
182134683adeSsg70180 		/*
182234683adeSsg70180 		 * Update array with the new switch mode values.
182334683adeSsg70180 		 */
182434683adeSsg70180 		if (updated & MD_smode) {
182534683adeSsg70180 			for (i = 0; i < smode_num; i++)
182634683adeSsg70180 				vswp->smode[i] = new_smode[i];
182734683adeSsg70180 
182834683adeSsg70180 			vswp->smode_num = smode_num;
182934683adeSsg70180 			vswp->smode_idx = 0;
183034683adeSsg70180 		}
183134683adeSsg70180 
183234683adeSsg70180 		/*
183334683adeSsg70180 		 * ..and attach, start the new device.
183434683adeSsg70180 		 */
183519b65a69Ssb155480 		rv = vsw_setup_switching(vswp);
183619b65a69Ssb155480 		if (rv == EAGAIN) {
183719b65a69Ssb155480 			/*
183819b65a69Ssb155480 			 * Unable to setup switching mode.
183919b65a69Ssb155480 			 * As the error is EAGAIN, schedule a timeout to retry
184019b65a69Ssb155480 			 * and return. Programming addresses of ports and
184119b65a69Ssb155480 			 * vsw interface will be done when the timeout handler
184219b65a69Ssb155480 			 * completes successfully.
184319b65a69Ssb155480 			 */
184419b65a69Ssb155480 			mutex_enter(&vswp->swtmout_lock);
184519b65a69Ssb155480 
184619b65a69Ssb155480 			vswp->swtmout_enabled = B_TRUE;
184719b65a69Ssb155480 			vswp->swtmout_id =
184819b65a69Ssb155480 			    timeout(vsw_setup_switching_timeout, vswp,
184919b65a69Ssb155480 			    (vsw_setup_switching_delay *
185019b65a69Ssb155480 			    drv_usectohz(MICROSEC)));
185119b65a69Ssb155480 
185219b65a69Ssb155480 			mutex_exit(&vswp->swtmout_lock);
185319b65a69Ssb155480 
185419b65a69Ssb155480 			return;
185519b65a69Ssb155480 
185619b65a69Ssb155480 		} else if (rv) {
185734683adeSsg70180 			goto fail_update;
185819b65a69Ssb155480 		}
185934683adeSsg70180 
186034683adeSsg70180 		/*
186119b65a69Ssb155480 		 * program unicst, mcst addrs of vsw interface
186219b65a69Ssb155480 		 * and ports in the physdev.
186334683adeSsg70180 		 */
186419b65a69Ssb155480 		vsw_set_addrs(vswp);
186534683adeSsg70180 
186619b65a69Ssb155480 	} else if (updated & MD_macaddr) {
186719b65a69Ssb155480 		/*
186819b65a69Ssb155480 		 * We enter here if only MD_macaddr is exclusively updated.
186919b65a69Ssb155480 		 * If MD_physname and/or MD_smode are also updated, then
187019b65a69Ssb155480 		 * as part of that, we would have implicitly processed
187119b65a69Ssb155480 		 * MD_macaddr update (above).
187219b65a69Ssb155480 		 */
187334683adeSsg70180 		cmn_err(CE_NOTE, "!vsw%d: changing mac address to 0x%lx",
187434683adeSsg70180 		    vswp->instance, macaddr);
187534683adeSsg70180 
187619b65a69Ssb155480 		READ_ENTER(&vswp->if_lockrw);
187719b65a69Ssb155480 		if (vswp->if_state & VSW_IF_UP) {
187834683adeSsg70180 
18795f94e909Ssg70180 			mutex_enter(&vswp->hw_lock);
188019b65a69Ssb155480 			/*
188119b65a69Ssb155480 			 * Remove old mac address of vsw interface
188219b65a69Ssb155480 			 * from the physdev
188319b65a69Ssb155480 			 */
18845f94e909Ssg70180 			(void) vsw_unset_hw(vswp, NULL, VSW_LOCALDEV);
188519b65a69Ssb155480 			/*
188619b65a69Ssb155480 			 * Program new mac address of vsw interface
188719b65a69Ssb155480 			 * in the physdev
188819b65a69Ssb155480 			 */
188919b65a69Ssb155480 			rv = vsw_set_hw(vswp, NULL, VSW_LOCALDEV);
18905f94e909Ssg70180 			mutex_exit(&vswp->hw_lock);
189119b65a69Ssb155480 			if (rv != 0) {
189219b65a69Ssb155480 				cmn_err(CE_NOTE,
189319b65a69Ssb155480 				    "!vsw%d: failed to program interface "
189419b65a69Ssb155480 				    "unicast address\n", vswp->instance);
189519b65a69Ssb155480 			}
18965f94e909Ssg70180 			/*
189734683adeSsg70180 			 * Notify the MAC layer of the changed address.
189834683adeSsg70180 			 */
189919b65a69Ssb155480 			mac_unicst_update(vswp->if_mh,
190019b65a69Ssb155480 			    (uint8_t *)&vswp->if_addr);
190119b65a69Ssb155480 
190219b65a69Ssb155480 		}
190319b65a69Ssb155480 		RW_EXIT(&vswp->if_lockrw);
190419b65a69Ssb155480 
190534683adeSsg70180 	}
190634683adeSsg70180 
190734683adeSsg70180 	return;
190834683adeSsg70180 
190934683adeSsg70180 fail_reconf:
191034683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: configuration unchanged", vswp->instance);
191134683adeSsg70180 	return;
191234683adeSsg70180 
191334683adeSsg70180 fail_update:
191434683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: update of configuration failed",
191534683adeSsg70180 	    vswp->instance);
191634683adeSsg70180 }
191734683adeSsg70180 
191834683adeSsg70180 /*
19191ae08745Sheppo  * Add a new port to the system.
19201ae08745Sheppo  *
19211ae08745Sheppo  * Returns 0 on success, 1 on failure.
19221ae08745Sheppo  */
19231ae08745Sheppo int
19241ae08745Sheppo vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node)
19251ae08745Sheppo {
19261ae08745Sheppo 	uint64_t		ldc_id;
19271ae08745Sheppo 	uint8_t			*addrp;
19281ae08745Sheppo 	int			i, addrsz;
19291ae08745Sheppo 	int			num_nodes = 0, nchan = 0;
19301ae08745Sheppo 	int			listsz = 0;
19311ae08745Sheppo 	mde_cookie_t		*listp = NULL;
19321ae08745Sheppo 	struct ether_addr	ea;
19331ae08745Sheppo 	uint64_t		macaddr;
19341ae08745Sheppo 	uint64_t		inst = 0;
19351ae08745Sheppo 	vsw_port_t		*port;
19361ae08745Sheppo 
19371ae08745Sheppo 	if (md_get_prop_val(mdp, *node, id_propname, &inst)) {
19381ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found", __func__,
19391ae08745Sheppo 		    id_propname);
19401ae08745Sheppo 		return (1);
19411ae08745Sheppo 	}
19421ae08745Sheppo 
19431ae08745Sheppo 	/*
19441ae08745Sheppo 	 * Find the channel endpoint node(s) (which should be under this
19451ae08745Sheppo 	 * port node) which contain the channel id(s).
19461ae08745Sheppo 	 */
19471ae08745Sheppo 	if ((num_nodes = md_node_count(mdp)) <= 0) {
19481ae08745Sheppo 		DERR(vswp, "%s: invalid number of nodes found (%d)",
19491ae08745Sheppo 		    __func__, num_nodes);
19501ae08745Sheppo 		return (1);
19511ae08745Sheppo 	}
19521ae08745Sheppo 
195334683adeSsg70180 	D2(vswp, "%s: %d nodes found", __func__, num_nodes);
195434683adeSsg70180 
19551ae08745Sheppo 	/* allocate enough space for node list */
19561ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
19571ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
19581ae08745Sheppo 
1959205eeb1aSlm66018 	nchan = md_scan_dag(mdp, *node, md_find_name(mdp, chan_propname),
19601ae08745Sheppo 	    md_find_name(mdp, "fwd"), listp);
19611ae08745Sheppo 
19621ae08745Sheppo 	if (nchan <= 0) {
19631ae08745Sheppo 		DWARN(vswp, "%s: no %s nodes found", __func__, chan_propname);
19641ae08745Sheppo 		kmem_free(listp, listsz);
19651ae08745Sheppo 		return (1);
19661ae08745Sheppo 	}
19671ae08745Sheppo 
19681ae08745Sheppo 	D2(vswp, "%s: %d %s nodes found", __func__, nchan, chan_propname);
19691ae08745Sheppo 
19701ae08745Sheppo 	/* use property from first node found */
19711ae08745Sheppo 	if (md_get_prop_val(mdp, listp[0], id_propname, &ldc_id)) {
19721ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found\n", __func__,
19731ae08745Sheppo 		    id_propname);
19741ae08745Sheppo 		kmem_free(listp, listsz);
19751ae08745Sheppo 		return (1);
19761ae08745Sheppo 	}
19771ae08745Sheppo 
19781ae08745Sheppo 	/* don't need list any more */
19791ae08745Sheppo 	kmem_free(listp, listsz);
19801ae08745Sheppo 
19811ae08745Sheppo 	D2(vswp, "%s: ldc_id 0x%llx", __func__, ldc_id);
19821ae08745Sheppo 
19831ae08745Sheppo 	/* read mac-address property */
19841ae08745Sheppo 	if (md_get_prop_data(mdp, *node, remaddr_propname,
19851ae08745Sheppo 	    &addrp, &addrsz)) {
19861ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found",
19871ae08745Sheppo 		    __func__, remaddr_propname);
19881ae08745Sheppo 		return (1);
19891ae08745Sheppo 	}
19901ae08745Sheppo 
19911ae08745Sheppo 	if (addrsz < ETHERADDRL) {
19921ae08745Sheppo 		DWARN(vswp, "%s: invalid address size", __func__);
19931ae08745Sheppo 		return (1);
19941ae08745Sheppo 	}
19951ae08745Sheppo 
19961ae08745Sheppo 	macaddr = *((uint64_t *)addrp);
19971ae08745Sheppo 	D2(vswp, "%s: remote mac address 0x%llx", __func__, macaddr);
19981ae08745Sheppo 
19991ae08745Sheppo 	for (i = ETHERADDRL - 1; i >= 0; i--) {
20001ae08745Sheppo 		ea.ether_addr_octet[i] = macaddr & 0xFF;
20011ae08745Sheppo 		macaddr >>= 8;
20021ae08745Sheppo 	}
20031ae08745Sheppo 
20041ae08745Sheppo 	if (vsw_port_attach(vswp, (int)inst, &ldc_id, 1, &ea) != 0) {
20051ae08745Sheppo 		DERR(vswp, "%s: failed to attach port", __func__);
20061ae08745Sheppo 		return (1);
20071ae08745Sheppo 	}
20081ae08745Sheppo 
20091ae08745Sheppo 	port = vsw_lookup_port(vswp, (int)inst);
20101ae08745Sheppo 
20111ae08745Sheppo 	/* just successfuly created the port, so it should exist */
20121ae08745Sheppo 	ASSERT(port != NULL);
20131ae08745Sheppo 
20141ae08745Sheppo 	return (0);
20151ae08745Sheppo }
20161ae08745Sheppo 
20171ae08745Sheppo /*
201806db247cSraghuram  * vsw_mac_rx -- A common function to send packets to the interface.
201906db247cSraghuram  * By default this function check if the interface is UP or not, the
202006db247cSraghuram  * rest of the behaviour depends on the flags as below:
20211ae08745Sheppo  *
202206db247cSraghuram  *	VSW_MACRX_PROMISC -- Check if the promisc mode set or not.
202306db247cSraghuram  *	VSW_MACRX_COPYMSG -- Make a copy of the message(s).
202406db247cSraghuram  *	VSW_MACRX_FREEMSG -- Free if the messages cannot be sent up the stack.
20251ae08745Sheppo  */
20261ae08745Sheppo void
202706db247cSraghuram vsw_mac_rx(vsw_t *vswp, int caller, mac_resource_handle_t mrh,
202806db247cSraghuram     mblk_t *mp, mblk_t *mpt, vsw_macrx_flags_t flags)
20291ae08745Sheppo {
203006db247cSraghuram 	int trigger = 0;
20311ae08745Sheppo 
203206db247cSraghuram 	D1(vswp, "%s:enter\n", __func__);
20331ae08745Sheppo 	READ_ENTER(&vswp->if_lockrw);
203406db247cSraghuram 	/* Check if the interface is up */
203506db247cSraghuram 	if (!(vswp->if_state & VSW_IF_UP)) {
20361ae08745Sheppo 		RW_EXIT(&vswp->if_lockrw);
203706db247cSraghuram 		/* Free messages only if FREEMSG flag specified */
203806db247cSraghuram 		if (flags & VSW_MACRX_FREEMSG) {
203906db247cSraghuram 			freemsgchain(mp);
204006db247cSraghuram 		}
204106db247cSraghuram 		D1(vswp, "%s:exit\n", __func__);
204206db247cSraghuram 		return;
204306db247cSraghuram 	}
204406db247cSraghuram 	/*
204506db247cSraghuram 	 * If PROMISC flag is passed, then check if
204606db247cSraghuram 	 * the interface is in the PROMISC mode.
204706db247cSraghuram 	 * If not, drop the messages.
204806db247cSraghuram 	 */
204906db247cSraghuram 	if (flags & VSW_MACRX_PROMISC) {
205006db247cSraghuram 		if (!(vswp->if_state & VSW_IF_PROMISC)) {
205106db247cSraghuram 			RW_EXIT(&vswp->if_lockrw);
205206db247cSraghuram 			/* Free messages only if FREEMSG flag specified */
205306db247cSraghuram 			if (flags & VSW_MACRX_FREEMSG) {
205406db247cSraghuram 				freemsgchain(mp);
205506db247cSraghuram 			}
205606db247cSraghuram 			D1(vswp, "%s:exit\n", __func__);
205706db247cSraghuram 			return;
205806db247cSraghuram 		}
205906db247cSraghuram 	}
206006db247cSraghuram 	RW_EXIT(&vswp->if_lockrw);
206106db247cSraghuram 	/*
206206db247cSraghuram 	 * If COPYMSG flag is passed, then make a copy
206306db247cSraghuram 	 * of the message chain and send up the copy.
206406db247cSraghuram 	 */
206506db247cSraghuram 	if (flags & VSW_MACRX_COPYMSG) {
206606db247cSraghuram 		mp = copymsgchain(mp);
206706db247cSraghuram 		if (mp) {
206806db247cSraghuram 			mpt = mp;
206906db247cSraghuram 			/* find the tail */
207006db247cSraghuram 			while (mpt->b_next != NULL) {
207106db247cSraghuram 				mpt = mpt->b_next;
207206db247cSraghuram 			}
207306db247cSraghuram 		} else {
207406db247cSraghuram 			D1(vswp, "%s:exit\n", __func__);
207506db247cSraghuram 			return;
207606db247cSraghuram 		}
207706db247cSraghuram 	}
207806db247cSraghuram 
207906db247cSraghuram 	/*
208006db247cSraghuram 	 * If the softint is not enabled or the packets are
208106db247cSraghuram 	 * passed by the physical device, then the caller
208206db247cSraghuram 	 * is expected to be at the interrupt context. For
208306db247cSraghuram 	 * this case, mac_rx() directly.
208406db247cSraghuram 	 */
208506db247cSraghuram 	if ((vswp->rx_softint == B_FALSE) || (caller == VSW_PHYSDEV)) {
208606db247cSraghuram 		ASSERT(servicing_interrupt());
208706db247cSraghuram 		D3(vswp, "%s: sending up stack", __func__);
2088ba2e4443Sseb 		mac_rx(vswp->if_mh, mrh, mp);
208906db247cSraghuram 		D1(vswp, "%s:exit\n", __func__);
209006db247cSraghuram 		return;
20911ae08745Sheppo 	}
20921ae08745Sheppo 
20931ae08745Sheppo 	/*
209406db247cSraghuram 	 * Here we may not be at the interrupt context, so
209506db247cSraghuram 	 * queue the packets and trigger a softint to post
209606db247cSraghuram 	 * the packets up the stack.
20971ae08745Sheppo 	 */
209806db247cSraghuram 	mutex_enter(&vswp->soft_lock);
209906db247cSraghuram 	if (vswp->rx_mhead == NULL) {
210006db247cSraghuram 		vswp->rx_mhead = mp;
210106db247cSraghuram 		vswp->rx_mtail = mpt;
210206db247cSraghuram 		trigger = 1;
21031ae08745Sheppo 	} else {
210406db247cSraghuram 		vswp->rx_mtail->b_next = mp;
210506db247cSraghuram 		vswp->rx_mtail = mpt;
21061ae08745Sheppo 	}
210706db247cSraghuram 	mutex_exit(&vswp->soft_lock);
210806db247cSraghuram 	if (trigger) {
210906db247cSraghuram 		D3(vswp, "%s: triggering the softint", __func__);
211006db247cSraghuram 		(void) ddi_intr_trigger_softint(vswp->soft_handle, NULL);
21111ae08745Sheppo 	}
21121ae08745Sheppo 	D1(vswp, "%s:exit\n", __func__);
21131ae08745Sheppo }
21141ae08745Sheppo 
21151ae08745Sheppo /*
211606db247cSraghuram  * vsw_rx_softintr -- vsw soft interrupt handler function.
211706db247cSraghuram  * Its job is to pickup the recieved packets that are queued
211806db247cSraghuram  * for the interface and send them up.
21191ae08745Sheppo  *
212006db247cSraghuram  * NOTE: An interrupt handler is being used to handle the upper
212106db247cSraghuram  * layer(s) requirement to send up only at interrupt context.
21221ae08745Sheppo  */
212306db247cSraghuram /* ARGSUSED */
212406db247cSraghuram static uint_t
212506db247cSraghuram vsw_rx_softintr(caddr_t arg1, caddr_t arg2)
21261ae08745Sheppo {
212706db247cSraghuram 	mblk_t *mp;
212806db247cSraghuram 	vsw_t *vswp = (vsw_t *)arg1;
21291ae08745Sheppo 
213006db247cSraghuram 	mutex_enter(&vswp->soft_lock);
213106db247cSraghuram 	mp = vswp->rx_mhead;
213206db247cSraghuram 	vswp->rx_mhead = vswp->rx_mtail = NULL;
213306db247cSraghuram 	mutex_exit(&vswp->soft_lock);
213406db247cSraghuram 	if (mp != NULL) {
213506db247cSraghuram 		READ_ENTER(&vswp->if_lockrw);
213606db247cSraghuram 		if (vswp->if_state & VSW_IF_UP) {
213706db247cSraghuram 			RW_EXIT(&vswp->if_lockrw);
213806db247cSraghuram 			mac_rx(vswp->if_mh, NULL, mp);
213906db247cSraghuram 		} else {
214006db247cSraghuram 			RW_EXIT(&vswp->if_lockrw);
21411ae08745Sheppo 			freemsgchain(mp);
21421ae08745Sheppo 		}
21431ae08745Sheppo 	}
214406db247cSraghuram 	D1(vswp, "%s:exit\n", __func__);
214506db247cSraghuram 	return (DDI_INTR_CLAIMED);
21461ae08745Sheppo }
21471ae08745Sheppo 
214806db247cSraghuram /* copy mac address of vsw into soft state structure */
21491ae08745Sheppo static void
215006db247cSraghuram vsw_save_lmacaddr(vsw_t *vswp, uint64_t macaddr)
21511ae08745Sheppo {
21521ae08745Sheppo 	int	i;
21531ae08745Sheppo 
215406db247cSraghuram 	WRITE_ENTER(&vswp->if_lockrw);
215506db247cSraghuram 	for (i = ETHERADDRL - 1; i >= 0; i--) {
215606db247cSraghuram 		vswp->if_addr.ether_addr_octet[i] = macaddr & 0xFF;
215706db247cSraghuram 		macaddr >>= 8;
21581ae08745Sheppo 	}
215906db247cSraghuram 	RW_EXIT(&vswp->if_lockrw);
21601ae08745Sheppo }
2161