xref: /titanic_53/usr/src/uts/sun4v/io/vsw.c (revision b071742beee33760f5b3bd8889b958469462a079)
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 /*
23*b071742bSsg70180  * Copyright 2007 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 */
151*b071742bSsg70180 static	void vsw_ldc_reinit(vsw_ldc_t *);
152*b071742bSsg70180 static	void vsw_process_conn_evt(vsw_ldc_t *, uint16_t);
153*b071742bSsg70180 static	void vsw_conn_task(void *);
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 
194*b071742bSsg70180 static int vsw_send_msg(vsw_ldc_t *, void *, int, boolean_t);
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 
232445b4c2eSsb155480 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,
293*b071742bSsg70180 	"sun4v Virtual Switch %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);
3293445b4c2eSsb155480 	rw_init(&ldcp->lane_in.dlistrw, NULL, RW_DRIVER, NULL);
3294445b4c2eSsb155480 	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 
3358445b4c2eSsb155480 	rw_destroy(&ldcp->lane_in.dlistrw);
3359445b4c2eSsb155480 	rw_destroy(&ldcp->lane_out.dlistrw);
3360445b4c2eSsb155480 
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);
3472445b4c2eSsb155480 	rw_destroy(&ldcp->lane_in.dlistrw);
3473445b4c2eSsb155480 	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 	if (ldc_status(ldcp->ldc_handle, &ldcp->ldc_status) != 0) {
35441ae08745Sheppo 		DERR(vswp, "%s: unable to get status", __func__);
35453af08d82Slm66018 		mutex_exit(&ldcp->status_lock);
35461ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35471ae08745Sheppo 		return (1);
35481ae08745Sheppo 
35491ae08745Sheppo 	}
3550*b071742bSsg70180 
3551*b071742bSsg70180 	if (ldcp->ldc_status == LDC_UP) {
3552*b071742bSsg70180 		D2(vswp, "%s: channel %ld now UP (%ld)", __func__,
3553*b071742bSsg70180 			ldcp->ldc_id, istatus);
35543af08d82Slm66018 		mutex_exit(&ldcp->status_lock);
35551ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35561ae08745Sheppo 
3557*b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_UP);
3558*b071742bSsg70180 		return (0);
35593af08d82Slm66018 	}
35603af08d82Slm66018 
3561*b071742bSsg70180 	mutex_exit(&ldcp->status_lock);
3562*b071742bSsg70180 	LDC_EXIT_LOCK(ldcp);
3563*b071742bSsg70180 
35641ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
35651ae08745Sheppo 	return (0);
35661ae08745Sheppo }
35671ae08745Sheppo 
35681ae08745Sheppo /* disable callbacks on the channel */
35691ae08745Sheppo static int
35701ae08745Sheppo vsw_ldc_uninit(vsw_ldc_t *ldcp)
35711ae08745Sheppo {
35721ae08745Sheppo 	vsw_t	*vswp = ldcp->ldc_vswp;
35731ae08745Sheppo 	int	rv;
35741ae08745Sheppo 
35751ae08745Sheppo 	D1(vswp, "vsw_ldc_uninit: enter: id(%lx)\n", ldcp->ldc_id);
35761ae08745Sheppo 
35771ae08745Sheppo 	LDC_ENTER_LOCK(ldcp);
35781ae08745Sheppo 
35791ae08745Sheppo 	rv = ldc_set_cb_mode(ldcp->ldc_handle, LDC_CB_DISABLE);
35801ae08745Sheppo 	if (rv != 0) {
35811ae08745Sheppo 		DERR(vswp, "vsw_ldc_uninit(%lld): error disabling "
35821ae08745Sheppo 			"interrupts (rv = %d)\n", ldcp->ldc_id, rv);
35831ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35841ae08745Sheppo 		return (1);
35851ae08745Sheppo 	}
35861ae08745Sheppo 
35873af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
35881ae08745Sheppo 	ldcp->ldc_status = LDC_INIT;
35893af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
35901ae08745Sheppo 
35911ae08745Sheppo 	LDC_EXIT_LOCK(ldcp);
35921ae08745Sheppo 
35931ae08745Sheppo 	D1(vswp, "vsw_ldc_uninit: exit: id(%lx)", ldcp->ldc_id);
35941ae08745Sheppo 
35951ae08745Sheppo 	return (0);
35961ae08745Sheppo }
35971ae08745Sheppo 
35981ae08745Sheppo static int
35991ae08745Sheppo vsw_init_ldcs(vsw_port_t *port)
36001ae08745Sheppo {
36011ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
36021ae08745Sheppo 	vsw_ldc_t	*ldcp;
36031ae08745Sheppo 
36041ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
36051ae08745Sheppo 	ldcp =  ldcl->head;
36061ae08745Sheppo 	for (; ldcp  != NULL; ldcp = ldcp->ldc_next) {
36071ae08745Sheppo 		(void) vsw_ldc_init(ldcp);
36081ae08745Sheppo 	}
36091ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
36101ae08745Sheppo 
36111ae08745Sheppo 	return (0);
36121ae08745Sheppo }
36131ae08745Sheppo 
36141ae08745Sheppo static int
36151ae08745Sheppo vsw_uninit_ldcs(vsw_port_t *port)
36161ae08745Sheppo {
36171ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
36181ae08745Sheppo 	vsw_ldc_t	*ldcp;
36191ae08745Sheppo 
36201ae08745Sheppo 	D1(NULL, "vsw_uninit_ldcs: enter\n");
36211ae08745Sheppo 
36221ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
36231ae08745Sheppo 	ldcp =  ldcl->head;
36241ae08745Sheppo 	for (; ldcp  != NULL; ldcp = ldcp->ldc_next) {
36251ae08745Sheppo 		(void) vsw_ldc_uninit(ldcp);
36261ae08745Sheppo 	}
36271ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
36281ae08745Sheppo 
36291ae08745Sheppo 	D1(NULL, "vsw_uninit_ldcs: exit\n");
36301ae08745Sheppo 
36311ae08745Sheppo 	return (0);
36321ae08745Sheppo }
36331ae08745Sheppo 
36341ae08745Sheppo /*
36351ae08745Sheppo  * Wait until the callback(s) associated with the ldcs under the specified
36361ae08745Sheppo  * port have completed.
36371ae08745Sheppo  *
36381ae08745Sheppo  * Prior to this function being invoked each channel under this port
36391ae08745Sheppo  * should have been quiesced via ldc_set_cb_mode(DISABLE).
36401ae08745Sheppo  *
36411ae08745Sheppo  * A short explaination of what we are doing below..
36421ae08745Sheppo  *
36431ae08745Sheppo  * The simplest approach would be to have a reference counter in
36441ae08745Sheppo  * the ldc structure which is increment/decremented by the callbacks as
36451ae08745Sheppo  * they use the channel. The drain function could then simply disable any
36461ae08745Sheppo  * further callbacks and do a cv_wait for the ref to hit zero. Unfortunately
36471ae08745Sheppo  * there is a tiny window here - before the callback is able to get the lock
36481ae08745Sheppo  * on the channel it is interrupted and this function gets to execute. It
36491ae08745Sheppo  * sees that the ref count is zero and believes its free to delete the
36501ae08745Sheppo  * associated data structures.
36511ae08745Sheppo  *
36521ae08745Sheppo  * We get around this by taking advantage of the fact that before the ldc
36531ae08745Sheppo  * framework invokes a callback it sets a flag to indicate that there is a
36541ae08745Sheppo  * callback active (or about to become active). If when we attempt to
36551ae08745Sheppo  * unregister a callback when this active flag is set then the unregister
36561ae08745Sheppo  * will fail with EWOULDBLOCK.
36571ae08745Sheppo  *
36581ae08745Sheppo  * If the unregister fails we do a cv_timedwait. We will either be signaled
36591ae08745Sheppo  * by the callback as it is exiting (note we have to wait a short period to
36601ae08745Sheppo  * allow the callback to return fully to the ldc framework and it to clear
36611ae08745Sheppo  * the active flag), or by the timer expiring. In either case we again attempt
36621ae08745Sheppo  * the unregister. We repeat this until we can succesfully unregister the
36631ae08745Sheppo  * callback.
36641ae08745Sheppo  *
36651ae08745Sheppo  * The reason we use a cv_timedwait rather than a simple cv_wait is to catch
36661ae08745Sheppo  * the case where the callback has finished but the ldc framework has not yet
36671ae08745Sheppo  * cleared the active flag. In this case we would never get a cv_signal.
36681ae08745Sheppo  */
36691ae08745Sheppo static int
36701ae08745Sheppo vsw_drain_ldcs(vsw_port_t *port)
36711ae08745Sheppo {
36721ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
36731ae08745Sheppo 	vsw_ldc_t	*ldcp;
36741ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
36751ae08745Sheppo 
36761ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
36771ae08745Sheppo 
36781ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
36791ae08745Sheppo 
36801ae08745Sheppo 	ldcp = ldcl->head;
36811ae08745Sheppo 
36821ae08745Sheppo 	for (; ldcp  != NULL; ldcp = ldcp->ldc_next) {
36831ae08745Sheppo 		/*
36841ae08745Sheppo 		 * If we can unregister the channel callback then we
36851ae08745Sheppo 		 * know that there is no callback either running or
36861ae08745Sheppo 		 * scheduled to run for this channel so move on to next
36871ae08745Sheppo 		 * channel in the list.
36881ae08745Sheppo 		 */
36891ae08745Sheppo 		mutex_enter(&ldcp->drain_cv_lock);
36901ae08745Sheppo 
36911ae08745Sheppo 		/* prompt active callbacks to quit */
36921ae08745Sheppo 		ldcp->drain_state = VSW_LDC_DRAINING;
36931ae08745Sheppo 
36941ae08745Sheppo 		if ((ldc_unreg_callback(ldcp->ldc_handle)) == 0) {
36951ae08745Sheppo 			D2(vswp, "%s: unreg callback for chan %ld", __func__,
36961ae08745Sheppo 				ldcp->ldc_id);
36971ae08745Sheppo 			mutex_exit(&ldcp->drain_cv_lock);
36981ae08745Sheppo 			continue;
36991ae08745Sheppo 		} else {
37001ae08745Sheppo 			/*
37011ae08745Sheppo 			 * If we end up here we know that either 1) a callback
37021ae08745Sheppo 			 * is currently executing, 2) is about to start (i.e.
37031ae08745Sheppo 			 * the ldc framework has set the active flag but
37041ae08745Sheppo 			 * has not actually invoked the callback yet, or 3)
37051ae08745Sheppo 			 * has finished and has returned to the ldc framework
37061ae08745Sheppo 			 * but the ldc framework has not yet cleared the
37071ae08745Sheppo 			 * active bit.
37081ae08745Sheppo 			 *
37091ae08745Sheppo 			 * Wait for it to finish.
37101ae08745Sheppo 			 */
37111ae08745Sheppo 			while (ldc_unreg_callback(ldcp->ldc_handle)
37121ae08745Sheppo 								== EWOULDBLOCK)
37131ae08745Sheppo 				(void) cv_timedwait(&ldcp->drain_cv,
37141ae08745Sheppo 					&ldcp->drain_cv_lock, lbolt + hz);
37151ae08745Sheppo 
37161ae08745Sheppo 			mutex_exit(&ldcp->drain_cv_lock);
37171ae08745Sheppo 			D2(vswp, "%s: unreg callback for chan %ld after "
37181ae08745Sheppo 				"timeout", __func__, ldcp->ldc_id);
37191ae08745Sheppo 		}
37201ae08745Sheppo 	}
37211ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
37221ae08745Sheppo 
37231ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
37241ae08745Sheppo 	return (0);
37251ae08745Sheppo }
37261ae08745Sheppo 
37271ae08745Sheppo /*
37281ae08745Sheppo  * Wait until all tasks which reference this port have completed.
37291ae08745Sheppo  *
37301ae08745Sheppo  * Prior to this function being invoked each channel under this port
37311ae08745Sheppo  * should have been quiesced via ldc_set_cb_mode(DISABLE).
37321ae08745Sheppo  */
37331ae08745Sheppo static int
37341ae08745Sheppo vsw_drain_port_taskq(vsw_port_t *port)
37351ae08745Sheppo {
37361ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
37371ae08745Sheppo 
37381ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
37391ae08745Sheppo 
37401ae08745Sheppo 	/*
37411ae08745Sheppo 	 * Mark the port as in the process of being detached, and
37421ae08745Sheppo 	 * dispatch a marker task to the queue so we know when all
37431ae08745Sheppo 	 * relevant tasks have completed.
37441ae08745Sheppo 	 */
37451ae08745Sheppo 	mutex_enter(&port->state_lock);
37461ae08745Sheppo 	port->state = VSW_PORT_DETACHING;
37471ae08745Sheppo 
37481ae08745Sheppo 	if ((vswp->taskq_p == NULL) ||
37491ae08745Sheppo 		(ddi_taskq_dispatch(vswp->taskq_p, vsw_marker_task,
37501ae08745Sheppo 			port, DDI_NOSLEEP) != DDI_SUCCESS)) {
37511ae08745Sheppo 		DERR(vswp, "%s: unable to dispatch marker task",
37521ae08745Sheppo 			__func__);
37531ae08745Sheppo 		mutex_exit(&port->state_lock);
37541ae08745Sheppo 		return (1);
37551ae08745Sheppo 	}
37561ae08745Sheppo 
37571ae08745Sheppo 	/*
37581ae08745Sheppo 	 * Wait for the marker task to finish.
37591ae08745Sheppo 	 */
37601ae08745Sheppo 	while (port->state != VSW_PORT_DETACHABLE)
37611ae08745Sheppo 		cv_wait(&port->state_cv, &port->state_lock);
37621ae08745Sheppo 
37631ae08745Sheppo 	mutex_exit(&port->state_lock);
37641ae08745Sheppo 
37651ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
37661ae08745Sheppo 
37671ae08745Sheppo 	return (0);
37681ae08745Sheppo }
37691ae08745Sheppo 
37701ae08745Sheppo static void
37711ae08745Sheppo vsw_marker_task(void *arg)
37721ae08745Sheppo {
37731ae08745Sheppo 	vsw_port_t	*port = arg;
37741ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
37751ae08745Sheppo 
37761ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
37771ae08745Sheppo 
37781ae08745Sheppo 	mutex_enter(&port->state_lock);
37791ae08745Sheppo 
37801ae08745Sheppo 	/*
37811ae08745Sheppo 	 * No further tasks should be dispatched which reference
37821ae08745Sheppo 	 * this port so ok to mark it as safe to detach.
37831ae08745Sheppo 	 */
37841ae08745Sheppo 	port->state = VSW_PORT_DETACHABLE;
37851ae08745Sheppo 
37861ae08745Sheppo 	cv_signal(&port->state_cv);
37871ae08745Sheppo 
37881ae08745Sheppo 	mutex_exit(&port->state_lock);
37891ae08745Sheppo 
37901ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
37911ae08745Sheppo }
37921ae08745Sheppo 
37931ae08745Sheppo static vsw_port_t *
37941ae08745Sheppo vsw_lookup_port(vsw_t *vswp, int p_instance)
37951ae08745Sheppo {
37961ae08745Sheppo 	vsw_port_list_t *plist = &vswp->plist;
37971ae08745Sheppo 	vsw_port_t	*port;
37981ae08745Sheppo 
37991ae08745Sheppo 	for (port = plist->head; port != NULL; port = port->p_next) {
38001ae08745Sheppo 		if (port->p_instance == p_instance) {
38011ae08745Sheppo 			D2(vswp, "vsw_lookup_port: found p_instance\n");
38021ae08745Sheppo 			return (port);
38031ae08745Sheppo 		}
38041ae08745Sheppo 	}
38051ae08745Sheppo 
38061ae08745Sheppo 	return (NULL);
38071ae08745Sheppo }
38081ae08745Sheppo 
38091ae08745Sheppo /*
38101ae08745Sheppo  * Search for and remove the specified port from the port
38111ae08745Sheppo  * list. Returns 0 if able to locate and remove port, otherwise
38121ae08745Sheppo  * returns 1.
38131ae08745Sheppo  */
38141ae08745Sheppo static int
38151ae08745Sheppo vsw_plist_del_node(vsw_t *vswp, vsw_port_t *port)
38161ae08745Sheppo {
38171ae08745Sheppo 	vsw_port_list_t *plist = &vswp->plist;
38181ae08745Sheppo 	vsw_port_t	*curr_p, *prev_p;
38191ae08745Sheppo 
38201ae08745Sheppo 	if (plist->head == NULL)
38211ae08745Sheppo 		return (1);
38221ae08745Sheppo 
38231ae08745Sheppo 	curr_p = prev_p = plist->head;
38241ae08745Sheppo 
38251ae08745Sheppo 	while (curr_p != NULL) {
38261ae08745Sheppo 		if (curr_p == port) {
38271ae08745Sheppo 			if (prev_p == curr_p) {
38281ae08745Sheppo 				plist->head = curr_p->p_next;
38291ae08745Sheppo 			} else {
38301ae08745Sheppo 				prev_p->p_next = curr_p->p_next;
38311ae08745Sheppo 			}
38321ae08745Sheppo 			plist->num_ports--;
38331ae08745Sheppo 			break;
38341ae08745Sheppo 		} else {
38351ae08745Sheppo 			prev_p = curr_p;
38361ae08745Sheppo 			curr_p = curr_p->p_next;
38371ae08745Sheppo 		}
38381ae08745Sheppo 	}
38391ae08745Sheppo 	return (0);
38401ae08745Sheppo }
38411ae08745Sheppo 
38421ae08745Sheppo /*
38431ae08745Sheppo  * Interrupt handler for ldc messages.
38441ae08745Sheppo  */
38451ae08745Sheppo static uint_t
38461ae08745Sheppo vsw_ldc_cb(uint64_t event, caddr_t arg)
38471ae08745Sheppo {
38481ae08745Sheppo 	vsw_ldc_t	*ldcp = (vsw_ldc_t  *)arg;
38491ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
38501ae08745Sheppo 
38511ae08745Sheppo 	D1(vswp, "%s: enter: ldcid (%lld)\n", __func__, ldcp->ldc_id);
38521ae08745Sheppo 
38531ae08745Sheppo 	mutex_enter(&ldcp->ldc_cblock);
38541ae08745Sheppo 
3855*b071742bSsg70180 	mutex_enter(&ldcp->status_lock);
38561ae08745Sheppo 	if ((ldcp->ldc_status == LDC_INIT) || (ldcp->ldc_handle == NULL)) {
3857*b071742bSsg70180 		mutex_exit(&ldcp->status_lock);
38581ae08745Sheppo 		mutex_exit(&ldcp->ldc_cblock);
38591ae08745Sheppo 		return (LDC_SUCCESS);
38601ae08745Sheppo 	}
38613af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
38623af08d82Slm66018 
38631ae08745Sheppo 	if (event & LDC_EVT_UP) {
38641ae08745Sheppo 		/*
3865*b071742bSsg70180 		 * Channel has come up.
38661ae08745Sheppo 		 */
38671ae08745Sheppo 		D2(vswp, "%s: id(%ld) event(%llx) UP: status(%ld)",
3868*b071742bSsg70180 			__func__, ldcp->ldc_id, event, ldcp->ldc_status);
3869*b071742bSsg70180 
3870*b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_UP);
38711ae08745Sheppo 
38721ae08745Sheppo 		ASSERT((event & (LDC_EVT_RESET | LDC_EVT_DOWN)) == 0);
38731ae08745Sheppo 	}
38741ae08745Sheppo 
38751ae08745Sheppo 	if (event & LDC_EVT_READ) {
38761ae08745Sheppo 		/*
38771ae08745Sheppo 		 * Data available for reading.
38781ae08745Sheppo 		 */
38791ae08745Sheppo 		D2(vswp, "%s: id(ld) event(%llx) data READ",
38801ae08745Sheppo 				__func__, ldcp->ldc_id, event);
38811ae08745Sheppo 
38821ae08745Sheppo 		vsw_process_pkt(ldcp);
38831ae08745Sheppo 
38841ae08745Sheppo 		ASSERT((event & (LDC_EVT_RESET | LDC_EVT_DOWN)) == 0);
38851ae08745Sheppo 
38861ae08745Sheppo 		goto vsw_cb_exit;
38871ae08745Sheppo 	}
38881ae08745Sheppo 
38893af08d82Slm66018 	if (event & (LDC_EVT_DOWN | LDC_EVT_RESET)) {
3890*b071742bSsg70180 		D2(vswp, "%s: id(%ld) event (%lx) DOWN/RESET: status(%ld)",
3891*b071742bSsg70180 			__func__, ldcp->ldc_id, event, ldcp->ldc_status);
38923af08d82Slm66018 
3893*b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
38941ae08745Sheppo 	}
38951ae08745Sheppo 
38961ae08745Sheppo 	/*
38971ae08745Sheppo 	 * Catch either LDC_EVT_WRITE which we don't support or any
38981ae08745Sheppo 	 * unknown event.
38991ae08745Sheppo 	 */
39001ae08745Sheppo 	if (event & ~(LDC_EVT_UP | LDC_EVT_RESET
39011ae08745Sheppo 					| LDC_EVT_DOWN | LDC_EVT_READ)) {
39021ae08745Sheppo 
39031ae08745Sheppo 		DERR(vswp, "%s: id(%ld) Unexpected event=(%llx) status(%ld)",
39041ae08745Sheppo 			__func__, ldcp->ldc_id, event, ldcp->ldc_status);
39051ae08745Sheppo 	}
39061ae08745Sheppo 
39071ae08745Sheppo vsw_cb_exit:
39081ae08745Sheppo 	mutex_exit(&ldcp->ldc_cblock);
39091ae08745Sheppo 
39101ae08745Sheppo 	/*
39111ae08745Sheppo 	 * Let the drain function know we are finishing if it
39121ae08745Sheppo 	 * is waiting.
39131ae08745Sheppo 	 */
39141ae08745Sheppo 	mutex_enter(&ldcp->drain_cv_lock);
39151ae08745Sheppo 	if (ldcp->drain_state == VSW_LDC_DRAINING)
39161ae08745Sheppo 		cv_signal(&ldcp->drain_cv);
39171ae08745Sheppo 	mutex_exit(&ldcp->drain_cv_lock);
39181ae08745Sheppo 
39191ae08745Sheppo 	return (LDC_SUCCESS);
39201ae08745Sheppo }
39211ae08745Sheppo 
39221ae08745Sheppo /*
3923*b071742bSsg70180  * Reinitialise data structures associated with the channel.
39241ae08745Sheppo  */
39251ae08745Sheppo static void
3926*b071742bSsg70180 vsw_ldc_reinit(vsw_ldc_t *ldcp)
39271ae08745Sheppo {
39281ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
39291ae08745Sheppo 	vsw_port_t	*port;
39301ae08745Sheppo 	vsw_ldc_list_t	*ldcl;
39311ae08745Sheppo 
39323af08d82Slm66018 	D1(vswp, "%s: enter", __func__);
39331ae08745Sheppo 
39341ae08745Sheppo 	port = ldcp->ldc_port;
39351ae08745Sheppo 	ldcl = &port->p_ldclist;
39361ae08745Sheppo 
39373af08d82Slm66018 	READ_ENTER(&ldcl->lockrw);
39381ae08745Sheppo 
39391ae08745Sheppo 	D2(vswp, "%s: in 0x%llx : out 0x%llx", __func__,
39401ae08745Sheppo 		ldcp->lane_in.lstate, ldcp->lane_out.lstate);
39411ae08745Sheppo 
39421ae08745Sheppo 	vsw_free_lane_resources(ldcp, INBOUND);
39431ae08745Sheppo 	vsw_free_lane_resources(ldcp, OUTBOUND);
39441ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
39451ae08745Sheppo 
39461ae08745Sheppo 	ldcp->lane_in.lstate = 0;
39471ae08745Sheppo 	ldcp->lane_out.lstate = 0;
39481ae08745Sheppo 
39491ae08745Sheppo 	/*
39501ae08745Sheppo 	 * Remove parent port from any multicast groups
39511ae08745Sheppo 	 * it may have registered with. Client must resend
39521ae08745Sheppo 	 * multicast add command after handshake completes.
39531ae08745Sheppo 	 */
39541ae08745Sheppo 	(void) vsw_del_fdb(vswp, port);
39551ae08745Sheppo 
39561ae08745Sheppo 	vsw_del_mcst_port(port);
39571ae08745Sheppo 
39581ae08745Sheppo 	ldcp->peer_session = 0;
39591ae08745Sheppo 	ldcp->session_status = 0;
39603af08d82Slm66018 	ldcp->hcnt = 0;
39613af08d82Slm66018 	ldcp->hphase = VSW_MILESTONE0;
39623af08d82Slm66018 
39633af08d82Slm66018 	D1(vswp, "%s: exit", __func__);
39643af08d82Slm66018 }
39653af08d82Slm66018 
39663af08d82Slm66018 /*
3967*b071742bSsg70180  * Process a connection event.
3968*b071742bSsg70180  *
3969*b071742bSsg70180  * Note - care must be taken to ensure that this function is
3970*b071742bSsg70180  * not called with the dlistrw lock held.
39713af08d82Slm66018  */
39723af08d82Slm66018 static void
3973*b071742bSsg70180 vsw_process_conn_evt(vsw_ldc_t *ldcp, uint16_t evt)
39743af08d82Slm66018 {
39753af08d82Slm66018 	vsw_t		*vswp = ldcp->ldc_vswp;
3976*b071742bSsg70180 	vsw_conn_evt_t	*conn = NULL;
39773af08d82Slm66018 
3978*b071742bSsg70180 	D1(vswp, "%s: enter", __func__);
39791ae08745Sheppo 
39801ae08745Sheppo 	/*
3981*b071742bSsg70180 	 * Check if either a reset or restart event is pending
3982*b071742bSsg70180 	 * or in progress. If so just return.
3983*b071742bSsg70180 	 *
3984*b071742bSsg70180 	 * A VSW_CONN_RESET event originates either with a LDC_RESET_EVT
3985*b071742bSsg70180 	 * being received by the callback handler, or a ECONNRESET error
3986*b071742bSsg70180 	 * code being returned from a ldc_read() or ldc_write() call.
3987*b071742bSsg70180 	 *
3988*b071742bSsg70180 	 * A VSW_CONN_RESTART event occurs when some error checking code
3989*b071742bSsg70180 	 * decides that there is a problem with data from the channel,
3990*b071742bSsg70180 	 * and that the handshake should be restarted.
3991*b071742bSsg70180 	 */
3992*b071742bSsg70180 	if (((evt == VSW_CONN_RESET) || (evt == VSW_CONN_RESTART)) &&
3993*b071742bSsg70180 			(ldstub((uint8_t *)&ldcp->reset_active)))
3994*b071742bSsg70180 		return;
3995*b071742bSsg70180 
3996*b071742bSsg70180 	/*
3997*b071742bSsg70180 	 * If it is an LDC_UP event we first check the recorded
3998*b071742bSsg70180 	 * state of the channel. If this is UP then we know that
3999*b071742bSsg70180 	 * the channel moving to the UP state has already been dealt
4000*b071742bSsg70180 	 * with and don't need to dispatch a  new task.
4001*b071742bSsg70180 	 *
4002*b071742bSsg70180 	 * The reason for this check is that when we do a ldc_up(),
4003*b071742bSsg70180 	 * depending on the state of the peer, we may or may not get
4004*b071742bSsg70180 	 * a LDC_UP event. As we can't depend on getting a LDC_UP evt
4005*b071742bSsg70180 	 * every time we do ldc_up() we explicitly check the channel
4006*b071742bSsg70180 	 * status to see has it come up (ldc_up() is asynch and will
4007*b071742bSsg70180 	 * complete at some undefined time), and take the appropriate
4008*b071742bSsg70180 	 * action.
4009*b071742bSsg70180 	 *
4010*b071742bSsg70180 	 * The flip side of this is that we may get a LDC_UP event
4011*b071742bSsg70180 	 * when we have already seen that the channel is up and have
4012*b071742bSsg70180 	 * dealt with that.
4013*b071742bSsg70180 	 */
4014*b071742bSsg70180 	mutex_enter(&ldcp->status_lock);
4015*b071742bSsg70180 	if (evt == VSW_CONN_UP) {
4016*b071742bSsg70180 		if ((ldcp->ldc_status == LDC_UP) ||
4017*b071742bSsg70180 					(ldcp->reset_active != 0)) {
4018*b071742bSsg70180 			mutex_exit(&ldcp->status_lock);
4019*b071742bSsg70180 			return;
4020*b071742bSsg70180 		}
4021*b071742bSsg70180 	}
4022*b071742bSsg70180 	mutex_exit(&ldcp->status_lock);
4023*b071742bSsg70180 
4024*b071742bSsg70180 	/*
4025*b071742bSsg70180 	 * The transaction group id allows us to identify and discard
4026*b071742bSsg70180 	 * any tasks which are still pending on the taskq and refer
4027*b071742bSsg70180 	 * to the handshake session we are about to restart or reset.
4028*b071742bSsg70180 	 * These stale messages no longer have any real meaning.
40291ae08745Sheppo 	 */
40301ae08745Sheppo 	mutex_enter(&ldcp->hss_lock);
40311ae08745Sheppo 	ldcp->hss_id++;
40321ae08745Sheppo 	mutex_exit(&ldcp->hss_lock);
40331ae08745Sheppo 
4034*b071742bSsg70180 	ASSERT(vswp->taskq_p != NULL);
4035*b071742bSsg70180 
4036*b071742bSsg70180 	if ((conn = kmem_zalloc(sizeof (vsw_conn_evt_t), KM_NOSLEEP)) == NULL) {
4037*b071742bSsg70180 		cmn_err(CE_WARN, "!vsw%d: unable to allocate memory for"
4038*b071742bSsg70180 			" connection event", vswp->instance);
4039*b071742bSsg70180 		goto err_exit;
4040*b071742bSsg70180 	}
4041*b071742bSsg70180 
4042*b071742bSsg70180 	conn->evt = evt;
4043*b071742bSsg70180 	conn->ldcp = ldcp;
4044*b071742bSsg70180 
4045*b071742bSsg70180 	if (ddi_taskq_dispatch(vswp->taskq_p, vsw_conn_task, conn,
4046*b071742bSsg70180 		DDI_NOSLEEP) != DDI_SUCCESS) {
4047*b071742bSsg70180 		cmn_err(CE_WARN, "!vsw%d: Can't dispatch connection task",
4048*b071742bSsg70180 			vswp->instance);
4049*b071742bSsg70180 
4050*b071742bSsg70180 		kmem_free(conn, sizeof (vsw_conn_evt_t));
4051*b071742bSsg70180 		goto err_exit;
4052*b071742bSsg70180 	}
4053*b071742bSsg70180 
4054*b071742bSsg70180 	D1(vswp, "%s: exit", __func__);
4055*b071742bSsg70180 	return;
4056*b071742bSsg70180 
4057*b071742bSsg70180 err_exit:
4058*b071742bSsg70180 	/*
4059*b071742bSsg70180 	 * Have mostly likely failed due to memory shortage. Clear the flag so
4060*b071742bSsg70180 	 * that future requests will at least be attempted and will hopefully
4061*b071742bSsg70180 	 * succeed.
4062*b071742bSsg70180 	 */
4063*b071742bSsg70180 	if ((evt == VSW_CONN_RESET) || (evt == VSW_CONN_RESTART))
4064*b071742bSsg70180 		ldcp->reset_active = 0;
4065*b071742bSsg70180 }
4066*b071742bSsg70180 
4067*b071742bSsg70180 /*
4068*b071742bSsg70180  * Deal with events relating to a connection. Invoked from a taskq.
4069*b071742bSsg70180  */
4070*b071742bSsg70180 static void
4071*b071742bSsg70180 vsw_conn_task(void *arg)
4072*b071742bSsg70180 {
4073*b071742bSsg70180 	vsw_conn_evt_t	*conn = (vsw_conn_evt_t *)arg;
4074*b071742bSsg70180 	vsw_ldc_t	*ldcp = NULL;
4075*b071742bSsg70180 	vsw_t		*vswp = NULL;
4076*b071742bSsg70180 	uint16_t	evt;
4077*b071742bSsg70180 	ldc_status_t	curr_status;
4078*b071742bSsg70180 
4079*b071742bSsg70180 	ldcp = conn->ldcp;
4080*b071742bSsg70180 	evt = conn->evt;
4081*b071742bSsg70180 	vswp = ldcp->ldc_vswp;
4082*b071742bSsg70180 
4083*b071742bSsg70180 	D1(vswp, "%s: enter", __func__);
4084*b071742bSsg70180 
4085*b071742bSsg70180 	/* can safely free now have copied out data */
4086*b071742bSsg70180 	kmem_free(conn, sizeof (vsw_conn_evt_t));
4087*b071742bSsg70180 
4088*b071742bSsg70180 	mutex_enter(&ldcp->status_lock);
4089*b071742bSsg70180 	if (ldc_status(ldcp->ldc_handle, &curr_status) != 0) {
4090*b071742bSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read status of "
4091*b071742bSsg70180 			"channel %ld", vswp->instance, ldcp->ldc_id);
4092*b071742bSsg70180 		mutex_exit(&ldcp->status_lock);
4093*b071742bSsg70180 		return;
4094*b071742bSsg70180 	}
4095*b071742bSsg70180 
4096*b071742bSsg70180 	/*
4097*b071742bSsg70180 	 * If we wish to restart the handshake on this channel, then if
4098*b071742bSsg70180 	 * the channel is UP we bring it DOWN to flush the underlying
4099*b071742bSsg70180 	 * ldc queue.
4100*b071742bSsg70180 	 */
4101*b071742bSsg70180 	if ((evt == VSW_CONN_RESTART) && (curr_status == LDC_UP))
4102*b071742bSsg70180 		(void) ldc_down(ldcp->ldc_handle);
4103*b071742bSsg70180 
4104*b071742bSsg70180 	/*
4105*b071742bSsg70180 	 * re-init all the associated data structures.
4106*b071742bSsg70180 	 */
4107*b071742bSsg70180 	vsw_ldc_reinit(ldcp);
4108*b071742bSsg70180 
4109*b071742bSsg70180 	/*
4110*b071742bSsg70180 	 * Bring the channel back up (note it does no harm to
4111*b071742bSsg70180 	 * do this even if the channel is already UP, Just
4112*b071742bSsg70180 	 * becomes effectively a no-op).
4113*b071742bSsg70180 	 */
4114*b071742bSsg70180 	(void) ldc_up(ldcp->ldc_handle);
4115*b071742bSsg70180 
4116*b071742bSsg70180 	/*
4117*b071742bSsg70180 	 * Check if channel is now UP. This will only happen if
4118*b071742bSsg70180 	 * peer has also done a ldc_up().
4119*b071742bSsg70180 	 */
4120*b071742bSsg70180 	if (ldc_status(ldcp->ldc_handle, &curr_status) != 0) {
4121*b071742bSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read status of "
4122*b071742bSsg70180 			"channel %ld", vswp->instance, ldcp->ldc_id);
4123*b071742bSsg70180 		mutex_exit(&ldcp->status_lock);
4124*b071742bSsg70180 		return;
4125*b071742bSsg70180 	}
4126*b071742bSsg70180 
4127*b071742bSsg70180 	ldcp->ldc_status = curr_status;
4128*b071742bSsg70180 
4129*b071742bSsg70180 	/* channel UP so restart handshake by sending version info */
4130*b071742bSsg70180 	if (curr_status == LDC_UP) {
41311ae08745Sheppo 		if (ldcp->hcnt++ > vsw_num_handshakes) {
413234683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: exceeded number of permitted"
41331ae08745Sheppo 				" handshake attempts (%d) on channel %ld",
413434683adeSsg70180 				vswp->instance, ldcp->hcnt, ldcp->ldc_id);
41353af08d82Slm66018 			mutex_exit(&ldcp->status_lock);
41363af08d82Slm66018 			return;
41373af08d82Slm66018 		}
4138*b071742bSsg70180 
4139*b071742bSsg70180 		if (ddi_taskq_dispatch(vswp->taskq_p, vsw_send_ver, ldcp,
4140*b071742bSsg70180 			DDI_NOSLEEP) != DDI_SUCCESS) {
4141*b071742bSsg70180 			cmn_err(CE_WARN, "!vsw%d: Can't dispatch version task",
4142*b071742bSsg70180 				vswp->instance);
41433af08d82Slm66018 
41443af08d82Slm66018 			/*
4145*b071742bSsg70180 			 * Don't count as valid restart attempt if couldn't
4146*b071742bSsg70180 			 * send version msg.
41473af08d82Slm66018 			 */
4148*b071742bSsg70180 			if (ldcp->hcnt > 0)
4149*b071742bSsg70180 				ldcp->hcnt--;
4150*b071742bSsg70180 		}
41513af08d82Slm66018 	}
41523af08d82Slm66018 
41533af08d82Slm66018 	/*
4154*b071742bSsg70180 	 * Mark that the process is complete by clearing the flag.
4155*b071742bSsg70180 	 *
4156*b071742bSsg70180 	 * Note is it possible that the taskq dispatch above may have failed,
4157*b071742bSsg70180 	 * most likely due to memory shortage. We still clear the flag so
4158*b071742bSsg70180 	 * future attempts will at least be attempted and will hopefully
4159*b071742bSsg70180 	 * succeed.
41603af08d82Slm66018 	 */
4161*b071742bSsg70180 	if ((evt == VSW_CONN_RESET) || (evt == VSW_CONN_RESTART))
4162445b4c2eSsb155480 		ldcp->reset_active = 0;
4163*b071742bSsg70180 
4164*b071742bSsg70180 	mutex_exit(&ldcp->status_lock);
41653af08d82Slm66018 
41663af08d82Slm66018 	D1(vswp, "%s: exit", __func__);
41673af08d82Slm66018 }
41683af08d82Slm66018 
41693af08d82Slm66018 /*
41701ae08745Sheppo  * returns 0 if legal for event signified by flag to have
41711ae08745Sheppo  * occured at the time it did. Otherwise returns 1.
41721ae08745Sheppo  */
41731ae08745Sheppo int
41741ae08745Sheppo vsw_check_flag(vsw_ldc_t *ldcp, int dir, uint64_t flag)
41751ae08745Sheppo {
41761ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
41771ae08745Sheppo 	uint64_t	state;
41781ae08745Sheppo 	uint64_t	phase;
41791ae08745Sheppo 
41801ae08745Sheppo 	if (dir == INBOUND)
41811ae08745Sheppo 		state = ldcp->lane_in.lstate;
41821ae08745Sheppo 	else
41831ae08745Sheppo 		state = ldcp->lane_out.lstate;
41841ae08745Sheppo 
41851ae08745Sheppo 	phase = ldcp->hphase;
41861ae08745Sheppo 
41871ae08745Sheppo 	switch (flag) {
41881ae08745Sheppo 	case VSW_VER_INFO_RECV:
41891ae08745Sheppo 		if (phase > VSW_MILESTONE0) {
41901ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): VER_INFO_RECV"
41911ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
4192*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
41931ae08745Sheppo 			return (1);
41941ae08745Sheppo 		}
41951ae08745Sheppo 		break;
41961ae08745Sheppo 
41971ae08745Sheppo 	case VSW_VER_ACK_RECV:
41981ae08745Sheppo 	case VSW_VER_NACK_RECV:
41991ae08745Sheppo 		if (!(state & VSW_VER_INFO_SENT)) {
42001ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious VER_ACK"
42011ae08745Sheppo 				" or VER_NACK when in state %d\n",
42021ae08745Sheppo 				ldcp->ldc_id, phase);
4203*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
42041ae08745Sheppo 			return (1);
42051ae08745Sheppo 		} else
42061ae08745Sheppo 			state &= ~VSW_VER_INFO_SENT;
42071ae08745Sheppo 		break;
42081ae08745Sheppo 
42091ae08745Sheppo 	case VSW_ATTR_INFO_RECV:
42101ae08745Sheppo 		if ((phase < VSW_MILESTONE1) || (phase >= VSW_MILESTONE2)) {
42111ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): ATTR_INFO_RECV"
42121ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
4213*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
42141ae08745Sheppo 			return (1);
42151ae08745Sheppo 		}
42161ae08745Sheppo 		break;
42171ae08745Sheppo 
42181ae08745Sheppo 	case VSW_ATTR_ACK_RECV:
42191ae08745Sheppo 	case VSW_ATTR_NACK_RECV:
42201ae08745Sheppo 		if (!(state & VSW_ATTR_INFO_SENT)) {
42211ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious ATTR_ACK"
42221ae08745Sheppo 				" or ATTR_NACK when in state %d\n",
42231ae08745Sheppo 				ldcp->ldc_id, phase);
4224*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
42251ae08745Sheppo 			return (1);
42261ae08745Sheppo 		} else
42271ae08745Sheppo 			state &= ~VSW_ATTR_INFO_SENT;
42281ae08745Sheppo 		break;
42291ae08745Sheppo 
42301ae08745Sheppo 	case VSW_DRING_INFO_RECV:
42311ae08745Sheppo 		if (phase < VSW_MILESTONE1) {
42321ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): DRING_INFO_RECV"
42331ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
4234*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
42351ae08745Sheppo 			return (1);
42361ae08745Sheppo 		}
42371ae08745Sheppo 		break;
42381ae08745Sheppo 
42391ae08745Sheppo 	case VSW_DRING_ACK_RECV:
42401ae08745Sheppo 	case VSW_DRING_NACK_RECV:
42411ae08745Sheppo 		if (!(state & VSW_DRING_INFO_SENT)) {
42421ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious DRING_ACK"
42431ae08745Sheppo 				" or DRING_NACK when in state %d\n",
42441ae08745Sheppo 				ldcp->ldc_id, phase);
4245*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
42461ae08745Sheppo 			return (1);
42471ae08745Sheppo 		} else
42481ae08745Sheppo 			state &= ~VSW_DRING_INFO_SENT;
42491ae08745Sheppo 		break;
42501ae08745Sheppo 
42511ae08745Sheppo 	case VSW_RDX_INFO_RECV:
42521ae08745Sheppo 		if (phase < VSW_MILESTONE3) {
42531ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): RDX_INFO_RECV"
42541ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
4255*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
42561ae08745Sheppo 			return (1);
42571ae08745Sheppo 		}
42581ae08745Sheppo 		break;
42591ae08745Sheppo 
42601ae08745Sheppo 	case VSW_RDX_ACK_RECV:
42611ae08745Sheppo 	case VSW_RDX_NACK_RECV:
42621ae08745Sheppo 		if (!(state & VSW_RDX_INFO_SENT)) {
42631ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious RDX_ACK"
42641ae08745Sheppo 				" or RDX_NACK when in state %d\n",
42651ae08745Sheppo 				ldcp->ldc_id, phase);
4266*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
42671ae08745Sheppo 			return (1);
42681ae08745Sheppo 		} else
42691ae08745Sheppo 			state &= ~VSW_RDX_INFO_SENT;
42701ae08745Sheppo 		break;
42711ae08745Sheppo 
42721ae08745Sheppo 	case VSW_MCST_INFO_RECV:
42731ae08745Sheppo 		if (phase < VSW_MILESTONE3) {
42741ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): VSW_MCST_INFO_RECV"
42751ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
4276*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
42771ae08745Sheppo 			return (1);
42781ae08745Sheppo 		}
42791ae08745Sheppo 		break;
42801ae08745Sheppo 
42811ae08745Sheppo 	default:
42821ae08745Sheppo 		DERR(vswp, "vsw_check_flag (%lld): unknown flag (%llx)",
42831ae08745Sheppo 				ldcp->ldc_id, flag);
42841ae08745Sheppo 		return (1);
42851ae08745Sheppo 	}
42861ae08745Sheppo 
42871ae08745Sheppo 	if (dir == INBOUND)
42881ae08745Sheppo 		ldcp->lane_in.lstate = state;
42891ae08745Sheppo 	else
42901ae08745Sheppo 		ldcp->lane_out.lstate = state;
42911ae08745Sheppo 
42921ae08745Sheppo 	D1(vswp, "vsw_check_flag (chan %lld): exit", ldcp->ldc_id);
42931ae08745Sheppo 
42941ae08745Sheppo 	return (0);
42951ae08745Sheppo }
42961ae08745Sheppo 
42971ae08745Sheppo void
42981ae08745Sheppo vsw_next_milestone(vsw_ldc_t *ldcp)
42991ae08745Sheppo {
43001ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
43011ae08745Sheppo 
43021ae08745Sheppo 	D1(vswp, "%s (chan %lld): enter (phase %ld)", __func__,
43031ae08745Sheppo 		ldcp->ldc_id, ldcp->hphase);
43041ae08745Sheppo 
43051ae08745Sheppo 	DUMP_FLAGS(ldcp->lane_in.lstate);
43061ae08745Sheppo 	DUMP_FLAGS(ldcp->lane_out.lstate);
43071ae08745Sheppo 
43081ae08745Sheppo 	switch (ldcp->hphase) {
43091ae08745Sheppo 
43101ae08745Sheppo 	case VSW_MILESTONE0:
43111ae08745Sheppo 		/*
43121ae08745Sheppo 		 * If we haven't started to handshake with our peer,
43131ae08745Sheppo 		 * start to do so now.
43141ae08745Sheppo 		 */
43151ae08745Sheppo 		if (ldcp->lane_out.lstate == 0) {
43161ae08745Sheppo 			D2(vswp, "%s: (chan %lld) starting handshake "
43171ae08745Sheppo 				"with peer", __func__, ldcp->ldc_id);
4318*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_UP);
43191ae08745Sheppo 		}
43201ae08745Sheppo 
43211ae08745Sheppo 		/*
43221ae08745Sheppo 		 * Only way to pass this milestone is to have successfully
43231ae08745Sheppo 		 * negotiated version info.
43241ae08745Sheppo 		 */
43251ae08745Sheppo 		if ((ldcp->lane_in.lstate & VSW_VER_ACK_SENT) &&
43261ae08745Sheppo 			(ldcp->lane_out.lstate & VSW_VER_ACK_RECV)) {
43271ae08745Sheppo 
43281ae08745Sheppo 			D2(vswp, "%s: (chan %lld) leaving milestone 0",
43291ae08745Sheppo 				__func__, ldcp->ldc_id);
43301ae08745Sheppo 
43311ae08745Sheppo 			/*
43321ae08745Sheppo 			 * Next milestone is passed when attribute
43331ae08745Sheppo 			 * information has been successfully exchanged.
43341ae08745Sheppo 			 */
43351ae08745Sheppo 			ldcp->hphase = VSW_MILESTONE1;
43361ae08745Sheppo 			vsw_send_attr(ldcp);
43371ae08745Sheppo 
43381ae08745Sheppo 		}
43391ae08745Sheppo 		break;
43401ae08745Sheppo 
43411ae08745Sheppo 	case VSW_MILESTONE1:
43421ae08745Sheppo 		/*
43431ae08745Sheppo 		 * Only way to pass this milestone is to have successfully
43441ae08745Sheppo 		 * negotiated attribute information.
43451ae08745Sheppo 		 */
43461ae08745Sheppo 		if (ldcp->lane_in.lstate & VSW_ATTR_ACK_SENT) {
43471ae08745Sheppo 
43481ae08745Sheppo 			ldcp->hphase = VSW_MILESTONE2;
43491ae08745Sheppo 
43501ae08745Sheppo 			/*
43511ae08745Sheppo 			 * If the peer device has said it wishes to
43521ae08745Sheppo 			 * use descriptor rings then we send it our ring
43531ae08745Sheppo 			 * info, otherwise we just set up a private ring
43541ae08745Sheppo 			 * which we use an internal buffer
43551ae08745Sheppo 			 */
43561ae08745Sheppo 			if (ldcp->lane_in.xfer_mode == VIO_DRING_MODE)
43571ae08745Sheppo 				vsw_send_dring_info(ldcp);
43581ae08745Sheppo 		}
43591ae08745Sheppo 		break;
43601ae08745Sheppo 
43611ae08745Sheppo 	case VSW_MILESTONE2:
43621ae08745Sheppo 		/*
43631ae08745Sheppo 		 * If peer has indicated in its attribute message that
43641ae08745Sheppo 		 * it wishes to use descriptor rings then the only way
43651ae08745Sheppo 		 * to pass this milestone is for us to have received
43661ae08745Sheppo 		 * valid dring info.
43671ae08745Sheppo 		 *
43681ae08745Sheppo 		 * If peer is not using descriptor rings then just fall
43691ae08745Sheppo 		 * through.
43701ae08745Sheppo 		 */
43711ae08745Sheppo 		if ((ldcp->lane_in.xfer_mode == VIO_DRING_MODE) &&
43721ae08745Sheppo 			(!(ldcp->lane_in.lstate & VSW_DRING_ACK_SENT)))
43731ae08745Sheppo 			break;
43741ae08745Sheppo 
43751ae08745Sheppo 		D2(vswp, "%s: (chan %lld) leaving milestone 2",
43761ae08745Sheppo 				__func__, ldcp->ldc_id);
43771ae08745Sheppo 
43781ae08745Sheppo 		ldcp->hphase = VSW_MILESTONE3;
43791ae08745Sheppo 		vsw_send_rdx(ldcp);
43801ae08745Sheppo 		break;
43811ae08745Sheppo 
43821ae08745Sheppo 	case VSW_MILESTONE3:
43831ae08745Sheppo 		/*
43841ae08745Sheppo 		 * Pass this milestone when all paramaters have been
43851ae08745Sheppo 		 * successfully exchanged and RDX sent in both directions.
43861ae08745Sheppo 		 *
43871ae08745Sheppo 		 * Mark outbound lane as available to transmit data.
43881ae08745Sheppo 		 */
4389*b071742bSsg70180 		if ((ldcp->lane_out.lstate & VSW_RDX_ACK_SENT) &&
4390*b071742bSsg70180 			(ldcp->lane_in.lstate & VSW_RDX_ACK_RECV)) {
43911ae08745Sheppo 
43921ae08745Sheppo 			D2(vswp, "%s: (chan %lld) leaving milestone 3",
43931ae08745Sheppo 				__func__, ldcp->ldc_id);
43943af08d82Slm66018 			D2(vswp, "%s: ** handshake complete (0x%llx : "
43953af08d82Slm66018 				"0x%llx) **", __func__, ldcp->lane_in.lstate,
43963af08d82Slm66018 				ldcp->lane_out.lstate);
43971ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_LANE_ACTIVE;
43981ae08745Sheppo 			ldcp->hphase = VSW_MILESTONE4;
43991ae08745Sheppo 			ldcp->hcnt = 0;
44001ae08745Sheppo 			DISPLAY_STATE();
44013af08d82Slm66018 		} else {
44023af08d82Slm66018 			D2(vswp, "%s: still in milestone 3 (0x%llx :"
44033af08d82Slm66018 				" 0x%llx", __func__, ldcp->lane_in.lstate,
44043af08d82Slm66018 				ldcp->lane_out.lstate);
44051ae08745Sheppo 		}
44061ae08745Sheppo 		break;
44071ae08745Sheppo 
44081ae08745Sheppo 	case VSW_MILESTONE4:
44091ae08745Sheppo 		D2(vswp, "%s: (chan %lld) in milestone 4", __func__,
44101ae08745Sheppo 							ldcp->ldc_id);
44111ae08745Sheppo 		break;
44121ae08745Sheppo 
44131ae08745Sheppo 	default:
44141ae08745Sheppo 		DERR(vswp, "%s: (chan %lld) Unknown Phase %x", __func__,
44151ae08745Sheppo 			ldcp->ldc_id, ldcp->hphase);
44161ae08745Sheppo 	}
44171ae08745Sheppo 
44181ae08745Sheppo 	D1(vswp, "%s (chan %lld): exit (phase %ld)", __func__, ldcp->ldc_id,
44191ae08745Sheppo 		ldcp->hphase);
44201ae08745Sheppo }
44211ae08745Sheppo 
44221ae08745Sheppo /*
44231ae08745Sheppo  * Check if major version is supported.
44241ae08745Sheppo  *
44251ae08745Sheppo  * Returns 0 if finds supported major number, and if necessary
44261ae08745Sheppo  * adjusts the minor field.
44271ae08745Sheppo  *
44281ae08745Sheppo  * Returns 1 if can't match major number exactly. Sets mjor/minor
44291ae08745Sheppo  * to next lowest support values, or to zero if no other values possible.
44301ae08745Sheppo  */
44311ae08745Sheppo static int
44321ae08745Sheppo vsw_supported_version(vio_ver_msg_t *vp)
44331ae08745Sheppo {
44341ae08745Sheppo 	int	i;
44351ae08745Sheppo 
44361ae08745Sheppo 	D1(NULL, "vsw_supported_version: enter");
44371ae08745Sheppo 
44381ae08745Sheppo 	for (i = 0; i < VSW_NUM_VER; i++) {
44391ae08745Sheppo 		if (vsw_versions[i].ver_major == vp->ver_major) {
44401ae08745Sheppo 			/*
44411ae08745Sheppo 			 * Matching or lower major version found. Update
44421ae08745Sheppo 			 * minor number if necessary.
44431ae08745Sheppo 			 */
44441ae08745Sheppo 			if (vp->ver_minor > vsw_versions[i].ver_minor) {
44451ae08745Sheppo 				D2(NULL, "%s: adjusting minor value"
44461ae08745Sheppo 					" from %d to %d", __func__,
44471ae08745Sheppo 					vp->ver_minor,
44481ae08745Sheppo 					vsw_versions[i].ver_minor);
44491ae08745Sheppo 				vp->ver_minor = vsw_versions[i].ver_minor;
44501ae08745Sheppo 			}
44511ae08745Sheppo 
44521ae08745Sheppo 			return (0);
44531ae08745Sheppo 		}
44541ae08745Sheppo 
44551ae08745Sheppo 		if (vsw_versions[i].ver_major < vp->ver_major) {
44561ae08745Sheppo 			if (vp->ver_minor > vsw_versions[i].ver_minor) {
44571ae08745Sheppo 				D2(NULL, "%s: adjusting minor value"
44581ae08745Sheppo 					" from %d to %d", __func__,
44591ae08745Sheppo 					vp->ver_minor,
44601ae08745Sheppo 					vsw_versions[i].ver_minor);
44611ae08745Sheppo 				vp->ver_minor = vsw_versions[i].ver_minor;
44621ae08745Sheppo 			}
44631ae08745Sheppo 			return (1);
44641ae08745Sheppo 		}
44651ae08745Sheppo 	}
44661ae08745Sheppo 
44671ae08745Sheppo 	/* No match was possible, zero out fields */
44681ae08745Sheppo 	vp->ver_major = 0;
44691ae08745Sheppo 	vp->ver_minor = 0;
44701ae08745Sheppo 
44711ae08745Sheppo 	D1(NULL, "vsw_supported_version: exit");
44721ae08745Sheppo 
44731ae08745Sheppo 	return (1);
44741ae08745Sheppo }
44751ae08745Sheppo 
44761ae08745Sheppo /*
44771ae08745Sheppo  * Main routine for processing messages received over LDC.
44781ae08745Sheppo  */
44791ae08745Sheppo static void
44801ae08745Sheppo vsw_process_pkt(void *arg)
44811ae08745Sheppo {
44821ae08745Sheppo 	vsw_ldc_t	*ldcp = (vsw_ldc_t  *)arg;
44831ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
44841ae08745Sheppo 	size_t		msglen;
44851ae08745Sheppo 	vio_msg_tag_t	tag;
44861ae08745Sheppo 	def_msg_t	dmsg;
44871ae08745Sheppo 	int 		rv = 0;
44881ae08745Sheppo 
44893af08d82Slm66018 
44901ae08745Sheppo 	D1(vswp, "%s enter: ldcid (%lld)\n", __func__, ldcp->ldc_id);
44911ae08745Sheppo 
44921ae08745Sheppo 	/*
44931ae08745Sheppo 	 * If channel is up read messages until channel is empty.
44941ae08745Sheppo 	 */
44951ae08745Sheppo 	do {
44961ae08745Sheppo 		msglen = sizeof (dmsg);
44971ae08745Sheppo 		rv = ldc_read(ldcp->ldc_handle, (caddr_t)&dmsg, &msglen);
44981ae08745Sheppo 
44991ae08745Sheppo 		if (rv != 0) {
45001ae08745Sheppo 			DERR(vswp, "%s :ldc_read err id(%lld) rv(%d) "
45011ae08745Sheppo 				"len(%d)\n", __func__, ldcp->ldc_id,
45021ae08745Sheppo 							rv, msglen);
45033af08d82Slm66018 		}
45043af08d82Slm66018 
45053af08d82Slm66018 		/* channel has been reset */
45063af08d82Slm66018 		if (rv == ECONNRESET) {
4507*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
45081ae08745Sheppo 			break;
45091ae08745Sheppo 		}
45101ae08745Sheppo 
45111ae08745Sheppo 		if (msglen == 0) {
45121ae08745Sheppo 			D2(vswp, "%s: ldc_read id(%lld) NODATA", __func__,
45131ae08745Sheppo 			ldcp->ldc_id);
45141ae08745Sheppo 			break;
45151ae08745Sheppo 		}
45161ae08745Sheppo 
45171ae08745Sheppo 		D2(vswp, "%s: ldc_read id(%lld): msglen(%d)", __func__,
45181ae08745Sheppo 		    ldcp->ldc_id, msglen);
45191ae08745Sheppo 
45201ae08745Sheppo 		/*
45211ae08745Sheppo 		 * Figure out what sort of packet we have gotten by
45221ae08745Sheppo 		 * examining the msg tag, and then switch it appropriately.
45231ae08745Sheppo 		 */
45241ae08745Sheppo 		bcopy(&dmsg, &tag, sizeof (vio_msg_tag_t));
45251ae08745Sheppo 
45261ae08745Sheppo 		switch (tag.vio_msgtype) {
45271ae08745Sheppo 		case VIO_TYPE_CTRL:
45281ae08745Sheppo 			vsw_dispatch_ctrl_task(ldcp, &dmsg, tag);
45291ae08745Sheppo 			break;
45301ae08745Sheppo 		case VIO_TYPE_DATA:
45311ae08745Sheppo 			vsw_process_data_pkt(ldcp, &dmsg, tag);
45321ae08745Sheppo 			break;
45331ae08745Sheppo 		case VIO_TYPE_ERR:
45341ae08745Sheppo 			vsw_process_err_pkt(ldcp, &dmsg, tag);
45351ae08745Sheppo 			break;
45361ae08745Sheppo 		default:
45371ae08745Sheppo 			DERR(vswp, "%s: Unknown tag(%lx) ", __func__,
45381ae08745Sheppo 				"id(%lx)\n", tag.vio_msgtype, ldcp->ldc_id);
45391ae08745Sheppo 			break;
45401ae08745Sheppo 		}
45411ae08745Sheppo 	} while (msglen);
45421ae08745Sheppo 
45431ae08745Sheppo 	D1(vswp, "%s exit: ldcid (%lld)\n", __func__, ldcp->ldc_id);
45441ae08745Sheppo }
45451ae08745Sheppo 
45461ae08745Sheppo /*
45471ae08745Sheppo  * Dispatch a task to process a VIO control message.
45481ae08745Sheppo  */
45491ae08745Sheppo static void
45501ae08745Sheppo vsw_dispatch_ctrl_task(vsw_ldc_t *ldcp, void *cpkt, vio_msg_tag_t tag)
45511ae08745Sheppo {
45521ae08745Sheppo 	vsw_ctrl_task_t		*ctaskp = NULL;
45531ae08745Sheppo 	vsw_port_t		*port = ldcp->ldc_port;
45541ae08745Sheppo 	vsw_t			*vswp = port->p_vswp;
45551ae08745Sheppo 
45561ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
45571ae08745Sheppo 
45581ae08745Sheppo 	/*
45591ae08745Sheppo 	 * We need to handle RDX ACK messages in-band as once they
45601ae08745Sheppo 	 * are exchanged it is possible that we will get an
45611ae08745Sheppo 	 * immediate (legitimate) data packet.
45621ae08745Sheppo 	 */
45631ae08745Sheppo 	if ((tag.vio_subtype_env == VIO_RDX) &&
45641ae08745Sheppo 		(tag.vio_subtype == VIO_SUBTYPE_ACK)) {
45653af08d82Slm66018 
4566*b071742bSsg70180 		if (vsw_check_flag(ldcp, INBOUND, VSW_RDX_ACK_RECV))
45671ae08745Sheppo 			return;
45681ae08745Sheppo 
4569*b071742bSsg70180 		ldcp->lane_in.lstate |= VSW_RDX_ACK_RECV;
45703af08d82Slm66018 		D2(vswp, "%s (%ld) handling RDX_ACK in place "
45713af08d82Slm66018 			"(ostate 0x%llx : hphase %d)", __func__,
4572*b071742bSsg70180 			ldcp->ldc_id, ldcp->lane_in.lstate, ldcp->hphase);
45731ae08745Sheppo 		vsw_next_milestone(ldcp);
45741ae08745Sheppo 		return;
45751ae08745Sheppo 	}
45761ae08745Sheppo 
45771ae08745Sheppo 	ctaskp = kmem_alloc(sizeof (vsw_ctrl_task_t), KM_NOSLEEP);
45781ae08745Sheppo 
45791ae08745Sheppo 	if (ctaskp == NULL) {
45801ae08745Sheppo 		DERR(vswp, "%s: unable to alloc space for ctrl"
45811ae08745Sheppo 			" msg", __func__);
4582*b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
45831ae08745Sheppo 		return;
45841ae08745Sheppo 	}
45851ae08745Sheppo 
45861ae08745Sheppo 	ctaskp->ldcp = ldcp;
45871ae08745Sheppo 	bcopy((def_msg_t *)cpkt, &ctaskp->pktp, sizeof (def_msg_t));
45881ae08745Sheppo 	mutex_enter(&ldcp->hss_lock);
45891ae08745Sheppo 	ctaskp->hss_id = ldcp->hss_id;
45901ae08745Sheppo 	mutex_exit(&ldcp->hss_lock);
45911ae08745Sheppo 
45921ae08745Sheppo 	/*
45931ae08745Sheppo 	 * Dispatch task to processing taskq if port is not in
45941ae08745Sheppo 	 * the process of being detached.
45951ae08745Sheppo 	 */
45961ae08745Sheppo 	mutex_enter(&port->state_lock);
45971ae08745Sheppo 	if (port->state == VSW_PORT_INIT) {
45981ae08745Sheppo 		if ((vswp->taskq_p == NULL) ||
45991ae08745Sheppo 			(ddi_taskq_dispatch(vswp->taskq_p,
46001ae08745Sheppo 			vsw_process_ctrl_pkt, ctaskp, DDI_NOSLEEP)
46011ae08745Sheppo 							!= DDI_SUCCESS)) {
46021ae08745Sheppo 			DERR(vswp, "%s: unable to dispatch task to taskq",
46031ae08745Sheppo 				__func__);
46041ae08745Sheppo 			kmem_free(ctaskp, sizeof (vsw_ctrl_task_t));
46051ae08745Sheppo 			mutex_exit(&port->state_lock);
4606*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
46071ae08745Sheppo 			return;
46081ae08745Sheppo 		}
46091ae08745Sheppo 	} else {
46101ae08745Sheppo 		DWARN(vswp, "%s: port %d detaching, not dispatching "
46111ae08745Sheppo 			"task", __func__, port->p_instance);
46121ae08745Sheppo 	}
46131ae08745Sheppo 
46141ae08745Sheppo 	mutex_exit(&port->state_lock);
46151ae08745Sheppo 
46161ae08745Sheppo 	D2(vswp, "%s: dispatched task to taskq for chan %d", __func__,
46171ae08745Sheppo 			ldcp->ldc_id);
46181ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
46191ae08745Sheppo }
46201ae08745Sheppo 
46211ae08745Sheppo /*
46221ae08745Sheppo  * Process a VIO ctrl message. Invoked from taskq.
46231ae08745Sheppo  */
46241ae08745Sheppo static void
46251ae08745Sheppo vsw_process_ctrl_pkt(void *arg)
46261ae08745Sheppo {
46271ae08745Sheppo 	vsw_ctrl_task_t	*ctaskp = (vsw_ctrl_task_t *)arg;
46281ae08745Sheppo 	vsw_ldc_t	*ldcp = ctaskp->ldcp;
46291ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
46301ae08745Sheppo 	vio_msg_tag_t	tag;
46311ae08745Sheppo 	uint16_t	env;
46321ae08745Sheppo 
46331ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
46341ae08745Sheppo 
46351ae08745Sheppo 	bcopy(&ctaskp->pktp, &tag, sizeof (vio_msg_tag_t));
46361ae08745Sheppo 	env = tag.vio_subtype_env;
46371ae08745Sheppo 
46381ae08745Sheppo 	/* stale pkt check */
46391ae08745Sheppo 	mutex_enter(&ldcp->hss_lock);
46401ae08745Sheppo 	if (ctaskp->hss_id < ldcp->hss_id) {
46411ae08745Sheppo 		DWARN(vswp, "%s: discarding stale packet belonging to"
46421ae08745Sheppo 			" earlier (%ld) handshake session", __func__,
46431ae08745Sheppo 			ctaskp->hss_id);
46441ae08745Sheppo 		mutex_exit(&ldcp->hss_lock);
46451ae08745Sheppo 		return;
46461ae08745Sheppo 	}
46471ae08745Sheppo 	mutex_exit(&ldcp->hss_lock);
46481ae08745Sheppo 
46491ae08745Sheppo 	/* session id check */
46501ae08745Sheppo 	if (ldcp->session_status & VSW_PEER_SESSION) {
46511ae08745Sheppo 		if (ldcp->peer_session != tag.vio_sid) {
46521ae08745Sheppo 			DERR(vswp, "%s (chan %d): invalid session id (%llx)",
46531ae08745Sheppo 				__func__, ldcp->ldc_id, tag.vio_sid);
46541ae08745Sheppo 			kmem_free(ctaskp, sizeof (vsw_ctrl_task_t));
4655*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
46561ae08745Sheppo 			return;
46571ae08745Sheppo 		}
46581ae08745Sheppo 	}
46591ae08745Sheppo 
46601ae08745Sheppo 	/*
46611ae08745Sheppo 	 * Switch on vio_subtype envelope, then let lower routines
46621ae08745Sheppo 	 * decide if its an INFO, ACK or NACK packet.
46631ae08745Sheppo 	 */
46641ae08745Sheppo 	switch (env) {
46651ae08745Sheppo 	case VIO_VER_INFO:
46661ae08745Sheppo 		vsw_process_ctrl_ver_pkt(ldcp, &ctaskp->pktp);
46671ae08745Sheppo 		break;
46681ae08745Sheppo 	case VIO_DRING_REG:
46691ae08745Sheppo 		vsw_process_ctrl_dring_reg_pkt(ldcp, &ctaskp->pktp);
46701ae08745Sheppo 		break;
46711ae08745Sheppo 	case VIO_DRING_UNREG:
46721ae08745Sheppo 		vsw_process_ctrl_dring_unreg_pkt(ldcp, &ctaskp->pktp);
46731ae08745Sheppo 		break;
46741ae08745Sheppo 	case VIO_ATTR_INFO:
46751ae08745Sheppo 		vsw_process_ctrl_attr_pkt(ldcp, &ctaskp->pktp);
46761ae08745Sheppo 		break;
46771ae08745Sheppo 	case VNET_MCAST_INFO:
46781ae08745Sheppo 		vsw_process_ctrl_mcst_pkt(ldcp, &ctaskp->pktp);
46791ae08745Sheppo 		break;
46801ae08745Sheppo 	case VIO_RDX:
46811ae08745Sheppo 		vsw_process_ctrl_rdx_pkt(ldcp, &ctaskp->pktp);
46821ae08745Sheppo 		break;
46831ae08745Sheppo 	default:
46841ae08745Sheppo 		DERR(vswp, "%s : unknown vio_subtype_env (%x)\n",
46851ae08745Sheppo 							__func__, env);
46861ae08745Sheppo 	}
46871ae08745Sheppo 
46881ae08745Sheppo 	kmem_free(ctaskp, sizeof (vsw_ctrl_task_t));
46891ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
46901ae08745Sheppo }
46911ae08745Sheppo 
46921ae08745Sheppo /*
46931ae08745Sheppo  * Version negotiation. We can end up here either because our peer
46941ae08745Sheppo  * has responded to a handshake message we have sent it, or our peer
46951ae08745Sheppo  * has initiated a handshake with us. If its the former then can only
46961ae08745Sheppo  * be ACK or NACK, if its the later can only be INFO.
46971ae08745Sheppo  *
46981ae08745Sheppo  * If its an ACK we move to the next stage of the handshake, namely
46991ae08745Sheppo  * attribute exchange. If its a NACK we see if we can specify another
47001ae08745Sheppo  * version, if we can't we stop.
47011ae08745Sheppo  *
47021ae08745Sheppo  * If it is an INFO we reset all params associated with communication
47031ae08745Sheppo  * in that direction over this channel (remember connection is
47041ae08745Sheppo  * essentially 2 independent simplex channels).
47051ae08745Sheppo  */
47061ae08745Sheppo void
47071ae08745Sheppo vsw_process_ctrl_ver_pkt(vsw_ldc_t *ldcp, void *pkt)
47081ae08745Sheppo {
47091ae08745Sheppo 	vio_ver_msg_t	*ver_pkt;
47101ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
47111ae08745Sheppo 
47121ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
47131ae08745Sheppo 
47141ae08745Sheppo 	/*
47151ae08745Sheppo 	 * We know this is a ctrl/version packet so
47161ae08745Sheppo 	 * cast it into the correct structure.
47171ae08745Sheppo 	 */
47181ae08745Sheppo 	ver_pkt = (vio_ver_msg_t *)pkt;
47191ae08745Sheppo 
47201ae08745Sheppo 	switch (ver_pkt->tag.vio_subtype) {
47211ae08745Sheppo 	case VIO_SUBTYPE_INFO:
47221ae08745Sheppo 		D2(vswp, "vsw_process_ctrl_ver_pkt: VIO_SUBTYPE_INFO\n");
47231ae08745Sheppo 
47241ae08745Sheppo 		/*
47251ae08745Sheppo 		 * Record the session id, which we will use from now
47261ae08745Sheppo 		 * until we see another VER_INFO msg. Even then the
47271ae08745Sheppo 		 * session id in most cases will be unchanged, execpt
47281ae08745Sheppo 		 * if channel was reset.
47291ae08745Sheppo 		 */
47301ae08745Sheppo 		if ((ldcp->session_status & VSW_PEER_SESSION) &&
47311ae08745Sheppo 			(ldcp->peer_session != ver_pkt->tag.vio_sid)) {
47321ae08745Sheppo 			DERR(vswp, "%s: updating session id for chan %lld "
47331ae08745Sheppo 				"from %llx to %llx", __func__, ldcp->ldc_id,
47341ae08745Sheppo 				ldcp->peer_session, ver_pkt->tag.vio_sid);
47351ae08745Sheppo 		}
47361ae08745Sheppo 
47371ae08745Sheppo 		ldcp->peer_session = ver_pkt->tag.vio_sid;
47381ae08745Sheppo 		ldcp->session_status |= VSW_PEER_SESSION;
47391ae08745Sheppo 
47401ae08745Sheppo 		/* Legal message at this time ? */
47411ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_VER_INFO_RECV))
47421ae08745Sheppo 			return;
47431ae08745Sheppo 
47441ae08745Sheppo 		/*
47451ae08745Sheppo 		 * First check the device class. Currently only expect
47461ae08745Sheppo 		 * to be talking to a network device. In the future may
47471ae08745Sheppo 		 * also talk to another switch.
47481ae08745Sheppo 		 */
47491ae08745Sheppo 		if (ver_pkt->dev_class != VDEV_NETWORK) {
47501ae08745Sheppo 			DERR(vswp, "%s: illegal device class %d", __func__,
47511ae08745Sheppo 				ver_pkt->dev_class);
47521ae08745Sheppo 
47531ae08745Sheppo 			ver_pkt->tag.vio_sid = ldcp->local_session;
47541ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
47551ae08745Sheppo 
47561ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)ver_pkt);
47571ae08745Sheppo 
4758*b071742bSsg70180 			(void) vsw_send_msg(ldcp, (void *)ver_pkt,
4759*b071742bSsg70180 					sizeof (vio_ver_msg_t), B_TRUE);
47601ae08745Sheppo 
47611ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_VER_NACK_SENT;
47621ae08745Sheppo 			vsw_next_milestone(ldcp);
47631ae08745Sheppo 			return;
47641ae08745Sheppo 		} else {
47651ae08745Sheppo 			ldcp->dev_class = ver_pkt->dev_class;
47661ae08745Sheppo 		}
47671ae08745Sheppo 
47681ae08745Sheppo 		/*
47691ae08745Sheppo 		 * Now check the version.
47701ae08745Sheppo 		 */
47711ae08745Sheppo 		if (vsw_supported_version(ver_pkt) == 0) {
47721ae08745Sheppo 			/*
47731ae08745Sheppo 			 * Support this major version and possibly
47741ae08745Sheppo 			 * adjusted minor version.
47751ae08745Sheppo 			 */
47761ae08745Sheppo 
47771ae08745Sheppo 			D2(vswp, "%s: accepted ver %d:%d", __func__,
47781ae08745Sheppo 				ver_pkt->ver_major, ver_pkt->ver_minor);
47791ae08745Sheppo 
47801ae08745Sheppo 			/* Store accepted values */
47811ae08745Sheppo 			ldcp->lane_in.ver_major = ver_pkt->ver_major;
47821ae08745Sheppo 			ldcp->lane_in.ver_minor = ver_pkt->ver_minor;
47831ae08745Sheppo 
47841ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
47851ae08745Sheppo 
47861ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_VER_ACK_SENT;
47871ae08745Sheppo 		} else {
47881ae08745Sheppo 			/*
47891ae08745Sheppo 			 * NACK back with the next lower major/minor
47901ae08745Sheppo 			 * pairing we support (if don't suuport any more
47911ae08745Sheppo 			 * versions then they will be set to zero.
47921ae08745Sheppo 			 */
47931ae08745Sheppo 
47941ae08745Sheppo 			D2(vswp, "%s: replying with ver %d:%d", __func__,
47951ae08745Sheppo 				ver_pkt->ver_major, ver_pkt->ver_minor);
47961ae08745Sheppo 
47971ae08745Sheppo 			/* Store updated values */
47981ae08745Sheppo 			ldcp->lane_in.ver_major = ver_pkt->ver_major;
47991ae08745Sheppo 			ldcp->lane_in.ver_minor = ver_pkt->ver_minor;
48001ae08745Sheppo 
48011ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
48021ae08745Sheppo 
48031ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_VER_NACK_SENT;
48041ae08745Sheppo 		}
48051ae08745Sheppo 
48061ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)ver_pkt);
48071ae08745Sheppo 		ver_pkt->tag.vio_sid = ldcp->local_session;
4808*b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)ver_pkt,
4809*b071742bSsg70180 			sizeof (vio_ver_msg_t), B_TRUE);
48101ae08745Sheppo 
48111ae08745Sheppo 		vsw_next_milestone(ldcp);
48121ae08745Sheppo 		break;
48131ae08745Sheppo 
48141ae08745Sheppo 	case VIO_SUBTYPE_ACK:
48151ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK\n", __func__);
48161ae08745Sheppo 
48171ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_VER_ACK_RECV))
48181ae08745Sheppo 			return;
48191ae08745Sheppo 
48201ae08745Sheppo 		/* Store updated values */
48211ae08745Sheppo 		ldcp->lane_in.ver_major = ver_pkt->ver_major;
48221ae08745Sheppo 		ldcp->lane_in.ver_minor = ver_pkt->ver_minor;
48231ae08745Sheppo 
48241ae08745Sheppo 
48251ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_VER_ACK_RECV;
48261ae08745Sheppo 		vsw_next_milestone(ldcp);
48271ae08745Sheppo 
48281ae08745Sheppo 		break;
48291ae08745Sheppo 
48301ae08745Sheppo 	case VIO_SUBTYPE_NACK:
48311ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK\n", __func__);
48321ae08745Sheppo 
48331ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_VER_NACK_RECV))
48341ae08745Sheppo 			return;
48351ae08745Sheppo 
48361ae08745Sheppo 		/*
48371ae08745Sheppo 		 * If our peer sent us a NACK with the ver fields set to
48381ae08745Sheppo 		 * zero then there is nothing more we can do. Otherwise see
48391ae08745Sheppo 		 * if we support either the version suggested, or a lesser
48401ae08745Sheppo 		 * one.
48411ae08745Sheppo 		 */
48421ae08745Sheppo 		if ((ver_pkt->ver_major == 0) && (ver_pkt->ver_minor == 0)) {
48431ae08745Sheppo 			DERR(vswp, "%s: peer unable to negotiate any "
48441ae08745Sheppo 				"further.", __func__);
48451ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_VER_NACK_RECV;
48461ae08745Sheppo 			vsw_next_milestone(ldcp);
48471ae08745Sheppo 			return;
48481ae08745Sheppo 		}
48491ae08745Sheppo 
48501ae08745Sheppo 		/*
48511ae08745Sheppo 		 * Check to see if we support this major version or
48521ae08745Sheppo 		 * a lower one. If we don't then maj/min will be set
48531ae08745Sheppo 		 * to zero.
48541ae08745Sheppo 		 */
48551ae08745Sheppo 		(void) vsw_supported_version(ver_pkt);
48561ae08745Sheppo 		if ((ver_pkt->ver_major == 0) && (ver_pkt->ver_minor == 0)) {
48571ae08745Sheppo 			/* Nothing more we can do */
48581ae08745Sheppo 			DERR(vswp, "%s: version negotiation failed.\n",
48591ae08745Sheppo 								__func__);
48601ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_VER_NACK_RECV;
48611ae08745Sheppo 			vsw_next_milestone(ldcp);
48621ae08745Sheppo 		} else {
48631ae08745Sheppo 			/* found a supported major version */
48641ae08745Sheppo 			ldcp->lane_out.ver_major = ver_pkt->ver_major;
48651ae08745Sheppo 			ldcp->lane_out.ver_minor = ver_pkt->ver_minor;
48661ae08745Sheppo 
48671ae08745Sheppo 			D2(vswp, "%s: resending with updated values (%x, %x)",
48681ae08745Sheppo 				__func__, ver_pkt->ver_major,
48691ae08745Sheppo 				ver_pkt->ver_minor);
48701ae08745Sheppo 
48711ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_VER_INFO_SENT;
48721ae08745Sheppo 			ver_pkt->tag.vio_sid = ldcp->local_session;
48731ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_INFO;
48741ae08745Sheppo 
48751ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)ver_pkt);
48761ae08745Sheppo 
4877*b071742bSsg70180 			(void) vsw_send_msg(ldcp, (void *)ver_pkt,
4878*b071742bSsg70180 				sizeof (vio_ver_msg_t), B_TRUE);
48791ae08745Sheppo 
48801ae08745Sheppo 			vsw_next_milestone(ldcp);
48811ae08745Sheppo 
48821ae08745Sheppo 		}
48831ae08745Sheppo 		break;
48841ae08745Sheppo 
48851ae08745Sheppo 	default:
48861ae08745Sheppo 		DERR(vswp, "%s: unknown vio_subtype %x\n", __func__,
48871ae08745Sheppo 			ver_pkt->tag.vio_subtype);
48881ae08745Sheppo 	}
48891ae08745Sheppo 
48901ae08745Sheppo 	D1(vswp, "%s(%lld): exit\n", __func__, ldcp->ldc_id);
48911ae08745Sheppo }
48921ae08745Sheppo 
48931ae08745Sheppo /*
48941ae08745Sheppo  * Process an attribute packet. We can end up here either because our peer
48951ae08745Sheppo  * has ACK/NACK'ed back to an earlier ATTR msg we had sent it, or our
48961ae08745Sheppo  * peer has sent us an attribute INFO message
48971ae08745Sheppo  *
48981ae08745Sheppo  * If its an ACK we then move to the next stage of the handshake which
48991ae08745Sheppo  * is to send our descriptor ring info to our peer. If its a NACK then
49001ae08745Sheppo  * there is nothing more we can (currently) do.
49011ae08745Sheppo  *
49021ae08745Sheppo  * If we get a valid/acceptable INFO packet (and we have already negotiated
49031ae08745Sheppo  * a version) we ACK back and set channel state to ATTR_RECV, otherwise we
49041ae08745Sheppo  * NACK back and reset channel state to INACTIV.
49051ae08745Sheppo  *
49061ae08745Sheppo  * FUTURE: in time we will probably negotiate over attributes, but for
49071ae08745Sheppo  * the moment unacceptable attributes are regarded as a fatal error.
49081ae08745Sheppo  *
49091ae08745Sheppo  */
49101ae08745Sheppo void
49111ae08745Sheppo vsw_process_ctrl_attr_pkt(vsw_ldc_t *ldcp, void *pkt)
49121ae08745Sheppo {
49131ae08745Sheppo 	vnet_attr_msg_t		*attr_pkt;
49141ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
49151ae08745Sheppo 	vsw_port_t		*port = ldcp->ldc_port;
49161ae08745Sheppo 	uint64_t		macaddr = 0;
49171ae08745Sheppo 	int			i;
49181ae08745Sheppo 
49191ae08745Sheppo 	D1(vswp, "%s(%lld) enter", __func__, ldcp->ldc_id);
49201ae08745Sheppo 
49211ae08745Sheppo 	/*
49221ae08745Sheppo 	 * We know this is a ctrl/attr packet so
49231ae08745Sheppo 	 * cast it into the correct structure.
49241ae08745Sheppo 	 */
49251ae08745Sheppo 	attr_pkt = (vnet_attr_msg_t *)pkt;
49261ae08745Sheppo 
49271ae08745Sheppo 	switch (attr_pkt->tag.vio_subtype) {
49281ae08745Sheppo 	case VIO_SUBTYPE_INFO:
49291ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
49301ae08745Sheppo 
49311ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_ATTR_INFO_RECV))
49321ae08745Sheppo 			return;
49331ae08745Sheppo 
49341ae08745Sheppo 		/*
49351ae08745Sheppo 		 * If the attributes are unacceptable then we NACK back.
49361ae08745Sheppo 		 */
49371ae08745Sheppo 		if (vsw_check_attr(attr_pkt, ldcp->ldc_port)) {
49381ae08745Sheppo 
49391ae08745Sheppo 			DERR(vswp, "%s (chan %d): invalid attributes",
49401ae08745Sheppo 				__func__, ldcp->ldc_id);
49411ae08745Sheppo 
49421ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
49431ae08745Sheppo 
49441ae08745Sheppo 			attr_pkt->tag.vio_sid = ldcp->local_session;
49451ae08745Sheppo 			attr_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
49461ae08745Sheppo 
49471ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)attr_pkt);
49481ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_ATTR_NACK_SENT;
4949*b071742bSsg70180 			(void) vsw_send_msg(ldcp, (void *)attr_pkt,
4950*b071742bSsg70180 				sizeof (vnet_attr_msg_t), B_TRUE);
49511ae08745Sheppo 
49521ae08745Sheppo 			vsw_next_milestone(ldcp);
49531ae08745Sheppo 			return;
49541ae08745Sheppo 		}
49551ae08745Sheppo 
49561ae08745Sheppo 		/*
49571ae08745Sheppo 		 * Otherwise store attributes for this lane and update
49581ae08745Sheppo 		 * lane state.
49591ae08745Sheppo 		 */
49601ae08745Sheppo 		ldcp->lane_in.mtu = attr_pkt->mtu;
49611ae08745Sheppo 		ldcp->lane_in.addr = attr_pkt->addr;
49621ae08745Sheppo 		ldcp->lane_in.addr_type = attr_pkt->addr_type;
49631ae08745Sheppo 		ldcp->lane_in.xfer_mode = attr_pkt->xfer_mode;
49641ae08745Sheppo 		ldcp->lane_in.ack_freq = attr_pkt->ack_freq;
49651ae08745Sheppo 
49661ae08745Sheppo 		macaddr = ldcp->lane_in.addr;
49671ae08745Sheppo 		for (i = ETHERADDRL - 1; i >= 0; i--) {
49681ae08745Sheppo 			port->p_macaddr.ether_addr_octet[i] = macaddr & 0xFF;
49691ae08745Sheppo 			macaddr >>= 8;
49701ae08745Sheppo 		}
49711ae08745Sheppo 
49721ae08745Sheppo 		/* create the fdb entry for this port/mac address */
49731ae08745Sheppo 		(void) vsw_add_fdb(vswp, port);
49741ae08745Sheppo 
49751ae08745Sheppo 		/* setup device specifc xmit routines */
49761ae08745Sheppo 		mutex_enter(&port->tx_lock);
49771ae08745Sheppo 		if (ldcp->lane_in.xfer_mode == VIO_DRING_MODE) {
49781ae08745Sheppo 			D2(vswp, "%s: mode = VIO_DRING_MODE", __func__);
49791ae08745Sheppo 			port->transmit = vsw_dringsend;
49801ae08745Sheppo 		} else if (ldcp->lane_in.xfer_mode == VIO_DESC_MODE) {
49811ae08745Sheppo 			D2(vswp, "%s: mode = VIO_DESC_MODE", __func__);
49821ae08745Sheppo 			vsw_create_privring(ldcp);
49831ae08745Sheppo 			port->transmit = vsw_descrsend;
49841ae08745Sheppo 		}
49851ae08745Sheppo 		mutex_exit(&port->tx_lock);
49861ae08745Sheppo 
49871ae08745Sheppo 		attr_pkt->tag.vio_sid = ldcp->local_session;
49881ae08745Sheppo 		attr_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
49891ae08745Sheppo 
49901ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)attr_pkt);
49911ae08745Sheppo 
49921ae08745Sheppo 		ldcp->lane_in.lstate |= VSW_ATTR_ACK_SENT;
49931ae08745Sheppo 
4994*b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)attr_pkt,
4995*b071742bSsg70180 				sizeof (vnet_attr_msg_t), B_TRUE);
49961ae08745Sheppo 
49971ae08745Sheppo 		vsw_next_milestone(ldcp);
49981ae08745Sheppo 		break;
49991ae08745Sheppo 
50001ae08745Sheppo 	case VIO_SUBTYPE_ACK:
50011ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
50021ae08745Sheppo 
50031ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_ATTR_ACK_RECV))
50041ae08745Sheppo 			return;
50051ae08745Sheppo 
50061ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_ATTR_ACK_RECV;
50071ae08745Sheppo 		vsw_next_milestone(ldcp);
50081ae08745Sheppo 		break;
50091ae08745Sheppo 
50101ae08745Sheppo 	case VIO_SUBTYPE_NACK:
50111ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
50121ae08745Sheppo 
50131ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_ATTR_NACK_RECV))
50141ae08745Sheppo 			return;
50151ae08745Sheppo 
50161ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_ATTR_NACK_RECV;
50171ae08745Sheppo 		vsw_next_milestone(ldcp);
50181ae08745Sheppo 		break;
50191ae08745Sheppo 
50201ae08745Sheppo 	default:
50211ae08745Sheppo 		DERR(vswp, "%s: unknown vio_subtype %x\n", __func__,
50221ae08745Sheppo 			attr_pkt->tag.vio_subtype);
50231ae08745Sheppo 	}
50241ae08745Sheppo 
50251ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
50261ae08745Sheppo }
50271ae08745Sheppo 
50281ae08745Sheppo /*
50291ae08745Sheppo  * Process a dring info packet. We can end up here either because our peer
50301ae08745Sheppo  * has ACK/NACK'ed back to an earlier DRING msg we had sent it, or our
50311ae08745Sheppo  * peer has sent us a dring INFO message.
50321ae08745Sheppo  *
50331ae08745Sheppo  * If we get a valid/acceptable INFO packet (and we have already negotiated
50341ae08745Sheppo  * a version) we ACK back and update the lane state, otherwise we NACK back.
50351ae08745Sheppo  *
50361ae08745Sheppo  * FUTURE: nothing to stop client from sending us info on multiple dring's
50371ae08745Sheppo  * but for the moment we will just use the first one we are given.
50381ae08745Sheppo  *
50391ae08745Sheppo  */
50401ae08745Sheppo void
50411ae08745Sheppo vsw_process_ctrl_dring_reg_pkt(vsw_ldc_t *ldcp, void *pkt)
50421ae08745Sheppo {
50431ae08745Sheppo 	vio_dring_reg_msg_t	*dring_pkt;
50441ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
50451ae08745Sheppo 	ldc_mem_info_t		minfo;
50461ae08745Sheppo 	dring_info_t		*dp, *dbp;
50471ae08745Sheppo 	int			dring_found = 0;
50481ae08745Sheppo 
50491ae08745Sheppo 	/*
50501ae08745Sheppo 	 * We know this is a ctrl/dring packet so
50511ae08745Sheppo 	 * cast it into the correct structure.
50521ae08745Sheppo 	 */
50531ae08745Sheppo 	dring_pkt = (vio_dring_reg_msg_t *)pkt;
50541ae08745Sheppo 
50551ae08745Sheppo 	D1(vswp, "%s(%lld) enter", __func__, ldcp->ldc_id);
50561ae08745Sheppo 
50571ae08745Sheppo 	switch (dring_pkt->tag.vio_subtype) {
50581ae08745Sheppo 	case VIO_SUBTYPE_INFO:
50591ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
50601ae08745Sheppo 
50611ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_DRING_INFO_RECV))
50621ae08745Sheppo 			return;
50631ae08745Sheppo 
50641ae08745Sheppo 		/*
50651ae08745Sheppo 		 * If the dring params are unacceptable then we NACK back.
50661ae08745Sheppo 		 */
50671ae08745Sheppo 		if (vsw_check_dring_info(dring_pkt)) {
50681ae08745Sheppo 
50691ae08745Sheppo 			DERR(vswp, "%s (%lld): invalid dring info",
50701ae08745Sheppo 				__func__, ldcp->ldc_id);
50711ae08745Sheppo 
50721ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
50731ae08745Sheppo 
50741ae08745Sheppo 			dring_pkt->tag.vio_sid = ldcp->local_session;
50751ae08745Sheppo 			dring_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
50761ae08745Sheppo 
50771ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)dring_pkt);
50781ae08745Sheppo 
50791ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_DRING_NACK_SENT;
50801ae08745Sheppo 
5081*b071742bSsg70180 			(void) vsw_send_msg(ldcp, (void *)dring_pkt,
5082*b071742bSsg70180 				sizeof (vio_dring_reg_msg_t), B_TRUE);
50831ae08745Sheppo 
50841ae08745Sheppo 			vsw_next_milestone(ldcp);
50851ae08745Sheppo 			return;
50861ae08745Sheppo 		}
50871ae08745Sheppo 
50881ae08745Sheppo 		/*
50891ae08745Sheppo 		 * Otherwise, attempt to map in the dring using the
50901ae08745Sheppo 		 * cookie. If that succeeds we send back a unique dring
50911ae08745Sheppo 		 * identifier that the sending side will use in future
50921ae08745Sheppo 		 * to refer to this descriptor ring.
50931ae08745Sheppo 		 */
50941ae08745Sheppo 		dp = kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
50951ae08745Sheppo 
50961ae08745Sheppo 		dp->num_descriptors = dring_pkt->num_descriptors;
50971ae08745Sheppo 		dp->descriptor_size = dring_pkt->descriptor_size;
50981ae08745Sheppo 		dp->options = dring_pkt->options;
50991ae08745Sheppo 		dp->ncookies = dring_pkt->ncookies;
51001ae08745Sheppo 
51011ae08745Sheppo 		/*
51021ae08745Sheppo 		 * Note: should only get one cookie. Enforced in
51031ae08745Sheppo 		 * the ldc layer.
51041ae08745Sheppo 		 */
51051ae08745Sheppo 		bcopy(&dring_pkt->cookie[0], &dp->cookie[0],
51061ae08745Sheppo 			sizeof (ldc_mem_cookie_t));
51071ae08745Sheppo 
51081ae08745Sheppo 		D2(vswp, "%s: num_desc %ld : desc_size %ld", __func__,
51091ae08745Sheppo 			dp->num_descriptors, dp->descriptor_size);
51101ae08745Sheppo 		D2(vswp, "%s: options 0x%lx: ncookies %ld", __func__,
51111ae08745Sheppo 			dp->options, dp->ncookies);
51121ae08745Sheppo 
51131ae08745Sheppo 		if ((ldc_mem_dring_map(ldcp->ldc_handle, &dp->cookie[0],
51141ae08745Sheppo 			dp->ncookies, dp->num_descriptors,
51151ae08745Sheppo 			dp->descriptor_size, LDC_SHADOW_MAP,
51161ae08745Sheppo 			&(dp->handle))) != 0) {
51171ae08745Sheppo 
51181ae08745Sheppo 			DERR(vswp, "%s: dring_map failed\n", __func__);
51191ae08745Sheppo 
51201ae08745Sheppo 			kmem_free(dp, sizeof (dring_info_t));
51211ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
51221ae08745Sheppo 
51231ae08745Sheppo 			dring_pkt->tag.vio_sid = ldcp->local_session;
51241ae08745Sheppo 			dring_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
51251ae08745Sheppo 
51261ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)dring_pkt);
51271ae08745Sheppo 
51281ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_DRING_NACK_SENT;
5129*b071742bSsg70180 			(void) vsw_send_msg(ldcp, (void *)dring_pkt,
5130*b071742bSsg70180 				sizeof (vio_dring_reg_msg_t), B_TRUE);
51311ae08745Sheppo 
51321ae08745Sheppo 			vsw_next_milestone(ldcp);
51331ae08745Sheppo 			return;
51341ae08745Sheppo 		}
51351ae08745Sheppo 
51361ae08745Sheppo 		if ((ldc_mem_dring_info(dp->handle, &minfo)) != 0) {
51371ae08745Sheppo 
51381ae08745Sheppo 			DERR(vswp, "%s: dring_addr failed\n", __func__);
51391ae08745Sheppo 
51401ae08745Sheppo 			kmem_free(dp, sizeof (dring_info_t));
51411ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
51421ae08745Sheppo 
51431ae08745Sheppo 			dring_pkt->tag.vio_sid = ldcp->local_session;
51441ae08745Sheppo 			dring_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
51451ae08745Sheppo 
51461ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)dring_pkt);
51471ae08745Sheppo 
51481ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_DRING_NACK_SENT;
5149*b071742bSsg70180 			(void) vsw_send_msg(ldcp, (void *)dring_pkt,
5150*b071742bSsg70180 				sizeof (vio_dring_reg_msg_t), B_TRUE);
51511ae08745Sheppo 
51521ae08745Sheppo 			vsw_next_milestone(ldcp);
51531ae08745Sheppo 			return;
51541ae08745Sheppo 		} else {
51551ae08745Sheppo 			/* store the address of the pub part of ring */
51561ae08745Sheppo 			dp->pub_addr = minfo.vaddr;
51571ae08745Sheppo 		}
51581ae08745Sheppo 
51591ae08745Sheppo 		/* no private section as we are importing */
51601ae08745Sheppo 		dp->priv_addr = NULL;
51611ae08745Sheppo 
51621ae08745Sheppo 		/*
51631ae08745Sheppo 		 * Using simple mono increasing int for ident at
51641ae08745Sheppo 		 * the moment.
51651ae08745Sheppo 		 */
51661ae08745Sheppo 		dp->ident = ldcp->next_ident;
51671ae08745Sheppo 		ldcp->next_ident++;
51681ae08745Sheppo 
51691ae08745Sheppo 		dp->end_idx = 0;
51701ae08745Sheppo 		dp->next = NULL;
51711ae08745Sheppo 
51721ae08745Sheppo 		/*
51731ae08745Sheppo 		 * Link it onto the end of the list of drings
51741ae08745Sheppo 		 * for this lane.
51751ae08745Sheppo 		 */
51761ae08745Sheppo 		if (ldcp->lane_in.dringp == NULL) {
51771ae08745Sheppo 			D2(vswp, "%s: adding first INBOUND dring", __func__);
51781ae08745Sheppo 			ldcp->lane_in.dringp = dp;
51791ae08745Sheppo 		} else {
51801ae08745Sheppo 			dbp = ldcp->lane_in.dringp;
51811ae08745Sheppo 
51821ae08745Sheppo 			while (dbp->next != NULL)
51831ae08745Sheppo 				dbp = dbp->next;
51841ae08745Sheppo 
51851ae08745Sheppo 			dbp->next = dp;
51861ae08745Sheppo 		}
51871ae08745Sheppo 
51881ae08745Sheppo 		/* acknowledge it */
51891ae08745Sheppo 		dring_pkt->tag.vio_sid = ldcp->local_session;
51901ae08745Sheppo 		dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
51911ae08745Sheppo 		dring_pkt->dring_ident = dp->ident;
51921ae08745Sheppo 
5193*b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)dring_pkt,
5194*b071742bSsg70180 			sizeof (vio_dring_reg_msg_t), B_TRUE);
51951ae08745Sheppo 
51961ae08745Sheppo 		ldcp->lane_in.lstate |= VSW_DRING_ACK_SENT;
51971ae08745Sheppo 		vsw_next_milestone(ldcp);
51981ae08745Sheppo 		break;
51991ae08745Sheppo 
52001ae08745Sheppo 	case VIO_SUBTYPE_ACK:
52011ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
52021ae08745Sheppo 
52031ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_DRING_ACK_RECV))
52041ae08745Sheppo 			return;
52051ae08745Sheppo 
52061ae08745Sheppo 		/*
52071ae08745Sheppo 		 * Peer is acknowledging our dring info and will have
52081ae08745Sheppo 		 * sent us a dring identifier which we will use to
52091ae08745Sheppo 		 * refer to this ring w.r.t. our peer.
52101ae08745Sheppo 		 */
52111ae08745Sheppo 		dp = ldcp->lane_out.dringp;
52121ae08745Sheppo 		if (dp != NULL) {
52131ae08745Sheppo 			/*
52141ae08745Sheppo 			 * Find the ring this ident should be associated
52151ae08745Sheppo 			 * with.
52161ae08745Sheppo 			 */
52171ae08745Sheppo 			if (vsw_dring_match(dp, dring_pkt)) {
52181ae08745Sheppo 				dring_found = 1;
52191ae08745Sheppo 
52201ae08745Sheppo 			} else while (dp != NULL) {
52211ae08745Sheppo 				if (vsw_dring_match(dp, dring_pkt)) {
52221ae08745Sheppo 					dring_found = 1;
52231ae08745Sheppo 					break;
52241ae08745Sheppo 				}
52251ae08745Sheppo 				dp = dp->next;
52261ae08745Sheppo 			}
52271ae08745Sheppo 
52281ae08745Sheppo 			if (dring_found == 0) {
52291ae08745Sheppo 				DERR(NULL, "%s: unrecognised ring cookie",
52301ae08745Sheppo 					__func__);
5231*b071742bSsg70180 				vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
52321ae08745Sheppo 				return;
52331ae08745Sheppo 			}
52341ae08745Sheppo 
52351ae08745Sheppo 		} else {
52361ae08745Sheppo 			DERR(vswp, "%s: DRING ACK received but no drings "
52371ae08745Sheppo 				"allocated", __func__);
5238*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
52391ae08745Sheppo 			return;
52401ae08745Sheppo 		}
52411ae08745Sheppo 
52421ae08745Sheppo 		/* store ident */
52431ae08745Sheppo 		dp->ident = dring_pkt->dring_ident;
52441ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_DRING_ACK_RECV;
52451ae08745Sheppo 		vsw_next_milestone(ldcp);
52461ae08745Sheppo 		break;
52471ae08745Sheppo 
52481ae08745Sheppo 	case VIO_SUBTYPE_NACK:
52491ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
52501ae08745Sheppo 
52511ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_DRING_NACK_RECV))
52521ae08745Sheppo 			return;
52531ae08745Sheppo 
52541ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_DRING_NACK_RECV;
52551ae08745Sheppo 		vsw_next_milestone(ldcp);
52561ae08745Sheppo 		break;
52571ae08745Sheppo 
52581ae08745Sheppo 	default:
52591ae08745Sheppo 		DERR(vswp, "%s: Unknown vio_subtype %x\n", __func__,
52601ae08745Sheppo 			dring_pkt->tag.vio_subtype);
52611ae08745Sheppo 	}
52621ae08745Sheppo 
52631ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
52641ae08745Sheppo }
52651ae08745Sheppo 
52661ae08745Sheppo /*
52671ae08745Sheppo  * Process a request from peer to unregister a dring.
52681ae08745Sheppo  *
52691ae08745Sheppo  * For the moment we just restart the handshake if our
52701ae08745Sheppo  * peer endpoint attempts to unregister a dring.
52711ae08745Sheppo  */
52721ae08745Sheppo void
52731ae08745Sheppo vsw_process_ctrl_dring_unreg_pkt(vsw_ldc_t *ldcp, void *pkt)
52741ae08745Sheppo {
52751ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
52761ae08745Sheppo 	vio_dring_unreg_msg_t	*dring_pkt;
52771ae08745Sheppo 
52781ae08745Sheppo 	/*
52791ae08745Sheppo 	 * We know this is a ctrl/dring packet so
52801ae08745Sheppo 	 * cast it into the correct structure.
52811ae08745Sheppo 	 */
52821ae08745Sheppo 	dring_pkt = (vio_dring_unreg_msg_t *)pkt;
52831ae08745Sheppo 
52841ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
52851ae08745Sheppo 
52861ae08745Sheppo 	switch (dring_pkt->tag.vio_subtype) {
52871ae08745Sheppo 	case VIO_SUBTYPE_INFO:
52881ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
52891ae08745Sheppo 
52901ae08745Sheppo 		DWARN(vswp, "%s: restarting handshake..", __func__);
52911ae08745Sheppo 		break;
52921ae08745Sheppo 
52931ae08745Sheppo 	case VIO_SUBTYPE_ACK:
52941ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
52951ae08745Sheppo 
52961ae08745Sheppo 		DWARN(vswp, "%s: restarting handshake..", __func__);
52971ae08745Sheppo 		break;
52981ae08745Sheppo 
52991ae08745Sheppo 	case VIO_SUBTYPE_NACK:
53001ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
53011ae08745Sheppo 
53021ae08745Sheppo 		DWARN(vswp, "%s: restarting handshake..", __func__);
53031ae08745Sheppo 		break;
53041ae08745Sheppo 
53051ae08745Sheppo 	default:
53061ae08745Sheppo 		DERR(vswp, "%s: Unknown vio_subtype %x\n", __func__,
53071ae08745Sheppo 			dring_pkt->tag.vio_subtype);
53081ae08745Sheppo 	}
53091ae08745Sheppo 
5310*b071742bSsg70180 	vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
5311*b071742bSsg70180 
53121ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
53131ae08745Sheppo }
53141ae08745Sheppo 
53151ae08745Sheppo #define	SND_MCST_NACK(ldcp, pkt) \
53161ae08745Sheppo 	pkt->tag.vio_subtype = VIO_SUBTYPE_NACK; \
53171ae08745Sheppo 	pkt->tag.vio_sid = ldcp->local_session; \
5318*b071742bSsg70180 	(void) vsw_send_msg(ldcp, (void *)pkt, \
5319*b071742bSsg70180 			sizeof (vnet_mcast_msg_t), B_TRUE);
53201ae08745Sheppo 
53211ae08745Sheppo /*
53221ae08745Sheppo  * Process a multicast request from a vnet.
53231ae08745Sheppo  *
53241ae08745Sheppo  * Vnet's specify a multicast address that they are interested in. This
53251ae08745Sheppo  * address is used as a key into the hash table which forms the multicast
53261ae08745Sheppo  * forwarding database (mFDB).
53271ae08745Sheppo  *
53281ae08745Sheppo  * The table keys are the multicast addresses, while the table entries
53291ae08745Sheppo  * are pointers to lists of ports which wish to receive packets for the
53301ae08745Sheppo  * specified multicast address.
53311ae08745Sheppo  *
53321ae08745Sheppo  * When a multicast packet is being switched we use the address as a key
53331ae08745Sheppo  * into the hash table, and then walk the appropriate port list forwarding
53341ae08745Sheppo  * the pkt to each port in turn.
53351ae08745Sheppo  *
53361ae08745Sheppo  * If a vnet is no longer interested in a particular multicast grouping
53371ae08745Sheppo  * we simply find the correct location in the hash table and then delete
53381ae08745Sheppo  * the relevant port from the port list.
53391ae08745Sheppo  *
53401ae08745Sheppo  * To deal with the case whereby a port is being deleted without first
53411ae08745Sheppo  * removing itself from the lists in the hash table, we maintain a list
53421ae08745Sheppo  * of multicast addresses the port has registered an interest in, within
53431ae08745Sheppo  * the port structure itself. We then simply walk that list of addresses
53441ae08745Sheppo  * using them as keys into the hash table and remove the port from the
53451ae08745Sheppo  * appropriate lists.
53461ae08745Sheppo  */
53471ae08745Sheppo static void
53481ae08745Sheppo vsw_process_ctrl_mcst_pkt(vsw_ldc_t *ldcp, void *pkt)
53491ae08745Sheppo {
53501ae08745Sheppo 	vnet_mcast_msg_t	*mcst_pkt;
53511ae08745Sheppo 	vsw_port_t		*port = ldcp->ldc_port;
53521ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
53531ae08745Sheppo 	int			i;
53541ae08745Sheppo 
53551ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
53561ae08745Sheppo 
53571ae08745Sheppo 	/*
53581ae08745Sheppo 	 * We know this is a ctrl/mcast packet so
53591ae08745Sheppo 	 * cast it into the correct structure.
53601ae08745Sheppo 	 */
53611ae08745Sheppo 	mcst_pkt = (vnet_mcast_msg_t *)pkt;
53621ae08745Sheppo 
53631ae08745Sheppo 	switch (mcst_pkt->tag.vio_subtype) {
53641ae08745Sheppo 	case VIO_SUBTYPE_INFO:
53651ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
53661ae08745Sheppo 
53671ae08745Sheppo 		/*
53681ae08745Sheppo 		 * Check if in correct state to receive a multicast
53691ae08745Sheppo 		 * message (i.e. handshake complete). If not reset
53701ae08745Sheppo 		 * the handshake.
53711ae08745Sheppo 		 */
53721ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_MCST_INFO_RECV))
53731ae08745Sheppo 			return;
53741ae08745Sheppo 
53751ae08745Sheppo 		/*
53761ae08745Sheppo 		 * Before attempting to add or remove address check
53771ae08745Sheppo 		 * that they are valid multicast addresses.
53781ae08745Sheppo 		 * If not, then NACK back.
53791ae08745Sheppo 		 */
53801ae08745Sheppo 		for (i = 0; i < mcst_pkt->count; i++) {
53811ae08745Sheppo 			if ((mcst_pkt->mca[i].ether_addr_octet[0] & 01) != 1) {
53821ae08745Sheppo 				DERR(vswp, "%s: invalid multicast address",
53831ae08745Sheppo 								__func__);
53841ae08745Sheppo 				SND_MCST_NACK(ldcp, mcst_pkt);
53851ae08745Sheppo 				return;
53861ae08745Sheppo 			}
53871ae08745Sheppo 		}
53881ae08745Sheppo 
53891ae08745Sheppo 		/*
53901ae08745Sheppo 		 * Now add/remove the addresses. If this fails we
53911ae08745Sheppo 		 * NACK back.
53921ae08745Sheppo 		 */
53931ae08745Sheppo 		if (vsw_add_rem_mcst(mcst_pkt, port) != 0) {
53941ae08745Sheppo 			SND_MCST_NACK(ldcp, mcst_pkt);
53951ae08745Sheppo 			return;
53961ae08745Sheppo 		}
53971ae08745Sheppo 
53981ae08745Sheppo 		mcst_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
53991ae08745Sheppo 		mcst_pkt->tag.vio_sid = ldcp->local_session;
54001ae08745Sheppo 
54011ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)mcst_pkt);
54021ae08745Sheppo 
5403*b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)mcst_pkt,
5404*b071742bSsg70180 				sizeof (vnet_mcast_msg_t), B_TRUE);
54051ae08745Sheppo 		break;
54061ae08745Sheppo 
54071ae08745Sheppo 	case VIO_SUBTYPE_ACK:
54081ae08745Sheppo 		DWARN(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
54091ae08745Sheppo 
54101ae08745Sheppo 		/*
54111ae08745Sheppo 		 * We shouldn't ever get a multicast ACK message as
54121ae08745Sheppo 		 * at the moment we never request multicast addresses
54131ae08745Sheppo 		 * to be set on some other device. This may change in
54141ae08745Sheppo 		 * the future if we have cascading switches.
54151ae08745Sheppo 		 */
54161ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_MCST_ACK_RECV))
54171ae08745Sheppo 			return;
54181ae08745Sheppo 
54191ae08745Sheppo 				/* Do nothing */
54201ae08745Sheppo 		break;
54211ae08745Sheppo 
54221ae08745Sheppo 	case VIO_SUBTYPE_NACK:
54231ae08745Sheppo 		DWARN(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
54241ae08745Sheppo 
54251ae08745Sheppo 		/*
54261ae08745Sheppo 		 * We shouldn't get a multicast NACK packet for the
54271ae08745Sheppo 		 * same reasons as we shouldn't get a ACK packet.
54281ae08745Sheppo 		 */
54291ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_MCST_NACK_RECV))
54301ae08745Sheppo 			return;
54311ae08745Sheppo 
54321ae08745Sheppo 				/* Do nothing */
54331ae08745Sheppo 		break;
54341ae08745Sheppo 
54351ae08745Sheppo 	default:
54361ae08745Sheppo 		DERR(vswp, "%s: unknown vio_subtype %x\n", __func__,
54371ae08745Sheppo 			mcst_pkt->tag.vio_subtype);
54381ae08745Sheppo 	}
54391ae08745Sheppo 
54401ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
54411ae08745Sheppo }
54421ae08745Sheppo 
54431ae08745Sheppo static void
54441ae08745Sheppo vsw_process_ctrl_rdx_pkt(vsw_ldc_t *ldcp, void *pkt)
54451ae08745Sheppo {
54461ae08745Sheppo 	vio_rdx_msg_t	*rdx_pkt;
54471ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
54481ae08745Sheppo 
54491ae08745Sheppo 	/*
54501ae08745Sheppo 	 * We know this is a ctrl/rdx packet so
54511ae08745Sheppo 	 * cast it into the correct structure.
54521ae08745Sheppo 	 */
54531ae08745Sheppo 	rdx_pkt = (vio_rdx_msg_t *)pkt;
54541ae08745Sheppo 
54551ae08745Sheppo 	D1(vswp, "%s(%lld) enter", __func__, ldcp->ldc_id);
54561ae08745Sheppo 
54571ae08745Sheppo 	switch (rdx_pkt->tag.vio_subtype) {
54581ae08745Sheppo 	case VIO_SUBTYPE_INFO:
54591ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
54601ae08745Sheppo 
5461*b071742bSsg70180 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_RDX_INFO_RECV))
54621ae08745Sheppo 			return;
54631ae08745Sheppo 
54641ae08745Sheppo 		rdx_pkt->tag.vio_sid = ldcp->local_session;
54651ae08745Sheppo 		rdx_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
54661ae08745Sheppo 
54671ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)rdx_pkt);
54681ae08745Sheppo 
5469*b071742bSsg70180 		ldcp->lane_out.lstate |= VSW_RDX_ACK_SENT;
54701ae08745Sheppo 
5471*b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)rdx_pkt,
5472*b071742bSsg70180 			sizeof (vio_rdx_msg_t), B_TRUE);
54731ae08745Sheppo 
54741ae08745Sheppo 		vsw_next_milestone(ldcp);
54751ae08745Sheppo 		break;
54761ae08745Sheppo 
54771ae08745Sheppo 	case VIO_SUBTYPE_ACK:
54781ae08745Sheppo 		/*
54791ae08745Sheppo 		 * Should be handled in-band by callback handler.
54801ae08745Sheppo 		 */
54811ae08745Sheppo 		DERR(vswp, "%s: Unexpected VIO_SUBTYPE_ACK", __func__);
5482*b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
54831ae08745Sheppo 		break;
54841ae08745Sheppo 
54851ae08745Sheppo 	case VIO_SUBTYPE_NACK:
54861ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
54871ae08745Sheppo 
5488*b071742bSsg70180 		if (vsw_check_flag(ldcp, INBOUND, VSW_RDX_NACK_RECV))
54891ae08745Sheppo 			return;
54901ae08745Sheppo 
5491*b071742bSsg70180 		ldcp->lane_in.lstate |= VSW_RDX_NACK_RECV;
54921ae08745Sheppo 		vsw_next_milestone(ldcp);
54931ae08745Sheppo 		break;
54941ae08745Sheppo 
54951ae08745Sheppo 	default:
54961ae08745Sheppo 		DERR(vswp, "%s: Unknown vio_subtype %x\n", __func__,
54971ae08745Sheppo 			rdx_pkt->tag.vio_subtype);
54981ae08745Sheppo 	}
54991ae08745Sheppo 
55001ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
55011ae08745Sheppo }
55021ae08745Sheppo 
55031ae08745Sheppo static void
55041ae08745Sheppo vsw_process_data_pkt(vsw_ldc_t *ldcp, void *dpkt, vio_msg_tag_t tag)
55051ae08745Sheppo {
55061ae08745Sheppo 	uint16_t	env = tag.vio_subtype_env;
55071ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
55081ae08745Sheppo 
55091ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
55101ae08745Sheppo 
55111ae08745Sheppo 	/* session id check */
55121ae08745Sheppo 	if (ldcp->session_status & VSW_PEER_SESSION) {
55131ae08745Sheppo 		if (ldcp->peer_session != tag.vio_sid) {
55141ae08745Sheppo 			DERR(vswp, "%s (chan %d): invalid session id (%llx)",
55151ae08745Sheppo 				__func__, ldcp->ldc_id, tag.vio_sid);
5516*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
55171ae08745Sheppo 			return;
55181ae08745Sheppo 		}
55191ae08745Sheppo 	}
55201ae08745Sheppo 
55211ae08745Sheppo 	/*
55221ae08745Sheppo 	 * It is an error for us to be getting data packets
55231ae08745Sheppo 	 * before the handshake has completed.
55241ae08745Sheppo 	 */
55251ae08745Sheppo 	if (ldcp->hphase != VSW_MILESTONE4) {
55261ae08745Sheppo 		DERR(vswp, "%s: got data packet before handshake complete "
55271ae08745Sheppo 			"hphase %d (%x: %x)", __func__, ldcp->hphase,
55281ae08745Sheppo 			ldcp->lane_in.lstate, ldcp->lane_out.lstate);
55291ae08745Sheppo 		DUMP_FLAGS(ldcp->lane_in.lstate);
55301ae08745Sheppo 		DUMP_FLAGS(ldcp->lane_out.lstate);
5531*b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
55321ae08745Sheppo 		return;
55331ae08745Sheppo 	}
55341ae08745Sheppo 
55351ae08745Sheppo 	/*
55361ae08745Sheppo 	 * Switch on vio_subtype envelope, then let lower routines
55371ae08745Sheppo 	 * decide if its an INFO, ACK or NACK packet.
55381ae08745Sheppo 	 */
55391ae08745Sheppo 	if (env == VIO_DRING_DATA) {
55401ae08745Sheppo 		vsw_process_data_dring_pkt(ldcp, dpkt);
55411ae08745Sheppo 	} else if (env == VIO_PKT_DATA) {
55421ae08745Sheppo 		vsw_process_data_raw_pkt(ldcp, dpkt);
55431ae08745Sheppo 	} else if (env == VIO_DESC_DATA) {
55441ae08745Sheppo 		vsw_process_data_ibnd_pkt(ldcp, dpkt);
55451ae08745Sheppo 	} else {
55461ae08745Sheppo 		DERR(vswp, "%s : unknown vio_subtype_env (%x)\n",
55471ae08745Sheppo 							__func__, env);
55481ae08745Sheppo 	}
55491ae08745Sheppo 
55501ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
55511ae08745Sheppo }
55521ae08745Sheppo 
55531ae08745Sheppo #define	SND_DRING_NACK(ldcp, pkt) \
55541ae08745Sheppo 	pkt->tag.vio_subtype = VIO_SUBTYPE_NACK; \
55551ae08745Sheppo 	pkt->tag.vio_sid = ldcp->local_session; \
5556*b071742bSsg70180 	(void) vsw_send_msg(ldcp, (void *)pkt, \
5557*b071742bSsg70180 			sizeof (vio_dring_msg_t), B_TRUE);
55581ae08745Sheppo 
55591ae08745Sheppo static void
55601ae08745Sheppo vsw_process_data_dring_pkt(vsw_ldc_t *ldcp, void *dpkt)
55611ae08745Sheppo {
55621ae08745Sheppo 	vio_dring_msg_t		*dring_pkt;
55631ae08745Sheppo 	vnet_public_desc_t	*pub_addr = NULL;
55641ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
55651ae08745Sheppo 	dring_info_t		*dp = NULL;
55661ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
55671ae08745Sheppo 	mblk_t			*mp = NULL;
55681ae08745Sheppo 	mblk_t			*bp = NULL;
55691ae08745Sheppo 	mblk_t			*bpt = NULL;
55701ae08745Sheppo 	size_t			nbytes = 0;
55711ae08745Sheppo 	size_t			off = 0;
55721ae08745Sheppo 	uint64_t		ncookies = 0;
55731ae08745Sheppo 	uint64_t		chain = 0;
5574d10e4ef2Snarayan 	uint64_t		j, len;
5575d10e4ef2Snarayan 	uint32_t		pos, start, datalen;
5576d10e4ef2Snarayan 	uint32_t		range_start, range_end;
5577d10e4ef2Snarayan 	int32_t			end, num, cnt = 0;
5578*b071742bSsg70180 	int			i, rv, msg_rv = 0;
55791ae08745Sheppo 	boolean_t		ack_needed = B_FALSE;
5580d10e4ef2Snarayan 	boolean_t		prev_desc_ack = B_FALSE;
5581d10e4ef2Snarayan 	int			read_attempts = 0;
55821ae08745Sheppo 
55831ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
55841ae08745Sheppo 
55851ae08745Sheppo 	/*
55861ae08745Sheppo 	 * We know this is a data/dring packet so
55871ae08745Sheppo 	 * cast it into the correct structure.
55881ae08745Sheppo 	 */
55891ae08745Sheppo 	dring_pkt = (vio_dring_msg_t *)dpkt;
55901ae08745Sheppo 
55911ae08745Sheppo 	/*
55921ae08745Sheppo 	 * Switch on the vio_subtype. If its INFO then we need to
55931ae08745Sheppo 	 * process the data. If its an ACK we need to make sure
55941ae08745Sheppo 	 * it makes sense (i.e did we send an earlier data/info),
55951ae08745Sheppo 	 * and if its a NACK then we maybe attempt a retry.
55961ae08745Sheppo 	 */
55971ae08745Sheppo 	switch (dring_pkt->tag.vio_subtype) {
55981ae08745Sheppo 	case VIO_SUBTYPE_INFO:
55991ae08745Sheppo 		D2(vswp, "%s(%lld): VIO_SUBTYPE_INFO", __func__, ldcp->ldc_id);
56001ae08745Sheppo 
5601445b4c2eSsb155480 		READ_ENTER(&ldcp->lane_in.dlistrw);
56021ae08745Sheppo 		if ((dp = vsw_ident2dring(&ldcp->lane_in,
56031ae08745Sheppo 				dring_pkt->dring_ident)) == NULL) {
5604445b4c2eSsb155480 			RW_EXIT(&ldcp->lane_in.dlistrw);
56051ae08745Sheppo 
56061ae08745Sheppo 			DERR(vswp, "%s(%lld): unable to find dring from "
56071ae08745Sheppo 				"ident 0x%llx", __func__, ldcp->ldc_id,
56081ae08745Sheppo 				dring_pkt->dring_ident);
56091ae08745Sheppo 
56101ae08745Sheppo 			SND_DRING_NACK(ldcp, dring_pkt);
56111ae08745Sheppo 			return;
56121ae08745Sheppo 		}
56131ae08745Sheppo 
5614d10e4ef2Snarayan 		start = pos = dring_pkt->start_idx;
56151ae08745Sheppo 		end = dring_pkt->end_idx;
5616d10e4ef2Snarayan 		len = dp->num_descriptors;
56171ae08745Sheppo 
5618d10e4ef2Snarayan 		range_start = range_end = pos;
5619d10e4ef2Snarayan 
5620d10e4ef2Snarayan 		D2(vswp, "%s(%lld): start index %ld : end %ld\n",
56211ae08745Sheppo 			__func__, ldcp->ldc_id, start, end);
56221ae08745Sheppo 
5623d10e4ef2Snarayan 		if (end == -1) {
5624d10e4ef2Snarayan 			num = -1;
56254bac2208Snarayan 		} else if (end >= 0) {
5626d10e4ef2Snarayan 			num = end >= pos ?
5627d10e4ef2Snarayan 				end - pos + 1: (len - pos + 1) + end;
5628d10e4ef2Snarayan 
56291ae08745Sheppo 			/* basic sanity check */
56301ae08745Sheppo 			if (end > len) {
5631445b4c2eSsb155480 				RW_EXIT(&ldcp->lane_in.dlistrw);
5632d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): endpoint %lld outside "
5633d10e4ef2Snarayan 					"ring length %lld", __func__,
5634d10e4ef2Snarayan 					ldcp->ldc_id, end, len);
56351ae08745Sheppo 
56361ae08745Sheppo 				SND_DRING_NACK(ldcp, dring_pkt);
56371ae08745Sheppo 				return;
56381ae08745Sheppo 			}
5639d10e4ef2Snarayan 		} else {
5640445b4c2eSsb155480 			RW_EXIT(&ldcp->lane_in.dlistrw);
5641d10e4ef2Snarayan 			DERR(vswp, "%s(%lld): invalid endpoint %lld",
5642d10e4ef2Snarayan 				__func__, ldcp->ldc_id, end);
5643d10e4ef2Snarayan 			SND_DRING_NACK(ldcp, dring_pkt);
56441ae08745Sheppo 			return;
56451ae08745Sheppo 		}
56461ae08745Sheppo 
5647d10e4ef2Snarayan 		while (cnt != num) {
5648d10e4ef2Snarayan vsw_recheck_desc:
5649d10e4ef2Snarayan 			if ((rv = ldc_mem_dring_acquire(dp->handle,
5650d10e4ef2Snarayan 							pos, pos)) != 0) {
5651445b4c2eSsb155480 				RW_EXIT(&ldcp->lane_in.dlistrw);
5652d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): unable to acquire "
5653d10e4ef2Snarayan 					"descriptor at pos %d: err %d",
5654d10e4ef2Snarayan 					__func__, pos, ldcp->ldc_id, rv);
5655d10e4ef2Snarayan 				SND_DRING_NACK(ldcp, dring_pkt);
5656d10e4ef2Snarayan 				return;
5657d10e4ef2Snarayan 			}
56581ae08745Sheppo 
5659d10e4ef2Snarayan 			pub_addr = (vnet_public_desc_t *)dp->pub_addr + pos;
56601ae08745Sheppo 
5661d10e4ef2Snarayan 			/*
5662d10e4ef2Snarayan 			 * When given a bounded range of descriptors
5663d10e4ef2Snarayan 			 * to process, its an error to hit a descriptor
5664d10e4ef2Snarayan 			 * which is not ready. In the non-bounded case
5665d10e4ef2Snarayan 			 * (end_idx == -1) this simply indicates we have
5666d10e4ef2Snarayan 			 * reached the end of the current active range.
5667d10e4ef2Snarayan 			 */
5668d10e4ef2Snarayan 			if (pub_addr->hdr.dstate != VIO_DESC_READY) {
5669d10e4ef2Snarayan 				/* unbound - no error */
5670d10e4ef2Snarayan 				if (end == -1) {
5671d10e4ef2Snarayan 					if (read_attempts == vsw_read_attempts)
5672d10e4ef2Snarayan 						break;
56731ae08745Sheppo 
5674d10e4ef2Snarayan 					delay(drv_usectohz(vsw_desc_delay));
5675d10e4ef2Snarayan 					read_attempts++;
5676d10e4ef2Snarayan 					goto vsw_recheck_desc;
5677d10e4ef2Snarayan 				}
56781ae08745Sheppo 
5679d10e4ef2Snarayan 				/* bounded - error - so NACK back */
5680445b4c2eSsb155480 				RW_EXIT(&ldcp->lane_in.dlistrw);
5681d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): descriptor not READY "
5682d10e4ef2Snarayan 					"(%d)", __func__, ldcp->ldc_id,
5683d10e4ef2Snarayan 					pub_addr->hdr.dstate);
5684d10e4ef2Snarayan 				SND_DRING_NACK(ldcp, dring_pkt);
5685d10e4ef2Snarayan 				return;
5686d10e4ef2Snarayan 			}
5687d10e4ef2Snarayan 
5688d10e4ef2Snarayan 			DTRACE_PROBE1(read_attempts, int, read_attempts);
5689d10e4ef2Snarayan 
5690d10e4ef2Snarayan 			range_end = pos;
5691d10e4ef2Snarayan 
5692d10e4ef2Snarayan 			/*
5693d10e4ef2Snarayan 			 * If we ACK'd the previous descriptor then now
5694d10e4ef2Snarayan 			 * record the new range start position for later
5695d10e4ef2Snarayan 			 * ACK's.
5696d10e4ef2Snarayan 			 */
5697d10e4ef2Snarayan 			if (prev_desc_ack) {
5698d10e4ef2Snarayan 				range_start = pos;
5699d10e4ef2Snarayan 
5700d10e4ef2Snarayan 				D2(vswp, "%s(%lld): updating range start "
5701d10e4ef2Snarayan 					"to be %d", __func__, ldcp->ldc_id,
5702d10e4ef2Snarayan 					range_start);
5703d10e4ef2Snarayan 
5704d10e4ef2Snarayan 				prev_desc_ack = B_FALSE;
5705d10e4ef2Snarayan 			}
57061ae08745Sheppo 
57071ae08745Sheppo 			/*
57081ae08745Sheppo 			 * Data is padded to align on 8 byte boundary,
57091ae08745Sheppo 			 * datalen is actual data length, i.e. minus that
57101ae08745Sheppo 			 * padding.
57111ae08745Sheppo 			 */
57121ae08745Sheppo 			datalen = pub_addr->nbytes;
57131ae08745Sheppo 
57141ae08745Sheppo 			/*
57151ae08745Sheppo 			 * Does peer wish us to ACK when we have finished
57161ae08745Sheppo 			 * with this descriptor ?
57171ae08745Sheppo 			 */
57181ae08745Sheppo 			if (pub_addr->hdr.ack)
57191ae08745Sheppo 				ack_needed = B_TRUE;
57201ae08745Sheppo 
57211ae08745Sheppo 			D2(vswp, "%s(%lld): processing desc %lld at pos"
57221ae08745Sheppo 				" 0x%llx : dstate 0x%lx : datalen 0x%lx",
5723d10e4ef2Snarayan 				__func__, ldcp->ldc_id, pos, pub_addr,
57241ae08745Sheppo 				pub_addr->hdr.dstate, datalen);
57251ae08745Sheppo 
57261ae08745Sheppo 			/*
57271ae08745Sheppo 			 * Mark that we are starting to process descriptor.
57281ae08745Sheppo 			 */
57291ae08745Sheppo 			pub_addr->hdr.dstate = VIO_DESC_ACCEPTED;
57301ae08745Sheppo 
5731d10e4ef2Snarayan 			mp = vio_allocb(ldcp->rxh);
5732d10e4ef2Snarayan 			if (mp == NULL) {
57331ae08745Sheppo 				/*
5734d10e4ef2Snarayan 				 * No free receive buffers available, so
5735d10e4ef2Snarayan 				 * fallback onto allocb(9F). Make sure that
5736d10e4ef2Snarayan 				 * we get a data buffer which is a multiple
5737d10e4ef2Snarayan 				 * of 8 as this is required by ldc_mem_copy.
57381ae08745Sheppo 				 */
5739d10e4ef2Snarayan 				DTRACE_PROBE(allocb);
5740d10e4ef2Snarayan 				mp = allocb(datalen + VNET_IPALIGN + 8,
5741d10e4ef2Snarayan 								BPRI_MED);
5742d10e4ef2Snarayan 			}
5743d10e4ef2Snarayan 
5744d10e4ef2Snarayan 			/*
5745d10e4ef2Snarayan 			 * Ensure that we ask ldc for an aligned
5746d10e4ef2Snarayan 			 * number of bytes.
5747d10e4ef2Snarayan 			 */
5748d10e4ef2Snarayan 			nbytes = datalen + VNET_IPALIGN;
57491ae08745Sheppo 			if (nbytes & 0x7) {
57501ae08745Sheppo 				off = 8 - (nbytes & 0x7);
57511ae08745Sheppo 				nbytes += off;
57521ae08745Sheppo 			}
57531ae08745Sheppo 
57541ae08745Sheppo 			ncookies = pub_addr->ncookies;
57551ae08745Sheppo 			rv = ldc_mem_copy(ldcp->ldc_handle,
57561ae08745Sheppo 				(caddr_t)mp->b_rptr, 0, &nbytes,
57571ae08745Sheppo 				pub_addr->memcookie, ncookies,
57581ae08745Sheppo 				LDC_COPY_IN);
57591ae08745Sheppo 
57601ae08745Sheppo 			if (rv != 0) {
57611ae08745Sheppo 				DERR(vswp, "%s(%d): unable to copy in "
5762d10e4ef2Snarayan 					"data from %d cookies in desc %d"
5763d10e4ef2Snarayan 					" (rv %d)", __func__, ldcp->ldc_id,
5764d10e4ef2Snarayan 					ncookies, pos, rv);
57651ae08745Sheppo 				freemsg(mp);
5766d10e4ef2Snarayan 
5767d10e4ef2Snarayan 				pub_addr->hdr.dstate = VIO_DESC_DONE;
57681ae08745Sheppo 				(void) ldc_mem_dring_release(dp->handle,
5769d10e4ef2Snarayan 								pos, pos);
5770d10e4ef2Snarayan 				break;
57711ae08745Sheppo 			} else {
57721ae08745Sheppo 				D2(vswp, "%s(%d): copied in %ld bytes"
57731ae08745Sheppo 					" using %d cookies", __func__,
57741ae08745Sheppo 					ldcp->ldc_id, nbytes, ncookies);
57751ae08745Sheppo 			}
57761ae08745Sheppo 
5777d10e4ef2Snarayan 			/* adjust the read pointer to skip over the padding */
5778d10e4ef2Snarayan 			mp->b_rptr += VNET_IPALIGN;
5779d10e4ef2Snarayan 
57801ae08745Sheppo 			/* point to the actual end of data */
57811ae08745Sheppo 			mp->b_wptr = mp->b_rptr + datalen;
57821ae08745Sheppo 
57831ae08745Sheppo 			/* build a chain of received packets */
57841ae08745Sheppo 			if (bp == NULL) {
57851ae08745Sheppo 				/* first pkt */
57861ae08745Sheppo 				bp = mp;
57871ae08745Sheppo 				bp->b_next = bp->b_prev = NULL;
57881ae08745Sheppo 				bpt = bp;
57891ae08745Sheppo 				chain = 1;
57901ae08745Sheppo 			} else {
57911ae08745Sheppo 				mp->b_next = NULL;
57921ae08745Sheppo 				mp->b_prev = bpt;
57931ae08745Sheppo 				bpt->b_next = mp;
57941ae08745Sheppo 				bpt = mp;
57951ae08745Sheppo 				chain++;
57961ae08745Sheppo 			}
57971ae08745Sheppo 
57981ae08745Sheppo 			/* mark we are finished with this descriptor */
57991ae08745Sheppo 			pub_addr->hdr.dstate = VIO_DESC_DONE;
58001ae08745Sheppo 
5801d10e4ef2Snarayan 			(void) ldc_mem_dring_release(dp->handle, pos, pos);
5802d10e4ef2Snarayan 
58031ae08745Sheppo 			/*
5804d10e4ef2Snarayan 			 * Send an ACK back to peer if requested.
58051ae08745Sheppo 			 */
58061ae08745Sheppo 			if (ack_needed) {
58071ae08745Sheppo 				ack_needed = B_FALSE;
58081ae08745Sheppo 
5809d10e4ef2Snarayan 				dring_pkt->start_idx = range_start;
5810d10e4ef2Snarayan 				dring_pkt->end_idx = range_end;
58111ae08745Sheppo 
5812d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): processed %d %d, ACK"
5813d10e4ef2Snarayan 					" requested", __func__, ldcp->ldc_id,
5814d10e4ef2Snarayan 					dring_pkt->start_idx,
5815d10e4ef2Snarayan 					dring_pkt->end_idx);
58161ae08745Sheppo 
5817d10e4ef2Snarayan 				dring_pkt->dring_process_state = VIO_DP_ACTIVE;
58181ae08745Sheppo 				dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
58191ae08745Sheppo 				dring_pkt->tag.vio_sid = ldcp->local_session;
5820*b071742bSsg70180 				msg_rv = vsw_send_msg(ldcp, (void *)dring_pkt,
5821*b071742bSsg70180 						sizeof (vio_dring_msg_t),
5822*b071742bSsg70180 						B_FALSE);
5823*b071742bSsg70180 
5824*b071742bSsg70180 				/*
5825*b071742bSsg70180 				 * Check if ACK was successfully sent. If not
5826*b071742bSsg70180 				 * we break and deal with that below.
5827*b071742bSsg70180 				 */
5828*b071742bSsg70180 				if (msg_rv != 0)
5829*b071742bSsg70180 					break;
5830d10e4ef2Snarayan 
5831d10e4ef2Snarayan 				prev_desc_ack = B_TRUE;
5832d10e4ef2Snarayan 				range_start = pos;
58331ae08745Sheppo 			}
58341ae08745Sheppo 
5835d10e4ef2Snarayan 			/* next descriptor */
5836d10e4ef2Snarayan 			pos = (pos + 1) % len;
5837d10e4ef2Snarayan 			cnt++;
5838d10e4ef2Snarayan 
5839d10e4ef2Snarayan 			/*
5840d10e4ef2Snarayan 			 * Break out of loop here and stop processing to
5841d10e4ef2Snarayan 			 * allow some other network device (or disk) to
5842d10e4ef2Snarayan 			 * get access to the cpu.
5843d10e4ef2Snarayan 			 */
5844d10e4ef2Snarayan 			if (chain > vsw_chain_len) {
5845d10e4ef2Snarayan 				D3(vswp, "%s(%lld): switching chain of %d "
5846d10e4ef2Snarayan 					"msgs", __func__, ldcp->ldc_id, chain);
5847d10e4ef2Snarayan 				break;
58481ae08745Sheppo 			}
58491ae08745Sheppo 		}
5850445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_in.dlistrw);
58511ae08745Sheppo 
5852*b071742bSsg70180 		/*
5853*b071742bSsg70180 		 * If when we attempted to send the ACK we found that the
5854*b071742bSsg70180 		 * channel had been reset then now handle this. We deal with
5855*b071742bSsg70180 		 * it here as we cannot reset the channel while holding the
5856*b071742bSsg70180 		 * dlistrw lock, and we don't want to acquire/release it
5857*b071742bSsg70180 		 * continuously in the above loop, as a channel reset should
5858*b071742bSsg70180 		 * be a rare event.
5859*b071742bSsg70180 		 */
5860*b071742bSsg70180 		if (msg_rv == ECONNRESET) {
5861*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
5862*b071742bSsg70180 			break;
5863*b071742bSsg70180 		}
5864*b071742bSsg70180 
58651ae08745Sheppo 		/* send the chain of packets to be switched */
5866d10e4ef2Snarayan 		if (bp != NULL) {
5867d10e4ef2Snarayan 			D3(vswp, "%s(%lld): switching chain of %d msgs",
5868d10e4ef2Snarayan 					__func__, ldcp->ldc_id, chain);
586934683adeSsg70180 			vswp->vsw_switch_frame(vswp, bp, VSW_VNETPORT,
58701ae08745Sheppo 							ldcp->ldc_port, NULL);
5871d10e4ef2Snarayan 		}
58721ae08745Sheppo 
5873d10e4ef2Snarayan 		DTRACE_PROBE1(msg_cnt, int, cnt);
5874d10e4ef2Snarayan 
5875d10e4ef2Snarayan 		/*
5876d10e4ef2Snarayan 		 * We are now finished so ACK back with the state
5877d10e4ef2Snarayan 		 * set to STOPPING so our peer knows we are finished
5878d10e4ef2Snarayan 		 */
5879d10e4ef2Snarayan 		dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
5880d10e4ef2Snarayan 		dring_pkt->tag.vio_sid = ldcp->local_session;
5881d10e4ef2Snarayan 
5882d10e4ef2Snarayan 		dring_pkt->dring_process_state = VIO_DP_STOPPED;
5883d10e4ef2Snarayan 
5884d10e4ef2Snarayan 		DTRACE_PROBE(stop_process_sent);
5885d10e4ef2Snarayan 
5886d10e4ef2Snarayan 		/*
5887d10e4ef2Snarayan 		 * We have not processed any more descriptors beyond
5888d10e4ef2Snarayan 		 * the last one we ACK'd.
5889d10e4ef2Snarayan 		 */
5890d10e4ef2Snarayan 		if (prev_desc_ack)
5891d10e4ef2Snarayan 			range_start = range_end;
5892d10e4ef2Snarayan 
5893d10e4ef2Snarayan 		dring_pkt->start_idx = range_start;
5894d10e4ef2Snarayan 		dring_pkt->end_idx = range_end;
5895d10e4ef2Snarayan 
5896d10e4ef2Snarayan 		D2(vswp, "%s(%lld) processed : %d : %d, now stopping",
5897d10e4ef2Snarayan 			__func__, ldcp->ldc_id, dring_pkt->start_idx,
5898d10e4ef2Snarayan 			dring_pkt->end_idx);
5899d10e4ef2Snarayan 
5900*b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)dring_pkt,
5901*b071742bSsg70180 				sizeof (vio_dring_msg_t), B_TRUE);
59021ae08745Sheppo 		break;
59031ae08745Sheppo 
59041ae08745Sheppo 	case VIO_SUBTYPE_ACK:
59051ae08745Sheppo 		D2(vswp, "%s(%lld): VIO_SUBTYPE_ACK", __func__, ldcp->ldc_id);
59061ae08745Sheppo 		/*
59071ae08745Sheppo 		 * Verify that the relevant descriptors are all
59081ae08745Sheppo 		 * marked as DONE
59091ae08745Sheppo 		 */
5910445b4c2eSsb155480 		READ_ENTER(&ldcp->lane_out.dlistrw);
59111ae08745Sheppo 		if ((dp = vsw_ident2dring(&ldcp->lane_out,
59121ae08745Sheppo 			dring_pkt->dring_ident)) == NULL) {
5913445b4c2eSsb155480 			RW_EXIT(&ldcp->lane_out.dlistrw);
59141ae08745Sheppo 			DERR(vswp, "%s: unknown ident in ACK", __func__);
59151ae08745Sheppo 			return;
59161ae08745Sheppo 		}
59171ae08745Sheppo 
59181ae08745Sheppo 		pub_addr = (vnet_public_desc_t *)dp->pub_addr;
59191ae08745Sheppo 		priv_addr = (vsw_private_desc_t *)dp->priv_addr;
59201ae08745Sheppo 
59211ae08745Sheppo 		start = end = 0;
59221ae08745Sheppo 		start = dring_pkt->start_idx;
59231ae08745Sheppo 		end = dring_pkt->end_idx;
59241ae08745Sheppo 		len = dp->num_descriptors;
59251ae08745Sheppo 
59261ae08745Sheppo 		j = num = 0;
59271ae08745Sheppo 		/* calculate # descriptors taking into a/c wrap around */
59281ae08745Sheppo 		num = end >= start ? end - start + 1: (len - start + 1) + end;
59291ae08745Sheppo 
59301ae08745Sheppo 		D2(vswp, "%s(%lld): start index %ld : end %ld : num %ld\n",
59311ae08745Sheppo 			__func__, ldcp->ldc_id, start, end, num);
59321ae08745Sheppo 
5933d10e4ef2Snarayan 		mutex_enter(&dp->dlock);
5934d10e4ef2Snarayan 		dp->last_ack_recv = end;
5935d10e4ef2Snarayan 		mutex_exit(&dp->dlock);
5936d10e4ef2Snarayan 
59371ae08745Sheppo 		for (i = start; j < num; i = (i + 1) % len, j++) {
59381ae08745Sheppo 			pub_addr = (vnet_public_desc_t *)dp->pub_addr + i;
59391ae08745Sheppo 			priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
59401ae08745Sheppo 
5941d10e4ef2Snarayan 			/*
5942d10e4ef2Snarayan 			 * If the last descriptor in a range has the ACK
5943d10e4ef2Snarayan 			 * bit set then we will get two messages from our
5944d10e4ef2Snarayan 			 * peer relating to it. The normal ACK msg and then
5945d10e4ef2Snarayan 			 * a subsequent STOP msg. The first message will have
5946d10e4ef2Snarayan 			 * resulted in the descriptor being reclaimed and
5947d10e4ef2Snarayan 			 * its state set to FREE so when we encounter a non
5948d10e4ef2Snarayan 			 * DONE descriptor we need to check to see if its
5949d10e4ef2Snarayan 			 * because we have just reclaimed it.
5950d10e4ef2Snarayan 			 */
5951d10e4ef2Snarayan 			mutex_enter(&priv_addr->dstate_lock);
5952d10e4ef2Snarayan 			if (pub_addr->hdr.dstate == VIO_DESC_DONE) {
59531ae08745Sheppo 				/* clear all the fields */
59541ae08745Sheppo 				bzero(priv_addr->datap, priv_addr->datalen);
59551ae08745Sheppo 				priv_addr->datalen = 0;
59561ae08745Sheppo 
59571ae08745Sheppo 				pub_addr->hdr.dstate = VIO_DESC_FREE;
59581ae08745Sheppo 				pub_addr->hdr.ack = 0;
5959d10e4ef2Snarayan 
59601ae08745Sheppo 				priv_addr->dstate = VIO_DESC_FREE;
5961d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
59621ae08745Sheppo 
59631ae08745Sheppo 				D3(vswp, "clearing descp %d : pub state "
59641ae08745Sheppo 					"0x%llx : priv state 0x%llx", i,
59651ae08745Sheppo 					pub_addr->hdr.dstate,
59661ae08745Sheppo 					priv_addr->dstate);
5967d10e4ef2Snarayan 
5968d10e4ef2Snarayan 			} else {
5969d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
5970d10e4ef2Snarayan 
5971d10e4ef2Snarayan 				if (dring_pkt->dring_process_state !=
5972d10e4ef2Snarayan 							VIO_DP_STOPPED) {
5973d10e4ef2Snarayan 					DERR(vswp, "%s: descriptor %lld at pos "
5974d10e4ef2Snarayan 						" 0x%llx not DONE (0x%lx)\n",
5975d10e4ef2Snarayan 						__func__, i, pub_addr,
5976d10e4ef2Snarayan 						pub_addr->hdr.dstate);
5977445b4c2eSsb155480 					RW_EXIT(&ldcp->lane_out.dlistrw);
5978d10e4ef2Snarayan 					return;
5979d10e4ef2Snarayan 				}
59801ae08745Sheppo 			}
59811ae08745Sheppo 		}
59821ae08745Sheppo 
5983d10e4ef2Snarayan 		/*
5984d10e4ef2Snarayan 		 * If our peer is stopping processing descriptors then
5985d10e4ef2Snarayan 		 * we check to make sure it has processed all the descriptors
5986d10e4ef2Snarayan 		 * we have updated. If not then we send it a new message
5987d10e4ef2Snarayan 		 * to prompt it to restart.
5988d10e4ef2Snarayan 		 */
5989d10e4ef2Snarayan 		if (dring_pkt->dring_process_state == VIO_DP_STOPPED) {
5990d10e4ef2Snarayan 			DTRACE_PROBE(stop_process_recv);
5991d10e4ef2Snarayan 			D2(vswp, "%s(%lld): got stopping msg : %d : %d",
5992d10e4ef2Snarayan 				__func__, ldcp->ldc_id, dring_pkt->start_idx,
5993d10e4ef2Snarayan 				dring_pkt->end_idx);
5994d10e4ef2Snarayan 
5995d10e4ef2Snarayan 			/*
5996d10e4ef2Snarayan 			 * Check next descriptor in public section of ring.
5997d10e4ef2Snarayan 			 * If its marked as READY then we need to prompt our
5998d10e4ef2Snarayan 			 * peer to start processing the ring again.
5999d10e4ef2Snarayan 			 */
6000d10e4ef2Snarayan 			i = (end + 1) % len;
6001d10e4ef2Snarayan 			pub_addr = (vnet_public_desc_t *)dp->pub_addr + i;
6002d10e4ef2Snarayan 			priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
6003d10e4ef2Snarayan 
6004d10e4ef2Snarayan 			/*
6005d10e4ef2Snarayan 			 * Hold the restart lock across all of this to
6006d10e4ef2Snarayan 			 * make sure that its not possible for us to
6007d10e4ef2Snarayan 			 * decide that a msg needs to be sent in the future
6008d10e4ef2Snarayan 			 * but the sending code having already checked is
6009d10e4ef2Snarayan 			 * about to exit.
6010d10e4ef2Snarayan 			 */
6011d10e4ef2Snarayan 			mutex_enter(&dp->restart_lock);
6012d10e4ef2Snarayan 			mutex_enter(&priv_addr->dstate_lock);
6013d10e4ef2Snarayan 			if (pub_addr->hdr.dstate == VIO_DESC_READY) {
6014d10e4ef2Snarayan 
6015d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
6016d10e4ef2Snarayan 
6017d10e4ef2Snarayan 				dring_pkt->tag.vio_subtype = VIO_SUBTYPE_INFO;
6018d10e4ef2Snarayan 				dring_pkt->tag.vio_sid = ldcp->local_session;
6019d10e4ef2Snarayan 
6020d10e4ef2Snarayan 				mutex_enter(&ldcp->lane_out.seq_lock);
6021d10e4ef2Snarayan 				dring_pkt->seq_num = ldcp->lane_out.seq_num++;
6022d10e4ef2Snarayan 				mutex_exit(&ldcp->lane_out.seq_lock);
6023d10e4ef2Snarayan 
6024d10e4ef2Snarayan 				dring_pkt->start_idx = (end + 1) % len;
6025d10e4ef2Snarayan 				dring_pkt->end_idx = -1;
6026d10e4ef2Snarayan 
6027d10e4ef2Snarayan 				D2(vswp, "%s(%lld) : sending restart msg:"
6028d10e4ef2Snarayan 					" %d : %d", __func__, ldcp->ldc_id,
6029d10e4ef2Snarayan 					dring_pkt->start_idx,
6030d10e4ef2Snarayan 					dring_pkt->end_idx);
6031d10e4ef2Snarayan 
6032*b071742bSsg70180 				msg_rv = vsw_send_msg(ldcp, (void *)dring_pkt,
6033*b071742bSsg70180 					sizeof (vio_dring_msg_t), B_FALSE);
6034*b071742bSsg70180 
6035d10e4ef2Snarayan 			} else {
6036d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
6037d10e4ef2Snarayan 				dp->restart_reqd = B_TRUE;
6038d10e4ef2Snarayan 			}
6039d10e4ef2Snarayan 			mutex_exit(&dp->restart_lock);
6040d10e4ef2Snarayan 		}
6041445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_out.dlistrw);
6042*b071742bSsg70180 
6043*b071742bSsg70180 		/* only do channel reset after dropping dlistrw lock */
6044*b071742bSsg70180 		if (msg_rv == ECONNRESET)
6045*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
6046*b071742bSsg70180 
60471ae08745Sheppo 		break;
60481ae08745Sheppo 
60491ae08745Sheppo 	case VIO_SUBTYPE_NACK:
60501ae08745Sheppo 		DWARN(vswp, "%s(%lld): VIO_SUBTYPE_NACK",
60511ae08745Sheppo 						__func__, ldcp->ldc_id);
60521ae08745Sheppo 		/*
60531ae08745Sheppo 		 * Something is badly wrong if we are getting NACK's
60541ae08745Sheppo 		 * for our data pkts. So reset the channel.
60551ae08745Sheppo 		 */
6056*b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
60571ae08745Sheppo 
60581ae08745Sheppo 		break;
60591ae08745Sheppo 
60601ae08745Sheppo 	default:
60611ae08745Sheppo 		DERR(vswp, "%s(%lld): Unknown vio_subtype %x\n", __func__,
60621ae08745Sheppo 			ldcp->ldc_id, dring_pkt->tag.vio_subtype);
60631ae08745Sheppo 	}
60641ae08745Sheppo 
60651ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
60661ae08745Sheppo }
60671ae08745Sheppo 
60681ae08745Sheppo /*
60691ae08745Sheppo  * VIO_PKT_DATA (a.k.a raw data mode )
60701ae08745Sheppo  *
60711ae08745Sheppo  * Note - currently not supported. Do nothing.
60721ae08745Sheppo  */
60731ae08745Sheppo static void
60741ae08745Sheppo vsw_process_data_raw_pkt(vsw_ldc_t *ldcp, void *dpkt)
60751ae08745Sheppo {
60761ae08745Sheppo 	_NOTE(ARGUNUSED(dpkt))
60771ae08745Sheppo 
60781ae08745Sheppo 	D1(NULL, "%s (%lld): enter\n", __func__, ldcp->ldc_id);
60791ae08745Sheppo 
60801ae08745Sheppo 	DERR(NULL, "%s (%lld): currently  not supported",
60811ae08745Sheppo 						__func__, ldcp->ldc_id);
60821ae08745Sheppo 
60831ae08745Sheppo 	D1(NULL, "%s (%lld): exit\n", __func__, ldcp->ldc_id);
60841ae08745Sheppo }
60851ae08745Sheppo 
60861ae08745Sheppo /*
60871ae08745Sheppo  * Process an in-band descriptor message (most likely from
60881ae08745Sheppo  * OBP).
60891ae08745Sheppo  */
60901ae08745Sheppo static void
60911ae08745Sheppo vsw_process_data_ibnd_pkt(vsw_ldc_t *ldcp, void *pkt)
60921ae08745Sheppo {
6093445b4c2eSsb155480 	vnet_ibnd_desc_t	*ibnd_desc;
60941ae08745Sheppo 	dring_info_t		*dp = NULL;
60951ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
60961ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
60971ae08745Sheppo 	mblk_t			*mp = NULL;
60981ae08745Sheppo 	size_t			nbytes = 0;
60991ae08745Sheppo 	size_t			off = 0;
61001ae08745Sheppo 	uint64_t		idx = 0;
61014bac2208Snarayan 	uint32_t		num = 1, len, datalen = 0;
61021ae08745Sheppo 	uint64_t		ncookies = 0;
61034bac2208Snarayan 	int			i, rv;
61044bac2208Snarayan 	int			j = 0;
61051ae08745Sheppo 
61061ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
61071ae08745Sheppo 
6108445b4c2eSsb155480 	ibnd_desc = (vnet_ibnd_desc_t *)pkt;
61091ae08745Sheppo 
61101ae08745Sheppo 	switch (ibnd_desc->hdr.tag.vio_subtype) {
61111ae08745Sheppo 	case VIO_SUBTYPE_INFO:
61121ae08745Sheppo 		D1(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
61131ae08745Sheppo 
61141ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_DRING_INFO_RECV))
61151ae08745Sheppo 			return;
61161ae08745Sheppo 
61171ae08745Sheppo 		/*
61181ae08745Sheppo 		 * Data is padded to align on a 8 byte boundary,
61191ae08745Sheppo 		 * nbytes is actual data length, i.e. minus that
61201ae08745Sheppo 		 * padding.
61211ae08745Sheppo 		 */
61221ae08745Sheppo 		datalen = ibnd_desc->nbytes;
61231ae08745Sheppo 
61241ae08745Sheppo 		D2(vswp, "%s(%lld): processing inband desc : "
61251ae08745Sheppo 			": datalen 0x%lx", __func__, ldcp->ldc_id, datalen);
61261ae08745Sheppo 
61271ae08745Sheppo 		ncookies = ibnd_desc->ncookies;
61281ae08745Sheppo 
61291ae08745Sheppo 		/*
61301ae08745Sheppo 		 * allocb(9F) returns an aligned data block. We
61311ae08745Sheppo 		 * need to ensure that we ask ldc for an aligned
61321ae08745Sheppo 		 * number of bytes also.
61331ae08745Sheppo 		 */
61341ae08745Sheppo 		nbytes = datalen;
61351ae08745Sheppo 		if (nbytes & 0x7) {
61361ae08745Sheppo 			off = 8 - (nbytes & 0x7);
61371ae08745Sheppo 			nbytes += off;
61381ae08745Sheppo 		}
61391ae08745Sheppo 
61401ae08745Sheppo 		mp = allocb(datalen, BPRI_MED);
61411ae08745Sheppo 		if (mp == NULL) {
61421ae08745Sheppo 			DERR(vswp, "%s(%lld): allocb failed",
61431ae08745Sheppo 					__func__, ldcp->ldc_id);
61441ae08745Sheppo 			return;
61451ae08745Sheppo 		}
61461ae08745Sheppo 
61471ae08745Sheppo 		rv = ldc_mem_copy(ldcp->ldc_handle, (caddr_t)mp->b_rptr,
61481ae08745Sheppo 			0, &nbytes, ibnd_desc->memcookie, (uint64_t)ncookies,
61491ae08745Sheppo 			LDC_COPY_IN);
61501ae08745Sheppo 
61511ae08745Sheppo 		if (rv != 0) {
61521ae08745Sheppo 			DERR(vswp, "%s(%d): unable to copy in data from "
61531ae08745Sheppo 				"%d cookie(s)", __func__,
61541ae08745Sheppo 				ldcp->ldc_id, ncookies);
61551ae08745Sheppo 			freemsg(mp);
61561ae08745Sheppo 			return;
61571ae08745Sheppo 		} else {
61581ae08745Sheppo 			D2(vswp, "%s(%d): copied in %ld bytes using %d "
61591ae08745Sheppo 				"cookies", __func__, ldcp->ldc_id, nbytes,
61601ae08745Sheppo 				ncookies);
61611ae08745Sheppo 		}
61621ae08745Sheppo 
61631ae08745Sheppo 		/* point to the actual end of data */
61641ae08745Sheppo 		mp->b_wptr = mp->b_rptr + datalen;
61651ae08745Sheppo 
61661ae08745Sheppo 		/*
61671ae08745Sheppo 		 * We ACK back every in-band descriptor message we process
61681ae08745Sheppo 		 */
61691ae08745Sheppo 		ibnd_desc->hdr.tag.vio_subtype = VIO_SUBTYPE_ACK;
61701ae08745Sheppo 		ibnd_desc->hdr.tag.vio_sid = ldcp->local_session;
6171*b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)ibnd_desc,
6172*b071742bSsg70180 				sizeof (vnet_ibnd_desc_t), B_TRUE);
61731ae08745Sheppo 
61741ae08745Sheppo 		/* send the packet to be switched */
617534683adeSsg70180 		vswp->vsw_switch_frame(vswp, mp, VSW_VNETPORT,
61761ae08745Sheppo 					ldcp->ldc_port, NULL);
61771ae08745Sheppo 
61781ae08745Sheppo 		break;
61791ae08745Sheppo 
61801ae08745Sheppo 	case VIO_SUBTYPE_ACK:
61811ae08745Sheppo 		D1(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
61821ae08745Sheppo 
61831ae08745Sheppo 		/* Verify the ACK is valid */
61841ae08745Sheppo 		idx = ibnd_desc->hdr.desc_handle;
61851ae08745Sheppo 
61861ae08745Sheppo 		if (idx >= VSW_RING_NUM_EL) {
618734683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: corrupted ACK received "
618834683adeSsg70180 				"(idx %ld)", vswp->instance, idx);
61891ae08745Sheppo 			return;
61901ae08745Sheppo 		}
61911ae08745Sheppo 
61921ae08745Sheppo 		if ((dp = ldcp->lane_out.dringp) == NULL) {
61931ae08745Sheppo 			DERR(vswp, "%s: no dring found", __func__);
61941ae08745Sheppo 			return;
61951ae08745Sheppo 		}
61961ae08745Sheppo 
61974bac2208Snarayan 		len = dp->num_descriptors;
61984bac2208Snarayan 		/*
61994bac2208Snarayan 		 * If the descriptor we are being ACK'ed for is not the
62004bac2208Snarayan 		 * one we expected, then pkts were lost somwhere, either
62014bac2208Snarayan 		 * when we tried to send a msg, or a previous ACK msg from
62024bac2208Snarayan 		 * our peer. In either case we now reclaim the descriptors
62034bac2208Snarayan 		 * in the range from the last ACK we received up to the
62044bac2208Snarayan 		 * current ACK.
62054bac2208Snarayan 		 */
62064bac2208Snarayan 		if (idx != dp->last_ack_recv) {
62074bac2208Snarayan 			DWARN(vswp, "%s: dropped pkts detected, (%ld, %ld)",
62084bac2208Snarayan 				__func__, dp->last_ack_recv, idx);
62094bac2208Snarayan 			num = idx >= dp->last_ack_recv ?
62104bac2208Snarayan 				idx - dp->last_ack_recv + 1:
62114bac2208Snarayan 				(len - dp->last_ack_recv + 1) + idx;
62124bac2208Snarayan 		}
62131ae08745Sheppo 
62141ae08745Sheppo 		/*
62151ae08745Sheppo 		 * When we sent the in-band message to our peer we
62161ae08745Sheppo 		 * marked the copy in our private ring as READY. We now
62171ae08745Sheppo 		 * check that the descriptor we are being ACK'ed for is in
62181ae08745Sheppo 		 * fact READY, i.e. it is one we have shared with our peer.
62194bac2208Snarayan 		 *
62204bac2208Snarayan 		 * If its not we flag an error, but still reset the descr
62214bac2208Snarayan 		 * back to FREE.
62221ae08745Sheppo 		 */
62234bac2208Snarayan 		for (i = dp->last_ack_recv; j < num; i = (i + 1) % len, j++) {
62244bac2208Snarayan 			priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
6225d10e4ef2Snarayan 			mutex_enter(&priv_addr->dstate_lock);
62261ae08745Sheppo 			if (priv_addr->dstate != VIO_DESC_READY) {
62274bac2208Snarayan 				DERR(vswp, "%s: (%ld) desc at index %ld not "
62284bac2208Snarayan 					"READY (0x%lx)", __func__,
62294bac2208Snarayan 					ldcp->ldc_id, idx, priv_addr->dstate);
62304bac2208Snarayan 				DERR(vswp, "%s: bound %d: ncookies %ld : "
62314bac2208Snarayan 					"datalen %ld", __func__,
62324bac2208Snarayan 					priv_addr->bound, priv_addr->ncookies,
62334bac2208Snarayan 					priv_addr->datalen);
62344bac2208Snarayan 			}
62351ae08745Sheppo 			D2(vswp, "%s: (%lld) freeing descp at %lld", __func__,
62361ae08745Sheppo 				ldcp->ldc_id, idx);
62371ae08745Sheppo 			/* release resources associated with sent msg */
62381ae08745Sheppo 			bzero(priv_addr->datap, priv_addr->datalen);
62391ae08745Sheppo 			priv_addr->datalen = 0;
62401ae08745Sheppo 			priv_addr->dstate = VIO_DESC_FREE;
6241d10e4ef2Snarayan 			mutex_exit(&priv_addr->dstate_lock);
62421ae08745Sheppo 		}
62434bac2208Snarayan 		/* update to next expected value */
62444bac2208Snarayan 		dp->last_ack_recv = (idx + 1) % dp->num_descriptors;
62454bac2208Snarayan 
62461ae08745Sheppo 		break;
62471ae08745Sheppo 
62481ae08745Sheppo 	case VIO_SUBTYPE_NACK:
62491ae08745Sheppo 		DERR(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
62501ae08745Sheppo 
62511ae08745Sheppo 		/*
62521ae08745Sheppo 		 * We should only get a NACK if our peer doesn't like
62531ae08745Sheppo 		 * something about a message we have sent it. If this
62541ae08745Sheppo 		 * happens we just release the resources associated with
62551ae08745Sheppo 		 * the message. (We are relying on higher layers to decide
62561ae08745Sheppo 		 * whether or not to resend.
62571ae08745Sheppo 		 */
62581ae08745Sheppo 
62591ae08745Sheppo 		/* limit check */
62601ae08745Sheppo 		idx = ibnd_desc->hdr.desc_handle;
62611ae08745Sheppo 
62621ae08745Sheppo 		if (idx >= VSW_RING_NUM_EL) {
62631ae08745Sheppo 			DERR(vswp, "%s: corrupted NACK received (idx %lld)",
62641ae08745Sheppo 				__func__, idx);
62651ae08745Sheppo 			return;
62661ae08745Sheppo 		}
62671ae08745Sheppo 
62681ae08745Sheppo 		if ((dp = ldcp->lane_out.dringp) == NULL) {
62691ae08745Sheppo 			DERR(vswp, "%s: no dring found", __func__);
62701ae08745Sheppo 			return;
62711ae08745Sheppo 		}
62721ae08745Sheppo 
62731ae08745Sheppo 		priv_addr = (vsw_private_desc_t *)dp->priv_addr;
62741ae08745Sheppo 
62751ae08745Sheppo 		/* move to correct location in ring */
62761ae08745Sheppo 		priv_addr += idx;
62771ae08745Sheppo 
62781ae08745Sheppo 		/* release resources associated with sent msg */
6279d10e4ef2Snarayan 		mutex_enter(&priv_addr->dstate_lock);
62801ae08745Sheppo 		bzero(priv_addr->datap, priv_addr->datalen);
62811ae08745Sheppo 		priv_addr->datalen = 0;
62821ae08745Sheppo 		priv_addr->dstate = VIO_DESC_FREE;
6283d10e4ef2Snarayan 		mutex_exit(&priv_addr->dstate_lock);
62841ae08745Sheppo 
62851ae08745Sheppo 		break;
62861ae08745Sheppo 
62871ae08745Sheppo 	default:
62881ae08745Sheppo 		DERR(vswp, "%s(%lld): Unknown vio_subtype %x\n", __func__,
62891ae08745Sheppo 			ldcp->ldc_id, ibnd_desc->hdr.tag.vio_subtype);
62901ae08745Sheppo 	}
62911ae08745Sheppo 
62921ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
62931ae08745Sheppo }
62941ae08745Sheppo 
62951ae08745Sheppo static void
62961ae08745Sheppo vsw_process_err_pkt(vsw_ldc_t *ldcp, void *epkt, vio_msg_tag_t tag)
62971ae08745Sheppo {
62981ae08745Sheppo 	_NOTE(ARGUNUSED(epkt))
62991ae08745Sheppo 
63001ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
63011ae08745Sheppo 	uint16_t	env = tag.vio_subtype_env;
63021ae08745Sheppo 
63031ae08745Sheppo 	D1(vswp, "%s (%lld): enter\n", __func__, ldcp->ldc_id);
63041ae08745Sheppo 
63051ae08745Sheppo 	/*
63061ae08745Sheppo 	 * Error vio_subtypes have yet to be defined. So for
63071ae08745Sheppo 	 * the moment we can't do anything.
63081ae08745Sheppo 	 */
63091ae08745Sheppo 	D2(vswp, "%s: (%x) vio_subtype env", __func__, env);
63101ae08745Sheppo 
63111ae08745Sheppo 	D1(vswp, "%s (%lld): exit\n", __func__, ldcp->ldc_id);
63121ae08745Sheppo }
63131ae08745Sheppo 
63141ae08745Sheppo /*
63151ae08745Sheppo  * Switch the given ethernet frame when operating in layer 2 mode.
63161ae08745Sheppo  *
63171ae08745Sheppo  * vswp: pointer to the vsw instance
63181ae08745Sheppo  * mp: pointer to chain of ethernet frame(s) to be switched
63191ae08745Sheppo  * caller: identifies the source of this frame as:
63201ae08745Sheppo  * 		1. VSW_VNETPORT - a vsw port (connected to a vnet).
63211ae08745Sheppo  *		2. VSW_PHYSDEV - the physical ethernet device
63221ae08745Sheppo  *		3. VSW_LOCALDEV - vsw configured as a virtual interface
63231ae08745Sheppo  * arg: argument provided by the caller.
63241ae08745Sheppo  *		1. for VNETPORT - pointer to the corresponding vsw_port_t.
63251ae08745Sheppo  *		2. for PHYSDEV - NULL
63261ae08745Sheppo  *		3. for LOCALDEV - pointer to to this vsw_t(self)
63271ae08745Sheppo  */
63281ae08745Sheppo void
63291ae08745Sheppo vsw_switch_l2_frame(vsw_t *vswp, mblk_t *mp, int caller,
63301ae08745Sheppo 			vsw_port_t *arg, mac_resource_handle_t mrh)
63311ae08745Sheppo {
63321ae08745Sheppo 	struct ether_header	*ehp;
63331ae08745Sheppo 	vsw_port_t		*port = NULL;
63341ae08745Sheppo 	mblk_t			*bp, *ret_m;
63351ae08745Sheppo 	mblk_t			*nmp = NULL;
63361ae08745Sheppo 	vsw_port_list_t		*plist = &vswp->plist;
63371ae08745Sheppo 
63381ae08745Sheppo 	D1(vswp, "%s: enter (caller %d)", __func__, caller);
63391ae08745Sheppo 
63401ae08745Sheppo 	/*
63411ae08745Sheppo 	 * PERF: rather than breaking up the chain here, scan it
63421ae08745Sheppo 	 * to find all mblks heading to same destination and then
63431ae08745Sheppo 	 * pass that sub-chain to the lower transmit functions.
63441ae08745Sheppo 	 */
63451ae08745Sheppo 
63461ae08745Sheppo 	/* process the chain of packets */
63471ae08745Sheppo 	bp = mp;
63481ae08745Sheppo 	while (bp) {
63491ae08745Sheppo 		mp = bp;
63501ae08745Sheppo 		bp = bp->b_next;
63511ae08745Sheppo 		mp->b_next = mp->b_prev = NULL;
63521ae08745Sheppo 		ehp = (struct ether_header *)mp->b_rptr;
63531ae08745Sheppo 
63541ae08745Sheppo 		D2(vswp, "%s: mblk data buffer %lld : actual data size %lld",
63551ae08745Sheppo 			__func__, MBLKSIZE(mp), MBLKL(mp));
63561ae08745Sheppo 
63571ae08745Sheppo 		READ_ENTER(&vswp->if_lockrw);
63581ae08745Sheppo 		if (ether_cmp(&ehp->ether_dhost, &vswp->if_addr) == 0) {
63591ae08745Sheppo 			/*
63601ae08745Sheppo 			 * If destination is VSW_LOCALDEV (vsw as an eth
63611ae08745Sheppo 			 * interface) and if the device is up & running,
63621ae08745Sheppo 			 * send the packet up the stack on this host.
63631ae08745Sheppo 			 * If the virtual interface is down, drop the packet.
63641ae08745Sheppo 			 */
63651ae08745Sheppo 			if (caller != VSW_LOCALDEV) {
63661ae08745Sheppo 				if (vswp->if_state & VSW_IF_UP) {
63671ae08745Sheppo 					RW_EXIT(&vswp->if_lockrw);
6368ba2e4443Sseb 					mac_rx(vswp->if_mh, mrh, mp);
63691ae08745Sheppo 				} else {
63701ae08745Sheppo 					RW_EXIT(&vswp->if_lockrw);
63711ae08745Sheppo 					/* Interface down, drop pkt */
63721ae08745Sheppo 					freemsg(mp);
63731ae08745Sheppo 				}
63741ae08745Sheppo 			} else {
63751ae08745Sheppo 				RW_EXIT(&vswp->if_lockrw);
63761ae08745Sheppo 				freemsg(mp);
63771ae08745Sheppo 			}
63781ae08745Sheppo 			continue;
63791ae08745Sheppo 		}
63801ae08745Sheppo 		RW_EXIT(&vswp->if_lockrw);
63811ae08745Sheppo 
63821ae08745Sheppo 		READ_ENTER(&plist->lockrw);
63831ae08745Sheppo 		port = vsw_lookup_fdb(vswp, ehp);
63841ae08745Sheppo 		if (port) {
63851ae08745Sheppo 			/*
63861ae08745Sheppo 			 * Mark the port as in-use.
63871ae08745Sheppo 			 */
63881ae08745Sheppo 			mutex_enter(&port->ref_lock);
63891ae08745Sheppo 			port->ref_cnt++;
63901ae08745Sheppo 			mutex_exit(&port->ref_lock);
63911ae08745Sheppo 			RW_EXIT(&plist->lockrw);
63921ae08745Sheppo 
63931ae08745Sheppo 			/*
63941ae08745Sheppo 			 * If plumbed and in promisc mode then copy msg
63951ae08745Sheppo 			 * and send up the stack.
63961ae08745Sheppo 			 */
63971ae08745Sheppo 			READ_ENTER(&vswp->if_lockrw);
63981ae08745Sheppo 			if (VSW_U_P(vswp->if_state)) {
63991ae08745Sheppo 				RW_EXIT(&vswp->if_lockrw);
64001ae08745Sheppo 				nmp = copymsg(mp);
64011ae08745Sheppo 				if (nmp)
6402ba2e4443Sseb 					mac_rx(vswp->if_mh, mrh, nmp);
64031ae08745Sheppo 			} else {
64041ae08745Sheppo 				RW_EXIT(&vswp->if_lockrw);
64051ae08745Sheppo 			}
64061ae08745Sheppo 
64071ae08745Sheppo 			/*
64081ae08745Sheppo 			 * If the destination is in FDB, the packet
64091ae08745Sheppo 			 * should be forwarded to the correponding
64101ae08745Sheppo 			 * vsw_port (connected to a vnet device -
64111ae08745Sheppo 			 * VSW_VNETPORT)
64121ae08745Sheppo 			 */
64131ae08745Sheppo 			(void) vsw_portsend(port, mp);
64141ae08745Sheppo 
64151ae08745Sheppo 			/*
64161ae08745Sheppo 			 * Decrement use count in port and check if
64171ae08745Sheppo 			 * should wake delete thread.
64181ae08745Sheppo 			 */
64191ae08745Sheppo 			mutex_enter(&port->ref_lock);
64201ae08745Sheppo 			port->ref_cnt--;
64211ae08745Sheppo 			if (port->ref_cnt == 0)
64221ae08745Sheppo 				cv_signal(&port->ref_cv);
64231ae08745Sheppo 			mutex_exit(&port->ref_lock);
64241ae08745Sheppo 		} else {
64251ae08745Sheppo 			RW_EXIT(&plist->lockrw);
64261ae08745Sheppo 			/*
64271ae08745Sheppo 			 * Destination not in FDB.
64281ae08745Sheppo 			 *
64291ae08745Sheppo 			 * If the destination is broadcast or
64301ae08745Sheppo 			 * multicast forward the packet to all
64311ae08745Sheppo 			 * (VNETPORTs, PHYSDEV, LOCALDEV),
64321ae08745Sheppo 			 * except the caller.
64331ae08745Sheppo 			 */
64341ae08745Sheppo 			if (IS_BROADCAST(ehp)) {
64351ae08745Sheppo 				D3(vswp, "%s: BROADCAST pkt", __func__);
64361ae08745Sheppo 				(void) vsw_forward_all(vswp, mp,
64371ae08745Sheppo 								caller, arg);
64381ae08745Sheppo 			} else if (IS_MULTICAST(ehp)) {
64391ae08745Sheppo 				D3(vswp, "%s: MULTICAST pkt", __func__);
64401ae08745Sheppo 				(void) vsw_forward_grp(vswp, mp,
64411ae08745Sheppo 							caller, arg);
64421ae08745Sheppo 			} else {
64431ae08745Sheppo 				/*
64441ae08745Sheppo 				 * If the destination is unicast, and came
64451ae08745Sheppo 				 * from either a logical network device or
64461ae08745Sheppo 				 * the switch itself when it is plumbed, then
64471ae08745Sheppo 				 * send it out on the physical device and also
64481ae08745Sheppo 				 * up the stack if the logical interface is
64491ae08745Sheppo 				 * in promiscious mode.
64501ae08745Sheppo 				 *
64511ae08745Sheppo 				 * NOTE:  The assumption here is that if we
64521ae08745Sheppo 				 * cannot find the destination in our fdb, its
64531ae08745Sheppo 				 * a unicast address, and came from either a
64541ae08745Sheppo 				 * vnet or down the stack (when plumbed) it
64551ae08745Sheppo 				 * must be destinded for an ethernet device
64561ae08745Sheppo 				 * outside our ldoms.
64571ae08745Sheppo 				 */
64581ae08745Sheppo 				if (caller == VSW_VNETPORT) {
64591ae08745Sheppo 					READ_ENTER(&vswp->if_lockrw);
64601ae08745Sheppo 					if (VSW_U_P(vswp->if_state)) {
64611ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
64621ae08745Sheppo 						nmp = copymsg(mp);
64631ae08745Sheppo 						if (nmp)
6464ba2e4443Sseb 							mac_rx(vswp->if_mh,
64651ae08745Sheppo 								mrh, nmp);
64661ae08745Sheppo 					} else {
64671ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
64681ae08745Sheppo 					}
64691ae08745Sheppo 					if ((ret_m = vsw_tx_msg(vswp, mp))
64701ae08745Sheppo 								!= NULL) {
64711ae08745Sheppo 						DERR(vswp, "%s: drop mblks to "
64721ae08745Sheppo 							"phys dev", __func__);
64731ae08745Sheppo 						freemsg(ret_m);
64741ae08745Sheppo 					}
64751ae08745Sheppo 
64761ae08745Sheppo 				} else if (caller == VSW_PHYSDEV) {
64771ae08745Sheppo 					/*
64781ae08745Sheppo 					 * Pkt seen because card in promisc
64791ae08745Sheppo 					 * mode. Send up stack if plumbed in
64801ae08745Sheppo 					 * promisc mode, else drop it.
64811ae08745Sheppo 					 */
64821ae08745Sheppo 					READ_ENTER(&vswp->if_lockrw);
64831ae08745Sheppo 					if (VSW_U_P(vswp->if_state)) {
64841ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
6485ba2e4443Sseb 						mac_rx(vswp->if_mh, mrh, mp);
64861ae08745Sheppo 					} else {
64871ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
64881ae08745Sheppo 						freemsg(mp);
64891ae08745Sheppo 					}
64901ae08745Sheppo 
64911ae08745Sheppo 				} else if (caller == VSW_LOCALDEV) {
64921ae08745Sheppo 					/*
64931ae08745Sheppo 					 * Pkt came down the stack, send out
64941ae08745Sheppo 					 * over physical device.
64951ae08745Sheppo 					 */
64961ae08745Sheppo 					if ((ret_m = vsw_tx_msg(vswp, mp))
64971ae08745Sheppo 								!= NULL) {
64981ae08745Sheppo 						DERR(vswp, "%s: drop mblks to "
64991ae08745Sheppo 							"phys dev", __func__);
65001ae08745Sheppo 						freemsg(ret_m);
65011ae08745Sheppo 					}
65021ae08745Sheppo 				}
65031ae08745Sheppo 			}
65041ae08745Sheppo 		}
65051ae08745Sheppo 	}
65061ae08745Sheppo 	D1(vswp, "%s: exit\n", __func__);
65071ae08745Sheppo }
65081ae08745Sheppo 
65091ae08745Sheppo /*
65101ae08745Sheppo  * Switch ethernet frame when in layer 3 mode (i.e. using IP
65111ae08745Sheppo  * layer to do the routing).
65121ae08745Sheppo  *
65131ae08745Sheppo  * There is a large amount of overlap between this function and
65141ae08745Sheppo  * vsw_switch_l2_frame. At some stage we need to revisit and refactor
65151ae08745Sheppo  * both these functions.
65161ae08745Sheppo  */
65171ae08745Sheppo void
65181ae08745Sheppo vsw_switch_l3_frame(vsw_t *vswp, mblk_t *mp, int caller,
65191ae08745Sheppo 			vsw_port_t *arg, mac_resource_handle_t mrh)
65201ae08745Sheppo {
65211ae08745Sheppo 	struct ether_header	*ehp;
65221ae08745Sheppo 	vsw_port_t		*port = NULL;
65231ae08745Sheppo 	mblk_t			*bp = NULL;
65241ae08745Sheppo 	vsw_port_list_t		*plist = &vswp->plist;
65251ae08745Sheppo 
65261ae08745Sheppo 	D1(vswp, "%s: enter (caller %d)", __func__, caller);
65271ae08745Sheppo 
65281ae08745Sheppo 	/*
65291ae08745Sheppo 	 * In layer 3 mode should only ever be switching packets
65301ae08745Sheppo 	 * between IP layer and vnet devices. So make sure thats
65311ae08745Sheppo 	 * who is invoking us.
65321ae08745Sheppo 	 */
65331ae08745Sheppo 	if ((caller != VSW_LOCALDEV) && (caller != VSW_VNETPORT)) {
65341ae08745Sheppo 		DERR(vswp, "%s: unexpected caller (%d)", __func__, caller);
65351ae08745Sheppo 		freemsgchain(mp);
65361ae08745Sheppo 		return;
65371ae08745Sheppo 	}
65381ae08745Sheppo 
65391ae08745Sheppo 	/* process the chain of packets */
65401ae08745Sheppo 	bp = mp;
65411ae08745Sheppo 	while (bp) {
65421ae08745Sheppo 		mp = bp;
65431ae08745Sheppo 		bp = bp->b_next;
65441ae08745Sheppo 		mp->b_next = mp->b_prev = NULL;
65451ae08745Sheppo 		ehp = (struct ether_header *)mp->b_rptr;
65461ae08745Sheppo 
65471ae08745Sheppo 		D2(vswp, "%s: mblk data buffer %lld : actual data size %lld",
65481ae08745Sheppo 			__func__, MBLKSIZE(mp), MBLKL(mp));
65491ae08745Sheppo 
65501ae08745Sheppo 		READ_ENTER(&plist->lockrw);
65511ae08745Sheppo 		port = vsw_lookup_fdb(vswp, ehp);
65521ae08745Sheppo 		if (port) {
65531ae08745Sheppo 			/*
65541ae08745Sheppo 			 * Mark port as in-use.
65551ae08745Sheppo 			 */
65561ae08745Sheppo 			mutex_enter(&port->ref_lock);
65571ae08745Sheppo 			port->ref_cnt++;
65581ae08745Sheppo 			mutex_exit(&port->ref_lock);
65591ae08745Sheppo 			RW_EXIT(&plist->lockrw);
65601ae08745Sheppo 
65611ae08745Sheppo 			D2(vswp, "%s: sending to target port", __func__);
65621ae08745Sheppo 			(void) vsw_portsend(port, mp);
65631ae08745Sheppo 
65641ae08745Sheppo 			/*
65651ae08745Sheppo 			 * Finished with port so decrement ref count and
65661ae08745Sheppo 			 * check if should wake delete thread.
65671ae08745Sheppo 			 */
65681ae08745Sheppo 			mutex_enter(&port->ref_lock);
65691ae08745Sheppo 			port->ref_cnt--;
65701ae08745Sheppo 			if (port->ref_cnt == 0)
65711ae08745Sheppo 				cv_signal(&port->ref_cv);
65721ae08745Sheppo 			mutex_exit(&port->ref_lock);
65731ae08745Sheppo 		} else {
65741ae08745Sheppo 			RW_EXIT(&plist->lockrw);
65751ae08745Sheppo 			/*
65761ae08745Sheppo 			 * Destination not in FDB
65771ae08745Sheppo 			 *
65781ae08745Sheppo 			 * If the destination is broadcast or
65791ae08745Sheppo 			 * multicast forward the packet to all
65801ae08745Sheppo 			 * (VNETPORTs, PHYSDEV, LOCALDEV),
65811ae08745Sheppo 			 * except the caller.
65821ae08745Sheppo 			 */
65831ae08745Sheppo 			if (IS_BROADCAST(ehp)) {
65841ae08745Sheppo 				D2(vswp, "%s: BROADCAST pkt", __func__);
65851ae08745Sheppo 				(void) vsw_forward_all(vswp, mp,
65861ae08745Sheppo 								caller, arg);
65871ae08745Sheppo 			} else if (IS_MULTICAST(ehp)) {
65881ae08745Sheppo 				D2(vswp, "%s: MULTICAST pkt", __func__);
65891ae08745Sheppo 				(void) vsw_forward_grp(vswp, mp,
65901ae08745Sheppo 							caller, arg);
65911ae08745Sheppo 			} else {
65921ae08745Sheppo 				/*
65931ae08745Sheppo 				 * Unicast pkt from vnet that we don't have
65941ae08745Sheppo 				 * an FDB entry for, so must be destinded for
65951ae08745Sheppo 				 * the outside world. Attempt to send up to the
65961ae08745Sheppo 				 * IP layer to allow it to deal with it.
65971ae08745Sheppo 				 */
65981ae08745Sheppo 				if (caller == VSW_VNETPORT) {
65991ae08745Sheppo 					READ_ENTER(&vswp->if_lockrw);
66001ae08745Sheppo 					if (vswp->if_state & VSW_IF_UP) {
66011ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
66021ae08745Sheppo 						D2(vswp, "%s: sending up",
66031ae08745Sheppo 							__func__);
6604ba2e4443Sseb 						mac_rx(vswp->if_mh, mrh, mp);
66051ae08745Sheppo 					} else {
66061ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
66071ae08745Sheppo 						/* Interface down, drop pkt */
66081ae08745Sheppo 						D2(vswp, "%s I/F down",
66091ae08745Sheppo 								__func__);
66101ae08745Sheppo 						freemsg(mp);
66111ae08745Sheppo 					}
66121ae08745Sheppo 				}
66131ae08745Sheppo 			}
66141ae08745Sheppo 		}
66151ae08745Sheppo 	}
66161ae08745Sheppo 
66171ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
66181ae08745Sheppo }
66191ae08745Sheppo 
66201ae08745Sheppo /*
66211ae08745Sheppo  * Forward the ethernet frame to all ports (VNETPORTs, PHYSDEV, LOCALDEV),
66221ae08745Sheppo  * except the caller (port on which frame arrived).
66231ae08745Sheppo  */
66241ae08745Sheppo static int
66251ae08745Sheppo vsw_forward_all(vsw_t *vswp, mblk_t *mp, int caller, vsw_port_t *arg)
66261ae08745Sheppo {
66271ae08745Sheppo 	vsw_port_list_t	*plist = &vswp->plist;
66281ae08745Sheppo 	vsw_port_t	*portp;
66291ae08745Sheppo 	mblk_t		*nmp = NULL;
66301ae08745Sheppo 	mblk_t		*ret_m = NULL;
66311ae08745Sheppo 	int		skip_port = 0;
66321ae08745Sheppo 
66331ae08745Sheppo 	D1(vswp, "vsw_forward_all: enter\n");
66341ae08745Sheppo 
66351ae08745Sheppo 	/*
66361ae08745Sheppo 	 * Broadcast message from inside ldoms so send to outside
66371ae08745Sheppo 	 * world if in either of layer 2 modes.
66381ae08745Sheppo 	 */
66391ae08745Sheppo 	if (((vswp->smode[vswp->smode_idx] == VSW_LAYER2) ||
66401ae08745Sheppo 		(vswp->smode[vswp->smode_idx] == VSW_LAYER2_PROMISC)) &&
66411ae08745Sheppo 		((caller == VSW_LOCALDEV) || (caller == VSW_VNETPORT))) {
66421ae08745Sheppo 
66431ae08745Sheppo 		nmp = dupmsg(mp);
66441ae08745Sheppo 		if (nmp) {
66451ae08745Sheppo 			if ((ret_m = vsw_tx_msg(vswp, nmp)) != NULL) {
66461ae08745Sheppo 				DERR(vswp, "%s: dropping pkt(s) "
66471ae08745Sheppo 				"consisting of %ld bytes of data for"
66481ae08745Sheppo 				" physical device", __func__, MBLKL(ret_m));
66491ae08745Sheppo 			freemsg(ret_m);
66501ae08745Sheppo 			}
66511ae08745Sheppo 		}
66521ae08745Sheppo 	}
66531ae08745Sheppo 
66541ae08745Sheppo 	if (caller == VSW_VNETPORT)
66551ae08745Sheppo 		skip_port = 1;
66561ae08745Sheppo 
66571ae08745Sheppo 	/*
66581ae08745Sheppo 	 * Broadcast message from other vnet (layer 2 or 3) or outside
66591ae08745Sheppo 	 * world (layer 2 only), send up stack if plumbed.
66601ae08745Sheppo 	 */
66611ae08745Sheppo 	if ((caller == VSW_PHYSDEV) || (caller == VSW_VNETPORT)) {
66621ae08745Sheppo 		READ_ENTER(&vswp->if_lockrw);
66631ae08745Sheppo 		if (vswp->if_state & VSW_IF_UP) {
66641ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
66651ae08745Sheppo 			nmp = copymsg(mp);
66661ae08745Sheppo 			if (nmp)
6667ba2e4443Sseb 				mac_rx(vswp->if_mh, NULL, nmp);
66681ae08745Sheppo 		} else {
66691ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
66701ae08745Sheppo 		}
66711ae08745Sheppo 	}
66721ae08745Sheppo 
66731ae08745Sheppo 	/* send it to all VNETPORTs */
66741ae08745Sheppo 	READ_ENTER(&plist->lockrw);
66751ae08745Sheppo 	for (portp = plist->head; portp != NULL; portp = portp->p_next) {
66761ae08745Sheppo 		D2(vswp, "vsw_forward_all: port %d", portp->p_instance);
66771ae08745Sheppo 		/*
66781ae08745Sheppo 		 * Caution ! - don't reorder these two checks as arg
66791ae08745Sheppo 		 * will be NULL if the caller is PHYSDEV. skip_port is
66801ae08745Sheppo 		 * only set if caller is VNETPORT.
66811ae08745Sheppo 		 */
66821ae08745Sheppo 		if ((skip_port) && (portp == arg))
66831ae08745Sheppo 			continue;
66841ae08745Sheppo 		else {
66851ae08745Sheppo 			nmp = dupmsg(mp);
66861ae08745Sheppo 			if (nmp) {
66871ae08745Sheppo 				(void) vsw_portsend(portp, nmp);
66881ae08745Sheppo 			} else {
66891ae08745Sheppo 				DERR(vswp, "vsw_forward_all: nmp NULL");
66901ae08745Sheppo 			}
66911ae08745Sheppo 		}
66921ae08745Sheppo 	}
66931ae08745Sheppo 	RW_EXIT(&plist->lockrw);
66941ae08745Sheppo 
66951ae08745Sheppo 	freemsg(mp);
66961ae08745Sheppo 
66971ae08745Sheppo 	D1(vswp, "vsw_forward_all: exit\n");
66981ae08745Sheppo 	return (0);
66991ae08745Sheppo }
67001ae08745Sheppo 
67011ae08745Sheppo /*
67021ae08745Sheppo  * Forward pkts to any devices or interfaces which have registered
67031ae08745Sheppo  * an interest in them (i.e. multicast groups).
67041ae08745Sheppo  */
67051ae08745Sheppo static int
67061ae08745Sheppo vsw_forward_grp(vsw_t *vswp, mblk_t *mp, int caller, vsw_port_t *arg)
67071ae08745Sheppo {
67081ae08745Sheppo 	struct ether_header	*ehp = (struct ether_header *)mp->b_rptr;
67091ae08745Sheppo 	mfdb_ent_t		*entp = NULL;
67101ae08745Sheppo 	mfdb_ent_t		*tpp = NULL;
67111ae08745Sheppo 	vsw_port_t 		*port;
67121ae08745Sheppo 	uint64_t		key = 0;
67131ae08745Sheppo 	mblk_t			*nmp = NULL;
67141ae08745Sheppo 	mblk_t			*ret_m = NULL;
67151ae08745Sheppo 	boolean_t		check_if = B_TRUE;
67161ae08745Sheppo 
67171ae08745Sheppo 	/*
67181ae08745Sheppo 	 * Convert address to hash table key
67191ae08745Sheppo 	 */
67201ae08745Sheppo 	KEY_HASH(key, ehp->ether_dhost);
67211ae08745Sheppo 
67221ae08745Sheppo 	D1(vswp, "%s: key 0x%llx", __func__, key);
67231ae08745Sheppo 
67241ae08745Sheppo 	/*
67251ae08745Sheppo 	 * If pkt came from either a vnet or down the stack (if we are
67261ae08745Sheppo 	 * plumbed) and we are in layer 2 mode, then we send the pkt out
67271ae08745Sheppo 	 * over the physical adapter, and then check to see if any other
67281ae08745Sheppo 	 * vnets are interested in it.
67291ae08745Sheppo 	 */
67301ae08745Sheppo 	if (((vswp->smode[vswp->smode_idx] == VSW_LAYER2) ||
67311ae08745Sheppo 		(vswp->smode[vswp->smode_idx] == VSW_LAYER2_PROMISC)) &&
67321ae08745Sheppo 		((caller == VSW_VNETPORT) || (caller == VSW_LOCALDEV))) {
67331ae08745Sheppo 		nmp = dupmsg(mp);
67341ae08745Sheppo 		if (nmp) {
67351ae08745Sheppo 			if ((ret_m = vsw_tx_msg(vswp, nmp)) != NULL) {
67361ae08745Sheppo 				DERR(vswp, "%s: dropping pkt(s) "
67371ae08745Sheppo 					"consisting of %ld bytes of "
67381ae08745Sheppo 					"data for physical device",
67391ae08745Sheppo 					__func__, MBLKL(ret_m));
67401ae08745Sheppo 				freemsg(ret_m);
67411ae08745Sheppo 			}
67421ae08745Sheppo 		}
67431ae08745Sheppo 	}
67441ae08745Sheppo 
67451ae08745Sheppo 	READ_ENTER(&vswp->mfdbrw);
67461ae08745Sheppo 	if (mod_hash_find(vswp->mfdb, (mod_hash_key_t)key,
67471ae08745Sheppo 				(mod_hash_val_t *)&entp) != 0) {
67481ae08745Sheppo 		D3(vswp, "%s: no table entry found for addr 0x%llx",
67491ae08745Sheppo 								__func__, key);
67501ae08745Sheppo 	} else {
67511ae08745Sheppo 		/*
67521ae08745Sheppo 		 * Send to list of devices associated with this address...
67531ae08745Sheppo 		 */
67541ae08745Sheppo 		for (tpp = entp; tpp != NULL; tpp = tpp->nextp) {
67551ae08745Sheppo 
67561ae08745Sheppo 			/* dont send to ourselves */
67571ae08745Sheppo 			if ((caller == VSW_VNETPORT) &&
67581ae08745Sheppo 				(tpp->d_addr == (void *)arg)) {
67591ae08745Sheppo 				port = (vsw_port_t *)tpp->d_addr;
67601ae08745Sheppo 				D3(vswp, "%s: not sending to ourselves"
67611ae08745Sheppo 					" : port %d", __func__,
67621ae08745Sheppo 					port->p_instance);
67631ae08745Sheppo 				continue;
67641ae08745Sheppo 
67651ae08745Sheppo 			} else if ((caller == VSW_LOCALDEV) &&
67661ae08745Sheppo 				(tpp->d_type == VSW_LOCALDEV)) {
67671ae08745Sheppo 				D3(vswp, "%s: not sending back up stack",
67681ae08745Sheppo 					__func__);
67691ae08745Sheppo 				continue;
67701ae08745Sheppo 			}
67711ae08745Sheppo 
67721ae08745Sheppo 			if (tpp->d_type == VSW_VNETPORT) {
67731ae08745Sheppo 				port = (vsw_port_t *)tpp->d_addr;
67741ae08745Sheppo 				D3(vswp, "%s: sending to port %ld for "
67751ae08745Sheppo 					" addr 0x%llx", __func__,
67761ae08745Sheppo 					port->p_instance, key);
67771ae08745Sheppo 
67781ae08745Sheppo 				nmp = dupmsg(mp);
67791ae08745Sheppo 				if (nmp)
67801ae08745Sheppo 					(void) vsw_portsend(port, nmp);
67811ae08745Sheppo 			} else {
67821ae08745Sheppo 				if (vswp->if_state & VSW_IF_UP) {
67831ae08745Sheppo 					nmp = copymsg(mp);
67841ae08745Sheppo 					if (nmp)
6785ba2e4443Sseb 						mac_rx(vswp->if_mh, NULL, nmp);
67861ae08745Sheppo 					check_if = B_FALSE;
67871ae08745Sheppo 					D3(vswp, "%s: sending up stack"
67881ae08745Sheppo 						" for addr 0x%llx", __func__,
67891ae08745Sheppo 						key);
67901ae08745Sheppo 				}
67911ae08745Sheppo 			}
67921ae08745Sheppo 		}
67931ae08745Sheppo 	}
67941ae08745Sheppo 
67951ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
67961ae08745Sheppo 
67971ae08745Sheppo 	/*
67981ae08745Sheppo 	 * If the pkt came from either a vnet or from physical device,
67991ae08745Sheppo 	 * and if we havent already sent the pkt up the stack then we
68001ae08745Sheppo 	 * check now if we can/should (i.e. the interface is plumbed
68011ae08745Sheppo 	 * and in promisc mode).
68021ae08745Sheppo 	 */
68031ae08745Sheppo 	if ((check_if) &&
68041ae08745Sheppo 		((caller == VSW_VNETPORT) || (caller == VSW_PHYSDEV))) {
68051ae08745Sheppo 		READ_ENTER(&vswp->if_lockrw);
68061ae08745Sheppo 		if (VSW_U_P(vswp->if_state)) {
68071ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
68081ae08745Sheppo 			D3(vswp, "%s: (caller %d) finally sending up stack"
68091ae08745Sheppo 				" for addr 0x%llx", __func__, caller, key);
68101ae08745Sheppo 			nmp = copymsg(mp);
68111ae08745Sheppo 			if (nmp)
6812ba2e4443Sseb 				mac_rx(vswp->if_mh, NULL, nmp);
68131ae08745Sheppo 		} else {
68141ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
68151ae08745Sheppo 		}
68161ae08745Sheppo 	}
68171ae08745Sheppo 
68181ae08745Sheppo 	freemsg(mp);
68191ae08745Sheppo 
68201ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
68211ae08745Sheppo 
68221ae08745Sheppo 	return (0);
68231ae08745Sheppo }
68241ae08745Sheppo 
68251ae08745Sheppo /* transmit the packet over the given port */
68261ae08745Sheppo static int
68271ae08745Sheppo vsw_portsend(vsw_port_t *port, mblk_t *mp)
68281ae08745Sheppo {
68291ae08745Sheppo 	vsw_ldc_list_t 	*ldcl = &port->p_ldclist;
68301ae08745Sheppo 	vsw_ldc_t 	*ldcp;
68311ae08745Sheppo 	int		status = 0;
68321ae08745Sheppo 
68331ae08745Sheppo 
68341ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
68351ae08745Sheppo 	/*
68361ae08745Sheppo 	 * Note for now, we have a single channel.
68371ae08745Sheppo 	 */
68381ae08745Sheppo 	ldcp = ldcl->head;
68391ae08745Sheppo 	if (ldcp == NULL) {
68401ae08745Sheppo 		DERR(port->p_vswp, "vsw_portsend: no ldc: dropping packet\n");
68411ae08745Sheppo 		freemsg(mp);
68421ae08745Sheppo 		RW_EXIT(&ldcl->lockrw);
68431ae08745Sheppo 		return (1);
68441ae08745Sheppo 	}
68451ae08745Sheppo 
68461ae08745Sheppo 	/*
68471ae08745Sheppo 	 * Send the message out using the appropriate
68481ae08745Sheppo 	 * transmit function which will free mblock when it
68491ae08745Sheppo 	 * is finished with it.
68501ae08745Sheppo 	 */
68511ae08745Sheppo 	mutex_enter(&port->tx_lock);
68521ae08745Sheppo 	if (port->transmit != NULL)
68531ae08745Sheppo 		status = (*port->transmit)(ldcp, mp);
68541ae08745Sheppo 	else {
68551ae08745Sheppo 		freemsg(mp);
68561ae08745Sheppo 	}
68571ae08745Sheppo 	mutex_exit(&port->tx_lock);
68581ae08745Sheppo 
68591ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
68601ae08745Sheppo 
68611ae08745Sheppo 	return (status);
68621ae08745Sheppo }
68631ae08745Sheppo 
68641ae08745Sheppo /*
68651ae08745Sheppo  * Send packet out via descriptor ring to a logical device.
68661ae08745Sheppo  */
68671ae08745Sheppo static int
68681ae08745Sheppo vsw_dringsend(vsw_ldc_t *ldcp, mblk_t *mp)
68691ae08745Sheppo {
68701ae08745Sheppo 	vio_dring_msg_t		dring_pkt;
68711ae08745Sheppo 	dring_info_t		*dp = NULL;
68721ae08745Sheppo 	vsw_private_desc_t	*priv_desc = NULL;
6873d10e4ef2Snarayan 	vnet_public_desc_t	*pub = NULL;
68741ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
68751ae08745Sheppo 	mblk_t			*bp;
68761ae08745Sheppo 	size_t			n, size;
68771ae08745Sheppo 	caddr_t			bufp;
68781ae08745Sheppo 	int			idx;
68791ae08745Sheppo 	int			status = LDC_TX_SUCCESS;
68801ae08745Sheppo 
68811ae08745Sheppo 	D1(vswp, "%s(%lld): enter\n", __func__, ldcp->ldc_id);
68821ae08745Sheppo 
68831ae08745Sheppo 	/* TODO: make test a macro */
68841ae08745Sheppo 	if ((!(ldcp->lane_out.lstate & VSW_LANE_ACTIVE)) ||
68851ae08745Sheppo 		(ldcp->ldc_status != LDC_UP) || (ldcp->ldc_handle == NULL)) {
68861ae08745Sheppo 		DWARN(vswp, "%s(%lld) status(%d) lstate(0x%llx), dropping "
68871ae08745Sheppo 			"packet\n", __func__, ldcp->ldc_id, ldcp->ldc_status,
68881ae08745Sheppo 			ldcp->lane_out.lstate);
68891ae08745Sheppo 		freemsg(mp);
68901ae08745Sheppo 		return (LDC_TX_FAILURE);
68911ae08745Sheppo 	}
68921ae08745Sheppo 
68931ae08745Sheppo 	/*
68941ae08745Sheppo 	 * Note - using first ring only, this may change
68951ae08745Sheppo 	 * in the future.
68961ae08745Sheppo 	 */
6897445b4c2eSsb155480 	READ_ENTER(&ldcp->lane_out.dlistrw);
68981ae08745Sheppo 	if ((dp = ldcp->lane_out.dringp) == NULL) {
6899445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_out.dlistrw);
69001ae08745Sheppo 		DERR(vswp, "%s(%lld): no dring for outbound lane on"
69011ae08745Sheppo 			" channel %d", __func__, ldcp->ldc_id, ldcp->ldc_id);
69021ae08745Sheppo 		freemsg(mp);
69031ae08745Sheppo 		return (LDC_TX_FAILURE);
69041ae08745Sheppo 	}
69051ae08745Sheppo 
69061ae08745Sheppo 	size = msgsize(mp);
69071ae08745Sheppo 	if (size > (size_t)ETHERMAX) {
6908445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_out.dlistrw);
69091ae08745Sheppo 		DERR(vswp, "%s(%lld) invalid size (%ld)\n", __func__,
69101ae08745Sheppo 		    ldcp->ldc_id, size);
6911d10e4ef2Snarayan 		freemsg(mp);
6912d10e4ef2Snarayan 		return (LDC_TX_FAILURE);
69131ae08745Sheppo 	}
69141ae08745Sheppo 
69151ae08745Sheppo 	/*
69161ae08745Sheppo 	 * Find a free descriptor
69171ae08745Sheppo 	 *
69181ae08745Sheppo 	 * Note: for the moment we are assuming that we will only
69191ae08745Sheppo 	 * have one dring going from the switch to each of its
69201ae08745Sheppo 	 * peers. This may change in the future.
69211ae08745Sheppo 	 */
69221ae08745Sheppo 	if (vsw_dring_find_free_desc(dp, &priv_desc, &idx) != 0) {
6923d10e4ef2Snarayan 		D2(vswp, "%s(%lld): no descriptor available for ring "
69241ae08745Sheppo 			"at 0x%llx", __func__, ldcp->ldc_id, dp);
69251ae08745Sheppo 
69261ae08745Sheppo 		/* nothing more we can do */
69271ae08745Sheppo 		status = LDC_TX_NORESOURCES;
69281ae08745Sheppo 		goto vsw_dringsend_free_exit;
69291ae08745Sheppo 	} else {
69301ae08745Sheppo 		D2(vswp, "%s(%lld): free private descriptor found at pos "
69311ae08745Sheppo 			"%ld addr 0x%llx\n", __func__, ldcp->ldc_id, idx,
69321ae08745Sheppo 			priv_desc);
69331ae08745Sheppo 	}
69341ae08745Sheppo 
69351ae08745Sheppo 	/* copy data into the descriptor */
69361ae08745Sheppo 	bufp = priv_desc->datap;
6937d10e4ef2Snarayan 	bufp += VNET_IPALIGN;
69381ae08745Sheppo 	for (bp = mp, n = 0; bp != NULL; bp = bp->b_cont) {
69391ae08745Sheppo 		n = MBLKL(bp);
69401ae08745Sheppo 		bcopy(bp->b_rptr, bufp, n);
69411ae08745Sheppo 		bufp += n;
69421ae08745Sheppo 	}
69431ae08745Sheppo 
69441ae08745Sheppo 	priv_desc->datalen = (size < (size_t)ETHERMIN) ? ETHERMIN : size;
6945d10e4ef2Snarayan 
6946d10e4ef2Snarayan 	pub = priv_desc->descp;
6947d10e4ef2Snarayan 	pub->nbytes = priv_desc->datalen;
6948d10e4ef2Snarayan 
6949d10e4ef2Snarayan 	mutex_enter(&priv_desc->dstate_lock);
6950d10e4ef2Snarayan 	pub->hdr.dstate = VIO_DESC_READY;
6951d10e4ef2Snarayan 	mutex_exit(&priv_desc->dstate_lock);
69521ae08745Sheppo 
69531ae08745Sheppo 	/*
6954d10e4ef2Snarayan 	 * Determine whether or not we need to send a message to our
6955d10e4ef2Snarayan 	 * peer prompting them to read our newly updated descriptor(s).
69561ae08745Sheppo 	 */
6957d10e4ef2Snarayan 	mutex_enter(&dp->restart_lock);
6958d10e4ef2Snarayan 	if (dp->restart_reqd) {
6959d10e4ef2Snarayan 		dp->restart_reqd = B_FALSE;
6960d10e4ef2Snarayan 		mutex_exit(&dp->restart_lock);
69611ae08745Sheppo 
69621ae08745Sheppo 		/*
69631ae08745Sheppo 		 * Send a vio_dring_msg to peer to prompt them to read
69641ae08745Sheppo 		 * the updated descriptor ring.
69651ae08745Sheppo 		 */
69661ae08745Sheppo 		dring_pkt.tag.vio_msgtype = VIO_TYPE_DATA;
69671ae08745Sheppo 		dring_pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
69681ae08745Sheppo 		dring_pkt.tag.vio_subtype_env = VIO_DRING_DATA;
69691ae08745Sheppo 		dring_pkt.tag.vio_sid = ldcp->local_session;
69701ae08745Sheppo 
69711ae08745Sheppo 		/* Note - for now using first ring */
69721ae08745Sheppo 		dring_pkt.dring_ident = dp->ident;
69731ae08745Sheppo 
6974d10e4ef2Snarayan 		mutex_enter(&ldcp->lane_out.seq_lock);
69751ae08745Sheppo 		dring_pkt.seq_num = ldcp->lane_out.seq_num++;
6976d10e4ef2Snarayan 		mutex_exit(&ldcp->lane_out.seq_lock);
69771ae08745Sheppo 
6978d10e4ef2Snarayan 		/*
6979d10e4ef2Snarayan 		 * If last_ack_recv is -1 then we know we've not
6980d10e4ef2Snarayan 		 * received any ack's yet, so this must be the first
6981d10e4ef2Snarayan 		 * msg sent, so set the start to the begining of the ring.
6982d10e4ef2Snarayan 		 */
6983d10e4ef2Snarayan 		mutex_enter(&dp->dlock);
6984d10e4ef2Snarayan 		if (dp->last_ack_recv == -1) {
6985d10e4ef2Snarayan 			dring_pkt.start_idx = 0;
6986d10e4ef2Snarayan 		} else {
6987d10e4ef2Snarayan 			dring_pkt.start_idx = (dp->last_ack_recv + 1) %
6988d10e4ef2Snarayan 						dp->num_descriptors;
6989d10e4ef2Snarayan 		}
6990d10e4ef2Snarayan 		dring_pkt.end_idx = -1;
6991d10e4ef2Snarayan 		mutex_exit(&dp->dlock);
69921ae08745Sheppo 
69931ae08745Sheppo 		D3(vswp, "%s(%lld): dring 0x%llx : ident 0x%llx\n", __func__,
69941ae08745Sheppo 			ldcp->ldc_id, dp, dring_pkt.dring_ident);
6995d10e4ef2Snarayan 		D3(vswp, "%s(%lld): start %lld : end %lld : seq %lld\n",
6996d10e4ef2Snarayan 			__func__, ldcp->ldc_id, dring_pkt.start_idx,
6997d10e4ef2Snarayan 			dring_pkt.end_idx, dring_pkt.seq_num);
69981ae08745Sheppo 
6999*b071742bSsg70180 		RW_EXIT(&ldcp->lane_out.dlistrw);
7000*b071742bSsg70180 
7001*b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)&dring_pkt,
7002*b071742bSsg70180 					sizeof (vio_dring_msg_t), B_TRUE);
7003*b071742bSsg70180 
7004*b071742bSsg70180 		/* free the message block */
7005*b071742bSsg70180 		freemsg(mp);
7006*b071742bSsg70180 		return (status);
7007*b071742bSsg70180 
7008d10e4ef2Snarayan 	} else {
7009d10e4ef2Snarayan 		mutex_exit(&dp->restart_lock);
7010d10e4ef2Snarayan 		D2(vswp, "%s(%lld): updating descp %d", __func__,
7011d10e4ef2Snarayan 			ldcp->ldc_id, idx);
7012d10e4ef2Snarayan 	}
70131ae08745Sheppo 
70141ae08745Sheppo vsw_dringsend_free_exit:
70151ae08745Sheppo 
7016445b4c2eSsb155480 	RW_EXIT(&ldcp->lane_out.dlistrw);
7017445b4c2eSsb155480 
70181ae08745Sheppo 	/* free the message block */
70191ae08745Sheppo 	freemsg(mp);
70201ae08745Sheppo 
70211ae08745Sheppo 	D1(vswp, "%s(%lld): exit\n", __func__, ldcp->ldc_id);
70221ae08745Sheppo 	return (status);
70231ae08745Sheppo }
70241ae08745Sheppo 
70251ae08745Sheppo /*
70261ae08745Sheppo  * Send an in-band descriptor message over ldc.
70271ae08745Sheppo  */
70281ae08745Sheppo static int
70291ae08745Sheppo vsw_descrsend(vsw_ldc_t *ldcp, mblk_t *mp)
70301ae08745Sheppo {
70311ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
7032445b4c2eSsb155480 	vnet_ibnd_desc_t	ibnd_msg;
70331ae08745Sheppo 	vsw_private_desc_t	*priv_desc = NULL;
70341ae08745Sheppo 	dring_info_t		*dp = NULL;
70351ae08745Sheppo 	size_t			n, size = 0;
70361ae08745Sheppo 	caddr_t			bufp;
70371ae08745Sheppo 	mblk_t			*bp;
70381ae08745Sheppo 	int			idx, i;
70391ae08745Sheppo 	int			status = LDC_TX_SUCCESS;
70401ae08745Sheppo 	static int		warn_msg = 1;
70411ae08745Sheppo 
70421ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
70431ae08745Sheppo 
70441ae08745Sheppo 	ASSERT(mp != NULL);
70451ae08745Sheppo 
70461ae08745Sheppo 	if ((!(ldcp->lane_out.lstate & VSW_LANE_ACTIVE)) ||
70471ae08745Sheppo 		(ldcp->ldc_status != LDC_UP) || (ldcp->ldc_handle == NULL)) {
70481ae08745Sheppo 		DERR(vswp, "%s(%lld) status(%d) state (0x%llx), dropping pkt",
70491ae08745Sheppo 			__func__, ldcp->ldc_id, ldcp->ldc_status,
70501ae08745Sheppo 			ldcp->lane_out.lstate);
70511ae08745Sheppo 		freemsg(mp);
70521ae08745Sheppo 		return (LDC_TX_FAILURE);
70531ae08745Sheppo 	}
70541ae08745Sheppo 
70551ae08745Sheppo 	/*
70561ae08745Sheppo 	 * only expect single dring to exist, which we use
70571ae08745Sheppo 	 * as an internal buffer, rather than a transfer channel.
70581ae08745Sheppo 	 */
7059445b4c2eSsb155480 	READ_ENTER(&ldcp->lane_out.dlistrw);
70601ae08745Sheppo 	if ((dp = ldcp->lane_out.dringp) == NULL) {
70611ae08745Sheppo 		DERR(vswp, "%s(%lld): no dring for outbound lane",
70621ae08745Sheppo 			__func__, ldcp->ldc_id);
70631ae08745Sheppo 		DERR(vswp, "%s(%lld) status(%d) state (0x%llx)",
70641ae08745Sheppo 			__func__, ldcp->ldc_id, ldcp->ldc_status,
70651ae08745Sheppo 			ldcp->lane_out.lstate);
7066445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_out.dlistrw);
70671ae08745Sheppo 		freemsg(mp);
70681ae08745Sheppo 		return (LDC_TX_FAILURE);
70691ae08745Sheppo 	}
70701ae08745Sheppo 
70711ae08745Sheppo 	size = msgsize(mp);
70721ae08745Sheppo 	if (size > (size_t)ETHERMAX) {
7073*b071742bSsg70180 		RW_EXIT(&ldcp->lane_out.dlistrw);
70741ae08745Sheppo 		DERR(vswp, "%s(%lld) invalid size (%ld)\n", __func__,
70751ae08745Sheppo 		    ldcp->ldc_id, size);
7076d10e4ef2Snarayan 		freemsg(mp);
7077d10e4ef2Snarayan 		return (LDC_TX_FAILURE);
70781ae08745Sheppo 	}
70791ae08745Sheppo 
70801ae08745Sheppo 	/*
70811ae08745Sheppo 	 * Find a free descriptor in our buffer ring
70821ae08745Sheppo 	 */
70831ae08745Sheppo 	if (vsw_dring_find_free_desc(dp, &priv_desc, &idx) != 0) {
7084*b071742bSsg70180 		RW_EXIT(&ldcp->lane_out.dlistrw);
70851ae08745Sheppo 		if (warn_msg) {
70861ae08745Sheppo 			DERR(vswp, "%s(%lld): no descriptor available for ring "
70871ae08745Sheppo 			"at 0x%llx", __func__, ldcp->ldc_id, dp);
70881ae08745Sheppo 			warn_msg = 0;
70891ae08745Sheppo 		}
70901ae08745Sheppo 
70911ae08745Sheppo 		/* nothing more we can do */
70921ae08745Sheppo 		status = LDC_TX_NORESOURCES;
70931ae08745Sheppo 		goto vsw_descrsend_free_exit;
70941ae08745Sheppo 	} else {
70951ae08745Sheppo 		D2(vswp, "%s(%lld): free private descriptor found at pos "
70961ae08745Sheppo 			"%ld addr 0x%x\n", __func__, ldcp->ldc_id, idx,
70971ae08745Sheppo 			priv_desc);
70981ae08745Sheppo 		warn_msg = 1;
70991ae08745Sheppo 	}
71001ae08745Sheppo 
71011ae08745Sheppo 	/* copy data into the descriptor */
71021ae08745Sheppo 	bufp = priv_desc->datap;
71031ae08745Sheppo 	for (bp = mp, n = 0; bp != NULL; bp = bp->b_cont) {
71041ae08745Sheppo 		n = MBLKL(bp);
71051ae08745Sheppo 		bcopy(bp->b_rptr, bufp, n);
71061ae08745Sheppo 		bufp += n;
71071ae08745Sheppo 	}
71081ae08745Sheppo 
71091ae08745Sheppo 	priv_desc->datalen = (size < (size_t)ETHERMIN) ? ETHERMIN : size;
71101ae08745Sheppo 
71111ae08745Sheppo 	/* create and send the in-band descp msg */
71121ae08745Sheppo 	ibnd_msg.hdr.tag.vio_msgtype = VIO_TYPE_DATA;
71131ae08745Sheppo 	ibnd_msg.hdr.tag.vio_subtype = VIO_SUBTYPE_INFO;
71141ae08745Sheppo 	ibnd_msg.hdr.tag.vio_subtype_env = VIO_DESC_DATA;
71151ae08745Sheppo 	ibnd_msg.hdr.tag.vio_sid = ldcp->local_session;
71161ae08745Sheppo 
7117d10e4ef2Snarayan 	mutex_enter(&ldcp->lane_out.seq_lock);
71181ae08745Sheppo 	ibnd_msg.hdr.seq_num = ldcp->lane_out.seq_num++;
7119d10e4ef2Snarayan 	mutex_exit(&ldcp->lane_out.seq_lock);
71201ae08745Sheppo 
71211ae08745Sheppo 	/*
71221ae08745Sheppo 	 * Copy the mem cookies describing the data from the
71231ae08745Sheppo 	 * private region of the descriptor ring into the inband
71241ae08745Sheppo 	 * descriptor.
71251ae08745Sheppo 	 */
71261ae08745Sheppo 	for (i = 0; i < priv_desc->ncookies; i++) {
71271ae08745Sheppo 		bcopy(&priv_desc->memcookie[i], &ibnd_msg.memcookie[i],
71281ae08745Sheppo 			sizeof (ldc_mem_cookie_t));
71291ae08745Sheppo 	}
71301ae08745Sheppo 
71311ae08745Sheppo 	ibnd_msg.hdr.desc_handle = idx;
71321ae08745Sheppo 	ibnd_msg.ncookies = priv_desc->ncookies;
71331ae08745Sheppo 	ibnd_msg.nbytes = size;
71341ae08745Sheppo 
7135*b071742bSsg70180 	RW_EXIT(&ldcp->lane_out.dlistrw);
7136*b071742bSsg70180 
7137*b071742bSsg70180 	(void) vsw_send_msg(ldcp, (void *)&ibnd_msg,
7138*b071742bSsg70180 			sizeof (vnet_ibnd_desc_t), B_TRUE);
71391ae08745Sheppo 
71401ae08745Sheppo vsw_descrsend_free_exit:
71411ae08745Sheppo 
71421ae08745Sheppo 	/* free the allocated message blocks */
71431ae08745Sheppo 	freemsg(mp);
71441ae08745Sheppo 
71451ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
71461ae08745Sheppo 	return (status);
71471ae08745Sheppo }
71481ae08745Sheppo 
71491ae08745Sheppo static void
71503af08d82Slm66018 vsw_send_ver(void *arg)
71511ae08745Sheppo {
71523af08d82Slm66018 	vsw_ldc_t	*ldcp = (vsw_ldc_t *)arg;
71531ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
71541ae08745Sheppo 	lane_t		*lp = &ldcp->lane_out;
71551ae08745Sheppo 	vio_ver_msg_t	ver_msg;
71561ae08745Sheppo 
71571ae08745Sheppo 	D1(vswp, "%s enter", __func__);
71581ae08745Sheppo 
71591ae08745Sheppo 	ver_msg.tag.vio_msgtype = VIO_TYPE_CTRL;
71601ae08745Sheppo 	ver_msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
71611ae08745Sheppo 	ver_msg.tag.vio_subtype_env = VIO_VER_INFO;
71621ae08745Sheppo 	ver_msg.tag.vio_sid = ldcp->local_session;
71631ae08745Sheppo 
71641ae08745Sheppo 	ver_msg.ver_major = vsw_versions[0].ver_major;
71651ae08745Sheppo 	ver_msg.ver_minor = vsw_versions[0].ver_minor;
71661ae08745Sheppo 	ver_msg.dev_class = VDEV_NETWORK_SWITCH;
71671ae08745Sheppo 
71681ae08745Sheppo 	lp->lstate |= VSW_VER_INFO_SENT;
71691ae08745Sheppo 	lp->ver_major = ver_msg.ver_major;
71701ae08745Sheppo 	lp->ver_minor = ver_msg.ver_minor;
71711ae08745Sheppo 
71721ae08745Sheppo 	DUMP_TAG(ver_msg.tag);
71731ae08745Sheppo 
7174*b071742bSsg70180 	(void) vsw_send_msg(ldcp, &ver_msg, sizeof (vio_ver_msg_t), B_TRUE);
71751ae08745Sheppo 
71761ae08745Sheppo 	D1(vswp, "%s (%d): exit", __func__, ldcp->ldc_id);
71771ae08745Sheppo }
71781ae08745Sheppo 
71791ae08745Sheppo static void
71801ae08745Sheppo vsw_send_attr(vsw_ldc_t *ldcp)
71811ae08745Sheppo {
71821ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
71831ae08745Sheppo 	lane_t			*lp = &ldcp->lane_out;
71841ae08745Sheppo 	vnet_attr_msg_t		attr_msg;
71851ae08745Sheppo 
71861ae08745Sheppo 	D1(vswp, "%s (%ld) enter", __func__, ldcp->ldc_id);
71871ae08745Sheppo 
71881ae08745Sheppo 	/*
71891ae08745Sheppo 	 * Subtype is set to INFO by default
71901ae08745Sheppo 	 */
71911ae08745Sheppo 	attr_msg.tag.vio_msgtype = VIO_TYPE_CTRL;
71921ae08745Sheppo 	attr_msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
71931ae08745Sheppo 	attr_msg.tag.vio_subtype_env = VIO_ATTR_INFO;
71941ae08745Sheppo 	attr_msg.tag.vio_sid = ldcp->local_session;
71951ae08745Sheppo 
71961ae08745Sheppo 	/* payload copied from default settings for lane */
71971ae08745Sheppo 	attr_msg.mtu = lp->mtu;
71981ae08745Sheppo 	attr_msg.addr_type = lp->addr_type;
71991ae08745Sheppo 	attr_msg.xfer_mode = lp->xfer_mode;
72001ae08745Sheppo 	attr_msg.ack_freq = lp->xfer_mode;
72011ae08745Sheppo 
72021ae08745Sheppo 	READ_ENTER(&vswp->if_lockrw);
72031ae08745Sheppo 	bcopy(&(vswp->if_addr), &(attr_msg.addr), ETHERADDRL);
72041ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
72051ae08745Sheppo 
72061ae08745Sheppo 	ldcp->lane_out.lstate |= VSW_ATTR_INFO_SENT;
72071ae08745Sheppo 
72081ae08745Sheppo 	DUMP_TAG(attr_msg.tag);
72091ae08745Sheppo 
7210*b071742bSsg70180 	(void) vsw_send_msg(ldcp, &attr_msg, sizeof (vnet_attr_msg_t), B_TRUE);
72111ae08745Sheppo 
7212*b071742bSsg70180 	D1(vswp, "%s (%ld) exit", __func__, ldcp->ldc_id);
72131ae08745Sheppo }
72141ae08745Sheppo 
72151ae08745Sheppo /*
72161ae08745Sheppo  * Create dring info msg (which also results in the creation of
72171ae08745Sheppo  * a dring).
72181ae08745Sheppo  */
72191ae08745Sheppo static vio_dring_reg_msg_t *
72201ae08745Sheppo vsw_create_dring_info_pkt(vsw_ldc_t *ldcp)
72211ae08745Sheppo {
72221ae08745Sheppo 	vio_dring_reg_msg_t	*mp;
72231ae08745Sheppo 	dring_info_t		*dp;
72241ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
72251ae08745Sheppo 
72261ae08745Sheppo 	D1(vswp, "vsw_create_dring_info_pkt enter\n");
72271ae08745Sheppo 
72281ae08745Sheppo 	/*
72291ae08745Sheppo 	 * If we can't create a dring, obviously no point sending
72301ae08745Sheppo 	 * a message.
72311ae08745Sheppo 	 */
72321ae08745Sheppo 	if ((dp = vsw_create_dring(ldcp)) == NULL)
72331ae08745Sheppo 		return (NULL);
72341ae08745Sheppo 
72351ae08745Sheppo 	mp = kmem_zalloc(sizeof (vio_dring_reg_msg_t), KM_SLEEP);
72361ae08745Sheppo 
72371ae08745Sheppo 	mp->tag.vio_msgtype = VIO_TYPE_CTRL;
72381ae08745Sheppo 	mp->tag.vio_subtype = VIO_SUBTYPE_INFO;
72391ae08745Sheppo 	mp->tag.vio_subtype_env = VIO_DRING_REG;
72401ae08745Sheppo 	mp->tag.vio_sid = ldcp->local_session;
72411ae08745Sheppo 
72421ae08745Sheppo 	/* payload */
72431ae08745Sheppo 	mp->num_descriptors = dp->num_descriptors;
72441ae08745Sheppo 	mp->descriptor_size = dp->descriptor_size;
72451ae08745Sheppo 	mp->options = dp->options;
72461ae08745Sheppo 	mp->ncookies = dp->ncookies;
72471ae08745Sheppo 	bcopy(&dp->cookie[0], &mp->cookie[0], sizeof (ldc_mem_cookie_t));
72481ae08745Sheppo 
72491ae08745Sheppo 	mp->dring_ident = 0;
72501ae08745Sheppo 
72511ae08745Sheppo 	D1(vswp, "vsw_create_dring_info_pkt exit\n");
72521ae08745Sheppo 
72531ae08745Sheppo 	return (mp);
72541ae08745Sheppo }
72551ae08745Sheppo 
72561ae08745Sheppo static void
72571ae08745Sheppo vsw_send_dring_info(vsw_ldc_t *ldcp)
72581ae08745Sheppo {
72591ae08745Sheppo 	vio_dring_reg_msg_t	*dring_msg;
72601ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
72611ae08745Sheppo 
72621ae08745Sheppo 	D1(vswp, "%s: (%ld) enter", __func__, ldcp->ldc_id);
72631ae08745Sheppo 
72641ae08745Sheppo 	dring_msg = vsw_create_dring_info_pkt(ldcp);
72651ae08745Sheppo 	if (dring_msg == NULL) {
726634683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: %s: error creating msg",
726734683adeSsg70180 			vswp->instance, __func__);
72681ae08745Sheppo 		return;
72691ae08745Sheppo 	}
72701ae08745Sheppo 
72711ae08745Sheppo 	ldcp->lane_out.lstate |= VSW_DRING_INFO_SENT;
72721ae08745Sheppo 
72731ae08745Sheppo 	DUMP_TAG_PTR((vio_msg_tag_t *)dring_msg);
72741ae08745Sheppo 
7275*b071742bSsg70180 	(void) vsw_send_msg(ldcp, dring_msg,
7276*b071742bSsg70180 		sizeof (vio_dring_reg_msg_t), B_TRUE);
72771ae08745Sheppo 
72781ae08745Sheppo 	kmem_free(dring_msg, sizeof (vio_dring_reg_msg_t));
72791ae08745Sheppo 
72801ae08745Sheppo 	D1(vswp, "%s: (%ld) exit", __func__, ldcp->ldc_id);
72811ae08745Sheppo }
72821ae08745Sheppo 
72831ae08745Sheppo static void
72841ae08745Sheppo vsw_send_rdx(vsw_ldc_t *ldcp)
72851ae08745Sheppo {
72861ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
72871ae08745Sheppo 	vio_rdx_msg_t	rdx_msg;
72881ae08745Sheppo 
72891ae08745Sheppo 	D1(vswp, "%s (%ld) enter", __func__, ldcp->ldc_id);
72901ae08745Sheppo 
72911ae08745Sheppo 	rdx_msg.tag.vio_msgtype = VIO_TYPE_CTRL;
72921ae08745Sheppo 	rdx_msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
72931ae08745Sheppo 	rdx_msg.tag.vio_subtype_env = VIO_RDX;
72941ae08745Sheppo 	rdx_msg.tag.vio_sid = ldcp->local_session;
72951ae08745Sheppo 
7296*b071742bSsg70180 	ldcp->lane_in.lstate |= VSW_RDX_INFO_SENT;
72971ae08745Sheppo 
72981ae08745Sheppo 	DUMP_TAG(rdx_msg.tag);
72991ae08745Sheppo 
7300*b071742bSsg70180 	(void) vsw_send_msg(ldcp, &rdx_msg, sizeof (vio_rdx_msg_t), B_TRUE);
73011ae08745Sheppo 
73021ae08745Sheppo 	D1(vswp, "%s (%ld) exit", __func__, ldcp->ldc_id);
73031ae08745Sheppo }
73041ae08745Sheppo 
73051ae08745Sheppo /*
73061ae08745Sheppo  * Generic routine to send message out over ldc channel.
7307*b071742bSsg70180  *
7308*b071742bSsg70180  * It is possible that when we attempt to write over the ldc channel
7309*b071742bSsg70180  * that we get notified that it has been reset. Depending on the value
7310*b071742bSsg70180  * of the handle_reset flag we either handle that event here or simply
7311*b071742bSsg70180  * notify the caller that the channel was reset.
73121ae08745Sheppo  */
7313*b071742bSsg70180 static int
7314*b071742bSsg70180 vsw_send_msg(vsw_ldc_t *ldcp, void *msgp, int size, boolean_t handle_reset)
73151ae08745Sheppo {
73161ae08745Sheppo 	int		rv;
73171ae08745Sheppo 	size_t		msglen = size;
73181ae08745Sheppo 	vio_msg_tag_t	*tag = (vio_msg_tag_t *)msgp;
73191ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
73201ae08745Sheppo 
73211ae08745Sheppo 	D1(vswp, "vsw_send_msg (%lld) enter : sending %d bytes",
73221ae08745Sheppo 			ldcp->ldc_id, size);
73231ae08745Sheppo 
73241ae08745Sheppo 	D2(vswp, "send_msg: type 0x%llx", tag->vio_msgtype);
73251ae08745Sheppo 	D2(vswp, "send_msg: stype 0x%llx", tag->vio_subtype);
73261ae08745Sheppo 	D2(vswp, "send_msg: senv 0x%llx", tag->vio_subtype_env);
73271ae08745Sheppo 
73281ae08745Sheppo 	mutex_enter(&ldcp->ldc_txlock);
73291ae08745Sheppo 	do {
73301ae08745Sheppo 		msglen = size;
73311ae08745Sheppo 		rv = ldc_write(ldcp->ldc_handle, (caddr_t)msgp, &msglen);
73321ae08745Sheppo 	} while (rv == EWOULDBLOCK && --vsw_wretries > 0);
73331ae08745Sheppo 
73341ae08745Sheppo 	if ((rv != 0) || (msglen != size)) {
73351ae08745Sheppo 		DERR(vswp, "vsw_send_msg:ldc_write failed: chan(%lld) "
73361ae08745Sheppo 			"rv(%d) size (%d) msglen(%d)\n", ldcp->ldc_id,
73371ae08745Sheppo 			rv, size, msglen);
73381ae08745Sheppo 	}
73393af08d82Slm66018 	mutex_exit(&ldcp->ldc_txlock);
73403af08d82Slm66018 
7341*b071742bSsg70180 	/*
7342*b071742bSsg70180 	 * If channel has been reset we either handle it here or
7343*b071742bSsg70180 	 * simply report back that it has been reset and let caller
7344*b071742bSsg70180 	 * decide what to do.
7345*b071742bSsg70180 	 */
73463af08d82Slm66018 	if (rv == ECONNRESET) {
7347*b071742bSsg70180 		DWARN(vswp, "%s (%lld) channel reset",
7348*b071742bSsg70180 					__func__, ldcp->ldc_id);
7349*b071742bSsg70180 
7350*b071742bSsg70180 		/*
7351*b071742bSsg70180 		 * N.B - must never be holding the dlistrw lock when
7352*b071742bSsg70180 		 * we do a reset of the channel.
7353*b071742bSsg70180 		 */
7354*b071742bSsg70180 		if (handle_reset) {
7355*b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
7356*b071742bSsg70180 		}
73573af08d82Slm66018 	}
73581ae08745Sheppo 
7359*b071742bSsg70180 	return (rv);
73601ae08745Sheppo }
73611ae08745Sheppo 
73621ae08745Sheppo /*
73631ae08745Sheppo  * Add an entry into FDB, for the given mac address and port_id.
73641ae08745Sheppo  * Returns 0 on success, 1 on failure.
73651ae08745Sheppo  *
73661ae08745Sheppo  * Lock protecting FDB must be held by calling process.
73671ae08745Sheppo  */
73681ae08745Sheppo static int
73691ae08745Sheppo vsw_add_fdb(vsw_t *vswp, vsw_port_t *port)
73701ae08745Sheppo {
73711ae08745Sheppo 	uint64_t	addr = 0;
73721ae08745Sheppo 
73731ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
73741ae08745Sheppo 
73751ae08745Sheppo 	KEY_HASH(addr, port->p_macaddr);
73761ae08745Sheppo 
73771ae08745Sheppo 	D2(vswp, "%s: key = 0x%llx", __func__, addr);
73781ae08745Sheppo 
73791ae08745Sheppo 	/*
73801ae08745Sheppo 	 * Note: duplicate keys will be rejected by mod_hash.
73811ae08745Sheppo 	 */
73821ae08745Sheppo 	if (mod_hash_insert(vswp->fdb, (mod_hash_key_t)addr,
73831ae08745Sheppo 				(mod_hash_val_t)port) != 0) {
73841ae08745Sheppo 		DERR(vswp, "%s: unable to add entry into fdb.", __func__);
73851ae08745Sheppo 		return (1);
73861ae08745Sheppo 	}
73871ae08745Sheppo 
73881ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
73891ae08745Sheppo 	return (0);
73901ae08745Sheppo }
73911ae08745Sheppo 
73921ae08745Sheppo /*
73931ae08745Sheppo  * Remove an entry from FDB.
73941ae08745Sheppo  * Returns 0 on success, 1 on failure.
73951ae08745Sheppo  */
73961ae08745Sheppo static int
73971ae08745Sheppo vsw_del_fdb(vsw_t *vswp, vsw_port_t *port)
73981ae08745Sheppo {
73991ae08745Sheppo 	uint64_t	addr = 0;
74001ae08745Sheppo 
74011ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
74021ae08745Sheppo 
74031ae08745Sheppo 	KEY_HASH(addr, port->p_macaddr);
74041ae08745Sheppo 
74051ae08745Sheppo 	D2(vswp, "%s: key = 0x%llx", __func__, addr);
74061ae08745Sheppo 
74071ae08745Sheppo 	(void) mod_hash_destroy(vswp->fdb, (mod_hash_val_t)addr);
74081ae08745Sheppo 
74091ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
74101ae08745Sheppo 
74111ae08745Sheppo 	return (0);
74121ae08745Sheppo }
74131ae08745Sheppo 
74141ae08745Sheppo /*
74151ae08745Sheppo  * Search fdb for a given mac address.
74161ae08745Sheppo  * Returns pointer to the entry if found, else returns NULL.
74171ae08745Sheppo  */
74181ae08745Sheppo static vsw_port_t *
74191ae08745Sheppo vsw_lookup_fdb(vsw_t *vswp, struct ether_header *ehp)
74201ae08745Sheppo {
74211ae08745Sheppo 	uint64_t	key = 0;
74221ae08745Sheppo 	vsw_port_t	*port = NULL;
74231ae08745Sheppo 
74241ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
74251ae08745Sheppo 
74261ae08745Sheppo 	KEY_HASH(key, ehp->ether_dhost);
74271ae08745Sheppo 
74281ae08745Sheppo 	D2(vswp, "%s: key = 0x%llx", __func__, key);
74291ae08745Sheppo 
74301ae08745Sheppo 	if (mod_hash_find(vswp->fdb, (mod_hash_key_t)key,
74311ae08745Sheppo 				(mod_hash_val_t *)&port) != 0) {
743234683adeSsg70180 		D2(vswp, "%s: no port found", __func__);
74331ae08745Sheppo 		return (NULL);
74341ae08745Sheppo 	}
74351ae08745Sheppo 
74361ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
74371ae08745Sheppo 
74381ae08745Sheppo 	return (port);
74391ae08745Sheppo }
74401ae08745Sheppo 
74411ae08745Sheppo /*
74421ae08745Sheppo  * Add or remove multicast address(es).
74431ae08745Sheppo  *
74441ae08745Sheppo  * Returns 0 on success, 1 on failure.
74451ae08745Sheppo  */
74461ae08745Sheppo static int
74471ae08745Sheppo vsw_add_rem_mcst(vnet_mcast_msg_t *mcst_pkt, vsw_port_t *port)
74481ae08745Sheppo {
74491ae08745Sheppo 	mcst_addr_t		*mcst_p = NULL;
74501ae08745Sheppo 	vsw_t			*vswp = port->p_vswp;
74511ae08745Sheppo 	uint64_t		addr = 0x0;
745234683adeSsg70180 	int			i;
74531ae08745Sheppo 
74541ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
74551ae08745Sheppo 
74561ae08745Sheppo 	D2(vswp, "%s: %d addresses", __func__, mcst_pkt->count);
74571ae08745Sheppo 
745834683adeSsg70180 	mutex_enter(&vswp->mac_lock);
745934683adeSsg70180 	if (vswp->mh == NULL) {
746034683adeSsg70180 		mutex_exit(&vswp->mac_lock);
7461e1ebb9ecSlm66018 		return (1);
746234683adeSsg70180 	}
746334683adeSsg70180 	mutex_exit(&vswp->mac_lock);
7464e1ebb9ecSlm66018 
74651ae08745Sheppo 	for (i = 0; i < mcst_pkt->count; i++) {
74661ae08745Sheppo 		/*
74671ae08745Sheppo 		 * Convert address into form that can be used
74681ae08745Sheppo 		 * as hash table key.
74691ae08745Sheppo 		 */
74701ae08745Sheppo 		KEY_HASH(addr, mcst_pkt->mca[i]);
74711ae08745Sheppo 
74721ae08745Sheppo 		/*
74731ae08745Sheppo 		 * Add or delete the specified address/port combination.
74741ae08745Sheppo 		 */
74751ae08745Sheppo 		if (mcst_pkt->set == 0x1) {
74761ae08745Sheppo 			D3(vswp, "%s: adding multicast address 0x%llx for "
74771ae08745Sheppo 				"port %ld", __func__, addr, port->p_instance);
74781ae08745Sheppo 			if (vsw_add_mcst(vswp, VSW_VNETPORT, addr, port) == 0) {
74791ae08745Sheppo 				/*
74801ae08745Sheppo 				 * Update the list of multicast
74811ae08745Sheppo 				 * addresses contained within the
74821ae08745Sheppo 				 * port structure to include this new
74831ae08745Sheppo 				 * one.
74841ae08745Sheppo 				 */
74851ae08745Sheppo 				mcst_p = kmem_alloc(sizeof (mcst_addr_t),
74861ae08745Sheppo 								KM_NOSLEEP);
74871ae08745Sheppo 				if (mcst_p == NULL) {
74881ae08745Sheppo 					DERR(vswp, "%s: unable to alloc mem",
74891ae08745Sheppo 						__func__);
74901ae08745Sheppo 					return (1);
74911ae08745Sheppo 				}
74921ae08745Sheppo 
74931ae08745Sheppo 				mcst_p->nextp = NULL;
74941ae08745Sheppo 				mcst_p->addr = addr;
74951ae08745Sheppo 
74961ae08745Sheppo 				mutex_enter(&port->mca_lock);
74971ae08745Sheppo 				mcst_p->nextp = port->mcap;
74981ae08745Sheppo 				port->mcap = mcst_p;
74991ae08745Sheppo 				mutex_exit(&port->mca_lock);
75001ae08745Sheppo 
75011ae08745Sheppo 				/*
75021ae08745Sheppo 				 * Program the address into HW. If the addr
75031ae08745Sheppo 				 * has already been programmed then the MAC
75041ae08745Sheppo 				 * just increments a ref counter (which is
75051ae08745Sheppo 				 * used when the address is being deleted)
75061ae08745Sheppo 				 */
750734683adeSsg70180 				mutex_enter(&vswp->mac_lock);
750834683adeSsg70180 				if ((vswp->mh == NULL) ||
750934683adeSsg70180 					mac_multicst_add(vswp->mh,
751034683adeSsg70180 						(uchar_t *)&mcst_pkt->mca[i])) {
751134683adeSsg70180 					mutex_exit(&vswp->mac_lock);
751234683adeSsg70180 					cmn_err(CE_WARN, "!vsw%d: unable to "
751334683adeSsg70180 						"add multicast address",
751434683adeSsg70180 						vswp->instance);
7515e1ebb9ecSlm66018 					(void) vsw_del_mcst(vswp, VSW_VNETPORT,
7516e1ebb9ecSlm66018 						addr, port);
7517e1ebb9ecSlm66018 					vsw_del_addr(VSW_VNETPORT, port, addr);
751834683adeSsg70180 					return (1);
7519e1ebb9ecSlm66018 				}
752034683adeSsg70180 				mutex_exit(&vswp->mac_lock);
75211ae08745Sheppo 
75221ae08745Sheppo 			} else {
75231ae08745Sheppo 				DERR(vswp, "%s: error adding multicast "
75241ae08745Sheppo 					"address 0x%llx for port %ld",
75251ae08745Sheppo 					__func__, addr, port->p_instance);
75261ae08745Sheppo 				return (1);
75271ae08745Sheppo 			}
75281ae08745Sheppo 		} else {
75291ae08745Sheppo 			/*
75301ae08745Sheppo 			 * Delete an entry from the multicast hash
75311ae08745Sheppo 			 * table and update the address list
75321ae08745Sheppo 			 * appropriately.
75331ae08745Sheppo 			 */
75341ae08745Sheppo 			if (vsw_del_mcst(vswp, VSW_VNETPORT, addr, port) == 0) {
75351ae08745Sheppo 				D3(vswp, "%s: deleting multicast address "
75361ae08745Sheppo 					"0x%llx for port %ld", __func__, addr,
75371ae08745Sheppo 					port->p_instance);
75381ae08745Sheppo 
75391ae08745Sheppo 				vsw_del_addr(VSW_VNETPORT, port, addr);
75401ae08745Sheppo 
75411ae08745Sheppo 				/*
75421ae08745Sheppo 				 * Remove the address from HW. The address
75431ae08745Sheppo 				 * will actually only be removed once the ref
75441ae08745Sheppo 				 * count within the MAC layer has dropped to
75451ae08745Sheppo 				 * zero. I.e. we can safely call this fn even
75461ae08745Sheppo 				 * if other ports are interested in this
75471ae08745Sheppo 				 * address.
75481ae08745Sheppo 				 */
754934683adeSsg70180 				mutex_enter(&vswp->mac_lock);
755034683adeSsg70180 				if ((vswp->mh == NULL) ||
755134683adeSsg70180 					mac_multicst_remove(vswp->mh,
755234683adeSsg70180 						(uchar_t *)&mcst_pkt->mca[i])) {
755334683adeSsg70180 					mutex_exit(&vswp->mac_lock);
755434683adeSsg70180 					cmn_err(CE_WARN, "!vsw%d: unable to "
755534683adeSsg70180 						"remove multicast address",
755634683adeSsg70180 						vswp->instance);
755734683adeSsg70180 					return (1);
755834683adeSsg70180 				}
755934683adeSsg70180 				mutex_exit(&vswp->mac_lock);
75601ae08745Sheppo 
75611ae08745Sheppo 			} else {
75621ae08745Sheppo 				DERR(vswp, "%s: error deleting multicast "
75631ae08745Sheppo 					"addr 0x%llx for port %ld",
75641ae08745Sheppo 					__func__, addr, port->p_instance);
75651ae08745Sheppo 				return (1);
75661ae08745Sheppo 			}
75671ae08745Sheppo 		}
75681ae08745Sheppo 	}
75691ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
75701ae08745Sheppo 	return (0);
75711ae08745Sheppo }
75721ae08745Sheppo 
75731ae08745Sheppo /*
75741ae08745Sheppo  * Add a new multicast entry.
75751ae08745Sheppo  *
75761ae08745Sheppo  * Search hash table based on address. If match found then
75771ae08745Sheppo  * update associated val (which is chain of ports), otherwise
75781ae08745Sheppo  * create new key/val (addr/port) pair and insert into table.
75791ae08745Sheppo  */
75801ae08745Sheppo static int
75811ae08745Sheppo vsw_add_mcst(vsw_t *vswp, uint8_t devtype, uint64_t addr, void *arg)
75821ae08745Sheppo {
75831ae08745Sheppo 	int		dup = 0;
75841ae08745Sheppo 	int		rv = 0;
75851ae08745Sheppo 	mfdb_ent_t	*ment = NULL;
75861ae08745Sheppo 	mfdb_ent_t	*tmp_ent = NULL;
75871ae08745Sheppo 	mfdb_ent_t	*new_ent = NULL;
75881ae08745Sheppo 	void		*tgt = NULL;
75891ae08745Sheppo 
75901ae08745Sheppo 	if (devtype == VSW_VNETPORT) {
75911ae08745Sheppo 		/*
75921ae08745Sheppo 		 * Being invoked from a vnet.
75931ae08745Sheppo 		 */
75941ae08745Sheppo 		ASSERT(arg != NULL);
75951ae08745Sheppo 		tgt = arg;
75961ae08745Sheppo 		D2(NULL, "%s: port %d : address 0x%llx", __func__,
75971ae08745Sheppo 			((vsw_port_t *)arg)->p_instance, addr);
75981ae08745Sheppo 	} else {
75991ae08745Sheppo 		/*
76001ae08745Sheppo 		 * We are being invoked via the m_multicst mac entry
76011ae08745Sheppo 		 * point.
76021ae08745Sheppo 		 */
76031ae08745Sheppo 		D2(NULL, "%s: address 0x%llx", __func__, addr);
76041ae08745Sheppo 		tgt = (void *)vswp;
76051ae08745Sheppo 	}
76061ae08745Sheppo 
76071ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
76081ae08745Sheppo 	if (mod_hash_find(vswp->mfdb, (mod_hash_key_t)addr,
76091ae08745Sheppo 				(mod_hash_val_t *)&ment) != 0) {
76101ae08745Sheppo 
76111ae08745Sheppo 		/* address not currently in table */
76121ae08745Sheppo 		ment = kmem_alloc(sizeof (mfdb_ent_t), KM_SLEEP);
76131ae08745Sheppo 		ment->d_addr = (void *)tgt;
76141ae08745Sheppo 		ment->d_type = devtype;
76151ae08745Sheppo 		ment->nextp = NULL;
76161ae08745Sheppo 
76171ae08745Sheppo 		if (mod_hash_insert(vswp->mfdb, (mod_hash_key_t)addr,
76181ae08745Sheppo 			(mod_hash_val_t)ment) != 0) {
76191ae08745Sheppo 			DERR(vswp, "%s: hash table insertion failed", __func__);
76201ae08745Sheppo 			kmem_free(ment, sizeof (mfdb_ent_t));
76211ae08745Sheppo 			rv = 1;
76221ae08745Sheppo 		} else {
76231ae08745Sheppo 			D2(vswp, "%s: added initial entry for 0x%llx to "
76241ae08745Sheppo 				"table", __func__, addr);
76251ae08745Sheppo 		}
76261ae08745Sheppo 	} else {
76271ae08745Sheppo 		/*
76281ae08745Sheppo 		 * Address in table. Check to see if specified port
76291ae08745Sheppo 		 * is already associated with the address. If not add
76301ae08745Sheppo 		 * it now.
76311ae08745Sheppo 		 */
76321ae08745Sheppo 		tmp_ent = ment;
76331ae08745Sheppo 		while (tmp_ent != NULL) {
76341ae08745Sheppo 			if (tmp_ent->d_addr == (void *)tgt) {
76351ae08745Sheppo 				if (devtype == VSW_VNETPORT) {
76361ae08745Sheppo 					DERR(vswp, "%s: duplicate port entry "
76371ae08745Sheppo 						"found for portid %ld and key "
76381ae08745Sheppo 						"0x%llx", __func__,
76391ae08745Sheppo 						((vsw_port_t *)arg)->p_instance,
76401ae08745Sheppo 						addr);
76411ae08745Sheppo 				} else {
76421ae08745Sheppo 					DERR(vswp, "%s: duplicate entry found"
76431ae08745Sheppo 						"for key 0x%llx",
76441ae08745Sheppo 						__func__, addr);
76451ae08745Sheppo 				}
76461ae08745Sheppo 				rv = 1;
76471ae08745Sheppo 				dup = 1;
76481ae08745Sheppo 				break;
76491ae08745Sheppo 			}
76501ae08745Sheppo 			tmp_ent = tmp_ent->nextp;
76511ae08745Sheppo 		}
76521ae08745Sheppo 
76531ae08745Sheppo 		/*
76541ae08745Sheppo 		 * Port not on list so add it to end now.
76551ae08745Sheppo 		 */
76561ae08745Sheppo 		if (0 == dup) {
76571ae08745Sheppo 			D2(vswp, "%s: added entry for 0x%llx to table",
76581ae08745Sheppo 				__func__, addr);
76591ae08745Sheppo 			new_ent = kmem_alloc(sizeof (mfdb_ent_t), KM_SLEEP);
76601ae08745Sheppo 			new_ent->d_addr = (void *)tgt;
76611ae08745Sheppo 			new_ent->d_type = devtype;
76621ae08745Sheppo 			new_ent->nextp = NULL;
76631ae08745Sheppo 
76641ae08745Sheppo 			tmp_ent = ment;
76651ae08745Sheppo 			while (tmp_ent->nextp != NULL)
76661ae08745Sheppo 				tmp_ent = tmp_ent->nextp;
76671ae08745Sheppo 
76681ae08745Sheppo 			tmp_ent->nextp = new_ent;
76691ae08745Sheppo 		}
76701ae08745Sheppo 	}
76711ae08745Sheppo 
76721ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
76731ae08745Sheppo 	return (rv);
76741ae08745Sheppo }
76751ae08745Sheppo 
76761ae08745Sheppo /*
76771ae08745Sheppo  * Remove a multicast entry from the hashtable.
76781ae08745Sheppo  *
76791ae08745Sheppo  * Search hash table based on address. If match found, scan
76801ae08745Sheppo  * list of ports associated with address. If specified port
76811ae08745Sheppo  * found remove it from list.
76821ae08745Sheppo  */
76831ae08745Sheppo static int
76841ae08745Sheppo vsw_del_mcst(vsw_t *vswp, uint8_t devtype, uint64_t addr, void *arg)
76851ae08745Sheppo {
76861ae08745Sheppo 	mfdb_ent_t	*ment = NULL;
76871ae08745Sheppo 	mfdb_ent_t	*curr_p, *prev_p;
76881ae08745Sheppo 	void		*tgt = NULL;
76891ae08745Sheppo 
76901ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
76911ae08745Sheppo 
76921ae08745Sheppo 	if (devtype == VSW_VNETPORT) {
76931ae08745Sheppo 		tgt = (vsw_port_t *)arg;
76941ae08745Sheppo 		D2(vswp, "%s: removing port %d from mFDB for address"
76951ae08745Sheppo 			" 0x%llx", __func__, ((vsw_port_t *)tgt)->p_instance,
76961ae08745Sheppo 			addr);
76971ae08745Sheppo 	} else {
76981ae08745Sheppo 		D2(vswp, "%s: removing entry", __func__);
76991ae08745Sheppo 		tgt = (void *)vswp;
77001ae08745Sheppo 	}
77011ae08745Sheppo 
77021ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
77031ae08745Sheppo 	if (mod_hash_find(vswp->mfdb, (mod_hash_key_t)addr,
77041ae08745Sheppo 				(mod_hash_val_t *)&ment) != 0) {
77051ae08745Sheppo 		D2(vswp, "%s: address 0x%llx not in table", __func__, addr);
77061ae08745Sheppo 		RW_EXIT(&vswp->mfdbrw);
77071ae08745Sheppo 		return (1);
77081ae08745Sheppo 	}
77091ae08745Sheppo 
77101ae08745Sheppo 	prev_p = curr_p = ment;
77111ae08745Sheppo 
77121ae08745Sheppo 	while (curr_p != NULL) {
77131ae08745Sheppo 		if (curr_p->d_addr == (void *)tgt) {
77141ae08745Sheppo 			if (devtype == VSW_VNETPORT) {
77151ae08745Sheppo 				D2(vswp, "%s: port %d found", __func__,
77161ae08745Sheppo 					((vsw_port_t *)tgt)->p_instance);
77171ae08745Sheppo 			} else {
77181ae08745Sheppo 				D2(vswp, "%s: instance found", __func__);
77191ae08745Sheppo 			}
77201ae08745Sheppo 
77211ae08745Sheppo 			if (prev_p == curr_p) {
77221ae08745Sheppo 				/*
77231ae08745Sheppo 				 * head of list, if no other element is in
77241ae08745Sheppo 				 * list then destroy this entry, otherwise
77251ae08745Sheppo 				 * just replace it with updated value.
77261ae08745Sheppo 				 */
77271ae08745Sheppo 				ment = curr_p->nextp;
77281ae08745Sheppo 				kmem_free(curr_p, sizeof (mfdb_ent_t));
77291ae08745Sheppo 				if (ment == NULL) {
77301ae08745Sheppo 					(void) mod_hash_destroy(vswp->mfdb,
77311ae08745Sheppo 							(mod_hash_val_t)addr);
77321ae08745Sheppo 				} else {
77331ae08745Sheppo 					(void) mod_hash_replace(vswp->mfdb,
77341ae08745Sheppo 							(mod_hash_key_t)addr,
77351ae08745Sheppo 							(mod_hash_val_t)ment);
77361ae08745Sheppo 				}
77371ae08745Sheppo 			} else {
77381ae08745Sheppo 				/*
77391ae08745Sheppo 				 * Not head of list, no need to do
77401ae08745Sheppo 				 * replacement, just adjust list pointers.
77411ae08745Sheppo 				 */
77421ae08745Sheppo 				prev_p->nextp = curr_p->nextp;
77431ae08745Sheppo 				kmem_free(curr_p, sizeof (mfdb_ent_t));
77441ae08745Sheppo 			}
77451ae08745Sheppo 			break;
77461ae08745Sheppo 		}
77471ae08745Sheppo 
77481ae08745Sheppo 		prev_p = curr_p;
77491ae08745Sheppo 		curr_p = curr_p->nextp;
77501ae08745Sheppo 	}
77511ae08745Sheppo 
77521ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
77531ae08745Sheppo 
77541ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
77551ae08745Sheppo 
77561ae08745Sheppo 	return (0);
77571ae08745Sheppo }
77581ae08745Sheppo 
77591ae08745Sheppo /*
77601ae08745Sheppo  * Port is being deleted, but has registered an interest in one
77611ae08745Sheppo  * or more multicast groups. Using the list of addresses maintained
77621ae08745Sheppo  * within the port structure find the appropriate entry in the hash
77631ae08745Sheppo  * table and remove this port from the list of interested ports.
77641ae08745Sheppo  */
77651ae08745Sheppo static void
77661ae08745Sheppo vsw_del_mcst_port(vsw_port_t *port)
77671ae08745Sheppo {
77681ae08745Sheppo 	mcst_addr_t	*mcst_p = NULL;
77691ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
77701ae08745Sheppo 
77711ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
77721ae08745Sheppo 
77731ae08745Sheppo 	mutex_enter(&port->mca_lock);
77741ae08745Sheppo 	while (port->mcap != NULL) {
77751ae08745Sheppo 		(void) vsw_del_mcst(vswp, VSW_VNETPORT,
77761ae08745Sheppo 					port->mcap->addr, port);
77771ae08745Sheppo 
77781ae08745Sheppo 		mcst_p = port->mcap->nextp;
77791ae08745Sheppo 		kmem_free(port->mcap, sizeof (mcst_addr_t));
77801ae08745Sheppo 		port->mcap = mcst_p;
77811ae08745Sheppo 	}
77821ae08745Sheppo 	mutex_exit(&port->mca_lock);
77831ae08745Sheppo 
77841ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
77851ae08745Sheppo }
77861ae08745Sheppo 
77871ae08745Sheppo /*
77881ae08745Sheppo  * This vsw instance is detaching, but has registered an interest in one
77891ae08745Sheppo  * or more multicast groups. Using the list of addresses maintained
77901ae08745Sheppo  * within the vsw structure find the appropriate entry in the hash
77911ae08745Sheppo  * table and remove this instance from the list of interested ports.
77921ae08745Sheppo  */
77931ae08745Sheppo static void
77941ae08745Sheppo vsw_del_mcst_vsw(vsw_t *vswp)
77951ae08745Sheppo {
77961ae08745Sheppo 	mcst_addr_t	*next_p = NULL;
77971ae08745Sheppo 
77981ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
77991ae08745Sheppo 
78001ae08745Sheppo 	mutex_enter(&vswp->mca_lock);
78011ae08745Sheppo 
78021ae08745Sheppo 	while (vswp->mcap != NULL) {
78031ae08745Sheppo 		DERR(vswp, "%s: deleting addr 0x%llx",
78041ae08745Sheppo 			__func__, vswp->mcap->addr);
78051ae08745Sheppo 		(void) vsw_del_mcst(vswp, VSW_LOCALDEV,
78061ae08745Sheppo 				vswp->mcap->addr, NULL);
78071ae08745Sheppo 
78081ae08745Sheppo 		next_p = vswp->mcap->nextp;
78091ae08745Sheppo 		kmem_free(vswp->mcap, sizeof (mcst_addr_t));
78101ae08745Sheppo 		vswp->mcap = next_p;
78111ae08745Sheppo 	}
78121ae08745Sheppo 
78131ae08745Sheppo 	vswp->mcap = NULL;
78141ae08745Sheppo 	mutex_exit(&vswp->mca_lock);
78151ae08745Sheppo 
78161ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
78171ae08745Sheppo }
78181ae08745Sheppo 
78191ae08745Sheppo 
78201ae08745Sheppo /*
78211ae08745Sheppo  * Remove the specified address from the list of address maintained
78221ae08745Sheppo  * in this port node.
78231ae08745Sheppo  */
78241ae08745Sheppo static void
78251ae08745Sheppo vsw_del_addr(uint8_t devtype, void *arg, uint64_t addr)
78261ae08745Sheppo {
78271ae08745Sheppo 	vsw_t		*vswp = NULL;
78281ae08745Sheppo 	vsw_port_t	*port = NULL;
78291ae08745Sheppo 	mcst_addr_t	*prev_p = NULL;
78301ae08745Sheppo 	mcst_addr_t	*curr_p = NULL;
78311ae08745Sheppo 
78321ae08745Sheppo 	D1(NULL, "%s: enter : devtype %d : addr 0x%llx",
78331ae08745Sheppo 		__func__, devtype, addr);
78341ae08745Sheppo 
78351ae08745Sheppo 	if (devtype == VSW_VNETPORT) {
78361ae08745Sheppo 		port = (vsw_port_t *)arg;
78371ae08745Sheppo 		mutex_enter(&port->mca_lock);
78381ae08745Sheppo 		prev_p = curr_p = port->mcap;
78391ae08745Sheppo 	} else {
78401ae08745Sheppo 		vswp = (vsw_t *)arg;
78411ae08745Sheppo 		mutex_enter(&vswp->mca_lock);
78421ae08745Sheppo 		prev_p = curr_p = vswp->mcap;
78431ae08745Sheppo 	}
78441ae08745Sheppo 
78451ae08745Sheppo 	while (curr_p != NULL) {
78461ae08745Sheppo 		if (curr_p->addr == addr) {
78471ae08745Sheppo 			D2(NULL, "%s: address found", __func__);
78481ae08745Sheppo 			/* match found */
78491ae08745Sheppo 			if (prev_p == curr_p) {
78501ae08745Sheppo 				/* list head */
78511ae08745Sheppo 				if (devtype == VSW_VNETPORT)
78521ae08745Sheppo 					port->mcap = curr_p->nextp;
78531ae08745Sheppo 				else
78541ae08745Sheppo 					vswp->mcap = curr_p->nextp;
78551ae08745Sheppo 			} else {
78561ae08745Sheppo 				prev_p->nextp = curr_p->nextp;
78571ae08745Sheppo 			}
78581ae08745Sheppo 			kmem_free(curr_p, sizeof (mcst_addr_t));
78591ae08745Sheppo 			break;
78601ae08745Sheppo 		} else {
78611ae08745Sheppo 			prev_p = curr_p;
78621ae08745Sheppo 			curr_p = curr_p->nextp;
78631ae08745Sheppo 		}
78641ae08745Sheppo 	}
78651ae08745Sheppo 
78661ae08745Sheppo 	if (devtype == VSW_VNETPORT)
78671ae08745Sheppo 		mutex_exit(&port->mca_lock);
78681ae08745Sheppo 	else
78691ae08745Sheppo 		mutex_exit(&vswp->mca_lock);
78701ae08745Sheppo 
78711ae08745Sheppo 	D1(NULL, "%s: exit", __func__);
78721ae08745Sheppo }
78731ae08745Sheppo 
78741ae08745Sheppo /*
78751ae08745Sheppo  * Creates a descriptor ring (dring) and links it into the
78761ae08745Sheppo  * link of outbound drings for this channel.
78771ae08745Sheppo  *
78781ae08745Sheppo  * Returns NULL if creation failed.
78791ae08745Sheppo  */
78801ae08745Sheppo static dring_info_t *
78811ae08745Sheppo vsw_create_dring(vsw_ldc_t *ldcp)
78821ae08745Sheppo {
78831ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
78841ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
78851ae08745Sheppo 	ldc_mem_info_t		minfo;
78861ae08745Sheppo 	dring_info_t		*dp, *tp;
78871ae08745Sheppo 	int			i;
78881ae08745Sheppo 
78891ae08745Sheppo 	dp = (dring_info_t *)kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
78901ae08745Sheppo 
78911ae08745Sheppo 	mutex_init(&dp->dlock, NULL, MUTEX_DRIVER, NULL);
78921ae08745Sheppo 
78931ae08745Sheppo 	/* create public section of ring */
78941ae08745Sheppo 	if ((ldc_mem_dring_create(VSW_RING_NUM_EL,
78951ae08745Sheppo 			VSW_PUB_SIZE, &dp->handle)) != 0) {
78961ae08745Sheppo 
78971ae08745Sheppo 		DERR(vswp, "vsw_create_dring(%lld): ldc dring create "
78981ae08745Sheppo 			"failed", ldcp->ldc_id);
78991ae08745Sheppo 		goto create_fail_exit;
79001ae08745Sheppo 	}
79011ae08745Sheppo 
79021ae08745Sheppo 	ASSERT(dp->handle != NULL);
79031ae08745Sheppo 
79041ae08745Sheppo 	/*
79051ae08745Sheppo 	 * Get the base address of the public section of the ring.
79061ae08745Sheppo 	 */
79071ae08745Sheppo 	if ((ldc_mem_dring_info(dp->handle, &minfo)) != 0) {
79081ae08745Sheppo 		DERR(vswp, "vsw_create_dring(%lld): dring info failed\n",
79091ae08745Sheppo 			ldcp->ldc_id);
79101ae08745Sheppo 		goto dring_fail_exit;
79111ae08745Sheppo 	} else {
79121ae08745Sheppo 		ASSERT(minfo.vaddr != 0);
79131ae08745Sheppo 		dp->pub_addr = minfo.vaddr;
79141ae08745Sheppo 	}
79151ae08745Sheppo 
79161ae08745Sheppo 	dp->num_descriptors = VSW_RING_NUM_EL;
79171ae08745Sheppo 	dp->descriptor_size = VSW_PUB_SIZE;
79181ae08745Sheppo 	dp->options = VIO_TX_DRING;
79191ae08745Sheppo 	dp->ncookies = 1;	/* guaranteed by ldc */
79201ae08745Sheppo 
79211ae08745Sheppo 	/*
79221ae08745Sheppo 	 * create private portion of ring
79231ae08745Sheppo 	 */
79241ae08745Sheppo 	dp->priv_addr = (vsw_private_desc_t *)kmem_zalloc(
79251ae08745Sheppo 		(sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL), KM_SLEEP);
79261ae08745Sheppo 
79271ae08745Sheppo 	if (vsw_setup_ring(ldcp, dp)) {
79281ae08745Sheppo 		DERR(vswp, "%s: unable to setup ring", __func__);
79291ae08745Sheppo 		goto dring_fail_exit;
79301ae08745Sheppo 	}
79311ae08745Sheppo 
79321ae08745Sheppo 	/* haven't used any descriptors yet */
79331ae08745Sheppo 	dp->end_idx = 0;
7934d10e4ef2Snarayan 	dp->last_ack_recv = -1;
79351ae08745Sheppo 
79361ae08745Sheppo 	/* bind dring to the channel */
79371ae08745Sheppo 	if ((ldc_mem_dring_bind(ldcp->ldc_handle, dp->handle,
79381ae08745Sheppo 		LDC_SHADOW_MAP, LDC_MEM_RW,
79391ae08745Sheppo 		&dp->cookie[0], &dp->ncookies)) != 0) {
79401ae08745Sheppo 		DERR(vswp, "vsw_create_dring: unable to bind to channel "
79411ae08745Sheppo 			"%lld", ldcp->ldc_id);
79421ae08745Sheppo 		goto dring_fail_exit;
79431ae08745Sheppo 	}
79441ae08745Sheppo 
7945d10e4ef2Snarayan 	mutex_init(&dp->restart_lock, NULL, MUTEX_DRIVER, NULL);
7946d10e4ef2Snarayan 	dp->restart_reqd = B_TRUE;
7947d10e4ef2Snarayan 
79481ae08745Sheppo 	/*
79491ae08745Sheppo 	 * Only ever create rings for outgoing lane. Link it onto
79501ae08745Sheppo 	 * end of list.
79511ae08745Sheppo 	 */
7952445b4c2eSsb155480 	WRITE_ENTER(&ldcp->lane_out.dlistrw);
79531ae08745Sheppo 	if (ldcp->lane_out.dringp == NULL) {
79541ae08745Sheppo 		D2(vswp, "vsw_create_dring: adding first outbound ring");
79551ae08745Sheppo 		ldcp->lane_out.dringp = dp;
79561ae08745Sheppo 	} else {
79571ae08745Sheppo 		tp = ldcp->lane_out.dringp;
79581ae08745Sheppo 		while (tp->next != NULL)
79591ae08745Sheppo 			tp = tp->next;
79601ae08745Sheppo 
79611ae08745Sheppo 		tp->next = dp;
79621ae08745Sheppo 	}
7963445b4c2eSsb155480 	RW_EXIT(&ldcp->lane_out.dlistrw);
79641ae08745Sheppo 
79651ae08745Sheppo 	return (dp);
79661ae08745Sheppo 
79671ae08745Sheppo dring_fail_exit:
79681ae08745Sheppo 	(void) ldc_mem_dring_destroy(dp->handle);
79691ae08745Sheppo 
79701ae08745Sheppo create_fail_exit:
79711ae08745Sheppo 	if (dp->priv_addr != NULL) {
79721ae08745Sheppo 		priv_addr = dp->priv_addr;
79731ae08745Sheppo 		for (i = 0; i < VSW_RING_NUM_EL; i++) {
79741ae08745Sheppo 			if (priv_addr->memhandle != NULL)
79751ae08745Sheppo 				(void) ldc_mem_free_handle(
79761ae08745Sheppo 						priv_addr->memhandle);
79771ae08745Sheppo 			priv_addr++;
79781ae08745Sheppo 		}
79791ae08745Sheppo 		kmem_free(dp->priv_addr,
79801ae08745Sheppo 			(sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL));
79811ae08745Sheppo 	}
79821ae08745Sheppo 	mutex_destroy(&dp->dlock);
79831ae08745Sheppo 
79841ae08745Sheppo 	kmem_free(dp, sizeof (dring_info_t));
79851ae08745Sheppo 	return (NULL);
79861ae08745Sheppo }
79871ae08745Sheppo 
79881ae08745Sheppo /*
79891ae08745Sheppo  * Create a ring consisting of just a private portion and link
79901ae08745Sheppo  * it into the list of rings for the outbound lane.
79911ae08745Sheppo  *
79921ae08745Sheppo  * These type of rings are used primarily for temporary data
79931ae08745Sheppo  * storage (i.e. as data buffers).
79941ae08745Sheppo  */
79951ae08745Sheppo void
79961ae08745Sheppo vsw_create_privring(vsw_ldc_t *ldcp)
79971ae08745Sheppo {
79981ae08745Sheppo 	dring_info_t		*dp, *tp;
79991ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
80001ae08745Sheppo 
80011ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
80021ae08745Sheppo 
80031ae08745Sheppo 	dp = kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
80041ae08745Sheppo 
80051ae08745Sheppo 	mutex_init(&dp->dlock, NULL, MUTEX_DRIVER, NULL);
80061ae08745Sheppo 
80071ae08745Sheppo 	/* no public section */
80081ae08745Sheppo 	dp->pub_addr = NULL;
80091ae08745Sheppo 
80101ae08745Sheppo 	dp->priv_addr = kmem_zalloc((sizeof (vsw_private_desc_t) *
80111ae08745Sheppo 					VSW_RING_NUM_EL), KM_SLEEP);
80121ae08745Sheppo 
80134bac2208Snarayan 	dp->num_descriptors = VSW_RING_NUM_EL;
80144bac2208Snarayan 
80151ae08745Sheppo 	if (vsw_setup_ring(ldcp, dp)) {
80161ae08745Sheppo 		DERR(vswp, "%s: setup of ring failed", __func__);
80171ae08745Sheppo 		kmem_free(dp->priv_addr,
80181ae08745Sheppo 			(sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL));
80191ae08745Sheppo 		mutex_destroy(&dp->dlock);
80201ae08745Sheppo 		kmem_free(dp, sizeof (dring_info_t));
80211ae08745Sheppo 		return;
80221ae08745Sheppo 	}
80231ae08745Sheppo 
80241ae08745Sheppo 	/* haven't used any descriptors yet */
80251ae08745Sheppo 	dp->end_idx = 0;
80261ae08745Sheppo 
8027d10e4ef2Snarayan 	mutex_init(&dp->restart_lock, NULL, MUTEX_DRIVER, NULL);
8028d10e4ef2Snarayan 	dp->restart_reqd = B_TRUE;
8029d10e4ef2Snarayan 
80301ae08745Sheppo 	/*
80311ae08745Sheppo 	 * Only ever create rings for outgoing lane. Link it onto
80321ae08745Sheppo 	 * end of list.
80331ae08745Sheppo 	 */
8034445b4c2eSsb155480 	WRITE_ENTER(&ldcp->lane_out.dlistrw);
80351ae08745Sheppo 	if (ldcp->lane_out.dringp == NULL) {
80361ae08745Sheppo 		D2(vswp, "%s: adding first outbound privring", __func__);
80371ae08745Sheppo 		ldcp->lane_out.dringp = dp;
80381ae08745Sheppo 	} else {
80391ae08745Sheppo 		tp = ldcp->lane_out.dringp;
80401ae08745Sheppo 		while (tp->next != NULL)
80411ae08745Sheppo 			tp = tp->next;
80421ae08745Sheppo 
80431ae08745Sheppo 		tp->next = dp;
80441ae08745Sheppo 	}
8045445b4c2eSsb155480 	RW_EXIT(&ldcp->lane_out.dlistrw);
80461ae08745Sheppo 
80471ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
80481ae08745Sheppo }
80491ae08745Sheppo 
80501ae08745Sheppo /*
80511ae08745Sheppo  * Setup the descriptors in the dring. Returns 0 on success, 1 on
80521ae08745Sheppo  * failure.
80531ae08745Sheppo  */
80541ae08745Sheppo int
80551ae08745Sheppo vsw_setup_ring(vsw_ldc_t *ldcp, dring_info_t *dp)
80561ae08745Sheppo {
80571ae08745Sheppo 	vnet_public_desc_t	*pub_addr = NULL;
80581ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
80591ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
80601ae08745Sheppo 	uint64_t		*tmpp;
80611ae08745Sheppo 	uint64_t		offset = 0;
80621ae08745Sheppo 	uint32_t		ncookies = 0;
80631ae08745Sheppo 	static char		*name = "vsw_setup_ring";
8064d10e4ef2Snarayan 	int			i, j, nc, rv;
80651ae08745Sheppo 
80661ae08745Sheppo 	priv_addr = dp->priv_addr;
80671ae08745Sheppo 	pub_addr = dp->pub_addr;
80681ae08745Sheppo 
8069d10e4ef2Snarayan 	/* public section may be null but private should never be */
8070d10e4ef2Snarayan 	ASSERT(priv_addr != NULL);
8071d10e4ef2Snarayan 
80721ae08745Sheppo 	/*
80731ae08745Sheppo 	 * Allocate the region of memory which will be used to hold
80741ae08745Sheppo 	 * the data the descriptors will refer to.
80751ae08745Sheppo 	 */
80761ae08745Sheppo 	dp->data_sz = (VSW_RING_NUM_EL * VSW_RING_EL_DATA_SZ);
80771ae08745Sheppo 	dp->data_addr = kmem_alloc(dp->data_sz, KM_SLEEP);
80781ae08745Sheppo 
80791ae08745Sheppo 	D2(vswp, "%s: allocated %lld bytes at 0x%llx\n", name,
80801ae08745Sheppo 		dp->data_sz, dp->data_addr);
80811ae08745Sheppo 
80821ae08745Sheppo 	tmpp = (uint64_t *)dp->data_addr;
80831ae08745Sheppo 	offset = VSW_RING_EL_DATA_SZ / sizeof (tmpp);
80841ae08745Sheppo 
80851ae08745Sheppo 	/*
80861ae08745Sheppo 	 * Initialise some of the private and public (if they exist)
80871ae08745Sheppo 	 * descriptor fields.
80881ae08745Sheppo 	 */
80891ae08745Sheppo 	for (i = 0; i < VSW_RING_NUM_EL; i++) {
8090d10e4ef2Snarayan 		mutex_init(&priv_addr->dstate_lock, NULL, MUTEX_DRIVER, NULL);
8091d10e4ef2Snarayan 
80921ae08745Sheppo 		if ((ldc_mem_alloc_handle(ldcp->ldc_handle,
80931ae08745Sheppo 			&priv_addr->memhandle)) != 0) {
80941ae08745Sheppo 			DERR(vswp, "%s: alloc mem handle failed", name);
80951ae08745Sheppo 			goto setup_ring_cleanup;
80961ae08745Sheppo 		}
80971ae08745Sheppo 
80981ae08745Sheppo 		priv_addr->datap = (void *)tmpp;
80991ae08745Sheppo 
81001ae08745Sheppo 		rv = ldc_mem_bind_handle(priv_addr->memhandle,
81011ae08745Sheppo 			(caddr_t)priv_addr->datap, VSW_RING_EL_DATA_SZ,
81021ae08745Sheppo 			LDC_SHADOW_MAP, LDC_MEM_R|LDC_MEM_W,
81031ae08745Sheppo 			&(priv_addr->memcookie[0]), &ncookies);
81041ae08745Sheppo 		if (rv != 0) {
81051ae08745Sheppo 			DERR(vswp, "%s(%lld): ldc_mem_bind_handle failed "
81061ae08745Sheppo 				"(rv %d)", name, ldcp->ldc_id, rv);
81071ae08745Sheppo 			goto setup_ring_cleanup;
81081ae08745Sheppo 		}
81091ae08745Sheppo 		priv_addr->bound = 1;
81101ae08745Sheppo 
81111ae08745Sheppo 		D2(vswp, "%s: %d: memcookie 0 : addr 0x%llx : size 0x%llx",
81121ae08745Sheppo 			name, i, priv_addr->memcookie[0].addr,
81131ae08745Sheppo 			priv_addr->memcookie[0].size);
81141ae08745Sheppo 
81151ae08745Sheppo 		if (ncookies >= (uint32_t)(VSW_MAX_COOKIES + 1)) {
81161ae08745Sheppo 			DERR(vswp, "%s(%lld) ldc_mem_bind_handle returned "
81171ae08745Sheppo 				"invalid num of cookies (%d) for size 0x%llx",
81181ae08745Sheppo 				name, ldcp->ldc_id, ncookies,
81191ae08745Sheppo 				VSW_RING_EL_DATA_SZ);
81201ae08745Sheppo 
81211ae08745Sheppo 			goto setup_ring_cleanup;
81221ae08745Sheppo 		} else {
81231ae08745Sheppo 			for (j = 1; j < ncookies; j++) {
81241ae08745Sheppo 				rv = ldc_mem_nextcookie(priv_addr->memhandle,
81251ae08745Sheppo 					&(priv_addr->memcookie[j]));
81261ae08745Sheppo 				if (rv != 0) {
81271ae08745Sheppo 					DERR(vswp, "%s: ldc_mem_nextcookie "
81281ae08745Sheppo 						"failed rv (%d)", name, rv);
81291ae08745Sheppo 					goto setup_ring_cleanup;
81301ae08745Sheppo 				}
81311ae08745Sheppo 				D3(vswp, "%s: memcookie %d : addr 0x%llx : "
81321ae08745Sheppo 					"size 0x%llx", name, j,
81331ae08745Sheppo 					priv_addr->memcookie[j].addr,
81341ae08745Sheppo 					priv_addr->memcookie[j].size);
81351ae08745Sheppo 			}
81361ae08745Sheppo 
81371ae08745Sheppo 		}
81381ae08745Sheppo 		priv_addr->ncookies = ncookies;
81391ae08745Sheppo 		priv_addr->dstate = VIO_DESC_FREE;
81401ae08745Sheppo 
81411ae08745Sheppo 		if (pub_addr != NULL) {
81421ae08745Sheppo 
81431ae08745Sheppo 			/* link pub and private sides */
81441ae08745Sheppo 			priv_addr->descp = pub_addr;
81451ae08745Sheppo 
8146d10e4ef2Snarayan 			pub_addr->ncookies = priv_addr->ncookies;
8147d10e4ef2Snarayan 
8148d10e4ef2Snarayan 			for (nc = 0; nc < pub_addr->ncookies; nc++) {
8149d10e4ef2Snarayan 				bcopy(&priv_addr->memcookie[nc],
8150d10e4ef2Snarayan 					&pub_addr->memcookie[nc],
8151d10e4ef2Snarayan 					sizeof (ldc_mem_cookie_t));
8152d10e4ef2Snarayan 			}
8153d10e4ef2Snarayan 
81541ae08745Sheppo 			pub_addr->hdr.dstate = VIO_DESC_FREE;
81551ae08745Sheppo 			pub_addr++;
81561ae08745Sheppo 		}
81571ae08745Sheppo 
81581ae08745Sheppo 		/*
81591ae08745Sheppo 		 * move to next element in the dring and the next
81601ae08745Sheppo 		 * position in the data buffer.
81611ae08745Sheppo 		 */
81621ae08745Sheppo 		priv_addr++;
81631ae08745Sheppo 		tmpp += offset;
81641ae08745Sheppo 	}
81651ae08745Sheppo 
81661ae08745Sheppo 	return (0);
81671ae08745Sheppo 
81681ae08745Sheppo setup_ring_cleanup:
81691ae08745Sheppo 	priv_addr = dp->priv_addr;
81701ae08745Sheppo 
8171d10e4ef2Snarayan 	for (j = 0; j < i; j++) {
81721ae08745Sheppo 		(void) ldc_mem_unbind_handle(priv_addr->memhandle);
81731ae08745Sheppo 		(void) ldc_mem_free_handle(priv_addr->memhandle);
81741ae08745Sheppo 
8175d10e4ef2Snarayan 		mutex_destroy(&priv_addr->dstate_lock);
8176d10e4ef2Snarayan 
81771ae08745Sheppo 		priv_addr++;
81781ae08745Sheppo 	}
81791ae08745Sheppo 	kmem_free(dp->data_addr, dp->data_sz);
81801ae08745Sheppo 
81811ae08745Sheppo 	return (1);
81821ae08745Sheppo }
81831ae08745Sheppo 
81841ae08745Sheppo /*
81851ae08745Sheppo  * Searches the private section of a ring for a free descriptor,
81861ae08745Sheppo  * starting at the location of the last free descriptor found
81871ae08745Sheppo  * previously.
81881ae08745Sheppo  *
8189d10e4ef2Snarayan  * Returns 0 if free descriptor is available, and updates state
8190d10e4ef2Snarayan  * of private descriptor to VIO_DESC_READY,  otherwise returns 1.
81911ae08745Sheppo  *
81921ae08745Sheppo  * FUTURE: might need to return contiguous range of descriptors
81931ae08745Sheppo  * as dring info msg assumes all will be contiguous.
81941ae08745Sheppo  */
81951ae08745Sheppo static int
81961ae08745Sheppo vsw_dring_find_free_desc(dring_info_t *dringp,
81971ae08745Sheppo 		vsw_private_desc_t **priv_p, int *idx)
81981ae08745Sheppo {
8199d10e4ef2Snarayan 	vsw_private_desc_t	*addr = NULL;
82001ae08745Sheppo 	int			num = VSW_RING_NUM_EL;
82011ae08745Sheppo 	int			ret = 1;
82021ae08745Sheppo 
82031ae08745Sheppo 	D1(NULL, "%s enter\n", __func__);
82041ae08745Sheppo 
8205d10e4ef2Snarayan 	ASSERT(dringp->priv_addr != NULL);
82061ae08745Sheppo 
82071ae08745Sheppo 	D2(NULL, "%s: searching ring, dringp 0x%llx : start pos %lld",
8208d10e4ef2Snarayan 			__func__, dringp, dringp->end_idx);
82091ae08745Sheppo 
8210d10e4ef2Snarayan 	addr = (vsw_private_desc_t *)dringp->priv_addr + dringp->end_idx;
8211d10e4ef2Snarayan 
8212d10e4ef2Snarayan 	mutex_enter(&addr->dstate_lock);
82131ae08745Sheppo 	if (addr->dstate == VIO_DESC_FREE) {
8214d10e4ef2Snarayan 		addr->dstate = VIO_DESC_READY;
82151ae08745Sheppo 		*priv_p = addr;
8216d10e4ef2Snarayan 		*idx = dringp->end_idx;
8217d10e4ef2Snarayan 		dringp->end_idx = (dringp->end_idx + 1) % num;
82181ae08745Sheppo 		ret = 0;
8219d10e4ef2Snarayan 
82201ae08745Sheppo 	}
8221d10e4ef2Snarayan 	mutex_exit(&addr->dstate_lock);
82221ae08745Sheppo 
82231ae08745Sheppo 	/* ring full */
82241ae08745Sheppo 	if (ret == 1) {
8225d10e4ef2Snarayan 		D2(NULL, "%s: no desp free: started at %d", __func__,
8226d10e4ef2Snarayan 			dringp->end_idx);
82271ae08745Sheppo 	}
82281ae08745Sheppo 
82291ae08745Sheppo 	D1(NULL, "%s: exit\n", __func__);
82301ae08745Sheppo 
82311ae08745Sheppo 	return (ret);
82321ae08745Sheppo }
82331ae08745Sheppo 
82341ae08745Sheppo /*
82351ae08745Sheppo  * Map from a dring identifier to the ring itself. Returns
82361ae08745Sheppo  * pointer to ring or NULL if no match found.
8237445b4c2eSsb155480  *
8238445b4c2eSsb155480  * Should be called with dlistrw rwlock held as reader.
82391ae08745Sheppo  */
82401ae08745Sheppo static dring_info_t *
82411ae08745Sheppo vsw_ident2dring(lane_t *lane, uint64_t ident)
82421ae08745Sheppo {
82431ae08745Sheppo 	dring_info_t	*dp = NULL;
82441ae08745Sheppo 
82451ae08745Sheppo 	if ((dp = lane->dringp) == NULL) {
82461ae08745Sheppo 		return (NULL);
82471ae08745Sheppo 	} else {
82481ae08745Sheppo 		if (dp->ident == ident)
82491ae08745Sheppo 			return (dp);
82501ae08745Sheppo 
82511ae08745Sheppo 		while (dp != NULL) {
82521ae08745Sheppo 			if (dp->ident == ident)
82531ae08745Sheppo 				break;
82541ae08745Sheppo 			dp = dp->next;
82551ae08745Sheppo 		}
82561ae08745Sheppo 	}
82571ae08745Sheppo 
82581ae08745Sheppo 	return (dp);
82591ae08745Sheppo }
82601ae08745Sheppo 
82611ae08745Sheppo /*
82621ae08745Sheppo  * Set the default lane attributes. These are copied into
82631ae08745Sheppo  * the attr msg we send to our peer. If they are not acceptable
82641ae08745Sheppo  * then (currently) the handshake ends.
82651ae08745Sheppo  */
82661ae08745Sheppo static void
82671ae08745Sheppo vsw_set_lane_attr(vsw_t *vswp, lane_t *lp)
82681ae08745Sheppo {
82691ae08745Sheppo 	bzero(lp, sizeof (lane_t));
82701ae08745Sheppo 
82711ae08745Sheppo 	READ_ENTER(&vswp->if_lockrw);
82721ae08745Sheppo 	ether_copy(&(vswp->if_addr), &(lp->addr));
82731ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
82741ae08745Sheppo 
82751ae08745Sheppo 	lp->mtu = VSW_MTU;
82761ae08745Sheppo 	lp->addr_type = ADDR_TYPE_MAC;
82771ae08745Sheppo 	lp->xfer_mode = VIO_DRING_MODE;
82781ae08745Sheppo 	lp->ack_freq = 0;	/* for shared mode */
8279d10e4ef2Snarayan 
8280d10e4ef2Snarayan 	mutex_enter(&lp->seq_lock);
82811ae08745Sheppo 	lp->seq_num = VNET_ISS;
8282d10e4ef2Snarayan 	mutex_exit(&lp->seq_lock);
82831ae08745Sheppo }
82841ae08745Sheppo 
82851ae08745Sheppo /*
82861ae08745Sheppo  * Verify that the attributes are acceptable.
82871ae08745Sheppo  *
82881ae08745Sheppo  * FUTURE: If some attributes are not acceptable, change them
82891ae08745Sheppo  * our desired values.
82901ae08745Sheppo  */
82911ae08745Sheppo static int
82921ae08745Sheppo vsw_check_attr(vnet_attr_msg_t *pkt, vsw_port_t *port)
82931ae08745Sheppo {
82941ae08745Sheppo 	int	ret = 0;
82951ae08745Sheppo 
82961ae08745Sheppo 	D1(NULL, "vsw_check_attr enter\n");
82971ae08745Sheppo 
82981ae08745Sheppo 	/*
82991ae08745Sheppo 	 * Note we currently only support in-band descriptors
83001ae08745Sheppo 	 * and descriptor rings, not packet based transfer (VIO_PKT_MODE)
83011ae08745Sheppo 	 */
83021ae08745Sheppo 	if ((pkt->xfer_mode != VIO_DESC_MODE) &&
83031ae08745Sheppo 			(pkt->xfer_mode != VIO_DRING_MODE)) {
83041ae08745Sheppo 		D2(NULL, "vsw_check_attr: unknown mode %x\n",
83051ae08745Sheppo 			pkt->xfer_mode);
83061ae08745Sheppo 		ret = 1;
83071ae08745Sheppo 	}
83081ae08745Sheppo 
83091ae08745Sheppo 	/* Only support MAC addresses at moment. */
83101ae08745Sheppo 	if ((pkt->addr_type != ADDR_TYPE_MAC) || (pkt->addr == 0)) {
83111ae08745Sheppo 		D2(NULL, "vsw_check_attr: invalid addr_type %x, "
83121ae08745Sheppo 			"or address 0x%llx\n", pkt->addr_type,
83131ae08745Sheppo 			pkt->addr);
83141ae08745Sheppo 		ret = 1;
83151ae08745Sheppo 	}
83161ae08745Sheppo 
83171ae08745Sheppo 	/*
83181ae08745Sheppo 	 * MAC address supplied by device should match that stored
83191ae08745Sheppo 	 * in the vsw-port OBP node. Need to decide what to do if they
83201ae08745Sheppo 	 * don't match, for the moment just warn but don't fail.
83211ae08745Sheppo 	 */
83221ae08745Sheppo 	if (bcmp(&pkt->addr, &port->p_macaddr, ETHERADDRL) != 0) {
83231ae08745Sheppo 		DERR(NULL, "vsw_check_attr: device supplied address "
83241ae08745Sheppo 			"0x%llx doesn't match node address 0x%llx\n",
83251ae08745Sheppo 			pkt->addr, port->p_macaddr);
83261ae08745Sheppo 	}
83271ae08745Sheppo 
83281ae08745Sheppo 	/*
83291ae08745Sheppo 	 * Ack freq only makes sense in pkt mode, in shared
83301ae08745Sheppo 	 * mode the ring descriptors say whether or not to
83311ae08745Sheppo 	 * send back an ACK.
83321ae08745Sheppo 	 */
83331ae08745Sheppo 	if ((pkt->xfer_mode == VIO_DRING_MODE) &&
83341ae08745Sheppo 				(pkt->ack_freq > 0)) {
83351ae08745Sheppo 		D2(NULL, "vsw_check_attr: non zero ack freq "
83361ae08745Sheppo 			" in SHM mode\n");
83371ae08745Sheppo 		ret = 1;
83381ae08745Sheppo 	}
83391ae08745Sheppo 
83401ae08745Sheppo 	/*
83411ae08745Sheppo 	 * Note: for the moment we only support ETHER
83421ae08745Sheppo 	 * frames. This may change in the future.
83431ae08745Sheppo 	 */
83441ae08745Sheppo 	if ((pkt->mtu > VSW_MTU) || (pkt->mtu <= 0)) {
83451ae08745Sheppo 		D2(NULL, "vsw_check_attr: invalid MTU (0x%llx)\n",
83461ae08745Sheppo 			pkt->mtu);
83471ae08745Sheppo 		ret = 1;
83481ae08745Sheppo 	}
83491ae08745Sheppo 
83501ae08745Sheppo 	D1(NULL, "vsw_check_attr exit\n");
83511ae08745Sheppo 
83521ae08745Sheppo 	return (ret);
83531ae08745Sheppo }
83541ae08745Sheppo 
83551ae08745Sheppo /*
83561ae08745Sheppo  * Returns 1 if there is a problem, 0 otherwise.
83571ae08745Sheppo  */
83581ae08745Sheppo static int
83591ae08745Sheppo vsw_check_dring_info(vio_dring_reg_msg_t *pkt)
83601ae08745Sheppo {
83611ae08745Sheppo 	_NOTE(ARGUNUSED(pkt))
83621ae08745Sheppo 
83631ae08745Sheppo 	int	ret = 0;
83641ae08745Sheppo 
83651ae08745Sheppo 	D1(NULL, "vsw_check_dring_info enter\n");
83661ae08745Sheppo 
83671ae08745Sheppo 	if ((pkt->num_descriptors == 0) ||
83681ae08745Sheppo 		(pkt->descriptor_size == 0) ||
83691ae08745Sheppo 		(pkt->ncookies != 1)) {
83701ae08745Sheppo 		DERR(NULL, "vsw_check_dring_info: invalid dring msg");
83711ae08745Sheppo 		ret = 1;
83721ae08745Sheppo 	}
83731ae08745Sheppo 
83741ae08745Sheppo 	D1(NULL, "vsw_check_dring_info exit\n");
83751ae08745Sheppo 
83761ae08745Sheppo 	return (ret);
83771ae08745Sheppo }
83781ae08745Sheppo 
83791ae08745Sheppo /*
83801ae08745Sheppo  * Returns 1 if two memory cookies match. Otherwise returns 0.
83811ae08745Sheppo  */
83821ae08745Sheppo static int
83831ae08745Sheppo vsw_mem_cookie_match(ldc_mem_cookie_t *m1, ldc_mem_cookie_t *m2)
83841ae08745Sheppo {
83851ae08745Sheppo 	if ((m1->addr != m2->addr) ||
83861ae08745Sheppo 		(m2->size != m2->size)) {
83871ae08745Sheppo 		return (0);
83881ae08745Sheppo 	} else {
83891ae08745Sheppo 		return (1);
83901ae08745Sheppo 	}
83911ae08745Sheppo }
83921ae08745Sheppo 
83931ae08745Sheppo /*
83941ae08745Sheppo  * Returns 1 if ring described in reg message matches that
83951ae08745Sheppo  * described by dring_info structure. Otherwise returns 0.
83961ae08745Sheppo  */
83971ae08745Sheppo static int
83981ae08745Sheppo vsw_dring_match(dring_info_t *dp, vio_dring_reg_msg_t *msg)
83991ae08745Sheppo {
84001ae08745Sheppo 	if ((msg->descriptor_size != dp->descriptor_size) ||
84011ae08745Sheppo 		(msg->num_descriptors != dp->num_descriptors) ||
84021ae08745Sheppo 		(msg->ncookies != dp->ncookies) ||
84031ae08745Sheppo 		!(vsw_mem_cookie_match(&msg->cookie[0], &dp->cookie[0]))) {
84041ae08745Sheppo 		return (0);
84051ae08745Sheppo 	} else {
84061ae08745Sheppo 		return (1);
84071ae08745Sheppo 	}
84081ae08745Sheppo 
84091ae08745Sheppo }
84101ae08745Sheppo 
84111ae08745Sheppo static caddr_t
84121ae08745Sheppo vsw_print_ethaddr(uint8_t *a, char *ebuf)
84131ae08745Sheppo {
84141ae08745Sheppo 	(void) sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
84151ae08745Sheppo 	    a[0], a[1], a[2], a[3], a[4], a[5]);
84161ae08745Sheppo 	return (ebuf);
84171ae08745Sheppo }
84181ae08745Sheppo 
84191ae08745Sheppo /*
84201ae08745Sheppo  * Reset and free all the resources associated with
84211ae08745Sheppo  * the channel.
84221ae08745Sheppo  */
84231ae08745Sheppo static void
84241ae08745Sheppo vsw_free_lane_resources(vsw_ldc_t *ldcp, uint64_t dir)
84251ae08745Sheppo {
84261ae08745Sheppo 	dring_info_t		*dp, *dpp;
84271ae08745Sheppo 	lane_t			*lp = NULL;
84281ae08745Sheppo 	int			rv = 0;
84291ae08745Sheppo 
84301ae08745Sheppo 	ASSERT(ldcp != NULL);
84311ae08745Sheppo 
84321ae08745Sheppo 	D1(ldcp->ldc_vswp, "%s (%lld): enter", __func__, ldcp->ldc_id);
84331ae08745Sheppo 
84341ae08745Sheppo 	if (dir == INBOUND) {
84351ae08745Sheppo 		D2(ldcp->ldc_vswp, "%s: freeing INBOUND lane"
84361ae08745Sheppo 			" of channel %lld", __func__, ldcp->ldc_id);
84371ae08745Sheppo 		lp = &ldcp->lane_in;
84381ae08745Sheppo 	} else {
84391ae08745Sheppo 		D2(ldcp->ldc_vswp, "%s: freeing OUTBOUND lane"
84401ae08745Sheppo 			" of channel %lld", __func__, ldcp->ldc_id);
84411ae08745Sheppo 		lp = &ldcp->lane_out;
84421ae08745Sheppo 	}
84431ae08745Sheppo 
84441ae08745Sheppo 	lp->lstate = VSW_LANE_INACTIV;
8445d10e4ef2Snarayan 	mutex_enter(&lp->seq_lock);
84461ae08745Sheppo 	lp->seq_num = VNET_ISS;
8447d10e4ef2Snarayan 	mutex_exit(&lp->seq_lock);
84481ae08745Sheppo 	if (lp->dringp) {
84491ae08745Sheppo 		if (dir == INBOUND) {
8450445b4c2eSsb155480 			WRITE_ENTER(&lp->dlistrw);
84511ae08745Sheppo 			dp = lp->dringp;
84521ae08745Sheppo 			while (dp != NULL) {
84531ae08745Sheppo 				dpp = dp->next;
84541ae08745Sheppo 				if (dp->handle != NULL)
84551ae08745Sheppo 					(void) ldc_mem_dring_unmap(dp->handle);
84561ae08745Sheppo 				kmem_free(dp, sizeof (dring_info_t));
84571ae08745Sheppo 				dp = dpp;
84581ae08745Sheppo 			}
8459445b4c2eSsb155480 			RW_EXIT(&lp->dlistrw);
84601ae08745Sheppo 		} else {
84611ae08745Sheppo 			/*
84621ae08745Sheppo 			 * unbind, destroy exported dring, free dring struct
84631ae08745Sheppo 			 */
8464445b4c2eSsb155480 			WRITE_ENTER(&lp->dlistrw);
84651ae08745Sheppo 			dp = lp->dringp;
84661ae08745Sheppo 			rv = vsw_free_ring(dp);
8467445b4c2eSsb155480 			RW_EXIT(&lp->dlistrw);
84681ae08745Sheppo 		}
84691ae08745Sheppo 		if (rv == 0) {
84701ae08745Sheppo 			lp->dringp = NULL;
84711ae08745Sheppo 		}
84721ae08745Sheppo 	}
84731ae08745Sheppo 
84741ae08745Sheppo 	D1(ldcp->ldc_vswp, "%s (%lld): exit", __func__, ldcp->ldc_id);
84751ae08745Sheppo }
84761ae08745Sheppo 
84771ae08745Sheppo /*
84781ae08745Sheppo  * Free ring and all associated resources.
8479445b4c2eSsb155480  *
8480445b4c2eSsb155480  * Should be called with dlistrw rwlock held as writer.
84811ae08745Sheppo  */
84821ae08745Sheppo static int
84831ae08745Sheppo vsw_free_ring(dring_info_t *dp)
84841ae08745Sheppo {
84851ae08745Sheppo 	vsw_private_desc_t	*paddr = NULL;
84861ae08745Sheppo 	dring_info_t		*dpp;
84871ae08745Sheppo 	int			i, rv = 1;
84881ae08745Sheppo 
84891ae08745Sheppo 	while (dp != NULL) {
84901ae08745Sheppo 		mutex_enter(&dp->dlock);
84911ae08745Sheppo 		dpp = dp->next;
84921ae08745Sheppo 		if (dp->priv_addr != NULL) {
84931ae08745Sheppo 			/*
84941ae08745Sheppo 			 * First unbind and free the memory handles
84951ae08745Sheppo 			 * stored in each descriptor within the ring.
84961ae08745Sheppo 			 */
84971ae08745Sheppo 			for (i = 0; i < VSW_RING_NUM_EL; i++) {
84981ae08745Sheppo 				paddr = (vsw_private_desc_t *)
84991ae08745Sheppo 						dp->priv_addr + i;
85001ae08745Sheppo 				if (paddr->memhandle != NULL) {
85011ae08745Sheppo 					if (paddr->bound == 1) {
85021ae08745Sheppo 						rv = ldc_mem_unbind_handle(
85031ae08745Sheppo 							paddr->memhandle);
85041ae08745Sheppo 
85051ae08745Sheppo 						if (rv != 0) {
85061ae08745Sheppo 							DERR(NULL, "error "
85071ae08745Sheppo 							"unbinding handle for "
85081ae08745Sheppo 							"ring 0x%llx at pos %d",
85091ae08745Sheppo 							dp, i);
85101ae08745Sheppo 							mutex_exit(&dp->dlock);
85111ae08745Sheppo 							return (rv);
85121ae08745Sheppo 						}
85131ae08745Sheppo 						paddr->bound = 0;
85141ae08745Sheppo 					}
85151ae08745Sheppo 
85161ae08745Sheppo 					rv = ldc_mem_free_handle(
85171ae08745Sheppo 							paddr->memhandle);
85181ae08745Sheppo 					if (rv != 0) {
85191ae08745Sheppo 						DERR(NULL, "error freeing "
85201ae08745Sheppo 							"handle for ring "
85211ae08745Sheppo 							"0x%llx at pos %d",
85221ae08745Sheppo 							dp, i);
85231ae08745Sheppo 						mutex_exit(&dp->dlock);
85241ae08745Sheppo 						return (rv);
85251ae08745Sheppo 					}
85261ae08745Sheppo 					paddr->memhandle = NULL;
85271ae08745Sheppo 				}
8528d10e4ef2Snarayan 				mutex_destroy(&paddr->dstate_lock);
85291ae08745Sheppo 			}
85301ae08745Sheppo 			kmem_free(dp->priv_addr, (sizeof (vsw_private_desc_t)
85311ae08745Sheppo 					* VSW_RING_NUM_EL));
85321ae08745Sheppo 		}
85331ae08745Sheppo 
85341ae08745Sheppo 		/*
85351ae08745Sheppo 		 * Now unbind and destroy the ring itself.
85361ae08745Sheppo 		 */
85371ae08745Sheppo 		if (dp->handle != NULL) {
85381ae08745Sheppo 			(void) ldc_mem_dring_unbind(dp->handle);
85391ae08745Sheppo 			(void) ldc_mem_dring_destroy(dp->handle);
85401ae08745Sheppo 		}
85411ae08745Sheppo 
85421ae08745Sheppo 		if (dp->data_addr != NULL) {
85431ae08745Sheppo 			kmem_free(dp->data_addr, dp->data_sz);
85441ae08745Sheppo 		}
85451ae08745Sheppo 
85461ae08745Sheppo 		mutex_exit(&dp->dlock);
85471ae08745Sheppo 		mutex_destroy(&dp->dlock);
8548d10e4ef2Snarayan 		mutex_destroy(&dp->restart_lock);
85491ae08745Sheppo 		kmem_free(dp, sizeof (dring_info_t));
85501ae08745Sheppo 
85511ae08745Sheppo 		dp = dpp;
85521ae08745Sheppo 	}
85531ae08745Sheppo 	return (0);
85541ae08745Sheppo }
85551ae08745Sheppo 
85561ae08745Sheppo /*
85571ae08745Sheppo  * Debugging routines
85581ae08745Sheppo  */
85591ae08745Sheppo static void
85601ae08745Sheppo display_state(void)
85611ae08745Sheppo {
85621ae08745Sheppo 	vsw_t		*vswp;
85631ae08745Sheppo 	vsw_port_list_t	*plist;
85641ae08745Sheppo 	vsw_port_t 	*port;
85651ae08745Sheppo 	vsw_ldc_list_t	*ldcl;
85661ae08745Sheppo 	vsw_ldc_t 	*ldcp;
85671ae08745Sheppo 
85681ae08745Sheppo 	cmn_err(CE_NOTE, "***** system state *****");
85691ae08745Sheppo 
85701ae08745Sheppo 	for (vswp = vsw_head; vswp; vswp = vswp->next) {
85711ae08745Sheppo 		plist = &vswp->plist;
85721ae08745Sheppo 		READ_ENTER(&plist->lockrw);
85731ae08745Sheppo 		cmn_err(CE_CONT, "vsw instance %d has %d ports attached\n",
85741ae08745Sheppo 			vswp->instance, plist->num_ports);
85751ae08745Sheppo 
85761ae08745Sheppo 		for (port = plist->head; port != NULL; port = port->p_next) {
85771ae08745Sheppo 			ldcl = &port->p_ldclist;
85781ae08745Sheppo 			cmn_err(CE_CONT, "port %d : %d ldcs attached\n",
85791ae08745Sheppo 				port->p_instance, ldcl->num_ldcs);
85801ae08745Sheppo 			READ_ENTER(&ldcl->lockrw);
85811ae08745Sheppo 			ldcp = ldcl->head;
85821ae08745Sheppo 			for (; ldcp != NULL; ldcp = ldcp->ldc_next) {
85831ae08745Sheppo 				cmn_err(CE_CONT, "chan %lu : dev %d : "
85841ae08745Sheppo 					"status %d : phase %u\n",
85851ae08745Sheppo 					ldcp->ldc_id, ldcp->dev_class,
85861ae08745Sheppo 					ldcp->ldc_status, ldcp->hphase);
85871ae08745Sheppo 				cmn_err(CE_CONT, "chan %lu : lsession %lu : "
85881ae08745Sheppo 					"psession %lu\n",
85891ae08745Sheppo 					ldcp->ldc_id,
85901ae08745Sheppo 					ldcp->local_session,
85911ae08745Sheppo 					ldcp->peer_session);
85921ae08745Sheppo 
85931ae08745Sheppo 				cmn_err(CE_CONT, "Inbound lane:\n");
85941ae08745Sheppo 				display_lane(&ldcp->lane_in);
85951ae08745Sheppo 				cmn_err(CE_CONT, "Outbound lane:\n");
85961ae08745Sheppo 				display_lane(&ldcp->lane_out);
85971ae08745Sheppo 			}
85981ae08745Sheppo 			RW_EXIT(&ldcl->lockrw);
85991ae08745Sheppo 		}
86001ae08745Sheppo 		RW_EXIT(&plist->lockrw);
86011ae08745Sheppo 	}
86021ae08745Sheppo 	cmn_err(CE_NOTE, "***** system state *****");
86031ae08745Sheppo }
86041ae08745Sheppo 
86051ae08745Sheppo static void
86061ae08745Sheppo display_lane(lane_t *lp)
86071ae08745Sheppo {
86081ae08745Sheppo 	dring_info_t	*drp;
86091ae08745Sheppo 
86101ae08745Sheppo 	cmn_err(CE_CONT, "ver 0x%x:0x%x : state %lx : mtu 0x%lx\n",
86111ae08745Sheppo 		lp->ver_major, lp->ver_minor, lp->lstate, lp->mtu);
86121ae08745Sheppo 	cmn_err(CE_CONT, "addr_type %d : addr 0x%lx : xmode %d\n",
86131ae08745Sheppo 		lp->addr_type, lp->addr, lp->xfer_mode);
86141ae08745Sheppo 	cmn_err(CE_CONT, "dringp 0x%lx\n", (uint64_t)lp->dringp);
86151ae08745Sheppo 
86161ae08745Sheppo 	cmn_err(CE_CONT, "Dring info:\n");
86171ae08745Sheppo 	for (drp = lp->dringp; drp != NULL; drp = drp->next) {
86181ae08745Sheppo 		cmn_err(CE_CONT, "\tnum_desc %u : dsize %u\n",
86191ae08745Sheppo 			drp->num_descriptors, drp->descriptor_size);
86201ae08745Sheppo 		cmn_err(CE_CONT, "\thandle 0x%lx\n", drp->handle);
86211ae08745Sheppo 		cmn_err(CE_CONT, "\tpub_addr 0x%lx : priv_addr 0x%lx\n",
86221ae08745Sheppo 			(uint64_t)drp->pub_addr, (uint64_t)drp->priv_addr);
86231ae08745Sheppo 		cmn_err(CE_CONT, "\tident 0x%lx : end_idx %lu\n",
86241ae08745Sheppo 			drp->ident, drp->end_idx);
86251ae08745Sheppo 		display_ring(drp);
86261ae08745Sheppo 	}
86271ae08745Sheppo }
86281ae08745Sheppo 
86291ae08745Sheppo static void
86301ae08745Sheppo display_ring(dring_info_t *dringp)
86311ae08745Sheppo {
86321ae08745Sheppo 	uint64_t		i;
86331ae08745Sheppo 	uint64_t		priv_count = 0;
86341ae08745Sheppo 	uint64_t		pub_count = 0;
86351ae08745Sheppo 	vnet_public_desc_t	*pub_addr = NULL;
86361ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
86371ae08745Sheppo 
86381ae08745Sheppo 	for (i = 0; i < VSW_RING_NUM_EL; i++) {
86391ae08745Sheppo 		if (dringp->pub_addr != NULL) {
86401ae08745Sheppo 			pub_addr = (vnet_public_desc_t *)dringp->pub_addr + i;
86411ae08745Sheppo 
86421ae08745Sheppo 			if (pub_addr->hdr.dstate == VIO_DESC_FREE)
86431ae08745Sheppo 				pub_count++;
86441ae08745Sheppo 		}
86451ae08745Sheppo 
86461ae08745Sheppo 		if (dringp->priv_addr != NULL) {
86471ae08745Sheppo 			priv_addr =
86481ae08745Sheppo 				(vsw_private_desc_t *)dringp->priv_addr + i;
86491ae08745Sheppo 
86501ae08745Sheppo 			if (priv_addr->dstate == VIO_DESC_FREE)
86511ae08745Sheppo 				priv_count++;
86521ae08745Sheppo 		}
86531ae08745Sheppo 	}
86541ae08745Sheppo 	cmn_err(CE_CONT, "\t%lu elements: %lu priv free: %lu pub free\n",
86551ae08745Sheppo 			i, priv_count, pub_count);
86561ae08745Sheppo }
86571ae08745Sheppo 
86581ae08745Sheppo static void
86591ae08745Sheppo dump_flags(uint64_t state)
86601ae08745Sheppo {
86611ae08745Sheppo 	int	i;
86621ae08745Sheppo 
86631ae08745Sheppo 	typedef struct flag_name {
86641ae08745Sheppo 		int	flag_val;
86651ae08745Sheppo 		char	*flag_name;
86661ae08745Sheppo 	} flag_name_t;
86671ae08745Sheppo 
86681ae08745Sheppo 	flag_name_t	flags[] = {
86691ae08745Sheppo 		VSW_VER_INFO_SENT, "VSW_VER_INFO_SENT",
86701ae08745Sheppo 		VSW_VER_INFO_RECV, "VSW_VER_INFO_RECV",
86711ae08745Sheppo 		VSW_VER_ACK_RECV, "VSW_VER_ACK_RECV",
86721ae08745Sheppo 		VSW_VER_ACK_SENT, "VSW_VER_ACK_SENT",
86731ae08745Sheppo 		VSW_VER_NACK_RECV, "VSW_VER_NACK_RECV",
86741ae08745Sheppo 		VSW_VER_NACK_SENT, "VSW_VER_NACK_SENT",
86751ae08745Sheppo 		VSW_ATTR_INFO_SENT, "VSW_ATTR_INFO_SENT",
86761ae08745Sheppo 		VSW_ATTR_INFO_RECV, "VSW_ATTR_INFO_RECV",
86771ae08745Sheppo 		VSW_ATTR_ACK_SENT, "VSW_ATTR_ACK_SENT",
86781ae08745Sheppo 		VSW_ATTR_ACK_RECV, "VSW_ATTR_ACK_RECV",
86791ae08745Sheppo 		VSW_ATTR_NACK_SENT, "VSW_ATTR_NACK_SENT",
86801ae08745Sheppo 		VSW_ATTR_NACK_RECV, "VSW_ATTR_NACK_RECV",
86811ae08745Sheppo 		VSW_DRING_INFO_SENT, "VSW_DRING_INFO_SENT",
86821ae08745Sheppo 		VSW_DRING_INFO_RECV, "VSW_DRING_INFO_RECV",
86831ae08745Sheppo 		VSW_DRING_ACK_SENT, "VSW_DRING_ACK_SENT",
86841ae08745Sheppo 		VSW_DRING_ACK_RECV, "VSW_DRING_ACK_RECV",
86851ae08745Sheppo 		VSW_DRING_NACK_SENT, "VSW_DRING_NACK_SENT",
86861ae08745Sheppo 		VSW_DRING_NACK_RECV, "VSW_DRING_NACK_RECV",
86871ae08745Sheppo 		VSW_RDX_INFO_SENT, "VSW_RDX_INFO_SENT",
86881ae08745Sheppo 		VSW_RDX_INFO_RECV, "VSW_RDX_INFO_RECV",
86891ae08745Sheppo 		VSW_RDX_ACK_SENT, "VSW_RDX_ACK_SENT",
86901ae08745Sheppo 		VSW_RDX_ACK_RECV, "VSW_RDX_ACK_RECV",
86911ae08745Sheppo 		VSW_RDX_NACK_SENT, "VSW_RDX_NACK_SENT",
86921ae08745Sheppo 		VSW_RDX_NACK_RECV, "VSW_RDX_NACK_RECV",
86931ae08745Sheppo 		VSW_MCST_INFO_SENT, "VSW_MCST_INFO_SENT",
86941ae08745Sheppo 		VSW_MCST_INFO_RECV, "VSW_MCST_INFO_RECV",
86951ae08745Sheppo 		VSW_MCST_ACK_SENT, "VSW_MCST_ACK_SENT",
86961ae08745Sheppo 		VSW_MCST_ACK_RECV, "VSW_MCST_ACK_RECV",
86971ae08745Sheppo 		VSW_MCST_NACK_SENT, "VSW_MCST_NACK_SENT",
86981ae08745Sheppo 		VSW_MCST_NACK_RECV, "VSW_MCST_NACK_RECV",
86991ae08745Sheppo 		VSW_LANE_ACTIVE, "VSW_LANE_ACTIVE"};
87001ae08745Sheppo 
87011ae08745Sheppo 	DERR(NULL, "DUMP_FLAGS: %llx\n", state);
87021ae08745Sheppo 	for (i = 0; i < sizeof (flags)/sizeof (flag_name_t); i++) {
87031ae08745Sheppo 		if (state & flags[i].flag_val)
87041ae08745Sheppo 			DERR(NULL, "DUMP_FLAGS %s", flags[i].flag_name);
87051ae08745Sheppo 	}
87061ae08745Sheppo }
8707