xref: /titanic_53/usr/src/uts/sun4v/io/vsw.c (revision 808f26a819b6259a3340d8d53074a2f1635315cb)
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 #include <sys/types.h>
281ae08745Sheppo #include <sys/errno.h>
291ae08745Sheppo #include <sys/debug.h>
301ae08745Sheppo #include <sys/time.h>
311ae08745Sheppo #include <sys/sysmacros.h>
321ae08745Sheppo #include <sys/systm.h>
331ae08745Sheppo #include <sys/user.h>
341ae08745Sheppo #include <sys/stropts.h>
351ae08745Sheppo #include <sys/stream.h>
361ae08745Sheppo #include <sys/strlog.h>
371ae08745Sheppo #include <sys/strsubr.h>
381ae08745Sheppo #include <sys/cmn_err.h>
391ae08745Sheppo #include <sys/cpu.h>
401ae08745Sheppo #include <sys/kmem.h>
411ae08745Sheppo #include <sys/conf.h>
421ae08745Sheppo #include <sys/ddi.h>
431ae08745Sheppo #include <sys/sunddi.h>
441ae08745Sheppo #include <sys/ksynch.h>
451ae08745Sheppo #include <sys/stat.h>
461ae08745Sheppo #include <sys/kstat.h>
471ae08745Sheppo #include <sys/vtrace.h>
481ae08745Sheppo #include <sys/strsun.h>
491ae08745Sheppo #include <sys/dlpi.h>
501ae08745Sheppo #include <sys/ethernet.h>
511ae08745Sheppo #include <net/if.h>
521ae08745Sheppo #include <sys/varargs.h>
531ae08745Sheppo #include <sys/machsystm.h>
541ae08745Sheppo #include <sys/modctl.h>
551ae08745Sheppo #include <sys/modhash.h>
56da14cebeSEric Cheng #include <sys/mac_provider.h>
57ba2e4443Sseb #include <sys/mac_ether.h>
581ae08745Sheppo #include <sys/taskq.h>
591ae08745Sheppo #include <sys/note.h>
601ae08745Sheppo #include <sys/mach_descrip.h>
61da14cebeSEric Cheng #include <sys/mac_provider.h>
621ae08745Sheppo #include <sys/mdeg.h>
631ae08745Sheppo #include <sys/ldc.h>
641ae08745Sheppo #include <sys/vsw_fdb.h>
651ae08745Sheppo #include <sys/vsw.h>
661ae08745Sheppo #include <sys/vio_mailbox.h>
671ae08745Sheppo #include <sys/vnet_mailbox.h>
681ae08745Sheppo #include <sys/vnet_common.h>
69d10e4ef2Snarayan #include <sys/vio_util.h>
70d10e4ef2Snarayan #include <sys/sdt.h>
7119b65a69Ssb155480 #include <sys/atomic.h>
7206db247cSraghuram #include <sys/callb.h>
73c1c61f44Ssb155480 #include <sys/vlan.h>
741ae08745Sheppo 
751ae08745Sheppo /*
761ae08745Sheppo  * Function prototypes.
771ae08745Sheppo  */
781ae08745Sheppo static	int vsw_attach(dev_info_t *, ddi_attach_cmd_t);
791ae08745Sheppo static	int vsw_detach(dev_info_t *, ddi_detach_cmd_t);
8034683adeSsg70180 static	int vsw_get_md_physname(vsw_t *, md_t *, mde_cookie_t, char *);
81da14cebeSEric Cheng static	int vsw_get_md_smodes(vsw_t *, md_t *, mde_cookie_t, uint8_t *);
821ae08745Sheppo 
831ae08745Sheppo /* MDEG routines */
8434683adeSsg70180 static	int vsw_mdeg_register(vsw_t *vswp);
851ae08745Sheppo static	void vsw_mdeg_unregister(vsw_t *vswp);
861ae08745Sheppo static	int vsw_mdeg_cb(void *cb_argp, mdeg_result_t *);
8734683adeSsg70180 static	int vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *);
8819b65a69Ssb155480 static	int vsw_get_initial_md_properties(vsw_t *vswp, md_t *, mde_cookie_t);
89c1c61f44Ssb155480 static	int vsw_read_mdprops(vsw_t *vswp);
90c1c61f44Ssb155480 static	void vsw_vlan_read_ids(void *arg, int type, md_t *mdp,
91da14cebeSEric Cheng 	mde_cookie_t node, uint16_t *pvidp, vsw_vlanid_t **vidspp,
92c1c61f44Ssb155480 	uint16_t *nvidsp, uint16_t *default_idp);
93c1c61f44Ssb155480 static	int vsw_port_read_props(vsw_port_t *portp, vsw_t *vswp,
94c1c61f44Ssb155480 	md_t *mdp, mde_cookie_t *node);
95f0ca1d9aSsb155480 static	void vsw_read_pri_eth_types(vsw_t *vswp, md_t *mdp,
96f0ca1d9aSsb155480 	mde_cookie_t node);
977b1f684aSSriharsha Basavapatna static	void vsw_mtu_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
987b1f684aSSriharsha Basavapatna 	uint32_t *mtu);
997b1f684aSSriharsha Basavapatna static	int vsw_mtu_update(vsw_t *vswp, uint32_t mtu);
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);
102da14cebeSEric Cheng static boolean_t vsw_cmp_vids(vsw_vlanid_t *vids1,
103da14cebeSEric Cheng 	vsw_vlanid_t *vids2, int nvids);
1041ae08745Sheppo 
10506db247cSraghuram /* Mac driver related routines */
10606db247cSraghuram static int vsw_mac_register(vsw_t *);
10706db247cSraghuram static int vsw_mac_unregister(vsw_t *);
10806db247cSraghuram static int vsw_m_stat(void *, uint_t, uint64_t *);
10906db247cSraghuram static void vsw_m_stop(void *arg);
11006db247cSraghuram static int vsw_m_start(void *arg);
11106db247cSraghuram static int vsw_m_unicst(void *arg, const uint8_t *);
11206db247cSraghuram static int vsw_m_multicst(void *arg, boolean_t, const uint8_t *);
11306db247cSraghuram static int vsw_m_promisc(void *arg, boolean_t);
11406db247cSraghuram static mblk_t *vsw_m_tx(void *arg, mblk_t *);
115f0ca1d9aSsb155480 void vsw_mac_rx(vsw_t *vswp, mac_resource_handle_t mrh,
116f0ca1d9aSsb155480     mblk_t *mp, vsw_macrx_flags_t flags);
1171ae08745Sheppo 
11806db247cSraghuram /*
11906db247cSraghuram  * Functions imported from other files.
12006db247cSraghuram  */
121*808f26a8SSriharsha Basavapatna extern void vsw_setup_switching_thread(void *arg);
122*808f26a8SSriharsha Basavapatna extern int vsw_setup_switching_start(vsw_t *vswp);
123*808f26a8SSriharsha Basavapatna extern void vsw_setup_switching_stop(vsw_t *vswp);
12406db247cSraghuram extern int vsw_setup_switching(vsw_t *);
1257a327842Swentaoy extern void vsw_switch_frame_nop(vsw_t *vswp, mblk_t *mp, int caller,
1267a327842Swentaoy     vsw_port_t *port, mac_resource_handle_t mrh);
12706db247cSraghuram extern int vsw_add_mcst(vsw_t *, uint8_t, uint64_t, void *);
12806db247cSraghuram extern int vsw_del_mcst(vsw_t *, uint8_t, uint64_t, void *);
12906db247cSraghuram extern void vsw_del_mcst_vsw(vsw_t *);
13006db247cSraghuram extern mcst_addr_t *vsw_del_addr(uint8_t devtype, void *arg, uint64_t addr);
13106db247cSraghuram extern int vsw_detach_ports(vsw_t *vswp);
13206db247cSraghuram extern int vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node);
13306db247cSraghuram extern int vsw_port_detach(vsw_t *vswp, int p_instance);
134c1c61f44Ssb155480 static int vsw_port_update(vsw_t *vswp, md_t *curr_mdp, mde_cookie_t curr_mdex,
135c1c61f44Ssb155480 	md_t *prev_mdp, mde_cookie_t prev_mdex);
136c1c61f44Ssb155480 extern	int vsw_port_attach(vsw_port_t *port);
13706db247cSraghuram extern vsw_port_t *vsw_lookup_port(vsw_t *vswp, int p_instance);
13806db247cSraghuram extern int vsw_mac_open(vsw_t *vswp);
13906db247cSraghuram extern void vsw_mac_close(vsw_t *vswp);
140da14cebeSEric Cheng extern void vsw_mac_cleanup_ports(vsw_t *vswp);
14106db247cSraghuram extern void vsw_unset_addrs(vsw_t *vswp);
14271bdf936SWENTAO YANG extern void vsw_setup_layer2_post_process(vsw_t *vswp);
143c1c61f44Ssb155480 extern void vsw_create_vlans(void *arg, int type);
144c1c61f44Ssb155480 extern void vsw_destroy_vlans(void *arg, int type);
145c1c61f44Ssb155480 extern void vsw_vlan_add_ids(void *arg, int type);
146c1c61f44Ssb155480 extern void vsw_vlan_remove_ids(void *arg, int type);
147c1c61f44Ssb155480 extern void vsw_vlan_unaware_port_reset(vsw_port_t *portp);
148c1c61f44Ssb155480 extern uint32_t vsw_vlan_frame_untag(void *arg, int type, mblk_t **np,
149c1c61f44Ssb155480 	mblk_t **npt);
150c1c61f44Ssb155480 extern mblk_t *vsw_vlan_frame_pretag(void *arg, int type, mblk_t *mp);
151678453a8Sspeer extern void vsw_hio_cleanup(vsw_t *vswp);
152da14cebeSEric Cheng extern void vsw_hio_start_ports(vsw_t *vswp);
153da14cebeSEric Cheng extern void vsw_hio_port_update(vsw_port_t *portp, boolean_t hio_enabled);
154da14cebeSEric Cheng extern int vsw_mac_multicast_add(vsw_t *, vsw_port_t *, mcst_addr_t *, int);
155da14cebeSEric Cheng extern void vsw_mac_multicast_remove(vsw_t *, vsw_port_t *, mcst_addr_t *, int);
156da14cebeSEric Cheng extern void vsw_mac_port_reconfig_vlans(vsw_port_t *portp, uint16_t new_pvid,
157da14cebeSEric Cheng     vsw_vlanid_t *new_vids, int new_nvids);
158da14cebeSEric Cheng extern int vsw_mac_client_init(vsw_t *vswp, vsw_port_t *port, int type);
159da14cebeSEric Cheng extern void vsw_mac_client_cleanup(vsw_t *vswp, vsw_port_t *port, int type);
160da14cebeSEric Cheng extern void vsw_if_mac_reconfig(vsw_t *vswp, boolean_t update_vlans,
161da14cebeSEric Cheng     uint16_t new_pvid, vsw_vlanid_t *new_vids, int new_nvids);
1627b1f684aSSriharsha Basavapatna extern void vsw_reset_ports(vsw_t *vswp);
1637b1f684aSSriharsha Basavapatna extern void vsw_port_reset(vsw_port_t *portp);
164678453a8Sspeer void vsw_hio_port_update(vsw_port_t *portp, boolean_t hio_enabled);
16506db247cSraghuram 
16606db247cSraghuram /*
16706db247cSraghuram  * Internal tunables.
16806db247cSraghuram  */
169445b4c2eSsb155480 int	vsw_num_handshakes = VNET_NUM_HANDSHAKES; /* # of handshake attempts */
1701ae08745Sheppo int	vsw_wretries = 100;		/* # of write attempts */
171d10e4ef2Snarayan int	vsw_desc_delay = 0;		/* delay in us */
172d10e4ef2Snarayan int	vsw_read_attempts = 5;		/* # of reads of descriptor */
17319b65a69Ssb155480 int	vsw_setup_switching_delay = 3;	/* setup sw timeout interval in sec */
1740e8b4070Ssb155480 int	vsw_mac_open_retries = 300;	/* max # of mac_open() retries */
1750e8b4070Ssb155480 					/* 300*3 = 900sec(15min) of max tmout */
17606db247cSraghuram int	vsw_ldc_tx_delay = 5;		/* delay(ticks) for tx retries */
17706db247cSraghuram int	vsw_ldc_tx_retries = 10;	/* # of ldc tx retries */
17806db247cSraghuram boolean_t vsw_ldc_rxthr_enabled = B_TRUE;	/* LDC Rx thread enabled */
17906db247cSraghuram boolean_t vsw_ldc_txthr_enabled = B_TRUE;	/* LDC Tx thread enabled */
180d10e4ef2Snarayan 
181c1c61f44Ssb155480 uint32_t	vsw_fdb_nchains = 8;	/* # of chains in fdb hash table */
182c1c61f44Ssb155480 uint32_t	vsw_vlan_nchains = 4;	/* # of chains in vlan id hash table */
183c1c61f44Ssb155480 uint32_t	vsw_ethermtu = 1500;	/* mtu of the device */
184c1c61f44Ssb155480 
185c1c61f44Ssb155480 /* delay in usec to wait for all references on a fdb entry to be dropped */
186c1c61f44Ssb155480 uint32_t vsw_fdbe_refcnt_delay = 10;
187c1c61f44Ssb155480 
188c1c61f44Ssb155480 /*
189c1c61f44Ssb155480  * Default vlan id. This is only used internally when the "default-vlan-id"
190c1c61f44Ssb155480  * property is not present in the MD device node. Therefore, this should not be
191c1c61f44Ssb155480  * used as a tunable; if this value is changed, the corresponding variable
192c1c61f44Ssb155480  * should be updated to the same value in all vnets connected to this vsw.
193c1c61f44Ssb155480  */
194c1c61f44Ssb155480 uint16_t	vsw_default_vlan_id = 1;
195c1c61f44Ssb155480 
196f0ca1d9aSsb155480 /*
197f0ca1d9aSsb155480  * Workaround for a version handshake bug in obp's vnet.
198f0ca1d9aSsb155480  * If vsw initiates version negotiation starting from the highest version,
199f0ca1d9aSsb155480  * obp sends a nack and terminates version handshake. To workaround
200f0ca1d9aSsb155480  * this, we do not initiate version handshake when the channel comes up.
201f0ca1d9aSsb155480  * Instead, we wait for the peer to send its version info msg and go through
202f0ca1d9aSsb155480  * the version protocol exchange. If we successfully negotiate a version,
203f0ca1d9aSsb155480  * before sending the ack, we send our version info msg to the peer
204f0ca1d9aSsb155480  * using the <major,minor> version that we are about to ack.
205f0ca1d9aSsb155480  */
206f0ca1d9aSsb155480 boolean_t vsw_obp_ver_proto_workaround = B_TRUE;
207f0ca1d9aSsb155480 
208f0ca1d9aSsb155480 /*
209f0ca1d9aSsb155480  * In the absence of "priority-ether-types" property in MD, the following
210f0ca1d9aSsb155480  * internal tunable can be set to specify a single priority ethertype.
211f0ca1d9aSsb155480  */
212f0ca1d9aSsb155480 uint64_t vsw_pri_eth_type = 0;
213f0ca1d9aSsb155480 
214f0ca1d9aSsb155480 /*
215f0ca1d9aSsb155480  * Number of transmit priority buffers that are preallocated per device.
216f0ca1d9aSsb155480  * This number is chosen to be a small value to throttle transmission
217f0ca1d9aSsb155480  * of priority packets. Note: Must be a power of 2 for vio_create_mblks().
218f0ca1d9aSsb155480  */
219f0ca1d9aSsb155480 uint32_t vsw_pri_tx_nmblks = 64;
220d10e4ef2Snarayan 
22151aa9d07Ssb155480 /*
22251aa9d07Ssb155480  * Number of RARP packets sent to announce macaddr to the physical switch,
22351aa9d07Ssb155480  * after vsw's physical device is changed dynamically or after a guest (client
22451aa9d07Ssb155480  * vnet) is live migrated in.
22551aa9d07Ssb155480  */
22651aa9d07Ssb155480 uint32_t vsw_publish_macaddr_count = 3;
22751aa9d07Ssb155480 
228678453a8Sspeer boolean_t vsw_hio_enabled = B_TRUE;	/* Enable/disable HybridIO */
229678453a8Sspeer int vsw_hio_max_cleanup_retries = 10;	/* Max retries for HybridIO cleanp */
230678453a8Sspeer int vsw_hio_cleanup_delay = 10000;	/* 10ms */
231678453a8Sspeer 
232f0ca1d9aSsb155480 /* Number of transmit descriptors -  must be power of 2 */
233f0ca1d9aSsb155480 uint32_t vsw_ntxds = VSW_RING_NUM_EL;
234f0ca1d9aSsb155480 
23506db247cSraghuram /*
23606db247cSraghuram  * Max number of mblks received in one receive operation.
23706db247cSraghuram  */
23806db247cSraghuram uint32_t vsw_chain_len = (VSW_NUM_MBLKS * 0.6);
23906db247cSraghuram 
24006db247cSraghuram /*
2417b1f684aSSriharsha Basavapatna  * Internal tunables for receive buffer pools, that is,  the size and number of
2427b1f684aSSriharsha Basavapatna  * mblks for each pool. At least 3 sizes must be specified if these are used.
2437b1f684aSSriharsha Basavapatna  * The sizes must be specified in increasing order. Non-zero value of the first
2447b1f684aSSriharsha Basavapatna  * size will be used as a hint to use these values instead of the algorithm
2457b1f684aSSriharsha Basavapatna  * that determines the sizes based on MTU.
24606db247cSraghuram  */
2477b1f684aSSriharsha Basavapatna uint32_t vsw_mblk_size1 = 0;
2487b1f684aSSriharsha Basavapatna uint32_t vsw_mblk_size2 = 0;
2497b1f684aSSriharsha Basavapatna uint32_t vsw_mblk_size3 = 0;
2507b1f684aSSriharsha Basavapatna uint32_t vsw_mblk_size4 = 0;
25106db247cSraghuram uint32_t vsw_num_mblks1 = VSW_NUM_MBLKS;	/* number of mblks for pool1 */
25206db247cSraghuram uint32_t vsw_num_mblks2 = VSW_NUM_MBLKS;	/* number of mblks for pool2 */
25306db247cSraghuram uint32_t vsw_num_mblks3 = VSW_NUM_MBLKS;	/* number of mblks for pool3 */
2547b1f684aSSriharsha Basavapatna uint32_t vsw_num_mblks4 = VSW_NUM_MBLKS;	/* number of mblks for pool4 */
2557b1f684aSSriharsha Basavapatna 
2567b1f684aSSriharsha Basavapatna /*
2577b1f684aSSriharsha Basavapatna  * Set this to non-zero to enable additional internal receive buffer pools
2587b1f684aSSriharsha Basavapatna  * based on the MTU of the device for better performance at the cost of more
2597b1f684aSSriharsha Basavapatna  * memory consumption. This is turned off by default, to use allocb(9F) for
2607b1f684aSSriharsha Basavapatna  * receive buffer allocations of sizes > 2K.
2617b1f684aSSriharsha Basavapatna  */
2627b1f684aSSriharsha Basavapatna boolean_t vsw_jumbo_rxpools = B_FALSE;
26306db247cSraghuram 
26406db247cSraghuram /*
265f0ca1d9aSsb155480  * vsw_max_tx_qcount is the maximum # of packets that can be queued
266f0ca1d9aSsb155480  * before the tx worker thread begins processing the queue. Its value
267f0ca1d9aSsb155480  * is chosen to be 4x the default length of tx descriptor ring.
268f0ca1d9aSsb155480  */
269f0ca1d9aSsb155480 uint32_t vsw_max_tx_qcount = 4 * VSW_RING_NUM_EL;
270f0ca1d9aSsb155480 
271f0ca1d9aSsb155480 /*
27206db247cSraghuram  * MAC callbacks
27306db247cSraghuram  */
274ba2e4443Sseb static	mac_callbacks_t	vsw_m_callbacks = {
275ba2e4443Sseb 	0,
276ba2e4443Sseb 	vsw_m_stat,
277ba2e4443Sseb 	vsw_m_start,
278ba2e4443Sseb 	vsw_m_stop,
279ba2e4443Sseb 	vsw_m_promisc,
280ba2e4443Sseb 	vsw_m_multicst,
281ba2e4443Sseb 	vsw_m_unicst,
282ba2e4443Sseb 	vsw_m_tx,
283ba2e4443Sseb 	NULL,
284ba2e4443Sseb 	NULL,
285ba2e4443Sseb 	NULL
286ba2e4443Sseb };
287ba2e4443Sseb 
2881ae08745Sheppo static	struct	cb_ops	vsw_cb_ops = {
2891ae08745Sheppo 	nulldev,			/* cb_open */
2901ae08745Sheppo 	nulldev,			/* cb_close */
2911ae08745Sheppo 	nodev,				/* cb_strategy */
2921ae08745Sheppo 	nodev,				/* cb_print */
2931ae08745Sheppo 	nodev,				/* cb_dump */
2941ae08745Sheppo 	nodev,				/* cb_read */
2951ae08745Sheppo 	nodev,				/* cb_write */
2961ae08745Sheppo 	nodev,				/* cb_ioctl */
2971ae08745Sheppo 	nodev,				/* cb_devmap */
2981ae08745Sheppo 	nodev,				/* cb_mmap */
2991ae08745Sheppo 	nodev,				/* cb_segmap */
3001ae08745Sheppo 	nochpoll,			/* cb_chpoll */
3011ae08745Sheppo 	ddi_prop_op,			/* cb_prop_op */
3021ae08745Sheppo 	NULL,				/* cb_stream */
3031ae08745Sheppo 	D_MP,				/* cb_flag */
3041ae08745Sheppo 	CB_REV,				/* rev */
3051ae08745Sheppo 	nodev,				/* int (*cb_aread)() */
3061ae08745Sheppo 	nodev				/* int (*cb_awrite)() */
3071ae08745Sheppo };
3081ae08745Sheppo 
3091ae08745Sheppo static	struct	dev_ops	vsw_ops = {
3101ae08745Sheppo 	DEVO_REV,		/* devo_rev */
3111ae08745Sheppo 	0,			/* devo_refcnt */
31271184a40SWENTAO YANG 	NULL,			/* devo_getinfo */
3131ae08745Sheppo 	nulldev,		/* devo_identify */
3141ae08745Sheppo 	nulldev,		/* devo_probe */
3151ae08745Sheppo 	vsw_attach,		/* devo_attach */
3161ae08745Sheppo 	vsw_detach,		/* devo_detach */
3171ae08745Sheppo 	nodev,			/* devo_reset */
3181ae08745Sheppo 	&vsw_cb_ops,		/* devo_cb_ops */
3191ae08745Sheppo 	(struct bus_ops *)NULL,	/* devo_bus_ops */
3201ae08745Sheppo 	ddi_power		/* devo_power */
3211ae08745Sheppo };
3221ae08745Sheppo 
3231ae08745Sheppo extern	struct	mod_ops	mod_driverops;
3241ae08745Sheppo static struct modldrv vswmodldrv = {
3251ae08745Sheppo 	&mod_driverops,
326205eeb1aSlm66018 	"sun4v Virtual Switch",
3271ae08745Sheppo 	&vsw_ops,
3281ae08745Sheppo };
3291ae08745Sheppo 
3301ae08745Sheppo #define	LDC_ENTER_LOCK(ldcp)	\
3311ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_cblock));\
33206db247cSraghuram 				mutex_enter(&((ldcp)->ldc_rxlock));\
3331ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_txlock));
3341ae08745Sheppo #define	LDC_EXIT_LOCK(ldcp)	\
3351ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_txlock));\
33606db247cSraghuram 				mutex_exit(&((ldcp)->ldc_rxlock));\
3371ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_cblock));
3381ae08745Sheppo 
3391ae08745Sheppo /* Driver soft state ptr  */
3401ae08745Sheppo static void	*vsw_state;
3411ae08745Sheppo 
3421ae08745Sheppo /*
3431ae08745Sheppo  * Linked list of "vsw_t" structures - one per instance.
3441ae08745Sheppo  */
3451ae08745Sheppo vsw_t		*vsw_head = NULL;
3461ae08745Sheppo krwlock_t	vsw_rw;
3471ae08745Sheppo 
3481ae08745Sheppo /*
3491ae08745Sheppo  * Property names
3501ae08745Sheppo  */
3511ae08745Sheppo static char vdev_propname[] = "virtual-device";
3521ae08745Sheppo static char vsw_propname[] = "virtual-network-switch";
3531ae08745Sheppo static char physdev_propname[] = "vsw-phys-dev";
3541ae08745Sheppo static char smode_propname[] = "vsw-switch-mode";
3551ae08745Sheppo static char macaddr_propname[] = "local-mac-address";
3561ae08745Sheppo static char remaddr_propname[] = "remote-mac-address";
3571ae08745Sheppo static char ldcids_propname[] = "ldc-ids";
3581ae08745Sheppo static char chan_propname[] = "channel-endpoint";
3591ae08745Sheppo static char id_propname[] = "id";
3601ae08745Sheppo static char reg_propname[] = "reg";
361f0ca1d9aSsb155480 static char pri_types_propname[] = "priority-ether-types";
362c1c61f44Ssb155480 static char vsw_pvid_propname[] = "port-vlan-id";
363c1c61f44Ssb155480 static char vsw_vid_propname[] = "vlan-id";
364c1c61f44Ssb155480 static char vsw_dvid_propname[] = "default-vlan-id";
365c1c61f44Ssb155480 static char port_pvid_propname[] = "remote-port-vlan-id";
366c1c61f44Ssb155480 static char port_vid_propname[] = "remote-vlan-id";
367678453a8Sspeer static char hybrid_propname[] = "hybrid";
3687b1f684aSSriharsha Basavapatna static char vsw_mtu_propname[] = "mtu";
3691ae08745Sheppo 
3701ae08745Sheppo /*
3711ae08745Sheppo  * Matching criteria passed to the MDEG to register interest
3721ae08745Sheppo  * in changes to 'virtual-device-port' nodes identified by their
3731ae08745Sheppo  * 'id' property.
3741ae08745Sheppo  */
3751ae08745Sheppo static md_prop_match_t vport_prop_match[] = {
3761ae08745Sheppo 	{ MDET_PROP_VAL,    "id"   },
3771ae08745Sheppo 	{ MDET_LIST_END,    NULL    }
3781ae08745Sheppo };
3791ae08745Sheppo 
3801ae08745Sheppo static mdeg_node_match_t vport_match = { "virtual-device-port",
3811ae08745Sheppo 						vport_prop_match };
3821ae08745Sheppo 
3831ae08745Sheppo /*
38434683adeSsg70180  * Matching criteria passed to the MDEG to register interest
38534683adeSsg70180  * in changes to 'virtual-device' nodes (i.e. vsw nodes) identified
38634683adeSsg70180  * by their 'name' and 'cfg-handle' properties.
38734683adeSsg70180  */
38834683adeSsg70180 static md_prop_match_t vdev_prop_match[] = {
38934683adeSsg70180 	{ MDET_PROP_STR,    "name"   },
39034683adeSsg70180 	{ MDET_PROP_VAL,    "cfg-handle" },
39134683adeSsg70180 	{ MDET_LIST_END,    NULL    }
39234683adeSsg70180 };
39334683adeSsg70180 
39434683adeSsg70180 static mdeg_node_match_t vdev_match = { "virtual-device",
39534683adeSsg70180 						vdev_prop_match };
39634683adeSsg70180 
39734683adeSsg70180 
39834683adeSsg70180 /*
3991ae08745Sheppo  * Specification of an MD node passed to the MDEG to filter any
4001ae08745Sheppo  * 'vport' nodes that do not belong to the specified node. This
4011ae08745Sheppo  * template is copied for each vsw instance and filled in with
4021ae08745Sheppo  * the appropriate 'cfg-handle' value before being passed to the MDEG.
4031ae08745Sheppo  */
4041ae08745Sheppo static mdeg_prop_spec_t vsw_prop_template[] = {
4051ae08745Sheppo 	{ MDET_PROP_STR,    "name",		vsw_propname },
4061ae08745Sheppo 	{ MDET_PROP_VAL,    "cfg-handle",	NULL	},
4071ae08745Sheppo 	{ MDET_LIST_END,    NULL,		NULL	}
4081ae08745Sheppo };
4091ae08745Sheppo 
4101ae08745Sheppo #define	VSW_SET_MDEG_PROP_INST(specp, val)	(specp)[1].ps_val = (val);
4111ae08745Sheppo 
41206db247cSraghuram #ifdef	DEBUG
4137636cb21Slm66018 /*
4141ae08745Sheppo  * Print debug messages - set to 0x1f to enable all msgs
4151ae08745Sheppo  * or 0x0 to turn all off.
4161ae08745Sheppo  */
4171ae08745Sheppo int vswdbg = 0x0;
4181ae08745Sheppo 
4191ae08745Sheppo /*
4201ae08745Sheppo  * debug levels:
4211ae08745Sheppo  * 0x01:	Function entry/exit tracing
4221ae08745Sheppo  * 0x02:	Internal function messages
4231ae08745Sheppo  * 0x04:	Verbose internal messages
4241ae08745Sheppo  * 0x08:	Warning messages
4251ae08745Sheppo  * 0x10:	Error messages
4261ae08745Sheppo  */
4271ae08745Sheppo 
42806db247cSraghuram void
4291ae08745Sheppo vswdebug(vsw_t *vswp, const char *fmt, ...)
4301ae08745Sheppo {
4311ae08745Sheppo 	char buf[512];
4321ae08745Sheppo 	va_list ap;
4331ae08745Sheppo 
4341ae08745Sheppo 	va_start(ap, fmt);
4351ae08745Sheppo 	(void) vsprintf(buf, fmt, ap);
4361ae08745Sheppo 	va_end(ap);
4371ae08745Sheppo 
4381ae08745Sheppo 	if (vswp == NULL)
4391ae08745Sheppo 		cmn_err(CE_CONT, "%s\n", buf);
4401ae08745Sheppo 	else
4411ae08745Sheppo 		cmn_err(CE_CONT, "vsw%d: %s\n", vswp->instance, buf);
4421ae08745Sheppo }
4431ae08745Sheppo 
4441ae08745Sheppo #endif	/* DEBUG */
4451ae08745Sheppo 
4461ae08745Sheppo static struct modlinkage modlinkage = {
4471ae08745Sheppo 	MODREV_1,
4481ae08745Sheppo 	&vswmodldrv,
4491ae08745Sheppo 	NULL
4501ae08745Sheppo };
4511ae08745Sheppo 
4521ae08745Sheppo int
4531ae08745Sheppo _init(void)
4541ae08745Sheppo {
4551ae08745Sheppo 	int status;
4561ae08745Sheppo 
4571ae08745Sheppo 	rw_init(&vsw_rw, NULL, RW_DRIVER, NULL);
4581ae08745Sheppo 
4591ae08745Sheppo 	status = ddi_soft_state_init(&vsw_state, sizeof (vsw_t), 1);
4601ae08745Sheppo 	if (status != 0) {
4611ae08745Sheppo 		return (status);
4621ae08745Sheppo 	}
4631ae08745Sheppo 
46406db247cSraghuram 	mac_init_ops(&vsw_ops, DRV_NAME);
4651ae08745Sheppo 	status = mod_install(&modlinkage);
4661ae08745Sheppo 	if (status != 0) {
4671ae08745Sheppo 		ddi_soft_state_fini(&vsw_state);
4681ae08745Sheppo 	}
4691ae08745Sheppo 	return (status);
4701ae08745Sheppo }
4711ae08745Sheppo 
4721ae08745Sheppo int
4731ae08745Sheppo _fini(void)
4741ae08745Sheppo {
4751ae08745Sheppo 	int status;
4761ae08745Sheppo 
4771ae08745Sheppo 	status = mod_remove(&modlinkage);
4781ae08745Sheppo 	if (status != 0)
4791ae08745Sheppo 		return (status);
4801ae08745Sheppo 	mac_fini_ops(&vsw_ops);
4811ae08745Sheppo 	ddi_soft_state_fini(&vsw_state);
4821ae08745Sheppo 
4831ae08745Sheppo 	rw_destroy(&vsw_rw);
4841ae08745Sheppo 
4851ae08745Sheppo 	return (status);
4861ae08745Sheppo }
4871ae08745Sheppo 
4881ae08745Sheppo int
4891ae08745Sheppo _info(struct modinfo *modinfop)
4901ae08745Sheppo {
4911ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
4921ae08745Sheppo }
4931ae08745Sheppo 
4941ae08745Sheppo static int
4951ae08745Sheppo vsw_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4961ae08745Sheppo {
4971ae08745Sheppo 	vsw_t		*vswp;
49834683adeSsg70180 	int		instance;
4991ae08745Sheppo 	char		hashname[MAXNAMELEN];
5001ae08745Sheppo 	char		qname[TASKQ_NAMELEN];
5017636cb21Slm66018 	enum		{ PROG_init = 0x00,
50219b65a69Ssb155480 				PROG_locks = 0x01,
50319b65a69Ssb155480 				PROG_readmd = 0x02,
50419b65a69Ssb155480 				PROG_fdb = 0x04,
50519b65a69Ssb155480 				PROG_mfdb = 0x08,
50619b65a69Ssb155480 				PROG_taskq = 0x10,
507f0ca1d9aSsb155480 				PROG_swmode = 0x20,
508f0ca1d9aSsb155480 				PROG_macreg = 0x40,
509f0ca1d9aSsb155480 				PROG_mdreg = 0x80}
5101ae08745Sheppo 			progress;
5111ae08745Sheppo 
5121ae08745Sheppo 	progress = PROG_init;
51319b65a69Ssb155480 	int		rv;
5141ae08745Sheppo 
5151ae08745Sheppo 	switch (cmd) {
5161ae08745Sheppo 	case DDI_ATTACH:
5171ae08745Sheppo 		break;
5181ae08745Sheppo 	case DDI_RESUME:
5191ae08745Sheppo 		/* nothing to do for this non-device */
5201ae08745Sheppo 		return (DDI_SUCCESS);
5211ae08745Sheppo 	case DDI_PM_RESUME:
5221ae08745Sheppo 	default:
5231ae08745Sheppo 		return (DDI_FAILURE);
5241ae08745Sheppo 	}
5251ae08745Sheppo 
5261ae08745Sheppo 	instance = ddi_get_instance(dip);
5271ae08745Sheppo 	if (ddi_soft_state_zalloc(vsw_state, instance) != DDI_SUCCESS) {
5281ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_soft_state_zalloc failed", instance);
5291ae08745Sheppo 		return (DDI_FAILURE);
5301ae08745Sheppo 	}
5311ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
5321ae08745Sheppo 
5331ae08745Sheppo 	if (vswp == NULL) {
5341ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_get_soft_state failed", instance);
5351ae08745Sheppo 		goto vsw_attach_fail;
5361ae08745Sheppo 	}
5371ae08745Sheppo 
5381ae08745Sheppo 	vswp->dip = dip;
5391ae08745Sheppo 	vswp->instance = instance;
5401ae08745Sheppo 	ddi_set_driver_private(dip, (caddr_t)vswp);
5411ae08745Sheppo 
542da14cebeSEric Cheng 	mutex_init(&vswp->mac_lock, NULL, MUTEX_DRIVER, NULL);
54319b65a69Ssb155480 	mutex_init(&vswp->mca_lock, NULL, MUTEX_DRIVER, NULL);
544*808f26a8SSriharsha Basavapatna 	mutex_init(&vswp->sw_thr_lock, NULL, MUTEX_DRIVER, NULL);
545*808f26a8SSriharsha Basavapatna 	cv_init(&vswp->sw_thr_cv, NULL, CV_DRIVER, NULL);
546da14cebeSEric Cheng 	rw_init(&vswp->maccl_rwlock, NULL, RW_DRIVER, NULL);
5471ae08745Sheppo 	rw_init(&vswp->if_lockrw, NULL, RW_DRIVER, NULL);
54819b65a69Ssb155480 	rw_init(&vswp->mfdbrw, NULL, RW_DRIVER, NULL);
54919b65a69Ssb155480 	rw_init(&vswp->plist.lockrw, NULL, RW_DRIVER, NULL);
55019b65a69Ssb155480 
55119b65a69Ssb155480 	progress |= PROG_locks;
55219b65a69Ssb155480 
55319b65a69Ssb155480 	rv = vsw_read_mdprops(vswp);
55419b65a69Ssb155480 	if (rv != 0)
55519b65a69Ssb155480 		goto vsw_attach_fail;
55619b65a69Ssb155480 
55719b65a69Ssb155480 	progress |= PROG_readmd;
5581ae08745Sheppo 
5591ae08745Sheppo 	/* setup the unicast forwarding database  */
5601ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_unicst_table-%d",
5611ae08745Sheppo 	    vswp->instance);
5621ae08745Sheppo 	D2(vswp, "creating unicast hash table (%s)...", hashname);
563c1c61f44Ssb155480 	vswp->fdb_nchains = vsw_fdb_nchains;
564c1c61f44Ssb155480 	vswp->fdb_hashp = mod_hash_create_ptrhash(hashname, vswp->fdb_nchains,
5651ae08745Sheppo 	    mod_hash_null_valdtor, sizeof (void *));
566c1c61f44Ssb155480 	vsw_create_vlans((void *)vswp, VSW_LOCALDEV);
5671ae08745Sheppo 	progress |= PROG_fdb;
5681ae08745Sheppo 
5691ae08745Sheppo 	/* setup the multicast fowarding database */
5701ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_mcst_table-%d",
5711ae08745Sheppo 	    vswp->instance);
5721ae08745Sheppo 	D2(vswp, "creating multicast hash table %s)...", hashname);
573c1c61f44Ssb155480 	vswp->mfdb = mod_hash_create_ptrhash(hashname, vsw_fdb_nchains,
5741ae08745Sheppo 	    mod_hash_null_valdtor, sizeof (void *));
5751ae08745Sheppo 
5761ae08745Sheppo 	progress |= PROG_mfdb;
5771ae08745Sheppo 
5781ae08745Sheppo 	/*
5791ae08745Sheppo 	 * Create the taskq which will process all the VIO
5801ae08745Sheppo 	 * control messages.
5811ae08745Sheppo 	 */
5821ae08745Sheppo 	(void) snprintf(qname, TASKQ_NAMELEN, "vsw_taskq%d", vswp->instance);
5831ae08745Sheppo 	if ((vswp->taskq_p = ddi_taskq_create(vswp->dip, qname, 1,
5841ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
58534683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to create task queue",
58634683adeSsg70180 		    vswp->instance);
5871ae08745Sheppo 		goto vsw_attach_fail;
5881ae08745Sheppo 	}
5891ae08745Sheppo 
5901ae08745Sheppo 	progress |= PROG_taskq;
5911ae08745Sheppo 
592d10e4ef2Snarayan 	/* prevent auto-detaching */
593d10e4ef2Snarayan 	if (ddi_prop_update_int(DDI_DEV_T_NONE, vswp->dip,
594d10e4ef2Snarayan 	    DDI_NO_AUTODETACH, 1) != DDI_SUCCESS) {
59534683adeSsg70180 		cmn_err(CE_NOTE, "!Unable to set \"%s\" property for "
596d10e4ef2Snarayan 		    "instance %u", DDI_NO_AUTODETACH, instance);
597d10e4ef2Snarayan 	}
598d10e4ef2Snarayan 
5991ae08745Sheppo 	/*
6007a327842Swentaoy 	 * The null switching function is set to avoid panic until
6017a327842Swentaoy 	 * switch mode is setup.
6027a327842Swentaoy 	 */
6037a327842Swentaoy 	vswp->vsw_switch_frame = vsw_switch_frame_nop;
6047a327842Swentaoy 
6057a327842Swentaoy 	/*
606*808f26a8SSriharsha Basavapatna 	 * Setup the required switching mode, based on the mdprops that we read
607*808f26a8SSriharsha Basavapatna 	 * earlier. We start a thread to do this, to avoid calling mac_open()
608*808f26a8SSriharsha Basavapatna 	 * directly from attach().
60919b65a69Ssb155480 	 */
610*808f26a8SSriharsha Basavapatna 	rv = vsw_setup_switching_start(vswp);
611*808f26a8SSriharsha Basavapatna 	if (rv != 0) {
612*808f26a8SSriharsha Basavapatna 		goto vsw_attach_fail;
613*808f26a8SSriharsha Basavapatna 	}
61419b65a69Ssb155480 
61519b65a69Ssb155480 	progress |= PROG_swmode;
61619b65a69Ssb155480 
61719b65a69Ssb155480 	/* Register with mac layer as a provider */
61819b65a69Ssb155480 	rv = vsw_mac_register(vswp);
61919b65a69Ssb155480 	if (rv != 0)
62019b65a69Ssb155480 		goto vsw_attach_fail;
62119b65a69Ssb155480 
62219b65a69Ssb155480 	progress |= PROG_macreg;
62319b65a69Ssb155480 
62419b65a69Ssb155480 	/*
62534683adeSsg70180 	 * Now we have everything setup, register an interest in
62634683adeSsg70180 	 * specific MD nodes.
62734683adeSsg70180 	 *
62834683adeSsg70180 	 * The callback is invoked in 2 cases, firstly if upon mdeg
62934683adeSsg70180 	 * registration there are existing nodes which match our specified
63034683adeSsg70180 	 * criteria, and secondly if the MD is changed (and again, there
63134683adeSsg70180 	 * are nodes which we are interested in present within it. Note
63234683adeSsg70180 	 * that our callback will be invoked even if our specified nodes
63334683adeSsg70180 	 * have not actually changed).
63434683adeSsg70180 	 *
6351ae08745Sheppo 	 */
63619b65a69Ssb155480 	rv = vsw_mdeg_register(vswp);
63719b65a69Ssb155480 	if (rv != 0)
63834683adeSsg70180 		goto vsw_attach_fail;
6391ae08745Sheppo 
64019b65a69Ssb155480 	progress |= PROG_mdreg;
64119b65a69Ssb155480 
64219b65a69Ssb155480 	WRITE_ENTER(&vsw_rw);
64319b65a69Ssb155480 	vswp->next = vsw_head;
64419b65a69Ssb155480 	vsw_head = vswp;
64519b65a69Ssb155480 	RW_EXIT(&vsw_rw);
64619b65a69Ssb155480 
64719b65a69Ssb155480 	ddi_report_dev(vswp->dip);
6481ae08745Sheppo 	return (DDI_SUCCESS);
6491ae08745Sheppo 
6501ae08745Sheppo vsw_attach_fail:
6511ae08745Sheppo 	DERR(NULL, "vsw_attach: failed");
6521ae08745Sheppo 
65319b65a69Ssb155480 	if (progress & PROG_mdreg) {
65419b65a69Ssb155480 		vsw_mdeg_unregister(vswp);
65519b65a69Ssb155480 		(void) vsw_detach_ports(vswp);
65619b65a69Ssb155480 	}
65719b65a69Ssb155480 
65819b65a69Ssb155480 	if (progress & PROG_macreg)
65919b65a69Ssb155480 		(void) vsw_mac_unregister(vswp);
66019b65a69Ssb155480 
66119b65a69Ssb155480 	if (progress & PROG_swmode) {
662*808f26a8SSriharsha Basavapatna 		vsw_setup_switching_stop(vswp);
663678453a8Sspeer 		vsw_hio_cleanup(vswp);
664da14cebeSEric Cheng 		mutex_enter(&vswp->mac_lock);
66519b65a69Ssb155480 		vsw_mac_close(vswp);
666da14cebeSEric Cheng 		mutex_exit(&vswp->mac_lock);
66719b65a69Ssb155480 	}
66819b65a69Ssb155480 
6691ae08745Sheppo 	if (progress & PROG_taskq)
6701ae08745Sheppo 		ddi_taskq_destroy(vswp->taskq_p);
6711ae08745Sheppo 
67219b65a69Ssb155480 	if (progress & PROG_mfdb)
6731ae08745Sheppo 		mod_hash_destroy_hash(vswp->mfdb);
6741ae08745Sheppo 
675c1c61f44Ssb155480 	if (progress & PROG_fdb) {
676c1c61f44Ssb155480 		vsw_destroy_vlans(vswp, VSW_LOCALDEV);
677c1c61f44Ssb155480 		mod_hash_destroy_hash(vswp->fdb_hashp);
678c1c61f44Ssb155480 	}
6791ae08745Sheppo 
680f0ca1d9aSsb155480 	if (progress & PROG_readmd) {
681f0ca1d9aSsb155480 		if (VSW_PRI_ETH_DEFINED(vswp)) {
682f0ca1d9aSsb155480 			kmem_free(vswp->pri_types,
683f0ca1d9aSsb155480 			    sizeof (uint16_t) * vswp->pri_num_types);
684f0ca1d9aSsb155480 		}
685f0ca1d9aSsb155480 		(void) vio_destroy_mblks(vswp->pri_tx_vmp);
686f0ca1d9aSsb155480 	}
687f0ca1d9aSsb155480 
68819b65a69Ssb155480 	if (progress & PROG_locks) {
68919b65a69Ssb155480 		rw_destroy(&vswp->plist.lockrw);
69019b65a69Ssb155480 		rw_destroy(&vswp->mfdbrw);
6911ae08745Sheppo 		rw_destroy(&vswp->if_lockrw);
692da14cebeSEric Cheng 		rw_destroy(&vswp->maccl_rwlock);
693*808f26a8SSriharsha Basavapatna 		cv_destroy(&vswp->sw_thr_cv);
694*808f26a8SSriharsha Basavapatna 		mutex_destroy(&vswp->sw_thr_lock);
69519b65a69Ssb155480 		mutex_destroy(&vswp->mca_lock);
696da14cebeSEric Cheng 		mutex_destroy(&vswp->mac_lock);
69734683adeSsg70180 	}
6981ae08745Sheppo 
6991ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
7001ae08745Sheppo 	return (DDI_FAILURE);
7011ae08745Sheppo }
7021ae08745Sheppo 
7031ae08745Sheppo static int
7041ae08745Sheppo vsw_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
7051ae08745Sheppo {
706d10e4ef2Snarayan 	vio_mblk_pool_t		*poolp, *npoolp;
7071ae08745Sheppo 	vsw_t			**vswpp, *vswp;
7081ae08745Sheppo 	int 			instance;
7091ae08745Sheppo 
7101ae08745Sheppo 	instance = ddi_get_instance(dip);
7111ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
7121ae08745Sheppo 
7131ae08745Sheppo 	if (vswp == NULL) {
7141ae08745Sheppo 		return (DDI_FAILURE);
7151ae08745Sheppo 	}
7161ae08745Sheppo 
7171ae08745Sheppo 	switch (cmd) {
7181ae08745Sheppo 	case DDI_DETACH:
7191ae08745Sheppo 		break;
7201ae08745Sheppo 	case DDI_SUSPEND:
7211ae08745Sheppo 	case DDI_PM_SUSPEND:
7221ae08745Sheppo 	default:
7231ae08745Sheppo 		return (DDI_FAILURE);
7241ae08745Sheppo 	}
7251ae08745Sheppo 
7261ae08745Sheppo 	D2(vswp, "detaching instance %d", instance);
7271ae08745Sheppo 
728*808f26a8SSriharsha Basavapatna 	/* Stop any pending thread to setup switching mode. */
729*808f26a8SSriharsha Basavapatna 	vsw_setup_switching_stop(vswp);
73019b65a69Ssb155480 
731da14cebeSEric Cheng 	/* Cleanup the interface's mac client */
732da14cebeSEric Cheng 	vsw_mac_client_cleanup(vswp, NULL, VSW_LOCALDEV);
733da14cebeSEric Cheng 
73434683adeSsg70180 	if (vswp->if_state & VSW_IF_REG) {
7351ae08745Sheppo 		if (vsw_mac_unregister(vswp) != 0) {
73634683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to detach from "
73734683adeSsg70180 			    "MAC layer", vswp->instance);
7381ae08745Sheppo 			return (DDI_FAILURE);
7391ae08745Sheppo 		}
740d10e4ef2Snarayan 	}
7411ae08745Sheppo 
7421ae08745Sheppo 	vsw_mdeg_unregister(vswp);
7431ae08745Sheppo 
744da14cebeSEric Cheng 	/* cleanup HybridIO */
745da14cebeSEric Cheng 	vsw_hio_cleanup(vswp);
7461ae08745Sheppo 
7471ae08745Sheppo 	if (vsw_detach_ports(vswp) != 0) {
7481ef0bbb5Snarayan 		cmn_err(CE_WARN, "!vsw%d: Unable to unconfigure ports",
74934683adeSsg70180 		    vswp->instance);
7501ae08745Sheppo 		return (DDI_FAILURE);
7511ae08745Sheppo 	}
7521ae08745Sheppo 
75334683adeSsg70180 	rw_destroy(&vswp->if_lockrw);
75434683adeSsg70180 
755da14cebeSEric Cheng 	vsw_mac_cleanup_ports(vswp);
7565f94e909Ssg70180 
7571ae08745Sheppo 	/*
758e1ebb9ecSlm66018 	 * Now that the ports have been deleted, stop and close
759e1ebb9ecSlm66018 	 * the physical device.
760e1ebb9ecSlm66018 	 */
761da14cebeSEric Cheng 	mutex_enter(&vswp->mac_lock);
76219b65a69Ssb155480 	vsw_mac_close(vswp);
763da14cebeSEric Cheng 	mutex_exit(&vswp->mac_lock);
76419b65a69Ssb155480 
765da14cebeSEric Cheng 	mutex_destroy(&vswp->mac_lock);
766*808f26a8SSriharsha Basavapatna 	cv_destroy(&vswp->sw_thr_cv);
767*808f26a8SSriharsha Basavapatna 	mutex_destroy(&vswp->sw_thr_lock);
768da14cebeSEric Cheng 	rw_destroy(&vswp->maccl_rwlock);
769e1ebb9ecSlm66018 
770e1ebb9ecSlm66018 	/*
771d10e4ef2Snarayan 	 * Destroy any free pools that may still exist.
772d10e4ef2Snarayan 	 */
773d10e4ef2Snarayan 	poolp = vswp->rxh;
774d10e4ef2Snarayan 	while (poolp != NULL) {
775d10e4ef2Snarayan 		npoolp = vswp->rxh = poolp->nextp;
776d10e4ef2Snarayan 		if (vio_destroy_mblks(poolp) != 0) {
777d10e4ef2Snarayan 			vswp->rxh = poolp;
778d10e4ef2Snarayan 			return (DDI_FAILURE);
779d10e4ef2Snarayan 		}
780d10e4ef2Snarayan 		poolp = npoolp;
781d10e4ef2Snarayan 	}
782d10e4ef2Snarayan 
783d10e4ef2Snarayan 	/*
7841ae08745Sheppo 	 * Remove this instance from any entries it may be on in
7851ae08745Sheppo 	 * the hash table by using the list of addresses maintained
7861ae08745Sheppo 	 * in the vsw_t structure.
7871ae08745Sheppo 	 */
7881ae08745Sheppo 	vsw_del_mcst_vsw(vswp);
7891ae08745Sheppo 
7901ae08745Sheppo 	vswp->mcap = NULL;
7911ae08745Sheppo 	mutex_destroy(&vswp->mca_lock);
7921ae08745Sheppo 
7931ae08745Sheppo 	/*
7941ae08745Sheppo 	 * By now any pending tasks have finished and the underlying
7951ae08745Sheppo 	 * ldc's have been destroyed, so its safe to delete the control
7961ae08745Sheppo 	 * message taskq.
7971ae08745Sheppo 	 */
7981ae08745Sheppo 	if (vswp->taskq_p != NULL)
7991ae08745Sheppo 		ddi_taskq_destroy(vswp->taskq_p);
8001ae08745Sheppo 
8011ae08745Sheppo 	/*
8021ae08745Sheppo 	 * At this stage all the data pointers in the hash table
8031ae08745Sheppo 	 * should be NULL, as all the ports have been removed and will
8041ae08745Sheppo 	 * have deleted themselves from the port lists which the data
8051ae08745Sheppo 	 * pointers point to. Hence we can destroy the table using the
8061ae08745Sheppo 	 * default destructors.
8071ae08745Sheppo 	 */
8081ae08745Sheppo 	D2(vswp, "vsw_detach: destroying hash tables..");
809c1c61f44Ssb155480 	vsw_destroy_vlans(vswp, VSW_LOCALDEV);
810c1c61f44Ssb155480 	mod_hash_destroy_hash(vswp->fdb_hashp);
811c1c61f44Ssb155480 	vswp->fdb_hashp = NULL;
8121ae08745Sheppo 
8131ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
8141ae08745Sheppo 	mod_hash_destroy_hash(vswp->mfdb);
8151ae08745Sheppo 	vswp->mfdb = NULL;
8161ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
8171ae08745Sheppo 	rw_destroy(&vswp->mfdbrw);
8181ae08745Sheppo 
819f0ca1d9aSsb155480 	/* free pri_types table */
820f0ca1d9aSsb155480 	if (VSW_PRI_ETH_DEFINED(vswp)) {
821f0ca1d9aSsb155480 		kmem_free(vswp->pri_types,
822f0ca1d9aSsb155480 		    sizeof (uint16_t) * vswp->pri_num_types);
823f0ca1d9aSsb155480 		(void) vio_destroy_mblks(vswp->pri_tx_vmp);
824f0ca1d9aSsb155480 	}
825f0ca1d9aSsb155480 
8261ae08745Sheppo 	ddi_remove_minor_node(dip, NULL);
8271ae08745Sheppo 
8281ae08745Sheppo 	rw_destroy(&vswp->plist.lockrw);
8291ae08745Sheppo 	WRITE_ENTER(&vsw_rw);
8301ae08745Sheppo 	for (vswpp = &vsw_head; *vswpp; vswpp = &(*vswpp)->next) {
8311ae08745Sheppo 		if (*vswpp == vswp) {
8321ae08745Sheppo 			*vswpp = vswp->next;
8331ae08745Sheppo 			break;
8341ae08745Sheppo 		}
8351ae08745Sheppo 	}
8361ae08745Sheppo 	RW_EXIT(&vsw_rw);
8371ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
8381ae08745Sheppo 
8391ae08745Sheppo 	return (DDI_SUCCESS);
8401ae08745Sheppo }
8411ae08745Sheppo 
8421ae08745Sheppo /*
84334683adeSsg70180  * Get the value of the "vsw-phys-dev" property in the specified
84434683adeSsg70180  * node. This property is the name of the physical device that
84534683adeSsg70180  * the virtual switch will use to talk to the outside world.
84634683adeSsg70180  *
84734683adeSsg70180  * Note it is valid for this property to be NULL (but the property
84834683adeSsg70180  * itself must exist). Callers of this routine should verify that
84934683adeSsg70180  * the value returned is what they expected (i.e. either NULL or non NULL).
85034683adeSsg70180  *
85134683adeSsg70180  * On success returns value of the property in region pointed to by
85234683adeSsg70180  * the 'name' argument, and with return value of 0. Otherwise returns 1.
8531ae08745Sheppo  */
85434683adeSsg70180 static int
85534683adeSsg70180 vsw_get_md_physname(vsw_t *vswp, md_t *mdp, mde_cookie_t node, char *name)
8561ae08745Sheppo {
85734683adeSsg70180 	int		len = 0;
858f2b610cfSwentaoy 	int		instance;
8591ae08745Sheppo 	char		*physname = NULL;
8601ae08745Sheppo 	char		*dev;
861f2b610cfSwentaoy 	const char	*dev_name;
862f2b610cfSwentaoy 	char		myname[MAXNAMELEN];
863f2b610cfSwentaoy 
864f2b610cfSwentaoy 	dev_name = ddi_driver_name(vswp->dip);
865f2b610cfSwentaoy 	instance = ddi_get_instance(vswp->dip);
866f2b610cfSwentaoy 	(void) snprintf(myname, MAXNAMELEN, "%s%d", dev_name, instance);
8671ae08745Sheppo 
86834683adeSsg70180 	if (md_get_prop_data(mdp, node, physdev_propname,
8691ae08745Sheppo 	    (uint8_t **)(&physname), &len) != 0) {
87034683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get name(s) of physical "
87134683adeSsg70180 		    "device(s) from MD", vswp->instance);
87234683adeSsg70180 		return (1);
8731ae08745Sheppo 	} else if ((strlen(physname) + 1) > LIFNAMSIZ) {
87434683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: %s is too long a device name",
87534683adeSsg70180 		    vswp->instance, physname);
87634683adeSsg70180 		return (1);
877f2b610cfSwentaoy 	} else if (strcmp(myname, physname) == 0) {
878f2b610cfSwentaoy 		/*
879f2b610cfSwentaoy 		 * Prevent the vswitch from opening itself as the
880f2b610cfSwentaoy 		 * network device.
881f2b610cfSwentaoy 		 */
882f2b610cfSwentaoy 		cmn_err(CE_WARN, "!vsw%d: %s is an invalid device name",
883f2b610cfSwentaoy 		    vswp->instance, physname);
884f2b610cfSwentaoy 		return (1);
8851ae08745Sheppo 	} else {
88634683adeSsg70180 		(void) strncpy(name, physname, strlen(physname) + 1);
8871ae08745Sheppo 		D2(vswp, "%s: using first device specified (%s)",
88834683adeSsg70180 		    __func__, physname);
8891ae08745Sheppo 	}
8901ae08745Sheppo 
8911ae08745Sheppo #ifdef DEBUG
8921ae08745Sheppo 	/*
8931ae08745Sheppo 	 * As a temporary measure to aid testing we check to see if there
8941ae08745Sheppo 	 * is a vsw.conf file present. If there is we use the value of the
8951ae08745Sheppo 	 * vsw_physname property in the file as the name of the physical
8961ae08745Sheppo 	 * device, overriding the value from the MD.
8971ae08745Sheppo 	 *
8981ae08745Sheppo 	 * There may be multiple devices listed, but for the moment
8991ae08745Sheppo 	 * we just use the first one.
9001ae08745Sheppo 	 */
9011ae08745Sheppo 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vswp->dip, 0,
9021ae08745Sheppo 	    "vsw_physname", &dev) == DDI_PROP_SUCCESS) {
9031ae08745Sheppo 		if ((strlen(dev) + 1) > LIFNAMSIZ) {
90434683adeSsg70180 			cmn_err(CE_WARN, "vsw%d: %s is too long a device name",
90534683adeSsg70180 			    vswp->instance, dev);
90634683adeSsg70180 			ddi_prop_free(dev);
90734683adeSsg70180 			return (1);
9081ae08745Sheppo 		} else {
90934683adeSsg70180 			cmn_err(CE_NOTE, "vsw%d: Using device name (%s) from "
91034683adeSsg70180 			    "config file", vswp->instance, dev);
9111ae08745Sheppo 
91234683adeSsg70180 			(void) strncpy(name, dev, strlen(dev) + 1);
9131ae08745Sheppo 		}
9141ae08745Sheppo 
9151ae08745Sheppo 		ddi_prop_free(dev);
9161ae08745Sheppo 	}
9171ae08745Sheppo #endif
9181ae08745Sheppo 
91934683adeSsg70180 	return (0);
92034683adeSsg70180 }
921e1ebb9ecSlm66018 
922e1ebb9ecSlm66018 /*
92334683adeSsg70180  * Read the 'vsw-switch-mode' property from the specified MD node.
92434683adeSsg70180  *
925da14cebeSEric Cheng  * Returns 0 on success, otherwise returns 1.
926e1ebb9ecSlm66018  */
92734683adeSsg70180 static int
928da14cebeSEric Cheng vsw_get_md_smodes(vsw_t *vswp, md_t *mdp, mde_cookie_t node, uint8_t *mode)
92934683adeSsg70180 {
93034683adeSsg70180 	int		len = 0;
93134683adeSsg70180 	char		*smode = NULL;
93234683adeSsg70180 	char		*curr_mode = NULL;
93334683adeSsg70180 
93434683adeSsg70180 	D1(vswp, "%s: enter", __func__);
9351ae08745Sheppo 
9361ae08745Sheppo 	/*
9371ae08745Sheppo 	 * Get the switch-mode property. The modes are listed in
9381ae08745Sheppo 	 * decreasing order of preference, i.e. prefered mode is
9391ae08745Sheppo 	 * first item in list.
9401ae08745Sheppo 	 */
9411ae08745Sheppo 	len = 0;
94234683adeSsg70180 	if (md_get_prop_data(mdp, node, smode_propname,
9431ae08745Sheppo 	    (uint8_t **)(&smode), &len) != 0) {
9441ae08745Sheppo 		/*
945e1ebb9ecSlm66018 		 * Unable to get switch-mode property from MD, nothing
946e1ebb9ecSlm66018 		 * more we can do.
9471ae08745Sheppo 		 */
94834683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get switch mode property"
94934683adeSsg70180 		    " from the MD", vswp->instance);
95034683adeSsg70180 		return (1);
951e1ebb9ecSlm66018 	}
952e1ebb9ecSlm66018 
9531ae08745Sheppo 	curr_mode = smode;
9541ae08745Sheppo 	/*
9551ae08745Sheppo 	 * Modes of operation:
9561ae08745Sheppo 	 * 'switched'	 - layer 2 switching, underlying HW in
957e1ebb9ecSlm66018 	 *			programmed mode.
9581ae08745Sheppo 	 * 'promiscuous' - layer 2 switching, underlying HW in
9591ae08745Sheppo 	 *			promiscuous mode.
9601ae08745Sheppo 	 * 'routed'	 - layer 3 (i.e. IP) routing, underlying HW
9611ae08745Sheppo 	 *			in non-promiscuous mode.
9621ae08745Sheppo 	 */
963da14cebeSEric Cheng 	while (curr_mode < (smode + len)) {
9641ae08745Sheppo 		D2(vswp, "%s: curr_mode = [%s]", __func__, curr_mode);
965e1ebb9ecSlm66018 		if (strcmp(curr_mode, "switched") == 0) {
966da14cebeSEric Cheng 			*mode = VSW_LAYER2;
967e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "promiscuous") == 0) {
968da14cebeSEric Cheng 			*mode = VSW_LAYER2 | VSW_LAYER2_PROMISC;
969e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "routed") == 0) {
970da14cebeSEric Cheng 			*mode = VSW_LAYER3;
971e1ebb9ecSlm66018 		} else {
972da14cebeSEric Cheng 			cmn_err(CE_WARN, "!vsw%d: Unknown switch mode %s, "
973da14cebeSEric Cheng 			    "setting to default switched mode",
974da14cebeSEric Cheng 			    vswp->instance, curr_mode);
975da14cebeSEric Cheng 			*mode = VSW_LAYER2;
9761ae08745Sheppo 		}
9771ae08745Sheppo 		curr_mode += strlen(curr_mode) + 1;
9781ae08745Sheppo 	}
9791ae08745Sheppo 
980da14cebeSEric Cheng 	D2(vswp, "%s: %d mode", __func__, *mode);
9811ae08745Sheppo 
9821ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
98334683adeSsg70180 
98434683adeSsg70180 	return (0);
9851ae08745Sheppo }
9861ae08745Sheppo 
987e1ebb9ecSlm66018 /*
9881ae08745Sheppo  * Register with the MAC layer as a network device, so we
9891ae08745Sheppo  * can be plumbed if necessary.
9901ae08745Sheppo  */
9911ae08745Sheppo static int
9921ae08745Sheppo vsw_mac_register(vsw_t *vswp)
9931ae08745Sheppo {
994ba2e4443Sseb 	mac_register_t	*macp;
995ba2e4443Sseb 	int		rv;
9961ae08745Sheppo 
9971ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
9981ae08745Sheppo 
999ba2e4443Sseb 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
1000ba2e4443Sseb 		return (EINVAL);
1001ba2e4443Sseb 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
10021ae08745Sheppo 	macp->m_driver = vswp;
1003ba2e4443Sseb 	macp->m_dip = vswp->dip;
1004ba2e4443Sseb 	macp->m_src_addr = (uint8_t *)&vswp->if_addr;
1005ba2e4443Sseb 	macp->m_callbacks = &vsw_m_callbacks;
1006ba2e4443Sseb 	macp->m_min_sdu = 0;
10077b1f684aSSriharsha Basavapatna 	macp->m_max_sdu = vswp->mtu;
1008c1c61f44Ssb155480 	macp->m_margin = VLAN_TAGSZ;
1009ba2e4443Sseb 	rv = mac_register(macp, &vswp->if_mh);
1010ba2e4443Sseb 	mac_free(macp);
101119b65a69Ssb155480 	if (rv != 0) {
101219b65a69Ssb155480 		/*
101319b65a69Ssb155480 		 * Treat this as a non-fatal error as we may be
101419b65a69Ssb155480 		 * able to operate in some other mode.
101519b65a69Ssb155480 		 */
101619b65a69Ssb155480 		cmn_err(CE_NOTE, "!vsw%d: Unable to register as "
101719b65a69Ssb155480 		    "a provider with MAC layer", vswp->instance);
101819b65a69Ssb155480 		return (rv);
101919b65a69Ssb155480 	}
102019b65a69Ssb155480 
1021ba2e4443Sseb 	vswp->if_state |= VSW_IF_REG;
10221ae08745Sheppo 
10231ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
10241ae08745Sheppo 
10251ae08745Sheppo 	return (rv);
10261ae08745Sheppo }
10271ae08745Sheppo 
10281ae08745Sheppo static int
10291ae08745Sheppo vsw_mac_unregister(vsw_t *vswp)
10301ae08745Sheppo {
10311ae08745Sheppo 	int		rv = 0;
10321ae08745Sheppo 
10331ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10341ae08745Sheppo 
10351ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
10361ae08745Sheppo 
1037ba2e4443Sseb 	if (vswp->if_state & VSW_IF_REG) {
1038ba2e4443Sseb 		rv = mac_unregister(vswp->if_mh);
10391ae08745Sheppo 		if (rv != 0) {
10401ae08745Sheppo 			DWARN(vswp, "%s: unable to unregister from MAC "
10411ae08745Sheppo 			    "framework", __func__);
10421ae08745Sheppo 
10431ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
10441ae08745Sheppo 			D1(vswp, "%s: fail exit", __func__);
10451ae08745Sheppo 			return (rv);
10461ae08745Sheppo 		}
10471ae08745Sheppo 
1048ba2e4443Sseb 		/* mark i/f as down and unregistered */
1049ba2e4443Sseb 		vswp->if_state &= ~(VSW_IF_UP | VSW_IF_REG);
10501ae08745Sheppo 	}
10511ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
10521ae08745Sheppo 
10531ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
10541ae08745Sheppo 
10551ae08745Sheppo 	return (rv);
10561ae08745Sheppo }
10571ae08745Sheppo 
1058ba2e4443Sseb static int
1059ba2e4443Sseb vsw_m_stat(void *arg, uint_t stat, uint64_t *val)
10601ae08745Sheppo {
10611ae08745Sheppo 	vsw_t			*vswp = (vsw_t *)arg;
10621ae08745Sheppo 
10631ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10641ae08745Sheppo 
1065da14cebeSEric Cheng 	mutex_enter(&vswp->mac_lock);
106634683adeSsg70180 	if (vswp->mh == NULL) {
1067da14cebeSEric Cheng 		mutex_exit(&vswp->mac_lock);
1068ba2e4443Sseb 		return (EINVAL);
106934683adeSsg70180 	}
10701ae08745Sheppo 
10711ae08745Sheppo 	/* return stats from underlying device */
1072ba2e4443Sseb 	*val = mac_stat_get(vswp->mh, stat);
107334683adeSsg70180 
1074da14cebeSEric Cheng 	mutex_exit(&vswp->mac_lock);
107534683adeSsg70180 
1076ba2e4443Sseb 	return (0);
10771ae08745Sheppo }
10781ae08745Sheppo 
10791ae08745Sheppo static void
10801ae08745Sheppo vsw_m_stop(void *arg)
10811ae08745Sheppo {
10821ae08745Sheppo 	vsw_t	*vswp = (vsw_t *)arg;
10831ae08745Sheppo 
10841ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10851ae08745Sheppo 
10861ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
10871ae08745Sheppo 	vswp->if_state &= ~VSW_IF_UP;
10881ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
10891ae08745Sheppo 
1090da14cebeSEric Cheng 	/* Cleanup and close the mac client */
1091da14cebeSEric Cheng 	vsw_mac_client_cleanup(vswp, NULL, VSW_LOCALDEV);
10925f94e909Ssg70180 
10931ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
10941ae08745Sheppo }
10951ae08745Sheppo 
10961ae08745Sheppo static int
10971ae08745Sheppo vsw_m_start(void *arg)
10981ae08745Sheppo {
1099da14cebeSEric Cheng 	int		rv;
11001ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
11011ae08745Sheppo 
11021ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11031ae08745Sheppo 
11041ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
11051ae08745Sheppo 
110619b65a69Ssb155480 	vswp->if_state |= VSW_IF_UP;
110719b65a69Ssb155480 
110819b65a69Ssb155480 	if (vswp->switching_setup_done == B_FALSE) {
110919b65a69Ssb155480 		/*
111019b65a69Ssb155480 		 * If the switching mode has not been setup yet, just
111119b65a69Ssb155480 		 * return. The unicast address will be programmed
111219b65a69Ssb155480 		 * after the physical device is successfully setup by the
111319b65a69Ssb155480 		 * timeout handler.
111419b65a69Ssb155480 		 */
111519b65a69Ssb155480 		RW_EXIT(&vswp->if_lockrw);
111619b65a69Ssb155480 		return (0);
111719b65a69Ssb155480 	}
111819b65a69Ssb155480 
111919b65a69Ssb155480 	/* if in layer2 mode, program unicast address. */
112019b65a69Ssb155480 	if (vswp->mh != NULL) {
1121da14cebeSEric Cheng 		/* Init a mac client and program addresses */
1122da14cebeSEric Cheng 		rv = vsw_mac_client_init(vswp, NULL, VSW_LOCALDEV);
1123da14cebeSEric Cheng 		if (rv != 0) {
1124da14cebeSEric Cheng 			cmn_err(CE_NOTE,
1125da14cebeSEric Cheng 			    "!vsw%d: failed to program interface "
1126da14cebeSEric Cheng 			    "unicast address\n", vswp->instance);
1127da14cebeSEric Cheng 		}
112819b65a69Ssb155480 	}
112919b65a69Ssb155480 
113019b65a69Ssb155480 	RW_EXIT(&vswp->if_lockrw);
11315f94e909Ssg70180 
11321ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
11331ae08745Sheppo 	return (0);
11341ae08745Sheppo }
11351ae08745Sheppo 
11361ae08745Sheppo /*
11371ae08745Sheppo  * Change the local interface address.
11385f94e909Ssg70180  *
11395f94e909Ssg70180  * Note: we don't support this entry point. The local
11405f94e909Ssg70180  * mac address of the switch can only be changed via its
11415f94e909Ssg70180  * MD node properties.
11421ae08745Sheppo  */
11431ae08745Sheppo static int
11441ae08745Sheppo vsw_m_unicst(void *arg, const uint8_t *macaddr)
11451ae08745Sheppo {
11465f94e909Ssg70180 	_NOTE(ARGUNUSED(arg, macaddr))
11471ae08745Sheppo 
11485f94e909Ssg70180 	return (DDI_FAILURE);
11491ae08745Sheppo }
11501ae08745Sheppo 
11511ae08745Sheppo static int
11521ae08745Sheppo vsw_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
11531ae08745Sheppo {
11541ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
11551ae08745Sheppo 	mcst_addr_t	*mcst_p = NULL;
11561ae08745Sheppo 	uint64_t	addr = 0x0;
1157e1ebb9ecSlm66018 	int		i, ret = 0;
11581ae08745Sheppo 
11591ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11601ae08745Sheppo 
11611ae08745Sheppo 	/*
11621ae08745Sheppo 	 * Convert address into form that can be used
11631ae08745Sheppo 	 * as hash table key.
11641ae08745Sheppo 	 */
11651ae08745Sheppo 	for (i = 0; i < ETHERADDRL; i++) {
11661ae08745Sheppo 		addr = (addr << 8) | mca[i];
11671ae08745Sheppo 	}
11681ae08745Sheppo 
11691ae08745Sheppo 	D2(vswp, "%s: addr = 0x%llx", __func__, addr);
11701ae08745Sheppo 
11711ae08745Sheppo 	if (add) {
11721ae08745Sheppo 		D2(vswp, "%s: adding multicast", __func__);
11731ae08745Sheppo 		if (vsw_add_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
11741ae08745Sheppo 			/*
11751ae08745Sheppo 			 * Update the list of multicast addresses
11761ae08745Sheppo 			 * contained within the vsw_t structure to
11771ae08745Sheppo 			 * include this new one.
11781ae08745Sheppo 			 */
11791ae08745Sheppo 			mcst_p = kmem_zalloc(sizeof (mcst_addr_t), KM_NOSLEEP);
11801ae08745Sheppo 			if (mcst_p == NULL) {
11811ae08745Sheppo 				DERR(vswp, "%s unable to alloc mem", __func__);
118219b65a69Ssb155480 				(void) vsw_del_mcst(vswp,
118319b65a69Ssb155480 				    VSW_LOCALDEV, addr, NULL);
11841ae08745Sheppo 				return (1);
11851ae08745Sheppo 			}
11861ae08745Sheppo 			mcst_p->addr = addr;
118719b65a69Ssb155480 			ether_copy(mca, &mcst_p->mca);
11881ae08745Sheppo 
11891ae08745Sheppo 			/*
11901ae08745Sheppo 			 * Call into the underlying driver to program the
11911ae08745Sheppo 			 * address into HW.
11921ae08745Sheppo 			 */
1193da14cebeSEric Cheng 			ret = vsw_mac_multicast_add(vswp, NULL, mcst_p,
1194da14cebeSEric Cheng 			    VSW_LOCALDEV);
1195e1ebb9ecSlm66018 			if (ret != 0) {
119619b65a69Ssb155480 				(void) vsw_del_mcst(vswp,
119719b65a69Ssb155480 				    VSW_LOCALDEV, addr, NULL);
119819b65a69Ssb155480 				kmem_free(mcst_p, sizeof (*mcst_p));
119919b65a69Ssb155480 				return (ret);
1200e1ebb9ecSlm66018 			}
120119b65a69Ssb155480 
120219b65a69Ssb155480 			mutex_enter(&vswp->mca_lock);
120319b65a69Ssb155480 			mcst_p->nextp = vswp->mcap;
120419b65a69Ssb155480 			vswp->mcap = mcst_p;
120519b65a69Ssb155480 			mutex_exit(&vswp->mca_lock);
12061ae08745Sheppo 		} else {
1207da14cebeSEric Cheng 			cmn_err(CE_WARN, "!vsw%d: unable to add multicast "
120834683adeSsg70180 			    "address", vswp->instance);
1209e1ebb9ecSlm66018 		}
1210e1ebb9ecSlm66018 		return (ret);
1211e1ebb9ecSlm66018 	}
1212e1ebb9ecSlm66018 
12131ae08745Sheppo 	D2(vswp, "%s: removing multicast", __func__);
12141ae08745Sheppo 	/*
12151ae08745Sheppo 	 * Remove the address from the hash table..
12161ae08745Sheppo 	 */
12171ae08745Sheppo 	if (vsw_del_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
12181ae08745Sheppo 
12191ae08745Sheppo 		/*
12201ae08745Sheppo 		 * ..and then from the list maintained in the
12211ae08745Sheppo 		 * vsw_t structure.
12221ae08745Sheppo 		 */
122319b65a69Ssb155480 		mcst_p = vsw_del_addr(VSW_LOCALDEV, vswp, addr);
122419b65a69Ssb155480 		ASSERT(mcst_p != NULL);
12251ae08745Sheppo 
1226da14cebeSEric Cheng 		vsw_mac_multicast_remove(vswp, NULL, mcst_p, VSW_LOCALDEV);
122719b65a69Ssb155480 		kmem_free(mcst_p, sizeof (*mcst_p));
12281ae08745Sheppo 	}
12291ae08745Sheppo 
12301ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
12311ae08745Sheppo 
12321ae08745Sheppo 	return (0);
12331ae08745Sheppo }
12341ae08745Sheppo 
12351ae08745Sheppo static int
12361ae08745Sheppo vsw_m_promisc(void *arg, boolean_t on)
12371ae08745Sheppo {
12381ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
12391ae08745Sheppo 
12401ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
12411ae08745Sheppo 
12421ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
12431ae08745Sheppo 	if (on)
12441ae08745Sheppo 		vswp->if_state |= VSW_IF_PROMISC;
12451ae08745Sheppo 	else
12461ae08745Sheppo 		vswp->if_state &= ~VSW_IF_PROMISC;
12471ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
12481ae08745Sheppo 
12491ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
12501ae08745Sheppo 
12511ae08745Sheppo 	return (0);
12521ae08745Sheppo }
12531ae08745Sheppo 
12541ae08745Sheppo static mblk_t *
12551ae08745Sheppo vsw_m_tx(void *arg, mblk_t *mp)
12561ae08745Sheppo {
12571ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
12581ae08745Sheppo 
12591ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
12601ae08745Sheppo 
1261c1c61f44Ssb155480 	mp = vsw_vlan_frame_pretag(vswp, VSW_LOCALDEV, mp);
1262c1c61f44Ssb155480 
1263c1c61f44Ssb155480 	if (mp == NULL) {
1264c1c61f44Ssb155480 		return (NULL);
1265c1c61f44Ssb155480 	}
1266c1c61f44Ssb155480 
126734683adeSsg70180 	vswp->vsw_switch_frame(vswp, mp, VSW_LOCALDEV, NULL, NULL);
12681ae08745Sheppo 
12691ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
12701ae08745Sheppo 
12711ae08745Sheppo 	return (NULL);
12721ae08745Sheppo }
12731ae08745Sheppo 
12741ae08745Sheppo /*
12751ae08745Sheppo  * Register for machine description (MD) updates.
127634683adeSsg70180  *
127734683adeSsg70180  * Returns 0 on success, 1 on failure.
12781ae08745Sheppo  */
127934683adeSsg70180 static int
12801ae08745Sheppo vsw_mdeg_register(vsw_t *vswp)
12811ae08745Sheppo {
12821ae08745Sheppo 	mdeg_prop_spec_t	*pspecp;
12831ae08745Sheppo 	mdeg_node_spec_t	*inst_specp;
128434683adeSsg70180 	mdeg_handle_t		mdeg_hdl, mdeg_port_hdl;
12851ae08745Sheppo 	size_t			templatesz;
128619b65a69Ssb155480 	int			rv;
12871ae08745Sheppo 
12881ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
12891ae08745Sheppo 
129034683adeSsg70180 	/*
12911ae08745Sheppo 	 * Allocate and initialize a per-instance copy
12921ae08745Sheppo 	 * of the global property spec array that will
12931ae08745Sheppo 	 * uniquely identify this vsw instance.
12941ae08745Sheppo 	 */
12951ae08745Sheppo 	templatesz = sizeof (vsw_prop_template);
12961ae08745Sheppo 	pspecp = kmem_zalloc(templatesz, KM_SLEEP);
12971ae08745Sheppo 
12981ae08745Sheppo 	bcopy(vsw_prop_template, pspecp, templatesz);
12991ae08745Sheppo 
130019b65a69Ssb155480 	VSW_SET_MDEG_PROP_INST(pspecp, vswp->regprop);
13011ae08745Sheppo 
13021ae08745Sheppo 	/* initialize the complete prop spec structure */
13031ae08745Sheppo 	inst_specp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
13041ae08745Sheppo 	inst_specp->namep = "virtual-device";
13051ae08745Sheppo 	inst_specp->specp = pspecp;
13061ae08745Sheppo 
130719b65a69Ssb155480 	D2(vswp, "%s: instance %d registering with mdeg", __func__,
130819b65a69Ssb155480 	    vswp->regprop);
130934683adeSsg70180 	/*
131034683adeSsg70180 	 * Register an interest in 'virtual-device' nodes with a
131134683adeSsg70180 	 * 'name' property of 'virtual-network-switch'
131234683adeSsg70180 	 */
131334683adeSsg70180 	rv = mdeg_register(inst_specp, &vdev_match, vsw_mdeg_cb,
13141ae08745Sheppo 	    (void *)vswp, &mdeg_hdl);
131534683adeSsg70180 	if (rv != MDEG_SUCCESS) {
131634683adeSsg70180 		DERR(vswp, "%s: mdeg_register failed (%d) for vsw node",
131734683adeSsg70180 		    __func__, rv);
131834683adeSsg70180 		goto mdeg_reg_fail;
131934683adeSsg70180 	}
13201ae08745Sheppo 
132134683adeSsg70180 	/*
132234683adeSsg70180 	 * Register an interest in 'vsw-port' nodes.
132334683adeSsg70180 	 */
132434683adeSsg70180 	rv = mdeg_register(inst_specp, &vport_match, vsw_port_mdeg_cb,
132534683adeSsg70180 	    (void *)vswp, &mdeg_port_hdl);
13261ae08745Sheppo 	if (rv != MDEG_SUCCESS) {
13271ae08745Sheppo 		DERR(vswp, "%s: mdeg_register failed (%d)\n", __func__, rv);
132834683adeSsg70180 		(void) mdeg_unregister(mdeg_hdl);
132934683adeSsg70180 		goto mdeg_reg_fail;
13301ae08745Sheppo 	}
13311ae08745Sheppo 
13321ae08745Sheppo 	/* save off data that will be needed later */
13331ae08745Sheppo 	vswp->inst_spec = inst_specp;
13341ae08745Sheppo 	vswp->mdeg_hdl = mdeg_hdl;
133534683adeSsg70180 	vswp->mdeg_port_hdl = mdeg_port_hdl;
13361ae08745Sheppo 
13371ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
133834683adeSsg70180 	return (0);
133934683adeSsg70180 
134034683adeSsg70180 mdeg_reg_fail:
134134683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: Unable to register MDEG callbacks",
134234683adeSsg70180 	    vswp->instance);
134334683adeSsg70180 	kmem_free(pspecp, templatesz);
134434683adeSsg70180 	kmem_free(inst_specp, sizeof (mdeg_node_spec_t));
134534683adeSsg70180 
134634683adeSsg70180 	vswp->mdeg_hdl = NULL;
134734683adeSsg70180 	vswp->mdeg_port_hdl = NULL;
134834683adeSsg70180 
134934683adeSsg70180 	return (1);
13501ae08745Sheppo }
13511ae08745Sheppo 
13521ae08745Sheppo static void
13531ae08745Sheppo vsw_mdeg_unregister(vsw_t *vswp)
13541ae08745Sheppo {
13551ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: enter");
13561ae08745Sheppo 
135734683adeSsg70180 	if (vswp->mdeg_hdl != NULL)
13581ae08745Sheppo 		(void) mdeg_unregister(vswp->mdeg_hdl);
13591ae08745Sheppo 
136034683adeSsg70180 	if (vswp->mdeg_port_hdl != NULL)
136134683adeSsg70180 		(void) mdeg_unregister(vswp->mdeg_port_hdl);
136234683adeSsg70180 
136334683adeSsg70180 	if (vswp->inst_spec != NULL) {
13641ae08745Sheppo 		if (vswp->inst_spec->specp != NULL) {
13651ae08745Sheppo 			(void) kmem_free(vswp->inst_spec->specp,
13661ae08745Sheppo 			    sizeof (vsw_prop_template));
13671ae08745Sheppo 			vswp->inst_spec->specp = NULL;
13681ae08745Sheppo 		}
13691ae08745Sheppo 
1370205eeb1aSlm66018 		(void) kmem_free(vswp->inst_spec, sizeof (mdeg_node_spec_t));
13711ae08745Sheppo 		vswp->inst_spec = NULL;
13721ae08745Sheppo 	}
13731ae08745Sheppo 
13741ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: exit");
13751ae08745Sheppo }
13761ae08745Sheppo 
137734683adeSsg70180 /*
137834683adeSsg70180  * Mdeg callback invoked for the vsw node itself.
137934683adeSsg70180  */
13801ae08745Sheppo static int
13811ae08745Sheppo vsw_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
13821ae08745Sheppo {
13831ae08745Sheppo 	vsw_t		*vswp;
13841ae08745Sheppo 	md_t		*mdp;
13851ae08745Sheppo 	mde_cookie_t	node;
13861ae08745Sheppo 	uint64_t	inst;
138734683adeSsg70180 	char		*node_name = NULL;
13881ae08745Sheppo 
13891ae08745Sheppo 	if (resp == NULL)
13901ae08745Sheppo 		return (MDEG_FAILURE);
13911ae08745Sheppo 
13921ae08745Sheppo 	vswp = (vsw_t *)cb_argp;
13931ae08745Sheppo 
139434683adeSsg70180 	D1(vswp, "%s: added %d : removed %d : curr matched %d"
139534683adeSsg70180 	    " : prev matched %d", __func__, resp->added.nelem,
139634683adeSsg70180 	    resp->removed.nelem, resp->match_curr.nelem,
139734683adeSsg70180 	    resp->match_prev.nelem);
139834683adeSsg70180 
139934683adeSsg70180 	/*
140019b65a69Ssb155480 	 * We get an initial callback for this node as 'added'
140119b65a69Ssb155480 	 * after registering with mdeg. Note that we would have
140219b65a69Ssb155480 	 * already gathered information about this vsw node by
140319b65a69Ssb155480 	 * walking MD earlier during attach (in vsw_read_mdprops()).
140419b65a69Ssb155480 	 * So, there is a window where the properties of this
140519b65a69Ssb155480 	 * node might have changed when we get this initial 'added'
140619b65a69Ssb155480 	 * callback. We handle this as if an update occured
140719b65a69Ssb155480 	 * and invoke the same function which handles updates to
140819b65a69Ssb155480 	 * the properties of this vsw-node if any.
140919b65a69Ssb155480 	 *
141034683adeSsg70180 	 * A non-zero 'match' value indicates that the MD has been
141119b65a69Ssb155480 	 * updated and that a virtual-network-switch node is
141219b65a69Ssb155480 	 * present which may or may not have been updated. It is
141319b65a69Ssb155480 	 * up to the clients to examine their own nodes and
141419b65a69Ssb155480 	 * determine if they have changed.
141534683adeSsg70180 	 */
141619b65a69Ssb155480 	if (resp->added.nelem != 0) {
141734683adeSsg70180 
141819b65a69Ssb155480 		if (resp->added.nelem != 1) {
141919b65a69Ssb155480 			cmn_err(CE_NOTE, "!vsw%d: number of nodes added "
142019b65a69Ssb155480 			    "invalid: %d\n", vswp->instance, resp->added.nelem);
142119b65a69Ssb155480 			return (MDEG_FAILURE);
142219b65a69Ssb155480 		}
142319b65a69Ssb155480 
142419b65a69Ssb155480 		mdp = resp->added.mdp;
142519b65a69Ssb155480 		node = resp->added.mdep[0];
142619b65a69Ssb155480 
142719b65a69Ssb155480 	} else if (resp->match_curr.nelem != 0) {
142819b65a69Ssb155480 
142919b65a69Ssb155480 		if (resp->match_curr.nelem != 1) {
143019b65a69Ssb155480 			cmn_err(CE_NOTE, "!vsw%d: number of nodes updated "
143119b65a69Ssb155480 			    "invalid: %d\n", vswp->instance,
143219b65a69Ssb155480 			    resp->match_curr.nelem);
143319b65a69Ssb155480 			return (MDEG_FAILURE);
143419b65a69Ssb155480 		}
143519b65a69Ssb155480 
143619b65a69Ssb155480 		mdp = resp->match_curr.mdp;
143719b65a69Ssb155480 		node = resp->match_curr.mdep[0];
143819b65a69Ssb155480 
143919b65a69Ssb155480 	} else {
144019b65a69Ssb155480 		return (MDEG_FAILURE);
144119b65a69Ssb155480 	}
144219b65a69Ssb155480 
144319b65a69Ssb155480 	/* Validate name and instance */
144434683adeSsg70180 	if (md_get_prop_str(mdp, node, "name", &node_name) != 0) {
144519b65a69Ssb155480 		DERR(vswp, "%s: unable to get node name\n",  __func__);
144619b65a69Ssb155480 		return (MDEG_FAILURE);
144719b65a69Ssb155480 	}
144819b65a69Ssb155480 
144919b65a69Ssb155480 	/* is this a virtual-network-switch? */
145019b65a69Ssb155480 	if (strcmp(node_name, vsw_propname) != 0) {
145119b65a69Ssb155480 		DERR(vswp, "%s: Invalid node name: %s\n",
145219b65a69Ssb155480 		    __func__, node_name);
145319b65a69Ssb155480 		return (MDEG_FAILURE);
145434683adeSsg70180 	}
145534683adeSsg70180 
145634683adeSsg70180 	if (md_get_prop_val(mdp, node, "cfg-handle", &inst)) {
145719b65a69Ssb155480 		DERR(vswp, "%s: prop(cfg-handle) not found\n",
145819b65a69Ssb155480 		    __func__);
145919b65a69Ssb155480 		return (MDEG_FAILURE);
146034683adeSsg70180 	}
146134683adeSsg70180 
146219b65a69Ssb155480 	/* is this the right instance of vsw? */
146319b65a69Ssb155480 	if (inst != vswp->regprop) {
146419b65a69Ssb155480 		DERR(vswp, "%s: Invalid cfg-handle: %lx\n",
146519b65a69Ssb155480 		    __func__, inst);
146619b65a69Ssb155480 		return (MDEG_FAILURE);
146719b65a69Ssb155480 	}
146834683adeSsg70180 
146934683adeSsg70180 	vsw_update_md_prop(vswp, mdp, node);
147034683adeSsg70180 
147134683adeSsg70180 	return (MDEG_SUCCESS);
147234683adeSsg70180 }
147334683adeSsg70180 
147434683adeSsg70180 /*
147534683adeSsg70180  * Mdeg callback invoked for changes to the vsw-port nodes
147634683adeSsg70180  * under the vsw node.
147734683adeSsg70180  */
147834683adeSsg70180 static int
147934683adeSsg70180 vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
148034683adeSsg70180 {
148134683adeSsg70180 	vsw_t		*vswp;
148234683adeSsg70180 	int		idx;
148334683adeSsg70180 	md_t		*mdp;
148434683adeSsg70180 	mde_cookie_t	node;
148534683adeSsg70180 	uint64_t	inst;
14861ef0bbb5Snarayan 	int		rv;
148734683adeSsg70180 
148834683adeSsg70180 	if ((resp == NULL) || (cb_argp == NULL))
148934683adeSsg70180 		return (MDEG_FAILURE);
149034683adeSsg70180 
149134683adeSsg70180 	vswp = (vsw_t *)cb_argp;
149234683adeSsg70180 
149334683adeSsg70180 	D2(vswp, "%s: added %d : removed %d : curr matched %d"
149434683adeSsg70180 	    " : prev matched %d", __func__, resp->added.nelem,
149534683adeSsg70180 	    resp->removed.nelem, resp->match_curr.nelem,
14961ae08745Sheppo 	    resp->match_prev.nelem);
14971ae08745Sheppo 
14981ae08745Sheppo 	/* process added ports */
14991ae08745Sheppo 	for (idx = 0; idx < resp->added.nelem; idx++) {
15001ae08745Sheppo 		mdp = resp->added.mdp;
15011ae08745Sheppo 		node = resp->added.mdep[idx];
15021ae08745Sheppo 
15031ae08745Sheppo 		D2(vswp, "%s: adding node(%d) 0x%lx", __func__, idx, node);
15041ae08745Sheppo 
15051ef0bbb5Snarayan 		if ((rv = vsw_port_add(vswp, mdp, &node)) != 0) {
150634683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to add new port "
15071ef0bbb5Snarayan 			    "(0x%lx), err=%d", vswp->instance, node, rv);
15081ae08745Sheppo 		}
15091ae08745Sheppo 	}
15101ae08745Sheppo 
15111ae08745Sheppo 	/* process removed ports */
15121ae08745Sheppo 	for (idx = 0; idx < resp->removed.nelem; idx++) {
15131ae08745Sheppo 		mdp = resp->removed.mdp;
15141ae08745Sheppo 		node = resp->removed.mdep[idx];
15151ae08745Sheppo 
15161ae08745Sheppo 		if (md_get_prop_val(mdp, node, id_propname, &inst)) {
151734683adeSsg70180 			DERR(vswp, "%s: prop(%s) not found in port(%d)",
15181ae08745Sheppo 			    __func__, id_propname, idx);
15191ae08745Sheppo 			continue;
15201ae08745Sheppo 		}
15211ae08745Sheppo 
15221ae08745Sheppo 		D2(vswp, "%s: removing node(%d) 0x%lx", __func__, idx, node);
15231ae08745Sheppo 
15241ae08745Sheppo 		if (vsw_port_detach(vswp, inst) != 0) {
152534683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to remove port %ld",
152634683adeSsg70180 			    vswp->instance, inst);
15271ae08745Sheppo 		}
15281ae08745Sheppo 	}
15291ae08745Sheppo 
1530c1c61f44Ssb155480 	for (idx = 0; idx < resp->match_curr.nelem; idx++) {
1531c1c61f44Ssb155480 		(void) vsw_port_update(vswp, resp->match_curr.mdp,
1532c1c61f44Ssb155480 		    resp->match_curr.mdep[idx],
1533c1c61f44Ssb155480 		    resp->match_prev.mdp,
1534c1c61f44Ssb155480 		    resp->match_prev.mdep[idx]);
1535c1c61f44Ssb155480 	}
15361ae08745Sheppo 
15371ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
15381ae08745Sheppo 
15391ae08745Sheppo 	return (MDEG_SUCCESS);
15401ae08745Sheppo }
15411ae08745Sheppo 
15421ae08745Sheppo /*
154319b65a69Ssb155480  * Scan the machine description for this instance of vsw
154419b65a69Ssb155480  * and read its properties. Called only from vsw_attach().
154519b65a69Ssb155480  * Returns: 0 on success, 1 on failure.
154619b65a69Ssb155480  */
154719b65a69Ssb155480 static int
154819b65a69Ssb155480 vsw_read_mdprops(vsw_t *vswp)
154919b65a69Ssb155480 {
155019b65a69Ssb155480 	md_t		*mdp = NULL;
155119b65a69Ssb155480 	mde_cookie_t	rootnode;
155219b65a69Ssb155480 	mde_cookie_t	*listp = NULL;
155319b65a69Ssb155480 	uint64_t	inst;
155419b65a69Ssb155480 	uint64_t	cfgh;
155519b65a69Ssb155480 	char		*name;
155619b65a69Ssb155480 	int		rv = 1;
155719b65a69Ssb155480 	int		num_nodes = 0;
155819b65a69Ssb155480 	int		num_devs = 0;
155919b65a69Ssb155480 	int		listsz = 0;
156019b65a69Ssb155480 	int		i;
156119b65a69Ssb155480 
156219b65a69Ssb155480 	/*
156319b65a69Ssb155480 	 * In each 'virtual-device' node in the MD there is a
156419b65a69Ssb155480 	 * 'cfg-handle' property which is the MD's concept of
156519b65a69Ssb155480 	 * an instance number (this may be completely different from
156619b65a69Ssb155480 	 * the device drivers instance #). OBP reads that value and
156719b65a69Ssb155480 	 * stores it in the 'reg' property of the appropriate node in
156819b65a69Ssb155480 	 * the device tree. We first read this reg property and use this
156919b65a69Ssb155480 	 * to compare against the 'cfg-handle' property of vsw nodes
157019b65a69Ssb155480 	 * in MD to get to this specific vsw instance and then read
157119b65a69Ssb155480 	 * other properties that we are interested in.
157219b65a69Ssb155480 	 * We also cache the value of 'reg' property and use it later
157319b65a69Ssb155480 	 * to register callbacks with mdeg (see vsw_mdeg_register())
157419b65a69Ssb155480 	 */
157519b65a69Ssb155480 	inst = ddi_prop_get_int(DDI_DEV_T_ANY, vswp->dip,
157619b65a69Ssb155480 	    DDI_PROP_DONTPASS, reg_propname, -1);
157719b65a69Ssb155480 	if (inst == -1) {
157819b65a69Ssb155480 		cmn_err(CE_NOTE, "!vsw%d: Unable to read %s property from "
157919b65a69Ssb155480 		    "OBP device tree", vswp->instance, reg_propname);
158019b65a69Ssb155480 		return (rv);
158119b65a69Ssb155480 	}
158219b65a69Ssb155480 
158319b65a69Ssb155480 	vswp->regprop = inst;
158419b65a69Ssb155480 
158519b65a69Ssb155480 	if ((mdp = md_get_handle()) == NULL) {
158619b65a69Ssb155480 		DWARN(vswp, "%s: cannot init MD\n", __func__);
158719b65a69Ssb155480 		return (rv);
158819b65a69Ssb155480 	}
158919b65a69Ssb155480 
159019b65a69Ssb155480 	num_nodes = md_node_count(mdp);
159119b65a69Ssb155480 	ASSERT(num_nodes > 0);
159219b65a69Ssb155480 
159319b65a69Ssb155480 	listsz = num_nodes * sizeof (mde_cookie_t);
159419b65a69Ssb155480 	listp = (mde_cookie_t *)kmem_zalloc(listsz, KM_SLEEP);
159519b65a69Ssb155480 
159619b65a69Ssb155480 	rootnode = md_root_node(mdp);
159719b65a69Ssb155480 
159819b65a69Ssb155480 	/* search for all "virtual_device" nodes */
159919b65a69Ssb155480 	num_devs = md_scan_dag(mdp, rootnode,
160019b65a69Ssb155480 	    md_find_name(mdp, vdev_propname),
160119b65a69Ssb155480 	    md_find_name(mdp, "fwd"), listp);
160219b65a69Ssb155480 	if (num_devs <= 0) {
160319b65a69Ssb155480 		DWARN(vswp, "%s: invalid num_devs:%d\n", __func__, num_devs);
160419b65a69Ssb155480 		goto vsw_readmd_exit;
160519b65a69Ssb155480 	}
160619b65a69Ssb155480 
160719b65a69Ssb155480 	/*
160819b65a69Ssb155480 	 * Now loop through the list of virtual-devices looking for
160919b65a69Ssb155480 	 * devices with name "virtual-network-switch" and for each
161019b65a69Ssb155480 	 * such device compare its instance with what we have from
161119b65a69Ssb155480 	 * the 'reg' property to find the right node in MD and then
161219b65a69Ssb155480 	 * read all its properties.
161319b65a69Ssb155480 	 */
161419b65a69Ssb155480 	for (i = 0; i < num_devs; i++) {
161519b65a69Ssb155480 
161619b65a69Ssb155480 		if (md_get_prop_str(mdp, listp[i], "name", &name) != 0) {
161719b65a69Ssb155480 			DWARN(vswp, "%s: name property not found\n",
161819b65a69Ssb155480 			    __func__);
161919b65a69Ssb155480 			goto vsw_readmd_exit;
162019b65a69Ssb155480 		}
162119b65a69Ssb155480 
162219b65a69Ssb155480 		/* is this a virtual-network-switch? */
162319b65a69Ssb155480 		if (strcmp(name, vsw_propname) != 0)
162419b65a69Ssb155480 			continue;
162519b65a69Ssb155480 
162619b65a69Ssb155480 		if (md_get_prop_val(mdp, listp[i], "cfg-handle", &cfgh) != 0) {
162719b65a69Ssb155480 			DWARN(vswp, "%s: cfg-handle property not found\n",
162819b65a69Ssb155480 			    __func__);
162919b65a69Ssb155480 			goto vsw_readmd_exit;
163019b65a69Ssb155480 		}
163119b65a69Ssb155480 
163219b65a69Ssb155480 		/* is this the required instance of vsw? */
163319b65a69Ssb155480 		if (inst != cfgh)
163419b65a69Ssb155480 			continue;
163519b65a69Ssb155480 
163619b65a69Ssb155480 		/* now read all properties of this vsw instance */
163719b65a69Ssb155480 		rv = vsw_get_initial_md_properties(vswp, mdp, listp[i]);
163819b65a69Ssb155480 		break;
163919b65a69Ssb155480 	}
164019b65a69Ssb155480 
164119b65a69Ssb155480 vsw_readmd_exit:
164219b65a69Ssb155480 
164319b65a69Ssb155480 	kmem_free(listp, listsz);
164419b65a69Ssb155480 	(void) md_fini_handle(mdp);
164519b65a69Ssb155480 	return (rv);
164619b65a69Ssb155480 }
164719b65a69Ssb155480 
164819b65a69Ssb155480 /*
164934683adeSsg70180  * Read the initial start-of-day values from the specified MD node.
165034683adeSsg70180  */
165119b65a69Ssb155480 static int
165234683adeSsg70180 vsw_get_initial_md_properties(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
165334683adeSsg70180 {
165434683adeSsg70180 	uint64_t	macaddr = 0;
165534683adeSsg70180 
165634683adeSsg70180 	D1(vswp, "%s: enter", __func__);
165734683adeSsg70180 
165819b65a69Ssb155480 	if (vsw_get_md_physname(vswp, mdp, node, vswp->physname) != 0) {
165919b65a69Ssb155480 		return (1);
166034683adeSsg70180 	}
166134683adeSsg70180 
166234683adeSsg70180 	/* mac address for vswitch device itself */
166334683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
166434683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
166534683adeSsg70180 		    vswp->instance);
166619b65a69Ssb155480 		return (1);
166719b65a69Ssb155480 	}
166834683adeSsg70180 
166919b65a69Ssb155480 	vsw_save_lmacaddr(vswp, macaddr);
167034683adeSsg70180 
1671da14cebeSEric Cheng 	if (vsw_get_md_smodes(vswp, mdp, node, &vswp->smode)) {
16721ef0bbb5Snarayan 		DWARN(vswp, "%s: Unable to read %s property from MD, "
16731ef0bbb5Snarayan 		    "defaulting to 'switched' mode",
16741ef0bbb5Snarayan 		    __func__, smode_propname);
167534683adeSsg70180 
1676da14cebeSEric Cheng 		vswp->smode = VSW_LAYER2;
167734683adeSsg70180 	}
167834683adeSsg70180 
16797b1f684aSSriharsha Basavapatna 	/* read mtu */
16807b1f684aSSriharsha Basavapatna 	vsw_mtu_read(vswp, mdp, node, &vswp->mtu);
16817b1f684aSSriharsha Basavapatna 	if (vswp->mtu < ETHERMTU || vswp->mtu > VNET_MAX_MTU) {
16827b1f684aSSriharsha Basavapatna 		vswp->mtu = ETHERMTU;
16837b1f684aSSriharsha Basavapatna 	}
16847b1f684aSSriharsha Basavapatna 	vswp->max_frame_size = vswp->mtu + sizeof (struct ether_header) +
16857b1f684aSSriharsha Basavapatna 	    VLAN_TAGSZ;
16867b1f684aSSriharsha Basavapatna 
1687c1c61f44Ssb155480 	/* read vlan id properties of this vsw instance */
1688c1c61f44Ssb155480 	vsw_vlan_read_ids(vswp, VSW_LOCALDEV, mdp, node, &vswp->pvid,
1689c1c61f44Ssb155480 	    &vswp->vids, &vswp->nvids, &vswp->default_vlan_id);
1690c1c61f44Ssb155480 
1691c1c61f44Ssb155480 	/* read priority-ether-types */
1692f0ca1d9aSsb155480 	vsw_read_pri_eth_types(vswp, mdp, node);
1693f0ca1d9aSsb155480 
169434683adeSsg70180 	D1(vswp, "%s: exit", __func__);
169519b65a69Ssb155480 	return (0);
169634683adeSsg70180 }
169734683adeSsg70180 
169834683adeSsg70180 /*
1699c1c61f44Ssb155480  * Read vlan id properties of the given MD node.
1700c1c61f44Ssb155480  * Arguments:
1701c1c61f44Ssb155480  *   arg:          device argument(vsw device or a port)
1702c1c61f44Ssb155480  *   type:         type of arg; VSW_LOCALDEV(vsw device) or VSW_VNETPORT(port)
1703c1c61f44Ssb155480  *   mdp:          machine description
1704c1c61f44Ssb155480  *   node:         md node cookie
1705c1c61f44Ssb155480  *
1706c1c61f44Ssb155480  * Returns:
1707c1c61f44Ssb155480  *   pvidp:        port-vlan-id of the node
1708c1c61f44Ssb155480  *   vidspp:       list of vlan-ids of the node
1709c1c61f44Ssb155480  *   nvidsp:       # of vlan-ids in the list
1710c1c61f44Ssb155480  *   default_idp:  default-vlan-id of the node(if node is vsw device)
1711c1c61f44Ssb155480  */
1712c1c61f44Ssb155480 static void
1713c1c61f44Ssb155480 vsw_vlan_read_ids(void *arg, int type, md_t *mdp, mde_cookie_t node,
1714da14cebeSEric Cheng 	uint16_t *pvidp, vsw_vlanid_t **vidspp, uint16_t *nvidsp,
1715c1c61f44Ssb155480 	uint16_t *default_idp)
1716c1c61f44Ssb155480 {
1717c1c61f44Ssb155480 	vsw_t		*vswp;
1718c1c61f44Ssb155480 	vsw_port_t	*portp;
1719c1c61f44Ssb155480 	char		*pvid_propname;
1720c1c61f44Ssb155480 	char		*vid_propname;
1721c1c61f44Ssb155480 	uint_t		nvids = 0;
1722c1c61f44Ssb155480 	uint32_t	vids_size;
1723c1c61f44Ssb155480 	int		rv;
1724c1c61f44Ssb155480 	int		i;
1725c1c61f44Ssb155480 	uint64_t	*data;
1726c1c61f44Ssb155480 	uint64_t	val;
1727c1c61f44Ssb155480 	int		size;
1728c1c61f44Ssb155480 	int		inst;
1729c1c61f44Ssb155480 
1730c1c61f44Ssb155480 	if (type == VSW_LOCALDEV) {
1731c1c61f44Ssb155480 
1732c1c61f44Ssb155480 		vswp = (vsw_t *)arg;
1733c1c61f44Ssb155480 		pvid_propname = vsw_pvid_propname;
1734c1c61f44Ssb155480 		vid_propname = vsw_vid_propname;
1735c1c61f44Ssb155480 		inst = vswp->instance;
1736c1c61f44Ssb155480 
1737c1c61f44Ssb155480 	} else if (type == VSW_VNETPORT) {
1738c1c61f44Ssb155480 
1739c1c61f44Ssb155480 		portp = (vsw_port_t *)arg;
1740c1c61f44Ssb155480 		vswp = portp->p_vswp;
1741c1c61f44Ssb155480 		pvid_propname = port_pvid_propname;
1742c1c61f44Ssb155480 		vid_propname = port_vid_propname;
1743c1c61f44Ssb155480 		inst = portp->p_instance;
1744c1c61f44Ssb155480 
1745c1c61f44Ssb155480 	} else {
1746c1c61f44Ssb155480 		return;
1747c1c61f44Ssb155480 	}
1748c1c61f44Ssb155480 
1749c1c61f44Ssb155480 	if (type == VSW_LOCALDEV && default_idp != NULL) {
1750c1c61f44Ssb155480 		rv = md_get_prop_val(mdp, node, vsw_dvid_propname, &val);
1751c1c61f44Ssb155480 		if (rv != 0) {
1752c1c61f44Ssb155480 			DWARN(vswp, "%s: prop(%s) not found", __func__,
1753c1c61f44Ssb155480 			    vsw_dvid_propname);
1754c1c61f44Ssb155480 
1755c1c61f44Ssb155480 			*default_idp = vsw_default_vlan_id;
1756c1c61f44Ssb155480 		} else {
1757c1c61f44Ssb155480 			*default_idp = val & 0xFFF;
1758c1c61f44Ssb155480 			D2(vswp, "%s: %s(%d): (%d)\n", __func__,
1759c1c61f44Ssb155480 			    vsw_dvid_propname, inst, *default_idp);
1760c1c61f44Ssb155480 		}
1761c1c61f44Ssb155480 	}
1762c1c61f44Ssb155480 
1763c1c61f44Ssb155480 	rv = md_get_prop_val(mdp, node, pvid_propname, &val);
1764c1c61f44Ssb155480 	if (rv != 0) {
1765c1c61f44Ssb155480 		DWARN(vswp, "%s: prop(%s) not found", __func__, pvid_propname);
1766c1c61f44Ssb155480 		*pvidp = vsw_default_vlan_id;
1767c1c61f44Ssb155480 	} else {
1768c1c61f44Ssb155480 
1769c1c61f44Ssb155480 		*pvidp = val & 0xFFF;
1770c1c61f44Ssb155480 		D2(vswp, "%s: %s(%d): (%d)\n", __func__,
1771c1c61f44Ssb155480 		    pvid_propname, inst, *pvidp);
1772c1c61f44Ssb155480 	}
1773c1c61f44Ssb155480 
1774c1c61f44Ssb155480 	rv = md_get_prop_data(mdp, node, vid_propname, (uint8_t **)&data,
1775c1c61f44Ssb155480 	    &size);
1776c1c61f44Ssb155480 	if (rv != 0) {
1777c1c61f44Ssb155480 		D2(vswp, "%s: prop(%s) not found", __func__, vid_propname);
1778c1c61f44Ssb155480 		size = 0;
1779c1c61f44Ssb155480 	} else {
1780c1c61f44Ssb155480 		size /= sizeof (uint64_t);
1781c1c61f44Ssb155480 	}
1782c1c61f44Ssb155480 	nvids = size;
1783c1c61f44Ssb155480 
1784c1c61f44Ssb155480 	if (nvids != 0) {
1785c1c61f44Ssb155480 		D2(vswp, "%s: %s(%d): ", __func__, vid_propname, inst);
1786da14cebeSEric Cheng 		vids_size = sizeof (vsw_vlanid_t) * nvids;
1787c1c61f44Ssb155480 		*vidspp = kmem_zalloc(vids_size, KM_SLEEP);
1788c1c61f44Ssb155480 		for (i = 0; i < nvids; i++) {
1789da14cebeSEric Cheng 			(*vidspp)[i].vl_vid = data[i] & 0xFFFF;
1790da14cebeSEric Cheng 			(*vidspp)[i].vl_set = B_FALSE;
1791da14cebeSEric Cheng 			D2(vswp, " %d ", (*vidspp)[i].vl_vid);
1792c1c61f44Ssb155480 		}
1793c1c61f44Ssb155480 		D2(vswp, "\n");
1794c1c61f44Ssb155480 	}
1795c1c61f44Ssb155480 
1796c1c61f44Ssb155480 	*nvidsp = nvids;
1797c1c61f44Ssb155480 }
1798c1c61f44Ssb155480 
1799c1c61f44Ssb155480 /*
1800f0ca1d9aSsb155480  * This function reads "priority-ether-types" property from md. This property
1801f0ca1d9aSsb155480  * is used to enable support for priority frames. Applications which need
1802f0ca1d9aSsb155480  * guaranteed and timely delivery of certain high priority frames to/from
1803f0ca1d9aSsb155480  * a vnet or vsw within ldoms, should configure this property by providing
1804f0ca1d9aSsb155480  * the ether type(s) for which the priority facility is needed.
1805f0ca1d9aSsb155480  * Normal data frames are delivered over a ldc channel using the descriptor
1806f0ca1d9aSsb155480  * ring mechanism which is constrained by factors such as descriptor ring size,
1807f0ca1d9aSsb155480  * the rate at which the ring is processed at the peer ldc end point, etc.
1808f0ca1d9aSsb155480  * The priority mechanism provides an Out-Of-Band path to send/receive frames
1809f0ca1d9aSsb155480  * as raw pkt data (VIO_PKT_DATA) messages over the channel, avoiding the
1810f0ca1d9aSsb155480  * descriptor ring path and enables a more reliable and timely delivery of
1811f0ca1d9aSsb155480  * frames to the peer.
1812f0ca1d9aSsb155480  */
1813f0ca1d9aSsb155480 static void
1814f0ca1d9aSsb155480 vsw_read_pri_eth_types(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
1815f0ca1d9aSsb155480 {
1816f0ca1d9aSsb155480 	int		rv;
1817f0ca1d9aSsb155480 	uint16_t	*types;
1818f0ca1d9aSsb155480 	uint64_t	*data;
1819f0ca1d9aSsb155480 	int		size;
1820f0ca1d9aSsb155480 	int		i;
1821f0ca1d9aSsb155480 	size_t		mblk_sz;
1822f0ca1d9aSsb155480 
1823f0ca1d9aSsb155480 	rv = md_get_prop_data(mdp, node, pri_types_propname,
1824f0ca1d9aSsb155480 	    (uint8_t **)&data, &size);
1825f0ca1d9aSsb155480 	if (rv != 0) {
1826f0ca1d9aSsb155480 		/*
1827f0ca1d9aSsb155480 		 * Property may not exist if we are running pre-ldoms1.1 f/w.
1828f0ca1d9aSsb155480 		 * Check if 'vsw_pri_eth_type' has been set in that case.
1829f0ca1d9aSsb155480 		 */
1830f0ca1d9aSsb155480 		if (vsw_pri_eth_type != 0) {
1831f0ca1d9aSsb155480 			size = sizeof (vsw_pri_eth_type);
1832f0ca1d9aSsb155480 			data = &vsw_pri_eth_type;
1833f0ca1d9aSsb155480 		} else {
1834f0ca1d9aSsb155480 			D3(vswp, "%s: prop(%s) not found", __func__,
1835f0ca1d9aSsb155480 			    pri_types_propname);
1836f0ca1d9aSsb155480 			size = 0;
1837f0ca1d9aSsb155480 		}
1838f0ca1d9aSsb155480 	}
1839f0ca1d9aSsb155480 
1840f0ca1d9aSsb155480 	if (size == 0) {
1841f0ca1d9aSsb155480 		vswp->pri_num_types = 0;
1842f0ca1d9aSsb155480 		return;
1843f0ca1d9aSsb155480 	}
1844f0ca1d9aSsb155480 
1845f0ca1d9aSsb155480 	/*
1846f0ca1d9aSsb155480 	 * we have some priority-ether-types defined;
1847f0ca1d9aSsb155480 	 * allocate a table of these types and also
1848f0ca1d9aSsb155480 	 * allocate a pool of mblks to transmit these
1849f0ca1d9aSsb155480 	 * priority packets.
1850f0ca1d9aSsb155480 	 */
1851f0ca1d9aSsb155480 	size /= sizeof (uint64_t);
1852f0ca1d9aSsb155480 	vswp->pri_num_types = size;
1853f0ca1d9aSsb155480 	vswp->pri_types = kmem_zalloc(size * sizeof (uint16_t), KM_SLEEP);
1854f0ca1d9aSsb155480 	for (i = 0, types = vswp->pri_types; i < size; i++) {
1855f0ca1d9aSsb155480 		types[i] = data[i] & 0xFFFF;
1856f0ca1d9aSsb155480 	}
1857f0ca1d9aSsb155480 	mblk_sz = (VIO_PKT_DATA_HDRSIZE + ETHERMAX + 7) & ~7;
1858f0ca1d9aSsb155480 	(void) vio_create_mblks(vsw_pri_tx_nmblks, mblk_sz, &vswp->pri_tx_vmp);
1859f0ca1d9aSsb155480 }
1860f0ca1d9aSsb155480 
18617b1f684aSSriharsha Basavapatna static void
18627b1f684aSSriharsha Basavapatna vsw_mtu_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node, uint32_t *mtu)
18637b1f684aSSriharsha Basavapatna {
18647b1f684aSSriharsha Basavapatna 	int		rv;
18657b1f684aSSriharsha Basavapatna 	int		inst;
18667b1f684aSSriharsha Basavapatna 	uint64_t	val;
18677b1f684aSSriharsha Basavapatna 	char		*mtu_propname;
18687b1f684aSSriharsha Basavapatna 
18697b1f684aSSriharsha Basavapatna 	mtu_propname = vsw_mtu_propname;
18707b1f684aSSriharsha Basavapatna 	inst = vswp->instance;
18717b1f684aSSriharsha Basavapatna 
18727b1f684aSSriharsha Basavapatna 	rv = md_get_prop_val(mdp, node, mtu_propname, &val);
18737b1f684aSSriharsha Basavapatna 	if (rv != 0) {
18747b1f684aSSriharsha Basavapatna 		D3(vswp, "%s: prop(%s) not found", __func__, mtu_propname);
18757b1f684aSSriharsha Basavapatna 		*mtu = vsw_ethermtu;
18767b1f684aSSriharsha Basavapatna 	} else {
18777b1f684aSSriharsha Basavapatna 
18787b1f684aSSriharsha Basavapatna 		*mtu = val & 0xFFFF;
18797b1f684aSSriharsha Basavapatna 		D2(vswp, "%s: %s(%d): (%d)\n", __func__,
18807b1f684aSSriharsha Basavapatna 		    mtu_propname, inst, *mtu);
18817b1f684aSSriharsha Basavapatna 	}
18827b1f684aSSriharsha Basavapatna }
18837b1f684aSSriharsha Basavapatna 
18847b1f684aSSriharsha Basavapatna /*
18857b1f684aSSriharsha Basavapatna  * Update the mtu of the vsw device. We first check if the device has been
18867b1f684aSSriharsha Basavapatna  * plumbed and if so fail the mtu update. Otherwise, we continue to update the
18877b1f684aSSriharsha Basavapatna  * new mtu and reset all ports to initiate handshake re-negotiation with peers
18887b1f684aSSriharsha Basavapatna  * using the new mtu.
18897b1f684aSSriharsha Basavapatna  */
18907b1f684aSSriharsha Basavapatna static int
18917b1f684aSSriharsha Basavapatna vsw_mtu_update(vsw_t *vswp, uint32_t mtu)
18927b1f684aSSriharsha Basavapatna {
18937b1f684aSSriharsha Basavapatna 	int	rv;
18947b1f684aSSriharsha Basavapatna 
18957b1f684aSSriharsha Basavapatna 	WRITE_ENTER(&vswp->if_lockrw);
18967b1f684aSSriharsha Basavapatna 
18977b1f684aSSriharsha Basavapatna 	if (vswp->if_state & VSW_IF_UP) {
18987b1f684aSSriharsha Basavapatna 
18997b1f684aSSriharsha Basavapatna 		RW_EXIT(&vswp->if_lockrw);
19007b1f684aSSriharsha Basavapatna 
19017b1f684aSSriharsha Basavapatna 		cmn_err(CE_NOTE, "!vsw%d: Unable to process mtu update"
19027b1f684aSSriharsha Basavapatna 		    " as the device is plumbed\n", vswp->instance);
19037b1f684aSSriharsha Basavapatna 		return (EBUSY);
19047b1f684aSSriharsha Basavapatna 
19057b1f684aSSriharsha Basavapatna 	} else {
19067b1f684aSSriharsha Basavapatna 
19077b1f684aSSriharsha Basavapatna 		D2(vswp, "%s: curr_mtu(%d) new_mtu(%d)\n",
19087b1f684aSSriharsha Basavapatna 		    __func__, vswp->mtu, mtu);
19097b1f684aSSriharsha Basavapatna 
19107b1f684aSSriharsha Basavapatna 		vswp->mtu = mtu;
19117b1f684aSSriharsha Basavapatna 		vswp->max_frame_size = vswp->mtu +
19127b1f684aSSriharsha Basavapatna 		    sizeof (struct ether_header) + VLAN_TAGSZ;
19137b1f684aSSriharsha Basavapatna 
19147b1f684aSSriharsha Basavapatna 		rv = mac_maxsdu_update(vswp->if_mh, mtu);
19157b1f684aSSriharsha Basavapatna 		if (rv != 0) {
19167b1f684aSSriharsha Basavapatna 			cmn_err(CE_NOTE,
19177b1f684aSSriharsha Basavapatna 			    "!vsw%d: Unable to update mtu with mac"
19187b1f684aSSriharsha Basavapatna 			    " layer\n", vswp->instance);
19197b1f684aSSriharsha Basavapatna 		}
19207b1f684aSSriharsha Basavapatna 
19217b1f684aSSriharsha Basavapatna 		RW_EXIT(&vswp->if_lockrw);
19227b1f684aSSriharsha Basavapatna 
19237b1f684aSSriharsha Basavapatna 		/* Reset ports to renegotiate with the new mtu */
19247b1f684aSSriharsha Basavapatna 		vsw_reset_ports(vswp);
19257b1f684aSSriharsha Basavapatna 
19267b1f684aSSriharsha Basavapatna 	}
19277b1f684aSSriharsha Basavapatna 
19287b1f684aSSriharsha Basavapatna 	return (0);
19297b1f684aSSriharsha Basavapatna }
19307b1f684aSSriharsha Basavapatna 
1931f0ca1d9aSsb155480 /*
193234683adeSsg70180  * Check to see if the relevant properties in the specified node have
193334683adeSsg70180  * changed, and if so take the appropriate action.
193434683adeSsg70180  *
193534683adeSsg70180  * If any of the properties are missing or invalid we don't take
193634683adeSsg70180  * any action, as this function should only be invoked when modifications
193734683adeSsg70180  * have been made to what we assume is a working configuration, which
193834683adeSsg70180  * we leave active.
193934683adeSsg70180  *
194034683adeSsg70180  * Note it is legal for this routine to be invoked even if none of the
194134683adeSsg70180  * properties in the port node within the MD have actually changed.
194234683adeSsg70180  */
194334683adeSsg70180 static void
194434683adeSsg70180 vsw_update_md_prop(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
194534683adeSsg70180 {
194634683adeSsg70180 	char		physname[LIFNAMSIZ];
194734683adeSsg70180 	char		drv[LIFNAMSIZ];
194834683adeSsg70180 	uint_t		ddi_instance;
1949da14cebeSEric Cheng 	uint8_t		new_smode;
1950da14cebeSEric Cheng 	int		i;
195134683adeSsg70180 	uint64_t 	macaddr = 0;
195234683adeSsg70180 	enum		{MD_init = 0x1,
195334683adeSsg70180 				MD_physname = 0x2,
195434683adeSsg70180 				MD_macaddr = 0x4,
1955c1c61f44Ssb155480 				MD_smode = 0x8,
19567b1f684aSSriharsha Basavapatna 				MD_vlans = 0x10,
19577b1f684aSSriharsha Basavapatna 				MD_mtu = 0x20} updated;
195819b65a69Ssb155480 	int		rv;
1959c1c61f44Ssb155480 	uint16_t	pvid;
1960da14cebeSEric Cheng 	vsw_vlanid_t	*vids;
1961c1c61f44Ssb155480 	uint16_t	nvids;
19627b1f684aSSriharsha Basavapatna 	uint32_t	mtu;
196334683adeSsg70180 
196434683adeSsg70180 	updated = MD_init;
196534683adeSsg70180 
196634683adeSsg70180 	D1(vswp, "%s: enter", __func__);
196734683adeSsg70180 
196834683adeSsg70180 	/*
196934683adeSsg70180 	 * Check if name of physical device in MD has changed.
197034683adeSsg70180 	 */
197134683adeSsg70180 	if (vsw_get_md_physname(vswp, mdp, node, (char *)&physname) == 0) {
197234683adeSsg70180 		/*
197334683adeSsg70180 		 * Do basic sanity check on new device name/instance,
197434683adeSsg70180 		 * if its non NULL. It is valid for the device name to
197534683adeSsg70180 		 * have changed from a non NULL to a NULL value, i.e.
197634683adeSsg70180 		 * the vsw is being changed to 'routed' mode.
197734683adeSsg70180 		 */
197834683adeSsg70180 		if ((strlen(physname) != 0) &&
197919b65a69Ssb155480 		    (ddi_parse(physname, drv,
198019b65a69Ssb155480 		    &ddi_instance) != DDI_SUCCESS)) {
19811ef0bbb5Snarayan 			cmn_err(CE_WARN, "!vsw%d: physical device %s is not"
198234683adeSsg70180 			    " a valid device name/instance",
198334683adeSsg70180 			    vswp->instance, physname);
198434683adeSsg70180 			goto fail_reconf;
198534683adeSsg70180 		}
198634683adeSsg70180 
198734683adeSsg70180 		if (strcmp(physname, vswp->physname)) {
198834683adeSsg70180 			D2(vswp, "%s: device name changed from %s to %s",
198934683adeSsg70180 			    __func__, vswp->physname, physname);
199034683adeSsg70180 
199134683adeSsg70180 			updated |= MD_physname;
199234683adeSsg70180 		} else {
199334683adeSsg70180 			D2(vswp, "%s: device name unchanged at %s",
199434683adeSsg70180 			    __func__, vswp->physname);
199534683adeSsg70180 		}
199634683adeSsg70180 	} else {
199734683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read name of physical "
199834683adeSsg70180 		    "device from updated MD.", vswp->instance);
199934683adeSsg70180 		goto fail_reconf;
200034683adeSsg70180 	}
200134683adeSsg70180 
200234683adeSsg70180 	/*
200334683adeSsg70180 	 * Check if MAC address has changed.
200434683adeSsg70180 	 */
200534683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
200634683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
200734683adeSsg70180 		    vswp->instance);
200834683adeSsg70180 		goto fail_reconf;
200934683adeSsg70180 	} else {
201019b65a69Ssb155480 		uint64_t maddr = macaddr;
201134683adeSsg70180 		READ_ENTER(&vswp->if_lockrw);
201234683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
201319b65a69Ssb155480 			if (vswp->if_addr.ether_addr_octet[i]
201419b65a69Ssb155480 			    != (macaddr & 0xFF)) {
201534683adeSsg70180 				D2(vswp, "%s: octet[%d] 0x%x != 0x%x",
201634683adeSsg70180 				    __func__, i,
201734683adeSsg70180 				    vswp->if_addr.ether_addr_octet[i],
201834683adeSsg70180 				    (macaddr & 0xFF));
201934683adeSsg70180 				updated |= MD_macaddr;
202019b65a69Ssb155480 				macaddr = maddr;
202134683adeSsg70180 				break;
202234683adeSsg70180 			}
202334683adeSsg70180 			macaddr >>= 8;
202434683adeSsg70180 		}
202534683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
202619b65a69Ssb155480 		if (updated & MD_macaddr) {
202719b65a69Ssb155480 			vsw_save_lmacaddr(vswp, macaddr);
202819b65a69Ssb155480 		}
202934683adeSsg70180 	}
203034683adeSsg70180 
203134683adeSsg70180 	/*
203234683adeSsg70180 	 * Check if switching modes have changed.
203334683adeSsg70180 	 */
2034da14cebeSEric Cheng 	if (vsw_get_md_smodes(vswp, mdp, node, &new_smode)) {
203534683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read %s property from MD",
203634683adeSsg70180 		    vswp->instance, smode_propname);
203734683adeSsg70180 		goto fail_reconf;
203834683adeSsg70180 	} else {
2039da14cebeSEric Cheng 		if (new_smode != vswp->smode) {
2040da14cebeSEric Cheng 			D2(vswp, "%s: switching mode changed from %d to %d",
2041da14cebeSEric Cheng 			    __func__, vswp->smode, new_smode);
204234683adeSsg70180 
204334683adeSsg70180 			updated |= MD_smode;
204434683adeSsg70180 		}
204534683adeSsg70180 	}
204634683adeSsg70180 
2047c1c61f44Ssb155480 	/* Read the vlan ids */
2048c1c61f44Ssb155480 	vsw_vlan_read_ids(vswp, VSW_LOCALDEV, mdp, node, &pvid, &vids,
2049c1c61f44Ssb155480 	    &nvids, NULL);
2050c1c61f44Ssb155480 
2051c1c61f44Ssb155480 	/* Determine if there are any vlan id updates */
2052c1c61f44Ssb155480 	if ((pvid != vswp->pvid) ||		/* pvid changed? */
2053c1c61f44Ssb155480 	    (nvids != vswp->nvids) ||		/* # of vids changed? */
2054c1c61f44Ssb155480 	    ((nvids != 0) && (vswp->nvids != 0) &&	/* vids changed? */
2055da14cebeSEric Cheng 	    !vsw_cmp_vids(vids, vswp->vids, nvids))) {
2056c1c61f44Ssb155480 		updated |= MD_vlans;
2057c1c61f44Ssb155480 	}
2058c1c61f44Ssb155480 
20597b1f684aSSriharsha Basavapatna 	/* Read mtu */
20607b1f684aSSriharsha Basavapatna 	vsw_mtu_read(vswp, mdp, node, &mtu);
20617b1f684aSSriharsha Basavapatna 	if (mtu != vswp->mtu) {
20627b1f684aSSriharsha Basavapatna 		if (mtu >= ETHERMTU && mtu <= VNET_MAX_MTU) {
20637b1f684aSSriharsha Basavapatna 			updated |= MD_mtu;
20647b1f684aSSriharsha Basavapatna 		} else {
20657b1f684aSSriharsha Basavapatna 			cmn_err(CE_NOTE, "!vsw%d: Unable to process mtu update"
20667b1f684aSSriharsha Basavapatna 			    " as the specified value:%d is invalid\n",
20677b1f684aSSriharsha Basavapatna 			    vswp->instance, mtu);
20687b1f684aSSriharsha Basavapatna 		}
20697b1f684aSSriharsha Basavapatna 	}
20707b1f684aSSriharsha Basavapatna 
207134683adeSsg70180 	/*
207234683adeSsg70180 	 * Now make any changes which are needed...
207334683adeSsg70180 	 */
207434683adeSsg70180 
2075da14cebeSEric Cheng 	if (updated & (MD_physname | MD_smode | MD_mtu)) {
207634683adeSsg70180 
207734683adeSsg70180 		/*
2078*808f26a8SSriharsha Basavapatna 		 * Stop any pending thread to setup switching mode.
207934683adeSsg70180 		 */
2080*808f26a8SSriharsha Basavapatna 		vsw_setup_switching_stop(vswp);
208119b65a69Ssb155480 
2082678453a8Sspeer 		/* Cleanup HybridIO */
2083678453a8Sspeer 		vsw_hio_cleanup(vswp);
2084678453a8Sspeer 
208519b65a69Ssb155480 		/*
208619b65a69Ssb155480 		 * Remove unicst, mcst addrs of vsw interface
2087da14cebeSEric Cheng 		 * and ports from the physdev. This also closes
2088da14cebeSEric Cheng 		 * the corresponding mac clients.
208919b65a69Ssb155480 		 */
209019b65a69Ssb155480 		vsw_unset_addrs(vswp);
209119b65a69Ssb155480 
209219b65a69Ssb155480 		/*
209319b65a69Ssb155480 		 * Stop, detach and close the old device..
209419b65a69Ssb155480 		 */
2095da14cebeSEric Cheng 		mutex_enter(&vswp->mac_lock);
209619b65a69Ssb155480 		vsw_mac_close(vswp);
2097da14cebeSEric Cheng 		mutex_exit(&vswp->mac_lock);
209834683adeSsg70180 
209934683adeSsg70180 		/*
210034683adeSsg70180 		 * Update phys name.
210134683adeSsg70180 		 */
210234683adeSsg70180 		if (updated & MD_physname) {
210334683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: changing from %s to %s",
210434683adeSsg70180 			    vswp->instance, vswp->physname, physname);
210534683adeSsg70180 			(void) strncpy(vswp->physname,
210634683adeSsg70180 			    physname, strlen(physname) + 1);
210734683adeSsg70180 		}
210834683adeSsg70180 
210934683adeSsg70180 		/*
211034683adeSsg70180 		 * Update array with the new switch mode values.
211134683adeSsg70180 		 */
211234683adeSsg70180 		if (updated & MD_smode) {
2113da14cebeSEric Cheng 			vswp->smode = new_smode;
2114da14cebeSEric Cheng 		}
211534683adeSsg70180 
2116da14cebeSEric Cheng 		/* Update mtu */
2117da14cebeSEric Cheng 		if (updated & MD_mtu) {
2118da14cebeSEric Cheng 			rv = vsw_mtu_update(vswp, mtu);
2119da14cebeSEric Cheng 			if (rv != 0) {
2120da14cebeSEric Cheng 				goto fail_update;
2121da14cebeSEric Cheng 			}
212234683adeSsg70180 		}
212334683adeSsg70180 
212434683adeSsg70180 		/*
212534683adeSsg70180 		 * ..and attach, start the new device.
212634683adeSsg70180 		 */
212719b65a69Ssb155480 		rv = vsw_setup_switching(vswp);
212819b65a69Ssb155480 		if (rv == EAGAIN) {
212919b65a69Ssb155480 			/*
213019b65a69Ssb155480 			 * Unable to setup switching mode.
2131*808f26a8SSriharsha Basavapatna 			 * As the error is EAGAIN, schedule a thread to retry
213219b65a69Ssb155480 			 * and return. Programming addresses of ports and
2133*808f26a8SSriharsha Basavapatna 			 * vsw interface will be done by the thread when the
2134*808f26a8SSriharsha Basavapatna 			 * switching setup completes successfully.
213519b65a69Ssb155480 			 */
2136*808f26a8SSriharsha Basavapatna 			if (vsw_setup_switching_start(vswp) != 0) {
2137*808f26a8SSriharsha Basavapatna 				goto fail_update;
2138*808f26a8SSriharsha Basavapatna 			}
213919b65a69Ssb155480 			return;
214019b65a69Ssb155480 
214119b65a69Ssb155480 		} else if (rv) {
214234683adeSsg70180 			goto fail_update;
214319b65a69Ssb155480 		}
214434683adeSsg70180 
214571bdf936SWENTAO YANG 		vsw_setup_layer2_post_process(vswp);
214619b65a69Ssb155480 	} else if (updated & MD_macaddr) {
214719b65a69Ssb155480 		/*
214819b65a69Ssb155480 		 * We enter here if only MD_macaddr is exclusively updated.
214919b65a69Ssb155480 		 * If MD_physname and/or MD_smode are also updated, then
215019b65a69Ssb155480 		 * as part of that, we would have implicitly processed
215119b65a69Ssb155480 		 * MD_macaddr update (above).
215219b65a69Ssb155480 		 */
215334683adeSsg70180 		cmn_err(CE_NOTE, "!vsw%d: changing mac address to 0x%lx",
215434683adeSsg70180 		    vswp->instance, macaddr);
215534683adeSsg70180 
215619b65a69Ssb155480 		READ_ENTER(&vswp->if_lockrw);
215719b65a69Ssb155480 		if (vswp->if_state & VSW_IF_UP) {
2158da14cebeSEric Cheng 			/* reconfigure with new address */
2159da14cebeSEric Cheng 			vsw_if_mac_reconfig(vswp, B_FALSE, 0, NULL, 0);
216034683adeSsg70180 
21615f94e909Ssg70180 			/*
216234683adeSsg70180 			 * Notify the MAC layer of the changed address.
216334683adeSsg70180 			 */
216419b65a69Ssb155480 			mac_unicst_update(vswp->if_mh,
216519b65a69Ssb155480 			    (uint8_t *)&vswp->if_addr);
216619b65a69Ssb155480 
216719b65a69Ssb155480 		}
216819b65a69Ssb155480 		RW_EXIT(&vswp->if_lockrw);
216919b65a69Ssb155480 
217034683adeSsg70180 	}
217134683adeSsg70180 
2172c1c61f44Ssb155480 	if (updated & MD_vlans) {
2173c1c61f44Ssb155480 		/* Remove existing vlan ids from the hash table. */
2174c1c61f44Ssb155480 		vsw_vlan_remove_ids(vswp, VSW_LOCALDEV);
2175c1c61f44Ssb155480 
2176da14cebeSEric Cheng 		if (vswp->if_state & VSW_IF_UP) {
2177da14cebeSEric Cheng 			vsw_if_mac_reconfig(vswp, B_TRUE, pvid, vids, nvids);
2178da14cebeSEric Cheng 		} else {
2179c1c61f44Ssb155480 			if (vswp->nvids != 0) {
2180da14cebeSEric Cheng 				kmem_free(vswp->vids,
2181da14cebeSEric Cheng 				    sizeof (vsw_vlanid_t) * vswp->nvids);
2182c1c61f44Ssb155480 			}
2183c1c61f44Ssb155480 			vswp->vids = vids;
2184da14cebeSEric Cheng 			vswp->nvids = nvids;
2185da14cebeSEric Cheng 			vswp->pvid = pvid;
2186c1c61f44Ssb155480 		}
2187c1c61f44Ssb155480 
2188c1c61f44Ssb155480 		/* add these new vlan ids into hash table */
2189c1c61f44Ssb155480 		vsw_vlan_add_ids(vswp, VSW_LOCALDEV);
2190c1c61f44Ssb155480 	} else {
2191c1c61f44Ssb155480 		if (nvids != 0) {
2192da14cebeSEric Cheng 			kmem_free(vids, sizeof (vsw_vlanid_t) * nvids);
2193c1c61f44Ssb155480 		}
2194c1c61f44Ssb155480 	}
2195c1c61f44Ssb155480 
219634683adeSsg70180 	return;
219734683adeSsg70180 
219834683adeSsg70180 fail_reconf:
219934683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: configuration unchanged", vswp->instance);
220034683adeSsg70180 	return;
220134683adeSsg70180 
220234683adeSsg70180 fail_update:
22031ef0bbb5Snarayan 	cmn_err(CE_WARN, "!vsw%d: re-configuration failed",
220434683adeSsg70180 	    vswp->instance);
220534683adeSsg70180 }
220634683adeSsg70180 
220734683adeSsg70180 /*
2208c1c61f44Ssb155480  * Read the port's md properties.
22091ae08745Sheppo  */
2210c1c61f44Ssb155480 static int
2211c1c61f44Ssb155480 vsw_port_read_props(vsw_port_t *portp, vsw_t *vswp,
2212c1c61f44Ssb155480 	md_t *mdp, mde_cookie_t *node)
22131ae08745Sheppo {
22141ae08745Sheppo 	uint64_t		ldc_id;
22151ae08745Sheppo 	uint8_t			*addrp;
22161ae08745Sheppo 	int			i, addrsz;
22171ae08745Sheppo 	int			num_nodes = 0, nchan = 0;
22181ae08745Sheppo 	int			listsz = 0;
22191ae08745Sheppo 	mde_cookie_t		*listp = NULL;
22201ae08745Sheppo 	struct ether_addr	ea;
22211ae08745Sheppo 	uint64_t		macaddr;
22221ae08745Sheppo 	uint64_t		inst = 0;
2223678453a8Sspeer 	uint64_t		val;
22241ae08745Sheppo 
22251ae08745Sheppo 	if (md_get_prop_val(mdp, *node, id_propname, &inst)) {
22261ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found", __func__,
22271ae08745Sheppo 		    id_propname);
22281ae08745Sheppo 		return (1);
22291ae08745Sheppo 	}
22301ae08745Sheppo 
22311ae08745Sheppo 	/*
22321ae08745Sheppo 	 * Find the channel endpoint node(s) (which should be under this
22331ae08745Sheppo 	 * port node) which contain the channel id(s).
22341ae08745Sheppo 	 */
22351ae08745Sheppo 	if ((num_nodes = md_node_count(mdp)) <= 0) {
22361ae08745Sheppo 		DERR(vswp, "%s: invalid number of nodes found (%d)",
22371ae08745Sheppo 		    __func__, num_nodes);
22381ae08745Sheppo 		return (1);
22391ae08745Sheppo 	}
22401ae08745Sheppo 
224134683adeSsg70180 	D2(vswp, "%s: %d nodes found", __func__, num_nodes);
224234683adeSsg70180 
22431ae08745Sheppo 	/* allocate enough space for node list */
22441ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
22451ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
22461ae08745Sheppo 
2247205eeb1aSlm66018 	nchan = md_scan_dag(mdp, *node, md_find_name(mdp, chan_propname),
22481ae08745Sheppo 	    md_find_name(mdp, "fwd"), listp);
22491ae08745Sheppo 
22501ae08745Sheppo 	if (nchan <= 0) {
22511ae08745Sheppo 		DWARN(vswp, "%s: no %s nodes found", __func__, chan_propname);
22521ae08745Sheppo 		kmem_free(listp, listsz);
22531ae08745Sheppo 		return (1);
22541ae08745Sheppo 	}
22551ae08745Sheppo 
22561ae08745Sheppo 	D2(vswp, "%s: %d %s nodes found", __func__, nchan, chan_propname);
22571ae08745Sheppo 
22581ae08745Sheppo 	/* use property from first node found */
22591ae08745Sheppo 	if (md_get_prop_val(mdp, listp[0], id_propname, &ldc_id)) {
22601ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found\n", __func__,
22611ae08745Sheppo 		    id_propname);
22621ae08745Sheppo 		kmem_free(listp, listsz);
22631ae08745Sheppo 		return (1);
22641ae08745Sheppo 	}
22651ae08745Sheppo 
22661ae08745Sheppo 	/* don't need list any more */
22671ae08745Sheppo 	kmem_free(listp, listsz);
22681ae08745Sheppo 
22691ae08745Sheppo 	D2(vswp, "%s: ldc_id 0x%llx", __func__, ldc_id);
22701ae08745Sheppo 
22711ae08745Sheppo 	/* read mac-address property */
22721ae08745Sheppo 	if (md_get_prop_data(mdp, *node, remaddr_propname,
22731ae08745Sheppo 	    &addrp, &addrsz)) {
22741ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found",
22751ae08745Sheppo 		    __func__, remaddr_propname);
22761ae08745Sheppo 		return (1);
22771ae08745Sheppo 	}
22781ae08745Sheppo 
22791ae08745Sheppo 	if (addrsz < ETHERADDRL) {
22801ae08745Sheppo 		DWARN(vswp, "%s: invalid address size", __func__);
22811ae08745Sheppo 		return (1);
22821ae08745Sheppo 	}
22831ae08745Sheppo 
22841ae08745Sheppo 	macaddr = *((uint64_t *)addrp);
22851ae08745Sheppo 	D2(vswp, "%s: remote mac address 0x%llx", __func__, macaddr);
22861ae08745Sheppo 
22871ae08745Sheppo 	for (i = ETHERADDRL - 1; i >= 0; i--) {
22881ae08745Sheppo 		ea.ether_addr_octet[i] = macaddr & 0xFF;
22891ae08745Sheppo 		macaddr >>= 8;
22901ae08745Sheppo 	}
22911ae08745Sheppo 
2292c1c61f44Ssb155480 	/* now update all properties into the port */
2293c1c61f44Ssb155480 	portp->p_vswp = vswp;
2294c1c61f44Ssb155480 	portp->p_instance = inst;
2295da14cebeSEric Cheng 	portp->addr_set = B_FALSE;
2296c1c61f44Ssb155480 	ether_copy(&ea, &portp->p_macaddr);
2297c1c61f44Ssb155480 	if (nchan > VSW_PORT_MAX_LDCS) {
2298c1c61f44Ssb155480 		D2(vswp, "%s: using first of %d ldc ids",
2299c1c61f44Ssb155480 		    __func__, nchan);
2300c1c61f44Ssb155480 		nchan = VSW_PORT_MAX_LDCS;
2301c1c61f44Ssb155480 	}
2302c1c61f44Ssb155480 	portp->num_ldcs = nchan;
2303c1c61f44Ssb155480 	portp->ldc_ids =
2304c1c61f44Ssb155480 	    kmem_zalloc(sizeof (uint64_t) * nchan, KM_SLEEP);
2305c1c61f44Ssb155480 	bcopy(&ldc_id, (portp->ldc_ids), sizeof (uint64_t) * nchan);
2306c1c61f44Ssb155480 
2307c1c61f44Ssb155480 	/* read vlan id properties of this port node */
2308c1c61f44Ssb155480 	vsw_vlan_read_ids(portp, VSW_VNETPORT, mdp, *node, &portp->pvid,
2309c1c61f44Ssb155480 	    &portp->vids, &portp->nvids, NULL);
2310c1c61f44Ssb155480 
2311678453a8Sspeer 	/* Check if hybrid property is present */
2312678453a8Sspeer 	if (md_get_prop_val(mdp, *node, hybrid_propname, &val) == 0) {
2313678453a8Sspeer 		D1(vswp, "%s: prop(%s) found\n", __func__, hybrid_propname);
2314678453a8Sspeer 		portp->p_hio_enabled = B_TRUE;
2315678453a8Sspeer 	} else {
2316678453a8Sspeer 		portp->p_hio_enabled = B_FALSE;
2317678453a8Sspeer 	}
2318678453a8Sspeer 	/*
2319678453a8Sspeer 	 * Port hio capability determined after version
2320678453a8Sspeer 	 * negotiation, i.e., when we know the peer is HybridIO capable.
2321678453a8Sspeer 	 */
2322678453a8Sspeer 	portp->p_hio_capable = B_FALSE;
2323c1c61f44Ssb155480 	return (0);
2324c1c61f44Ssb155480 }
2325c1c61f44Ssb155480 
2326c1c61f44Ssb155480 /*
2327c1c61f44Ssb155480  * Add a new port to the system.
2328c1c61f44Ssb155480  *
2329c1c61f44Ssb155480  * Returns 0 on success, 1 on failure.
2330c1c61f44Ssb155480  */
2331c1c61f44Ssb155480 int
2332c1c61f44Ssb155480 vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node)
2333c1c61f44Ssb155480 {
2334c1c61f44Ssb155480 	vsw_port_t	*portp;
2335c1c61f44Ssb155480 	int		rv;
2336c1c61f44Ssb155480 
2337c1c61f44Ssb155480 	portp = kmem_zalloc(sizeof (vsw_port_t), KM_SLEEP);
2338c1c61f44Ssb155480 
2339c1c61f44Ssb155480 	rv = vsw_port_read_props(portp, vswp, mdp, node);
2340c1c61f44Ssb155480 	if (rv != 0) {
2341c1c61f44Ssb155480 		kmem_free(portp, sizeof (*portp));
2342c1c61f44Ssb155480 		return (1);
2343c1c61f44Ssb155480 	}
2344c1c61f44Ssb155480 
2345c1c61f44Ssb155480 	rv = vsw_port_attach(portp);
2346c1c61f44Ssb155480 	if (rv != 0) {
23471ae08745Sheppo 		DERR(vswp, "%s: failed to attach port", __func__);
23481ae08745Sheppo 		return (1);
23491ae08745Sheppo 	}
23501ae08745Sheppo 
2351c1c61f44Ssb155480 	return (0);
2352c1c61f44Ssb155480 }
23531ae08745Sheppo 
2354c1c61f44Ssb155480 static int
2355c1c61f44Ssb155480 vsw_port_update(vsw_t *vswp, md_t *curr_mdp, mde_cookie_t curr_mdex,
2356c1c61f44Ssb155480 	md_t *prev_mdp, mde_cookie_t prev_mdex)
2357c1c61f44Ssb155480 {
2358c1c61f44Ssb155480 	uint64_t	cport_num;
2359c1c61f44Ssb155480 	uint64_t	pport_num;
2360c1c61f44Ssb155480 	vsw_port_list_t	*plistp;
2361c1c61f44Ssb155480 	vsw_port_t	*portp;
2362c1c61f44Ssb155480 	boolean_t	updated_vlans = B_FALSE;
2363c1c61f44Ssb155480 	uint16_t	pvid;
2364da14cebeSEric Cheng 	vsw_vlanid_t	*vids;
2365c1c61f44Ssb155480 	uint16_t	nvids;
2366678453a8Sspeer 	uint64_t	val;
2367678453a8Sspeer 	boolean_t	hio_enabled = B_FALSE;
2368c1c61f44Ssb155480 
2369c1c61f44Ssb155480 	/*
2370c1c61f44Ssb155480 	 * For now, we get port updates only if vlan ids changed.
2371c1c61f44Ssb155480 	 * We read the port num and do some sanity check.
2372c1c61f44Ssb155480 	 */
2373c1c61f44Ssb155480 	if (md_get_prop_val(curr_mdp, curr_mdex, id_propname, &cport_num)) {
2374c1c61f44Ssb155480 		return (1);
2375c1c61f44Ssb155480 	}
2376c1c61f44Ssb155480 
2377c1c61f44Ssb155480 	if (md_get_prop_val(prev_mdp, prev_mdex, id_propname, &pport_num)) {
2378c1c61f44Ssb155480 		return (1);
2379c1c61f44Ssb155480 	}
2380c1c61f44Ssb155480 	if (cport_num != pport_num)
2381c1c61f44Ssb155480 		return (1);
2382c1c61f44Ssb155480 
2383c1c61f44Ssb155480 	plistp = &(vswp->plist);
2384c1c61f44Ssb155480 
2385c1c61f44Ssb155480 	READ_ENTER(&plistp->lockrw);
2386c1c61f44Ssb155480 
2387c1c61f44Ssb155480 	portp = vsw_lookup_port(vswp, cport_num);
2388c1c61f44Ssb155480 	if (portp == NULL) {
2389c1c61f44Ssb155480 		RW_EXIT(&plistp->lockrw);
2390c1c61f44Ssb155480 		return (1);
2391c1c61f44Ssb155480 	}
2392c1c61f44Ssb155480 
2393c1c61f44Ssb155480 	/* Read the vlan ids */
2394c1c61f44Ssb155480 	vsw_vlan_read_ids(portp, VSW_VNETPORT, curr_mdp, curr_mdex, &pvid,
2395c1c61f44Ssb155480 	    &vids, &nvids, NULL);
2396c1c61f44Ssb155480 
2397c1c61f44Ssb155480 	/* Determine if there are any vlan id updates */
2398c1c61f44Ssb155480 	if ((pvid != portp->pvid) ||		/* pvid changed? */
2399c1c61f44Ssb155480 	    (nvids != portp->nvids) ||		/* # of vids changed? */
2400c1c61f44Ssb155480 	    ((nvids != 0) && (portp->nvids != 0) &&	/* vids changed? */
2401da14cebeSEric Cheng 	    !vsw_cmp_vids(vids, portp->vids, nvids))) {
2402c1c61f44Ssb155480 		updated_vlans = B_TRUE;
2403c1c61f44Ssb155480 	}
2404c1c61f44Ssb155480 
2405678453a8Sspeer 	if (updated_vlans == B_TRUE) {
2406c1c61f44Ssb155480 
2407c1c61f44Ssb155480 		/* Remove existing vlan ids from the hash table. */
2408c1c61f44Ssb155480 		vsw_vlan_remove_ids(portp, VSW_VNETPORT);
2409c1c61f44Ssb155480 
2410da14cebeSEric Cheng 		/* Reconfigure vlans with network device */
2411da14cebeSEric Cheng 		vsw_mac_port_reconfig_vlans(portp, pvid, vids, nvids);
2412c1c61f44Ssb155480 
2413c1c61f44Ssb155480 		/* add these new vlan ids into hash table */
2414c1c61f44Ssb155480 		vsw_vlan_add_ids(portp, VSW_VNETPORT);
2415c1c61f44Ssb155480 
2416c1c61f44Ssb155480 		/* reset the port if it is vlan unaware (ver < 1.3) */
2417c1c61f44Ssb155480 		vsw_vlan_unaware_port_reset(portp);
2418678453a8Sspeer 	}
2419678453a8Sspeer 
2420678453a8Sspeer 	/* Check if hybrid property is present */
2421678453a8Sspeer 	if (md_get_prop_val(curr_mdp, curr_mdex, hybrid_propname, &val) == 0) {
2422678453a8Sspeer 		D1(vswp, "%s: prop(%s) found\n", __func__, hybrid_propname);
2423678453a8Sspeer 		hio_enabled = B_TRUE;
2424678453a8Sspeer 	}
2425678453a8Sspeer 
2426678453a8Sspeer 	if (portp->p_hio_enabled != hio_enabled) {
2427678453a8Sspeer 		vsw_hio_port_update(portp, hio_enabled);
2428678453a8Sspeer 	}
2429c1c61f44Ssb155480 
2430c1c61f44Ssb155480 	RW_EXIT(&plistp->lockrw);
24311ae08745Sheppo 
24321ae08745Sheppo 	return (0);
24331ae08745Sheppo }
24341ae08745Sheppo 
24351ae08745Sheppo /*
243606db247cSraghuram  * vsw_mac_rx -- A common function to send packets to the interface.
243706db247cSraghuram  * By default this function check if the interface is UP or not, the
243806db247cSraghuram  * rest of the behaviour depends on the flags as below:
24391ae08745Sheppo  *
244006db247cSraghuram  *	VSW_MACRX_PROMISC -- Check if the promisc mode set or not.
244106db247cSraghuram  *	VSW_MACRX_COPYMSG -- Make a copy of the message(s).
244206db247cSraghuram  *	VSW_MACRX_FREEMSG -- Free if the messages cannot be sent up the stack.
24431ae08745Sheppo  */
24441ae08745Sheppo void
2445f0ca1d9aSsb155480 vsw_mac_rx(vsw_t *vswp, mac_resource_handle_t mrh,
2446f0ca1d9aSsb155480     mblk_t *mp, vsw_macrx_flags_t flags)
24471ae08745Sheppo {
2448c1c61f44Ssb155480 	mblk_t		*mpt;
2449c1c61f44Ssb155480 
245006db247cSraghuram 	D1(vswp, "%s:enter\n", __func__);
24511ae08745Sheppo 	READ_ENTER(&vswp->if_lockrw);
245206db247cSraghuram 	/* Check if the interface is up */
245306db247cSraghuram 	if (!(vswp->if_state & VSW_IF_UP)) {
24541ae08745Sheppo 		RW_EXIT(&vswp->if_lockrw);
245506db247cSraghuram 		/* Free messages only if FREEMSG flag specified */
245606db247cSraghuram 		if (flags & VSW_MACRX_FREEMSG) {
245706db247cSraghuram 			freemsgchain(mp);
245806db247cSraghuram 		}
245906db247cSraghuram 		D1(vswp, "%s:exit\n", __func__);
246006db247cSraghuram 		return;
246106db247cSraghuram 	}
246206db247cSraghuram 	/*
246306db247cSraghuram 	 * If PROMISC flag is passed, then check if
246406db247cSraghuram 	 * the interface is in the PROMISC mode.
246506db247cSraghuram 	 * If not, drop the messages.
246606db247cSraghuram 	 */
246706db247cSraghuram 	if (flags & VSW_MACRX_PROMISC) {
246806db247cSraghuram 		if (!(vswp->if_state & VSW_IF_PROMISC)) {
246906db247cSraghuram 			RW_EXIT(&vswp->if_lockrw);
247006db247cSraghuram 			/* Free messages only if FREEMSG flag specified */
247106db247cSraghuram 			if (flags & VSW_MACRX_FREEMSG) {
247206db247cSraghuram 				freemsgchain(mp);
247306db247cSraghuram 			}
247406db247cSraghuram 			D1(vswp, "%s:exit\n", __func__);
247506db247cSraghuram 			return;
247606db247cSraghuram 		}
247706db247cSraghuram 	}
247806db247cSraghuram 	RW_EXIT(&vswp->if_lockrw);
247906db247cSraghuram 	/*
248006db247cSraghuram 	 * If COPYMSG flag is passed, then make a copy
248106db247cSraghuram 	 * of the message chain and send up the copy.
248206db247cSraghuram 	 */
248306db247cSraghuram 	if (flags & VSW_MACRX_COPYMSG) {
248406db247cSraghuram 		mp = copymsgchain(mp);
2485f0ca1d9aSsb155480 		if (mp == NULL) {
248606db247cSraghuram 			D1(vswp, "%s:exit\n", __func__);
248706db247cSraghuram 			return;
248806db247cSraghuram 		}
248906db247cSraghuram 	}
249006db247cSraghuram 
2491f0ca1d9aSsb155480 	D2(vswp, "%s: sending up stack", __func__);
2492c1c61f44Ssb155480 
2493c1c61f44Ssb155480 	mpt = NULL;
2494c1c61f44Ssb155480 	(void) vsw_vlan_frame_untag(vswp, VSW_LOCALDEV, &mp, &mpt);
2495c1c61f44Ssb155480 	if (mp != NULL) {
2496ba2e4443Sseb 		mac_rx(vswp->if_mh, mrh, mp);
2497c1c61f44Ssb155480 	}
249806db247cSraghuram 	D1(vswp, "%s:exit\n", __func__);
24991ae08745Sheppo }
25001ae08745Sheppo 
250106db247cSraghuram /* copy mac address of vsw into soft state structure */
25021ae08745Sheppo static void
250306db247cSraghuram vsw_save_lmacaddr(vsw_t *vswp, uint64_t macaddr)
25041ae08745Sheppo {
25051ae08745Sheppo 	int	i;
25061ae08745Sheppo 
250706db247cSraghuram 	WRITE_ENTER(&vswp->if_lockrw);
250806db247cSraghuram 	for (i = ETHERADDRL - 1; i >= 0; i--) {
250906db247cSraghuram 		vswp->if_addr.ether_addr_octet[i] = macaddr & 0xFF;
251006db247cSraghuram 		macaddr >>= 8;
25111ae08745Sheppo 	}
251206db247cSraghuram 	RW_EXIT(&vswp->if_lockrw);
25131ae08745Sheppo }
2514da14cebeSEric Cheng 
2515da14cebeSEric Cheng /* Compare VLAN ids, array size expected to be same. */
2516da14cebeSEric Cheng static boolean_t
2517da14cebeSEric Cheng vsw_cmp_vids(vsw_vlanid_t *vids1, vsw_vlanid_t *vids2, int nvids)
2518da14cebeSEric Cheng {
2519da14cebeSEric Cheng 	int i, j;
2520da14cebeSEric Cheng 	uint16_t vid;
2521da14cebeSEric Cheng 
2522da14cebeSEric Cheng 	for (i = 0; i < nvids; i++) {
2523da14cebeSEric Cheng 		vid = vids1[i].vl_vid;
2524da14cebeSEric Cheng 		for (j = 0; j < nvids; j++) {
2525da14cebeSEric Cheng 			if (vid == vids2[i].vl_vid)
2526da14cebeSEric Cheng 				break;
2527da14cebeSEric Cheng 		}
2528da14cebeSEric Cheng 		if (j == nvids) {
2529da14cebeSEric Cheng 			return (B_FALSE);
2530da14cebeSEric Cheng 		}
2531da14cebeSEric Cheng 	}
2532da14cebeSEric Cheng 	return (B_TRUE);
2533da14cebeSEric Cheng }
2534