xref: /onnv-gate/usr/src/uts/sun4v/io/vsw.c (revision 13098:496fd9979cfc)
11991Sheppo /*
21991Sheppo  * CDDL HEADER START
31991Sheppo  *
41991Sheppo  * The contents of this file are subject to the terms of the
51991Sheppo  * Common Development and Distribution License (the "License").
61991Sheppo  * You may not use this file except in compliance with the License.
71991Sheppo  *
81991Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91991Sheppo  * or http://www.opensolaris.org/os/licensing.
101991Sheppo  * See the License for the specific language governing permissions
111991Sheppo  * and limitations under the License.
121991Sheppo  *
131991Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141991Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151991Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161991Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171991Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181991Sheppo  *
191991Sheppo  * CDDL HEADER END
201991Sheppo  */
211991Sheppo 
221991Sheppo /*
2312300SSriharsha.Basavapatna@Sun.COM  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
241991Sheppo  */
251991Sheppo 
261991Sheppo #include <sys/types.h>
271991Sheppo #include <sys/errno.h>
281991Sheppo #include <sys/debug.h>
291991Sheppo #include <sys/time.h>
301991Sheppo #include <sys/sysmacros.h>
311991Sheppo #include <sys/systm.h>
321991Sheppo #include <sys/user.h>
331991Sheppo #include <sys/stropts.h>
341991Sheppo #include <sys/stream.h>
351991Sheppo #include <sys/strlog.h>
361991Sheppo #include <sys/strsubr.h>
371991Sheppo #include <sys/cmn_err.h>
381991Sheppo #include <sys/cpu.h>
391991Sheppo #include <sys/kmem.h>
401991Sheppo #include <sys/conf.h>
411991Sheppo #include <sys/ddi.h>
421991Sheppo #include <sys/sunddi.h>
431991Sheppo #include <sys/ksynch.h>
441991Sheppo #include <sys/stat.h>
451991Sheppo #include <sys/kstat.h>
461991Sheppo #include <sys/vtrace.h>
471991Sheppo #include <sys/strsun.h>
481991Sheppo #include <sys/dlpi.h>
491991Sheppo #include <sys/ethernet.h>
501991Sheppo #include <net/if.h>
511991Sheppo #include <sys/varargs.h>
521991Sheppo #include <sys/machsystm.h>
531991Sheppo #include <sys/modctl.h>
541991Sheppo #include <sys/modhash.h>
558275SEric Cheng #include <sys/mac_provider.h>
562311Sseb #include <sys/mac_ether.h>
571991Sheppo #include <sys/taskq.h>
581991Sheppo #include <sys/note.h>
591991Sheppo #include <sys/mach_descrip.h>
608275SEric Cheng #include <sys/mac_provider.h>
611991Sheppo #include <sys/mdeg.h>
621991Sheppo #include <sys/ldc.h>
631991Sheppo #include <sys/vsw_fdb.h>
641991Sheppo #include <sys/vsw.h>
651991Sheppo #include <sys/vio_mailbox.h>
661991Sheppo #include <sys/vnet_mailbox.h>
671991Sheppo #include <sys/vnet_common.h>
682336Snarayan #include <sys/vio_util.h>
692336Snarayan #include <sys/sdt.h>
705171Ssb155480 #include <sys/atomic.h>
715373Sraghuram #include <sys/callb.h>
726419Ssb155480 #include <sys/vlan.h>
731991Sheppo 
741991Sheppo /*
751991Sheppo  * Function prototypes.
761991Sheppo  */
771991Sheppo static	int vsw_attach(dev_info_t *, ddi_attach_cmd_t);
781991Sheppo static	int vsw_detach(dev_info_t *, ddi_detach_cmd_t);
799217SWentao.Yang@Sun.COM static	int vsw_unattach(vsw_t *vswp);
803166Ssg70180 static	int vsw_get_md_physname(vsw_t *, md_t *, mde_cookie_t, char *);
818275SEric Cheng static	int vsw_get_md_smodes(vsw_t *, md_t *, mde_cookie_t, uint8_t *);
8210041SWentao.Yang@Sun.COM void vsw_destroy_rxpools(void *);
831991Sheppo 
841991Sheppo /* MDEG routines */
853166Ssg70180 static	int vsw_mdeg_register(vsw_t *vswp);
861991Sheppo static	void vsw_mdeg_unregister(vsw_t *vswp);
871991Sheppo static	int vsw_mdeg_cb(void *cb_argp, mdeg_result_t *);
883166Ssg70180 static	int vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *);
895171Ssb155480 static	int vsw_get_initial_md_properties(vsw_t *vswp, md_t *, mde_cookie_t);
906419Ssb155480 static	int vsw_read_mdprops(vsw_t *vswp);
916419Ssb155480 static	void vsw_vlan_read_ids(void *arg, int type, md_t *mdp,
928275SEric Cheng 	mde_cookie_t node, uint16_t *pvidp, vsw_vlanid_t **vidspp,
936419Ssb155480 	uint16_t *nvidsp, uint16_t *default_idp);
9410795SWentao.Yang@Sun.COM static	void vsw_port_read_bandwidth(vsw_port_t *portp, md_t *mdp,
9510795SWentao.Yang@Sun.COM 	mde_cookie_t node, uint64_t *bw);
966419Ssb155480 static	int vsw_port_read_props(vsw_port_t *portp, vsw_t *vswp,
976419Ssb155480 	md_t *mdp, mde_cookie_t *node);
985935Ssb155480 static	void vsw_read_pri_eth_types(vsw_t *vswp, md_t *mdp,
995935Ssb155480 	mde_cookie_t node);
1007529SSriharsha.Basavapatna@Sun.COM static	void vsw_mtu_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
1017529SSriharsha.Basavapatna@Sun.COM 	uint32_t *mtu);
1027529SSriharsha.Basavapatna@Sun.COM static	int vsw_mtu_update(vsw_t *vswp, uint32_t mtu);
1039336SSriharsha.Basavapatna@Sun.COM static	void vsw_linkprop_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
1049336SSriharsha.Basavapatna@Sun.COM 	boolean_t *pls);
10510795SWentao.Yang@Sun.COM static	void vsw_bandwidth_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
10610795SWentao.Yang@Sun.COM 	uint64_t *bw);
1073166Ssg70180 static	void vsw_update_md_prop(vsw_t *, md_t *, mde_cookie_t);
1085171Ssb155480 static void vsw_save_lmacaddr(vsw_t *vswp, uint64_t macaddr);
1098275SEric Cheng static boolean_t vsw_cmp_vids(vsw_vlanid_t *vids1,
1108275SEric Cheng 	vsw_vlanid_t *vids2, int nvids);
1111991Sheppo 
1125373Sraghuram /* Mac driver related routines */
1135373Sraghuram static int vsw_mac_register(vsw_t *);
1145373Sraghuram static int vsw_mac_unregister(vsw_t *);
1155373Sraghuram static int vsw_m_stat(void *, uint_t, uint64_t *);
1165373Sraghuram static void vsw_m_stop(void *arg);
1175373Sraghuram static int vsw_m_start(void *arg);
1185373Sraghuram static int vsw_m_unicst(void *arg, const uint8_t *);
1195373Sraghuram static int vsw_m_multicst(void *arg, boolean_t, const uint8_t *);
1205373Sraghuram static int vsw_m_promisc(void *arg, boolean_t);
1215373Sraghuram static mblk_t *vsw_m_tx(void *arg, mblk_t *);
1229819SSriharsha.Basavapatna@Sun.COM void vsw_mac_link_update(vsw_t *vswp, link_state_t link_state);
1235935Ssb155480 void vsw_mac_rx(vsw_t *vswp, mac_resource_handle_t mrh,
1245935Ssb155480     mblk_t *mp, vsw_macrx_flags_t flags);
1259336SSriharsha.Basavapatna@Sun.COM void vsw_physlink_state_update(vsw_t *vswp);
1261991Sheppo 
1275373Sraghuram /*
1285373Sraghuram  * Functions imported from other files.
1295373Sraghuram  */
1308370SSriharsha.Basavapatna@Sun.COM extern void vsw_setup_switching_thread(void *arg);
1318370SSriharsha.Basavapatna@Sun.COM extern int vsw_setup_switching_start(vsw_t *vswp);
1328370SSriharsha.Basavapatna@Sun.COM extern void vsw_setup_switching_stop(vsw_t *vswp);
1335373Sraghuram extern int vsw_setup_switching(vsw_t *);
1346537Swentaoy extern void vsw_switch_frame_nop(vsw_t *vswp, mblk_t *mp, int caller,
1356537Swentaoy     vsw_port_t *port, mac_resource_handle_t mrh);
1365373Sraghuram extern int vsw_add_mcst(vsw_t *, uint8_t, uint64_t, void *);
1375373Sraghuram extern int vsw_del_mcst(vsw_t *, uint8_t, uint64_t, void *);
1385373Sraghuram extern void vsw_del_mcst_vsw(vsw_t *);
1395373Sraghuram extern mcst_addr_t *vsw_del_addr(uint8_t devtype, void *arg, uint64_t addr);
1409217SWentao.Yang@Sun.COM extern void vsw_detach_ports(vsw_t *vswp);
1415373Sraghuram extern int vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node);
1425373Sraghuram extern int vsw_port_detach(vsw_t *vswp, int p_instance);
1436419Ssb155480 static int vsw_port_update(vsw_t *vswp, md_t *curr_mdp, mde_cookie_t curr_mdex,
1446419Ssb155480 	md_t *prev_mdp, mde_cookie_t prev_mdex);
1456419Ssb155480 extern	int vsw_port_attach(vsw_port_t *port);
1465373Sraghuram extern vsw_port_t *vsw_lookup_port(vsw_t *vswp, int p_instance);
1475373Sraghuram extern int vsw_mac_open(vsw_t *vswp);
1485373Sraghuram extern void vsw_mac_close(vsw_t *vswp);
1498275SEric Cheng extern void vsw_mac_cleanup_ports(vsw_t *vswp);
1505373Sraghuram extern void vsw_unset_addrs(vsw_t *vswp);
1519819SSriharsha.Basavapatna@Sun.COM extern void vsw_setup_switching_post_process(vsw_t *vswp);
1526419Ssb155480 extern void vsw_create_vlans(void *arg, int type);
1536419Ssb155480 extern void vsw_destroy_vlans(void *arg, int type);
1546419Ssb155480 extern void vsw_vlan_add_ids(void *arg, int type);
1556419Ssb155480 extern void vsw_vlan_remove_ids(void *arg, int type);
1566419Ssb155480 extern void vsw_vlan_unaware_port_reset(vsw_port_t *portp);
1576419Ssb155480 extern uint32_t vsw_vlan_frame_untag(void *arg, int type, mblk_t **np,
1586419Ssb155480 	mblk_t **npt);
1596419Ssb155480 extern mblk_t *vsw_vlan_frame_pretag(void *arg, int type, mblk_t *mp);
1606495Sspeer extern void vsw_hio_cleanup(vsw_t *vswp);
1618275SEric Cheng extern void vsw_hio_start_ports(vsw_t *vswp);
1628275SEric Cheng extern void vsw_hio_port_update(vsw_port_t *portp, boolean_t hio_enabled);
1638275SEric Cheng extern int vsw_mac_multicast_add(vsw_t *, vsw_port_t *, mcst_addr_t *, int);
1648275SEric Cheng extern void vsw_mac_multicast_remove(vsw_t *, vsw_port_t *, mcst_addr_t *, int);
1658275SEric Cheng extern void vsw_mac_port_reconfig_vlans(vsw_port_t *portp, uint16_t new_pvid,
1668275SEric Cheng     vsw_vlanid_t *new_vids, int new_nvids);
1678275SEric Cheng extern int vsw_mac_client_init(vsw_t *vswp, vsw_port_t *port, int type);
1688275SEric Cheng extern void vsw_mac_client_cleanup(vsw_t *vswp, vsw_port_t *port, int type);
1698275SEric Cheng extern void vsw_if_mac_reconfig(vsw_t *vswp, boolean_t update_vlans,
1708275SEric Cheng     uint16_t new_pvid, vsw_vlanid_t *new_vids, int new_nvids);
1717529SSriharsha.Basavapatna@Sun.COM extern void vsw_reset_ports(vsw_t *vswp);
1727529SSriharsha.Basavapatna@Sun.COM extern void vsw_port_reset(vsw_port_t *portp);
1739336SSriharsha.Basavapatna@Sun.COM extern void vsw_physlink_update_ports(vsw_t *vswp);
17410795SWentao.Yang@Sun.COM extern void vsw_update_bandwidth(vsw_t *vswp, vsw_port_t *port, int type,
17510795SWentao.Yang@Sun.COM     uint64_t maxbw);
1765373Sraghuram 
1775373Sraghuram /*
1785373Sraghuram  * Internal tunables.
1795373Sraghuram  */
1803297Ssb155480 int	vsw_num_handshakes = VNET_NUM_HANDSHAKES; /* # of handshake attempts */
1811991Sheppo int	vsw_wretries = 100;		/* # of write attempts */
1825171Ssb155480 int	vsw_setup_switching_delay = 3;	/* setup sw timeout interval in sec */
1836829Ssb155480 int	vsw_mac_open_retries = 300;	/* max # of mac_open() retries */
1846829Ssb155480 					/* 300*3 = 900sec(15min) of max tmout */
1855373Sraghuram int	vsw_ldc_tx_delay = 5;		/* delay(ticks) for tx retries */
1865373Sraghuram int	vsw_ldc_tx_retries = 10;	/* # of ldc tx retries */
1879217SWentao.Yang@Sun.COM int	vsw_ldc_retries = 5;		/* # of ldc_close() retries */
1889217SWentao.Yang@Sun.COM int	vsw_ldc_delay = 1000;		/* 1 ms delay for ldc_close() */
1895373Sraghuram boolean_t vsw_ldc_rxthr_enabled = B_TRUE;	/* LDC Rx thread enabled */
1905373Sraghuram boolean_t vsw_ldc_txthr_enabled = B_TRUE;	/* LDC Tx thread enabled */
19110041SWentao.Yang@Sun.COM int	vsw_rxpool_cleanup_delay = 100000;	/* 100ms */
19210041SWentao.Yang@Sun.COM 
1932336Snarayan 
1946419Ssb155480 uint32_t	vsw_fdb_nchains = 8;	/* # of chains in fdb hash table */
1956419Ssb155480 uint32_t	vsw_vlan_nchains = 4;	/* # of chains in vlan id hash table */
1966419Ssb155480 uint32_t	vsw_ethermtu = 1500;	/* mtu of the device */
1976419Ssb155480 
1986419Ssb155480 /* delay in usec to wait for all references on a fdb entry to be dropped */
1996419Ssb155480 uint32_t vsw_fdbe_refcnt_delay = 10;
2006419Ssb155480 
2016419Ssb155480 /*
2026419Ssb155480  * Default vlan id. This is only used internally when the "default-vlan-id"
2036419Ssb155480  * property is not present in the MD device node. Therefore, this should not be
2046419Ssb155480  * used as a tunable; if this value is changed, the corresponding variable
2056419Ssb155480  * should be updated to the same value in all vnets connected to this vsw.
2066419Ssb155480  */
2076419Ssb155480 uint16_t	vsw_default_vlan_id = 1;
2086419Ssb155480 
2095935Ssb155480 /*
2105935Ssb155480  * Workaround for a version handshake bug in obp's vnet.
2115935Ssb155480  * If vsw initiates version negotiation starting from the highest version,
2125935Ssb155480  * obp sends a nack and terminates version handshake. To workaround
2135935Ssb155480  * this, we do not initiate version handshake when the channel comes up.
2145935Ssb155480  * Instead, we wait for the peer to send its version info msg and go through
2155935Ssb155480  * the version protocol exchange. If we successfully negotiate a version,
2165935Ssb155480  * before sending the ack, we send our version info msg to the peer
2175935Ssb155480  * using the <major,minor> version that we are about to ack.
2185935Ssb155480  */
2195935Ssb155480 boolean_t vsw_obp_ver_proto_workaround = B_TRUE;
2205935Ssb155480 
2215935Ssb155480 /*
2225935Ssb155480  * In the absence of "priority-ether-types" property in MD, the following
2235935Ssb155480  * internal tunable can be set to specify a single priority ethertype.
2245935Ssb155480  */
2255935Ssb155480 uint64_t vsw_pri_eth_type = 0;
2265935Ssb155480 
2275935Ssb155480 /*
2285935Ssb155480  * Number of transmit priority buffers that are preallocated per device.
2295935Ssb155480  * This number is chosen to be a small value to throttle transmission
2305935Ssb155480  * of priority packets. Note: Must be a power of 2 for vio_create_mblks().
2315935Ssb155480  */
2325935Ssb155480 uint32_t vsw_pri_tx_nmblks = 64;
2335373Sraghuram 
2347027Ssb155480 /*
2357027Ssb155480  * Number of RARP packets sent to announce macaddr to the physical switch,
2367027Ssb155480  * after vsw's physical device is changed dynamically or after a guest (client
2377027Ssb155480  * vnet) is live migrated in.
2387027Ssb155480  */
2397027Ssb155480 uint32_t vsw_publish_macaddr_count = 3;
2407027Ssb155480 
24112011SSriharsha.Basavapatna@Sun.COM /*
24212011SSriharsha.Basavapatna@Sun.COM  * Enable/disable HybridIO
24312011SSriharsha.Basavapatna@Sun.COM  */
24412011SSriharsha.Basavapatna@Sun.COM boolean_t vsw_hio_enabled = B_TRUE;
24512011SSriharsha.Basavapatna@Sun.COM 
24612011SSriharsha.Basavapatna@Sun.COM /*
24712011SSriharsha.Basavapatna@Sun.COM  * Max retries for HybridIO cleanup
24812011SSriharsha.Basavapatna@Sun.COM  */
24912011SSriharsha.Basavapatna@Sun.COM int vsw_hio_max_cleanup_retries = 10;
25012011SSriharsha.Basavapatna@Sun.COM 
25112011SSriharsha.Basavapatna@Sun.COM /*
25212011SSriharsha.Basavapatna@Sun.COM  * 10ms delay for HybridIO cleanup
25312011SSriharsha.Basavapatna@Sun.COM  */
25412011SSriharsha.Basavapatna@Sun.COM int vsw_hio_cleanup_delay = 10000;
25512011SSriharsha.Basavapatna@Sun.COM 
25612011SSriharsha.Basavapatna@Sun.COM /*
25712011SSriharsha.Basavapatna@Sun.COM  * Descriptor ring modes of LDC data transfer:
25812011SSriharsha.Basavapatna@Sun.COM  *
25912011SSriharsha.Basavapatna@Sun.COM  * 1) TxDring mode:
26012011SSriharsha.Basavapatna@Sun.COM  * In versions < v1.6 of VIO Protocol, we support only TxDring mode. In this
26112011SSriharsha.Basavapatna@Sun.COM  * mode, we create a transmit descriptor ring and export it to the peer through
26212011SSriharsha.Basavapatna@Sun.COM  * dring registration process of handshake. The descriptor ring is exported
26312011SSriharsha.Basavapatna@Sun.COM  * using LDC shared memory. Each descriptor is associated with a data buffer.
26412011SSriharsha.Basavapatna@Sun.COM  * The data buffer is also exported over LDC and the cookies for this data
26512011SSriharsha.Basavapatna@Sun.COM  * buffer are provided in the descriptor. The peer maps this ring as its
26612011SSriharsha.Basavapatna@Sun.COM  * receive ring. Similarly, the peer exports a transmit descriptor ring which
26712011SSriharsha.Basavapatna@Sun.COM  * is mapped by this device as its receive ring. In this mode, in a given data
26812011SSriharsha.Basavapatna@Sun.COM  * transfer direction, the transmitter copies the data to the exported data
26912011SSriharsha.Basavapatna@Sun.COM  * buffer (owned by itself), bound to the descriptor. The receiver uses the LDC
27012011SSriharsha.Basavapatna@Sun.COM  * cookies specified in the descriptor to copy the data into the receiving
27112011SSriharsha.Basavapatna@Sun.COM  * guest through the hypervisor (ldc_mem_copy()).
27212011SSriharsha.Basavapatna@Sun.COM  *
27312011SSriharsha.Basavapatna@Sun.COM  * 2) RxDringData mode:
27412011SSriharsha.Basavapatna@Sun.COM  * In versions >= v1.6 of VIO Protocol, we also support RxDringData mode. In
27512011SSriharsha.Basavapatna@Sun.COM  * this mode, we create a receive descriptor ring and export it to the peer
27612011SSriharsha.Basavapatna@Sun.COM  * through dring registration process of handshake. In addition, we export a
27712011SSriharsha.Basavapatna@Sun.COM  * receive buffer area and provide that information also in the dring
27812011SSriharsha.Basavapatna@Sun.COM  * registration message. The descriptor ring and the data buffer area are
27912011SSriharsha.Basavapatna@Sun.COM  * exported using LDC shared memory. Each descriptor is associated with a data
28012011SSriharsha.Basavapatna@Sun.COM  * buffer in the data buffer area and the offset of the specific data buffer
28112011SSriharsha.Basavapatna@Sun.COM  * within this area is specified in the descriptor. The peer maps this ring
28212011SSriharsha.Basavapatna@Sun.COM  * along with the data buffer area as its transmit ring. Similarly, the peer
28312011SSriharsha.Basavapatna@Sun.COM  * exports a receive ring which is mapped by this device as its transmit ring,
28412011SSriharsha.Basavapatna@Sun.COM  * along with its buffer area. In this mode, in a given data transfer
28512011SSriharsha.Basavapatna@Sun.COM  * direction, the transmitter copies the data to the data buffer offset
28612011SSriharsha.Basavapatna@Sun.COM  * specified in the descriptor. The receiver simply picks up the data buffer
28712011SSriharsha.Basavapatna@Sun.COM  * (owned by itself) without any copy operation into the receiving guest.
28812011SSriharsha.Basavapatna@Sun.COM  *
289*13098SWentao.Yang@Sun.COM  * We enable RxDringData mode during handshake negotiations if LDC supports
290*13098SWentao.Yang@Sun.COM  * mapping in large areas of shared memory(see ldc_is_viotsb_configured() API),
291*13098SWentao.Yang@Sun.COM  * which is required to support RxDringData mode.
29212011SSriharsha.Basavapatna@Sun.COM  */
29312011SSriharsha.Basavapatna@Sun.COM 
29412011SSriharsha.Basavapatna@Sun.COM /*
29512011SSriharsha.Basavapatna@Sun.COM  * Number of descriptors;  must be power of 2.
29612011SSriharsha.Basavapatna@Sun.COM  */
29712011SSriharsha.Basavapatna@Sun.COM uint32_t vsw_num_descriptors = VSW_NUM_DESCRIPTORS;
29812011SSriharsha.Basavapatna@Sun.COM 
29912011SSriharsha.Basavapatna@Sun.COM /*
30012011SSriharsha.Basavapatna@Sun.COM  * In RxDringData mode, # of buffers is determined by multiplying the # of
30112011SSriharsha.Basavapatna@Sun.COM  * descriptors with the factor below. Note that the factor must be > 1; i.e,
30212011SSriharsha.Basavapatna@Sun.COM  * the # of buffers must always be > # of descriptors. This is needed because,
30312011SSriharsha.Basavapatna@Sun.COM  * while the shared memory buffers are sent up the stack on the receiver, the
30412011SSriharsha.Basavapatna@Sun.COM  * sender needs additional buffers that can be used for further transmits.
30512011SSriharsha.Basavapatna@Sun.COM  * See vsw_setup_rx_dring() for details.
30612011SSriharsha.Basavapatna@Sun.COM  */
30712011SSriharsha.Basavapatna@Sun.COM uint32_t vsw_nrbufs_factor = 2;
30812011SSriharsha.Basavapatna@Sun.COM 
30912011SSriharsha.Basavapatna@Sun.COM /*
31012011SSriharsha.Basavapatna@Sun.COM  * Delay when rx descr not ready; used in both dring modes.
31112011SSriharsha.Basavapatna@Sun.COM  */
31212011SSriharsha.Basavapatna@Sun.COM int	vsw_recv_delay = 0;
31312011SSriharsha.Basavapatna@Sun.COM 
31412011SSriharsha.Basavapatna@Sun.COM /*
31512011SSriharsha.Basavapatna@Sun.COM  * Retry when rx descr not ready; used in both dring modes.
31612011SSriharsha.Basavapatna@Sun.COM  */
31712011SSriharsha.Basavapatna@Sun.COM int	vsw_recv_retries = 5;
3185935Ssb155480 
3195373Sraghuram /*
3205373Sraghuram  * Max number of mblks received in one receive operation.
3215373Sraghuram  */
3225373Sraghuram uint32_t vsw_chain_len = (VSW_NUM_MBLKS * 0.6);
3235373Sraghuram 
3245373Sraghuram /*
3257529SSriharsha.Basavapatna@Sun.COM  * Internal tunables for receive buffer pools, that is,  the size and number of
3267529SSriharsha.Basavapatna@Sun.COM  * mblks for each pool. At least 3 sizes must be specified if these are used.
3277529SSriharsha.Basavapatna@Sun.COM  * The sizes must be specified in increasing order. Non-zero value of the first
3287529SSriharsha.Basavapatna@Sun.COM  * size will be used as a hint to use these values instead of the algorithm
32912011SSriharsha.Basavapatna@Sun.COM  * that determines the sizes based on MTU. Used in TxDring mode only.
3305373Sraghuram  */
3317529SSriharsha.Basavapatna@Sun.COM uint32_t vsw_mblk_size1 = 0;
3327529SSriharsha.Basavapatna@Sun.COM uint32_t vsw_mblk_size2 = 0;
3337529SSriharsha.Basavapatna@Sun.COM uint32_t vsw_mblk_size3 = 0;
3347529SSriharsha.Basavapatna@Sun.COM uint32_t vsw_mblk_size4 = 0;
3355373Sraghuram uint32_t vsw_num_mblks1 = VSW_NUM_MBLKS;	/* number of mblks for pool1 */
3365373Sraghuram uint32_t vsw_num_mblks2 = VSW_NUM_MBLKS;	/* number of mblks for pool2 */
3375373Sraghuram uint32_t vsw_num_mblks3 = VSW_NUM_MBLKS;	/* number of mblks for pool3 */
3387529SSriharsha.Basavapatna@Sun.COM uint32_t vsw_num_mblks4 = VSW_NUM_MBLKS;	/* number of mblks for pool4 */
3397529SSriharsha.Basavapatna@Sun.COM 
3407529SSriharsha.Basavapatna@Sun.COM /*
3417529SSriharsha.Basavapatna@Sun.COM  * Set this to non-zero to enable additional internal receive buffer pools
3427529SSriharsha.Basavapatna@Sun.COM  * based on the MTU of the device for better performance at the cost of more
3437529SSriharsha.Basavapatna@Sun.COM  * memory consumption. This is turned off by default, to use allocb(9F) for
3447529SSriharsha.Basavapatna@Sun.COM  * receive buffer allocations of sizes > 2K.
3457529SSriharsha.Basavapatna@Sun.COM  */
3467529SSriharsha.Basavapatna@Sun.COM boolean_t vsw_jumbo_rxpools = B_FALSE;
3475373Sraghuram 
3485373Sraghuram /*
3495935Ssb155480  * vsw_max_tx_qcount is the maximum # of packets that can be queued
3505935Ssb155480  * before the tx worker thread begins processing the queue. Its value
3515935Ssb155480  * is chosen to be 4x the default length of tx descriptor ring.
3525935Ssb155480  */
35312011SSriharsha.Basavapatna@Sun.COM uint32_t vsw_max_tx_qcount = 4 * VSW_NUM_DESCRIPTORS;
3545935Ssb155480 
3555935Ssb155480 /*
3565373Sraghuram  * MAC callbacks
3575373Sraghuram  */
3582311Sseb static	mac_callbacks_t	vsw_m_callbacks = {
3592311Sseb 	0,
3602311Sseb 	vsw_m_stat,
3612311Sseb 	vsw_m_start,
3622311Sseb 	vsw_m_stop,
3632311Sseb 	vsw_m_promisc,
3642311Sseb 	vsw_m_multicst,
3652311Sseb 	vsw_m_unicst,
36611878SVenu.Iyer@Sun.COM 	vsw_m_tx
3672311Sseb };
3682311Sseb 
3691991Sheppo static	struct	cb_ops	vsw_cb_ops = {
3701991Sheppo 	nulldev,			/* cb_open */
3711991Sheppo 	nulldev,			/* cb_close */
3721991Sheppo 	nodev,				/* cb_strategy */
3731991Sheppo 	nodev,				/* cb_print */
3741991Sheppo 	nodev,				/* cb_dump */
3751991Sheppo 	nodev,				/* cb_read */
3761991Sheppo 	nodev,				/* cb_write */
3771991Sheppo 	nodev,				/* cb_ioctl */
3781991Sheppo 	nodev,				/* cb_devmap */
3791991Sheppo 	nodev,				/* cb_mmap */
3801991Sheppo 	nodev,				/* cb_segmap */
3811991Sheppo 	nochpoll,			/* cb_chpoll */
3821991Sheppo 	ddi_prop_op,			/* cb_prop_op */
3831991Sheppo 	NULL,				/* cb_stream */
3841991Sheppo 	D_MP,				/* cb_flag */
3851991Sheppo 	CB_REV,				/* rev */
3861991Sheppo 	nodev,				/* int (*cb_aread)() */
3871991Sheppo 	nodev				/* int (*cb_awrite)() */
3881991Sheppo };
3891991Sheppo 
3901991Sheppo static	struct	dev_ops	vsw_ops = {
3911991Sheppo 	DEVO_REV,		/* devo_rev */
3921991Sheppo 	0,			/* devo_refcnt */
3937739SWentao.Yang@Sun.COM 	NULL,			/* devo_getinfo */
3941991Sheppo 	nulldev,		/* devo_identify */
3951991Sheppo 	nulldev,		/* devo_probe */
3961991Sheppo 	vsw_attach,		/* devo_attach */
3971991Sheppo 	vsw_detach,		/* devo_detach */
3981991Sheppo 	nodev,			/* devo_reset */
3991991Sheppo 	&vsw_cb_ops,		/* devo_cb_ops */
4001991Sheppo 	(struct bus_ops *)NULL,	/* devo_bus_ops */
4011991Sheppo 	ddi_power		/* devo_power */
4021991Sheppo };
4031991Sheppo 
4041991Sheppo extern	struct	mod_ops	mod_driverops;
4051991Sheppo static struct modldrv vswmodldrv = {
4061991Sheppo 	&mod_driverops,
4074838Slm66018 	"sun4v Virtual Switch",
4081991Sheppo 	&vsw_ops,
4091991Sheppo };
4101991Sheppo 
4111991Sheppo #define	LDC_ENTER_LOCK(ldcp)	\
4121991Sheppo 				mutex_enter(&((ldcp)->ldc_cblock));\
4135373Sraghuram 				mutex_enter(&((ldcp)->ldc_rxlock));\
4141991Sheppo 				mutex_enter(&((ldcp)->ldc_txlock));
4151991Sheppo #define	LDC_EXIT_LOCK(ldcp)	\
4161991Sheppo 				mutex_exit(&((ldcp)->ldc_txlock));\
4175373Sraghuram 				mutex_exit(&((ldcp)->ldc_rxlock));\
4181991Sheppo 				mutex_exit(&((ldcp)->ldc_cblock));
4191991Sheppo 
4201991Sheppo /* Driver soft state ptr  */
4211991Sheppo static void	*vsw_state;
4221991Sheppo 
4231991Sheppo /*
4241991Sheppo  * Linked list of "vsw_t" structures - one per instance.
4251991Sheppo  */
4261991Sheppo vsw_t		*vsw_head = NULL;
4271991Sheppo krwlock_t	vsw_rw;
4281991Sheppo 
4291991Sheppo /*
4301991Sheppo  * Property names
4311991Sheppo  */
4321991Sheppo static char vdev_propname[] = "virtual-device";
4331991Sheppo static char vsw_propname[] = "virtual-network-switch";
4341991Sheppo static char physdev_propname[] = "vsw-phys-dev";
4351991Sheppo static char smode_propname[] = "vsw-switch-mode";
4361991Sheppo static char macaddr_propname[] = "local-mac-address";
4371991Sheppo static char remaddr_propname[] = "remote-mac-address";
4381991Sheppo static char ldcids_propname[] = "ldc-ids";
4391991Sheppo static char chan_propname[] = "channel-endpoint";
4401991Sheppo static char id_propname[] = "id";
4411991Sheppo static char reg_propname[] = "reg";
4425935Ssb155480 static char pri_types_propname[] = "priority-ether-types";
4436419Ssb155480 static char vsw_pvid_propname[] = "port-vlan-id";
4446419Ssb155480 static char vsw_vid_propname[] = "vlan-id";
4456419Ssb155480 static char vsw_dvid_propname[] = "default-vlan-id";
4466419Ssb155480 static char port_pvid_propname[] = "remote-port-vlan-id";
4476419Ssb155480 static char port_vid_propname[] = "remote-vlan-id";
4486495Sspeer static char hybrid_propname[] = "hybrid";
4497529SSriharsha.Basavapatna@Sun.COM static char vsw_mtu_propname[] = "mtu";
4509336SSriharsha.Basavapatna@Sun.COM static char vsw_linkprop_propname[] = "linkprop";
45110795SWentao.Yang@Sun.COM static char vsw_maxbw_propname[] = "maxbw";
45210795SWentao.Yang@Sun.COM static char port_maxbw_propname[] = "maxbw";
4531991Sheppo 
4541991Sheppo /*
4551991Sheppo  * Matching criteria passed to the MDEG to register interest
4561991Sheppo  * in changes to 'virtual-device-port' nodes identified by their
4571991Sheppo  * 'id' property.
4581991Sheppo  */
4591991Sheppo static md_prop_match_t vport_prop_match[] = {
4601991Sheppo 	{ MDET_PROP_VAL,    "id"   },
4611991Sheppo 	{ MDET_LIST_END,    NULL    }
4621991Sheppo };
4631991Sheppo 
4641991Sheppo static mdeg_node_match_t vport_match = { "virtual-device-port",
4651991Sheppo 						vport_prop_match };
4661991Sheppo 
4671991Sheppo /*
4683166Ssg70180  * Matching criteria passed to the MDEG to register interest
4693166Ssg70180  * in changes to 'virtual-device' nodes (i.e. vsw nodes) identified
4703166Ssg70180  * by their 'name' and 'cfg-handle' properties.
4713166Ssg70180  */
4723166Ssg70180 static md_prop_match_t vdev_prop_match[] = {
4733166Ssg70180 	{ MDET_PROP_STR,    "name"   },
4743166Ssg70180 	{ MDET_PROP_VAL,    "cfg-handle" },
4753166Ssg70180 	{ MDET_LIST_END,    NULL    }
4763166Ssg70180 };
4773166Ssg70180 
4783166Ssg70180 static mdeg_node_match_t vdev_match = { "virtual-device",
4793166Ssg70180 						vdev_prop_match };
4803166Ssg70180 
4813166Ssg70180 
4823166Ssg70180 /*
4831991Sheppo  * Specification of an MD node passed to the MDEG to filter any
4841991Sheppo  * 'vport' nodes that do not belong to the specified node. This
4851991Sheppo  * template is copied for each vsw instance and filled in with
4861991Sheppo  * the appropriate 'cfg-handle' value before being passed to the MDEG.
4871991Sheppo  */
4881991Sheppo static mdeg_prop_spec_t vsw_prop_template[] = {
4891991Sheppo 	{ MDET_PROP_STR,    "name",		vsw_propname },
4901991Sheppo 	{ MDET_PROP_VAL,    "cfg-handle",	NULL	},
4911991Sheppo 	{ MDET_LIST_END,    NULL,		NULL	}
4921991Sheppo };
4931991Sheppo 
4941991Sheppo #define	VSW_SET_MDEG_PROP_INST(specp, val)	(specp)[1].ps_val = (val);
4951991Sheppo 
4965373Sraghuram #ifdef	DEBUG
4972748Slm66018 /*
4981991Sheppo  * Print debug messages - set to 0x1f to enable all msgs
4991991Sheppo  * or 0x0 to turn all off.
5001991Sheppo  */
5011991Sheppo int vswdbg = 0x0;
5021991Sheppo 
5031991Sheppo /*
5041991Sheppo  * debug levels:
5051991Sheppo  * 0x01:	Function entry/exit tracing
5061991Sheppo  * 0x02:	Internal function messages
5071991Sheppo  * 0x04:	Verbose internal messages
5081991Sheppo  * 0x08:	Warning messages
5091991Sheppo  * 0x10:	Error messages
5101991Sheppo  */
5111991Sheppo 
5125373Sraghuram void
vswdebug(vsw_t * vswp,const char * fmt,...)5131991Sheppo vswdebug(vsw_t *vswp, const char *fmt, ...)
5141991Sheppo {
5151991Sheppo 	char buf[512];
5161991Sheppo 	va_list ap;
5171991Sheppo 
5181991Sheppo 	va_start(ap, fmt);
5191991Sheppo 	(void) vsprintf(buf, fmt, ap);
5201991Sheppo 	va_end(ap);
5211991Sheppo 
5221991Sheppo 	if (vswp == NULL)
5231991Sheppo 		cmn_err(CE_CONT, "%s\n", buf);
5241991Sheppo 	else
5251991Sheppo 		cmn_err(CE_CONT, "vsw%d: %s\n", vswp->instance, buf);
5261991Sheppo }
5271991Sheppo 
5281991Sheppo #endif	/* DEBUG */
5291991Sheppo 
5301991Sheppo static struct modlinkage modlinkage = {
5311991Sheppo 	MODREV_1,
5321991Sheppo 	&vswmodldrv,
5331991Sheppo 	NULL
5341991Sheppo };
5351991Sheppo 
5361991Sheppo int
_init(void)5371991Sheppo _init(void)
5381991Sheppo {
5391991Sheppo 	int status;
5401991Sheppo 
5411991Sheppo 	rw_init(&vsw_rw, NULL, RW_DRIVER, NULL);
5421991Sheppo 
5431991Sheppo 	status = ddi_soft_state_init(&vsw_state, sizeof (vsw_t), 1);
5441991Sheppo 	if (status != 0) {
5451991Sheppo 		return (status);
5461991Sheppo 	}
5471991Sheppo 
5485373Sraghuram 	mac_init_ops(&vsw_ops, DRV_NAME);
5491991Sheppo 	status = mod_install(&modlinkage);
5501991Sheppo 	if (status != 0) {
5511991Sheppo 		ddi_soft_state_fini(&vsw_state);
5521991Sheppo 	}
5531991Sheppo 	return (status);
5541991Sheppo }
5551991Sheppo 
5561991Sheppo int
_fini(void)5571991Sheppo _fini(void)
5581991Sheppo {
5591991Sheppo 	int status;
5601991Sheppo 
5611991Sheppo 	status = mod_remove(&modlinkage);
5621991Sheppo 	if (status != 0)
5631991Sheppo 		return (status);
5641991Sheppo 	mac_fini_ops(&vsw_ops);
5651991Sheppo 	ddi_soft_state_fini(&vsw_state);
5661991Sheppo 
5671991Sheppo 	rw_destroy(&vsw_rw);
5681991Sheppo 
5691991Sheppo 	return (status);
5701991Sheppo }
5711991Sheppo 
5721991Sheppo int
_info(struct modinfo * modinfop)5731991Sheppo _info(struct modinfo *modinfop)
5741991Sheppo {
5751991Sheppo 	return (mod_info(&modlinkage, modinfop));
5761991Sheppo }
5771991Sheppo 
5781991Sheppo static int
vsw_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)5791991Sheppo vsw_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5801991Sheppo {
5819217SWentao.Yang@Sun.COM 	vsw_t			*vswp;
5829217SWentao.Yang@Sun.COM 	int			instance;
5839217SWentao.Yang@Sun.COM 	char			hashname[MAXNAMELEN];
5849217SWentao.Yang@Sun.COM 	char			qname[TASKQ_NAMELEN];
5859217SWentao.Yang@Sun.COM 	vsw_attach_progress_t	progress = PROG_init;
5869217SWentao.Yang@Sun.COM 	int			rv;
5871991Sheppo 
5881991Sheppo 	switch (cmd) {
5891991Sheppo 	case DDI_ATTACH:
5901991Sheppo 		break;
5911991Sheppo 	case DDI_RESUME:
5921991Sheppo 		/* nothing to do for this non-device */
5931991Sheppo 		return (DDI_SUCCESS);
5941991Sheppo 	case DDI_PM_RESUME:
5951991Sheppo 	default:
5961991Sheppo 		return (DDI_FAILURE);
5971991Sheppo 	}
5981991Sheppo 
5991991Sheppo 	instance = ddi_get_instance(dip);
6001991Sheppo 	if (ddi_soft_state_zalloc(vsw_state, instance) != DDI_SUCCESS) {
6011991Sheppo 		DERR(NULL, "vsw%d: ddi_soft_state_zalloc failed", instance);
6021991Sheppo 		return (DDI_FAILURE);
6031991Sheppo 	}
6041991Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
6051991Sheppo 
6061991Sheppo 	if (vswp == NULL) {
6071991Sheppo 		DERR(NULL, "vsw%d: ddi_get_soft_state failed", instance);
6081991Sheppo 		goto vsw_attach_fail;
6091991Sheppo 	}
6101991Sheppo 
6111991Sheppo 	vswp->dip = dip;
6121991Sheppo 	vswp->instance = instance;
6139336SSriharsha.Basavapatna@Sun.COM 	vswp->phys_link_state = LINK_STATE_UNKNOWN;
6141991Sheppo 	ddi_set_driver_private(dip, (caddr_t)vswp);
6151991Sheppo 
6168275SEric Cheng 	mutex_init(&vswp->mac_lock, NULL, MUTEX_DRIVER, NULL);
6175171Ssb155480 	mutex_init(&vswp->mca_lock, NULL, MUTEX_DRIVER, NULL);
6188370SSriharsha.Basavapatna@Sun.COM 	mutex_init(&vswp->sw_thr_lock, NULL, MUTEX_DRIVER, NULL);
6198370SSriharsha.Basavapatna@Sun.COM 	cv_init(&vswp->sw_thr_cv, NULL, CV_DRIVER, NULL);
6208275SEric Cheng 	rw_init(&vswp->maccl_rwlock, NULL, RW_DRIVER, NULL);
6211991Sheppo 	rw_init(&vswp->if_lockrw, NULL, RW_DRIVER, NULL);
6225171Ssb155480 	rw_init(&vswp->mfdbrw, NULL, RW_DRIVER, NULL);
6235171Ssb155480 	rw_init(&vswp->plist.lockrw, NULL, RW_DRIVER, NULL);
6245171Ssb155480 
6255171Ssb155480 	progress |= PROG_locks;
6265171Ssb155480 
6275171Ssb155480 	rv = vsw_read_mdprops(vswp);
6285171Ssb155480 	if (rv != 0)
6295171Ssb155480 		goto vsw_attach_fail;
6305171Ssb155480 
6315171Ssb155480 	progress |= PROG_readmd;
6321991Sheppo 
6331991Sheppo 	/* setup the unicast forwarding database  */
6341991Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_unicst_table-%d",
6354838Slm66018 	    vswp->instance);
6361991Sheppo 	D2(vswp, "creating unicast hash table (%s)...", hashname);
6376419Ssb155480 	vswp->fdb_nchains = vsw_fdb_nchains;
6386419Ssb155480 	vswp->fdb_hashp = mod_hash_create_ptrhash(hashname, vswp->fdb_nchains,
6394838Slm66018 	    mod_hash_null_valdtor, sizeof (void *));
6406419Ssb155480 	vsw_create_vlans((void *)vswp, VSW_LOCALDEV);
6411991Sheppo 	progress |= PROG_fdb;
6421991Sheppo 
6431991Sheppo 	/* setup the multicast fowarding database */
6441991Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_mcst_table-%d",
6454838Slm66018 	    vswp->instance);
6461991Sheppo 	D2(vswp, "creating multicast hash table %s)...", hashname);
6476419Ssb155480 	vswp->mfdb = mod_hash_create_ptrhash(hashname, vsw_fdb_nchains,
6484838Slm66018 	    mod_hash_null_valdtor, sizeof (void *));
6491991Sheppo 
6501991Sheppo 	progress |= PROG_mfdb;
6511991Sheppo 
6521991Sheppo 	/*
6531991Sheppo 	 * Create the taskq which will process all the VIO
6541991Sheppo 	 * control messages.
6551991Sheppo 	 */
65612011SSriharsha.Basavapatna@Sun.COM 	(void) snprintf(qname, TASKQ_NAMELEN, "taskq%d", vswp->instance);
6571991Sheppo 	if ((vswp->taskq_p = ddi_taskq_create(vswp->dip, qname, 1,
6584838Slm66018 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
6593166Ssg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to create task queue",
6604838Slm66018 		    vswp->instance);
6611991Sheppo 		goto vsw_attach_fail;
6621991Sheppo 	}
6631991Sheppo 
6641991Sheppo 	progress |= PROG_taskq;
6651991Sheppo 
66612011SSriharsha.Basavapatna@Sun.COM 	(void) snprintf(qname, TASKQ_NAMELEN, "rxpool_taskq%d",
66710041SWentao.Yang@Sun.COM 	    vswp->instance);
66810041SWentao.Yang@Sun.COM 	if ((vswp->rxp_taskq = ddi_taskq_create(vswp->dip, qname, 1,
66910041SWentao.Yang@Sun.COM 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
67010041SWentao.Yang@Sun.COM 		cmn_err(CE_WARN, "!vsw%d: Unable to create rxp task queue",
67110041SWentao.Yang@Sun.COM 		    vswp->instance);
67210041SWentao.Yang@Sun.COM 		goto vsw_attach_fail;
67310041SWentao.Yang@Sun.COM 	}
67410041SWentao.Yang@Sun.COM 
67510041SWentao.Yang@Sun.COM 	progress |= PROG_rxp_taskq;
67610041SWentao.Yang@Sun.COM 
6772336Snarayan 	/* prevent auto-detaching */
6782336Snarayan 	if (ddi_prop_update_int(DDI_DEV_T_NONE, vswp->dip,
6794838Slm66018 	    DDI_NO_AUTODETACH, 1) != DDI_SUCCESS) {
6803166Ssg70180 		cmn_err(CE_NOTE, "!Unable to set \"%s\" property for "
6814838Slm66018 		    "instance %u", DDI_NO_AUTODETACH, instance);
6822336Snarayan 	}
6832336Snarayan 
6841991Sheppo 	/*
6856537Swentaoy 	 * The null switching function is set to avoid panic until
6866537Swentaoy 	 * switch mode is setup.
6876537Swentaoy 	 */
6886537Swentaoy 	vswp->vsw_switch_frame = vsw_switch_frame_nop;
6896537Swentaoy 
6906537Swentaoy 	/*
6918370SSriharsha.Basavapatna@Sun.COM 	 * Setup the required switching mode, based on the mdprops that we read
6928370SSriharsha.Basavapatna@Sun.COM 	 * earlier. We start a thread to do this, to avoid calling mac_open()
6938370SSriharsha.Basavapatna@Sun.COM 	 * directly from attach().
6945171Ssb155480 	 */
6958370SSriharsha.Basavapatna@Sun.COM 	rv = vsw_setup_switching_start(vswp);
6968370SSriharsha.Basavapatna@Sun.COM 	if (rv != 0) {
6978370SSriharsha.Basavapatna@Sun.COM 		goto vsw_attach_fail;
6988370SSriharsha.Basavapatna@Sun.COM 	}
6995171Ssb155480 
7005171Ssb155480 	progress |= PROG_swmode;
7015171Ssb155480 
7025171Ssb155480 	/* Register with mac layer as a provider */
7035171Ssb155480 	rv = vsw_mac_register(vswp);
7045171Ssb155480 	if (rv != 0)
7055171Ssb155480 		goto vsw_attach_fail;
7065171Ssb155480 
7075171Ssb155480 	progress |= PROG_macreg;
7085171Ssb155480 
7095171Ssb155480 	/*
7103166Ssg70180 	 * Now we have everything setup, register an interest in
7113166Ssg70180 	 * specific MD nodes.
7123166Ssg70180 	 *
7133166Ssg70180 	 * The callback is invoked in 2 cases, firstly if upon mdeg
7143166Ssg70180 	 * registration there are existing nodes which match our specified
7153166Ssg70180 	 * criteria, and secondly if the MD is changed (and again, there
7163166Ssg70180 	 * are nodes which we are interested in present within it. Note
7173166Ssg70180 	 * that our callback will be invoked even if our specified nodes
7183166Ssg70180 	 * have not actually changed).
7193166Ssg70180 	 *
7205171Ssb155480 	 */
7215171Ssb155480 	rv = vsw_mdeg_register(vswp);
7225171Ssb155480 	if (rv != 0)
7233166Ssg70180 		goto vsw_attach_fail;
7241991Sheppo 
7255171Ssb155480 	progress |= PROG_mdreg;
7265171Ssb155480 
7279217SWentao.Yang@Sun.COM 	vswp->attach_progress = progress;
7289217SWentao.Yang@Sun.COM 
7295171Ssb155480 	WRITE_ENTER(&vsw_rw);
7305171Ssb155480 	vswp->next = vsw_head;
7315171Ssb155480 	vsw_head = vswp;
7325171Ssb155480 	RW_EXIT(&vsw_rw);
7335171Ssb155480 
7345171Ssb155480 	ddi_report_dev(vswp->dip);
7351991Sheppo 	return (DDI_SUCCESS);
7361991Sheppo 
7371991Sheppo vsw_attach_fail:
7381991Sheppo 	DERR(NULL, "vsw_attach: failed");
7391991Sheppo 
7409217SWentao.Yang@Sun.COM 	vswp->attach_progress = progress;
7419217SWentao.Yang@Sun.COM 	(void) vsw_unattach(vswp);
7421991Sheppo 	ddi_soft_state_free(vsw_state, instance);
7431991Sheppo 	return (DDI_FAILURE);
7441991Sheppo }
7451991Sheppo 
7461991Sheppo static int
vsw_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)7471991Sheppo vsw_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
7481991Sheppo {
7492336Snarayan 	vsw_t			**vswpp, *vswp;
7502336Snarayan 	int 			instance;
7511991Sheppo 
7521991Sheppo 	instance = ddi_get_instance(dip);
7531991Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
7541991Sheppo 
7551991Sheppo 	if (vswp == NULL) {
7561991Sheppo 		return (DDI_FAILURE);
7571991Sheppo 	}
7581991Sheppo 
7591991Sheppo 	switch (cmd) {
7601991Sheppo 	case DDI_DETACH:
7611991Sheppo 		break;
7621991Sheppo 	case DDI_SUSPEND:
7631991Sheppo 	case DDI_PM_SUSPEND:
7641991Sheppo 	default:
7651991Sheppo 		return (DDI_FAILURE);
7661991Sheppo 	}
7671991Sheppo 
7681991Sheppo 	D2(vswp, "detaching instance %d", instance);
7691991Sheppo 
7709217SWentao.Yang@Sun.COM 	if (vsw_unattach(vswp) != 0) {
7711991Sheppo 		return (DDI_FAILURE);
7721991Sheppo 	}
7731991Sheppo 
7741991Sheppo 	ddi_remove_minor_node(dip, NULL);
7751991Sheppo 
7761991Sheppo 	WRITE_ENTER(&vsw_rw);
7771991Sheppo 	for (vswpp = &vsw_head; *vswpp; vswpp = &(*vswpp)->next) {
7781991Sheppo 		if (*vswpp == vswp) {
7791991Sheppo 			*vswpp = vswp->next;
7801991Sheppo 			break;
7811991Sheppo 		}
7821991Sheppo 	}
7831991Sheppo 	RW_EXIT(&vsw_rw);
7849217SWentao.Yang@Sun.COM 
7851991Sheppo 	ddi_soft_state_free(vsw_state, instance);
7861991Sheppo 
7871991Sheppo 	return (DDI_SUCCESS);
7881991Sheppo }
7891991Sheppo 
7901991Sheppo /*
7919217SWentao.Yang@Sun.COM  * Common routine to handle vsw_attach() failure and vsw_detach(). Note that
7929217SWentao.Yang@Sun.COM  * the only reason this function could fail is if mac_unregister() fails.
7939217SWentao.Yang@Sun.COM  * Otherwise, this function must ensure that all resources are freed and return
7949217SWentao.Yang@Sun.COM  * success.
7959217SWentao.Yang@Sun.COM  */
7969217SWentao.Yang@Sun.COM static int
vsw_unattach(vsw_t * vswp)7979217SWentao.Yang@Sun.COM vsw_unattach(vsw_t *vswp)
7989217SWentao.Yang@Sun.COM {
7999217SWentao.Yang@Sun.COM 	vsw_attach_progress_t	progress;
8009217SWentao.Yang@Sun.COM 
8019217SWentao.Yang@Sun.COM 	progress = vswp->attach_progress;
8029217SWentao.Yang@Sun.COM 
8039217SWentao.Yang@Sun.COM 	/*
8049217SWentao.Yang@Sun.COM 	 * Unregister from the gldv3 subsystem. This can fail, in particular
8059217SWentao.Yang@Sun.COM 	 * if there are still any open references to this mac device; in which
8069217SWentao.Yang@Sun.COM 	 * case we just return failure without continuing to detach further.
8079217SWentao.Yang@Sun.COM 	 */
8089217SWentao.Yang@Sun.COM 	if (progress & PROG_macreg) {
8099217SWentao.Yang@Sun.COM 		if (vsw_mac_unregister(vswp) != 0) {
8109217SWentao.Yang@Sun.COM 			cmn_err(CE_WARN, "!vsw%d: Unable to detach from "
8119217SWentao.Yang@Sun.COM 			    "MAC layer", vswp->instance);
8129217SWentao.Yang@Sun.COM 			return (1);
8139217SWentao.Yang@Sun.COM 		}
8149217SWentao.Yang@Sun.COM 		progress &= ~PROG_macreg;
8159217SWentao.Yang@Sun.COM 	}
8169217SWentao.Yang@Sun.COM 
8179217SWentao.Yang@Sun.COM 	/*
8189217SWentao.Yang@Sun.COM 	 * Now that we have unregistered from gldv3, we must finish all other
8199217SWentao.Yang@Sun.COM 	 * steps and successfully return from this function; otherwise we will
8209217SWentao.Yang@Sun.COM 	 * end up leaving the device in a broken/unusable state.
8219217SWentao.Yang@Sun.COM 	 *
8229217SWentao.Yang@Sun.COM 	 * If we have registered with mdeg, unregister now to stop further
8239217SWentao.Yang@Sun.COM 	 * callbacks to this vsw device and/or its ports. Then, detach any
8249217SWentao.Yang@Sun.COM 	 * existing ports.
8259217SWentao.Yang@Sun.COM 	 */
8269217SWentao.Yang@Sun.COM 	if (progress & PROG_mdreg) {
8279217SWentao.Yang@Sun.COM 		vsw_mdeg_unregister(vswp);
8289217SWentao.Yang@Sun.COM 		vsw_detach_ports(vswp);
8299217SWentao.Yang@Sun.COM 		progress &= ~PROG_mdreg;
8309217SWentao.Yang@Sun.COM 	}
8319217SWentao.Yang@Sun.COM 
8329217SWentao.Yang@Sun.COM 	/*
8339217SWentao.Yang@Sun.COM 	 * If we have started a thread to setup the switching mode, stop it, if
8349217SWentao.Yang@Sun.COM 	 * it is still running. If it has finished setting up the switching
8359217SWentao.Yang@Sun.COM 	 * mode, then we need to clean up some additional things if we are
8369217SWentao.Yang@Sun.COM 	 * running in L2 mode: first free up any hybrid resources; then stop
8379217SWentao.Yang@Sun.COM 	 * and close the underlying physical device. Note that we would have
8389217SWentao.Yang@Sun.COM 	 * already released all per mac_client resources (ucast, mcast addrs,
8399217SWentao.Yang@Sun.COM 	 * hio-shares etc) as all the ports are detached and if the vsw device
8409217SWentao.Yang@Sun.COM 	 * itself was in use as an interface, it has been unplumbed (otherwise
8419217SWentao.Yang@Sun.COM 	 * mac_unregister() above would fail).
8429217SWentao.Yang@Sun.COM 	 */
8439217SWentao.Yang@Sun.COM 	if (progress & PROG_swmode) {
8449217SWentao.Yang@Sun.COM 
8459217SWentao.Yang@Sun.COM 		vsw_setup_switching_stop(vswp);
8469217SWentao.Yang@Sun.COM 
8479217SWentao.Yang@Sun.COM 		if (vswp->hio_capable == B_TRUE) {
8489217SWentao.Yang@Sun.COM 			vsw_hio_cleanup(vswp);
8499217SWentao.Yang@Sun.COM 			vswp->hio_capable = B_FALSE;
8509217SWentao.Yang@Sun.COM 		}
8519217SWentao.Yang@Sun.COM 
8529217SWentao.Yang@Sun.COM 		mutex_enter(&vswp->mac_lock);
8539217SWentao.Yang@Sun.COM 		vsw_mac_close(vswp);
8549217SWentao.Yang@Sun.COM 		mutex_exit(&vswp->mac_lock);
8559217SWentao.Yang@Sun.COM 
8569217SWentao.Yang@Sun.COM 		progress &= ~PROG_swmode;
8579217SWentao.Yang@Sun.COM 	}
8589217SWentao.Yang@Sun.COM 
8599217SWentao.Yang@Sun.COM 	/*
86010041SWentao.Yang@Sun.COM 	 * We now destroy the taskq used to clean up rx mblk pools that
86110041SWentao.Yang@Sun.COM 	 * couldn't be destroyed when the ports/channels were detached.
86210041SWentao.Yang@Sun.COM 	 * We implicitly wait for those tasks to complete in
86310041SWentao.Yang@Sun.COM 	 * ddi_taskq_destroy().
86410041SWentao.Yang@Sun.COM 	 */
86510041SWentao.Yang@Sun.COM 	if (progress & PROG_rxp_taskq) {
86610041SWentao.Yang@Sun.COM 		ddi_taskq_destroy(vswp->rxp_taskq);
86710041SWentao.Yang@Sun.COM 		progress &= ~PROG_rxp_taskq;
86810041SWentao.Yang@Sun.COM 	}
86910041SWentao.Yang@Sun.COM 
87010041SWentao.Yang@Sun.COM 	/*
8719217SWentao.Yang@Sun.COM 	 * By now any pending tasks have finished and the underlying
8729217SWentao.Yang@Sun.COM 	 * ldc's have been destroyed, so its safe to delete the control
8739217SWentao.Yang@Sun.COM 	 * message taskq.
8749217SWentao.Yang@Sun.COM 	 */
8759217SWentao.Yang@Sun.COM 	if (progress & PROG_taskq) {
8769217SWentao.Yang@Sun.COM 		ddi_taskq_destroy(vswp->taskq_p);
8779217SWentao.Yang@Sun.COM 		progress &= ~PROG_taskq;
8789217SWentao.Yang@Sun.COM 	}
8799217SWentao.Yang@Sun.COM 
8809217SWentao.Yang@Sun.COM 	/* Destroy the multicast hash table */
8819217SWentao.Yang@Sun.COM 	if (progress & PROG_mfdb) {
8829217SWentao.Yang@Sun.COM 		mod_hash_destroy_hash(vswp->mfdb);
8839217SWentao.Yang@Sun.COM 		progress &= ~PROG_mfdb;
8849217SWentao.Yang@Sun.COM 	}
8859217SWentao.Yang@Sun.COM 
8869217SWentao.Yang@Sun.COM 	/* Destroy the vlan hash table and fdb */
8879217SWentao.Yang@Sun.COM 	if (progress & PROG_fdb) {
8889217SWentao.Yang@Sun.COM 		vsw_destroy_vlans(vswp, VSW_LOCALDEV);
8899217SWentao.Yang@Sun.COM 		mod_hash_destroy_hash(vswp->fdb_hashp);
8909217SWentao.Yang@Sun.COM 		progress &= ~PROG_fdb;
8919217SWentao.Yang@Sun.COM 	}
8929217SWentao.Yang@Sun.COM 
8939217SWentao.Yang@Sun.COM 	if (progress & PROG_readmd) {
8949217SWentao.Yang@Sun.COM 		if (VSW_PRI_ETH_DEFINED(vswp)) {
8959217SWentao.Yang@Sun.COM 			kmem_free(vswp->pri_types,
8969217SWentao.Yang@Sun.COM 			    sizeof (uint16_t) * vswp->pri_num_types);
8979217SWentao.Yang@Sun.COM 			(void) vio_destroy_mblks(vswp->pri_tx_vmp);
8989217SWentao.Yang@Sun.COM 		}
8999217SWentao.Yang@Sun.COM 		progress &= ~PROG_readmd;
9009217SWentao.Yang@Sun.COM 	}
9019217SWentao.Yang@Sun.COM 
9029217SWentao.Yang@Sun.COM 	if (progress & PROG_locks) {
9039217SWentao.Yang@Sun.COM 		rw_destroy(&vswp->plist.lockrw);
9049217SWentao.Yang@Sun.COM 		rw_destroy(&vswp->mfdbrw);
9059217SWentao.Yang@Sun.COM 		rw_destroy(&vswp->if_lockrw);
9069217SWentao.Yang@Sun.COM 		rw_destroy(&vswp->maccl_rwlock);
9079217SWentao.Yang@Sun.COM 		cv_destroy(&vswp->sw_thr_cv);
9089217SWentao.Yang@Sun.COM 		mutex_destroy(&vswp->sw_thr_lock);
9099217SWentao.Yang@Sun.COM 		mutex_destroy(&vswp->mca_lock);
9109217SWentao.Yang@Sun.COM 		mutex_destroy(&vswp->mac_lock);
9119217SWentao.Yang@Sun.COM 		progress &= ~PROG_locks;
9129217SWentao.Yang@Sun.COM 	}
9139217SWentao.Yang@Sun.COM 
9149217SWentao.Yang@Sun.COM 	vswp->attach_progress = progress;
9159217SWentao.Yang@Sun.COM 
9169217SWentao.Yang@Sun.COM 	return (0);
9179217SWentao.Yang@Sun.COM }
9189217SWentao.Yang@Sun.COM 
91910041SWentao.Yang@Sun.COM void
vsw_destroy_rxpools(void * arg)92010041SWentao.Yang@Sun.COM vsw_destroy_rxpools(void *arg)
9219217SWentao.Yang@Sun.COM {
92210041SWentao.Yang@Sun.COM 	vio_mblk_pool_t	*poolp = (vio_mblk_pool_t *)arg;
92310041SWentao.Yang@Sun.COM 	vio_mblk_pool_t	*npoolp;
92410041SWentao.Yang@Sun.COM 
9259217SWentao.Yang@Sun.COM 	while (poolp != NULL) {
92610041SWentao.Yang@Sun.COM 		npoolp =  poolp->nextp;
92710041SWentao.Yang@Sun.COM 		while (vio_destroy_mblks(poolp) != 0) {
92812300SSriharsha.Basavapatna@Sun.COM 			delay(drv_usectohz(vsw_rxpool_cleanup_delay));
9299217SWentao.Yang@Sun.COM 		}
9309217SWentao.Yang@Sun.COM 		poolp = npoolp;
9319217SWentao.Yang@Sun.COM 	}
9329217SWentao.Yang@Sun.COM }
9339217SWentao.Yang@Sun.COM 
9349217SWentao.Yang@Sun.COM /*
9353166Ssg70180  * Get the value of the "vsw-phys-dev" property in the specified
9363166Ssg70180  * node. This property is the name of the physical device that
9373166Ssg70180  * the virtual switch will use to talk to the outside world.
9383166Ssg70180  *
9393166Ssg70180  * Note it is valid for this property to be NULL (but the property
9403166Ssg70180  * itself must exist). Callers of this routine should verify that
9413166Ssg70180  * the value returned is what they expected (i.e. either NULL or non NULL).
9423166Ssg70180  *
9433166Ssg70180  * On success returns value of the property in region pointed to by
9443166Ssg70180  * the 'name' argument, and with return value of 0. Otherwise returns 1.
9451991Sheppo  */
9463166Ssg70180 static int
vsw_get_md_physname(vsw_t * vswp,md_t * mdp,mde_cookie_t node,char * name)9473166Ssg70180 vsw_get_md_physname(vsw_t *vswp, md_t *mdp, mde_cookie_t node, char *name)
9483166Ssg70180 {
9495462Swentaoy 	int		len = 0;
9505462Swentaoy 	int		instance;
9515462Swentaoy 	char		*physname = NULL;
9525462Swentaoy 	char		*dev;
9535462Swentaoy 	const char	*dev_name;
9545462Swentaoy 	char		myname[MAXNAMELEN];
9555462Swentaoy 
9565462Swentaoy 	dev_name = ddi_driver_name(vswp->dip);
9575462Swentaoy 	instance = ddi_get_instance(vswp->dip);
9585462Swentaoy 	(void) snprintf(myname, MAXNAMELEN, "%s%d", dev_name, instance);
9593166Ssg70180 
9603166Ssg70180 	if (md_get_prop_data(mdp, node, physdev_propname,
9614838Slm66018 	    (uint8_t **)(&physname), &len) != 0) {
9623166Ssg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get name(s) of physical "
9634838Slm66018 		    "device(s) from MD", vswp->instance);
9643166Ssg70180 		return (1);
9651991Sheppo 	} else if ((strlen(physname) + 1) > LIFNAMSIZ) {
9663166Ssg70180 		cmn_err(CE_WARN, "!vsw%d: %s is too long a device name",
9674838Slm66018 		    vswp->instance, physname);
9683166Ssg70180 		return (1);
9695462Swentaoy 	} else if (strcmp(myname, physname) == 0) {
9705462Swentaoy 		/*
9715462Swentaoy 		 * Prevent the vswitch from opening itself as the
9725462Swentaoy 		 * network device.
9735462Swentaoy 		 */
9745462Swentaoy 		cmn_err(CE_WARN, "!vsw%d: %s is an invalid device name",
9755462Swentaoy 		    vswp->instance, physname);
9765462Swentaoy 		return (1);
9771991Sheppo 	} else {
9783166Ssg70180 		(void) strncpy(name, physname, strlen(physname) + 1);
9791991Sheppo 		D2(vswp, "%s: using first device specified (%s)",
9804838Slm66018 		    __func__, physname);
9811991Sheppo 	}
9821991Sheppo 
9831991Sheppo #ifdef DEBUG
9841991Sheppo 	/*
9851991Sheppo 	 * As a temporary measure to aid testing we check to see if there
9861991Sheppo 	 * is a vsw.conf file present. If there is we use the value of the
9871991Sheppo 	 * vsw_physname property in the file as the name of the physical
9881991Sheppo 	 * device, overriding the value from the MD.
9891991Sheppo 	 *
9901991Sheppo 	 * There may be multiple devices listed, but for the moment
9911991Sheppo 	 * we just use the first one.
9921991Sheppo 	 */
9931991Sheppo 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vswp->dip, 0,
9944838Slm66018 	    "vsw_physname", &dev) == DDI_PROP_SUCCESS) {
9951991Sheppo 		if ((strlen(dev) + 1) > LIFNAMSIZ) {
9963166Ssg70180 			cmn_err(CE_WARN, "vsw%d: %s is too long a device name",
9974838Slm66018 			    vswp->instance, dev);
9983166Ssg70180 			ddi_prop_free(dev);
9993166Ssg70180 			return (1);
10001991Sheppo 		} else {
10013166Ssg70180 			cmn_err(CE_NOTE, "vsw%d: Using device name (%s) from "
10024838Slm66018 			    "config file", vswp->instance, dev);
10033166Ssg70180 
10043166Ssg70180 			(void) strncpy(name, dev, strlen(dev) + 1);
10051991Sheppo 		}
10061991Sheppo 
10071991Sheppo 		ddi_prop_free(dev);
10081991Sheppo 	}
10091991Sheppo #endif
10101991Sheppo 
10113166Ssg70180 	return (0);
10123166Ssg70180 }
10133166Ssg70180 
10143166Ssg70180 /*
10153166Ssg70180  * Read the 'vsw-switch-mode' property from the specified MD node.
10163166Ssg70180  *
10178275SEric Cheng  * Returns 0 on success, otherwise returns 1.
10183166Ssg70180  */
10193166Ssg70180 static int
vsw_get_md_smodes(vsw_t * vswp,md_t * mdp,mde_cookie_t node,uint8_t * mode)10208275SEric Cheng vsw_get_md_smodes(vsw_t *vswp, md_t *mdp, mde_cookie_t node, uint8_t *mode)
10213166Ssg70180 {
10223166Ssg70180 	int		len = 0;
10233166Ssg70180 	char		*smode = NULL;
10243166Ssg70180 	char		*curr_mode = NULL;
10253166Ssg70180 
10263166Ssg70180 	D1(vswp, "%s: enter", __func__);
10271991Sheppo 
10281991Sheppo 	/*
10291991Sheppo 	 * Get the switch-mode property. The modes are listed in
10301991Sheppo 	 * decreasing order of preference, i.e. prefered mode is
10311991Sheppo 	 * first item in list.
10321991Sheppo 	 */
10331991Sheppo 	len = 0;
10343166Ssg70180 	if (md_get_prop_data(mdp, node, smode_propname,
10354838Slm66018 	    (uint8_t **)(&smode), &len) != 0) {
10361991Sheppo 		/*
10372410Slm66018 		 * Unable to get switch-mode property from MD, nothing
10382410Slm66018 		 * more we can do.
10391991Sheppo 		 */
10403166Ssg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get switch mode property"
10414838Slm66018 		    " from the MD", vswp->instance);
10423166Ssg70180 		return (1);
10432410Slm66018 	}
10442410Slm66018 
10452410Slm66018 	curr_mode = smode;
10462410Slm66018 	/*
10472410Slm66018 	 * Modes of operation:
10482410Slm66018 	 * 'switched'	 - layer 2 switching, underlying HW in
10492410Slm66018 	 *			programmed mode.
10502410Slm66018 	 * 'promiscuous' - layer 2 switching, underlying HW in
10512410Slm66018 	 *			promiscuous mode.
10522410Slm66018 	 * 'routed'	 - layer 3 (i.e. IP) routing, underlying HW
10532410Slm66018 	 *			in non-promiscuous mode.
10542410Slm66018 	 */
10558275SEric Cheng 	while (curr_mode < (smode + len)) {
10562410Slm66018 		D2(vswp, "%s: curr_mode = [%s]", __func__, curr_mode);
10572410Slm66018 		if (strcmp(curr_mode, "switched") == 0) {
10588275SEric Cheng 			*mode = VSW_LAYER2;
10592410Slm66018 		} else if (strcmp(curr_mode, "promiscuous") == 0) {
10608275SEric Cheng 			*mode = VSW_LAYER2 | VSW_LAYER2_PROMISC;
10612410Slm66018 		} else if (strcmp(curr_mode, "routed") == 0) {
10628275SEric Cheng 			*mode = VSW_LAYER3;
10632410Slm66018 		} else {
10648275SEric Cheng 			cmn_err(CE_WARN, "!vsw%d: Unknown switch mode %s, "
10658275SEric Cheng 			    "setting to default switched mode",
10668275SEric Cheng 			    vswp->instance, curr_mode);
10678275SEric Cheng 			*mode = VSW_LAYER2;
10681991Sheppo 		}
10692410Slm66018 		curr_mode += strlen(curr_mode) + 1;
10702410Slm66018 	}
10713166Ssg70180 
10728275SEric Cheng 	D2(vswp, "%s: %d mode", __func__, *mode);
10731991Sheppo 
10741991Sheppo 	D1(vswp, "%s: exit", __func__);
10753166Ssg70180 
10763166Ssg70180 	return (0);
10771991Sheppo }
10781991Sheppo 
10792410Slm66018 /*
10801991Sheppo  * Register with the MAC layer as a network device, so we
10811991Sheppo  * can be plumbed if necessary.
10821991Sheppo  */
10831991Sheppo static int
vsw_mac_register(vsw_t * vswp)10841991Sheppo vsw_mac_register(vsw_t *vswp)
10851991Sheppo {
10862311Sseb 	mac_register_t	*macp;
10872311Sseb 	int		rv;
10881991Sheppo 
10891991Sheppo 	D1(vswp, "%s: enter", __func__);
10901991Sheppo 
10912311Sseb 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
10922311Sseb 		return (EINVAL);
10932311Sseb 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
10942311Sseb 	macp->m_driver = vswp;
10951991Sheppo 	macp->m_dip = vswp->dip;
10962311Sseb 	macp->m_src_addr = (uint8_t *)&vswp->if_addr;
10972311Sseb 	macp->m_callbacks = &vsw_m_callbacks;
10982311Sseb 	macp->m_min_sdu = 0;
10997529SSriharsha.Basavapatna@Sun.COM 	macp->m_max_sdu = vswp->mtu;
11006419Ssb155480 	macp->m_margin = VLAN_TAGSZ;
11012311Sseb 	rv = mac_register(macp, &vswp->if_mh);
11022311Sseb 	mac_free(macp);
11035171Ssb155480 	if (rv != 0) {
11045171Ssb155480 		/*
11055171Ssb155480 		 * Treat this as a non-fatal error as we may be
11065171Ssb155480 		 * able to operate in some other mode.
11075171Ssb155480 		 */
11085171Ssb155480 		cmn_err(CE_NOTE, "!vsw%d: Unable to register as "
11095171Ssb155480 		    "a provider with MAC layer", vswp->instance);
11105171Ssb155480 		return (rv);
11115171Ssb155480 	}
11125171Ssb155480 
11135171Ssb155480 	vswp->if_state |= VSW_IF_REG;
11141991Sheppo 
11151991Sheppo 	D1(vswp, "%s: exit", __func__);
11161991Sheppo 
11171991Sheppo 	return (rv);
11181991Sheppo }
11191991Sheppo 
11201991Sheppo static int
vsw_mac_unregister(vsw_t * vswp)11211991Sheppo vsw_mac_unregister(vsw_t *vswp)
11221991Sheppo {
11231991Sheppo 	int		rv = 0;
11241991Sheppo 
11251991Sheppo 	D1(vswp, "%s: enter", __func__);
11261991Sheppo 
11271991Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
11281991Sheppo 
11292311Sseb 	if (vswp->if_state & VSW_IF_REG) {
11302311Sseb 		rv = mac_unregister(vswp->if_mh);
11311991Sheppo 		if (rv != 0) {
11321991Sheppo 			DWARN(vswp, "%s: unable to unregister from MAC "
11334838Slm66018 			    "framework", __func__);
11341991Sheppo 
11351991Sheppo 			RW_EXIT(&vswp->if_lockrw);
11361991Sheppo 			D1(vswp, "%s: fail exit", __func__);
11371991Sheppo 			return (rv);
11381991Sheppo 		}
11391991Sheppo 
11402311Sseb 		/* mark i/f as down and unregistered */
11412311Sseb 		vswp->if_state &= ~(VSW_IF_UP | VSW_IF_REG);
11421991Sheppo 	}
11431991Sheppo 	RW_EXIT(&vswp->if_lockrw);
11441991Sheppo 
11451991Sheppo 	D1(vswp, "%s: exit", __func__);
11461991Sheppo 
11471991Sheppo 	return (rv);
11481991Sheppo }
11491991Sheppo 
11502311Sseb static int
vsw_m_stat(void * arg,uint_t stat,uint64_t * val)11512311Sseb vsw_m_stat(void *arg, uint_t stat, uint64_t *val)
11521991Sheppo {
11531991Sheppo 	vsw_t			*vswp = (vsw_t *)arg;
11541991Sheppo 
11551991Sheppo 	D1(vswp, "%s: enter", __func__);
11561991Sheppo 
11578275SEric Cheng 	mutex_enter(&vswp->mac_lock);
11583166Ssg70180 	if (vswp->mh == NULL) {
11598275SEric Cheng 		mutex_exit(&vswp->mac_lock);
11602311Sseb 		return (EINVAL);
11613166Ssg70180 	}
11621991Sheppo 
11631991Sheppo 	/* return stats from underlying device */
11642311Sseb 	*val = mac_stat_get(vswp->mh, stat);
11653166Ssg70180 
11668275SEric Cheng 	mutex_exit(&vswp->mac_lock);
11673166Ssg70180 
11682311Sseb 	return (0);
11691991Sheppo }
11701991Sheppo 
11711991Sheppo static void
vsw_m_stop(void * arg)11721991Sheppo vsw_m_stop(void *arg)
11731991Sheppo {
11747529SSriharsha.Basavapatna@Sun.COM 	vsw_t	*vswp = (vsw_t *)arg;
11751991Sheppo 
11761991Sheppo 	D1(vswp, "%s: enter", __func__);
11771991Sheppo 
11781991Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
11791991Sheppo 	vswp->if_state &= ~VSW_IF_UP;
11801991Sheppo 	RW_EXIT(&vswp->if_lockrw);
11811991Sheppo 
11828275SEric Cheng 	/* Cleanup and close the mac client */
11838275SEric Cheng 	vsw_mac_client_cleanup(vswp, NULL, VSW_LOCALDEV);
11843784Ssg70180 
11851991Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
11861991Sheppo }
11871991Sheppo 
11881991Sheppo static int
vsw_m_start(void * arg)11891991Sheppo vsw_m_start(void *arg)
11901991Sheppo {
11918275SEric Cheng 	int		rv;
11921991Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
11931991Sheppo 
11941991Sheppo 	D1(vswp, "%s: enter", __func__);
11951991Sheppo 
11961991Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
11975171Ssb155480 
11981991Sheppo 	vswp->if_state |= VSW_IF_UP;
11995171Ssb155480 
12005171Ssb155480 	if (vswp->switching_setup_done == B_FALSE) {
12015171Ssb155480 		/*
12025171Ssb155480 		 * If the switching mode has not been setup yet, just
12035171Ssb155480 		 * return. The unicast address will be programmed
12045171Ssb155480 		 * after the physical device is successfully setup by the
12055171Ssb155480 		 * timeout handler.
12065171Ssb155480 		 */
12075171Ssb155480 		RW_EXIT(&vswp->if_lockrw);
12085171Ssb155480 		return (0);
12095171Ssb155480 	}
12105171Ssb155480 
12115171Ssb155480 	/* if in layer2 mode, program unicast address. */
12125171Ssb155480 	if (vswp->mh != NULL) {
12138275SEric Cheng 		/* Init a mac client and program addresses */
12148275SEric Cheng 		rv = vsw_mac_client_init(vswp, NULL, VSW_LOCALDEV);
12158275SEric Cheng 		if (rv != 0) {
12168275SEric Cheng 			cmn_err(CE_NOTE,
12178275SEric Cheng 			    "!vsw%d: failed to program interface "
12188275SEric Cheng 			    "unicast address\n", vswp->instance);
12198275SEric Cheng 		}
12205171Ssb155480 	}
12215171Ssb155480 
12221991Sheppo 	RW_EXIT(&vswp->if_lockrw);
12231991Sheppo 
12241991Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
12251991Sheppo 	return (0);
12261991Sheppo }
12271991Sheppo 
12281991Sheppo /*
12291991Sheppo  * Change the local interface address.
12303784Ssg70180  *
12313784Ssg70180  * Note: we don't support this entry point. The local
12323784Ssg70180  * mac address of the switch can only be changed via its
12333784Ssg70180  * MD node properties.
12341991Sheppo  */
12351991Sheppo static int
vsw_m_unicst(void * arg,const uint8_t * macaddr)12361991Sheppo vsw_m_unicst(void *arg, const uint8_t *macaddr)
12371991Sheppo {
12383784Ssg70180 	_NOTE(ARGUNUSED(arg, macaddr))
12393784Ssg70180 
12403784Ssg70180 	return (DDI_FAILURE);
12411991Sheppo }
12421991Sheppo 
12431991Sheppo static int
vsw_m_multicst(void * arg,boolean_t add,const uint8_t * mca)12441991Sheppo vsw_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
12451991Sheppo {
12461991Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
12471991Sheppo 	mcst_addr_t	*mcst_p = NULL;
12481991Sheppo 	uint64_t	addr = 0x0;
12492410Slm66018 	int		i, ret = 0;
12501991Sheppo 
12511991Sheppo 	D1(vswp, "%s: enter", __func__);
12521991Sheppo 
12531991Sheppo 	/*
12541991Sheppo 	 * Convert address into form that can be used
12551991Sheppo 	 * as hash table key.
12561991Sheppo 	 */
12571991Sheppo 	for (i = 0; i < ETHERADDRL; i++) {
12581991Sheppo 		addr = (addr << 8) | mca[i];
12591991Sheppo 	}
12601991Sheppo 
12611991Sheppo 	D2(vswp, "%s: addr = 0x%llx", __func__, addr);
12621991Sheppo 
12631991Sheppo 	if (add) {
12641991Sheppo 		D2(vswp, "%s: adding multicast", __func__);
12651991Sheppo 		if (vsw_add_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
12661991Sheppo 			/*
12671991Sheppo 			 * Update the list of multicast addresses
12681991Sheppo 			 * contained within the vsw_t structure to
12691991Sheppo 			 * include this new one.
12701991Sheppo 			 */
12711991Sheppo 			mcst_p = kmem_zalloc(sizeof (mcst_addr_t), KM_NOSLEEP);
12721991Sheppo 			if (mcst_p == NULL) {
12731991Sheppo 				DERR(vswp, "%s unable to alloc mem", __func__);
12745171Ssb155480 				(void) vsw_del_mcst(vswp,
12755171Ssb155480 				    VSW_LOCALDEV, addr, NULL);
12761991Sheppo 				return (1);
12771991Sheppo 			}
12781991Sheppo 			mcst_p->addr = addr;
12795171Ssb155480 			ether_copy(mca, &mcst_p->mca);
12801991Sheppo 
12811991Sheppo 			/*
12821991Sheppo 			 * Call into the underlying driver to program the
12831991Sheppo 			 * address into HW.
12841991Sheppo 			 */
12858275SEric Cheng 			ret = vsw_mac_multicast_add(vswp, NULL, mcst_p,
12868275SEric Cheng 			    VSW_LOCALDEV);
12878275SEric Cheng 			if (ret != 0) {
12888275SEric Cheng 				(void) vsw_del_mcst(vswp,
12898275SEric Cheng 				    VSW_LOCALDEV, addr, NULL);
12908275SEric Cheng 				kmem_free(mcst_p, sizeof (*mcst_p));
12918275SEric Cheng 				return (ret);
12922410Slm66018 			}
12935171Ssb155480 
12945171Ssb155480 			mutex_enter(&vswp->mca_lock);
12955171Ssb155480 			mcst_p->nextp = vswp->mcap;
12965171Ssb155480 			vswp->mcap = mcst_p;
12975171Ssb155480 			mutex_exit(&vswp->mca_lock);
12982410Slm66018 		} else {
12998275SEric Cheng 			cmn_err(CE_WARN, "!vsw%d: unable to add multicast "
13004838Slm66018 			    "address", vswp->instance);
13011991Sheppo 		}
13022410Slm66018 		return (ret);
13032410Slm66018 	}
13042410Slm66018 
13052410Slm66018 	D2(vswp, "%s: removing multicast", __func__);
13062410Slm66018 	/*
13072410Slm66018 	 * Remove the address from the hash table..
13082410Slm66018 	 */
13092410Slm66018 	if (vsw_del_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
13102410Slm66018 
13111991Sheppo 		/*
13122410Slm66018 		 * ..and then from the list maintained in the
13132410Slm66018 		 * vsw_t structure.
13141991Sheppo 		 */
13155171Ssb155480 		mcst_p = vsw_del_addr(VSW_LOCALDEV, vswp, addr);
13165171Ssb155480 		ASSERT(mcst_p != NULL);
13172410Slm66018 
13188275SEric Cheng 		vsw_mac_multicast_remove(vswp, NULL, mcst_p, VSW_LOCALDEV);
13195171Ssb155480 		kmem_free(mcst_p, sizeof (*mcst_p));
13201991Sheppo 	}
13211991Sheppo 
13221991Sheppo 	D1(vswp, "%s: exit", __func__);
13231991Sheppo 
13241991Sheppo 	return (0);
13251991Sheppo }
13261991Sheppo 
13271991Sheppo static int
vsw_m_promisc(void * arg,boolean_t on)13281991Sheppo vsw_m_promisc(void *arg, boolean_t on)
13291991Sheppo {
13301991Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
13311991Sheppo 
13321991Sheppo 	D1(vswp, "%s: enter", __func__);
13331991Sheppo 
13341991Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
13351991Sheppo 	if (on)
13361991Sheppo 		vswp->if_state |= VSW_IF_PROMISC;
13371991Sheppo 	else
13381991Sheppo 		vswp->if_state &= ~VSW_IF_PROMISC;
13391991Sheppo 	RW_EXIT(&vswp->if_lockrw);
13401991Sheppo 
13411991Sheppo 	D1(vswp, "%s: exit", __func__);
13421991Sheppo 
13431991Sheppo 	return (0);
13441991Sheppo }
13451991Sheppo 
13461991Sheppo static mblk_t *
vsw_m_tx(void * arg,mblk_t * mp)13471991Sheppo vsw_m_tx(void *arg, mblk_t *mp)
13481991Sheppo {
13491991Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
13501991Sheppo 
13511991Sheppo 	D1(vswp, "%s: enter", __func__);
13521991Sheppo 
13536419Ssb155480 	mp = vsw_vlan_frame_pretag(vswp, VSW_LOCALDEV, mp);
13546419Ssb155480 
13556419Ssb155480 	if (mp == NULL) {
13566419Ssb155480 		return (NULL);
13576419Ssb155480 	}
13586419Ssb155480 
13593166Ssg70180 	vswp->vsw_switch_frame(vswp, mp, VSW_LOCALDEV, NULL, NULL);
13601991Sheppo 
13611991Sheppo 	D1(vswp, "%s: exit", __func__);
13621991Sheppo 
13631991Sheppo 	return (NULL);
13641991Sheppo }
13651991Sheppo 
13661991Sheppo /*
13671991Sheppo  * Register for machine description (MD) updates.
13683166Ssg70180  *
13693166Ssg70180  * Returns 0 on success, 1 on failure.
13701991Sheppo  */
13713166Ssg70180 static int
vsw_mdeg_register(vsw_t * vswp)13721991Sheppo vsw_mdeg_register(vsw_t *vswp)
13731991Sheppo {
13741991Sheppo 	mdeg_prop_spec_t	*pspecp;
13751991Sheppo 	mdeg_node_spec_t	*inst_specp;
13763166Ssg70180 	mdeg_handle_t		mdeg_hdl, mdeg_port_hdl;
13771991Sheppo 	size_t			templatesz;
13785171Ssb155480 	int			rv;
13791991Sheppo 
13801991Sheppo 	D1(vswp, "%s: enter", __func__);
13811991Sheppo 
13823166Ssg70180 	/*
13831991Sheppo 	 * Allocate and initialize a per-instance copy
13841991Sheppo 	 * of the global property spec array that will
13851991Sheppo 	 * uniquely identify this vsw instance.
13861991Sheppo 	 */
13871991Sheppo 	templatesz = sizeof (vsw_prop_template);
13881991Sheppo 	pspecp = kmem_zalloc(templatesz, KM_SLEEP);
13891991Sheppo 
13901991Sheppo 	bcopy(vsw_prop_template, pspecp, templatesz);
13911991Sheppo 
13925171Ssb155480 	VSW_SET_MDEG_PROP_INST(pspecp, vswp->regprop);
13931991Sheppo 
13941991Sheppo 	/* initialize the complete prop spec structure */
13951991Sheppo 	inst_specp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
13961991Sheppo 	inst_specp->namep = "virtual-device";
13971991Sheppo 	inst_specp->specp = pspecp;
13981991Sheppo 
13995171Ssb155480 	D2(vswp, "%s: instance %d registering with mdeg", __func__,
14005171Ssb155480 	    vswp->regprop);
14013166Ssg70180 	/*
14023166Ssg70180 	 * Register an interest in 'virtual-device' nodes with a
14033166Ssg70180 	 * 'name' property of 'virtual-network-switch'
14043166Ssg70180 	 */
14053166Ssg70180 	rv = mdeg_register(inst_specp, &vdev_match, vsw_mdeg_cb,
14061991Sheppo 	    (void *)vswp, &mdeg_hdl);
14073166Ssg70180 	if (rv != MDEG_SUCCESS) {
14083166Ssg70180 		DERR(vswp, "%s: mdeg_register failed (%d) for vsw node",
14094838Slm66018 		    __func__, rv);
14103166Ssg70180 		goto mdeg_reg_fail;
14113166Ssg70180 	}
14123166Ssg70180 
14133166Ssg70180 	/*
14143166Ssg70180 	 * Register an interest in 'vsw-port' nodes.
14153166Ssg70180 	 */
14163166Ssg70180 	rv = mdeg_register(inst_specp, &vport_match, vsw_port_mdeg_cb,
14173166Ssg70180 	    (void *)vswp, &mdeg_port_hdl);
14181991Sheppo 	if (rv != MDEG_SUCCESS) {
14191991Sheppo 		DERR(vswp, "%s: mdeg_register failed (%d)\n", __func__, rv);
14203166Ssg70180 		(void) mdeg_unregister(mdeg_hdl);
14213166Ssg70180 		goto mdeg_reg_fail;
14221991Sheppo 	}
14231991Sheppo 
14241991Sheppo 	/* save off data that will be needed later */
14251991Sheppo 	vswp->inst_spec = inst_specp;
14261991Sheppo 	vswp->mdeg_hdl = mdeg_hdl;
14273166Ssg70180 	vswp->mdeg_port_hdl = mdeg_port_hdl;
14281991Sheppo 
14291991Sheppo 	D1(vswp, "%s: exit", __func__);
14303166Ssg70180 	return (0);
14313166Ssg70180 
14323166Ssg70180 mdeg_reg_fail:
14333166Ssg70180 	cmn_err(CE_WARN, "!vsw%d: Unable to register MDEG callbacks",
14344838Slm66018 	    vswp->instance);
14353166Ssg70180 	kmem_free(pspecp, templatesz);
14363166Ssg70180 	kmem_free(inst_specp, sizeof (mdeg_node_spec_t));
14373166Ssg70180 
14383166Ssg70180 	vswp->mdeg_hdl = NULL;
14393166Ssg70180 	vswp->mdeg_port_hdl = NULL;
14403166Ssg70180 
14413166Ssg70180 	return (1);
14421991Sheppo }
14431991Sheppo 
14441991Sheppo static void
vsw_mdeg_unregister(vsw_t * vswp)14451991Sheppo vsw_mdeg_unregister(vsw_t *vswp)
14461991Sheppo {
14471991Sheppo 	D1(vswp, "vsw_mdeg_unregister: enter");
14481991Sheppo 
14493166Ssg70180 	if (vswp->mdeg_hdl != NULL)
14503166Ssg70180 		(void) mdeg_unregister(vswp->mdeg_hdl);
14513166Ssg70180 
14523166Ssg70180 	if (vswp->mdeg_port_hdl != NULL)
14533166Ssg70180 		(void) mdeg_unregister(vswp->mdeg_port_hdl);
14541991Sheppo 
14551991Sheppo 	if (vswp->inst_spec != NULL) {
14563166Ssg70180 		if (vswp->inst_spec->specp != NULL) {
14573166Ssg70180 			(void) kmem_free(vswp->inst_spec->specp,
14584838Slm66018 			    sizeof (vsw_prop_template));
14593166Ssg70180 			vswp->inst_spec->specp = NULL;
14603166Ssg70180 		}
14613166Ssg70180 
14624838Slm66018 		(void) kmem_free(vswp->inst_spec, sizeof (mdeg_node_spec_t));
14631991Sheppo 		vswp->inst_spec = NULL;
14641991Sheppo 	}
14651991Sheppo 
14661991Sheppo 	D1(vswp, "vsw_mdeg_unregister: exit");
14671991Sheppo }
14681991Sheppo 
14693166Ssg70180 /*
14703166Ssg70180  * Mdeg callback invoked for the vsw node itself.
14713166Ssg70180  */
14721991Sheppo static int
vsw_mdeg_cb(void * cb_argp,mdeg_result_t * resp)14731991Sheppo vsw_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
14741991Sheppo {
14751991Sheppo 	vsw_t		*vswp;
14761991Sheppo 	md_t		*mdp;
14771991Sheppo 	mde_cookie_t	node;
14781991Sheppo 	uint64_t	inst;
14793166Ssg70180 	char		*node_name = NULL;
14801991Sheppo 
14811991Sheppo 	if (resp == NULL)
14821991Sheppo 		return (MDEG_FAILURE);
14831991Sheppo 
14841991Sheppo 	vswp = (vsw_t *)cb_argp;
14851991Sheppo 
14863166Ssg70180 	D1(vswp, "%s: added %d : removed %d : curr matched %d"
14874838Slm66018 	    " : prev matched %d", __func__, resp->added.nelem,
14884838Slm66018 	    resp->removed.nelem, resp->match_curr.nelem,
14894838Slm66018 	    resp->match_prev.nelem);
14903166Ssg70180 
14913166Ssg70180 	/*
14925171Ssb155480 	 * We get an initial callback for this node as 'added'
14935171Ssb155480 	 * after registering with mdeg. Note that we would have
14945171Ssb155480 	 * already gathered information about this vsw node by
14955171Ssb155480 	 * walking MD earlier during attach (in vsw_read_mdprops()).
14965171Ssb155480 	 * So, there is a window where the properties of this
14975171Ssb155480 	 * node might have changed when we get this initial 'added'
14985171Ssb155480 	 * callback. We handle this as if an update occured
14995171Ssb155480 	 * and invoke the same function which handles updates to
15005171Ssb155480 	 * the properties of this vsw-node if any.
15015171Ssb155480 	 *
15025171Ssb155480 	 * A non-zero 'match' value indicates that the MD has been
15035171Ssb155480 	 * updated and that a virtual-network-switch node is
15045171Ssb155480 	 * present which may or may not have been updated. It is
15055171Ssb155480 	 * up to the clients to examine their own nodes and
15065171Ssb155480 	 * determine if they have changed.
15075171Ssb155480 	 */
15085171Ssb155480 	if (resp->added.nelem != 0) {
15095171Ssb155480 
15105171Ssb155480 		if (resp->added.nelem != 1) {
15115171Ssb155480 			cmn_err(CE_NOTE, "!vsw%d: number of nodes added "
15125171Ssb155480 			    "invalid: %d\n", vswp->instance, resp->added.nelem);
15135171Ssb155480 			return (MDEG_FAILURE);
15145171Ssb155480 		}
15155171Ssb155480 
15163166Ssg70180 		mdp = resp->added.mdp;
15175171Ssb155480 		node = resp->added.mdep[0];
15185171Ssb155480 
15195171Ssb155480 	} else if (resp->match_curr.nelem != 0) {
15205171Ssb155480 
15215171Ssb155480 		if (resp->match_curr.nelem != 1) {
15225171Ssb155480 			cmn_err(CE_NOTE, "!vsw%d: number of nodes updated "
15235171Ssb155480 			    "invalid: %d\n", vswp->instance,
15245171Ssb155480 			    resp->match_curr.nelem);
15255171Ssb155480 			return (MDEG_FAILURE);
15265171Ssb155480 		}
15275171Ssb155480 
15283166Ssg70180 		mdp = resp->match_curr.mdp;
15295171Ssb155480 		node = resp->match_curr.mdep[0];
15305171Ssb155480 
15315171Ssb155480 	} else {
15325171Ssb155480 		return (MDEG_FAILURE);
15335171Ssb155480 	}
15345171Ssb155480 
15355171Ssb155480 	/* Validate name and instance */
15365171Ssb155480 	if (md_get_prop_str(mdp, node, "name", &node_name) != 0) {
15375171Ssb155480 		DERR(vswp, "%s: unable to get node name\n",  __func__);
15385171Ssb155480 		return (MDEG_FAILURE);
15395171Ssb155480 	}
15405171Ssb155480 
15415171Ssb155480 	/* is this a virtual-network-switch? */
15425171Ssb155480 	if (strcmp(node_name, vsw_propname) != 0) {
15435171Ssb155480 		DERR(vswp, "%s: Invalid node name: %s\n",
15445171Ssb155480 		    __func__, node_name);
15455171Ssb155480 		return (MDEG_FAILURE);
15465171Ssb155480 	}
15475171Ssb155480 
15485171Ssb155480 	if (md_get_prop_val(mdp, node, "cfg-handle", &inst)) {
15495171Ssb155480 		DERR(vswp, "%s: prop(cfg-handle) not found\n",
15505171Ssb155480 		    __func__);
15515171Ssb155480 		return (MDEG_FAILURE);
15525171Ssb155480 	}
15535171Ssb155480 
15545171Ssb155480 	/* is this the right instance of vsw? */
15555171Ssb155480 	if (inst != vswp->regprop) {
15565171Ssb155480 		DERR(vswp, "%s: Invalid cfg-handle: %lx\n",
15575171Ssb155480 		    __func__, inst);
15585171Ssb155480 		return (MDEG_FAILURE);
15595171Ssb155480 	}
15605171Ssb155480 
15615171Ssb155480 	vsw_update_md_prop(vswp, mdp, node);
15623166Ssg70180 
15633166Ssg70180 	return (MDEG_SUCCESS);
15643166Ssg70180 }
15653166Ssg70180 
15663166Ssg70180 /*
15673166Ssg70180  * Mdeg callback invoked for changes to the vsw-port nodes
15683166Ssg70180  * under the vsw node.
15693166Ssg70180  */
15703166Ssg70180 static int
vsw_port_mdeg_cb(void * cb_argp,mdeg_result_t * resp)15713166Ssg70180 vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
15723166Ssg70180 {
15733166Ssg70180 	vsw_t		*vswp;
15743166Ssg70180 	int		idx;
15753166Ssg70180 	md_t		*mdp;
15763166Ssg70180 	mde_cookie_t	node;
15773166Ssg70180 	uint64_t	inst;
15786241Snarayan 	int		rv;
15793166Ssg70180 
15803166Ssg70180 	if ((resp == NULL) || (cb_argp == NULL))
15813166Ssg70180 		return (MDEG_FAILURE);
15823166Ssg70180 
15833166Ssg70180 	vswp = (vsw_t *)cb_argp;
15843166Ssg70180 
15853166Ssg70180 	D2(vswp, "%s: added %d : removed %d : curr matched %d"
15864838Slm66018 	    " : prev matched %d", __func__, resp->added.nelem,
15874838Slm66018 	    resp->removed.nelem, resp->match_curr.nelem,
15884838Slm66018 	    resp->match_prev.nelem);
15891991Sheppo 
15901991Sheppo 	/* process added ports */
15911991Sheppo 	for (idx = 0; idx < resp->added.nelem; idx++) {
15921991Sheppo 		mdp = resp->added.mdp;
15931991Sheppo 		node = resp->added.mdep[idx];
15941991Sheppo 
15951991Sheppo 		D2(vswp, "%s: adding node(%d) 0x%lx", __func__, idx, node);
15961991Sheppo 
15976241Snarayan 		if ((rv = vsw_port_add(vswp, mdp, &node)) != 0) {
15983166Ssg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to add new port "
15996241Snarayan 			    "(0x%lx), err=%d", vswp->instance, node, rv);
16001991Sheppo 		}
16011991Sheppo 	}
16021991Sheppo 
16031991Sheppo 	/* process removed ports */
16041991Sheppo 	for (idx = 0; idx < resp->removed.nelem; idx++) {
16051991Sheppo 		mdp = resp->removed.mdp;
16061991Sheppo 		node = resp->removed.mdep[idx];
16071991Sheppo 
16081991Sheppo 		if (md_get_prop_val(mdp, node, id_propname, &inst)) {
16093166Ssg70180 			DERR(vswp, "%s: prop(%s) not found in port(%d)",
16104838Slm66018 			    __func__, id_propname, idx);
16111991Sheppo 			continue;
16121991Sheppo 		}
16131991Sheppo 
16141991Sheppo 		D2(vswp, "%s: removing node(%d) 0x%lx", __func__, idx, node);
16151991Sheppo 
16161991Sheppo 		if (vsw_port_detach(vswp, inst) != 0) {
16173166Ssg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to remove port %ld",
16184838Slm66018 			    vswp->instance, inst);
16191991Sheppo 		}
16201991Sheppo 	}
16211991Sheppo 
16226419Ssb155480 	for (idx = 0; idx < resp->match_curr.nelem; idx++) {
16236419Ssb155480 		(void) vsw_port_update(vswp, resp->match_curr.mdp,
16246419Ssb155480 		    resp->match_curr.mdep[idx],
16256419Ssb155480 		    resp->match_prev.mdp,
16266419Ssb155480 		    resp->match_prev.mdep[idx]);
16276419Ssb155480 	}
16281991Sheppo 
16291991Sheppo 	D1(vswp, "%s: exit", __func__);
16301991Sheppo 
16311991Sheppo 	return (MDEG_SUCCESS);
16321991Sheppo }
16331991Sheppo 
16341991Sheppo /*
16355171Ssb155480  * Scan the machine description for this instance of vsw
16365171Ssb155480  * and read its properties. Called only from vsw_attach().
16375171Ssb155480  * Returns: 0 on success, 1 on failure.
16385171Ssb155480  */
16395171Ssb155480 static int
vsw_read_mdprops(vsw_t * vswp)16405171Ssb155480 vsw_read_mdprops(vsw_t *vswp)
16415171Ssb155480 {
16425171Ssb155480 	md_t		*mdp = NULL;
16435171Ssb155480 	mde_cookie_t	rootnode;
16445171Ssb155480 	mde_cookie_t	*listp = NULL;
16455171Ssb155480 	uint64_t	inst;
16465171Ssb155480 	uint64_t	cfgh;
16475171Ssb155480 	char		*name;
16485171Ssb155480 	int		rv = 1;
16495171Ssb155480 	int		num_nodes = 0;
16505171Ssb155480 	int		num_devs = 0;
16515171Ssb155480 	int		listsz = 0;
16525171Ssb155480 	int		i;
16535171Ssb155480 
16545171Ssb155480 	/*
16555171Ssb155480 	 * In each 'virtual-device' node in the MD there is a
16565171Ssb155480 	 * 'cfg-handle' property which is the MD's concept of
16575171Ssb155480 	 * an instance number (this may be completely different from
16585171Ssb155480 	 * the device drivers instance #). OBP reads that value and
16595171Ssb155480 	 * stores it in the 'reg' property of the appropriate node in
16605171Ssb155480 	 * the device tree. We first read this reg property and use this
16615171Ssb155480 	 * to compare against the 'cfg-handle' property of vsw nodes
16625171Ssb155480 	 * in MD to get to this specific vsw instance and then read
16635171Ssb155480 	 * other properties that we are interested in.
16645171Ssb155480 	 * We also cache the value of 'reg' property and use it later
16655171Ssb155480 	 * to register callbacks with mdeg (see vsw_mdeg_register())
16665171Ssb155480 	 */
16675171Ssb155480 	inst = ddi_prop_get_int(DDI_DEV_T_ANY, vswp->dip,
16685171Ssb155480 	    DDI_PROP_DONTPASS, reg_propname, -1);
16695171Ssb155480 	if (inst == -1) {
16705171Ssb155480 		cmn_err(CE_NOTE, "!vsw%d: Unable to read %s property from "
16715171Ssb155480 		    "OBP device tree", vswp->instance, reg_propname);
16725171Ssb155480 		return (rv);
16735171Ssb155480 	}
16745171Ssb155480 
16755171Ssb155480 	vswp->regprop = inst;
16765171Ssb155480 
16775171Ssb155480 	if ((mdp = md_get_handle()) == NULL) {
16785171Ssb155480 		DWARN(vswp, "%s: cannot init MD\n", __func__);
16795171Ssb155480 		return (rv);
16805171Ssb155480 	}
16815171Ssb155480 
16825171Ssb155480 	num_nodes = md_node_count(mdp);
16835171Ssb155480 	ASSERT(num_nodes > 0);
16845171Ssb155480 
16855171Ssb155480 	listsz = num_nodes * sizeof (mde_cookie_t);
16865171Ssb155480 	listp = (mde_cookie_t *)kmem_zalloc(listsz, KM_SLEEP);
16875171Ssb155480 
16885171Ssb155480 	rootnode = md_root_node(mdp);
16895171Ssb155480 
16905171Ssb155480 	/* search for all "virtual_device" nodes */
16915171Ssb155480 	num_devs = md_scan_dag(mdp, rootnode,
16925171Ssb155480 	    md_find_name(mdp, vdev_propname),
16935171Ssb155480 	    md_find_name(mdp, "fwd"), listp);
16945171Ssb155480 	if (num_devs <= 0) {
16955171Ssb155480 		DWARN(vswp, "%s: invalid num_devs:%d\n", __func__, num_devs);
16965171Ssb155480 		goto vsw_readmd_exit;
16975171Ssb155480 	}
16985171Ssb155480 
16995171Ssb155480 	/*
17005171Ssb155480 	 * Now loop through the list of virtual-devices looking for
17015171Ssb155480 	 * devices with name "virtual-network-switch" and for each
17025171Ssb155480 	 * such device compare its instance with what we have from
17035171Ssb155480 	 * the 'reg' property to find the right node in MD and then
17045171Ssb155480 	 * read all its properties.
17055171Ssb155480 	 */
17065171Ssb155480 	for (i = 0; i < num_devs; i++) {
17075171Ssb155480 
17085171Ssb155480 		if (md_get_prop_str(mdp, listp[i], "name", &name) != 0) {
17095171Ssb155480 			DWARN(vswp, "%s: name property not found\n",
17105171Ssb155480 			    __func__);
17115171Ssb155480 			goto vsw_readmd_exit;
17125171Ssb155480 		}
17135171Ssb155480 
17145171Ssb155480 		/* is this a virtual-network-switch? */
17155171Ssb155480 		if (strcmp(name, vsw_propname) != 0)
17165171Ssb155480 			continue;
17175171Ssb155480 
17185171Ssb155480 		if (md_get_prop_val(mdp, listp[i], "cfg-handle", &cfgh) != 0) {
17195171Ssb155480 			DWARN(vswp, "%s: cfg-handle property not found\n",
17205171Ssb155480 			    __func__);
17215171Ssb155480 			goto vsw_readmd_exit;
17225171Ssb155480 		}
17235171Ssb155480 
17245171Ssb155480 		/* is this the required instance of vsw? */
17255171Ssb155480 		if (inst != cfgh)
17265171Ssb155480 			continue;
17275171Ssb155480 
17285171Ssb155480 		/* now read all properties of this vsw instance */
17295171Ssb155480 		rv = vsw_get_initial_md_properties(vswp, mdp, listp[i]);
17305171Ssb155480 		break;
17315171Ssb155480 	}
17325171Ssb155480 
17335171Ssb155480 vsw_readmd_exit:
17345171Ssb155480 
17355171Ssb155480 	kmem_free(listp, listsz);
17365171Ssb155480 	(void) md_fini_handle(mdp);
17375171Ssb155480 	return (rv);
17385171Ssb155480 }
17395171Ssb155480 
17405171Ssb155480 /*
17413166Ssg70180  * Read the initial start-of-day values from the specified MD node.
17423166Ssg70180  */
17435171Ssb155480 static int
vsw_get_initial_md_properties(vsw_t * vswp,md_t * mdp,mde_cookie_t node)17443166Ssg70180 vsw_get_initial_md_properties(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
17453166Ssg70180 {
17468275SEric Cheng 	uint64_t	macaddr = 0;
17473166Ssg70180 
17483166Ssg70180 	D1(vswp, "%s: enter", __func__);
17493166Ssg70180 
17505171Ssb155480 	if (vsw_get_md_physname(vswp, mdp, node, vswp->physname) != 0) {
17515171Ssb155480 		return (1);
17523166Ssg70180 	}
17533166Ssg70180 
17543166Ssg70180 	/* mac address for vswitch device itself */
17553166Ssg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
17563166Ssg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
17574838Slm66018 		    vswp->instance);
17585171Ssb155480 		return (1);
17595171Ssb155480 	}
17605171Ssb155480 
17615171Ssb155480 	vsw_save_lmacaddr(vswp, macaddr);
17623166Ssg70180 
17638275SEric Cheng 	if (vsw_get_md_smodes(vswp, mdp, node, &vswp->smode)) {
17646241Snarayan 		DWARN(vswp, "%s: Unable to read %s property from MD, "
17656241Snarayan 		    "defaulting to 'switched' mode",
17666241Snarayan 		    __func__, smode_propname);
17673166Ssg70180 
17688275SEric Cheng 		vswp->smode = VSW_LAYER2;
17693166Ssg70180 	}
17703166Ssg70180 
17719336SSriharsha.Basavapatna@Sun.COM 	/*
17729336SSriharsha.Basavapatna@Sun.COM 	 * Read the 'linkprop' property to know if this
17739336SSriharsha.Basavapatna@Sun.COM 	 * vsw device wants to get physical link updates.
17749336SSriharsha.Basavapatna@Sun.COM 	 */
17759336SSriharsha.Basavapatna@Sun.COM 	vsw_linkprop_read(vswp, mdp, node, &vswp->pls_update);
17769336SSriharsha.Basavapatna@Sun.COM 
17777529SSriharsha.Basavapatna@Sun.COM 	/* read mtu */
17787529SSriharsha.Basavapatna@Sun.COM 	vsw_mtu_read(vswp, mdp, node, &vswp->mtu);
17797529SSriharsha.Basavapatna@Sun.COM 	if (vswp->mtu < ETHERMTU || vswp->mtu > VNET_MAX_MTU) {
17807529SSriharsha.Basavapatna@Sun.COM 		vswp->mtu = ETHERMTU;
17817529SSriharsha.Basavapatna@Sun.COM 	}
17827529SSriharsha.Basavapatna@Sun.COM 	vswp->max_frame_size = vswp->mtu + sizeof (struct ether_header) +
17837529SSriharsha.Basavapatna@Sun.COM 	    VLAN_TAGSZ;
17847529SSriharsha.Basavapatna@Sun.COM 
17856419Ssb155480 	/* read vlan id properties of this vsw instance */
17866419Ssb155480 	vsw_vlan_read_ids(vswp, VSW_LOCALDEV, mdp, node, &vswp->pvid,
17876419Ssb155480 	    &vswp->vids, &vswp->nvids, &vswp->default_vlan_id);
17886419Ssb155480 
17896419Ssb155480 	/* read priority-ether-types */
17905935Ssb155480 	vsw_read_pri_eth_types(vswp, mdp, node);
17915935Ssb155480 
179210795SWentao.Yang@Sun.COM 	/* read bandwidth property of this vsw instance */
179310795SWentao.Yang@Sun.COM 	vsw_bandwidth_read(vswp, mdp, node, &vswp->bandwidth);
179410795SWentao.Yang@Sun.COM 
17953166Ssg70180 	D1(vswp, "%s: exit", __func__);
17965171Ssb155480 	return (0);
17973166Ssg70180 }
17983166Ssg70180 
17993166Ssg70180 /*
18006419Ssb155480  * Read vlan id properties of the given MD node.
18016419Ssb155480  * Arguments:
18026419Ssb155480  *   arg:          device argument(vsw device or a port)
18036419Ssb155480  *   type:         type of arg; VSW_LOCALDEV(vsw device) or VSW_VNETPORT(port)
18046419Ssb155480  *   mdp:          machine description
18056419Ssb155480  *   node:         md node cookie
18066419Ssb155480  *
18076419Ssb155480  * Returns:
18086419Ssb155480  *   pvidp:        port-vlan-id of the node
18096419Ssb155480  *   vidspp:       list of vlan-ids of the node
18106419Ssb155480  *   nvidsp:       # of vlan-ids in the list
18116419Ssb155480  *   default_idp:  default-vlan-id of the node(if node is vsw device)
18126419Ssb155480  */
18136419Ssb155480 static void
vsw_vlan_read_ids(void * arg,int type,md_t * mdp,mde_cookie_t node,uint16_t * pvidp,vsw_vlanid_t ** vidspp,uint16_t * nvidsp,uint16_t * default_idp)18146419Ssb155480 vsw_vlan_read_ids(void *arg, int type, md_t *mdp, mde_cookie_t node,
18158275SEric Cheng 	uint16_t *pvidp, vsw_vlanid_t **vidspp, uint16_t *nvidsp,
18166419Ssb155480 	uint16_t *default_idp)
18176419Ssb155480 {
18186419Ssb155480 	vsw_t		*vswp;
18196419Ssb155480 	vsw_port_t	*portp;
18206419Ssb155480 	char		*pvid_propname;
18216419Ssb155480 	char		*vid_propname;
18226419Ssb155480 	uint_t		nvids = 0;
18236419Ssb155480 	uint32_t	vids_size;
18246419Ssb155480 	int		rv;
18256419Ssb155480 	int		i;
18266419Ssb155480 	uint64_t	*data;
18276419Ssb155480 	uint64_t	val;
18286419Ssb155480 	int		size;
18296419Ssb155480 	int		inst;
18306419Ssb155480 
18316419Ssb155480 	if (type == VSW_LOCALDEV) {
18326419Ssb155480 
18336419Ssb155480 		vswp = (vsw_t *)arg;
18346419Ssb155480 		pvid_propname = vsw_pvid_propname;
18356419Ssb155480 		vid_propname = vsw_vid_propname;
18366419Ssb155480 		inst = vswp->instance;
18376419Ssb155480 
18386419Ssb155480 	} else if (type == VSW_VNETPORT) {
18396419Ssb155480 
18406419Ssb155480 		portp = (vsw_port_t *)arg;
18416419Ssb155480 		vswp = portp->p_vswp;
18426419Ssb155480 		pvid_propname = port_pvid_propname;
18436419Ssb155480 		vid_propname = port_vid_propname;
18446419Ssb155480 		inst = portp->p_instance;
18456419Ssb155480 
18466419Ssb155480 	} else {
18476419Ssb155480 		return;
18486419Ssb155480 	}
18496419Ssb155480 
18506419Ssb155480 	if (type == VSW_LOCALDEV && default_idp != NULL) {
18516419Ssb155480 		rv = md_get_prop_val(mdp, node, vsw_dvid_propname, &val);
18526419Ssb155480 		if (rv != 0) {
18536419Ssb155480 			DWARN(vswp, "%s: prop(%s) not found", __func__,
18546419Ssb155480 			    vsw_dvid_propname);
18556419Ssb155480 
18566419Ssb155480 			*default_idp = vsw_default_vlan_id;
18576419Ssb155480 		} else {
18586419Ssb155480 			*default_idp = val & 0xFFF;
18596419Ssb155480 			D2(vswp, "%s: %s(%d): (%d)\n", __func__,
18606419Ssb155480 			    vsw_dvid_propname, inst, *default_idp);
18616419Ssb155480 		}
18626419Ssb155480 	}
18636419Ssb155480 
18646419Ssb155480 	rv = md_get_prop_val(mdp, node, pvid_propname, &val);
18656419Ssb155480 	if (rv != 0) {
18666419Ssb155480 		DWARN(vswp, "%s: prop(%s) not found", __func__, pvid_propname);
18676419Ssb155480 		*pvidp = vsw_default_vlan_id;
18686419Ssb155480 	} else {
18696419Ssb155480 
18706419Ssb155480 		*pvidp = val & 0xFFF;
18716419Ssb155480 		D2(vswp, "%s: %s(%d): (%d)\n", __func__,
18726419Ssb155480 		    pvid_propname, inst, *pvidp);
18736419Ssb155480 	}
18746419Ssb155480 
18756419Ssb155480 	rv = md_get_prop_data(mdp, node, vid_propname, (uint8_t **)&data,
18766419Ssb155480 	    &size);
18776419Ssb155480 	if (rv != 0) {
18786419Ssb155480 		D2(vswp, "%s: prop(%s) not found", __func__, vid_propname);
18796419Ssb155480 		size = 0;
18806419Ssb155480 	} else {
18816419Ssb155480 		size /= sizeof (uint64_t);
18826419Ssb155480 	}
18836419Ssb155480 	nvids = size;
18846419Ssb155480 
18856419Ssb155480 	if (nvids != 0) {
18866419Ssb155480 		D2(vswp, "%s: %s(%d): ", __func__, vid_propname, inst);
18878275SEric Cheng 		vids_size = sizeof (vsw_vlanid_t) * nvids;
18886419Ssb155480 		*vidspp = kmem_zalloc(vids_size, KM_SLEEP);
18896419Ssb155480 		for (i = 0; i < nvids; i++) {
18908275SEric Cheng 			(*vidspp)[i].vl_vid = data[i] & 0xFFFF;
18918275SEric Cheng 			(*vidspp)[i].vl_set = B_FALSE;
18928275SEric Cheng 			D2(vswp, " %d ", (*vidspp)[i].vl_vid);
18936419Ssb155480 		}
18946419Ssb155480 		D2(vswp, "\n");
18956419Ssb155480 	}
18966419Ssb155480 
18976419Ssb155480 	*nvidsp = nvids;
18986419Ssb155480 }
18996419Ssb155480 
190010795SWentao.Yang@Sun.COM static void
vsw_port_read_bandwidth(vsw_port_t * portp,md_t * mdp,mde_cookie_t node,uint64_t * bw)190110795SWentao.Yang@Sun.COM vsw_port_read_bandwidth(vsw_port_t *portp, md_t *mdp, mde_cookie_t node,
190210795SWentao.Yang@Sun.COM     uint64_t *bw)
190310795SWentao.Yang@Sun.COM {
190410795SWentao.Yang@Sun.COM 	int		rv;
190510795SWentao.Yang@Sun.COM 	uint64_t	val;
190610795SWentao.Yang@Sun.COM 	vsw_t		*vswp;
190710795SWentao.Yang@Sun.COM 
190810795SWentao.Yang@Sun.COM 	vswp = portp->p_vswp;
190910795SWentao.Yang@Sun.COM 
191010795SWentao.Yang@Sun.COM 	rv = md_get_prop_val(mdp, node, port_maxbw_propname, &val);
191110795SWentao.Yang@Sun.COM 
191210795SWentao.Yang@Sun.COM 	if (rv != 0) {
191310795SWentao.Yang@Sun.COM 		*bw = 0;
191410795SWentao.Yang@Sun.COM 		D3(vswp, "%s: prop(%s) not found\n", __func__,
191510795SWentao.Yang@Sun.COM 		    port_maxbw_propname);
191610795SWentao.Yang@Sun.COM 	} else {
191710795SWentao.Yang@Sun.COM 		*bw = val;
191810795SWentao.Yang@Sun.COM 		D3(vswp, "%s: %s nodes found", __func__, port_maxbw_propname);
191910795SWentao.Yang@Sun.COM 	}
192010795SWentao.Yang@Sun.COM }
192110795SWentao.Yang@Sun.COM 
19226419Ssb155480 /*
19235935Ssb155480  * This function reads "priority-ether-types" property from md. This property
19245935Ssb155480  * is used to enable support for priority frames. Applications which need
19255935Ssb155480  * guaranteed and timely delivery of certain high priority frames to/from
19265935Ssb155480  * a vnet or vsw within ldoms, should configure this property by providing
19275935Ssb155480  * the ether type(s) for which the priority facility is needed.
19285935Ssb155480  * Normal data frames are delivered over a ldc channel using the descriptor
19295935Ssb155480  * ring mechanism which is constrained by factors such as descriptor ring size,
19305935Ssb155480  * the rate at which the ring is processed at the peer ldc end point, etc.
19315935Ssb155480  * The priority mechanism provides an Out-Of-Band path to send/receive frames
19325935Ssb155480  * as raw pkt data (VIO_PKT_DATA) messages over the channel, avoiding the
19335935Ssb155480  * descriptor ring path and enables a more reliable and timely delivery of
19345935Ssb155480  * frames to the peer.
19355935Ssb155480  */
19365935Ssb155480 static void
vsw_read_pri_eth_types(vsw_t * vswp,md_t * mdp,mde_cookie_t node)19375935Ssb155480 vsw_read_pri_eth_types(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
19385935Ssb155480 {
19395935Ssb155480 	int		rv;
19405935Ssb155480 	uint16_t	*types;
19415935Ssb155480 	uint64_t	*data;
19425935Ssb155480 	int		size;
19435935Ssb155480 	int		i;
19445935Ssb155480 	size_t		mblk_sz;
19455935Ssb155480 
19465935Ssb155480 	rv = md_get_prop_data(mdp, node, pri_types_propname,
19475935Ssb155480 	    (uint8_t **)&data, &size);
19485935Ssb155480 	if (rv != 0) {
19495935Ssb155480 		/*
19505935Ssb155480 		 * Property may not exist if we are running pre-ldoms1.1 f/w.
19515935Ssb155480 		 * Check if 'vsw_pri_eth_type' has been set in that case.
19525935Ssb155480 		 */
19535935Ssb155480 		if (vsw_pri_eth_type != 0) {
19545935Ssb155480 			size = sizeof (vsw_pri_eth_type);
19555935Ssb155480 			data = &vsw_pri_eth_type;
19565935Ssb155480 		} else {
19575935Ssb155480 			D3(vswp, "%s: prop(%s) not found", __func__,
19585935Ssb155480 			    pri_types_propname);
19595935Ssb155480 			size = 0;
19605935Ssb155480 		}
19615935Ssb155480 	}
19625935Ssb155480 
19635935Ssb155480 	if (size == 0) {
19645935Ssb155480 		vswp->pri_num_types = 0;
19655935Ssb155480 		return;
19665935Ssb155480 	}
19675935Ssb155480 
19685935Ssb155480 	/*
19695935Ssb155480 	 * we have some priority-ether-types defined;
19705935Ssb155480 	 * allocate a table of these types and also
19715935Ssb155480 	 * allocate a pool of mblks to transmit these
19725935Ssb155480 	 * priority packets.
19735935Ssb155480 	 */
19745935Ssb155480 	size /= sizeof (uint64_t);
19755935Ssb155480 	vswp->pri_num_types = size;
19765935Ssb155480 	vswp->pri_types = kmem_zalloc(size * sizeof (uint16_t), KM_SLEEP);
19775935Ssb155480 	for (i = 0, types = vswp->pri_types; i < size; i++) {
19785935Ssb155480 		types[i] = data[i] & 0xFFFF;
19795935Ssb155480 	}
19805935Ssb155480 	mblk_sz = (VIO_PKT_DATA_HDRSIZE + ETHERMAX + 7) & ~7;
198112011SSriharsha.Basavapatna@Sun.COM 	(void) vio_create_mblks(vsw_pri_tx_nmblks, mblk_sz, NULL,
198212011SSriharsha.Basavapatna@Sun.COM 	    &vswp->pri_tx_vmp);
19835935Ssb155480 }
19845935Ssb155480 
19857529SSriharsha.Basavapatna@Sun.COM static void
vsw_mtu_read(vsw_t * vswp,md_t * mdp,mde_cookie_t node,uint32_t * mtu)19867529SSriharsha.Basavapatna@Sun.COM vsw_mtu_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node, uint32_t *mtu)
19877529SSriharsha.Basavapatna@Sun.COM {
19887529SSriharsha.Basavapatna@Sun.COM 	int		rv;
19897529SSriharsha.Basavapatna@Sun.COM 	int		inst;
19907529SSriharsha.Basavapatna@Sun.COM 	uint64_t	val;
19917529SSriharsha.Basavapatna@Sun.COM 	char		*mtu_propname;
19927529SSriharsha.Basavapatna@Sun.COM 
19937529SSriharsha.Basavapatna@Sun.COM 	mtu_propname = vsw_mtu_propname;
19947529SSriharsha.Basavapatna@Sun.COM 	inst = vswp->instance;
19957529SSriharsha.Basavapatna@Sun.COM 
19967529SSriharsha.Basavapatna@Sun.COM 	rv = md_get_prop_val(mdp, node, mtu_propname, &val);
19977529SSriharsha.Basavapatna@Sun.COM 	if (rv != 0) {
19987529SSriharsha.Basavapatna@Sun.COM 		D3(vswp, "%s: prop(%s) not found", __func__, mtu_propname);
19997529SSriharsha.Basavapatna@Sun.COM 		*mtu = vsw_ethermtu;
20007529SSriharsha.Basavapatna@Sun.COM 	} else {
20017529SSriharsha.Basavapatna@Sun.COM 
20027529SSriharsha.Basavapatna@Sun.COM 		*mtu = val & 0xFFFF;
20037529SSriharsha.Basavapatna@Sun.COM 		D2(vswp, "%s: %s(%d): (%d)\n", __func__,
20047529SSriharsha.Basavapatna@Sun.COM 		    mtu_propname, inst, *mtu);
20057529SSriharsha.Basavapatna@Sun.COM 	}
20067529SSriharsha.Basavapatna@Sun.COM }
20077529SSriharsha.Basavapatna@Sun.COM 
20087529SSriharsha.Basavapatna@Sun.COM /*
20097529SSriharsha.Basavapatna@Sun.COM  * Update the mtu of the vsw device. We first check if the device has been
20107529SSriharsha.Basavapatna@Sun.COM  * plumbed and if so fail the mtu update. Otherwise, we continue to update the
20117529SSriharsha.Basavapatna@Sun.COM  * new mtu and reset all ports to initiate handshake re-negotiation with peers
20127529SSriharsha.Basavapatna@Sun.COM  * using the new mtu.
20137529SSriharsha.Basavapatna@Sun.COM  */
20147529SSriharsha.Basavapatna@Sun.COM static int
vsw_mtu_update(vsw_t * vswp,uint32_t mtu)20157529SSriharsha.Basavapatna@Sun.COM vsw_mtu_update(vsw_t *vswp, uint32_t mtu)
20167529SSriharsha.Basavapatna@Sun.COM {
20177529SSriharsha.Basavapatna@Sun.COM 	int	rv;
20187529SSriharsha.Basavapatna@Sun.COM 
20197529SSriharsha.Basavapatna@Sun.COM 	WRITE_ENTER(&vswp->if_lockrw);
20207529SSriharsha.Basavapatna@Sun.COM 
20217529SSriharsha.Basavapatna@Sun.COM 	if (vswp->if_state & VSW_IF_UP) {
20227529SSriharsha.Basavapatna@Sun.COM 
20237529SSriharsha.Basavapatna@Sun.COM 		RW_EXIT(&vswp->if_lockrw);
20247529SSriharsha.Basavapatna@Sun.COM 
20257529SSriharsha.Basavapatna@Sun.COM 		cmn_err(CE_NOTE, "!vsw%d: Unable to process mtu update"
20267529SSriharsha.Basavapatna@Sun.COM 		    " as the device is plumbed\n", vswp->instance);
20277529SSriharsha.Basavapatna@Sun.COM 		return (EBUSY);
20287529SSriharsha.Basavapatna@Sun.COM 
20297529SSriharsha.Basavapatna@Sun.COM 	} else {
20307529SSriharsha.Basavapatna@Sun.COM 
20317529SSriharsha.Basavapatna@Sun.COM 		D2(vswp, "%s: curr_mtu(%d) new_mtu(%d)\n",
20327529SSriharsha.Basavapatna@Sun.COM 		    __func__, vswp->mtu, mtu);
20337529SSriharsha.Basavapatna@Sun.COM 
20347529SSriharsha.Basavapatna@Sun.COM 		vswp->mtu = mtu;
20357529SSriharsha.Basavapatna@Sun.COM 		vswp->max_frame_size = vswp->mtu +
20367529SSriharsha.Basavapatna@Sun.COM 		    sizeof (struct ether_header) + VLAN_TAGSZ;
20377529SSriharsha.Basavapatna@Sun.COM 
20387529SSriharsha.Basavapatna@Sun.COM 		rv = mac_maxsdu_update(vswp->if_mh, mtu);
20397529SSriharsha.Basavapatna@Sun.COM 		if (rv != 0) {
20407529SSriharsha.Basavapatna@Sun.COM 			cmn_err(CE_NOTE,
20417529SSriharsha.Basavapatna@Sun.COM 			    "!vsw%d: Unable to update mtu with mac"
20427529SSriharsha.Basavapatna@Sun.COM 			    " layer\n", vswp->instance);
20437529SSriharsha.Basavapatna@Sun.COM 		}
20447529SSriharsha.Basavapatna@Sun.COM 
20457529SSriharsha.Basavapatna@Sun.COM 		RW_EXIT(&vswp->if_lockrw);
20467529SSriharsha.Basavapatna@Sun.COM 
20477529SSriharsha.Basavapatna@Sun.COM 		/* Reset ports to renegotiate with the new mtu */
20487529SSriharsha.Basavapatna@Sun.COM 		vsw_reset_ports(vswp);
20497529SSriharsha.Basavapatna@Sun.COM 
20507529SSriharsha.Basavapatna@Sun.COM 	}
20517529SSriharsha.Basavapatna@Sun.COM 
20527529SSriharsha.Basavapatna@Sun.COM 	return (0);
20537529SSriharsha.Basavapatna@Sun.COM }
20547529SSriharsha.Basavapatna@Sun.COM 
20559336SSriharsha.Basavapatna@Sun.COM static void
vsw_linkprop_read(vsw_t * vswp,md_t * mdp,mde_cookie_t node,boolean_t * pls)20569336SSriharsha.Basavapatna@Sun.COM vsw_linkprop_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
20579336SSriharsha.Basavapatna@Sun.COM 	boolean_t *pls)
20589336SSriharsha.Basavapatna@Sun.COM {
20599336SSriharsha.Basavapatna@Sun.COM 	int		rv;
20609336SSriharsha.Basavapatna@Sun.COM 	uint64_t	val;
20619336SSriharsha.Basavapatna@Sun.COM 	char		*linkpropname;
20629336SSriharsha.Basavapatna@Sun.COM 
20639336SSriharsha.Basavapatna@Sun.COM 	linkpropname = vsw_linkprop_propname;
20649336SSriharsha.Basavapatna@Sun.COM 
20659336SSriharsha.Basavapatna@Sun.COM 	rv = md_get_prop_val(mdp, node, linkpropname, &val);
20669336SSriharsha.Basavapatna@Sun.COM 	if (rv != 0) {
20679336SSriharsha.Basavapatna@Sun.COM 		D3(vswp, "%s: prop(%s) not found", __func__, linkpropname);
20689336SSriharsha.Basavapatna@Sun.COM 		*pls = B_FALSE;
20699336SSriharsha.Basavapatna@Sun.COM 	} else {
20709336SSriharsha.Basavapatna@Sun.COM 
20719336SSriharsha.Basavapatna@Sun.COM 		*pls = (val & 0x1) ? B_TRUE : B_FALSE;
20729336SSriharsha.Basavapatna@Sun.COM 		D2(vswp, "%s: %s(%d): (%d)\n", __func__, linkpropname,
20739336SSriharsha.Basavapatna@Sun.COM 		    vswp->instance, *pls);
20749336SSriharsha.Basavapatna@Sun.COM 	}
20759336SSriharsha.Basavapatna@Sun.COM }
20769336SSriharsha.Basavapatna@Sun.COM 
20779819SSriharsha.Basavapatna@Sun.COM void
vsw_mac_link_update(vsw_t * vswp,link_state_t link_state)20789336SSriharsha.Basavapatna@Sun.COM vsw_mac_link_update(vsw_t *vswp, link_state_t link_state)
20799336SSriharsha.Basavapatna@Sun.COM {
20809336SSriharsha.Basavapatna@Sun.COM 	READ_ENTER(&vswp->if_lockrw);
20819819SSriharsha.Basavapatna@Sun.COM 
20829819SSriharsha.Basavapatna@Sun.COM 	if (vswp->if_state & VSW_IF_REG) {
20839819SSriharsha.Basavapatna@Sun.COM 		mac_link_update(vswp->if_mh, link_state);
20849336SSriharsha.Basavapatna@Sun.COM 	}
20859819SSriharsha.Basavapatna@Sun.COM 
20869336SSriharsha.Basavapatna@Sun.COM 	RW_EXIT(&vswp->if_lockrw);
20879336SSriharsha.Basavapatna@Sun.COM }
20889336SSriharsha.Basavapatna@Sun.COM 
20899336SSriharsha.Basavapatna@Sun.COM void
vsw_physlink_state_update(vsw_t * vswp)20909336SSriharsha.Basavapatna@Sun.COM vsw_physlink_state_update(vsw_t *vswp)
20919336SSriharsha.Basavapatna@Sun.COM {
20929336SSriharsha.Basavapatna@Sun.COM 	if (vswp->pls_update == B_TRUE) {
20939336SSriharsha.Basavapatna@Sun.COM 		vsw_mac_link_update(vswp, vswp->phys_link_state);
20949336SSriharsha.Basavapatna@Sun.COM 	}
20959336SSriharsha.Basavapatna@Sun.COM 	vsw_physlink_update_ports(vswp);
20969336SSriharsha.Basavapatna@Sun.COM }
20979336SSriharsha.Basavapatna@Sun.COM 
209810795SWentao.Yang@Sun.COM static void
vsw_bandwidth_read(vsw_t * vswp,md_t * mdp,mde_cookie_t node,uint64_t * bw)209910795SWentao.Yang@Sun.COM vsw_bandwidth_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node, uint64_t *bw)
210010795SWentao.Yang@Sun.COM {
210110795SWentao.Yang@Sun.COM 	/* read the vsw bandwidth from md */
210210795SWentao.Yang@Sun.COM 	int		rv;
210310795SWentao.Yang@Sun.COM 	uint64_t	val;
210410795SWentao.Yang@Sun.COM 
210510795SWentao.Yang@Sun.COM 	rv = md_get_prop_val(mdp, node, vsw_maxbw_propname, &val);
210610795SWentao.Yang@Sun.COM 	if (rv != 0) {
210710795SWentao.Yang@Sun.COM 		*bw = 0;
210810795SWentao.Yang@Sun.COM 		D3(vswp, "%s: prop(%s) not found", __func__,
210910795SWentao.Yang@Sun.COM 		    vsw_maxbw_propname);
211010795SWentao.Yang@Sun.COM 	} else {
211110795SWentao.Yang@Sun.COM 		*bw = val;
211210795SWentao.Yang@Sun.COM 		D3(vswp, "%s: %s(%d): (%ld)\n", __func__,
211310795SWentao.Yang@Sun.COM 		    vsw_maxbw_propname, vswp->instance, *bw);
211410795SWentao.Yang@Sun.COM 	}
211510795SWentao.Yang@Sun.COM }
211610795SWentao.Yang@Sun.COM 
21175935Ssb155480 /*
21183166Ssg70180  * Check to see if the relevant properties in the specified node have
21193166Ssg70180  * changed, and if so take the appropriate action.
21203166Ssg70180  *
21213166Ssg70180  * If any of the properties are missing or invalid we don't take
21223166Ssg70180  * any action, as this function should only be invoked when modifications
21233166Ssg70180  * have been made to what we assume is a working configuration, which
21243166Ssg70180  * we leave active.
21253166Ssg70180  *
21263166Ssg70180  * Note it is legal for this routine to be invoked even if none of the
21273166Ssg70180  * properties in the port node within the MD have actually changed.
21283166Ssg70180  */
21293166Ssg70180 static void
vsw_update_md_prop(vsw_t * vswp,md_t * mdp,mde_cookie_t node)21303166Ssg70180 vsw_update_md_prop(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
21313166Ssg70180 {
21323166Ssg70180 	char		physname[LIFNAMSIZ];
21333166Ssg70180 	char		drv[LIFNAMSIZ];
21343166Ssg70180 	uint_t		ddi_instance;
21358275SEric Cheng 	uint8_t		new_smode;
21368275SEric Cheng 	int		i;
21373166Ssg70180 	uint64_t 	macaddr = 0;
21383166Ssg70180 	enum		{MD_init = 0x1,
21393166Ssg70180 				MD_physname = 0x2,
21403166Ssg70180 				MD_macaddr = 0x4,
21416419Ssb155480 				MD_smode = 0x8,
21427529SSriharsha.Basavapatna@Sun.COM 				MD_vlans = 0x10,
21439336SSriharsha.Basavapatna@Sun.COM 				MD_mtu = 0x20,
214410795SWentao.Yang@Sun.COM 				MD_pls = 0x40,
214510795SWentao.Yang@Sun.COM 				MD_bw = 0x80} updated;
21465171Ssb155480 	int		rv;
21476419Ssb155480 	uint16_t	pvid;
21488275SEric Cheng 	vsw_vlanid_t	*vids;
21496419Ssb155480 	uint16_t	nvids;
21507529SSriharsha.Basavapatna@Sun.COM 	uint32_t	mtu;
21519336SSriharsha.Basavapatna@Sun.COM 	boolean_t	pls_update;
215210795SWentao.Yang@Sun.COM 	uint64_t	maxbw;
21533166Ssg70180 
21543166Ssg70180 	updated = MD_init;
21553166Ssg70180 
21563166Ssg70180 	D1(vswp, "%s: enter", __func__);
21573166Ssg70180 
21583166Ssg70180 	/*
21593166Ssg70180 	 * Check if name of physical device in MD has changed.
21603166Ssg70180 	 */
21613166Ssg70180 	if (vsw_get_md_physname(vswp, mdp, node, (char *)&physname) == 0) {
21623166Ssg70180 		/*
21633166Ssg70180 		 * Do basic sanity check on new device name/instance,
21643166Ssg70180 		 * if its non NULL. It is valid for the device name to
21653166Ssg70180 		 * have changed from a non NULL to a NULL value, i.e.
21663166Ssg70180 		 * the vsw is being changed to 'routed' mode.
21673166Ssg70180 		 */
21683166Ssg70180 		if ((strlen(physname) != 0) &&
21695171Ssb155480 		    (ddi_parse(physname, drv,
21705171Ssb155480 		    &ddi_instance) != DDI_SUCCESS)) {
21716241Snarayan 			cmn_err(CE_WARN, "!vsw%d: physical device %s is not"
21724838Slm66018 			    " a valid device name/instance",
21734838Slm66018 			    vswp->instance, physname);
21743166Ssg70180 			goto fail_reconf;
21753166Ssg70180 		}
21763166Ssg70180 
21773166Ssg70180 		if (strcmp(physname, vswp->physname)) {
21783166Ssg70180 			D2(vswp, "%s: device name changed from %s to %s",
21794838Slm66018 			    __func__, vswp->physname, physname);
21803166Ssg70180 
21813166Ssg70180 			updated |= MD_physname;
21823166Ssg70180 		} else {
21833166Ssg70180 			D2(vswp, "%s: device name unchanged at %s",
21844838Slm66018 			    __func__, vswp->physname);
21853166Ssg70180 		}
21863166Ssg70180 	} else {
21873166Ssg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read name of physical "
21884838Slm66018 		    "device from updated MD.", vswp->instance);
21893166Ssg70180 		goto fail_reconf;
21903166Ssg70180 	}
21913166Ssg70180 
21923166Ssg70180 	/*
21933166Ssg70180 	 * Check if MAC address has changed.
21943166Ssg70180 	 */
21953166Ssg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
21963166Ssg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
21974838Slm66018 		    vswp->instance);
21983166Ssg70180 		goto fail_reconf;
21993166Ssg70180 	} else {
22005171Ssb155480 		uint64_t maddr = macaddr;
22013166Ssg70180 		READ_ENTER(&vswp->if_lockrw);
22023166Ssg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
22035171Ssb155480 			if (vswp->if_addr.ether_addr_octet[i]
22045171Ssb155480 			    != (macaddr & 0xFF)) {
22053166Ssg70180 				D2(vswp, "%s: octet[%d] 0x%x != 0x%x",
22064838Slm66018 				    __func__, i,
22074838Slm66018 				    vswp->if_addr.ether_addr_octet[i],
22084838Slm66018 				    (macaddr & 0xFF));
22093166Ssg70180 				updated |= MD_macaddr;
22105171Ssb155480 				macaddr = maddr;
22113166Ssg70180 				break;
22123166Ssg70180 			}
22133166Ssg70180 			macaddr >>= 8;
22143166Ssg70180 		}
22153166Ssg70180 		RW_EXIT(&vswp->if_lockrw);
22165171Ssb155480 		if (updated & MD_macaddr) {
22175171Ssb155480 			vsw_save_lmacaddr(vswp, macaddr);
22185171Ssb155480 		}
22193166Ssg70180 	}
22203166Ssg70180 
22213166Ssg70180 	/*
22223166Ssg70180 	 * Check if switching modes have changed.
22233166Ssg70180 	 */
22248275SEric Cheng 	if (vsw_get_md_smodes(vswp, mdp, node, &new_smode)) {
22253166Ssg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read %s property from MD",
22264838Slm66018 		    vswp->instance, smode_propname);
22273166Ssg70180 		goto fail_reconf;
22283166Ssg70180 	} else {
22298275SEric Cheng 		if (new_smode != vswp->smode) {
22308275SEric Cheng 			D2(vswp, "%s: switching mode changed from %d to %d",
22318275SEric Cheng 			    __func__, vswp->smode, new_smode);
22323166Ssg70180 
22338275SEric Cheng 			updated |= MD_smode;
22343166Ssg70180 		}
22353166Ssg70180 	}
22363166Ssg70180 
22376419Ssb155480 	/* Read the vlan ids */
22386419Ssb155480 	vsw_vlan_read_ids(vswp, VSW_LOCALDEV, mdp, node, &pvid, &vids,
22396419Ssb155480 	    &nvids, NULL);
22406419Ssb155480 
22416419Ssb155480 	/* Determine if there are any vlan id updates */
22426419Ssb155480 	if ((pvid != vswp->pvid) ||		/* pvid changed? */
22436419Ssb155480 	    (nvids != vswp->nvids) ||		/* # of vids changed? */
22446419Ssb155480 	    ((nvids != 0) && (vswp->nvids != 0) &&	/* vids changed? */
22458275SEric Cheng 	    !vsw_cmp_vids(vids, vswp->vids, nvids))) {
22466419Ssb155480 		updated |= MD_vlans;
22476419Ssb155480 	}
22486419Ssb155480 
22497529SSriharsha.Basavapatna@Sun.COM 	/* Read mtu */
22507529SSriharsha.Basavapatna@Sun.COM 	vsw_mtu_read(vswp, mdp, node, &mtu);
22517529SSriharsha.Basavapatna@Sun.COM 	if (mtu != vswp->mtu) {
22527529SSriharsha.Basavapatna@Sun.COM 		if (mtu >= ETHERMTU && mtu <= VNET_MAX_MTU) {
22537529SSriharsha.Basavapatna@Sun.COM 			updated |= MD_mtu;
22547529SSriharsha.Basavapatna@Sun.COM 		} else {
22557529SSriharsha.Basavapatna@Sun.COM 			cmn_err(CE_NOTE, "!vsw%d: Unable to process mtu update"
22567529SSriharsha.Basavapatna@Sun.COM 			    " as the specified value:%d is invalid\n",
22577529SSriharsha.Basavapatna@Sun.COM 			    vswp->instance, mtu);
22587529SSriharsha.Basavapatna@Sun.COM 		}
22597529SSriharsha.Basavapatna@Sun.COM 	}
22607529SSriharsha.Basavapatna@Sun.COM 
22613166Ssg70180 	/*
22629336SSriharsha.Basavapatna@Sun.COM 	 * Read the 'linkprop' property.
22639336SSriharsha.Basavapatna@Sun.COM 	 */
22649336SSriharsha.Basavapatna@Sun.COM 	vsw_linkprop_read(vswp, mdp, node, &pls_update);
22659336SSriharsha.Basavapatna@Sun.COM 	if (pls_update != vswp->pls_update) {
22669336SSriharsha.Basavapatna@Sun.COM 		updated |= MD_pls;
22679336SSriharsha.Basavapatna@Sun.COM 	}
22689336SSriharsha.Basavapatna@Sun.COM 
226910795SWentao.Yang@Sun.COM 	/* Read bandwidth */
227010795SWentao.Yang@Sun.COM 	vsw_bandwidth_read(vswp, mdp, node, &maxbw);
227110795SWentao.Yang@Sun.COM 	if (maxbw != vswp->bandwidth) {
227210795SWentao.Yang@Sun.COM 		if (maxbw >= MRP_MAXBW_MINVAL || maxbw == 0) {
227310795SWentao.Yang@Sun.COM 			updated |= MD_bw;
227410795SWentao.Yang@Sun.COM 		} else {
227510795SWentao.Yang@Sun.COM 			cmn_err(CE_NOTE, "!vsw%d: Unable to process bandwidth"
227610795SWentao.Yang@Sun.COM 			    " update as the specified value:%ld is invalid\n",
227710795SWentao.Yang@Sun.COM 			    vswp->instance, maxbw);
227810795SWentao.Yang@Sun.COM 		}
227910795SWentao.Yang@Sun.COM 	}
228010795SWentao.Yang@Sun.COM 
22819336SSriharsha.Basavapatna@Sun.COM 	/*
22823166Ssg70180 	 * Now make any changes which are needed...
22833166Ssg70180 	 */
22849336SSriharsha.Basavapatna@Sun.COM 	if (updated & MD_pls) {
22859336SSriharsha.Basavapatna@Sun.COM 
22869336SSriharsha.Basavapatna@Sun.COM 		/* save the updated property. */
22879336SSriharsha.Basavapatna@Sun.COM 		vswp->pls_update = pls_update;
22889336SSriharsha.Basavapatna@Sun.COM 
22899336SSriharsha.Basavapatna@Sun.COM 		if (pls_update == B_FALSE) {
22909336SSriharsha.Basavapatna@Sun.COM 			/*
22919336SSriharsha.Basavapatna@Sun.COM 			 * Phys link state update is now disabled for this vsw
22929336SSriharsha.Basavapatna@Sun.COM 			 * interface. If we had previously reported a link-down
22939336SSriharsha.Basavapatna@Sun.COM 			 * to the stack, undo that by sending a link-up.
22949336SSriharsha.Basavapatna@Sun.COM 			 */
22959336SSriharsha.Basavapatna@Sun.COM 			if (vswp->phys_link_state == LINK_STATE_DOWN) {
22969336SSriharsha.Basavapatna@Sun.COM 				vsw_mac_link_update(vswp, LINK_STATE_UP);
22979336SSriharsha.Basavapatna@Sun.COM 			}
22989336SSriharsha.Basavapatna@Sun.COM 		} else {
22999336SSriharsha.Basavapatna@Sun.COM 			/*
23009336SSriharsha.Basavapatna@Sun.COM 			 * Phys link state update is now enabled. Send up an
23019336SSriharsha.Basavapatna@Sun.COM 			 * update based on the current phys link state.
23029336SSriharsha.Basavapatna@Sun.COM 			 */
23039819SSriharsha.Basavapatna@Sun.COM 			if (vswp->smode & VSW_LAYER2) {
23049819SSriharsha.Basavapatna@Sun.COM 				vsw_mac_link_update(vswp,
23059819SSriharsha.Basavapatna@Sun.COM 				    vswp->phys_link_state);
23069819SSriharsha.Basavapatna@Sun.COM 			}
23079336SSriharsha.Basavapatna@Sun.COM 		}
23089336SSriharsha.Basavapatna@Sun.COM 
23099336SSriharsha.Basavapatna@Sun.COM 	}
23103166Ssg70180 
23118275SEric Cheng 	if (updated & (MD_physname | MD_smode | MD_mtu)) {
23125171Ssb155480 
23133166Ssg70180 		/*
23148370SSriharsha.Basavapatna@Sun.COM 		 * Stop any pending thread to setup switching mode.
23153166Ssg70180 		 */
23168370SSriharsha.Basavapatna@Sun.COM 		vsw_setup_switching_stop(vswp);
23173166Ssg70180 
23186495Sspeer 		/* Cleanup HybridIO */
23196495Sspeer 		vsw_hio_cleanup(vswp);
23206495Sspeer 
23213166Ssg70180 		/*
23225171Ssb155480 		 * Remove unicst, mcst addrs of vsw interface
23238275SEric Cheng 		 * and ports from the physdev. This also closes
23248275SEric Cheng 		 * the corresponding mac clients.
23253166Ssg70180 		 */
23265171Ssb155480 		vsw_unset_addrs(vswp);
23275171Ssb155480 
23285171Ssb155480 		/*
23295171Ssb155480 		 * Stop, detach and close the old device..
23305171Ssb155480 		 */
23318275SEric Cheng 		mutex_enter(&vswp->mac_lock);
23325171Ssb155480 		vsw_mac_close(vswp);
23338275SEric Cheng 		mutex_exit(&vswp->mac_lock);
23343166Ssg70180 
23353166Ssg70180 		/*
23363166Ssg70180 		 * Update phys name.
23373166Ssg70180 		 */
23383166Ssg70180 		if (updated & MD_physname) {
23393166Ssg70180 			cmn_err(CE_NOTE, "!vsw%d: changing from %s to %s",
23404838Slm66018 			    vswp->instance, vswp->physname, physname);
23413166Ssg70180 			(void) strncpy(vswp->physname,
23424838Slm66018 			    physname, strlen(physname) + 1);
23433166Ssg70180 		}
23443166Ssg70180 
23453166Ssg70180 		/*
23463166Ssg70180 		 * Update array with the new switch mode values.
23473166Ssg70180 		 */
23483166Ssg70180 		if (updated & MD_smode) {
23498275SEric Cheng 			vswp->smode = new_smode;
23508275SEric Cheng 		}
23513166Ssg70180 
23528275SEric Cheng 		/* Update mtu */
23538275SEric Cheng 		if (updated & MD_mtu) {
23548275SEric Cheng 			rv = vsw_mtu_update(vswp, mtu);
23558275SEric Cheng 			if (rv != 0) {
23568275SEric Cheng 				goto fail_update;
23578275SEric Cheng 			}
23583166Ssg70180 		}
23593166Ssg70180 
23603166Ssg70180 		/*
23613166Ssg70180 		 * ..and attach, start the new device.
23623166Ssg70180 		 */
23635171Ssb155480 		rv = vsw_setup_switching(vswp);
23645171Ssb155480 		if (rv == EAGAIN) {
23655171Ssb155480 			/*
23665171Ssb155480 			 * Unable to setup switching mode.
23678370SSriharsha.Basavapatna@Sun.COM 			 * As the error is EAGAIN, schedule a thread to retry
23685171Ssb155480 			 * and return. Programming addresses of ports and
23698370SSriharsha.Basavapatna@Sun.COM 			 * vsw interface will be done by the thread when the
23708370SSriharsha.Basavapatna@Sun.COM 			 * switching setup completes successfully.
23715171Ssb155480 			 */
23728370SSriharsha.Basavapatna@Sun.COM 			if (vsw_setup_switching_start(vswp) != 0) {
23738370SSriharsha.Basavapatna@Sun.COM 				goto fail_update;
23748370SSriharsha.Basavapatna@Sun.COM 			}
23755171Ssb155480 			return;
23765171Ssb155480 
23775171Ssb155480 		} else if (rv) {
23783166Ssg70180 			goto fail_update;
23795171Ssb155480 		}
23803166Ssg70180 
23819819SSriharsha.Basavapatna@Sun.COM 		vsw_setup_switching_post_process(vswp);
23825171Ssb155480 	} else if (updated & MD_macaddr) {
23835171Ssb155480 		/*
23845171Ssb155480 		 * We enter here if only MD_macaddr is exclusively updated.
23855171Ssb155480 		 * If MD_physname and/or MD_smode are also updated, then
23865171Ssb155480 		 * as part of that, we would have implicitly processed
23875171Ssb155480 		 * MD_macaddr update (above).
23885171Ssb155480 		 */
23893166Ssg70180 		cmn_err(CE_NOTE, "!vsw%d: changing mac address to 0x%lx",
23904838Slm66018 		    vswp->instance, macaddr);
23913166Ssg70180 
23925171Ssb155480 		READ_ENTER(&vswp->if_lockrw);
23935171Ssb155480 		if (vswp->if_state & VSW_IF_UP) {
23948275SEric Cheng 			/* reconfigure with new address */
23958275SEric Cheng 			vsw_if_mac_reconfig(vswp, B_FALSE, 0, NULL, 0);
23965171Ssb155480 
23975171Ssb155480 			/*
23985171Ssb155480 			 * Notify the MAC layer of the changed address.
23995171Ssb155480 			 */
24005171Ssb155480 			mac_unicst_update(vswp->if_mh,
24015171Ssb155480 			    (uint8_t *)&vswp->if_addr);
24025171Ssb155480 
24033166Ssg70180 		}
24043166Ssg70180 		RW_EXIT(&vswp->if_lockrw);
24053166Ssg70180 
24063166Ssg70180 	}
24073166Ssg70180 
24086419Ssb155480 	if (updated & MD_vlans) {
24096419Ssb155480 		/* Remove existing vlan ids from the hash table. */
24106419Ssb155480 		vsw_vlan_remove_ids(vswp, VSW_LOCALDEV);
24116419Ssb155480 
24128275SEric Cheng 		if (vswp->if_state & VSW_IF_UP) {
24138275SEric Cheng 			vsw_if_mac_reconfig(vswp, B_TRUE, pvid, vids, nvids);
24148275SEric Cheng 		} else {
24158275SEric Cheng 			if (vswp->nvids != 0) {
24168275SEric Cheng 				kmem_free(vswp->vids,
24178275SEric Cheng 				    sizeof (vsw_vlanid_t) * vswp->nvids);
24188275SEric Cheng 			}
24198275SEric Cheng 			vswp->vids = vids;
24206419Ssb155480 			vswp->nvids = nvids;
24218275SEric Cheng 			vswp->pvid = pvid;
24226419Ssb155480 		}
24236419Ssb155480 
24246419Ssb155480 		/* add these new vlan ids into hash table */
24256419Ssb155480 		vsw_vlan_add_ids(vswp, VSW_LOCALDEV);
24266419Ssb155480 	} else {
24276419Ssb155480 		if (nvids != 0) {
24288275SEric Cheng 			kmem_free(vids, sizeof (vsw_vlanid_t) * nvids);
24296419Ssb155480 		}
24306419Ssb155480 	}
24316419Ssb155480 
243210795SWentao.Yang@Sun.COM 	if (updated & MD_bw) {
243310795SWentao.Yang@Sun.COM 		vsw_update_bandwidth(vswp, NULL, VSW_LOCALDEV, maxbw);
243410795SWentao.Yang@Sun.COM 	}
243510795SWentao.Yang@Sun.COM 
24363166Ssg70180 	return;
24373166Ssg70180 
24383166Ssg70180 fail_reconf:
24393166Ssg70180 	cmn_err(CE_WARN, "!vsw%d: configuration unchanged", vswp->instance);
24403166Ssg70180 	return;
24413166Ssg70180 
24423166Ssg70180 fail_update:
24436241Snarayan 	cmn_err(CE_WARN, "!vsw%d: re-configuration failed",
24444838Slm66018 	    vswp->instance);
24453166Ssg70180 }
24463166Ssg70180 
24473166Ssg70180 /*
24486419Ssb155480  * Read the port's md properties.
24491991Sheppo  */
24506419Ssb155480 static int
vsw_port_read_props(vsw_port_t * portp,vsw_t * vswp,md_t * mdp,mde_cookie_t * node)24516419Ssb155480 vsw_port_read_props(vsw_port_t *portp, vsw_t *vswp,
24526419Ssb155480 	md_t *mdp, mde_cookie_t *node)
24531991Sheppo {
24541991Sheppo 	uint64_t		ldc_id;
24551991Sheppo 	uint8_t			*addrp;
24561991Sheppo 	int			i, addrsz;
24571991Sheppo 	int			num_nodes = 0, nchan = 0;
24581991Sheppo 	int			listsz = 0;
24591991Sheppo 	mde_cookie_t		*listp = NULL;
24601991Sheppo 	struct ether_addr	ea;
24611991Sheppo 	uint64_t		macaddr;
24621991Sheppo 	uint64_t		inst = 0;
24636495Sspeer 	uint64_t		val;
24641991Sheppo 
24651991Sheppo 	if (md_get_prop_val(mdp, *node, id_propname, &inst)) {
24661991Sheppo 		DWARN(vswp, "%s: prop(%s) not found", __func__,
24674838Slm66018 		    id_propname);
24681991Sheppo 		return (1);
24691991Sheppo 	}
24701991Sheppo 
24711991Sheppo 	/*
24721991Sheppo 	 * Find the channel endpoint node(s) (which should be under this
24731991Sheppo 	 * port node) which contain the channel id(s).
24741991Sheppo 	 */
24751991Sheppo 	if ((num_nodes = md_node_count(mdp)) <= 0) {
24761991Sheppo 		DERR(vswp, "%s: invalid number of nodes found (%d)",
24774838Slm66018 		    __func__, num_nodes);
24781991Sheppo 		return (1);
24791991Sheppo 	}
24801991Sheppo 
24813166Ssg70180 	D2(vswp, "%s: %d nodes found", __func__, num_nodes);
24823166Ssg70180 
24831991Sheppo 	/* allocate enough space for node list */
24841991Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
24851991Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
24861991Sheppo 
24874838Slm66018 	nchan = md_scan_dag(mdp, *node, md_find_name(mdp, chan_propname),
24884838Slm66018 	    md_find_name(mdp, "fwd"), listp);
24891991Sheppo 
24901991Sheppo 	if (nchan <= 0) {
24911991Sheppo 		DWARN(vswp, "%s: no %s nodes found", __func__, chan_propname);
24921991Sheppo 		kmem_free(listp, listsz);
24931991Sheppo 		return (1);
24941991Sheppo 	}
24951991Sheppo 
24961991Sheppo 	D2(vswp, "%s: %d %s nodes found", __func__, nchan, chan_propname);
24971991Sheppo 
24981991Sheppo 	/* use property from first node found */
24991991Sheppo 	if (md_get_prop_val(mdp, listp[0], id_propname, &ldc_id)) {
25001991Sheppo 		DWARN(vswp, "%s: prop(%s) not found\n", __func__,
25014838Slm66018 		    id_propname);
25021991Sheppo 		kmem_free(listp, listsz);
25031991Sheppo 		return (1);
25041991Sheppo 	}
25051991Sheppo 
25061991Sheppo 	/* don't need list any more */
25071991Sheppo 	kmem_free(listp, listsz);
25081991Sheppo 
25091991Sheppo 	D2(vswp, "%s: ldc_id 0x%llx", __func__, ldc_id);
25101991Sheppo 
25111991Sheppo 	/* read mac-address property */
25121991Sheppo 	if (md_get_prop_data(mdp, *node, remaddr_propname,
25134838Slm66018 	    &addrp, &addrsz)) {
25141991Sheppo 		DWARN(vswp, "%s: prop(%s) not found",
25154838Slm66018 		    __func__, remaddr_propname);
25161991Sheppo 		return (1);
25171991Sheppo 	}
25181991Sheppo 
25191991Sheppo 	if (addrsz < ETHERADDRL) {
25201991Sheppo 		DWARN(vswp, "%s: invalid address size", __func__);
25211991Sheppo 		return (1);
25221991Sheppo 	}
25231991Sheppo 
25241991Sheppo 	macaddr = *((uint64_t *)addrp);
25251991Sheppo 	D2(vswp, "%s: remote mac address 0x%llx", __func__, macaddr);
25261991Sheppo 
25271991Sheppo 	for (i = ETHERADDRL - 1; i >= 0; i--) {
25281991Sheppo 		ea.ether_addr_octet[i] = macaddr & 0xFF;
25291991Sheppo 		macaddr >>= 8;
25301991Sheppo 	}
25311991Sheppo 
25326419Ssb155480 	/* now update all properties into the port */
25336419Ssb155480 	portp->p_vswp = vswp;
25346419Ssb155480 	portp->p_instance = inst;
25358275SEric Cheng 	portp->addr_set = B_FALSE;
25366419Ssb155480 	ether_copy(&ea, &portp->p_macaddr);
25376419Ssb155480 	if (nchan > VSW_PORT_MAX_LDCS) {
25386419Ssb155480 		D2(vswp, "%s: using first of %d ldc ids",
25396419Ssb155480 		    __func__, nchan);
25406419Ssb155480 		nchan = VSW_PORT_MAX_LDCS;
25416419Ssb155480 	}
25426419Ssb155480 	portp->num_ldcs = nchan;
25436419Ssb155480 	portp->ldc_ids =
25446419Ssb155480 	    kmem_zalloc(sizeof (uint64_t) * nchan, KM_SLEEP);
25456419Ssb155480 	bcopy(&ldc_id, (portp->ldc_ids), sizeof (uint64_t) * nchan);
25466419Ssb155480 
25476419Ssb155480 	/* read vlan id properties of this port node */
25486419Ssb155480 	vsw_vlan_read_ids(portp, VSW_VNETPORT, mdp, *node, &portp->pvid,
25496419Ssb155480 	    &portp->vids, &portp->nvids, NULL);
25506419Ssb155480 
25516495Sspeer 	/* Check if hybrid property is present */
25526495Sspeer 	if (md_get_prop_val(mdp, *node, hybrid_propname, &val) == 0) {
25536495Sspeer 		D1(vswp, "%s: prop(%s) found\n", __func__, hybrid_propname);
25546495Sspeer 		portp->p_hio_enabled = B_TRUE;
25556495Sspeer 	} else {
25566495Sspeer 		portp->p_hio_enabled = B_FALSE;
25576495Sspeer 	}
25586495Sspeer 	/*
25596495Sspeer 	 * Port hio capability determined after version
25606495Sspeer 	 * negotiation, i.e., when we know the peer is HybridIO capable.
25616495Sspeer 	 */
25626495Sspeer 	portp->p_hio_capable = B_FALSE;
256310795SWentao.Yang@Sun.COM 
256410795SWentao.Yang@Sun.COM 	/* Read bandwidth of this port */
256510795SWentao.Yang@Sun.COM 	vsw_port_read_bandwidth(portp, mdp, *node, &portp->p_bandwidth);
256610795SWentao.Yang@Sun.COM 
25676419Ssb155480 	return (0);
25686419Ssb155480 }
25696419Ssb155480 
25706419Ssb155480 /*
25716419Ssb155480  * Add a new port to the system.
25726419Ssb155480  *
25736419Ssb155480  * Returns 0 on success, 1 on failure.
25746419Ssb155480  */
25756419Ssb155480 int
vsw_port_add(vsw_t * vswp,md_t * mdp,mde_cookie_t * node)25766419Ssb155480 vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node)
25776419Ssb155480 {
25786419Ssb155480 	vsw_port_t	*portp;
25796419Ssb155480 	int		rv;
25806419Ssb155480 
25816419Ssb155480 	portp = kmem_zalloc(sizeof (vsw_port_t), KM_SLEEP);
25826419Ssb155480 
25836419Ssb155480 	rv = vsw_port_read_props(portp, vswp, mdp, node);
25846419Ssb155480 	if (rv != 0) {
25856419Ssb155480 		kmem_free(portp, sizeof (*portp));
25866419Ssb155480 		return (1);
25876419Ssb155480 	}
25886419Ssb155480 
25896419Ssb155480 	rv = vsw_port_attach(portp);
25906419Ssb155480 	if (rv != 0) {
25911991Sheppo 		DERR(vswp, "%s: failed to attach port", __func__);
25921991Sheppo 		return (1);
25931991Sheppo 	}
25941991Sheppo 
25956419Ssb155480 	return (0);
25966419Ssb155480 }
25976419Ssb155480 
25986419Ssb155480 static int
vsw_port_update(vsw_t * vswp,md_t * curr_mdp,mde_cookie_t curr_mdex,md_t * prev_mdp,mde_cookie_t prev_mdex)25996419Ssb155480 vsw_port_update(vsw_t *vswp, md_t *curr_mdp, mde_cookie_t curr_mdex,
26006419Ssb155480 	md_t *prev_mdp, mde_cookie_t prev_mdex)
26016419Ssb155480 {
26026419Ssb155480 	uint64_t	cport_num;
26036419Ssb155480 	uint64_t	pport_num;
26046419Ssb155480 	vsw_port_list_t	*plistp;
26056419Ssb155480 	vsw_port_t	*portp;
26066419Ssb155480 	uint16_t	pvid;
26078275SEric Cheng 	vsw_vlanid_t	*vids;
26086419Ssb155480 	uint16_t	nvids;
26096495Sspeer 	uint64_t	val;
26106495Sspeer 	boolean_t	hio_enabled = B_FALSE;
261110795SWentao.Yang@Sun.COM 	uint64_t	maxbw;
261210795SWentao.Yang@Sun.COM 	enum		{P_MD_init = 0x1,
261310795SWentao.Yang@Sun.COM 				P_MD_vlans = 0x2,
261410795SWentao.Yang@Sun.COM 				P_MD_hio = 0x4,
261510795SWentao.Yang@Sun.COM 				P_MD_maxbw = 0x8} updated;
261610795SWentao.Yang@Sun.COM 
261710795SWentao.Yang@Sun.COM 	updated = P_MD_init;
26186419Ssb155480 
26196419Ssb155480 	/*
26206419Ssb155480 	 * For now, we get port updates only if vlan ids changed.
26216419Ssb155480 	 * We read the port num and do some sanity check.
26226419Ssb155480 	 */
26236419Ssb155480 	if (md_get_prop_val(curr_mdp, curr_mdex, id_propname, &cport_num)) {
26246419Ssb155480 		return (1);
26256419Ssb155480 	}
26266419Ssb155480 
26276419Ssb155480 	if (md_get_prop_val(prev_mdp, prev_mdex, id_propname, &pport_num)) {
26286419Ssb155480 		return (1);
26296419Ssb155480 	}
26306419Ssb155480 	if (cport_num != pport_num)
26316419Ssb155480 		return (1);
26326419Ssb155480 
26336419Ssb155480 	plistp = &(vswp->plist);
26346419Ssb155480 
26356419Ssb155480 	READ_ENTER(&plistp->lockrw);
26366419Ssb155480 
26376419Ssb155480 	portp = vsw_lookup_port(vswp, cport_num);
26386419Ssb155480 	if (portp == NULL) {
26396419Ssb155480 		RW_EXIT(&plistp->lockrw);
26406419Ssb155480 		return (1);
26416419Ssb155480 	}
26421991Sheppo 
26436419Ssb155480 	/* Read the vlan ids */
26446419Ssb155480 	vsw_vlan_read_ids(portp, VSW_VNETPORT, curr_mdp, curr_mdex, &pvid,
26456419Ssb155480 	    &vids, &nvids, NULL);
26466419Ssb155480 
26476419Ssb155480 	/* Determine if there are any vlan id updates */
26486419Ssb155480 	if ((pvid != portp->pvid) ||		/* pvid changed? */
26496419Ssb155480 	    (nvids != portp->nvids) ||		/* # of vids changed? */
26506419Ssb155480 	    ((nvids != 0) && (portp->nvids != 0) &&	/* vids changed? */
26518275SEric Cheng 	    !vsw_cmp_vids(vids, portp->vids, nvids))) {
265210795SWentao.Yang@Sun.COM 		updated |= P_MD_vlans;
265310795SWentao.Yang@Sun.COM 	}
265410795SWentao.Yang@Sun.COM 
265510795SWentao.Yang@Sun.COM 	/* Check if hybrid property is present */
265610795SWentao.Yang@Sun.COM 	if (md_get_prop_val(curr_mdp, curr_mdex, hybrid_propname, &val) == 0) {
265710795SWentao.Yang@Sun.COM 		D1(vswp, "%s: prop(%s) found\n", __func__, hybrid_propname);
265810795SWentao.Yang@Sun.COM 		hio_enabled = B_TRUE;
265910795SWentao.Yang@Sun.COM 	}
266010795SWentao.Yang@Sun.COM 
266110795SWentao.Yang@Sun.COM 	if (portp->p_hio_enabled != hio_enabled) {
266210795SWentao.Yang@Sun.COM 		updated |= P_MD_hio;
26636419Ssb155480 	}
26646419Ssb155480 
266510795SWentao.Yang@Sun.COM 	/* Check if maxbw property is present */
266610795SWentao.Yang@Sun.COM 	vsw_port_read_bandwidth(portp, curr_mdp, curr_mdex, &maxbw);
266710795SWentao.Yang@Sun.COM 	if (maxbw != portp->p_bandwidth) {
266810795SWentao.Yang@Sun.COM 		if (maxbw >= MRP_MAXBW_MINVAL || maxbw == 0) {
266910795SWentao.Yang@Sun.COM 			updated |= P_MD_maxbw;
267010795SWentao.Yang@Sun.COM 		} else {
267110795SWentao.Yang@Sun.COM 			cmn_err(CE_NOTE, "!vsw%d: Unable to process bandwidth"
267210795SWentao.Yang@Sun.COM 			    " update for port %d as the specified value:%ld"
267310795SWentao.Yang@Sun.COM 			    " is invalid\n",
267410795SWentao.Yang@Sun.COM 			    vswp->instance, portp->p_instance, maxbw);
267510795SWentao.Yang@Sun.COM 		}
267610795SWentao.Yang@Sun.COM 	}
267710795SWentao.Yang@Sun.COM 
267810795SWentao.Yang@Sun.COM 	if (updated & P_MD_vlans) {
26796495Sspeer 		/* Remove existing vlan ids from the hash table. */
26806495Sspeer 		vsw_vlan_remove_ids(portp, VSW_VNETPORT);
26816495Sspeer 
26828275SEric Cheng 		/* Reconfigure vlans with network device */
26838275SEric Cheng 		vsw_mac_port_reconfig_vlans(portp, pvid, vids, nvids);
26846495Sspeer 
26856495Sspeer 		/* add these new vlan ids into hash table */
26866495Sspeer 		vsw_vlan_add_ids(portp, VSW_VNETPORT);
26876495Sspeer 
26886495Sspeer 		/* reset the port if it is vlan unaware (ver < 1.3) */
26896495Sspeer 		vsw_vlan_unaware_port_reset(portp);
26906419Ssb155480 	}
26916419Ssb155480 
269210795SWentao.Yang@Sun.COM 	if (updated & P_MD_hio) {
269310795SWentao.Yang@Sun.COM 		vsw_hio_port_update(portp, hio_enabled);
26946419Ssb155480 	}
26956419Ssb155480 
269610795SWentao.Yang@Sun.COM 	if (updated & P_MD_maxbw) {
269710795SWentao.Yang@Sun.COM 		vsw_update_bandwidth(NULL, portp, VSW_VNETPORT, maxbw);
26986495Sspeer 	}
26996419Ssb155480 
27006419Ssb155480 	RW_EXIT(&plistp->lockrw);
27011991Sheppo 
27021991Sheppo 	return (0);
27031991Sheppo }
27041991Sheppo 
27051991Sheppo /*
27065373Sraghuram  * vsw_mac_rx -- A common function to send packets to the interface.
27075373Sraghuram  * By default this function check if the interface is UP or not, the
27085373Sraghuram  * rest of the behaviour depends on the flags as below:
27091991Sheppo  *
27105373Sraghuram  *	VSW_MACRX_PROMISC -- Check if the promisc mode set or not.
27115373Sraghuram  *	VSW_MACRX_COPYMSG -- Make a copy of the message(s).
27125373Sraghuram  *	VSW_MACRX_FREEMSG -- Free if the messages cannot be sent up the stack.
27131991Sheppo  */
27145373Sraghuram void
vsw_mac_rx(vsw_t * vswp,mac_resource_handle_t mrh,mblk_t * mp,vsw_macrx_flags_t flags)27155935Ssb155480 vsw_mac_rx(vsw_t *vswp, mac_resource_handle_t mrh,
27165935Ssb155480     mblk_t *mp, vsw_macrx_flags_t flags)
27171991Sheppo {
27186419Ssb155480 	mblk_t		*mpt;
27196419Ssb155480 
27205373Sraghuram 	D1(vswp, "%s:enter\n", __func__);
27215373Sraghuram 	READ_ENTER(&vswp->if_lockrw);
27225373Sraghuram 	/* Check if the interface is up */
27235373Sraghuram 	if (!(vswp->if_state & VSW_IF_UP)) {
27245373Sraghuram 		RW_EXIT(&vswp->if_lockrw);
27255373Sraghuram 		/* Free messages only if FREEMSG flag specified */
27265373Sraghuram 		if (flags & VSW_MACRX_FREEMSG) {
27275373Sraghuram 			freemsgchain(mp);
27281991Sheppo 		}
27295373Sraghuram 		D1(vswp, "%s:exit\n", __func__);
27305373Sraghuram 		return;
27311991Sheppo 	}
27321991Sheppo 	/*
27335373Sraghuram 	 * If PROMISC flag is passed, then check if
27345373Sraghuram 	 * the interface is in the PROMISC mode.
27355373Sraghuram 	 * If not, drop the messages.
27361991Sheppo 	 */
27375373Sraghuram 	if (flags & VSW_MACRX_PROMISC) {
27385373Sraghuram 		if (!(vswp->if_state & VSW_IF_PROMISC)) {
27395373Sraghuram 			RW_EXIT(&vswp->if_lockrw);
27405373Sraghuram 			/* Free messages only if FREEMSG flag specified */
27415373Sraghuram 			if (flags & VSW_MACRX_FREEMSG) {
27425373Sraghuram 				freemsgchain(mp);
27435373Sraghuram 			}
27445373Sraghuram 			D1(vswp, "%s:exit\n", __func__);
27453715Ssg70180 			return;
27463715Ssg70180 		}
27473715Ssg70180 	}
27485373Sraghuram 	RW_EXIT(&vswp->if_lockrw);
27493715Ssg70180 	/*
27505373Sraghuram 	 * If COPYMSG flag is passed, then make a copy
27515373Sraghuram 	 * of the message chain and send up the copy.
27522793Slm66018 	 */
27535373Sraghuram 	if (flags & VSW_MACRX_COPYMSG) {
27545373Sraghuram 		mp = copymsgchain(mp);
27555935Ssb155480 		if (mp == NULL) {
27565373Sraghuram 			D1(vswp, "%s:exit\n", __func__);
27571991Sheppo 			return;
27581991Sheppo 		}
27591991Sheppo 	}
27601991Sheppo 
27615935Ssb155480 	D2(vswp, "%s: sending up stack", __func__);
27626419Ssb155480 
27636419Ssb155480 	mpt = NULL;
27646419Ssb155480 	(void) vsw_vlan_frame_untag(vswp, VSW_LOCALDEV, &mp, &mpt);
27656419Ssb155480 	if (mp != NULL) {
27666419Ssb155480 		mac_rx(vswp->if_mh, mrh, mp);
27676419Ssb155480 	}
27685373Sraghuram 	D1(vswp, "%s:exit\n", __func__);
27691991Sheppo }
27701991Sheppo 
27715373Sraghuram /* copy mac address of vsw into soft state structure */
27721991Sheppo static void
vsw_save_lmacaddr(vsw_t * vswp,uint64_t macaddr)27735373Sraghuram vsw_save_lmacaddr(vsw_t *vswp, uint64_t macaddr)
27741991Sheppo {
27751991Sheppo 	int	i;
27761991Sheppo 
27775373Sraghuram 	WRITE_ENTER(&vswp->if_lockrw);
27785373Sraghuram 	for (i = ETHERADDRL - 1; i >= 0; i--) {
27795373Sraghuram 		vswp->if_addr.ether_addr_octet[i] = macaddr & 0xFF;
27805373Sraghuram 		macaddr >>= 8;
27811991Sheppo 	}
27825373Sraghuram 	RW_EXIT(&vswp->if_lockrw);
27831991Sheppo }
27848275SEric Cheng 
27858275SEric Cheng /* Compare VLAN ids, array size expected to be same. */
27868275SEric Cheng static boolean_t
vsw_cmp_vids(vsw_vlanid_t * vids1,vsw_vlanid_t * vids2,int nvids)27878275SEric Cheng vsw_cmp_vids(vsw_vlanid_t *vids1, vsw_vlanid_t *vids2, int nvids)
27888275SEric Cheng {
27898275SEric Cheng 	int i, j;
27908275SEric Cheng 	uint16_t vid;
27918275SEric Cheng 
27928275SEric Cheng 	for (i = 0; i < nvids; i++) {
27938275SEric Cheng 		vid = vids1[i].vl_vid;
27948275SEric Cheng 		for (j = 0; j < nvids; j++) {
27958275SEric Cheng 			if (vid == vids2[i].vl_vid)
27968275SEric Cheng 				break;
27978275SEric Cheng 		}
27988275SEric Cheng 		if (j == nvids) {
27998275SEric Cheng 			return (B_FALSE);
28008275SEric Cheng 		}
28018275SEric Cheng 	}
28028275SEric Cheng 	return (B_TRUE);
28038275SEric Cheng }
2804