xref: /titanic_50/usr/src/uts/sun4v/io/vsw.c (revision 445b4c2ed2d52ef648ae6b36e4f5e14ff3d234af)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo 
221ae08745Sheppo /*
231ae08745Sheppo  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
241ae08745Sheppo  * Use is subject to license terms.
251ae08745Sheppo  */
261ae08745Sheppo 
271ae08745Sheppo #pragma ident	"%Z%%M%	%I%	%E% SMI"
281ae08745Sheppo 
291ae08745Sheppo #include <sys/types.h>
301ae08745Sheppo #include <sys/errno.h>
311ae08745Sheppo #include <sys/debug.h>
321ae08745Sheppo #include <sys/time.h>
331ae08745Sheppo #include <sys/sysmacros.h>
341ae08745Sheppo #include <sys/systm.h>
351ae08745Sheppo #include <sys/user.h>
361ae08745Sheppo #include <sys/stropts.h>
371ae08745Sheppo #include <sys/stream.h>
381ae08745Sheppo #include <sys/strlog.h>
391ae08745Sheppo #include <sys/strsubr.h>
401ae08745Sheppo #include <sys/cmn_err.h>
411ae08745Sheppo #include <sys/cpu.h>
421ae08745Sheppo #include <sys/kmem.h>
431ae08745Sheppo #include <sys/conf.h>
441ae08745Sheppo #include <sys/ddi.h>
451ae08745Sheppo #include <sys/sunddi.h>
461ae08745Sheppo #include <sys/ksynch.h>
471ae08745Sheppo #include <sys/stat.h>
481ae08745Sheppo #include <sys/kstat.h>
491ae08745Sheppo #include <sys/vtrace.h>
501ae08745Sheppo #include <sys/strsun.h>
511ae08745Sheppo #include <sys/dlpi.h>
521ae08745Sheppo #include <sys/ethernet.h>
531ae08745Sheppo #include <net/if.h>
541ae08745Sheppo #include <sys/varargs.h>
551ae08745Sheppo #include <sys/machsystm.h>
561ae08745Sheppo #include <sys/modctl.h>
571ae08745Sheppo #include <sys/modhash.h>
581ae08745Sheppo #include <sys/mac.h>
59ba2e4443Sseb #include <sys/mac_ether.h>
601ae08745Sheppo #include <sys/taskq.h>
611ae08745Sheppo #include <sys/note.h>
621ae08745Sheppo #include <sys/mach_descrip.h>
631ae08745Sheppo #include <sys/mac.h>
641ae08745Sheppo #include <sys/mdeg.h>
651ae08745Sheppo #include <sys/ldc.h>
661ae08745Sheppo #include <sys/vsw_fdb.h>
671ae08745Sheppo #include <sys/vsw.h>
681ae08745Sheppo #include <sys/vio_mailbox.h>
691ae08745Sheppo #include <sys/vnet_mailbox.h>
701ae08745Sheppo #include <sys/vnet_common.h>
71d10e4ef2Snarayan #include <sys/vio_util.h>
72d10e4ef2Snarayan #include <sys/sdt.h>
731ae08745Sheppo 
741ae08745Sheppo /*
751ae08745Sheppo  * Function prototypes.
761ae08745Sheppo  */
771ae08745Sheppo static	int vsw_attach(dev_info_t *, ddi_attach_cmd_t);
781ae08745Sheppo static	int vsw_detach(dev_info_t *, ddi_detach_cmd_t);
791ae08745Sheppo static	int vsw_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
8034683adeSsg70180 static	int vsw_get_md_physname(vsw_t *, md_t *, mde_cookie_t, char *);
8134683adeSsg70180 static	int vsw_get_md_smodes(vsw_t *, md_t *, mde_cookie_t, uint8_t *, int *);
82e1ebb9ecSlm66018 static	int vsw_get_physaddr(vsw_t *);
8334683adeSsg70180 static	int vsw_setup_switching(vsw_t *);
841ae08745Sheppo static	int vsw_setup_layer2(vsw_t *);
851ae08745Sheppo static	int vsw_setup_layer3(vsw_t *);
861ae08745Sheppo 
877636cb21Slm66018 /* MAC Ring table functions. */
887636cb21Slm66018 static void vsw_mac_ring_tbl_init(vsw_t *vswp);
897636cb21Slm66018 static void vsw_mac_ring_tbl_destroy(vsw_t *vswp);
907636cb21Slm66018 static void vsw_queue_worker(vsw_mac_ring_t *rrp);
917636cb21Slm66018 static void vsw_queue_stop(vsw_queue_t *vqp);
927636cb21Slm66018 static vsw_queue_t *vsw_queue_create();
937636cb21Slm66018 static void vsw_queue_destroy(vsw_queue_t *vqp);
947636cb21Slm66018 
951ae08745Sheppo /* MAC layer routines */
967636cb21Slm66018 static mac_resource_handle_t vsw_mac_ring_add_cb(void *arg,
977636cb21Slm66018 		mac_resource_t *mrp);
98e1ebb9ecSlm66018 static	int vsw_get_hw_maddr(vsw_t *);
99e1ebb9ecSlm66018 static	int vsw_set_hw(vsw_t *, vsw_port_t *);
100e1ebb9ecSlm66018 static	int vsw_set_hw_promisc(vsw_t *, vsw_port_t *);
101e1ebb9ecSlm66018 static	int vsw_unset_hw(vsw_t *, vsw_port_t *);
102e1ebb9ecSlm66018 static	int vsw_unset_hw_promisc(vsw_t *, vsw_port_t *);
103e1ebb9ecSlm66018 static	int vsw_reconfig_hw(vsw_t *);
1047636cb21Slm66018 static int vsw_mac_attach(vsw_t *vswp);
1057636cb21Slm66018 static void vsw_mac_detach(vsw_t *vswp);
1067636cb21Slm66018 
1077636cb21Slm66018 static void vsw_rx_queue_cb(void *, mac_resource_handle_t, mblk_t *);
1081ae08745Sheppo static void vsw_rx_cb(void *, mac_resource_handle_t, mblk_t *);
1091ae08745Sheppo static mblk_t *vsw_tx_msg(vsw_t *, mblk_t *);
1101ae08745Sheppo static int vsw_mac_register(vsw_t *);
1111ae08745Sheppo static int vsw_mac_unregister(vsw_t *);
112ba2e4443Sseb static int vsw_m_stat(void *, uint_t, uint64_t *);
1131ae08745Sheppo static void vsw_m_stop(void *arg);
1141ae08745Sheppo static int vsw_m_start(void *arg);
1151ae08745Sheppo static int vsw_m_unicst(void *arg, const uint8_t *);
1161ae08745Sheppo static int vsw_m_multicst(void *arg, boolean_t, const uint8_t *);
1171ae08745Sheppo static int vsw_m_promisc(void *arg, boolean_t);
1181ae08745Sheppo static mblk_t *vsw_m_tx(void *arg, mblk_t *);
1191ae08745Sheppo 
1201ae08745Sheppo /* MDEG routines */
12134683adeSsg70180 static	int vsw_mdeg_register(vsw_t *vswp);
1221ae08745Sheppo static	void vsw_mdeg_unregister(vsw_t *vswp);
1231ae08745Sheppo static	int vsw_mdeg_cb(void *cb_argp, mdeg_result_t *);
12434683adeSsg70180 static	int vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *);
12534683adeSsg70180 static	void vsw_get_initial_md_properties(vsw_t *vswp, md_t *, mde_cookie_t);
12634683adeSsg70180 static	void vsw_update_md_prop(vsw_t *, md_t *, mde_cookie_t);
1271ae08745Sheppo 
1281ae08745Sheppo /* Port add/deletion routines */
1291ae08745Sheppo static	int vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node);
1301ae08745Sheppo static	int vsw_port_attach(vsw_t *vswp, int p_instance,
1311ae08745Sheppo 	uint64_t *ldcids, int nids, struct ether_addr *macaddr);
1321ae08745Sheppo static	int vsw_detach_ports(vsw_t *vswp);
1331ae08745Sheppo static	int vsw_port_detach(vsw_t *vswp, int p_instance);
1341ae08745Sheppo static	int vsw_port_delete(vsw_port_t *port);
1351ae08745Sheppo static	int vsw_ldc_attach(vsw_port_t *port, uint64_t ldc_id);
1361ae08745Sheppo static	int vsw_ldc_detach(vsw_port_t *port, uint64_t ldc_id);
1371ae08745Sheppo static	int vsw_init_ldcs(vsw_port_t *port);
1381ae08745Sheppo static	int vsw_uninit_ldcs(vsw_port_t *port);
1391ae08745Sheppo static	int vsw_ldc_init(vsw_ldc_t *ldcp);
1401ae08745Sheppo static	int vsw_ldc_uninit(vsw_ldc_t *ldcp);
1411ae08745Sheppo static	int vsw_drain_ldcs(vsw_port_t *port);
1421ae08745Sheppo static	int vsw_drain_port_taskq(vsw_port_t *port);
1431ae08745Sheppo static	void vsw_marker_task(void *);
1441ae08745Sheppo static	vsw_port_t *vsw_lookup_port(vsw_t *vswp, int p_instance);
1451ae08745Sheppo static	int vsw_plist_del_node(vsw_t *, vsw_port_t *port);
1461ae08745Sheppo 
1471ae08745Sheppo /* Interrupt routines */
1481ae08745Sheppo static	uint_t vsw_ldc_cb(uint64_t cb, caddr_t arg);
1491ae08745Sheppo 
1501ae08745Sheppo /* Handshake routines */
1513af08d82Slm66018 static	void vsw_restart_ldc(vsw_ldc_t *);
1521ae08745Sheppo static	void vsw_restart_handshake(vsw_ldc_t *);
1533af08d82Slm66018 static	void vsw_handle_reset(vsw_ldc_t *);
1541ae08745Sheppo static	int vsw_check_flag(vsw_ldc_t *, int, uint64_t);
1551ae08745Sheppo static	void vsw_next_milestone(vsw_ldc_t *);
1561ae08745Sheppo static	int vsw_supported_version(vio_ver_msg_t *);
1571ae08745Sheppo 
1581ae08745Sheppo /* Data processing routines */
1591ae08745Sheppo static void vsw_process_pkt(void *);
1601ae08745Sheppo static void vsw_dispatch_ctrl_task(vsw_ldc_t *, void *, vio_msg_tag_t);
1611ae08745Sheppo static void vsw_process_ctrl_pkt(void *);
1621ae08745Sheppo static void vsw_process_ctrl_ver_pkt(vsw_ldc_t *, void *);
1631ae08745Sheppo static void vsw_process_ctrl_attr_pkt(vsw_ldc_t *, void *);
1641ae08745Sheppo static void vsw_process_ctrl_mcst_pkt(vsw_ldc_t *, void *);
1651ae08745Sheppo static void vsw_process_ctrl_dring_reg_pkt(vsw_ldc_t *, void *);
1661ae08745Sheppo static void vsw_process_ctrl_dring_unreg_pkt(vsw_ldc_t *, void *);
1671ae08745Sheppo static void vsw_process_ctrl_rdx_pkt(vsw_ldc_t *, void *);
1681ae08745Sheppo static void vsw_process_data_pkt(vsw_ldc_t *, void *, vio_msg_tag_t);
1691ae08745Sheppo static void vsw_process_data_dring_pkt(vsw_ldc_t *, void *);
1701ae08745Sheppo static void vsw_process_data_raw_pkt(vsw_ldc_t *, void *);
1711ae08745Sheppo static void vsw_process_data_ibnd_pkt(vsw_ldc_t *, void *);
1721ae08745Sheppo static void vsw_process_err_pkt(vsw_ldc_t *, void *, vio_msg_tag_t);
1731ae08745Sheppo 
1741ae08745Sheppo /* Switching/data transmit routines */
1751ae08745Sheppo static	void vsw_switch_l2_frame(vsw_t *vswp, mblk_t *mp, int caller,
1761ae08745Sheppo 	    vsw_port_t *port, mac_resource_handle_t);
1771ae08745Sheppo static	void vsw_switch_l3_frame(vsw_t *vswp, mblk_t *mp, int caller,
1781ae08745Sheppo 	    vsw_port_t *port, mac_resource_handle_t);
1791ae08745Sheppo static	int vsw_forward_all(vsw_t *vswp, mblk_t *mp, int caller,
1801ae08745Sheppo 	    vsw_port_t *port);
1811ae08745Sheppo static	int vsw_forward_grp(vsw_t *vswp, mblk_t *mp, int caller,
1821ae08745Sheppo 	    vsw_port_t *port);
1831ae08745Sheppo static	int vsw_portsend(vsw_port_t *, mblk_t *);
1841ae08745Sheppo static	int vsw_dringsend(vsw_ldc_t *, mblk_t *);
1851ae08745Sheppo static	int vsw_descrsend(vsw_ldc_t *, mblk_t *);
1861ae08745Sheppo 
1871ae08745Sheppo /* Packet creation routines */
1883af08d82Slm66018 static void vsw_send_ver(void *);
1891ae08745Sheppo static void vsw_send_attr(vsw_ldc_t *);
1901ae08745Sheppo static vio_dring_reg_msg_t *vsw_create_dring_info_pkt(vsw_ldc_t *);
1911ae08745Sheppo static void vsw_send_dring_info(vsw_ldc_t *);
1921ae08745Sheppo static void vsw_send_rdx(vsw_ldc_t *);
1931ae08745Sheppo 
1941ae08745Sheppo static void vsw_send_msg(vsw_ldc_t *, void *, int);
1951ae08745Sheppo 
1961ae08745Sheppo /* Forwarding database (FDB) routines */
1971ae08745Sheppo static	int vsw_add_fdb(vsw_t *vswp, vsw_port_t *port);
1981ae08745Sheppo static	int vsw_del_fdb(vsw_t *vswp, vsw_port_t *port);
1991ae08745Sheppo static	vsw_port_t *vsw_lookup_fdb(vsw_t *vswp, struct ether_header *);
2001ae08745Sheppo static	int vsw_add_rem_mcst(vnet_mcast_msg_t *, vsw_port_t *);
2011ae08745Sheppo static	int vsw_add_mcst(vsw_t *, uint8_t, uint64_t, void *);
2021ae08745Sheppo static	int vsw_del_mcst(vsw_t *, uint8_t, uint64_t, void *);
2031ae08745Sheppo static	void vsw_del_addr(uint8_t, void *, uint64_t);
2041ae08745Sheppo static	void vsw_del_mcst_port(vsw_port_t *);
2051ae08745Sheppo static	void vsw_del_mcst_vsw(vsw_t *);
2061ae08745Sheppo 
2071ae08745Sheppo /* Dring routines */
2081ae08745Sheppo static dring_info_t *vsw_create_dring(vsw_ldc_t *);
2091ae08745Sheppo static void vsw_create_privring(vsw_ldc_t *);
2101ae08745Sheppo static int vsw_setup_ring(vsw_ldc_t *ldcp, dring_info_t *dp);
2111ae08745Sheppo static int vsw_dring_find_free_desc(dring_info_t *, vsw_private_desc_t **,
2121ae08745Sheppo     int *);
2131ae08745Sheppo static dring_info_t *vsw_ident2dring(lane_t *, uint64_t);
2141ae08745Sheppo 
2151ae08745Sheppo static void vsw_set_lane_attr(vsw_t *, lane_t *);
2161ae08745Sheppo static int vsw_check_attr(vnet_attr_msg_t *, vsw_port_t *);
2171ae08745Sheppo static int vsw_dring_match(dring_info_t *dp, vio_dring_reg_msg_t *msg);
2181ae08745Sheppo static int vsw_mem_cookie_match(ldc_mem_cookie_t *, ldc_mem_cookie_t *);
2191ae08745Sheppo static int vsw_check_dring_info(vio_dring_reg_msg_t *);
2201ae08745Sheppo 
2211ae08745Sheppo /* Misc support routines */
2221ae08745Sheppo static	caddr_t vsw_print_ethaddr(uint8_t *addr, char *ebuf);
2231ae08745Sheppo static void vsw_free_lane_resources(vsw_ldc_t *, uint64_t);
2241ae08745Sheppo static int vsw_free_ring(dring_info_t *);
2251ae08745Sheppo 
2261ae08745Sheppo /* Debugging routines */
2271ae08745Sheppo static void dump_flags(uint64_t);
2281ae08745Sheppo static void display_state(void);
2291ae08745Sheppo static void display_lane(lane_t *);
2301ae08745Sheppo static void display_ring(dring_info_t *);
2311ae08745Sheppo 
232*445b4c2eSsb155480 int	vsw_num_handshakes = VNET_NUM_HANDSHAKES; /* # of handshake attempts */
2331ae08745Sheppo int	vsw_wretries = 100;		/* # of write attempts */
234d10e4ef2Snarayan int	vsw_chain_len = 150;		/* max # of mblks in msg chain */
235d10e4ef2Snarayan int	vsw_desc_delay = 0;		/* delay in us */
236d10e4ef2Snarayan int	vsw_read_attempts = 5;		/* # of reads of descriptor */
237d10e4ef2Snarayan 
238d10e4ef2Snarayan uint32_t	vsw_mblk_size = VSW_MBLK_SIZE;
239d10e4ef2Snarayan uint32_t	vsw_num_mblks = VSW_NUM_MBLKS;
240d10e4ef2Snarayan 
241ba2e4443Sseb static	mac_callbacks_t	vsw_m_callbacks = {
242ba2e4443Sseb 	0,
243ba2e4443Sseb 	vsw_m_stat,
244ba2e4443Sseb 	vsw_m_start,
245ba2e4443Sseb 	vsw_m_stop,
246ba2e4443Sseb 	vsw_m_promisc,
247ba2e4443Sseb 	vsw_m_multicst,
248ba2e4443Sseb 	vsw_m_unicst,
249ba2e4443Sseb 	vsw_m_tx,
250ba2e4443Sseb 	NULL,
251ba2e4443Sseb 	NULL,
252ba2e4443Sseb 	NULL
253ba2e4443Sseb };
254ba2e4443Sseb 
2551ae08745Sheppo static	struct	cb_ops	vsw_cb_ops = {
2561ae08745Sheppo 	nulldev,			/* cb_open */
2571ae08745Sheppo 	nulldev,			/* cb_close */
2581ae08745Sheppo 	nodev,				/* cb_strategy */
2591ae08745Sheppo 	nodev,				/* cb_print */
2601ae08745Sheppo 	nodev,				/* cb_dump */
2611ae08745Sheppo 	nodev,				/* cb_read */
2621ae08745Sheppo 	nodev,				/* cb_write */
2631ae08745Sheppo 	nodev,				/* cb_ioctl */
2641ae08745Sheppo 	nodev,				/* cb_devmap */
2651ae08745Sheppo 	nodev,				/* cb_mmap */
2661ae08745Sheppo 	nodev,				/* cb_segmap */
2671ae08745Sheppo 	nochpoll,			/* cb_chpoll */
2681ae08745Sheppo 	ddi_prop_op,			/* cb_prop_op */
2691ae08745Sheppo 	NULL,				/* cb_stream */
2701ae08745Sheppo 	D_MP,				/* cb_flag */
2711ae08745Sheppo 	CB_REV,				/* rev */
2721ae08745Sheppo 	nodev,				/* int (*cb_aread)() */
2731ae08745Sheppo 	nodev				/* int (*cb_awrite)() */
2741ae08745Sheppo };
2751ae08745Sheppo 
2761ae08745Sheppo static	struct	dev_ops	vsw_ops = {
2771ae08745Sheppo 	DEVO_REV,		/* devo_rev */
2781ae08745Sheppo 	0,			/* devo_refcnt */
2791ae08745Sheppo 	vsw_getinfo,		/* devo_getinfo */
2801ae08745Sheppo 	nulldev,		/* devo_identify */
2811ae08745Sheppo 	nulldev,		/* devo_probe */
2821ae08745Sheppo 	vsw_attach,		/* devo_attach */
2831ae08745Sheppo 	vsw_detach,		/* devo_detach */
2841ae08745Sheppo 	nodev,			/* devo_reset */
2851ae08745Sheppo 	&vsw_cb_ops,		/* devo_cb_ops */
2861ae08745Sheppo 	(struct bus_ops *)NULL,	/* devo_bus_ops */
2871ae08745Sheppo 	ddi_power		/* devo_power */
2881ae08745Sheppo };
2891ae08745Sheppo 
2901ae08745Sheppo extern	struct	mod_ops	mod_driverops;
2911ae08745Sheppo static struct modldrv vswmodldrv = {
2921ae08745Sheppo 	&mod_driverops,
2931ae08745Sheppo 	"sun4v Virtual Switch Driver %I%",
2941ae08745Sheppo 	&vsw_ops,
2951ae08745Sheppo };
2961ae08745Sheppo 
2971ae08745Sheppo #define	LDC_ENTER_LOCK(ldcp)	\
2981ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_cblock));\
2991ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_txlock));
3001ae08745Sheppo #define	LDC_EXIT_LOCK(ldcp)	\
3011ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_txlock));\
3021ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_cblock));
3031ae08745Sheppo 
3041ae08745Sheppo /* Driver soft state ptr  */
3051ae08745Sheppo static void	*vsw_state;
3061ae08745Sheppo 
3071ae08745Sheppo /*
3081ae08745Sheppo  * Linked list of "vsw_t" structures - one per instance.
3091ae08745Sheppo  */
3101ae08745Sheppo vsw_t		*vsw_head = NULL;
3111ae08745Sheppo krwlock_t	vsw_rw;
3121ae08745Sheppo 
3131ae08745Sheppo /*
3141ae08745Sheppo  * Property names
3151ae08745Sheppo  */
3161ae08745Sheppo static char vdev_propname[] = "virtual-device";
3171ae08745Sheppo static char vsw_propname[] = "virtual-network-switch";
3181ae08745Sheppo static char physdev_propname[] = "vsw-phys-dev";
3191ae08745Sheppo static char smode_propname[] = "vsw-switch-mode";
3201ae08745Sheppo static char macaddr_propname[] = "local-mac-address";
3211ae08745Sheppo static char remaddr_propname[] = "remote-mac-address";
3221ae08745Sheppo static char ldcids_propname[] = "ldc-ids";
3231ae08745Sheppo static char chan_propname[] = "channel-endpoint";
3241ae08745Sheppo static char id_propname[] = "id";
3251ae08745Sheppo static char reg_propname[] = "reg";
3261ae08745Sheppo 
3271ae08745Sheppo /* supported versions */
3281ae08745Sheppo static	ver_sup_t	vsw_versions[] = { {1, 0} };
3291ae08745Sheppo 
3301ae08745Sheppo /*
3311ae08745Sheppo  * Matching criteria passed to the MDEG to register interest
3321ae08745Sheppo  * in changes to 'virtual-device-port' nodes identified by their
3331ae08745Sheppo  * 'id' property.
3341ae08745Sheppo  */
3351ae08745Sheppo static md_prop_match_t vport_prop_match[] = {
3361ae08745Sheppo 	{ MDET_PROP_VAL,    "id"   },
3371ae08745Sheppo 	{ MDET_LIST_END,    NULL    }
3381ae08745Sheppo };
3391ae08745Sheppo 
3401ae08745Sheppo static mdeg_node_match_t vport_match = { "virtual-device-port",
3411ae08745Sheppo 						vport_prop_match };
3421ae08745Sheppo 
3431ae08745Sheppo /*
34434683adeSsg70180  * Matching criteria passed to the MDEG to register interest
34534683adeSsg70180  * in changes to 'virtual-device' nodes (i.e. vsw nodes) identified
34634683adeSsg70180  * by their 'name' and 'cfg-handle' properties.
34734683adeSsg70180  */
34834683adeSsg70180 static md_prop_match_t vdev_prop_match[] = {
34934683adeSsg70180 	{ MDET_PROP_STR,    "name"   },
35034683adeSsg70180 	{ MDET_PROP_VAL,    "cfg-handle" },
35134683adeSsg70180 	{ MDET_LIST_END,    NULL    }
35234683adeSsg70180 };
35334683adeSsg70180 
35434683adeSsg70180 static mdeg_node_match_t vdev_match = { "virtual-device",
35534683adeSsg70180 						vdev_prop_match };
35634683adeSsg70180 
35734683adeSsg70180 
35834683adeSsg70180 /*
3591ae08745Sheppo  * Specification of an MD node passed to the MDEG to filter any
3601ae08745Sheppo  * 'vport' nodes that do not belong to the specified node. This
3611ae08745Sheppo  * template is copied for each vsw instance and filled in with
3621ae08745Sheppo  * the appropriate 'cfg-handle' value before being passed to the MDEG.
3631ae08745Sheppo  */
3641ae08745Sheppo static mdeg_prop_spec_t vsw_prop_template[] = {
3651ae08745Sheppo 	{ MDET_PROP_STR,    "name",		vsw_propname },
3661ae08745Sheppo 	{ MDET_PROP_VAL,    "cfg-handle",	NULL	},
3671ae08745Sheppo 	{ MDET_LIST_END,    NULL,		NULL	}
3681ae08745Sheppo };
3691ae08745Sheppo 
3701ae08745Sheppo #define	VSW_SET_MDEG_PROP_INST(specp, val)	(specp)[1].ps_val = (val);
3711ae08745Sheppo 
3721ae08745Sheppo /*
3737636cb21Slm66018  * From /etc/system enable/disable thread per ring. This is a mode
3747636cb21Slm66018  * selection that is done a vsw driver attach time.
3757636cb21Slm66018  */
3767636cb21Slm66018 boolean_t vsw_multi_ring_enable = B_FALSE;
3777636cb21Slm66018 int vsw_mac_rx_rings = VSW_MAC_RX_RINGS;
3787636cb21Slm66018 
3797636cb21Slm66018 /*
3801ae08745Sheppo  * Print debug messages - set to 0x1f to enable all msgs
3811ae08745Sheppo  * or 0x0 to turn all off.
3821ae08745Sheppo  */
3831ae08745Sheppo int vswdbg = 0x0;
3841ae08745Sheppo 
3851ae08745Sheppo /*
3861ae08745Sheppo  * debug levels:
3871ae08745Sheppo  * 0x01:	Function entry/exit tracing
3881ae08745Sheppo  * 0x02:	Internal function messages
3891ae08745Sheppo  * 0x04:	Verbose internal messages
3901ae08745Sheppo  * 0x08:	Warning messages
3911ae08745Sheppo  * 0x10:	Error messages
3921ae08745Sheppo  */
3931ae08745Sheppo 
3941ae08745Sheppo static void
3951ae08745Sheppo vswdebug(vsw_t *vswp, const char *fmt, ...)
3961ae08745Sheppo {
3971ae08745Sheppo 	char buf[512];
3981ae08745Sheppo 	va_list ap;
3991ae08745Sheppo 
4001ae08745Sheppo 	va_start(ap, fmt);
4011ae08745Sheppo 	(void) vsprintf(buf, fmt, ap);
4021ae08745Sheppo 	va_end(ap);
4031ae08745Sheppo 
4041ae08745Sheppo 	if (vswp == NULL)
4051ae08745Sheppo 		cmn_err(CE_CONT, "%s\n", buf);
4061ae08745Sheppo 	else
4071ae08745Sheppo 		cmn_err(CE_CONT, "vsw%d: %s\n", vswp->instance, buf);
4081ae08745Sheppo }
4091ae08745Sheppo 
4101ae08745Sheppo /*
4111ae08745Sheppo  * For the moment the state dump routines have their own
4121ae08745Sheppo  * private flag.
4131ae08745Sheppo  */
4141ae08745Sheppo #define	DUMP_STATE	0
4151ae08745Sheppo 
4161ae08745Sheppo #if DUMP_STATE
4171ae08745Sheppo 
4181ae08745Sheppo #define	DUMP_TAG(tag) \
4191ae08745Sheppo {			\
4201ae08745Sheppo 	D1(NULL, "DUMP_TAG: type 0x%llx", (tag).vio_msgtype); \
4211ae08745Sheppo 	D1(NULL, "DUMP_TAG: stype 0x%llx", (tag).vio_subtype);	\
4221ae08745Sheppo 	D1(NULL, "DUMP_TAG: senv 0x%llx", (tag).vio_subtype_env);	\
4231ae08745Sheppo }
4241ae08745Sheppo 
4251ae08745Sheppo #define	DUMP_TAG_PTR(tag) \
4261ae08745Sheppo {			\
4271ae08745Sheppo 	D1(NULL, "DUMP_TAG: type 0x%llx", (tag)->vio_msgtype); \
4281ae08745Sheppo 	D1(NULL, "DUMP_TAG: stype 0x%llx", (tag)->vio_subtype);	\
4291ae08745Sheppo 	D1(NULL, "DUMP_TAG: senv 0x%llx", (tag)->vio_subtype_env);	\
4301ae08745Sheppo }
4311ae08745Sheppo 
4321ae08745Sheppo #define	DUMP_FLAGS(flags) dump_flags(flags);
4331ae08745Sheppo #define	DISPLAY_STATE()	display_state()
4341ae08745Sheppo 
4351ae08745Sheppo #else
4361ae08745Sheppo 
4371ae08745Sheppo #define	DUMP_TAG(tag)
4381ae08745Sheppo #define	DUMP_TAG_PTR(tag)
4391ae08745Sheppo #define	DUMP_FLAGS(state)
4401ae08745Sheppo #define	DISPLAY_STATE()
4411ae08745Sheppo 
4421ae08745Sheppo #endif	/* DUMP_STATE */
4431ae08745Sheppo 
4441ae08745Sheppo #ifdef DEBUG
4451ae08745Sheppo 
4461ae08745Sheppo #define	D1		\
4471ae08745Sheppo if (vswdbg & 0x01)	\
4481ae08745Sheppo 	vswdebug
4491ae08745Sheppo 
4501ae08745Sheppo #define	D2		\
4511ae08745Sheppo if (vswdbg & 0x02)	\
4521ae08745Sheppo 	vswdebug
4531ae08745Sheppo 
4541ae08745Sheppo #define	D3		\
4551ae08745Sheppo if (vswdbg & 0x04)	\
4561ae08745Sheppo 	vswdebug
4571ae08745Sheppo 
4581ae08745Sheppo #define	DWARN		\
4591ae08745Sheppo if (vswdbg & 0x08)	\
4601ae08745Sheppo 	vswdebug
4611ae08745Sheppo 
4621ae08745Sheppo #define	DERR		\
4631ae08745Sheppo if (vswdbg & 0x10)	\
4641ae08745Sheppo 	vswdebug
4651ae08745Sheppo 
4661ae08745Sheppo #else
4671ae08745Sheppo 
4681ae08745Sheppo #define	DERR		if (0)	vswdebug
4691ae08745Sheppo #define	DWARN		if (0)	vswdebug
4701ae08745Sheppo #define	D1		if (0)	vswdebug
4711ae08745Sheppo #define	D2		if (0)	vswdebug
4721ae08745Sheppo #define	D3		if (0)	vswdebug
4731ae08745Sheppo 
4741ae08745Sheppo #endif	/* DEBUG */
4751ae08745Sheppo 
4761ae08745Sheppo static struct modlinkage modlinkage = {
4771ae08745Sheppo 	MODREV_1,
4781ae08745Sheppo 	&vswmodldrv,
4791ae08745Sheppo 	NULL
4801ae08745Sheppo };
4811ae08745Sheppo 
4821ae08745Sheppo int
4831ae08745Sheppo _init(void)
4841ae08745Sheppo {
4851ae08745Sheppo 	int status;
4861ae08745Sheppo 
4871ae08745Sheppo 	rw_init(&vsw_rw, NULL, RW_DRIVER, NULL);
4881ae08745Sheppo 
4891ae08745Sheppo 	status = ddi_soft_state_init(&vsw_state, sizeof (vsw_t), 1);
4901ae08745Sheppo 	if (status != 0) {
4911ae08745Sheppo 		return (status);
4921ae08745Sheppo 	}
4931ae08745Sheppo 
4941ae08745Sheppo 	mac_init_ops(&vsw_ops, "vsw");
4951ae08745Sheppo 	status = mod_install(&modlinkage);
4961ae08745Sheppo 	if (status != 0) {
4971ae08745Sheppo 		ddi_soft_state_fini(&vsw_state);
4981ae08745Sheppo 	}
4991ae08745Sheppo 	return (status);
5001ae08745Sheppo }
5011ae08745Sheppo 
5021ae08745Sheppo int
5031ae08745Sheppo _fini(void)
5041ae08745Sheppo {
5051ae08745Sheppo 	int status;
5061ae08745Sheppo 
5071ae08745Sheppo 	status = mod_remove(&modlinkage);
5081ae08745Sheppo 	if (status != 0)
5091ae08745Sheppo 		return (status);
5101ae08745Sheppo 	mac_fini_ops(&vsw_ops);
5111ae08745Sheppo 	ddi_soft_state_fini(&vsw_state);
5121ae08745Sheppo 
5131ae08745Sheppo 	rw_destroy(&vsw_rw);
5141ae08745Sheppo 
5151ae08745Sheppo 	return (status);
5161ae08745Sheppo }
5171ae08745Sheppo 
5181ae08745Sheppo int
5191ae08745Sheppo _info(struct modinfo *modinfop)
5201ae08745Sheppo {
5211ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
5221ae08745Sheppo }
5231ae08745Sheppo 
5241ae08745Sheppo static int
5251ae08745Sheppo vsw_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5261ae08745Sheppo {
5271ae08745Sheppo 	vsw_t		*vswp;
52834683adeSsg70180 	int		instance;
5291ae08745Sheppo 	char		hashname[MAXNAMELEN];
5301ae08745Sheppo 	char		qname[TASKQ_NAMELEN];
5317636cb21Slm66018 	enum		{ PROG_init = 0x00,
5327636cb21Slm66018 				PROG_if_lock = 0x01,
5337636cb21Slm66018 				PROG_fdb = 0x02,
5347636cb21Slm66018 				PROG_mfdb = 0x04,
5357636cb21Slm66018 				PROG_report_dev = 0x08,
5367636cb21Slm66018 				PROG_plist = 0x10,
5371ae08745Sheppo 				PROG_taskq = 0x20}
5381ae08745Sheppo 			progress;
5391ae08745Sheppo 
5401ae08745Sheppo 	progress = PROG_init;
5411ae08745Sheppo 
5421ae08745Sheppo 	switch (cmd) {
5431ae08745Sheppo 	case DDI_ATTACH:
5441ae08745Sheppo 		break;
5451ae08745Sheppo 	case DDI_RESUME:
5461ae08745Sheppo 		/* nothing to do for this non-device */
5471ae08745Sheppo 		return (DDI_SUCCESS);
5481ae08745Sheppo 	case DDI_PM_RESUME:
5491ae08745Sheppo 	default:
5501ae08745Sheppo 		return (DDI_FAILURE);
5511ae08745Sheppo 	}
5521ae08745Sheppo 
5531ae08745Sheppo 	instance = ddi_get_instance(dip);
5541ae08745Sheppo 	if (ddi_soft_state_zalloc(vsw_state, instance) != DDI_SUCCESS) {
5551ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_soft_state_zalloc failed", instance);
5561ae08745Sheppo 		return (DDI_FAILURE);
5571ae08745Sheppo 	}
5581ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
5591ae08745Sheppo 
5601ae08745Sheppo 	if (vswp == NULL) {
5611ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_get_soft_state failed", instance);
5621ae08745Sheppo 		goto vsw_attach_fail;
5631ae08745Sheppo 	}
5641ae08745Sheppo 
5651ae08745Sheppo 	vswp->dip = dip;
5661ae08745Sheppo 	vswp->instance = instance;
5671ae08745Sheppo 	ddi_set_driver_private(dip, (caddr_t)vswp);
5681ae08745Sheppo 
56934683adeSsg70180 	mutex_init(&vswp->mac_lock, NULL, MUTEX_DRIVER, NULL);
5701ae08745Sheppo 	rw_init(&vswp->if_lockrw, NULL, RW_DRIVER, NULL);
5711ae08745Sheppo 	progress |= PROG_if_lock;
5721ae08745Sheppo 
5731ae08745Sheppo 	/* setup the unicast forwarding database  */
5741ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_unicst_table-%d",
5751ae08745Sheppo 							vswp->instance);
5761ae08745Sheppo 	D2(vswp, "creating unicast hash table (%s)...", hashname);
5771ae08745Sheppo 	vswp->fdb = mod_hash_create_ptrhash(hashname, VSW_NCHAINS,
5781ae08745Sheppo 		mod_hash_null_valdtor, sizeof (void *));
5791ae08745Sheppo 
5801ae08745Sheppo 	progress |= PROG_fdb;
5811ae08745Sheppo 
5821ae08745Sheppo 	/* setup the multicast fowarding database */
5831ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_mcst_table-%d",
5841ae08745Sheppo 							vswp->instance);
5851ae08745Sheppo 	D2(vswp, "creating multicast hash table %s)...", hashname);
5861ae08745Sheppo 	rw_init(&vswp->mfdbrw, NULL, RW_DRIVER, NULL);
5871ae08745Sheppo 	vswp->mfdb = mod_hash_create_ptrhash(hashname, VSW_NCHAINS,
5881ae08745Sheppo 			mod_hash_null_valdtor, sizeof (void *));
5891ae08745Sheppo 
5901ae08745Sheppo 	progress |= PROG_mfdb;
5911ae08745Sheppo 
5921ae08745Sheppo 	/*
5931ae08745Sheppo 	 * create lock protecting list of multicast addresses
5941ae08745Sheppo 	 * which could come via m_multicst() entry point when plumbed.
5951ae08745Sheppo 	 */
5961ae08745Sheppo 	mutex_init(&vswp->mca_lock, NULL, MUTEX_DRIVER, NULL);
5971ae08745Sheppo 	vswp->mcap = NULL;
5981ae08745Sheppo 
5991ae08745Sheppo 	ddi_report_dev(vswp->dip);
6001ae08745Sheppo 
6011ae08745Sheppo 	progress |= PROG_report_dev;
6021ae08745Sheppo 
6031ae08745Sheppo 	WRITE_ENTER(&vsw_rw);
6041ae08745Sheppo 	vswp->next = vsw_head;
6051ae08745Sheppo 	vsw_head = vswp;
6061ae08745Sheppo 	RW_EXIT(&vsw_rw);
6071ae08745Sheppo 
6081ae08745Sheppo 	/* setup the port list */
6091ae08745Sheppo 	rw_init(&vswp->plist.lockrw, NULL, RW_DRIVER, NULL);
6101ae08745Sheppo 	vswp->plist.head = NULL;
6111ae08745Sheppo 
6121ae08745Sheppo 	progress |= PROG_plist;
6131ae08745Sheppo 
6141ae08745Sheppo 	/*
6151ae08745Sheppo 	 * Create the taskq which will process all the VIO
6161ae08745Sheppo 	 * control messages.
6171ae08745Sheppo 	 */
6181ae08745Sheppo 	(void) snprintf(qname, TASKQ_NAMELEN, "vsw_taskq%d", vswp->instance);
6191ae08745Sheppo 	if ((vswp->taskq_p = ddi_taskq_create(vswp->dip, qname, 1,
6201ae08745Sheppo 					TASKQ_DEFAULTPRI, 0)) == NULL) {
62134683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to create task queue",
62234683adeSsg70180 			vswp->instance);
6231ae08745Sheppo 		goto vsw_attach_fail;
6241ae08745Sheppo 	}
6251ae08745Sheppo 
6261ae08745Sheppo 	progress |= PROG_taskq;
6271ae08745Sheppo 
628d10e4ef2Snarayan 	/* prevent auto-detaching */
629d10e4ef2Snarayan 	if (ddi_prop_update_int(DDI_DEV_T_NONE, vswp->dip,
630d10e4ef2Snarayan 				DDI_NO_AUTODETACH, 1) != DDI_SUCCESS) {
63134683adeSsg70180 		cmn_err(CE_NOTE, "!Unable to set \"%s\" property for "
632d10e4ef2Snarayan 			"instance %u", DDI_NO_AUTODETACH, instance);
633d10e4ef2Snarayan 	}
634d10e4ef2Snarayan 
6351ae08745Sheppo 	/*
63634683adeSsg70180 	 * Now we have everything setup, register an interest in
63734683adeSsg70180 	 * specific MD nodes.
63834683adeSsg70180 	 *
63934683adeSsg70180 	 * The callback is invoked in 2 cases, firstly if upon mdeg
64034683adeSsg70180 	 * registration there are existing nodes which match our specified
64134683adeSsg70180 	 * criteria, and secondly if the MD is changed (and again, there
64234683adeSsg70180 	 * are nodes which we are interested in present within it. Note
64334683adeSsg70180 	 * that our callback will be invoked even if our specified nodes
64434683adeSsg70180 	 * have not actually changed).
64534683adeSsg70180 	 *
64634683adeSsg70180 	 * Until the callback is invoked we cannot switch any pkts as
64734683adeSsg70180 	 * we don't know basic information such as what mode we are
64834683adeSsg70180 	 * operating in. However we expect the callback to be invoked
64934683adeSsg70180 	 * immediately upon registration as this driver should only
65034683adeSsg70180 	 * be attaching if there are vsw nodes in the MD.
6511ae08745Sheppo 	 */
65234683adeSsg70180 	if (vsw_mdeg_register(vswp))
65334683adeSsg70180 		goto vsw_attach_fail;
6541ae08745Sheppo 
6551ae08745Sheppo 	return (DDI_SUCCESS);
6561ae08745Sheppo 
6571ae08745Sheppo vsw_attach_fail:
6581ae08745Sheppo 	DERR(NULL, "vsw_attach: failed");
6591ae08745Sheppo 
6601ae08745Sheppo 	if (progress & PROG_taskq)
6611ae08745Sheppo 		ddi_taskq_destroy(vswp->taskq_p);
6621ae08745Sheppo 
6631ae08745Sheppo 	if (progress & PROG_plist)
6641ae08745Sheppo 		rw_destroy(&vswp->plist.lockrw);
6651ae08745Sheppo 
6661ae08745Sheppo 	if (progress & PROG_report_dev) {
6671ae08745Sheppo 		ddi_remove_minor_node(dip, NULL);
6681ae08745Sheppo 		mutex_destroy(&vswp->mca_lock);
6691ae08745Sheppo 	}
6701ae08745Sheppo 
6711ae08745Sheppo 	if (progress & PROG_mfdb) {
6721ae08745Sheppo 		mod_hash_destroy_hash(vswp->mfdb);
6731ae08745Sheppo 		vswp->mfdb = NULL;
6741ae08745Sheppo 		rw_destroy(&vswp->mfdbrw);
6751ae08745Sheppo 	}
6761ae08745Sheppo 
6771ae08745Sheppo 	if (progress & PROG_fdb) {
6781ae08745Sheppo 		mod_hash_destroy_hash(vswp->fdb);
6791ae08745Sheppo 		vswp->fdb = NULL;
6801ae08745Sheppo 	}
6811ae08745Sheppo 
68234683adeSsg70180 	if (progress & PROG_if_lock) {
6831ae08745Sheppo 		rw_destroy(&vswp->if_lockrw);
68434683adeSsg70180 		mutex_destroy(&vswp->mac_lock);
68534683adeSsg70180 	}
6861ae08745Sheppo 
6871ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
6881ae08745Sheppo 	return (DDI_FAILURE);
6891ae08745Sheppo }
6901ae08745Sheppo 
6911ae08745Sheppo static int
6921ae08745Sheppo vsw_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6931ae08745Sheppo {
694d10e4ef2Snarayan 	vio_mblk_pool_t		*poolp, *npoolp;
6951ae08745Sheppo 	vsw_t			**vswpp, *vswp;
6961ae08745Sheppo 	int 			instance;
6971ae08745Sheppo 
6981ae08745Sheppo 	instance = ddi_get_instance(dip);
6991ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
7001ae08745Sheppo 
7011ae08745Sheppo 	if (vswp == NULL) {
7021ae08745Sheppo 		return (DDI_FAILURE);
7031ae08745Sheppo 	}
7041ae08745Sheppo 
7051ae08745Sheppo 	switch (cmd) {
7061ae08745Sheppo 	case DDI_DETACH:
7071ae08745Sheppo 		break;
7081ae08745Sheppo 	case DDI_SUSPEND:
7091ae08745Sheppo 	case DDI_PM_SUSPEND:
7101ae08745Sheppo 	default:
7111ae08745Sheppo 		return (DDI_FAILURE);
7121ae08745Sheppo 	}
7131ae08745Sheppo 
7141ae08745Sheppo 	D2(vswp, "detaching instance %d", instance);
7151ae08745Sheppo 
71634683adeSsg70180 	if (vswp->if_state & VSW_IF_REG) {
7171ae08745Sheppo 		if (vsw_mac_unregister(vswp) != 0) {
71834683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to detach from "
71934683adeSsg70180 				"MAC layer", vswp->instance);
7201ae08745Sheppo 			return (DDI_FAILURE);
7211ae08745Sheppo 		}
722d10e4ef2Snarayan 	}
7231ae08745Sheppo 
7241ae08745Sheppo 	vsw_mdeg_unregister(vswp);
7251ae08745Sheppo 
726e1ebb9ecSlm66018 	/* remove mac layer callback */
72734683adeSsg70180 	mutex_enter(&vswp->mac_lock);
728e1ebb9ecSlm66018 	if ((vswp->mh != NULL) && (vswp->mrh != NULL)) {
729e1ebb9ecSlm66018 		mac_rx_remove(vswp->mh, vswp->mrh);
730e1ebb9ecSlm66018 		vswp->mrh = NULL;
7311ae08745Sheppo 	}
73234683adeSsg70180 	mutex_exit(&vswp->mac_lock);
7331ae08745Sheppo 
7341ae08745Sheppo 	if (vsw_detach_ports(vswp) != 0) {
73534683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to detach ports",
73634683adeSsg70180 							vswp->instance);
7371ae08745Sheppo 		return (DDI_FAILURE);
7381ae08745Sheppo 	}
7391ae08745Sheppo 
74034683adeSsg70180 	rw_destroy(&vswp->if_lockrw);
74134683adeSsg70180 
7421ae08745Sheppo 	/*
743e1ebb9ecSlm66018 	 * Now that the ports have been deleted, stop and close
744e1ebb9ecSlm66018 	 * the physical device.
745e1ebb9ecSlm66018 	 */
74634683adeSsg70180 	mutex_enter(&vswp->mac_lock);
747e1ebb9ecSlm66018 	if (vswp->mh != NULL) {
7487636cb21Slm66018 		if (vswp->mstarted)
749e1ebb9ecSlm66018 			mac_stop(vswp->mh);
7507636cb21Slm66018 		if (vswp->mresources)
7517636cb21Slm66018 			mac_resource_set(vswp->mh, NULL, NULL);
752e1ebb9ecSlm66018 		mac_close(vswp->mh);
753e1ebb9ecSlm66018 
754e1ebb9ecSlm66018 		vswp->mh = NULL;
755e1ebb9ecSlm66018 		vswp->txinfo = NULL;
756e1ebb9ecSlm66018 	}
75734683adeSsg70180 	mutex_exit(&vswp->mac_lock);
75834683adeSsg70180 	mutex_destroy(&vswp->mac_lock);
759e1ebb9ecSlm66018 
760e1ebb9ecSlm66018 	/*
761d10e4ef2Snarayan 	 * Destroy any free pools that may still exist.
762d10e4ef2Snarayan 	 */
763d10e4ef2Snarayan 	poolp = vswp->rxh;
764d10e4ef2Snarayan 	while (poolp != NULL) {
765d10e4ef2Snarayan 		npoolp = vswp->rxh = poolp->nextp;
766d10e4ef2Snarayan 		if (vio_destroy_mblks(poolp) != 0) {
767d10e4ef2Snarayan 			vswp->rxh = poolp;
768d10e4ef2Snarayan 			return (DDI_FAILURE);
769d10e4ef2Snarayan 		}
770d10e4ef2Snarayan 		poolp = npoolp;
771d10e4ef2Snarayan 	}
772d10e4ef2Snarayan 
773d10e4ef2Snarayan 	/*
7741ae08745Sheppo 	 * Remove this instance from any entries it may be on in
7751ae08745Sheppo 	 * the hash table by using the list of addresses maintained
7761ae08745Sheppo 	 * in the vsw_t structure.
7771ae08745Sheppo 	 */
7781ae08745Sheppo 	vsw_del_mcst_vsw(vswp);
7791ae08745Sheppo 
7801ae08745Sheppo 	vswp->mcap = NULL;
7811ae08745Sheppo 	mutex_destroy(&vswp->mca_lock);
7821ae08745Sheppo 
7831ae08745Sheppo 	/*
7841ae08745Sheppo 	 * By now any pending tasks have finished and the underlying
7851ae08745Sheppo 	 * ldc's have been destroyed, so its safe to delete the control
7861ae08745Sheppo 	 * message taskq.
7871ae08745Sheppo 	 */
7881ae08745Sheppo 	if (vswp->taskq_p != NULL)
7891ae08745Sheppo 		ddi_taskq_destroy(vswp->taskq_p);
7901ae08745Sheppo 
7911ae08745Sheppo 	/*
7921ae08745Sheppo 	 * At this stage all the data pointers in the hash table
7931ae08745Sheppo 	 * should be NULL, as all the ports have been removed and will
7941ae08745Sheppo 	 * have deleted themselves from the port lists which the data
7951ae08745Sheppo 	 * pointers point to. Hence we can destroy the table using the
7961ae08745Sheppo 	 * default destructors.
7971ae08745Sheppo 	 */
7981ae08745Sheppo 	D2(vswp, "vsw_detach: destroying hash tables..");
7991ae08745Sheppo 	mod_hash_destroy_hash(vswp->fdb);
8001ae08745Sheppo 	vswp->fdb = NULL;
8011ae08745Sheppo 
8021ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
8031ae08745Sheppo 	mod_hash_destroy_hash(vswp->mfdb);
8041ae08745Sheppo 	vswp->mfdb = NULL;
8051ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
8061ae08745Sheppo 	rw_destroy(&vswp->mfdbrw);
8071ae08745Sheppo 
8081ae08745Sheppo 	ddi_remove_minor_node(dip, NULL);
8091ae08745Sheppo 
8101ae08745Sheppo 	rw_destroy(&vswp->plist.lockrw);
8111ae08745Sheppo 	WRITE_ENTER(&vsw_rw);
8121ae08745Sheppo 	for (vswpp = &vsw_head; *vswpp; vswpp = &(*vswpp)->next) {
8131ae08745Sheppo 		if (*vswpp == vswp) {
8141ae08745Sheppo 			*vswpp = vswp->next;
8151ae08745Sheppo 			break;
8161ae08745Sheppo 		}
8171ae08745Sheppo 	}
8181ae08745Sheppo 	RW_EXIT(&vsw_rw);
8191ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
8201ae08745Sheppo 
8211ae08745Sheppo 	return (DDI_SUCCESS);
8221ae08745Sheppo }
8231ae08745Sheppo 
8241ae08745Sheppo static int
8251ae08745Sheppo vsw_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
8261ae08745Sheppo {
8271ae08745Sheppo 	_NOTE(ARGUNUSED(dip))
8281ae08745Sheppo 
8291ae08745Sheppo 	vsw_t	*vswp = NULL;
8301ae08745Sheppo 	dev_t	dev = (dev_t)arg;
8311ae08745Sheppo 	int	instance;
8321ae08745Sheppo 
8331ae08745Sheppo 	instance = getminor(dev);
8341ae08745Sheppo 
8351ae08745Sheppo 	switch (infocmd) {
8361ae08745Sheppo 	case DDI_INFO_DEVT2DEVINFO:
8371ae08745Sheppo 		if ((vswp = ddi_get_soft_state(vsw_state, instance)) == NULL) {
8381ae08745Sheppo 			*result = NULL;
8391ae08745Sheppo 			return (DDI_FAILURE);
8401ae08745Sheppo 		}
8411ae08745Sheppo 		*result = vswp->dip;
8421ae08745Sheppo 		return (DDI_SUCCESS);
8431ae08745Sheppo 
8441ae08745Sheppo 	case DDI_INFO_DEVT2INSTANCE:
8451ae08745Sheppo 		*result = (void *)(uintptr_t)instance;
8461ae08745Sheppo 		return (DDI_SUCCESS);
8471ae08745Sheppo 
8481ae08745Sheppo 	default:
8491ae08745Sheppo 		*result = NULL;
8501ae08745Sheppo 		return (DDI_FAILURE);
8511ae08745Sheppo 	}
8521ae08745Sheppo }
8531ae08745Sheppo 
8541ae08745Sheppo /*
85534683adeSsg70180  * Get the value of the "vsw-phys-dev" property in the specified
85634683adeSsg70180  * node. This property is the name of the physical device that
85734683adeSsg70180  * the virtual switch will use to talk to the outside world.
85834683adeSsg70180  *
85934683adeSsg70180  * Note it is valid for this property to be NULL (but the property
86034683adeSsg70180  * itself must exist). Callers of this routine should verify that
86134683adeSsg70180  * the value returned is what they expected (i.e. either NULL or non NULL).
86234683adeSsg70180  *
86334683adeSsg70180  * On success returns value of the property in region pointed to by
86434683adeSsg70180  * the 'name' argument, and with return value of 0. Otherwise returns 1.
8651ae08745Sheppo  */
86634683adeSsg70180 static int
86734683adeSsg70180 vsw_get_md_physname(vsw_t *vswp, md_t *mdp, mde_cookie_t node, char *name)
8681ae08745Sheppo {
86934683adeSsg70180 	int	len = 0;
8701ae08745Sheppo 	char	*physname = NULL;
8711ae08745Sheppo 	char	*dev;
8721ae08745Sheppo 
87334683adeSsg70180 	if (md_get_prop_data(mdp, node, physdev_propname,
8741ae08745Sheppo 				(uint8_t **)(&physname), &len) != 0) {
87534683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get name(s) of physical "
87634683adeSsg70180 				"device(s) from MD", vswp->instance);
87734683adeSsg70180 		return (1);
8781ae08745Sheppo 	} else if ((strlen(physname) + 1) > LIFNAMSIZ) {
87934683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: %s is too long a device name",
88034683adeSsg70180 			vswp->instance, physname);
88134683adeSsg70180 		return (1);
8821ae08745Sheppo 	} else {
88334683adeSsg70180 		(void) strncpy(name, physname, strlen(physname) + 1);
8841ae08745Sheppo 		D2(vswp, "%s: using first device specified (%s)",
88534683adeSsg70180 			__func__, physname);
8861ae08745Sheppo 	}
8871ae08745Sheppo 
8881ae08745Sheppo #ifdef DEBUG
8891ae08745Sheppo 	/*
8901ae08745Sheppo 	 * As a temporary measure to aid testing we check to see if there
8911ae08745Sheppo 	 * is a vsw.conf file present. If there is we use the value of the
8921ae08745Sheppo 	 * vsw_physname property in the file as the name of the physical
8931ae08745Sheppo 	 * device, overriding the value from the MD.
8941ae08745Sheppo 	 *
8951ae08745Sheppo 	 * There may be multiple devices listed, but for the moment
8961ae08745Sheppo 	 * we just use the first one.
8971ae08745Sheppo 	 */
8981ae08745Sheppo 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vswp->dip, 0,
8991ae08745Sheppo 		"vsw_physname", &dev) == DDI_PROP_SUCCESS) {
9001ae08745Sheppo 		if ((strlen(dev) + 1) > LIFNAMSIZ) {
90134683adeSsg70180 			cmn_err(CE_WARN, "vsw%d: %s is too long a device name",
90234683adeSsg70180 				vswp->instance, dev);
90334683adeSsg70180 			ddi_prop_free(dev);
90434683adeSsg70180 			return (1);
9051ae08745Sheppo 		} else {
90634683adeSsg70180 			cmn_err(CE_NOTE, "vsw%d: Using device name (%s) from "
90734683adeSsg70180 				"config file", vswp->instance, dev);
9081ae08745Sheppo 
90934683adeSsg70180 			(void) strncpy(name, dev, strlen(dev) + 1);
9101ae08745Sheppo 		}
9111ae08745Sheppo 
9121ae08745Sheppo 		ddi_prop_free(dev);
9131ae08745Sheppo 	}
9141ae08745Sheppo #endif
9151ae08745Sheppo 
91634683adeSsg70180 	return (0);
91734683adeSsg70180 }
918e1ebb9ecSlm66018 
919e1ebb9ecSlm66018 /*
92034683adeSsg70180  * Read the 'vsw-switch-mode' property from the specified MD node.
92134683adeSsg70180  *
92234683adeSsg70180  * Returns 0 on success and the number of modes found in 'found',
92334683adeSsg70180  * otherwise returns 1.
924e1ebb9ecSlm66018  */
92534683adeSsg70180 static int
92634683adeSsg70180 vsw_get_md_smodes(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
92734683adeSsg70180 						uint8_t *modes, int *found)
92834683adeSsg70180 {
92934683adeSsg70180 	int		len = 0;
93034683adeSsg70180 	int		smode_num = 0;
93134683adeSsg70180 	char		*smode = NULL;
93234683adeSsg70180 	char		*curr_mode = NULL;
93334683adeSsg70180 
93434683adeSsg70180 	D1(vswp, "%s: enter", __func__);
9351ae08745Sheppo 
9361ae08745Sheppo 	/*
9371ae08745Sheppo 	 * Get the switch-mode property. The modes are listed in
9381ae08745Sheppo 	 * decreasing order of preference, i.e. prefered mode is
9391ae08745Sheppo 	 * first item in list.
9401ae08745Sheppo 	 */
9411ae08745Sheppo 	len = 0;
94234683adeSsg70180 	smode_num = 0;
94334683adeSsg70180 	if (md_get_prop_data(mdp, node, smode_propname,
9441ae08745Sheppo 				(uint8_t **)(&smode), &len) != 0) {
9451ae08745Sheppo 		/*
946e1ebb9ecSlm66018 		 * Unable to get switch-mode property from MD, nothing
947e1ebb9ecSlm66018 		 * more we can do.
9481ae08745Sheppo 		 */
94934683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get switch mode property"
95034683adeSsg70180 			" from the MD", vswp->instance);
95134683adeSsg70180 		*found = 0;
95234683adeSsg70180 		return (1);
953e1ebb9ecSlm66018 	}
954e1ebb9ecSlm66018 
9551ae08745Sheppo 	curr_mode = smode;
9561ae08745Sheppo 	/*
9571ae08745Sheppo 	 * Modes of operation:
9581ae08745Sheppo 	 * 'switched'	 - layer 2 switching, underlying HW in
959e1ebb9ecSlm66018 	 *			programmed mode.
9601ae08745Sheppo 	 * 'promiscuous' - layer 2 switching, underlying HW in
9611ae08745Sheppo 	 *			promiscuous mode.
9621ae08745Sheppo 	 * 'routed'	 - layer 3 (i.e. IP) routing, underlying HW
9631ae08745Sheppo 	 *			in non-promiscuous mode.
9641ae08745Sheppo 	 */
96534683adeSsg70180 	while ((curr_mode < (smode + len)) && (smode_num < NUM_SMODES)) {
9661ae08745Sheppo 		D2(vswp, "%s: curr_mode = [%s]", __func__, curr_mode);
967e1ebb9ecSlm66018 		if (strcmp(curr_mode, "switched") == 0) {
96834683adeSsg70180 			modes[smode_num++] = VSW_LAYER2;
969e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "promiscuous") == 0) {
97034683adeSsg70180 			modes[smode_num++] = VSW_LAYER2_PROMISC;
971e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "routed") == 0) {
97234683adeSsg70180 			modes[smode_num++] = VSW_LAYER3;
973e1ebb9ecSlm66018 		} else {
97434683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unknown switch mode %s, "
97534683adeSsg70180 				"setting to default switched mode",
97634683adeSsg70180 				vswp->instance, curr_mode);
97734683adeSsg70180 			modes[smode_num++] = VSW_LAYER2;
9781ae08745Sheppo 		}
9791ae08745Sheppo 		curr_mode += strlen(curr_mode) + 1;
9801ae08745Sheppo 	}
98134683adeSsg70180 	*found = smode_num;
9821ae08745Sheppo 
98334683adeSsg70180 	D2(vswp, "%s: %d modes found", __func__, smode_num);
9841ae08745Sheppo 
9851ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
98634683adeSsg70180 
98734683adeSsg70180 	return (0);
9881ae08745Sheppo }
9891ae08745Sheppo 
990e1ebb9ecSlm66018 /*
991e1ebb9ecSlm66018  * Get the mac address of the physical device.
992e1ebb9ecSlm66018  *
993e1ebb9ecSlm66018  * Returns 0 on success, 1 on failure.
994e1ebb9ecSlm66018  */
995e1ebb9ecSlm66018 static int
996e1ebb9ecSlm66018 vsw_get_physaddr(vsw_t *vswp)
997e1ebb9ecSlm66018 {
998e1ebb9ecSlm66018 	mac_handle_t	mh;
999e1ebb9ecSlm66018 	char		drv[LIFNAMSIZ];
1000e1ebb9ecSlm66018 	uint_t		ddi_instance;
1001e1ebb9ecSlm66018 
1002e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1003e1ebb9ecSlm66018 
1004e1ebb9ecSlm66018 	if (ddi_parse(vswp->physname, drv, &ddi_instance) != DDI_SUCCESS)
1005e1ebb9ecSlm66018 		return (1);
1006e1ebb9ecSlm66018 
1007e1ebb9ecSlm66018 	if (mac_open(vswp->physname, ddi_instance, &mh) != 0) {
100834683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: mac_open %s failed",
100934683adeSsg70180 				vswp->instance, vswp->physname);
1010e1ebb9ecSlm66018 		return (1);
1011e1ebb9ecSlm66018 	}
1012e1ebb9ecSlm66018 
1013e1ebb9ecSlm66018 	READ_ENTER(&vswp->if_lockrw);
1014e1ebb9ecSlm66018 	mac_unicst_get(mh, vswp->if_addr.ether_addr_octet);
1015e1ebb9ecSlm66018 	RW_EXIT(&vswp->if_lockrw);
1016e1ebb9ecSlm66018 
1017e1ebb9ecSlm66018 	mac_close(mh);
1018e1ebb9ecSlm66018 
1019e1ebb9ecSlm66018 	vswp->mdprops |= VSW_DEV_MACADDR;
1020e1ebb9ecSlm66018 
1021e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1022e1ebb9ecSlm66018 
1023e1ebb9ecSlm66018 	return (0);
1024e1ebb9ecSlm66018 }
1025e1ebb9ecSlm66018 
1026e1ebb9ecSlm66018 /*
1027e1ebb9ecSlm66018  * Check to see if the card supports the setting of multiple unicst
1028e1ebb9ecSlm66018  * addresses.
1029e1ebb9ecSlm66018  *
1030e1ebb9ecSlm66018  * Returns 0 if card supports the programming of multiple unicast addresses
1031e1ebb9ecSlm66018  * and there are free address slots available, otherwise returns 1.
1032e1ebb9ecSlm66018  */
1033e1ebb9ecSlm66018 static int
1034e1ebb9ecSlm66018 vsw_get_hw_maddr(vsw_t *vswp)
1035e1ebb9ecSlm66018 {
1036e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1037e1ebb9ecSlm66018 
103834683adeSsg70180 	mutex_enter(&vswp->mac_lock);
1039e1ebb9ecSlm66018 	if (vswp->mh == NULL) {
104034683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1041e1ebb9ecSlm66018 		return (1);
1042e1ebb9ecSlm66018 	}
1043e1ebb9ecSlm66018 
1044e1ebb9ecSlm66018 	if (!mac_capab_get(vswp->mh, MAC_CAPAB_MULTIADDRESS, &vswp->maddr)) {
1045e1ebb9ecSlm66018 		DWARN(vswp, "Unable to get capabilities of"
1046e1ebb9ecSlm66018 			" underlying device (%s)", vswp->physname);
104734683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1048e1ebb9ecSlm66018 		return (1);
1049e1ebb9ecSlm66018 	}
105034683adeSsg70180 	mutex_exit(&vswp->mac_lock);
1051e1ebb9ecSlm66018 
1052e1ebb9ecSlm66018 	if (vswp->maddr.maddr_naddrfree == 0) {
10537636cb21Slm66018 		cmn_err(CE_WARN,
105434683adeSsg70180 			"!vsw%d: device %s has no free unicast address slots",
105534683adeSsg70180 				vswp->instance, vswp->physname);
1056e1ebb9ecSlm66018 		return (1);
1057e1ebb9ecSlm66018 	}
1058e1ebb9ecSlm66018 
1059e1ebb9ecSlm66018 	D2(vswp, "%s: %d addrs : %d free", __func__,
1060e1ebb9ecSlm66018 		vswp->maddr.maddr_naddr, vswp->maddr.maddr_naddrfree);
1061e1ebb9ecSlm66018 
1062e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1063e1ebb9ecSlm66018 
1064e1ebb9ecSlm66018 	return (0);
1065e1ebb9ecSlm66018 }
1066e1ebb9ecSlm66018 
1067e1ebb9ecSlm66018 /*
106834683adeSsg70180  * Setup the required switching mode.
106934683adeSsg70180  *
107034683adeSsg70180  * Returns 0 on success, 1 on failure.
107134683adeSsg70180  */
107234683adeSsg70180 static int
107334683adeSsg70180 vsw_setup_switching(vsw_t *vswp)
107434683adeSsg70180 {
107534683adeSsg70180 	int	i, rv = 1;
107634683adeSsg70180 
107734683adeSsg70180 	D1(vswp, "%s: enter", __func__);
107834683adeSsg70180 
107934683adeSsg70180 	/* select best switching mode */
108034683adeSsg70180 	for (i = 0; i < vswp->smode_num; i++) {
108134683adeSsg70180 		vswp->smode_idx = i;
108234683adeSsg70180 		switch (vswp->smode[i]) {
108334683adeSsg70180 		case VSW_LAYER2:
108434683adeSsg70180 		case VSW_LAYER2_PROMISC:
108534683adeSsg70180 			rv = vsw_setup_layer2(vswp);
108634683adeSsg70180 			break;
108734683adeSsg70180 
108834683adeSsg70180 		case VSW_LAYER3:
108934683adeSsg70180 			rv = vsw_setup_layer3(vswp);
109034683adeSsg70180 			break;
109134683adeSsg70180 
109234683adeSsg70180 		default:
109334683adeSsg70180 			DERR(vswp, "unknown switch mode");
109434683adeSsg70180 			rv = 1;
109534683adeSsg70180 			break;
109634683adeSsg70180 		}
109734683adeSsg70180 
109834683adeSsg70180 		if (rv == 0)
109934683adeSsg70180 			break;
110034683adeSsg70180 	}
110134683adeSsg70180 
110234683adeSsg70180 	if (rv == 1) {
110334683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to setup specified "
110434683adeSsg70180 			"switching mode", vswp->instance);
110534683adeSsg70180 		return (rv);
110634683adeSsg70180 	}
110734683adeSsg70180 
110834683adeSsg70180 	D2(vswp, "%s: Operating in mode %d", __func__,
110934683adeSsg70180 					vswp->smode[vswp->smode_idx]);
111034683adeSsg70180 
111134683adeSsg70180 	D1(vswp, "%s: exit", __func__);
111234683adeSsg70180 
111334683adeSsg70180 	return (0);
111434683adeSsg70180 }
111534683adeSsg70180 
111634683adeSsg70180 /*
1117e1ebb9ecSlm66018  * Setup for layer 2 switching.
1118e1ebb9ecSlm66018  *
1119e1ebb9ecSlm66018  * Returns 0 on success, 1 on failure.
1120e1ebb9ecSlm66018  */
11211ae08745Sheppo static int
11221ae08745Sheppo vsw_setup_layer2(vsw_t *vswp)
11231ae08745Sheppo {
11241ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11251ae08745Sheppo 
112634683adeSsg70180 	vswp->vsw_switch_frame = vsw_switch_l2_frame;
11271ae08745Sheppo 
11281ae08745Sheppo 	/*
11291ae08745Sheppo 	 * Attempt to link into the MAC layer so we can get
11301ae08745Sheppo 	 * and send packets out over the physical adapter.
11311ae08745Sheppo 	 */
11321ae08745Sheppo 	if (vswp->mdprops & VSW_MD_PHYSNAME) {
11331ae08745Sheppo 		if (vsw_mac_attach(vswp) != 0) {
11341ae08745Sheppo 			/*
11351ae08745Sheppo 			 * Registration with the MAC layer has failed,
11361ae08745Sheppo 			 * so return 1 so that can fall back to next
11371ae08745Sheppo 			 * prefered switching method.
11381ae08745Sheppo 			 */
113934683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to join as MAC layer "
114034683adeSsg70180 				"client", vswp->instance);
1141e1ebb9ecSlm66018 			return (1);
11421ae08745Sheppo 		}
1143e1ebb9ecSlm66018 
1144e1ebb9ecSlm66018 		if (vswp->smode[vswp->smode_idx] == VSW_LAYER2) {
1145e1ebb9ecSlm66018 			/*
1146e1ebb9ecSlm66018 			 * Verify that underlying device can support multiple
1147e1ebb9ecSlm66018 			 * unicast mac addresses, and has free capacity.
1148e1ebb9ecSlm66018 			 */
1149e1ebb9ecSlm66018 			if (vsw_get_hw_maddr(vswp) != 0) {
115034683adeSsg70180 				cmn_err(CE_WARN, "!vsw%d: Unable to setup "
115134683adeSsg70180 					"switching", vswp->instance);
1152e1ebb9ecSlm66018 				vsw_mac_detach(vswp);
1153e1ebb9ecSlm66018 				return (1);
1154e1ebb9ecSlm66018 			}
1155e1ebb9ecSlm66018 		}
1156e1ebb9ecSlm66018 
11571ae08745Sheppo 	} else {
1158e1ebb9ecSlm66018 		/*
1159e1ebb9ecSlm66018 		 * No physical device name found in MD which is
1160e1ebb9ecSlm66018 		 * required for layer 2.
1161e1ebb9ecSlm66018 		 */
116234683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: no physical device name specified",
116334683adeSsg70180 			vswp->instance);
1164e1ebb9ecSlm66018 		return (1);
11651ae08745Sheppo 	}
11661ae08745Sheppo 
11671ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
11681ae08745Sheppo 
1169e1ebb9ecSlm66018 	return (0);
11701ae08745Sheppo }
11711ae08745Sheppo 
11721ae08745Sheppo static int
11731ae08745Sheppo vsw_setup_layer3(vsw_t *vswp)
11741ae08745Sheppo {
11751ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11761ae08745Sheppo 
11771ae08745Sheppo 	D2(vswp, "%s: operating in layer 3 mode", __func__);
117834683adeSsg70180 	vswp->vsw_switch_frame = vsw_switch_l3_frame;
11791ae08745Sheppo 
11801ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
11811ae08745Sheppo 
11821ae08745Sheppo 	return (0);
11831ae08745Sheppo }
11841ae08745Sheppo 
11851ae08745Sheppo /*
11861ae08745Sheppo  * Link into the MAC layer to gain access to the services provided by
11871ae08745Sheppo  * the underlying physical device driver (which should also have
11881ae08745Sheppo  * registered with the MAC layer).
11891ae08745Sheppo  *
11901ae08745Sheppo  * Only when in layer 2 mode.
11911ae08745Sheppo  */
11921ae08745Sheppo static int
11931ae08745Sheppo vsw_mac_attach(vsw_t *vswp)
11941ae08745Sheppo {
1195ba2e4443Sseb 	char	drv[LIFNAMSIZ];
1196ba2e4443Sseb 	uint_t	ddi_instance;
1197ba2e4443Sseb 
11987636cb21Slm66018 	D1(vswp, "%s: enter", __func__);
11991ae08745Sheppo 
120034683adeSsg70180 	ASSERT(vswp->mh == NULL);
120134683adeSsg70180 	ASSERT(vswp->mrh == NULL);
120234683adeSsg70180 	ASSERT(vswp->mstarted == B_FALSE);
120334683adeSsg70180 	ASSERT(vswp->mresources == B_FALSE);
12041ae08745Sheppo 
12051ae08745Sheppo 	ASSERT(vswp->mdprops & VSW_MD_PHYSNAME);
12061ae08745Sheppo 
120734683adeSsg70180 	mutex_enter(&vswp->mac_lock);
1208ba2e4443Sseb 	if (ddi_parse(vswp->physname, drv, &ddi_instance) != DDI_SUCCESS) {
120934683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: invalid device name: %s",
121034683adeSsg70180 			vswp->instance, vswp->physname);
1211ba2e4443Sseb 		goto mac_fail_exit;
1212ba2e4443Sseb 	}
121334683adeSsg70180 
1214ba2e4443Sseb 	if ((mac_open(vswp->physname, ddi_instance, &vswp->mh)) != 0) {
121534683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: mac_open %s failed",
121634683adeSsg70180 			vswp->instance, vswp->physname);
12171ae08745Sheppo 		goto mac_fail_exit;
12181ae08745Sheppo 	}
12191ae08745Sheppo 
12207636cb21Slm66018 	ASSERT(vswp->mh != NULL);
12217636cb21Slm66018 
12221ae08745Sheppo 	D2(vswp, "vsw_mac_attach: using device %s", vswp->physname);
12231ae08745Sheppo 
12247636cb21Slm66018 	if (vsw_multi_ring_enable) {
122534683adeSsg70180 		/*
122634683adeSsg70180 		 * Initialize the ring table.
122734683adeSsg70180 		 */
12287636cb21Slm66018 		vsw_mac_ring_tbl_init(vswp);
12291ae08745Sheppo 
12307636cb21Slm66018 		/*
123134683adeSsg70180 		 * Register our rx callback function.
12327636cb21Slm66018 		 */
12337636cb21Slm66018 		vswp->mrh = mac_rx_add(vswp->mh,
12347636cb21Slm66018 			vsw_rx_queue_cb, (void *)vswp);
123534683adeSsg70180 		ASSERT(vswp->mrh != NULL);
12367636cb21Slm66018 
12377636cb21Slm66018 		/*
12387636cb21Slm66018 		 * Register our mac resource callback.
12397636cb21Slm66018 		 */
12407636cb21Slm66018 		mac_resource_set(vswp->mh, vsw_mac_ring_add_cb, (void *)vswp);
12417636cb21Slm66018 		vswp->mresources = B_TRUE;
12427636cb21Slm66018 
12437636cb21Slm66018 		/*
12447636cb21Slm66018 		 * Get the ring resources available to us from
12457636cb21Slm66018 		 * the mac below us.
12467636cb21Slm66018 		 */
12477636cb21Slm66018 		mac_resources(vswp->mh);
12487636cb21Slm66018 	} else {
12497636cb21Slm66018 		/*
12507636cb21Slm66018 		 * Just register our rx callback function
12517636cb21Slm66018 		 */
12527636cb21Slm66018 		vswp->mrh = mac_rx_add(vswp->mh, vsw_rx_cb, (void *)vswp);
12537636cb21Slm66018 		ASSERT(vswp->mrh != NULL);
125434683adeSsg70180 	}
12557636cb21Slm66018 
12567636cb21Slm66018 	/* Get the MAC tx fn */
12571ae08745Sheppo 	vswp->txinfo = mac_tx_get(vswp->mh);
12581ae08745Sheppo 
12591ae08745Sheppo 	/* start the interface */
12601ae08745Sheppo 	if (mac_start(vswp->mh) != 0) {
126134683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Could not start mac interface",
126234683adeSsg70180 			vswp->instance);
12631ae08745Sheppo 		goto mac_fail_exit;
12641ae08745Sheppo 	}
12651ae08745Sheppo 
126634683adeSsg70180 	mutex_exit(&vswp->mac_lock);
126734683adeSsg70180 
12687636cb21Slm66018 	vswp->mstarted = B_TRUE;
12697636cb21Slm66018 
12707636cb21Slm66018 	D1(vswp, "%s: exit", __func__);
12711ae08745Sheppo 	return (0);
12721ae08745Sheppo 
12731ae08745Sheppo mac_fail_exit:
127434683adeSsg70180 	mutex_exit(&vswp->mac_lock);
12757636cb21Slm66018 	vsw_mac_detach(vswp);
12761ae08745Sheppo 
12777636cb21Slm66018 	D1(vswp, "%s: exit", __func__);
12781ae08745Sheppo 	return (1);
12791ae08745Sheppo }
12801ae08745Sheppo 
12811ae08745Sheppo static void
12821ae08745Sheppo vsw_mac_detach(vsw_t *vswp)
12831ae08745Sheppo {
12841ae08745Sheppo 	D1(vswp, "vsw_mac_detach: enter");
12851ae08745Sheppo 
12867636cb21Slm66018 	ASSERT(vswp != NULL);
12877636cb21Slm66018 
12887636cb21Slm66018 	if (vsw_multi_ring_enable) {
12897636cb21Slm66018 		vsw_mac_ring_tbl_destroy(vswp);
12907636cb21Slm66018 	}
12917636cb21Slm66018 
129234683adeSsg70180 	mutex_enter(&vswp->mac_lock);
129334683adeSsg70180 
1294b9a6d57aSsg70180 	if (vswp->mh != NULL) {
12957636cb21Slm66018 		if (vswp->mstarted)
12967636cb21Slm66018 			mac_stop(vswp->mh);
12971ae08745Sheppo 		if (vswp->mrh != NULL)
12981ae08745Sheppo 			mac_rx_remove(vswp->mh, vswp->mrh);
12997636cb21Slm66018 		if (vswp->mresources)
13007636cb21Slm66018 			mac_resource_set(vswp->mh, NULL, NULL);
13011ae08745Sheppo 		mac_close(vswp->mh);
1302b9a6d57aSsg70180 	}
13031ae08745Sheppo 
13041ae08745Sheppo 	vswp->mrh = NULL;
13051ae08745Sheppo 	vswp->mh = NULL;
13061ae08745Sheppo 	vswp->txinfo = NULL;
13077636cb21Slm66018 	vswp->mstarted = B_FALSE;
13081ae08745Sheppo 
130934683adeSsg70180 	mutex_exit(&vswp->mac_lock);
131034683adeSsg70180 
13111ae08745Sheppo 	D1(vswp, "vsw_mac_detach: exit");
13121ae08745Sheppo }
13131ae08745Sheppo 
13141ae08745Sheppo /*
1315e1ebb9ecSlm66018  * Depending on the mode specified, the capabilites and capacity
1316e1ebb9ecSlm66018  * of the underlying device setup the physical device.
13171ae08745Sheppo  *
1318e1ebb9ecSlm66018  * If in layer 3 mode, then do nothing.
1319e1ebb9ecSlm66018  *
1320e1ebb9ecSlm66018  * If in layer 2 programmed mode attempt to program the unicast address
1321e1ebb9ecSlm66018  * associated with the port into the physical device. If this is not
1322e1ebb9ecSlm66018  * possible due to resource exhaustion or simply because the device does
1323e1ebb9ecSlm66018  * not support multiple unicast addresses then if required fallback onto
1324e1ebb9ecSlm66018  * putting the card into promisc mode.
1325e1ebb9ecSlm66018  *
1326e1ebb9ecSlm66018  * If in promisc mode then simply set the card into promisc mode.
1327e1ebb9ecSlm66018  *
1328e1ebb9ecSlm66018  * Returns 0 success, 1 on failure.
13291ae08745Sheppo  */
1330e1ebb9ecSlm66018 static int
1331e1ebb9ecSlm66018 vsw_set_hw(vsw_t *vswp, vsw_port_t *port)
13321ae08745Sheppo {
1333e1ebb9ecSlm66018 	mac_multi_addr_t	mac_addr;
1334e1ebb9ecSlm66018 	void			*mah;
1335e1ebb9ecSlm66018 	int			err;
13361ae08745Sheppo 
1337e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1338e1ebb9ecSlm66018 
1339e1ebb9ecSlm66018 	if (vswp->smode[vswp->smode_idx] == VSW_LAYER3)
1340e1ebb9ecSlm66018 		return (0);
1341e1ebb9ecSlm66018 
1342e1ebb9ecSlm66018 	if (vswp->smode[vswp->smode_idx] == VSW_LAYER2_PROMISC) {
1343e1ebb9ecSlm66018 		return (vsw_set_hw_promisc(vswp, port));
1344e1ebb9ecSlm66018 	}
1345e1ebb9ecSlm66018 
1346e1ebb9ecSlm66018 	if (vswp->maddr.maddr_handle == NULL)
1347e1ebb9ecSlm66018 		return (1);
1348e1ebb9ecSlm66018 
1349e1ebb9ecSlm66018 	mah = vswp->maddr.maddr_handle;
1350e1ebb9ecSlm66018 
1351e1ebb9ecSlm66018 	/*
1352e1ebb9ecSlm66018 	 * Attempt to program the unicast address into the HW.
1353e1ebb9ecSlm66018 	 */
1354e1ebb9ecSlm66018 	mac_addr.mma_addrlen = ETHERADDRL;
1355e1ebb9ecSlm66018 	ether_copy(&port->p_macaddr, &mac_addr.mma_addr);
1356e1ebb9ecSlm66018 
1357e1ebb9ecSlm66018 	err = vswp->maddr.maddr_add(mah, &mac_addr);
1358e1ebb9ecSlm66018 	if (err != 0) {
135934683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: failed to program addr "
1360e1ebb9ecSlm66018 			"%x:%x:%x:%x:%x:%x for port %d into device %s "
136134683adeSsg70180 			": err %d", vswp->instance,
136234683adeSsg70180 			port->p_macaddr.ether_addr_octet[0],
1363e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[1],
1364e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[2],
1365e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[3],
1366e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[4],
1367e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[5],
1368e1ebb9ecSlm66018 			port->p_instance, vswp->physname, err);
1369e1ebb9ecSlm66018 
1370e1ebb9ecSlm66018 		/*
1371e1ebb9ecSlm66018 		 * Mark that attempt should be made to re-config sometime
1372e1ebb9ecSlm66018 		 * in future if a port is deleted.
1373e1ebb9ecSlm66018 		 */
1374e1ebb9ecSlm66018 		vswp->recfg_reqd = B_TRUE;
1375e1ebb9ecSlm66018 
1376e1ebb9ecSlm66018 		/*
1377e1ebb9ecSlm66018 		 * Only 1 mode specified, nothing more to do.
1378e1ebb9ecSlm66018 		 */
1379e1ebb9ecSlm66018 		if (vswp->smode_num == 1)
1380e1ebb9ecSlm66018 			return (err);
1381e1ebb9ecSlm66018 
1382e1ebb9ecSlm66018 		/*
1383e1ebb9ecSlm66018 		 * If promiscuous was next mode specified try to
1384e1ebb9ecSlm66018 		 * set the card into that mode.
1385e1ebb9ecSlm66018 		 */
1386e1ebb9ecSlm66018 		if ((vswp->smode_idx <= (vswp->smode_num - 2)) &&
1387e1ebb9ecSlm66018 			(vswp->smode[vswp->smode_idx + 1]
1388e1ebb9ecSlm66018 					== VSW_LAYER2_PROMISC)) {
1389e1ebb9ecSlm66018 			vswp->smode_idx += 1;
1390e1ebb9ecSlm66018 			return (vsw_set_hw_promisc(vswp, port));
1391e1ebb9ecSlm66018 		}
1392e1ebb9ecSlm66018 		return (err);
1393e1ebb9ecSlm66018 	}
1394e1ebb9ecSlm66018 
1395e1ebb9ecSlm66018 	port->addr_slot = mac_addr.mma_slot;
1396e1ebb9ecSlm66018 	port->addr_set = VSW_ADDR_HW;
1397e1ebb9ecSlm66018 
1398e1ebb9ecSlm66018 	D2(vswp, "programmed addr %x:%x:%x:%x:%x:%x for port %d "
1399e1ebb9ecSlm66018 		"into slot %d of device %s",
1400e1ebb9ecSlm66018 		port->p_macaddr.ether_addr_octet[0],
1401e1ebb9ecSlm66018 		port->p_macaddr.ether_addr_octet[1],
1402e1ebb9ecSlm66018 		port->p_macaddr.ether_addr_octet[2],
1403e1ebb9ecSlm66018 		port->p_macaddr.ether_addr_octet[3],
1404e1ebb9ecSlm66018 		port->p_macaddr.ether_addr_octet[4],
1405e1ebb9ecSlm66018 		port->p_macaddr.ether_addr_octet[5],
1406e1ebb9ecSlm66018 		port->p_instance, port->addr_slot, vswp->physname);
1407e1ebb9ecSlm66018 
1408e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1409e1ebb9ecSlm66018 
1410e1ebb9ecSlm66018 	return (0);
1411e1ebb9ecSlm66018 }
1412e1ebb9ecSlm66018 
1413e1ebb9ecSlm66018 /*
1414e1ebb9ecSlm66018  * If in layer 3 mode do nothing.
1415e1ebb9ecSlm66018  *
1416e1ebb9ecSlm66018  * If in layer 2 switched mode remove the address from the physical
1417e1ebb9ecSlm66018  * device.
1418e1ebb9ecSlm66018  *
1419e1ebb9ecSlm66018  * If in layer 2 promiscuous mode disable promisc mode.
1420e1ebb9ecSlm66018  *
1421e1ebb9ecSlm66018  * Returns 0 on success.
1422e1ebb9ecSlm66018  */
1423e1ebb9ecSlm66018 static int
1424e1ebb9ecSlm66018 vsw_unset_hw(vsw_t *vswp, vsw_port_t *port)
1425e1ebb9ecSlm66018 {
1426e1ebb9ecSlm66018 	int		err;
1427e1ebb9ecSlm66018 	void		*mah;
1428e1ebb9ecSlm66018 
1429e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1430e1ebb9ecSlm66018 
1431e1ebb9ecSlm66018 	if (vswp->smode[vswp->smode_idx] == VSW_LAYER3)
1432e1ebb9ecSlm66018 		return (0);
1433e1ebb9ecSlm66018 
1434e1ebb9ecSlm66018 	if (port->addr_set == VSW_ADDR_PROMISC) {
1435e1ebb9ecSlm66018 		return (vsw_unset_hw_promisc(vswp, port));
1436e1ebb9ecSlm66018 	}
1437e1ebb9ecSlm66018 
1438e1ebb9ecSlm66018 	if (port->addr_set == VSW_ADDR_HW) {
1439e1ebb9ecSlm66018 		if (vswp->maddr.maddr_handle == NULL)
1440e1ebb9ecSlm66018 			return (1);
1441e1ebb9ecSlm66018 
1442e1ebb9ecSlm66018 		mah = vswp->maddr.maddr_handle;
1443e1ebb9ecSlm66018 
1444e1ebb9ecSlm66018 		err = vswp->maddr.maddr_remove(mah, port->addr_slot);
1445e1ebb9ecSlm66018 		if (err != 0) {
144634683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to remove addr "
1447e1ebb9ecSlm66018 				"%x:%x:%x:%x:%x:%x for port %d from device %s"
144834683adeSsg70180 				" : (err %d)", vswp->instance,
1449e1ebb9ecSlm66018 				port->p_macaddr.ether_addr_octet[0],
1450e1ebb9ecSlm66018 				port->p_macaddr.ether_addr_octet[1],
1451e1ebb9ecSlm66018 				port->p_macaddr.ether_addr_octet[2],
1452e1ebb9ecSlm66018 				port->p_macaddr.ether_addr_octet[3],
1453e1ebb9ecSlm66018 				port->p_macaddr.ether_addr_octet[4],
1454e1ebb9ecSlm66018 				port->p_macaddr.ether_addr_octet[5],
1455e1ebb9ecSlm66018 				port->p_instance, vswp->physname, err);
1456e1ebb9ecSlm66018 			return (err);
1457e1ebb9ecSlm66018 		}
1458e1ebb9ecSlm66018 
1459e1ebb9ecSlm66018 		port->addr_set = VSW_ADDR_UNSET;
1460e1ebb9ecSlm66018 
1461e1ebb9ecSlm66018 		D2(vswp, "removed addr %x:%x:%x:%x:%x:%x for "
1462e1ebb9ecSlm66018 			"port %d from device %s",
1463e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[0],
1464e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[1],
1465e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[2],
1466e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[3],
1467e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[4],
1468e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[5],
1469e1ebb9ecSlm66018 			port->p_instance, vswp->physname);
1470e1ebb9ecSlm66018 	}
1471e1ebb9ecSlm66018 
1472e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1473e1ebb9ecSlm66018 	return (0);
1474e1ebb9ecSlm66018 }
1475e1ebb9ecSlm66018 
1476e1ebb9ecSlm66018 /*
1477e1ebb9ecSlm66018  * Set network card into promisc mode.
1478e1ebb9ecSlm66018  *
1479e1ebb9ecSlm66018  * Returns 0 on success, 1 on failure.
1480e1ebb9ecSlm66018  */
1481e1ebb9ecSlm66018 static int
1482e1ebb9ecSlm66018 vsw_set_hw_promisc(vsw_t *vswp, vsw_port_t *port)
1483e1ebb9ecSlm66018 {
1484e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1485e1ebb9ecSlm66018 
148634683adeSsg70180 	mutex_enter(&vswp->mac_lock);
148734683adeSsg70180 	if (vswp->mh == NULL) {
148834683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1489e1ebb9ecSlm66018 		return (1);
149034683adeSsg70180 	}
1491e1ebb9ecSlm66018 
1492e1ebb9ecSlm66018 	if (vswp->promisc_cnt++ == 0) {
1493e1ebb9ecSlm66018 		if (mac_promisc_set(vswp->mh, B_TRUE, MAC_DEVPROMISC) != 0) {
1494e1ebb9ecSlm66018 			vswp->promisc_cnt--;
149534683adeSsg70180 			mutex_exit(&vswp->mac_lock);
1496e1ebb9ecSlm66018 			return (1);
1497e1ebb9ecSlm66018 		}
149834683adeSsg70180 		cmn_err(CE_NOTE, "!vsw%d: switching device %s into "
149934683adeSsg70180 			"promiscuous mode", vswp->instance, vswp->physname);
1500e1ebb9ecSlm66018 	}
150134683adeSsg70180 	mutex_exit(&vswp->mac_lock);
1502e1ebb9ecSlm66018 	port->addr_set = VSW_ADDR_PROMISC;
1503e1ebb9ecSlm66018 
1504e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1505e1ebb9ecSlm66018 
1506e1ebb9ecSlm66018 	return (0);
1507e1ebb9ecSlm66018 }
1508e1ebb9ecSlm66018 
1509e1ebb9ecSlm66018 /*
1510e1ebb9ecSlm66018  * Turn off promiscuous mode on network card.
1511e1ebb9ecSlm66018  *
1512e1ebb9ecSlm66018  * Returns 0 on success, 1 on failure.
1513e1ebb9ecSlm66018  */
1514e1ebb9ecSlm66018 static int
1515e1ebb9ecSlm66018 vsw_unset_hw_promisc(vsw_t *vswp, vsw_port_t *port)
1516e1ebb9ecSlm66018 {
1517e1ebb9ecSlm66018 	vsw_port_list_t 	*plist = &vswp->plist;
1518e1ebb9ecSlm66018 
151934683adeSsg70180 	D2(vswp, "%s: enter", __func__);
1520e1ebb9ecSlm66018 
152134683adeSsg70180 	mutex_enter(&vswp->mac_lock);
152234683adeSsg70180 	if (vswp->mh == NULL) {
152334683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1524e1ebb9ecSlm66018 		return (1);
152534683adeSsg70180 	}
1526e1ebb9ecSlm66018 
1527e1ebb9ecSlm66018 	ASSERT(port->addr_set == VSW_ADDR_PROMISC);
1528e1ebb9ecSlm66018 
1529e1ebb9ecSlm66018 	if (--vswp->promisc_cnt == 0) {
1530e1ebb9ecSlm66018 		if (mac_promisc_set(vswp->mh, B_FALSE, MAC_DEVPROMISC) != 0) {
1531e1ebb9ecSlm66018 			vswp->promisc_cnt++;
153234683adeSsg70180 			mutex_exit(&vswp->mac_lock);
1533e1ebb9ecSlm66018 			return (1);
1534e1ebb9ecSlm66018 		}
1535e1ebb9ecSlm66018 
1536e1ebb9ecSlm66018 		/*
1537e1ebb9ecSlm66018 		 * We are exiting promisc mode either because we were
1538e1ebb9ecSlm66018 		 * only in promisc mode because we had failed over from
1539e1ebb9ecSlm66018 		 * switched mode due to HW resource issues, or the user
1540e1ebb9ecSlm66018 		 * wanted the card in promisc mode for all the ports and
1541e1ebb9ecSlm66018 		 * the last port is now being deleted. Tweak the message
1542e1ebb9ecSlm66018 		 * accordingly.
1543e1ebb9ecSlm66018 		 */
1544e1ebb9ecSlm66018 		if (plist->num_ports != 0) {
154534683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: switching device %s back to "
154634683adeSsg70180 				"programmed mode", vswp->instance,
154734683adeSsg70180 				vswp->physname);
15481ae08745Sheppo 		} else {
154934683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: switching device %s out of "
155034683adeSsg70180 				"promiscuous mode", vswp->instance,
155134683adeSsg70180 				vswp->physname);
15521ae08745Sheppo 		}
15531ae08745Sheppo 	}
155434683adeSsg70180 	mutex_exit(&vswp->mac_lock);
1555e1ebb9ecSlm66018 	port->addr_set = VSW_ADDR_UNSET;
1556e1ebb9ecSlm66018 
1557e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1558e1ebb9ecSlm66018 	return (0);
1559e1ebb9ecSlm66018 }
1560e1ebb9ecSlm66018 
1561e1ebb9ecSlm66018 /*
1562e1ebb9ecSlm66018  * Determine whether or not we are operating in our prefered
1563e1ebb9ecSlm66018  * mode and if not whether the physical resources now allow us
1564e1ebb9ecSlm66018  * to operate in it.
1565e1ebb9ecSlm66018  *
1566e1ebb9ecSlm66018  * Should only be invoked after port which is being deleted has been
1567e1ebb9ecSlm66018  * removed from the port list.
1568e1ebb9ecSlm66018  */
1569e1ebb9ecSlm66018 static int
1570e1ebb9ecSlm66018 vsw_reconfig_hw(vsw_t *vswp)
1571e1ebb9ecSlm66018 {
1572e1ebb9ecSlm66018 	vsw_port_list_t 	*plist = &vswp->plist;
1573e1ebb9ecSlm66018 	mac_multi_addr_t	mac_addr;
1574e1ebb9ecSlm66018 	vsw_port_t		*tp;
1575e1ebb9ecSlm66018 	void			*mah;
1576e1ebb9ecSlm66018 	int			rv = 0;
1577e1ebb9ecSlm66018 	int			s_idx;
1578e1ebb9ecSlm66018 
1579e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1580e1ebb9ecSlm66018 
1581e1ebb9ecSlm66018 	if (vswp->maddr.maddr_handle == NULL)
1582e1ebb9ecSlm66018 		return (1);
1583e1ebb9ecSlm66018 
1584e1ebb9ecSlm66018 	/*
1585e1ebb9ecSlm66018 	 * Check if there are now sufficient HW resources to
1586e1ebb9ecSlm66018 	 * attempt a re-config.
1587e1ebb9ecSlm66018 	 */
1588e1ebb9ecSlm66018 	if (plist->num_ports > vswp->maddr.maddr_naddrfree)
1589e1ebb9ecSlm66018 		return (1);
1590e1ebb9ecSlm66018 
1591e1ebb9ecSlm66018 	/*
1592e1ebb9ecSlm66018 	 * If we are in layer 2 (i.e. switched) or would like to be
1593e1ebb9ecSlm66018 	 * in layer 2 then check if any ports need to be programmed
1594e1ebb9ecSlm66018 	 * into the HW.
1595e1ebb9ecSlm66018 	 *
1596e1ebb9ecSlm66018 	 * This can happen in two cases - switched was specified as
1597e1ebb9ecSlm66018 	 * the prefered mode of operation but we exhausted the HW
1598e1ebb9ecSlm66018 	 * resources and so failed over to the next specifed mode,
1599e1ebb9ecSlm66018 	 * or switched was the only mode specified so after HW
1600e1ebb9ecSlm66018 	 * resources were exhausted there was nothing more we
1601e1ebb9ecSlm66018 	 * could do.
1602e1ebb9ecSlm66018 	 */
1603e1ebb9ecSlm66018 	if (vswp->smode_idx > 0)
1604e1ebb9ecSlm66018 		s_idx = vswp->smode_idx - 1;
1605e1ebb9ecSlm66018 	else
1606e1ebb9ecSlm66018 		s_idx = vswp->smode_idx;
1607e1ebb9ecSlm66018 
1608e1ebb9ecSlm66018 	if (vswp->smode[s_idx] == VSW_LAYER2) {
1609e1ebb9ecSlm66018 		mah = vswp->maddr.maddr_handle;
1610e1ebb9ecSlm66018 
1611e1ebb9ecSlm66018 		D2(vswp, "%s: attempting reconfig..", __func__);
1612e1ebb9ecSlm66018 
1613e1ebb9ecSlm66018 		/*
1614e1ebb9ecSlm66018 		 * Scan the port list for any port whose address has not
1615e1ebb9ecSlm66018 		 * be programmed in HW - there should be a max of one.
1616e1ebb9ecSlm66018 		 */
1617e1ebb9ecSlm66018 		for (tp = plist->head; tp != NULL; tp = tp->p_next) {
1618e1ebb9ecSlm66018 			if (tp->addr_set != VSW_ADDR_HW) {
1619e1ebb9ecSlm66018 				mac_addr.mma_addrlen = ETHERADDRL;
1620e1ebb9ecSlm66018 				ether_copy(&tp->p_macaddr, &mac_addr.mma_addr);
1621e1ebb9ecSlm66018 
1622e1ebb9ecSlm66018 				rv = vswp->maddr.maddr_add(mah, &mac_addr);
1623e1ebb9ecSlm66018 				if (rv != 0) {
1624e1ebb9ecSlm66018 					DWARN(vswp, "Error setting addr in "
1625e1ebb9ecSlm66018 						"HW for port %d err %d",
1626e1ebb9ecSlm66018 						tp->p_instance, rv);
1627e1ebb9ecSlm66018 					goto reconfig_err_exit;
1628e1ebb9ecSlm66018 				}
1629e1ebb9ecSlm66018 				tp->addr_slot = mac_addr.mma_slot;
1630e1ebb9ecSlm66018 
1631e1ebb9ecSlm66018 				D2(vswp, "re-programmed port %d "
1632e1ebb9ecSlm66018 					"addr %x:%x:%x:%x:%x:%x into slot %d"
1633e1ebb9ecSlm66018 					" of device %s", tp->p_instance,
1634e1ebb9ecSlm66018 					tp->p_macaddr.ether_addr_octet[0],
1635e1ebb9ecSlm66018 					tp->p_macaddr.ether_addr_octet[1],
1636e1ebb9ecSlm66018 					tp->p_macaddr.ether_addr_octet[2],
1637e1ebb9ecSlm66018 					tp->p_macaddr.ether_addr_octet[3],
1638e1ebb9ecSlm66018 					tp->p_macaddr.ether_addr_octet[4],
1639e1ebb9ecSlm66018 					tp->p_macaddr.ether_addr_octet[5],
1640e1ebb9ecSlm66018 					tp->addr_slot, vswp->physname);
1641e1ebb9ecSlm66018 
1642e1ebb9ecSlm66018 				/*
1643e1ebb9ecSlm66018 				 * If up to now we had to put the card into
1644e1ebb9ecSlm66018 				 * promisc mode to see this address, we
1645e1ebb9ecSlm66018 				 * can now safely disable promisc mode.
1646e1ebb9ecSlm66018 				 */
1647e1ebb9ecSlm66018 				if (tp->addr_set == VSW_ADDR_PROMISC)
1648e1ebb9ecSlm66018 					(void) vsw_unset_hw_promisc(vswp, tp);
1649e1ebb9ecSlm66018 
1650e1ebb9ecSlm66018 				tp->addr_set = VSW_ADDR_HW;
1651e1ebb9ecSlm66018 			}
1652e1ebb9ecSlm66018 		}
1653e1ebb9ecSlm66018 
1654e1ebb9ecSlm66018 		/* no further re-config needed */
1655e1ebb9ecSlm66018 		vswp->recfg_reqd = B_FALSE;
1656e1ebb9ecSlm66018 
1657e1ebb9ecSlm66018 		vswp->smode_idx = s_idx;
1658e1ebb9ecSlm66018 
1659e1ebb9ecSlm66018 		return (0);
1660e1ebb9ecSlm66018 	}
1661e1ebb9ecSlm66018 
1662e1ebb9ecSlm66018 reconfig_err_exit:
1663e1ebb9ecSlm66018 	return (rv);
16641ae08745Sheppo }
16651ae08745Sheppo 
16667636cb21Slm66018 static void
16677636cb21Slm66018 vsw_mac_ring_tbl_entry_init(vsw_t *vswp, vsw_mac_ring_t *ringp)
16687636cb21Slm66018 {
16697636cb21Slm66018 	ringp->ring_state = VSW_MAC_RING_FREE;
16707636cb21Slm66018 	ringp->ring_arg = NULL;
16717636cb21Slm66018 	ringp->ring_blank = NULL;
16727636cb21Slm66018 	ringp->ring_vqp = NULL;
16737636cb21Slm66018 	ringp->ring_vswp = vswp;
16747636cb21Slm66018 }
16757636cb21Slm66018 
16767636cb21Slm66018 static void
16777636cb21Slm66018 vsw_mac_ring_tbl_init(vsw_t *vswp)
16787636cb21Slm66018 {
16797636cb21Slm66018 	int		i;
16807636cb21Slm66018 
16817636cb21Slm66018 	mutex_init(&vswp->mac_ring_lock, NULL, MUTEX_DRIVER, NULL);
16827636cb21Slm66018 
16837636cb21Slm66018 	vswp->mac_ring_tbl_sz = vsw_mac_rx_rings;
16847636cb21Slm66018 	vswp->mac_ring_tbl  =
16857636cb21Slm66018 		kmem_alloc(vsw_mac_rx_rings * sizeof (vsw_mac_ring_t),
16867636cb21Slm66018 		KM_SLEEP);
16877636cb21Slm66018 
16887636cb21Slm66018 	for (i = 0; i < vswp->mac_ring_tbl_sz; i++)
16897636cb21Slm66018 		vsw_mac_ring_tbl_entry_init(vswp, &vswp->mac_ring_tbl[i]);
16907636cb21Slm66018 }
16917636cb21Slm66018 
16927636cb21Slm66018 static void
16937636cb21Slm66018 vsw_mac_ring_tbl_destroy(vsw_t *vswp)
16947636cb21Slm66018 {
16957636cb21Slm66018 	int		i;
169634683adeSsg70180 	vsw_mac_ring_t	*ringp;
16977636cb21Slm66018 
16987636cb21Slm66018 	mutex_enter(&vswp->mac_ring_lock);
16997636cb21Slm66018 	for (i = 0; i < vswp->mac_ring_tbl_sz; i++) {
170034683adeSsg70180 		ringp = &vswp->mac_ring_tbl[i];
170134683adeSsg70180 
170234683adeSsg70180 		if (ringp->ring_state != VSW_MAC_RING_FREE) {
17037636cb21Slm66018 			/*
17047636cb21Slm66018 			 * Destroy the queue.
17057636cb21Slm66018 			 */
170634683adeSsg70180 			vsw_queue_stop(ringp->ring_vqp);
170734683adeSsg70180 			vsw_queue_destroy(ringp->ring_vqp);
17087636cb21Slm66018 
17097636cb21Slm66018 			/*
17107636cb21Slm66018 			 * Re-initialize the structure.
17117636cb21Slm66018 			 */
171234683adeSsg70180 			vsw_mac_ring_tbl_entry_init(vswp, ringp);
17137636cb21Slm66018 		}
17147636cb21Slm66018 	}
17157636cb21Slm66018 	mutex_exit(&vswp->mac_ring_lock);
17167636cb21Slm66018 
17177636cb21Slm66018 	mutex_destroy(&vswp->mac_ring_lock);
17187636cb21Slm66018 	kmem_free(vswp->mac_ring_tbl,
17197636cb21Slm66018 		vswp->mac_ring_tbl_sz * sizeof (vsw_mac_ring_t));
17207636cb21Slm66018 	vswp->mac_ring_tbl_sz = 0;
17217636cb21Slm66018 }
17227636cb21Slm66018 
17237636cb21Slm66018 /*
17247636cb21Slm66018  * Handle resource add callbacks from the driver below.
17257636cb21Slm66018  */
17267636cb21Slm66018 static mac_resource_handle_t
17277636cb21Slm66018 vsw_mac_ring_add_cb(void *arg, mac_resource_t *mrp)
17287636cb21Slm66018 {
17297636cb21Slm66018 	vsw_t		*vswp = (vsw_t *)arg;
17307636cb21Slm66018 	mac_rx_fifo_t	*mrfp = (mac_rx_fifo_t *)mrp;
17317636cb21Slm66018 	vsw_mac_ring_t	*ringp;
17327636cb21Slm66018 	vsw_queue_t	*vqp;
17337636cb21Slm66018 	int		i;
17347636cb21Slm66018 
17357636cb21Slm66018 	ASSERT(vswp != NULL);
17367636cb21Slm66018 	ASSERT(mrp != NULL);
17377636cb21Slm66018 	ASSERT(vswp->mac_ring_tbl != NULL);
17387636cb21Slm66018 
17397636cb21Slm66018 	D1(vswp, "%s: enter", __func__);
17407636cb21Slm66018 
17417636cb21Slm66018 	/*
17427636cb21Slm66018 	 * Check to make sure we have the correct resource type.
17437636cb21Slm66018 	 */
17447636cb21Slm66018 	if (mrp->mr_type != MAC_RX_FIFO)
17457636cb21Slm66018 		return (NULL);
17467636cb21Slm66018 
17477636cb21Slm66018 	/*
17487636cb21Slm66018 	 * Find a open entry in the ring table.
17497636cb21Slm66018 	 */
17507636cb21Slm66018 	mutex_enter(&vswp->mac_ring_lock);
17517636cb21Slm66018 	for (i = 0; i < vswp->mac_ring_tbl_sz; i++) {
17527636cb21Slm66018 		ringp = &vswp->mac_ring_tbl[i];
17537636cb21Slm66018 
17547636cb21Slm66018 		/*
17557636cb21Slm66018 		 * Check for an empty slot, if found, then setup queue
17567636cb21Slm66018 		 * and thread.
17577636cb21Slm66018 		 */
17587636cb21Slm66018 		if (ringp->ring_state == VSW_MAC_RING_FREE) {
17597636cb21Slm66018 			/*
17607636cb21Slm66018 			 * Create the queue for this ring.
17617636cb21Slm66018 			 */
17627636cb21Slm66018 			vqp = vsw_queue_create();
17637636cb21Slm66018 
17647636cb21Slm66018 			/*
17657636cb21Slm66018 			 * Initialize the ring data structure.
17667636cb21Slm66018 			 */
17677636cb21Slm66018 			ringp->ring_vqp = vqp;
17687636cb21Slm66018 			ringp->ring_arg = mrfp->mrf_arg;
17697636cb21Slm66018 			ringp->ring_blank = mrfp->mrf_blank;
17707636cb21Slm66018 			ringp->ring_state = VSW_MAC_RING_INUSE;
17717636cb21Slm66018 
17727636cb21Slm66018 			/*
17737636cb21Slm66018 			 * Create the worker thread.
17747636cb21Slm66018 			 */
17757636cb21Slm66018 			vqp->vq_worker = thread_create(NULL, 0,
17767636cb21Slm66018 				vsw_queue_worker, ringp, 0, &p0,
17777636cb21Slm66018 				TS_RUN, minclsyspri);
17787636cb21Slm66018 			if (vqp->vq_worker == NULL) {
17797636cb21Slm66018 				vsw_queue_destroy(vqp);
17807636cb21Slm66018 				vsw_mac_ring_tbl_entry_init(vswp, ringp);
17817636cb21Slm66018 				ringp = NULL;
17827636cb21Slm66018 			}
17837636cb21Slm66018 
178434683adeSsg70180 			if (ringp != NULL) {
178534683adeSsg70180 				/*
178634683adeSsg70180 				 * Make sure thread get's running state for
178734683adeSsg70180 				 * this ring.
178834683adeSsg70180 				 */
178934683adeSsg70180 				mutex_enter(&vqp->vq_lock);
179034683adeSsg70180 				while ((vqp->vq_state != VSW_QUEUE_RUNNING) &&
179134683adeSsg70180 					(vqp->vq_state != VSW_QUEUE_DRAINED)) {
179234683adeSsg70180 					cv_wait(&vqp->vq_cv, &vqp->vq_lock);
179334683adeSsg70180 				}
179434683adeSsg70180 
179534683adeSsg70180 				/*
179634683adeSsg70180 				 * If the thread is not running, cleanup.
179734683adeSsg70180 				 */
179834683adeSsg70180 				if (vqp->vq_state == VSW_QUEUE_DRAINED) {
179934683adeSsg70180 					vsw_queue_destroy(vqp);
180034683adeSsg70180 					vsw_mac_ring_tbl_entry_init(vswp,
180134683adeSsg70180 						ringp);
180234683adeSsg70180 					ringp = NULL;
180334683adeSsg70180 				}
180434683adeSsg70180 				mutex_exit(&vqp->vq_lock);
180534683adeSsg70180 			}
180634683adeSsg70180 
18077636cb21Slm66018 			mutex_exit(&vswp->mac_ring_lock);
18087636cb21Slm66018 			D1(vswp, "%s: exit", __func__);
18097636cb21Slm66018 			return ((mac_resource_handle_t)ringp);
18107636cb21Slm66018 		}
18117636cb21Slm66018 	}
18127636cb21Slm66018 	mutex_exit(&vswp->mac_ring_lock);
18137636cb21Slm66018 
18147636cb21Slm66018 	/*
18157636cb21Slm66018 	 * No slots in the ring table available.
18167636cb21Slm66018 	 */
18177636cb21Slm66018 	D1(vswp, "%s: exit", __func__);
18187636cb21Slm66018 	return (NULL);
18197636cb21Slm66018 }
18207636cb21Slm66018 
18217636cb21Slm66018 static void
18227636cb21Slm66018 vsw_queue_stop(vsw_queue_t *vqp)
18237636cb21Slm66018 {
18247636cb21Slm66018 	mutex_enter(&vqp->vq_lock);
18257636cb21Slm66018 
18267636cb21Slm66018 	if (vqp->vq_state == VSW_QUEUE_RUNNING) {
18277636cb21Slm66018 		vqp->vq_state = VSW_QUEUE_STOP;
18287636cb21Slm66018 		cv_signal(&vqp->vq_cv);
18297636cb21Slm66018 
18307636cb21Slm66018 		while (vqp->vq_state != VSW_QUEUE_DRAINED)
18317636cb21Slm66018 			cv_wait(&vqp->vq_cv, &vqp->vq_lock);
18327636cb21Slm66018 	}
18337636cb21Slm66018 
183434683adeSsg70180 	vqp->vq_state = VSW_QUEUE_STOPPED;
183534683adeSsg70180 
18367636cb21Slm66018 	mutex_exit(&vqp->vq_lock);
18377636cb21Slm66018 }
18387636cb21Slm66018 
18397636cb21Slm66018 static vsw_queue_t *
18407636cb21Slm66018 vsw_queue_create()
18417636cb21Slm66018 {
18427636cb21Slm66018 	vsw_queue_t *vqp;
18437636cb21Slm66018 
18447636cb21Slm66018 	vqp = kmem_zalloc(sizeof (vsw_queue_t), KM_SLEEP);
18457636cb21Slm66018 
18467636cb21Slm66018 	mutex_init(&vqp->vq_lock, NULL, MUTEX_DRIVER, NULL);
18477636cb21Slm66018 	cv_init(&vqp->vq_cv, NULL, CV_DRIVER, NULL);
18487636cb21Slm66018 	vqp->vq_first = NULL;
18497636cb21Slm66018 	vqp->vq_last = NULL;
185034683adeSsg70180 	vqp->vq_state = VSW_QUEUE_STOPPED;
18517636cb21Slm66018 
18527636cb21Slm66018 	return (vqp);
18537636cb21Slm66018 }
18547636cb21Slm66018 
18557636cb21Slm66018 static void
18567636cb21Slm66018 vsw_queue_destroy(vsw_queue_t *vqp)
18577636cb21Slm66018 {
18587636cb21Slm66018 	cv_destroy(&vqp->vq_cv);
18597636cb21Slm66018 	mutex_destroy(&vqp->vq_lock);
18607636cb21Slm66018 	kmem_free(vqp, sizeof (vsw_queue_t));
18617636cb21Slm66018 }
18627636cb21Slm66018 
18637636cb21Slm66018 static void
18647636cb21Slm66018 vsw_queue_worker(vsw_mac_ring_t *rrp)
18657636cb21Slm66018 {
18667636cb21Slm66018 	mblk_t		*mp;
18677636cb21Slm66018 	vsw_queue_t	*vqp = rrp->ring_vqp;
18687636cb21Slm66018 	vsw_t		*vswp = rrp->ring_vswp;
18697636cb21Slm66018 
18707636cb21Slm66018 	mutex_enter(&vqp->vq_lock);
18717636cb21Slm66018 
187234683adeSsg70180 	ASSERT(vqp->vq_state == VSW_QUEUE_STOPPED);
18737636cb21Slm66018 
18747636cb21Slm66018 	/*
18757636cb21Slm66018 	 * Set the state to running, since the thread is now active.
18767636cb21Slm66018 	 */
18777636cb21Slm66018 	vqp->vq_state = VSW_QUEUE_RUNNING;
187834683adeSsg70180 	cv_signal(&vqp->vq_cv);
18797636cb21Slm66018 
18807636cb21Slm66018 	while (vqp->vq_state == VSW_QUEUE_RUNNING) {
18817636cb21Slm66018 		/*
18827636cb21Slm66018 		 * Wait for work to do or the state has changed
18837636cb21Slm66018 		 * to not running.
18847636cb21Slm66018 		 */
18857636cb21Slm66018 		while ((vqp->vq_state == VSW_QUEUE_RUNNING) &&
18867636cb21Slm66018 				(vqp->vq_first == NULL)) {
18877636cb21Slm66018 			cv_wait(&vqp->vq_cv, &vqp->vq_lock);
18887636cb21Slm66018 		}
18897636cb21Slm66018 
18907636cb21Slm66018 		/*
18917636cb21Slm66018 		 * Process packets that we received from the interface.
18927636cb21Slm66018 		 */
18937636cb21Slm66018 		if (vqp->vq_first != NULL) {
18947636cb21Slm66018 			mp = vqp->vq_first;
18957636cb21Slm66018 
18967636cb21Slm66018 			vqp->vq_first = NULL;
18977636cb21Slm66018 			vqp->vq_last = NULL;
18987636cb21Slm66018 
18997636cb21Slm66018 			mutex_exit(&vqp->vq_lock);
19007636cb21Slm66018 
19017636cb21Slm66018 			/* switch the chain of packets received */
190234683adeSsg70180 			vswp->vsw_switch_frame(vswp, mp,
190334683adeSsg70180 						VSW_PHYSDEV, NULL, NULL);
19047636cb21Slm66018 
19057636cb21Slm66018 			mutex_enter(&vqp->vq_lock);
19067636cb21Slm66018 		}
19077636cb21Slm66018 	}
19087636cb21Slm66018 
19097636cb21Slm66018 	/*
19107636cb21Slm66018 	 * We are drained and signal we are done.
19117636cb21Slm66018 	 */
19127636cb21Slm66018 	vqp->vq_state = VSW_QUEUE_DRAINED;
19137636cb21Slm66018 	cv_signal(&vqp->vq_cv);
19147636cb21Slm66018 
19157636cb21Slm66018 	/*
19167636cb21Slm66018 	 * Exit lock and drain the remaining packets.
19177636cb21Slm66018 	 */
19187636cb21Slm66018 	mutex_exit(&vqp->vq_lock);
19197636cb21Slm66018 
19207636cb21Slm66018 	/*
19217636cb21Slm66018 	 * Exit the thread
19227636cb21Slm66018 	 */
19237636cb21Slm66018 	thread_exit();
19247636cb21Slm66018 }
19257636cb21Slm66018 
19267636cb21Slm66018 /*
19277636cb21Slm66018  * static void
19287636cb21Slm66018  * vsw_rx_queue_cb() - Receive callback routine when
19297636cb21Slm66018  *	vsw_multi_ring_enable is non-zero.  Queue the packets
19307636cb21Slm66018  *	to a packet queue for a worker thread to process.
19317636cb21Slm66018  */
19327636cb21Slm66018 static void
19337636cb21Slm66018 vsw_rx_queue_cb(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
19347636cb21Slm66018 {
19357636cb21Slm66018 	vsw_mac_ring_t	*ringp = (vsw_mac_ring_t *)mrh;
19367636cb21Slm66018 	vsw_t		*vswp = (vsw_t *)arg;
19377636cb21Slm66018 	vsw_queue_t	*vqp;
19387636cb21Slm66018 	mblk_t		*bp, *last;
19397636cb21Slm66018 
19407636cb21Slm66018 	ASSERT(mrh != NULL);
19417636cb21Slm66018 	ASSERT(vswp != NULL);
19427636cb21Slm66018 	ASSERT(mp != NULL);
19437636cb21Slm66018 
19447636cb21Slm66018 	D1(vswp, "%s: enter", __func__);
19457636cb21Slm66018 
19467636cb21Slm66018 	/*
19477636cb21Slm66018 	 * Find the last element in the mblk chain.
19487636cb21Slm66018 	 */
19497636cb21Slm66018 	bp = mp;
19507636cb21Slm66018 	do {
19517636cb21Slm66018 		last = bp;
19527636cb21Slm66018 		bp = bp->b_next;
19537636cb21Slm66018 	} while (bp != NULL);
19547636cb21Slm66018 
19557636cb21Slm66018 	/* Get the queue for the packets */
19567636cb21Slm66018 	vqp = ringp->ring_vqp;
19577636cb21Slm66018 
19587636cb21Slm66018 	/*
19597636cb21Slm66018 	 * Grab the lock such we can queue the packets.
19607636cb21Slm66018 	 */
19617636cb21Slm66018 	mutex_enter(&vqp->vq_lock);
19627636cb21Slm66018 
19637636cb21Slm66018 	if (vqp->vq_state != VSW_QUEUE_RUNNING) {
19647636cb21Slm66018 		freemsg(mp);
196534683adeSsg70180 		mutex_exit(&vqp->vq_lock);
19667636cb21Slm66018 		goto vsw_rx_queue_cb_exit;
19677636cb21Slm66018 	}
19687636cb21Slm66018 
19697636cb21Slm66018 	/*
19707636cb21Slm66018 	 * Add the mblk chain to the queue.  If there
19717636cb21Slm66018 	 * is some mblks in the queue, then add the new
19727636cb21Slm66018 	 * chain to the end.
19737636cb21Slm66018 	 */
19747636cb21Slm66018 	if (vqp->vq_first == NULL)
19757636cb21Slm66018 		vqp->vq_first = mp;
19767636cb21Slm66018 	else
19777636cb21Slm66018 		vqp->vq_last->b_next = mp;
19787636cb21Slm66018 
19797636cb21Slm66018 	vqp->vq_last = last;
19807636cb21Slm66018 
19817636cb21Slm66018 	/*
19827636cb21Slm66018 	 * Signal the worker thread that there is work to
19837636cb21Slm66018 	 * do.
19847636cb21Slm66018 	 */
19857636cb21Slm66018 	cv_signal(&vqp->vq_cv);
19867636cb21Slm66018 
19877636cb21Slm66018 	/*
19887636cb21Slm66018 	 * Let go of the lock and exit.
19897636cb21Slm66018 	 */
19907636cb21Slm66018 	mutex_exit(&vqp->vq_lock);
199134683adeSsg70180 
199234683adeSsg70180 vsw_rx_queue_cb_exit:
19937636cb21Slm66018 	D1(vswp, "%s: exit", __func__);
19947636cb21Slm66018 }
19957636cb21Slm66018 
19961ae08745Sheppo /*
19971ae08745Sheppo  * receive callback routine. Invoked by MAC layer when there
19981ae08745Sheppo  * are pkts being passed up from physical device.
19991ae08745Sheppo  *
20001ae08745Sheppo  * PERF: It may be more efficient when the card is in promisc
20011ae08745Sheppo  * mode to check the dest address of the pkts here (against
20021ae08745Sheppo  * the FDB) rather than checking later. Needs to be investigated.
20031ae08745Sheppo  */
20041ae08745Sheppo static void
20051ae08745Sheppo vsw_rx_cb(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
20061ae08745Sheppo {
20071ae08745Sheppo 	_NOTE(ARGUNUSED(mrh))
20081ae08745Sheppo 
20091ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
20101ae08745Sheppo 
20111ae08745Sheppo 	ASSERT(vswp != NULL);
20121ae08745Sheppo 
20131ae08745Sheppo 	D1(vswp, "vsw_rx_cb: enter");
20141ae08745Sheppo 
20151ae08745Sheppo 	/* switch the chain of packets received */
201634683adeSsg70180 	vswp->vsw_switch_frame(vswp, mp, VSW_PHYSDEV, NULL, NULL);
20171ae08745Sheppo 
20181ae08745Sheppo 	D1(vswp, "vsw_rx_cb: exit");
20191ae08745Sheppo }
20201ae08745Sheppo 
20211ae08745Sheppo /*
20221ae08745Sheppo  * Send a message out over the physical device via the MAC layer.
20231ae08745Sheppo  *
20241ae08745Sheppo  * Returns any mblks that it was unable to transmit.
20251ae08745Sheppo  */
20261ae08745Sheppo static mblk_t *
20271ae08745Sheppo vsw_tx_msg(vsw_t *vswp, mblk_t *mp)
20281ae08745Sheppo {
20291ae08745Sheppo 	const mac_txinfo_t	*mtp;
20301ae08745Sheppo 	mblk_t			*nextp;
20311ae08745Sheppo 
203234683adeSsg70180 	mutex_enter(&vswp->mac_lock);
20331ae08745Sheppo 	if (vswp->mh == NULL) {
20341ae08745Sheppo 		DERR(vswp, "vsw_tx_msg: dropping pkts: no tx routine avail");
203534683adeSsg70180 		mutex_exit(&vswp->mac_lock);
20361ae08745Sheppo 		return (mp);
20371ae08745Sheppo 	} else {
20381ae08745Sheppo 		for (;;) {
20391ae08745Sheppo 			nextp = mp->b_next;
20401ae08745Sheppo 			mp->b_next = NULL;
20411ae08745Sheppo 
20421ae08745Sheppo 			mtp = vswp->txinfo;
204334683adeSsg70180 
20441ae08745Sheppo 			if ((mp = mtp->mt_fn(mtp->mt_arg, mp)) != NULL) {
20451ae08745Sheppo 				mp->b_next = nextp;
20461ae08745Sheppo 				break;
20471ae08745Sheppo 			}
20481ae08745Sheppo 
20491ae08745Sheppo 			if ((mp = nextp) == NULL)
20501ae08745Sheppo 				break;
20511ae08745Sheppo 		}
20521ae08745Sheppo 	}
205334683adeSsg70180 	mutex_exit(&vswp->mac_lock);
20541ae08745Sheppo 
20551ae08745Sheppo 	return (mp);
20561ae08745Sheppo }
20571ae08745Sheppo 
20581ae08745Sheppo /*
20591ae08745Sheppo  * Register with the MAC layer as a network device, so we
20601ae08745Sheppo  * can be plumbed if necessary.
20611ae08745Sheppo  */
20621ae08745Sheppo static int
20631ae08745Sheppo vsw_mac_register(vsw_t *vswp)
20641ae08745Sheppo {
2065ba2e4443Sseb 	mac_register_t	*macp;
2066ba2e4443Sseb 	int		rv;
20671ae08745Sheppo 
20681ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
20691ae08745Sheppo 
2070ba2e4443Sseb 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
2071ba2e4443Sseb 		return (EINVAL);
2072ba2e4443Sseb 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
20731ae08745Sheppo 	macp->m_driver = vswp;
2074ba2e4443Sseb 	macp->m_dip = vswp->dip;
2075ba2e4443Sseb 	macp->m_src_addr = (uint8_t *)&vswp->if_addr;
2076ba2e4443Sseb 	macp->m_callbacks = &vsw_m_callbacks;
2077ba2e4443Sseb 	macp->m_min_sdu = 0;
2078ba2e4443Sseb 	macp->m_max_sdu = ETHERMTU;
2079ba2e4443Sseb 	rv = mac_register(macp, &vswp->if_mh);
2080ba2e4443Sseb 	mac_free(macp);
2081ba2e4443Sseb 	if (rv == 0)
2082ba2e4443Sseb 		vswp->if_state |= VSW_IF_REG;
20831ae08745Sheppo 
20841ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
20851ae08745Sheppo 
20861ae08745Sheppo 	return (rv);
20871ae08745Sheppo }
20881ae08745Sheppo 
20891ae08745Sheppo static int
20901ae08745Sheppo vsw_mac_unregister(vsw_t *vswp)
20911ae08745Sheppo {
20921ae08745Sheppo 	int		rv = 0;
20931ae08745Sheppo 
20941ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
20951ae08745Sheppo 
20961ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
20971ae08745Sheppo 
2098ba2e4443Sseb 	if (vswp->if_state & VSW_IF_REG) {
2099ba2e4443Sseb 		rv = mac_unregister(vswp->if_mh);
21001ae08745Sheppo 		if (rv != 0) {
21011ae08745Sheppo 			DWARN(vswp, "%s: unable to unregister from MAC "
21021ae08745Sheppo 				"framework", __func__);
21031ae08745Sheppo 
21041ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
21051ae08745Sheppo 			D1(vswp, "%s: fail exit", __func__);
21061ae08745Sheppo 			return (rv);
21071ae08745Sheppo 		}
21081ae08745Sheppo 
2109ba2e4443Sseb 		/* mark i/f as down and unregistered */
2110ba2e4443Sseb 		vswp->if_state &= ~(VSW_IF_UP | VSW_IF_REG);
21111ae08745Sheppo 	}
21121ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
21131ae08745Sheppo 
21141ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
21151ae08745Sheppo 
21161ae08745Sheppo 	return (rv);
21171ae08745Sheppo }
21181ae08745Sheppo 
2119ba2e4443Sseb static int
2120ba2e4443Sseb vsw_m_stat(void *arg, uint_t stat, uint64_t *val)
21211ae08745Sheppo {
21221ae08745Sheppo 	vsw_t			*vswp = (vsw_t *)arg;
21231ae08745Sheppo 
21241ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
21251ae08745Sheppo 
212634683adeSsg70180 	mutex_enter(&vswp->mac_lock);
212734683adeSsg70180 	if (vswp->mh == NULL) {
212834683adeSsg70180 		mutex_exit(&vswp->mac_lock);
2129ba2e4443Sseb 		return (EINVAL);
213034683adeSsg70180 	}
21311ae08745Sheppo 
21321ae08745Sheppo 	/* return stats from underlying device */
2133ba2e4443Sseb 	*val = mac_stat_get(vswp->mh, stat);
213434683adeSsg70180 
213534683adeSsg70180 	mutex_exit(&vswp->mac_lock);
213634683adeSsg70180 
2137ba2e4443Sseb 	return (0);
21381ae08745Sheppo }
21391ae08745Sheppo 
21401ae08745Sheppo static void
21411ae08745Sheppo vsw_m_stop(void *arg)
21421ae08745Sheppo {
21431ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
21441ae08745Sheppo 
21451ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
21461ae08745Sheppo 
21471ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
21481ae08745Sheppo 	vswp->if_state &= ~VSW_IF_UP;
21491ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
21501ae08745Sheppo 
21511ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
21521ae08745Sheppo }
21531ae08745Sheppo 
21541ae08745Sheppo static int
21551ae08745Sheppo vsw_m_start(void *arg)
21561ae08745Sheppo {
21571ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
21581ae08745Sheppo 
21591ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
21601ae08745Sheppo 
21611ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
21621ae08745Sheppo 	vswp->if_state |= VSW_IF_UP;
21631ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
21641ae08745Sheppo 
21651ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
21661ae08745Sheppo 	return (0);
21671ae08745Sheppo }
21681ae08745Sheppo 
21691ae08745Sheppo /*
21701ae08745Sheppo  * Change the local interface address.
21711ae08745Sheppo  */
21721ae08745Sheppo static int
21731ae08745Sheppo vsw_m_unicst(void *arg, const uint8_t *macaddr)
21741ae08745Sheppo {
21751ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
21761ae08745Sheppo 
21771ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
21781ae08745Sheppo 
21791ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
21801ae08745Sheppo 	ether_copy(macaddr, &vswp->if_addr);
21811ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
21821ae08745Sheppo 
21831ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
21841ae08745Sheppo 
21851ae08745Sheppo 	return (0);
21861ae08745Sheppo }
21871ae08745Sheppo 
21881ae08745Sheppo static int
21891ae08745Sheppo vsw_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
21901ae08745Sheppo {
21911ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
21921ae08745Sheppo 	mcst_addr_t	*mcst_p = NULL;
21931ae08745Sheppo 	uint64_t	addr = 0x0;
2194e1ebb9ecSlm66018 	int		i, ret = 0;
21951ae08745Sheppo 
21961ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
21971ae08745Sheppo 
21981ae08745Sheppo 	/*
21991ae08745Sheppo 	 * Convert address into form that can be used
22001ae08745Sheppo 	 * as hash table key.
22011ae08745Sheppo 	 */
22021ae08745Sheppo 	for (i = 0; i < ETHERADDRL; i++) {
22031ae08745Sheppo 		addr = (addr << 8) | mca[i];
22041ae08745Sheppo 	}
22051ae08745Sheppo 
22061ae08745Sheppo 	D2(vswp, "%s: addr = 0x%llx", __func__, addr);
22071ae08745Sheppo 
22081ae08745Sheppo 	if (add) {
22091ae08745Sheppo 		D2(vswp, "%s: adding multicast", __func__);
22101ae08745Sheppo 		if (vsw_add_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
22111ae08745Sheppo 			/*
22121ae08745Sheppo 			 * Update the list of multicast addresses
22131ae08745Sheppo 			 * contained within the vsw_t structure to
22141ae08745Sheppo 			 * include this new one.
22151ae08745Sheppo 			 */
22161ae08745Sheppo 			mcst_p = kmem_zalloc(sizeof (mcst_addr_t), KM_NOSLEEP);
22171ae08745Sheppo 			if (mcst_p == NULL) {
22181ae08745Sheppo 				DERR(vswp, "%s unable to alloc mem", __func__);
22191ae08745Sheppo 				return (1);
22201ae08745Sheppo 			}
22211ae08745Sheppo 			mcst_p->addr = addr;
22221ae08745Sheppo 
22231ae08745Sheppo 			mutex_enter(&vswp->mca_lock);
22241ae08745Sheppo 			mcst_p->nextp = vswp->mcap;
22251ae08745Sheppo 			vswp->mcap = mcst_p;
22261ae08745Sheppo 			mutex_exit(&vswp->mca_lock);
22271ae08745Sheppo 
22281ae08745Sheppo 			/*
22291ae08745Sheppo 			 * Call into the underlying driver to program the
22301ae08745Sheppo 			 * address into HW.
22311ae08745Sheppo 			 */
223234683adeSsg70180 			mutex_enter(&vswp->mac_lock);
2233e1ebb9ecSlm66018 			if (vswp->mh != NULL) {
2234e1ebb9ecSlm66018 				ret = mac_multicst_add(vswp->mh, mca);
2235e1ebb9ecSlm66018 				if (ret != 0) {
223634683adeSsg70180 					cmn_err(CE_WARN, "!vsw%d: unable to "
223734683adeSsg70180 						"add multicast address",
223834683adeSsg70180 						vswp->instance);
223934683adeSsg70180 					mutex_exit(&vswp->mac_lock);
2240e1ebb9ecSlm66018 					goto vsw_remove_addr;
2241e1ebb9ecSlm66018 				}
22421ae08745Sheppo 			}
224334683adeSsg70180 			mutex_exit(&vswp->mac_lock);
22441ae08745Sheppo 		} else {
224534683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: unable to add multicast "
224634683adeSsg70180 				"address", vswp->instance);
2247e1ebb9ecSlm66018 		}
2248e1ebb9ecSlm66018 		return (ret);
2249e1ebb9ecSlm66018 	}
2250e1ebb9ecSlm66018 
2251e1ebb9ecSlm66018 vsw_remove_addr:
2252e1ebb9ecSlm66018 
22531ae08745Sheppo 	D2(vswp, "%s: removing multicast", __func__);
22541ae08745Sheppo 	/*
22551ae08745Sheppo 	 * Remove the address from the hash table..
22561ae08745Sheppo 	 */
22571ae08745Sheppo 	if (vsw_del_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
22581ae08745Sheppo 
22591ae08745Sheppo 		/*
22601ae08745Sheppo 		 * ..and then from the list maintained in the
22611ae08745Sheppo 		 * vsw_t structure.
22621ae08745Sheppo 		 */
22631ae08745Sheppo 		vsw_del_addr(VSW_LOCALDEV, vswp, addr);
22641ae08745Sheppo 
226534683adeSsg70180 		mutex_enter(&vswp->mac_lock);
22661ae08745Sheppo 		if (vswp->mh != NULL)
22671ae08745Sheppo 			(void) mac_multicst_remove(vswp->mh, mca);
226834683adeSsg70180 		mutex_exit(&vswp->mac_lock);
22691ae08745Sheppo 	}
22701ae08745Sheppo 
22711ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
22721ae08745Sheppo 
22731ae08745Sheppo 	return (0);
22741ae08745Sheppo }
22751ae08745Sheppo 
22761ae08745Sheppo static int
22771ae08745Sheppo vsw_m_promisc(void *arg, boolean_t on)
22781ae08745Sheppo {
22791ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
22801ae08745Sheppo 
22811ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
22821ae08745Sheppo 
22831ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
22841ae08745Sheppo 	if (on)
22851ae08745Sheppo 		vswp->if_state |= VSW_IF_PROMISC;
22861ae08745Sheppo 	else
22871ae08745Sheppo 		vswp->if_state &= ~VSW_IF_PROMISC;
22881ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
22891ae08745Sheppo 
22901ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
22911ae08745Sheppo 
22921ae08745Sheppo 	return (0);
22931ae08745Sheppo }
22941ae08745Sheppo 
22951ae08745Sheppo static mblk_t *
22961ae08745Sheppo vsw_m_tx(void *arg, mblk_t *mp)
22971ae08745Sheppo {
22981ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
22991ae08745Sheppo 
23001ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
23011ae08745Sheppo 
230234683adeSsg70180 	vswp->vsw_switch_frame(vswp, mp, VSW_LOCALDEV, NULL, NULL);
23031ae08745Sheppo 
23041ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
23051ae08745Sheppo 
23061ae08745Sheppo 	return (NULL);
23071ae08745Sheppo }
23081ae08745Sheppo 
23091ae08745Sheppo /*
23101ae08745Sheppo  * Register for machine description (MD) updates.
231134683adeSsg70180  *
231234683adeSsg70180  * Returns 0 on success, 1 on failure.
23131ae08745Sheppo  */
231434683adeSsg70180 static int
23151ae08745Sheppo vsw_mdeg_register(vsw_t *vswp)
23161ae08745Sheppo {
23171ae08745Sheppo 	mdeg_prop_spec_t	*pspecp;
23181ae08745Sheppo 	mdeg_node_spec_t	*inst_specp;
231934683adeSsg70180 	mdeg_handle_t		mdeg_hdl, mdeg_port_hdl;
23201ae08745Sheppo 	size_t			templatesz;
23211ae08745Sheppo 	int			inst, rv;
23221ae08745Sheppo 
23231ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
23241ae08745Sheppo 
232534683adeSsg70180 	/*
232634683adeSsg70180 	 * In each 'virtual-device' node in the MD there is a
232734683adeSsg70180 	 * 'cfg-handle' property which is the MD's concept of
232834683adeSsg70180 	 * an instance number (this may be completely different from
232934683adeSsg70180 	 * the device drivers instance #). OBP reads that value and
233034683adeSsg70180 	 * stores it in the 'reg' property of the appropriate node in
233134683adeSsg70180 	 * the device tree. So we use the 'reg' value when registering
233234683adeSsg70180 	 * with the mdeg framework, to ensure we get events for the
233334683adeSsg70180 	 * correct nodes.
233434683adeSsg70180 	 */
23351ae08745Sheppo 	inst = ddi_prop_get_int(DDI_DEV_T_ANY, vswp->dip,
23361ae08745Sheppo 		DDI_PROP_DONTPASS, reg_propname, -1);
23371ae08745Sheppo 	if (inst == -1) {
233834683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read %s property from "
233934683adeSsg70180 			"OBP device tree", vswp->instance, reg_propname);
234034683adeSsg70180 		return (1);
23411ae08745Sheppo 	}
23421ae08745Sheppo 
23431ae08745Sheppo 	D2(vswp, "%s: instance %d registering with mdeg", __func__, inst);
23441ae08745Sheppo 
23451ae08745Sheppo 	/*
23461ae08745Sheppo 	 * Allocate and initialize a per-instance copy
23471ae08745Sheppo 	 * of the global property spec array that will
23481ae08745Sheppo 	 * uniquely identify this vsw instance.
23491ae08745Sheppo 	 */
23501ae08745Sheppo 	templatesz = sizeof (vsw_prop_template);
23511ae08745Sheppo 	pspecp = kmem_zalloc(templatesz, KM_SLEEP);
23521ae08745Sheppo 
23531ae08745Sheppo 	bcopy(vsw_prop_template, pspecp, templatesz);
23541ae08745Sheppo 
23551ae08745Sheppo 	VSW_SET_MDEG_PROP_INST(pspecp, inst);
23561ae08745Sheppo 
23571ae08745Sheppo 	/* initialize the complete prop spec structure */
23581ae08745Sheppo 	inst_specp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
23591ae08745Sheppo 	inst_specp->namep = "virtual-device";
23601ae08745Sheppo 	inst_specp->specp = pspecp;
23611ae08745Sheppo 
236234683adeSsg70180 	/*
236334683adeSsg70180 	 * Register an interest in 'virtual-device' nodes with a
236434683adeSsg70180 	 * 'name' property of 'virtual-network-switch'
236534683adeSsg70180 	 */
236634683adeSsg70180 	rv = mdeg_register(inst_specp, &vdev_match, vsw_mdeg_cb,
23671ae08745Sheppo 	    (void *)vswp, &mdeg_hdl);
236834683adeSsg70180 	if (rv != MDEG_SUCCESS) {
236934683adeSsg70180 		DERR(vswp, "%s: mdeg_register failed (%d) for vsw node",
237034683adeSsg70180 			__func__, rv);
237134683adeSsg70180 		goto mdeg_reg_fail;
237234683adeSsg70180 	}
23731ae08745Sheppo 
237434683adeSsg70180 	/*
237534683adeSsg70180 	 * Register an interest in 'vsw-port' nodes.
237634683adeSsg70180 	 */
237734683adeSsg70180 	rv = mdeg_register(inst_specp, &vport_match, vsw_port_mdeg_cb,
237834683adeSsg70180 	    (void *)vswp, &mdeg_port_hdl);
23791ae08745Sheppo 	if (rv != MDEG_SUCCESS) {
23801ae08745Sheppo 		DERR(vswp, "%s: mdeg_register failed (%d)\n", __func__, rv);
238134683adeSsg70180 		(void) mdeg_unregister(mdeg_hdl);
238234683adeSsg70180 		goto mdeg_reg_fail;
23831ae08745Sheppo 	}
23841ae08745Sheppo 
23851ae08745Sheppo 	/* save off data that will be needed later */
23861ae08745Sheppo 	vswp->inst_spec = inst_specp;
23871ae08745Sheppo 	vswp->mdeg_hdl = mdeg_hdl;
238834683adeSsg70180 	vswp->mdeg_port_hdl = mdeg_port_hdl;
23891ae08745Sheppo 
23901ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
239134683adeSsg70180 	return (0);
239234683adeSsg70180 
239334683adeSsg70180 mdeg_reg_fail:
239434683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: Unable to register MDEG callbacks",
239534683adeSsg70180 				vswp->instance);
239634683adeSsg70180 	kmem_free(pspecp, templatesz);
239734683adeSsg70180 	kmem_free(inst_specp, sizeof (mdeg_node_spec_t));
239834683adeSsg70180 
239934683adeSsg70180 	vswp->mdeg_hdl = NULL;
240034683adeSsg70180 	vswp->mdeg_port_hdl = NULL;
240134683adeSsg70180 
240234683adeSsg70180 	return (1);
24031ae08745Sheppo }
24041ae08745Sheppo 
24051ae08745Sheppo static void
24061ae08745Sheppo vsw_mdeg_unregister(vsw_t *vswp)
24071ae08745Sheppo {
24081ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: enter");
24091ae08745Sheppo 
241034683adeSsg70180 	if (vswp->mdeg_hdl != NULL)
24111ae08745Sheppo 		(void) mdeg_unregister(vswp->mdeg_hdl);
24121ae08745Sheppo 
241334683adeSsg70180 	if (vswp->mdeg_port_hdl != NULL)
241434683adeSsg70180 		(void) mdeg_unregister(vswp->mdeg_port_hdl);
241534683adeSsg70180 
241634683adeSsg70180 	if (vswp->inst_spec != NULL) {
24171ae08745Sheppo 		if (vswp->inst_spec->specp != NULL) {
24181ae08745Sheppo 			(void) kmem_free(vswp->inst_spec->specp,
24191ae08745Sheppo 				sizeof (vsw_prop_template));
24201ae08745Sheppo 			vswp->inst_spec->specp = NULL;
24211ae08745Sheppo 		}
24221ae08745Sheppo 
24231ae08745Sheppo 		(void) kmem_free(vswp->inst_spec,
24241ae08745Sheppo 			sizeof (mdeg_node_spec_t));
24251ae08745Sheppo 		vswp->inst_spec = NULL;
24261ae08745Sheppo 	}
24271ae08745Sheppo 
24281ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: exit");
24291ae08745Sheppo }
24301ae08745Sheppo 
243134683adeSsg70180 /*
243234683adeSsg70180  * Mdeg callback invoked for the vsw node itself.
243334683adeSsg70180  */
24341ae08745Sheppo static int
24351ae08745Sheppo vsw_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
24361ae08745Sheppo {
24371ae08745Sheppo 	vsw_t		*vswp;
24381ae08745Sheppo 	int		idx;
24391ae08745Sheppo 	md_t		*mdp;
24401ae08745Sheppo 	mde_cookie_t	node;
24411ae08745Sheppo 	uint64_t	inst;
244234683adeSsg70180 	char		*node_name = NULL;
24431ae08745Sheppo 
24441ae08745Sheppo 	if (resp == NULL)
24451ae08745Sheppo 		return (MDEG_FAILURE);
24461ae08745Sheppo 
24471ae08745Sheppo 	vswp = (vsw_t *)cb_argp;
24481ae08745Sheppo 
244934683adeSsg70180 	D1(vswp, "%s: added %d : removed %d : curr matched %d"
245034683adeSsg70180 		" : prev matched %d", __func__, resp->added.nelem,
245134683adeSsg70180 		resp->removed.nelem, resp->match_curr.nelem,
245234683adeSsg70180 		resp->match_prev.nelem);
245334683adeSsg70180 
245434683adeSsg70180 	/*
245534683adeSsg70180 	 * Expect 'added' to be non-zero if virtual-network-switch
245634683adeSsg70180 	 * nodes exist in the MD when the driver attaches.
245734683adeSsg70180 	 */
245834683adeSsg70180 	for (idx = 0; idx < resp->added.nelem; idx++) {
245934683adeSsg70180 		mdp = resp->added.mdp;
246034683adeSsg70180 		node = resp->added.mdep[idx];
246134683adeSsg70180 
246234683adeSsg70180 		if (md_get_prop_str(mdp, node, "name", &node_name) != 0) {
246334683adeSsg70180 			DERR(vswp, "%s: unable to get node name for "
246434683adeSsg70180 				"node(%d) 0x%lx", __func__, idx, node);
246534683adeSsg70180 			continue;
246634683adeSsg70180 		}
246734683adeSsg70180 
246834683adeSsg70180 		if (md_get_prop_val(mdp, node, "cfg-handle", &inst)) {
246934683adeSsg70180 			DERR(vswp, "%s: prop(cfg-handle) not found port(%d)",
247034683adeSsg70180 				__func__, idx);
247134683adeSsg70180 			continue;
247234683adeSsg70180 		}
247334683adeSsg70180 
247434683adeSsg70180 		D2(vswp, "%s: added node(%d) 0x%lx with name %s "
247534683adeSsg70180 			"and inst %d", __func__, idx, node, node_name, inst);
247634683adeSsg70180 
247734683adeSsg70180 		vsw_get_initial_md_properties(vswp, mdp, node);
247834683adeSsg70180 	}
247934683adeSsg70180 
248034683adeSsg70180 	/*
248134683adeSsg70180 	 * A non-zero 'match' value indicates that the MD has been
248234683adeSsg70180 	 * updated and that a virtual-network-switch node is present
248334683adeSsg70180 	 * which may or may not have been updated. It is up to the clients
248434683adeSsg70180 	 * to examine their own nodes and determine if they have changed.
248534683adeSsg70180 	 */
248634683adeSsg70180 	for (idx = 0; idx < resp->match_curr.nelem; idx++) {
248734683adeSsg70180 		mdp = resp->match_curr.mdp;
248834683adeSsg70180 		node = resp->match_curr.mdep[idx];
248934683adeSsg70180 
249034683adeSsg70180 		if (md_get_prop_str(mdp, node, "name", &node_name) != 0) {
249134683adeSsg70180 			DERR(vswp, "%s: unable to get node name for "
249234683adeSsg70180 				"node(%d) 0x%lx", __func__, idx, node);
249334683adeSsg70180 			continue;
249434683adeSsg70180 		}
249534683adeSsg70180 
249634683adeSsg70180 		if (md_get_prop_val(mdp, node, "cfg-handle", &inst)) {
249734683adeSsg70180 			DERR(vswp, "%s: prop(cfg-handle) not found port(%d)",
249834683adeSsg70180 				__func__, idx);
249934683adeSsg70180 			continue;
250034683adeSsg70180 		}
250134683adeSsg70180 
250234683adeSsg70180 		D2(vswp, "%s: changed node(%d) 0x%lx with name %s "
250334683adeSsg70180 			"and inst %d", __func__, idx, node, node_name, inst);
250434683adeSsg70180 
250534683adeSsg70180 		vsw_update_md_prop(vswp, mdp, node);
250634683adeSsg70180 	}
250734683adeSsg70180 
250834683adeSsg70180 	return (MDEG_SUCCESS);
250934683adeSsg70180 }
251034683adeSsg70180 
251134683adeSsg70180 /*
251234683adeSsg70180  * Mdeg callback invoked for changes to the vsw-port nodes
251334683adeSsg70180  * under the vsw node.
251434683adeSsg70180  */
251534683adeSsg70180 static int
251634683adeSsg70180 vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
251734683adeSsg70180 {
251834683adeSsg70180 	vsw_t		*vswp;
251934683adeSsg70180 	int		idx;
252034683adeSsg70180 	md_t		*mdp;
252134683adeSsg70180 	mde_cookie_t	node;
252234683adeSsg70180 	uint64_t	inst;
252334683adeSsg70180 
252434683adeSsg70180 	if ((resp == NULL) || (cb_argp == NULL))
252534683adeSsg70180 		return (MDEG_FAILURE);
252634683adeSsg70180 
252734683adeSsg70180 	vswp = (vsw_t *)cb_argp;
252834683adeSsg70180 
252934683adeSsg70180 	D2(vswp, "%s: added %d : removed %d : curr matched %d"
253034683adeSsg70180 		" : prev matched %d", __func__, resp->added.nelem,
253134683adeSsg70180 		resp->removed.nelem, resp->match_curr.nelem,
25321ae08745Sheppo 		resp->match_prev.nelem);
25331ae08745Sheppo 
25341ae08745Sheppo 	/* process added ports */
25351ae08745Sheppo 	for (idx = 0; idx < resp->added.nelem; idx++) {
25361ae08745Sheppo 		mdp = resp->added.mdp;
25371ae08745Sheppo 		node = resp->added.mdep[idx];
25381ae08745Sheppo 
25391ae08745Sheppo 		D2(vswp, "%s: adding node(%d) 0x%lx", __func__, idx, node);
25401ae08745Sheppo 
25411ae08745Sheppo 		if (vsw_port_add(vswp, mdp, &node) != 0) {
254234683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to add new port "
254334683adeSsg70180 				"(0x%lx)", vswp->instance, node);
25441ae08745Sheppo 		}
25451ae08745Sheppo 	}
25461ae08745Sheppo 
25471ae08745Sheppo 	/* process removed ports */
25481ae08745Sheppo 	for (idx = 0; idx < resp->removed.nelem; idx++) {
25491ae08745Sheppo 		mdp = resp->removed.mdp;
25501ae08745Sheppo 		node = resp->removed.mdep[idx];
25511ae08745Sheppo 
25521ae08745Sheppo 		if (md_get_prop_val(mdp, node, id_propname, &inst)) {
255334683adeSsg70180 			DERR(vswp, "%s: prop(%s) not found in port(%d)",
25541ae08745Sheppo 				__func__, id_propname, idx);
25551ae08745Sheppo 			continue;
25561ae08745Sheppo 		}
25571ae08745Sheppo 
25581ae08745Sheppo 		D2(vswp, "%s: removing node(%d) 0x%lx", __func__, idx, node);
25591ae08745Sheppo 
25601ae08745Sheppo 		if (vsw_port_detach(vswp, inst) != 0) {
256134683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to remove port %ld",
256234683adeSsg70180 				vswp->instance, inst);
25631ae08745Sheppo 		}
25641ae08745Sheppo 	}
25651ae08745Sheppo 
25661ae08745Sheppo 	/*
25671ae08745Sheppo 	 * Currently no support for updating already active ports.
25681ae08745Sheppo 	 * So, ignore the match_curr and match_priv arrays for now.
25691ae08745Sheppo 	 */
25701ae08745Sheppo 
25711ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
25721ae08745Sheppo 
25731ae08745Sheppo 	return (MDEG_SUCCESS);
25741ae08745Sheppo }
25751ae08745Sheppo 
25761ae08745Sheppo /*
257734683adeSsg70180  * Read the initial start-of-day values from the specified MD node.
257834683adeSsg70180  */
257934683adeSsg70180 static void
258034683adeSsg70180 vsw_get_initial_md_properties(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
258134683adeSsg70180 {
258234683adeSsg70180 	int		i;
258334683adeSsg70180 	uint64_t 	macaddr = 0;
258434683adeSsg70180 
258534683adeSsg70180 	D1(vswp, "%s: enter", __func__);
258634683adeSsg70180 
258734683adeSsg70180 	if (vsw_get_md_physname(vswp, mdp, node, vswp->physname) == 0) {
258834683adeSsg70180 		/*
258934683adeSsg70180 		 * Note it is valid for the physname property to
259034683adeSsg70180 		 * be NULL so check actual name length to determine
259134683adeSsg70180 		 * if we have a actual device name.
259234683adeSsg70180 		 */
259334683adeSsg70180 		if (strlen(vswp->physname) > 0)
259434683adeSsg70180 			vswp->mdprops |= VSW_MD_PHYSNAME;
259534683adeSsg70180 	} else {
259634683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read name of physical "
259734683adeSsg70180 			"device from MD", vswp->instance);
259834683adeSsg70180 		return;
259934683adeSsg70180 	}
260034683adeSsg70180 
260134683adeSsg70180 	/* mac address for vswitch device itself */
260234683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
260334683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
260434683adeSsg70180 			vswp->instance);
260534683adeSsg70180 
260634683adeSsg70180 		/*
260734683adeSsg70180 		 * Fallback to using the mac address of the physical
260834683adeSsg70180 		 * device.
260934683adeSsg70180 		 */
261034683adeSsg70180 		if (vsw_get_physaddr(vswp) == 0) {
261134683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: Using MAC address from "
261234683adeSsg70180 				"physical device (%s)", vswp->instance,
261334683adeSsg70180 				vswp->physname);
261434683adeSsg70180 		} else {
261534683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address"
261634683adeSsg70180 				"from device %s", vswp->instance,
261734683adeSsg70180 				vswp->physname);
261834683adeSsg70180 		}
261934683adeSsg70180 	} else {
262034683adeSsg70180 		WRITE_ENTER(&vswp->if_lockrw);
262134683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
262234683adeSsg70180 			vswp->if_addr.ether_addr_octet[i] = macaddr & 0xFF;
262334683adeSsg70180 			macaddr >>= 8;
262434683adeSsg70180 		}
262534683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
262634683adeSsg70180 		vswp->mdprops |= VSW_MD_MACADDR;
262734683adeSsg70180 	}
262834683adeSsg70180 
262934683adeSsg70180 	if (vsw_get_md_smodes(vswp, mdp, node,
263034683adeSsg70180 				vswp->smode, &vswp->smode_num)) {
263134683adeSsg70180 		cmn_err(CE_WARN, "vsw%d: Unable to read %s property from "
263234683adeSsg70180 			"MD, defaulting to programmed mode", vswp->instance,
263334683adeSsg70180 			smode_propname);
263434683adeSsg70180 
263534683adeSsg70180 		for (i = 0; i < NUM_SMODES; i++)
263634683adeSsg70180 			vswp->smode[i] = VSW_LAYER2;
263734683adeSsg70180 
263834683adeSsg70180 		vswp->smode_num = NUM_SMODES;
263934683adeSsg70180 	} else {
264034683adeSsg70180 		ASSERT(vswp->smode_num != 0);
264134683adeSsg70180 		vswp->mdprops |= VSW_MD_SMODE;
264234683adeSsg70180 	}
264334683adeSsg70180 
264434683adeSsg70180 	/*
264534683adeSsg70180 	 * Unable to setup any switching mode, nothing more
264634683adeSsg70180 	 * we can do.
264734683adeSsg70180 	 */
264834683adeSsg70180 	if (vsw_setup_switching(vswp))
264934683adeSsg70180 		return;
265034683adeSsg70180 
265134683adeSsg70180 	WRITE_ENTER(&vswp->if_lockrw);
265234683adeSsg70180 	vswp->if_state &= ~VSW_IF_UP;
265334683adeSsg70180 	RW_EXIT(&vswp->if_lockrw);
265434683adeSsg70180 	if (vswp->mdprops & (VSW_MD_MACADDR | VSW_DEV_MACADDR)) {
265534683adeSsg70180 		if (vsw_mac_register(vswp) != 0) {
265634683adeSsg70180 			/*
265734683adeSsg70180 			 * Treat this as a non-fatal error as we may be
265834683adeSsg70180 			 * able to operate in some other mode.
265934683adeSsg70180 			 */
266034683adeSsg70180 			cmn_err(CE_WARN, "vsw%d: Unable to register as "
266134683adeSsg70180 				"provider with MAC layer", vswp->instance);
266234683adeSsg70180 		}
266334683adeSsg70180 	}
266434683adeSsg70180 
266534683adeSsg70180 	D1(vswp, "%s: exit", __func__);
266634683adeSsg70180 }
266734683adeSsg70180 
266834683adeSsg70180 /*
266934683adeSsg70180  * Check to see if the relevant properties in the specified node have
267034683adeSsg70180  * changed, and if so take the appropriate action.
267134683adeSsg70180  *
267234683adeSsg70180  * If any of the properties are missing or invalid we don't take
267334683adeSsg70180  * any action, as this function should only be invoked when modifications
267434683adeSsg70180  * have been made to what we assume is a working configuration, which
267534683adeSsg70180  * we leave active.
267634683adeSsg70180  *
267734683adeSsg70180  * Note it is legal for this routine to be invoked even if none of the
267834683adeSsg70180  * properties in the port node within the MD have actually changed.
267934683adeSsg70180  */
268034683adeSsg70180 static void
268134683adeSsg70180 vsw_update_md_prop(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
268234683adeSsg70180 {
268334683adeSsg70180 	char		physname[LIFNAMSIZ];
268434683adeSsg70180 	char		drv[LIFNAMSIZ];
268534683adeSsg70180 	uint_t		ddi_instance;
268634683adeSsg70180 	uint8_t		new_smode[NUM_SMODES];
268734683adeSsg70180 	int		i, smode_num = 0;
268834683adeSsg70180 	uint64_t 	macaddr = 0;
268934683adeSsg70180 	vsw_port_list_t *plist = &vswp->plist;
269034683adeSsg70180 	vsw_port_t	*port = NULL;
269134683adeSsg70180 	enum		{MD_init = 0x1,
269234683adeSsg70180 				MD_physname = 0x2,
269334683adeSsg70180 				MD_macaddr = 0x4,
269434683adeSsg70180 				MD_smode = 0x8} updated;
269534683adeSsg70180 
269634683adeSsg70180 	updated = MD_init;
269734683adeSsg70180 
269834683adeSsg70180 	D1(vswp, "%s: enter", __func__);
269934683adeSsg70180 
270034683adeSsg70180 	/*
270134683adeSsg70180 	 * Check if name of physical device in MD has changed.
270234683adeSsg70180 	 */
270334683adeSsg70180 	if (vsw_get_md_physname(vswp, mdp, node, (char *)&physname) == 0) {
270434683adeSsg70180 		/*
270534683adeSsg70180 		 * Do basic sanity check on new device name/instance,
270634683adeSsg70180 		 * if its non NULL. It is valid for the device name to
270734683adeSsg70180 		 * have changed from a non NULL to a NULL value, i.e.
270834683adeSsg70180 		 * the vsw is being changed to 'routed' mode.
270934683adeSsg70180 		 */
271034683adeSsg70180 		if ((strlen(physname) != 0) &&
271134683adeSsg70180 			(ddi_parse(physname, drv,
271234683adeSsg70180 				&ddi_instance) != DDI_SUCCESS)) {
271334683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: new device name %s is not"
271434683adeSsg70180 				" a valid device name/instance",
271534683adeSsg70180 				vswp->instance, physname);
271634683adeSsg70180 			goto fail_reconf;
271734683adeSsg70180 		}
271834683adeSsg70180 
271934683adeSsg70180 		if (strcmp(physname, vswp->physname)) {
272034683adeSsg70180 			D2(vswp, "%s: device name changed from %s to %s",
272134683adeSsg70180 					__func__, vswp->physname, physname);
272234683adeSsg70180 
272334683adeSsg70180 			updated |= MD_physname;
272434683adeSsg70180 		} else {
272534683adeSsg70180 			D2(vswp, "%s: device name unchanged at %s",
272634683adeSsg70180 					__func__, vswp->physname);
272734683adeSsg70180 		}
272834683adeSsg70180 	} else {
272934683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read name of physical "
273034683adeSsg70180 			"device from updated MD.", vswp->instance);
273134683adeSsg70180 		goto fail_reconf;
273234683adeSsg70180 	}
273334683adeSsg70180 
273434683adeSsg70180 	/*
273534683adeSsg70180 	 * Check if MAC address has changed.
273634683adeSsg70180 	 */
273734683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
273834683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
273934683adeSsg70180 			vswp->instance);
274034683adeSsg70180 		goto fail_reconf;
274134683adeSsg70180 	} else {
274234683adeSsg70180 		READ_ENTER(&vswp->if_lockrw);
274334683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
274434683adeSsg70180 			if (vswp->if_addr.ether_addr_octet[i]
274534683adeSsg70180 							!= (macaddr & 0xFF)) {
274634683adeSsg70180 				D2(vswp, "%s: octet[%d] 0x%x != 0x%x",
274734683adeSsg70180 					__func__, i,
274834683adeSsg70180 					vswp->if_addr.ether_addr_octet[i],
274934683adeSsg70180 					(macaddr & 0xFF));
275034683adeSsg70180 				updated |= MD_macaddr;
275134683adeSsg70180 				break;
275234683adeSsg70180 			}
275334683adeSsg70180 			macaddr >>= 8;
275434683adeSsg70180 		}
275534683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
275634683adeSsg70180 	}
275734683adeSsg70180 
275834683adeSsg70180 	/*
275934683adeSsg70180 	 * Check if switching modes have changed.
276034683adeSsg70180 	 */
276134683adeSsg70180 	if (vsw_get_md_smodes(vswp, mdp, node,
276234683adeSsg70180 				new_smode, &smode_num)) {
276334683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read %s property from MD",
276434683adeSsg70180 					vswp->instance, smode_propname);
276534683adeSsg70180 		goto fail_reconf;
276634683adeSsg70180 	} else {
276734683adeSsg70180 		ASSERT(smode_num != 0);
276834683adeSsg70180 		if (smode_num != vswp->smode_num) {
276934683adeSsg70180 			D2(vswp, "%s: number of modes changed from %d to %d",
277034683adeSsg70180 				__func__, vswp->smode_num, smode_num);
277134683adeSsg70180 		}
277234683adeSsg70180 
277334683adeSsg70180 		for (i = 0; i < smode_num; i++) {
277434683adeSsg70180 			if (new_smode[i] != vswp->smode[i]) {
277534683adeSsg70180 				D2(vswp, "%s: mode changed from %d to %d",
277634683adeSsg70180 					__func__, vswp->smode[i], new_smode[i]);
277734683adeSsg70180 				updated |= MD_smode;
277834683adeSsg70180 				break;
277934683adeSsg70180 			}
278034683adeSsg70180 		}
278134683adeSsg70180 	}
278234683adeSsg70180 
278334683adeSsg70180 	/*
278434683adeSsg70180 	 * Now make any changes which are needed...
278534683adeSsg70180 	 */
278634683adeSsg70180 
278734683adeSsg70180 	if (updated & (MD_physname | MD_smode)) {
278834683adeSsg70180 		/*
278934683adeSsg70180 		 * Disconnect all ports from the current card
279034683adeSsg70180 		 */
279134683adeSsg70180 		WRITE_ENTER(&plist->lockrw);
279234683adeSsg70180 		for (port = plist->head; port != NULL; port = port->p_next) {
279334683adeSsg70180 			/* Remove address if was programmed into HW. */
279434683adeSsg70180 			if (vsw_unset_hw(vswp, port)) {
279534683adeSsg70180 				RW_EXIT(&plist->lockrw);
279634683adeSsg70180 				goto fail_update;
279734683adeSsg70180 			}
279834683adeSsg70180 		}
279934683adeSsg70180 		RW_EXIT(&plist->lockrw);
280034683adeSsg70180 
280134683adeSsg70180 		/*
280234683adeSsg70180 		 * Stop, detach the old device..
280334683adeSsg70180 		 */
280434683adeSsg70180 		vsw_mac_detach(vswp);
280534683adeSsg70180 
280634683adeSsg70180 		/*
280734683adeSsg70180 		 * Update phys name.
280834683adeSsg70180 		 */
280934683adeSsg70180 		if (updated & MD_physname) {
281034683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: changing from %s to %s",
281134683adeSsg70180 				vswp->instance, vswp->physname, physname);
281234683adeSsg70180 			(void) strncpy(vswp->physname,
281334683adeSsg70180 					physname, strlen(physname) + 1);
281434683adeSsg70180 
281534683adeSsg70180 			if (strlen(vswp->physname) > 0)
281634683adeSsg70180 				vswp->mdprops |= VSW_MD_PHYSNAME;
281734683adeSsg70180 		}
281834683adeSsg70180 
281934683adeSsg70180 		/*
282034683adeSsg70180 		 * Update array with the new switch mode values.
282134683adeSsg70180 		 */
282234683adeSsg70180 		if (updated & MD_smode) {
282334683adeSsg70180 			for (i = 0; i < smode_num; i++)
282434683adeSsg70180 				vswp->smode[i] = new_smode[i];
282534683adeSsg70180 
282634683adeSsg70180 			vswp->smode_num = smode_num;
282734683adeSsg70180 			vswp->smode_idx = 0;
282834683adeSsg70180 		}
282934683adeSsg70180 
283034683adeSsg70180 		/*
283134683adeSsg70180 		 * ..and attach, start the new device.
283234683adeSsg70180 		 */
283334683adeSsg70180 		if (vsw_setup_switching(vswp))
283434683adeSsg70180 			goto fail_update;
283534683adeSsg70180 
283634683adeSsg70180 		/*
283734683adeSsg70180 		 * Connect ports to new card.
283834683adeSsg70180 		 */
283934683adeSsg70180 		WRITE_ENTER(&plist->lockrw);
284034683adeSsg70180 		for (port = plist->head; port != NULL; port = port->p_next) {
284134683adeSsg70180 			if (vsw_set_hw(vswp, port)) {
284234683adeSsg70180 				RW_EXIT(&plist->lockrw);
284334683adeSsg70180 				goto fail_update;
284434683adeSsg70180 			}
284534683adeSsg70180 		}
284634683adeSsg70180 		RW_EXIT(&plist->lockrw);
284734683adeSsg70180 	}
284834683adeSsg70180 
284934683adeSsg70180 	if (updated & MD_macaddr) {
285034683adeSsg70180 		cmn_err(CE_NOTE, "!vsw%d: changing mac address to 0x%lx",
285134683adeSsg70180 				vswp->instance, macaddr);
285234683adeSsg70180 
285334683adeSsg70180 		WRITE_ENTER(&vswp->if_lockrw);
285434683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
285534683adeSsg70180 			vswp->if_addr.ether_addr_octet[i] = macaddr & 0xFF;
285634683adeSsg70180 			macaddr >>= 8;
285734683adeSsg70180 		}
285834683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
285934683adeSsg70180 
286034683adeSsg70180 		/*
286134683adeSsg70180 		 * Notify the MAC layer of the changed address.
286234683adeSsg70180 		 */
286334683adeSsg70180 		mac_unicst_update(vswp->if_mh, (uint8_t *)&vswp->if_addr);
286434683adeSsg70180 	}
286534683adeSsg70180 
286634683adeSsg70180 	return;
286734683adeSsg70180 
286834683adeSsg70180 fail_reconf:
286934683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: configuration unchanged", vswp->instance);
287034683adeSsg70180 	return;
287134683adeSsg70180 
287234683adeSsg70180 fail_update:
287334683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: update of configuration failed",
287434683adeSsg70180 			vswp->instance);
287534683adeSsg70180 }
287634683adeSsg70180 
287734683adeSsg70180 /*
28781ae08745Sheppo  * Add a new port to the system.
28791ae08745Sheppo  *
28801ae08745Sheppo  * Returns 0 on success, 1 on failure.
28811ae08745Sheppo  */
28821ae08745Sheppo int
28831ae08745Sheppo vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node)
28841ae08745Sheppo {
28851ae08745Sheppo 	uint64_t		ldc_id;
28861ae08745Sheppo 	uint8_t			*addrp;
28871ae08745Sheppo 	int			i, addrsz;
28881ae08745Sheppo 	int			num_nodes = 0, nchan = 0;
28891ae08745Sheppo 	int			listsz = 0;
28901ae08745Sheppo 	mde_cookie_t		*listp = NULL;
28911ae08745Sheppo 	struct ether_addr	ea;
28921ae08745Sheppo 	uint64_t		macaddr;
28931ae08745Sheppo 	uint64_t		inst = 0;
28941ae08745Sheppo 	vsw_port_t		*port;
28951ae08745Sheppo 
28961ae08745Sheppo 	if (md_get_prop_val(mdp, *node, id_propname, &inst)) {
28971ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found", __func__,
28981ae08745Sheppo 			id_propname);
28991ae08745Sheppo 		return (1);
29001ae08745Sheppo 	}
29011ae08745Sheppo 
29021ae08745Sheppo 	/*
29031ae08745Sheppo 	 * Find the channel endpoint node(s) (which should be under this
29041ae08745Sheppo 	 * port node) which contain the channel id(s).
29051ae08745Sheppo 	 */
29061ae08745Sheppo 	if ((num_nodes = md_node_count(mdp)) <= 0) {
29071ae08745Sheppo 		DERR(vswp, "%s: invalid number of nodes found (%d)",
29081ae08745Sheppo 			__func__, num_nodes);
29091ae08745Sheppo 		return (1);
29101ae08745Sheppo 	}
29111ae08745Sheppo 
291234683adeSsg70180 	D2(vswp, "%s: %d nodes found", __func__, num_nodes);
291334683adeSsg70180 
29141ae08745Sheppo 	/* allocate enough space for node list */
29151ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
29161ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
29171ae08745Sheppo 
29181ae08745Sheppo 	nchan = md_scan_dag(mdp, *node,
29191ae08745Sheppo 		md_find_name(mdp, chan_propname),
29201ae08745Sheppo 		md_find_name(mdp, "fwd"), listp);
29211ae08745Sheppo 
29221ae08745Sheppo 	if (nchan <= 0) {
29231ae08745Sheppo 		DWARN(vswp, "%s: no %s nodes found", __func__, chan_propname);
29241ae08745Sheppo 		kmem_free(listp, listsz);
29251ae08745Sheppo 		return (1);
29261ae08745Sheppo 	}
29271ae08745Sheppo 
29281ae08745Sheppo 	D2(vswp, "%s: %d %s nodes found", __func__, nchan, chan_propname);
29291ae08745Sheppo 
29301ae08745Sheppo 	/* use property from first node found */
29311ae08745Sheppo 	if (md_get_prop_val(mdp, listp[0], id_propname, &ldc_id)) {
29321ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found\n", __func__,
29331ae08745Sheppo 			id_propname);
29341ae08745Sheppo 		kmem_free(listp, listsz);
29351ae08745Sheppo 		return (1);
29361ae08745Sheppo 	}
29371ae08745Sheppo 
29381ae08745Sheppo 	/* don't need list any more */
29391ae08745Sheppo 	kmem_free(listp, listsz);
29401ae08745Sheppo 
29411ae08745Sheppo 	D2(vswp, "%s: ldc_id 0x%llx", __func__, ldc_id);
29421ae08745Sheppo 
29431ae08745Sheppo 	/* read mac-address property */
29441ae08745Sheppo 	if (md_get_prop_data(mdp, *node, remaddr_propname,
29451ae08745Sheppo 					&addrp, &addrsz)) {
29461ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found",
29471ae08745Sheppo 				__func__, remaddr_propname);
29481ae08745Sheppo 		return (1);
29491ae08745Sheppo 	}
29501ae08745Sheppo 
29511ae08745Sheppo 	if (addrsz < ETHERADDRL) {
29521ae08745Sheppo 		DWARN(vswp, "%s: invalid address size", __func__);
29531ae08745Sheppo 		return (1);
29541ae08745Sheppo 	}
29551ae08745Sheppo 
29561ae08745Sheppo 	macaddr = *((uint64_t *)addrp);
29571ae08745Sheppo 	D2(vswp, "%s: remote mac address 0x%llx", __func__, macaddr);
29581ae08745Sheppo 
29591ae08745Sheppo 	for (i = ETHERADDRL - 1; i >= 0; i--) {
29601ae08745Sheppo 		ea.ether_addr_octet[i] = macaddr & 0xFF;
29611ae08745Sheppo 		macaddr >>= 8;
29621ae08745Sheppo 	}
29631ae08745Sheppo 
29641ae08745Sheppo 	if (vsw_port_attach(vswp, (int)inst, &ldc_id, 1, &ea) != 0) {
29651ae08745Sheppo 		DERR(vswp, "%s: failed to attach port", __func__);
29661ae08745Sheppo 		return (1);
29671ae08745Sheppo 	}
29681ae08745Sheppo 
29691ae08745Sheppo 	port = vsw_lookup_port(vswp, (int)inst);
29701ae08745Sheppo 
29711ae08745Sheppo 	/* just successfuly created the port, so it should exist */
29721ae08745Sheppo 	ASSERT(port != NULL);
29731ae08745Sheppo 
29741ae08745Sheppo 	return (0);
29751ae08745Sheppo }
29761ae08745Sheppo 
29771ae08745Sheppo /*
29781ae08745Sheppo  * Attach the specified port.
29791ae08745Sheppo  *
29801ae08745Sheppo  * Returns 0 on success, 1 on failure.
29811ae08745Sheppo  */
29821ae08745Sheppo static int
29831ae08745Sheppo vsw_port_attach(vsw_t *vswp, int p_instance, uint64_t *ldcids, int nids,
29841ae08745Sheppo struct ether_addr *macaddr)
29851ae08745Sheppo {
29861ae08745Sheppo 	vsw_port_list_t		*plist = &vswp->plist;
29871ae08745Sheppo 	vsw_port_t		*port, **prev_port;
29881ae08745Sheppo 	int			i;
29891ae08745Sheppo 
29901ae08745Sheppo 	D1(vswp, "%s: enter : port %d", __func__, p_instance);
29911ae08745Sheppo 
29921ae08745Sheppo 	/* port already exists? */
29931ae08745Sheppo 	READ_ENTER(&plist->lockrw);
29941ae08745Sheppo 	for (port = plist->head; port != NULL; port = port->p_next) {
29951ae08745Sheppo 		if (port->p_instance == p_instance) {
29961ae08745Sheppo 			DWARN(vswp, "%s: port instance %d already attached",
29971ae08745Sheppo 				__func__, p_instance);
29981ae08745Sheppo 			RW_EXIT(&plist->lockrw);
29991ae08745Sheppo 			return (1);
30001ae08745Sheppo 		}
30011ae08745Sheppo 	}
30021ae08745Sheppo 	RW_EXIT(&plist->lockrw);
30031ae08745Sheppo 
30041ae08745Sheppo 	port = kmem_zalloc(sizeof (vsw_port_t), KM_SLEEP);
30051ae08745Sheppo 	port->p_vswp = vswp;
30061ae08745Sheppo 	port->p_instance = p_instance;
30071ae08745Sheppo 	port->p_ldclist.num_ldcs = 0;
30081ae08745Sheppo 	port->p_ldclist.head = NULL;
3009e1ebb9ecSlm66018 	port->addr_set = VSW_ADDR_UNSET;
30101ae08745Sheppo 
30111ae08745Sheppo 	rw_init(&port->p_ldclist.lockrw, NULL, RW_DRIVER, NULL);
30121ae08745Sheppo 
30131ae08745Sheppo 	mutex_init(&port->tx_lock, NULL, MUTEX_DRIVER, NULL);
30141ae08745Sheppo 	mutex_init(&port->mca_lock, NULL, MUTEX_DRIVER, NULL);
30151ae08745Sheppo 
30161ae08745Sheppo 	mutex_init(&port->ref_lock, NULL, MUTEX_DRIVER, NULL);
30171ae08745Sheppo 	cv_init(&port->ref_cv, NULL, CV_DRIVER, NULL);
30181ae08745Sheppo 
30191ae08745Sheppo 	mutex_init(&port->state_lock, NULL, MUTEX_DRIVER, NULL);
30201ae08745Sheppo 	cv_init(&port->state_cv, NULL, CV_DRIVER, NULL);
30211ae08745Sheppo 	port->state = VSW_PORT_INIT;
30221ae08745Sheppo 
30231ae08745Sheppo 	if (nids > VSW_PORT_MAX_LDCS) {
30241ae08745Sheppo 		D2(vswp, "%s: using first of %d ldc ids",
30251ae08745Sheppo 			__func__, nids);
30261ae08745Sheppo 		nids = VSW_PORT_MAX_LDCS;
30271ae08745Sheppo 	}
30281ae08745Sheppo 
30291ae08745Sheppo 	D2(vswp, "%s: %d nids", __func__, nids);
30301ae08745Sheppo 	for (i = 0; i < nids; i++) {
30311ae08745Sheppo 		D2(vswp, "%s: ldcid (%llx)", __func__, (uint64_t)ldcids[i]);
30321ae08745Sheppo 		if (vsw_ldc_attach(port, (uint64_t)ldcids[i]) != 0) {
30331ae08745Sheppo 			DERR(vswp, "%s: ldc_attach failed", __func__);
30341ae08745Sheppo 
30351ae08745Sheppo 			rw_destroy(&port->p_ldclist.lockrw);
30361ae08745Sheppo 
30371ae08745Sheppo 			cv_destroy(&port->ref_cv);
30381ae08745Sheppo 			mutex_destroy(&port->ref_lock);
30391ae08745Sheppo 
30401ae08745Sheppo 			cv_destroy(&port->state_cv);
30411ae08745Sheppo 			mutex_destroy(&port->state_lock);
30421ae08745Sheppo 
30431ae08745Sheppo 			mutex_destroy(&port->tx_lock);
30441ae08745Sheppo 			mutex_destroy(&port->mca_lock);
30451ae08745Sheppo 			kmem_free(port, sizeof (vsw_port_t));
30461ae08745Sheppo 			return (1);
30471ae08745Sheppo 		}
30481ae08745Sheppo 	}
30491ae08745Sheppo 
30501ae08745Sheppo 	ether_copy(macaddr, &port->p_macaddr);
30511ae08745Sheppo 
30521ae08745Sheppo 	WRITE_ENTER(&plist->lockrw);
30531ae08745Sheppo 
30541ae08745Sheppo 	/* create the fdb entry for this port/mac address */
30551ae08745Sheppo 	(void) vsw_add_fdb(vswp, port);
30561ae08745Sheppo 
3057e1ebb9ecSlm66018 	(void) vsw_set_hw(vswp, port);
3058e1ebb9ecSlm66018 
30591ae08745Sheppo 	/* link it into the list of ports for this vsw instance */
30601ae08745Sheppo 	prev_port = (vsw_port_t **)(&plist->head);
30611ae08745Sheppo 	port->p_next = *prev_port;
30621ae08745Sheppo 	*prev_port = port;
30631ae08745Sheppo 	plist->num_ports++;
30641ae08745Sheppo 	RW_EXIT(&plist->lockrw);
30651ae08745Sheppo 
30661ae08745Sheppo 	/*
30671ae08745Sheppo 	 * Initialise the port and any ldc's under it.
30681ae08745Sheppo 	 */
30691ae08745Sheppo 	(void) vsw_init_ldcs(port);
30701ae08745Sheppo 
30711ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
30721ae08745Sheppo 	return (0);
30731ae08745Sheppo }
30741ae08745Sheppo 
30751ae08745Sheppo /*
30761ae08745Sheppo  * Detach the specified port.
30771ae08745Sheppo  *
30781ae08745Sheppo  * Returns 0 on success, 1 on failure.
30791ae08745Sheppo  */
30801ae08745Sheppo static int
30811ae08745Sheppo vsw_port_detach(vsw_t *vswp, int p_instance)
30821ae08745Sheppo {
30831ae08745Sheppo 	vsw_port_t	*port = NULL;
30841ae08745Sheppo 	vsw_port_list_t	*plist = &vswp->plist;
30851ae08745Sheppo 
30861ae08745Sheppo 	D1(vswp, "%s: enter: port id %d", __func__, p_instance);
30871ae08745Sheppo 
30881ae08745Sheppo 	WRITE_ENTER(&plist->lockrw);
30891ae08745Sheppo 
30901ae08745Sheppo 	if ((port = vsw_lookup_port(vswp, p_instance)) == NULL) {
30911ae08745Sheppo 		RW_EXIT(&plist->lockrw);
30921ae08745Sheppo 		return (1);
30931ae08745Sheppo 	}
30941ae08745Sheppo 
30951ae08745Sheppo 	if (vsw_plist_del_node(vswp, port)) {
30961ae08745Sheppo 		RW_EXIT(&plist->lockrw);
30971ae08745Sheppo 		return (1);
30981ae08745Sheppo 	}
30991ae08745Sheppo 
3100e1ebb9ecSlm66018 	/* Remove address if was programmed into HW. */
3101e1ebb9ecSlm66018 	(void) vsw_unset_hw(vswp, port);
3102e1ebb9ecSlm66018 
31031ae08745Sheppo 	/* Remove the fdb entry for this port/mac address */
31041ae08745Sheppo 	(void) vsw_del_fdb(vswp, port);
31051ae08745Sheppo 
31061ae08745Sheppo 	/* Remove any multicast addresses.. */
31071ae08745Sheppo 	vsw_del_mcst_port(port);
31081ae08745Sheppo 
31091ae08745Sheppo 	/*
3110e1ebb9ecSlm66018 	 * No longer need to hold writer lock on port list now
3111e1ebb9ecSlm66018 	 * that we have unlinked the target port from the list.
31121ae08745Sheppo 	 */
31131ae08745Sheppo 	RW_EXIT(&plist->lockrw);
31141ae08745Sheppo 
3115e1ebb9ecSlm66018 	READ_ENTER(&plist->lockrw);
3116e1ebb9ecSlm66018 
3117e1ebb9ecSlm66018 	if (vswp->recfg_reqd)
3118e1ebb9ecSlm66018 		(void) vsw_reconfig_hw(vswp);
3119e1ebb9ecSlm66018 
3120e1ebb9ecSlm66018 	RW_EXIT(&plist->lockrw);
3121e1ebb9ecSlm66018 
31221ae08745Sheppo 	if (vsw_port_delete(port)) {
31231ae08745Sheppo 		return (1);
31241ae08745Sheppo 	}
31251ae08745Sheppo 
31261ae08745Sheppo 	D1(vswp, "%s: exit: p_instance(%d)", __func__, p_instance);
31271ae08745Sheppo 	return (0);
31281ae08745Sheppo }
31291ae08745Sheppo 
31301ae08745Sheppo /*
31311ae08745Sheppo  * Detach all active ports.
31321ae08745Sheppo  *
31331ae08745Sheppo  * Returns 0 on success, 1 on failure.
31341ae08745Sheppo  */
31351ae08745Sheppo static int
31361ae08745Sheppo vsw_detach_ports(vsw_t *vswp)
31371ae08745Sheppo {
31381ae08745Sheppo 	vsw_port_list_t 	*plist = &vswp->plist;
31391ae08745Sheppo 	vsw_port_t		*port = NULL;
31401ae08745Sheppo 
31411ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
31421ae08745Sheppo 
31431ae08745Sheppo 	WRITE_ENTER(&plist->lockrw);
31441ae08745Sheppo 
31451ae08745Sheppo 	while ((port = plist->head) != NULL) {
31461ae08745Sheppo 		if (vsw_plist_del_node(vswp, port)) {
31471ae08745Sheppo 			DERR(vswp, "%s: Error deleting port %d"
31481ae08745Sheppo 				" from port list", __func__,
31491ae08745Sheppo 				port->p_instance);
31501ae08745Sheppo 			RW_EXIT(&plist->lockrw);
31511ae08745Sheppo 			return (1);
31521ae08745Sheppo 		}
31531ae08745Sheppo 
3154e1ebb9ecSlm66018 		/* Remove address if was programmed into HW. */
3155e1ebb9ecSlm66018 		(void) vsw_unset_hw(vswp, port);
3156e1ebb9ecSlm66018 
31571ae08745Sheppo 		/* Remove the fdb entry for this port/mac address */
31581ae08745Sheppo 		(void) vsw_del_fdb(vswp, port);
31591ae08745Sheppo 
31601ae08745Sheppo 		/* Remove any multicast addresses.. */
31611ae08745Sheppo 		vsw_del_mcst_port(port);
31621ae08745Sheppo 
31631ae08745Sheppo 		/*
31641ae08745Sheppo 		 * No longer need to hold the lock on the port list
31651ae08745Sheppo 		 * now that we have unlinked the target port from the
31661ae08745Sheppo 		 * list.
31671ae08745Sheppo 		 */
31681ae08745Sheppo 		RW_EXIT(&plist->lockrw);
31691ae08745Sheppo 		if (vsw_port_delete(port)) {
31701ae08745Sheppo 			DERR(vswp, "%s: Error deleting port %d",
31711ae08745Sheppo 				__func__, port->p_instance);
31721ae08745Sheppo 			return (1);
31731ae08745Sheppo 		}
31741ae08745Sheppo 		WRITE_ENTER(&plist->lockrw);
31751ae08745Sheppo 	}
31761ae08745Sheppo 	RW_EXIT(&plist->lockrw);
31771ae08745Sheppo 
31781ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
31791ae08745Sheppo 
31801ae08745Sheppo 	return (0);
31811ae08745Sheppo }
31821ae08745Sheppo 
31831ae08745Sheppo /*
31841ae08745Sheppo  * Delete the specified port.
31851ae08745Sheppo  *
31861ae08745Sheppo  * Returns 0 on success, 1 on failure.
31871ae08745Sheppo  */
31881ae08745Sheppo static int
31891ae08745Sheppo vsw_port_delete(vsw_port_t *port)
31901ae08745Sheppo {
31911ae08745Sheppo 	vsw_ldc_list_t 		*ldcl;
31921ae08745Sheppo 	vsw_t			*vswp = port->p_vswp;
31931ae08745Sheppo 
31941ae08745Sheppo 	D1(vswp, "%s: enter : port id %d", __func__, port->p_instance);
31951ae08745Sheppo 
31961ae08745Sheppo 	(void) vsw_uninit_ldcs(port);
31971ae08745Sheppo 
31981ae08745Sheppo 	/*
31991ae08745Sheppo 	 * Wait for any pending ctrl msg tasks which reference this
32001ae08745Sheppo 	 * port to finish.
32011ae08745Sheppo 	 */
32021ae08745Sheppo 	if (vsw_drain_port_taskq(port))
32031ae08745Sheppo 		return (1);
32041ae08745Sheppo 
32051ae08745Sheppo 	/*
32061ae08745Sheppo 	 * Wait for port reference count to hit zero.
32071ae08745Sheppo 	 */
32081ae08745Sheppo 	mutex_enter(&port->ref_lock);
32091ae08745Sheppo 	while (port->ref_cnt != 0)
32101ae08745Sheppo 		cv_wait(&port->ref_cv, &port->ref_lock);
32111ae08745Sheppo 	mutex_exit(&port->ref_lock);
32121ae08745Sheppo 
32131ae08745Sheppo 	/*
32141ae08745Sheppo 	 * Wait for any active callbacks to finish
32151ae08745Sheppo 	 */
32161ae08745Sheppo 	if (vsw_drain_ldcs(port))
32171ae08745Sheppo 		return (1);
32181ae08745Sheppo 
32191ae08745Sheppo 	ldcl = &port->p_ldclist;
32201ae08745Sheppo 	WRITE_ENTER(&ldcl->lockrw);
32211ae08745Sheppo 	while (ldcl->num_ldcs > 0) {
32221ae08745Sheppo 		if (vsw_ldc_detach(port, ldcl->head->ldc_id) != 0) {;
322334683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: unable to detach ldc %ld",
322434683adeSsg70180 					vswp->instance, ldcl->head->ldc_id);
32251ae08745Sheppo 			RW_EXIT(&ldcl->lockrw);
32261ae08745Sheppo 			return (1);
32271ae08745Sheppo 		}
32281ae08745Sheppo 	}
32291ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
32301ae08745Sheppo 
32311ae08745Sheppo 	rw_destroy(&port->p_ldclist.lockrw);
32321ae08745Sheppo 
32331ae08745Sheppo 	mutex_destroy(&port->mca_lock);
32341ae08745Sheppo 	mutex_destroy(&port->tx_lock);
32351ae08745Sheppo 	cv_destroy(&port->ref_cv);
32361ae08745Sheppo 	mutex_destroy(&port->ref_lock);
32371ae08745Sheppo 
32381ae08745Sheppo 	cv_destroy(&port->state_cv);
32391ae08745Sheppo 	mutex_destroy(&port->state_lock);
32401ae08745Sheppo 
32411ae08745Sheppo 	kmem_free(port, sizeof (vsw_port_t));
32421ae08745Sheppo 
32431ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
32441ae08745Sheppo 
32451ae08745Sheppo 	return (0);
32461ae08745Sheppo }
32471ae08745Sheppo 
32481ae08745Sheppo /*
32491ae08745Sheppo  * Attach a logical domain channel (ldc) under a specified port.
32501ae08745Sheppo  *
32511ae08745Sheppo  * Returns 0 on success, 1 on failure.
32521ae08745Sheppo  */
32531ae08745Sheppo static int
32541ae08745Sheppo vsw_ldc_attach(vsw_port_t *port, uint64_t ldc_id)
32551ae08745Sheppo {
32561ae08745Sheppo 	vsw_t 		*vswp = port->p_vswp;
32571ae08745Sheppo 	vsw_ldc_list_t *ldcl = &port->p_ldclist;
32581ae08745Sheppo 	vsw_ldc_t 	*ldcp = NULL;
32591ae08745Sheppo 	ldc_attr_t 	attr;
32601ae08745Sheppo 	ldc_status_t	istatus;
32611ae08745Sheppo 	int 		status = DDI_FAILURE;
3262d10e4ef2Snarayan 	int		rv;
32633af08d82Slm66018 	enum		{ PROG_init = 0x0, PROG_mblks = 0x1,
32643af08d82Slm66018 				PROG_callback = 0x2}
32653af08d82Slm66018 			progress;
32663af08d82Slm66018 
32673af08d82Slm66018 	progress = PROG_init;
32681ae08745Sheppo 
32691ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
32701ae08745Sheppo 
32711ae08745Sheppo 	ldcp = kmem_zalloc(sizeof (vsw_ldc_t), KM_NOSLEEP);
32721ae08745Sheppo 	if (ldcp == NULL) {
32731ae08745Sheppo 		DERR(vswp, "%s: kmem_zalloc failed", __func__);
32741ae08745Sheppo 		return (1);
32751ae08745Sheppo 	}
32761ae08745Sheppo 	ldcp->ldc_id = ldc_id;
32771ae08745Sheppo 
3278d10e4ef2Snarayan 	/* allocate pool of receive mblks */
3279d10e4ef2Snarayan 	rv = vio_create_mblks(vsw_num_mblks, vsw_mblk_size, &(ldcp->rxh));
3280d10e4ef2Snarayan 	if (rv) {
3281d10e4ef2Snarayan 		DWARN(vswp, "%s: unable to create free mblk pool for"
3282d10e4ef2Snarayan 			" channel %ld (rv %d)", __func__, ldc_id, rv);
3283d10e4ef2Snarayan 		kmem_free(ldcp, sizeof (vsw_ldc_t));
3284d10e4ef2Snarayan 		return (1);
3285d10e4ef2Snarayan 	}
3286d10e4ef2Snarayan 
32873af08d82Slm66018 	progress |= PROG_mblks;
32883af08d82Slm66018 
32891ae08745Sheppo 	mutex_init(&ldcp->ldc_txlock, NULL, MUTEX_DRIVER, NULL);
32901ae08745Sheppo 	mutex_init(&ldcp->ldc_cblock, NULL, MUTEX_DRIVER, NULL);
32911ae08745Sheppo 	mutex_init(&ldcp->drain_cv_lock, NULL, MUTEX_DRIVER, NULL);
32921ae08745Sheppo 	cv_init(&ldcp->drain_cv, NULL, CV_DRIVER, NULL);
3293*445b4c2eSsb155480 	rw_init(&ldcp->lane_in.dlistrw, NULL, RW_DRIVER, NULL);
3294*445b4c2eSsb155480 	rw_init(&ldcp->lane_out.dlistrw, NULL, RW_DRIVER, NULL);
32951ae08745Sheppo 
32961ae08745Sheppo 	/* required for handshake with peer */
32971ae08745Sheppo 	ldcp->local_session = (uint64_t)ddi_get_lbolt();
32981ae08745Sheppo 	ldcp->peer_session = 0;
32991ae08745Sheppo 	ldcp->session_status = 0;
33001ae08745Sheppo 
33011ae08745Sheppo 	mutex_init(&ldcp->hss_lock, NULL, MUTEX_DRIVER, NULL);
33021ae08745Sheppo 	ldcp->hss_id = 1;	/* Initial handshake session id */
33031ae08745Sheppo 
33041ae08745Sheppo 	/* only set for outbound lane, inbound set by peer */
3305d10e4ef2Snarayan 	mutex_init(&ldcp->lane_in.seq_lock, NULL, MUTEX_DRIVER, NULL);
3306d10e4ef2Snarayan 	mutex_init(&ldcp->lane_out.seq_lock, NULL, MUTEX_DRIVER, NULL);
33071ae08745Sheppo 	vsw_set_lane_attr(vswp, &ldcp->lane_out);
33081ae08745Sheppo 
33091ae08745Sheppo 	attr.devclass = LDC_DEV_NT_SVC;
33101ae08745Sheppo 	attr.instance = ddi_get_instance(vswp->dip);
33111ae08745Sheppo 	attr.mode = LDC_MODE_UNRELIABLE;
3312e1ebb9ecSlm66018 	attr.mtu = VSW_LDC_MTU;
33131ae08745Sheppo 	status = ldc_init(ldc_id, &attr, &ldcp->ldc_handle);
33141ae08745Sheppo 	if (status != 0) {
33151ae08745Sheppo 		DERR(vswp, "%s(%lld): ldc_init failed, rv (%d)",
33161ae08745Sheppo 		    __func__, ldc_id, status);
3317d10e4ef2Snarayan 		goto ldc_attach_fail;
33181ae08745Sheppo 	}
33191ae08745Sheppo 
33201ae08745Sheppo 	status = ldc_reg_callback(ldcp->ldc_handle, vsw_ldc_cb, (caddr_t)ldcp);
33211ae08745Sheppo 	if (status != 0) {
33221ae08745Sheppo 		DERR(vswp, "%s(%lld): ldc_reg_callback failed, rv (%d)",
33231ae08745Sheppo 		    __func__, ldc_id, status);
33241ae08745Sheppo 		(void) ldc_fini(ldcp->ldc_handle);
3325d10e4ef2Snarayan 		goto ldc_attach_fail;
33261ae08745Sheppo 	}
33271ae08745Sheppo 
33283af08d82Slm66018 	progress |= PROG_callback;
33293af08d82Slm66018 
33303af08d82Slm66018 	mutex_init(&ldcp->status_lock, NULL, MUTEX_DRIVER, NULL);
33311ae08745Sheppo 
33321ae08745Sheppo 	if (ldc_status(ldcp->ldc_handle, &istatus) != 0) {
33331ae08745Sheppo 		DERR(vswp, "%s: ldc_status failed", __func__);
33343af08d82Slm66018 		mutex_destroy(&ldcp->status_lock);
33353af08d82Slm66018 		goto ldc_attach_fail;
33361ae08745Sheppo 	}
33371ae08745Sheppo 
33381ae08745Sheppo 	ldcp->ldc_status = istatus;
33391ae08745Sheppo 	ldcp->ldc_port = port;
33401ae08745Sheppo 	ldcp->ldc_vswp = vswp;
33411ae08745Sheppo 
33421ae08745Sheppo 	/* link it into the list of channels for this port */
33431ae08745Sheppo 	WRITE_ENTER(&ldcl->lockrw);
33441ae08745Sheppo 	ldcp->ldc_next = ldcl->head;
33451ae08745Sheppo 	ldcl->head = ldcp;
33461ae08745Sheppo 	ldcl->num_ldcs++;
33471ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
33481ae08745Sheppo 
33491ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
33501ae08745Sheppo 	return (0);
3351d10e4ef2Snarayan 
3352d10e4ef2Snarayan ldc_attach_fail:
3353d10e4ef2Snarayan 	mutex_destroy(&ldcp->ldc_txlock);
3354d10e4ef2Snarayan 	mutex_destroy(&ldcp->ldc_cblock);
3355d10e4ef2Snarayan 
3356d10e4ef2Snarayan 	cv_destroy(&ldcp->drain_cv);
3357d10e4ef2Snarayan 
3358*445b4c2eSsb155480 	rw_destroy(&ldcp->lane_in.dlistrw);
3359*445b4c2eSsb155480 	rw_destroy(&ldcp->lane_out.dlistrw);
3360*445b4c2eSsb155480 
33613af08d82Slm66018 	if (progress & PROG_callback) {
33623af08d82Slm66018 		(void) ldc_unreg_callback(ldcp->ldc_handle);
33633af08d82Slm66018 	}
33643af08d82Slm66018 
33653af08d82Slm66018 	if ((progress & PROG_mblks) && (ldcp->rxh != NULL)) {
3366d10e4ef2Snarayan 		if (vio_destroy_mblks(ldcp->rxh) != 0) {
3367d10e4ef2Snarayan 			/*
3368d10e4ef2Snarayan 			 * Something odd has happened, as the destroy
3369d10e4ef2Snarayan 			 * will only fail if some mblks have been allocated
3370d10e4ef2Snarayan 			 * from the pool already (which shouldn't happen)
3371d10e4ef2Snarayan 			 * and have not been returned.
3372d10e4ef2Snarayan 			 *
3373d10e4ef2Snarayan 			 * Add the pool pointer to a list maintained in
3374d10e4ef2Snarayan 			 * the device instance. Another attempt will be made
3375d10e4ef2Snarayan 			 * to free the pool when the device itself detaches.
3376d10e4ef2Snarayan 			 */
337734683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Creation of ldc channel %ld "
337834683adeSsg70180 				"failed and cannot destroy associated mblk "
337934683adeSsg70180 				"pool", vswp->instance, ldc_id);
3380d10e4ef2Snarayan 			ldcp->rxh->nextp =  vswp->rxh;
3381d10e4ef2Snarayan 			vswp->rxh = ldcp->rxh;
3382d10e4ef2Snarayan 		}
3383d10e4ef2Snarayan 	}
3384d10e4ef2Snarayan 	mutex_destroy(&ldcp->drain_cv_lock);
3385d10e4ef2Snarayan 	mutex_destroy(&ldcp->hss_lock);
3386d10e4ef2Snarayan 
3387d10e4ef2Snarayan 	mutex_destroy(&ldcp->lane_in.seq_lock);
3388d10e4ef2Snarayan 	mutex_destroy(&ldcp->lane_out.seq_lock);
3389d10e4ef2Snarayan 	kmem_free(ldcp, sizeof (vsw_ldc_t));
3390d10e4ef2Snarayan 
3391d10e4ef2Snarayan 	return (1);
33921ae08745Sheppo }
33931ae08745Sheppo 
33941ae08745Sheppo /*
33951ae08745Sheppo  * Detach a logical domain channel (ldc) belonging to a
33961ae08745Sheppo  * particular port.
33971ae08745Sheppo  *
33981ae08745Sheppo  * Returns 0 on success, 1 on failure.
33991ae08745Sheppo  */
34001ae08745Sheppo static int
34011ae08745Sheppo vsw_ldc_detach(vsw_port_t *port, uint64_t ldc_id)
34021ae08745Sheppo {
34031ae08745Sheppo 	vsw_t 		*vswp = port->p_vswp;
34041ae08745Sheppo 	vsw_ldc_t 	*ldcp, *prev_ldcp;
34051ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
34061ae08745Sheppo 	int 		rv;
34071ae08745Sheppo 
34081ae08745Sheppo 	prev_ldcp = ldcl->head;
34091ae08745Sheppo 	for (; (ldcp = prev_ldcp) != NULL; prev_ldcp = ldcp->ldc_next) {
34101ae08745Sheppo 		if (ldcp->ldc_id == ldc_id) {
34111ae08745Sheppo 			break;
34121ae08745Sheppo 		}
34131ae08745Sheppo 	}
34141ae08745Sheppo 
34151ae08745Sheppo 	/* specified ldc id not found */
34161ae08745Sheppo 	if (ldcp == NULL) {
34171ae08745Sheppo 		DERR(vswp, "%s: ldcp = NULL", __func__);
34181ae08745Sheppo 		return (1);
34191ae08745Sheppo 	}
34201ae08745Sheppo 
34211ae08745Sheppo 	D2(vswp, "%s: detaching channel %lld", __func__, ldcp->ldc_id);
34221ae08745Sheppo 
34231ae08745Sheppo 	/*
34241ae08745Sheppo 	 * Before we can close the channel we must release any mapped
34251ae08745Sheppo 	 * resources (e.g. drings).
34261ae08745Sheppo 	 */
34271ae08745Sheppo 	vsw_free_lane_resources(ldcp, INBOUND);
34281ae08745Sheppo 	vsw_free_lane_resources(ldcp, OUTBOUND);
34291ae08745Sheppo 
34301ae08745Sheppo 	/*
34311ae08745Sheppo 	 * If the close fails we are in serious trouble, as won't
34321ae08745Sheppo 	 * be able to delete the parent port.
34331ae08745Sheppo 	 */
34341ae08745Sheppo 	if ((rv = ldc_close(ldcp->ldc_handle)) != 0) {
34351ae08745Sheppo 		DERR(vswp, "%s: error %d closing channel %lld",
34361ae08745Sheppo 			__func__, rv, ldcp->ldc_id);
34371ae08745Sheppo 		return (1);
34381ae08745Sheppo 	}
34391ae08745Sheppo 
34401ae08745Sheppo 	(void) ldc_fini(ldcp->ldc_handle);
34411ae08745Sheppo 
34421ae08745Sheppo 	ldcp->ldc_status = LDC_INIT;
34431ae08745Sheppo 	ldcp->ldc_handle = NULL;
34441ae08745Sheppo 	ldcp->ldc_vswp = NULL;
3445d10e4ef2Snarayan 
3446d10e4ef2Snarayan 	if (ldcp->rxh != NULL) {
3447d10e4ef2Snarayan 		if (vio_destroy_mblks(ldcp->rxh)) {
3448d10e4ef2Snarayan 			/*
3449d10e4ef2Snarayan 			 * Mostly likely some mblks are still in use and
3450d10e4ef2Snarayan 			 * have not been returned to the pool. Add the pool
3451d10e4ef2Snarayan 			 * to the list maintained in the device instance.
3452d10e4ef2Snarayan 			 * Another attempt will be made to destroy the pool
3453d10e4ef2Snarayan 			 * when the device detaches.
3454d10e4ef2Snarayan 			 */
3455d10e4ef2Snarayan 			ldcp->rxh->nextp =  vswp->rxh;
3456d10e4ef2Snarayan 			vswp->rxh = ldcp->rxh;
3457d10e4ef2Snarayan 		}
3458d10e4ef2Snarayan 	}
3459d10e4ef2Snarayan 
34603af08d82Slm66018 	/* unlink it from the list */
34613af08d82Slm66018 	prev_ldcp = ldcp->ldc_next;
34623af08d82Slm66018 	ldcl->num_ldcs--;
34633af08d82Slm66018 
34641ae08745Sheppo 	mutex_destroy(&ldcp->ldc_txlock);
34651ae08745Sheppo 	mutex_destroy(&ldcp->ldc_cblock);
34661ae08745Sheppo 	cv_destroy(&ldcp->drain_cv);
34671ae08745Sheppo 	mutex_destroy(&ldcp->drain_cv_lock);
34681ae08745Sheppo 	mutex_destroy(&ldcp->hss_lock);
3469d10e4ef2Snarayan 	mutex_destroy(&ldcp->lane_in.seq_lock);
3470d10e4ef2Snarayan 	mutex_destroy(&ldcp->lane_out.seq_lock);
34713af08d82Slm66018 	mutex_destroy(&ldcp->status_lock);
3472*445b4c2eSsb155480 	rw_destroy(&ldcp->lane_in.dlistrw);
3473*445b4c2eSsb155480 	rw_destroy(&ldcp->lane_out.dlistrw);
34741ae08745Sheppo 
34751ae08745Sheppo 	kmem_free(ldcp, sizeof (vsw_ldc_t));
34761ae08745Sheppo 
34771ae08745Sheppo 	return (0);
34781ae08745Sheppo }
34791ae08745Sheppo 
34801ae08745Sheppo /*
34811ae08745Sheppo  * Open and attempt to bring up the channel. Note that channel
34821ae08745Sheppo  * can only be brought up if peer has also opened channel.
34831ae08745Sheppo  *
34841ae08745Sheppo  * Returns 0 if can open and bring up channel, otherwise
34851ae08745Sheppo  * returns 1.
34861ae08745Sheppo  */
34871ae08745Sheppo static int
34881ae08745Sheppo vsw_ldc_init(vsw_ldc_t *ldcp)
34891ae08745Sheppo {
34901ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
34911ae08745Sheppo 	ldc_status_t	istatus = 0;
34921ae08745Sheppo 	int		rv;
34931ae08745Sheppo 
34941ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
34951ae08745Sheppo 
34961ae08745Sheppo 	LDC_ENTER_LOCK(ldcp);
34971ae08745Sheppo 
34981ae08745Sheppo 	/* don't start at 0 in case clients don't like that */
34991ae08745Sheppo 	ldcp->next_ident = 1;
35001ae08745Sheppo 
35011ae08745Sheppo 	rv = ldc_open(ldcp->ldc_handle);
35021ae08745Sheppo 	if (rv != 0) {
35031ae08745Sheppo 		DERR(vswp, "%s: ldc_open failed: id(%lld) rv(%d)",
35041ae08745Sheppo 		    __func__, ldcp->ldc_id, rv);
35051ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35061ae08745Sheppo 		return (1);
35071ae08745Sheppo 	}
35081ae08745Sheppo 
35091ae08745Sheppo 	if (ldc_status(ldcp->ldc_handle, &istatus) != 0) {
35101ae08745Sheppo 		DERR(vswp, "%s: unable to get status", __func__);
35111ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35121ae08745Sheppo 		return (1);
35131ae08745Sheppo 
35141ae08745Sheppo 	} else if (istatus != LDC_OPEN && istatus != LDC_READY) {
35151ae08745Sheppo 		DERR(vswp, "%s: id (%lld) status(%d) is not OPEN/READY",
35161ae08745Sheppo 		    __func__, ldcp->ldc_id, istatus);
35171ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35181ae08745Sheppo 		return (1);
35191ae08745Sheppo 	}
35201ae08745Sheppo 
35213af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
35221ae08745Sheppo 	ldcp->ldc_status = istatus;
35233af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
35243af08d82Slm66018 
35251ae08745Sheppo 	rv = ldc_up(ldcp->ldc_handle);
35261ae08745Sheppo 	if (rv != 0) {
35271ae08745Sheppo 		/*
35281ae08745Sheppo 		 * Not a fatal error for ldc_up() to fail, as peer
35291ae08745Sheppo 		 * end point may simply not be ready yet.
35301ae08745Sheppo 		 */
35311ae08745Sheppo 		D2(vswp, "%s: ldc_up err id(%lld) rv(%d)", __func__,
35321ae08745Sheppo 			ldcp->ldc_id, rv);
35331ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35341ae08745Sheppo 		return (1);
35351ae08745Sheppo 	}
35361ae08745Sheppo 
35371ae08745Sheppo 	/*
35381ae08745Sheppo 	 * ldc_up() call is non-blocking so need to explicitly
35391ae08745Sheppo 	 * check channel status to see if in fact the channel
35401ae08745Sheppo 	 * is UP.
35411ae08745Sheppo 	 */
35423af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
35433af08d82Slm66018 	istatus = ldcp->ldc_status;
35443af08d82Slm66018 	if (ldc_status(ldcp->ldc_handle, &ldcp->ldc_status) != 0) {
35451ae08745Sheppo 		DERR(vswp, "%s: unable to get status", __func__);
35463af08d82Slm66018 		mutex_exit(&ldcp->status_lock);
35471ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35481ae08745Sheppo 		return (1);
35491ae08745Sheppo 
35501ae08745Sheppo 	}
35513af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
35521ae08745Sheppo 	LDC_EXIT_LOCK(ldcp);
35531ae08745Sheppo 
35543af08d82Slm66018 	if ((istatus != LDC_UP) && (ldcp->ldc_status == LDC_UP)) {
35553af08d82Slm66018 		D2(vswp, "%s: channel %ld now UP (%ld)", __func__,
35563af08d82Slm66018 			ldcp->ldc_id, istatus);
35573af08d82Slm66018 		vsw_restart_handshake(ldcp);
35583af08d82Slm66018 	}
35593af08d82Slm66018 
35601ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
35611ae08745Sheppo 	return (0);
35621ae08745Sheppo }
35631ae08745Sheppo 
35641ae08745Sheppo /* disable callbacks on the channel */
35651ae08745Sheppo static int
35661ae08745Sheppo vsw_ldc_uninit(vsw_ldc_t *ldcp)
35671ae08745Sheppo {
35681ae08745Sheppo 	vsw_t	*vswp = ldcp->ldc_vswp;
35691ae08745Sheppo 	int	rv;
35701ae08745Sheppo 
35711ae08745Sheppo 	D1(vswp, "vsw_ldc_uninit: enter: id(%lx)\n", ldcp->ldc_id);
35721ae08745Sheppo 
35731ae08745Sheppo 	LDC_ENTER_LOCK(ldcp);
35741ae08745Sheppo 
35751ae08745Sheppo 	rv = ldc_set_cb_mode(ldcp->ldc_handle, LDC_CB_DISABLE);
35761ae08745Sheppo 	if (rv != 0) {
35771ae08745Sheppo 		DERR(vswp, "vsw_ldc_uninit(%lld): error disabling "
35781ae08745Sheppo 			"interrupts (rv = %d)\n", ldcp->ldc_id, rv);
35791ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35801ae08745Sheppo 		return (1);
35811ae08745Sheppo 	}
35821ae08745Sheppo 
35833af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
35841ae08745Sheppo 	ldcp->ldc_status = LDC_INIT;
35853af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
35861ae08745Sheppo 
35871ae08745Sheppo 	LDC_EXIT_LOCK(ldcp);
35881ae08745Sheppo 
35891ae08745Sheppo 	D1(vswp, "vsw_ldc_uninit: exit: id(%lx)", ldcp->ldc_id);
35901ae08745Sheppo 
35911ae08745Sheppo 	return (0);
35921ae08745Sheppo }
35931ae08745Sheppo 
35941ae08745Sheppo static int
35951ae08745Sheppo vsw_init_ldcs(vsw_port_t *port)
35961ae08745Sheppo {
35971ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
35981ae08745Sheppo 	vsw_ldc_t	*ldcp;
35991ae08745Sheppo 
36001ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
36011ae08745Sheppo 	ldcp =  ldcl->head;
36021ae08745Sheppo 	for (; ldcp  != NULL; ldcp = ldcp->ldc_next) {
36031ae08745Sheppo 		(void) vsw_ldc_init(ldcp);
36041ae08745Sheppo 	}
36051ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
36061ae08745Sheppo 
36071ae08745Sheppo 	return (0);
36081ae08745Sheppo }
36091ae08745Sheppo 
36101ae08745Sheppo static int
36111ae08745Sheppo vsw_uninit_ldcs(vsw_port_t *port)
36121ae08745Sheppo {
36131ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
36141ae08745Sheppo 	vsw_ldc_t	*ldcp;
36151ae08745Sheppo 
36161ae08745Sheppo 	D1(NULL, "vsw_uninit_ldcs: enter\n");
36171ae08745Sheppo 
36181ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
36191ae08745Sheppo 	ldcp =  ldcl->head;
36201ae08745Sheppo 	for (; ldcp  != NULL; ldcp = ldcp->ldc_next) {
36211ae08745Sheppo 		(void) vsw_ldc_uninit(ldcp);
36221ae08745Sheppo 	}
36231ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
36241ae08745Sheppo 
36251ae08745Sheppo 	D1(NULL, "vsw_uninit_ldcs: exit\n");
36261ae08745Sheppo 
36271ae08745Sheppo 	return (0);
36281ae08745Sheppo }
36291ae08745Sheppo 
36301ae08745Sheppo /*
36311ae08745Sheppo  * Wait until the callback(s) associated with the ldcs under the specified
36321ae08745Sheppo  * port have completed.
36331ae08745Sheppo  *
36341ae08745Sheppo  * Prior to this function being invoked each channel under this port
36351ae08745Sheppo  * should have been quiesced via ldc_set_cb_mode(DISABLE).
36361ae08745Sheppo  *
36371ae08745Sheppo  * A short explaination of what we are doing below..
36381ae08745Sheppo  *
36391ae08745Sheppo  * The simplest approach would be to have a reference counter in
36401ae08745Sheppo  * the ldc structure which is increment/decremented by the callbacks as
36411ae08745Sheppo  * they use the channel. The drain function could then simply disable any
36421ae08745Sheppo  * further callbacks and do a cv_wait for the ref to hit zero. Unfortunately
36431ae08745Sheppo  * there is a tiny window here - before the callback is able to get the lock
36441ae08745Sheppo  * on the channel it is interrupted and this function gets to execute. It
36451ae08745Sheppo  * sees that the ref count is zero and believes its free to delete the
36461ae08745Sheppo  * associated data structures.
36471ae08745Sheppo  *
36481ae08745Sheppo  * We get around this by taking advantage of the fact that before the ldc
36491ae08745Sheppo  * framework invokes a callback it sets a flag to indicate that there is a
36501ae08745Sheppo  * callback active (or about to become active). If when we attempt to
36511ae08745Sheppo  * unregister a callback when this active flag is set then the unregister
36521ae08745Sheppo  * will fail with EWOULDBLOCK.
36531ae08745Sheppo  *
36541ae08745Sheppo  * If the unregister fails we do a cv_timedwait. We will either be signaled
36551ae08745Sheppo  * by the callback as it is exiting (note we have to wait a short period to
36561ae08745Sheppo  * allow the callback to return fully to the ldc framework and it to clear
36571ae08745Sheppo  * the active flag), or by the timer expiring. In either case we again attempt
36581ae08745Sheppo  * the unregister. We repeat this until we can succesfully unregister the
36591ae08745Sheppo  * callback.
36601ae08745Sheppo  *
36611ae08745Sheppo  * The reason we use a cv_timedwait rather than a simple cv_wait is to catch
36621ae08745Sheppo  * the case where the callback has finished but the ldc framework has not yet
36631ae08745Sheppo  * cleared the active flag. In this case we would never get a cv_signal.
36641ae08745Sheppo  */
36651ae08745Sheppo static int
36661ae08745Sheppo vsw_drain_ldcs(vsw_port_t *port)
36671ae08745Sheppo {
36681ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
36691ae08745Sheppo 	vsw_ldc_t	*ldcp;
36701ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
36711ae08745Sheppo 
36721ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
36731ae08745Sheppo 
36741ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
36751ae08745Sheppo 
36761ae08745Sheppo 	ldcp = ldcl->head;
36771ae08745Sheppo 
36781ae08745Sheppo 	for (; ldcp  != NULL; ldcp = ldcp->ldc_next) {
36791ae08745Sheppo 		/*
36801ae08745Sheppo 		 * If we can unregister the channel callback then we
36811ae08745Sheppo 		 * know that there is no callback either running or
36821ae08745Sheppo 		 * scheduled to run for this channel so move on to next
36831ae08745Sheppo 		 * channel in the list.
36841ae08745Sheppo 		 */
36851ae08745Sheppo 		mutex_enter(&ldcp->drain_cv_lock);
36861ae08745Sheppo 
36871ae08745Sheppo 		/* prompt active callbacks to quit */
36881ae08745Sheppo 		ldcp->drain_state = VSW_LDC_DRAINING;
36891ae08745Sheppo 
36901ae08745Sheppo 		if ((ldc_unreg_callback(ldcp->ldc_handle)) == 0) {
36911ae08745Sheppo 			D2(vswp, "%s: unreg callback for chan %ld", __func__,
36921ae08745Sheppo 				ldcp->ldc_id);
36931ae08745Sheppo 			mutex_exit(&ldcp->drain_cv_lock);
36941ae08745Sheppo 			continue;
36951ae08745Sheppo 		} else {
36961ae08745Sheppo 			/*
36971ae08745Sheppo 			 * If we end up here we know that either 1) a callback
36981ae08745Sheppo 			 * is currently executing, 2) is about to start (i.e.
36991ae08745Sheppo 			 * the ldc framework has set the active flag but
37001ae08745Sheppo 			 * has not actually invoked the callback yet, or 3)
37011ae08745Sheppo 			 * has finished and has returned to the ldc framework
37021ae08745Sheppo 			 * but the ldc framework has not yet cleared the
37031ae08745Sheppo 			 * active bit.
37041ae08745Sheppo 			 *
37051ae08745Sheppo 			 * Wait for it to finish.
37061ae08745Sheppo 			 */
37071ae08745Sheppo 			while (ldc_unreg_callback(ldcp->ldc_handle)
37081ae08745Sheppo 								== EWOULDBLOCK)
37091ae08745Sheppo 				(void) cv_timedwait(&ldcp->drain_cv,
37101ae08745Sheppo 					&ldcp->drain_cv_lock, lbolt + hz);
37111ae08745Sheppo 
37121ae08745Sheppo 			mutex_exit(&ldcp->drain_cv_lock);
37131ae08745Sheppo 			D2(vswp, "%s: unreg callback for chan %ld after "
37141ae08745Sheppo 				"timeout", __func__, ldcp->ldc_id);
37151ae08745Sheppo 		}
37161ae08745Sheppo 	}
37171ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
37181ae08745Sheppo 
37191ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
37201ae08745Sheppo 	return (0);
37211ae08745Sheppo }
37221ae08745Sheppo 
37231ae08745Sheppo /*
37241ae08745Sheppo  * Wait until all tasks which reference this port have completed.
37251ae08745Sheppo  *
37261ae08745Sheppo  * Prior to this function being invoked each channel under this port
37271ae08745Sheppo  * should have been quiesced via ldc_set_cb_mode(DISABLE).
37281ae08745Sheppo  */
37291ae08745Sheppo static int
37301ae08745Sheppo vsw_drain_port_taskq(vsw_port_t *port)
37311ae08745Sheppo {
37321ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
37331ae08745Sheppo 
37341ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
37351ae08745Sheppo 
37361ae08745Sheppo 	/*
37371ae08745Sheppo 	 * Mark the port as in the process of being detached, and
37381ae08745Sheppo 	 * dispatch a marker task to the queue so we know when all
37391ae08745Sheppo 	 * relevant tasks have completed.
37401ae08745Sheppo 	 */
37411ae08745Sheppo 	mutex_enter(&port->state_lock);
37421ae08745Sheppo 	port->state = VSW_PORT_DETACHING;
37431ae08745Sheppo 
37441ae08745Sheppo 	if ((vswp->taskq_p == NULL) ||
37451ae08745Sheppo 		(ddi_taskq_dispatch(vswp->taskq_p, vsw_marker_task,
37461ae08745Sheppo 			port, DDI_NOSLEEP) != DDI_SUCCESS)) {
37471ae08745Sheppo 		DERR(vswp, "%s: unable to dispatch marker task",
37481ae08745Sheppo 			__func__);
37491ae08745Sheppo 		mutex_exit(&port->state_lock);
37501ae08745Sheppo 		return (1);
37511ae08745Sheppo 	}
37521ae08745Sheppo 
37531ae08745Sheppo 	/*
37541ae08745Sheppo 	 * Wait for the marker task to finish.
37551ae08745Sheppo 	 */
37561ae08745Sheppo 	while (port->state != VSW_PORT_DETACHABLE)
37571ae08745Sheppo 		cv_wait(&port->state_cv, &port->state_lock);
37581ae08745Sheppo 
37591ae08745Sheppo 	mutex_exit(&port->state_lock);
37601ae08745Sheppo 
37611ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
37621ae08745Sheppo 
37631ae08745Sheppo 	return (0);
37641ae08745Sheppo }
37651ae08745Sheppo 
37661ae08745Sheppo static void
37671ae08745Sheppo vsw_marker_task(void *arg)
37681ae08745Sheppo {
37691ae08745Sheppo 	vsw_port_t	*port = arg;
37701ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
37711ae08745Sheppo 
37721ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
37731ae08745Sheppo 
37741ae08745Sheppo 	mutex_enter(&port->state_lock);
37751ae08745Sheppo 
37761ae08745Sheppo 	/*
37771ae08745Sheppo 	 * No further tasks should be dispatched which reference
37781ae08745Sheppo 	 * this port so ok to mark it as safe to detach.
37791ae08745Sheppo 	 */
37801ae08745Sheppo 	port->state = VSW_PORT_DETACHABLE;
37811ae08745Sheppo 
37821ae08745Sheppo 	cv_signal(&port->state_cv);
37831ae08745Sheppo 
37841ae08745Sheppo 	mutex_exit(&port->state_lock);
37851ae08745Sheppo 
37861ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
37871ae08745Sheppo }
37881ae08745Sheppo 
37891ae08745Sheppo static vsw_port_t *
37901ae08745Sheppo vsw_lookup_port(vsw_t *vswp, int p_instance)
37911ae08745Sheppo {
37921ae08745Sheppo 	vsw_port_list_t *plist = &vswp->plist;
37931ae08745Sheppo 	vsw_port_t	*port;
37941ae08745Sheppo 
37951ae08745Sheppo 	for (port = plist->head; port != NULL; port = port->p_next) {
37961ae08745Sheppo 		if (port->p_instance == p_instance) {
37971ae08745Sheppo 			D2(vswp, "vsw_lookup_port: found p_instance\n");
37981ae08745Sheppo 			return (port);
37991ae08745Sheppo 		}
38001ae08745Sheppo 	}
38011ae08745Sheppo 
38021ae08745Sheppo 	return (NULL);
38031ae08745Sheppo }
38041ae08745Sheppo 
38051ae08745Sheppo /*
38061ae08745Sheppo  * Search for and remove the specified port from the port
38071ae08745Sheppo  * list. Returns 0 if able to locate and remove port, otherwise
38081ae08745Sheppo  * returns 1.
38091ae08745Sheppo  */
38101ae08745Sheppo static int
38111ae08745Sheppo vsw_plist_del_node(vsw_t *vswp, vsw_port_t *port)
38121ae08745Sheppo {
38131ae08745Sheppo 	vsw_port_list_t *plist = &vswp->plist;
38141ae08745Sheppo 	vsw_port_t	*curr_p, *prev_p;
38151ae08745Sheppo 
38161ae08745Sheppo 	if (plist->head == NULL)
38171ae08745Sheppo 		return (1);
38181ae08745Sheppo 
38191ae08745Sheppo 	curr_p = prev_p = plist->head;
38201ae08745Sheppo 
38211ae08745Sheppo 	while (curr_p != NULL) {
38221ae08745Sheppo 		if (curr_p == port) {
38231ae08745Sheppo 			if (prev_p == curr_p) {
38241ae08745Sheppo 				plist->head = curr_p->p_next;
38251ae08745Sheppo 			} else {
38261ae08745Sheppo 				prev_p->p_next = curr_p->p_next;
38271ae08745Sheppo 			}
38281ae08745Sheppo 			plist->num_ports--;
38291ae08745Sheppo 			break;
38301ae08745Sheppo 		} else {
38311ae08745Sheppo 			prev_p = curr_p;
38321ae08745Sheppo 			curr_p = curr_p->p_next;
38331ae08745Sheppo 		}
38341ae08745Sheppo 	}
38351ae08745Sheppo 	return (0);
38361ae08745Sheppo }
38371ae08745Sheppo 
38381ae08745Sheppo /*
38391ae08745Sheppo  * Interrupt handler for ldc messages.
38401ae08745Sheppo  */
38411ae08745Sheppo static uint_t
38421ae08745Sheppo vsw_ldc_cb(uint64_t event, caddr_t arg)
38431ae08745Sheppo {
38441ae08745Sheppo 	vsw_ldc_t	*ldcp = (vsw_ldc_t  *)arg;
38451ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
38461ae08745Sheppo 	ldc_status_t	lstatus;
38471ae08745Sheppo 	int		rv;
38481ae08745Sheppo 
38491ae08745Sheppo 	D1(vswp, "%s: enter: ldcid (%lld)\n", __func__, ldcp->ldc_id);
38501ae08745Sheppo 
38511ae08745Sheppo 	mutex_enter(&ldcp->ldc_cblock);
38521ae08745Sheppo 
38531ae08745Sheppo 	if ((ldcp->ldc_status == LDC_INIT) || (ldcp->ldc_handle == NULL)) {
38541ae08745Sheppo 		mutex_exit(&ldcp->ldc_cblock);
38551ae08745Sheppo 		return (LDC_SUCCESS);
38561ae08745Sheppo 	}
38571ae08745Sheppo 
38583af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
38593af08d82Slm66018 	lstatus = ldcp->ldc_status;
38603af08d82Slm66018 	rv = ldc_status(ldcp->ldc_handle, &ldcp->ldc_status);
38613af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
38623af08d82Slm66018 	if (rv != 0) {
386334683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read channel state",
386434683adeSsg70180 			vswp->instance);
38653af08d82Slm66018 		goto vsw_cb_exit;
38663af08d82Slm66018 	}
38673af08d82Slm66018 
38681ae08745Sheppo 	if (event & LDC_EVT_UP) {
38691ae08745Sheppo 		/*
38701ae08745Sheppo 		 * Channel has come up, get the state and then start
38711ae08745Sheppo 		 * the handshake.
38721ae08745Sheppo 		 */
38731ae08745Sheppo 		D2(vswp, "%s: id(%ld) event(%llx) UP: status(%ld)",
38743af08d82Slm66018 			__func__, ldcp->ldc_id, event, lstatus);
38753af08d82Slm66018 		D2(vswp, "%s: UP: old status %ld : cur status %ld",
38763af08d82Slm66018 			__func__, lstatus, ldcp->ldc_status);
38773af08d82Slm66018 		if ((ldcp->ldc_status != lstatus) &&
38783af08d82Slm66018 					(ldcp->ldc_status == LDC_UP)) {
3879*445b4c2eSsb155480 				ldcp->reset_active = 0;
38801ae08745Sheppo 				vsw_restart_handshake(ldcp);
38813af08d82Slm66018 		}
38821ae08745Sheppo 
38831ae08745Sheppo 		ASSERT((event & (LDC_EVT_RESET | LDC_EVT_DOWN)) == 0);
38841ae08745Sheppo 	}
38851ae08745Sheppo 
38861ae08745Sheppo 	if (event & LDC_EVT_READ) {
38871ae08745Sheppo 		/*
38881ae08745Sheppo 		 * Data available for reading.
38891ae08745Sheppo 		 */
38901ae08745Sheppo 		D2(vswp, "%s: id(ld) event(%llx) data READ",
38911ae08745Sheppo 				__func__, ldcp->ldc_id, event);
38921ae08745Sheppo 
38931ae08745Sheppo 		vsw_process_pkt(ldcp);
38941ae08745Sheppo 
38951ae08745Sheppo 		ASSERT((event & (LDC_EVT_RESET | LDC_EVT_DOWN)) == 0);
38961ae08745Sheppo 
38971ae08745Sheppo 		goto vsw_cb_exit;
38981ae08745Sheppo 	}
38991ae08745Sheppo 
39003af08d82Slm66018 	if (event & (LDC_EVT_DOWN | LDC_EVT_RESET)) {
39013af08d82Slm66018 		D2(vswp, "%s: id(%ld) event(%llx) DOWN/RESET",
39023af08d82Slm66018 					__func__, ldcp->ldc_id, event);
39033af08d82Slm66018 
39043af08d82Slm66018 		/* attempt to restart the connection */
39053af08d82Slm66018 		vsw_restart_ldc(ldcp);
39063af08d82Slm66018 
39073af08d82Slm66018 		/*
39083af08d82Slm66018 		 * vsw_restart_ldc() will attempt to bring the channel
39093af08d82Slm66018 		 * back up. Check here to see if that succeeded.
39103af08d82Slm66018 		 */
39113af08d82Slm66018 		mutex_enter(&ldcp->status_lock);
39123af08d82Slm66018 		lstatus = ldcp->ldc_status;
39133af08d82Slm66018 		rv = ldc_status(ldcp->ldc_handle, &ldcp->ldc_status);
39143af08d82Slm66018 		mutex_exit(&ldcp->status_lock);
39151ae08745Sheppo 		if (rv != 0) {
39163af08d82Slm66018 			DERR(vswp, "%s: unable to read status for channel %ld",
39173af08d82Slm66018 				__func__, ldcp->ldc_id);
39183af08d82Slm66018 			goto vsw_cb_exit;
39191ae08745Sheppo 		}
39201ae08745Sheppo 
39213af08d82Slm66018 		D2(vswp, "%s: id(%ld) event(%llx) DOWN/RESET event:"
39223af08d82Slm66018 			" old status %ld : cur status %ld", __func__,
39233af08d82Slm66018 			ldcp->ldc_id, event, lstatus, ldcp->ldc_status);
39243af08d82Slm66018 
39253af08d82Slm66018 		/*
39263af08d82Slm66018 		 * If channel was not previously UP then (re)start the
39273af08d82Slm66018 		 * handshake.
39283af08d82Slm66018 		 */
39293af08d82Slm66018 		if ((ldcp->ldc_status == LDC_UP) && (lstatus != LDC_UP)) {
39303af08d82Slm66018 			D2(vswp, "%s: channel %ld now UP, restarting "
39313af08d82Slm66018 				"handshake", __func__, ldcp->ldc_id);
3932*445b4c2eSsb155480 			ldcp->reset_active = 0;
39333af08d82Slm66018 			vsw_restart_handshake(ldcp);
39341ae08745Sheppo 		}
39351ae08745Sheppo 	}
39361ae08745Sheppo 
39371ae08745Sheppo 	/*
39381ae08745Sheppo 	 * Catch either LDC_EVT_WRITE which we don't support or any
39391ae08745Sheppo 	 * unknown event.
39401ae08745Sheppo 	 */
39411ae08745Sheppo 	if (event & ~(LDC_EVT_UP | LDC_EVT_RESET
39421ae08745Sheppo 					| LDC_EVT_DOWN | LDC_EVT_READ)) {
39431ae08745Sheppo 
39441ae08745Sheppo 		DERR(vswp, "%s: id(%ld) Unexpected event=(%llx) status(%ld)",
39451ae08745Sheppo 			__func__, ldcp->ldc_id, event, ldcp->ldc_status);
39461ae08745Sheppo 	}
39471ae08745Sheppo 
39481ae08745Sheppo vsw_cb_exit:
39491ae08745Sheppo 	mutex_exit(&ldcp->ldc_cblock);
39501ae08745Sheppo 
39511ae08745Sheppo 	/*
39521ae08745Sheppo 	 * Let the drain function know we are finishing if it
39531ae08745Sheppo 	 * is waiting.
39541ae08745Sheppo 	 */
39551ae08745Sheppo 	mutex_enter(&ldcp->drain_cv_lock);
39561ae08745Sheppo 	if (ldcp->drain_state == VSW_LDC_DRAINING)
39571ae08745Sheppo 		cv_signal(&ldcp->drain_cv);
39581ae08745Sheppo 	mutex_exit(&ldcp->drain_cv_lock);
39591ae08745Sheppo 
39601ae08745Sheppo 	return (LDC_SUCCESS);
39611ae08745Sheppo }
39621ae08745Sheppo 
39631ae08745Sheppo /*
39643af08d82Slm66018  * Restart the connection with our peer. Free any existing
39653af08d82Slm66018  * data structures and then attempt to bring channel back
39663af08d82Slm66018  * up.
39671ae08745Sheppo  */
39681ae08745Sheppo static void
39693af08d82Slm66018 vsw_restart_ldc(vsw_ldc_t *ldcp)
39701ae08745Sheppo {
39713af08d82Slm66018 	int		rv;
39721ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
39731ae08745Sheppo 	vsw_port_t	*port;
39741ae08745Sheppo 	vsw_ldc_list_t	*ldcl;
39751ae08745Sheppo 
39763af08d82Slm66018 	D1(vswp, "%s: enter", __func__);
39771ae08745Sheppo 
3978*445b4c2eSsb155480 	/*
3979*445b4c2eSsb155480 	 * Check if reset already in progress for this channel.
3980*445b4c2eSsb155480 	 */
3981*445b4c2eSsb155480 	if (ldstub((uint8_t *)&ldcp->reset_active))
3982*445b4c2eSsb155480 		return;
3983*445b4c2eSsb155480 
39841ae08745Sheppo 	port = ldcp->ldc_port;
39851ae08745Sheppo 	ldcl = &port->p_ldclist;
39861ae08745Sheppo 
39873af08d82Slm66018 	READ_ENTER(&ldcl->lockrw);
39881ae08745Sheppo 
39891ae08745Sheppo 	D2(vswp, "%s: in 0x%llx : out 0x%llx", __func__,
39901ae08745Sheppo 		ldcp->lane_in.lstate, ldcp->lane_out.lstate);
39911ae08745Sheppo 
39921ae08745Sheppo 	vsw_free_lane_resources(ldcp, INBOUND);
39931ae08745Sheppo 	vsw_free_lane_resources(ldcp, OUTBOUND);
39941ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
39951ae08745Sheppo 
39961ae08745Sheppo 	ldcp->lane_in.lstate = 0;
39971ae08745Sheppo 	ldcp->lane_out.lstate = 0;
39981ae08745Sheppo 
39991ae08745Sheppo 	/*
40001ae08745Sheppo 	 * Remove parent port from any multicast groups
40011ae08745Sheppo 	 * it may have registered with. Client must resend
40021ae08745Sheppo 	 * multicast add command after handshake completes.
40031ae08745Sheppo 	 */
40041ae08745Sheppo 	(void) vsw_del_fdb(vswp, port);
40051ae08745Sheppo 
40061ae08745Sheppo 	vsw_del_mcst_port(port);
40071ae08745Sheppo 
40081ae08745Sheppo 	ldcp->peer_session = 0;
40091ae08745Sheppo 	ldcp->session_status = 0;
40103af08d82Slm66018 	ldcp->hcnt = 0;
40113af08d82Slm66018 	ldcp->hphase = VSW_MILESTONE0;
40123af08d82Slm66018 
40133af08d82Slm66018 	rv = ldc_up(ldcp->ldc_handle);
40143af08d82Slm66018 	if (rv != 0) {
40153af08d82Slm66018 		/*
40163af08d82Slm66018 		 * Not a fatal error for ldc_up() to fail, as peer
40173af08d82Slm66018 		 * end point may simply not be ready yet.
40183af08d82Slm66018 		 */
40193af08d82Slm66018 		D2(vswp, "%s: ldc_up err id(%lld) rv(%d)", __func__,
40203af08d82Slm66018 			ldcp->ldc_id, rv);
40213af08d82Slm66018 	}
40223af08d82Slm66018 
40233af08d82Slm66018 	D1(vswp, "%s: exit", __func__);
40243af08d82Slm66018 }
40253af08d82Slm66018 
40263af08d82Slm66018 /*
40273af08d82Slm66018  * (Re)start a handshake with our peer by sending them
40283af08d82Slm66018  * our version info.
40293af08d82Slm66018  */
40303af08d82Slm66018 static void
40313af08d82Slm66018 vsw_restart_handshake(vsw_ldc_t *ldcp)
40323af08d82Slm66018 {
40333af08d82Slm66018 	vsw_t		*vswp = ldcp->ldc_vswp;
40343af08d82Slm66018 
40353af08d82Slm66018 	D1(vswp, "vsw_restart_handshake: enter");
40363af08d82Slm66018 
40373af08d82Slm66018 	if (ldcp->hphase != VSW_MILESTONE0) {
40383af08d82Slm66018 		vsw_restart_ldc(ldcp);
40393af08d82Slm66018 	}
40401ae08745Sheppo 
40411ae08745Sheppo 	/*
40421ae08745Sheppo 	 * We now increment the transaction group id. This allows
40431ae08745Sheppo 	 * us to identify and disard any tasks which are still pending
40441ae08745Sheppo 	 * on the taskq and refer to the handshake session we are about
40451ae08745Sheppo 	 * to restart. These stale messages no longer have any real
40461ae08745Sheppo 	 * meaning.
40471ae08745Sheppo 	 */
40481ae08745Sheppo 	mutex_enter(&ldcp->hss_lock);
40491ae08745Sheppo 	ldcp->hss_id++;
40501ae08745Sheppo 	mutex_exit(&ldcp->hss_lock);
40511ae08745Sheppo 
40521ae08745Sheppo 	if (ldcp->hcnt++ > vsw_num_handshakes) {
405334683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: exceeded number of permitted "
40541ae08745Sheppo 			"handshake attempts (%d) on channel %ld",
405534683adeSsg70180 			vswp->instance, ldcp->hcnt, ldcp->ldc_id);
40561ae08745Sheppo 		return;
40571ae08745Sheppo 	}
40581ae08745Sheppo 
40593af08d82Slm66018 	if ((vswp->taskq_p == NULL) ||
40603af08d82Slm66018 		(ddi_taskq_dispatch(vswp->taskq_p, vsw_send_ver, ldcp,
40613af08d82Slm66018 			DDI_NOSLEEP) != DDI_SUCCESS)) {
406234683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Can't dispatch version handshake "
406334683adeSsg70180 			"task", vswp->instance);
40643af08d82Slm66018 	}
40651ae08745Sheppo 
40661ae08745Sheppo 	D1(vswp, "vsw_restart_handshake: exit");
40671ae08745Sheppo }
40681ae08745Sheppo 
40691ae08745Sheppo /*
40703af08d82Slm66018  * Deal appropriately with a ECONNRESET event encountered in a ldc_*
40713af08d82Slm66018  * call.
40723af08d82Slm66018  */
40733af08d82Slm66018 static void
40743af08d82Slm66018 vsw_handle_reset(vsw_ldc_t *ldcp)
40753af08d82Slm66018 {
40763af08d82Slm66018 	vsw_t		*vswp = ldcp->ldc_vswp;
40773af08d82Slm66018 	ldc_status_t	lstatus;
40783af08d82Slm66018 
40793af08d82Slm66018 	D1(vswp, "%s: enter", __func__);
40803af08d82Slm66018 
40813af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
40823af08d82Slm66018 	lstatus = ldcp->ldc_status;
40833af08d82Slm66018 	if (ldc_status(ldcp->ldc_handle, &ldcp->ldc_status) != 0) {
40843af08d82Slm66018 		DERR(vswp, "%s: unable to read status for channel %ld",
40853af08d82Slm66018 			__func__, ldcp->ldc_id);
40863af08d82Slm66018 		mutex_exit(&ldcp->status_lock);
40873af08d82Slm66018 		return;
40883af08d82Slm66018 	}
40893af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
40903af08d82Slm66018 
40913af08d82Slm66018 	/*
40923af08d82Slm66018 	 * Check the channel's previous recorded state to
40933af08d82Slm66018 	 * determine if this is the first ECONNRESET event
40943af08d82Slm66018 	 * we've gotten for this particular channel (i.e. was
40953af08d82Slm66018 	 * previously up but is no longer). If so, terminate
40963af08d82Slm66018 	 * the channel.
40973af08d82Slm66018 	 */
40983af08d82Slm66018 	if ((ldcp->ldc_status != LDC_UP) && (lstatus == LDC_UP)) {
40993af08d82Slm66018 		vsw_restart_ldc(ldcp);
41003af08d82Slm66018 	}
41013af08d82Slm66018 
41023af08d82Slm66018 	/*
41033af08d82Slm66018 	 * vsw_restart_ldc() will also attempt to bring channel
41043af08d82Slm66018 	 * back up. Check here if that succeeds.
41053af08d82Slm66018 	 */
41063af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
41073af08d82Slm66018 	lstatus = ldcp->ldc_status;
41083af08d82Slm66018 	if (ldc_status(ldcp->ldc_handle, &ldcp->ldc_status) != 0) {
41093af08d82Slm66018 		DERR(vswp, "%s: unable to read status for channel %ld",
41103af08d82Slm66018 			__func__, ldcp->ldc_id);
41113af08d82Slm66018 		mutex_exit(&ldcp->status_lock);
41123af08d82Slm66018 		return;
41133af08d82Slm66018 	}
41143af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
41153af08d82Slm66018 
41163af08d82Slm66018 	/*
41173af08d82Slm66018 	 * If channel is now up and no one else (i.e. the callback routine)
41183af08d82Slm66018 	 * has dealt with it then we restart the handshake here.
41193af08d82Slm66018 	 */
41203af08d82Slm66018 	if ((lstatus != LDC_UP) && (ldcp->ldc_status == LDC_UP)) {
4121*445b4c2eSsb155480 		ldcp->reset_active = 0;
41223af08d82Slm66018 		vsw_restart_handshake(ldcp);
41233af08d82Slm66018 	}
41243af08d82Slm66018 
41253af08d82Slm66018 	D1(vswp, "%s: exit", __func__);
41263af08d82Slm66018 }
41273af08d82Slm66018 
41283af08d82Slm66018 /*
41291ae08745Sheppo  * returns 0 if legal for event signified by flag to have
41301ae08745Sheppo  * occured at the time it did. Otherwise returns 1.
41311ae08745Sheppo  */
41321ae08745Sheppo int
41331ae08745Sheppo vsw_check_flag(vsw_ldc_t *ldcp, int dir, uint64_t flag)
41341ae08745Sheppo {
41351ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
41361ae08745Sheppo 	uint64_t	state;
41371ae08745Sheppo 	uint64_t	phase;
41381ae08745Sheppo 
41391ae08745Sheppo 	if (dir == INBOUND)
41401ae08745Sheppo 		state = ldcp->lane_in.lstate;
41411ae08745Sheppo 	else
41421ae08745Sheppo 		state = ldcp->lane_out.lstate;
41431ae08745Sheppo 
41441ae08745Sheppo 	phase = ldcp->hphase;
41451ae08745Sheppo 
41461ae08745Sheppo 	switch (flag) {
41471ae08745Sheppo 	case VSW_VER_INFO_RECV:
41481ae08745Sheppo 		if (phase > VSW_MILESTONE0) {
41491ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): VER_INFO_RECV"
41501ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
41511ae08745Sheppo 			vsw_restart_handshake(ldcp);
41521ae08745Sheppo 			return (1);
41531ae08745Sheppo 		}
41541ae08745Sheppo 		break;
41551ae08745Sheppo 
41561ae08745Sheppo 	case VSW_VER_ACK_RECV:
41571ae08745Sheppo 	case VSW_VER_NACK_RECV:
41581ae08745Sheppo 		if (!(state & VSW_VER_INFO_SENT)) {
41591ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious VER_ACK"
41601ae08745Sheppo 				" or VER_NACK when in state %d\n",
41611ae08745Sheppo 				ldcp->ldc_id, phase);
41621ae08745Sheppo 			vsw_restart_handshake(ldcp);
41631ae08745Sheppo 			return (1);
41641ae08745Sheppo 		} else
41651ae08745Sheppo 			state &= ~VSW_VER_INFO_SENT;
41661ae08745Sheppo 		break;
41671ae08745Sheppo 
41681ae08745Sheppo 	case VSW_ATTR_INFO_RECV:
41691ae08745Sheppo 		if ((phase < VSW_MILESTONE1) || (phase >= VSW_MILESTONE2)) {
41701ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): ATTR_INFO_RECV"
41711ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
41721ae08745Sheppo 			vsw_restart_handshake(ldcp);
41731ae08745Sheppo 			return (1);
41741ae08745Sheppo 		}
41751ae08745Sheppo 		break;
41761ae08745Sheppo 
41771ae08745Sheppo 	case VSW_ATTR_ACK_RECV:
41781ae08745Sheppo 	case VSW_ATTR_NACK_RECV:
41791ae08745Sheppo 		if (!(state & VSW_ATTR_INFO_SENT)) {
41801ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious ATTR_ACK"
41811ae08745Sheppo 				" or ATTR_NACK when in state %d\n",
41821ae08745Sheppo 				ldcp->ldc_id, phase);
41831ae08745Sheppo 			vsw_restart_handshake(ldcp);
41841ae08745Sheppo 			return (1);
41851ae08745Sheppo 		} else
41861ae08745Sheppo 			state &= ~VSW_ATTR_INFO_SENT;
41871ae08745Sheppo 		break;
41881ae08745Sheppo 
41891ae08745Sheppo 	case VSW_DRING_INFO_RECV:
41901ae08745Sheppo 		if (phase < VSW_MILESTONE1) {
41911ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): DRING_INFO_RECV"
41921ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
41931ae08745Sheppo 			vsw_restart_handshake(ldcp);
41941ae08745Sheppo 			return (1);
41951ae08745Sheppo 		}
41961ae08745Sheppo 		break;
41971ae08745Sheppo 
41981ae08745Sheppo 	case VSW_DRING_ACK_RECV:
41991ae08745Sheppo 	case VSW_DRING_NACK_RECV:
42001ae08745Sheppo 		if (!(state & VSW_DRING_INFO_SENT)) {
42011ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious DRING_ACK"
42021ae08745Sheppo 				" or DRING_NACK when in state %d\n",
42031ae08745Sheppo 				ldcp->ldc_id, phase);
42041ae08745Sheppo 			vsw_restart_handshake(ldcp);
42051ae08745Sheppo 			return (1);
42061ae08745Sheppo 		} else
42071ae08745Sheppo 			state &= ~VSW_DRING_INFO_SENT;
42081ae08745Sheppo 		break;
42091ae08745Sheppo 
42101ae08745Sheppo 	case VSW_RDX_INFO_RECV:
42111ae08745Sheppo 		if (phase < VSW_MILESTONE3) {
42121ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): RDX_INFO_RECV"
42131ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
42141ae08745Sheppo 			vsw_restart_handshake(ldcp);
42151ae08745Sheppo 			return (1);
42161ae08745Sheppo 		}
42171ae08745Sheppo 		break;
42181ae08745Sheppo 
42191ae08745Sheppo 	case VSW_RDX_ACK_RECV:
42201ae08745Sheppo 	case VSW_RDX_NACK_RECV:
42211ae08745Sheppo 		if (!(state & VSW_RDX_INFO_SENT)) {
42221ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious RDX_ACK"
42231ae08745Sheppo 				" or RDX_NACK when in state %d\n",
42241ae08745Sheppo 				ldcp->ldc_id, phase);
42251ae08745Sheppo 			vsw_restart_handshake(ldcp);
42261ae08745Sheppo 			return (1);
42271ae08745Sheppo 		} else
42281ae08745Sheppo 			state &= ~VSW_RDX_INFO_SENT;
42291ae08745Sheppo 		break;
42301ae08745Sheppo 
42311ae08745Sheppo 	case VSW_MCST_INFO_RECV:
42321ae08745Sheppo 		if (phase < VSW_MILESTONE3) {
42331ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): VSW_MCST_INFO_RECV"
42341ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
42351ae08745Sheppo 			vsw_restart_handshake(ldcp);
42361ae08745Sheppo 			return (1);
42371ae08745Sheppo 		}
42381ae08745Sheppo 		break;
42391ae08745Sheppo 
42401ae08745Sheppo 	default:
42411ae08745Sheppo 		DERR(vswp, "vsw_check_flag (%lld): unknown flag (%llx)",
42421ae08745Sheppo 				ldcp->ldc_id, flag);
42431ae08745Sheppo 		return (1);
42441ae08745Sheppo 	}
42451ae08745Sheppo 
42461ae08745Sheppo 	if (dir == INBOUND)
42471ae08745Sheppo 		ldcp->lane_in.lstate = state;
42481ae08745Sheppo 	else
42491ae08745Sheppo 		ldcp->lane_out.lstate = state;
42501ae08745Sheppo 
42511ae08745Sheppo 	D1(vswp, "vsw_check_flag (chan %lld): exit", ldcp->ldc_id);
42521ae08745Sheppo 
42531ae08745Sheppo 	return (0);
42541ae08745Sheppo }
42551ae08745Sheppo 
42561ae08745Sheppo void
42571ae08745Sheppo vsw_next_milestone(vsw_ldc_t *ldcp)
42581ae08745Sheppo {
42591ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
42601ae08745Sheppo 
42611ae08745Sheppo 	D1(vswp, "%s (chan %lld): enter (phase %ld)", __func__,
42621ae08745Sheppo 		ldcp->ldc_id, ldcp->hphase);
42631ae08745Sheppo 
42641ae08745Sheppo 	DUMP_FLAGS(ldcp->lane_in.lstate);
42651ae08745Sheppo 	DUMP_FLAGS(ldcp->lane_out.lstate);
42661ae08745Sheppo 
42671ae08745Sheppo 	switch (ldcp->hphase) {
42681ae08745Sheppo 
42691ae08745Sheppo 	case VSW_MILESTONE0:
42701ae08745Sheppo 		/*
42711ae08745Sheppo 		 * If we haven't started to handshake with our peer,
42721ae08745Sheppo 		 * start to do so now.
42731ae08745Sheppo 		 */
42741ae08745Sheppo 		if (ldcp->lane_out.lstate == 0) {
42751ae08745Sheppo 			D2(vswp, "%s: (chan %lld) starting handshake "
42761ae08745Sheppo 				"with peer", __func__, ldcp->ldc_id);
42771ae08745Sheppo 			vsw_restart_handshake(ldcp);
42781ae08745Sheppo 		}
42791ae08745Sheppo 
42801ae08745Sheppo 		/*
42811ae08745Sheppo 		 * Only way to pass this milestone is to have successfully
42821ae08745Sheppo 		 * negotiated version info.
42831ae08745Sheppo 		 */
42841ae08745Sheppo 		if ((ldcp->lane_in.lstate & VSW_VER_ACK_SENT) &&
42851ae08745Sheppo 			(ldcp->lane_out.lstate & VSW_VER_ACK_RECV)) {
42861ae08745Sheppo 
42871ae08745Sheppo 			D2(vswp, "%s: (chan %lld) leaving milestone 0",
42881ae08745Sheppo 				__func__, ldcp->ldc_id);
42891ae08745Sheppo 
42901ae08745Sheppo 			/*
42911ae08745Sheppo 			 * Next milestone is passed when attribute
42921ae08745Sheppo 			 * information has been successfully exchanged.
42931ae08745Sheppo 			 */
42941ae08745Sheppo 			ldcp->hphase = VSW_MILESTONE1;
42951ae08745Sheppo 			vsw_send_attr(ldcp);
42961ae08745Sheppo 
42971ae08745Sheppo 		}
42981ae08745Sheppo 		break;
42991ae08745Sheppo 
43001ae08745Sheppo 	case VSW_MILESTONE1:
43011ae08745Sheppo 		/*
43021ae08745Sheppo 		 * Only way to pass this milestone is to have successfully
43031ae08745Sheppo 		 * negotiated attribute information.
43041ae08745Sheppo 		 */
43051ae08745Sheppo 		if (ldcp->lane_in.lstate & VSW_ATTR_ACK_SENT) {
43061ae08745Sheppo 
43071ae08745Sheppo 			ldcp->hphase = VSW_MILESTONE2;
43081ae08745Sheppo 
43091ae08745Sheppo 			/*
43101ae08745Sheppo 			 * If the peer device has said it wishes to
43111ae08745Sheppo 			 * use descriptor rings then we send it our ring
43121ae08745Sheppo 			 * info, otherwise we just set up a private ring
43131ae08745Sheppo 			 * which we use an internal buffer
43141ae08745Sheppo 			 */
43151ae08745Sheppo 			if (ldcp->lane_in.xfer_mode == VIO_DRING_MODE)
43161ae08745Sheppo 				vsw_send_dring_info(ldcp);
43171ae08745Sheppo 		}
43181ae08745Sheppo 		break;
43191ae08745Sheppo 
43201ae08745Sheppo 	case VSW_MILESTONE2:
43211ae08745Sheppo 		/*
43221ae08745Sheppo 		 * If peer has indicated in its attribute message that
43231ae08745Sheppo 		 * it wishes to use descriptor rings then the only way
43241ae08745Sheppo 		 * to pass this milestone is for us to have received
43251ae08745Sheppo 		 * valid dring info.
43261ae08745Sheppo 		 *
43271ae08745Sheppo 		 * If peer is not using descriptor rings then just fall
43281ae08745Sheppo 		 * through.
43291ae08745Sheppo 		 */
43301ae08745Sheppo 		if ((ldcp->lane_in.xfer_mode == VIO_DRING_MODE) &&
43311ae08745Sheppo 			(!(ldcp->lane_in.lstate & VSW_DRING_ACK_SENT)))
43321ae08745Sheppo 			break;
43331ae08745Sheppo 
43341ae08745Sheppo 		D2(vswp, "%s: (chan %lld) leaving milestone 2",
43351ae08745Sheppo 				__func__, ldcp->ldc_id);
43361ae08745Sheppo 
43371ae08745Sheppo 		ldcp->hphase = VSW_MILESTONE3;
43381ae08745Sheppo 		vsw_send_rdx(ldcp);
43391ae08745Sheppo 		break;
43401ae08745Sheppo 
43411ae08745Sheppo 	case VSW_MILESTONE3:
43421ae08745Sheppo 		/*
43431ae08745Sheppo 		 * Pass this milestone when all paramaters have been
43441ae08745Sheppo 		 * successfully exchanged and RDX sent in both directions.
43451ae08745Sheppo 		 *
43461ae08745Sheppo 		 * Mark outbound lane as available to transmit data.
43471ae08745Sheppo 		 */
43481ae08745Sheppo 		if ((ldcp->lane_in.lstate & VSW_RDX_ACK_SENT) &&
43491ae08745Sheppo 			(ldcp->lane_out.lstate & VSW_RDX_ACK_RECV)) {
43501ae08745Sheppo 
43511ae08745Sheppo 			D2(vswp, "%s: (chan %lld) leaving milestone 3",
43521ae08745Sheppo 				__func__, ldcp->ldc_id);
43533af08d82Slm66018 			D2(vswp, "%s: ** handshake complete (0x%llx : "
43543af08d82Slm66018 				"0x%llx) **", __func__, ldcp->lane_in.lstate,
43553af08d82Slm66018 				ldcp->lane_out.lstate);
43561ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_LANE_ACTIVE;
43571ae08745Sheppo 			ldcp->hphase = VSW_MILESTONE4;
43581ae08745Sheppo 			ldcp->hcnt = 0;
43591ae08745Sheppo 			DISPLAY_STATE();
43603af08d82Slm66018 		} else {
43613af08d82Slm66018 			D2(vswp, "%s: still in milestone 3 (0x%llx :"
43623af08d82Slm66018 				" 0x%llx", __func__, ldcp->lane_in.lstate,
43633af08d82Slm66018 				ldcp->lane_out.lstate);
43641ae08745Sheppo 		}
43651ae08745Sheppo 		break;
43661ae08745Sheppo 
43671ae08745Sheppo 	case VSW_MILESTONE4:
43681ae08745Sheppo 		D2(vswp, "%s: (chan %lld) in milestone 4", __func__,
43691ae08745Sheppo 							ldcp->ldc_id);
43701ae08745Sheppo 		break;
43711ae08745Sheppo 
43721ae08745Sheppo 	default:
43731ae08745Sheppo 		DERR(vswp, "%s: (chan %lld) Unknown Phase %x", __func__,
43741ae08745Sheppo 			ldcp->ldc_id, ldcp->hphase);
43751ae08745Sheppo 	}
43761ae08745Sheppo 
43771ae08745Sheppo 	D1(vswp, "%s (chan %lld): exit (phase %ld)", __func__, ldcp->ldc_id,
43781ae08745Sheppo 		ldcp->hphase);
43791ae08745Sheppo }
43801ae08745Sheppo 
43811ae08745Sheppo /*
43821ae08745Sheppo  * Check if major version is supported.
43831ae08745Sheppo  *
43841ae08745Sheppo  * Returns 0 if finds supported major number, and if necessary
43851ae08745Sheppo  * adjusts the minor field.
43861ae08745Sheppo  *
43871ae08745Sheppo  * Returns 1 if can't match major number exactly. Sets mjor/minor
43881ae08745Sheppo  * to next lowest support values, or to zero if no other values possible.
43891ae08745Sheppo  */
43901ae08745Sheppo static int
43911ae08745Sheppo vsw_supported_version(vio_ver_msg_t *vp)
43921ae08745Sheppo {
43931ae08745Sheppo 	int	i;
43941ae08745Sheppo 
43951ae08745Sheppo 	D1(NULL, "vsw_supported_version: enter");
43961ae08745Sheppo 
43971ae08745Sheppo 	for (i = 0; i < VSW_NUM_VER; i++) {
43981ae08745Sheppo 		if (vsw_versions[i].ver_major == vp->ver_major) {
43991ae08745Sheppo 			/*
44001ae08745Sheppo 			 * Matching or lower major version found. Update
44011ae08745Sheppo 			 * minor number if necessary.
44021ae08745Sheppo 			 */
44031ae08745Sheppo 			if (vp->ver_minor > vsw_versions[i].ver_minor) {
44041ae08745Sheppo 				D2(NULL, "%s: adjusting minor value"
44051ae08745Sheppo 					" from %d to %d", __func__,
44061ae08745Sheppo 					vp->ver_minor,
44071ae08745Sheppo 					vsw_versions[i].ver_minor);
44081ae08745Sheppo 				vp->ver_minor = vsw_versions[i].ver_minor;
44091ae08745Sheppo 			}
44101ae08745Sheppo 
44111ae08745Sheppo 			return (0);
44121ae08745Sheppo 		}
44131ae08745Sheppo 
44141ae08745Sheppo 		if (vsw_versions[i].ver_major < vp->ver_major) {
44151ae08745Sheppo 			if (vp->ver_minor > vsw_versions[i].ver_minor) {
44161ae08745Sheppo 				D2(NULL, "%s: adjusting minor value"
44171ae08745Sheppo 					" from %d to %d", __func__,
44181ae08745Sheppo 					vp->ver_minor,
44191ae08745Sheppo 					vsw_versions[i].ver_minor);
44201ae08745Sheppo 				vp->ver_minor = vsw_versions[i].ver_minor;
44211ae08745Sheppo 			}
44221ae08745Sheppo 			return (1);
44231ae08745Sheppo 		}
44241ae08745Sheppo 	}
44251ae08745Sheppo 
44261ae08745Sheppo 	/* No match was possible, zero out fields */
44271ae08745Sheppo 	vp->ver_major = 0;
44281ae08745Sheppo 	vp->ver_minor = 0;
44291ae08745Sheppo 
44301ae08745Sheppo 	D1(NULL, "vsw_supported_version: exit");
44311ae08745Sheppo 
44321ae08745Sheppo 	return (1);
44331ae08745Sheppo }
44341ae08745Sheppo 
44351ae08745Sheppo /*
44361ae08745Sheppo  * Main routine for processing messages received over LDC.
44371ae08745Sheppo  */
44381ae08745Sheppo static void
44391ae08745Sheppo vsw_process_pkt(void *arg)
44401ae08745Sheppo {
44411ae08745Sheppo 	vsw_ldc_t	*ldcp = (vsw_ldc_t  *)arg;
44421ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
44431ae08745Sheppo 	size_t		msglen;
44441ae08745Sheppo 	vio_msg_tag_t	tag;
44451ae08745Sheppo 	def_msg_t	dmsg;
44461ae08745Sheppo 	int 		rv = 0;
44471ae08745Sheppo 
44483af08d82Slm66018 
44491ae08745Sheppo 	D1(vswp, "%s enter: ldcid (%lld)\n", __func__, ldcp->ldc_id);
44501ae08745Sheppo 
44511ae08745Sheppo 	/*
44521ae08745Sheppo 	 * If channel is up read messages until channel is empty.
44531ae08745Sheppo 	 */
44541ae08745Sheppo 	do {
44551ae08745Sheppo 		msglen = sizeof (dmsg);
44561ae08745Sheppo 		rv = ldc_read(ldcp->ldc_handle, (caddr_t)&dmsg, &msglen);
44571ae08745Sheppo 
44581ae08745Sheppo 		if (rv != 0) {
44591ae08745Sheppo 			DERR(vswp, "%s :ldc_read err id(%lld) rv(%d) "
44601ae08745Sheppo 				"len(%d)\n", __func__, ldcp->ldc_id,
44611ae08745Sheppo 							rv, msglen);
44623af08d82Slm66018 		}
44633af08d82Slm66018 
44643af08d82Slm66018 		/* channel has been reset */
44653af08d82Slm66018 		if (rv == ECONNRESET) {
44663af08d82Slm66018 			vsw_handle_reset(ldcp);
44671ae08745Sheppo 			break;
44681ae08745Sheppo 		}
44691ae08745Sheppo 
44701ae08745Sheppo 		if (msglen == 0) {
44711ae08745Sheppo 			D2(vswp, "%s: ldc_read id(%lld) NODATA", __func__,
44721ae08745Sheppo 			ldcp->ldc_id);
44731ae08745Sheppo 			break;
44741ae08745Sheppo 		}
44751ae08745Sheppo 
44761ae08745Sheppo 		D2(vswp, "%s: ldc_read id(%lld): msglen(%d)", __func__,
44771ae08745Sheppo 		    ldcp->ldc_id, msglen);
44781ae08745Sheppo 
44791ae08745Sheppo 		/*
44801ae08745Sheppo 		 * Figure out what sort of packet we have gotten by
44811ae08745Sheppo 		 * examining the msg tag, and then switch it appropriately.
44821ae08745Sheppo 		 */
44831ae08745Sheppo 		bcopy(&dmsg, &tag, sizeof (vio_msg_tag_t));
44841ae08745Sheppo 
44851ae08745Sheppo 		switch (tag.vio_msgtype) {
44861ae08745Sheppo 		case VIO_TYPE_CTRL:
44871ae08745Sheppo 			vsw_dispatch_ctrl_task(ldcp, &dmsg, tag);
44881ae08745Sheppo 			break;
44891ae08745Sheppo 		case VIO_TYPE_DATA:
44901ae08745Sheppo 			vsw_process_data_pkt(ldcp, &dmsg, tag);
44911ae08745Sheppo 			break;
44921ae08745Sheppo 		case VIO_TYPE_ERR:
44931ae08745Sheppo 			vsw_process_err_pkt(ldcp, &dmsg, tag);
44941ae08745Sheppo 			break;
44951ae08745Sheppo 		default:
44961ae08745Sheppo 			DERR(vswp, "%s: Unknown tag(%lx) ", __func__,
44971ae08745Sheppo 				"id(%lx)\n", tag.vio_msgtype, ldcp->ldc_id);
44981ae08745Sheppo 			break;
44991ae08745Sheppo 		}
45001ae08745Sheppo 	} while (msglen);
45011ae08745Sheppo 
45021ae08745Sheppo 	D1(vswp, "%s exit: ldcid (%lld)\n", __func__, ldcp->ldc_id);
45031ae08745Sheppo }
45041ae08745Sheppo 
45051ae08745Sheppo /*
45061ae08745Sheppo  * Dispatch a task to process a VIO control message.
45071ae08745Sheppo  */
45081ae08745Sheppo static void
45091ae08745Sheppo vsw_dispatch_ctrl_task(vsw_ldc_t *ldcp, void *cpkt, vio_msg_tag_t tag)
45101ae08745Sheppo {
45111ae08745Sheppo 	vsw_ctrl_task_t		*ctaskp = NULL;
45121ae08745Sheppo 	vsw_port_t		*port = ldcp->ldc_port;
45131ae08745Sheppo 	vsw_t			*vswp = port->p_vswp;
45141ae08745Sheppo 
45151ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
45161ae08745Sheppo 
45171ae08745Sheppo 	/*
45181ae08745Sheppo 	 * We need to handle RDX ACK messages in-band as once they
45191ae08745Sheppo 	 * are exchanged it is possible that we will get an
45201ae08745Sheppo 	 * immediate (legitimate) data packet.
45211ae08745Sheppo 	 */
45221ae08745Sheppo 	if ((tag.vio_subtype_env == VIO_RDX) &&
45231ae08745Sheppo 		(tag.vio_subtype == VIO_SUBTYPE_ACK)) {
45243af08d82Slm66018 
45251ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_RDX_ACK_RECV))
45261ae08745Sheppo 			return;
45271ae08745Sheppo 
45281ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_RDX_ACK_RECV;
45293af08d82Slm66018 		D2(vswp, "%s (%ld) handling RDX_ACK in place "
45303af08d82Slm66018 			"(ostate 0x%llx : hphase %d)", __func__,
45313af08d82Slm66018 			ldcp->ldc_id, ldcp->lane_out.lstate, ldcp->hphase);
45321ae08745Sheppo 		vsw_next_milestone(ldcp);
45331ae08745Sheppo 		return;
45341ae08745Sheppo 	}
45351ae08745Sheppo 
45361ae08745Sheppo 	ctaskp = kmem_alloc(sizeof (vsw_ctrl_task_t), KM_NOSLEEP);
45371ae08745Sheppo 
45381ae08745Sheppo 	if (ctaskp == NULL) {
45391ae08745Sheppo 		DERR(vswp, "%s: unable to alloc space for ctrl"
45401ae08745Sheppo 			" msg", __func__);
45411ae08745Sheppo 		vsw_restart_handshake(ldcp);
45421ae08745Sheppo 		return;
45431ae08745Sheppo 	}
45441ae08745Sheppo 
45451ae08745Sheppo 	ctaskp->ldcp = ldcp;
45461ae08745Sheppo 	bcopy((def_msg_t *)cpkt, &ctaskp->pktp, sizeof (def_msg_t));
45471ae08745Sheppo 	mutex_enter(&ldcp->hss_lock);
45481ae08745Sheppo 	ctaskp->hss_id = ldcp->hss_id;
45491ae08745Sheppo 	mutex_exit(&ldcp->hss_lock);
45501ae08745Sheppo 
45511ae08745Sheppo 	/*
45521ae08745Sheppo 	 * Dispatch task to processing taskq if port is not in
45531ae08745Sheppo 	 * the process of being detached.
45541ae08745Sheppo 	 */
45551ae08745Sheppo 	mutex_enter(&port->state_lock);
45561ae08745Sheppo 	if (port->state == VSW_PORT_INIT) {
45571ae08745Sheppo 		if ((vswp->taskq_p == NULL) ||
45581ae08745Sheppo 			(ddi_taskq_dispatch(vswp->taskq_p,
45591ae08745Sheppo 			vsw_process_ctrl_pkt, ctaskp, DDI_NOSLEEP)
45601ae08745Sheppo 							!= DDI_SUCCESS)) {
45611ae08745Sheppo 			DERR(vswp, "%s: unable to dispatch task to taskq",
45621ae08745Sheppo 				__func__);
45631ae08745Sheppo 			kmem_free(ctaskp, sizeof (vsw_ctrl_task_t));
45641ae08745Sheppo 			mutex_exit(&port->state_lock);
45651ae08745Sheppo 			vsw_restart_handshake(ldcp);
45661ae08745Sheppo 			return;
45671ae08745Sheppo 		}
45681ae08745Sheppo 	} else {
45691ae08745Sheppo 		DWARN(vswp, "%s: port %d detaching, not dispatching "
45701ae08745Sheppo 			"task", __func__, port->p_instance);
45711ae08745Sheppo 	}
45721ae08745Sheppo 
45731ae08745Sheppo 	mutex_exit(&port->state_lock);
45741ae08745Sheppo 
45751ae08745Sheppo 	D2(vswp, "%s: dispatched task to taskq for chan %d", __func__,
45761ae08745Sheppo 			ldcp->ldc_id);
45771ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
45781ae08745Sheppo }
45791ae08745Sheppo 
45801ae08745Sheppo /*
45811ae08745Sheppo  * Process a VIO ctrl message. Invoked from taskq.
45821ae08745Sheppo  */
45831ae08745Sheppo static void
45841ae08745Sheppo vsw_process_ctrl_pkt(void *arg)
45851ae08745Sheppo {
45861ae08745Sheppo 	vsw_ctrl_task_t	*ctaskp = (vsw_ctrl_task_t *)arg;
45871ae08745Sheppo 	vsw_ldc_t	*ldcp = ctaskp->ldcp;
45881ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
45891ae08745Sheppo 	vio_msg_tag_t	tag;
45901ae08745Sheppo 	uint16_t	env;
45911ae08745Sheppo 
45921ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
45931ae08745Sheppo 
45941ae08745Sheppo 	bcopy(&ctaskp->pktp, &tag, sizeof (vio_msg_tag_t));
45951ae08745Sheppo 	env = tag.vio_subtype_env;
45961ae08745Sheppo 
45971ae08745Sheppo 	/* stale pkt check */
45981ae08745Sheppo 	mutex_enter(&ldcp->hss_lock);
45991ae08745Sheppo 	if (ctaskp->hss_id < ldcp->hss_id) {
46001ae08745Sheppo 		DWARN(vswp, "%s: discarding stale packet belonging to"
46011ae08745Sheppo 			" earlier (%ld) handshake session", __func__,
46021ae08745Sheppo 			ctaskp->hss_id);
46031ae08745Sheppo 		mutex_exit(&ldcp->hss_lock);
46041ae08745Sheppo 		return;
46051ae08745Sheppo 	}
46061ae08745Sheppo 	mutex_exit(&ldcp->hss_lock);
46071ae08745Sheppo 
46081ae08745Sheppo 	/* session id check */
46091ae08745Sheppo 	if (ldcp->session_status & VSW_PEER_SESSION) {
46101ae08745Sheppo 		if (ldcp->peer_session != tag.vio_sid) {
46111ae08745Sheppo 			DERR(vswp, "%s (chan %d): invalid session id (%llx)",
46121ae08745Sheppo 				__func__, ldcp->ldc_id, tag.vio_sid);
46131ae08745Sheppo 			kmem_free(ctaskp, sizeof (vsw_ctrl_task_t));
46141ae08745Sheppo 			vsw_restart_handshake(ldcp);
46151ae08745Sheppo 			return;
46161ae08745Sheppo 		}
46171ae08745Sheppo 	}
46181ae08745Sheppo 
46191ae08745Sheppo 	/*
46201ae08745Sheppo 	 * Switch on vio_subtype envelope, then let lower routines
46211ae08745Sheppo 	 * decide if its an INFO, ACK or NACK packet.
46221ae08745Sheppo 	 */
46231ae08745Sheppo 	switch (env) {
46241ae08745Sheppo 	case VIO_VER_INFO:
46251ae08745Sheppo 		vsw_process_ctrl_ver_pkt(ldcp, &ctaskp->pktp);
46261ae08745Sheppo 		break;
46271ae08745Sheppo 	case VIO_DRING_REG:
46281ae08745Sheppo 		vsw_process_ctrl_dring_reg_pkt(ldcp, &ctaskp->pktp);
46291ae08745Sheppo 		break;
46301ae08745Sheppo 	case VIO_DRING_UNREG:
46311ae08745Sheppo 		vsw_process_ctrl_dring_unreg_pkt(ldcp, &ctaskp->pktp);
46321ae08745Sheppo 		break;
46331ae08745Sheppo 	case VIO_ATTR_INFO:
46341ae08745Sheppo 		vsw_process_ctrl_attr_pkt(ldcp, &ctaskp->pktp);
46351ae08745Sheppo 		break;
46361ae08745Sheppo 	case VNET_MCAST_INFO:
46371ae08745Sheppo 		vsw_process_ctrl_mcst_pkt(ldcp, &ctaskp->pktp);
46381ae08745Sheppo 		break;
46391ae08745Sheppo 	case VIO_RDX:
46401ae08745Sheppo 		vsw_process_ctrl_rdx_pkt(ldcp, &ctaskp->pktp);
46411ae08745Sheppo 		break;
46421ae08745Sheppo 	default:
46431ae08745Sheppo 		DERR(vswp, "%s : unknown vio_subtype_env (%x)\n",
46441ae08745Sheppo 							__func__, env);
46451ae08745Sheppo 	}
46461ae08745Sheppo 
46471ae08745Sheppo 	kmem_free(ctaskp, sizeof (vsw_ctrl_task_t));
46481ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
46491ae08745Sheppo }
46501ae08745Sheppo 
46511ae08745Sheppo /*
46521ae08745Sheppo  * Version negotiation. We can end up here either because our peer
46531ae08745Sheppo  * has responded to a handshake message we have sent it, or our peer
46541ae08745Sheppo  * has initiated a handshake with us. If its the former then can only
46551ae08745Sheppo  * be ACK or NACK, if its the later can only be INFO.
46561ae08745Sheppo  *
46571ae08745Sheppo  * If its an ACK we move to the next stage of the handshake, namely
46581ae08745Sheppo  * attribute exchange. If its a NACK we see if we can specify another
46591ae08745Sheppo  * version, if we can't we stop.
46601ae08745Sheppo  *
46611ae08745Sheppo  * If it is an INFO we reset all params associated with communication
46621ae08745Sheppo  * in that direction over this channel (remember connection is
46631ae08745Sheppo  * essentially 2 independent simplex channels).
46641ae08745Sheppo  */
46651ae08745Sheppo void
46661ae08745Sheppo vsw_process_ctrl_ver_pkt(vsw_ldc_t *ldcp, void *pkt)
46671ae08745Sheppo {
46681ae08745Sheppo 	vio_ver_msg_t	*ver_pkt;
46691ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
46701ae08745Sheppo 
46711ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
46721ae08745Sheppo 
46731ae08745Sheppo 	/*
46741ae08745Sheppo 	 * We know this is a ctrl/version packet so
46751ae08745Sheppo 	 * cast it into the correct structure.
46761ae08745Sheppo 	 */
46771ae08745Sheppo 	ver_pkt = (vio_ver_msg_t *)pkt;
46781ae08745Sheppo 
46791ae08745Sheppo 	switch (ver_pkt->tag.vio_subtype) {
46801ae08745Sheppo 	case VIO_SUBTYPE_INFO:
46811ae08745Sheppo 		D2(vswp, "vsw_process_ctrl_ver_pkt: VIO_SUBTYPE_INFO\n");
46821ae08745Sheppo 
46831ae08745Sheppo 		/*
46841ae08745Sheppo 		 * Record the session id, which we will use from now
46851ae08745Sheppo 		 * until we see another VER_INFO msg. Even then the
46861ae08745Sheppo 		 * session id in most cases will be unchanged, execpt
46871ae08745Sheppo 		 * if channel was reset.
46881ae08745Sheppo 		 */
46891ae08745Sheppo 		if ((ldcp->session_status & VSW_PEER_SESSION) &&
46901ae08745Sheppo 			(ldcp->peer_session != ver_pkt->tag.vio_sid)) {
46911ae08745Sheppo 			DERR(vswp, "%s: updating session id for chan %lld "
46921ae08745Sheppo 				"from %llx to %llx", __func__, ldcp->ldc_id,
46931ae08745Sheppo 				ldcp->peer_session, ver_pkt->tag.vio_sid);
46941ae08745Sheppo 		}
46951ae08745Sheppo 
46961ae08745Sheppo 		ldcp->peer_session = ver_pkt->tag.vio_sid;
46971ae08745Sheppo 		ldcp->session_status |= VSW_PEER_SESSION;
46981ae08745Sheppo 
46991ae08745Sheppo 		/* Legal message at this time ? */
47001ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_VER_INFO_RECV))
47011ae08745Sheppo 			return;
47021ae08745Sheppo 
47031ae08745Sheppo 		/*
47041ae08745Sheppo 		 * First check the device class. Currently only expect
47051ae08745Sheppo 		 * to be talking to a network device. In the future may
47061ae08745Sheppo 		 * also talk to another switch.
47071ae08745Sheppo 		 */
47081ae08745Sheppo 		if (ver_pkt->dev_class != VDEV_NETWORK) {
47091ae08745Sheppo 			DERR(vswp, "%s: illegal device class %d", __func__,
47101ae08745Sheppo 				ver_pkt->dev_class);
47111ae08745Sheppo 
47121ae08745Sheppo 			ver_pkt->tag.vio_sid = ldcp->local_session;
47131ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
47141ae08745Sheppo 
47151ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)ver_pkt);
47161ae08745Sheppo 
47171ae08745Sheppo 			vsw_send_msg(ldcp, (void *)ver_pkt,
47181ae08745Sheppo 					sizeof (vio_ver_msg_t));
47191ae08745Sheppo 
47201ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_VER_NACK_SENT;
47211ae08745Sheppo 			vsw_next_milestone(ldcp);
47221ae08745Sheppo 			return;
47231ae08745Sheppo 		} else {
47241ae08745Sheppo 			ldcp->dev_class = ver_pkt->dev_class;
47251ae08745Sheppo 		}
47261ae08745Sheppo 
47271ae08745Sheppo 		/*
47281ae08745Sheppo 		 * Now check the version.
47291ae08745Sheppo 		 */
47301ae08745Sheppo 		if (vsw_supported_version(ver_pkt) == 0) {
47311ae08745Sheppo 			/*
47321ae08745Sheppo 			 * Support this major version and possibly
47331ae08745Sheppo 			 * adjusted minor version.
47341ae08745Sheppo 			 */
47351ae08745Sheppo 
47361ae08745Sheppo 			D2(vswp, "%s: accepted ver %d:%d", __func__,
47371ae08745Sheppo 				ver_pkt->ver_major, ver_pkt->ver_minor);
47381ae08745Sheppo 
47391ae08745Sheppo 			/* Store accepted values */
47401ae08745Sheppo 			ldcp->lane_in.ver_major = ver_pkt->ver_major;
47411ae08745Sheppo 			ldcp->lane_in.ver_minor = ver_pkt->ver_minor;
47421ae08745Sheppo 
47431ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
47441ae08745Sheppo 
47451ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_VER_ACK_SENT;
47461ae08745Sheppo 		} else {
47471ae08745Sheppo 			/*
47481ae08745Sheppo 			 * NACK back with the next lower major/minor
47491ae08745Sheppo 			 * pairing we support (if don't suuport any more
47501ae08745Sheppo 			 * versions then they will be set to zero.
47511ae08745Sheppo 			 */
47521ae08745Sheppo 
47531ae08745Sheppo 			D2(vswp, "%s: replying with ver %d:%d", __func__,
47541ae08745Sheppo 				ver_pkt->ver_major, ver_pkt->ver_minor);
47551ae08745Sheppo 
47561ae08745Sheppo 			/* Store updated values */
47571ae08745Sheppo 			ldcp->lane_in.ver_major = ver_pkt->ver_major;
47581ae08745Sheppo 			ldcp->lane_in.ver_minor = ver_pkt->ver_minor;
47591ae08745Sheppo 
47601ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
47611ae08745Sheppo 
47621ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_VER_NACK_SENT;
47631ae08745Sheppo 		}
47641ae08745Sheppo 
47651ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)ver_pkt);
47661ae08745Sheppo 		ver_pkt->tag.vio_sid = ldcp->local_session;
47671ae08745Sheppo 		vsw_send_msg(ldcp, (void *)ver_pkt, sizeof (vio_ver_msg_t));
47681ae08745Sheppo 
47691ae08745Sheppo 		vsw_next_milestone(ldcp);
47701ae08745Sheppo 		break;
47711ae08745Sheppo 
47721ae08745Sheppo 	case VIO_SUBTYPE_ACK:
47731ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK\n", __func__);
47741ae08745Sheppo 
47751ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_VER_ACK_RECV))
47761ae08745Sheppo 			return;
47771ae08745Sheppo 
47781ae08745Sheppo 		/* Store updated values */
47791ae08745Sheppo 		ldcp->lane_in.ver_major = ver_pkt->ver_major;
47801ae08745Sheppo 		ldcp->lane_in.ver_minor = ver_pkt->ver_minor;
47811ae08745Sheppo 
47821ae08745Sheppo 
47831ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_VER_ACK_RECV;
47841ae08745Sheppo 		vsw_next_milestone(ldcp);
47851ae08745Sheppo 
47861ae08745Sheppo 		break;
47871ae08745Sheppo 
47881ae08745Sheppo 	case VIO_SUBTYPE_NACK:
47891ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK\n", __func__);
47901ae08745Sheppo 
47911ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_VER_NACK_RECV))
47921ae08745Sheppo 			return;
47931ae08745Sheppo 
47941ae08745Sheppo 		/*
47951ae08745Sheppo 		 * If our peer sent us a NACK with the ver fields set to
47961ae08745Sheppo 		 * zero then there is nothing more we can do. Otherwise see
47971ae08745Sheppo 		 * if we support either the version suggested, or a lesser
47981ae08745Sheppo 		 * one.
47991ae08745Sheppo 		 */
48001ae08745Sheppo 		if ((ver_pkt->ver_major == 0) && (ver_pkt->ver_minor == 0)) {
48011ae08745Sheppo 			DERR(vswp, "%s: peer unable to negotiate any "
48021ae08745Sheppo 				"further.", __func__);
48031ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_VER_NACK_RECV;
48041ae08745Sheppo 			vsw_next_milestone(ldcp);
48051ae08745Sheppo 			return;
48061ae08745Sheppo 		}
48071ae08745Sheppo 
48081ae08745Sheppo 		/*
48091ae08745Sheppo 		 * Check to see if we support this major version or
48101ae08745Sheppo 		 * a lower one. If we don't then maj/min will be set
48111ae08745Sheppo 		 * to zero.
48121ae08745Sheppo 		 */
48131ae08745Sheppo 		(void) vsw_supported_version(ver_pkt);
48141ae08745Sheppo 		if ((ver_pkt->ver_major == 0) && (ver_pkt->ver_minor == 0)) {
48151ae08745Sheppo 			/* Nothing more we can do */
48161ae08745Sheppo 			DERR(vswp, "%s: version negotiation failed.\n",
48171ae08745Sheppo 								__func__);
48181ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_VER_NACK_RECV;
48191ae08745Sheppo 			vsw_next_milestone(ldcp);
48201ae08745Sheppo 		} else {
48211ae08745Sheppo 			/* found a supported major version */
48221ae08745Sheppo 			ldcp->lane_out.ver_major = ver_pkt->ver_major;
48231ae08745Sheppo 			ldcp->lane_out.ver_minor = ver_pkt->ver_minor;
48241ae08745Sheppo 
48251ae08745Sheppo 			D2(vswp, "%s: resending with updated values (%x, %x)",
48261ae08745Sheppo 				__func__, ver_pkt->ver_major,
48271ae08745Sheppo 				ver_pkt->ver_minor);
48281ae08745Sheppo 
48291ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_VER_INFO_SENT;
48301ae08745Sheppo 			ver_pkt->tag.vio_sid = ldcp->local_session;
48311ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_INFO;
48321ae08745Sheppo 
48331ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)ver_pkt);
48341ae08745Sheppo 
48351ae08745Sheppo 			vsw_send_msg(ldcp, (void *)ver_pkt,
48361ae08745Sheppo 					sizeof (vio_ver_msg_t));
48371ae08745Sheppo 
48381ae08745Sheppo 			vsw_next_milestone(ldcp);
48391ae08745Sheppo 
48401ae08745Sheppo 		}
48411ae08745Sheppo 		break;
48421ae08745Sheppo 
48431ae08745Sheppo 	default:
48441ae08745Sheppo 		DERR(vswp, "%s: unknown vio_subtype %x\n", __func__,
48451ae08745Sheppo 			ver_pkt->tag.vio_subtype);
48461ae08745Sheppo 	}
48471ae08745Sheppo 
48481ae08745Sheppo 	D1(vswp, "%s(%lld): exit\n", __func__, ldcp->ldc_id);
48491ae08745Sheppo }
48501ae08745Sheppo 
48511ae08745Sheppo /*
48521ae08745Sheppo  * Process an attribute packet. We can end up here either because our peer
48531ae08745Sheppo  * has ACK/NACK'ed back to an earlier ATTR msg we had sent it, or our
48541ae08745Sheppo  * peer has sent us an attribute INFO message
48551ae08745Sheppo  *
48561ae08745Sheppo  * If its an ACK we then move to the next stage of the handshake which
48571ae08745Sheppo  * is to send our descriptor ring info to our peer. If its a NACK then
48581ae08745Sheppo  * there is nothing more we can (currently) do.
48591ae08745Sheppo  *
48601ae08745Sheppo  * If we get a valid/acceptable INFO packet (and we have already negotiated
48611ae08745Sheppo  * a version) we ACK back and set channel state to ATTR_RECV, otherwise we
48621ae08745Sheppo  * NACK back and reset channel state to INACTIV.
48631ae08745Sheppo  *
48641ae08745Sheppo  * FUTURE: in time we will probably negotiate over attributes, but for
48651ae08745Sheppo  * the moment unacceptable attributes are regarded as a fatal error.
48661ae08745Sheppo  *
48671ae08745Sheppo  */
48681ae08745Sheppo void
48691ae08745Sheppo vsw_process_ctrl_attr_pkt(vsw_ldc_t *ldcp, void *pkt)
48701ae08745Sheppo {
48711ae08745Sheppo 	vnet_attr_msg_t		*attr_pkt;
48721ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
48731ae08745Sheppo 	vsw_port_t		*port = ldcp->ldc_port;
48741ae08745Sheppo 	uint64_t		macaddr = 0;
48751ae08745Sheppo 	int			i;
48761ae08745Sheppo 
48771ae08745Sheppo 	D1(vswp, "%s(%lld) enter", __func__, ldcp->ldc_id);
48781ae08745Sheppo 
48791ae08745Sheppo 	/*
48801ae08745Sheppo 	 * We know this is a ctrl/attr packet so
48811ae08745Sheppo 	 * cast it into the correct structure.
48821ae08745Sheppo 	 */
48831ae08745Sheppo 	attr_pkt = (vnet_attr_msg_t *)pkt;
48841ae08745Sheppo 
48851ae08745Sheppo 	switch (attr_pkt->tag.vio_subtype) {
48861ae08745Sheppo 	case VIO_SUBTYPE_INFO:
48871ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
48881ae08745Sheppo 
48891ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_ATTR_INFO_RECV))
48901ae08745Sheppo 			return;
48911ae08745Sheppo 
48921ae08745Sheppo 		/*
48931ae08745Sheppo 		 * If the attributes are unacceptable then we NACK back.
48941ae08745Sheppo 		 */
48951ae08745Sheppo 		if (vsw_check_attr(attr_pkt, ldcp->ldc_port)) {
48961ae08745Sheppo 
48971ae08745Sheppo 			DERR(vswp, "%s (chan %d): invalid attributes",
48981ae08745Sheppo 				__func__, ldcp->ldc_id);
48991ae08745Sheppo 
49001ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
49011ae08745Sheppo 
49021ae08745Sheppo 			attr_pkt->tag.vio_sid = ldcp->local_session;
49031ae08745Sheppo 			attr_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
49041ae08745Sheppo 
49051ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)attr_pkt);
49061ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_ATTR_NACK_SENT;
49071ae08745Sheppo 			vsw_send_msg(ldcp, (void *)attr_pkt,
49081ae08745Sheppo 					sizeof (vnet_attr_msg_t));
49091ae08745Sheppo 
49101ae08745Sheppo 			vsw_next_milestone(ldcp);
49111ae08745Sheppo 			return;
49121ae08745Sheppo 		}
49131ae08745Sheppo 
49141ae08745Sheppo 		/*
49151ae08745Sheppo 		 * Otherwise store attributes for this lane and update
49161ae08745Sheppo 		 * lane state.
49171ae08745Sheppo 		 */
49181ae08745Sheppo 		ldcp->lane_in.mtu = attr_pkt->mtu;
49191ae08745Sheppo 		ldcp->lane_in.addr = attr_pkt->addr;
49201ae08745Sheppo 		ldcp->lane_in.addr_type = attr_pkt->addr_type;
49211ae08745Sheppo 		ldcp->lane_in.xfer_mode = attr_pkt->xfer_mode;
49221ae08745Sheppo 		ldcp->lane_in.ack_freq = attr_pkt->ack_freq;
49231ae08745Sheppo 
49241ae08745Sheppo 		macaddr = ldcp->lane_in.addr;
49251ae08745Sheppo 		for (i = ETHERADDRL - 1; i >= 0; i--) {
49261ae08745Sheppo 			port->p_macaddr.ether_addr_octet[i] = macaddr & 0xFF;
49271ae08745Sheppo 			macaddr >>= 8;
49281ae08745Sheppo 		}
49291ae08745Sheppo 
49301ae08745Sheppo 		/* create the fdb entry for this port/mac address */
49311ae08745Sheppo 		(void) vsw_add_fdb(vswp, port);
49321ae08745Sheppo 
49331ae08745Sheppo 		/* setup device specifc xmit routines */
49341ae08745Sheppo 		mutex_enter(&port->tx_lock);
49351ae08745Sheppo 		if (ldcp->lane_in.xfer_mode == VIO_DRING_MODE) {
49361ae08745Sheppo 			D2(vswp, "%s: mode = VIO_DRING_MODE", __func__);
49371ae08745Sheppo 			port->transmit = vsw_dringsend;
49381ae08745Sheppo 		} else if (ldcp->lane_in.xfer_mode == VIO_DESC_MODE) {
49391ae08745Sheppo 			D2(vswp, "%s: mode = VIO_DESC_MODE", __func__);
49401ae08745Sheppo 			vsw_create_privring(ldcp);
49411ae08745Sheppo 			port->transmit = vsw_descrsend;
49421ae08745Sheppo 		}
49431ae08745Sheppo 		mutex_exit(&port->tx_lock);
49441ae08745Sheppo 
49451ae08745Sheppo 		attr_pkt->tag.vio_sid = ldcp->local_session;
49461ae08745Sheppo 		attr_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
49471ae08745Sheppo 
49481ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)attr_pkt);
49491ae08745Sheppo 
49501ae08745Sheppo 		ldcp->lane_in.lstate |= VSW_ATTR_ACK_SENT;
49511ae08745Sheppo 
49521ae08745Sheppo 		vsw_send_msg(ldcp, (void *)attr_pkt,
49531ae08745Sheppo 					sizeof (vnet_attr_msg_t));
49541ae08745Sheppo 
49551ae08745Sheppo 		vsw_next_milestone(ldcp);
49561ae08745Sheppo 		break;
49571ae08745Sheppo 
49581ae08745Sheppo 	case VIO_SUBTYPE_ACK:
49591ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
49601ae08745Sheppo 
49611ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_ATTR_ACK_RECV))
49621ae08745Sheppo 			return;
49631ae08745Sheppo 
49641ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_ATTR_ACK_RECV;
49651ae08745Sheppo 		vsw_next_milestone(ldcp);
49661ae08745Sheppo 		break;
49671ae08745Sheppo 
49681ae08745Sheppo 	case VIO_SUBTYPE_NACK:
49691ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
49701ae08745Sheppo 
49711ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_ATTR_NACK_RECV))
49721ae08745Sheppo 			return;
49731ae08745Sheppo 
49741ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_ATTR_NACK_RECV;
49751ae08745Sheppo 		vsw_next_milestone(ldcp);
49761ae08745Sheppo 		break;
49771ae08745Sheppo 
49781ae08745Sheppo 	default:
49791ae08745Sheppo 		DERR(vswp, "%s: unknown vio_subtype %x\n", __func__,
49801ae08745Sheppo 			attr_pkt->tag.vio_subtype);
49811ae08745Sheppo 	}
49821ae08745Sheppo 
49831ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
49841ae08745Sheppo }
49851ae08745Sheppo 
49861ae08745Sheppo /*
49871ae08745Sheppo  * Process a dring info packet. We can end up here either because our peer
49881ae08745Sheppo  * has ACK/NACK'ed back to an earlier DRING msg we had sent it, or our
49891ae08745Sheppo  * peer has sent us a dring INFO message.
49901ae08745Sheppo  *
49911ae08745Sheppo  * If we get a valid/acceptable INFO packet (and we have already negotiated
49921ae08745Sheppo  * a version) we ACK back and update the lane state, otherwise we NACK back.
49931ae08745Sheppo  *
49941ae08745Sheppo  * FUTURE: nothing to stop client from sending us info on multiple dring's
49951ae08745Sheppo  * but for the moment we will just use the first one we are given.
49961ae08745Sheppo  *
49971ae08745Sheppo  */
49981ae08745Sheppo void
49991ae08745Sheppo vsw_process_ctrl_dring_reg_pkt(vsw_ldc_t *ldcp, void *pkt)
50001ae08745Sheppo {
50011ae08745Sheppo 	vio_dring_reg_msg_t	*dring_pkt;
50021ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
50031ae08745Sheppo 	ldc_mem_info_t		minfo;
50041ae08745Sheppo 	dring_info_t		*dp, *dbp;
50051ae08745Sheppo 	int			dring_found = 0;
50061ae08745Sheppo 
50071ae08745Sheppo 	/*
50081ae08745Sheppo 	 * We know this is a ctrl/dring packet so
50091ae08745Sheppo 	 * cast it into the correct structure.
50101ae08745Sheppo 	 */
50111ae08745Sheppo 	dring_pkt = (vio_dring_reg_msg_t *)pkt;
50121ae08745Sheppo 
50131ae08745Sheppo 	D1(vswp, "%s(%lld) enter", __func__, ldcp->ldc_id);
50141ae08745Sheppo 
50151ae08745Sheppo 	switch (dring_pkt->tag.vio_subtype) {
50161ae08745Sheppo 	case VIO_SUBTYPE_INFO:
50171ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
50181ae08745Sheppo 
50191ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_DRING_INFO_RECV))
50201ae08745Sheppo 			return;
50211ae08745Sheppo 
50221ae08745Sheppo 		/*
50231ae08745Sheppo 		 * If the dring params are unacceptable then we NACK back.
50241ae08745Sheppo 		 */
50251ae08745Sheppo 		if (vsw_check_dring_info(dring_pkt)) {
50261ae08745Sheppo 
50271ae08745Sheppo 			DERR(vswp, "%s (%lld): invalid dring info",
50281ae08745Sheppo 				__func__, ldcp->ldc_id);
50291ae08745Sheppo 
50301ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
50311ae08745Sheppo 
50321ae08745Sheppo 			dring_pkt->tag.vio_sid = ldcp->local_session;
50331ae08745Sheppo 			dring_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
50341ae08745Sheppo 
50351ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)dring_pkt);
50361ae08745Sheppo 
50371ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_DRING_NACK_SENT;
50381ae08745Sheppo 
50391ae08745Sheppo 			vsw_send_msg(ldcp, (void *)dring_pkt,
50401ae08745Sheppo 					sizeof (vio_dring_reg_msg_t));
50411ae08745Sheppo 
50421ae08745Sheppo 			vsw_next_milestone(ldcp);
50431ae08745Sheppo 			return;
50441ae08745Sheppo 		}
50451ae08745Sheppo 
50461ae08745Sheppo 		/*
50471ae08745Sheppo 		 * Otherwise, attempt to map in the dring using the
50481ae08745Sheppo 		 * cookie. If that succeeds we send back a unique dring
50491ae08745Sheppo 		 * identifier that the sending side will use in future
50501ae08745Sheppo 		 * to refer to this descriptor ring.
50511ae08745Sheppo 		 */
50521ae08745Sheppo 		dp = kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
50531ae08745Sheppo 
50541ae08745Sheppo 		dp->num_descriptors = dring_pkt->num_descriptors;
50551ae08745Sheppo 		dp->descriptor_size = dring_pkt->descriptor_size;
50561ae08745Sheppo 		dp->options = dring_pkt->options;
50571ae08745Sheppo 		dp->ncookies = dring_pkt->ncookies;
50581ae08745Sheppo 
50591ae08745Sheppo 		/*
50601ae08745Sheppo 		 * Note: should only get one cookie. Enforced in
50611ae08745Sheppo 		 * the ldc layer.
50621ae08745Sheppo 		 */
50631ae08745Sheppo 		bcopy(&dring_pkt->cookie[0], &dp->cookie[0],
50641ae08745Sheppo 			sizeof (ldc_mem_cookie_t));
50651ae08745Sheppo 
50661ae08745Sheppo 		D2(vswp, "%s: num_desc %ld : desc_size %ld", __func__,
50671ae08745Sheppo 			dp->num_descriptors, dp->descriptor_size);
50681ae08745Sheppo 		D2(vswp, "%s: options 0x%lx: ncookies %ld", __func__,
50691ae08745Sheppo 			dp->options, dp->ncookies);
50701ae08745Sheppo 
50711ae08745Sheppo 		if ((ldc_mem_dring_map(ldcp->ldc_handle, &dp->cookie[0],
50721ae08745Sheppo 			dp->ncookies, dp->num_descriptors,
50731ae08745Sheppo 			dp->descriptor_size, LDC_SHADOW_MAP,
50741ae08745Sheppo 			&(dp->handle))) != 0) {
50751ae08745Sheppo 
50761ae08745Sheppo 			DERR(vswp, "%s: dring_map failed\n", __func__);
50771ae08745Sheppo 
50781ae08745Sheppo 			kmem_free(dp, sizeof (dring_info_t));
50791ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
50801ae08745Sheppo 
50811ae08745Sheppo 			dring_pkt->tag.vio_sid = ldcp->local_session;
50821ae08745Sheppo 			dring_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
50831ae08745Sheppo 
50841ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)dring_pkt);
50851ae08745Sheppo 
50861ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_DRING_NACK_SENT;
50871ae08745Sheppo 			vsw_send_msg(ldcp, (void *)dring_pkt,
50881ae08745Sheppo 				sizeof (vio_dring_reg_msg_t));
50891ae08745Sheppo 
50901ae08745Sheppo 			vsw_next_milestone(ldcp);
50911ae08745Sheppo 			return;
50921ae08745Sheppo 		}
50931ae08745Sheppo 
50941ae08745Sheppo 		if ((ldc_mem_dring_info(dp->handle, &minfo)) != 0) {
50951ae08745Sheppo 
50961ae08745Sheppo 			DERR(vswp, "%s: dring_addr failed\n", __func__);
50971ae08745Sheppo 
50981ae08745Sheppo 			kmem_free(dp, sizeof (dring_info_t));
50991ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
51001ae08745Sheppo 
51011ae08745Sheppo 			dring_pkt->tag.vio_sid = ldcp->local_session;
51021ae08745Sheppo 			dring_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
51031ae08745Sheppo 
51041ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)dring_pkt);
51051ae08745Sheppo 
51061ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_DRING_NACK_SENT;
51071ae08745Sheppo 			vsw_send_msg(ldcp, (void *)dring_pkt,
51081ae08745Sheppo 				sizeof (vio_dring_reg_msg_t));
51091ae08745Sheppo 
51101ae08745Sheppo 			vsw_next_milestone(ldcp);
51111ae08745Sheppo 			return;
51121ae08745Sheppo 		} else {
51131ae08745Sheppo 			/* store the address of the pub part of ring */
51141ae08745Sheppo 			dp->pub_addr = minfo.vaddr;
51151ae08745Sheppo 		}
51161ae08745Sheppo 
51171ae08745Sheppo 		/* no private section as we are importing */
51181ae08745Sheppo 		dp->priv_addr = NULL;
51191ae08745Sheppo 
51201ae08745Sheppo 		/*
51211ae08745Sheppo 		 * Using simple mono increasing int for ident at
51221ae08745Sheppo 		 * the moment.
51231ae08745Sheppo 		 */
51241ae08745Sheppo 		dp->ident = ldcp->next_ident;
51251ae08745Sheppo 		ldcp->next_ident++;
51261ae08745Sheppo 
51271ae08745Sheppo 		dp->end_idx = 0;
51281ae08745Sheppo 		dp->next = NULL;
51291ae08745Sheppo 
51301ae08745Sheppo 		/*
51311ae08745Sheppo 		 * Link it onto the end of the list of drings
51321ae08745Sheppo 		 * for this lane.
51331ae08745Sheppo 		 */
51341ae08745Sheppo 		if (ldcp->lane_in.dringp == NULL) {
51351ae08745Sheppo 			D2(vswp, "%s: adding first INBOUND dring", __func__);
51361ae08745Sheppo 			ldcp->lane_in.dringp = dp;
51371ae08745Sheppo 		} else {
51381ae08745Sheppo 			dbp = ldcp->lane_in.dringp;
51391ae08745Sheppo 
51401ae08745Sheppo 			while (dbp->next != NULL)
51411ae08745Sheppo 				dbp = dbp->next;
51421ae08745Sheppo 
51431ae08745Sheppo 			dbp->next = dp;
51441ae08745Sheppo 		}
51451ae08745Sheppo 
51461ae08745Sheppo 		/* acknowledge it */
51471ae08745Sheppo 		dring_pkt->tag.vio_sid = ldcp->local_session;
51481ae08745Sheppo 		dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
51491ae08745Sheppo 		dring_pkt->dring_ident = dp->ident;
51501ae08745Sheppo 
51511ae08745Sheppo 		vsw_send_msg(ldcp, (void *)dring_pkt,
51521ae08745Sheppo 				sizeof (vio_dring_reg_msg_t));
51531ae08745Sheppo 
51541ae08745Sheppo 		ldcp->lane_in.lstate |= VSW_DRING_ACK_SENT;
51551ae08745Sheppo 		vsw_next_milestone(ldcp);
51561ae08745Sheppo 		break;
51571ae08745Sheppo 
51581ae08745Sheppo 	case VIO_SUBTYPE_ACK:
51591ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
51601ae08745Sheppo 
51611ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_DRING_ACK_RECV))
51621ae08745Sheppo 			return;
51631ae08745Sheppo 
51641ae08745Sheppo 		/*
51651ae08745Sheppo 		 * Peer is acknowledging our dring info and will have
51661ae08745Sheppo 		 * sent us a dring identifier which we will use to
51671ae08745Sheppo 		 * refer to this ring w.r.t. our peer.
51681ae08745Sheppo 		 */
51691ae08745Sheppo 		dp = ldcp->lane_out.dringp;
51701ae08745Sheppo 		if (dp != NULL) {
51711ae08745Sheppo 			/*
51721ae08745Sheppo 			 * Find the ring this ident should be associated
51731ae08745Sheppo 			 * with.
51741ae08745Sheppo 			 */
51751ae08745Sheppo 			if (vsw_dring_match(dp, dring_pkt)) {
51761ae08745Sheppo 				dring_found = 1;
51771ae08745Sheppo 
51781ae08745Sheppo 			} else while (dp != NULL) {
51791ae08745Sheppo 				if (vsw_dring_match(dp, dring_pkt)) {
51801ae08745Sheppo 					dring_found = 1;
51811ae08745Sheppo 					break;
51821ae08745Sheppo 				}
51831ae08745Sheppo 				dp = dp->next;
51841ae08745Sheppo 			}
51851ae08745Sheppo 
51861ae08745Sheppo 			if (dring_found == 0) {
51871ae08745Sheppo 				DERR(NULL, "%s: unrecognised ring cookie",
51881ae08745Sheppo 					__func__);
51891ae08745Sheppo 				vsw_restart_handshake(ldcp);
51901ae08745Sheppo 				return;
51911ae08745Sheppo 			}
51921ae08745Sheppo 
51931ae08745Sheppo 		} else {
51941ae08745Sheppo 			DERR(vswp, "%s: DRING ACK received but no drings "
51951ae08745Sheppo 				"allocated", __func__);
51961ae08745Sheppo 			vsw_restart_handshake(ldcp);
51971ae08745Sheppo 			return;
51981ae08745Sheppo 		}
51991ae08745Sheppo 
52001ae08745Sheppo 		/* store ident */
52011ae08745Sheppo 		dp->ident = dring_pkt->dring_ident;
52021ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_DRING_ACK_RECV;
52031ae08745Sheppo 		vsw_next_milestone(ldcp);
52041ae08745Sheppo 		break;
52051ae08745Sheppo 
52061ae08745Sheppo 	case VIO_SUBTYPE_NACK:
52071ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
52081ae08745Sheppo 
52091ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_DRING_NACK_RECV))
52101ae08745Sheppo 			return;
52111ae08745Sheppo 
52121ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_DRING_NACK_RECV;
52131ae08745Sheppo 		vsw_next_milestone(ldcp);
52141ae08745Sheppo 		break;
52151ae08745Sheppo 
52161ae08745Sheppo 	default:
52171ae08745Sheppo 		DERR(vswp, "%s: Unknown vio_subtype %x\n", __func__,
52181ae08745Sheppo 			dring_pkt->tag.vio_subtype);
52191ae08745Sheppo 	}
52201ae08745Sheppo 
52211ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
52221ae08745Sheppo }
52231ae08745Sheppo 
52241ae08745Sheppo /*
52251ae08745Sheppo  * Process a request from peer to unregister a dring.
52261ae08745Sheppo  *
52271ae08745Sheppo  * For the moment we just restart the handshake if our
52281ae08745Sheppo  * peer endpoint attempts to unregister a dring.
52291ae08745Sheppo  */
52301ae08745Sheppo void
52311ae08745Sheppo vsw_process_ctrl_dring_unreg_pkt(vsw_ldc_t *ldcp, void *pkt)
52321ae08745Sheppo {
52331ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
52341ae08745Sheppo 	vio_dring_unreg_msg_t	*dring_pkt;
52351ae08745Sheppo 
52361ae08745Sheppo 	/*
52371ae08745Sheppo 	 * We know this is a ctrl/dring packet so
52381ae08745Sheppo 	 * cast it into the correct structure.
52391ae08745Sheppo 	 */
52401ae08745Sheppo 	dring_pkt = (vio_dring_unreg_msg_t *)pkt;
52411ae08745Sheppo 
52421ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
52431ae08745Sheppo 
52441ae08745Sheppo 	switch (dring_pkt->tag.vio_subtype) {
52451ae08745Sheppo 	case VIO_SUBTYPE_INFO:
52461ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
52471ae08745Sheppo 
52481ae08745Sheppo 		DWARN(vswp, "%s: restarting handshake..", __func__);
52491ae08745Sheppo 		vsw_restart_handshake(ldcp);
52501ae08745Sheppo 		break;
52511ae08745Sheppo 
52521ae08745Sheppo 	case VIO_SUBTYPE_ACK:
52531ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
52541ae08745Sheppo 
52551ae08745Sheppo 		DWARN(vswp, "%s: restarting handshake..", __func__);
52561ae08745Sheppo 		vsw_restart_handshake(ldcp);
52571ae08745Sheppo 		break;
52581ae08745Sheppo 
52591ae08745Sheppo 	case VIO_SUBTYPE_NACK:
52601ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
52611ae08745Sheppo 
52621ae08745Sheppo 		DWARN(vswp, "%s: restarting handshake..", __func__);
52631ae08745Sheppo 		vsw_restart_handshake(ldcp);
52641ae08745Sheppo 		break;
52651ae08745Sheppo 
52661ae08745Sheppo 	default:
52671ae08745Sheppo 		DERR(vswp, "%s: Unknown vio_subtype %x\n", __func__,
52681ae08745Sheppo 			dring_pkt->tag.vio_subtype);
52691ae08745Sheppo 		vsw_restart_handshake(ldcp);
52701ae08745Sheppo 	}
52711ae08745Sheppo 
52721ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
52731ae08745Sheppo }
52741ae08745Sheppo 
52751ae08745Sheppo #define	SND_MCST_NACK(ldcp, pkt) \
52761ae08745Sheppo 	pkt->tag.vio_subtype = VIO_SUBTYPE_NACK; \
52771ae08745Sheppo 	pkt->tag.vio_sid = ldcp->local_session; \
52781ae08745Sheppo 	vsw_send_msg(ldcp, (void *)pkt, sizeof (vnet_mcast_msg_t));
52791ae08745Sheppo 
52801ae08745Sheppo /*
52811ae08745Sheppo  * Process a multicast request from a vnet.
52821ae08745Sheppo  *
52831ae08745Sheppo  * Vnet's specify a multicast address that they are interested in. This
52841ae08745Sheppo  * address is used as a key into the hash table which forms the multicast
52851ae08745Sheppo  * forwarding database (mFDB).
52861ae08745Sheppo  *
52871ae08745Sheppo  * The table keys are the multicast addresses, while the table entries
52881ae08745Sheppo  * are pointers to lists of ports which wish to receive packets for the
52891ae08745Sheppo  * specified multicast address.
52901ae08745Sheppo  *
52911ae08745Sheppo  * When a multicast packet is being switched we use the address as a key
52921ae08745Sheppo  * into the hash table, and then walk the appropriate port list forwarding
52931ae08745Sheppo  * the pkt to each port in turn.
52941ae08745Sheppo  *
52951ae08745Sheppo  * If a vnet is no longer interested in a particular multicast grouping
52961ae08745Sheppo  * we simply find the correct location in the hash table and then delete
52971ae08745Sheppo  * the relevant port from the port list.
52981ae08745Sheppo  *
52991ae08745Sheppo  * To deal with the case whereby a port is being deleted without first
53001ae08745Sheppo  * removing itself from the lists in the hash table, we maintain a list
53011ae08745Sheppo  * of multicast addresses the port has registered an interest in, within
53021ae08745Sheppo  * the port structure itself. We then simply walk that list of addresses
53031ae08745Sheppo  * using them as keys into the hash table and remove the port from the
53041ae08745Sheppo  * appropriate lists.
53051ae08745Sheppo  */
53061ae08745Sheppo static void
53071ae08745Sheppo vsw_process_ctrl_mcst_pkt(vsw_ldc_t *ldcp, void *pkt)
53081ae08745Sheppo {
53091ae08745Sheppo 	vnet_mcast_msg_t	*mcst_pkt;
53101ae08745Sheppo 	vsw_port_t		*port = ldcp->ldc_port;
53111ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
53121ae08745Sheppo 	int			i;
53131ae08745Sheppo 
53141ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
53151ae08745Sheppo 
53161ae08745Sheppo 	/*
53171ae08745Sheppo 	 * We know this is a ctrl/mcast packet so
53181ae08745Sheppo 	 * cast it into the correct structure.
53191ae08745Sheppo 	 */
53201ae08745Sheppo 	mcst_pkt = (vnet_mcast_msg_t *)pkt;
53211ae08745Sheppo 
53221ae08745Sheppo 	switch (mcst_pkt->tag.vio_subtype) {
53231ae08745Sheppo 	case VIO_SUBTYPE_INFO:
53241ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
53251ae08745Sheppo 
53261ae08745Sheppo 		/*
53271ae08745Sheppo 		 * Check if in correct state to receive a multicast
53281ae08745Sheppo 		 * message (i.e. handshake complete). If not reset
53291ae08745Sheppo 		 * the handshake.
53301ae08745Sheppo 		 */
53311ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_MCST_INFO_RECV))
53321ae08745Sheppo 			return;
53331ae08745Sheppo 
53341ae08745Sheppo 		/*
53351ae08745Sheppo 		 * Before attempting to add or remove address check
53361ae08745Sheppo 		 * that they are valid multicast addresses.
53371ae08745Sheppo 		 * If not, then NACK back.
53381ae08745Sheppo 		 */
53391ae08745Sheppo 		for (i = 0; i < mcst_pkt->count; i++) {
53401ae08745Sheppo 			if ((mcst_pkt->mca[i].ether_addr_octet[0] & 01) != 1) {
53411ae08745Sheppo 				DERR(vswp, "%s: invalid multicast address",
53421ae08745Sheppo 								__func__);
53431ae08745Sheppo 				SND_MCST_NACK(ldcp, mcst_pkt);
53441ae08745Sheppo 				return;
53451ae08745Sheppo 			}
53461ae08745Sheppo 		}
53471ae08745Sheppo 
53481ae08745Sheppo 		/*
53491ae08745Sheppo 		 * Now add/remove the addresses. If this fails we
53501ae08745Sheppo 		 * NACK back.
53511ae08745Sheppo 		 */
53521ae08745Sheppo 		if (vsw_add_rem_mcst(mcst_pkt, port) != 0) {
53531ae08745Sheppo 			SND_MCST_NACK(ldcp, mcst_pkt);
53541ae08745Sheppo 			return;
53551ae08745Sheppo 		}
53561ae08745Sheppo 
53571ae08745Sheppo 		mcst_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
53581ae08745Sheppo 		mcst_pkt->tag.vio_sid = ldcp->local_session;
53591ae08745Sheppo 
53601ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)mcst_pkt);
53611ae08745Sheppo 
53621ae08745Sheppo 		vsw_send_msg(ldcp, (void *)mcst_pkt,
53631ae08745Sheppo 					sizeof (vnet_mcast_msg_t));
53641ae08745Sheppo 		break;
53651ae08745Sheppo 
53661ae08745Sheppo 	case VIO_SUBTYPE_ACK:
53671ae08745Sheppo 		DWARN(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
53681ae08745Sheppo 
53691ae08745Sheppo 		/*
53701ae08745Sheppo 		 * We shouldn't ever get a multicast ACK message as
53711ae08745Sheppo 		 * at the moment we never request multicast addresses
53721ae08745Sheppo 		 * to be set on some other device. This may change in
53731ae08745Sheppo 		 * the future if we have cascading switches.
53741ae08745Sheppo 		 */
53751ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_MCST_ACK_RECV))
53761ae08745Sheppo 			return;
53771ae08745Sheppo 
53781ae08745Sheppo 				/* Do nothing */
53791ae08745Sheppo 		break;
53801ae08745Sheppo 
53811ae08745Sheppo 	case VIO_SUBTYPE_NACK:
53821ae08745Sheppo 		DWARN(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
53831ae08745Sheppo 
53841ae08745Sheppo 		/*
53851ae08745Sheppo 		 * We shouldn't get a multicast NACK packet for the
53861ae08745Sheppo 		 * same reasons as we shouldn't get a ACK packet.
53871ae08745Sheppo 		 */
53881ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_MCST_NACK_RECV))
53891ae08745Sheppo 			return;
53901ae08745Sheppo 
53911ae08745Sheppo 				/* Do nothing */
53921ae08745Sheppo 		break;
53931ae08745Sheppo 
53941ae08745Sheppo 	default:
53951ae08745Sheppo 		DERR(vswp, "%s: unknown vio_subtype %x\n", __func__,
53961ae08745Sheppo 			mcst_pkt->tag.vio_subtype);
53971ae08745Sheppo 	}
53981ae08745Sheppo 
53991ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
54001ae08745Sheppo }
54011ae08745Sheppo 
54021ae08745Sheppo static void
54031ae08745Sheppo vsw_process_ctrl_rdx_pkt(vsw_ldc_t *ldcp, void *pkt)
54041ae08745Sheppo {
54051ae08745Sheppo 	vio_rdx_msg_t	*rdx_pkt;
54061ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
54071ae08745Sheppo 
54081ae08745Sheppo 	/*
54091ae08745Sheppo 	 * We know this is a ctrl/rdx packet so
54101ae08745Sheppo 	 * cast it into the correct structure.
54111ae08745Sheppo 	 */
54121ae08745Sheppo 	rdx_pkt = (vio_rdx_msg_t *)pkt;
54131ae08745Sheppo 
54141ae08745Sheppo 	D1(vswp, "%s(%lld) enter", __func__, ldcp->ldc_id);
54151ae08745Sheppo 
54161ae08745Sheppo 	switch (rdx_pkt->tag.vio_subtype) {
54171ae08745Sheppo 	case VIO_SUBTYPE_INFO:
54181ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
54191ae08745Sheppo 
54201ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_RDX_INFO_RECV))
54211ae08745Sheppo 			return;
54221ae08745Sheppo 
54231ae08745Sheppo 		rdx_pkt->tag.vio_sid = ldcp->local_session;
54241ae08745Sheppo 		rdx_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
54251ae08745Sheppo 
54261ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)rdx_pkt);
54271ae08745Sheppo 
54281ae08745Sheppo 		ldcp->lane_in.lstate |= VSW_RDX_ACK_SENT;
54291ae08745Sheppo 
54301ae08745Sheppo 		vsw_send_msg(ldcp, (void *)rdx_pkt,
54311ae08745Sheppo 				sizeof (vio_rdx_msg_t));
54321ae08745Sheppo 
54331ae08745Sheppo 		vsw_next_milestone(ldcp);
54341ae08745Sheppo 		break;
54351ae08745Sheppo 
54361ae08745Sheppo 	case VIO_SUBTYPE_ACK:
54371ae08745Sheppo 		/*
54381ae08745Sheppo 		 * Should be handled in-band by callback handler.
54391ae08745Sheppo 		 */
54401ae08745Sheppo 		DERR(vswp, "%s: Unexpected VIO_SUBTYPE_ACK", __func__);
54411ae08745Sheppo 		vsw_restart_handshake(ldcp);
54421ae08745Sheppo 		break;
54431ae08745Sheppo 
54441ae08745Sheppo 	case VIO_SUBTYPE_NACK:
54451ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
54461ae08745Sheppo 
54471ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_RDX_NACK_RECV))
54481ae08745Sheppo 			return;
54491ae08745Sheppo 
54501ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_RDX_NACK_RECV;
54511ae08745Sheppo 		vsw_next_milestone(ldcp);
54521ae08745Sheppo 		break;
54531ae08745Sheppo 
54541ae08745Sheppo 	default:
54551ae08745Sheppo 		DERR(vswp, "%s: Unknown vio_subtype %x\n", __func__,
54561ae08745Sheppo 			rdx_pkt->tag.vio_subtype);
54571ae08745Sheppo 	}
54581ae08745Sheppo 
54591ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
54601ae08745Sheppo }
54611ae08745Sheppo 
54621ae08745Sheppo static void
54631ae08745Sheppo vsw_process_data_pkt(vsw_ldc_t *ldcp, void *dpkt, vio_msg_tag_t tag)
54641ae08745Sheppo {
54651ae08745Sheppo 	uint16_t	env = tag.vio_subtype_env;
54661ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
54671ae08745Sheppo 
54681ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
54691ae08745Sheppo 
54701ae08745Sheppo 	/* session id check */
54711ae08745Sheppo 	if (ldcp->session_status & VSW_PEER_SESSION) {
54721ae08745Sheppo 		if (ldcp->peer_session != tag.vio_sid) {
54731ae08745Sheppo 			DERR(vswp, "%s (chan %d): invalid session id (%llx)",
54741ae08745Sheppo 				__func__, ldcp->ldc_id, tag.vio_sid);
54751ae08745Sheppo 			vsw_restart_handshake(ldcp);
54761ae08745Sheppo 			return;
54771ae08745Sheppo 		}
54781ae08745Sheppo 	}
54791ae08745Sheppo 
54801ae08745Sheppo 	/*
54811ae08745Sheppo 	 * It is an error for us to be getting data packets
54821ae08745Sheppo 	 * before the handshake has completed.
54831ae08745Sheppo 	 */
54841ae08745Sheppo 	if (ldcp->hphase != VSW_MILESTONE4) {
54851ae08745Sheppo 		DERR(vswp, "%s: got data packet before handshake complete "
54861ae08745Sheppo 			"hphase %d (%x: %x)", __func__, ldcp->hphase,
54871ae08745Sheppo 			ldcp->lane_in.lstate, ldcp->lane_out.lstate);
54881ae08745Sheppo 		DUMP_FLAGS(ldcp->lane_in.lstate);
54891ae08745Sheppo 		DUMP_FLAGS(ldcp->lane_out.lstate);
54901ae08745Sheppo 		vsw_restart_handshake(ldcp);
54911ae08745Sheppo 		return;
54921ae08745Sheppo 	}
54931ae08745Sheppo 
54941ae08745Sheppo 	/*
54951ae08745Sheppo 	 * Switch on vio_subtype envelope, then let lower routines
54961ae08745Sheppo 	 * decide if its an INFO, ACK or NACK packet.
54971ae08745Sheppo 	 */
54981ae08745Sheppo 	if (env == VIO_DRING_DATA) {
54991ae08745Sheppo 		vsw_process_data_dring_pkt(ldcp, dpkt);
55001ae08745Sheppo 	} else if (env == VIO_PKT_DATA) {
55011ae08745Sheppo 		vsw_process_data_raw_pkt(ldcp, dpkt);
55021ae08745Sheppo 	} else if (env == VIO_DESC_DATA) {
55031ae08745Sheppo 		vsw_process_data_ibnd_pkt(ldcp, dpkt);
55041ae08745Sheppo 	} else {
55051ae08745Sheppo 		DERR(vswp, "%s : unknown vio_subtype_env (%x)\n",
55061ae08745Sheppo 							__func__, env);
55071ae08745Sheppo 	}
55081ae08745Sheppo 
55091ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
55101ae08745Sheppo }
55111ae08745Sheppo 
55121ae08745Sheppo #define	SND_DRING_NACK(ldcp, pkt) \
55131ae08745Sheppo 	pkt->tag.vio_subtype = VIO_SUBTYPE_NACK; \
55141ae08745Sheppo 	pkt->tag.vio_sid = ldcp->local_session; \
55151ae08745Sheppo 	vsw_send_msg(ldcp, (void *)pkt, sizeof (vio_dring_msg_t));
55161ae08745Sheppo 
55171ae08745Sheppo static void
55181ae08745Sheppo vsw_process_data_dring_pkt(vsw_ldc_t *ldcp, void *dpkt)
55191ae08745Sheppo {
55201ae08745Sheppo 	vio_dring_msg_t		*dring_pkt;
55211ae08745Sheppo 	vnet_public_desc_t	*pub_addr = NULL;
55221ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
55231ae08745Sheppo 	dring_info_t		*dp = NULL;
55241ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
55251ae08745Sheppo 	mblk_t			*mp = NULL;
55261ae08745Sheppo 	mblk_t			*bp = NULL;
55271ae08745Sheppo 	mblk_t			*bpt = NULL;
55281ae08745Sheppo 	size_t			nbytes = 0;
55291ae08745Sheppo 	size_t			off = 0;
55301ae08745Sheppo 	uint64_t		ncookies = 0;
55311ae08745Sheppo 	uint64_t		chain = 0;
5532d10e4ef2Snarayan 	uint64_t		j, len;
5533d10e4ef2Snarayan 	uint32_t		pos, start, datalen;
5534d10e4ef2Snarayan 	uint32_t		range_start, range_end;
5535d10e4ef2Snarayan 	int32_t			end, num, cnt = 0;
5536d10e4ef2Snarayan 	int			i, rv;
55371ae08745Sheppo 	boolean_t		ack_needed = B_FALSE;
5538d10e4ef2Snarayan 	boolean_t		prev_desc_ack = B_FALSE;
5539d10e4ef2Snarayan 	int			read_attempts = 0;
55401ae08745Sheppo 
55411ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
55421ae08745Sheppo 
55431ae08745Sheppo 	/*
55441ae08745Sheppo 	 * We know this is a data/dring packet so
55451ae08745Sheppo 	 * cast it into the correct structure.
55461ae08745Sheppo 	 */
55471ae08745Sheppo 	dring_pkt = (vio_dring_msg_t *)dpkt;
55481ae08745Sheppo 
55491ae08745Sheppo 	/*
55501ae08745Sheppo 	 * Switch on the vio_subtype. If its INFO then we need to
55511ae08745Sheppo 	 * process the data. If its an ACK we need to make sure
55521ae08745Sheppo 	 * it makes sense (i.e did we send an earlier data/info),
55531ae08745Sheppo 	 * and if its a NACK then we maybe attempt a retry.
55541ae08745Sheppo 	 */
55551ae08745Sheppo 	switch (dring_pkt->tag.vio_subtype) {
55561ae08745Sheppo 	case VIO_SUBTYPE_INFO:
55571ae08745Sheppo 		D2(vswp, "%s(%lld): VIO_SUBTYPE_INFO", __func__, ldcp->ldc_id);
55581ae08745Sheppo 
5559*445b4c2eSsb155480 		READ_ENTER(&ldcp->lane_in.dlistrw);
55601ae08745Sheppo 		if ((dp = vsw_ident2dring(&ldcp->lane_in,
55611ae08745Sheppo 				dring_pkt->dring_ident)) == NULL) {
5562*445b4c2eSsb155480 			RW_EXIT(&ldcp->lane_in.dlistrw);
55631ae08745Sheppo 
55641ae08745Sheppo 			DERR(vswp, "%s(%lld): unable to find dring from "
55651ae08745Sheppo 				"ident 0x%llx", __func__, ldcp->ldc_id,
55661ae08745Sheppo 				dring_pkt->dring_ident);
55671ae08745Sheppo 
55681ae08745Sheppo 			SND_DRING_NACK(ldcp, dring_pkt);
55691ae08745Sheppo 			return;
55701ae08745Sheppo 		}
55711ae08745Sheppo 
5572d10e4ef2Snarayan 		start = pos = dring_pkt->start_idx;
55731ae08745Sheppo 		end = dring_pkt->end_idx;
5574d10e4ef2Snarayan 		len = dp->num_descriptors;
55751ae08745Sheppo 
5576d10e4ef2Snarayan 		range_start = range_end = pos;
5577d10e4ef2Snarayan 
5578d10e4ef2Snarayan 		D2(vswp, "%s(%lld): start index %ld : end %ld\n",
55791ae08745Sheppo 			__func__, ldcp->ldc_id, start, end);
55801ae08745Sheppo 
5581d10e4ef2Snarayan 		if (end == -1) {
5582d10e4ef2Snarayan 			num = -1;
55834bac2208Snarayan 		} else if (end >= 0) {
5584d10e4ef2Snarayan 			num = end >= pos ?
5585d10e4ef2Snarayan 				end - pos + 1: (len - pos + 1) + end;
5586d10e4ef2Snarayan 
55871ae08745Sheppo 			/* basic sanity check */
55881ae08745Sheppo 			if (end > len) {
5589*445b4c2eSsb155480 				RW_EXIT(&ldcp->lane_in.dlistrw);
5590d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): endpoint %lld outside "
5591d10e4ef2Snarayan 					"ring length %lld", __func__,
5592d10e4ef2Snarayan 					ldcp->ldc_id, end, len);
55931ae08745Sheppo 
55941ae08745Sheppo 				SND_DRING_NACK(ldcp, dring_pkt);
55951ae08745Sheppo 				return;
55961ae08745Sheppo 			}
5597d10e4ef2Snarayan 		} else {
5598*445b4c2eSsb155480 			RW_EXIT(&ldcp->lane_in.dlistrw);
5599d10e4ef2Snarayan 			DERR(vswp, "%s(%lld): invalid endpoint %lld",
5600d10e4ef2Snarayan 				__func__, ldcp->ldc_id, end);
5601d10e4ef2Snarayan 			SND_DRING_NACK(ldcp, dring_pkt);
56021ae08745Sheppo 			return;
56031ae08745Sheppo 		}
56041ae08745Sheppo 
5605d10e4ef2Snarayan 		while (cnt != num) {
5606d10e4ef2Snarayan vsw_recheck_desc:
5607d10e4ef2Snarayan 			if ((rv = ldc_mem_dring_acquire(dp->handle,
5608d10e4ef2Snarayan 							pos, pos)) != 0) {
5609*445b4c2eSsb155480 				RW_EXIT(&ldcp->lane_in.dlistrw);
5610d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): unable to acquire "
5611d10e4ef2Snarayan 					"descriptor at pos %d: err %d",
5612d10e4ef2Snarayan 					__func__, pos, ldcp->ldc_id, rv);
5613d10e4ef2Snarayan 				SND_DRING_NACK(ldcp, dring_pkt);
5614d10e4ef2Snarayan 				return;
5615d10e4ef2Snarayan 			}
56161ae08745Sheppo 
5617d10e4ef2Snarayan 			pub_addr = (vnet_public_desc_t *)dp->pub_addr + pos;
56181ae08745Sheppo 
5619d10e4ef2Snarayan 			/*
5620d10e4ef2Snarayan 			 * When given a bounded range of descriptors
5621d10e4ef2Snarayan 			 * to process, its an error to hit a descriptor
5622d10e4ef2Snarayan 			 * which is not ready. In the non-bounded case
5623d10e4ef2Snarayan 			 * (end_idx == -1) this simply indicates we have
5624d10e4ef2Snarayan 			 * reached the end of the current active range.
5625d10e4ef2Snarayan 			 */
5626d10e4ef2Snarayan 			if (pub_addr->hdr.dstate != VIO_DESC_READY) {
5627d10e4ef2Snarayan 				/* unbound - no error */
5628d10e4ef2Snarayan 				if (end == -1) {
5629d10e4ef2Snarayan 					if (read_attempts == vsw_read_attempts)
5630d10e4ef2Snarayan 						break;
56311ae08745Sheppo 
5632d10e4ef2Snarayan 					delay(drv_usectohz(vsw_desc_delay));
5633d10e4ef2Snarayan 					read_attempts++;
5634d10e4ef2Snarayan 					goto vsw_recheck_desc;
5635d10e4ef2Snarayan 				}
56361ae08745Sheppo 
5637d10e4ef2Snarayan 				/* bounded - error - so NACK back */
5638*445b4c2eSsb155480 				RW_EXIT(&ldcp->lane_in.dlistrw);
5639d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): descriptor not READY "
5640d10e4ef2Snarayan 					"(%d)", __func__, ldcp->ldc_id,
5641d10e4ef2Snarayan 					pub_addr->hdr.dstate);
5642d10e4ef2Snarayan 				SND_DRING_NACK(ldcp, dring_pkt);
5643d10e4ef2Snarayan 				return;
5644d10e4ef2Snarayan 			}
5645d10e4ef2Snarayan 
5646d10e4ef2Snarayan 			DTRACE_PROBE1(read_attempts, int, read_attempts);
5647d10e4ef2Snarayan 
5648d10e4ef2Snarayan 			range_end = pos;
5649d10e4ef2Snarayan 
5650d10e4ef2Snarayan 			/*
5651d10e4ef2Snarayan 			 * If we ACK'd the previous descriptor then now
5652d10e4ef2Snarayan 			 * record the new range start position for later
5653d10e4ef2Snarayan 			 * ACK's.
5654d10e4ef2Snarayan 			 */
5655d10e4ef2Snarayan 			if (prev_desc_ack) {
5656d10e4ef2Snarayan 				range_start = pos;
5657d10e4ef2Snarayan 
5658d10e4ef2Snarayan 				D2(vswp, "%s(%lld): updating range start "
5659d10e4ef2Snarayan 					"to be %d", __func__, ldcp->ldc_id,
5660d10e4ef2Snarayan 					range_start);
5661d10e4ef2Snarayan 
5662d10e4ef2Snarayan 				prev_desc_ack = B_FALSE;
5663d10e4ef2Snarayan 			}
56641ae08745Sheppo 
56651ae08745Sheppo 			/*
56661ae08745Sheppo 			 * Data is padded to align on 8 byte boundary,
56671ae08745Sheppo 			 * datalen is actual data length, i.e. minus that
56681ae08745Sheppo 			 * padding.
56691ae08745Sheppo 			 */
56701ae08745Sheppo 			datalen = pub_addr->nbytes;
56711ae08745Sheppo 
56721ae08745Sheppo 			/*
56731ae08745Sheppo 			 * Does peer wish us to ACK when we have finished
56741ae08745Sheppo 			 * with this descriptor ?
56751ae08745Sheppo 			 */
56761ae08745Sheppo 			if (pub_addr->hdr.ack)
56771ae08745Sheppo 				ack_needed = B_TRUE;
56781ae08745Sheppo 
56791ae08745Sheppo 			D2(vswp, "%s(%lld): processing desc %lld at pos"
56801ae08745Sheppo 				" 0x%llx : dstate 0x%lx : datalen 0x%lx",
5681d10e4ef2Snarayan 				__func__, ldcp->ldc_id, pos, pub_addr,
56821ae08745Sheppo 				pub_addr->hdr.dstate, datalen);
56831ae08745Sheppo 
56841ae08745Sheppo 			/*
56851ae08745Sheppo 			 * Mark that we are starting to process descriptor.
56861ae08745Sheppo 			 */
56871ae08745Sheppo 			pub_addr->hdr.dstate = VIO_DESC_ACCEPTED;
56881ae08745Sheppo 
5689d10e4ef2Snarayan 			mp = vio_allocb(ldcp->rxh);
5690d10e4ef2Snarayan 			if (mp == NULL) {
56911ae08745Sheppo 				/*
5692d10e4ef2Snarayan 				 * No free receive buffers available, so
5693d10e4ef2Snarayan 				 * fallback onto allocb(9F). Make sure that
5694d10e4ef2Snarayan 				 * we get a data buffer which is a multiple
5695d10e4ef2Snarayan 				 * of 8 as this is required by ldc_mem_copy.
56961ae08745Sheppo 				 */
5697d10e4ef2Snarayan 				DTRACE_PROBE(allocb);
5698d10e4ef2Snarayan 				mp = allocb(datalen + VNET_IPALIGN + 8,
5699d10e4ef2Snarayan 								BPRI_MED);
5700d10e4ef2Snarayan 			}
5701d10e4ef2Snarayan 
5702d10e4ef2Snarayan 			/*
5703d10e4ef2Snarayan 			 * Ensure that we ask ldc for an aligned
5704d10e4ef2Snarayan 			 * number of bytes.
5705d10e4ef2Snarayan 			 */
5706d10e4ef2Snarayan 			nbytes = datalen + VNET_IPALIGN;
57071ae08745Sheppo 			if (nbytes & 0x7) {
57081ae08745Sheppo 				off = 8 - (nbytes & 0x7);
57091ae08745Sheppo 				nbytes += off;
57101ae08745Sheppo 			}
57111ae08745Sheppo 
57121ae08745Sheppo 			ncookies = pub_addr->ncookies;
57131ae08745Sheppo 			rv = ldc_mem_copy(ldcp->ldc_handle,
57141ae08745Sheppo 				(caddr_t)mp->b_rptr, 0, &nbytes,
57151ae08745Sheppo 				pub_addr->memcookie, ncookies,
57161ae08745Sheppo 				LDC_COPY_IN);
57171ae08745Sheppo 
57181ae08745Sheppo 			if (rv != 0) {
57191ae08745Sheppo 				DERR(vswp, "%s(%d): unable to copy in "
5720d10e4ef2Snarayan 					"data from %d cookies in desc %d"
5721d10e4ef2Snarayan 					" (rv %d)", __func__, ldcp->ldc_id,
5722d10e4ef2Snarayan 					ncookies, pos, rv);
57231ae08745Sheppo 				freemsg(mp);
5724d10e4ef2Snarayan 
5725d10e4ef2Snarayan 				pub_addr->hdr.dstate = VIO_DESC_DONE;
57261ae08745Sheppo 				(void) ldc_mem_dring_release(dp->handle,
5727d10e4ef2Snarayan 								pos, pos);
5728d10e4ef2Snarayan 				break;
57291ae08745Sheppo 			} else {
57301ae08745Sheppo 				D2(vswp, "%s(%d): copied in %ld bytes"
57311ae08745Sheppo 					" using %d cookies", __func__,
57321ae08745Sheppo 					ldcp->ldc_id, nbytes, ncookies);
57331ae08745Sheppo 			}
57341ae08745Sheppo 
5735d10e4ef2Snarayan 			/* adjust the read pointer to skip over the padding */
5736d10e4ef2Snarayan 			mp->b_rptr += VNET_IPALIGN;
5737d10e4ef2Snarayan 
57381ae08745Sheppo 			/* point to the actual end of data */
57391ae08745Sheppo 			mp->b_wptr = mp->b_rptr + datalen;
57401ae08745Sheppo 
57411ae08745Sheppo 			/* build a chain of received packets */
57421ae08745Sheppo 			if (bp == NULL) {
57431ae08745Sheppo 				/* first pkt */
57441ae08745Sheppo 				bp = mp;
57451ae08745Sheppo 				bp->b_next = bp->b_prev = NULL;
57461ae08745Sheppo 				bpt = bp;
57471ae08745Sheppo 				chain = 1;
57481ae08745Sheppo 			} else {
57491ae08745Sheppo 				mp->b_next = NULL;
57501ae08745Sheppo 				mp->b_prev = bpt;
57511ae08745Sheppo 				bpt->b_next = mp;
57521ae08745Sheppo 				bpt = mp;
57531ae08745Sheppo 				chain++;
57541ae08745Sheppo 			}
57551ae08745Sheppo 
57561ae08745Sheppo 			/* mark we are finished with this descriptor */
57571ae08745Sheppo 			pub_addr->hdr.dstate = VIO_DESC_DONE;
57581ae08745Sheppo 
5759d10e4ef2Snarayan 			(void) ldc_mem_dring_release(dp->handle, pos, pos);
5760d10e4ef2Snarayan 
57611ae08745Sheppo 			/*
5762d10e4ef2Snarayan 			 * Send an ACK back to peer if requested.
57631ae08745Sheppo 			 */
57641ae08745Sheppo 			if (ack_needed) {
57651ae08745Sheppo 				ack_needed = B_FALSE;
57661ae08745Sheppo 
5767d10e4ef2Snarayan 				dring_pkt->start_idx = range_start;
5768d10e4ef2Snarayan 				dring_pkt->end_idx = range_end;
57691ae08745Sheppo 
5770d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): processed %d %d, ACK"
5771d10e4ef2Snarayan 					" requested", __func__, ldcp->ldc_id,
5772d10e4ef2Snarayan 					dring_pkt->start_idx,
5773d10e4ef2Snarayan 					dring_pkt->end_idx);
57741ae08745Sheppo 
5775d10e4ef2Snarayan 				dring_pkt->dring_process_state = VIO_DP_ACTIVE;
57761ae08745Sheppo 				dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
57771ae08745Sheppo 				dring_pkt->tag.vio_sid = ldcp->local_session;
57781ae08745Sheppo 				vsw_send_msg(ldcp, (void *)dring_pkt,
57791ae08745Sheppo 					sizeof (vio_dring_msg_t));
5780d10e4ef2Snarayan 
5781d10e4ef2Snarayan 				prev_desc_ack = B_TRUE;
5782d10e4ef2Snarayan 				range_start = pos;
57831ae08745Sheppo 			}
57841ae08745Sheppo 
5785d10e4ef2Snarayan 			/* next descriptor */
5786d10e4ef2Snarayan 			pos = (pos + 1) % len;
5787d10e4ef2Snarayan 			cnt++;
5788d10e4ef2Snarayan 
5789d10e4ef2Snarayan 			/*
5790d10e4ef2Snarayan 			 * Break out of loop here and stop processing to
5791d10e4ef2Snarayan 			 * allow some other network device (or disk) to
5792d10e4ef2Snarayan 			 * get access to the cpu.
5793d10e4ef2Snarayan 			 */
5794d10e4ef2Snarayan 			/* send the chain of packets to be switched */
5795d10e4ef2Snarayan 			if (chain > vsw_chain_len) {
5796d10e4ef2Snarayan 				D3(vswp, "%s(%lld): switching chain of %d "
5797d10e4ef2Snarayan 					"msgs", __func__, ldcp->ldc_id, chain);
579834683adeSsg70180 				vswp->vsw_switch_frame(vswp, bp, VSW_VNETPORT,
5799d10e4ef2Snarayan 							ldcp->ldc_port, NULL);
5800d10e4ef2Snarayan 				bp = NULL;
5801d10e4ef2Snarayan 				break;
58021ae08745Sheppo 			}
58031ae08745Sheppo 		}
5804*445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_in.dlistrw);
58051ae08745Sheppo 
58061ae08745Sheppo 		/* send the chain of packets to be switched */
5807d10e4ef2Snarayan 		if (bp != NULL) {
5808d10e4ef2Snarayan 			D3(vswp, "%s(%lld): switching chain of %d msgs",
5809d10e4ef2Snarayan 					__func__, ldcp->ldc_id, chain);
581034683adeSsg70180 			vswp->vsw_switch_frame(vswp, bp, VSW_VNETPORT,
58111ae08745Sheppo 							ldcp->ldc_port, NULL);
5812d10e4ef2Snarayan 		}
58131ae08745Sheppo 
5814d10e4ef2Snarayan 		DTRACE_PROBE1(msg_cnt, int, cnt);
5815d10e4ef2Snarayan 
5816d10e4ef2Snarayan 		/*
5817d10e4ef2Snarayan 		 * We are now finished so ACK back with the state
5818d10e4ef2Snarayan 		 * set to STOPPING so our peer knows we are finished
5819d10e4ef2Snarayan 		 */
5820d10e4ef2Snarayan 		dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
5821d10e4ef2Snarayan 		dring_pkt->tag.vio_sid = ldcp->local_session;
5822d10e4ef2Snarayan 
5823d10e4ef2Snarayan 		dring_pkt->dring_process_state = VIO_DP_STOPPED;
5824d10e4ef2Snarayan 
5825d10e4ef2Snarayan 		DTRACE_PROBE(stop_process_sent);
5826d10e4ef2Snarayan 
5827d10e4ef2Snarayan 		/*
5828d10e4ef2Snarayan 		 * We have not processed any more descriptors beyond
5829d10e4ef2Snarayan 		 * the last one we ACK'd.
5830d10e4ef2Snarayan 		 */
5831d10e4ef2Snarayan 		if (prev_desc_ack)
5832d10e4ef2Snarayan 			range_start = range_end;
5833d10e4ef2Snarayan 
5834d10e4ef2Snarayan 		dring_pkt->start_idx = range_start;
5835d10e4ef2Snarayan 		dring_pkt->end_idx = range_end;
5836d10e4ef2Snarayan 
5837d10e4ef2Snarayan 		D2(vswp, "%s(%lld) processed : %d : %d, now stopping",
5838d10e4ef2Snarayan 			__func__, ldcp->ldc_id, dring_pkt->start_idx,
5839d10e4ef2Snarayan 			dring_pkt->end_idx);
5840d10e4ef2Snarayan 
5841d10e4ef2Snarayan 		vsw_send_msg(ldcp, (void *)dring_pkt,
5842d10e4ef2Snarayan 					sizeof (vio_dring_msg_t));
58431ae08745Sheppo 		break;
58441ae08745Sheppo 
58451ae08745Sheppo 	case VIO_SUBTYPE_ACK:
58461ae08745Sheppo 		D2(vswp, "%s(%lld): VIO_SUBTYPE_ACK", __func__, ldcp->ldc_id);
58471ae08745Sheppo 		/*
58481ae08745Sheppo 		 * Verify that the relevant descriptors are all
58491ae08745Sheppo 		 * marked as DONE
58501ae08745Sheppo 		 */
5851*445b4c2eSsb155480 		READ_ENTER(&ldcp->lane_out.dlistrw);
58521ae08745Sheppo 		if ((dp = vsw_ident2dring(&ldcp->lane_out,
58531ae08745Sheppo 			dring_pkt->dring_ident)) == NULL) {
5854*445b4c2eSsb155480 			RW_EXIT(&ldcp->lane_out.dlistrw);
58551ae08745Sheppo 			DERR(vswp, "%s: unknown ident in ACK", __func__);
58561ae08745Sheppo 			return;
58571ae08745Sheppo 		}
58581ae08745Sheppo 
58591ae08745Sheppo 		pub_addr = (vnet_public_desc_t *)dp->pub_addr;
58601ae08745Sheppo 		priv_addr = (vsw_private_desc_t *)dp->priv_addr;
58611ae08745Sheppo 
58621ae08745Sheppo 		start = end = 0;
58631ae08745Sheppo 		start = dring_pkt->start_idx;
58641ae08745Sheppo 		end = dring_pkt->end_idx;
58651ae08745Sheppo 		len = dp->num_descriptors;
58661ae08745Sheppo 
58671ae08745Sheppo 		j = num = 0;
58681ae08745Sheppo 		/* calculate # descriptors taking into a/c wrap around */
58691ae08745Sheppo 		num = end >= start ? end - start + 1: (len - start + 1) + end;
58701ae08745Sheppo 
58711ae08745Sheppo 		D2(vswp, "%s(%lld): start index %ld : end %ld : num %ld\n",
58721ae08745Sheppo 			__func__, ldcp->ldc_id, start, end, num);
58731ae08745Sheppo 
5874d10e4ef2Snarayan 		mutex_enter(&dp->dlock);
5875d10e4ef2Snarayan 		dp->last_ack_recv = end;
5876d10e4ef2Snarayan 		mutex_exit(&dp->dlock);
5877d10e4ef2Snarayan 
58781ae08745Sheppo 		for (i = start; j < num; i = (i + 1) % len, j++) {
58791ae08745Sheppo 			pub_addr = (vnet_public_desc_t *)dp->pub_addr + i;
58801ae08745Sheppo 			priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
58811ae08745Sheppo 
5882d10e4ef2Snarayan 			/*
5883d10e4ef2Snarayan 			 * If the last descriptor in a range has the ACK
5884d10e4ef2Snarayan 			 * bit set then we will get two messages from our
5885d10e4ef2Snarayan 			 * peer relating to it. The normal ACK msg and then
5886d10e4ef2Snarayan 			 * a subsequent STOP msg. The first message will have
5887d10e4ef2Snarayan 			 * resulted in the descriptor being reclaimed and
5888d10e4ef2Snarayan 			 * its state set to FREE so when we encounter a non
5889d10e4ef2Snarayan 			 * DONE descriptor we need to check to see if its
5890d10e4ef2Snarayan 			 * because we have just reclaimed it.
5891d10e4ef2Snarayan 			 */
5892d10e4ef2Snarayan 			mutex_enter(&priv_addr->dstate_lock);
5893d10e4ef2Snarayan 			if (pub_addr->hdr.dstate == VIO_DESC_DONE) {
58941ae08745Sheppo 				/* clear all the fields */
58951ae08745Sheppo 				bzero(priv_addr->datap, priv_addr->datalen);
58961ae08745Sheppo 				priv_addr->datalen = 0;
58971ae08745Sheppo 
58981ae08745Sheppo 				pub_addr->hdr.dstate = VIO_DESC_FREE;
58991ae08745Sheppo 				pub_addr->hdr.ack = 0;
5900d10e4ef2Snarayan 
59011ae08745Sheppo 				priv_addr->dstate = VIO_DESC_FREE;
5902d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
59031ae08745Sheppo 
59041ae08745Sheppo 				D3(vswp, "clearing descp %d : pub state "
59051ae08745Sheppo 					"0x%llx : priv state 0x%llx", i,
59061ae08745Sheppo 					pub_addr->hdr.dstate,
59071ae08745Sheppo 					priv_addr->dstate);
5908d10e4ef2Snarayan 
5909d10e4ef2Snarayan 			} else {
5910d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
5911d10e4ef2Snarayan 
5912d10e4ef2Snarayan 				if (dring_pkt->dring_process_state !=
5913d10e4ef2Snarayan 							VIO_DP_STOPPED) {
5914d10e4ef2Snarayan 					DERR(vswp, "%s: descriptor %lld at pos "
5915d10e4ef2Snarayan 						" 0x%llx not DONE (0x%lx)\n",
5916d10e4ef2Snarayan 						__func__, i, pub_addr,
5917d10e4ef2Snarayan 						pub_addr->hdr.dstate);
5918*445b4c2eSsb155480 					RW_EXIT(&ldcp->lane_out.dlistrw);
5919d10e4ef2Snarayan 					return;
5920d10e4ef2Snarayan 				}
59211ae08745Sheppo 			}
59221ae08745Sheppo 		}
59231ae08745Sheppo 
5924d10e4ef2Snarayan 		/*
5925d10e4ef2Snarayan 		 * If our peer is stopping processing descriptors then
5926d10e4ef2Snarayan 		 * we check to make sure it has processed all the descriptors
5927d10e4ef2Snarayan 		 * we have updated. If not then we send it a new message
5928d10e4ef2Snarayan 		 * to prompt it to restart.
5929d10e4ef2Snarayan 		 */
5930d10e4ef2Snarayan 		if (dring_pkt->dring_process_state == VIO_DP_STOPPED) {
5931d10e4ef2Snarayan 			DTRACE_PROBE(stop_process_recv);
5932d10e4ef2Snarayan 			D2(vswp, "%s(%lld): got stopping msg : %d : %d",
5933d10e4ef2Snarayan 				__func__, ldcp->ldc_id, dring_pkt->start_idx,
5934d10e4ef2Snarayan 				dring_pkt->end_idx);
5935d10e4ef2Snarayan 
5936d10e4ef2Snarayan 			/*
5937d10e4ef2Snarayan 			 * Check next descriptor in public section of ring.
5938d10e4ef2Snarayan 			 * If its marked as READY then we need to prompt our
5939d10e4ef2Snarayan 			 * peer to start processing the ring again.
5940d10e4ef2Snarayan 			 */
5941d10e4ef2Snarayan 			i = (end + 1) % len;
5942d10e4ef2Snarayan 			pub_addr = (vnet_public_desc_t *)dp->pub_addr + i;
5943d10e4ef2Snarayan 			priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
5944d10e4ef2Snarayan 
5945d10e4ef2Snarayan 			/*
5946d10e4ef2Snarayan 			 * Hold the restart lock across all of this to
5947d10e4ef2Snarayan 			 * make sure that its not possible for us to
5948d10e4ef2Snarayan 			 * decide that a msg needs to be sent in the future
5949d10e4ef2Snarayan 			 * but the sending code having already checked is
5950d10e4ef2Snarayan 			 * about to exit.
5951d10e4ef2Snarayan 			 */
5952d10e4ef2Snarayan 			mutex_enter(&dp->restart_lock);
5953d10e4ef2Snarayan 			mutex_enter(&priv_addr->dstate_lock);
5954d10e4ef2Snarayan 			if (pub_addr->hdr.dstate == VIO_DESC_READY) {
5955d10e4ef2Snarayan 
5956d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
5957d10e4ef2Snarayan 
5958d10e4ef2Snarayan 				dring_pkt->tag.vio_subtype = VIO_SUBTYPE_INFO;
5959d10e4ef2Snarayan 				dring_pkt->tag.vio_sid = ldcp->local_session;
5960d10e4ef2Snarayan 
5961d10e4ef2Snarayan 				mutex_enter(&ldcp->lane_out.seq_lock);
5962d10e4ef2Snarayan 				dring_pkt->seq_num = ldcp->lane_out.seq_num++;
5963d10e4ef2Snarayan 				mutex_exit(&ldcp->lane_out.seq_lock);
5964d10e4ef2Snarayan 
5965d10e4ef2Snarayan 				dring_pkt->start_idx = (end + 1) % len;
5966d10e4ef2Snarayan 				dring_pkt->end_idx = -1;
5967d10e4ef2Snarayan 
5968d10e4ef2Snarayan 				D2(vswp, "%s(%lld) : sending restart msg:"
5969d10e4ef2Snarayan 					" %d : %d", __func__, ldcp->ldc_id,
5970d10e4ef2Snarayan 					dring_pkt->start_idx,
5971d10e4ef2Snarayan 					dring_pkt->end_idx);
5972d10e4ef2Snarayan 
5973d10e4ef2Snarayan 				vsw_send_msg(ldcp, (void *)dring_pkt,
5974d10e4ef2Snarayan 						sizeof (vio_dring_msg_t));
5975d10e4ef2Snarayan 			} else {
5976d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
5977d10e4ef2Snarayan 				dp->restart_reqd = B_TRUE;
5978d10e4ef2Snarayan 			}
5979d10e4ef2Snarayan 			mutex_exit(&dp->restart_lock);
5980d10e4ef2Snarayan 		}
5981*445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_out.dlistrw);
59821ae08745Sheppo 		break;
59831ae08745Sheppo 
59841ae08745Sheppo 	case VIO_SUBTYPE_NACK:
59851ae08745Sheppo 		DWARN(vswp, "%s(%lld): VIO_SUBTYPE_NACK",
59861ae08745Sheppo 						__func__, ldcp->ldc_id);
59871ae08745Sheppo 		/*
59881ae08745Sheppo 		 * Something is badly wrong if we are getting NACK's
59891ae08745Sheppo 		 * for our data pkts. So reset the channel.
59901ae08745Sheppo 		 */
59911ae08745Sheppo 		vsw_restart_handshake(ldcp);
59921ae08745Sheppo 
59931ae08745Sheppo 		break;
59941ae08745Sheppo 
59951ae08745Sheppo 	default:
59961ae08745Sheppo 		DERR(vswp, "%s(%lld): Unknown vio_subtype %x\n", __func__,
59971ae08745Sheppo 			ldcp->ldc_id, dring_pkt->tag.vio_subtype);
59981ae08745Sheppo 	}
59991ae08745Sheppo 
60001ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
60011ae08745Sheppo }
60021ae08745Sheppo 
60031ae08745Sheppo /*
60041ae08745Sheppo  * VIO_PKT_DATA (a.k.a raw data mode )
60051ae08745Sheppo  *
60061ae08745Sheppo  * Note - currently not supported. Do nothing.
60071ae08745Sheppo  */
60081ae08745Sheppo static void
60091ae08745Sheppo vsw_process_data_raw_pkt(vsw_ldc_t *ldcp, void *dpkt)
60101ae08745Sheppo {
60111ae08745Sheppo 	_NOTE(ARGUNUSED(dpkt))
60121ae08745Sheppo 
60131ae08745Sheppo 	D1(NULL, "%s (%lld): enter\n", __func__, ldcp->ldc_id);
60141ae08745Sheppo 
60151ae08745Sheppo 	DERR(NULL, "%s (%lld): currently  not supported",
60161ae08745Sheppo 						__func__, ldcp->ldc_id);
60171ae08745Sheppo 
60181ae08745Sheppo 	D1(NULL, "%s (%lld): exit\n", __func__, ldcp->ldc_id);
60191ae08745Sheppo }
60201ae08745Sheppo 
60211ae08745Sheppo /*
60221ae08745Sheppo  * Process an in-band descriptor message (most likely from
60231ae08745Sheppo  * OBP).
60241ae08745Sheppo  */
60251ae08745Sheppo static void
60261ae08745Sheppo vsw_process_data_ibnd_pkt(vsw_ldc_t *ldcp, void *pkt)
60271ae08745Sheppo {
6028*445b4c2eSsb155480 	vnet_ibnd_desc_t	*ibnd_desc;
60291ae08745Sheppo 	dring_info_t		*dp = NULL;
60301ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
60311ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
60321ae08745Sheppo 	mblk_t			*mp = NULL;
60331ae08745Sheppo 	size_t			nbytes = 0;
60341ae08745Sheppo 	size_t			off = 0;
60351ae08745Sheppo 	uint64_t		idx = 0;
60364bac2208Snarayan 	uint32_t		num = 1, len, datalen = 0;
60371ae08745Sheppo 	uint64_t		ncookies = 0;
60384bac2208Snarayan 	int			i, rv;
60394bac2208Snarayan 	int			j = 0;
60401ae08745Sheppo 
60411ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
60421ae08745Sheppo 
6043*445b4c2eSsb155480 	ibnd_desc = (vnet_ibnd_desc_t *)pkt;
60441ae08745Sheppo 
60451ae08745Sheppo 	switch (ibnd_desc->hdr.tag.vio_subtype) {
60461ae08745Sheppo 	case VIO_SUBTYPE_INFO:
60471ae08745Sheppo 		D1(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
60481ae08745Sheppo 
60491ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_DRING_INFO_RECV))
60501ae08745Sheppo 			return;
60511ae08745Sheppo 
60521ae08745Sheppo 		/*
60531ae08745Sheppo 		 * Data is padded to align on a 8 byte boundary,
60541ae08745Sheppo 		 * nbytes is actual data length, i.e. minus that
60551ae08745Sheppo 		 * padding.
60561ae08745Sheppo 		 */
60571ae08745Sheppo 		datalen = ibnd_desc->nbytes;
60581ae08745Sheppo 
60591ae08745Sheppo 		D2(vswp, "%s(%lld): processing inband desc : "
60601ae08745Sheppo 			": datalen 0x%lx", __func__, ldcp->ldc_id, datalen);
60611ae08745Sheppo 
60621ae08745Sheppo 		ncookies = ibnd_desc->ncookies;
60631ae08745Sheppo 
60641ae08745Sheppo 		/*
60651ae08745Sheppo 		 * allocb(9F) returns an aligned data block. We
60661ae08745Sheppo 		 * need to ensure that we ask ldc for an aligned
60671ae08745Sheppo 		 * number of bytes also.
60681ae08745Sheppo 		 */
60691ae08745Sheppo 		nbytes = datalen;
60701ae08745Sheppo 		if (nbytes & 0x7) {
60711ae08745Sheppo 			off = 8 - (nbytes & 0x7);
60721ae08745Sheppo 			nbytes += off;
60731ae08745Sheppo 		}
60741ae08745Sheppo 
60751ae08745Sheppo 		mp = allocb(datalen, BPRI_MED);
60761ae08745Sheppo 		if (mp == NULL) {
60771ae08745Sheppo 			DERR(vswp, "%s(%lld): allocb failed",
60781ae08745Sheppo 					__func__, ldcp->ldc_id);
60791ae08745Sheppo 			return;
60801ae08745Sheppo 		}
60811ae08745Sheppo 
60821ae08745Sheppo 		rv = ldc_mem_copy(ldcp->ldc_handle, (caddr_t)mp->b_rptr,
60831ae08745Sheppo 			0, &nbytes, ibnd_desc->memcookie, (uint64_t)ncookies,
60841ae08745Sheppo 			LDC_COPY_IN);
60851ae08745Sheppo 
60861ae08745Sheppo 		if (rv != 0) {
60871ae08745Sheppo 			DERR(vswp, "%s(%d): unable to copy in data from "
60881ae08745Sheppo 				"%d cookie(s)", __func__,
60891ae08745Sheppo 				ldcp->ldc_id, ncookies);
60901ae08745Sheppo 			freemsg(mp);
60911ae08745Sheppo 			return;
60921ae08745Sheppo 		} else {
60931ae08745Sheppo 			D2(vswp, "%s(%d): copied in %ld bytes using %d "
60941ae08745Sheppo 				"cookies", __func__, ldcp->ldc_id, nbytes,
60951ae08745Sheppo 				ncookies);
60961ae08745Sheppo 		}
60971ae08745Sheppo 
60981ae08745Sheppo 		/* point to the actual end of data */
60991ae08745Sheppo 		mp->b_wptr = mp->b_rptr + datalen;
61001ae08745Sheppo 
61011ae08745Sheppo 		/*
61021ae08745Sheppo 		 * We ACK back every in-band descriptor message we process
61031ae08745Sheppo 		 */
61041ae08745Sheppo 		ibnd_desc->hdr.tag.vio_subtype = VIO_SUBTYPE_ACK;
61051ae08745Sheppo 		ibnd_desc->hdr.tag.vio_sid = ldcp->local_session;
61061ae08745Sheppo 		vsw_send_msg(ldcp, (void *)ibnd_desc,
6107*445b4c2eSsb155480 				sizeof (vnet_ibnd_desc_t));
61081ae08745Sheppo 
61091ae08745Sheppo 		/* send the packet to be switched */
611034683adeSsg70180 		vswp->vsw_switch_frame(vswp, mp, VSW_VNETPORT,
61111ae08745Sheppo 					ldcp->ldc_port, NULL);
61121ae08745Sheppo 
61131ae08745Sheppo 		break;
61141ae08745Sheppo 
61151ae08745Sheppo 	case VIO_SUBTYPE_ACK:
61161ae08745Sheppo 		D1(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
61171ae08745Sheppo 
61181ae08745Sheppo 		/* Verify the ACK is valid */
61191ae08745Sheppo 		idx = ibnd_desc->hdr.desc_handle;
61201ae08745Sheppo 
61211ae08745Sheppo 		if (idx >= VSW_RING_NUM_EL) {
612234683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: corrupted ACK received "
612334683adeSsg70180 				"(idx %ld)", vswp->instance, idx);
61241ae08745Sheppo 			return;
61251ae08745Sheppo 		}
61261ae08745Sheppo 
61271ae08745Sheppo 		if ((dp = ldcp->lane_out.dringp) == NULL) {
61281ae08745Sheppo 			DERR(vswp, "%s: no dring found", __func__);
61291ae08745Sheppo 			return;
61301ae08745Sheppo 		}
61311ae08745Sheppo 
61324bac2208Snarayan 		len = dp->num_descriptors;
61334bac2208Snarayan 		/*
61344bac2208Snarayan 		 * If the descriptor we are being ACK'ed for is not the
61354bac2208Snarayan 		 * one we expected, then pkts were lost somwhere, either
61364bac2208Snarayan 		 * when we tried to send a msg, or a previous ACK msg from
61374bac2208Snarayan 		 * our peer. In either case we now reclaim the descriptors
61384bac2208Snarayan 		 * in the range from the last ACK we received up to the
61394bac2208Snarayan 		 * current ACK.
61404bac2208Snarayan 		 */
61414bac2208Snarayan 		if (idx != dp->last_ack_recv) {
61424bac2208Snarayan 			DWARN(vswp, "%s: dropped pkts detected, (%ld, %ld)",
61434bac2208Snarayan 				__func__, dp->last_ack_recv, idx);
61444bac2208Snarayan 			num = idx >= dp->last_ack_recv ?
61454bac2208Snarayan 				idx - dp->last_ack_recv + 1:
61464bac2208Snarayan 				(len - dp->last_ack_recv + 1) + idx;
61474bac2208Snarayan 		}
61481ae08745Sheppo 
61491ae08745Sheppo 		/*
61501ae08745Sheppo 		 * When we sent the in-band message to our peer we
61511ae08745Sheppo 		 * marked the copy in our private ring as READY. We now
61521ae08745Sheppo 		 * check that the descriptor we are being ACK'ed for is in
61531ae08745Sheppo 		 * fact READY, i.e. it is one we have shared with our peer.
61544bac2208Snarayan 		 *
61554bac2208Snarayan 		 * If its not we flag an error, but still reset the descr
61564bac2208Snarayan 		 * back to FREE.
61571ae08745Sheppo 		 */
61584bac2208Snarayan 		for (i = dp->last_ack_recv; j < num; i = (i + 1) % len, j++) {
61594bac2208Snarayan 			priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
6160d10e4ef2Snarayan 			mutex_enter(&priv_addr->dstate_lock);
61611ae08745Sheppo 			if (priv_addr->dstate != VIO_DESC_READY) {
61624bac2208Snarayan 				DERR(vswp, "%s: (%ld) desc at index %ld not "
61634bac2208Snarayan 					"READY (0x%lx)", __func__,
61644bac2208Snarayan 					ldcp->ldc_id, idx, priv_addr->dstate);
61654bac2208Snarayan 				DERR(vswp, "%s: bound %d: ncookies %ld : "
61664bac2208Snarayan 					"datalen %ld", __func__,
61674bac2208Snarayan 					priv_addr->bound, priv_addr->ncookies,
61684bac2208Snarayan 					priv_addr->datalen);
61694bac2208Snarayan 			}
61701ae08745Sheppo 			D2(vswp, "%s: (%lld) freeing descp at %lld", __func__,
61711ae08745Sheppo 				ldcp->ldc_id, idx);
61721ae08745Sheppo 			/* release resources associated with sent msg */
61731ae08745Sheppo 			bzero(priv_addr->datap, priv_addr->datalen);
61741ae08745Sheppo 			priv_addr->datalen = 0;
61751ae08745Sheppo 			priv_addr->dstate = VIO_DESC_FREE;
6176d10e4ef2Snarayan 			mutex_exit(&priv_addr->dstate_lock);
61771ae08745Sheppo 		}
61784bac2208Snarayan 		/* update to next expected value */
61794bac2208Snarayan 		dp->last_ack_recv = (idx + 1) % dp->num_descriptors;
61804bac2208Snarayan 
61811ae08745Sheppo 		break;
61821ae08745Sheppo 
61831ae08745Sheppo 	case VIO_SUBTYPE_NACK:
61841ae08745Sheppo 		DERR(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
61851ae08745Sheppo 
61861ae08745Sheppo 		/*
61871ae08745Sheppo 		 * We should only get a NACK if our peer doesn't like
61881ae08745Sheppo 		 * something about a message we have sent it. If this
61891ae08745Sheppo 		 * happens we just release the resources associated with
61901ae08745Sheppo 		 * the message. (We are relying on higher layers to decide
61911ae08745Sheppo 		 * whether or not to resend.
61921ae08745Sheppo 		 */
61931ae08745Sheppo 
61941ae08745Sheppo 		/* limit check */
61951ae08745Sheppo 		idx = ibnd_desc->hdr.desc_handle;
61961ae08745Sheppo 
61971ae08745Sheppo 		if (idx >= VSW_RING_NUM_EL) {
61981ae08745Sheppo 			DERR(vswp, "%s: corrupted NACK received (idx %lld)",
61991ae08745Sheppo 				__func__, idx);
62001ae08745Sheppo 			return;
62011ae08745Sheppo 		}
62021ae08745Sheppo 
62031ae08745Sheppo 		if ((dp = ldcp->lane_out.dringp) == NULL) {
62041ae08745Sheppo 			DERR(vswp, "%s: no dring found", __func__);
62051ae08745Sheppo 			return;
62061ae08745Sheppo 		}
62071ae08745Sheppo 
62081ae08745Sheppo 		priv_addr = (vsw_private_desc_t *)dp->priv_addr;
62091ae08745Sheppo 
62101ae08745Sheppo 		/* move to correct location in ring */
62111ae08745Sheppo 		priv_addr += idx;
62121ae08745Sheppo 
62131ae08745Sheppo 		/* release resources associated with sent msg */
6214d10e4ef2Snarayan 		mutex_enter(&priv_addr->dstate_lock);
62151ae08745Sheppo 		bzero(priv_addr->datap, priv_addr->datalen);
62161ae08745Sheppo 		priv_addr->datalen = 0;
62171ae08745Sheppo 		priv_addr->dstate = VIO_DESC_FREE;
6218d10e4ef2Snarayan 		mutex_exit(&priv_addr->dstate_lock);
62191ae08745Sheppo 
62201ae08745Sheppo 		break;
62211ae08745Sheppo 
62221ae08745Sheppo 	default:
62231ae08745Sheppo 		DERR(vswp, "%s(%lld): Unknown vio_subtype %x\n", __func__,
62241ae08745Sheppo 			ldcp->ldc_id, ibnd_desc->hdr.tag.vio_subtype);
62251ae08745Sheppo 	}
62261ae08745Sheppo 
62271ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
62281ae08745Sheppo }
62291ae08745Sheppo 
62301ae08745Sheppo static void
62311ae08745Sheppo vsw_process_err_pkt(vsw_ldc_t *ldcp, void *epkt, vio_msg_tag_t tag)
62321ae08745Sheppo {
62331ae08745Sheppo 	_NOTE(ARGUNUSED(epkt))
62341ae08745Sheppo 
62351ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
62361ae08745Sheppo 	uint16_t	env = tag.vio_subtype_env;
62371ae08745Sheppo 
62381ae08745Sheppo 	D1(vswp, "%s (%lld): enter\n", __func__, ldcp->ldc_id);
62391ae08745Sheppo 
62401ae08745Sheppo 	/*
62411ae08745Sheppo 	 * Error vio_subtypes have yet to be defined. So for
62421ae08745Sheppo 	 * the moment we can't do anything.
62431ae08745Sheppo 	 */
62441ae08745Sheppo 	D2(vswp, "%s: (%x) vio_subtype env", __func__, env);
62451ae08745Sheppo 
62461ae08745Sheppo 	D1(vswp, "%s (%lld): exit\n", __func__, ldcp->ldc_id);
62471ae08745Sheppo }
62481ae08745Sheppo 
62491ae08745Sheppo /*
62501ae08745Sheppo  * Switch the given ethernet frame when operating in layer 2 mode.
62511ae08745Sheppo  *
62521ae08745Sheppo  * vswp: pointer to the vsw instance
62531ae08745Sheppo  * mp: pointer to chain of ethernet frame(s) to be switched
62541ae08745Sheppo  * caller: identifies the source of this frame as:
62551ae08745Sheppo  * 		1. VSW_VNETPORT - a vsw port (connected to a vnet).
62561ae08745Sheppo  *		2. VSW_PHYSDEV - the physical ethernet device
62571ae08745Sheppo  *		3. VSW_LOCALDEV - vsw configured as a virtual interface
62581ae08745Sheppo  * arg: argument provided by the caller.
62591ae08745Sheppo  *		1. for VNETPORT - pointer to the corresponding vsw_port_t.
62601ae08745Sheppo  *		2. for PHYSDEV - NULL
62611ae08745Sheppo  *		3. for LOCALDEV - pointer to to this vsw_t(self)
62621ae08745Sheppo  */
62631ae08745Sheppo void
62641ae08745Sheppo vsw_switch_l2_frame(vsw_t *vswp, mblk_t *mp, int caller,
62651ae08745Sheppo 			vsw_port_t *arg, mac_resource_handle_t mrh)
62661ae08745Sheppo {
62671ae08745Sheppo 	struct ether_header	*ehp;
62681ae08745Sheppo 	vsw_port_t		*port = NULL;
62691ae08745Sheppo 	mblk_t			*bp, *ret_m;
62701ae08745Sheppo 	mblk_t			*nmp = NULL;
62711ae08745Sheppo 	vsw_port_list_t		*plist = &vswp->plist;
62721ae08745Sheppo 
62731ae08745Sheppo 	D1(vswp, "%s: enter (caller %d)", __func__, caller);
62741ae08745Sheppo 
62751ae08745Sheppo 	/*
62761ae08745Sheppo 	 * PERF: rather than breaking up the chain here, scan it
62771ae08745Sheppo 	 * to find all mblks heading to same destination and then
62781ae08745Sheppo 	 * pass that sub-chain to the lower transmit functions.
62791ae08745Sheppo 	 */
62801ae08745Sheppo 
62811ae08745Sheppo 	/* process the chain of packets */
62821ae08745Sheppo 	bp = mp;
62831ae08745Sheppo 	while (bp) {
62841ae08745Sheppo 		mp = bp;
62851ae08745Sheppo 		bp = bp->b_next;
62861ae08745Sheppo 		mp->b_next = mp->b_prev = NULL;
62871ae08745Sheppo 		ehp = (struct ether_header *)mp->b_rptr;
62881ae08745Sheppo 
62891ae08745Sheppo 		D2(vswp, "%s: mblk data buffer %lld : actual data size %lld",
62901ae08745Sheppo 			__func__, MBLKSIZE(mp), MBLKL(mp));
62911ae08745Sheppo 
62921ae08745Sheppo 		READ_ENTER(&vswp->if_lockrw);
62931ae08745Sheppo 		if (ether_cmp(&ehp->ether_dhost, &vswp->if_addr) == 0) {
62941ae08745Sheppo 			/*
62951ae08745Sheppo 			 * If destination is VSW_LOCALDEV (vsw as an eth
62961ae08745Sheppo 			 * interface) and if the device is up & running,
62971ae08745Sheppo 			 * send the packet up the stack on this host.
62981ae08745Sheppo 			 * If the virtual interface is down, drop the packet.
62991ae08745Sheppo 			 */
63001ae08745Sheppo 			if (caller != VSW_LOCALDEV) {
63011ae08745Sheppo 				if (vswp->if_state & VSW_IF_UP) {
63021ae08745Sheppo 					RW_EXIT(&vswp->if_lockrw);
6303ba2e4443Sseb 					mac_rx(vswp->if_mh, mrh, mp);
63041ae08745Sheppo 				} else {
63051ae08745Sheppo 					RW_EXIT(&vswp->if_lockrw);
63061ae08745Sheppo 					/* Interface down, drop pkt */
63071ae08745Sheppo 					freemsg(mp);
63081ae08745Sheppo 				}
63091ae08745Sheppo 			} else {
63101ae08745Sheppo 				RW_EXIT(&vswp->if_lockrw);
63111ae08745Sheppo 				freemsg(mp);
63121ae08745Sheppo 			}
63131ae08745Sheppo 			continue;
63141ae08745Sheppo 		}
63151ae08745Sheppo 		RW_EXIT(&vswp->if_lockrw);
63161ae08745Sheppo 
63171ae08745Sheppo 		READ_ENTER(&plist->lockrw);
63181ae08745Sheppo 		port = vsw_lookup_fdb(vswp, ehp);
63191ae08745Sheppo 		if (port) {
63201ae08745Sheppo 			/*
63211ae08745Sheppo 			 * Mark the port as in-use.
63221ae08745Sheppo 			 */
63231ae08745Sheppo 			mutex_enter(&port->ref_lock);
63241ae08745Sheppo 			port->ref_cnt++;
63251ae08745Sheppo 			mutex_exit(&port->ref_lock);
63261ae08745Sheppo 			RW_EXIT(&plist->lockrw);
63271ae08745Sheppo 
63281ae08745Sheppo 			/*
63291ae08745Sheppo 			 * If plumbed and in promisc mode then copy msg
63301ae08745Sheppo 			 * and send up the stack.
63311ae08745Sheppo 			 */
63321ae08745Sheppo 			READ_ENTER(&vswp->if_lockrw);
63331ae08745Sheppo 			if (VSW_U_P(vswp->if_state)) {
63341ae08745Sheppo 				RW_EXIT(&vswp->if_lockrw);
63351ae08745Sheppo 				nmp = copymsg(mp);
63361ae08745Sheppo 				if (nmp)
6337ba2e4443Sseb 					mac_rx(vswp->if_mh, mrh, nmp);
63381ae08745Sheppo 			} else {
63391ae08745Sheppo 				RW_EXIT(&vswp->if_lockrw);
63401ae08745Sheppo 			}
63411ae08745Sheppo 
63421ae08745Sheppo 			/*
63431ae08745Sheppo 			 * If the destination is in FDB, the packet
63441ae08745Sheppo 			 * should be forwarded to the correponding
63451ae08745Sheppo 			 * vsw_port (connected to a vnet device -
63461ae08745Sheppo 			 * VSW_VNETPORT)
63471ae08745Sheppo 			 */
63481ae08745Sheppo 			(void) vsw_portsend(port, mp);
63491ae08745Sheppo 
63501ae08745Sheppo 			/*
63511ae08745Sheppo 			 * Decrement use count in port and check if
63521ae08745Sheppo 			 * should wake delete thread.
63531ae08745Sheppo 			 */
63541ae08745Sheppo 			mutex_enter(&port->ref_lock);
63551ae08745Sheppo 			port->ref_cnt--;
63561ae08745Sheppo 			if (port->ref_cnt == 0)
63571ae08745Sheppo 				cv_signal(&port->ref_cv);
63581ae08745Sheppo 			mutex_exit(&port->ref_lock);
63591ae08745Sheppo 		} else {
63601ae08745Sheppo 			RW_EXIT(&plist->lockrw);
63611ae08745Sheppo 			/*
63621ae08745Sheppo 			 * Destination not in FDB.
63631ae08745Sheppo 			 *
63641ae08745Sheppo 			 * If the destination is broadcast or
63651ae08745Sheppo 			 * multicast forward the packet to all
63661ae08745Sheppo 			 * (VNETPORTs, PHYSDEV, LOCALDEV),
63671ae08745Sheppo 			 * except the caller.
63681ae08745Sheppo 			 */
63691ae08745Sheppo 			if (IS_BROADCAST(ehp)) {
63701ae08745Sheppo 				D3(vswp, "%s: BROADCAST pkt", __func__);
63711ae08745Sheppo 				(void) vsw_forward_all(vswp, mp,
63721ae08745Sheppo 								caller, arg);
63731ae08745Sheppo 			} else if (IS_MULTICAST(ehp)) {
63741ae08745Sheppo 				D3(vswp, "%s: MULTICAST pkt", __func__);
63751ae08745Sheppo 				(void) vsw_forward_grp(vswp, mp,
63761ae08745Sheppo 							caller, arg);
63771ae08745Sheppo 			} else {
63781ae08745Sheppo 				/*
63791ae08745Sheppo 				 * If the destination is unicast, and came
63801ae08745Sheppo 				 * from either a logical network device or
63811ae08745Sheppo 				 * the switch itself when it is plumbed, then
63821ae08745Sheppo 				 * send it out on the physical device and also
63831ae08745Sheppo 				 * up the stack if the logical interface is
63841ae08745Sheppo 				 * in promiscious mode.
63851ae08745Sheppo 				 *
63861ae08745Sheppo 				 * NOTE:  The assumption here is that if we
63871ae08745Sheppo 				 * cannot find the destination in our fdb, its
63881ae08745Sheppo 				 * a unicast address, and came from either a
63891ae08745Sheppo 				 * vnet or down the stack (when plumbed) it
63901ae08745Sheppo 				 * must be destinded for an ethernet device
63911ae08745Sheppo 				 * outside our ldoms.
63921ae08745Sheppo 				 */
63931ae08745Sheppo 				if (caller == VSW_VNETPORT) {
63941ae08745Sheppo 					READ_ENTER(&vswp->if_lockrw);
63951ae08745Sheppo 					if (VSW_U_P(vswp->if_state)) {
63961ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
63971ae08745Sheppo 						nmp = copymsg(mp);
63981ae08745Sheppo 						if (nmp)
6399ba2e4443Sseb 							mac_rx(vswp->if_mh,
64001ae08745Sheppo 								mrh, nmp);
64011ae08745Sheppo 					} else {
64021ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
64031ae08745Sheppo 					}
64041ae08745Sheppo 					if ((ret_m = vsw_tx_msg(vswp, mp))
64051ae08745Sheppo 								!= NULL) {
64061ae08745Sheppo 						DERR(vswp, "%s: drop mblks to "
64071ae08745Sheppo 							"phys dev", __func__);
64081ae08745Sheppo 						freemsg(ret_m);
64091ae08745Sheppo 					}
64101ae08745Sheppo 
64111ae08745Sheppo 				} else if (caller == VSW_PHYSDEV) {
64121ae08745Sheppo 					/*
64131ae08745Sheppo 					 * Pkt seen because card in promisc
64141ae08745Sheppo 					 * mode. Send up stack if plumbed in
64151ae08745Sheppo 					 * promisc mode, else drop it.
64161ae08745Sheppo 					 */
64171ae08745Sheppo 					READ_ENTER(&vswp->if_lockrw);
64181ae08745Sheppo 					if (VSW_U_P(vswp->if_state)) {
64191ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
6420ba2e4443Sseb 						mac_rx(vswp->if_mh, mrh, mp);
64211ae08745Sheppo 					} else {
64221ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
64231ae08745Sheppo 						freemsg(mp);
64241ae08745Sheppo 					}
64251ae08745Sheppo 
64261ae08745Sheppo 				} else if (caller == VSW_LOCALDEV) {
64271ae08745Sheppo 					/*
64281ae08745Sheppo 					 * Pkt came down the stack, send out
64291ae08745Sheppo 					 * over physical device.
64301ae08745Sheppo 					 */
64311ae08745Sheppo 					if ((ret_m = vsw_tx_msg(vswp, mp))
64321ae08745Sheppo 								!= NULL) {
64331ae08745Sheppo 						DERR(vswp, "%s: drop mblks to "
64341ae08745Sheppo 							"phys dev", __func__);
64351ae08745Sheppo 						freemsg(ret_m);
64361ae08745Sheppo 					}
64371ae08745Sheppo 				}
64381ae08745Sheppo 			}
64391ae08745Sheppo 		}
64401ae08745Sheppo 	}
64411ae08745Sheppo 	D1(vswp, "%s: exit\n", __func__);
64421ae08745Sheppo }
64431ae08745Sheppo 
64441ae08745Sheppo /*
64451ae08745Sheppo  * Switch ethernet frame when in layer 3 mode (i.e. using IP
64461ae08745Sheppo  * layer to do the routing).
64471ae08745Sheppo  *
64481ae08745Sheppo  * There is a large amount of overlap between this function and
64491ae08745Sheppo  * vsw_switch_l2_frame. At some stage we need to revisit and refactor
64501ae08745Sheppo  * both these functions.
64511ae08745Sheppo  */
64521ae08745Sheppo void
64531ae08745Sheppo vsw_switch_l3_frame(vsw_t *vswp, mblk_t *mp, int caller,
64541ae08745Sheppo 			vsw_port_t *arg, mac_resource_handle_t mrh)
64551ae08745Sheppo {
64561ae08745Sheppo 	struct ether_header	*ehp;
64571ae08745Sheppo 	vsw_port_t		*port = NULL;
64581ae08745Sheppo 	mblk_t			*bp = NULL;
64591ae08745Sheppo 	vsw_port_list_t		*plist = &vswp->plist;
64601ae08745Sheppo 
64611ae08745Sheppo 	D1(vswp, "%s: enter (caller %d)", __func__, caller);
64621ae08745Sheppo 
64631ae08745Sheppo 	/*
64641ae08745Sheppo 	 * In layer 3 mode should only ever be switching packets
64651ae08745Sheppo 	 * between IP layer and vnet devices. So make sure thats
64661ae08745Sheppo 	 * who is invoking us.
64671ae08745Sheppo 	 */
64681ae08745Sheppo 	if ((caller != VSW_LOCALDEV) && (caller != VSW_VNETPORT)) {
64691ae08745Sheppo 		DERR(vswp, "%s: unexpected caller (%d)", __func__, caller);
64701ae08745Sheppo 		freemsgchain(mp);
64711ae08745Sheppo 		return;
64721ae08745Sheppo 	}
64731ae08745Sheppo 
64741ae08745Sheppo 	/* process the chain of packets */
64751ae08745Sheppo 	bp = mp;
64761ae08745Sheppo 	while (bp) {
64771ae08745Sheppo 		mp = bp;
64781ae08745Sheppo 		bp = bp->b_next;
64791ae08745Sheppo 		mp->b_next = mp->b_prev = NULL;
64801ae08745Sheppo 		ehp = (struct ether_header *)mp->b_rptr;
64811ae08745Sheppo 
64821ae08745Sheppo 		D2(vswp, "%s: mblk data buffer %lld : actual data size %lld",
64831ae08745Sheppo 			__func__, MBLKSIZE(mp), MBLKL(mp));
64841ae08745Sheppo 
64851ae08745Sheppo 		READ_ENTER(&plist->lockrw);
64861ae08745Sheppo 		port = vsw_lookup_fdb(vswp, ehp);
64871ae08745Sheppo 		if (port) {
64881ae08745Sheppo 			/*
64891ae08745Sheppo 			 * Mark port as in-use.
64901ae08745Sheppo 			 */
64911ae08745Sheppo 			mutex_enter(&port->ref_lock);
64921ae08745Sheppo 			port->ref_cnt++;
64931ae08745Sheppo 			mutex_exit(&port->ref_lock);
64941ae08745Sheppo 			RW_EXIT(&plist->lockrw);
64951ae08745Sheppo 
64961ae08745Sheppo 			D2(vswp, "%s: sending to target port", __func__);
64971ae08745Sheppo 			(void) vsw_portsend(port, mp);
64981ae08745Sheppo 
64991ae08745Sheppo 			/*
65001ae08745Sheppo 			 * Finished with port so decrement ref count and
65011ae08745Sheppo 			 * check if should wake delete thread.
65021ae08745Sheppo 			 */
65031ae08745Sheppo 			mutex_enter(&port->ref_lock);
65041ae08745Sheppo 			port->ref_cnt--;
65051ae08745Sheppo 			if (port->ref_cnt == 0)
65061ae08745Sheppo 				cv_signal(&port->ref_cv);
65071ae08745Sheppo 			mutex_exit(&port->ref_lock);
65081ae08745Sheppo 		} else {
65091ae08745Sheppo 			RW_EXIT(&plist->lockrw);
65101ae08745Sheppo 			/*
65111ae08745Sheppo 			 * Destination not in FDB
65121ae08745Sheppo 			 *
65131ae08745Sheppo 			 * If the destination is broadcast or
65141ae08745Sheppo 			 * multicast forward the packet to all
65151ae08745Sheppo 			 * (VNETPORTs, PHYSDEV, LOCALDEV),
65161ae08745Sheppo 			 * except the caller.
65171ae08745Sheppo 			 */
65181ae08745Sheppo 			if (IS_BROADCAST(ehp)) {
65191ae08745Sheppo 				D2(vswp, "%s: BROADCAST pkt", __func__);
65201ae08745Sheppo 				(void) vsw_forward_all(vswp, mp,
65211ae08745Sheppo 								caller, arg);
65221ae08745Sheppo 			} else if (IS_MULTICAST(ehp)) {
65231ae08745Sheppo 				D2(vswp, "%s: MULTICAST pkt", __func__);
65241ae08745Sheppo 				(void) vsw_forward_grp(vswp, mp,
65251ae08745Sheppo 							caller, arg);
65261ae08745Sheppo 			} else {
65271ae08745Sheppo 				/*
65281ae08745Sheppo 				 * Unicast pkt from vnet that we don't have
65291ae08745Sheppo 				 * an FDB entry for, so must be destinded for
65301ae08745Sheppo 				 * the outside world. Attempt to send up to the
65311ae08745Sheppo 				 * IP layer to allow it to deal with it.
65321ae08745Sheppo 				 */
65331ae08745Sheppo 				if (caller == VSW_VNETPORT) {
65341ae08745Sheppo 					READ_ENTER(&vswp->if_lockrw);
65351ae08745Sheppo 					if (vswp->if_state & VSW_IF_UP) {
65361ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
65371ae08745Sheppo 						D2(vswp, "%s: sending up",
65381ae08745Sheppo 							__func__);
6539ba2e4443Sseb 						mac_rx(vswp->if_mh, mrh, mp);
65401ae08745Sheppo 					} else {
65411ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
65421ae08745Sheppo 						/* Interface down, drop pkt */
65431ae08745Sheppo 						D2(vswp, "%s I/F down",
65441ae08745Sheppo 								__func__);
65451ae08745Sheppo 						freemsg(mp);
65461ae08745Sheppo 					}
65471ae08745Sheppo 				}
65481ae08745Sheppo 			}
65491ae08745Sheppo 		}
65501ae08745Sheppo 	}
65511ae08745Sheppo 
65521ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
65531ae08745Sheppo }
65541ae08745Sheppo 
65551ae08745Sheppo /*
65561ae08745Sheppo  * Forward the ethernet frame to all ports (VNETPORTs, PHYSDEV, LOCALDEV),
65571ae08745Sheppo  * except the caller (port on which frame arrived).
65581ae08745Sheppo  */
65591ae08745Sheppo static int
65601ae08745Sheppo vsw_forward_all(vsw_t *vswp, mblk_t *mp, int caller, vsw_port_t *arg)
65611ae08745Sheppo {
65621ae08745Sheppo 	vsw_port_list_t	*plist = &vswp->plist;
65631ae08745Sheppo 	vsw_port_t	*portp;
65641ae08745Sheppo 	mblk_t		*nmp = NULL;
65651ae08745Sheppo 	mblk_t		*ret_m = NULL;
65661ae08745Sheppo 	int		skip_port = 0;
65671ae08745Sheppo 
65681ae08745Sheppo 	D1(vswp, "vsw_forward_all: enter\n");
65691ae08745Sheppo 
65701ae08745Sheppo 	/*
65711ae08745Sheppo 	 * Broadcast message from inside ldoms so send to outside
65721ae08745Sheppo 	 * world if in either of layer 2 modes.
65731ae08745Sheppo 	 */
65741ae08745Sheppo 	if (((vswp->smode[vswp->smode_idx] == VSW_LAYER2) ||
65751ae08745Sheppo 		(vswp->smode[vswp->smode_idx] == VSW_LAYER2_PROMISC)) &&
65761ae08745Sheppo 		((caller == VSW_LOCALDEV) || (caller == VSW_VNETPORT))) {
65771ae08745Sheppo 
65781ae08745Sheppo 		nmp = dupmsg(mp);
65791ae08745Sheppo 		if (nmp) {
65801ae08745Sheppo 			if ((ret_m = vsw_tx_msg(vswp, nmp)) != NULL) {
65811ae08745Sheppo 				DERR(vswp, "%s: dropping pkt(s) "
65821ae08745Sheppo 				"consisting of %ld bytes of data for"
65831ae08745Sheppo 				" physical device", __func__, MBLKL(ret_m));
65841ae08745Sheppo 			freemsg(ret_m);
65851ae08745Sheppo 			}
65861ae08745Sheppo 		}
65871ae08745Sheppo 	}
65881ae08745Sheppo 
65891ae08745Sheppo 	if (caller == VSW_VNETPORT)
65901ae08745Sheppo 		skip_port = 1;
65911ae08745Sheppo 
65921ae08745Sheppo 	/*
65931ae08745Sheppo 	 * Broadcast message from other vnet (layer 2 or 3) or outside
65941ae08745Sheppo 	 * world (layer 2 only), send up stack if plumbed.
65951ae08745Sheppo 	 */
65961ae08745Sheppo 	if ((caller == VSW_PHYSDEV) || (caller == VSW_VNETPORT)) {
65971ae08745Sheppo 		READ_ENTER(&vswp->if_lockrw);
65981ae08745Sheppo 		if (vswp->if_state & VSW_IF_UP) {
65991ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
66001ae08745Sheppo 			nmp = copymsg(mp);
66011ae08745Sheppo 			if (nmp)
6602ba2e4443Sseb 				mac_rx(vswp->if_mh, NULL, nmp);
66031ae08745Sheppo 		} else {
66041ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
66051ae08745Sheppo 		}
66061ae08745Sheppo 	}
66071ae08745Sheppo 
66081ae08745Sheppo 	/* send it to all VNETPORTs */
66091ae08745Sheppo 	READ_ENTER(&plist->lockrw);
66101ae08745Sheppo 	for (portp = plist->head; portp != NULL; portp = portp->p_next) {
66111ae08745Sheppo 		D2(vswp, "vsw_forward_all: port %d", portp->p_instance);
66121ae08745Sheppo 		/*
66131ae08745Sheppo 		 * Caution ! - don't reorder these two checks as arg
66141ae08745Sheppo 		 * will be NULL if the caller is PHYSDEV. skip_port is
66151ae08745Sheppo 		 * only set if caller is VNETPORT.
66161ae08745Sheppo 		 */
66171ae08745Sheppo 		if ((skip_port) && (portp == arg))
66181ae08745Sheppo 			continue;
66191ae08745Sheppo 		else {
66201ae08745Sheppo 			nmp = dupmsg(mp);
66211ae08745Sheppo 			if (nmp) {
66221ae08745Sheppo 				(void) vsw_portsend(portp, nmp);
66231ae08745Sheppo 			} else {
66241ae08745Sheppo 				DERR(vswp, "vsw_forward_all: nmp NULL");
66251ae08745Sheppo 			}
66261ae08745Sheppo 		}
66271ae08745Sheppo 	}
66281ae08745Sheppo 	RW_EXIT(&plist->lockrw);
66291ae08745Sheppo 
66301ae08745Sheppo 	freemsg(mp);
66311ae08745Sheppo 
66321ae08745Sheppo 	D1(vswp, "vsw_forward_all: exit\n");
66331ae08745Sheppo 	return (0);
66341ae08745Sheppo }
66351ae08745Sheppo 
66361ae08745Sheppo /*
66371ae08745Sheppo  * Forward pkts to any devices or interfaces which have registered
66381ae08745Sheppo  * an interest in them (i.e. multicast groups).
66391ae08745Sheppo  */
66401ae08745Sheppo static int
66411ae08745Sheppo vsw_forward_grp(vsw_t *vswp, mblk_t *mp, int caller, vsw_port_t *arg)
66421ae08745Sheppo {
66431ae08745Sheppo 	struct ether_header	*ehp = (struct ether_header *)mp->b_rptr;
66441ae08745Sheppo 	mfdb_ent_t		*entp = NULL;
66451ae08745Sheppo 	mfdb_ent_t		*tpp = NULL;
66461ae08745Sheppo 	vsw_port_t 		*port;
66471ae08745Sheppo 	uint64_t		key = 0;
66481ae08745Sheppo 	mblk_t			*nmp = NULL;
66491ae08745Sheppo 	mblk_t			*ret_m = NULL;
66501ae08745Sheppo 	boolean_t		check_if = B_TRUE;
66511ae08745Sheppo 
66521ae08745Sheppo 	/*
66531ae08745Sheppo 	 * Convert address to hash table key
66541ae08745Sheppo 	 */
66551ae08745Sheppo 	KEY_HASH(key, ehp->ether_dhost);
66561ae08745Sheppo 
66571ae08745Sheppo 	D1(vswp, "%s: key 0x%llx", __func__, key);
66581ae08745Sheppo 
66591ae08745Sheppo 	/*
66601ae08745Sheppo 	 * If pkt came from either a vnet or down the stack (if we are
66611ae08745Sheppo 	 * plumbed) and we are in layer 2 mode, then we send the pkt out
66621ae08745Sheppo 	 * over the physical adapter, and then check to see if any other
66631ae08745Sheppo 	 * vnets are interested in it.
66641ae08745Sheppo 	 */
66651ae08745Sheppo 	if (((vswp->smode[vswp->smode_idx] == VSW_LAYER2) ||
66661ae08745Sheppo 		(vswp->smode[vswp->smode_idx] == VSW_LAYER2_PROMISC)) &&
66671ae08745Sheppo 		((caller == VSW_VNETPORT) || (caller == VSW_LOCALDEV))) {
66681ae08745Sheppo 		nmp = dupmsg(mp);
66691ae08745Sheppo 		if (nmp) {
66701ae08745Sheppo 			if ((ret_m = vsw_tx_msg(vswp, nmp)) != NULL) {
66711ae08745Sheppo 				DERR(vswp, "%s: dropping pkt(s) "
66721ae08745Sheppo 					"consisting of %ld bytes of "
66731ae08745Sheppo 					"data for physical device",
66741ae08745Sheppo 					__func__, MBLKL(ret_m));
66751ae08745Sheppo 				freemsg(ret_m);
66761ae08745Sheppo 			}
66771ae08745Sheppo 		}
66781ae08745Sheppo 	}
66791ae08745Sheppo 
66801ae08745Sheppo 	READ_ENTER(&vswp->mfdbrw);
66811ae08745Sheppo 	if (mod_hash_find(vswp->mfdb, (mod_hash_key_t)key,
66821ae08745Sheppo 				(mod_hash_val_t *)&entp) != 0) {
66831ae08745Sheppo 		D3(vswp, "%s: no table entry found for addr 0x%llx",
66841ae08745Sheppo 								__func__, key);
66851ae08745Sheppo 	} else {
66861ae08745Sheppo 		/*
66871ae08745Sheppo 		 * Send to list of devices associated with this address...
66881ae08745Sheppo 		 */
66891ae08745Sheppo 		for (tpp = entp; tpp != NULL; tpp = tpp->nextp) {
66901ae08745Sheppo 
66911ae08745Sheppo 			/* dont send to ourselves */
66921ae08745Sheppo 			if ((caller == VSW_VNETPORT) &&
66931ae08745Sheppo 				(tpp->d_addr == (void *)arg)) {
66941ae08745Sheppo 				port = (vsw_port_t *)tpp->d_addr;
66951ae08745Sheppo 				D3(vswp, "%s: not sending to ourselves"
66961ae08745Sheppo 					" : port %d", __func__,
66971ae08745Sheppo 					port->p_instance);
66981ae08745Sheppo 				continue;
66991ae08745Sheppo 
67001ae08745Sheppo 			} else if ((caller == VSW_LOCALDEV) &&
67011ae08745Sheppo 				(tpp->d_type == VSW_LOCALDEV)) {
67021ae08745Sheppo 				D3(vswp, "%s: not sending back up stack",
67031ae08745Sheppo 					__func__);
67041ae08745Sheppo 				continue;
67051ae08745Sheppo 			}
67061ae08745Sheppo 
67071ae08745Sheppo 			if (tpp->d_type == VSW_VNETPORT) {
67081ae08745Sheppo 				port = (vsw_port_t *)tpp->d_addr;
67091ae08745Sheppo 				D3(vswp, "%s: sending to port %ld for "
67101ae08745Sheppo 					" addr 0x%llx", __func__,
67111ae08745Sheppo 					port->p_instance, key);
67121ae08745Sheppo 
67131ae08745Sheppo 				nmp = dupmsg(mp);
67141ae08745Sheppo 				if (nmp)
67151ae08745Sheppo 					(void) vsw_portsend(port, nmp);
67161ae08745Sheppo 			} else {
67171ae08745Sheppo 				if (vswp->if_state & VSW_IF_UP) {
67181ae08745Sheppo 					nmp = copymsg(mp);
67191ae08745Sheppo 					if (nmp)
6720ba2e4443Sseb 						mac_rx(vswp->if_mh, NULL, nmp);
67211ae08745Sheppo 					check_if = B_FALSE;
67221ae08745Sheppo 					D3(vswp, "%s: sending up stack"
67231ae08745Sheppo 						" for addr 0x%llx", __func__,
67241ae08745Sheppo 						key);
67251ae08745Sheppo 				}
67261ae08745Sheppo 			}
67271ae08745Sheppo 		}
67281ae08745Sheppo 	}
67291ae08745Sheppo 
67301ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
67311ae08745Sheppo 
67321ae08745Sheppo 	/*
67331ae08745Sheppo 	 * If the pkt came from either a vnet or from physical device,
67341ae08745Sheppo 	 * and if we havent already sent the pkt up the stack then we
67351ae08745Sheppo 	 * check now if we can/should (i.e. the interface is plumbed
67361ae08745Sheppo 	 * and in promisc mode).
67371ae08745Sheppo 	 */
67381ae08745Sheppo 	if ((check_if) &&
67391ae08745Sheppo 		((caller == VSW_VNETPORT) || (caller == VSW_PHYSDEV))) {
67401ae08745Sheppo 		READ_ENTER(&vswp->if_lockrw);
67411ae08745Sheppo 		if (VSW_U_P(vswp->if_state)) {
67421ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
67431ae08745Sheppo 			D3(vswp, "%s: (caller %d) finally sending up stack"
67441ae08745Sheppo 				" for addr 0x%llx", __func__, caller, key);
67451ae08745Sheppo 			nmp = copymsg(mp);
67461ae08745Sheppo 			if (nmp)
6747ba2e4443Sseb 				mac_rx(vswp->if_mh, NULL, nmp);
67481ae08745Sheppo 		} else {
67491ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
67501ae08745Sheppo 		}
67511ae08745Sheppo 	}
67521ae08745Sheppo 
67531ae08745Sheppo 	freemsg(mp);
67541ae08745Sheppo 
67551ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
67561ae08745Sheppo 
67571ae08745Sheppo 	return (0);
67581ae08745Sheppo }
67591ae08745Sheppo 
67601ae08745Sheppo /* transmit the packet over the given port */
67611ae08745Sheppo static int
67621ae08745Sheppo vsw_portsend(vsw_port_t *port, mblk_t *mp)
67631ae08745Sheppo {
67641ae08745Sheppo 	vsw_ldc_list_t 	*ldcl = &port->p_ldclist;
67651ae08745Sheppo 	vsw_ldc_t 	*ldcp;
67661ae08745Sheppo 	int		status = 0;
67671ae08745Sheppo 
67681ae08745Sheppo 
67691ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
67701ae08745Sheppo 	/*
67711ae08745Sheppo 	 * Note for now, we have a single channel.
67721ae08745Sheppo 	 */
67731ae08745Sheppo 	ldcp = ldcl->head;
67741ae08745Sheppo 	if (ldcp == NULL) {
67751ae08745Sheppo 		DERR(port->p_vswp, "vsw_portsend: no ldc: dropping packet\n");
67761ae08745Sheppo 		freemsg(mp);
67771ae08745Sheppo 		RW_EXIT(&ldcl->lockrw);
67781ae08745Sheppo 		return (1);
67791ae08745Sheppo 	}
67801ae08745Sheppo 
67811ae08745Sheppo 	/*
67821ae08745Sheppo 	 * Send the message out using the appropriate
67831ae08745Sheppo 	 * transmit function which will free mblock when it
67841ae08745Sheppo 	 * is finished with it.
67851ae08745Sheppo 	 */
67861ae08745Sheppo 	mutex_enter(&port->tx_lock);
67871ae08745Sheppo 	if (port->transmit != NULL)
67881ae08745Sheppo 		status = (*port->transmit)(ldcp, mp);
67891ae08745Sheppo 	else {
67901ae08745Sheppo 		freemsg(mp);
67911ae08745Sheppo 	}
67921ae08745Sheppo 	mutex_exit(&port->tx_lock);
67931ae08745Sheppo 
67941ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
67951ae08745Sheppo 
67961ae08745Sheppo 	return (status);
67971ae08745Sheppo }
67981ae08745Sheppo 
67991ae08745Sheppo /*
68001ae08745Sheppo  * Send packet out via descriptor ring to a logical device.
68011ae08745Sheppo  */
68021ae08745Sheppo static int
68031ae08745Sheppo vsw_dringsend(vsw_ldc_t *ldcp, mblk_t *mp)
68041ae08745Sheppo {
68051ae08745Sheppo 	vio_dring_msg_t		dring_pkt;
68061ae08745Sheppo 	dring_info_t		*dp = NULL;
68071ae08745Sheppo 	vsw_private_desc_t	*priv_desc = NULL;
6808d10e4ef2Snarayan 	vnet_public_desc_t	*pub = NULL;
68091ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
68101ae08745Sheppo 	mblk_t			*bp;
68111ae08745Sheppo 	size_t			n, size;
68121ae08745Sheppo 	caddr_t			bufp;
68131ae08745Sheppo 	int			idx;
68141ae08745Sheppo 	int			status = LDC_TX_SUCCESS;
68151ae08745Sheppo 
68161ae08745Sheppo 	D1(vswp, "%s(%lld): enter\n", __func__, ldcp->ldc_id);
68171ae08745Sheppo 
68181ae08745Sheppo 	/* TODO: make test a macro */
68191ae08745Sheppo 	if ((!(ldcp->lane_out.lstate & VSW_LANE_ACTIVE)) ||
68201ae08745Sheppo 		(ldcp->ldc_status != LDC_UP) || (ldcp->ldc_handle == NULL)) {
68211ae08745Sheppo 		DWARN(vswp, "%s(%lld) status(%d) lstate(0x%llx), dropping "
68221ae08745Sheppo 			"packet\n", __func__, ldcp->ldc_id, ldcp->ldc_status,
68231ae08745Sheppo 			ldcp->lane_out.lstate);
68241ae08745Sheppo 		freemsg(mp);
68251ae08745Sheppo 		return (LDC_TX_FAILURE);
68261ae08745Sheppo 	}
68271ae08745Sheppo 
68281ae08745Sheppo 	/*
68291ae08745Sheppo 	 * Note - using first ring only, this may change
68301ae08745Sheppo 	 * in the future.
68311ae08745Sheppo 	 */
6832*445b4c2eSsb155480 	READ_ENTER(&ldcp->lane_out.dlistrw);
68331ae08745Sheppo 	if ((dp = ldcp->lane_out.dringp) == NULL) {
6834*445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_out.dlistrw);
68351ae08745Sheppo 		DERR(vswp, "%s(%lld): no dring for outbound lane on"
68361ae08745Sheppo 			" channel %d", __func__, ldcp->ldc_id, ldcp->ldc_id);
68371ae08745Sheppo 		freemsg(mp);
68381ae08745Sheppo 		return (LDC_TX_FAILURE);
68391ae08745Sheppo 	}
68401ae08745Sheppo 
68411ae08745Sheppo 	size = msgsize(mp);
68421ae08745Sheppo 	if (size > (size_t)ETHERMAX) {
6843*445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_out.dlistrw);
68441ae08745Sheppo 		DERR(vswp, "%s(%lld) invalid size (%ld)\n", __func__,
68451ae08745Sheppo 		    ldcp->ldc_id, size);
6846d10e4ef2Snarayan 		freemsg(mp);
6847d10e4ef2Snarayan 		return (LDC_TX_FAILURE);
68481ae08745Sheppo 	}
68491ae08745Sheppo 
68501ae08745Sheppo 	/*
68511ae08745Sheppo 	 * Find a free descriptor
68521ae08745Sheppo 	 *
68531ae08745Sheppo 	 * Note: for the moment we are assuming that we will only
68541ae08745Sheppo 	 * have one dring going from the switch to each of its
68551ae08745Sheppo 	 * peers. This may change in the future.
68561ae08745Sheppo 	 */
68571ae08745Sheppo 	if (vsw_dring_find_free_desc(dp, &priv_desc, &idx) != 0) {
6858d10e4ef2Snarayan 		D2(vswp, "%s(%lld): no descriptor available for ring "
68591ae08745Sheppo 			"at 0x%llx", __func__, ldcp->ldc_id, dp);
68601ae08745Sheppo 
68611ae08745Sheppo 		/* nothing more we can do */
68621ae08745Sheppo 		status = LDC_TX_NORESOURCES;
68631ae08745Sheppo 		goto vsw_dringsend_free_exit;
68641ae08745Sheppo 	} else {
68651ae08745Sheppo 		D2(vswp, "%s(%lld): free private descriptor found at pos "
68661ae08745Sheppo 			"%ld addr 0x%llx\n", __func__, ldcp->ldc_id, idx,
68671ae08745Sheppo 			priv_desc);
68681ae08745Sheppo 	}
68691ae08745Sheppo 
68701ae08745Sheppo 	/* copy data into the descriptor */
68711ae08745Sheppo 	bufp = priv_desc->datap;
6872d10e4ef2Snarayan 	bufp += VNET_IPALIGN;
68731ae08745Sheppo 	for (bp = mp, n = 0; bp != NULL; bp = bp->b_cont) {
68741ae08745Sheppo 		n = MBLKL(bp);
68751ae08745Sheppo 		bcopy(bp->b_rptr, bufp, n);
68761ae08745Sheppo 		bufp += n;
68771ae08745Sheppo 	}
68781ae08745Sheppo 
68791ae08745Sheppo 	priv_desc->datalen = (size < (size_t)ETHERMIN) ? ETHERMIN : size;
6880d10e4ef2Snarayan 
6881d10e4ef2Snarayan 	pub = priv_desc->descp;
6882d10e4ef2Snarayan 	pub->nbytes = priv_desc->datalen;
6883d10e4ef2Snarayan 
6884d10e4ef2Snarayan 	mutex_enter(&priv_desc->dstate_lock);
6885d10e4ef2Snarayan 	pub->hdr.dstate = VIO_DESC_READY;
6886d10e4ef2Snarayan 	mutex_exit(&priv_desc->dstate_lock);
68871ae08745Sheppo 
68881ae08745Sheppo 	/*
6889d10e4ef2Snarayan 	 * Determine whether or not we need to send a message to our
6890d10e4ef2Snarayan 	 * peer prompting them to read our newly updated descriptor(s).
68911ae08745Sheppo 	 */
6892d10e4ef2Snarayan 	mutex_enter(&dp->restart_lock);
6893d10e4ef2Snarayan 	if (dp->restart_reqd) {
6894d10e4ef2Snarayan 		dp->restart_reqd = B_FALSE;
6895d10e4ef2Snarayan 		mutex_exit(&dp->restart_lock);
68961ae08745Sheppo 
68971ae08745Sheppo 		/*
68981ae08745Sheppo 		 * Send a vio_dring_msg to peer to prompt them to read
68991ae08745Sheppo 		 * the updated descriptor ring.
69001ae08745Sheppo 		 */
69011ae08745Sheppo 		dring_pkt.tag.vio_msgtype = VIO_TYPE_DATA;
69021ae08745Sheppo 		dring_pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
69031ae08745Sheppo 		dring_pkt.tag.vio_subtype_env = VIO_DRING_DATA;
69041ae08745Sheppo 		dring_pkt.tag.vio_sid = ldcp->local_session;
69051ae08745Sheppo 
69061ae08745Sheppo 		/* Note - for now using first ring */
69071ae08745Sheppo 		dring_pkt.dring_ident = dp->ident;
69081ae08745Sheppo 
6909d10e4ef2Snarayan 		mutex_enter(&ldcp->lane_out.seq_lock);
69101ae08745Sheppo 		dring_pkt.seq_num = ldcp->lane_out.seq_num++;
6911d10e4ef2Snarayan 		mutex_exit(&ldcp->lane_out.seq_lock);
69121ae08745Sheppo 
6913d10e4ef2Snarayan 		/*
6914d10e4ef2Snarayan 		 * If last_ack_recv is -1 then we know we've not
6915d10e4ef2Snarayan 		 * received any ack's yet, so this must be the first
6916d10e4ef2Snarayan 		 * msg sent, so set the start to the begining of the ring.
6917d10e4ef2Snarayan 		 */
6918d10e4ef2Snarayan 		mutex_enter(&dp->dlock);
6919d10e4ef2Snarayan 		if (dp->last_ack_recv == -1) {
6920d10e4ef2Snarayan 			dring_pkt.start_idx = 0;
6921d10e4ef2Snarayan 		} else {
6922d10e4ef2Snarayan 			dring_pkt.start_idx = (dp->last_ack_recv + 1) %
6923d10e4ef2Snarayan 						dp->num_descriptors;
6924d10e4ef2Snarayan 		}
6925d10e4ef2Snarayan 		dring_pkt.end_idx = -1;
6926d10e4ef2Snarayan 		mutex_exit(&dp->dlock);
69271ae08745Sheppo 
69281ae08745Sheppo 		D3(vswp, "%s(%lld): dring 0x%llx : ident 0x%llx\n", __func__,
69291ae08745Sheppo 			ldcp->ldc_id, dp, dring_pkt.dring_ident);
6930d10e4ef2Snarayan 		D3(vswp, "%s(%lld): start %lld : end %lld : seq %lld\n",
6931d10e4ef2Snarayan 			__func__, ldcp->ldc_id, dring_pkt.start_idx,
6932d10e4ef2Snarayan 			dring_pkt.end_idx, dring_pkt.seq_num);
69331ae08745Sheppo 
6934d10e4ef2Snarayan 		vsw_send_msg(ldcp, (void *)&dring_pkt,
6935d10e4ef2Snarayan 						sizeof (vio_dring_msg_t));
6936d10e4ef2Snarayan 	} else {
6937d10e4ef2Snarayan 		mutex_exit(&dp->restart_lock);
6938d10e4ef2Snarayan 		D2(vswp, "%s(%lld): updating descp %d", __func__,
6939d10e4ef2Snarayan 			ldcp->ldc_id, idx);
6940d10e4ef2Snarayan 	}
69411ae08745Sheppo 
69421ae08745Sheppo vsw_dringsend_free_exit:
69431ae08745Sheppo 
6944*445b4c2eSsb155480 	RW_EXIT(&ldcp->lane_out.dlistrw);
6945*445b4c2eSsb155480 
69461ae08745Sheppo 	/* free the message block */
69471ae08745Sheppo 	freemsg(mp);
69481ae08745Sheppo 
69491ae08745Sheppo 	D1(vswp, "%s(%lld): exit\n", __func__, ldcp->ldc_id);
69501ae08745Sheppo 	return (status);
69511ae08745Sheppo }
69521ae08745Sheppo 
69531ae08745Sheppo /*
69541ae08745Sheppo  * Send an in-band descriptor message over ldc.
69551ae08745Sheppo  */
69561ae08745Sheppo static int
69571ae08745Sheppo vsw_descrsend(vsw_ldc_t *ldcp, mblk_t *mp)
69581ae08745Sheppo {
69591ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
6960*445b4c2eSsb155480 	vnet_ibnd_desc_t	ibnd_msg;
69611ae08745Sheppo 	vsw_private_desc_t	*priv_desc = NULL;
69621ae08745Sheppo 	dring_info_t		*dp = NULL;
69631ae08745Sheppo 	size_t			n, size = 0;
69641ae08745Sheppo 	caddr_t			bufp;
69651ae08745Sheppo 	mblk_t			*bp;
69661ae08745Sheppo 	int			idx, i;
69671ae08745Sheppo 	int			status = LDC_TX_SUCCESS;
69681ae08745Sheppo 	static int		warn_msg = 1;
69691ae08745Sheppo 
69701ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
69711ae08745Sheppo 
69721ae08745Sheppo 	ASSERT(mp != NULL);
69731ae08745Sheppo 
69741ae08745Sheppo 	if ((!(ldcp->lane_out.lstate & VSW_LANE_ACTIVE)) ||
69751ae08745Sheppo 		(ldcp->ldc_status != LDC_UP) || (ldcp->ldc_handle == NULL)) {
69761ae08745Sheppo 		DERR(vswp, "%s(%lld) status(%d) state (0x%llx), dropping pkt",
69771ae08745Sheppo 			__func__, ldcp->ldc_id, ldcp->ldc_status,
69781ae08745Sheppo 			ldcp->lane_out.lstate);
69791ae08745Sheppo 		freemsg(mp);
69801ae08745Sheppo 		return (LDC_TX_FAILURE);
69811ae08745Sheppo 	}
69821ae08745Sheppo 
69831ae08745Sheppo 	/*
69841ae08745Sheppo 	 * only expect single dring to exist, which we use
69851ae08745Sheppo 	 * as an internal buffer, rather than a transfer channel.
69861ae08745Sheppo 	 */
6987*445b4c2eSsb155480 	READ_ENTER(&ldcp->lane_out.dlistrw);
69881ae08745Sheppo 	if ((dp = ldcp->lane_out.dringp) == NULL) {
69891ae08745Sheppo 		DERR(vswp, "%s(%lld): no dring for outbound lane",
69901ae08745Sheppo 			__func__, ldcp->ldc_id);
69911ae08745Sheppo 		DERR(vswp, "%s(%lld) status(%d) state (0x%llx)",
69921ae08745Sheppo 			__func__, ldcp->ldc_id, ldcp->ldc_status,
69931ae08745Sheppo 			ldcp->lane_out.lstate);
6994*445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_out.dlistrw);
69951ae08745Sheppo 		freemsg(mp);
69961ae08745Sheppo 		return (LDC_TX_FAILURE);
69971ae08745Sheppo 	}
69981ae08745Sheppo 
69991ae08745Sheppo 	size = msgsize(mp);
70001ae08745Sheppo 	if (size > (size_t)ETHERMAX) {
70011ae08745Sheppo 		DERR(vswp, "%s(%lld) invalid size (%ld)\n", __func__,
70021ae08745Sheppo 		    ldcp->ldc_id, size);
7003d10e4ef2Snarayan 		freemsg(mp);
7004d10e4ef2Snarayan 		return (LDC_TX_FAILURE);
70051ae08745Sheppo 	}
70061ae08745Sheppo 
70071ae08745Sheppo 	/*
70081ae08745Sheppo 	 * Find a free descriptor in our buffer ring
70091ae08745Sheppo 	 */
70101ae08745Sheppo 	if (vsw_dring_find_free_desc(dp, &priv_desc, &idx) != 0) {
70111ae08745Sheppo 		if (warn_msg) {
70121ae08745Sheppo 			DERR(vswp, "%s(%lld): no descriptor available for ring "
70131ae08745Sheppo 			"at 0x%llx", __func__, ldcp->ldc_id, dp);
70141ae08745Sheppo 			warn_msg = 0;
70151ae08745Sheppo 		}
70161ae08745Sheppo 
70171ae08745Sheppo 		/* nothing more we can do */
70181ae08745Sheppo 		status = LDC_TX_NORESOURCES;
70191ae08745Sheppo 		goto vsw_descrsend_free_exit;
70201ae08745Sheppo 	} else {
70211ae08745Sheppo 		D2(vswp, "%s(%lld): free private descriptor found at pos "
70221ae08745Sheppo 			"%ld addr 0x%x\n", __func__, ldcp->ldc_id, idx,
70231ae08745Sheppo 			priv_desc);
70241ae08745Sheppo 		warn_msg = 1;
70251ae08745Sheppo 	}
70261ae08745Sheppo 
70271ae08745Sheppo 	/* copy data into the descriptor */
70281ae08745Sheppo 	bufp = priv_desc->datap;
70291ae08745Sheppo 	for (bp = mp, n = 0; bp != NULL; bp = bp->b_cont) {
70301ae08745Sheppo 		n = MBLKL(bp);
70311ae08745Sheppo 		bcopy(bp->b_rptr, bufp, n);
70321ae08745Sheppo 		bufp += n;
70331ae08745Sheppo 	}
70341ae08745Sheppo 
70351ae08745Sheppo 	priv_desc->datalen = (size < (size_t)ETHERMIN) ? ETHERMIN : size;
70361ae08745Sheppo 
70371ae08745Sheppo 	/* create and send the in-band descp msg */
70381ae08745Sheppo 	ibnd_msg.hdr.tag.vio_msgtype = VIO_TYPE_DATA;
70391ae08745Sheppo 	ibnd_msg.hdr.tag.vio_subtype = VIO_SUBTYPE_INFO;
70401ae08745Sheppo 	ibnd_msg.hdr.tag.vio_subtype_env = VIO_DESC_DATA;
70411ae08745Sheppo 	ibnd_msg.hdr.tag.vio_sid = ldcp->local_session;
70421ae08745Sheppo 
7043d10e4ef2Snarayan 	mutex_enter(&ldcp->lane_out.seq_lock);
70441ae08745Sheppo 	ibnd_msg.hdr.seq_num = ldcp->lane_out.seq_num++;
7045d10e4ef2Snarayan 	mutex_exit(&ldcp->lane_out.seq_lock);
70461ae08745Sheppo 
70471ae08745Sheppo 	/*
70481ae08745Sheppo 	 * Copy the mem cookies describing the data from the
70491ae08745Sheppo 	 * private region of the descriptor ring into the inband
70501ae08745Sheppo 	 * descriptor.
70511ae08745Sheppo 	 */
70521ae08745Sheppo 	for (i = 0; i < priv_desc->ncookies; i++) {
70531ae08745Sheppo 		bcopy(&priv_desc->memcookie[i], &ibnd_msg.memcookie[i],
70541ae08745Sheppo 			sizeof (ldc_mem_cookie_t));
70551ae08745Sheppo 	}
70561ae08745Sheppo 
70571ae08745Sheppo 	ibnd_msg.hdr.desc_handle = idx;
70581ae08745Sheppo 	ibnd_msg.ncookies = priv_desc->ncookies;
70591ae08745Sheppo 	ibnd_msg.nbytes = size;
70601ae08745Sheppo 
7061*445b4c2eSsb155480 	vsw_send_msg(ldcp, (void *)&ibnd_msg, sizeof (vnet_ibnd_desc_t));
70621ae08745Sheppo 
70631ae08745Sheppo vsw_descrsend_free_exit:
70641ae08745Sheppo 
7065*445b4c2eSsb155480 	RW_EXIT(&ldcp->lane_out.dlistrw);
7066*445b4c2eSsb155480 
70671ae08745Sheppo 	/* free the allocated message blocks */
70681ae08745Sheppo 	freemsg(mp);
70691ae08745Sheppo 
70701ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
70711ae08745Sheppo 	return (status);
70721ae08745Sheppo }
70731ae08745Sheppo 
70741ae08745Sheppo static void
70753af08d82Slm66018 vsw_send_ver(void *arg)
70761ae08745Sheppo {
70773af08d82Slm66018 	vsw_ldc_t	*ldcp = (vsw_ldc_t *)arg;
70781ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
70791ae08745Sheppo 	lane_t		*lp = &ldcp->lane_out;
70801ae08745Sheppo 	vio_ver_msg_t	ver_msg;
70811ae08745Sheppo 
70821ae08745Sheppo 	D1(vswp, "%s enter", __func__);
70831ae08745Sheppo 
70841ae08745Sheppo 	ver_msg.tag.vio_msgtype = VIO_TYPE_CTRL;
70851ae08745Sheppo 	ver_msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
70861ae08745Sheppo 	ver_msg.tag.vio_subtype_env = VIO_VER_INFO;
70871ae08745Sheppo 	ver_msg.tag.vio_sid = ldcp->local_session;
70881ae08745Sheppo 
70891ae08745Sheppo 	ver_msg.ver_major = vsw_versions[0].ver_major;
70901ae08745Sheppo 	ver_msg.ver_minor = vsw_versions[0].ver_minor;
70911ae08745Sheppo 	ver_msg.dev_class = VDEV_NETWORK_SWITCH;
70921ae08745Sheppo 
70931ae08745Sheppo 	lp->lstate |= VSW_VER_INFO_SENT;
70941ae08745Sheppo 	lp->ver_major = ver_msg.ver_major;
70951ae08745Sheppo 	lp->ver_minor = ver_msg.ver_minor;
70961ae08745Sheppo 
70971ae08745Sheppo 	DUMP_TAG(ver_msg.tag);
70981ae08745Sheppo 
70991ae08745Sheppo 	vsw_send_msg(ldcp, &ver_msg, sizeof (vio_ver_msg_t));
71001ae08745Sheppo 
71011ae08745Sheppo 	D1(vswp, "%s (%d): exit", __func__, ldcp->ldc_id);
71021ae08745Sheppo }
71031ae08745Sheppo 
71041ae08745Sheppo static void
71051ae08745Sheppo vsw_send_attr(vsw_ldc_t *ldcp)
71061ae08745Sheppo {
71071ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
71081ae08745Sheppo 	lane_t			*lp = &ldcp->lane_out;
71091ae08745Sheppo 	vnet_attr_msg_t		attr_msg;
71101ae08745Sheppo 
71111ae08745Sheppo 	D1(vswp, "%s (%ld) enter", __func__, ldcp->ldc_id);
71121ae08745Sheppo 
71131ae08745Sheppo 	/*
71141ae08745Sheppo 	 * Subtype is set to INFO by default
71151ae08745Sheppo 	 */
71161ae08745Sheppo 	attr_msg.tag.vio_msgtype = VIO_TYPE_CTRL;
71171ae08745Sheppo 	attr_msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
71181ae08745Sheppo 	attr_msg.tag.vio_subtype_env = VIO_ATTR_INFO;
71191ae08745Sheppo 	attr_msg.tag.vio_sid = ldcp->local_session;
71201ae08745Sheppo 
71211ae08745Sheppo 	/* payload copied from default settings for lane */
71221ae08745Sheppo 	attr_msg.mtu = lp->mtu;
71231ae08745Sheppo 	attr_msg.addr_type = lp->addr_type;
71241ae08745Sheppo 	attr_msg.xfer_mode = lp->xfer_mode;
71251ae08745Sheppo 	attr_msg.ack_freq = lp->xfer_mode;
71261ae08745Sheppo 
71271ae08745Sheppo 	READ_ENTER(&vswp->if_lockrw);
71281ae08745Sheppo 	bcopy(&(vswp->if_addr), &(attr_msg.addr), ETHERADDRL);
71291ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
71301ae08745Sheppo 
71311ae08745Sheppo 	ldcp->lane_out.lstate |= VSW_ATTR_INFO_SENT;
71321ae08745Sheppo 
71331ae08745Sheppo 	DUMP_TAG(attr_msg.tag);
71341ae08745Sheppo 
71351ae08745Sheppo 	vsw_send_msg(ldcp, &attr_msg, sizeof (vnet_attr_msg_t));
71361ae08745Sheppo 
71371ae08745Sheppo 	D1(vswp, "%s (%ld) enter", __func__, ldcp->ldc_id);
71381ae08745Sheppo }
71391ae08745Sheppo 
71401ae08745Sheppo /*
71411ae08745Sheppo  * Create dring info msg (which also results in the creation of
71421ae08745Sheppo  * a dring).
71431ae08745Sheppo  */
71441ae08745Sheppo static vio_dring_reg_msg_t *
71451ae08745Sheppo vsw_create_dring_info_pkt(vsw_ldc_t *ldcp)
71461ae08745Sheppo {
71471ae08745Sheppo 	vio_dring_reg_msg_t	*mp;
71481ae08745Sheppo 	dring_info_t		*dp;
71491ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
71501ae08745Sheppo 
71511ae08745Sheppo 	D1(vswp, "vsw_create_dring_info_pkt enter\n");
71521ae08745Sheppo 
71531ae08745Sheppo 	/*
71541ae08745Sheppo 	 * If we can't create a dring, obviously no point sending
71551ae08745Sheppo 	 * a message.
71561ae08745Sheppo 	 */
71571ae08745Sheppo 	if ((dp = vsw_create_dring(ldcp)) == NULL)
71581ae08745Sheppo 		return (NULL);
71591ae08745Sheppo 
71601ae08745Sheppo 	mp = kmem_zalloc(sizeof (vio_dring_reg_msg_t), KM_SLEEP);
71611ae08745Sheppo 
71621ae08745Sheppo 	mp->tag.vio_msgtype = VIO_TYPE_CTRL;
71631ae08745Sheppo 	mp->tag.vio_subtype = VIO_SUBTYPE_INFO;
71641ae08745Sheppo 	mp->tag.vio_subtype_env = VIO_DRING_REG;
71651ae08745Sheppo 	mp->tag.vio_sid = ldcp->local_session;
71661ae08745Sheppo 
71671ae08745Sheppo 	/* payload */
71681ae08745Sheppo 	mp->num_descriptors = dp->num_descriptors;
71691ae08745Sheppo 	mp->descriptor_size = dp->descriptor_size;
71701ae08745Sheppo 	mp->options = dp->options;
71711ae08745Sheppo 	mp->ncookies = dp->ncookies;
71721ae08745Sheppo 	bcopy(&dp->cookie[0], &mp->cookie[0], sizeof (ldc_mem_cookie_t));
71731ae08745Sheppo 
71741ae08745Sheppo 	mp->dring_ident = 0;
71751ae08745Sheppo 
71761ae08745Sheppo 	D1(vswp, "vsw_create_dring_info_pkt exit\n");
71771ae08745Sheppo 
71781ae08745Sheppo 	return (mp);
71791ae08745Sheppo }
71801ae08745Sheppo 
71811ae08745Sheppo static void
71821ae08745Sheppo vsw_send_dring_info(vsw_ldc_t *ldcp)
71831ae08745Sheppo {
71841ae08745Sheppo 	vio_dring_reg_msg_t	*dring_msg;
71851ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
71861ae08745Sheppo 
71871ae08745Sheppo 	D1(vswp, "%s: (%ld) enter", __func__, ldcp->ldc_id);
71881ae08745Sheppo 
71891ae08745Sheppo 	dring_msg = vsw_create_dring_info_pkt(ldcp);
71901ae08745Sheppo 	if (dring_msg == NULL) {
719134683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: %s: error creating msg",
719234683adeSsg70180 			vswp->instance, __func__);
71931ae08745Sheppo 		return;
71941ae08745Sheppo 	}
71951ae08745Sheppo 
71961ae08745Sheppo 	ldcp->lane_out.lstate |= VSW_DRING_INFO_SENT;
71971ae08745Sheppo 
71981ae08745Sheppo 	DUMP_TAG_PTR((vio_msg_tag_t *)dring_msg);
71991ae08745Sheppo 
72001ae08745Sheppo 	vsw_send_msg(ldcp, dring_msg,
72011ae08745Sheppo 		sizeof (vio_dring_reg_msg_t));
72021ae08745Sheppo 
72031ae08745Sheppo 	kmem_free(dring_msg, sizeof (vio_dring_reg_msg_t));
72041ae08745Sheppo 
72051ae08745Sheppo 	D1(vswp, "%s: (%ld) exit", __func__, ldcp->ldc_id);
72061ae08745Sheppo }
72071ae08745Sheppo 
72081ae08745Sheppo static void
72091ae08745Sheppo vsw_send_rdx(vsw_ldc_t *ldcp)
72101ae08745Sheppo {
72111ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
72121ae08745Sheppo 	vio_rdx_msg_t	rdx_msg;
72131ae08745Sheppo 
72141ae08745Sheppo 	D1(vswp, "%s (%ld) enter", __func__, ldcp->ldc_id);
72151ae08745Sheppo 
72161ae08745Sheppo 	rdx_msg.tag.vio_msgtype = VIO_TYPE_CTRL;
72171ae08745Sheppo 	rdx_msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
72181ae08745Sheppo 	rdx_msg.tag.vio_subtype_env = VIO_RDX;
72191ae08745Sheppo 	rdx_msg.tag.vio_sid = ldcp->local_session;
72201ae08745Sheppo 
72211ae08745Sheppo 	ldcp->lane_out.lstate |= VSW_RDX_INFO_SENT;
72221ae08745Sheppo 
72231ae08745Sheppo 	DUMP_TAG(rdx_msg.tag);
72241ae08745Sheppo 
72251ae08745Sheppo 	vsw_send_msg(ldcp, &rdx_msg, sizeof (vio_rdx_msg_t));
72261ae08745Sheppo 
72271ae08745Sheppo 	D1(vswp, "%s (%ld) exit", __func__, ldcp->ldc_id);
72281ae08745Sheppo }
72291ae08745Sheppo 
72301ae08745Sheppo /*
72311ae08745Sheppo  * Generic routine to send message out over ldc channel.
72321ae08745Sheppo  */
72331ae08745Sheppo static void
72341ae08745Sheppo vsw_send_msg(vsw_ldc_t *ldcp, void *msgp, int size)
72351ae08745Sheppo {
72361ae08745Sheppo 	int		rv;
72371ae08745Sheppo 	size_t		msglen = size;
72381ae08745Sheppo 	vio_msg_tag_t	*tag = (vio_msg_tag_t *)msgp;
72391ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
72401ae08745Sheppo 
72411ae08745Sheppo 	D1(vswp, "vsw_send_msg (%lld) enter : sending %d bytes",
72421ae08745Sheppo 			ldcp->ldc_id, size);
72431ae08745Sheppo 
72441ae08745Sheppo 	D2(vswp, "send_msg: type 0x%llx", tag->vio_msgtype);
72451ae08745Sheppo 	D2(vswp, "send_msg: stype 0x%llx", tag->vio_subtype);
72461ae08745Sheppo 	D2(vswp, "send_msg: senv 0x%llx", tag->vio_subtype_env);
72471ae08745Sheppo 
72481ae08745Sheppo 	mutex_enter(&ldcp->ldc_txlock);
72491ae08745Sheppo 	do {
72501ae08745Sheppo 		msglen = size;
72511ae08745Sheppo 		rv = ldc_write(ldcp->ldc_handle, (caddr_t)msgp, &msglen);
72521ae08745Sheppo 	} while (rv == EWOULDBLOCK && --vsw_wretries > 0);
72531ae08745Sheppo 
72541ae08745Sheppo 	if ((rv != 0) || (msglen != size)) {
72551ae08745Sheppo 		DERR(vswp, "vsw_send_msg:ldc_write failed: chan(%lld) "
72561ae08745Sheppo 			"rv(%d) size (%d) msglen(%d)\n", ldcp->ldc_id,
72571ae08745Sheppo 			rv, size, msglen);
72581ae08745Sheppo 	}
72593af08d82Slm66018 	mutex_exit(&ldcp->ldc_txlock);
72603af08d82Slm66018 
72613af08d82Slm66018 	/* channel has been reset */
72623af08d82Slm66018 	if (rv == ECONNRESET) {
72633af08d82Slm66018 		vsw_handle_reset(ldcp);
72643af08d82Slm66018 	}
72651ae08745Sheppo 
72661ae08745Sheppo 	D1(vswp, "vsw_send_msg (%lld) exit : sent %d bytes",
72671ae08745Sheppo 			ldcp->ldc_id, msglen);
72681ae08745Sheppo }
72691ae08745Sheppo 
72701ae08745Sheppo /*
72711ae08745Sheppo  * Add an entry into FDB, for the given mac address and port_id.
72721ae08745Sheppo  * Returns 0 on success, 1 on failure.
72731ae08745Sheppo  *
72741ae08745Sheppo  * Lock protecting FDB must be held by calling process.
72751ae08745Sheppo  */
72761ae08745Sheppo static int
72771ae08745Sheppo vsw_add_fdb(vsw_t *vswp, vsw_port_t *port)
72781ae08745Sheppo {
72791ae08745Sheppo 	uint64_t	addr = 0;
72801ae08745Sheppo 
72811ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
72821ae08745Sheppo 
72831ae08745Sheppo 	KEY_HASH(addr, port->p_macaddr);
72841ae08745Sheppo 
72851ae08745Sheppo 	D2(vswp, "%s: key = 0x%llx", __func__, addr);
72861ae08745Sheppo 
72871ae08745Sheppo 	/*
72881ae08745Sheppo 	 * Note: duplicate keys will be rejected by mod_hash.
72891ae08745Sheppo 	 */
72901ae08745Sheppo 	if (mod_hash_insert(vswp->fdb, (mod_hash_key_t)addr,
72911ae08745Sheppo 				(mod_hash_val_t)port) != 0) {
72921ae08745Sheppo 		DERR(vswp, "%s: unable to add entry into fdb.", __func__);
72931ae08745Sheppo 		return (1);
72941ae08745Sheppo 	}
72951ae08745Sheppo 
72961ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
72971ae08745Sheppo 	return (0);
72981ae08745Sheppo }
72991ae08745Sheppo 
73001ae08745Sheppo /*
73011ae08745Sheppo  * Remove an entry from FDB.
73021ae08745Sheppo  * Returns 0 on success, 1 on failure.
73031ae08745Sheppo  */
73041ae08745Sheppo static int
73051ae08745Sheppo vsw_del_fdb(vsw_t *vswp, vsw_port_t *port)
73061ae08745Sheppo {
73071ae08745Sheppo 	uint64_t	addr = 0;
73081ae08745Sheppo 
73091ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
73101ae08745Sheppo 
73111ae08745Sheppo 	KEY_HASH(addr, port->p_macaddr);
73121ae08745Sheppo 
73131ae08745Sheppo 	D2(vswp, "%s: key = 0x%llx", __func__, addr);
73141ae08745Sheppo 
73151ae08745Sheppo 	(void) mod_hash_destroy(vswp->fdb, (mod_hash_val_t)addr);
73161ae08745Sheppo 
73171ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
73181ae08745Sheppo 
73191ae08745Sheppo 	return (0);
73201ae08745Sheppo }
73211ae08745Sheppo 
73221ae08745Sheppo /*
73231ae08745Sheppo  * Search fdb for a given mac address.
73241ae08745Sheppo  * Returns pointer to the entry if found, else returns NULL.
73251ae08745Sheppo  */
73261ae08745Sheppo static vsw_port_t *
73271ae08745Sheppo vsw_lookup_fdb(vsw_t *vswp, struct ether_header *ehp)
73281ae08745Sheppo {
73291ae08745Sheppo 	uint64_t	key = 0;
73301ae08745Sheppo 	vsw_port_t	*port = NULL;
73311ae08745Sheppo 
73321ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
73331ae08745Sheppo 
73341ae08745Sheppo 	KEY_HASH(key, ehp->ether_dhost);
73351ae08745Sheppo 
73361ae08745Sheppo 	D2(vswp, "%s: key = 0x%llx", __func__, key);
73371ae08745Sheppo 
73381ae08745Sheppo 	if (mod_hash_find(vswp->fdb, (mod_hash_key_t)key,
73391ae08745Sheppo 				(mod_hash_val_t *)&port) != 0) {
734034683adeSsg70180 		D2(vswp, "%s: no port found", __func__);
73411ae08745Sheppo 		return (NULL);
73421ae08745Sheppo 	}
73431ae08745Sheppo 
73441ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
73451ae08745Sheppo 
73461ae08745Sheppo 	return (port);
73471ae08745Sheppo }
73481ae08745Sheppo 
73491ae08745Sheppo /*
73501ae08745Sheppo  * Add or remove multicast address(es).
73511ae08745Sheppo  *
73521ae08745Sheppo  * Returns 0 on success, 1 on failure.
73531ae08745Sheppo  */
73541ae08745Sheppo static int
73551ae08745Sheppo vsw_add_rem_mcst(vnet_mcast_msg_t *mcst_pkt, vsw_port_t *port)
73561ae08745Sheppo {
73571ae08745Sheppo 	mcst_addr_t		*mcst_p = NULL;
73581ae08745Sheppo 	vsw_t			*vswp = port->p_vswp;
73591ae08745Sheppo 	uint64_t		addr = 0x0;
736034683adeSsg70180 	int			i;
73611ae08745Sheppo 
73621ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
73631ae08745Sheppo 
73641ae08745Sheppo 	D2(vswp, "%s: %d addresses", __func__, mcst_pkt->count);
73651ae08745Sheppo 
736634683adeSsg70180 	mutex_enter(&vswp->mac_lock);
736734683adeSsg70180 	if (vswp->mh == NULL) {
736834683adeSsg70180 		mutex_exit(&vswp->mac_lock);
7369e1ebb9ecSlm66018 		return (1);
737034683adeSsg70180 	}
737134683adeSsg70180 	mutex_exit(&vswp->mac_lock);
7372e1ebb9ecSlm66018 
73731ae08745Sheppo 	for (i = 0; i < mcst_pkt->count; i++) {
73741ae08745Sheppo 		/*
73751ae08745Sheppo 		 * Convert address into form that can be used
73761ae08745Sheppo 		 * as hash table key.
73771ae08745Sheppo 		 */
73781ae08745Sheppo 		KEY_HASH(addr, mcst_pkt->mca[i]);
73791ae08745Sheppo 
73801ae08745Sheppo 		/*
73811ae08745Sheppo 		 * Add or delete the specified address/port combination.
73821ae08745Sheppo 		 */
73831ae08745Sheppo 		if (mcst_pkt->set == 0x1) {
73841ae08745Sheppo 			D3(vswp, "%s: adding multicast address 0x%llx for "
73851ae08745Sheppo 				"port %ld", __func__, addr, port->p_instance);
73861ae08745Sheppo 			if (vsw_add_mcst(vswp, VSW_VNETPORT, addr, port) == 0) {
73871ae08745Sheppo 				/*
73881ae08745Sheppo 				 * Update the list of multicast
73891ae08745Sheppo 				 * addresses contained within the
73901ae08745Sheppo 				 * port structure to include this new
73911ae08745Sheppo 				 * one.
73921ae08745Sheppo 				 */
73931ae08745Sheppo 				mcst_p = kmem_alloc(sizeof (mcst_addr_t),
73941ae08745Sheppo 								KM_NOSLEEP);
73951ae08745Sheppo 				if (mcst_p == NULL) {
73961ae08745Sheppo 					DERR(vswp, "%s: unable to alloc mem",
73971ae08745Sheppo 						__func__);
73981ae08745Sheppo 					return (1);
73991ae08745Sheppo 				}
74001ae08745Sheppo 
74011ae08745Sheppo 				mcst_p->nextp = NULL;
74021ae08745Sheppo 				mcst_p->addr = addr;
74031ae08745Sheppo 
74041ae08745Sheppo 				mutex_enter(&port->mca_lock);
74051ae08745Sheppo 				mcst_p->nextp = port->mcap;
74061ae08745Sheppo 				port->mcap = mcst_p;
74071ae08745Sheppo 				mutex_exit(&port->mca_lock);
74081ae08745Sheppo 
74091ae08745Sheppo 				/*
74101ae08745Sheppo 				 * Program the address into HW. If the addr
74111ae08745Sheppo 				 * has already been programmed then the MAC
74121ae08745Sheppo 				 * just increments a ref counter (which is
74131ae08745Sheppo 				 * used when the address is being deleted)
74141ae08745Sheppo 				 */
741534683adeSsg70180 				mutex_enter(&vswp->mac_lock);
741634683adeSsg70180 				if ((vswp->mh == NULL) ||
741734683adeSsg70180 					mac_multicst_add(vswp->mh,
741834683adeSsg70180 						(uchar_t *)&mcst_pkt->mca[i])) {
741934683adeSsg70180 					mutex_exit(&vswp->mac_lock);
742034683adeSsg70180 					cmn_err(CE_WARN, "!vsw%d: unable to "
742134683adeSsg70180 						"add multicast address",
742234683adeSsg70180 						vswp->instance);
7423e1ebb9ecSlm66018 					(void) vsw_del_mcst(vswp, VSW_VNETPORT,
7424e1ebb9ecSlm66018 						addr, port);
7425e1ebb9ecSlm66018 					vsw_del_addr(VSW_VNETPORT, port, addr);
742634683adeSsg70180 					return (1);
7427e1ebb9ecSlm66018 				}
742834683adeSsg70180 				mutex_exit(&vswp->mac_lock);
74291ae08745Sheppo 
74301ae08745Sheppo 			} else {
74311ae08745Sheppo 				DERR(vswp, "%s: error adding multicast "
74321ae08745Sheppo 					"address 0x%llx for port %ld",
74331ae08745Sheppo 					__func__, addr, port->p_instance);
74341ae08745Sheppo 				return (1);
74351ae08745Sheppo 			}
74361ae08745Sheppo 		} else {
74371ae08745Sheppo 			/*
74381ae08745Sheppo 			 * Delete an entry from the multicast hash
74391ae08745Sheppo 			 * table and update the address list
74401ae08745Sheppo 			 * appropriately.
74411ae08745Sheppo 			 */
74421ae08745Sheppo 			if (vsw_del_mcst(vswp, VSW_VNETPORT, addr, port) == 0) {
74431ae08745Sheppo 				D3(vswp, "%s: deleting multicast address "
74441ae08745Sheppo 					"0x%llx for port %ld", __func__, addr,
74451ae08745Sheppo 					port->p_instance);
74461ae08745Sheppo 
74471ae08745Sheppo 				vsw_del_addr(VSW_VNETPORT, port, addr);
74481ae08745Sheppo 
74491ae08745Sheppo 				/*
74501ae08745Sheppo 				 * Remove the address from HW. The address
74511ae08745Sheppo 				 * will actually only be removed once the ref
74521ae08745Sheppo 				 * count within the MAC layer has dropped to
74531ae08745Sheppo 				 * zero. I.e. we can safely call this fn even
74541ae08745Sheppo 				 * if other ports are interested in this
74551ae08745Sheppo 				 * address.
74561ae08745Sheppo 				 */
745734683adeSsg70180 				mutex_enter(&vswp->mac_lock);
745834683adeSsg70180 				if ((vswp->mh == NULL) ||
745934683adeSsg70180 					mac_multicst_remove(vswp->mh,
746034683adeSsg70180 						(uchar_t *)&mcst_pkt->mca[i])) {
746134683adeSsg70180 					mutex_exit(&vswp->mac_lock);
746234683adeSsg70180 					cmn_err(CE_WARN, "!vsw%d: unable to "
746334683adeSsg70180 						"remove multicast address",
746434683adeSsg70180 						vswp->instance);
746534683adeSsg70180 					return (1);
746634683adeSsg70180 				}
746734683adeSsg70180 				mutex_exit(&vswp->mac_lock);
74681ae08745Sheppo 
74691ae08745Sheppo 			} else {
74701ae08745Sheppo 				DERR(vswp, "%s: error deleting multicast "
74711ae08745Sheppo 					"addr 0x%llx for port %ld",
74721ae08745Sheppo 					__func__, addr, port->p_instance);
74731ae08745Sheppo 				return (1);
74741ae08745Sheppo 			}
74751ae08745Sheppo 		}
74761ae08745Sheppo 	}
74771ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
74781ae08745Sheppo 	return (0);
74791ae08745Sheppo }
74801ae08745Sheppo 
74811ae08745Sheppo /*
74821ae08745Sheppo  * Add a new multicast entry.
74831ae08745Sheppo  *
74841ae08745Sheppo  * Search hash table based on address. If match found then
74851ae08745Sheppo  * update associated val (which is chain of ports), otherwise
74861ae08745Sheppo  * create new key/val (addr/port) pair and insert into table.
74871ae08745Sheppo  */
74881ae08745Sheppo static int
74891ae08745Sheppo vsw_add_mcst(vsw_t *vswp, uint8_t devtype, uint64_t addr, void *arg)
74901ae08745Sheppo {
74911ae08745Sheppo 	int		dup = 0;
74921ae08745Sheppo 	int		rv = 0;
74931ae08745Sheppo 	mfdb_ent_t	*ment = NULL;
74941ae08745Sheppo 	mfdb_ent_t	*tmp_ent = NULL;
74951ae08745Sheppo 	mfdb_ent_t	*new_ent = NULL;
74961ae08745Sheppo 	void		*tgt = NULL;
74971ae08745Sheppo 
74981ae08745Sheppo 	if (devtype == VSW_VNETPORT) {
74991ae08745Sheppo 		/*
75001ae08745Sheppo 		 * Being invoked from a vnet.
75011ae08745Sheppo 		 */
75021ae08745Sheppo 		ASSERT(arg != NULL);
75031ae08745Sheppo 		tgt = arg;
75041ae08745Sheppo 		D2(NULL, "%s: port %d : address 0x%llx", __func__,
75051ae08745Sheppo 			((vsw_port_t *)arg)->p_instance, addr);
75061ae08745Sheppo 	} else {
75071ae08745Sheppo 		/*
75081ae08745Sheppo 		 * We are being invoked via the m_multicst mac entry
75091ae08745Sheppo 		 * point.
75101ae08745Sheppo 		 */
75111ae08745Sheppo 		D2(NULL, "%s: address 0x%llx", __func__, addr);
75121ae08745Sheppo 		tgt = (void *)vswp;
75131ae08745Sheppo 	}
75141ae08745Sheppo 
75151ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
75161ae08745Sheppo 	if (mod_hash_find(vswp->mfdb, (mod_hash_key_t)addr,
75171ae08745Sheppo 				(mod_hash_val_t *)&ment) != 0) {
75181ae08745Sheppo 
75191ae08745Sheppo 		/* address not currently in table */
75201ae08745Sheppo 		ment = kmem_alloc(sizeof (mfdb_ent_t), KM_SLEEP);
75211ae08745Sheppo 		ment->d_addr = (void *)tgt;
75221ae08745Sheppo 		ment->d_type = devtype;
75231ae08745Sheppo 		ment->nextp = NULL;
75241ae08745Sheppo 
75251ae08745Sheppo 		if (mod_hash_insert(vswp->mfdb, (mod_hash_key_t)addr,
75261ae08745Sheppo 			(mod_hash_val_t)ment) != 0) {
75271ae08745Sheppo 			DERR(vswp, "%s: hash table insertion failed", __func__);
75281ae08745Sheppo 			kmem_free(ment, sizeof (mfdb_ent_t));
75291ae08745Sheppo 			rv = 1;
75301ae08745Sheppo 		} else {
75311ae08745Sheppo 			D2(vswp, "%s: added initial entry for 0x%llx to "
75321ae08745Sheppo 				"table", __func__, addr);
75331ae08745Sheppo 		}
75341ae08745Sheppo 	} else {
75351ae08745Sheppo 		/*
75361ae08745Sheppo 		 * Address in table. Check to see if specified port
75371ae08745Sheppo 		 * is already associated with the address. If not add
75381ae08745Sheppo 		 * it now.
75391ae08745Sheppo 		 */
75401ae08745Sheppo 		tmp_ent = ment;
75411ae08745Sheppo 		while (tmp_ent != NULL) {
75421ae08745Sheppo 			if (tmp_ent->d_addr == (void *)tgt) {
75431ae08745Sheppo 				if (devtype == VSW_VNETPORT) {
75441ae08745Sheppo 					DERR(vswp, "%s: duplicate port entry "
75451ae08745Sheppo 						"found for portid %ld and key "
75461ae08745Sheppo 						"0x%llx", __func__,
75471ae08745Sheppo 						((vsw_port_t *)arg)->p_instance,
75481ae08745Sheppo 						addr);
75491ae08745Sheppo 				} else {
75501ae08745Sheppo 					DERR(vswp, "%s: duplicate entry found"
75511ae08745Sheppo 						"for key 0x%llx",
75521ae08745Sheppo 						__func__, addr);
75531ae08745Sheppo 				}
75541ae08745Sheppo 				rv = 1;
75551ae08745Sheppo 				dup = 1;
75561ae08745Sheppo 				break;
75571ae08745Sheppo 			}
75581ae08745Sheppo 			tmp_ent = tmp_ent->nextp;
75591ae08745Sheppo 		}
75601ae08745Sheppo 
75611ae08745Sheppo 		/*
75621ae08745Sheppo 		 * Port not on list so add it to end now.
75631ae08745Sheppo 		 */
75641ae08745Sheppo 		if (0 == dup) {
75651ae08745Sheppo 			D2(vswp, "%s: added entry for 0x%llx to table",
75661ae08745Sheppo 				__func__, addr);
75671ae08745Sheppo 			new_ent = kmem_alloc(sizeof (mfdb_ent_t), KM_SLEEP);
75681ae08745Sheppo 			new_ent->d_addr = (void *)tgt;
75691ae08745Sheppo 			new_ent->d_type = devtype;
75701ae08745Sheppo 			new_ent->nextp = NULL;
75711ae08745Sheppo 
75721ae08745Sheppo 			tmp_ent = ment;
75731ae08745Sheppo 			while (tmp_ent->nextp != NULL)
75741ae08745Sheppo 				tmp_ent = tmp_ent->nextp;
75751ae08745Sheppo 
75761ae08745Sheppo 			tmp_ent->nextp = new_ent;
75771ae08745Sheppo 		}
75781ae08745Sheppo 	}
75791ae08745Sheppo 
75801ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
75811ae08745Sheppo 	return (rv);
75821ae08745Sheppo }
75831ae08745Sheppo 
75841ae08745Sheppo /*
75851ae08745Sheppo  * Remove a multicast entry from the hashtable.
75861ae08745Sheppo  *
75871ae08745Sheppo  * Search hash table based on address. If match found, scan
75881ae08745Sheppo  * list of ports associated with address. If specified port
75891ae08745Sheppo  * found remove it from list.
75901ae08745Sheppo  */
75911ae08745Sheppo static int
75921ae08745Sheppo vsw_del_mcst(vsw_t *vswp, uint8_t devtype, uint64_t addr, void *arg)
75931ae08745Sheppo {
75941ae08745Sheppo 	mfdb_ent_t	*ment = NULL;
75951ae08745Sheppo 	mfdb_ent_t	*curr_p, *prev_p;
75961ae08745Sheppo 	void		*tgt = NULL;
75971ae08745Sheppo 
75981ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
75991ae08745Sheppo 
76001ae08745Sheppo 	if (devtype == VSW_VNETPORT) {
76011ae08745Sheppo 		tgt = (vsw_port_t *)arg;
76021ae08745Sheppo 		D2(vswp, "%s: removing port %d from mFDB for address"
76031ae08745Sheppo 			" 0x%llx", __func__, ((vsw_port_t *)tgt)->p_instance,
76041ae08745Sheppo 			addr);
76051ae08745Sheppo 	} else {
76061ae08745Sheppo 		D2(vswp, "%s: removing entry", __func__);
76071ae08745Sheppo 		tgt = (void *)vswp;
76081ae08745Sheppo 	}
76091ae08745Sheppo 
76101ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
76111ae08745Sheppo 	if (mod_hash_find(vswp->mfdb, (mod_hash_key_t)addr,
76121ae08745Sheppo 				(mod_hash_val_t *)&ment) != 0) {
76131ae08745Sheppo 		D2(vswp, "%s: address 0x%llx not in table", __func__, addr);
76141ae08745Sheppo 		RW_EXIT(&vswp->mfdbrw);
76151ae08745Sheppo 		return (1);
76161ae08745Sheppo 	}
76171ae08745Sheppo 
76181ae08745Sheppo 	prev_p = curr_p = ment;
76191ae08745Sheppo 
76201ae08745Sheppo 	while (curr_p != NULL) {
76211ae08745Sheppo 		if (curr_p->d_addr == (void *)tgt) {
76221ae08745Sheppo 			if (devtype == VSW_VNETPORT) {
76231ae08745Sheppo 				D2(vswp, "%s: port %d found", __func__,
76241ae08745Sheppo 					((vsw_port_t *)tgt)->p_instance);
76251ae08745Sheppo 			} else {
76261ae08745Sheppo 				D2(vswp, "%s: instance found", __func__);
76271ae08745Sheppo 			}
76281ae08745Sheppo 
76291ae08745Sheppo 			if (prev_p == curr_p) {
76301ae08745Sheppo 				/*
76311ae08745Sheppo 				 * head of list, if no other element is in
76321ae08745Sheppo 				 * list then destroy this entry, otherwise
76331ae08745Sheppo 				 * just replace it with updated value.
76341ae08745Sheppo 				 */
76351ae08745Sheppo 				ment = curr_p->nextp;
76361ae08745Sheppo 				kmem_free(curr_p, sizeof (mfdb_ent_t));
76371ae08745Sheppo 				if (ment == NULL) {
76381ae08745Sheppo 					(void) mod_hash_destroy(vswp->mfdb,
76391ae08745Sheppo 							(mod_hash_val_t)addr);
76401ae08745Sheppo 				} else {
76411ae08745Sheppo 					(void) mod_hash_replace(vswp->mfdb,
76421ae08745Sheppo 							(mod_hash_key_t)addr,
76431ae08745Sheppo 							(mod_hash_val_t)ment);
76441ae08745Sheppo 				}
76451ae08745Sheppo 			} else {
76461ae08745Sheppo 				/*
76471ae08745Sheppo 				 * Not head of list, no need to do
76481ae08745Sheppo 				 * replacement, just adjust list pointers.
76491ae08745Sheppo 				 */
76501ae08745Sheppo 				prev_p->nextp = curr_p->nextp;
76511ae08745Sheppo 				kmem_free(curr_p, sizeof (mfdb_ent_t));
76521ae08745Sheppo 			}
76531ae08745Sheppo 			break;
76541ae08745Sheppo 		}
76551ae08745Sheppo 
76561ae08745Sheppo 		prev_p = curr_p;
76571ae08745Sheppo 		curr_p = curr_p->nextp;
76581ae08745Sheppo 	}
76591ae08745Sheppo 
76601ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
76611ae08745Sheppo 
76621ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
76631ae08745Sheppo 
76641ae08745Sheppo 	return (0);
76651ae08745Sheppo }
76661ae08745Sheppo 
76671ae08745Sheppo /*
76681ae08745Sheppo  * Port is being deleted, but has registered an interest in one
76691ae08745Sheppo  * or more multicast groups. Using the list of addresses maintained
76701ae08745Sheppo  * within the port structure find the appropriate entry in the hash
76711ae08745Sheppo  * table and remove this port from the list of interested ports.
76721ae08745Sheppo  */
76731ae08745Sheppo static void
76741ae08745Sheppo vsw_del_mcst_port(vsw_port_t *port)
76751ae08745Sheppo {
76761ae08745Sheppo 	mcst_addr_t	*mcst_p = NULL;
76771ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
76781ae08745Sheppo 
76791ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
76801ae08745Sheppo 
76811ae08745Sheppo 	mutex_enter(&port->mca_lock);
76821ae08745Sheppo 	while (port->mcap != NULL) {
76831ae08745Sheppo 		(void) vsw_del_mcst(vswp, VSW_VNETPORT,
76841ae08745Sheppo 					port->mcap->addr, port);
76851ae08745Sheppo 
76861ae08745Sheppo 		mcst_p = port->mcap->nextp;
76871ae08745Sheppo 		kmem_free(port->mcap, sizeof (mcst_addr_t));
76881ae08745Sheppo 		port->mcap = mcst_p;
76891ae08745Sheppo 	}
76901ae08745Sheppo 	mutex_exit(&port->mca_lock);
76911ae08745Sheppo 
76921ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
76931ae08745Sheppo }
76941ae08745Sheppo 
76951ae08745Sheppo /*
76961ae08745Sheppo  * This vsw instance is detaching, but has registered an interest in one
76971ae08745Sheppo  * or more multicast groups. Using the list of addresses maintained
76981ae08745Sheppo  * within the vsw structure find the appropriate entry in the hash
76991ae08745Sheppo  * table and remove this instance from the list of interested ports.
77001ae08745Sheppo  */
77011ae08745Sheppo static void
77021ae08745Sheppo vsw_del_mcst_vsw(vsw_t *vswp)
77031ae08745Sheppo {
77041ae08745Sheppo 	mcst_addr_t	*next_p = NULL;
77051ae08745Sheppo 
77061ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
77071ae08745Sheppo 
77081ae08745Sheppo 	mutex_enter(&vswp->mca_lock);
77091ae08745Sheppo 
77101ae08745Sheppo 	while (vswp->mcap != NULL) {
77111ae08745Sheppo 		DERR(vswp, "%s: deleting addr 0x%llx",
77121ae08745Sheppo 			__func__, vswp->mcap->addr);
77131ae08745Sheppo 		(void) vsw_del_mcst(vswp, VSW_LOCALDEV,
77141ae08745Sheppo 				vswp->mcap->addr, NULL);
77151ae08745Sheppo 
77161ae08745Sheppo 		next_p = vswp->mcap->nextp;
77171ae08745Sheppo 		kmem_free(vswp->mcap, sizeof (mcst_addr_t));
77181ae08745Sheppo 		vswp->mcap = next_p;
77191ae08745Sheppo 	}
77201ae08745Sheppo 
77211ae08745Sheppo 	vswp->mcap = NULL;
77221ae08745Sheppo 	mutex_exit(&vswp->mca_lock);
77231ae08745Sheppo 
77241ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
77251ae08745Sheppo }
77261ae08745Sheppo 
77271ae08745Sheppo 
77281ae08745Sheppo /*
77291ae08745Sheppo  * Remove the specified address from the list of address maintained
77301ae08745Sheppo  * in this port node.
77311ae08745Sheppo  */
77321ae08745Sheppo static void
77331ae08745Sheppo vsw_del_addr(uint8_t devtype, void *arg, uint64_t addr)
77341ae08745Sheppo {
77351ae08745Sheppo 	vsw_t		*vswp = NULL;
77361ae08745Sheppo 	vsw_port_t	*port = NULL;
77371ae08745Sheppo 	mcst_addr_t	*prev_p = NULL;
77381ae08745Sheppo 	mcst_addr_t	*curr_p = NULL;
77391ae08745Sheppo 
77401ae08745Sheppo 	D1(NULL, "%s: enter : devtype %d : addr 0x%llx",
77411ae08745Sheppo 		__func__, devtype, addr);
77421ae08745Sheppo 
77431ae08745Sheppo 	if (devtype == VSW_VNETPORT) {
77441ae08745Sheppo 		port = (vsw_port_t *)arg;
77451ae08745Sheppo 		mutex_enter(&port->mca_lock);
77461ae08745Sheppo 		prev_p = curr_p = port->mcap;
77471ae08745Sheppo 	} else {
77481ae08745Sheppo 		vswp = (vsw_t *)arg;
77491ae08745Sheppo 		mutex_enter(&vswp->mca_lock);
77501ae08745Sheppo 		prev_p = curr_p = vswp->mcap;
77511ae08745Sheppo 	}
77521ae08745Sheppo 
77531ae08745Sheppo 	while (curr_p != NULL) {
77541ae08745Sheppo 		if (curr_p->addr == addr) {
77551ae08745Sheppo 			D2(NULL, "%s: address found", __func__);
77561ae08745Sheppo 			/* match found */
77571ae08745Sheppo 			if (prev_p == curr_p) {
77581ae08745Sheppo 				/* list head */
77591ae08745Sheppo 				if (devtype == VSW_VNETPORT)
77601ae08745Sheppo 					port->mcap = curr_p->nextp;
77611ae08745Sheppo 				else
77621ae08745Sheppo 					vswp->mcap = curr_p->nextp;
77631ae08745Sheppo 			} else {
77641ae08745Sheppo 				prev_p->nextp = curr_p->nextp;
77651ae08745Sheppo 			}
77661ae08745Sheppo 			kmem_free(curr_p, sizeof (mcst_addr_t));
77671ae08745Sheppo 			break;
77681ae08745Sheppo 		} else {
77691ae08745Sheppo 			prev_p = curr_p;
77701ae08745Sheppo 			curr_p = curr_p->nextp;
77711ae08745Sheppo 		}
77721ae08745Sheppo 	}
77731ae08745Sheppo 
77741ae08745Sheppo 	if (devtype == VSW_VNETPORT)
77751ae08745Sheppo 		mutex_exit(&port->mca_lock);
77761ae08745Sheppo 	else
77771ae08745Sheppo 		mutex_exit(&vswp->mca_lock);
77781ae08745Sheppo 
77791ae08745Sheppo 	D1(NULL, "%s: exit", __func__);
77801ae08745Sheppo }
77811ae08745Sheppo 
77821ae08745Sheppo /*
77831ae08745Sheppo  * Creates a descriptor ring (dring) and links it into the
77841ae08745Sheppo  * link of outbound drings for this channel.
77851ae08745Sheppo  *
77861ae08745Sheppo  * Returns NULL if creation failed.
77871ae08745Sheppo  */
77881ae08745Sheppo static dring_info_t *
77891ae08745Sheppo vsw_create_dring(vsw_ldc_t *ldcp)
77901ae08745Sheppo {
77911ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
77921ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
77931ae08745Sheppo 	ldc_mem_info_t		minfo;
77941ae08745Sheppo 	dring_info_t		*dp, *tp;
77951ae08745Sheppo 	int			i;
77961ae08745Sheppo 
77971ae08745Sheppo 	dp = (dring_info_t *)kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
77981ae08745Sheppo 
77991ae08745Sheppo 	mutex_init(&dp->dlock, NULL, MUTEX_DRIVER, NULL);
78001ae08745Sheppo 
78011ae08745Sheppo 	/* create public section of ring */
78021ae08745Sheppo 	if ((ldc_mem_dring_create(VSW_RING_NUM_EL,
78031ae08745Sheppo 			VSW_PUB_SIZE, &dp->handle)) != 0) {
78041ae08745Sheppo 
78051ae08745Sheppo 		DERR(vswp, "vsw_create_dring(%lld): ldc dring create "
78061ae08745Sheppo 			"failed", ldcp->ldc_id);
78071ae08745Sheppo 		goto create_fail_exit;
78081ae08745Sheppo 	}
78091ae08745Sheppo 
78101ae08745Sheppo 	ASSERT(dp->handle != NULL);
78111ae08745Sheppo 
78121ae08745Sheppo 	/*
78131ae08745Sheppo 	 * Get the base address of the public section of the ring.
78141ae08745Sheppo 	 */
78151ae08745Sheppo 	if ((ldc_mem_dring_info(dp->handle, &minfo)) != 0) {
78161ae08745Sheppo 		DERR(vswp, "vsw_create_dring(%lld): dring info failed\n",
78171ae08745Sheppo 			ldcp->ldc_id);
78181ae08745Sheppo 		goto dring_fail_exit;
78191ae08745Sheppo 	} else {
78201ae08745Sheppo 		ASSERT(minfo.vaddr != 0);
78211ae08745Sheppo 		dp->pub_addr = minfo.vaddr;
78221ae08745Sheppo 	}
78231ae08745Sheppo 
78241ae08745Sheppo 	dp->num_descriptors = VSW_RING_NUM_EL;
78251ae08745Sheppo 	dp->descriptor_size = VSW_PUB_SIZE;
78261ae08745Sheppo 	dp->options = VIO_TX_DRING;
78271ae08745Sheppo 	dp->ncookies = 1;	/* guaranteed by ldc */
78281ae08745Sheppo 
78291ae08745Sheppo 	/*
78301ae08745Sheppo 	 * create private portion of ring
78311ae08745Sheppo 	 */
78321ae08745Sheppo 	dp->priv_addr = (vsw_private_desc_t *)kmem_zalloc(
78331ae08745Sheppo 		(sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL), KM_SLEEP);
78341ae08745Sheppo 
78351ae08745Sheppo 	if (vsw_setup_ring(ldcp, dp)) {
78361ae08745Sheppo 		DERR(vswp, "%s: unable to setup ring", __func__);
78371ae08745Sheppo 		goto dring_fail_exit;
78381ae08745Sheppo 	}
78391ae08745Sheppo 
78401ae08745Sheppo 	/* haven't used any descriptors yet */
78411ae08745Sheppo 	dp->end_idx = 0;
7842d10e4ef2Snarayan 	dp->last_ack_recv = -1;
78431ae08745Sheppo 
78441ae08745Sheppo 	/* bind dring to the channel */
78451ae08745Sheppo 	if ((ldc_mem_dring_bind(ldcp->ldc_handle, dp->handle,
78461ae08745Sheppo 		LDC_SHADOW_MAP, LDC_MEM_RW,
78471ae08745Sheppo 		&dp->cookie[0], &dp->ncookies)) != 0) {
78481ae08745Sheppo 		DERR(vswp, "vsw_create_dring: unable to bind to channel "
78491ae08745Sheppo 			"%lld", ldcp->ldc_id);
78501ae08745Sheppo 		goto dring_fail_exit;
78511ae08745Sheppo 	}
78521ae08745Sheppo 
7853d10e4ef2Snarayan 	mutex_init(&dp->restart_lock, NULL, MUTEX_DRIVER, NULL);
7854d10e4ef2Snarayan 	dp->restart_reqd = B_TRUE;
7855d10e4ef2Snarayan 
78561ae08745Sheppo 	/*
78571ae08745Sheppo 	 * Only ever create rings for outgoing lane. Link it onto
78581ae08745Sheppo 	 * end of list.
78591ae08745Sheppo 	 */
7860*445b4c2eSsb155480 	WRITE_ENTER(&ldcp->lane_out.dlistrw);
78611ae08745Sheppo 	if (ldcp->lane_out.dringp == NULL) {
78621ae08745Sheppo 		D2(vswp, "vsw_create_dring: adding first outbound ring");
78631ae08745Sheppo 		ldcp->lane_out.dringp = dp;
78641ae08745Sheppo 	} else {
78651ae08745Sheppo 		tp = ldcp->lane_out.dringp;
78661ae08745Sheppo 		while (tp->next != NULL)
78671ae08745Sheppo 			tp = tp->next;
78681ae08745Sheppo 
78691ae08745Sheppo 		tp->next = dp;
78701ae08745Sheppo 	}
7871*445b4c2eSsb155480 	RW_EXIT(&ldcp->lane_out.dlistrw);
78721ae08745Sheppo 
78731ae08745Sheppo 	return (dp);
78741ae08745Sheppo 
78751ae08745Sheppo dring_fail_exit:
78761ae08745Sheppo 	(void) ldc_mem_dring_destroy(dp->handle);
78771ae08745Sheppo 
78781ae08745Sheppo create_fail_exit:
78791ae08745Sheppo 	if (dp->priv_addr != NULL) {
78801ae08745Sheppo 		priv_addr = dp->priv_addr;
78811ae08745Sheppo 		for (i = 0; i < VSW_RING_NUM_EL; i++) {
78821ae08745Sheppo 			if (priv_addr->memhandle != NULL)
78831ae08745Sheppo 				(void) ldc_mem_free_handle(
78841ae08745Sheppo 						priv_addr->memhandle);
78851ae08745Sheppo 			priv_addr++;
78861ae08745Sheppo 		}
78871ae08745Sheppo 		kmem_free(dp->priv_addr,
78881ae08745Sheppo 			(sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL));
78891ae08745Sheppo 	}
78901ae08745Sheppo 	mutex_destroy(&dp->dlock);
78911ae08745Sheppo 
78921ae08745Sheppo 	kmem_free(dp, sizeof (dring_info_t));
78931ae08745Sheppo 	return (NULL);
78941ae08745Sheppo }
78951ae08745Sheppo 
78961ae08745Sheppo /*
78971ae08745Sheppo  * Create a ring consisting of just a private portion and link
78981ae08745Sheppo  * it into the list of rings for the outbound lane.
78991ae08745Sheppo  *
79001ae08745Sheppo  * These type of rings are used primarily for temporary data
79011ae08745Sheppo  * storage (i.e. as data buffers).
79021ae08745Sheppo  */
79031ae08745Sheppo void
79041ae08745Sheppo vsw_create_privring(vsw_ldc_t *ldcp)
79051ae08745Sheppo {
79061ae08745Sheppo 	dring_info_t		*dp, *tp;
79071ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
79081ae08745Sheppo 
79091ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
79101ae08745Sheppo 
79111ae08745Sheppo 	dp = kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
79121ae08745Sheppo 
79131ae08745Sheppo 	mutex_init(&dp->dlock, NULL, MUTEX_DRIVER, NULL);
79141ae08745Sheppo 
79151ae08745Sheppo 	/* no public section */
79161ae08745Sheppo 	dp->pub_addr = NULL;
79171ae08745Sheppo 
79181ae08745Sheppo 	dp->priv_addr = kmem_zalloc((sizeof (vsw_private_desc_t) *
79191ae08745Sheppo 					VSW_RING_NUM_EL), KM_SLEEP);
79201ae08745Sheppo 
79214bac2208Snarayan 	dp->num_descriptors = VSW_RING_NUM_EL;
79224bac2208Snarayan 
79231ae08745Sheppo 	if (vsw_setup_ring(ldcp, dp)) {
79241ae08745Sheppo 		DERR(vswp, "%s: setup of ring failed", __func__);
79251ae08745Sheppo 		kmem_free(dp->priv_addr,
79261ae08745Sheppo 			(sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL));
79271ae08745Sheppo 		mutex_destroy(&dp->dlock);
79281ae08745Sheppo 		kmem_free(dp, sizeof (dring_info_t));
79291ae08745Sheppo 		return;
79301ae08745Sheppo 	}
79311ae08745Sheppo 
79321ae08745Sheppo 	/* haven't used any descriptors yet */
79331ae08745Sheppo 	dp->end_idx = 0;
79341ae08745Sheppo 
7935d10e4ef2Snarayan 	mutex_init(&dp->restart_lock, NULL, MUTEX_DRIVER, NULL);
7936d10e4ef2Snarayan 	dp->restart_reqd = B_TRUE;
7937d10e4ef2Snarayan 
79381ae08745Sheppo 	/*
79391ae08745Sheppo 	 * Only ever create rings for outgoing lane. Link it onto
79401ae08745Sheppo 	 * end of list.
79411ae08745Sheppo 	 */
7942*445b4c2eSsb155480 	WRITE_ENTER(&ldcp->lane_out.dlistrw);
79431ae08745Sheppo 	if (ldcp->lane_out.dringp == NULL) {
79441ae08745Sheppo 		D2(vswp, "%s: adding first outbound privring", __func__);
79451ae08745Sheppo 		ldcp->lane_out.dringp = dp;
79461ae08745Sheppo 	} else {
79471ae08745Sheppo 		tp = ldcp->lane_out.dringp;
79481ae08745Sheppo 		while (tp->next != NULL)
79491ae08745Sheppo 			tp = tp->next;
79501ae08745Sheppo 
79511ae08745Sheppo 		tp->next = dp;
79521ae08745Sheppo 	}
7953*445b4c2eSsb155480 	RW_EXIT(&ldcp->lane_out.dlistrw);
79541ae08745Sheppo 
79551ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
79561ae08745Sheppo }
79571ae08745Sheppo 
79581ae08745Sheppo /*
79591ae08745Sheppo  * Setup the descriptors in the dring. Returns 0 on success, 1 on
79601ae08745Sheppo  * failure.
79611ae08745Sheppo  */
79621ae08745Sheppo int
79631ae08745Sheppo vsw_setup_ring(vsw_ldc_t *ldcp, dring_info_t *dp)
79641ae08745Sheppo {
79651ae08745Sheppo 	vnet_public_desc_t	*pub_addr = NULL;
79661ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
79671ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
79681ae08745Sheppo 	uint64_t		*tmpp;
79691ae08745Sheppo 	uint64_t		offset = 0;
79701ae08745Sheppo 	uint32_t		ncookies = 0;
79711ae08745Sheppo 	static char		*name = "vsw_setup_ring";
7972d10e4ef2Snarayan 	int			i, j, nc, rv;
79731ae08745Sheppo 
79741ae08745Sheppo 	priv_addr = dp->priv_addr;
79751ae08745Sheppo 	pub_addr = dp->pub_addr;
79761ae08745Sheppo 
7977d10e4ef2Snarayan 	/* public section may be null but private should never be */
7978d10e4ef2Snarayan 	ASSERT(priv_addr != NULL);
7979d10e4ef2Snarayan 
79801ae08745Sheppo 	/*
79811ae08745Sheppo 	 * Allocate the region of memory which will be used to hold
79821ae08745Sheppo 	 * the data the descriptors will refer to.
79831ae08745Sheppo 	 */
79841ae08745Sheppo 	dp->data_sz = (VSW_RING_NUM_EL * VSW_RING_EL_DATA_SZ);
79851ae08745Sheppo 	dp->data_addr = kmem_alloc(dp->data_sz, KM_SLEEP);
79861ae08745Sheppo 
79871ae08745Sheppo 	D2(vswp, "%s: allocated %lld bytes at 0x%llx\n", name,
79881ae08745Sheppo 		dp->data_sz, dp->data_addr);
79891ae08745Sheppo 
79901ae08745Sheppo 	tmpp = (uint64_t *)dp->data_addr;
79911ae08745Sheppo 	offset = VSW_RING_EL_DATA_SZ / sizeof (tmpp);
79921ae08745Sheppo 
79931ae08745Sheppo 	/*
79941ae08745Sheppo 	 * Initialise some of the private and public (if they exist)
79951ae08745Sheppo 	 * descriptor fields.
79961ae08745Sheppo 	 */
79971ae08745Sheppo 	for (i = 0; i < VSW_RING_NUM_EL; i++) {
7998d10e4ef2Snarayan 		mutex_init(&priv_addr->dstate_lock, NULL, MUTEX_DRIVER, NULL);
7999d10e4ef2Snarayan 
80001ae08745Sheppo 		if ((ldc_mem_alloc_handle(ldcp->ldc_handle,
80011ae08745Sheppo 			&priv_addr->memhandle)) != 0) {
80021ae08745Sheppo 			DERR(vswp, "%s: alloc mem handle failed", name);
80031ae08745Sheppo 			goto setup_ring_cleanup;
80041ae08745Sheppo 		}
80051ae08745Sheppo 
80061ae08745Sheppo 		priv_addr->datap = (void *)tmpp;
80071ae08745Sheppo 
80081ae08745Sheppo 		rv = ldc_mem_bind_handle(priv_addr->memhandle,
80091ae08745Sheppo 			(caddr_t)priv_addr->datap, VSW_RING_EL_DATA_SZ,
80101ae08745Sheppo 			LDC_SHADOW_MAP, LDC_MEM_R|LDC_MEM_W,
80111ae08745Sheppo 			&(priv_addr->memcookie[0]), &ncookies);
80121ae08745Sheppo 		if (rv != 0) {
80131ae08745Sheppo 			DERR(vswp, "%s(%lld): ldc_mem_bind_handle failed "
80141ae08745Sheppo 				"(rv %d)", name, ldcp->ldc_id, rv);
80151ae08745Sheppo 			goto setup_ring_cleanup;
80161ae08745Sheppo 		}
80171ae08745Sheppo 		priv_addr->bound = 1;
80181ae08745Sheppo 
80191ae08745Sheppo 		D2(vswp, "%s: %d: memcookie 0 : addr 0x%llx : size 0x%llx",
80201ae08745Sheppo 			name, i, priv_addr->memcookie[0].addr,
80211ae08745Sheppo 			priv_addr->memcookie[0].size);
80221ae08745Sheppo 
80231ae08745Sheppo 		if (ncookies >= (uint32_t)(VSW_MAX_COOKIES + 1)) {
80241ae08745Sheppo 			DERR(vswp, "%s(%lld) ldc_mem_bind_handle returned "
80251ae08745Sheppo 				"invalid num of cookies (%d) for size 0x%llx",
80261ae08745Sheppo 				name, ldcp->ldc_id, ncookies,
80271ae08745Sheppo 				VSW_RING_EL_DATA_SZ);
80281ae08745Sheppo 
80291ae08745Sheppo 			goto setup_ring_cleanup;
80301ae08745Sheppo 		} else {
80311ae08745Sheppo 			for (j = 1; j < ncookies; j++) {
80321ae08745Sheppo 				rv = ldc_mem_nextcookie(priv_addr->memhandle,
80331ae08745Sheppo 					&(priv_addr->memcookie[j]));
80341ae08745Sheppo 				if (rv != 0) {
80351ae08745Sheppo 					DERR(vswp, "%s: ldc_mem_nextcookie "
80361ae08745Sheppo 						"failed rv (%d)", name, rv);
80371ae08745Sheppo 					goto setup_ring_cleanup;
80381ae08745Sheppo 				}
80391ae08745Sheppo 				D3(vswp, "%s: memcookie %d : addr 0x%llx : "
80401ae08745Sheppo 					"size 0x%llx", name, j,
80411ae08745Sheppo 					priv_addr->memcookie[j].addr,
80421ae08745Sheppo 					priv_addr->memcookie[j].size);
80431ae08745Sheppo 			}
80441ae08745Sheppo 
80451ae08745Sheppo 		}
80461ae08745Sheppo 		priv_addr->ncookies = ncookies;
80471ae08745Sheppo 		priv_addr->dstate = VIO_DESC_FREE;
80481ae08745Sheppo 
80491ae08745Sheppo 		if (pub_addr != NULL) {
80501ae08745Sheppo 
80511ae08745Sheppo 			/* link pub and private sides */
80521ae08745Sheppo 			priv_addr->descp = pub_addr;
80531ae08745Sheppo 
8054d10e4ef2Snarayan 			pub_addr->ncookies = priv_addr->ncookies;
8055d10e4ef2Snarayan 
8056d10e4ef2Snarayan 			for (nc = 0; nc < pub_addr->ncookies; nc++) {
8057d10e4ef2Snarayan 				bcopy(&priv_addr->memcookie[nc],
8058d10e4ef2Snarayan 					&pub_addr->memcookie[nc],
8059d10e4ef2Snarayan 					sizeof (ldc_mem_cookie_t));
8060d10e4ef2Snarayan 			}
8061d10e4ef2Snarayan 
80621ae08745Sheppo 			pub_addr->hdr.dstate = VIO_DESC_FREE;
80631ae08745Sheppo 			pub_addr++;
80641ae08745Sheppo 		}
80651ae08745Sheppo 
80661ae08745Sheppo 		/*
80671ae08745Sheppo 		 * move to next element in the dring and the next
80681ae08745Sheppo 		 * position in the data buffer.
80691ae08745Sheppo 		 */
80701ae08745Sheppo 		priv_addr++;
80711ae08745Sheppo 		tmpp += offset;
80721ae08745Sheppo 	}
80731ae08745Sheppo 
80741ae08745Sheppo 	return (0);
80751ae08745Sheppo 
80761ae08745Sheppo setup_ring_cleanup:
80771ae08745Sheppo 	priv_addr = dp->priv_addr;
80781ae08745Sheppo 
8079d10e4ef2Snarayan 	for (j = 0; j < i; j++) {
80801ae08745Sheppo 		(void) ldc_mem_unbind_handle(priv_addr->memhandle);
80811ae08745Sheppo 		(void) ldc_mem_free_handle(priv_addr->memhandle);
80821ae08745Sheppo 
8083d10e4ef2Snarayan 		mutex_destroy(&priv_addr->dstate_lock);
8084d10e4ef2Snarayan 
80851ae08745Sheppo 		priv_addr++;
80861ae08745Sheppo 	}
80871ae08745Sheppo 	kmem_free(dp->data_addr, dp->data_sz);
80881ae08745Sheppo 
80891ae08745Sheppo 	return (1);
80901ae08745Sheppo }
80911ae08745Sheppo 
80921ae08745Sheppo /*
80931ae08745Sheppo  * Searches the private section of a ring for a free descriptor,
80941ae08745Sheppo  * starting at the location of the last free descriptor found
80951ae08745Sheppo  * previously.
80961ae08745Sheppo  *
8097d10e4ef2Snarayan  * Returns 0 if free descriptor is available, and updates state
8098d10e4ef2Snarayan  * of private descriptor to VIO_DESC_READY,  otherwise returns 1.
80991ae08745Sheppo  *
81001ae08745Sheppo  * FUTURE: might need to return contiguous range of descriptors
81011ae08745Sheppo  * as dring info msg assumes all will be contiguous.
81021ae08745Sheppo  */
81031ae08745Sheppo static int
81041ae08745Sheppo vsw_dring_find_free_desc(dring_info_t *dringp,
81051ae08745Sheppo 		vsw_private_desc_t **priv_p, int *idx)
81061ae08745Sheppo {
8107d10e4ef2Snarayan 	vsw_private_desc_t	*addr = NULL;
81081ae08745Sheppo 	int			num = VSW_RING_NUM_EL;
81091ae08745Sheppo 	int			ret = 1;
81101ae08745Sheppo 
81111ae08745Sheppo 	D1(NULL, "%s enter\n", __func__);
81121ae08745Sheppo 
8113d10e4ef2Snarayan 	ASSERT(dringp->priv_addr != NULL);
81141ae08745Sheppo 
81151ae08745Sheppo 	D2(NULL, "%s: searching ring, dringp 0x%llx : start pos %lld",
8116d10e4ef2Snarayan 			__func__, dringp, dringp->end_idx);
81171ae08745Sheppo 
8118d10e4ef2Snarayan 	addr = (vsw_private_desc_t *)dringp->priv_addr + dringp->end_idx;
8119d10e4ef2Snarayan 
8120d10e4ef2Snarayan 	mutex_enter(&addr->dstate_lock);
81211ae08745Sheppo 	if (addr->dstate == VIO_DESC_FREE) {
8122d10e4ef2Snarayan 		addr->dstate = VIO_DESC_READY;
81231ae08745Sheppo 		*priv_p = addr;
8124d10e4ef2Snarayan 		*idx = dringp->end_idx;
8125d10e4ef2Snarayan 		dringp->end_idx = (dringp->end_idx + 1) % num;
81261ae08745Sheppo 		ret = 0;
8127d10e4ef2Snarayan 
81281ae08745Sheppo 	}
8129d10e4ef2Snarayan 	mutex_exit(&addr->dstate_lock);
81301ae08745Sheppo 
81311ae08745Sheppo 	/* ring full */
81321ae08745Sheppo 	if (ret == 1) {
8133d10e4ef2Snarayan 		D2(NULL, "%s: no desp free: started at %d", __func__,
8134d10e4ef2Snarayan 			dringp->end_idx);
81351ae08745Sheppo 	}
81361ae08745Sheppo 
81371ae08745Sheppo 	D1(NULL, "%s: exit\n", __func__);
81381ae08745Sheppo 
81391ae08745Sheppo 	return (ret);
81401ae08745Sheppo }
81411ae08745Sheppo 
81421ae08745Sheppo /*
81431ae08745Sheppo  * Map from a dring identifier to the ring itself. Returns
81441ae08745Sheppo  * pointer to ring or NULL if no match found.
8145*445b4c2eSsb155480  *
8146*445b4c2eSsb155480  * Should be called with dlistrw rwlock held as reader.
81471ae08745Sheppo  */
81481ae08745Sheppo static dring_info_t *
81491ae08745Sheppo vsw_ident2dring(lane_t *lane, uint64_t ident)
81501ae08745Sheppo {
81511ae08745Sheppo 	dring_info_t	*dp = NULL;
81521ae08745Sheppo 
81531ae08745Sheppo 	if ((dp = lane->dringp) == NULL) {
81541ae08745Sheppo 		return (NULL);
81551ae08745Sheppo 	} else {
81561ae08745Sheppo 		if (dp->ident == ident)
81571ae08745Sheppo 			return (dp);
81581ae08745Sheppo 
81591ae08745Sheppo 		while (dp != NULL) {
81601ae08745Sheppo 			if (dp->ident == ident)
81611ae08745Sheppo 				break;
81621ae08745Sheppo 			dp = dp->next;
81631ae08745Sheppo 		}
81641ae08745Sheppo 	}
81651ae08745Sheppo 
81661ae08745Sheppo 	return (dp);
81671ae08745Sheppo }
81681ae08745Sheppo 
81691ae08745Sheppo /*
81701ae08745Sheppo  * Set the default lane attributes. These are copied into
81711ae08745Sheppo  * the attr msg we send to our peer. If they are not acceptable
81721ae08745Sheppo  * then (currently) the handshake ends.
81731ae08745Sheppo  */
81741ae08745Sheppo static void
81751ae08745Sheppo vsw_set_lane_attr(vsw_t *vswp, lane_t *lp)
81761ae08745Sheppo {
81771ae08745Sheppo 	bzero(lp, sizeof (lane_t));
81781ae08745Sheppo 
81791ae08745Sheppo 	READ_ENTER(&vswp->if_lockrw);
81801ae08745Sheppo 	ether_copy(&(vswp->if_addr), &(lp->addr));
81811ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
81821ae08745Sheppo 
81831ae08745Sheppo 	lp->mtu = VSW_MTU;
81841ae08745Sheppo 	lp->addr_type = ADDR_TYPE_MAC;
81851ae08745Sheppo 	lp->xfer_mode = VIO_DRING_MODE;
81861ae08745Sheppo 	lp->ack_freq = 0;	/* for shared mode */
8187d10e4ef2Snarayan 
8188d10e4ef2Snarayan 	mutex_enter(&lp->seq_lock);
81891ae08745Sheppo 	lp->seq_num = VNET_ISS;
8190d10e4ef2Snarayan 	mutex_exit(&lp->seq_lock);
81911ae08745Sheppo }
81921ae08745Sheppo 
81931ae08745Sheppo /*
81941ae08745Sheppo  * Verify that the attributes are acceptable.
81951ae08745Sheppo  *
81961ae08745Sheppo  * FUTURE: If some attributes are not acceptable, change them
81971ae08745Sheppo  * our desired values.
81981ae08745Sheppo  */
81991ae08745Sheppo static int
82001ae08745Sheppo vsw_check_attr(vnet_attr_msg_t *pkt, vsw_port_t *port)
82011ae08745Sheppo {
82021ae08745Sheppo 	int	ret = 0;
82031ae08745Sheppo 
82041ae08745Sheppo 	D1(NULL, "vsw_check_attr enter\n");
82051ae08745Sheppo 
82061ae08745Sheppo 	/*
82071ae08745Sheppo 	 * Note we currently only support in-band descriptors
82081ae08745Sheppo 	 * and descriptor rings, not packet based transfer (VIO_PKT_MODE)
82091ae08745Sheppo 	 */
82101ae08745Sheppo 	if ((pkt->xfer_mode != VIO_DESC_MODE) &&
82111ae08745Sheppo 			(pkt->xfer_mode != VIO_DRING_MODE)) {
82121ae08745Sheppo 		D2(NULL, "vsw_check_attr: unknown mode %x\n",
82131ae08745Sheppo 			pkt->xfer_mode);
82141ae08745Sheppo 		ret = 1;
82151ae08745Sheppo 	}
82161ae08745Sheppo 
82171ae08745Sheppo 	/* Only support MAC addresses at moment. */
82181ae08745Sheppo 	if ((pkt->addr_type != ADDR_TYPE_MAC) || (pkt->addr == 0)) {
82191ae08745Sheppo 		D2(NULL, "vsw_check_attr: invalid addr_type %x, "
82201ae08745Sheppo 			"or address 0x%llx\n", pkt->addr_type,
82211ae08745Sheppo 			pkt->addr);
82221ae08745Sheppo 		ret = 1;
82231ae08745Sheppo 	}
82241ae08745Sheppo 
82251ae08745Sheppo 	/*
82261ae08745Sheppo 	 * MAC address supplied by device should match that stored
82271ae08745Sheppo 	 * in the vsw-port OBP node. Need to decide what to do if they
82281ae08745Sheppo 	 * don't match, for the moment just warn but don't fail.
82291ae08745Sheppo 	 */
82301ae08745Sheppo 	if (bcmp(&pkt->addr, &port->p_macaddr, ETHERADDRL) != 0) {
82311ae08745Sheppo 		DERR(NULL, "vsw_check_attr: device supplied address "
82321ae08745Sheppo 			"0x%llx doesn't match node address 0x%llx\n",
82331ae08745Sheppo 			pkt->addr, port->p_macaddr);
82341ae08745Sheppo 	}
82351ae08745Sheppo 
82361ae08745Sheppo 	/*
82371ae08745Sheppo 	 * Ack freq only makes sense in pkt mode, in shared
82381ae08745Sheppo 	 * mode the ring descriptors say whether or not to
82391ae08745Sheppo 	 * send back an ACK.
82401ae08745Sheppo 	 */
82411ae08745Sheppo 	if ((pkt->xfer_mode == VIO_DRING_MODE) &&
82421ae08745Sheppo 				(pkt->ack_freq > 0)) {
82431ae08745Sheppo 		D2(NULL, "vsw_check_attr: non zero ack freq "
82441ae08745Sheppo 			" in SHM mode\n");
82451ae08745Sheppo 		ret = 1;
82461ae08745Sheppo 	}
82471ae08745Sheppo 
82481ae08745Sheppo 	/*
82491ae08745Sheppo 	 * Note: for the moment we only support ETHER
82501ae08745Sheppo 	 * frames. This may change in the future.
82511ae08745Sheppo 	 */
82521ae08745Sheppo 	if ((pkt->mtu > VSW_MTU) || (pkt->mtu <= 0)) {
82531ae08745Sheppo 		D2(NULL, "vsw_check_attr: invalid MTU (0x%llx)\n",
82541ae08745Sheppo 			pkt->mtu);
82551ae08745Sheppo 		ret = 1;
82561ae08745Sheppo 	}
82571ae08745Sheppo 
82581ae08745Sheppo 	D1(NULL, "vsw_check_attr exit\n");
82591ae08745Sheppo 
82601ae08745Sheppo 	return (ret);
82611ae08745Sheppo }
82621ae08745Sheppo 
82631ae08745Sheppo /*
82641ae08745Sheppo  * Returns 1 if there is a problem, 0 otherwise.
82651ae08745Sheppo  */
82661ae08745Sheppo static int
82671ae08745Sheppo vsw_check_dring_info(vio_dring_reg_msg_t *pkt)
82681ae08745Sheppo {
82691ae08745Sheppo 	_NOTE(ARGUNUSED(pkt))
82701ae08745Sheppo 
82711ae08745Sheppo 	int	ret = 0;
82721ae08745Sheppo 
82731ae08745Sheppo 	D1(NULL, "vsw_check_dring_info enter\n");
82741ae08745Sheppo 
82751ae08745Sheppo 	if ((pkt->num_descriptors == 0) ||
82761ae08745Sheppo 		(pkt->descriptor_size == 0) ||
82771ae08745Sheppo 		(pkt->ncookies != 1)) {
82781ae08745Sheppo 		DERR(NULL, "vsw_check_dring_info: invalid dring msg");
82791ae08745Sheppo 		ret = 1;
82801ae08745Sheppo 	}
82811ae08745Sheppo 
82821ae08745Sheppo 	D1(NULL, "vsw_check_dring_info exit\n");
82831ae08745Sheppo 
82841ae08745Sheppo 	return (ret);
82851ae08745Sheppo }
82861ae08745Sheppo 
82871ae08745Sheppo /*
82881ae08745Sheppo  * Returns 1 if two memory cookies match. Otherwise returns 0.
82891ae08745Sheppo  */
82901ae08745Sheppo static int
82911ae08745Sheppo vsw_mem_cookie_match(ldc_mem_cookie_t *m1, ldc_mem_cookie_t *m2)
82921ae08745Sheppo {
82931ae08745Sheppo 	if ((m1->addr != m2->addr) ||
82941ae08745Sheppo 		(m2->size != m2->size)) {
82951ae08745Sheppo 		return (0);
82961ae08745Sheppo 	} else {
82971ae08745Sheppo 		return (1);
82981ae08745Sheppo 	}
82991ae08745Sheppo }
83001ae08745Sheppo 
83011ae08745Sheppo /*
83021ae08745Sheppo  * Returns 1 if ring described in reg message matches that
83031ae08745Sheppo  * described by dring_info structure. Otherwise returns 0.
83041ae08745Sheppo  */
83051ae08745Sheppo static int
83061ae08745Sheppo vsw_dring_match(dring_info_t *dp, vio_dring_reg_msg_t *msg)
83071ae08745Sheppo {
83081ae08745Sheppo 	if ((msg->descriptor_size != dp->descriptor_size) ||
83091ae08745Sheppo 		(msg->num_descriptors != dp->num_descriptors) ||
83101ae08745Sheppo 		(msg->ncookies != dp->ncookies) ||
83111ae08745Sheppo 		!(vsw_mem_cookie_match(&msg->cookie[0], &dp->cookie[0]))) {
83121ae08745Sheppo 		return (0);
83131ae08745Sheppo 	} else {
83141ae08745Sheppo 		return (1);
83151ae08745Sheppo 	}
83161ae08745Sheppo 
83171ae08745Sheppo }
83181ae08745Sheppo 
83191ae08745Sheppo static caddr_t
83201ae08745Sheppo vsw_print_ethaddr(uint8_t *a, char *ebuf)
83211ae08745Sheppo {
83221ae08745Sheppo 	(void) sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
83231ae08745Sheppo 	    a[0], a[1], a[2], a[3], a[4], a[5]);
83241ae08745Sheppo 	return (ebuf);
83251ae08745Sheppo }
83261ae08745Sheppo 
83271ae08745Sheppo /*
83281ae08745Sheppo  * Reset and free all the resources associated with
83291ae08745Sheppo  * the channel.
83301ae08745Sheppo  */
83311ae08745Sheppo static void
83321ae08745Sheppo vsw_free_lane_resources(vsw_ldc_t *ldcp, uint64_t dir)
83331ae08745Sheppo {
83341ae08745Sheppo 	dring_info_t		*dp, *dpp;
83351ae08745Sheppo 	lane_t			*lp = NULL;
83361ae08745Sheppo 	int			rv = 0;
83371ae08745Sheppo 
83381ae08745Sheppo 	ASSERT(ldcp != NULL);
83391ae08745Sheppo 
83401ae08745Sheppo 	D1(ldcp->ldc_vswp, "%s (%lld): enter", __func__, ldcp->ldc_id);
83411ae08745Sheppo 
83421ae08745Sheppo 	if (dir == INBOUND) {
83431ae08745Sheppo 		D2(ldcp->ldc_vswp, "%s: freeing INBOUND lane"
83441ae08745Sheppo 			" of channel %lld", __func__, ldcp->ldc_id);
83451ae08745Sheppo 		lp = &ldcp->lane_in;
83461ae08745Sheppo 	} else {
83471ae08745Sheppo 		D2(ldcp->ldc_vswp, "%s: freeing OUTBOUND lane"
83481ae08745Sheppo 			" of channel %lld", __func__, ldcp->ldc_id);
83491ae08745Sheppo 		lp = &ldcp->lane_out;
83501ae08745Sheppo 	}
83511ae08745Sheppo 
83521ae08745Sheppo 	lp->lstate = VSW_LANE_INACTIV;
8353d10e4ef2Snarayan 	mutex_enter(&lp->seq_lock);
83541ae08745Sheppo 	lp->seq_num = VNET_ISS;
8355d10e4ef2Snarayan 	mutex_exit(&lp->seq_lock);
83561ae08745Sheppo 	if (lp->dringp) {
83571ae08745Sheppo 		if (dir == INBOUND) {
8358*445b4c2eSsb155480 			WRITE_ENTER(&lp->dlistrw);
83591ae08745Sheppo 			dp = lp->dringp;
83601ae08745Sheppo 			while (dp != NULL) {
83611ae08745Sheppo 				dpp = dp->next;
83621ae08745Sheppo 				if (dp->handle != NULL)
83631ae08745Sheppo 					(void) ldc_mem_dring_unmap(dp->handle);
83641ae08745Sheppo 				kmem_free(dp, sizeof (dring_info_t));
83651ae08745Sheppo 				dp = dpp;
83661ae08745Sheppo 			}
8367*445b4c2eSsb155480 			RW_EXIT(&lp->dlistrw);
83681ae08745Sheppo 		} else {
83691ae08745Sheppo 			/*
83701ae08745Sheppo 			 * unbind, destroy exported dring, free dring struct
83711ae08745Sheppo 			 */
8372*445b4c2eSsb155480 			WRITE_ENTER(&lp->dlistrw);
83731ae08745Sheppo 			dp = lp->dringp;
83741ae08745Sheppo 			rv = vsw_free_ring(dp);
8375*445b4c2eSsb155480 			RW_EXIT(&lp->dlistrw);
83761ae08745Sheppo 		}
83771ae08745Sheppo 		if (rv == 0) {
83781ae08745Sheppo 			lp->dringp = NULL;
83791ae08745Sheppo 		}
83801ae08745Sheppo 	}
83811ae08745Sheppo 
83821ae08745Sheppo 	D1(ldcp->ldc_vswp, "%s (%lld): exit", __func__, ldcp->ldc_id);
83831ae08745Sheppo }
83841ae08745Sheppo 
83851ae08745Sheppo /*
83861ae08745Sheppo  * Free ring and all associated resources.
8387*445b4c2eSsb155480  *
8388*445b4c2eSsb155480  * Should be called with dlistrw rwlock held as writer.
83891ae08745Sheppo  */
83901ae08745Sheppo static int
83911ae08745Sheppo vsw_free_ring(dring_info_t *dp)
83921ae08745Sheppo {
83931ae08745Sheppo 	vsw_private_desc_t	*paddr = NULL;
83941ae08745Sheppo 	dring_info_t		*dpp;
83951ae08745Sheppo 	int			i, rv = 1;
83961ae08745Sheppo 
83971ae08745Sheppo 	while (dp != NULL) {
83981ae08745Sheppo 		mutex_enter(&dp->dlock);
83991ae08745Sheppo 		dpp = dp->next;
84001ae08745Sheppo 		if (dp->priv_addr != NULL) {
84011ae08745Sheppo 			/*
84021ae08745Sheppo 			 * First unbind and free the memory handles
84031ae08745Sheppo 			 * stored in each descriptor within the ring.
84041ae08745Sheppo 			 */
84051ae08745Sheppo 			for (i = 0; i < VSW_RING_NUM_EL; i++) {
84061ae08745Sheppo 				paddr = (vsw_private_desc_t *)
84071ae08745Sheppo 						dp->priv_addr + i;
84081ae08745Sheppo 				if (paddr->memhandle != NULL) {
84091ae08745Sheppo 					if (paddr->bound == 1) {
84101ae08745Sheppo 						rv = ldc_mem_unbind_handle(
84111ae08745Sheppo 							paddr->memhandle);
84121ae08745Sheppo 
84131ae08745Sheppo 						if (rv != 0) {
84141ae08745Sheppo 							DERR(NULL, "error "
84151ae08745Sheppo 							"unbinding handle for "
84161ae08745Sheppo 							"ring 0x%llx at pos %d",
84171ae08745Sheppo 							dp, i);
84181ae08745Sheppo 							mutex_exit(&dp->dlock);
84191ae08745Sheppo 							return (rv);
84201ae08745Sheppo 						}
84211ae08745Sheppo 						paddr->bound = 0;
84221ae08745Sheppo 					}
84231ae08745Sheppo 
84241ae08745Sheppo 					rv = ldc_mem_free_handle(
84251ae08745Sheppo 							paddr->memhandle);
84261ae08745Sheppo 					if (rv != 0) {
84271ae08745Sheppo 						DERR(NULL, "error freeing "
84281ae08745Sheppo 							"handle for ring "
84291ae08745Sheppo 							"0x%llx at pos %d",
84301ae08745Sheppo 							dp, i);
84311ae08745Sheppo 						mutex_exit(&dp->dlock);
84321ae08745Sheppo 						return (rv);
84331ae08745Sheppo 					}
84341ae08745Sheppo 					paddr->memhandle = NULL;
84351ae08745Sheppo 				}
8436d10e4ef2Snarayan 				mutex_destroy(&paddr->dstate_lock);
84371ae08745Sheppo 			}
84381ae08745Sheppo 			kmem_free(dp->priv_addr, (sizeof (vsw_private_desc_t)
84391ae08745Sheppo 					* VSW_RING_NUM_EL));
84401ae08745Sheppo 		}
84411ae08745Sheppo 
84421ae08745Sheppo 		/*
84431ae08745Sheppo 		 * Now unbind and destroy the ring itself.
84441ae08745Sheppo 		 */
84451ae08745Sheppo 		if (dp->handle != NULL) {
84461ae08745Sheppo 			(void) ldc_mem_dring_unbind(dp->handle);
84471ae08745Sheppo 			(void) ldc_mem_dring_destroy(dp->handle);
84481ae08745Sheppo 		}
84491ae08745Sheppo 
84501ae08745Sheppo 		if (dp->data_addr != NULL) {
84511ae08745Sheppo 			kmem_free(dp->data_addr, dp->data_sz);
84521ae08745Sheppo 		}
84531ae08745Sheppo 
84541ae08745Sheppo 		mutex_exit(&dp->dlock);
84551ae08745Sheppo 		mutex_destroy(&dp->dlock);
8456d10e4ef2Snarayan 		mutex_destroy(&dp->restart_lock);
84571ae08745Sheppo 		kmem_free(dp, sizeof (dring_info_t));
84581ae08745Sheppo 
84591ae08745Sheppo 		dp = dpp;
84601ae08745Sheppo 	}
84611ae08745Sheppo 	return (0);
84621ae08745Sheppo }
84631ae08745Sheppo 
84641ae08745Sheppo /*
84651ae08745Sheppo  * Debugging routines
84661ae08745Sheppo  */
84671ae08745Sheppo static void
84681ae08745Sheppo display_state(void)
84691ae08745Sheppo {
84701ae08745Sheppo 	vsw_t		*vswp;
84711ae08745Sheppo 	vsw_port_list_t	*plist;
84721ae08745Sheppo 	vsw_port_t 	*port;
84731ae08745Sheppo 	vsw_ldc_list_t	*ldcl;
84741ae08745Sheppo 	vsw_ldc_t 	*ldcp;
84751ae08745Sheppo 
84761ae08745Sheppo 	cmn_err(CE_NOTE, "***** system state *****");
84771ae08745Sheppo 
84781ae08745Sheppo 	for (vswp = vsw_head; vswp; vswp = vswp->next) {
84791ae08745Sheppo 		plist = &vswp->plist;
84801ae08745Sheppo 		READ_ENTER(&plist->lockrw);
84811ae08745Sheppo 		cmn_err(CE_CONT, "vsw instance %d has %d ports attached\n",
84821ae08745Sheppo 			vswp->instance, plist->num_ports);
84831ae08745Sheppo 
84841ae08745Sheppo 		for (port = plist->head; port != NULL; port = port->p_next) {
84851ae08745Sheppo 			ldcl = &port->p_ldclist;
84861ae08745Sheppo 			cmn_err(CE_CONT, "port %d : %d ldcs attached\n",
84871ae08745Sheppo 				port->p_instance, ldcl->num_ldcs);
84881ae08745Sheppo 			READ_ENTER(&ldcl->lockrw);
84891ae08745Sheppo 			ldcp = ldcl->head;
84901ae08745Sheppo 			for (; ldcp != NULL; ldcp = ldcp->ldc_next) {
84911ae08745Sheppo 				cmn_err(CE_CONT, "chan %lu : dev %d : "
84921ae08745Sheppo 					"status %d : phase %u\n",
84931ae08745Sheppo 					ldcp->ldc_id, ldcp->dev_class,
84941ae08745Sheppo 					ldcp->ldc_status, ldcp->hphase);
84951ae08745Sheppo 				cmn_err(CE_CONT, "chan %lu : lsession %lu : "
84961ae08745Sheppo 					"psession %lu\n",
84971ae08745Sheppo 					ldcp->ldc_id,
84981ae08745Sheppo 					ldcp->local_session,
84991ae08745Sheppo 					ldcp->peer_session);
85001ae08745Sheppo 
85011ae08745Sheppo 				cmn_err(CE_CONT, "Inbound lane:\n");
85021ae08745Sheppo 				display_lane(&ldcp->lane_in);
85031ae08745Sheppo 				cmn_err(CE_CONT, "Outbound lane:\n");
85041ae08745Sheppo 				display_lane(&ldcp->lane_out);
85051ae08745Sheppo 			}
85061ae08745Sheppo 			RW_EXIT(&ldcl->lockrw);
85071ae08745Sheppo 		}
85081ae08745Sheppo 		RW_EXIT(&plist->lockrw);
85091ae08745Sheppo 	}
85101ae08745Sheppo 	cmn_err(CE_NOTE, "***** system state *****");
85111ae08745Sheppo }
85121ae08745Sheppo 
85131ae08745Sheppo static void
85141ae08745Sheppo display_lane(lane_t *lp)
85151ae08745Sheppo {
85161ae08745Sheppo 	dring_info_t	*drp;
85171ae08745Sheppo 
85181ae08745Sheppo 	cmn_err(CE_CONT, "ver 0x%x:0x%x : state %lx : mtu 0x%lx\n",
85191ae08745Sheppo 		lp->ver_major, lp->ver_minor, lp->lstate, lp->mtu);
85201ae08745Sheppo 	cmn_err(CE_CONT, "addr_type %d : addr 0x%lx : xmode %d\n",
85211ae08745Sheppo 		lp->addr_type, lp->addr, lp->xfer_mode);
85221ae08745Sheppo 	cmn_err(CE_CONT, "dringp 0x%lx\n", (uint64_t)lp->dringp);
85231ae08745Sheppo 
85241ae08745Sheppo 	cmn_err(CE_CONT, "Dring info:\n");
85251ae08745Sheppo 	for (drp = lp->dringp; drp != NULL; drp = drp->next) {
85261ae08745Sheppo 		cmn_err(CE_CONT, "\tnum_desc %u : dsize %u\n",
85271ae08745Sheppo 			drp->num_descriptors, drp->descriptor_size);
85281ae08745Sheppo 		cmn_err(CE_CONT, "\thandle 0x%lx\n", drp->handle);
85291ae08745Sheppo 		cmn_err(CE_CONT, "\tpub_addr 0x%lx : priv_addr 0x%lx\n",
85301ae08745Sheppo 			(uint64_t)drp->pub_addr, (uint64_t)drp->priv_addr);
85311ae08745Sheppo 		cmn_err(CE_CONT, "\tident 0x%lx : end_idx %lu\n",
85321ae08745Sheppo 			drp->ident, drp->end_idx);
85331ae08745Sheppo 		display_ring(drp);
85341ae08745Sheppo 	}
85351ae08745Sheppo }
85361ae08745Sheppo 
85371ae08745Sheppo static void
85381ae08745Sheppo display_ring(dring_info_t *dringp)
85391ae08745Sheppo {
85401ae08745Sheppo 	uint64_t		i;
85411ae08745Sheppo 	uint64_t		priv_count = 0;
85421ae08745Sheppo 	uint64_t		pub_count = 0;
85431ae08745Sheppo 	vnet_public_desc_t	*pub_addr = NULL;
85441ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
85451ae08745Sheppo 
85461ae08745Sheppo 	for (i = 0; i < VSW_RING_NUM_EL; i++) {
85471ae08745Sheppo 		if (dringp->pub_addr != NULL) {
85481ae08745Sheppo 			pub_addr = (vnet_public_desc_t *)dringp->pub_addr + i;
85491ae08745Sheppo 
85501ae08745Sheppo 			if (pub_addr->hdr.dstate == VIO_DESC_FREE)
85511ae08745Sheppo 				pub_count++;
85521ae08745Sheppo 		}
85531ae08745Sheppo 
85541ae08745Sheppo 		if (dringp->priv_addr != NULL) {
85551ae08745Sheppo 			priv_addr =
85561ae08745Sheppo 				(vsw_private_desc_t *)dringp->priv_addr + i;
85571ae08745Sheppo 
85581ae08745Sheppo 			if (priv_addr->dstate == VIO_DESC_FREE)
85591ae08745Sheppo 				priv_count++;
85601ae08745Sheppo 		}
85611ae08745Sheppo 	}
85621ae08745Sheppo 	cmn_err(CE_CONT, "\t%lu elements: %lu priv free: %lu pub free\n",
85631ae08745Sheppo 			i, priv_count, pub_count);
85641ae08745Sheppo }
85651ae08745Sheppo 
85661ae08745Sheppo static void
85671ae08745Sheppo dump_flags(uint64_t state)
85681ae08745Sheppo {
85691ae08745Sheppo 	int	i;
85701ae08745Sheppo 
85711ae08745Sheppo 	typedef struct flag_name {
85721ae08745Sheppo 		int	flag_val;
85731ae08745Sheppo 		char	*flag_name;
85741ae08745Sheppo 	} flag_name_t;
85751ae08745Sheppo 
85761ae08745Sheppo 	flag_name_t	flags[] = {
85771ae08745Sheppo 		VSW_VER_INFO_SENT, "VSW_VER_INFO_SENT",
85781ae08745Sheppo 		VSW_VER_INFO_RECV, "VSW_VER_INFO_RECV",
85791ae08745Sheppo 		VSW_VER_ACK_RECV, "VSW_VER_ACK_RECV",
85801ae08745Sheppo 		VSW_VER_ACK_SENT, "VSW_VER_ACK_SENT",
85811ae08745Sheppo 		VSW_VER_NACK_RECV, "VSW_VER_NACK_RECV",
85821ae08745Sheppo 		VSW_VER_NACK_SENT, "VSW_VER_NACK_SENT",
85831ae08745Sheppo 		VSW_ATTR_INFO_SENT, "VSW_ATTR_INFO_SENT",
85841ae08745Sheppo 		VSW_ATTR_INFO_RECV, "VSW_ATTR_INFO_RECV",
85851ae08745Sheppo 		VSW_ATTR_ACK_SENT, "VSW_ATTR_ACK_SENT",
85861ae08745Sheppo 		VSW_ATTR_ACK_RECV, "VSW_ATTR_ACK_RECV",
85871ae08745Sheppo 		VSW_ATTR_NACK_SENT, "VSW_ATTR_NACK_SENT",
85881ae08745Sheppo 		VSW_ATTR_NACK_RECV, "VSW_ATTR_NACK_RECV",
85891ae08745Sheppo 		VSW_DRING_INFO_SENT, "VSW_DRING_INFO_SENT",
85901ae08745Sheppo 		VSW_DRING_INFO_RECV, "VSW_DRING_INFO_RECV",
85911ae08745Sheppo 		VSW_DRING_ACK_SENT, "VSW_DRING_ACK_SENT",
85921ae08745Sheppo 		VSW_DRING_ACK_RECV, "VSW_DRING_ACK_RECV",
85931ae08745Sheppo 		VSW_DRING_NACK_SENT, "VSW_DRING_NACK_SENT",
85941ae08745Sheppo 		VSW_DRING_NACK_RECV, "VSW_DRING_NACK_RECV",
85951ae08745Sheppo 		VSW_RDX_INFO_SENT, "VSW_RDX_INFO_SENT",
85961ae08745Sheppo 		VSW_RDX_INFO_RECV, "VSW_RDX_INFO_RECV",
85971ae08745Sheppo 		VSW_RDX_ACK_SENT, "VSW_RDX_ACK_SENT",
85981ae08745Sheppo 		VSW_RDX_ACK_RECV, "VSW_RDX_ACK_RECV",
85991ae08745Sheppo 		VSW_RDX_NACK_SENT, "VSW_RDX_NACK_SENT",
86001ae08745Sheppo 		VSW_RDX_NACK_RECV, "VSW_RDX_NACK_RECV",
86011ae08745Sheppo 		VSW_MCST_INFO_SENT, "VSW_MCST_INFO_SENT",
86021ae08745Sheppo 		VSW_MCST_INFO_RECV, "VSW_MCST_INFO_RECV",
86031ae08745Sheppo 		VSW_MCST_ACK_SENT, "VSW_MCST_ACK_SENT",
86041ae08745Sheppo 		VSW_MCST_ACK_RECV, "VSW_MCST_ACK_RECV",
86051ae08745Sheppo 		VSW_MCST_NACK_SENT, "VSW_MCST_NACK_SENT",
86061ae08745Sheppo 		VSW_MCST_NACK_RECV, "VSW_MCST_NACK_RECV",
86071ae08745Sheppo 		VSW_LANE_ACTIVE, "VSW_LANE_ACTIVE"};
86081ae08745Sheppo 
86091ae08745Sheppo 	DERR(NULL, "DUMP_FLAGS: %llx\n", state);
86101ae08745Sheppo 	for (i = 0; i < sizeof (flags)/sizeof (flag_name_t); i++) {
86111ae08745Sheppo 		if (state & flags[i].flag_val)
86121ae08745Sheppo 			DERR(NULL, "DUMP_FLAGS %s", flags[i].flag_name);
86131ae08745Sheppo 	}
86141ae08745Sheppo }
8615