xref: /titanic_53/usr/src/uts/sun4v/io/vsw.c (revision bce0a86e5d4d65341c5aca6da2595c848297b2aa)
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 /*
236f09f0feSWENTAO YANG  * Copyright 2009 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);
806f09f0feSWENTAO YANG static	int vsw_unattach(vsw_t *vswp);
8134683adeSsg70180 static	int vsw_get_md_physname(vsw_t *, md_t *, mde_cookie_t, char *);
82da14cebeSEric Cheng static	int vsw_get_md_smodes(vsw_t *, md_t *, mde_cookie_t, uint8_t *);
8334b64c01SWENTAO YANG void vsw_destroy_rxpools(void *);
841ae08745Sheppo 
851ae08745Sheppo /* MDEG routines */
8634683adeSsg70180 static	int vsw_mdeg_register(vsw_t *vswp);
871ae08745Sheppo static	void vsw_mdeg_unregister(vsw_t *vswp);
881ae08745Sheppo static	int vsw_mdeg_cb(void *cb_argp, mdeg_result_t *);
8934683adeSsg70180 static	int vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *);
9019b65a69Ssb155480 static	int vsw_get_initial_md_properties(vsw_t *vswp, md_t *, mde_cookie_t);
91c1c61f44Ssb155480 static	int vsw_read_mdprops(vsw_t *vswp);
92c1c61f44Ssb155480 static	void vsw_vlan_read_ids(void *arg, int type, md_t *mdp,
93da14cebeSEric Cheng 	mde_cookie_t node, uint16_t *pvidp, vsw_vlanid_t **vidspp,
94c1c61f44Ssb155480 	uint16_t *nvidsp, uint16_t *default_idp);
95*bce0a86eSWENTAO YANG static	void vsw_port_read_bandwidth(vsw_port_t *portp, md_t *mdp,
96*bce0a86eSWENTAO YANG 	mde_cookie_t node, uint64_t *bw);
97c1c61f44Ssb155480 static	int vsw_port_read_props(vsw_port_t *portp, vsw_t *vswp,
98c1c61f44Ssb155480 	md_t *mdp, mde_cookie_t *node);
99f0ca1d9aSsb155480 static	void vsw_read_pri_eth_types(vsw_t *vswp, md_t *mdp,
100f0ca1d9aSsb155480 	mde_cookie_t node);
1017b1f684aSSriharsha Basavapatna static	void vsw_mtu_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
1027b1f684aSSriharsha Basavapatna 	uint32_t *mtu);
1037b1f684aSSriharsha Basavapatna static	int vsw_mtu_update(vsw_t *vswp, uint32_t mtu);
1041107ea93SSriharsha Basavapatna static	void vsw_linkprop_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
1051107ea93SSriharsha Basavapatna 	boolean_t *pls);
106*bce0a86eSWENTAO YANG static	void vsw_bandwidth_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
107*bce0a86eSWENTAO YANG 	uint64_t *bw);
10834683adeSsg70180 static	void vsw_update_md_prop(vsw_t *, md_t *, mde_cookie_t);
10919b65a69Ssb155480 static void vsw_save_lmacaddr(vsw_t *vswp, uint64_t macaddr);
110da14cebeSEric Cheng static boolean_t vsw_cmp_vids(vsw_vlanid_t *vids1,
111da14cebeSEric Cheng 	vsw_vlanid_t *vids2, int nvids);
1121ae08745Sheppo 
11306db247cSraghuram /* Mac driver related routines */
11406db247cSraghuram static int vsw_mac_register(vsw_t *);
11506db247cSraghuram static int vsw_mac_unregister(vsw_t *);
11606db247cSraghuram static int vsw_m_stat(void *, uint_t, uint64_t *);
11706db247cSraghuram static void vsw_m_stop(void *arg);
11806db247cSraghuram static int vsw_m_start(void *arg);
11906db247cSraghuram static int vsw_m_unicst(void *arg, const uint8_t *);
12006db247cSraghuram static int vsw_m_multicst(void *arg, boolean_t, const uint8_t *);
12106db247cSraghuram static int vsw_m_promisc(void *arg, boolean_t);
12206db247cSraghuram static mblk_t *vsw_m_tx(void *arg, mblk_t *);
123d8a518c8SSriharsha Basavapatna void vsw_mac_link_update(vsw_t *vswp, link_state_t link_state);
124f0ca1d9aSsb155480 void vsw_mac_rx(vsw_t *vswp, mac_resource_handle_t mrh,
125f0ca1d9aSsb155480     mblk_t *mp, vsw_macrx_flags_t flags);
1261107ea93SSriharsha Basavapatna void vsw_physlink_state_update(vsw_t *vswp);
1271ae08745Sheppo 
12806db247cSraghuram /*
12906db247cSraghuram  * Functions imported from other files.
13006db247cSraghuram  */
131808f26a8SSriharsha Basavapatna extern void vsw_setup_switching_thread(void *arg);
132808f26a8SSriharsha Basavapatna extern int vsw_setup_switching_start(vsw_t *vswp);
133808f26a8SSriharsha Basavapatna extern void vsw_setup_switching_stop(vsw_t *vswp);
13406db247cSraghuram extern int vsw_setup_switching(vsw_t *);
1357a327842Swentaoy extern void vsw_switch_frame_nop(vsw_t *vswp, mblk_t *mp, int caller,
1367a327842Swentaoy     vsw_port_t *port, mac_resource_handle_t mrh);
13706db247cSraghuram extern int vsw_add_mcst(vsw_t *, uint8_t, uint64_t, void *);
13806db247cSraghuram extern int vsw_del_mcst(vsw_t *, uint8_t, uint64_t, void *);
13906db247cSraghuram extern void vsw_del_mcst_vsw(vsw_t *);
14006db247cSraghuram extern mcst_addr_t *vsw_del_addr(uint8_t devtype, void *arg, uint64_t addr);
1416f09f0feSWENTAO YANG extern void vsw_detach_ports(vsw_t *vswp);
14206db247cSraghuram extern int vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node);
14306db247cSraghuram extern int vsw_port_detach(vsw_t *vswp, int p_instance);
144c1c61f44Ssb155480 static int vsw_port_update(vsw_t *vswp, md_t *curr_mdp, mde_cookie_t curr_mdex,
145c1c61f44Ssb155480 	md_t *prev_mdp, mde_cookie_t prev_mdex);
146c1c61f44Ssb155480 extern	int vsw_port_attach(vsw_port_t *port);
14706db247cSraghuram extern vsw_port_t *vsw_lookup_port(vsw_t *vswp, int p_instance);
14806db247cSraghuram extern int vsw_mac_open(vsw_t *vswp);
14906db247cSraghuram extern void vsw_mac_close(vsw_t *vswp);
150da14cebeSEric Cheng extern void vsw_mac_cleanup_ports(vsw_t *vswp);
15106db247cSraghuram extern void vsw_unset_addrs(vsw_t *vswp);
152d8a518c8SSriharsha Basavapatna extern void vsw_setup_switching_post_process(vsw_t *vswp);
153c1c61f44Ssb155480 extern void vsw_create_vlans(void *arg, int type);
154c1c61f44Ssb155480 extern void vsw_destroy_vlans(void *arg, int type);
155c1c61f44Ssb155480 extern void vsw_vlan_add_ids(void *arg, int type);
156c1c61f44Ssb155480 extern void vsw_vlan_remove_ids(void *arg, int type);
157c1c61f44Ssb155480 extern void vsw_vlan_unaware_port_reset(vsw_port_t *portp);
158c1c61f44Ssb155480 extern uint32_t vsw_vlan_frame_untag(void *arg, int type, mblk_t **np,
159c1c61f44Ssb155480 	mblk_t **npt);
160c1c61f44Ssb155480 extern mblk_t *vsw_vlan_frame_pretag(void *arg, int type, mblk_t *mp);
161678453a8Sspeer extern void vsw_hio_cleanup(vsw_t *vswp);
162da14cebeSEric Cheng extern void vsw_hio_start_ports(vsw_t *vswp);
163da14cebeSEric Cheng extern void vsw_hio_port_update(vsw_port_t *portp, boolean_t hio_enabled);
164da14cebeSEric Cheng extern int vsw_mac_multicast_add(vsw_t *, vsw_port_t *, mcst_addr_t *, int);
165da14cebeSEric Cheng extern void vsw_mac_multicast_remove(vsw_t *, vsw_port_t *, mcst_addr_t *, int);
166da14cebeSEric Cheng extern void vsw_mac_port_reconfig_vlans(vsw_port_t *portp, uint16_t new_pvid,
167da14cebeSEric Cheng     vsw_vlanid_t *new_vids, int new_nvids);
168da14cebeSEric Cheng extern int vsw_mac_client_init(vsw_t *vswp, vsw_port_t *port, int type);
169da14cebeSEric Cheng extern void vsw_mac_client_cleanup(vsw_t *vswp, vsw_port_t *port, int type);
170da14cebeSEric Cheng extern void vsw_if_mac_reconfig(vsw_t *vswp, boolean_t update_vlans,
171da14cebeSEric Cheng     uint16_t new_pvid, vsw_vlanid_t *new_vids, int new_nvids);
1727b1f684aSSriharsha Basavapatna extern void vsw_reset_ports(vsw_t *vswp);
1737b1f684aSSriharsha Basavapatna extern void vsw_port_reset(vsw_port_t *portp);
1741107ea93SSriharsha Basavapatna extern void vsw_physlink_update_ports(vsw_t *vswp);
175*bce0a86eSWENTAO YANG extern void vsw_update_bandwidth(vsw_t *vswp, vsw_port_t *port, int type,
176*bce0a86eSWENTAO YANG     uint64_t maxbw);
17706db247cSraghuram 
17806db247cSraghuram /*
17906db247cSraghuram  * Internal tunables.
18006db247cSraghuram  */
181445b4c2eSsb155480 int	vsw_num_handshakes = VNET_NUM_HANDSHAKES; /* # of handshake attempts */
1821ae08745Sheppo int	vsw_wretries = 100;		/* # of write attempts */
183d10e4ef2Snarayan int	vsw_desc_delay = 0;		/* delay in us */
184d10e4ef2Snarayan int	vsw_read_attempts = 5;		/* # of reads of descriptor */
18519b65a69Ssb155480 int	vsw_setup_switching_delay = 3;	/* setup sw timeout interval in sec */
1860e8b4070Ssb155480 int	vsw_mac_open_retries = 300;	/* max # of mac_open() retries */
1870e8b4070Ssb155480 					/* 300*3 = 900sec(15min) of max tmout */
18806db247cSraghuram int	vsw_ldc_tx_delay = 5;		/* delay(ticks) for tx retries */
18906db247cSraghuram int	vsw_ldc_tx_retries = 10;	/* # of ldc tx retries */
1906f09f0feSWENTAO YANG int	vsw_ldc_retries = 5;		/* # of ldc_close() retries */
1916f09f0feSWENTAO YANG int	vsw_ldc_delay = 1000;		/* 1 ms delay for ldc_close() */
19206db247cSraghuram boolean_t vsw_ldc_rxthr_enabled = B_TRUE;	/* LDC Rx thread enabled */
19306db247cSraghuram boolean_t vsw_ldc_txthr_enabled = B_TRUE;	/* LDC Tx thread enabled */
19434b64c01SWENTAO YANG int	vsw_rxpool_cleanup_delay = 100000;	/* 100ms */
19534b64c01SWENTAO YANG 
196d10e4ef2Snarayan 
197c1c61f44Ssb155480 uint32_t	vsw_fdb_nchains = 8;	/* # of chains in fdb hash table */
198c1c61f44Ssb155480 uint32_t	vsw_vlan_nchains = 4;	/* # of chains in vlan id hash table */
199c1c61f44Ssb155480 uint32_t	vsw_ethermtu = 1500;	/* mtu of the device */
200c1c61f44Ssb155480 
201c1c61f44Ssb155480 /* delay in usec to wait for all references on a fdb entry to be dropped */
202c1c61f44Ssb155480 uint32_t vsw_fdbe_refcnt_delay = 10;
203c1c61f44Ssb155480 
204c1c61f44Ssb155480 /*
205c1c61f44Ssb155480  * Default vlan id. This is only used internally when the "default-vlan-id"
206c1c61f44Ssb155480  * property is not present in the MD device node. Therefore, this should not be
207c1c61f44Ssb155480  * used as a tunable; if this value is changed, the corresponding variable
208c1c61f44Ssb155480  * should be updated to the same value in all vnets connected to this vsw.
209c1c61f44Ssb155480  */
210c1c61f44Ssb155480 uint16_t	vsw_default_vlan_id = 1;
211c1c61f44Ssb155480 
212f0ca1d9aSsb155480 /*
213f0ca1d9aSsb155480  * Workaround for a version handshake bug in obp's vnet.
214f0ca1d9aSsb155480  * If vsw initiates version negotiation starting from the highest version,
215f0ca1d9aSsb155480  * obp sends a nack and terminates version handshake. To workaround
216f0ca1d9aSsb155480  * this, we do not initiate version handshake when the channel comes up.
217f0ca1d9aSsb155480  * Instead, we wait for the peer to send its version info msg and go through
218f0ca1d9aSsb155480  * the version protocol exchange. If we successfully negotiate a version,
219f0ca1d9aSsb155480  * before sending the ack, we send our version info msg to the peer
220f0ca1d9aSsb155480  * using the <major,minor> version that we are about to ack.
221f0ca1d9aSsb155480  */
222f0ca1d9aSsb155480 boolean_t vsw_obp_ver_proto_workaround = B_TRUE;
223f0ca1d9aSsb155480 
224f0ca1d9aSsb155480 /*
225f0ca1d9aSsb155480  * In the absence of "priority-ether-types" property in MD, the following
226f0ca1d9aSsb155480  * internal tunable can be set to specify a single priority ethertype.
227f0ca1d9aSsb155480  */
228f0ca1d9aSsb155480 uint64_t vsw_pri_eth_type = 0;
229f0ca1d9aSsb155480 
230f0ca1d9aSsb155480 /*
231f0ca1d9aSsb155480  * Number of transmit priority buffers that are preallocated per device.
232f0ca1d9aSsb155480  * This number is chosen to be a small value to throttle transmission
233f0ca1d9aSsb155480  * of priority packets. Note: Must be a power of 2 for vio_create_mblks().
234f0ca1d9aSsb155480  */
235f0ca1d9aSsb155480 uint32_t vsw_pri_tx_nmblks = 64;
236d10e4ef2Snarayan 
23751aa9d07Ssb155480 /*
23851aa9d07Ssb155480  * Number of RARP packets sent to announce macaddr to the physical switch,
23951aa9d07Ssb155480  * after vsw's physical device is changed dynamically or after a guest (client
24051aa9d07Ssb155480  * vnet) is live migrated in.
24151aa9d07Ssb155480  */
24251aa9d07Ssb155480 uint32_t vsw_publish_macaddr_count = 3;
24351aa9d07Ssb155480 
244678453a8Sspeer boolean_t vsw_hio_enabled = B_TRUE;	/* Enable/disable HybridIO */
245678453a8Sspeer int vsw_hio_max_cleanup_retries = 10;	/* Max retries for HybridIO cleanp */
246678453a8Sspeer int vsw_hio_cleanup_delay = 10000;	/* 10ms */
247678453a8Sspeer 
248f0ca1d9aSsb155480 /* Number of transmit descriptors -  must be power of 2 */
249f0ca1d9aSsb155480 uint32_t vsw_ntxds = VSW_RING_NUM_EL;
250f0ca1d9aSsb155480 
25106db247cSraghuram /*
25206db247cSraghuram  * Max number of mblks received in one receive operation.
25306db247cSraghuram  */
25406db247cSraghuram uint32_t vsw_chain_len = (VSW_NUM_MBLKS * 0.6);
25506db247cSraghuram 
25606db247cSraghuram /*
2577b1f684aSSriharsha Basavapatna  * Internal tunables for receive buffer pools, that is,  the size and number of
2587b1f684aSSriharsha Basavapatna  * mblks for each pool. At least 3 sizes must be specified if these are used.
2597b1f684aSSriharsha Basavapatna  * The sizes must be specified in increasing order. Non-zero value of the first
2607b1f684aSSriharsha Basavapatna  * size will be used as a hint to use these values instead of the algorithm
2617b1f684aSSriharsha Basavapatna  * that determines the sizes based on MTU.
26206db247cSraghuram  */
2637b1f684aSSriharsha Basavapatna uint32_t vsw_mblk_size1 = 0;
2647b1f684aSSriharsha Basavapatna uint32_t vsw_mblk_size2 = 0;
2657b1f684aSSriharsha Basavapatna uint32_t vsw_mblk_size3 = 0;
2667b1f684aSSriharsha Basavapatna uint32_t vsw_mblk_size4 = 0;
26706db247cSraghuram uint32_t vsw_num_mblks1 = VSW_NUM_MBLKS;	/* number of mblks for pool1 */
26806db247cSraghuram uint32_t vsw_num_mblks2 = VSW_NUM_MBLKS;	/* number of mblks for pool2 */
26906db247cSraghuram uint32_t vsw_num_mblks3 = VSW_NUM_MBLKS;	/* number of mblks for pool3 */
2707b1f684aSSriharsha Basavapatna uint32_t vsw_num_mblks4 = VSW_NUM_MBLKS;	/* number of mblks for pool4 */
2717b1f684aSSriharsha Basavapatna 
2727b1f684aSSriharsha Basavapatna /*
2737b1f684aSSriharsha Basavapatna  * Set this to non-zero to enable additional internal receive buffer pools
2747b1f684aSSriharsha Basavapatna  * based on the MTU of the device for better performance at the cost of more
2757b1f684aSSriharsha Basavapatna  * memory consumption. This is turned off by default, to use allocb(9F) for
2767b1f684aSSriharsha Basavapatna  * receive buffer allocations of sizes > 2K.
2777b1f684aSSriharsha Basavapatna  */
2787b1f684aSSriharsha Basavapatna boolean_t vsw_jumbo_rxpools = B_FALSE;
27906db247cSraghuram 
28006db247cSraghuram /*
281f0ca1d9aSsb155480  * vsw_max_tx_qcount is the maximum # of packets that can be queued
282f0ca1d9aSsb155480  * before the tx worker thread begins processing the queue. Its value
283f0ca1d9aSsb155480  * is chosen to be 4x the default length of tx descriptor ring.
284f0ca1d9aSsb155480  */
285f0ca1d9aSsb155480 uint32_t vsw_max_tx_qcount = 4 * VSW_RING_NUM_EL;
286f0ca1d9aSsb155480 
287f0ca1d9aSsb155480 /*
28806db247cSraghuram  * MAC callbacks
28906db247cSraghuram  */
290ba2e4443Sseb static	mac_callbacks_t	vsw_m_callbacks = {
291ba2e4443Sseb 	0,
292ba2e4443Sseb 	vsw_m_stat,
293ba2e4443Sseb 	vsw_m_start,
294ba2e4443Sseb 	vsw_m_stop,
295ba2e4443Sseb 	vsw_m_promisc,
296ba2e4443Sseb 	vsw_m_multicst,
297ba2e4443Sseb 	vsw_m_unicst,
298ba2e4443Sseb 	vsw_m_tx,
299ba2e4443Sseb 	NULL,
300ba2e4443Sseb 	NULL,
301ba2e4443Sseb 	NULL
302ba2e4443Sseb };
303ba2e4443Sseb 
3041ae08745Sheppo static	struct	cb_ops	vsw_cb_ops = {
3051ae08745Sheppo 	nulldev,			/* cb_open */
3061ae08745Sheppo 	nulldev,			/* cb_close */
3071ae08745Sheppo 	nodev,				/* cb_strategy */
3081ae08745Sheppo 	nodev,				/* cb_print */
3091ae08745Sheppo 	nodev,				/* cb_dump */
3101ae08745Sheppo 	nodev,				/* cb_read */
3111ae08745Sheppo 	nodev,				/* cb_write */
3121ae08745Sheppo 	nodev,				/* cb_ioctl */
3131ae08745Sheppo 	nodev,				/* cb_devmap */
3141ae08745Sheppo 	nodev,				/* cb_mmap */
3151ae08745Sheppo 	nodev,				/* cb_segmap */
3161ae08745Sheppo 	nochpoll,			/* cb_chpoll */
3171ae08745Sheppo 	ddi_prop_op,			/* cb_prop_op */
3181ae08745Sheppo 	NULL,				/* cb_stream */
3191ae08745Sheppo 	D_MP,				/* cb_flag */
3201ae08745Sheppo 	CB_REV,				/* rev */
3211ae08745Sheppo 	nodev,				/* int (*cb_aread)() */
3221ae08745Sheppo 	nodev				/* int (*cb_awrite)() */
3231ae08745Sheppo };
3241ae08745Sheppo 
3251ae08745Sheppo static	struct	dev_ops	vsw_ops = {
3261ae08745Sheppo 	DEVO_REV,		/* devo_rev */
3271ae08745Sheppo 	0,			/* devo_refcnt */
32871184a40SWENTAO YANG 	NULL,			/* devo_getinfo */
3291ae08745Sheppo 	nulldev,		/* devo_identify */
3301ae08745Sheppo 	nulldev,		/* devo_probe */
3311ae08745Sheppo 	vsw_attach,		/* devo_attach */
3321ae08745Sheppo 	vsw_detach,		/* devo_detach */
3331ae08745Sheppo 	nodev,			/* devo_reset */
3341ae08745Sheppo 	&vsw_cb_ops,		/* devo_cb_ops */
3351ae08745Sheppo 	(struct bus_ops *)NULL,	/* devo_bus_ops */
3361ae08745Sheppo 	ddi_power		/* devo_power */
3371ae08745Sheppo };
3381ae08745Sheppo 
3391ae08745Sheppo extern	struct	mod_ops	mod_driverops;
3401ae08745Sheppo static struct modldrv vswmodldrv = {
3411ae08745Sheppo 	&mod_driverops,
342205eeb1aSlm66018 	"sun4v Virtual Switch",
3431ae08745Sheppo 	&vsw_ops,
3441ae08745Sheppo };
3451ae08745Sheppo 
3461ae08745Sheppo #define	LDC_ENTER_LOCK(ldcp)	\
3471ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_cblock));\
34806db247cSraghuram 				mutex_enter(&((ldcp)->ldc_rxlock));\
3491ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_txlock));
3501ae08745Sheppo #define	LDC_EXIT_LOCK(ldcp)	\
3511ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_txlock));\
35206db247cSraghuram 				mutex_exit(&((ldcp)->ldc_rxlock));\
3531ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_cblock));
3541ae08745Sheppo 
3551ae08745Sheppo /* Driver soft state ptr  */
3561ae08745Sheppo static void	*vsw_state;
3571ae08745Sheppo 
3581ae08745Sheppo /*
3591ae08745Sheppo  * Linked list of "vsw_t" structures - one per instance.
3601ae08745Sheppo  */
3611ae08745Sheppo vsw_t		*vsw_head = NULL;
3621ae08745Sheppo krwlock_t	vsw_rw;
3631ae08745Sheppo 
3641ae08745Sheppo /*
3651ae08745Sheppo  * Property names
3661ae08745Sheppo  */
3671ae08745Sheppo static char vdev_propname[] = "virtual-device";
3681ae08745Sheppo static char vsw_propname[] = "virtual-network-switch";
3691ae08745Sheppo static char physdev_propname[] = "vsw-phys-dev";
3701ae08745Sheppo static char smode_propname[] = "vsw-switch-mode";
3711ae08745Sheppo static char macaddr_propname[] = "local-mac-address";
3721ae08745Sheppo static char remaddr_propname[] = "remote-mac-address";
3731ae08745Sheppo static char ldcids_propname[] = "ldc-ids";
3741ae08745Sheppo static char chan_propname[] = "channel-endpoint";
3751ae08745Sheppo static char id_propname[] = "id";
3761ae08745Sheppo static char reg_propname[] = "reg";
377f0ca1d9aSsb155480 static char pri_types_propname[] = "priority-ether-types";
378c1c61f44Ssb155480 static char vsw_pvid_propname[] = "port-vlan-id";
379c1c61f44Ssb155480 static char vsw_vid_propname[] = "vlan-id";
380c1c61f44Ssb155480 static char vsw_dvid_propname[] = "default-vlan-id";
381c1c61f44Ssb155480 static char port_pvid_propname[] = "remote-port-vlan-id";
382c1c61f44Ssb155480 static char port_vid_propname[] = "remote-vlan-id";
383678453a8Sspeer static char hybrid_propname[] = "hybrid";
3847b1f684aSSriharsha Basavapatna static char vsw_mtu_propname[] = "mtu";
3851107ea93SSriharsha Basavapatna static char vsw_linkprop_propname[] = "linkprop";
386*bce0a86eSWENTAO YANG static char vsw_maxbw_propname[] = "maxbw";
387*bce0a86eSWENTAO YANG static char port_maxbw_propname[] = "maxbw";
3881ae08745Sheppo 
3891ae08745Sheppo /*
3901ae08745Sheppo  * Matching criteria passed to the MDEG to register interest
3911ae08745Sheppo  * in changes to 'virtual-device-port' nodes identified by their
3921ae08745Sheppo  * 'id' property.
3931ae08745Sheppo  */
3941ae08745Sheppo static md_prop_match_t vport_prop_match[] = {
3951ae08745Sheppo 	{ MDET_PROP_VAL,    "id"   },
3961ae08745Sheppo 	{ MDET_LIST_END,    NULL    }
3971ae08745Sheppo };
3981ae08745Sheppo 
3991ae08745Sheppo static mdeg_node_match_t vport_match = { "virtual-device-port",
4001ae08745Sheppo 						vport_prop_match };
4011ae08745Sheppo 
4021ae08745Sheppo /*
40334683adeSsg70180  * Matching criteria passed to the MDEG to register interest
40434683adeSsg70180  * in changes to 'virtual-device' nodes (i.e. vsw nodes) identified
40534683adeSsg70180  * by their 'name' and 'cfg-handle' properties.
40634683adeSsg70180  */
40734683adeSsg70180 static md_prop_match_t vdev_prop_match[] = {
40834683adeSsg70180 	{ MDET_PROP_STR,    "name"   },
40934683adeSsg70180 	{ MDET_PROP_VAL,    "cfg-handle" },
41034683adeSsg70180 	{ MDET_LIST_END,    NULL    }
41134683adeSsg70180 };
41234683adeSsg70180 
41334683adeSsg70180 static mdeg_node_match_t vdev_match = { "virtual-device",
41434683adeSsg70180 						vdev_prop_match };
41534683adeSsg70180 
41634683adeSsg70180 
41734683adeSsg70180 /*
4181ae08745Sheppo  * Specification of an MD node passed to the MDEG to filter any
4191ae08745Sheppo  * 'vport' nodes that do not belong to the specified node. This
4201ae08745Sheppo  * template is copied for each vsw instance and filled in with
4211ae08745Sheppo  * the appropriate 'cfg-handle' value before being passed to the MDEG.
4221ae08745Sheppo  */
4231ae08745Sheppo static mdeg_prop_spec_t vsw_prop_template[] = {
4241ae08745Sheppo 	{ MDET_PROP_STR,    "name",		vsw_propname },
4251ae08745Sheppo 	{ MDET_PROP_VAL,    "cfg-handle",	NULL	},
4261ae08745Sheppo 	{ MDET_LIST_END,    NULL,		NULL	}
4271ae08745Sheppo };
4281ae08745Sheppo 
4291ae08745Sheppo #define	VSW_SET_MDEG_PROP_INST(specp, val)	(specp)[1].ps_val = (val);
4301ae08745Sheppo 
43106db247cSraghuram #ifdef	DEBUG
4327636cb21Slm66018 /*
4331ae08745Sheppo  * Print debug messages - set to 0x1f to enable all msgs
4341ae08745Sheppo  * or 0x0 to turn all off.
4351ae08745Sheppo  */
4361ae08745Sheppo int vswdbg = 0x0;
4371ae08745Sheppo 
4381ae08745Sheppo /*
4391ae08745Sheppo  * debug levels:
4401ae08745Sheppo  * 0x01:	Function entry/exit tracing
4411ae08745Sheppo  * 0x02:	Internal function messages
4421ae08745Sheppo  * 0x04:	Verbose internal messages
4431ae08745Sheppo  * 0x08:	Warning messages
4441ae08745Sheppo  * 0x10:	Error messages
4451ae08745Sheppo  */
4461ae08745Sheppo 
44706db247cSraghuram void
4481ae08745Sheppo vswdebug(vsw_t *vswp, const char *fmt, ...)
4491ae08745Sheppo {
4501ae08745Sheppo 	char buf[512];
4511ae08745Sheppo 	va_list ap;
4521ae08745Sheppo 
4531ae08745Sheppo 	va_start(ap, fmt);
4541ae08745Sheppo 	(void) vsprintf(buf, fmt, ap);
4551ae08745Sheppo 	va_end(ap);
4561ae08745Sheppo 
4571ae08745Sheppo 	if (vswp == NULL)
4581ae08745Sheppo 		cmn_err(CE_CONT, "%s\n", buf);
4591ae08745Sheppo 	else
4601ae08745Sheppo 		cmn_err(CE_CONT, "vsw%d: %s\n", vswp->instance, buf);
4611ae08745Sheppo }
4621ae08745Sheppo 
4631ae08745Sheppo #endif	/* DEBUG */
4641ae08745Sheppo 
4651ae08745Sheppo static struct modlinkage modlinkage = {
4661ae08745Sheppo 	MODREV_1,
4671ae08745Sheppo 	&vswmodldrv,
4681ae08745Sheppo 	NULL
4691ae08745Sheppo };
4701ae08745Sheppo 
4711ae08745Sheppo int
4721ae08745Sheppo _init(void)
4731ae08745Sheppo {
4741ae08745Sheppo 	int status;
4751ae08745Sheppo 
4761ae08745Sheppo 	rw_init(&vsw_rw, NULL, RW_DRIVER, NULL);
4771ae08745Sheppo 
4781ae08745Sheppo 	status = ddi_soft_state_init(&vsw_state, sizeof (vsw_t), 1);
4791ae08745Sheppo 	if (status != 0) {
4801ae08745Sheppo 		return (status);
4811ae08745Sheppo 	}
4821ae08745Sheppo 
48306db247cSraghuram 	mac_init_ops(&vsw_ops, DRV_NAME);
4841ae08745Sheppo 	status = mod_install(&modlinkage);
4851ae08745Sheppo 	if (status != 0) {
4861ae08745Sheppo 		ddi_soft_state_fini(&vsw_state);
4871ae08745Sheppo 	}
4881ae08745Sheppo 	return (status);
4891ae08745Sheppo }
4901ae08745Sheppo 
4911ae08745Sheppo int
4921ae08745Sheppo _fini(void)
4931ae08745Sheppo {
4941ae08745Sheppo 	int status;
4951ae08745Sheppo 
4961ae08745Sheppo 	status = mod_remove(&modlinkage);
4971ae08745Sheppo 	if (status != 0)
4981ae08745Sheppo 		return (status);
4991ae08745Sheppo 	mac_fini_ops(&vsw_ops);
5001ae08745Sheppo 	ddi_soft_state_fini(&vsw_state);
5011ae08745Sheppo 
5021ae08745Sheppo 	rw_destroy(&vsw_rw);
5031ae08745Sheppo 
5041ae08745Sheppo 	return (status);
5051ae08745Sheppo }
5061ae08745Sheppo 
5071ae08745Sheppo int
5081ae08745Sheppo _info(struct modinfo *modinfop)
5091ae08745Sheppo {
5101ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
5111ae08745Sheppo }
5121ae08745Sheppo 
5131ae08745Sheppo static int
5141ae08745Sheppo vsw_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5151ae08745Sheppo {
5161ae08745Sheppo 	vsw_t			*vswp;
51734683adeSsg70180 	int			instance;
5181ae08745Sheppo 	char			hashname[MAXNAMELEN];
5191ae08745Sheppo 	char			qname[TASKQ_NAMELEN];
5206f09f0feSWENTAO YANG 	vsw_attach_progress_t	progress = PROG_init;
52119b65a69Ssb155480 	int			rv;
5221ae08745Sheppo 
5231ae08745Sheppo 	switch (cmd) {
5241ae08745Sheppo 	case DDI_ATTACH:
5251ae08745Sheppo 		break;
5261ae08745Sheppo 	case DDI_RESUME:
5271ae08745Sheppo 		/* nothing to do for this non-device */
5281ae08745Sheppo 		return (DDI_SUCCESS);
5291ae08745Sheppo 	case DDI_PM_RESUME:
5301ae08745Sheppo 	default:
5311ae08745Sheppo 		return (DDI_FAILURE);
5321ae08745Sheppo 	}
5331ae08745Sheppo 
5341ae08745Sheppo 	instance = ddi_get_instance(dip);
5351ae08745Sheppo 	if (ddi_soft_state_zalloc(vsw_state, instance) != DDI_SUCCESS) {
5361ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_soft_state_zalloc failed", instance);
5371ae08745Sheppo 		return (DDI_FAILURE);
5381ae08745Sheppo 	}
5391ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
5401ae08745Sheppo 
5411ae08745Sheppo 	if (vswp == NULL) {
5421ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_get_soft_state failed", instance);
5431ae08745Sheppo 		goto vsw_attach_fail;
5441ae08745Sheppo 	}
5451ae08745Sheppo 
5461ae08745Sheppo 	vswp->dip = dip;
5471ae08745Sheppo 	vswp->instance = instance;
5481107ea93SSriharsha Basavapatna 	vswp->phys_link_state = LINK_STATE_UNKNOWN;
5491ae08745Sheppo 	ddi_set_driver_private(dip, (caddr_t)vswp);
5501ae08745Sheppo 
551da14cebeSEric Cheng 	mutex_init(&vswp->mac_lock, NULL, MUTEX_DRIVER, NULL);
55219b65a69Ssb155480 	mutex_init(&vswp->mca_lock, NULL, MUTEX_DRIVER, NULL);
553808f26a8SSriharsha Basavapatna 	mutex_init(&vswp->sw_thr_lock, NULL, MUTEX_DRIVER, NULL);
554808f26a8SSriharsha Basavapatna 	cv_init(&vswp->sw_thr_cv, NULL, CV_DRIVER, NULL);
555da14cebeSEric Cheng 	rw_init(&vswp->maccl_rwlock, NULL, RW_DRIVER, NULL);
5561ae08745Sheppo 	rw_init(&vswp->if_lockrw, NULL, RW_DRIVER, NULL);
55719b65a69Ssb155480 	rw_init(&vswp->mfdbrw, NULL, RW_DRIVER, NULL);
55819b65a69Ssb155480 	rw_init(&vswp->plist.lockrw, NULL, RW_DRIVER, NULL);
55919b65a69Ssb155480 
56019b65a69Ssb155480 	progress |= PROG_locks;
56119b65a69Ssb155480 
56219b65a69Ssb155480 	rv = vsw_read_mdprops(vswp);
56319b65a69Ssb155480 	if (rv != 0)
56419b65a69Ssb155480 		goto vsw_attach_fail;
56519b65a69Ssb155480 
56619b65a69Ssb155480 	progress |= PROG_readmd;
5671ae08745Sheppo 
5681ae08745Sheppo 	/* setup the unicast forwarding database  */
5691ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_unicst_table-%d",
5701ae08745Sheppo 	    vswp->instance);
5711ae08745Sheppo 	D2(vswp, "creating unicast hash table (%s)...", hashname);
572c1c61f44Ssb155480 	vswp->fdb_nchains = vsw_fdb_nchains;
573c1c61f44Ssb155480 	vswp->fdb_hashp = mod_hash_create_ptrhash(hashname, vswp->fdb_nchains,
5741ae08745Sheppo 	    mod_hash_null_valdtor, sizeof (void *));
575c1c61f44Ssb155480 	vsw_create_vlans((void *)vswp, VSW_LOCALDEV);
5761ae08745Sheppo 	progress |= PROG_fdb;
5771ae08745Sheppo 
5781ae08745Sheppo 	/* setup the multicast fowarding database */
5791ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_mcst_table-%d",
5801ae08745Sheppo 	    vswp->instance);
5811ae08745Sheppo 	D2(vswp, "creating multicast hash table %s)...", hashname);
582c1c61f44Ssb155480 	vswp->mfdb = mod_hash_create_ptrhash(hashname, vsw_fdb_nchains,
5831ae08745Sheppo 	    mod_hash_null_valdtor, sizeof (void *));
5841ae08745Sheppo 
5851ae08745Sheppo 	progress |= PROG_mfdb;
5861ae08745Sheppo 
5871ae08745Sheppo 	/*
5881ae08745Sheppo 	 * Create the taskq which will process all the VIO
5891ae08745Sheppo 	 * control messages.
5901ae08745Sheppo 	 */
5911ae08745Sheppo 	(void) snprintf(qname, TASKQ_NAMELEN, "vsw_taskq%d", vswp->instance);
5921ae08745Sheppo 	if ((vswp->taskq_p = ddi_taskq_create(vswp->dip, qname, 1,
5931ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
59434683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to create task queue",
59534683adeSsg70180 		    vswp->instance);
5961ae08745Sheppo 		goto vsw_attach_fail;
5971ae08745Sheppo 	}
5981ae08745Sheppo 
5991ae08745Sheppo 	progress |= PROG_taskq;
6001ae08745Sheppo 
60134b64c01SWENTAO YANG 	(void) snprintf(qname, TASKQ_NAMELEN, "vsw_rxp_taskq%d",
60234b64c01SWENTAO YANG 	    vswp->instance);
60334b64c01SWENTAO YANG 	if ((vswp->rxp_taskq = ddi_taskq_create(vswp->dip, qname, 1,
60434b64c01SWENTAO YANG 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
60534b64c01SWENTAO YANG 		cmn_err(CE_WARN, "!vsw%d: Unable to create rxp task queue",
60634b64c01SWENTAO YANG 		    vswp->instance);
60734b64c01SWENTAO YANG 		goto vsw_attach_fail;
60834b64c01SWENTAO YANG 	}
60934b64c01SWENTAO YANG 
61034b64c01SWENTAO YANG 	progress |= PROG_rxp_taskq;
61134b64c01SWENTAO YANG 
612d10e4ef2Snarayan 	/* prevent auto-detaching */
613d10e4ef2Snarayan 	if (ddi_prop_update_int(DDI_DEV_T_NONE, vswp->dip,
614d10e4ef2Snarayan 	    DDI_NO_AUTODETACH, 1) != DDI_SUCCESS) {
61534683adeSsg70180 		cmn_err(CE_NOTE, "!Unable to set \"%s\" property for "
616d10e4ef2Snarayan 		    "instance %u", DDI_NO_AUTODETACH, instance);
617d10e4ef2Snarayan 	}
618d10e4ef2Snarayan 
6191ae08745Sheppo 	/*
6207a327842Swentaoy 	 * The null switching function is set to avoid panic until
6217a327842Swentaoy 	 * switch mode is setup.
6227a327842Swentaoy 	 */
6237a327842Swentaoy 	vswp->vsw_switch_frame = vsw_switch_frame_nop;
6247a327842Swentaoy 
6257a327842Swentaoy 	/*
626808f26a8SSriharsha Basavapatna 	 * Setup the required switching mode, based on the mdprops that we read
627808f26a8SSriharsha Basavapatna 	 * earlier. We start a thread to do this, to avoid calling mac_open()
628808f26a8SSriharsha Basavapatna 	 * directly from attach().
62919b65a69Ssb155480 	 */
630808f26a8SSriharsha Basavapatna 	rv = vsw_setup_switching_start(vswp);
631808f26a8SSriharsha Basavapatna 	if (rv != 0) {
632808f26a8SSriharsha Basavapatna 		goto vsw_attach_fail;
633808f26a8SSriharsha Basavapatna 	}
63419b65a69Ssb155480 
63519b65a69Ssb155480 	progress |= PROG_swmode;
63619b65a69Ssb155480 
63719b65a69Ssb155480 	/* Register with mac layer as a provider */
63819b65a69Ssb155480 	rv = vsw_mac_register(vswp);
63919b65a69Ssb155480 	if (rv != 0)
64019b65a69Ssb155480 		goto vsw_attach_fail;
64119b65a69Ssb155480 
64219b65a69Ssb155480 	progress |= PROG_macreg;
64319b65a69Ssb155480 
64419b65a69Ssb155480 	/*
64534683adeSsg70180 	 * Now we have everything setup, register an interest in
64634683adeSsg70180 	 * specific MD nodes.
64734683adeSsg70180 	 *
64834683adeSsg70180 	 * The callback is invoked in 2 cases, firstly if upon mdeg
64934683adeSsg70180 	 * registration there are existing nodes which match our specified
65034683adeSsg70180 	 * criteria, and secondly if the MD is changed (and again, there
65134683adeSsg70180 	 * are nodes which we are interested in present within it. Note
65234683adeSsg70180 	 * that our callback will be invoked even if our specified nodes
65334683adeSsg70180 	 * have not actually changed).
65434683adeSsg70180 	 *
6551ae08745Sheppo 	 */
65619b65a69Ssb155480 	rv = vsw_mdeg_register(vswp);
65719b65a69Ssb155480 	if (rv != 0)
65834683adeSsg70180 		goto vsw_attach_fail;
6591ae08745Sheppo 
66019b65a69Ssb155480 	progress |= PROG_mdreg;
66119b65a69Ssb155480 
6626f09f0feSWENTAO YANG 	vswp->attach_progress = progress;
6636f09f0feSWENTAO YANG 
66419b65a69Ssb155480 	WRITE_ENTER(&vsw_rw);
66519b65a69Ssb155480 	vswp->next = vsw_head;
66619b65a69Ssb155480 	vsw_head = vswp;
66719b65a69Ssb155480 	RW_EXIT(&vsw_rw);
66819b65a69Ssb155480 
66919b65a69Ssb155480 	ddi_report_dev(vswp->dip);
6701ae08745Sheppo 	return (DDI_SUCCESS);
6711ae08745Sheppo 
6721ae08745Sheppo vsw_attach_fail:
6731ae08745Sheppo 	DERR(NULL, "vsw_attach: failed");
6741ae08745Sheppo 
6756f09f0feSWENTAO YANG 	vswp->attach_progress = progress;
6766f09f0feSWENTAO YANG 	(void) vsw_unattach(vswp);
6771ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
6781ae08745Sheppo 	return (DDI_FAILURE);
6791ae08745Sheppo }
6801ae08745Sheppo 
6811ae08745Sheppo static int
6821ae08745Sheppo vsw_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6831ae08745Sheppo {
6841ae08745Sheppo 	vsw_t			**vswpp, *vswp;
6851ae08745Sheppo 	int 			instance;
6861ae08745Sheppo 
6871ae08745Sheppo 	instance = ddi_get_instance(dip);
6881ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
6891ae08745Sheppo 
6901ae08745Sheppo 	if (vswp == NULL) {
6911ae08745Sheppo 		return (DDI_FAILURE);
6921ae08745Sheppo 	}
6931ae08745Sheppo 
6941ae08745Sheppo 	switch (cmd) {
6951ae08745Sheppo 	case DDI_DETACH:
6961ae08745Sheppo 		break;
6971ae08745Sheppo 	case DDI_SUSPEND:
6981ae08745Sheppo 	case DDI_PM_SUSPEND:
6991ae08745Sheppo 	default:
7001ae08745Sheppo 		return (DDI_FAILURE);
7011ae08745Sheppo 	}
7021ae08745Sheppo 
7031ae08745Sheppo 	D2(vswp, "detaching instance %d", instance);
7041ae08745Sheppo 
7056f09f0feSWENTAO YANG 	if (vsw_unattach(vswp) != 0) {
7061ae08745Sheppo 		return (DDI_FAILURE);
7071ae08745Sheppo 	}
708f0ca1d9aSsb155480 
7091ae08745Sheppo 	ddi_remove_minor_node(dip, NULL);
7101ae08745Sheppo 
7111ae08745Sheppo 	WRITE_ENTER(&vsw_rw);
7121ae08745Sheppo 	for (vswpp = &vsw_head; *vswpp; vswpp = &(*vswpp)->next) {
7131ae08745Sheppo 		if (*vswpp == vswp) {
7141ae08745Sheppo 			*vswpp = vswp->next;
7151ae08745Sheppo 			break;
7161ae08745Sheppo 		}
7171ae08745Sheppo 	}
7181ae08745Sheppo 	RW_EXIT(&vsw_rw);
7196f09f0feSWENTAO YANG 
7201ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
7211ae08745Sheppo 
7221ae08745Sheppo 	return (DDI_SUCCESS);
7231ae08745Sheppo }
7241ae08745Sheppo 
7251ae08745Sheppo /*
7266f09f0feSWENTAO YANG  * Common routine to handle vsw_attach() failure and vsw_detach(). Note that
7276f09f0feSWENTAO YANG  * the only reason this function could fail is if mac_unregister() fails.
7286f09f0feSWENTAO YANG  * Otherwise, this function must ensure that all resources are freed and return
7296f09f0feSWENTAO YANG  * success.
7306f09f0feSWENTAO YANG  */
7316f09f0feSWENTAO YANG static int
7326f09f0feSWENTAO YANG vsw_unattach(vsw_t *vswp)
7336f09f0feSWENTAO YANG {
7346f09f0feSWENTAO YANG 	vsw_attach_progress_t	progress;
7356f09f0feSWENTAO YANG 
7366f09f0feSWENTAO YANG 	progress = vswp->attach_progress;
7376f09f0feSWENTAO YANG 
7386f09f0feSWENTAO YANG 	/*
7396f09f0feSWENTAO YANG 	 * Unregister from the gldv3 subsystem. This can fail, in particular
7406f09f0feSWENTAO YANG 	 * if there are still any open references to this mac device; in which
7416f09f0feSWENTAO YANG 	 * case we just return failure without continuing to detach further.
7426f09f0feSWENTAO YANG 	 */
7436f09f0feSWENTAO YANG 	if (progress & PROG_macreg) {
7446f09f0feSWENTAO YANG 		if (vsw_mac_unregister(vswp) != 0) {
7456f09f0feSWENTAO YANG 			cmn_err(CE_WARN, "!vsw%d: Unable to detach from "
7466f09f0feSWENTAO YANG 			    "MAC layer", vswp->instance);
7476f09f0feSWENTAO YANG 			return (1);
7486f09f0feSWENTAO YANG 		}
7496f09f0feSWENTAO YANG 		progress &= ~PROG_macreg;
7506f09f0feSWENTAO YANG 	}
7516f09f0feSWENTAO YANG 
7526f09f0feSWENTAO YANG 	/*
7536f09f0feSWENTAO YANG 	 * Now that we have unregistered from gldv3, we must finish all other
7546f09f0feSWENTAO YANG 	 * steps and successfully return from this function; otherwise we will
7556f09f0feSWENTAO YANG 	 * end up leaving the device in a broken/unusable state.
7566f09f0feSWENTAO YANG 	 *
7576f09f0feSWENTAO YANG 	 * If we have registered with mdeg, unregister now to stop further
7586f09f0feSWENTAO YANG 	 * callbacks to this vsw device and/or its ports. Then, detach any
7596f09f0feSWENTAO YANG 	 * existing ports.
7606f09f0feSWENTAO YANG 	 */
7616f09f0feSWENTAO YANG 	if (progress & PROG_mdreg) {
7626f09f0feSWENTAO YANG 		vsw_mdeg_unregister(vswp);
7636f09f0feSWENTAO YANG 		vsw_detach_ports(vswp);
7646f09f0feSWENTAO YANG 		progress &= ~PROG_mdreg;
7656f09f0feSWENTAO YANG 	}
7666f09f0feSWENTAO YANG 
7676f09f0feSWENTAO YANG 	/*
7686f09f0feSWENTAO YANG 	 * If we have started a thread to setup the switching mode, stop it, if
7696f09f0feSWENTAO YANG 	 * it is still running. If it has finished setting up the switching
7706f09f0feSWENTAO YANG 	 * mode, then we need to clean up some additional things if we are
7716f09f0feSWENTAO YANG 	 * running in L2 mode: first free up any hybrid resources; then stop
7726f09f0feSWENTAO YANG 	 * and close the underlying physical device. Note that we would have
7736f09f0feSWENTAO YANG 	 * already released all per mac_client resources (ucast, mcast addrs,
7746f09f0feSWENTAO YANG 	 * hio-shares etc) as all the ports are detached and if the vsw device
7756f09f0feSWENTAO YANG 	 * itself was in use as an interface, it has been unplumbed (otherwise
7766f09f0feSWENTAO YANG 	 * mac_unregister() above would fail).
7776f09f0feSWENTAO YANG 	 */
7786f09f0feSWENTAO YANG 	if (progress & PROG_swmode) {
7796f09f0feSWENTAO YANG 
7806f09f0feSWENTAO YANG 		vsw_setup_switching_stop(vswp);
7816f09f0feSWENTAO YANG 
7826f09f0feSWENTAO YANG 		if (vswp->hio_capable == B_TRUE) {
7836f09f0feSWENTAO YANG 			vsw_hio_cleanup(vswp);
7846f09f0feSWENTAO YANG 			vswp->hio_capable = B_FALSE;
7856f09f0feSWENTAO YANG 		}
7866f09f0feSWENTAO YANG 
7876f09f0feSWENTAO YANG 		mutex_enter(&vswp->mac_lock);
7886f09f0feSWENTAO YANG 		vsw_mac_close(vswp);
7896f09f0feSWENTAO YANG 		mutex_exit(&vswp->mac_lock);
7906f09f0feSWENTAO YANG 
7916f09f0feSWENTAO YANG 		progress &= ~PROG_swmode;
7926f09f0feSWENTAO YANG 	}
7936f09f0feSWENTAO YANG 
7946f09f0feSWENTAO YANG 	/*
79534b64c01SWENTAO YANG 	 * We now destroy the taskq used to clean up rx mblk pools that
79634b64c01SWENTAO YANG 	 * couldn't be destroyed when the ports/channels were detached.
79734b64c01SWENTAO YANG 	 * We implicitly wait for those tasks to complete in
79834b64c01SWENTAO YANG 	 * ddi_taskq_destroy().
79934b64c01SWENTAO YANG 	 */
80034b64c01SWENTAO YANG 	if (progress & PROG_rxp_taskq) {
80134b64c01SWENTAO YANG 		ddi_taskq_destroy(vswp->rxp_taskq);
80234b64c01SWENTAO YANG 		progress &= ~PROG_rxp_taskq;
80334b64c01SWENTAO YANG 	}
80434b64c01SWENTAO YANG 
80534b64c01SWENTAO YANG 	/*
8066f09f0feSWENTAO YANG 	 * By now any pending tasks have finished and the underlying
8076f09f0feSWENTAO YANG 	 * ldc's have been destroyed, so its safe to delete the control
8086f09f0feSWENTAO YANG 	 * message taskq.
8096f09f0feSWENTAO YANG 	 */
8106f09f0feSWENTAO YANG 	if (progress & PROG_taskq) {
8116f09f0feSWENTAO YANG 		ddi_taskq_destroy(vswp->taskq_p);
8126f09f0feSWENTAO YANG 		progress &= ~PROG_taskq;
8136f09f0feSWENTAO YANG 	}
8146f09f0feSWENTAO YANG 
8156f09f0feSWENTAO YANG 	/* Destroy the multicast hash table */
8166f09f0feSWENTAO YANG 	if (progress & PROG_mfdb) {
8176f09f0feSWENTAO YANG 		mod_hash_destroy_hash(vswp->mfdb);
8186f09f0feSWENTAO YANG 		progress &= ~PROG_mfdb;
8196f09f0feSWENTAO YANG 	}
8206f09f0feSWENTAO YANG 
8216f09f0feSWENTAO YANG 	/* Destroy the vlan hash table and fdb */
8226f09f0feSWENTAO YANG 	if (progress & PROG_fdb) {
8236f09f0feSWENTAO YANG 		vsw_destroy_vlans(vswp, VSW_LOCALDEV);
8246f09f0feSWENTAO YANG 		mod_hash_destroy_hash(vswp->fdb_hashp);
8256f09f0feSWENTAO YANG 		progress &= ~PROG_fdb;
8266f09f0feSWENTAO YANG 	}
8276f09f0feSWENTAO YANG 
8286f09f0feSWENTAO YANG 	if (progress & PROG_readmd) {
8296f09f0feSWENTAO YANG 		if (VSW_PRI_ETH_DEFINED(vswp)) {
8306f09f0feSWENTAO YANG 			kmem_free(vswp->pri_types,
8316f09f0feSWENTAO YANG 			    sizeof (uint16_t) * vswp->pri_num_types);
8326f09f0feSWENTAO YANG 			(void) vio_destroy_mblks(vswp->pri_tx_vmp);
8336f09f0feSWENTAO YANG 		}
8346f09f0feSWENTAO YANG 		progress &= ~PROG_readmd;
8356f09f0feSWENTAO YANG 	}
8366f09f0feSWENTAO YANG 
8376f09f0feSWENTAO YANG 	if (progress & PROG_locks) {
8386f09f0feSWENTAO YANG 		rw_destroy(&vswp->plist.lockrw);
8396f09f0feSWENTAO YANG 		rw_destroy(&vswp->mfdbrw);
8406f09f0feSWENTAO YANG 		rw_destroy(&vswp->if_lockrw);
8416f09f0feSWENTAO YANG 		rw_destroy(&vswp->maccl_rwlock);
8426f09f0feSWENTAO YANG 		cv_destroy(&vswp->sw_thr_cv);
8436f09f0feSWENTAO YANG 		mutex_destroy(&vswp->sw_thr_lock);
8446f09f0feSWENTAO YANG 		mutex_destroy(&vswp->mca_lock);
8456f09f0feSWENTAO YANG 		mutex_destroy(&vswp->mac_lock);
8466f09f0feSWENTAO YANG 		progress &= ~PROG_locks;
8476f09f0feSWENTAO YANG 	}
8486f09f0feSWENTAO YANG 
8496f09f0feSWENTAO YANG 	vswp->attach_progress = progress;
8506f09f0feSWENTAO YANG 
8516f09f0feSWENTAO YANG 	return (0);
8526f09f0feSWENTAO YANG }
8536f09f0feSWENTAO YANG 
85434b64c01SWENTAO YANG void
85534b64c01SWENTAO YANG vsw_destroy_rxpools(void *arg)
8566f09f0feSWENTAO YANG {
85734b64c01SWENTAO YANG 	vio_mblk_pool_t	*poolp = (vio_mblk_pool_t *)arg;
85834b64c01SWENTAO YANG 	vio_mblk_pool_t	*npoolp;
8596f09f0feSWENTAO YANG 
8606f09f0feSWENTAO YANG 	while (poolp != NULL) {
86134b64c01SWENTAO YANG 		npoolp =  poolp->nextp;
86234b64c01SWENTAO YANG 		while (vio_destroy_mblks(poolp) != 0) {
86334b64c01SWENTAO YANG 			drv_usecwait(vsw_rxpool_cleanup_delay);
8646f09f0feSWENTAO YANG 		}
8656f09f0feSWENTAO YANG 		poolp = npoolp;
8666f09f0feSWENTAO YANG 	}
8676f09f0feSWENTAO YANG }
8686f09f0feSWENTAO YANG 
8696f09f0feSWENTAO YANG /*
87034683adeSsg70180  * Get the value of the "vsw-phys-dev" property in the specified
87134683adeSsg70180  * node. This property is the name of the physical device that
87234683adeSsg70180  * the virtual switch will use to talk to the outside world.
87334683adeSsg70180  *
87434683adeSsg70180  * Note it is valid for this property to be NULL (but the property
87534683adeSsg70180  * itself must exist). Callers of this routine should verify that
87634683adeSsg70180  * the value returned is what they expected (i.e. either NULL or non NULL).
87734683adeSsg70180  *
87834683adeSsg70180  * On success returns value of the property in region pointed to by
87934683adeSsg70180  * the 'name' argument, and with return value of 0. Otherwise returns 1.
8801ae08745Sheppo  */
88134683adeSsg70180 static int
88234683adeSsg70180 vsw_get_md_physname(vsw_t *vswp, md_t *mdp, mde_cookie_t node, char *name)
8831ae08745Sheppo {
88434683adeSsg70180 	int		len = 0;
885f2b610cfSwentaoy 	int		instance;
8861ae08745Sheppo 	char		*physname = NULL;
8871ae08745Sheppo 	char		*dev;
888f2b610cfSwentaoy 	const char	*dev_name;
889f2b610cfSwentaoy 	char		myname[MAXNAMELEN];
890f2b610cfSwentaoy 
891f2b610cfSwentaoy 	dev_name = ddi_driver_name(vswp->dip);
892f2b610cfSwentaoy 	instance = ddi_get_instance(vswp->dip);
893f2b610cfSwentaoy 	(void) snprintf(myname, MAXNAMELEN, "%s%d", dev_name, instance);
8941ae08745Sheppo 
89534683adeSsg70180 	if (md_get_prop_data(mdp, node, physdev_propname,
8961ae08745Sheppo 	    (uint8_t **)(&physname), &len) != 0) {
89734683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get name(s) of physical "
89834683adeSsg70180 		    "device(s) from MD", vswp->instance);
89934683adeSsg70180 		return (1);
9001ae08745Sheppo 	} else if ((strlen(physname) + 1) > LIFNAMSIZ) {
90134683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: %s is too long a device name",
90234683adeSsg70180 		    vswp->instance, physname);
90334683adeSsg70180 		return (1);
904f2b610cfSwentaoy 	} else if (strcmp(myname, physname) == 0) {
905f2b610cfSwentaoy 		/*
906f2b610cfSwentaoy 		 * Prevent the vswitch from opening itself as the
907f2b610cfSwentaoy 		 * network device.
908f2b610cfSwentaoy 		 */
909f2b610cfSwentaoy 		cmn_err(CE_WARN, "!vsw%d: %s is an invalid device name",
910f2b610cfSwentaoy 		    vswp->instance, physname);
911f2b610cfSwentaoy 		return (1);
9121ae08745Sheppo 	} else {
91334683adeSsg70180 		(void) strncpy(name, physname, strlen(physname) + 1);
9141ae08745Sheppo 		D2(vswp, "%s: using first device specified (%s)",
91534683adeSsg70180 		    __func__, physname);
9161ae08745Sheppo 	}
9171ae08745Sheppo 
9181ae08745Sheppo #ifdef DEBUG
9191ae08745Sheppo 	/*
9201ae08745Sheppo 	 * As a temporary measure to aid testing we check to see if there
9211ae08745Sheppo 	 * is a vsw.conf file present. If there is we use the value of the
9221ae08745Sheppo 	 * vsw_physname property in the file as the name of the physical
9231ae08745Sheppo 	 * device, overriding the value from the MD.
9241ae08745Sheppo 	 *
9251ae08745Sheppo 	 * There may be multiple devices listed, but for the moment
9261ae08745Sheppo 	 * we just use the first one.
9271ae08745Sheppo 	 */
9281ae08745Sheppo 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vswp->dip, 0,
9291ae08745Sheppo 	    "vsw_physname", &dev) == DDI_PROP_SUCCESS) {
9301ae08745Sheppo 		if ((strlen(dev) + 1) > LIFNAMSIZ) {
93134683adeSsg70180 			cmn_err(CE_WARN, "vsw%d: %s is too long a device name",
93234683adeSsg70180 			    vswp->instance, dev);
93334683adeSsg70180 			ddi_prop_free(dev);
93434683adeSsg70180 			return (1);
9351ae08745Sheppo 		} else {
93634683adeSsg70180 			cmn_err(CE_NOTE, "vsw%d: Using device name (%s) from "
93734683adeSsg70180 			    "config file", vswp->instance, dev);
9381ae08745Sheppo 
93934683adeSsg70180 			(void) strncpy(name, dev, strlen(dev) + 1);
9401ae08745Sheppo 		}
9411ae08745Sheppo 
9421ae08745Sheppo 		ddi_prop_free(dev);
9431ae08745Sheppo 	}
9441ae08745Sheppo #endif
9451ae08745Sheppo 
94634683adeSsg70180 	return (0);
94734683adeSsg70180 }
948e1ebb9ecSlm66018 
949e1ebb9ecSlm66018 /*
95034683adeSsg70180  * Read the 'vsw-switch-mode' property from the specified MD node.
95134683adeSsg70180  *
952da14cebeSEric Cheng  * Returns 0 on success, otherwise returns 1.
953e1ebb9ecSlm66018  */
95434683adeSsg70180 static int
955da14cebeSEric Cheng vsw_get_md_smodes(vsw_t *vswp, md_t *mdp, mde_cookie_t node, uint8_t *mode)
95634683adeSsg70180 {
95734683adeSsg70180 	int		len = 0;
95834683adeSsg70180 	char		*smode = NULL;
95934683adeSsg70180 	char		*curr_mode = NULL;
96034683adeSsg70180 
96134683adeSsg70180 	D1(vswp, "%s: enter", __func__);
9621ae08745Sheppo 
9631ae08745Sheppo 	/*
9641ae08745Sheppo 	 * Get the switch-mode property. The modes are listed in
9651ae08745Sheppo 	 * decreasing order of preference, i.e. prefered mode is
9661ae08745Sheppo 	 * first item in list.
9671ae08745Sheppo 	 */
9681ae08745Sheppo 	len = 0;
96934683adeSsg70180 	if (md_get_prop_data(mdp, node, smode_propname,
9701ae08745Sheppo 	    (uint8_t **)(&smode), &len) != 0) {
9711ae08745Sheppo 		/*
972e1ebb9ecSlm66018 		 * Unable to get switch-mode property from MD, nothing
973e1ebb9ecSlm66018 		 * more we can do.
9741ae08745Sheppo 		 */
97534683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get switch mode property"
97634683adeSsg70180 		    " from the MD", vswp->instance);
97734683adeSsg70180 		return (1);
978e1ebb9ecSlm66018 	}
979e1ebb9ecSlm66018 
9801ae08745Sheppo 	curr_mode = smode;
9811ae08745Sheppo 	/*
9821ae08745Sheppo 	 * Modes of operation:
9831ae08745Sheppo 	 * 'switched'	 - layer 2 switching, underlying HW in
984e1ebb9ecSlm66018 	 *			programmed mode.
9851ae08745Sheppo 	 * 'promiscuous' - layer 2 switching, underlying HW in
9861ae08745Sheppo 	 *			promiscuous mode.
9871ae08745Sheppo 	 * 'routed'	 - layer 3 (i.e. IP) routing, underlying HW
9881ae08745Sheppo 	 *			in non-promiscuous mode.
9891ae08745Sheppo 	 */
990da14cebeSEric Cheng 	while (curr_mode < (smode + len)) {
9911ae08745Sheppo 		D2(vswp, "%s: curr_mode = [%s]", __func__, curr_mode);
992e1ebb9ecSlm66018 		if (strcmp(curr_mode, "switched") == 0) {
993da14cebeSEric Cheng 			*mode = VSW_LAYER2;
994e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "promiscuous") == 0) {
995da14cebeSEric Cheng 			*mode = VSW_LAYER2 | VSW_LAYER2_PROMISC;
996e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "routed") == 0) {
997da14cebeSEric Cheng 			*mode = VSW_LAYER3;
998e1ebb9ecSlm66018 		} else {
999da14cebeSEric Cheng 			cmn_err(CE_WARN, "!vsw%d: Unknown switch mode %s, "
1000da14cebeSEric Cheng 			    "setting to default switched mode",
1001da14cebeSEric Cheng 			    vswp->instance, curr_mode);
1002da14cebeSEric Cheng 			*mode = VSW_LAYER2;
10031ae08745Sheppo 		}
10041ae08745Sheppo 		curr_mode += strlen(curr_mode) + 1;
10051ae08745Sheppo 	}
10061ae08745Sheppo 
1007da14cebeSEric Cheng 	D2(vswp, "%s: %d mode", __func__, *mode);
10081ae08745Sheppo 
10091ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
101034683adeSsg70180 
101134683adeSsg70180 	return (0);
10121ae08745Sheppo }
10131ae08745Sheppo 
1014e1ebb9ecSlm66018 /*
10151ae08745Sheppo  * Register with the MAC layer as a network device, so we
10161ae08745Sheppo  * can be plumbed if necessary.
10171ae08745Sheppo  */
10181ae08745Sheppo static int
10191ae08745Sheppo vsw_mac_register(vsw_t *vswp)
10201ae08745Sheppo {
1021ba2e4443Sseb 	mac_register_t	*macp;
1022ba2e4443Sseb 	int		rv;
10231ae08745Sheppo 
10241ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10251ae08745Sheppo 
1026ba2e4443Sseb 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
1027ba2e4443Sseb 		return (EINVAL);
1028ba2e4443Sseb 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
10291ae08745Sheppo 	macp->m_driver = vswp;
1030ba2e4443Sseb 	macp->m_dip = vswp->dip;
1031ba2e4443Sseb 	macp->m_src_addr = (uint8_t *)&vswp->if_addr;
1032ba2e4443Sseb 	macp->m_callbacks = &vsw_m_callbacks;
1033ba2e4443Sseb 	macp->m_min_sdu = 0;
10347b1f684aSSriharsha Basavapatna 	macp->m_max_sdu = vswp->mtu;
1035c1c61f44Ssb155480 	macp->m_margin = VLAN_TAGSZ;
1036ba2e4443Sseb 	rv = mac_register(macp, &vswp->if_mh);
1037ba2e4443Sseb 	mac_free(macp);
103819b65a69Ssb155480 	if (rv != 0) {
103919b65a69Ssb155480 		/*
104019b65a69Ssb155480 		 * Treat this as a non-fatal error as we may be
104119b65a69Ssb155480 		 * able to operate in some other mode.
104219b65a69Ssb155480 		 */
104319b65a69Ssb155480 		cmn_err(CE_NOTE, "!vsw%d: Unable to register as "
104419b65a69Ssb155480 		    "a provider with MAC layer", vswp->instance);
104519b65a69Ssb155480 		return (rv);
104619b65a69Ssb155480 	}
104719b65a69Ssb155480 
1048ba2e4443Sseb 	vswp->if_state |= VSW_IF_REG;
10491ae08745Sheppo 
10501ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
10511ae08745Sheppo 
10521ae08745Sheppo 	return (rv);
10531ae08745Sheppo }
10541ae08745Sheppo 
10551ae08745Sheppo static int
10561ae08745Sheppo vsw_mac_unregister(vsw_t *vswp)
10571ae08745Sheppo {
10581ae08745Sheppo 	int		rv = 0;
10591ae08745Sheppo 
10601ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10611ae08745Sheppo 
10621ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
10631ae08745Sheppo 
1064ba2e4443Sseb 	if (vswp->if_state & VSW_IF_REG) {
1065ba2e4443Sseb 		rv = mac_unregister(vswp->if_mh);
10661ae08745Sheppo 		if (rv != 0) {
10671ae08745Sheppo 			DWARN(vswp, "%s: unable to unregister from MAC "
10681ae08745Sheppo 			    "framework", __func__);
10691ae08745Sheppo 
10701ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
10711ae08745Sheppo 			D1(vswp, "%s: fail exit", __func__);
10721ae08745Sheppo 			return (rv);
10731ae08745Sheppo 		}
10741ae08745Sheppo 
1075ba2e4443Sseb 		/* mark i/f as down and unregistered */
1076ba2e4443Sseb 		vswp->if_state &= ~(VSW_IF_UP | VSW_IF_REG);
10771ae08745Sheppo 	}
10781ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
10791ae08745Sheppo 
10801ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
10811ae08745Sheppo 
10821ae08745Sheppo 	return (rv);
10831ae08745Sheppo }
10841ae08745Sheppo 
1085ba2e4443Sseb static int
1086ba2e4443Sseb vsw_m_stat(void *arg, uint_t stat, uint64_t *val)
10871ae08745Sheppo {
10881ae08745Sheppo 	vsw_t			*vswp = (vsw_t *)arg;
10891ae08745Sheppo 
10901ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
10911ae08745Sheppo 
1092da14cebeSEric Cheng 	mutex_enter(&vswp->mac_lock);
109334683adeSsg70180 	if (vswp->mh == NULL) {
1094da14cebeSEric Cheng 		mutex_exit(&vswp->mac_lock);
1095ba2e4443Sseb 		return (EINVAL);
109634683adeSsg70180 	}
10971ae08745Sheppo 
10981ae08745Sheppo 	/* return stats from underlying device */
1099ba2e4443Sseb 	*val = mac_stat_get(vswp->mh, stat);
110034683adeSsg70180 
1101da14cebeSEric Cheng 	mutex_exit(&vswp->mac_lock);
110234683adeSsg70180 
1103ba2e4443Sseb 	return (0);
11041ae08745Sheppo }
11051ae08745Sheppo 
11061ae08745Sheppo static void
11071ae08745Sheppo vsw_m_stop(void *arg)
11081ae08745Sheppo {
11091ae08745Sheppo 	vsw_t	*vswp = (vsw_t *)arg;
11101ae08745Sheppo 
11111ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11121ae08745Sheppo 
11131ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
11141ae08745Sheppo 	vswp->if_state &= ~VSW_IF_UP;
11151ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
11161ae08745Sheppo 
1117da14cebeSEric Cheng 	/* Cleanup and close the mac client */
1118da14cebeSEric Cheng 	vsw_mac_client_cleanup(vswp, NULL, VSW_LOCALDEV);
11195f94e909Ssg70180 
11201ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
11211ae08745Sheppo }
11221ae08745Sheppo 
11231ae08745Sheppo static int
11241ae08745Sheppo vsw_m_start(void *arg)
11251ae08745Sheppo {
1126da14cebeSEric Cheng 	int		rv;
11271ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
11281ae08745Sheppo 
11291ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11301ae08745Sheppo 
11311ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
11321ae08745Sheppo 
113319b65a69Ssb155480 	vswp->if_state |= VSW_IF_UP;
113419b65a69Ssb155480 
113519b65a69Ssb155480 	if (vswp->switching_setup_done == B_FALSE) {
113619b65a69Ssb155480 		/*
113719b65a69Ssb155480 		 * If the switching mode has not been setup yet, just
113819b65a69Ssb155480 		 * return. The unicast address will be programmed
113919b65a69Ssb155480 		 * after the physical device is successfully setup by the
114019b65a69Ssb155480 		 * timeout handler.
114119b65a69Ssb155480 		 */
114219b65a69Ssb155480 		RW_EXIT(&vswp->if_lockrw);
114319b65a69Ssb155480 		return (0);
114419b65a69Ssb155480 	}
114519b65a69Ssb155480 
114619b65a69Ssb155480 	/* if in layer2 mode, program unicast address. */
114719b65a69Ssb155480 	if (vswp->mh != NULL) {
1148da14cebeSEric Cheng 		/* Init a mac client and program addresses */
1149da14cebeSEric Cheng 		rv = vsw_mac_client_init(vswp, NULL, VSW_LOCALDEV);
1150da14cebeSEric Cheng 		if (rv != 0) {
1151da14cebeSEric Cheng 			cmn_err(CE_NOTE,
1152da14cebeSEric Cheng 			    "!vsw%d: failed to program interface "
1153da14cebeSEric Cheng 			    "unicast address\n", vswp->instance);
1154da14cebeSEric Cheng 		}
115519b65a69Ssb155480 	}
115619b65a69Ssb155480 
115719b65a69Ssb155480 	RW_EXIT(&vswp->if_lockrw);
11585f94e909Ssg70180 
11591ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
11601ae08745Sheppo 	return (0);
11611ae08745Sheppo }
11621ae08745Sheppo 
11631ae08745Sheppo /*
11641ae08745Sheppo  * Change the local interface address.
11655f94e909Ssg70180  *
11665f94e909Ssg70180  * Note: we don't support this entry point. The local
11675f94e909Ssg70180  * mac address of the switch can only be changed via its
11685f94e909Ssg70180  * MD node properties.
11691ae08745Sheppo  */
11701ae08745Sheppo static int
11711ae08745Sheppo vsw_m_unicst(void *arg, const uint8_t *macaddr)
11721ae08745Sheppo {
11735f94e909Ssg70180 	_NOTE(ARGUNUSED(arg, macaddr))
11741ae08745Sheppo 
11755f94e909Ssg70180 	return (DDI_FAILURE);
11761ae08745Sheppo }
11771ae08745Sheppo 
11781ae08745Sheppo static int
11791ae08745Sheppo vsw_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
11801ae08745Sheppo {
11811ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
11821ae08745Sheppo 	mcst_addr_t	*mcst_p = NULL;
11831ae08745Sheppo 	uint64_t	addr = 0x0;
1184e1ebb9ecSlm66018 	int		i, ret = 0;
11851ae08745Sheppo 
11861ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11871ae08745Sheppo 
11881ae08745Sheppo 	/*
11891ae08745Sheppo 	 * Convert address into form that can be used
11901ae08745Sheppo 	 * as hash table key.
11911ae08745Sheppo 	 */
11921ae08745Sheppo 	for (i = 0; i < ETHERADDRL; i++) {
11931ae08745Sheppo 		addr = (addr << 8) | mca[i];
11941ae08745Sheppo 	}
11951ae08745Sheppo 
11961ae08745Sheppo 	D2(vswp, "%s: addr = 0x%llx", __func__, addr);
11971ae08745Sheppo 
11981ae08745Sheppo 	if (add) {
11991ae08745Sheppo 		D2(vswp, "%s: adding multicast", __func__);
12001ae08745Sheppo 		if (vsw_add_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
12011ae08745Sheppo 			/*
12021ae08745Sheppo 			 * Update the list of multicast addresses
12031ae08745Sheppo 			 * contained within the vsw_t structure to
12041ae08745Sheppo 			 * include this new one.
12051ae08745Sheppo 			 */
12061ae08745Sheppo 			mcst_p = kmem_zalloc(sizeof (mcst_addr_t), KM_NOSLEEP);
12071ae08745Sheppo 			if (mcst_p == NULL) {
12081ae08745Sheppo 				DERR(vswp, "%s unable to alloc mem", __func__);
120919b65a69Ssb155480 				(void) vsw_del_mcst(vswp,
121019b65a69Ssb155480 				    VSW_LOCALDEV, addr, NULL);
12111ae08745Sheppo 				return (1);
12121ae08745Sheppo 			}
12131ae08745Sheppo 			mcst_p->addr = addr;
121419b65a69Ssb155480 			ether_copy(mca, &mcst_p->mca);
12151ae08745Sheppo 
12161ae08745Sheppo 			/*
12171ae08745Sheppo 			 * Call into the underlying driver to program the
12181ae08745Sheppo 			 * address into HW.
12191ae08745Sheppo 			 */
1220da14cebeSEric Cheng 			ret = vsw_mac_multicast_add(vswp, NULL, mcst_p,
1221da14cebeSEric Cheng 			    VSW_LOCALDEV);
1222e1ebb9ecSlm66018 			if (ret != 0) {
122319b65a69Ssb155480 				(void) vsw_del_mcst(vswp,
122419b65a69Ssb155480 				    VSW_LOCALDEV, addr, NULL);
122519b65a69Ssb155480 				kmem_free(mcst_p, sizeof (*mcst_p));
122619b65a69Ssb155480 				return (ret);
1227e1ebb9ecSlm66018 			}
122819b65a69Ssb155480 
122919b65a69Ssb155480 			mutex_enter(&vswp->mca_lock);
123019b65a69Ssb155480 			mcst_p->nextp = vswp->mcap;
123119b65a69Ssb155480 			vswp->mcap = mcst_p;
123219b65a69Ssb155480 			mutex_exit(&vswp->mca_lock);
12331ae08745Sheppo 		} else {
1234da14cebeSEric Cheng 			cmn_err(CE_WARN, "!vsw%d: unable to add multicast "
123534683adeSsg70180 			    "address", vswp->instance);
1236e1ebb9ecSlm66018 		}
1237e1ebb9ecSlm66018 		return (ret);
1238e1ebb9ecSlm66018 	}
1239e1ebb9ecSlm66018 
12401ae08745Sheppo 	D2(vswp, "%s: removing multicast", __func__);
12411ae08745Sheppo 	/*
12421ae08745Sheppo 	 * Remove the address from the hash table..
12431ae08745Sheppo 	 */
12441ae08745Sheppo 	if (vsw_del_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
12451ae08745Sheppo 
12461ae08745Sheppo 		/*
12471ae08745Sheppo 		 * ..and then from the list maintained in the
12481ae08745Sheppo 		 * vsw_t structure.
12491ae08745Sheppo 		 */
125019b65a69Ssb155480 		mcst_p = vsw_del_addr(VSW_LOCALDEV, vswp, addr);
125119b65a69Ssb155480 		ASSERT(mcst_p != NULL);
12521ae08745Sheppo 
1253da14cebeSEric Cheng 		vsw_mac_multicast_remove(vswp, NULL, mcst_p, VSW_LOCALDEV);
125419b65a69Ssb155480 		kmem_free(mcst_p, sizeof (*mcst_p));
12551ae08745Sheppo 	}
12561ae08745Sheppo 
12571ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
12581ae08745Sheppo 
12591ae08745Sheppo 	return (0);
12601ae08745Sheppo }
12611ae08745Sheppo 
12621ae08745Sheppo static int
12631ae08745Sheppo vsw_m_promisc(void *arg, boolean_t on)
12641ae08745Sheppo {
12651ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
12661ae08745Sheppo 
12671ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
12681ae08745Sheppo 
12691ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
12701ae08745Sheppo 	if (on)
12711ae08745Sheppo 		vswp->if_state |= VSW_IF_PROMISC;
12721ae08745Sheppo 	else
12731ae08745Sheppo 		vswp->if_state &= ~VSW_IF_PROMISC;
12741ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
12751ae08745Sheppo 
12761ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
12771ae08745Sheppo 
12781ae08745Sheppo 	return (0);
12791ae08745Sheppo }
12801ae08745Sheppo 
12811ae08745Sheppo static mblk_t *
12821ae08745Sheppo vsw_m_tx(void *arg, mblk_t *mp)
12831ae08745Sheppo {
12841ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
12851ae08745Sheppo 
12861ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
12871ae08745Sheppo 
1288c1c61f44Ssb155480 	mp = vsw_vlan_frame_pretag(vswp, VSW_LOCALDEV, mp);
1289c1c61f44Ssb155480 
1290c1c61f44Ssb155480 	if (mp == NULL) {
1291c1c61f44Ssb155480 		return (NULL);
1292c1c61f44Ssb155480 	}
1293c1c61f44Ssb155480 
129434683adeSsg70180 	vswp->vsw_switch_frame(vswp, mp, VSW_LOCALDEV, NULL, NULL);
12951ae08745Sheppo 
12961ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
12971ae08745Sheppo 
12981ae08745Sheppo 	return (NULL);
12991ae08745Sheppo }
13001ae08745Sheppo 
13011ae08745Sheppo /*
13021ae08745Sheppo  * Register for machine description (MD) updates.
130334683adeSsg70180  *
130434683adeSsg70180  * Returns 0 on success, 1 on failure.
13051ae08745Sheppo  */
130634683adeSsg70180 static int
13071ae08745Sheppo vsw_mdeg_register(vsw_t *vswp)
13081ae08745Sheppo {
13091ae08745Sheppo 	mdeg_prop_spec_t	*pspecp;
13101ae08745Sheppo 	mdeg_node_spec_t	*inst_specp;
131134683adeSsg70180 	mdeg_handle_t		mdeg_hdl, mdeg_port_hdl;
13121ae08745Sheppo 	size_t			templatesz;
131319b65a69Ssb155480 	int			rv;
13141ae08745Sheppo 
13151ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
13161ae08745Sheppo 
131734683adeSsg70180 	/*
13181ae08745Sheppo 	 * Allocate and initialize a per-instance copy
13191ae08745Sheppo 	 * of the global property spec array that will
13201ae08745Sheppo 	 * uniquely identify this vsw instance.
13211ae08745Sheppo 	 */
13221ae08745Sheppo 	templatesz = sizeof (vsw_prop_template);
13231ae08745Sheppo 	pspecp = kmem_zalloc(templatesz, KM_SLEEP);
13241ae08745Sheppo 
13251ae08745Sheppo 	bcopy(vsw_prop_template, pspecp, templatesz);
13261ae08745Sheppo 
132719b65a69Ssb155480 	VSW_SET_MDEG_PROP_INST(pspecp, vswp->regprop);
13281ae08745Sheppo 
13291ae08745Sheppo 	/* initialize the complete prop spec structure */
13301ae08745Sheppo 	inst_specp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
13311ae08745Sheppo 	inst_specp->namep = "virtual-device";
13321ae08745Sheppo 	inst_specp->specp = pspecp;
13331ae08745Sheppo 
133419b65a69Ssb155480 	D2(vswp, "%s: instance %d registering with mdeg", __func__,
133519b65a69Ssb155480 	    vswp->regprop);
133634683adeSsg70180 	/*
133734683adeSsg70180 	 * Register an interest in 'virtual-device' nodes with a
133834683adeSsg70180 	 * 'name' property of 'virtual-network-switch'
133934683adeSsg70180 	 */
134034683adeSsg70180 	rv = mdeg_register(inst_specp, &vdev_match, vsw_mdeg_cb,
13411ae08745Sheppo 	    (void *)vswp, &mdeg_hdl);
134234683adeSsg70180 	if (rv != MDEG_SUCCESS) {
134334683adeSsg70180 		DERR(vswp, "%s: mdeg_register failed (%d) for vsw node",
134434683adeSsg70180 		    __func__, rv);
134534683adeSsg70180 		goto mdeg_reg_fail;
134634683adeSsg70180 	}
13471ae08745Sheppo 
134834683adeSsg70180 	/*
134934683adeSsg70180 	 * Register an interest in 'vsw-port' nodes.
135034683adeSsg70180 	 */
135134683adeSsg70180 	rv = mdeg_register(inst_specp, &vport_match, vsw_port_mdeg_cb,
135234683adeSsg70180 	    (void *)vswp, &mdeg_port_hdl);
13531ae08745Sheppo 	if (rv != MDEG_SUCCESS) {
13541ae08745Sheppo 		DERR(vswp, "%s: mdeg_register failed (%d)\n", __func__, rv);
135534683adeSsg70180 		(void) mdeg_unregister(mdeg_hdl);
135634683adeSsg70180 		goto mdeg_reg_fail;
13571ae08745Sheppo 	}
13581ae08745Sheppo 
13591ae08745Sheppo 	/* save off data that will be needed later */
13601ae08745Sheppo 	vswp->inst_spec = inst_specp;
13611ae08745Sheppo 	vswp->mdeg_hdl = mdeg_hdl;
136234683adeSsg70180 	vswp->mdeg_port_hdl = mdeg_port_hdl;
13631ae08745Sheppo 
13641ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
136534683adeSsg70180 	return (0);
136634683adeSsg70180 
136734683adeSsg70180 mdeg_reg_fail:
136834683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: Unable to register MDEG callbacks",
136934683adeSsg70180 	    vswp->instance);
137034683adeSsg70180 	kmem_free(pspecp, templatesz);
137134683adeSsg70180 	kmem_free(inst_specp, sizeof (mdeg_node_spec_t));
137234683adeSsg70180 
137334683adeSsg70180 	vswp->mdeg_hdl = NULL;
137434683adeSsg70180 	vswp->mdeg_port_hdl = NULL;
137534683adeSsg70180 
137634683adeSsg70180 	return (1);
13771ae08745Sheppo }
13781ae08745Sheppo 
13791ae08745Sheppo static void
13801ae08745Sheppo vsw_mdeg_unregister(vsw_t *vswp)
13811ae08745Sheppo {
13821ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: enter");
13831ae08745Sheppo 
138434683adeSsg70180 	if (vswp->mdeg_hdl != NULL)
13851ae08745Sheppo 		(void) mdeg_unregister(vswp->mdeg_hdl);
13861ae08745Sheppo 
138734683adeSsg70180 	if (vswp->mdeg_port_hdl != NULL)
138834683adeSsg70180 		(void) mdeg_unregister(vswp->mdeg_port_hdl);
138934683adeSsg70180 
139034683adeSsg70180 	if (vswp->inst_spec != NULL) {
13911ae08745Sheppo 		if (vswp->inst_spec->specp != NULL) {
13921ae08745Sheppo 			(void) kmem_free(vswp->inst_spec->specp,
13931ae08745Sheppo 			    sizeof (vsw_prop_template));
13941ae08745Sheppo 			vswp->inst_spec->specp = NULL;
13951ae08745Sheppo 		}
13961ae08745Sheppo 
1397205eeb1aSlm66018 		(void) kmem_free(vswp->inst_spec, sizeof (mdeg_node_spec_t));
13981ae08745Sheppo 		vswp->inst_spec = NULL;
13991ae08745Sheppo 	}
14001ae08745Sheppo 
14011ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: exit");
14021ae08745Sheppo }
14031ae08745Sheppo 
140434683adeSsg70180 /*
140534683adeSsg70180  * Mdeg callback invoked for the vsw node itself.
140634683adeSsg70180  */
14071ae08745Sheppo static int
14081ae08745Sheppo vsw_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
14091ae08745Sheppo {
14101ae08745Sheppo 	vsw_t		*vswp;
14111ae08745Sheppo 	md_t		*mdp;
14121ae08745Sheppo 	mde_cookie_t	node;
14131ae08745Sheppo 	uint64_t	inst;
141434683adeSsg70180 	char		*node_name = NULL;
14151ae08745Sheppo 
14161ae08745Sheppo 	if (resp == NULL)
14171ae08745Sheppo 		return (MDEG_FAILURE);
14181ae08745Sheppo 
14191ae08745Sheppo 	vswp = (vsw_t *)cb_argp;
14201ae08745Sheppo 
142134683adeSsg70180 	D1(vswp, "%s: added %d : removed %d : curr matched %d"
142234683adeSsg70180 	    " : prev matched %d", __func__, resp->added.nelem,
142334683adeSsg70180 	    resp->removed.nelem, resp->match_curr.nelem,
142434683adeSsg70180 	    resp->match_prev.nelem);
142534683adeSsg70180 
142634683adeSsg70180 	/*
142719b65a69Ssb155480 	 * We get an initial callback for this node as 'added'
142819b65a69Ssb155480 	 * after registering with mdeg. Note that we would have
142919b65a69Ssb155480 	 * already gathered information about this vsw node by
143019b65a69Ssb155480 	 * walking MD earlier during attach (in vsw_read_mdprops()).
143119b65a69Ssb155480 	 * So, there is a window where the properties of this
143219b65a69Ssb155480 	 * node might have changed when we get this initial 'added'
143319b65a69Ssb155480 	 * callback. We handle this as if an update occured
143419b65a69Ssb155480 	 * and invoke the same function which handles updates to
143519b65a69Ssb155480 	 * the properties of this vsw-node if any.
143619b65a69Ssb155480 	 *
143734683adeSsg70180 	 * A non-zero 'match' value indicates that the MD has been
143819b65a69Ssb155480 	 * updated and that a virtual-network-switch node is
143919b65a69Ssb155480 	 * present which may or may not have been updated. It is
144019b65a69Ssb155480 	 * up to the clients to examine their own nodes and
144119b65a69Ssb155480 	 * determine if they have changed.
144234683adeSsg70180 	 */
144319b65a69Ssb155480 	if (resp->added.nelem != 0) {
144434683adeSsg70180 
144519b65a69Ssb155480 		if (resp->added.nelem != 1) {
144619b65a69Ssb155480 			cmn_err(CE_NOTE, "!vsw%d: number of nodes added "
144719b65a69Ssb155480 			    "invalid: %d\n", vswp->instance, resp->added.nelem);
144819b65a69Ssb155480 			return (MDEG_FAILURE);
144919b65a69Ssb155480 		}
145019b65a69Ssb155480 
145119b65a69Ssb155480 		mdp = resp->added.mdp;
145219b65a69Ssb155480 		node = resp->added.mdep[0];
145319b65a69Ssb155480 
145419b65a69Ssb155480 	} else if (resp->match_curr.nelem != 0) {
145519b65a69Ssb155480 
145619b65a69Ssb155480 		if (resp->match_curr.nelem != 1) {
145719b65a69Ssb155480 			cmn_err(CE_NOTE, "!vsw%d: number of nodes updated "
145819b65a69Ssb155480 			    "invalid: %d\n", vswp->instance,
145919b65a69Ssb155480 			    resp->match_curr.nelem);
146019b65a69Ssb155480 			return (MDEG_FAILURE);
146119b65a69Ssb155480 		}
146219b65a69Ssb155480 
146319b65a69Ssb155480 		mdp = resp->match_curr.mdp;
146419b65a69Ssb155480 		node = resp->match_curr.mdep[0];
146519b65a69Ssb155480 
146619b65a69Ssb155480 	} else {
146719b65a69Ssb155480 		return (MDEG_FAILURE);
146819b65a69Ssb155480 	}
146919b65a69Ssb155480 
147019b65a69Ssb155480 	/* Validate name and instance */
147134683adeSsg70180 	if (md_get_prop_str(mdp, node, "name", &node_name) != 0) {
147219b65a69Ssb155480 		DERR(vswp, "%s: unable to get node name\n",  __func__);
147319b65a69Ssb155480 		return (MDEG_FAILURE);
147419b65a69Ssb155480 	}
147519b65a69Ssb155480 
147619b65a69Ssb155480 	/* is this a virtual-network-switch? */
147719b65a69Ssb155480 	if (strcmp(node_name, vsw_propname) != 0) {
147819b65a69Ssb155480 		DERR(vswp, "%s: Invalid node name: %s\n",
147919b65a69Ssb155480 		    __func__, node_name);
148019b65a69Ssb155480 		return (MDEG_FAILURE);
148134683adeSsg70180 	}
148234683adeSsg70180 
148334683adeSsg70180 	if (md_get_prop_val(mdp, node, "cfg-handle", &inst)) {
148419b65a69Ssb155480 		DERR(vswp, "%s: prop(cfg-handle) not found\n",
148519b65a69Ssb155480 		    __func__);
148619b65a69Ssb155480 		return (MDEG_FAILURE);
148734683adeSsg70180 	}
148834683adeSsg70180 
148919b65a69Ssb155480 	/* is this the right instance of vsw? */
149019b65a69Ssb155480 	if (inst != vswp->regprop) {
149119b65a69Ssb155480 		DERR(vswp, "%s: Invalid cfg-handle: %lx\n",
149219b65a69Ssb155480 		    __func__, inst);
149319b65a69Ssb155480 		return (MDEG_FAILURE);
149419b65a69Ssb155480 	}
149534683adeSsg70180 
149634683adeSsg70180 	vsw_update_md_prop(vswp, mdp, node);
149734683adeSsg70180 
149834683adeSsg70180 	return (MDEG_SUCCESS);
149934683adeSsg70180 }
150034683adeSsg70180 
150134683adeSsg70180 /*
150234683adeSsg70180  * Mdeg callback invoked for changes to the vsw-port nodes
150334683adeSsg70180  * under the vsw node.
150434683adeSsg70180  */
150534683adeSsg70180 static int
150634683adeSsg70180 vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
150734683adeSsg70180 {
150834683adeSsg70180 	vsw_t		*vswp;
150934683adeSsg70180 	int		idx;
151034683adeSsg70180 	md_t		*mdp;
151134683adeSsg70180 	mde_cookie_t	node;
151234683adeSsg70180 	uint64_t	inst;
15131ef0bbb5Snarayan 	int		rv;
151434683adeSsg70180 
151534683adeSsg70180 	if ((resp == NULL) || (cb_argp == NULL))
151634683adeSsg70180 		return (MDEG_FAILURE);
151734683adeSsg70180 
151834683adeSsg70180 	vswp = (vsw_t *)cb_argp;
151934683adeSsg70180 
152034683adeSsg70180 	D2(vswp, "%s: added %d : removed %d : curr matched %d"
152134683adeSsg70180 	    " : prev matched %d", __func__, resp->added.nelem,
152234683adeSsg70180 	    resp->removed.nelem, resp->match_curr.nelem,
15231ae08745Sheppo 	    resp->match_prev.nelem);
15241ae08745Sheppo 
15251ae08745Sheppo 	/* process added ports */
15261ae08745Sheppo 	for (idx = 0; idx < resp->added.nelem; idx++) {
15271ae08745Sheppo 		mdp = resp->added.mdp;
15281ae08745Sheppo 		node = resp->added.mdep[idx];
15291ae08745Sheppo 
15301ae08745Sheppo 		D2(vswp, "%s: adding node(%d) 0x%lx", __func__, idx, node);
15311ae08745Sheppo 
15321ef0bbb5Snarayan 		if ((rv = vsw_port_add(vswp, mdp, &node)) != 0) {
153334683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to add new port "
15341ef0bbb5Snarayan 			    "(0x%lx), err=%d", vswp->instance, node, rv);
15351ae08745Sheppo 		}
15361ae08745Sheppo 	}
15371ae08745Sheppo 
15381ae08745Sheppo 	/* process removed ports */
15391ae08745Sheppo 	for (idx = 0; idx < resp->removed.nelem; idx++) {
15401ae08745Sheppo 		mdp = resp->removed.mdp;
15411ae08745Sheppo 		node = resp->removed.mdep[idx];
15421ae08745Sheppo 
15431ae08745Sheppo 		if (md_get_prop_val(mdp, node, id_propname, &inst)) {
154434683adeSsg70180 			DERR(vswp, "%s: prop(%s) not found in port(%d)",
15451ae08745Sheppo 			    __func__, id_propname, idx);
15461ae08745Sheppo 			continue;
15471ae08745Sheppo 		}
15481ae08745Sheppo 
15491ae08745Sheppo 		D2(vswp, "%s: removing node(%d) 0x%lx", __func__, idx, node);
15501ae08745Sheppo 
15511ae08745Sheppo 		if (vsw_port_detach(vswp, inst) != 0) {
155234683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to remove port %ld",
155334683adeSsg70180 			    vswp->instance, inst);
15541ae08745Sheppo 		}
15551ae08745Sheppo 	}
15561ae08745Sheppo 
1557c1c61f44Ssb155480 	for (idx = 0; idx < resp->match_curr.nelem; idx++) {
1558c1c61f44Ssb155480 		(void) vsw_port_update(vswp, resp->match_curr.mdp,
1559c1c61f44Ssb155480 		    resp->match_curr.mdep[idx],
1560c1c61f44Ssb155480 		    resp->match_prev.mdp,
1561c1c61f44Ssb155480 		    resp->match_prev.mdep[idx]);
1562c1c61f44Ssb155480 	}
15631ae08745Sheppo 
15641ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
15651ae08745Sheppo 
15661ae08745Sheppo 	return (MDEG_SUCCESS);
15671ae08745Sheppo }
15681ae08745Sheppo 
15691ae08745Sheppo /*
157019b65a69Ssb155480  * Scan the machine description for this instance of vsw
157119b65a69Ssb155480  * and read its properties. Called only from vsw_attach().
157219b65a69Ssb155480  * Returns: 0 on success, 1 on failure.
157319b65a69Ssb155480  */
157419b65a69Ssb155480 static int
157519b65a69Ssb155480 vsw_read_mdprops(vsw_t *vswp)
157619b65a69Ssb155480 {
157719b65a69Ssb155480 	md_t		*mdp = NULL;
157819b65a69Ssb155480 	mde_cookie_t	rootnode;
157919b65a69Ssb155480 	mde_cookie_t	*listp = NULL;
158019b65a69Ssb155480 	uint64_t	inst;
158119b65a69Ssb155480 	uint64_t	cfgh;
158219b65a69Ssb155480 	char		*name;
158319b65a69Ssb155480 	int		rv = 1;
158419b65a69Ssb155480 	int		num_nodes = 0;
158519b65a69Ssb155480 	int		num_devs = 0;
158619b65a69Ssb155480 	int		listsz = 0;
158719b65a69Ssb155480 	int		i;
158819b65a69Ssb155480 
158919b65a69Ssb155480 	/*
159019b65a69Ssb155480 	 * In each 'virtual-device' node in the MD there is a
159119b65a69Ssb155480 	 * 'cfg-handle' property which is the MD's concept of
159219b65a69Ssb155480 	 * an instance number (this may be completely different from
159319b65a69Ssb155480 	 * the device drivers instance #). OBP reads that value and
159419b65a69Ssb155480 	 * stores it in the 'reg' property of the appropriate node in
159519b65a69Ssb155480 	 * the device tree. We first read this reg property and use this
159619b65a69Ssb155480 	 * to compare against the 'cfg-handle' property of vsw nodes
159719b65a69Ssb155480 	 * in MD to get to this specific vsw instance and then read
159819b65a69Ssb155480 	 * other properties that we are interested in.
159919b65a69Ssb155480 	 * We also cache the value of 'reg' property and use it later
160019b65a69Ssb155480 	 * to register callbacks with mdeg (see vsw_mdeg_register())
160119b65a69Ssb155480 	 */
160219b65a69Ssb155480 	inst = ddi_prop_get_int(DDI_DEV_T_ANY, vswp->dip,
160319b65a69Ssb155480 	    DDI_PROP_DONTPASS, reg_propname, -1);
160419b65a69Ssb155480 	if (inst == -1) {
160519b65a69Ssb155480 		cmn_err(CE_NOTE, "!vsw%d: Unable to read %s property from "
160619b65a69Ssb155480 		    "OBP device tree", vswp->instance, reg_propname);
160719b65a69Ssb155480 		return (rv);
160819b65a69Ssb155480 	}
160919b65a69Ssb155480 
161019b65a69Ssb155480 	vswp->regprop = inst;
161119b65a69Ssb155480 
161219b65a69Ssb155480 	if ((mdp = md_get_handle()) == NULL) {
161319b65a69Ssb155480 		DWARN(vswp, "%s: cannot init MD\n", __func__);
161419b65a69Ssb155480 		return (rv);
161519b65a69Ssb155480 	}
161619b65a69Ssb155480 
161719b65a69Ssb155480 	num_nodes = md_node_count(mdp);
161819b65a69Ssb155480 	ASSERT(num_nodes > 0);
161919b65a69Ssb155480 
162019b65a69Ssb155480 	listsz = num_nodes * sizeof (mde_cookie_t);
162119b65a69Ssb155480 	listp = (mde_cookie_t *)kmem_zalloc(listsz, KM_SLEEP);
162219b65a69Ssb155480 
162319b65a69Ssb155480 	rootnode = md_root_node(mdp);
162419b65a69Ssb155480 
162519b65a69Ssb155480 	/* search for all "virtual_device" nodes */
162619b65a69Ssb155480 	num_devs = md_scan_dag(mdp, rootnode,
162719b65a69Ssb155480 	    md_find_name(mdp, vdev_propname),
162819b65a69Ssb155480 	    md_find_name(mdp, "fwd"), listp);
162919b65a69Ssb155480 	if (num_devs <= 0) {
163019b65a69Ssb155480 		DWARN(vswp, "%s: invalid num_devs:%d\n", __func__, num_devs);
163119b65a69Ssb155480 		goto vsw_readmd_exit;
163219b65a69Ssb155480 	}
163319b65a69Ssb155480 
163419b65a69Ssb155480 	/*
163519b65a69Ssb155480 	 * Now loop through the list of virtual-devices looking for
163619b65a69Ssb155480 	 * devices with name "virtual-network-switch" and for each
163719b65a69Ssb155480 	 * such device compare its instance with what we have from
163819b65a69Ssb155480 	 * the 'reg' property to find the right node in MD and then
163919b65a69Ssb155480 	 * read all its properties.
164019b65a69Ssb155480 	 */
164119b65a69Ssb155480 	for (i = 0; i < num_devs; i++) {
164219b65a69Ssb155480 
164319b65a69Ssb155480 		if (md_get_prop_str(mdp, listp[i], "name", &name) != 0) {
164419b65a69Ssb155480 			DWARN(vswp, "%s: name property not found\n",
164519b65a69Ssb155480 			    __func__);
164619b65a69Ssb155480 			goto vsw_readmd_exit;
164719b65a69Ssb155480 		}
164819b65a69Ssb155480 
164919b65a69Ssb155480 		/* is this a virtual-network-switch? */
165019b65a69Ssb155480 		if (strcmp(name, vsw_propname) != 0)
165119b65a69Ssb155480 			continue;
165219b65a69Ssb155480 
165319b65a69Ssb155480 		if (md_get_prop_val(mdp, listp[i], "cfg-handle", &cfgh) != 0) {
165419b65a69Ssb155480 			DWARN(vswp, "%s: cfg-handle property not found\n",
165519b65a69Ssb155480 			    __func__);
165619b65a69Ssb155480 			goto vsw_readmd_exit;
165719b65a69Ssb155480 		}
165819b65a69Ssb155480 
165919b65a69Ssb155480 		/* is this the required instance of vsw? */
166019b65a69Ssb155480 		if (inst != cfgh)
166119b65a69Ssb155480 			continue;
166219b65a69Ssb155480 
166319b65a69Ssb155480 		/* now read all properties of this vsw instance */
166419b65a69Ssb155480 		rv = vsw_get_initial_md_properties(vswp, mdp, listp[i]);
166519b65a69Ssb155480 		break;
166619b65a69Ssb155480 	}
166719b65a69Ssb155480 
166819b65a69Ssb155480 vsw_readmd_exit:
166919b65a69Ssb155480 
167019b65a69Ssb155480 	kmem_free(listp, listsz);
167119b65a69Ssb155480 	(void) md_fini_handle(mdp);
167219b65a69Ssb155480 	return (rv);
167319b65a69Ssb155480 }
167419b65a69Ssb155480 
167519b65a69Ssb155480 /*
167634683adeSsg70180  * Read the initial start-of-day values from the specified MD node.
167734683adeSsg70180  */
167819b65a69Ssb155480 static int
167934683adeSsg70180 vsw_get_initial_md_properties(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
168034683adeSsg70180 {
168134683adeSsg70180 	uint64_t	macaddr = 0;
168234683adeSsg70180 
168334683adeSsg70180 	D1(vswp, "%s: enter", __func__);
168434683adeSsg70180 
168519b65a69Ssb155480 	if (vsw_get_md_physname(vswp, mdp, node, vswp->physname) != 0) {
168619b65a69Ssb155480 		return (1);
168734683adeSsg70180 	}
168834683adeSsg70180 
168934683adeSsg70180 	/* mac address for vswitch device itself */
169034683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
169134683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
169234683adeSsg70180 		    vswp->instance);
169319b65a69Ssb155480 		return (1);
169419b65a69Ssb155480 	}
169534683adeSsg70180 
169619b65a69Ssb155480 	vsw_save_lmacaddr(vswp, macaddr);
169734683adeSsg70180 
1698da14cebeSEric Cheng 	if (vsw_get_md_smodes(vswp, mdp, node, &vswp->smode)) {
16991ef0bbb5Snarayan 		DWARN(vswp, "%s: Unable to read %s property from MD, "
17001ef0bbb5Snarayan 		    "defaulting to 'switched' mode",
17011ef0bbb5Snarayan 		    __func__, smode_propname);
170234683adeSsg70180 
1703da14cebeSEric Cheng 		vswp->smode = VSW_LAYER2;
170434683adeSsg70180 	}
170534683adeSsg70180 
17061107ea93SSriharsha Basavapatna 	/*
17071107ea93SSriharsha Basavapatna 	 * Read the 'linkprop' property to know if this
17081107ea93SSriharsha Basavapatna 	 * vsw device wants to get physical link updates.
17091107ea93SSriharsha Basavapatna 	 */
17101107ea93SSriharsha Basavapatna 	vsw_linkprop_read(vswp, mdp, node, &vswp->pls_update);
17111107ea93SSriharsha Basavapatna 
17127b1f684aSSriharsha Basavapatna 	/* read mtu */
17137b1f684aSSriharsha Basavapatna 	vsw_mtu_read(vswp, mdp, node, &vswp->mtu);
17147b1f684aSSriharsha Basavapatna 	if (vswp->mtu < ETHERMTU || vswp->mtu > VNET_MAX_MTU) {
17157b1f684aSSriharsha Basavapatna 		vswp->mtu = ETHERMTU;
17167b1f684aSSriharsha Basavapatna 	}
17177b1f684aSSriharsha Basavapatna 	vswp->max_frame_size = vswp->mtu + sizeof (struct ether_header) +
17187b1f684aSSriharsha Basavapatna 	    VLAN_TAGSZ;
17197b1f684aSSriharsha Basavapatna 
1720c1c61f44Ssb155480 	/* read vlan id properties of this vsw instance */
1721c1c61f44Ssb155480 	vsw_vlan_read_ids(vswp, VSW_LOCALDEV, mdp, node, &vswp->pvid,
1722c1c61f44Ssb155480 	    &vswp->vids, &vswp->nvids, &vswp->default_vlan_id);
1723c1c61f44Ssb155480 
1724c1c61f44Ssb155480 	/* read priority-ether-types */
1725f0ca1d9aSsb155480 	vsw_read_pri_eth_types(vswp, mdp, node);
1726f0ca1d9aSsb155480 
1727*bce0a86eSWENTAO YANG 	/* read bandwidth property of this vsw instance */
1728*bce0a86eSWENTAO YANG 	vsw_bandwidth_read(vswp, mdp, node, &vswp->bandwidth);
1729*bce0a86eSWENTAO YANG 
173034683adeSsg70180 	D1(vswp, "%s: exit", __func__);
173119b65a69Ssb155480 	return (0);
173234683adeSsg70180 }
173334683adeSsg70180 
173434683adeSsg70180 /*
1735c1c61f44Ssb155480  * Read vlan id properties of the given MD node.
1736c1c61f44Ssb155480  * Arguments:
1737c1c61f44Ssb155480  *   arg:          device argument(vsw device or a port)
1738c1c61f44Ssb155480  *   type:         type of arg; VSW_LOCALDEV(vsw device) or VSW_VNETPORT(port)
1739c1c61f44Ssb155480  *   mdp:          machine description
1740c1c61f44Ssb155480  *   node:         md node cookie
1741c1c61f44Ssb155480  *
1742c1c61f44Ssb155480  * Returns:
1743c1c61f44Ssb155480  *   pvidp:        port-vlan-id of the node
1744c1c61f44Ssb155480  *   vidspp:       list of vlan-ids of the node
1745c1c61f44Ssb155480  *   nvidsp:       # of vlan-ids in the list
1746c1c61f44Ssb155480  *   default_idp:  default-vlan-id of the node(if node is vsw device)
1747c1c61f44Ssb155480  */
1748c1c61f44Ssb155480 static void
1749c1c61f44Ssb155480 vsw_vlan_read_ids(void *arg, int type, md_t *mdp, mde_cookie_t node,
1750da14cebeSEric Cheng 	uint16_t *pvidp, vsw_vlanid_t **vidspp, uint16_t *nvidsp,
1751c1c61f44Ssb155480 	uint16_t *default_idp)
1752c1c61f44Ssb155480 {
1753c1c61f44Ssb155480 	vsw_t		*vswp;
1754c1c61f44Ssb155480 	vsw_port_t	*portp;
1755c1c61f44Ssb155480 	char		*pvid_propname;
1756c1c61f44Ssb155480 	char		*vid_propname;
1757c1c61f44Ssb155480 	uint_t		nvids = 0;
1758c1c61f44Ssb155480 	uint32_t	vids_size;
1759c1c61f44Ssb155480 	int		rv;
1760c1c61f44Ssb155480 	int		i;
1761c1c61f44Ssb155480 	uint64_t	*data;
1762c1c61f44Ssb155480 	uint64_t	val;
1763c1c61f44Ssb155480 	int		size;
1764c1c61f44Ssb155480 	int		inst;
1765c1c61f44Ssb155480 
1766c1c61f44Ssb155480 	if (type == VSW_LOCALDEV) {
1767c1c61f44Ssb155480 
1768c1c61f44Ssb155480 		vswp = (vsw_t *)arg;
1769c1c61f44Ssb155480 		pvid_propname = vsw_pvid_propname;
1770c1c61f44Ssb155480 		vid_propname = vsw_vid_propname;
1771c1c61f44Ssb155480 		inst = vswp->instance;
1772c1c61f44Ssb155480 
1773c1c61f44Ssb155480 	} else if (type == VSW_VNETPORT) {
1774c1c61f44Ssb155480 
1775c1c61f44Ssb155480 		portp = (vsw_port_t *)arg;
1776c1c61f44Ssb155480 		vswp = portp->p_vswp;
1777c1c61f44Ssb155480 		pvid_propname = port_pvid_propname;
1778c1c61f44Ssb155480 		vid_propname = port_vid_propname;
1779c1c61f44Ssb155480 		inst = portp->p_instance;
1780c1c61f44Ssb155480 
1781c1c61f44Ssb155480 	} else {
1782c1c61f44Ssb155480 		return;
1783c1c61f44Ssb155480 	}
1784c1c61f44Ssb155480 
1785c1c61f44Ssb155480 	if (type == VSW_LOCALDEV && default_idp != NULL) {
1786c1c61f44Ssb155480 		rv = md_get_prop_val(mdp, node, vsw_dvid_propname, &val);
1787c1c61f44Ssb155480 		if (rv != 0) {
1788c1c61f44Ssb155480 			DWARN(vswp, "%s: prop(%s) not found", __func__,
1789c1c61f44Ssb155480 			    vsw_dvid_propname);
1790c1c61f44Ssb155480 
1791c1c61f44Ssb155480 			*default_idp = vsw_default_vlan_id;
1792c1c61f44Ssb155480 		} else {
1793c1c61f44Ssb155480 			*default_idp = val & 0xFFF;
1794c1c61f44Ssb155480 			D2(vswp, "%s: %s(%d): (%d)\n", __func__,
1795c1c61f44Ssb155480 			    vsw_dvid_propname, inst, *default_idp);
1796c1c61f44Ssb155480 		}
1797c1c61f44Ssb155480 	}
1798c1c61f44Ssb155480 
1799c1c61f44Ssb155480 	rv = md_get_prop_val(mdp, node, pvid_propname, &val);
1800c1c61f44Ssb155480 	if (rv != 0) {
1801c1c61f44Ssb155480 		DWARN(vswp, "%s: prop(%s) not found", __func__, pvid_propname);
1802c1c61f44Ssb155480 		*pvidp = vsw_default_vlan_id;
1803c1c61f44Ssb155480 	} else {
1804c1c61f44Ssb155480 
1805c1c61f44Ssb155480 		*pvidp = val & 0xFFF;
1806c1c61f44Ssb155480 		D2(vswp, "%s: %s(%d): (%d)\n", __func__,
1807c1c61f44Ssb155480 		    pvid_propname, inst, *pvidp);
1808c1c61f44Ssb155480 	}
1809c1c61f44Ssb155480 
1810c1c61f44Ssb155480 	rv = md_get_prop_data(mdp, node, vid_propname, (uint8_t **)&data,
1811c1c61f44Ssb155480 	    &size);
1812c1c61f44Ssb155480 	if (rv != 0) {
1813c1c61f44Ssb155480 		D2(vswp, "%s: prop(%s) not found", __func__, vid_propname);
1814c1c61f44Ssb155480 		size = 0;
1815c1c61f44Ssb155480 	} else {
1816c1c61f44Ssb155480 		size /= sizeof (uint64_t);
1817c1c61f44Ssb155480 	}
1818c1c61f44Ssb155480 	nvids = size;
1819c1c61f44Ssb155480 
1820c1c61f44Ssb155480 	if (nvids != 0) {
1821c1c61f44Ssb155480 		D2(vswp, "%s: %s(%d): ", __func__, vid_propname, inst);
1822da14cebeSEric Cheng 		vids_size = sizeof (vsw_vlanid_t) * nvids;
1823c1c61f44Ssb155480 		*vidspp = kmem_zalloc(vids_size, KM_SLEEP);
1824c1c61f44Ssb155480 		for (i = 0; i < nvids; i++) {
1825da14cebeSEric Cheng 			(*vidspp)[i].vl_vid = data[i] & 0xFFFF;
1826da14cebeSEric Cheng 			(*vidspp)[i].vl_set = B_FALSE;
1827da14cebeSEric Cheng 			D2(vswp, " %d ", (*vidspp)[i].vl_vid);
1828c1c61f44Ssb155480 		}
1829c1c61f44Ssb155480 		D2(vswp, "\n");
1830c1c61f44Ssb155480 	}
1831c1c61f44Ssb155480 
1832c1c61f44Ssb155480 	*nvidsp = nvids;
1833c1c61f44Ssb155480 }
1834c1c61f44Ssb155480 
1835*bce0a86eSWENTAO YANG static void
1836*bce0a86eSWENTAO YANG vsw_port_read_bandwidth(vsw_port_t *portp, md_t *mdp, mde_cookie_t node,
1837*bce0a86eSWENTAO YANG     uint64_t *bw)
1838*bce0a86eSWENTAO YANG {
1839*bce0a86eSWENTAO YANG 	int		rv;
1840*bce0a86eSWENTAO YANG 	uint64_t	val;
1841*bce0a86eSWENTAO YANG 	vsw_t		*vswp;
1842*bce0a86eSWENTAO YANG 
1843*bce0a86eSWENTAO YANG 	vswp = portp->p_vswp;
1844*bce0a86eSWENTAO YANG 
1845*bce0a86eSWENTAO YANG 	rv = md_get_prop_val(mdp, node, port_maxbw_propname, &val);
1846*bce0a86eSWENTAO YANG 
1847*bce0a86eSWENTAO YANG 	if (rv != 0) {
1848*bce0a86eSWENTAO YANG 		*bw = 0;
1849*bce0a86eSWENTAO YANG 		D3(vswp, "%s: prop(%s) not found\n", __func__,
1850*bce0a86eSWENTAO YANG 		    port_maxbw_propname);
1851*bce0a86eSWENTAO YANG 	} else {
1852*bce0a86eSWENTAO YANG 		*bw = val;
1853*bce0a86eSWENTAO YANG 		D3(vswp, "%s: %s nodes found", __func__, port_maxbw_propname);
1854*bce0a86eSWENTAO YANG 	}
1855*bce0a86eSWENTAO YANG }
1856*bce0a86eSWENTAO YANG 
1857c1c61f44Ssb155480 /*
1858f0ca1d9aSsb155480  * This function reads "priority-ether-types" property from md. This property
1859f0ca1d9aSsb155480  * is used to enable support for priority frames. Applications which need
1860f0ca1d9aSsb155480  * guaranteed and timely delivery of certain high priority frames to/from
1861f0ca1d9aSsb155480  * a vnet or vsw within ldoms, should configure this property by providing
1862f0ca1d9aSsb155480  * the ether type(s) for which the priority facility is needed.
1863f0ca1d9aSsb155480  * Normal data frames are delivered over a ldc channel using the descriptor
1864f0ca1d9aSsb155480  * ring mechanism which is constrained by factors such as descriptor ring size,
1865f0ca1d9aSsb155480  * the rate at which the ring is processed at the peer ldc end point, etc.
1866f0ca1d9aSsb155480  * The priority mechanism provides an Out-Of-Band path to send/receive frames
1867f0ca1d9aSsb155480  * as raw pkt data (VIO_PKT_DATA) messages over the channel, avoiding the
1868f0ca1d9aSsb155480  * descriptor ring path and enables a more reliable and timely delivery of
1869f0ca1d9aSsb155480  * frames to the peer.
1870f0ca1d9aSsb155480  */
1871f0ca1d9aSsb155480 static void
1872f0ca1d9aSsb155480 vsw_read_pri_eth_types(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
1873f0ca1d9aSsb155480 {
1874f0ca1d9aSsb155480 	int		rv;
1875f0ca1d9aSsb155480 	uint16_t	*types;
1876f0ca1d9aSsb155480 	uint64_t	*data;
1877f0ca1d9aSsb155480 	int		size;
1878f0ca1d9aSsb155480 	int		i;
1879f0ca1d9aSsb155480 	size_t		mblk_sz;
1880f0ca1d9aSsb155480 
1881f0ca1d9aSsb155480 	rv = md_get_prop_data(mdp, node, pri_types_propname,
1882f0ca1d9aSsb155480 	    (uint8_t **)&data, &size);
1883f0ca1d9aSsb155480 	if (rv != 0) {
1884f0ca1d9aSsb155480 		/*
1885f0ca1d9aSsb155480 		 * Property may not exist if we are running pre-ldoms1.1 f/w.
1886f0ca1d9aSsb155480 		 * Check if 'vsw_pri_eth_type' has been set in that case.
1887f0ca1d9aSsb155480 		 */
1888f0ca1d9aSsb155480 		if (vsw_pri_eth_type != 0) {
1889f0ca1d9aSsb155480 			size = sizeof (vsw_pri_eth_type);
1890f0ca1d9aSsb155480 			data = &vsw_pri_eth_type;
1891f0ca1d9aSsb155480 		} else {
1892f0ca1d9aSsb155480 			D3(vswp, "%s: prop(%s) not found", __func__,
1893f0ca1d9aSsb155480 			    pri_types_propname);
1894f0ca1d9aSsb155480 			size = 0;
1895f0ca1d9aSsb155480 		}
1896f0ca1d9aSsb155480 	}
1897f0ca1d9aSsb155480 
1898f0ca1d9aSsb155480 	if (size == 0) {
1899f0ca1d9aSsb155480 		vswp->pri_num_types = 0;
1900f0ca1d9aSsb155480 		return;
1901f0ca1d9aSsb155480 	}
1902f0ca1d9aSsb155480 
1903f0ca1d9aSsb155480 	/*
1904f0ca1d9aSsb155480 	 * we have some priority-ether-types defined;
1905f0ca1d9aSsb155480 	 * allocate a table of these types and also
1906f0ca1d9aSsb155480 	 * allocate a pool of mblks to transmit these
1907f0ca1d9aSsb155480 	 * priority packets.
1908f0ca1d9aSsb155480 	 */
1909f0ca1d9aSsb155480 	size /= sizeof (uint64_t);
1910f0ca1d9aSsb155480 	vswp->pri_num_types = size;
1911f0ca1d9aSsb155480 	vswp->pri_types = kmem_zalloc(size * sizeof (uint16_t), KM_SLEEP);
1912f0ca1d9aSsb155480 	for (i = 0, types = vswp->pri_types; i < size; i++) {
1913f0ca1d9aSsb155480 		types[i] = data[i] & 0xFFFF;
1914f0ca1d9aSsb155480 	}
1915f0ca1d9aSsb155480 	mblk_sz = (VIO_PKT_DATA_HDRSIZE + ETHERMAX + 7) & ~7;
1916f0ca1d9aSsb155480 	(void) vio_create_mblks(vsw_pri_tx_nmblks, mblk_sz, &vswp->pri_tx_vmp);
1917f0ca1d9aSsb155480 }
1918f0ca1d9aSsb155480 
19197b1f684aSSriharsha Basavapatna static void
19207b1f684aSSriharsha Basavapatna vsw_mtu_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node, uint32_t *mtu)
19217b1f684aSSriharsha Basavapatna {
19227b1f684aSSriharsha Basavapatna 	int		rv;
19237b1f684aSSriharsha Basavapatna 	int		inst;
19247b1f684aSSriharsha Basavapatna 	uint64_t	val;
19257b1f684aSSriharsha Basavapatna 	char		*mtu_propname;
19267b1f684aSSriharsha Basavapatna 
19277b1f684aSSriharsha Basavapatna 	mtu_propname = vsw_mtu_propname;
19287b1f684aSSriharsha Basavapatna 	inst = vswp->instance;
19297b1f684aSSriharsha Basavapatna 
19307b1f684aSSriharsha Basavapatna 	rv = md_get_prop_val(mdp, node, mtu_propname, &val);
19317b1f684aSSriharsha Basavapatna 	if (rv != 0) {
19327b1f684aSSriharsha Basavapatna 		D3(vswp, "%s: prop(%s) not found", __func__, mtu_propname);
19337b1f684aSSriharsha Basavapatna 		*mtu = vsw_ethermtu;
19347b1f684aSSriharsha Basavapatna 	} else {
19357b1f684aSSriharsha Basavapatna 
19367b1f684aSSriharsha Basavapatna 		*mtu = val & 0xFFFF;
19377b1f684aSSriharsha Basavapatna 		D2(vswp, "%s: %s(%d): (%d)\n", __func__,
19387b1f684aSSriharsha Basavapatna 		    mtu_propname, inst, *mtu);
19397b1f684aSSriharsha Basavapatna 	}
19407b1f684aSSriharsha Basavapatna }
19417b1f684aSSriharsha Basavapatna 
19427b1f684aSSriharsha Basavapatna /*
19437b1f684aSSriharsha Basavapatna  * Update the mtu of the vsw device. We first check if the device has been
19447b1f684aSSriharsha Basavapatna  * plumbed and if so fail the mtu update. Otherwise, we continue to update the
19457b1f684aSSriharsha Basavapatna  * new mtu and reset all ports to initiate handshake re-negotiation with peers
19467b1f684aSSriharsha Basavapatna  * using the new mtu.
19477b1f684aSSriharsha Basavapatna  */
19487b1f684aSSriharsha Basavapatna static int
19497b1f684aSSriharsha Basavapatna vsw_mtu_update(vsw_t *vswp, uint32_t mtu)
19507b1f684aSSriharsha Basavapatna {
19517b1f684aSSriharsha Basavapatna 	int	rv;
19527b1f684aSSriharsha Basavapatna 
19537b1f684aSSriharsha Basavapatna 	WRITE_ENTER(&vswp->if_lockrw);
19547b1f684aSSriharsha Basavapatna 
19557b1f684aSSriharsha Basavapatna 	if (vswp->if_state & VSW_IF_UP) {
19567b1f684aSSriharsha Basavapatna 
19577b1f684aSSriharsha Basavapatna 		RW_EXIT(&vswp->if_lockrw);
19587b1f684aSSriharsha Basavapatna 
19597b1f684aSSriharsha Basavapatna 		cmn_err(CE_NOTE, "!vsw%d: Unable to process mtu update"
19607b1f684aSSriharsha Basavapatna 		    " as the device is plumbed\n", vswp->instance);
19617b1f684aSSriharsha Basavapatna 		return (EBUSY);
19627b1f684aSSriharsha Basavapatna 
19637b1f684aSSriharsha Basavapatna 	} else {
19647b1f684aSSriharsha Basavapatna 
19657b1f684aSSriharsha Basavapatna 		D2(vswp, "%s: curr_mtu(%d) new_mtu(%d)\n",
19667b1f684aSSriharsha Basavapatna 		    __func__, vswp->mtu, mtu);
19677b1f684aSSriharsha Basavapatna 
19687b1f684aSSriharsha Basavapatna 		vswp->mtu = mtu;
19697b1f684aSSriharsha Basavapatna 		vswp->max_frame_size = vswp->mtu +
19707b1f684aSSriharsha Basavapatna 		    sizeof (struct ether_header) + VLAN_TAGSZ;
19717b1f684aSSriharsha Basavapatna 
19727b1f684aSSriharsha Basavapatna 		rv = mac_maxsdu_update(vswp->if_mh, mtu);
19737b1f684aSSriharsha Basavapatna 		if (rv != 0) {
19747b1f684aSSriharsha Basavapatna 			cmn_err(CE_NOTE,
19757b1f684aSSriharsha Basavapatna 			    "!vsw%d: Unable to update mtu with mac"
19767b1f684aSSriharsha Basavapatna 			    " layer\n", vswp->instance);
19777b1f684aSSriharsha Basavapatna 		}
19787b1f684aSSriharsha Basavapatna 
19797b1f684aSSriharsha Basavapatna 		RW_EXIT(&vswp->if_lockrw);
19807b1f684aSSriharsha Basavapatna 
19817b1f684aSSriharsha Basavapatna 		/* Reset ports to renegotiate with the new mtu */
19827b1f684aSSriharsha Basavapatna 		vsw_reset_ports(vswp);
19837b1f684aSSriharsha Basavapatna 
19847b1f684aSSriharsha Basavapatna 	}
19857b1f684aSSriharsha Basavapatna 
19867b1f684aSSriharsha Basavapatna 	return (0);
19877b1f684aSSriharsha Basavapatna }
19887b1f684aSSriharsha Basavapatna 
19891107ea93SSriharsha Basavapatna static void
19901107ea93SSriharsha Basavapatna vsw_linkprop_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
19911107ea93SSriharsha Basavapatna 	boolean_t *pls)
19921107ea93SSriharsha Basavapatna {
19931107ea93SSriharsha Basavapatna 	int		rv;
19941107ea93SSriharsha Basavapatna 	uint64_t	val;
19951107ea93SSriharsha Basavapatna 	char		*linkpropname;
19961107ea93SSriharsha Basavapatna 
19971107ea93SSriharsha Basavapatna 	linkpropname = vsw_linkprop_propname;
19981107ea93SSriharsha Basavapatna 
19991107ea93SSriharsha Basavapatna 	rv = md_get_prop_val(mdp, node, linkpropname, &val);
20001107ea93SSriharsha Basavapatna 	if (rv != 0) {
20011107ea93SSriharsha Basavapatna 		D3(vswp, "%s: prop(%s) not found", __func__, linkpropname);
20021107ea93SSriharsha Basavapatna 		*pls = B_FALSE;
20031107ea93SSriharsha Basavapatna 	} else {
20041107ea93SSriharsha Basavapatna 
20051107ea93SSriharsha Basavapatna 		*pls = (val & 0x1) ? B_TRUE : B_FALSE;
20061107ea93SSriharsha Basavapatna 		D2(vswp, "%s: %s(%d): (%d)\n", __func__, linkpropname,
20071107ea93SSriharsha Basavapatna 		    vswp->instance, *pls);
20081107ea93SSriharsha Basavapatna 	}
20091107ea93SSriharsha Basavapatna }
20101107ea93SSriharsha Basavapatna 
2011d8a518c8SSriharsha Basavapatna void
20121107ea93SSriharsha Basavapatna vsw_mac_link_update(vsw_t *vswp, link_state_t link_state)
20131107ea93SSriharsha Basavapatna {
20141107ea93SSriharsha Basavapatna 	READ_ENTER(&vswp->if_lockrw);
20151107ea93SSriharsha Basavapatna 
2016d8a518c8SSriharsha Basavapatna 	if (vswp->if_state & VSW_IF_REG) {
20171107ea93SSriharsha Basavapatna 		mac_link_update(vswp->if_mh, link_state);
20181107ea93SSriharsha Basavapatna 	}
20191107ea93SSriharsha Basavapatna 
2020d8a518c8SSriharsha Basavapatna 	RW_EXIT(&vswp->if_lockrw);
2021d8a518c8SSriharsha Basavapatna }
2022d8a518c8SSriharsha Basavapatna 
20231107ea93SSriharsha Basavapatna void
20241107ea93SSriharsha Basavapatna vsw_physlink_state_update(vsw_t *vswp)
20251107ea93SSriharsha Basavapatna {
20261107ea93SSriharsha Basavapatna 	if (vswp->pls_update == B_TRUE) {
20271107ea93SSriharsha Basavapatna 		vsw_mac_link_update(vswp, vswp->phys_link_state);
20281107ea93SSriharsha Basavapatna 	}
20291107ea93SSriharsha Basavapatna 	vsw_physlink_update_ports(vswp);
20301107ea93SSriharsha Basavapatna }
20311107ea93SSriharsha Basavapatna 
2032*bce0a86eSWENTAO YANG static void
2033*bce0a86eSWENTAO YANG vsw_bandwidth_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node, uint64_t *bw)
2034*bce0a86eSWENTAO YANG {
2035*bce0a86eSWENTAO YANG 	/* read the vsw bandwidth from md */
2036*bce0a86eSWENTAO YANG 	int		rv;
2037*bce0a86eSWENTAO YANG 	uint64_t	val;
2038*bce0a86eSWENTAO YANG 
2039*bce0a86eSWENTAO YANG 	rv = md_get_prop_val(mdp, node, vsw_maxbw_propname, &val);
2040*bce0a86eSWENTAO YANG 	if (rv != 0) {
2041*bce0a86eSWENTAO YANG 		*bw = 0;
2042*bce0a86eSWENTAO YANG 		D3(vswp, "%s: prop(%s) not found", __func__,
2043*bce0a86eSWENTAO YANG 		    vsw_maxbw_propname);
2044*bce0a86eSWENTAO YANG 	} else {
2045*bce0a86eSWENTAO YANG 		*bw = val;
2046*bce0a86eSWENTAO YANG 		D3(vswp, "%s: %s(%d): (%ld)\n", __func__,
2047*bce0a86eSWENTAO YANG 		    vsw_maxbw_propname, vswp->instance, *bw);
2048*bce0a86eSWENTAO YANG 	}
2049*bce0a86eSWENTAO YANG }
2050*bce0a86eSWENTAO YANG 
2051f0ca1d9aSsb155480 /*
205234683adeSsg70180  * Check to see if the relevant properties in the specified node have
205334683adeSsg70180  * changed, and if so take the appropriate action.
205434683adeSsg70180  *
205534683adeSsg70180  * If any of the properties are missing or invalid we don't take
205634683adeSsg70180  * any action, as this function should only be invoked when modifications
205734683adeSsg70180  * have been made to what we assume is a working configuration, which
205834683adeSsg70180  * we leave active.
205934683adeSsg70180  *
206034683adeSsg70180  * Note it is legal for this routine to be invoked even if none of the
206134683adeSsg70180  * properties in the port node within the MD have actually changed.
206234683adeSsg70180  */
206334683adeSsg70180 static void
206434683adeSsg70180 vsw_update_md_prop(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
206534683adeSsg70180 {
206634683adeSsg70180 	char		physname[LIFNAMSIZ];
206734683adeSsg70180 	char		drv[LIFNAMSIZ];
206834683adeSsg70180 	uint_t		ddi_instance;
2069da14cebeSEric Cheng 	uint8_t		new_smode;
2070da14cebeSEric Cheng 	int		i;
207134683adeSsg70180 	uint64_t 	macaddr = 0;
207234683adeSsg70180 	enum		{MD_init = 0x1,
207334683adeSsg70180 				MD_physname = 0x2,
207434683adeSsg70180 				MD_macaddr = 0x4,
2075c1c61f44Ssb155480 				MD_smode = 0x8,
20767b1f684aSSriharsha Basavapatna 				MD_vlans = 0x10,
20771107ea93SSriharsha Basavapatna 				MD_mtu = 0x20,
2078*bce0a86eSWENTAO YANG 				MD_pls = 0x40,
2079*bce0a86eSWENTAO YANG 				MD_bw = 0x80} updated;
208019b65a69Ssb155480 	int		rv;
2081c1c61f44Ssb155480 	uint16_t	pvid;
2082da14cebeSEric Cheng 	vsw_vlanid_t	*vids;
2083c1c61f44Ssb155480 	uint16_t	nvids;
20847b1f684aSSriharsha Basavapatna 	uint32_t	mtu;
20851107ea93SSriharsha Basavapatna 	boolean_t	pls_update;
2086*bce0a86eSWENTAO YANG 	uint64_t	maxbw;
208734683adeSsg70180 
208834683adeSsg70180 	updated = MD_init;
208934683adeSsg70180 
209034683adeSsg70180 	D1(vswp, "%s: enter", __func__);
209134683adeSsg70180 
209234683adeSsg70180 	/*
209334683adeSsg70180 	 * Check if name of physical device in MD has changed.
209434683adeSsg70180 	 */
209534683adeSsg70180 	if (vsw_get_md_physname(vswp, mdp, node, (char *)&physname) == 0) {
209634683adeSsg70180 		/*
209734683adeSsg70180 		 * Do basic sanity check on new device name/instance,
209834683adeSsg70180 		 * if its non NULL. It is valid for the device name to
209934683adeSsg70180 		 * have changed from a non NULL to a NULL value, i.e.
210034683adeSsg70180 		 * the vsw is being changed to 'routed' mode.
210134683adeSsg70180 		 */
210234683adeSsg70180 		if ((strlen(physname) != 0) &&
210319b65a69Ssb155480 		    (ddi_parse(physname, drv,
210419b65a69Ssb155480 		    &ddi_instance) != DDI_SUCCESS)) {
21051ef0bbb5Snarayan 			cmn_err(CE_WARN, "!vsw%d: physical device %s is not"
210634683adeSsg70180 			    " a valid device name/instance",
210734683adeSsg70180 			    vswp->instance, physname);
210834683adeSsg70180 			goto fail_reconf;
210934683adeSsg70180 		}
211034683adeSsg70180 
211134683adeSsg70180 		if (strcmp(physname, vswp->physname)) {
211234683adeSsg70180 			D2(vswp, "%s: device name changed from %s to %s",
211334683adeSsg70180 			    __func__, vswp->physname, physname);
211434683adeSsg70180 
211534683adeSsg70180 			updated |= MD_physname;
211634683adeSsg70180 		} else {
211734683adeSsg70180 			D2(vswp, "%s: device name unchanged at %s",
211834683adeSsg70180 			    __func__, vswp->physname);
211934683adeSsg70180 		}
212034683adeSsg70180 	} else {
212134683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read name of physical "
212234683adeSsg70180 		    "device from updated MD.", vswp->instance);
212334683adeSsg70180 		goto fail_reconf;
212434683adeSsg70180 	}
212534683adeSsg70180 
212634683adeSsg70180 	/*
212734683adeSsg70180 	 * Check if MAC address has changed.
212834683adeSsg70180 	 */
212934683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
213034683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
213134683adeSsg70180 		    vswp->instance);
213234683adeSsg70180 		goto fail_reconf;
213334683adeSsg70180 	} else {
213419b65a69Ssb155480 		uint64_t maddr = macaddr;
213534683adeSsg70180 		READ_ENTER(&vswp->if_lockrw);
213634683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
213719b65a69Ssb155480 			if (vswp->if_addr.ether_addr_octet[i]
213819b65a69Ssb155480 			    != (macaddr & 0xFF)) {
213934683adeSsg70180 				D2(vswp, "%s: octet[%d] 0x%x != 0x%x",
214034683adeSsg70180 				    __func__, i,
214134683adeSsg70180 				    vswp->if_addr.ether_addr_octet[i],
214234683adeSsg70180 				    (macaddr & 0xFF));
214334683adeSsg70180 				updated |= MD_macaddr;
214419b65a69Ssb155480 				macaddr = maddr;
214534683adeSsg70180 				break;
214634683adeSsg70180 			}
214734683adeSsg70180 			macaddr >>= 8;
214834683adeSsg70180 		}
214934683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
215019b65a69Ssb155480 		if (updated & MD_macaddr) {
215119b65a69Ssb155480 			vsw_save_lmacaddr(vswp, macaddr);
215219b65a69Ssb155480 		}
215334683adeSsg70180 	}
215434683adeSsg70180 
215534683adeSsg70180 	/*
215634683adeSsg70180 	 * Check if switching modes have changed.
215734683adeSsg70180 	 */
2158da14cebeSEric Cheng 	if (vsw_get_md_smodes(vswp, mdp, node, &new_smode)) {
215934683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read %s property from MD",
216034683adeSsg70180 		    vswp->instance, smode_propname);
216134683adeSsg70180 		goto fail_reconf;
216234683adeSsg70180 	} else {
2163da14cebeSEric Cheng 		if (new_smode != vswp->smode) {
2164da14cebeSEric Cheng 			D2(vswp, "%s: switching mode changed from %d to %d",
2165da14cebeSEric Cheng 			    __func__, vswp->smode, new_smode);
216634683adeSsg70180 
216734683adeSsg70180 			updated |= MD_smode;
216834683adeSsg70180 		}
216934683adeSsg70180 	}
217034683adeSsg70180 
2171c1c61f44Ssb155480 	/* Read the vlan ids */
2172c1c61f44Ssb155480 	vsw_vlan_read_ids(vswp, VSW_LOCALDEV, mdp, node, &pvid, &vids,
2173c1c61f44Ssb155480 	    &nvids, NULL);
2174c1c61f44Ssb155480 
2175c1c61f44Ssb155480 	/* Determine if there are any vlan id updates */
2176c1c61f44Ssb155480 	if ((pvid != vswp->pvid) ||		/* pvid changed? */
2177c1c61f44Ssb155480 	    (nvids != vswp->nvids) ||		/* # of vids changed? */
2178c1c61f44Ssb155480 	    ((nvids != 0) && (vswp->nvids != 0) &&	/* vids changed? */
2179da14cebeSEric Cheng 	    !vsw_cmp_vids(vids, vswp->vids, nvids))) {
2180c1c61f44Ssb155480 		updated |= MD_vlans;
2181c1c61f44Ssb155480 	}
2182c1c61f44Ssb155480 
21837b1f684aSSriharsha Basavapatna 	/* Read mtu */
21847b1f684aSSriharsha Basavapatna 	vsw_mtu_read(vswp, mdp, node, &mtu);
21857b1f684aSSriharsha Basavapatna 	if (mtu != vswp->mtu) {
21867b1f684aSSriharsha Basavapatna 		if (mtu >= ETHERMTU && mtu <= VNET_MAX_MTU) {
21877b1f684aSSriharsha Basavapatna 			updated |= MD_mtu;
21887b1f684aSSriharsha Basavapatna 		} else {
21897b1f684aSSriharsha Basavapatna 			cmn_err(CE_NOTE, "!vsw%d: Unable to process mtu update"
21907b1f684aSSriharsha Basavapatna 			    " as the specified value:%d is invalid\n",
21917b1f684aSSriharsha Basavapatna 			    vswp->instance, mtu);
21927b1f684aSSriharsha Basavapatna 		}
21937b1f684aSSriharsha Basavapatna 	}
21947b1f684aSSriharsha Basavapatna 
219534683adeSsg70180 	/*
21961107ea93SSriharsha Basavapatna 	 * Read the 'linkprop' property.
21971107ea93SSriharsha Basavapatna 	 */
21981107ea93SSriharsha Basavapatna 	vsw_linkprop_read(vswp, mdp, node, &pls_update);
21991107ea93SSriharsha Basavapatna 	if (pls_update != vswp->pls_update) {
22001107ea93SSriharsha Basavapatna 		updated |= MD_pls;
22011107ea93SSriharsha Basavapatna 	}
22021107ea93SSriharsha Basavapatna 
2203*bce0a86eSWENTAO YANG 	/* Read bandwidth */
2204*bce0a86eSWENTAO YANG 	vsw_bandwidth_read(vswp, mdp, node, &maxbw);
2205*bce0a86eSWENTAO YANG 	if (maxbw != vswp->bandwidth) {
2206*bce0a86eSWENTAO YANG 		if (maxbw >= MRP_MAXBW_MINVAL || maxbw == 0) {
2207*bce0a86eSWENTAO YANG 			updated |= MD_bw;
2208*bce0a86eSWENTAO YANG 		} else {
2209*bce0a86eSWENTAO YANG 			cmn_err(CE_NOTE, "!vsw%d: Unable to process bandwidth"
2210*bce0a86eSWENTAO YANG 			    " update as the specified value:%ld is invalid\n",
2211*bce0a86eSWENTAO YANG 			    vswp->instance, maxbw);
2212*bce0a86eSWENTAO YANG 		}
2213*bce0a86eSWENTAO YANG 	}
2214*bce0a86eSWENTAO YANG 
22151107ea93SSriharsha Basavapatna 	/*
221634683adeSsg70180 	 * Now make any changes which are needed...
221734683adeSsg70180 	 */
22181107ea93SSriharsha Basavapatna 	if (updated & MD_pls) {
22191107ea93SSriharsha Basavapatna 
22201107ea93SSriharsha Basavapatna 		/* save the updated property. */
22211107ea93SSriharsha Basavapatna 		vswp->pls_update = pls_update;
22221107ea93SSriharsha Basavapatna 
22231107ea93SSriharsha Basavapatna 		if (pls_update == B_FALSE) {
22241107ea93SSriharsha Basavapatna 			/*
22251107ea93SSriharsha Basavapatna 			 * Phys link state update is now disabled for this vsw
22261107ea93SSriharsha Basavapatna 			 * interface. If we had previously reported a link-down
22271107ea93SSriharsha Basavapatna 			 * to the stack, undo that by sending a link-up.
22281107ea93SSriharsha Basavapatna 			 */
22291107ea93SSriharsha Basavapatna 			if (vswp->phys_link_state == LINK_STATE_DOWN) {
22301107ea93SSriharsha Basavapatna 				vsw_mac_link_update(vswp, LINK_STATE_UP);
22311107ea93SSriharsha Basavapatna 			}
22321107ea93SSriharsha Basavapatna 		} else {
22331107ea93SSriharsha Basavapatna 			/*
22341107ea93SSriharsha Basavapatna 			 * Phys link state update is now enabled. Send up an
22351107ea93SSriharsha Basavapatna 			 * update based on the current phys link state.
22361107ea93SSriharsha Basavapatna 			 */
2237d8a518c8SSriharsha Basavapatna 			if (vswp->smode & VSW_LAYER2) {
2238d8a518c8SSriharsha Basavapatna 				vsw_mac_link_update(vswp,
2239d8a518c8SSriharsha Basavapatna 				    vswp->phys_link_state);
2240d8a518c8SSriharsha Basavapatna 			}
22411107ea93SSriharsha Basavapatna 		}
22421107ea93SSriharsha Basavapatna 
22431107ea93SSriharsha Basavapatna 	}
224434683adeSsg70180 
2245da14cebeSEric Cheng 	if (updated & (MD_physname | MD_smode | MD_mtu)) {
224634683adeSsg70180 
224734683adeSsg70180 		/*
2248808f26a8SSriharsha Basavapatna 		 * Stop any pending thread to setup switching mode.
224934683adeSsg70180 		 */
2250808f26a8SSriharsha Basavapatna 		vsw_setup_switching_stop(vswp);
225119b65a69Ssb155480 
2252678453a8Sspeer 		/* Cleanup HybridIO */
2253678453a8Sspeer 		vsw_hio_cleanup(vswp);
2254678453a8Sspeer 
225519b65a69Ssb155480 		/*
225619b65a69Ssb155480 		 * Remove unicst, mcst addrs of vsw interface
2257da14cebeSEric Cheng 		 * and ports from the physdev. This also closes
2258da14cebeSEric Cheng 		 * the corresponding mac clients.
225919b65a69Ssb155480 		 */
226019b65a69Ssb155480 		vsw_unset_addrs(vswp);
226119b65a69Ssb155480 
226219b65a69Ssb155480 		/*
226319b65a69Ssb155480 		 * Stop, detach and close the old device..
226419b65a69Ssb155480 		 */
2265da14cebeSEric Cheng 		mutex_enter(&vswp->mac_lock);
226619b65a69Ssb155480 		vsw_mac_close(vswp);
2267da14cebeSEric Cheng 		mutex_exit(&vswp->mac_lock);
226834683adeSsg70180 
226934683adeSsg70180 		/*
227034683adeSsg70180 		 * Update phys name.
227134683adeSsg70180 		 */
227234683adeSsg70180 		if (updated & MD_physname) {
227334683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: changing from %s to %s",
227434683adeSsg70180 			    vswp->instance, vswp->physname, physname);
227534683adeSsg70180 			(void) strncpy(vswp->physname,
227634683adeSsg70180 			    physname, strlen(physname) + 1);
227734683adeSsg70180 		}
227834683adeSsg70180 
227934683adeSsg70180 		/*
228034683adeSsg70180 		 * Update array with the new switch mode values.
228134683adeSsg70180 		 */
228234683adeSsg70180 		if (updated & MD_smode) {
2283da14cebeSEric Cheng 			vswp->smode = new_smode;
2284da14cebeSEric Cheng 		}
228534683adeSsg70180 
2286da14cebeSEric Cheng 		/* Update mtu */
2287da14cebeSEric Cheng 		if (updated & MD_mtu) {
2288da14cebeSEric Cheng 			rv = vsw_mtu_update(vswp, mtu);
2289da14cebeSEric Cheng 			if (rv != 0) {
2290da14cebeSEric Cheng 				goto fail_update;
2291da14cebeSEric Cheng 			}
229234683adeSsg70180 		}
229334683adeSsg70180 
229434683adeSsg70180 		/*
229534683adeSsg70180 		 * ..and attach, start the new device.
229634683adeSsg70180 		 */
229719b65a69Ssb155480 		rv = vsw_setup_switching(vswp);
229819b65a69Ssb155480 		if (rv == EAGAIN) {
229919b65a69Ssb155480 			/*
230019b65a69Ssb155480 			 * Unable to setup switching mode.
2301808f26a8SSriharsha Basavapatna 			 * As the error is EAGAIN, schedule a thread to retry
230219b65a69Ssb155480 			 * and return. Programming addresses of ports and
2303808f26a8SSriharsha Basavapatna 			 * vsw interface will be done by the thread when the
2304808f26a8SSriharsha Basavapatna 			 * switching setup completes successfully.
230519b65a69Ssb155480 			 */
2306808f26a8SSriharsha Basavapatna 			if (vsw_setup_switching_start(vswp) != 0) {
2307808f26a8SSriharsha Basavapatna 				goto fail_update;
2308808f26a8SSriharsha Basavapatna 			}
230919b65a69Ssb155480 			return;
231019b65a69Ssb155480 
231119b65a69Ssb155480 		} else if (rv) {
231234683adeSsg70180 			goto fail_update;
231319b65a69Ssb155480 		}
231434683adeSsg70180 
2315d8a518c8SSriharsha Basavapatna 		vsw_setup_switching_post_process(vswp);
231619b65a69Ssb155480 	} else if (updated & MD_macaddr) {
231719b65a69Ssb155480 		/*
231819b65a69Ssb155480 		 * We enter here if only MD_macaddr is exclusively updated.
231919b65a69Ssb155480 		 * If MD_physname and/or MD_smode are also updated, then
232019b65a69Ssb155480 		 * as part of that, we would have implicitly processed
232119b65a69Ssb155480 		 * MD_macaddr update (above).
232219b65a69Ssb155480 		 */
232334683adeSsg70180 		cmn_err(CE_NOTE, "!vsw%d: changing mac address to 0x%lx",
232434683adeSsg70180 		    vswp->instance, macaddr);
232534683adeSsg70180 
232619b65a69Ssb155480 		READ_ENTER(&vswp->if_lockrw);
232719b65a69Ssb155480 		if (vswp->if_state & VSW_IF_UP) {
2328da14cebeSEric Cheng 			/* reconfigure with new address */
2329da14cebeSEric Cheng 			vsw_if_mac_reconfig(vswp, B_FALSE, 0, NULL, 0);
233034683adeSsg70180 
23315f94e909Ssg70180 			/*
233234683adeSsg70180 			 * Notify the MAC layer of the changed address.
233334683adeSsg70180 			 */
233419b65a69Ssb155480 			mac_unicst_update(vswp->if_mh,
233519b65a69Ssb155480 			    (uint8_t *)&vswp->if_addr);
233619b65a69Ssb155480 
233719b65a69Ssb155480 		}
233819b65a69Ssb155480 		RW_EXIT(&vswp->if_lockrw);
233919b65a69Ssb155480 
234034683adeSsg70180 	}
234134683adeSsg70180 
2342c1c61f44Ssb155480 	if (updated & MD_vlans) {
2343c1c61f44Ssb155480 		/* Remove existing vlan ids from the hash table. */
2344c1c61f44Ssb155480 		vsw_vlan_remove_ids(vswp, VSW_LOCALDEV);
2345c1c61f44Ssb155480 
2346da14cebeSEric Cheng 		if (vswp->if_state & VSW_IF_UP) {
2347da14cebeSEric Cheng 			vsw_if_mac_reconfig(vswp, B_TRUE, pvid, vids, nvids);
2348da14cebeSEric Cheng 		} else {
2349c1c61f44Ssb155480 			if (vswp->nvids != 0) {
2350da14cebeSEric Cheng 				kmem_free(vswp->vids,
2351da14cebeSEric Cheng 				    sizeof (vsw_vlanid_t) * vswp->nvids);
2352c1c61f44Ssb155480 			}
2353c1c61f44Ssb155480 			vswp->vids = vids;
2354da14cebeSEric Cheng 			vswp->nvids = nvids;
2355da14cebeSEric Cheng 			vswp->pvid = pvid;
2356c1c61f44Ssb155480 		}
2357c1c61f44Ssb155480 
2358c1c61f44Ssb155480 		/* add these new vlan ids into hash table */
2359c1c61f44Ssb155480 		vsw_vlan_add_ids(vswp, VSW_LOCALDEV);
2360c1c61f44Ssb155480 	} else {
2361c1c61f44Ssb155480 		if (nvids != 0) {
2362da14cebeSEric Cheng 			kmem_free(vids, sizeof (vsw_vlanid_t) * nvids);
2363c1c61f44Ssb155480 		}
2364c1c61f44Ssb155480 	}
2365c1c61f44Ssb155480 
2366*bce0a86eSWENTAO YANG 	if (updated & MD_bw) {
2367*bce0a86eSWENTAO YANG 		vsw_update_bandwidth(vswp, NULL, VSW_LOCALDEV, maxbw);
2368*bce0a86eSWENTAO YANG 	}
2369*bce0a86eSWENTAO YANG 
237034683adeSsg70180 	return;
237134683adeSsg70180 
237234683adeSsg70180 fail_reconf:
237334683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: configuration unchanged", vswp->instance);
237434683adeSsg70180 	return;
237534683adeSsg70180 
237634683adeSsg70180 fail_update:
23771ef0bbb5Snarayan 	cmn_err(CE_WARN, "!vsw%d: re-configuration failed",
237834683adeSsg70180 	    vswp->instance);
237934683adeSsg70180 }
238034683adeSsg70180 
238134683adeSsg70180 /*
2382c1c61f44Ssb155480  * Read the port's md properties.
23831ae08745Sheppo  */
2384c1c61f44Ssb155480 static int
2385c1c61f44Ssb155480 vsw_port_read_props(vsw_port_t *portp, vsw_t *vswp,
2386c1c61f44Ssb155480 	md_t *mdp, mde_cookie_t *node)
23871ae08745Sheppo {
23881ae08745Sheppo 	uint64_t		ldc_id;
23891ae08745Sheppo 	uint8_t			*addrp;
23901ae08745Sheppo 	int			i, addrsz;
23911ae08745Sheppo 	int			num_nodes = 0, nchan = 0;
23921ae08745Sheppo 	int			listsz = 0;
23931ae08745Sheppo 	mde_cookie_t		*listp = NULL;
23941ae08745Sheppo 	struct ether_addr	ea;
23951ae08745Sheppo 	uint64_t		macaddr;
23961ae08745Sheppo 	uint64_t		inst = 0;
2397678453a8Sspeer 	uint64_t		val;
23981ae08745Sheppo 
23991ae08745Sheppo 	if (md_get_prop_val(mdp, *node, id_propname, &inst)) {
24001ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found", __func__,
24011ae08745Sheppo 		    id_propname);
24021ae08745Sheppo 		return (1);
24031ae08745Sheppo 	}
24041ae08745Sheppo 
24051ae08745Sheppo 	/*
24061ae08745Sheppo 	 * Find the channel endpoint node(s) (which should be under this
24071ae08745Sheppo 	 * port node) which contain the channel id(s).
24081ae08745Sheppo 	 */
24091ae08745Sheppo 	if ((num_nodes = md_node_count(mdp)) <= 0) {
24101ae08745Sheppo 		DERR(vswp, "%s: invalid number of nodes found (%d)",
24111ae08745Sheppo 		    __func__, num_nodes);
24121ae08745Sheppo 		return (1);
24131ae08745Sheppo 	}
24141ae08745Sheppo 
241534683adeSsg70180 	D2(vswp, "%s: %d nodes found", __func__, num_nodes);
241634683adeSsg70180 
24171ae08745Sheppo 	/* allocate enough space for node list */
24181ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
24191ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
24201ae08745Sheppo 
2421205eeb1aSlm66018 	nchan = md_scan_dag(mdp, *node, md_find_name(mdp, chan_propname),
24221ae08745Sheppo 	    md_find_name(mdp, "fwd"), listp);
24231ae08745Sheppo 
24241ae08745Sheppo 	if (nchan <= 0) {
24251ae08745Sheppo 		DWARN(vswp, "%s: no %s nodes found", __func__, chan_propname);
24261ae08745Sheppo 		kmem_free(listp, listsz);
24271ae08745Sheppo 		return (1);
24281ae08745Sheppo 	}
24291ae08745Sheppo 
24301ae08745Sheppo 	D2(vswp, "%s: %d %s nodes found", __func__, nchan, chan_propname);
24311ae08745Sheppo 
24321ae08745Sheppo 	/* use property from first node found */
24331ae08745Sheppo 	if (md_get_prop_val(mdp, listp[0], id_propname, &ldc_id)) {
24341ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found\n", __func__,
24351ae08745Sheppo 		    id_propname);
24361ae08745Sheppo 		kmem_free(listp, listsz);
24371ae08745Sheppo 		return (1);
24381ae08745Sheppo 	}
24391ae08745Sheppo 
24401ae08745Sheppo 	/* don't need list any more */
24411ae08745Sheppo 	kmem_free(listp, listsz);
24421ae08745Sheppo 
24431ae08745Sheppo 	D2(vswp, "%s: ldc_id 0x%llx", __func__, ldc_id);
24441ae08745Sheppo 
24451ae08745Sheppo 	/* read mac-address property */
24461ae08745Sheppo 	if (md_get_prop_data(mdp, *node, remaddr_propname,
24471ae08745Sheppo 	    &addrp, &addrsz)) {
24481ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found",
24491ae08745Sheppo 		    __func__, remaddr_propname);
24501ae08745Sheppo 		return (1);
24511ae08745Sheppo 	}
24521ae08745Sheppo 
24531ae08745Sheppo 	if (addrsz < ETHERADDRL) {
24541ae08745Sheppo 		DWARN(vswp, "%s: invalid address size", __func__);
24551ae08745Sheppo 		return (1);
24561ae08745Sheppo 	}
24571ae08745Sheppo 
24581ae08745Sheppo 	macaddr = *((uint64_t *)addrp);
24591ae08745Sheppo 	D2(vswp, "%s: remote mac address 0x%llx", __func__, macaddr);
24601ae08745Sheppo 
24611ae08745Sheppo 	for (i = ETHERADDRL - 1; i >= 0; i--) {
24621ae08745Sheppo 		ea.ether_addr_octet[i] = macaddr & 0xFF;
24631ae08745Sheppo 		macaddr >>= 8;
24641ae08745Sheppo 	}
24651ae08745Sheppo 
2466c1c61f44Ssb155480 	/* now update all properties into the port */
2467c1c61f44Ssb155480 	portp->p_vswp = vswp;
2468c1c61f44Ssb155480 	portp->p_instance = inst;
2469da14cebeSEric Cheng 	portp->addr_set = B_FALSE;
2470c1c61f44Ssb155480 	ether_copy(&ea, &portp->p_macaddr);
2471c1c61f44Ssb155480 	if (nchan > VSW_PORT_MAX_LDCS) {
2472c1c61f44Ssb155480 		D2(vswp, "%s: using first of %d ldc ids",
2473c1c61f44Ssb155480 		    __func__, nchan);
2474c1c61f44Ssb155480 		nchan = VSW_PORT_MAX_LDCS;
2475c1c61f44Ssb155480 	}
2476c1c61f44Ssb155480 	portp->num_ldcs = nchan;
2477c1c61f44Ssb155480 	portp->ldc_ids =
2478c1c61f44Ssb155480 	    kmem_zalloc(sizeof (uint64_t) * nchan, KM_SLEEP);
2479c1c61f44Ssb155480 	bcopy(&ldc_id, (portp->ldc_ids), sizeof (uint64_t) * nchan);
2480c1c61f44Ssb155480 
2481c1c61f44Ssb155480 	/* read vlan id properties of this port node */
2482c1c61f44Ssb155480 	vsw_vlan_read_ids(portp, VSW_VNETPORT, mdp, *node, &portp->pvid,
2483c1c61f44Ssb155480 	    &portp->vids, &portp->nvids, NULL);
2484c1c61f44Ssb155480 
2485678453a8Sspeer 	/* Check if hybrid property is present */
2486678453a8Sspeer 	if (md_get_prop_val(mdp, *node, hybrid_propname, &val) == 0) {
2487678453a8Sspeer 		D1(vswp, "%s: prop(%s) found\n", __func__, hybrid_propname);
2488678453a8Sspeer 		portp->p_hio_enabled = B_TRUE;
2489678453a8Sspeer 	} else {
2490678453a8Sspeer 		portp->p_hio_enabled = B_FALSE;
2491678453a8Sspeer 	}
2492678453a8Sspeer 	/*
2493678453a8Sspeer 	 * Port hio capability determined after version
2494678453a8Sspeer 	 * negotiation, i.e., when we know the peer is HybridIO capable.
2495678453a8Sspeer 	 */
2496678453a8Sspeer 	portp->p_hio_capable = B_FALSE;
2497*bce0a86eSWENTAO YANG 
2498*bce0a86eSWENTAO YANG 	/* Read bandwidth of this port */
2499*bce0a86eSWENTAO YANG 	vsw_port_read_bandwidth(portp, mdp, *node, &portp->p_bandwidth);
2500*bce0a86eSWENTAO YANG 
2501c1c61f44Ssb155480 	return (0);
2502c1c61f44Ssb155480 }
2503c1c61f44Ssb155480 
2504c1c61f44Ssb155480 /*
2505c1c61f44Ssb155480  * Add a new port to the system.
2506c1c61f44Ssb155480  *
2507c1c61f44Ssb155480  * Returns 0 on success, 1 on failure.
2508c1c61f44Ssb155480  */
2509c1c61f44Ssb155480 int
2510c1c61f44Ssb155480 vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node)
2511c1c61f44Ssb155480 {
2512c1c61f44Ssb155480 	vsw_port_t	*portp;
2513c1c61f44Ssb155480 	int		rv;
2514c1c61f44Ssb155480 
2515c1c61f44Ssb155480 	portp = kmem_zalloc(sizeof (vsw_port_t), KM_SLEEP);
2516c1c61f44Ssb155480 
2517c1c61f44Ssb155480 	rv = vsw_port_read_props(portp, vswp, mdp, node);
2518c1c61f44Ssb155480 	if (rv != 0) {
2519c1c61f44Ssb155480 		kmem_free(portp, sizeof (*portp));
2520c1c61f44Ssb155480 		return (1);
2521c1c61f44Ssb155480 	}
2522c1c61f44Ssb155480 
2523c1c61f44Ssb155480 	rv = vsw_port_attach(portp);
2524c1c61f44Ssb155480 	if (rv != 0) {
25251ae08745Sheppo 		DERR(vswp, "%s: failed to attach port", __func__);
25261ae08745Sheppo 		return (1);
25271ae08745Sheppo 	}
25281ae08745Sheppo 
2529c1c61f44Ssb155480 	return (0);
2530c1c61f44Ssb155480 }
25311ae08745Sheppo 
2532c1c61f44Ssb155480 static int
2533c1c61f44Ssb155480 vsw_port_update(vsw_t *vswp, md_t *curr_mdp, mde_cookie_t curr_mdex,
2534c1c61f44Ssb155480 	md_t *prev_mdp, mde_cookie_t prev_mdex)
2535c1c61f44Ssb155480 {
2536c1c61f44Ssb155480 	uint64_t	cport_num;
2537c1c61f44Ssb155480 	uint64_t	pport_num;
2538c1c61f44Ssb155480 	vsw_port_list_t	*plistp;
2539c1c61f44Ssb155480 	vsw_port_t	*portp;
2540c1c61f44Ssb155480 	uint16_t	pvid;
2541da14cebeSEric Cheng 	vsw_vlanid_t	*vids;
2542c1c61f44Ssb155480 	uint16_t	nvids;
2543678453a8Sspeer 	uint64_t	val;
2544678453a8Sspeer 	boolean_t	hio_enabled = B_FALSE;
2545*bce0a86eSWENTAO YANG 	uint64_t	maxbw;
2546*bce0a86eSWENTAO YANG 	enum		{P_MD_init = 0x1,
2547*bce0a86eSWENTAO YANG 				P_MD_vlans = 0x2,
2548*bce0a86eSWENTAO YANG 				P_MD_hio = 0x4,
2549*bce0a86eSWENTAO YANG 				P_MD_maxbw = 0x8} updated;
2550*bce0a86eSWENTAO YANG 
2551*bce0a86eSWENTAO YANG 	updated = P_MD_init;
2552c1c61f44Ssb155480 
2553c1c61f44Ssb155480 	/*
2554c1c61f44Ssb155480 	 * For now, we get port updates only if vlan ids changed.
2555c1c61f44Ssb155480 	 * We read the port num and do some sanity check.
2556c1c61f44Ssb155480 	 */
2557c1c61f44Ssb155480 	if (md_get_prop_val(curr_mdp, curr_mdex, id_propname, &cport_num)) {
2558c1c61f44Ssb155480 		return (1);
2559c1c61f44Ssb155480 	}
2560c1c61f44Ssb155480 
2561c1c61f44Ssb155480 	if (md_get_prop_val(prev_mdp, prev_mdex, id_propname, &pport_num)) {
2562c1c61f44Ssb155480 		return (1);
2563c1c61f44Ssb155480 	}
2564c1c61f44Ssb155480 	if (cport_num != pport_num)
2565c1c61f44Ssb155480 		return (1);
2566c1c61f44Ssb155480 
2567c1c61f44Ssb155480 	plistp = &(vswp->plist);
2568c1c61f44Ssb155480 
2569c1c61f44Ssb155480 	READ_ENTER(&plistp->lockrw);
2570c1c61f44Ssb155480 
2571c1c61f44Ssb155480 	portp = vsw_lookup_port(vswp, cport_num);
2572c1c61f44Ssb155480 	if (portp == NULL) {
2573c1c61f44Ssb155480 		RW_EXIT(&plistp->lockrw);
2574c1c61f44Ssb155480 		return (1);
2575c1c61f44Ssb155480 	}
2576c1c61f44Ssb155480 
2577c1c61f44Ssb155480 	/* Read the vlan ids */
2578c1c61f44Ssb155480 	vsw_vlan_read_ids(portp, VSW_VNETPORT, curr_mdp, curr_mdex, &pvid,
2579c1c61f44Ssb155480 	    &vids, &nvids, NULL);
2580c1c61f44Ssb155480 
2581c1c61f44Ssb155480 	/* Determine if there are any vlan id updates */
2582c1c61f44Ssb155480 	if ((pvid != portp->pvid) ||		/* pvid changed? */
2583c1c61f44Ssb155480 	    (nvids != portp->nvids) ||		/* # of vids changed? */
2584c1c61f44Ssb155480 	    ((nvids != 0) && (portp->nvids != 0) &&	/* vids changed? */
2585da14cebeSEric Cheng 	    !vsw_cmp_vids(vids, portp->vids, nvids))) {
2586*bce0a86eSWENTAO YANG 		updated |= P_MD_vlans;
2587c1c61f44Ssb155480 	}
2588c1c61f44Ssb155480 
2589*bce0a86eSWENTAO YANG 	/* Check if hybrid property is present */
2590*bce0a86eSWENTAO YANG 	if (md_get_prop_val(curr_mdp, curr_mdex, hybrid_propname, &val) == 0) {
2591*bce0a86eSWENTAO YANG 		D1(vswp, "%s: prop(%s) found\n", __func__, hybrid_propname);
2592*bce0a86eSWENTAO YANG 		hio_enabled = B_TRUE;
2593*bce0a86eSWENTAO YANG 	}
2594c1c61f44Ssb155480 
2595*bce0a86eSWENTAO YANG 	if (portp->p_hio_enabled != hio_enabled) {
2596*bce0a86eSWENTAO YANG 		updated |= P_MD_hio;
2597*bce0a86eSWENTAO YANG 	}
2598*bce0a86eSWENTAO YANG 
2599*bce0a86eSWENTAO YANG 	/* Check if maxbw property is present */
2600*bce0a86eSWENTAO YANG 	vsw_port_read_bandwidth(portp, curr_mdp, curr_mdex, &maxbw);
2601*bce0a86eSWENTAO YANG 	if (maxbw != portp->p_bandwidth) {
2602*bce0a86eSWENTAO YANG 		if (maxbw >= MRP_MAXBW_MINVAL || maxbw == 0) {
2603*bce0a86eSWENTAO YANG 			updated |= P_MD_maxbw;
2604*bce0a86eSWENTAO YANG 		} else {
2605*bce0a86eSWENTAO YANG 			cmn_err(CE_NOTE, "!vsw%d: Unable to process bandwidth"
2606*bce0a86eSWENTAO YANG 			    " update for port %d as the specified value:%ld"
2607*bce0a86eSWENTAO YANG 			    " is invalid\n",
2608*bce0a86eSWENTAO YANG 			    vswp->instance, portp->p_instance, maxbw);
2609*bce0a86eSWENTAO YANG 		}
2610*bce0a86eSWENTAO YANG 	}
2611*bce0a86eSWENTAO YANG 
2612*bce0a86eSWENTAO YANG 	if (updated & P_MD_vlans) {
2613c1c61f44Ssb155480 		/* Remove existing vlan ids from the hash table. */
2614c1c61f44Ssb155480 		vsw_vlan_remove_ids(portp, VSW_VNETPORT);
2615c1c61f44Ssb155480 
2616da14cebeSEric Cheng 		/* Reconfigure vlans with network device */
2617da14cebeSEric Cheng 		vsw_mac_port_reconfig_vlans(portp, pvid, vids, nvids);
2618c1c61f44Ssb155480 
2619c1c61f44Ssb155480 		/* add these new vlan ids into hash table */
2620c1c61f44Ssb155480 		vsw_vlan_add_ids(portp, VSW_VNETPORT);
2621c1c61f44Ssb155480 
2622c1c61f44Ssb155480 		/* reset the port if it is vlan unaware (ver < 1.3) */
2623c1c61f44Ssb155480 		vsw_vlan_unaware_port_reset(portp);
2624678453a8Sspeer 	}
2625678453a8Sspeer 
2626*bce0a86eSWENTAO YANG 	if (updated & P_MD_hio) {
2627*bce0a86eSWENTAO YANG 		vsw_hio_port_update(portp, hio_enabled);
2628678453a8Sspeer 	}
2629678453a8Sspeer 
2630*bce0a86eSWENTAO YANG 	if (updated & P_MD_maxbw) {
2631*bce0a86eSWENTAO YANG 		vsw_update_bandwidth(NULL, portp, VSW_VNETPORT, maxbw);
2632678453a8Sspeer 	}
2633c1c61f44Ssb155480 
2634c1c61f44Ssb155480 	RW_EXIT(&plistp->lockrw);
26351ae08745Sheppo 
26361ae08745Sheppo 	return (0);
26371ae08745Sheppo }
26381ae08745Sheppo 
26391ae08745Sheppo /*
264006db247cSraghuram  * vsw_mac_rx -- A common function to send packets to the interface.
264106db247cSraghuram  * By default this function check if the interface is UP or not, the
264206db247cSraghuram  * rest of the behaviour depends on the flags as below:
26431ae08745Sheppo  *
264406db247cSraghuram  *	VSW_MACRX_PROMISC -- Check if the promisc mode set or not.
264506db247cSraghuram  *	VSW_MACRX_COPYMSG -- Make a copy of the message(s).
264606db247cSraghuram  *	VSW_MACRX_FREEMSG -- Free if the messages cannot be sent up the stack.
26471ae08745Sheppo  */
26481ae08745Sheppo void
2649f0ca1d9aSsb155480 vsw_mac_rx(vsw_t *vswp, mac_resource_handle_t mrh,
2650f0ca1d9aSsb155480     mblk_t *mp, vsw_macrx_flags_t flags)
26511ae08745Sheppo {
2652c1c61f44Ssb155480 	mblk_t		*mpt;
2653c1c61f44Ssb155480 
265406db247cSraghuram 	D1(vswp, "%s:enter\n", __func__);
26551ae08745Sheppo 	READ_ENTER(&vswp->if_lockrw);
265606db247cSraghuram 	/* Check if the interface is up */
265706db247cSraghuram 	if (!(vswp->if_state & VSW_IF_UP)) {
26581ae08745Sheppo 		RW_EXIT(&vswp->if_lockrw);
265906db247cSraghuram 		/* Free messages only if FREEMSG flag specified */
266006db247cSraghuram 		if (flags & VSW_MACRX_FREEMSG) {
266106db247cSraghuram 			freemsgchain(mp);
266206db247cSraghuram 		}
266306db247cSraghuram 		D1(vswp, "%s:exit\n", __func__);
266406db247cSraghuram 		return;
266506db247cSraghuram 	}
266606db247cSraghuram 	/*
266706db247cSraghuram 	 * If PROMISC flag is passed, then check if
266806db247cSraghuram 	 * the interface is in the PROMISC mode.
266906db247cSraghuram 	 * If not, drop the messages.
267006db247cSraghuram 	 */
267106db247cSraghuram 	if (flags & VSW_MACRX_PROMISC) {
267206db247cSraghuram 		if (!(vswp->if_state & VSW_IF_PROMISC)) {
267306db247cSraghuram 			RW_EXIT(&vswp->if_lockrw);
267406db247cSraghuram 			/* Free messages only if FREEMSG flag specified */
267506db247cSraghuram 			if (flags & VSW_MACRX_FREEMSG) {
267606db247cSraghuram 				freemsgchain(mp);
267706db247cSraghuram 			}
267806db247cSraghuram 			D1(vswp, "%s:exit\n", __func__);
267906db247cSraghuram 			return;
268006db247cSraghuram 		}
268106db247cSraghuram 	}
268206db247cSraghuram 	RW_EXIT(&vswp->if_lockrw);
268306db247cSraghuram 	/*
268406db247cSraghuram 	 * If COPYMSG flag is passed, then make a copy
268506db247cSraghuram 	 * of the message chain and send up the copy.
268606db247cSraghuram 	 */
268706db247cSraghuram 	if (flags & VSW_MACRX_COPYMSG) {
268806db247cSraghuram 		mp = copymsgchain(mp);
2689f0ca1d9aSsb155480 		if (mp == NULL) {
269006db247cSraghuram 			D1(vswp, "%s:exit\n", __func__);
269106db247cSraghuram 			return;
269206db247cSraghuram 		}
269306db247cSraghuram 	}
269406db247cSraghuram 
2695f0ca1d9aSsb155480 	D2(vswp, "%s: sending up stack", __func__);
2696c1c61f44Ssb155480 
2697c1c61f44Ssb155480 	mpt = NULL;
2698c1c61f44Ssb155480 	(void) vsw_vlan_frame_untag(vswp, VSW_LOCALDEV, &mp, &mpt);
2699c1c61f44Ssb155480 	if (mp != NULL) {
2700ba2e4443Sseb 		mac_rx(vswp->if_mh, mrh, mp);
2701c1c61f44Ssb155480 	}
270206db247cSraghuram 	D1(vswp, "%s:exit\n", __func__);
27031ae08745Sheppo }
27041ae08745Sheppo 
270506db247cSraghuram /* copy mac address of vsw into soft state structure */
27061ae08745Sheppo static void
270706db247cSraghuram vsw_save_lmacaddr(vsw_t *vswp, uint64_t macaddr)
27081ae08745Sheppo {
27091ae08745Sheppo 	int	i;
27101ae08745Sheppo 
271106db247cSraghuram 	WRITE_ENTER(&vswp->if_lockrw);
271206db247cSraghuram 	for (i = ETHERADDRL - 1; i >= 0; i--) {
271306db247cSraghuram 		vswp->if_addr.ether_addr_octet[i] = macaddr & 0xFF;
271406db247cSraghuram 		macaddr >>= 8;
27151ae08745Sheppo 	}
271606db247cSraghuram 	RW_EXIT(&vswp->if_lockrw);
27171ae08745Sheppo }
2718da14cebeSEric Cheng 
2719da14cebeSEric Cheng /* Compare VLAN ids, array size expected to be same. */
2720da14cebeSEric Cheng static boolean_t
2721da14cebeSEric Cheng vsw_cmp_vids(vsw_vlanid_t *vids1, vsw_vlanid_t *vids2, int nvids)
2722da14cebeSEric Cheng {
2723da14cebeSEric Cheng 	int i, j;
2724da14cebeSEric Cheng 	uint16_t vid;
2725da14cebeSEric Cheng 
2726da14cebeSEric Cheng 	for (i = 0; i < nvids; i++) {
2727da14cebeSEric Cheng 		vid = vids1[i].vl_vid;
2728da14cebeSEric Cheng 		for (j = 0; j < nvids; j++) {
2729da14cebeSEric Cheng 			if (vid == vids2[i].vl_vid)
2730da14cebeSEric Cheng 				break;
2731da14cebeSEric Cheng 		}
2732da14cebeSEric Cheng 		if (j == nvids) {
2733da14cebeSEric Cheng 			return (B_FALSE);
2734da14cebeSEric Cheng 		}
2735da14cebeSEric Cheng 	}
2736da14cebeSEric Cheng 	return (B_TRUE);
2737da14cebeSEric Cheng }
2738