xref: /titanic_53/usr/src/uts/sun4v/io/vsw.c (revision 7a327842c3ad1ec3b602c454b218df4ebbc716fb)
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 /*
23f0ca1d9aSsb155480  * Copyright 2008 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>
75c1c61f44Ssb155480 #include <sys/vlan.h>
761ae08745Sheppo 
771ae08745Sheppo /*
781ae08745Sheppo  * Function prototypes.
791ae08745Sheppo  */
801ae08745Sheppo static	int vsw_attach(dev_info_t *, ddi_attach_cmd_t);
811ae08745Sheppo static	int vsw_detach(dev_info_t *, ddi_detach_cmd_t);
821ae08745Sheppo static	int vsw_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
8334683adeSsg70180 static	int vsw_get_md_physname(vsw_t *, md_t *, mde_cookie_t, char *);
8434683adeSsg70180 static	int vsw_get_md_smodes(vsw_t *, md_t *, mde_cookie_t, uint8_t *, int *);
851ae08745Sheppo 
861ae08745Sheppo /* MDEG routines */
8734683adeSsg70180 static	int vsw_mdeg_register(vsw_t *vswp);
881ae08745Sheppo static	void vsw_mdeg_unregister(vsw_t *vswp);
891ae08745Sheppo static	int vsw_mdeg_cb(void *cb_argp, mdeg_result_t *);
9034683adeSsg70180 static	int vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *);
9119b65a69Ssb155480 static	int vsw_get_initial_md_properties(vsw_t *vswp, md_t *, mde_cookie_t);
92c1c61f44Ssb155480 static	int vsw_read_mdprops(vsw_t *vswp);
93c1c61f44Ssb155480 static	void vsw_vlan_read_ids(void *arg, int type, md_t *mdp,
94c1c61f44Ssb155480 	mde_cookie_t node, uint16_t *pvidp, uint16_t **vidspp,
95c1c61f44Ssb155480 	uint16_t *nvidsp, uint16_t *default_idp);
96c1c61f44Ssb155480 static	int vsw_port_read_props(vsw_port_t *portp, vsw_t *vswp,
97c1c61f44Ssb155480 	md_t *mdp, mde_cookie_t *node);
98f0ca1d9aSsb155480 static	void vsw_read_pri_eth_types(vsw_t *vswp, md_t *mdp,
99f0ca1d9aSsb155480 	mde_cookie_t node);
10034683adeSsg70180 static	void vsw_update_md_prop(vsw_t *, md_t *, mde_cookie_t);
10119b65a69Ssb155480 static void vsw_save_lmacaddr(vsw_t *vswp, uint64_t macaddr);
1021ae08745Sheppo 
10306db247cSraghuram /* Mac driver related routines */
10406db247cSraghuram static int vsw_mac_register(vsw_t *);
10506db247cSraghuram static int vsw_mac_unregister(vsw_t *);
10606db247cSraghuram static int vsw_m_stat(void *, uint_t, uint64_t *);
10706db247cSraghuram static void vsw_m_stop(void *arg);
10806db247cSraghuram static int vsw_m_start(void *arg);
10906db247cSraghuram static int vsw_m_unicst(void *arg, const uint8_t *);
11006db247cSraghuram static int vsw_m_multicst(void *arg, boolean_t, const uint8_t *);
11106db247cSraghuram static int vsw_m_promisc(void *arg, boolean_t);
11206db247cSraghuram static mblk_t *vsw_m_tx(void *arg, mblk_t *);
113f0ca1d9aSsb155480 void vsw_mac_rx(vsw_t *vswp, mac_resource_handle_t mrh,
114f0ca1d9aSsb155480     mblk_t *mp, vsw_macrx_flags_t flags);
1151ae08745Sheppo 
11606db247cSraghuram /*
11706db247cSraghuram  * Functions imported from other files.
11806db247cSraghuram  */
11906db247cSraghuram extern void vsw_setup_switching_timeout(void *arg);
12006db247cSraghuram extern void vsw_stop_switching_timeout(vsw_t *vswp);
12106db247cSraghuram extern int vsw_setup_switching(vsw_t *);
122*7a327842Swentaoy extern void vsw_switch_frame_nop(vsw_t *vswp, mblk_t *mp, int caller,
123*7a327842Swentaoy     vsw_port_t *port, mac_resource_handle_t mrh);
12406db247cSraghuram extern int vsw_add_mcst(vsw_t *, uint8_t, uint64_t, void *);
12506db247cSraghuram extern int vsw_del_mcst(vsw_t *, uint8_t, uint64_t, void *);
12606db247cSraghuram extern void vsw_del_mcst_vsw(vsw_t *);
12706db247cSraghuram extern mcst_addr_t *vsw_del_addr(uint8_t devtype, void *arg, uint64_t addr);
12806db247cSraghuram extern int vsw_detach_ports(vsw_t *vswp);
12906db247cSraghuram extern int vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node);
13006db247cSraghuram extern int vsw_port_detach(vsw_t *vswp, int p_instance);
131c1c61f44Ssb155480 static int vsw_port_update(vsw_t *vswp, md_t *curr_mdp, mde_cookie_t curr_mdex,
132c1c61f44Ssb155480 	md_t *prev_mdp, mde_cookie_t prev_mdex);
133c1c61f44Ssb155480 extern	int vsw_port_attach(vsw_port_t *port);
13406db247cSraghuram extern vsw_port_t *vsw_lookup_port(vsw_t *vswp, int p_instance);
13506db247cSraghuram extern int vsw_mac_attach(vsw_t *vswp);
13606db247cSraghuram extern void vsw_mac_detach(vsw_t *vswp);
13706db247cSraghuram extern int vsw_mac_open(vsw_t *vswp);
13806db247cSraghuram extern void vsw_mac_close(vsw_t *vswp);
13906db247cSraghuram extern int vsw_set_hw(vsw_t *, vsw_port_t *, int);
14006db247cSraghuram extern int vsw_unset_hw(vsw_t *, vsw_port_t *, int);
14106db247cSraghuram extern void vsw_reconfig_hw(vsw_t *);
14206db247cSraghuram extern void vsw_unset_addrs(vsw_t *vswp);
14306db247cSraghuram extern void vsw_set_addrs(vsw_t *vswp);
144c1c61f44Ssb155480 extern void vsw_create_vlans(void *arg, int type);
145c1c61f44Ssb155480 extern void vsw_destroy_vlans(void *arg, int type);
146c1c61f44Ssb155480 extern void vsw_vlan_add_ids(void *arg, int type);
147c1c61f44Ssb155480 extern void vsw_vlan_remove_ids(void *arg, int type);
148c1c61f44Ssb155480 extern void vsw_vlan_unaware_port_reset(vsw_port_t *portp);
149c1c61f44Ssb155480 extern uint32_t vsw_vlan_frame_untag(void *arg, int type, mblk_t **np,
150c1c61f44Ssb155480 	mblk_t **npt);
151c1c61f44Ssb155480 extern mblk_t *vsw_vlan_frame_pretag(void *arg, int type, mblk_t *mp);
152678453a8Sspeer extern void vsw_hio_cleanup(vsw_t *vswp);
153678453a8Sspeer extern void vsw_hio_start_ports(vsw_t *vswp);
154678453a8Sspeer void vsw_hio_port_update(vsw_port_t *portp, boolean_t hio_enabled);
15506db247cSraghuram 
15606db247cSraghuram /*
15706db247cSraghuram  * Internal tunables.
15806db247cSraghuram  */
159445b4c2eSsb155480 int	vsw_num_handshakes = VNET_NUM_HANDSHAKES; /* # of handshake attempts */
1601ae08745Sheppo int	vsw_wretries = 100;		/* # of write attempts */
161d10e4ef2Snarayan int	vsw_desc_delay = 0;		/* delay in us */
162d10e4ef2Snarayan int	vsw_read_attempts = 5;		/* # of reads of descriptor */
16319b65a69Ssb155480 int	vsw_mac_open_retries = 20;	/* max # of mac_open() retries */
16419b65a69Ssb155480 int	vsw_setup_switching_delay = 3;	/* setup sw timeout interval in sec */
16506db247cSraghuram int	vsw_ldc_tx_delay = 5;		/* delay(ticks) for tx retries */
16606db247cSraghuram int	vsw_ldc_tx_retries = 10;	/* # of ldc tx retries */
16706db247cSraghuram boolean_t vsw_ldc_rxthr_enabled = B_TRUE;	/* LDC Rx thread enabled */
16806db247cSraghuram boolean_t vsw_ldc_txthr_enabled = B_TRUE;	/* LDC Tx thread enabled */
169d10e4ef2Snarayan 
170c1c61f44Ssb155480 uint32_t	vsw_fdb_nchains = 8;	/* # of chains in fdb hash table */
171c1c61f44Ssb155480 uint32_t	vsw_vlan_nchains = 4;	/* # of chains in vlan id hash table */
172c1c61f44Ssb155480 uint32_t	vsw_ethermtu = 1500;	/* mtu of the device */
173c1c61f44Ssb155480 
174*7a327842Swentaoy /* sw timeout for boot delay only, in milliseconds */
175*7a327842Swentaoy int vsw_setup_switching_boot_delay = 100 * MILLISEC;
176*7a327842Swentaoy 
177c1c61f44Ssb155480 /* delay in usec to wait for all references on a fdb entry to be dropped */
178c1c61f44Ssb155480 uint32_t vsw_fdbe_refcnt_delay = 10;
179c1c61f44Ssb155480 
180c1c61f44Ssb155480 /*
181c1c61f44Ssb155480  * Default vlan id. This is only used internally when the "default-vlan-id"
182c1c61f44Ssb155480  * property is not present in the MD device node. Therefore, this should not be
183c1c61f44Ssb155480  * used as a tunable; if this value is changed, the corresponding variable
184c1c61f44Ssb155480  * should be updated to the same value in all vnets connected to this vsw.
185c1c61f44Ssb155480  */
186c1c61f44Ssb155480 uint16_t	vsw_default_vlan_id = 1;
187c1c61f44Ssb155480 
188f0ca1d9aSsb155480 /*
189f0ca1d9aSsb155480  * Workaround for a version handshake bug in obp's vnet.
190f0ca1d9aSsb155480  * If vsw initiates version negotiation starting from the highest version,
191f0ca1d9aSsb155480  * obp sends a nack and terminates version handshake. To workaround
192f0ca1d9aSsb155480  * this, we do not initiate version handshake when the channel comes up.
193f0ca1d9aSsb155480  * Instead, we wait for the peer to send its version info msg and go through
194f0ca1d9aSsb155480  * the version protocol exchange. If we successfully negotiate a version,
195f0ca1d9aSsb155480  * before sending the ack, we send our version info msg to the peer
196f0ca1d9aSsb155480  * using the <major,minor> version that we are about to ack.
197f0ca1d9aSsb155480  */
198f0ca1d9aSsb155480 boolean_t vsw_obp_ver_proto_workaround = B_TRUE;
199f0ca1d9aSsb155480 
200f0ca1d9aSsb155480 /*
201f0ca1d9aSsb155480  * In the absence of "priority-ether-types" property in MD, the following
202f0ca1d9aSsb155480  * internal tunable can be set to specify a single priority ethertype.
203f0ca1d9aSsb155480  */
204f0ca1d9aSsb155480 uint64_t vsw_pri_eth_type = 0;
205f0ca1d9aSsb155480 
206f0ca1d9aSsb155480 /*
207f0ca1d9aSsb155480  * Number of transmit priority buffers that are preallocated per device.
208f0ca1d9aSsb155480  * This number is chosen to be a small value to throttle transmission
209f0ca1d9aSsb155480  * of priority packets. Note: Must be a power of 2 for vio_create_mblks().
210f0ca1d9aSsb155480  */
211f0ca1d9aSsb155480 uint32_t vsw_pri_tx_nmblks = 64;
212d10e4ef2Snarayan 
213678453a8Sspeer boolean_t vsw_hio_enabled = B_TRUE;	/* Enable/disable HybridIO */
214678453a8Sspeer int vsw_hio_max_cleanup_retries = 10;	/* Max retries for HybridIO cleanp */
215678453a8Sspeer int vsw_hio_cleanup_delay = 10000;	/* 10ms */
216678453a8Sspeer 
21706db247cSraghuram /*
21806db247cSraghuram  * External tunables.
21906db247cSraghuram  */
22006db247cSraghuram /*
22106db247cSraghuram  * Enable/disable thread per ring. This is a mode selection
22206db247cSraghuram  * that is done a vsw driver attach time.
22306db247cSraghuram  */
22406db247cSraghuram boolean_t vsw_multi_ring_enable = B_FALSE;
22506db247cSraghuram int vsw_mac_rx_rings = VSW_MAC_RX_RINGS;
22606db247cSraghuram 
227f0ca1d9aSsb155480 /* Number of transmit descriptors -  must be power of 2 */
228f0ca1d9aSsb155480 uint32_t vsw_ntxds = VSW_RING_NUM_EL;
229f0ca1d9aSsb155480 
23006db247cSraghuram /*
23106db247cSraghuram  * Max number of mblks received in one receive operation.
23206db247cSraghuram  */
23306db247cSraghuram uint32_t vsw_chain_len = (VSW_NUM_MBLKS * 0.6);
23406db247cSraghuram 
23506db247cSraghuram /*
23606db247cSraghuram  * Tunables for three different pools, that is, the size and
23706db247cSraghuram  * number of mblks for each pool.
23806db247cSraghuram  */
23906db247cSraghuram uint32_t vsw_mblk_size1 = VSW_MBLK_SZ_128;	/* size=128 for pool1 */
24006db247cSraghuram uint32_t vsw_mblk_size2 = VSW_MBLK_SZ_256;	/* size=256 for pool2 */
24106db247cSraghuram uint32_t vsw_mblk_size3 = VSW_MBLK_SZ_2048;	/* size=2048 for pool3 */
24206db247cSraghuram uint32_t vsw_num_mblks1 = VSW_NUM_MBLKS;	/* number of mblks for pool1 */
24306db247cSraghuram uint32_t vsw_num_mblks2 = VSW_NUM_MBLKS;	/* number of mblks for pool2 */
24406db247cSraghuram uint32_t vsw_num_mblks3 = VSW_NUM_MBLKS;	/* number of mblks for pool3 */
24506db247cSraghuram 
24606db247cSraghuram /*
247f0ca1d9aSsb155480  * vsw_max_tx_qcount is the maximum # of packets that can be queued
248f0ca1d9aSsb155480  * before the tx worker thread begins processing the queue. Its value
249f0ca1d9aSsb155480  * is chosen to be 4x the default length of tx descriptor ring.
250f0ca1d9aSsb155480  */
251f0ca1d9aSsb155480 uint32_t vsw_max_tx_qcount = 4 * VSW_RING_NUM_EL;
252f0ca1d9aSsb155480 
253f0ca1d9aSsb155480 /*
25406db247cSraghuram  * MAC callbacks
25506db247cSraghuram  */
256ba2e4443Sseb static	mac_callbacks_t	vsw_m_callbacks = {
257ba2e4443Sseb 	0,
258ba2e4443Sseb 	vsw_m_stat,
259ba2e4443Sseb 	vsw_m_start,
260ba2e4443Sseb 	vsw_m_stop,
261ba2e4443Sseb 	vsw_m_promisc,
262ba2e4443Sseb 	vsw_m_multicst,
263ba2e4443Sseb 	vsw_m_unicst,
264ba2e4443Sseb 	vsw_m_tx,
265ba2e4443Sseb 	NULL,
266ba2e4443Sseb 	NULL,
267ba2e4443Sseb 	NULL
268ba2e4443Sseb };
269ba2e4443Sseb 
2701ae08745Sheppo static	struct	cb_ops	vsw_cb_ops = {
2711ae08745Sheppo 	nulldev,			/* cb_open */
2721ae08745Sheppo 	nulldev,			/* cb_close */
2731ae08745Sheppo 	nodev,				/* cb_strategy */
2741ae08745Sheppo 	nodev,				/* cb_print */
2751ae08745Sheppo 	nodev,				/* cb_dump */
2761ae08745Sheppo 	nodev,				/* cb_read */
2771ae08745Sheppo 	nodev,				/* cb_write */
2781ae08745Sheppo 	nodev,				/* cb_ioctl */
2791ae08745Sheppo 	nodev,				/* cb_devmap */
2801ae08745Sheppo 	nodev,				/* cb_mmap */
2811ae08745Sheppo 	nodev,				/* cb_segmap */
2821ae08745Sheppo 	nochpoll,			/* cb_chpoll */
2831ae08745Sheppo 	ddi_prop_op,			/* cb_prop_op */
2841ae08745Sheppo 	NULL,				/* cb_stream */
2851ae08745Sheppo 	D_MP,				/* cb_flag */
2861ae08745Sheppo 	CB_REV,				/* rev */
2871ae08745Sheppo 	nodev,				/* int (*cb_aread)() */
2881ae08745Sheppo 	nodev				/* int (*cb_awrite)() */
2891ae08745Sheppo };
2901ae08745Sheppo 
2911ae08745Sheppo static	struct	dev_ops	vsw_ops = {
2921ae08745Sheppo 	DEVO_REV,		/* devo_rev */
2931ae08745Sheppo 	0,			/* devo_refcnt */
2941ae08745Sheppo 	vsw_getinfo,		/* devo_getinfo */
2951ae08745Sheppo 	nulldev,		/* devo_identify */
2961ae08745Sheppo 	nulldev,		/* devo_probe */
2971ae08745Sheppo 	vsw_attach,		/* devo_attach */
2981ae08745Sheppo 	vsw_detach,		/* devo_detach */
2991ae08745Sheppo 	nodev,			/* devo_reset */
3001ae08745Sheppo 	&vsw_cb_ops,		/* devo_cb_ops */
3011ae08745Sheppo 	(struct bus_ops *)NULL,	/* devo_bus_ops */
3021ae08745Sheppo 	ddi_power		/* devo_power */
3031ae08745Sheppo };
3041ae08745Sheppo 
3051ae08745Sheppo extern	struct	mod_ops	mod_driverops;
3061ae08745Sheppo static struct modldrv vswmodldrv = {
3071ae08745Sheppo 	&mod_driverops,
308205eeb1aSlm66018 	"sun4v Virtual Switch",
3091ae08745Sheppo 	&vsw_ops,
3101ae08745Sheppo };
3111ae08745Sheppo 
3121ae08745Sheppo #define	LDC_ENTER_LOCK(ldcp)	\
3131ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_cblock));\
31406db247cSraghuram 				mutex_enter(&((ldcp)->ldc_rxlock));\
3151ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_txlock));
3161ae08745Sheppo #define	LDC_EXIT_LOCK(ldcp)	\
3171ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_txlock));\
31806db247cSraghuram 				mutex_exit(&((ldcp)->ldc_rxlock));\
3191ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_cblock));
3201ae08745Sheppo 
3211ae08745Sheppo /* Driver soft state ptr  */
3221ae08745Sheppo static void	*vsw_state;
3231ae08745Sheppo 
3241ae08745Sheppo /*
3251ae08745Sheppo  * Linked list of "vsw_t" structures - one per instance.
3261ae08745Sheppo  */
3271ae08745Sheppo vsw_t		*vsw_head = NULL;
3281ae08745Sheppo krwlock_t	vsw_rw;
3291ae08745Sheppo 
3301ae08745Sheppo /*
3311ae08745Sheppo  * Property names
3321ae08745Sheppo  */
3331ae08745Sheppo static char vdev_propname[] = "virtual-device";
3341ae08745Sheppo static char vsw_propname[] = "virtual-network-switch";
3351ae08745Sheppo static char physdev_propname[] = "vsw-phys-dev";
3361ae08745Sheppo static char smode_propname[] = "vsw-switch-mode";
3371ae08745Sheppo static char macaddr_propname[] = "local-mac-address";
3381ae08745Sheppo static char remaddr_propname[] = "remote-mac-address";
3391ae08745Sheppo static char ldcids_propname[] = "ldc-ids";
3401ae08745Sheppo static char chan_propname[] = "channel-endpoint";
3411ae08745Sheppo static char id_propname[] = "id";
3421ae08745Sheppo static char reg_propname[] = "reg";
343f0ca1d9aSsb155480 static char pri_types_propname[] = "priority-ether-types";
344c1c61f44Ssb155480 static char vsw_pvid_propname[] = "port-vlan-id";
345c1c61f44Ssb155480 static char vsw_vid_propname[] = "vlan-id";
346c1c61f44Ssb155480 static char vsw_dvid_propname[] = "default-vlan-id";
347c1c61f44Ssb155480 static char port_pvid_propname[] = "remote-port-vlan-id";
348c1c61f44Ssb155480 static char port_vid_propname[] = "remote-vlan-id";
349678453a8Sspeer static char hybrid_propname[] = "hybrid";
3501ae08745Sheppo 
3511ae08745Sheppo /*
3521ae08745Sheppo  * Matching criteria passed to the MDEG to register interest
3531ae08745Sheppo  * in changes to 'virtual-device-port' nodes identified by their
3541ae08745Sheppo  * 'id' property.
3551ae08745Sheppo  */
3561ae08745Sheppo static md_prop_match_t vport_prop_match[] = {
3571ae08745Sheppo 	{ MDET_PROP_VAL,    "id"   },
3581ae08745Sheppo 	{ MDET_LIST_END,    NULL    }
3591ae08745Sheppo };
3601ae08745Sheppo 
3611ae08745Sheppo static mdeg_node_match_t vport_match = { "virtual-device-port",
3621ae08745Sheppo 						vport_prop_match };
3631ae08745Sheppo 
3641ae08745Sheppo /*
36534683adeSsg70180  * Matching criteria passed to the MDEG to register interest
36634683adeSsg70180  * in changes to 'virtual-device' nodes (i.e. vsw nodes) identified
36734683adeSsg70180  * by their 'name' and 'cfg-handle' properties.
36834683adeSsg70180  */
36934683adeSsg70180 static md_prop_match_t vdev_prop_match[] = {
37034683adeSsg70180 	{ MDET_PROP_STR,    "name"   },
37134683adeSsg70180 	{ MDET_PROP_VAL,    "cfg-handle" },
37234683adeSsg70180 	{ MDET_LIST_END,    NULL    }
37334683adeSsg70180 };
37434683adeSsg70180 
37534683adeSsg70180 static mdeg_node_match_t vdev_match = { "virtual-device",
37634683adeSsg70180 						vdev_prop_match };
37734683adeSsg70180 
37834683adeSsg70180 
37934683adeSsg70180 /*
3801ae08745Sheppo  * Specification of an MD node passed to the MDEG to filter any
3811ae08745Sheppo  * 'vport' nodes that do not belong to the specified node. This
3821ae08745Sheppo  * template is copied for each vsw instance and filled in with
3831ae08745Sheppo  * the appropriate 'cfg-handle' value before being passed to the MDEG.
3841ae08745Sheppo  */
3851ae08745Sheppo static mdeg_prop_spec_t vsw_prop_template[] = {
3861ae08745Sheppo 	{ MDET_PROP_STR,    "name",		vsw_propname },
3871ae08745Sheppo 	{ MDET_PROP_VAL,    "cfg-handle",	NULL	},
3881ae08745Sheppo 	{ MDET_LIST_END,    NULL,		NULL	}
3891ae08745Sheppo };
3901ae08745Sheppo 
3911ae08745Sheppo #define	VSW_SET_MDEG_PROP_INST(specp, val)	(specp)[1].ps_val = (val);
3921ae08745Sheppo 
39306db247cSraghuram #ifdef	DEBUG
3947636cb21Slm66018 /*
3951ae08745Sheppo  * Print debug messages - set to 0x1f to enable all msgs
3961ae08745Sheppo  * or 0x0 to turn all off.
3971ae08745Sheppo  */
3981ae08745Sheppo int vswdbg = 0x0;
3991ae08745Sheppo 
4001ae08745Sheppo /*
4011ae08745Sheppo  * debug levels:
4021ae08745Sheppo  * 0x01:	Function entry/exit tracing
4031ae08745Sheppo  * 0x02:	Internal function messages
4041ae08745Sheppo  * 0x04:	Verbose internal messages
4051ae08745Sheppo  * 0x08:	Warning messages
4061ae08745Sheppo  * 0x10:	Error messages
4071ae08745Sheppo  */
4081ae08745Sheppo 
40906db247cSraghuram void
4101ae08745Sheppo vswdebug(vsw_t *vswp, const char *fmt, ...)
4111ae08745Sheppo {
4121ae08745Sheppo 	char buf[512];
4131ae08745Sheppo 	va_list ap;
4141ae08745Sheppo 
4151ae08745Sheppo 	va_start(ap, fmt);
4161ae08745Sheppo 	(void) vsprintf(buf, fmt, ap);
4171ae08745Sheppo 	va_end(ap);
4181ae08745Sheppo 
4191ae08745Sheppo 	if (vswp == NULL)
4201ae08745Sheppo 		cmn_err(CE_CONT, "%s\n", buf);
4211ae08745Sheppo 	else
4221ae08745Sheppo 		cmn_err(CE_CONT, "vsw%d: %s\n", vswp->instance, buf);
4231ae08745Sheppo }
4241ae08745Sheppo 
4251ae08745Sheppo #endif	/* DEBUG */
4261ae08745Sheppo 
4271ae08745Sheppo static struct modlinkage modlinkage = {
4281ae08745Sheppo 	MODREV_1,
4291ae08745Sheppo 	&vswmodldrv,
4301ae08745Sheppo 	NULL
4311ae08745Sheppo };
4321ae08745Sheppo 
4331ae08745Sheppo int
4341ae08745Sheppo _init(void)
4351ae08745Sheppo {
4361ae08745Sheppo 	int status;
4371ae08745Sheppo 
4381ae08745Sheppo 	rw_init(&vsw_rw, NULL, RW_DRIVER, NULL);
4391ae08745Sheppo 
4401ae08745Sheppo 	status = ddi_soft_state_init(&vsw_state, sizeof (vsw_t), 1);
4411ae08745Sheppo 	if (status != 0) {
4421ae08745Sheppo 		return (status);
4431ae08745Sheppo 	}
4441ae08745Sheppo 
44506db247cSraghuram 	mac_init_ops(&vsw_ops, DRV_NAME);
4461ae08745Sheppo 	status = mod_install(&modlinkage);
4471ae08745Sheppo 	if (status != 0) {
4481ae08745Sheppo 		ddi_soft_state_fini(&vsw_state);
4491ae08745Sheppo 	}
4501ae08745Sheppo 	return (status);
4511ae08745Sheppo }
4521ae08745Sheppo 
4531ae08745Sheppo int
4541ae08745Sheppo _fini(void)
4551ae08745Sheppo {
4561ae08745Sheppo 	int status;
4571ae08745Sheppo 
4581ae08745Sheppo 	status = mod_remove(&modlinkage);
4591ae08745Sheppo 	if (status != 0)
4601ae08745Sheppo 		return (status);
4611ae08745Sheppo 	mac_fini_ops(&vsw_ops);
4621ae08745Sheppo 	ddi_soft_state_fini(&vsw_state);
4631ae08745Sheppo 
4641ae08745Sheppo 	rw_destroy(&vsw_rw);
4651ae08745Sheppo 
4661ae08745Sheppo 	return (status);
4671ae08745Sheppo }
4681ae08745Sheppo 
4691ae08745Sheppo int
4701ae08745Sheppo _info(struct modinfo *modinfop)
4711ae08745Sheppo {
4721ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
4731ae08745Sheppo }
4741ae08745Sheppo 
4751ae08745Sheppo static int
4761ae08745Sheppo vsw_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4771ae08745Sheppo {
4781ae08745Sheppo 	vsw_t		*vswp;
47934683adeSsg70180 	int		instance;
4801ae08745Sheppo 	char		hashname[MAXNAMELEN];
4811ae08745Sheppo 	char		qname[TASKQ_NAMELEN];
4827636cb21Slm66018 	enum		{ PROG_init = 0x00,
48319b65a69Ssb155480 				PROG_locks = 0x01,
48419b65a69Ssb155480 				PROG_readmd = 0x02,
48519b65a69Ssb155480 				PROG_fdb = 0x04,
48619b65a69Ssb155480 				PROG_mfdb = 0x08,
48719b65a69Ssb155480 				PROG_taskq = 0x10,
488f0ca1d9aSsb155480 				PROG_swmode = 0x20,
489f0ca1d9aSsb155480 				PROG_macreg = 0x40,
490f0ca1d9aSsb155480 				PROG_mdreg = 0x80}
4911ae08745Sheppo 			progress;
4921ae08745Sheppo 
4931ae08745Sheppo 	progress = PROG_init;
49419b65a69Ssb155480 	int		rv;
4951ae08745Sheppo 
4961ae08745Sheppo 	switch (cmd) {
4971ae08745Sheppo 	case DDI_ATTACH:
4981ae08745Sheppo 		break;
4991ae08745Sheppo 	case DDI_RESUME:
5001ae08745Sheppo 		/* nothing to do for this non-device */
5011ae08745Sheppo 		return (DDI_SUCCESS);
5021ae08745Sheppo 	case DDI_PM_RESUME:
5031ae08745Sheppo 	default:
5041ae08745Sheppo 		return (DDI_FAILURE);
5051ae08745Sheppo 	}
5061ae08745Sheppo 
5071ae08745Sheppo 	instance = ddi_get_instance(dip);
5081ae08745Sheppo 	if (ddi_soft_state_zalloc(vsw_state, instance) != DDI_SUCCESS) {
5091ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_soft_state_zalloc failed", instance);
5101ae08745Sheppo 		return (DDI_FAILURE);
5111ae08745Sheppo 	}
5121ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
5131ae08745Sheppo 
5141ae08745Sheppo 	if (vswp == NULL) {
5151ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_get_soft_state failed", instance);
5161ae08745Sheppo 		goto vsw_attach_fail;
5171ae08745Sheppo 	}
5181ae08745Sheppo 
5191ae08745Sheppo 	vswp->dip = dip;
5201ae08745Sheppo 	vswp->instance = instance;
5211ae08745Sheppo 	ddi_set_driver_private(dip, (caddr_t)vswp);
5221ae08745Sheppo 
5235f94e909Ssg70180 	mutex_init(&vswp->hw_lock, NULL, MUTEX_DRIVER, NULL);
52434683adeSsg70180 	mutex_init(&vswp->mac_lock, NULL, MUTEX_DRIVER, NULL);
52519b65a69Ssb155480 	mutex_init(&vswp->mca_lock, NULL, MUTEX_DRIVER, NULL);
52619b65a69Ssb155480 	mutex_init(&vswp->swtmout_lock, NULL, MUTEX_DRIVER, NULL);
5271ae08745Sheppo 	rw_init(&vswp->if_lockrw, NULL, RW_DRIVER, NULL);
52819b65a69Ssb155480 	rw_init(&vswp->mfdbrw, NULL, RW_DRIVER, NULL);
52919b65a69Ssb155480 	rw_init(&vswp->plist.lockrw, NULL, RW_DRIVER, NULL);
53019b65a69Ssb155480 
53119b65a69Ssb155480 	progress |= PROG_locks;
53219b65a69Ssb155480 
53319b65a69Ssb155480 	rv = vsw_read_mdprops(vswp);
53419b65a69Ssb155480 	if (rv != 0)
53519b65a69Ssb155480 		goto vsw_attach_fail;
53619b65a69Ssb155480 
53719b65a69Ssb155480 	progress |= PROG_readmd;
5381ae08745Sheppo 
5391ae08745Sheppo 	/* setup the unicast forwarding database  */
5401ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_unicst_table-%d",
5411ae08745Sheppo 	    vswp->instance);
5421ae08745Sheppo 	D2(vswp, "creating unicast hash table (%s)...", hashname);
543c1c61f44Ssb155480 	vswp->fdb_nchains = vsw_fdb_nchains;
544c1c61f44Ssb155480 	vswp->fdb_hashp = mod_hash_create_ptrhash(hashname, vswp->fdb_nchains,
5451ae08745Sheppo 	    mod_hash_null_valdtor, sizeof (void *));
546c1c61f44Ssb155480 	vsw_create_vlans((void *)vswp, VSW_LOCALDEV);
5471ae08745Sheppo 	progress |= PROG_fdb;
5481ae08745Sheppo 
5491ae08745Sheppo 	/* setup the multicast fowarding database */
5501ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_mcst_table-%d",
5511ae08745Sheppo 	    vswp->instance);
5521ae08745Sheppo 	D2(vswp, "creating multicast hash table %s)...", hashname);
553c1c61f44Ssb155480 	vswp->mfdb = mod_hash_create_ptrhash(hashname, vsw_fdb_nchains,
5541ae08745Sheppo 	    mod_hash_null_valdtor, sizeof (void *));
5551ae08745Sheppo 
5561ae08745Sheppo 	progress |= PROG_mfdb;
5571ae08745Sheppo 
5581ae08745Sheppo 	/*
5591ae08745Sheppo 	 * Create the taskq which will process all the VIO
5601ae08745Sheppo 	 * control messages.
5611ae08745Sheppo 	 */
5621ae08745Sheppo 	(void) snprintf(qname, TASKQ_NAMELEN, "vsw_taskq%d", vswp->instance);
5631ae08745Sheppo 	if ((vswp->taskq_p = ddi_taskq_create(vswp->dip, qname, 1,
5641ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
56534683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to create task queue",
56634683adeSsg70180 		    vswp->instance);
5671ae08745Sheppo 		goto vsw_attach_fail;
5681ae08745Sheppo 	}
5691ae08745Sheppo 
5701ae08745Sheppo 	progress |= PROG_taskq;
5711ae08745Sheppo 
572d10e4ef2Snarayan 	/* prevent auto-detaching */
573d10e4ef2Snarayan 	if (ddi_prop_update_int(DDI_DEV_T_NONE, vswp->dip,
574d10e4ef2Snarayan 	    DDI_NO_AUTODETACH, 1) != DDI_SUCCESS) {
57534683adeSsg70180 		cmn_err(CE_NOTE, "!Unable to set \"%s\" property for "
576d10e4ef2Snarayan 		    "instance %u", DDI_NO_AUTODETACH, instance);
577d10e4ef2Snarayan 	}
578d10e4ef2Snarayan 
5791ae08745Sheppo 	/*
580*7a327842Swentaoy 	 * The null switching function is set to avoid panic until
581*7a327842Swentaoy 	 * switch mode is setup.
582*7a327842Swentaoy 	 */
583*7a327842Swentaoy 	vswp->vsw_switch_frame = vsw_switch_frame_nop;
584*7a327842Swentaoy 
585*7a327842Swentaoy 	/*
58619b65a69Ssb155480 	 * Setup the required switching mode,
58719b65a69Ssb155480 	 * based on the mdprops that we read earlier.
588*7a327842Swentaoy 	 * schedule a short timeout (0.1 sec) for the first time
589*7a327842Swentaoy 	 * setup and avoid calling mac_open() directly here,
590*7a327842Swentaoy 	 * others are regular timeout 3 secs.
59119b65a69Ssb155480 	 */
59219b65a69Ssb155480 	mutex_enter(&vswp->swtmout_lock);
59319b65a69Ssb155480 
59419b65a69Ssb155480 	vswp->swtmout_enabled = B_TRUE;
595*7a327842Swentaoy 	vswp->swtmout_id = timeout(vsw_setup_switching_timeout, vswp,
596*7a327842Swentaoy 	    drv_usectohz(vsw_setup_switching_boot_delay));
59719b65a69Ssb155480 
59819b65a69Ssb155480 	mutex_exit(&vswp->swtmout_lock);
59919b65a69Ssb155480 
60019b65a69Ssb155480 	progress |= PROG_swmode;
60119b65a69Ssb155480 
60219b65a69Ssb155480 	/* Register with mac layer as a provider */
60319b65a69Ssb155480 	rv = vsw_mac_register(vswp);
60419b65a69Ssb155480 	if (rv != 0)
60519b65a69Ssb155480 		goto vsw_attach_fail;
60619b65a69Ssb155480 
60719b65a69Ssb155480 	progress |= PROG_macreg;
60819b65a69Ssb155480 
60919b65a69Ssb155480 	/*
61034683adeSsg70180 	 * Now we have everything setup, register an interest in
61134683adeSsg70180 	 * specific MD nodes.
61234683adeSsg70180 	 *
61334683adeSsg70180 	 * The callback is invoked in 2 cases, firstly if upon mdeg
61434683adeSsg70180 	 * registration there are existing nodes which match our specified
61534683adeSsg70180 	 * criteria, and secondly if the MD is changed (and again, there
61634683adeSsg70180 	 * are nodes which we are interested in present within it. Note
61734683adeSsg70180 	 * that our callback will be invoked even if our specified nodes
61834683adeSsg70180 	 * have not actually changed).
61934683adeSsg70180 	 *
6201ae08745Sheppo 	 */
62119b65a69Ssb155480 	rv = vsw_mdeg_register(vswp);
62219b65a69Ssb155480 	if (rv != 0)
62334683adeSsg70180 		goto vsw_attach_fail;
6241ae08745Sheppo 
62519b65a69Ssb155480 	progress |= PROG_mdreg;
62619b65a69Ssb155480 
62719b65a69Ssb155480 	WRITE_ENTER(&vsw_rw);
62819b65a69Ssb155480 	vswp->next = vsw_head;
62919b65a69Ssb155480 	vsw_head = vswp;
63019b65a69Ssb155480 	RW_EXIT(&vsw_rw);
63119b65a69Ssb155480 
63219b65a69Ssb155480 	ddi_report_dev(vswp->dip);
6331ae08745Sheppo 	return (DDI_SUCCESS);
6341ae08745Sheppo 
6351ae08745Sheppo vsw_attach_fail:
6361ae08745Sheppo 	DERR(NULL, "vsw_attach: failed");
6371ae08745Sheppo 
63819b65a69Ssb155480 	if (progress & PROG_mdreg) {
63919b65a69Ssb155480 		vsw_mdeg_unregister(vswp);
64019b65a69Ssb155480 		(void) vsw_detach_ports(vswp);
64119b65a69Ssb155480 	}
64219b65a69Ssb155480 
64319b65a69Ssb155480 	if (progress & PROG_macreg)
64419b65a69Ssb155480 		(void) vsw_mac_unregister(vswp);
64519b65a69Ssb155480 
64619b65a69Ssb155480 	if (progress & PROG_swmode) {
64719b65a69Ssb155480 		vsw_stop_switching_timeout(vswp);
648678453a8Sspeer 		vsw_hio_cleanup(vswp);
64919b65a69Ssb155480 		mutex_enter(&vswp->mac_lock);
65019b65a69Ssb155480 		vsw_mac_detach(vswp);
65119b65a69Ssb155480 		vsw_mac_close(vswp);
65219b65a69Ssb155480 		mutex_exit(&vswp->mac_lock);
65319b65a69Ssb155480 	}
65419b65a69Ssb155480 
6551ae08745Sheppo 	if (progress & PROG_taskq)
6561ae08745Sheppo 		ddi_taskq_destroy(vswp->taskq_p);
6571ae08745Sheppo 
65819b65a69Ssb155480 	if (progress & PROG_mfdb)
6591ae08745Sheppo 		mod_hash_destroy_hash(vswp->mfdb);
6601ae08745Sheppo 
661c1c61f44Ssb155480 	if (progress & PROG_fdb) {
662c1c61f44Ssb155480 		vsw_destroy_vlans(vswp, VSW_LOCALDEV);
663c1c61f44Ssb155480 		mod_hash_destroy_hash(vswp->fdb_hashp);
664c1c61f44Ssb155480 	}
6651ae08745Sheppo 
666f0ca1d9aSsb155480 	if (progress & PROG_readmd) {
667f0ca1d9aSsb155480 		if (VSW_PRI_ETH_DEFINED(vswp)) {
668f0ca1d9aSsb155480 			kmem_free(vswp->pri_types,
669f0ca1d9aSsb155480 			    sizeof (uint16_t) * vswp->pri_num_types);
670f0ca1d9aSsb155480 		}
671f0ca1d9aSsb155480 		(void) vio_destroy_mblks(vswp->pri_tx_vmp);
672f0ca1d9aSsb155480 	}
673f0ca1d9aSsb155480 
67419b65a69Ssb155480 	if (progress & PROG_locks) {
67519b65a69Ssb155480 		rw_destroy(&vswp->plist.lockrw);
67619b65a69Ssb155480 		rw_destroy(&vswp->mfdbrw);
6771ae08745Sheppo 		rw_destroy(&vswp->if_lockrw);
67819b65a69Ssb155480 		mutex_destroy(&vswp->swtmout_lock);
67919b65a69Ssb155480 		mutex_destroy(&vswp->mca_lock);
68034683adeSsg70180 		mutex_destroy(&vswp->mac_lock);
6815f94e909Ssg70180 		mutex_destroy(&vswp->hw_lock);
68234683adeSsg70180 	}
6831ae08745Sheppo 
6841ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
6851ae08745Sheppo 	return (DDI_FAILURE);
6861ae08745Sheppo }
6871ae08745Sheppo 
6881ae08745Sheppo static int
6891ae08745Sheppo vsw_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6901ae08745Sheppo {
691d10e4ef2Snarayan 	vio_mblk_pool_t		*poolp, *npoolp;
6921ae08745Sheppo 	vsw_t			**vswpp, *vswp;
6931ae08745Sheppo 	int 			instance;
6941ae08745Sheppo 
6951ae08745Sheppo 	instance = ddi_get_instance(dip);
6961ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
6971ae08745Sheppo 
6981ae08745Sheppo 	if (vswp == NULL) {
6991ae08745Sheppo 		return (DDI_FAILURE);
7001ae08745Sheppo 	}
7011ae08745Sheppo 
7021ae08745Sheppo 	switch (cmd) {
7031ae08745Sheppo 	case DDI_DETACH:
7041ae08745Sheppo 		break;
7051ae08745Sheppo 	case DDI_SUSPEND:
7061ae08745Sheppo 	case DDI_PM_SUSPEND:
7071ae08745Sheppo 	default:
7081ae08745Sheppo 		return (DDI_FAILURE);
7091ae08745Sheppo 	}
7101ae08745Sheppo 
7111ae08745Sheppo 	D2(vswp, "detaching instance %d", instance);
7121ae08745Sheppo 
71319b65a69Ssb155480 	/* Stop any pending timeout to setup switching mode. */
71419b65a69Ssb155480 	vsw_stop_switching_timeout(vswp);
71519b65a69Ssb155480 
71634683adeSsg70180 	if (vswp->if_state & VSW_IF_REG) {
7171ae08745Sheppo 		if (vsw_mac_unregister(vswp) != 0) {
71834683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to detach from "
71934683adeSsg70180 			    "MAC layer", vswp->instance);
7201ae08745Sheppo 			return (DDI_FAILURE);
7211ae08745Sheppo 		}
722d10e4ef2Snarayan 	}
7231ae08745Sheppo 
7241ae08745Sheppo 	vsw_mdeg_unregister(vswp);
7251ae08745Sheppo 
726e1ebb9ecSlm66018 	/* remove mac layer callback */
72734683adeSsg70180 	mutex_enter(&vswp->mac_lock);
728e1ebb9ecSlm66018 	if ((vswp->mh != NULL) && (vswp->mrh != NULL)) {
7291f8aaf0dSethindra 		mac_rx_remove(vswp->mh, vswp->mrh, B_TRUE);
730e1ebb9ecSlm66018 		vswp->mrh = NULL;
7311ae08745Sheppo 	}
73234683adeSsg70180 	mutex_exit(&vswp->mac_lock);
7331ae08745Sheppo 
7341ae08745Sheppo 	if (vsw_detach_ports(vswp) != 0) {
7351ef0bbb5Snarayan 		cmn_err(CE_WARN, "!vsw%d: Unable to unconfigure ports",
73634683adeSsg70180 		    vswp->instance);
7371ae08745Sheppo 		return (DDI_FAILURE);
7381ae08745Sheppo 	}
7391ae08745Sheppo 
74034683adeSsg70180 	rw_destroy(&vswp->if_lockrw);
74134683adeSsg70180 
742678453a8Sspeer 	/* cleanup HybridIO */
743678453a8Sspeer 	vsw_hio_cleanup(vswp);
744678453a8Sspeer 
7455f94e909Ssg70180 	mutex_destroy(&vswp->hw_lock);
7465f94e909Ssg70180 
7471ae08745Sheppo 	/*
748e1ebb9ecSlm66018 	 * Now that the ports have been deleted, stop and close
749e1ebb9ecSlm66018 	 * the physical device.
750e1ebb9ecSlm66018 	 */
75134683adeSsg70180 	mutex_enter(&vswp->mac_lock);
752e1ebb9ecSlm66018 
75319b65a69Ssb155480 	vsw_mac_detach(vswp);
75419b65a69Ssb155480 	vsw_mac_close(vswp);
75519b65a69Ssb155480 
75634683adeSsg70180 	mutex_exit(&vswp->mac_lock);
75719b65a69Ssb155480 
75834683adeSsg70180 	mutex_destroy(&vswp->mac_lock);
75919b65a69Ssb155480 	mutex_destroy(&vswp->swtmout_lock);
760e1ebb9ecSlm66018 
761e1ebb9ecSlm66018 	/*
762d10e4ef2Snarayan 	 * Destroy any free pools that may still exist.
763d10e4ef2Snarayan 	 */
764d10e4ef2Snarayan 	poolp = vswp->rxh;
765d10e4ef2Snarayan 	while (poolp != NULL) {
766d10e4ef2Snarayan 		npoolp = vswp->rxh = poolp->nextp;
767d10e4ef2Snarayan 		if (vio_destroy_mblks(poolp) != 0) {
768d10e4ef2Snarayan 			vswp->rxh = poolp;
769d10e4ef2Snarayan 			return (DDI_FAILURE);
770d10e4ef2Snarayan 		}
771d10e4ef2Snarayan 		poolp = npoolp;
772d10e4ef2Snarayan 	}
773d10e4ef2Snarayan 
774d10e4ef2Snarayan 	/*
7751ae08745Sheppo 	 * Remove this instance from any entries it may be on in
7761ae08745Sheppo 	 * the hash table by using the list of addresses maintained
7771ae08745Sheppo 	 * in the vsw_t structure.
7781ae08745Sheppo 	 */
7791ae08745Sheppo 	vsw_del_mcst_vsw(vswp);
7801ae08745Sheppo 
7811ae08745Sheppo 	vswp->mcap = NULL;
7821ae08745Sheppo 	mutex_destroy(&vswp->mca_lock);
7831ae08745Sheppo 
7841ae08745Sheppo 	/*
7851ae08745Sheppo 	 * By now any pending tasks have finished and the underlying
7861ae08745Sheppo 	 * ldc's have been destroyed, so its safe to delete the control
7871ae08745Sheppo 	 * message taskq.
7881ae08745Sheppo 	 */
7891ae08745Sheppo 	if (vswp->taskq_p != NULL)
7901ae08745Sheppo 		ddi_taskq_destroy(vswp->taskq_p);
7911ae08745Sheppo 
7921ae08745Sheppo 	/*
7931ae08745Sheppo 	 * At this stage all the data pointers in the hash table
7941ae08745Sheppo 	 * should be NULL, as all the ports have been removed and will
7951ae08745Sheppo 	 * have deleted themselves from the port lists which the data
7961ae08745Sheppo 	 * pointers point to. Hence we can destroy the table using the
7971ae08745Sheppo 	 * default destructors.
7981ae08745Sheppo 	 */
7991ae08745Sheppo 	D2(vswp, "vsw_detach: destroying hash tables..");
800c1c61f44Ssb155480 	vsw_destroy_vlans(vswp, VSW_LOCALDEV);
801c1c61f44Ssb155480 	mod_hash_destroy_hash(vswp->fdb_hashp);
802c1c61f44Ssb155480 	vswp->fdb_hashp = NULL;
8031ae08745Sheppo 
8041ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
8051ae08745Sheppo 	mod_hash_destroy_hash(vswp->mfdb);
8061ae08745Sheppo 	vswp->mfdb = NULL;
8071ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
8081ae08745Sheppo 	rw_destroy(&vswp->mfdbrw);
8091ae08745Sheppo 
810f0ca1d9aSsb155480 	/* free pri_types table */
811f0ca1d9aSsb155480 	if (VSW_PRI_ETH_DEFINED(vswp)) {
812f0ca1d9aSsb155480 		kmem_free(vswp->pri_types,
813f0ca1d9aSsb155480 		    sizeof (uint16_t) * vswp->pri_num_types);
814f0ca1d9aSsb155480 		(void) vio_destroy_mblks(vswp->pri_tx_vmp);
815f0ca1d9aSsb155480 	}
816f0ca1d9aSsb155480 
8171ae08745Sheppo 	ddi_remove_minor_node(dip, NULL);
8181ae08745Sheppo 
8191ae08745Sheppo 	rw_destroy(&vswp->plist.lockrw);
8201ae08745Sheppo 	WRITE_ENTER(&vsw_rw);
8211ae08745Sheppo 	for (vswpp = &vsw_head; *vswpp; vswpp = &(*vswpp)->next) {
8221ae08745Sheppo 		if (*vswpp == vswp) {
8231ae08745Sheppo 			*vswpp = vswp->next;
8241ae08745Sheppo 			break;
8251ae08745Sheppo 		}
8261ae08745Sheppo 	}
8271ae08745Sheppo 	RW_EXIT(&vsw_rw);
8281ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
8291ae08745Sheppo 
8301ae08745Sheppo 	return (DDI_SUCCESS);
8311ae08745Sheppo }
8321ae08745Sheppo 
8331ae08745Sheppo static int
8341ae08745Sheppo vsw_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
8351ae08745Sheppo {
8361ae08745Sheppo 	_NOTE(ARGUNUSED(dip))
8371ae08745Sheppo 
8381ae08745Sheppo 	vsw_t	*vswp = NULL;
8391ae08745Sheppo 	dev_t	dev = (dev_t)arg;
8401ae08745Sheppo 	int	instance;
8411ae08745Sheppo 
8421ae08745Sheppo 	instance = getminor(dev);
8431ae08745Sheppo 
8441ae08745Sheppo 	switch (infocmd) {
8451ae08745Sheppo 	case DDI_INFO_DEVT2DEVINFO:
8461ae08745Sheppo 		if ((vswp = ddi_get_soft_state(vsw_state, instance)) == NULL) {
8471ae08745Sheppo 			*result = NULL;
8481ae08745Sheppo 			return (DDI_FAILURE);
8491ae08745Sheppo 		}
8501ae08745Sheppo 		*result = vswp->dip;
8511ae08745Sheppo 		return (DDI_SUCCESS);
8521ae08745Sheppo 
8531ae08745Sheppo 	case DDI_INFO_DEVT2INSTANCE:
8541ae08745Sheppo 		*result = (void *)(uintptr_t)instance;
8551ae08745Sheppo 		return (DDI_SUCCESS);
8561ae08745Sheppo 
8571ae08745Sheppo 	default:
8581ae08745Sheppo 		*result = NULL;
8591ae08745Sheppo 		return (DDI_FAILURE);
8601ae08745Sheppo 	}
8611ae08745Sheppo }
8621ae08745Sheppo 
8631ae08745Sheppo /*
86434683adeSsg70180  * Get the value of the "vsw-phys-dev" property in the specified
86534683adeSsg70180  * node. This property is the name of the physical device that
86634683adeSsg70180  * the virtual switch will use to talk to the outside world.
86734683adeSsg70180  *
86834683adeSsg70180  * Note it is valid for this property to be NULL (but the property
86934683adeSsg70180  * itself must exist). Callers of this routine should verify that
87034683adeSsg70180  * the value returned is what they expected (i.e. either NULL or non NULL).
87134683adeSsg70180  *
87234683adeSsg70180  * On success returns value of the property in region pointed to by
87334683adeSsg70180  * the 'name' argument, and with return value of 0. Otherwise returns 1.
8741ae08745Sheppo  */
87534683adeSsg70180 static int
87634683adeSsg70180 vsw_get_md_physname(vsw_t *vswp, md_t *mdp, mde_cookie_t node, char *name)
8771ae08745Sheppo {
87834683adeSsg70180 	int		len = 0;
879f2b610cfSwentaoy 	int		instance;
8801ae08745Sheppo 	char		*physname = NULL;
8811ae08745Sheppo 	char		*dev;
882f2b610cfSwentaoy 	const char	*dev_name;
883f2b610cfSwentaoy 	char		myname[MAXNAMELEN];
884f2b610cfSwentaoy 
885f2b610cfSwentaoy 	dev_name = ddi_driver_name(vswp->dip);
886f2b610cfSwentaoy 	instance = ddi_get_instance(vswp->dip);
887f2b610cfSwentaoy 	(void) snprintf(myname, MAXNAMELEN, "%s%d", dev_name, instance);
8881ae08745Sheppo 
88934683adeSsg70180 	if (md_get_prop_data(mdp, node, physdev_propname,
8901ae08745Sheppo 	    (uint8_t **)(&physname), &len) != 0) {
89134683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get name(s) of physical "
89234683adeSsg70180 		    "device(s) from MD", vswp->instance);
89334683adeSsg70180 		return (1);
8941ae08745Sheppo 	} else if ((strlen(physname) + 1) > LIFNAMSIZ) {
89534683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: %s is too long a device name",
89634683adeSsg70180 		    vswp->instance, physname);
89734683adeSsg70180 		return (1);
898f2b610cfSwentaoy 	} else if (strcmp(myname, physname) == 0) {
899f2b610cfSwentaoy 		/*
900f2b610cfSwentaoy 		 * Prevent the vswitch from opening itself as the
901f2b610cfSwentaoy 		 * network device.
902f2b610cfSwentaoy 		 */
903f2b610cfSwentaoy 		cmn_err(CE_WARN, "!vsw%d: %s is an invalid device name",
904f2b610cfSwentaoy 		    vswp->instance, physname);
905f2b610cfSwentaoy 		return (1);
9061ae08745Sheppo 	} else {
90734683adeSsg70180 		(void) strncpy(name, physname, strlen(physname) + 1);
9081ae08745Sheppo 		D2(vswp, "%s: using first device specified (%s)",
90934683adeSsg70180 		    __func__, physname);
9101ae08745Sheppo 	}
9111ae08745Sheppo 
9121ae08745Sheppo #ifdef DEBUG
9131ae08745Sheppo 	/*
9141ae08745Sheppo 	 * As a temporary measure to aid testing we check to see if there
9151ae08745Sheppo 	 * is a vsw.conf file present. If there is we use the value of the
9161ae08745Sheppo 	 * vsw_physname property in the file as the name of the physical
9171ae08745Sheppo 	 * device, overriding the value from the MD.
9181ae08745Sheppo 	 *
9191ae08745Sheppo 	 * There may be multiple devices listed, but for the moment
9201ae08745Sheppo 	 * we just use the first one.
9211ae08745Sheppo 	 */
9221ae08745Sheppo 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vswp->dip, 0,
9231ae08745Sheppo 	    "vsw_physname", &dev) == DDI_PROP_SUCCESS) {
9241ae08745Sheppo 		if ((strlen(dev) + 1) > LIFNAMSIZ) {
92534683adeSsg70180 			cmn_err(CE_WARN, "vsw%d: %s is too long a device name",
92634683adeSsg70180 			    vswp->instance, dev);
92734683adeSsg70180 			ddi_prop_free(dev);
92834683adeSsg70180 			return (1);
9291ae08745Sheppo 		} else {
93034683adeSsg70180 			cmn_err(CE_NOTE, "vsw%d: Using device name (%s) from "
93134683adeSsg70180 			    "config file", vswp->instance, dev);
9321ae08745Sheppo 
93334683adeSsg70180 			(void) strncpy(name, dev, strlen(dev) + 1);
9341ae08745Sheppo 		}
9351ae08745Sheppo 
9361ae08745Sheppo 		ddi_prop_free(dev);
9371ae08745Sheppo 	}
9381ae08745Sheppo #endif
9391ae08745Sheppo 
94034683adeSsg70180 	return (0);
94134683adeSsg70180 }
942e1ebb9ecSlm66018 
943e1ebb9ecSlm66018 /*
94434683adeSsg70180  * Read the 'vsw-switch-mode' property from the specified MD node.
94534683adeSsg70180  *
94634683adeSsg70180  * Returns 0 on success and the number of modes found in 'found',
94734683adeSsg70180  * otherwise returns 1.
948e1ebb9ecSlm66018  */
94934683adeSsg70180 static int
95034683adeSsg70180 vsw_get_md_smodes(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
95134683adeSsg70180 						uint8_t *modes, int *found)
95234683adeSsg70180 {
95334683adeSsg70180 	int		len = 0;
95434683adeSsg70180 	int		smode_num = 0;
95534683adeSsg70180 	char		*smode = NULL;
95634683adeSsg70180 	char		*curr_mode = NULL;
95734683adeSsg70180 
95834683adeSsg70180 	D1(vswp, "%s: enter", __func__);
9591ae08745Sheppo 
9601ae08745Sheppo 	/*
9611ae08745Sheppo 	 * Get the switch-mode property. The modes are listed in
9621ae08745Sheppo 	 * decreasing order of preference, i.e. prefered mode is
9631ae08745Sheppo 	 * first item in list.
9641ae08745Sheppo 	 */
9651ae08745Sheppo 	len = 0;
96634683adeSsg70180 	smode_num = 0;
96734683adeSsg70180 	if (md_get_prop_data(mdp, node, smode_propname,
9681ae08745Sheppo 	    (uint8_t **)(&smode), &len) != 0) {
9691ae08745Sheppo 		/*
970e1ebb9ecSlm66018 		 * Unable to get switch-mode property from MD, nothing
971e1ebb9ecSlm66018 		 * more we can do.
9721ae08745Sheppo 		 */
97334683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get switch mode property"
97434683adeSsg70180 		    " from the MD", vswp->instance);
97534683adeSsg70180 		*found = 0;
97634683adeSsg70180 		return (1);
977e1ebb9ecSlm66018 	}
978e1ebb9ecSlm66018 
9791ae08745Sheppo 	curr_mode = smode;
9801ae08745Sheppo 	/*
9811ae08745Sheppo 	 * Modes of operation:
9821ae08745Sheppo 	 * 'switched'	 - layer 2 switching, underlying HW in
983e1ebb9ecSlm66018 	 *			programmed mode.
9841ae08745Sheppo 	 * 'promiscuous' - layer 2 switching, underlying HW in
9851ae08745Sheppo 	 *			promiscuous mode.
9861ae08745Sheppo 	 * 'routed'	 - layer 3 (i.e. IP) routing, underlying HW
9871ae08745Sheppo 	 *			in non-promiscuous mode.
9881ae08745Sheppo 	 */
98934683adeSsg70180 	while ((curr_mode < (smode + len)) && (smode_num < NUM_SMODES)) {
9901ae08745Sheppo 		D2(vswp, "%s: curr_mode = [%s]", __func__, curr_mode);
991e1ebb9ecSlm66018 		if (strcmp(curr_mode, "switched") == 0) {
99234683adeSsg70180 			modes[smode_num++] = VSW_LAYER2;
993e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "promiscuous") == 0) {
99434683adeSsg70180 			modes[smode_num++] = VSW_LAYER2_PROMISC;
995e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "routed") == 0) {
99634683adeSsg70180 			modes[smode_num++] = VSW_LAYER3;
997e1ebb9ecSlm66018 		} else {
9981ef0bbb5Snarayan 			DWARN(vswp, "%s: Unknown switch mode %s, "
9991ef0bbb5Snarayan 			    "setting to default 'switched' mode",
10001ef0bbb5Snarayan 			    __func__, curr_mode);
100134683adeSsg70180 			modes[smode_num++] = VSW_LAYER2;
10021ae08745Sheppo 		}
10031ae08745Sheppo 		curr_mode += strlen(curr_mode) + 1;
10041ae08745Sheppo 	}
100534683adeSsg70180 	*found = smode_num;
10061ae08745Sheppo 
100734683adeSsg70180 	D2(vswp, "%s: %d modes found", __func__, smode_num);
10081ae08745Sheppo 
10091ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
101034683adeSsg70180 
101134683adeSsg70180 	return (0);
10121ae08745Sheppo }
10131ae08745Sheppo 
1014e1ebb9ecSlm66018 /*
10151ae08745Sheppo  * Register with the MAC layer as a network device, so we
10161ae08745Sheppo  * can be plumbed if necessary.
10171ae08745Sheppo  */
10181ae08745Sheppo static int
10191ae08745Sheppo vsw_mac_register(vsw_t *vswp)
10201ae08745Sheppo {
1021ba2e4443Sseb 	mac_register_t	*macp;
1022ba2e4443Sseb 	int		rv;
10231ae08745Sheppo 
10241ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10251ae08745Sheppo 
1026ba2e4443Sseb 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
1027ba2e4443Sseb 		return (EINVAL);
1028ba2e4443Sseb 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
10291ae08745Sheppo 	macp->m_driver = vswp;
1030ba2e4443Sseb 	macp->m_dip = vswp->dip;
1031ba2e4443Sseb 	macp->m_src_addr = (uint8_t *)&vswp->if_addr;
1032ba2e4443Sseb 	macp->m_callbacks = &vsw_m_callbacks;
1033ba2e4443Sseb 	macp->m_min_sdu = 0;
1034c1c61f44Ssb155480 	macp->m_max_sdu = vsw_ethermtu;
1035c1c61f44Ssb155480 	macp->m_margin = VLAN_TAGSZ;
1036ba2e4443Sseb 	rv = mac_register(macp, &vswp->if_mh);
1037ba2e4443Sseb 	mac_free(macp);
103819b65a69Ssb155480 	if (rv != 0) {
103919b65a69Ssb155480 		/*
104019b65a69Ssb155480 		 * Treat this as a non-fatal error as we may be
104119b65a69Ssb155480 		 * able to operate in some other mode.
104219b65a69Ssb155480 		 */
104319b65a69Ssb155480 		cmn_err(CE_NOTE, "!vsw%d: Unable to register as "
104419b65a69Ssb155480 		    "a provider with MAC layer", vswp->instance);
104519b65a69Ssb155480 		return (rv);
104619b65a69Ssb155480 	}
104719b65a69Ssb155480 
1048ba2e4443Sseb 	vswp->if_state |= VSW_IF_REG;
10491ae08745Sheppo 
1050c1c61f44Ssb155480 	vswp->max_frame_size = vsw_ethermtu + sizeof (struct ether_header)
1051c1c61f44Ssb155480 	    + VLAN_TAGSZ;
1052c1c61f44Ssb155480 
10531ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
10541ae08745Sheppo 
10551ae08745Sheppo 	return (rv);
10561ae08745Sheppo }
10571ae08745Sheppo 
10581ae08745Sheppo static int
10591ae08745Sheppo vsw_mac_unregister(vsw_t *vswp)
10601ae08745Sheppo {
10611ae08745Sheppo 	int		rv = 0;
10621ae08745Sheppo 
10631ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10641ae08745Sheppo 
10651ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
10661ae08745Sheppo 
1067ba2e4443Sseb 	if (vswp->if_state & VSW_IF_REG) {
1068ba2e4443Sseb 		rv = mac_unregister(vswp->if_mh);
10691ae08745Sheppo 		if (rv != 0) {
10701ae08745Sheppo 			DWARN(vswp, "%s: unable to unregister from MAC "
10711ae08745Sheppo 			    "framework", __func__);
10721ae08745Sheppo 
10731ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
10741ae08745Sheppo 			D1(vswp, "%s: fail exit", __func__);
10751ae08745Sheppo 			return (rv);
10761ae08745Sheppo 		}
10771ae08745Sheppo 
1078ba2e4443Sseb 		/* mark i/f as down and unregistered */
1079ba2e4443Sseb 		vswp->if_state &= ~(VSW_IF_UP | VSW_IF_REG);
10801ae08745Sheppo 	}
10811ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
10821ae08745Sheppo 
10831ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
10841ae08745Sheppo 
10851ae08745Sheppo 	return (rv);
10861ae08745Sheppo }
10871ae08745Sheppo 
1088ba2e4443Sseb static int
1089ba2e4443Sseb vsw_m_stat(void *arg, uint_t stat, uint64_t *val)
10901ae08745Sheppo {
10911ae08745Sheppo 	vsw_t			*vswp = (vsw_t *)arg;
10921ae08745Sheppo 
10931ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10941ae08745Sheppo 
109534683adeSsg70180 	mutex_enter(&vswp->mac_lock);
109634683adeSsg70180 	if (vswp->mh == NULL) {
109734683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1098ba2e4443Sseb 		return (EINVAL);
109934683adeSsg70180 	}
11001ae08745Sheppo 
11011ae08745Sheppo 	/* return stats from underlying device */
1102ba2e4443Sseb 	*val = mac_stat_get(vswp->mh, stat);
110334683adeSsg70180 
110434683adeSsg70180 	mutex_exit(&vswp->mac_lock);
110534683adeSsg70180 
1106ba2e4443Sseb 	return (0);
11071ae08745Sheppo }
11081ae08745Sheppo 
11091ae08745Sheppo static void
11101ae08745Sheppo vsw_m_stop(void *arg)
11111ae08745Sheppo {
11121ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
11131ae08745Sheppo 
11141ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11151ae08745Sheppo 
11161ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
11171ae08745Sheppo 	vswp->if_state &= ~VSW_IF_UP;
11181ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
11191ae08745Sheppo 
11205f94e909Ssg70180 	mutex_enter(&vswp->hw_lock);
11215f94e909Ssg70180 
11225f94e909Ssg70180 	(void) vsw_unset_hw(vswp, NULL, VSW_LOCALDEV);
11235f94e909Ssg70180 
11245f94e909Ssg70180 	if (vswp->recfg_reqd)
11255f94e909Ssg70180 		vsw_reconfig_hw(vswp);
11265f94e909Ssg70180 
11275f94e909Ssg70180 	mutex_exit(&vswp->hw_lock);
11285f94e909Ssg70180 
11291ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
11301ae08745Sheppo }
11311ae08745Sheppo 
11321ae08745Sheppo static int
11331ae08745Sheppo vsw_m_start(void *arg)
11341ae08745Sheppo {
11351ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
11361ae08745Sheppo 
11371ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11381ae08745Sheppo 
11391ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
11401ae08745Sheppo 
114119b65a69Ssb155480 	vswp->if_state |= VSW_IF_UP;
114219b65a69Ssb155480 
114319b65a69Ssb155480 	if (vswp->switching_setup_done == B_FALSE) {
114419b65a69Ssb155480 		/*
114519b65a69Ssb155480 		 * If the switching mode has not been setup yet, just
114619b65a69Ssb155480 		 * return. The unicast address will be programmed
114719b65a69Ssb155480 		 * after the physical device is successfully setup by the
114819b65a69Ssb155480 		 * timeout handler.
114919b65a69Ssb155480 		 */
115019b65a69Ssb155480 		RW_EXIT(&vswp->if_lockrw);
115119b65a69Ssb155480 		return (0);
115219b65a69Ssb155480 	}
115319b65a69Ssb155480 
115419b65a69Ssb155480 	/* if in layer2 mode, program unicast address. */
115519b65a69Ssb155480 	if (vswp->mh != NULL) {
11565f94e909Ssg70180 		mutex_enter(&vswp->hw_lock);
11575f94e909Ssg70180 		(void) vsw_set_hw(vswp, NULL, VSW_LOCALDEV);
11585f94e909Ssg70180 		mutex_exit(&vswp->hw_lock);
115919b65a69Ssb155480 	}
116019b65a69Ssb155480 
116119b65a69Ssb155480 	RW_EXIT(&vswp->if_lockrw);
11625f94e909Ssg70180 
11631ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
11641ae08745Sheppo 	return (0);
11651ae08745Sheppo }
11661ae08745Sheppo 
11671ae08745Sheppo /*
11681ae08745Sheppo  * Change the local interface address.
11695f94e909Ssg70180  *
11705f94e909Ssg70180  * Note: we don't support this entry point. The local
11715f94e909Ssg70180  * mac address of the switch can only be changed via its
11725f94e909Ssg70180  * MD node properties.
11731ae08745Sheppo  */
11741ae08745Sheppo static int
11751ae08745Sheppo vsw_m_unicst(void *arg, const uint8_t *macaddr)
11761ae08745Sheppo {
11775f94e909Ssg70180 	_NOTE(ARGUNUSED(arg, macaddr))
11781ae08745Sheppo 
11795f94e909Ssg70180 	return (DDI_FAILURE);
11801ae08745Sheppo }
11811ae08745Sheppo 
11821ae08745Sheppo static int
11831ae08745Sheppo vsw_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
11841ae08745Sheppo {
11851ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
11861ae08745Sheppo 	mcst_addr_t	*mcst_p = NULL;
11871ae08745Sheppo 	uint64_t	addr = 0x0;
1188e1ebb9ecSlm66018 	int		i, ret = 0;
11891ae08745Sheppo 
11901ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11911ae08745Sheppo 
11921ae08745Sheppo 	/*
11931ae08745Sheppo 	 * Convert address into form that can be used
11941ae08745Sheppo 	 * as hash table key.
11951ae08745Sheppo 	 */
11961ae08745Sheppo 	for (i = 0; i < ETHERADDRL; i++) {
11971ae08745Sheppo 		addr = (addr << 8) | mca[i];
11981ae08745Sheppo 	}
11991ae08745Sheppo 
12001ae08745Sheppo 	D2(vswp, "%s: addr = 0x%llx", __func__, addr);
12011ae08745Sheppo 
12021ae08745Sheppo 	if (add) {
12031ae08745Sheppo 		D2(vswp, "%s: adding multicast", __func__);
12041ae08745Sheppo 		if (vsw_add_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
12051ae08745Sheppo 			/*
12061ae08745Sheppo 			 * Update the list of multicast addresses
12071ae08745Sheppo 			 * contained within the vsw_t structure to
12081ae08745Sheppo 			 * include this new one.
12091ae08745Sheppo 			 */
12101ae08745Sheppo 			mcst_p = kmem_zalloc(sizeof (mcst_addr_t), KM_NOSLEEP);
12111ae08745Sheppo 			if (mcst_p == NULL) {
12121ae08745Sheppo 				DERR(vswp, "%s unable to alloc mem", __func__);
121319b65a69Ssb155480 				(void) vsw_del_mcst(vswp,
121419b65a69Ssb155480 				    VSW_LOCALDEV, addr, NULL);
12151ae08745Sheppo 				return (1);
12161ae08745Sheppo 			}
12171ae08745Sheppo 			mcst_p->addr = addr;
121819b65a69Ssb155480 			ether_copy(mca, &mcst_p->mca);
12191ae08745Sheppo 
12201ae08745Sheppo 			/*
12211ae08745Sheppo 			 * Call into the underlying driver to program the
12221ae08745Sheppo 			 * address into HW.
12231ae08745Sheppo 			 */
122434683adeSsg70180 			mutex_enter(&vswp->mac_lock);
1225e1ebb9ecSlm66018 			if (vswp->mh != NULL) {
1226e1ebb9ecSlm66018 				ret = mac_multicst_add(vswp->mh, mca);
1227e1ebb9ecSlm66018 				if (ret != 0) {
12281ef0bbb5Snarayan 					cmn_err(CE_NOTE, "!vsw%d: unable to "
122934683adeSsg70180 					    "add multicast address",
123034683adeSsg70180 					    vswp->instance);
123134683adeSsg70180 					mutex_exit(&vswp->mac_lock);
123219b65a69Ssb155480 					(void) vsw_del_mcst(vswp,
123319b65a69Ssb155480 					    VSW_LOCALDEV, addr, NULL);
123419b65a69Ssb155480 					kmem_free(mcst_p, sizeof (*mcst_p));
123519b65a69Ssb155480 					return (ret);
1236e1ebb9ecSlm66018 				}
123719b65a69Ssb155480 				mcst_p->mac_added = B_TRUE;
12381ae08745Sheppo 			}
123934683adeSsg70180 			mutex_exit(&vswp->mac_lock);
124019b65a69Ssb155480 
124119b65a69Ssb155480 			mutex_enter(&vswp->mca_lock);
124219b65a69Ssb155480 			mcst_p->nextp = vswp->mcap;
124319b65a69Ssb155480 			vswp->mcap = mcst_p;
124419b65a69Ssb155480 			mutex_exit(&vswp->mca_lock);
12451ae08745Sheppo 		} else {
12461ef0bbb5Snarayan 			cmn_err(CE_NOTE, "!vsw%d: unable to add multicast "
124734683adeSsg70180 			    "address", vswp->instance);
1248e1ebb9ecSlm66018 		}
1249e1ebb9ecSlm66018 		return (ret);
1250e1ebb9ecSlm66018 	}
1251e1ebb9ecSlm66018 
12521ae08745Sheppo 	D2(vswp, "%s: removing multicast", __func__);
12531ae08745Sheppo 	/*
12541ae08745Sheppo 	 * Remove the address from the hash table..
12551ae08745Sheppo 	 */
12561ae08745Sheppo 	if (vsw_del_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
12571ae08745Sheppo 
12581ae08745Sheppo 		/*
12591ae08745Sheppo 		 * ..and then from the list maintained in the
12601ae08745Sheppo 		 * vsw_t structure.
12611ae08745Sheppo 		 */
126219b65a69Ssb155480 		mcst_p = vsw_del_addr(VSW_LOCALDEV, vswp, addr);
126319b65a69Ssb155480 		ASSERT(mcst_p != NULL);
12641ae08745Sheppo 
126534683adeSsg70180 		mutex_enter(&vswp->mac_lock);
126619b65a69Ssb155480 		if (vswp->mh != NULL && mcst_p->mac_added) {
12671ae08745Sheppo 			(void) mac_multicst_remove(vswp->mh, mca);
126819b65a69Ssb155480 			mcst_p->mac_added = B_FALSE;
126919b65a69Ssb155480 		}
127034683adeSsg70180 		mutex_exit(&vswp->mac_lock);
127119b65a69Ssb155480 		kmem_free(mcst_p, sizeof (*mcst_p));
12721ae08745Sheppo 	}
12731ae08745Sheppo 
12741ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
12751ae08745Sheppo 
12761ae08745Sheppo 	return (0);
12771ae08745Sheppo }
12781ae08745Sheppo 
12791ae08745Sheppo static int
12801ae08745Sheppo vsw_m_promisc(void *arg, boolean_t on)
12811ae08745Sheppo {
12821ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
12831ae08745Sheppo 
12841ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
12851ae08745Sheppo 
12861ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
12871ae08745Sheppo 	if (on)
12881ae08745Sheppo 		vswp->if_state |= VSW_IF_PROMISC;
12891ae08745Sheppo 	else
12901ae08745Sheppo 		vswp->if_state &= ~VSW_IF_PROMISC;
12911ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
12921ae08745Sheppo 
12931ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
12941ae08745Sheppo 
12951ae08745Sheppo 	return (0);
12961ae08745Sheppo }
12971ae08745Sheppo 
12981ae08745Sheppo static mblk_t *
12991ae08745Sheppo vsw_m_tx(void *arg, mblk_t *mp)
13001ae08745Sheppo {
13011ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
13021ae08745Sheppo 
13031ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
13041ae08745Sheppo 
1305c1c61f44Ssb155480 	mp = vsw_vlan_frame_pretag(vswp, VSW_LOCALDEV, mp);
1306c1c61f44Ssb155480 
1307c1c61f44Ssb155480 	if (mp == NULL) {
1308c1c61f44Ssb155480 		return (NULL);
1309c1c61f44Ssb155480 	}
1310c1c61f44Ssb155480 
131134683adeSsg70180 	vswp->vsw_switch_frame(vswp, mp, VSW_LOCALDEV, NULL, NULL);
13121ae08745Sheppo 
13131ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
13141ae08745Sheppo 
13151ae08745Sheppo 	return (NULL);
13161ae08745Sheppo }
13171ae08745Sheppo 
13181ae08745Sheppo /*
13191ae08745Sheppo  * Register for machine description (MD) updates.
132034683adeSsg70180  *
132134683adeSsg70180  * Returns 0 on success, 1 on failure.
13221ae08745Sheppo  */
132334683adeSsg70180 static int
13241ae08745Sheppo vsw_mdeg_register(vsw_t *vswp)
13251ae08745Sheppo {
13261ae08745Sheppo 	mdeg_prop_spec_t	*pspecp;
13271ae08745Sheppo 	mdeg_node_spec_t	*inst_specp;
132834683adeSsg70180 	mdeg_handle_t		mdeg_hdl, mdeg_port_hdl;
13291ae08745Sheppo 	size_t			templatesz;
133019b65a69Ssb155480 	int			rv;
13311ae08745Sheppo 
13321ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
13331ae08745Sheppo 
133434683adeSsg70180 	/*
13351ae08745Sheppo 	 * Allocate and initialize a per-instance copy
13361ae08745Sheppo 	 * of the global property spec array that will
13371ae08745Sheppo 	 * uniquely identify this vsw instance.
13381ae08745Sheppo 	 */
13391ae08745Sheppo 	templatesz = sizeof (vsw_prop_template);
13401ae08745Sheppo 	pspecp = kmem_zalloc(templatesz, KM_SLEEP);
13411ae08745Sheppo 
13421ae08745Sheppo 	bcopy(vsw_prop_template, pspecp, templatesz);
13431ae08745Sheppo 
134419b65a69Ssb155480 	VSW_SET_MDEG_PROP_INST(pspecp, vswp->regprop);
13451ae08745Sheppo 
13461ae08745Sheppo 	/* initialize the complete prop spec structure */
13471ae08745Sheppo 	inst_specp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
13481ae08745Sheppo 	inst_specp->namep = "virtual-device";
13491ae08745Sheppo 	inst_specp->specp = pspecp;
13501ae08745Sheppo 
135119b65a69Ssb155480 	D2(vswp, "%s: instance %d registering with mdeg", __func__,
135219b65a69Ssb155480 	    vswp->regprop);
135334683adeSsg70180 	/*
135434683adeSsg70180 	 * Register an interest in 'virtual-device' nodes with a
135534683adeSsg70180 	 * 'name' property of 'virtual-network-switch'
135634683adeSsg70180 	 */
135734683adeSsg70180 	rv = mdeg_register(inst_specp, &vdev_match, vsw_mdeg_cb,
13581ae08745Sheppo 	    (void *)vswp, &mdeg_hdl);
135934683adeSsg70180 	if (rv != MDEG_SUCCESS) {
136034683adeSsg70180 		DERR(vswp, "%s: mdeg_register failed (%d) for vsw node",
136134683adeSsg70180 		    __func__, rv);
136234683adeSsg70180 		goto mdeg_reg_fail;
136334683adeSsg70180 	}
13641ae08745Sheppo 
136534683adeSsg70180 	/*
136634683adeSsg70180 	 * Register an interest in 'vsw-port' nodes.
136734683adeSsg70180 	 */
136834683adeSsg70180 	rv = mdeg_register(inst_specp, &vport_match, vsw_port_mdeg_cb,
136934683adeSsg70180 	    (void *)vswp, &mdeg_port_hdl);
13701ae08745Sheppo 	if (rv != MDEG_SUCCESS) {
13711ae08745Sheppo 		DERR(vswp, "%s: mdeg_register failed (%d)\n", __func__, rv);
137234683adeSsg70180 		(void) mdeg_unregister(mdeg_hdl);
137334683adeSsg70180 		goto mdeg_reg_fail;
13741ae08745Sheppo 	}
13751ae08745Sheppo 
13761ae08745Sheppo 	/* save off data that will be needed later */
13771ae08745Sheppo 	vswp->inst_spec = inst_specp;
13781ae08745Sheppo 	vswp->mdeg_hdl = mdeg_hdl;
137934683adeSsg70180 	vswp->mdeg_port_hdl = mdeg_port_hdl;
13801ae08745Sheppo 
13811ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
138234683adeSsg70180 	return (0);
138334683adeSsg70180 
138434683adeSsg70180 mdeg_reg_fail:
138534683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: Unable to register MDEG callbacks",
138634683adeSsg70180 	    vswp->instance);
138734683adeSsg70180 	kmem_free(pspecp, templatesz);
138834683adeSsg70180 	kmem_free(inst_specp, sizeof (mdeg_node_spec_t));
138934683adeSsg70180 
139034683adeSsg70180 	vswp->mdeg_hdl = NULL;
139134683adeSsg70180 	vswp->mdeg_port_hdl = NULL;
139234683adeSsg70180 
139334683adeSsg70180 	return (1);
13941ae08745Sheppo }
13951ae08745Sheppo 
13961ae08745Sheppo static void
13971ae08745Sheppo vsw_mdeg_unregister(vsw_t *vswp)
13981ae08745Sheppo {
13991ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: enter");
14001ae08745Sheppo 
140134683adeSsg70180 	if (vswp->mdeg_hdl != NULL)
14021ae08745Sheppo 		(void) mdeg_unregister(vswp->mdeg_hdl);
14031ae08745Sheppo 
140434683adeSsg70180 	if (vswp->mdeg_port_hdl != NULL)
140534683adeSsg70180 		(void) mdeg_unregister(vswp->mdeg_port_hdl);
140634683adeSsg70180 
140734683adeSsg70180 	if (vswp->inst_spec != NULL) {
14081ae08745Sheppo 		if (vswp->inst_spec->specp != NULL) {
14091ae08745Sheppo 			(void) kmem_free(vswp->inst_spec->specp,
14101ae08745Sheppo 			    sizeof (vsw_prop_template));
14111ae08745Sheppo 			vswp->inst_spec->specp = NULL;
14121ae08745Sheppo 		}
14131ae08745Sheppo 
1414205eeb1aSlm66018 		(void) kmem_free(vswp->inst_spec, sizeof (mdeg_node_spec_t));
14151ae08745Sheppo 		vswp->inst_spec = NULL;
14161ae08745Sheppo 	}
14171ae08745Sheppo 
14181ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: exit");
14191ae08745Sheppo }
14201ae08745Sheppo 
142134683adeSsg70180 /*
142234683adeSsg70180  * Mdeg callback invoked for the vsw node itself.
142334683adeSsg70180  */
14241ae08745Sheppo static int
14251ae08745Sheppo vsw_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
14261ae08745Sheppo {
14271ae08745Sheppo 	vsw_t		*vswp;
14281ae08745Sheppo 	md_t		*mdp;
14291ae08745Sheppo 	mde_cookie_t	node;
14301ae08745Sheppo 	uint64_t	inst;
143134683adeSsg70180 	char		*node_name = NULL;
14321ae08745Sheppo 
14331ae08745Sheppo 	if (resp == NULL)
14341ae08745Sheppo 		return (MDEG_FAILURE);
14351ae08745Sheppo 
14361ae08745Sheppo 	vswp = (vsw_t *)cb_argp;
14371ae08745Sheppo 
143834683adeSsg70180 	D1(vswp, "%s: added %d : removed %d : curr matched %d"
143934683adeSsg70180 	    " : prev matched %d", __func__, resp->added.nelem,
144034683adeSsg70180 	    resp->removed.nelem, resp->match_curr.nelem,
144134683adeSsg70180 	    resp->match_prev.nelem);
144234683adeSsg70180 
144334683adeSsg70180 	/*
144419b65a69Ssb155480 	 * We get an initial callback for this node as 'added'
144519b65a69Ssb155480 	 * after registering with mdeg. Note that we would have
144619b65a69Ssb155480 	 * already gathered information about this vsw node by
144719b65a69Ssb155480 	 * walking MD earlier during attach (in vsw_read_mdprops()).
144819b65a69Ssb155480 	 * So, there is a window where the properties of this
144919b65a69Ssb155480 	 * node might have changed when we get this initial 'added'
145019b65a69Ssb155480 	 * callback. We handle this as if an update occured
145119b65a69Ssb155480 	 * and invoke the same function which handles updates to
145219b65a69Ssb155480 	 * the properties of this vsw-node if any.
145319b65a69Ssb155480 	 *
145434683adeSsg70180 	 * A non-zero 'match' value indicates that the MD has been
145519b65a69Ssb155480 	 * updated and that a virtual-network-switch node is
145619b65a69Ssb155480 	 * present which may or may not have been updated. It is
145719b65a69Ssb155480 	 * up to the clients to examine their own nodes and
145819b65a69Ssb155480 	 * determine if they have changed.
145934683adeSsg70180 	 */
146019b65a69Ssb155480 	if (resp->added.nelem != 0) {
146134683adeSsg70180 
146219b65a69Ssb155480 		if (resp->added.nelem != 1) {
146319b65a69Ssb155480 			cmn_err(CE_NOTE, "!vsw%d: number of nodes added "
146419b65a69Ssb155480 			    "invalid: %d\n", vswp->instance, resp->added.nelem);
146519b65a69Ssb155480 			return (MDEG_FAILURE);
146619b65a69Ssb155480 		}
146719b65a69Ssb155480 
146819b65a69Ssb155480 		mdp = resp->added.mdp;
146919b65a69Ssb155480 		node = resp->added.mdep[0];
147019b65a69Ssb155480 
147119b65a69Ssb155480 	} else if (resp->match_curr.nelem != 0) {
147219b65a69Ssb155480 
147319b65a69Ssb155480 		if (resp->match_curr.nelem != 1) {
147419b65a69Ssb155480 			cmn_err(CE_NOTE, "!vsw%d: number of nodes updated "
147519b65a69Ssb155480 			    "invalid: %d\n", vswp->instance,
147619b65a69Ssb155480 			    resp->match_curr.nelem);
147719b65a69Ssb155480 			return (MDEG_FAILURE);
147819b65a69Ssb155480 		}
147919b65a69Ssb155480 
148019b65a69Ssb155480 		mdp = resp->match_curr.mdp;
148119b65a69Ssb155480 		node = resp->match_curr.mdep[0];
148219b65a69Ssb155480 
148319b65a69Ssb155480 	} else {
148419b65a69Ssb155480 		return (MDEG_FAILURE);
148519b65a69Ssb155480 	}
148619b65a69Ssb155480 
148719b65a69Ssb155480 	/* Validate name and instance */
148834683adeSsg70180 	if (md_get_prop_str(mdp, node, "name", &node_name) != 0) {
148919b65a69Ssb155480 		DERR(vswp, "%s: unable to get node name\n",  __func__);
149019b65a69Ssb155480 		return (MDEG_FAILURE);
149119b65a69Ssb155480 	}
149219b65a69Ssb155480 
149319b65a69Ssb155480 	/* is this a virtual-network-switch? */
149419b65a69Ssb155480 	if (strcmp(node_name, vsw_propname) != 0) {
149519b65a69Ssb155480 		DERR(vswp, "%s: Invalid node name: %s\n",
149619b65a69Ssb155480 		    __func__, node_name);
149719b65a69Ssb155480 		return (MDEG_FAILURE);
149834683adeSsg70180 	}
149934683adeSsg70180 
150034683adeSsg70180 	if (md_get_prop_val(mdp, node, "cfg-handle", &inst)) {
150119b65a69Ssb155480 		DERR(vswp, "%s: prop(cfg-handle) not found\n",
150219b65a69Ssb155480 		    __func__);
150319b65a69Ssb155480 		return (MDEG_FAILURE);
150434683adeSsg70180 	}
150534683adeSsg70180 
150619b65a69Ssb155480 	/* is this the right instance of vsw? */
150719b65a69Ssb155480 	if (inst != vswp->regprop) {
150819b65a69Ssb155480 		DERR(vswp, "%s: Invalid cfg-handle: %lx\n",
150919b65a69Ssb155480 		    __func__, inst);
151019b65a69Ssb155480 		return (MDEG_FAILURE);
151119b65a69Ssb155480 	}
151234683adeSsg70180 
151334683adeSsg70180 	vsw_update_md_prop(vswp, mdp, node);
151434683adeSsg70180 
151534683adeSsg70180 	return (MDEG_SUCCESS);
151634683adeSsg70180 }
151734683adeSsg70180 
151834683adeSsg70180 /*
151934683adeSsg70180  * Mdeg callback invoked for changes to the vsw-port nodes
152034683adeSsg70180  * under the vsw node.
152134683adeSsg70180  */
152234683adeSsg70180 static int
152334683adeSsg70180 vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
152434683adeSsg70180 {
152534683adeSsg70180 	vsw_t		*vswp;
152634683adeSsg70180 	int		idx;
152734683adeSsg70180 	md_t		*mdp;
152834683adeSsg70180 	mde_cookie_t	node;
152934683adeSsg70180 	uint64_t	inst;
15301ef0bbb5Snarayan 	int		rv;
153134683adeSsg70180 
153234683adeSsg70180 	if ((resp == NULL) || (cb_argp == NULL))
153334683adeSsg70180 		return (MDEG_FAILURE);
153434683adeSsg70180 
153534683adeSsg70180 	vswp = (vsw_t *)cb_argp;
153634683adeSsg70180 
153734683adeSsg70180 	D2(vswp, "%s: added %d : removed %d : curr matched %d"
153834683adeSsg70180 	    " : prev matched %d", __func__, resp->added.nelem,
153934683adeSsg70180 	    resp->removed.nelem, resp->match_curr.nelem,
15401ae08745Sheppo 	    resp->match_prev.nelem);
15411ae08745Sheppo 
15421ae08745Sheppo 	/* process added ports */
15431ae08745Sheppo 	for (idx = 0; idx < resp->added.nelem; idx++) {
15441ae08745Sheppo 		mdp = resp->added.mdp;
15451ae08745Sheppo 		node = resp->added.mdep[idx];
15461ae08745Sheppo 
15471ae08745Sheppo 		D2(vswp, "%s: adding node(%d) 0x%lx", __func__, idx, node);
15481ae08745Sheppo 
15491ef0bbb5Snarayan 		if ((rv = vsw_port_add(vswp, mdp, &node)) != 0) {
155034683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to add new port "
15511ef0bbb5Snarayan 			    "(0x%lx), err=%d", vswp->instance, node, rv);
15521ae08745Sheppo 		}
15531ae08745Sheppo 	}
15541ae08745Sheppo 
15551ae08745Sheppo 	/* process removed ports */
15561ae08745Sheppo 	for (idx = 0; idx < resp->removed.nelem; idx++) {
15571ae08745Sheppo 		mdp = resp->removed.mdp;
15581ae08745Sheppo 		node = resp->removed.mdep[idx];
15591ae08745Sheppo 
15601ae08745Sheppo 		if (md_get_prop_val(mdp, node, id_propname, &inst)) {
156134683adeSsg70180 			DERR(vswp, "%s: prop(%s) not found in port(%d)",
15621ae08745Sheppo 			    __func__, id_propname, idx);
15631ae08745Sheppo 			continue;
15641ae08745Sheppo 		}
15651ae08745Sheppo 
15661ae08745Sheppo 		D2(vswp, "%s: removing node(%d) 0x%lx", __func__, idx, node);
15671ae08745Sheppo 
15681ae08745Sheppo 		if (vsw_port_detach(vswp, inst) != 0) {
156934683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to remove port %ld",
157034683adeSsg70180 			    vswp->instance, inst);
15711ae08745Sheppo 		}
15721ae08745Sheppo 	}
15731ae08745Sheppo 
1574c1c61f44Ssb155480 	for (idx = 0; idx < resp->match_curr.nelem; idx++) {
1575c1c61f44Ssb155480 		(void) vsw_port_update(vswp, resp->match_curr.mdp,
1576c1c61f44Ssb155480 		    resp->match_curr.mdep[idx],
1577c1c61f44Ssb155480 		    resp->match_prev.mdp,
1578c1c61f44Ssb155480 		    resp->match_prev.mdep[idx]);
1579c1c61f44Ssb155480 	}
15801ae08745Sheppo 
15811ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
15821ae08745Sheppo 
15831ae08745Sheppo 	return (MDEG_SUCCESS);
15841ae08745Sheppo }
15851ae08745Sheppo 
15861ae08745Sheppo /*
158719b65a69Ssb155480  * Scan the machine description for this instance of vsw
158819b65a69Ssb155480  * and read its properties. Called only from vsw_attach().
158919b65a69Ssb155480  * Returns: 0 on success, 1 on failure.
159019b65a69Ssb155480  */
159119b65a69Ssb155480 static int
159219b65a69Ssb155480 vsw_read_mdprops(vsw_t *vswp)
159319b65a69Ssb155480 {
159419b65a69Ssb155480 	md_t		*mdp = NULL;
159519b65a69Ssb155480 	mde_cookie_t	rootnode;
159619b65a69Ssb155480 	mde_cookie_t	*listp = NULL;
159719b65a69Ssb155480 	uint64_t	inst;
159819b65a69Ssb155480 	uint64_t	cfgh;
159919b65a69Ssb155480 	char		*name;
160019b65a69Ssb155480 	int		rv = 1;
160119b65a69Ssb155480 	int		num_nodes = 0;
160219b65a69Ssb155480 	int		num_devs = 0;
160319b65a69Ssb155480 	int		listsz = 0;
160419b65a69Ssb155480 	int		i;
160519b65a69Ssb155480 
160619b65a69Ssb155480 	/*
160719b65a69Ssb155480 	 * In each 'virtual-device' node in the MD there is a
160819b65a69Ssb155480 	 * 'cfg-handle' property which is the MD's concept of
160919b65a69Ssb155480 	 * an instance number (this may be completely different from
161019b65a69Ssb155480 	 * the device drivers instance #). OBP reads that value and
161119b65a69Ssb155480 	 * stores it in the 'reg' property of the appropriate node in
161219b65a69Ssb155480 	 * the device tree. We first read this reg property and use this
161319b65a69Ssb155480 	 * to compare against the 'cfg-handle' property of vsw nodes
161419b65a69Ssb155480 	 * in MD to get to this specific vsw instance and then read
161519b65a69Ssb155480 	 * other properties that we are interested in.
161619b65a69Ssb155480 	 * We also cache the value of 'reg' property and use it later
161719b65a69Ssb155480 	 * to register callbacks with mdeg (see vsw_mdeg_register())
161819b65a69Ssb155480 	 */
161919b65a69Ssb155480 	inst = ddi_prop_get_int(DDI_DEV_T_ANY, vswp->dip,
162019b65a69Ssb155480 	    DDI_PROP_DONTPASS, reg_propname, -1);
162119b65a69Ssb155480 	if (inst == -1) {
162219b65a69Ssb155480 		cmn_err(CE_NOTE, "!vsw%d: Unable to read %s property from "
162319b65a69Ssb155480 		    "OBP device tree", vswp->instance, reg_propname);
162419b65a69Ssb155480 		return (rv);
162519b65a69Ssb155480 	}
162619b65a69Ssb155480 
162719b65a69Ssb155480 	vswp->regprop = inst;
162819b65a69Ssb155480 
162919b65a69Ssb155480 	if ((mdp = md_get_handle()) == NULL) {
163019b65a69Ssb155480 		DWARN(vswp, "%s: cannot init MD\n", __func__);
163119b65a69Ssb155480 		return (rv);
163219b65a69Ssb155480 	}
163319b65a69Ssb155480 
163419b65a69Ssb155480 	num_nodes = md_node_count(mdp);
163519b65a69Ssb155480 	ASSERT(num_nodes > 0);
163619b65a69Ssb155480 
163719b65a69Ssb155480 	listsz = num_nodes * sizeof (mde_cookie_t);
163819b65a69Ssb155480 	listp = (mde_cookie_t *)kmem_zalloc(listsz, KM_SLEEP);
163919b65a69Ssb155480 
164019b65a69Ssb155480 	rootnode = md_root_node(mdp);
164119b65a69Ssb155480 
164219b65a69Ssb155480 	/* search for all "virtual_device" nodes */
164319b65a69Ssb155480 	num_devs = md_scan_dag(mdp, rootnode,
164419b65a69Ssb155480 	    md_find_name(mdp, vdev_propname),
164519b65a69Ssb155480 	    md_find_name(mdp, "fwd"), listp);
164619b65a69Ssb155480 	if (num_devs <= 0) {
164719b65a69Ssb155480 		DWARN(vswp, "%s: invalid num_devs:%d\n", __func__, num_devs);
164819b65a69Ssb155480 		goto vsw_readmd_exit;
164919b65a69Ssb155480 	}
165019b65a69Ssb155480 
165119b65a69Ssb155480 	/*
165219b65a69Ssb155480 	 * Now loop through the list of virtual-devices looking for
165319b65a69Ssb155480 	 * devices with name "virtual-network-switch" and for each
165419b65a69Ssb155480 	 * such device compare its instance with what we have from
165519b65a69Ssb155480 	 * the 'reg' property to find the right node in MD and then
165619b65a69Ssb155480 	 * read all its properties.
165719b65a69Ssb155480 	 */
165819b65a69Ssb155480 	for (i = 0; i < num_devs; i++) {
165919b65a69Ssb155480 
166019b65a69Ssb155480 		if (md_get_prop_str(mdp, listp[i], "name", &name) != 0) {
166119b65a69Ssb155480 			DWARN(vswp, "%s: name property not found\n",
166219b65a69Ssb155480 			    __func__);
166319b65a69Ssb155480 			goto vsw_readmd_exit;
166419b65a69Ssb155480 		}
166519b65a69Ssb155480 
166619b65a69Ssb155480 		/* is this a virtual-network-switch? */
166719b65a69Ssb155480 		if (strcmp(name, vsw_propname) != 0)
166819b65a69Ssb155480 			continue;
166919b65a69Ssb155480 
167019b65a69Ssb155480 		if (md_get_prop_val(mdp, listp[i], "cfg-handle", &cfgh) != 0) {
167119b65a69Ssb155480 			DWARN(vswp, "%s: cfg-handle property not found\n",
167219b65a69Ssb155480 			    __func__);
167319b65a69Ssb155480 			goto vsw_readmd_exit;
167419b65a69Ssb155480 		}
167519b65a69Ssb155480 
167619b65a69Ssb155480 		/* is this the required instance of vsw? */
167719b65a69Ssb155480 		if (inst != cfgh)
167819b65a69Ssb155480 			continue;
167919b65a69Ssb155480 
168019b65a69Ssb155480 		/* now read all properties of this vsw instance */
168119b65a69Ssb155480 		rv = vsw_get_initial_md_properties(vswp, mdp, listp[i]);
168219b65a69Ssb155480 		break;
168319b65a69Ssb155480 	}
168419b65a69Ssb155480 
168519b65a69Ssb155480 vsw_readmd_exit:
168619b65a69Ssb155480 
168719b65a69Ssb155480 	kmem_free(listp, listsz);
168819b65a69Ssb155480 	(void) md_fini_handle(mdp);
168919b65a69Ssb155480 	return (rv);
169019b65a69Ssb155480 }
169119b65a69Ssb155480 
169219b65a69Ssb155480 /*
169334683adeSsg70180  * Read the initial start-of-day values from the specified MD node.
169434683adeSsg70180  */
169519b65a69Ssb155480 static int
169634683adeSsg70180 vsw_get_initial_md_properties(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
169734683adeSsg70180 {
169834683adeSsg70180 	int		i;
169934683adeSsg70180 	uint64_t 	macaddr = 0;
170034683adeSsg70180 
170134683adeSsg70180 	D1(vswp, "%s: enter", __func__);
170234683adeSsg70180 
170319b65a69Ssb155480 	if (vsw_get_md_physname(vswp, mdp, node, vswp->physname) != 0) {
170419b65a69Ssb155480 		return (1);
170534683adeSsg70180 	}
170634683adeSsg70180 
170734683adeSsg70180 	/* mac address for vswitch device itself */
170834683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
170934683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
171034683adeSsg70180 		    vswp->instance);
171119b65a69Ssb155480 		return (1);
171219b65a69Ssb155480 	}
171334683adeSsg70180 
171419b65a69Ssb155480 	vsw_save_lmacaddr(vswp, macaddr);
171534683adeSsg70180 
1716205eeb1aSlm66018 	if (vsw_get_md_smodes(vswp, mdp, node, vswp->smode, &vswp->smode_num)) {
17171ef0bbb5Snarayan 		DWARN(vswp, "%s: Unable to read %s property from MD, "
17181ef0bbb5Snarayan 		    "defaulting to 'switched' mode",
17191ef0bbb5Snarayan 		    __func__, smode_propname);
172034683adeSsg70180 
172134683adeSsg70180 		for (i = 0; i < NUM_SMODES; i++)
172234683adeSsg70180 			vswp->smode[i] = VSW_LAYER2;
172334683adeSsg70180 
172434683adeSsg70180 		vswp->smode_num = NUM_SMODES;
172534683adeSsg70180 	} else {
172634683adeSsg70180 		ASSERT(vswp->smode_num != 0);
172734683adeSsg70180 	}
172834683adeSsg70180 
1729c1c61f44Ssb155480 	/* read vlan id properties of this vsw instance */
1730c1c61f44Ssb155480 	vsw_vlan_read_ids(vswp, VSW_LOCALDEV, mdp, node, &vswp->pvid,
1731c1c61f44Ssb155480 	    &vswp->vids, &vswp->nvids, &vswp->default_vlan_id);
1732c1c61f44Ssb155480 
1733c1c61f44Ssb155480 	/* read priority-ether-types */
1734f0ca1d9aSsb155480 	vsw_read_pri_eth_types(vswp, mdp, node);
1735f0ca1d9aSsb155480 
173634683adeSsg70180 	D1(vswp, "%s: exit", __func__);
173719b65a69Ssb155480 	return (0);
173834683adeSsg70180 }
173934683adeSsg70180 
174034683adeSsg70180 /*
1741c1c61f44Ssb155480  * Read vlan id properties of the given MD node.
1742c1c61f44Ssb155480  * Arguments:
1743c1c61f44Ssb155480  *   arg:          device argument(vsw device or a port)
1744c1c61f44Ssb155480  *   type:         type of arg; VSW_LOCALDEV(vsw device) or VSW_VNETPORT(port)
1745c1c61f44Ssb155480  *   mdp:          machine description
1746c1c61f44Ssb155480  *   node:         md node cookie
1747c1c61f44Ssb155480  *
1748c1c61f44Ssb155480  * Returns:
1749c1c61f44Ssb155480  *   pvidp:        port-vlan-id of the node
1750c1c61f44Ssb155480  *   vidspp:       list of vlan-ids of the node
1751c1c61f44Ssb155480  *   nvidsp:       # of vlan-ids in the list
1752c1c61f44Ssb155480  *   default_idp:  default-vlan-id of the node(if node is vsw device)
1753c1c61f44Ssb155480  */
1754c1c61f44Ssb155480 static void
1755c1c61f44Ssb155480 vsw_vlan_read_ids(void *arg, int type, md_t *mdp, mde_cookie_t node,
1756c1c61f44Ssb155480 	uint16_t *pvidp, uint16_t **vidspp, uint16_t *nvidsp,
1757c1c61f44Ssb155480 	uint16_t *default_idp)
1758c1c61f44Ssb155480 {
1759c1c61f44Ssb155480 	vsw_t		*vswp;
1760c1c61f44Ssb155480 	vsw_port_t	*portp;
1761c1c61f44Ssb155480 	char		*pvid_propname;
1762c1c61f44Ssb155480 	char		*vid_propname;
1763c1c61f44Ssb155480 	uint_t		nvids = 0;
1764c1c61f44Ssb155480 	uint32_t	vids_size;
1765c1c61f44Ssb155480 	int		rv;
1766c1c61f44Ssb155480 	int		i;
1767c1c61f44Ssb155480 	uint64_t	*data;
1768c1c61f44Ssb155480 	uint64_t	val;
1769c1c61f44Ssb155480 	int		size;
1770c1c61f44Ssb155480 	int		inst;
1771c1c61f44Ssb155480 
1772c1c61f44Ssb155480 	if (type == VSW_LOCALDEV) {
1773c1c61f44Ssb155480 
1774c1c61f44Ssb155480 		vswp = (vsw_t *)arg;
1775c1c61f44Ssb155480 		pvid_propname = vsw_pvid_propname;
1776c1c61f44Ssb155480 		vid_propname = vsw_vid_propname;
1777c1c61f44Ssb155480 		inst = vswp->instance;
1778c1c61f44Ssb155480 
1779c1c61f44Ssb155480 	} else if (type == VSW_VNETPORT) {
1780c1c61f44Ssb155480 
1781c1c61f44Ssb155480 		portp = (vsw_port_t *)arg;
1782c1c61f44Ssb155480 		vswp = portp->p_vswp;
1783c1c61f44Ssb155480 		pvid_propname = port_pvid_propname;
1784c1c61f44Ssb155480 		vid_propname = port_vid_propname;
1785c1c61f44Ssb155480 		inst = portp->p_instance;
1786c1c61f44Ssb155480 
1787c1c61f44Ssb155480 	} else {
1788c1c61f44Ssb155480 		return;
1789c1c61f44Ssb155480 	}
1790c1c61f44Ssb155480 
1791c1c61f44Ssb155480 	if (type == VSW_LOCALDEV && default_idp != NULL) {
1792c1c61f44Ssb155480 		rv = md_get_prop_val(mdp, node, vsw_dvid_propname, &val);
1793c1c61f44Ssb155480 		if (rv != 0) {
1794c1c61f44Ssb155480 			DWARN(vswp, "%s: prop(%s) not found", __func__,
1795c1c61f44Ssb155480 			    vsw_dvid_propname);
1796c1c61f44Ssb155480 
1797c1c61f44Ssb155480 			*default_idp = vsw_default_vlan_id;
1798c1c61f44Ssb155480 		} else {
1799c1c61f44Ssb155480 			*default_idp = val & 0xFFF;
1800c1c61f44Ssb155480 			D2(vswp, "%s: %s(%d): (%d)\n", __func__,
1801c1c61f44Ssb155480 			    vsw_dvid_propname, inst, *default_idp);
1802c1c61f44Ssb155480 		}
1803c1c61f44Ssb155480 	}
1804c1c61f44Ssb155480 
1805c1c61f44Ssb155480 	rv = md_get_prop_val(mdp, node, pvid_propname, &val);
1806c1c61f44Ssb155480 	if (rv != 0) {
1807c1c61f44Ssb155480 		DWARN(vswp, "%s: prop(%s) not found", __func__, pvid_propname);
1808c1c61f44Ssb155480 		*pvidp = vsw_default_vlan_id;
1809c1c61f44Ssb155480 	} else {
1810c1c61f44Ssb155480 
1811c1c61f44Ssb155480 		*pvidp = val & 0xFFF;
1812c1c61f44Ssb155480 		D2(vswp, "%s: %s(%d): (%d)\n", __func__,
1813c1c61f44Ssb155480 		    pvid_propname, inst, *pvidp);
1814c1c61f44Ssb155480 	}
1815c1c61f44Ssb155480 
1816c1c61f44Ssb155480 	rv = md_get_prop_data(mdp, node, vid_propname, (uint8_t **)&data,
1817c1c61f44Ssb155480 	    &size);
1818c1c61f44Ssb155480 	if (rv != 0) {
1819c1c61f44Ssb155480 		D2(vswp, "%s: prop(%s) not found", __func__, vid_propname);
1820c1c61f44Ssb155480 		size = 0;
1821c1c61f44Ssb155480 	} else {
1822c1c61f44Ssb155480 		size /= sizeof (uint64_t);
1823c1c61f44Ssb155480 	}
1824c1c61f44Ssb155480 	nvids = size;
1825c1c61f44Ssb155480 
1826c1c61f44Ssb155480 	if (nvids != 0) {
1827c1c61f44Ssb155480 		D2(vswp, "%s: %s(%d): ", __func__, vid_propname, inst);
1828c1c61f44Ssb155480 		vids_size = sizeof (uint16_t) * nvids;
1829c1c61f44Ssb155480 		*vidspp = kmem_zalloc(vids_size, KM_SLEEP);
1830c1c61f44Ssb155480 		for (i = 0; i < nvids; i++) {
1831c1c61f44Ssb155480 			(*vidspp)[i] = data[i] & 0xFFFF;
1832c1c61f44Ssb155480 			D2(vswp, " %d ", (*vidspp)[i]);
1833c1c61f44Ssb155480 		}
1834c1c61f44Ssb155480 		D2(vswp, "\n");
1835c1c61f44Ssb155480 	}
1836c1c61f44Ssb155480 
1837c1c61f44Ssb155480 	*nvidsp = nvids;
1838c1c61f44Ssb155480 }
1839c1c61f44Ssb155480 
1840c1c61f44Ssb155480 /*
1841f0ca1d9aSsb155480  * This function reads "priority-ether-types" property from md. This property
1842f0ca1d9aSsb155480  * is used to enable support for priority frames. Applications which need
1843f0ca1d9aSsb155480  * guaranteed and timely delivery of certain high priority frames to/from
1844f0ca1d9aSsb155480  * a vnet or vsw within ldoms, should configure this property by providing
1845f0ca1d9aSsb155480  * the ether type(s) for which the priority facility is needed.
1846f0ca1d9aSsb155480  * Normal data frames are delivered over a ldc channel using the descriptor
1847f0ca1d9aSsb155480  * ring mechanism which is constrained by factors such as descriptor ring size,
1848f0ca1d9aSsb155480  * the rate at which the ring is processed at the peer ldc end point, etc.
1849f0ca1d9aSsb155480  * The priority mechanism provides an Out-Of-Band path to send/receive frames
1850f0ca1d9aSsb155480  * as raw pkt data (VIO_PKT_DATA) messages over the channel, avoiding the
1851f0ca1d9aSsb155480  * descriptor ring path and enables a more reliable and timely delivery of
1852f0ca1d9aSsb155480  * frames to the peer.
1853f0ca1d9aSsb155480  */
1854f0ca1d9aSsb155480 static void
1855f0ca1d9aSsb155480 vsw_read_pri_eth_types(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
1856f0ca1d9aSsb155480 {
1857f0ca1d9aSsb155480 	int		rv;
1858f0ca1d9aSsb155480 	uint16_t	*types;
1859f0ca1d9aSsb155480 	uint64_t	*data;
1860f0ca1d9aSsb155480 	int		size;
1861f0ca1d9aSsb155480 	int		i;
1862f0ca1d9aSsb155480 	size_t		mblk_sz;
1863f0ca1d9aSsb155480 
1864f0ca1d9aSsb155480 	rv = md_get_prop_data(mdp, node, pri_types_propname,
1865f0ca1d9aSsb155480 	    (uint8_t **)&data, &size);
1866f0ca1d9aSsb155480 	if (rv != 0) {
1867f0ca1d9aSsb155480 		/*
1868f0ca1d9aSsb155480 		 * Property may not exist if we are running pre-ldoms1.1 f/w.
1869f0ca1d9aSsb155480 		 * Check if 'vsw_pri_eth_type' has been set in that case.
1870f0ca1d9aSsb155480 		 */
1871f0ca1d9aSsb155480 		if (vsw_pri_eth_type != 0) {
1872f0ca1d9aSsb155480 			size = sizeof (vsw_pri_eth_type);
1873f0ca1d9aSsb155480 			data = &vsw_pri_eth_type;
1874f0ca1d9aSsb155480 		} else {
1875f0ca1d9aSsb155480 			D3(vswp, "%s: prop(%s) not found", __func__,
1876f0ca1d9aSsb155480 			    pri_types_propname);
1877f0ca1d9aSsb155480 			size = 0;
1878f0ca1d9aSsb155480 		}
1879f0ca1d9aSsb155480 	}
1880f0ca1d9aSsb155480 
1881f0ca1d9aSsb155480 	if (size == 0) {
1882f0ca1d9aSsb155480 		vswp->pri_num_types = 0;
1883f0ca1d9aSsb155480 		return;
1884f0ca1d9aSsb155480 	}
1885f0ca1d9aSsb155480 
1886f0ca1d9aSsb155480 	/*
1887f0ca1d9aSsb155480 	 * we have some priority-ether-types defined;
1888f0ca1d9aSsb155480 	 * allocate a table of these types and also
1889f0ca1d9aSsb155480 	 * allocate a pool of mblks to transmit these
1890f0ca1d9aSsb155480 	 * priority packets.
1891f0ca1d9aSsb155480 	 */
1892f0ca1d9aSsb155480 	size /= sizeof (uint64_t);
1893f0ca1d9aSsb155480 	vswp->pri_num_types = size;
1894f0ca1d9aSsb155480 	vswp->pri_types = kmem_zalloc(size * sizeof (uint16_t), KM_SLEEP);
1895f0ca1d9aSsb155480 	for (i = 0, types = vswp->pri_types; i < size; i++) {
1896f0ca1d9aSsb155480 		types[i] = data[i] & 0xFFFF;
1897f0ca1d9aSsb155480 	}
1898f0ca1d9aSsb155480 	mblk_sz = (VIO_PKT_DATA_HDRSIZE + ETHERMAX + 7) & ~7;
1899f0ca1d9aSsb155480 	(void) vio_create_mblks(vsw_pri_tx_nmblks, mblk_sz, &vswp->pri_tx_vmp);
1900f0ca1d9aSsb155480 }
1901f0ca1d9aSsb155480 
1902f0ca1d9aSsb155480 /*
190334683adeSsg70180  * Check to see if the relevant properties in the specified node have
190434683adeSsg70180  * changed, and if so take the appropriate action.
190534683adeSsg70180  *
190634683adeSsg70180  * If any of the properties are missing or invalid we don't take
190734683adeSsg70180  * any action, as this function should only be invoked when modifications
190834683adeSsg70180  * have been made to what we assume is a working configuration, which
190934683adeSsg70180  * we leave active.
191034683adeSsg70180  *
191134683adeSsg70180  * Note it is legal for this routine to be invoked even if none of the
191234683adeSsg70180  * properties in the port node within the MD have actually changed.
191334683adeSsg70180  */
191434683adeSsg70180 static void
191534683adeSsg70180 vsw_update_md_prop(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
191634683adeSsg70180 {
191734683adeSsg70180 	char		physname[LIFNAMSIZ];
191834683adeSsg70180 	char		drv[LIFNAMSIZ];
191934683adeSsg70180 	uint_t		ddi_instance;
192034683adeSsg70180 	uint8_t		new_smode[NUM_SMODES];
192134683adeSsg70180 	int		i, smode_num = 0;
192234683adeSsg70180 	uint64_t 	macaddr = 0;
192334683adeSsg70180 	enum		{MD_init = 0x1,
192434683adeSsg70180 				MD_physname = 0x2,
192534683adeSsg70180 				MD_macaddr = 0x4,
1926c1c61f44Ssb155480 				MD_smode = 0x8,
1927c1c61f44Ssb155480 				MD_vlans = 0x10} updated;
192819b65a69Ssb155480 	int		rv;
1929c1c61f44Ssb155480 	uint16_t	pvid;
1930c1c61f44Ssb155480 	uint16_t	*vids;
1931c1c61f44Ssb155480 	uint16_t	nvids;
193234683adeSsg70180 
193334683adeSsg70180 	updated = MD_init;
193434683adeSsg70180 
193534683adeSsg70180 	D1(vswp, "%s: enter", __func__);
193634683adeSsg70180 
193734683adeSsg70180 	/*
193834683adeSsg70180 	 * Check if name of physical device in MD has changed.
193934683adeSsg70180 	 */
194034683adeSsg70180 	if (vsw_get_md_physname(vswp, mdp, node, (char *)&physname) == 0) {
194134683adeSsg70180 		/*
194234683adeSsg70180 		 * Do basic sanity check on new device name/instance,
194334683adeSsg70180 		 * if its non NULL. It is valid for the device name to
194434683adeSsg70180 		 * have changed from a non NULL to a NULL value, i.e.
194534683adeSsg70180 		 * the vsw is being changed to 'routed' mode.
194634683adeSsg70180 		 */
194734683adeSsg70180 		if ((strlen(physname) != 0) &&
194819b65a69Ssb155480 		    (ddi_parse(physname, drv,
194919b65a69Ssb155480 		    &ddi_instance) != DDI_SUCCESS)) {
19501ef0bbb5Snarayan 			cmn_err(CE_WARN, "!vsw%d: physical device %s is not"
195134683adeSsg70180 			    " a valid device name/instance",
195234683adeSsg70180 			    vswp->instance, physname);
195334683adeSsg70180 			goto fail_reconf;
195434683adeSsg70180 		}
195534683adeSsg70180 
195634683adeSsg70180 		if (strcmp(physname, vswp->physname)) {
195734683adeSsg70180 			D2(vswp, "%s: device name changed from %s to %s",
195834683adeSsg70180 			    __func__, vswp->physname, physname);
195934683adeSsg70180 
196034683adeSsg70180 			updated |= MD_physname;
196134683adeSsg70180 		} else {
196234683adeSsg70180 			D2(vswp, "%s: device name unchanged at %s",
196334683adeSsg70180 			    __func__, vswp->physname);
196434683adeSsg70180 		}
196534683adeSsg70180 	} else {
196634683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read name of physical "
196734683adeSsg70180 		    "device from updated MD.", vswp->instance);
196834683adeSsg70180 		goto fail_reconf;
196934683adeSsg70180 	}
197034683adeSsg70180 
197134683adeSsg70180 	/*
197234683adeSsg70180 	 * Check if MAC address has changed.
197334683adeSsg70180 	 */
197434683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
197534683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
197634683adeSsg70180 		    vswp->instance);
197734683adeSsg70180 		goto fail_reconf;
197834683adeSsg70180 	} else {
197919b65a69Ssb155480 		uint64_t maddr = macaddr;
198034683adeSsg70180 		READ_ENTER(&vswp->if_lockrw);
198134683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
198219b65a69Ssb155480 			if (vswp->if_addr.ether_addr_octet[i]
198319b65a69Ssb155480 			    != (macaddr & 0xFF)) {
198434683adeSsg70180 				D2(vswp, "%s: octet[%d] 0x%x != 0x%x",
198534683adeSsg70180 				    __func__, i,
198634683adeSsg70180 				    vswp->if_addr.ether_addr_octet[i],
198734683adeSsg70180 				    (macaddr & 0xFF));
198834683adeSsg70180 				updated |= MD_macaddr;
198919b65a69Ssb155480 				macaddr = maddr;
199034683adeSsg70180 				break;
199134683adeSsg70180 			}
199234683adeSsg70180 			macaddr >>= 8;
199334683adeSsg70180 		}
199434683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
199519b65a69Ssb155480 		if (updated & MD_macaddr) {
199619b65a69Ssb155480 			vsw_save_lmacaddr(vswp, macaddr);
199719b65a69Ssb155480 		}
199834683adeSsg70180 	}
199934683adeSsg70180 
200034683adeSsg70180 	/*
200134683adeSsg70180 	 * Check if switching modes have changed.
200234683adeSsg70180 	 */
200319b65a69Ssb155480 	if (vsw_get_md_smodes(vswp, mdp, node,
200419b65a69Ssb155480 	    new_smode, &smode_num)) {
200534683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read %s property from MD",
200634683adeSsg70180 		    vswp->instance, smode_propname);
200734683adeSsg70180 		goto fail_reconf;
200834683adeSsg70180 	} else {
200934683adeSsg70180 		ASSERT(smode_num != 0);
201034683adeSsg70180 		if (smode_num != vswp->smode_num) {
201134683adeSsg70180 			D2(vswp, "%s: number of modes changed from %d to %d",
201234683adeSsg70180 			    __func__, vswp->smode_num, smode_num);
201334683adeSsg70180 		}
201434683adeSsg70180 
201534683adeSsg70180 		for (i = 0; i < smode_num; i++) {
201634683adeSsg70180 			if (new_smode[i] != vswp->smode[i]) {
201734683adeSsg70180 				D2(vswp, "%s: mode changed from %d to %d",
201834683adeSsg70180 				    __func__, vswp->smode[i], new_smode[i]);
201934683adeSsg70180 				updated |= MD_smode;
202034683adeSsg70180 				break;
202134683adeSsg70180 			}
202234683adeSsg70180 		}
202334683adeSsg70180 	}
202434683adeSsg70180 
2025c1c61f44Ssb155480 	/* Read the vlan ids */
2026c1c61f44Ssb155480 	vsw_vlan_read_ids(vswp, VSW_LOCALDEV, mdp, node, &pvid, &vids,
2027c1c61f44Ssb155480 	    &nvids, NULL);
2028c1c61f44Ssb155480 
2029c1c61f44Ssb155480 	/* Determine if there are any vlan id updates */
2030c1c61f44Ssb155480 	if ((pvid != vswp->pvid) ||		/* pvid changed? */
2031c1c61f44Ssb155480 	    (nvids != vswp->nvids) ||		/* # of vids changed? */
2032c1c61f44Ssb155480 	    ((nvids != 0) && (vswp->nvids != 0) &&	/* vids changed? */
2033c1c61f44Ssb155480 	    bcmp(vids, vswp->vids, sizeof (uint16_t) * nvids))) {
2034c1c61f44Ssb155480 		updated |= MD_vlans;
2035c1c61f44Ssb155480 	}
2036c1c61f44Ssb155480 
203734683adeSsg70180 	/*
203834683adeSsg70180 	 * Now make any changes which are needed...
203934683adeSsg70180 	 */
204034683adeSsg70180 
204134683adeSsg70180 	if (updated & (MD_physname | MD_smode)) {
204234683adeSsg70180 
204334683adeSsg70180 		/*
204419b65a69Ssb155480 		 * Stop any pending timeout to setup switching mode.
204534683adeSsg70180 		 */
204619b65a69Ssb155480 		vsw_stop_switching_timeout(vswp);
204719b65a69Ssb155480 
2048678453a8Sspeer 		/* Cleanup HybridIO */
2049678453a8Sspeer 		vsw_hio_cleanup(vswp);
2050678453a8Sspeer 
205119b65a69Ssb155480 		/*
205219b65a69Ssb155480 		 * Remove unicst, mcst addrs of vsw interface
205319b65a69Ssb155480 		 * and ports from the physdev.
205419b65a69Ssb155480 		 */
205519b65a69Ssb155480 		vsw_unset_addrs(vswp);
205619b65a69Ssb155480 
205719b65a69Ssb155480 		/*
205819b65a69Ssb155480 		 * Stop, detach and close the old device..
205919b65a69Ssb155480 		 */
206019b65a69Ssb155480 		mutex_enter(&vswp->mac_lock);
206119b65a69Ssb155480 
206234683adeSsg70180 		vsw_mac_detach(vswp);
206319b65a69Ssb155480 		vsw_mac_close(vswp);
206419b65a69Ssb155480 
206519b65a69Ssb155480 		mutex_exit(&vswp->mac_lock);
206634683adeSsg70180 
206734683adeSsg70180 		/*
206834683adeSsg70180 		 * Update phys name.
206934683adeSsg70180 		 */
207034683adeSsg70180 		if (updated & MD_physname) {
207134683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: changing from %s to %s",
207234683adeSsg70180 			    vswp->instance, vswp->physname, physname);
207334683adeSsg70180 			(void) strncpy(vswp->physname,
207434683adeSsg70180 			    physname, strlen(physname) + 1);
207534683adeSsg70180 		}
207634683adeSsg70180 
207734683adeSsg70180 		/*
207834683adeSsg70180 		 * Update array with the new switch mode values.
207934683adeSsg70180 		 */
208034683adeSsg70180 		if (updated & MD_smode) {
208134683adeSsg70180 			for (i = 0; i < smode_num; i++)
208234683adeSsg70180 				vswp->smode[i] = new_smode[i];
208334683adeSsg70180 
208434683adeSsg70180 			vswp->smode_num = smode_num;
208534683adeSsg70180 			vswp->smode_idx = 0;
208634683adeSsg70180 		}
208734683adeSsg70180 
208834683adeSsg70180 		/*
208934683adeSsg70180 		 * ..and attach, start the new device.
209034683adeSsg70180 		 */
209119b65a69Ssb155480 		rv = vsw_setup_switching(vswp);
209219b65a69Ssb155480 		if (rv == EAGAIN) {
209319b65a69Ssb155480 			/*
209419b65a69Ssb155480 			 * Unable to setup switching mode.
209519b65a69Ssb155480 			 * As the error is EAGAIN, schedule a timeout to retry
209619b65a69Ssb155480 			 * and return. Programming addresses of ports and
209719b65a69Ssb155480 			 * vsw interface will be done when the timeout handler
209819b65a69Ssb155480 			 * completes successfully.
209919b65a69Ssb155480 			 */
210019b65a69Ssb155480 			mutex_enter(&vswp->swtmout_lock);
210119b65a69Ssb155480 
210219b65a69Ssb155480 			vswp->swtmout_enabled = B_TRUE;
210319b65a69Ssb155480 			vswp->swtmout_id =
210419b65a69Ssb155480 			    timeout(vsw_setup_switching_timeout, vswp,
210519b65a69Ssb155480 			    (vsw_setup_switching_delay *
210619b65a69Ssb155480 			    drv_usectohz(MICROSEC)));
210719b65a69Ssb155480 
210819b65a69Ssb155480 			mutex_exit(&vswp->swtmout_lock);
210919b65a69Ssb155480 
211019b65a69Ssb155480 			return;
211119b65a69Ssb155480 
211219b65a69Ssb155480 		} else if (rv) {
211334683adeSsg70180 			goto fail_update;
211419b65a69Ssb155480 		}
211534683adeSsg70180 
211634683adeSsg70180 		/*
211719b65a69Ssb155480 		 * program unicst, mcst addrs of vsw interface
211819b65a69Ssb155480 		 * and ports in the physdev.
211934683adeSsg70180 		 */
212019b65a69Ssb155480 		vsw_set_addrs(vswp);
212134683adeSsg70180 
2122678453a8Sspeer 		/* Start HIO for ports that have already connected */
2123678453a8Sspeer 		vsw_hio_start_ports(vswp);
2124678453a8Sspeer 
212519b65a69Ssb155480 	} else if (updated & MD_macaddr) {
212619b65a69Ssb155480 		/*
212719b65a69Ssb155480 		 * We enter here if only MD_macaddr is exclusively updated.
212819b65a69Ssb155480 		 * If MD_physname and/or MD_smode are also updated, then
212919b65a69Ssb155480 		 * as part of that, we would have implicitly processed
213019b65a69Ssb155480 		 * MD_macaddr update (above).
213119b65a69Ssb155480 		 */
213234683adeSsg70180 		cmn_err(CE_NOTE, "!vsw%d: changing mac address to 0x%lx",
213334683adeSsg70180 		    vswp->instance, macaddr);
213434683adeSsg70180 
213519b65a69Ssb155480 		READ_ENTER(&vswp->if_lockrw);
213619b65a69Ssb155480 		if (vswp->if_state & VSW_IF_UP) {
213734683adeSsg70180 
21385f94e909Ssg70180 			mutex_enter(&vswp->hw_lock);
213919b65a69Ssb155480 			/*
214019b65a69Ssb155480 			 * Remove old mac address of vsw interface
214119b65a69Ssb155480 			 * from the physdev
214219b65a69Ssb155480 			 */
21435f94e909Ssg70180 			(void) vsw_unset_hw(vswp, NULL, VSW_LOCALDEV);
214419b65a69Ssb155480 			/*
214519b65a69Ssb155480 			 * Program new mac address of vsw interface
214619b65a69Ssb155480 			 * in the physdev
214719b65a69Ssb155480 			 */
214819b65a69Ssb155480 			rv = vsw_set_hw(vswp, NULL, VSW_LOCALDEV);
21495f94e909Ssg70180 			mutex_exit(&vswp->hw_lock);
215019b65a69Ssb155480 			if (rv != 0) {
215119b65a69Ssb155480 				cmn_err(CE_NOTE,
215219b65a69Ssb155480 				    "!vsw%d: failed to program interface "
215319b65a69Ssb155480 				    "unicast address\n", vswp->instance);
215419b65a69Ssb155480 			}
21555f94e909Ssg70180 			/*
215634683adeSsg70180 			 * Notify the MAC layer of the changed address.
215734683adeSsg70180 			 */
215819b65a69Ssb155480 			mac_unicst_update(vswp->if_mh,
215919b65a69Ssb155480 			    (uint8_t *)&vswp->if_addr);
216019b65a69Ssb155480 
216119b65a69Ssb155480 		}
216219b65a69Ssb155480 		RW_EXIT(&vswp->if_lockrw);
216319b65a69Ssb155480 
216434683adeSsg70180 	}
216534683adeSsg70180 
2166c1c61f44Ssb155480 	if (updated & MD_vlans) {
2167c1c61f44Ssb155480 		/* Remove existing vlan ids from the hash table. */
2168c1c61f44Ssb155480 		vsw_vlan_remove_ids(vswp, VSW_LOCALDEV);
2169c1c61f44Ssb155480 
2170c1c61f44Ssb155480 		/* save the new vlan ids */
2171c1c61f44Ssb155480 		vswp->pvid = pvid;
2172c1c61f44Ssb155480 		if (vswp->nvids != 0) {
2173c1c61f44Ssb155480 			kmem_free(vswp->vids, sizeof (uint16_t) * vswp->nvids);
2174c1c61f44Ssb155480 			vswp->nvids = 0;
2175c1c61f44Ssb155480 		}
2176c1c61f44Ssb155480 		if (nvids != 0) {
2177c1c61f44Ssb155480 			vswp->nvids = nvids;
2178c1c61f44Ssb155480 			vswp->vids = vids;
2179c1c61f44Ssb155480 		}
2180c1c61f44Ssb155480 
2181c1c61f44Ssb155480 		/* add these new vlan ids into hash table */
2182c1c61f44Ssb155480 		vsw_vlan_add_ids(vswp, VSW_LOCALDEV);
2183c1c61f44Ssb155480 	} else {
2184c1c61f44Ssb155480 		if (nvids != 0) {
2185c1c61f44Ssb155480 			kmem_free(vids, sizeof (uint16_t) * nvids);
2186c1c61f44Ssb155480 		}
2187c1c61f44Ssb155480 	}
2188c1c61f44Ssb155480 
218934683adeSsg70180 	return;
219034683adeSsg70180 
219134683adeSsg70180 fail_reconf:
219234683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: configuration unchanged", vswp->instance);
219334683adeSsg70180 	return;
219434683adeSsg70180 
219534683adeSsg70180 fail_update:
21961ef0bbb5Snarayan 	cmn_err(CE_WARN, "!vsw%d: re-configuration failed",
219734683adeSsg70180 	    vswp->instance);
219834683adeSsg70180 }
219934683adeSsg70180 
220034683adeSsg70180 /*
2201c1c61f44Ssb155480  * Read the port's md properties.
22021ae08745Sheppo  */
2203c1c61f44Ssb155480 static int
2204c1c61f44Ssb155480 vsw_port_read_props(vsw_port_t *portp, vsw_t *vswp,
2205c1c61f44Ssb155480 	md_t *mdp, mde_cookie_t *node)
22061ae08745Sheppo {
22071ae08745Sheppo 	uint64_t		ldc_id;
22081ae08745Sheppo 	uint8_t			*addrp;
22091ae08745Sheppo 	int			i, addrsz;
22101ae08745Sheppo 	int			num_nodes = 0, nchan = 0;
22111ae08745Sheppo 	int			listsz = 0;
22121ae08745Sheppo 	mde_cookie_t		*listp = NULL;
22131ae08745Sheppo 	struct ether_addr	ea;
22141ae08745Sheppo 	uint64_t		macaddr;
22151ae08745Sheppo 	uint64_t		inst = 0;
2216678453a8Sspeer 	uint64_t		val;
22171ae08745Sheppo 
22181ae08745Sheppo 	if (md_get_prop_val(mdp, *node, id_propname, &inst)) {
22191ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found", __func__,
22201ae08745Sheppo 		    id_propname);
22211ae08745Sheppo 		return (1);
22221ae08745Sheppo 	}
22231ae08745Sheppo 
22241ae08745Sheppo 	/*
22251ae08745Sheppo 	 * Find the channel endpoint node(s) (which should be under this
22261ae08745Sheppo 	 * port node) which contain the channel id(s).
22271ae08745Sheppo 	 */
22281ae08745Sheppo 	if ((num_nodes = md_node_count(mdp)) <= 0) {
22291ae08745Sheppo 		DERR(vswp, "%s: invalid number of nodes found (%d)",
22301ae08745Sheppo 		    __func__, num_nodes);
22311ae08745Sheppo 		return (1);
22321ae08745Sheppo 	}
22331ae08745Sheppo 
223434683adeSsg70180 	D2(vswp, "%s: %d nodes found", __func__, num_nodes);
223534683adeSsg70180 
22361ae08745Sheppo 	/* allocate enough space for node list */
22371ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
22381ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
22391ae08745Sheppo 
2240205eeb1aSlm66018 	nchan = md_scan_dag(mdp, *node, md_find_name(mdp, chan_propname),
22411ae08745Sheppo 	    md_find_name(mdp, "fwd"), listp);
22421ae08745Sheppo 
22431ae08745Sheppo 	if (nchan <= 0) {
22441ae08745Sheppo 		DWARN(vswp, "%s: no %s nodes found", __func__, chan_propname);
22451ae08745Sheppo 		kmem_free(listp, listsz);
22461ae08745Sheppo 		return (1);
22471ae08745Sheppo 	}
22481ae08745Sheppo 
22491ae08745Sheppo 	D2(vswp, "%s: %d %s nodes found", __func__, nchan, chan_propname);
22501ae08745Sheppo 
22511ae08745Sheppo 	/* use property from first node found */
22521ae08745Sheppo 	if (md_get_prop_val(mdp, listp[0], id_propname, &ldc_id)) {
22531ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found\n", __func__,
22541ae08745Sheppo 		    id_propname);
22551ae08745Sheppo 		kmem_free(listp, listsz);
22561ae08745Sheppo 		return (1);
22571ae08745Sheppo 	}
22581ae08745Sheppo 
22591ae08745Sheppo 	/* don't need list any more */
22601ae08745Sheppo 	kmem_free(listp, listsz);
22611ae08745Sheppo 
22621ae08745Sheppo 	D2(vswp, "%s: ldc_id 0x%llx", __func__, ldc_id);
22631ae08745Sheppo 
22641ae08745Sheppo 	/* read mac-address property */
22651ae08745Sheppo 	if (md_get_prop_data(mdp, *node, remaddr_propname,
22661ae08745Sheppo 	    &addrp, &addrsz)) {
22671ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found",
22681ae08745Sheppo 		    __func__, remaddr_propname);
22691ae08745Sheppo 		return (1);
22701ae08745Sheppo 	}
22711ae08745Sheppo 
22721ae08745Sheppo 	if (addrsz < ETHERADDRL) {
22731ae08745Sheppo 		DWARN(vswp, "%s: invalid address size", __func__);
22741ae08745Sheppo 		return (1);
22751ae08745Sheppo 	}
22761ae08745Sheppo 
22771ae08745Sheppo 	macaddr = *((uint64_t *)addrp);
22781ae08745Sheppo 	D2(vswp, "%s: remote mac address 0x%llx", __func__, macaddr);
22791ae08745Sheppo 
22801ae08745Sheppo 	for (i = ETHERADDRL - 1; i >= 0; i--) {
22811ae08745Sheppo 		ea.ether_addr_octet[i] = macaddr & 0xFF;
22821ae08745Sheppo 		macaddr >>= 8;
22831ae08745Sheppo 	}
22841ae08745Sheppo 
2285c1c61f44Ssb155480 	/* now update all properties into the port */
2286c1c61f44Ssb155480 	portp->p_vswp = vswp;
2287c1c61f44Ssb155480 	portp->p_instance = inst;
2288c1c61f44Ssb155480 	portp->addr_set = VSW_ADDR_UNSET;
2289c1c61f44Ssb155480 	ether_copy(&ea, &portp->p_macaddr);
2290c1c61f44Ssb155480 	if (nchan > VSW_PORT_MAX_LDCS) {
2291c1c61f44Ssb155480 		D2(vswp, "%s: using first of %d ldc ids",
2292c1c61f44Ssb155480 		    __func__, nchan);
2293c1c61f44Ssb155480 		nchan = VSW_PORT_MAX_LDCS;
2294c1c61f44Ssb155480 	}
2295c1c61f44Ssb155480 	portp->num_ldcs = nchan;
2296c1c61f44Ssb155480 	portp->ldc_ids =
2297c1c61f44Ssb155480 	    kmem_zalloc(sizeof (uint64_t) * nchan, KM_SLEEP);
2298c1c61f44Ssb155480 	bcopy(&ldc_id, (portp->ldc_ids), sizeof (uint64_t) * nchan);
2299c1c61f44Ssb155480 
2300c1c61f44Ssb155480 	/* read vlan id properties of this port node */
2301c1c61f44Ssb155480 	vsw_vlan_read_ids(portp, VSW_VNETPORT, mdp, *node, &portp->pvid,
2302c1c61f44Ssb155480 	    &portp->vids, &portp->nvids, NULL);
2303c1c61f44Ssb155480 
2304678453a8Sspeer 	/* Check if hybrid property is present */
2305678453a8Sspeer 	if (md_get_prop_val(mdp, *node, hybrid_propname, &val) == 0) {
2306678453a8Sspeer 		D1(vswp, "%s: prop(%s) found\n", __func__, hybrid_propname);
2307678453a8Sspeer 		portp->p_hio_enabled = B_TRUE;
2308678453a8Sspeer 	} else {
2309678453a8Sspeer 		portp->p_hio_enabled = B_FALSE;
2310678453a8Sspeer 	}
2311678453a8Sspeer 	/*
2312678453a8Sspeer 	 * Port hio capability determined after version
2313678453a8Sspeer 	 * negotiation, i.e., when we know the peer is HybridIO capable.
2314678453a8Sspeer 	 */
2315678453a8Sspeer 	portp->p_hio_capable = B_FALSE;
2316c1c61f44Ssb155480 	return (0);
2317c1c61f44Ssb155480 }
2318c1c61f44Ssb155480 
2319c1c61f44Ssb155480 /*
2320c1c61f44Ssb155480  * Add a new port to the system.
2321c1c61f44Ssb155480  *
2322c1c61f44Ssb155480  * Returns 0 on success, 1 on failure.
2323c1c61f44Ssb155480  */
2324c1c61f44Ssb155480 int
2325c1c61f44Ssb155480 vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node)
2326c1c61f44Ssb155480 {
2327c1c61f44Ssb155480 	vsw_port_t	*portp;
2328c1c61f44Ssb155480 	int		rv;
2329c1c61f44Ssb155480 
2330c1c61f44Ssb155480 	portp = kmem_zalloc(sizeof (vsw_port_t), KM_SLEEP);
2331c1c61f44Ssb155480 
2332c1c61f44Ssb155480 	rv = vsw_port_read_props(portp, vswp, mdp, node);
2333c1c61f44Ssb155480 	if (rv != 0) {
2334c1c61f44Ssb155480 		kmem_free(portp, sizeof (*portp));
2335c1c61f44Ssb155480 		return (1);
2336c1c61f44Ssb155480 	}
2337c1c61f44Ssb155480 
2338c1c61f44Ssb155480 	rv = vsw_port_attach(portp);
2339c1c61f44Ssb155480 	if (rv != 0) {
23401ae08745Sheppo 		DERR(vswp, "%s: failed to attach port", __func__);
23411ae08745Sheppo 		return (1);
23421ae08745Sheppo 	}
23431ae08745Sheppo 
2344c1c61f44Ssb155480 	return (0);
2345c1c61f44Ssb155480 }
23461ae08745Sheppo 
2347c1c61f44Ssb155480 static int
2348c1c61f44Ssb155480 vsw_port_update(vsw_t *vswp, md_t *curr_mdp, mde_cookie_t curr_mdex,
2349c1c61f44Ssb155480 	md_t *prev_mdp, mde_cookie_t prev_mdex)
2350c1c61f44Ssb155480 {
2351c1c61f44Ssb155480 	uint64_t	cport_num;
2352c1c61f44Ssb155480 	uint64_t	pport_num;
2353c1c61f44Ssb155480 	vsw_port_list_t	*plistp;
2354c1c61f44Ssb155480 	vsw_port_t	*portp;
2355c1c61f44Ssb155480 	boolean_t	updated_vlans = B_FALSE;
2356c1c61f44Ssb155480 	uint16_t	pvid;
2357c1c61f44Ssb155480 	uint16_t	*vids;
2358c1c61f44Ssb155480 	uint16_t	nvids;
2359678453a8Sspeer 	uint64_t	val;
2360678453a8Sspeer 	boolean_t	hio_enabled = B_FALSE;
2361c1c61f44Ssb155480 
2362c1c61f44Ssb155480 	/*
2363c1c61f44Ssb155480 	 * For now, we get port updates only if vlan ids changed.
2364c1c61f44Ssb155480 	 * We read the port num and do some sanity check.
2365c1c61f44Ssb155480 	 */
2366c1c61f44Ssb155480 	if (md_get_prop_val(curr_mdp, curr_mdex, id_propname, &cport_num)) {
2367c1c61f44Ssb155480 		return (1);
2368c1c61f44Ssb155480 	}
2369c1c61f44Ssb155480 
2370c1c61f44Ssb155480 	if (md_get_prop_val(prev_mdp, prev_mdex, id_propname, &pport_num)) {
2371c1c61f44Ssb155480 		return (1);
2372c1c61f44Ssb155480 	}
2373c1c61f44Ssb155480 	if (cport_num != pport_num)
2374c1c61f44Ssb155480 		return (1);
2375c1c61f44Ssb155480 
2376c1c61f44Ssb155480 	plistp = &(vswp->plist);
2377c1c61f44Ssb155480 
2378c1c61f44Ssb155480 	READ_ENTER(&plistp->lockrw);
2379c1c61f44Ssb155480 
2380c1c61f44Ssb155480 	portp = vsw_lookup_port(vswp, cport_num);
2381c1c61f44Ssb155480 	if (portp == NULL) {
2382c1c61f44Ssb155480 		RW_EXIT(&plistp->lockrw);
2383c1c61f44Ssb155480 		return (1);
2384c1c61f44Ssb155480 	}
2385c1c61f44Ssb155480 
2386c1c61f44Ssb155480 	/* Read the vlan ids */
2387c1c61f44Ssb155480 	vsw_vlan_read_ids(portp, VSW_VNETPORT, curr_mdp, curr_mdex, &pvid,
2388c1c61f44Ssb155480 	    &vids, &nvids, NULL);
2389c1c61f44Ssb155480 
2390c1c61f44Ssb155480 	/* Determine if there are any vlan id updates */
2391c1c61f44Ssb155480 	if ((pvid != portp->pvid) ||		/* pvid changed? */
2392c1c61f44Ssb155480 	    (nvids != portp->nvids) ||		/* # of vids changed? */
2393c1c61f44Ssb155480 	    ((nvids != 0) && (portp->nvids != 0) &&	/* vids changed? */
2394c1c61f44Ssb155480 	    bcmp(vids, portp->vids, sizeof (uint16_t) * nvids))) {
2395c1c61f44Ssb155480 		updated_vlans = B_TRUE;
2396c1c61f44Ssb155480 	}
2397c1c61f44Ssb155480 
2398678453a8Sspeer 	if (updated_vlans == B_TRUE) {
2399c1c61f44Ssb155480 
2400c1c61f44Ssb155480 		/* Remove existing vlan ids from the hash table. */
2401c1c61f44Ssb155480 		vsw_vlan_remove_ids(portp, VSW_VNETPORT);
2402c1c61f44Ssb155480 
2403c1c61f44Ssb155480 		/* save the new vlan ids */
2404c1c61f44Ssb155480 		portp->pvid = pvid;
2405c1c61f44Ssb155480 		if (portp->nvids != 0) {
2406678453a8Sspeer 			kmem_free(portp->vids,
2407678453a8Sspeer 			    sizeof (uint16_t) * portp->nvids);
2408c1c61f44Ssb155480 			portp->nvids = 0;
2409c1c61f44Ssb155480 		}
2410c1c61f44Ssb155480 		if (nvids != 0) {
2411678453a8Sspeer 			portp->vids = kmem_zalloc(sizeof (uint16_t) *
2412678453a8Sspeer 			    nvids, KM_SLEEP);
2413c1c61f44Ssb155480 			bcopy(vids, portp->vids, sizeof (uint16_t) * nvids);
2414c1c61f44Ssb155480 			portp->nvids = nvids;
2415c1c61f44Ssb155480 			kmem_free(vids, sizeof (uint16_t) * nvids);
2416c1c61f44Ssb155480 		}
2417c1c61f44Ssb155480 
2418c1c61f44Ssb155480 		/* add these new vlan ids into hash table */
2419c1c61f44Ssb155480 		vsw_vlan_add_ids(portp, VSW_VNETPORT);
2420c1c61f44Ssb155480 
2421c1c61f44Ssb155480 		/* reset the port if it is vlan unaware (ver < 1.3) */
2422c1c61f44Ssb155480 		vsw_vlan_unaware_port_reset(portp);
2423678453a8Sspeer 	}
2424678453a8Sspeer 
2425678453a8Sspeer 	/* Check if hybrid property is present */
2426678453a8Sspeer 	if (md_get_prop_val(curr_mdp, curr_mdex, hybrid_propname, &val) == 0) {
2427678453a8Sspeer 		D1(vswp, "%s: prop(%s) found\n", __func__, hybrid_propname);
2428678453a8Sspeer 		hio_enabled = B_TRUE;
2429678453a8Sspeer 	}
2430678453a8Sspeer 
2431678453a8Sspeer 	if (portp->p_hio_enabled != hio_enabled) {
2432678453a8Sspeer 		vsw_hio_port_update(portp, hio_enabled);
2433678453a8Sspeer 	}
2434c1c61f44Ssb155480 
2435c1c61f44Ssb155480 	RW_EXIT(&plistp->lockrw);
24361ae08745Sheppo 
24371ae08745Sheppo 	return (0);
24381ae08745Sheppo }
24391ae08745Sheppo 
24401ae08745Sheppo /*
244106db247cSraghuram  * vsw_mac_rx -- A common function to send packets to the interface.
244206db247cSraghuram  * By default this function check if the interface is UP or not, the
244306db247cSraghuram  * rest of the behaviour depends on the flags as below:
24441ae08745Sheppo  *
244506db247cSraghuram  *	VSW_MACRX_PROMISC -- Check if the promisc mode set or not.
244606db247cSraghuram  *	VSW_MACRX_COPYMSG -- Make a copy of the message(s).
244706db247cSraghuram  *	VSW_MACRX_FREEMSG -- Free if the messages cannot be sent up the stack.
24481ae08745Sheppo  */
24491ae08745Sheppo void
2450f0ca1d9aSsb155480 vsw_mac_rx(vsw_t *vswp, mac_resource_handle_t mrh,
2451f0ca1d9aSsb155480     mblk_t *mp, vsw_macrx_flags_t flags)
24521ae08745Sheppo {
2453c1c61f44Ssb155480 	mblk_t		*mpt;
2454c1c61f44Ssb155480 
245506db247cSraghuram 	D1(vswp, "%s:enter\n", __func__);
24561ae08745Sheppo 	READ_ENTER(&vswp->if_lockrw);
245706db247cSraghuram 	/* Check if the interface is up */
245806db247cSraghuram 	if (!(vswp->if_state & VSW_IF_UP)) {
24591ae08745Sheppo 		RW_EXIT(&vswp->if_lockrw);
246006db247cSraghuram 		/* Free messages only if FREEMSG flag specified */
246106db247cSraghuram 		if (flags & VSW_MACRX_FREEMSG) {
246206db247cSraghuram 			freemsgchain(mp);
246306db247cSraghuram 		}
246406db247cSraghuram 		D1(vswp, "%s:exit\n", __func__);
246506db247cSraghuram 		return;
246606db247cSraghuram 	}
246706db247cSraghuram 	/*
246806db247cSraghuram 	 * If PROMISC flag is passed, then check if
246906db247cSraghuram 	 * the interface is in the PROMISC mode.
247006db247cSraghuram 	 * If not, drop the messages.
247106db247cSraghuram 	 */
247206db247cSraghuram 	if (flags & VSW_MACRX_PROMISC) {
247306db247cSraghuram 		if (!(vswp->if_state & VSW_IF_PROMISC)) {
247406db247cSraghuram 			RW_EXIT(&vswp->if_lockrw);
247506db247cSraghuram 			/* Free messages only if FREEMSG flag specified */
247606db247cSraghuram 			if (flags & VSW_MACRX_FREEMSG) {
247706db247cSraghuram 				freemsgchain(mp);
247806db247cSraghuram 			}
247906db247cSraghuram 			D1(vswp, "%s:exit\n", __func__);
248006db247cSraghuram 			return;
248106db247cSraghuram 		}
248206db247cSraghuram 	}
248306db247cSraghuram 	RW_EXIT(&vswp->if_lockrw);
248406db247cSraghuram 	/*
248506db247cSraghuram 	 * If COPYMSG flag is passed, then make a copy
248606db247cSraghuram 	 * of the message chain and send up the copy.
248706db247cSraghuram 	 */
248806db247cSraghuram 	if (flags & VSW_MACRX_COPYMSG) {
248906db247cSraghuram 		mp = copymsgchain(mp);
2490f0ca1d9aSsb155480 		if (mp == NULL) {
249106db247cSraghuram 			D1(vswp, "%s:exit\n", __func__);
249206db247cSraghuram 			return;
249306db247cSraghuram 		}
249406db247cSraghuram 	}
249506db247cSraghuram 
2496f0ca1d9aSsb155480 	D2(vswp, "%s: sending up stack", __func__);
2497c1c61f44Ssb155480 
2498c1c61f44Ssb155480 	mpt = NULL;
2499c1c61f44Ssb155480 	(void) vsw_vlan_frame_untag(vswp, VSW_LOCALDEV, &mp, &mpt);
2500c1c61f44Ssb155480 	if (mp != NULL) {
2501ba2e4443Sseb 		mac_rx(vswp->if_mh, mrh, mp);
2502c1c61f44Ssb155480 	}
250306db247cSraghuram 	D1(vswp, "%s:exit\n", __func__);
25041ae08745Sheppo }
25051ae08745Sheppo 
250606db247cSraghuram /* copy mac address of vsw into soft state structure */
25071ae08745Sheppo static void
250806db247cSraghuram vsw_save_lmacaddr(vsw_t *vswp, uint64_t macaddr)
25091ae08745Sheppo {
25101ae08745Sheppo 	int	i;
25111ae08745Sheppo 
251206db247cSraghuram 	WRITE_ENTER(&vswp->if_lockrw);
251306db247cSraghuram 	for (i = ETHERADDRL - 1; i >= 0; i--) {
251406db247cSraghuram 		vswp->if_addr.ether_addr_octet[i] = macaddr & 0xFF;
251506db247cSraghuram 		macaddr >>= 8;
25161ae08745Sheppo 	}
251706db247cSraghuram 	RW_EXIT(&vswp->if_lockrw);
25181ae08745Sheppo }
2519