xref: /titanic_53/usr/src/uts/sun4v/io/vsw.c (revision da86a4da4df500ceaeb1a164540967b4109e81c2)
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 /*
23b071742bSsg70180  * 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 *);
995f94e909Ssg70180 static	int vsw_set_hw(vsw_t *, vsw_port_t *, int);
1005f94e909Ssg70180 static	int vsw_set_hw_addr(vsw_t *, mac_multi_addr_t *);
1015f94e909Ssg70180 static	int vsw_set_hw_promisc(vsw_t *, vsw_port_t *, int);
1025f94e909Ssg70180 static	int vsw_unset_hw(vsw_t *, vsw_port_t *, int);
1035f94e909Ssg70180 static	int vsw_unset_hw_addr(vsw_t *, int);
1045f94e909Ssg70180 static	int vsw_unset_hw_promisc(vsw_t *, vsw_port_t *, int);
1055f94e909Ssg70180 static void vsw_reconfig_hw(vsw_t *);
1065f94e909Ssg70180 static int vsw_prog_if(vsw_t *);
1075f94e909Ssg70180 static int vsw_prog_ports(vsw_t *);
1087636cb21Slm66018 static int vsw_mac_attach(vsw_t *vswp);
1097636cb21Slm66018 static void vsw_mac_detach(vsw_t *vswp);
1107636cb21Slm66018 
1117636cb21Slm66018 static void vsw_rx_queue_cb(void *, mac_resource_handle_t, mblk_t *);
1121ae08745Sheppo static void vsw_rx_cb(void *, mac_resource_handle_t, mblk_t *);
1131ae08745Sheppo static mblk_t *vsw_tx_msg(vsw_t *, mblk_t *);
1141ae08745Sheppo static int vsw_mac_register(vsw_t *);
1151ae08745Sheppo static int vsw_mac_unregister(vsw_t *);
116ba2e4443Sseb static int vsw_m_stat(void *, uint_t, uint64_t *);
1171ae08745Sheppo static void vsw_m_stop(void *arg);
1181ae08745Sheppo static int vsw_m_start(void *arg);
1191ae08745Sheppo static int vsw_m_unicst(void *arg, const uint8_t *);
1201ae08745Sheppo static int vsw_m_multicst(void *arg, boolean_t, const uint8_t *);
1211ae08745Sheppo static int vsw_m_promisc(void *arg, boolean_t);
1221ae08745Sheppo static mblk_t *vsw_m_tx(void *arg, mblk_t *);
1231ae08745Sheppo 
1241ae08745Sheppo /* MDEG routines */
12534683adeSsg70180 static	int vsw_mdeg_register(vsw_t *vswp);
1261ae08745Sheppo static	void vsw_mdeg_unregister(vsw_t *vswp);
1271ae08745Sheppo static	int vsw_mdeg_cb(void *cb_argp, mdeg_result_t *);
12834683adeSsg70180 static	int vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *);
12934683adeSsg70180 static	void vsw_get_initial_md_properties(vsw_t *vswp, md_t *, mde_cookie_t);
13034683adeSsg70180 static	void vsw_update_md_prop(vsw_t *, md_t *, mde_cookie_t);
1311ae08745Sheppo 
1321ae08745Sheppo /* Port add/deletion routines */
1331ae08745Sheppo static	int vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node);
1341ae08745Sheppo static	int vsw_port_attach(vsw_t *vswp, int p_instance,
1351ae08745Sheppo 	uint64_t *ldcids, int nids, struct ether_addr *macaddr);
1361ae08745Sheppo static	int vsw_detach_ports(vsw_t *vswp);
1371ae08745Sheppo static	int vsw_port_detach(vsw_t *vswp, int p_instance);
1381ae08745Sheppo static	int vsw_port_delete(vsw_port_t *port);
1391ae08745Sheppo static	int vsw_ldc_attach(vsw_port_t *port, uint64_t ldc_id);
1401ae08745Sheppo static	int vsw_ldc_detach(vsw_port_t *port, uint64_t ldc_id);
1411ae08745Sheppo static	int vsw_init_ldcs(vsw_port_t *port);
1421ae08745Sheppo static	int vsw_uninit_ldcs(vsw_port_t *port);
1431ae08745Sheppo static	int vsw_ldc_init(vsw_ldc_t *ldcp);
1441ae08745Sheppo static	int vsw_ldc_uninit(vsw_ldc_t *ldcp);
1451ae08745Sheppo static	int vsw_drain_ldcs(vsw_port_t *port);
1461ae08745Sheppo static	int vsw_drain_port_taskq(vsw_port_t *port);
1471ae08745Sheppo static	void vsw_marker_task(void *);
1481ae08745Sheppo static	vsw_port_t *vsw_lookup_port(vsw_t *vswp, int p_instance);
1491ae08745Sheppo static	int vsw_plist_del_node(vsw_t *, vsw_port_t *port);
1501ae08745Sheppo 
1511ae08745Sheppo /* Interrupt routines */
1521ae08745Sheppo static	uint_t vsw_ldc_cb(uint64_t cb, caddr_t arg);
1531ae08745Sheppo 
1541ae08745Sheppo /* Handshake routines */
155b071742bSsg70180 static	void vsw_ldc_reinit(vsw_ldc_t *);
156b071742bSsg70180 static	void vsw_process_conn_evt(vsw_ldc_t *, uint16_t);
157b071742bSsg70180 static	void vsw_conn_task(void *);
1581ae08745Sheppo static	int vsw_check_flag(vsw_ldc_t *, int, uint64_t);
1591ae08745Sheppo static	void vsw_next_milestone(vsw_ldc_t *);
1601ae08745Sheppo static	int vsw_supported_version(vio_ver_msg_t *);
1611ae08745Sheppo 
1621ae08745Sheppo /* Data processing routines */
1631ae08745Sheppo static void vsw_process_pkt(void *);
1641ae08745Sheppo static void vsw_dispatch_ctrl_task(vsw_ldc_t *, void *, vio_msg_tag_t);
1651ae08745Sheppo static void vsw_process_ctrl_pkt(void *);
1661ae08745Sheppo static void vsw_process_ctrl_ver_pkt(vsw_ldc_t *, void *);
1671ae08745Sheppo static void vsw_process_ctrl_attr_pkt(vsw_ldc_t *, void *);
1681ae08745Sheppo static void vsw_process_ctrl_mcst_pkt(vsw_ldc_t *, void *);
1691ae08745Sheppo static void vsw_process_ctrl_dring_reg_pkt(vsw_ldc_t *, void *);
1701ae08745Sheppo static void vsw_process_ctrl_dring_unreg_pkt(vsw_ldc_t *, void *);
1711ae08745Sheppo static void vsw_process_ctrl_rdx_pkt(vsw_ldc_t *, void *);
1721ae08745Sheppo static void vsw_process_data_pkt(vsw_ldc_t *, void *, vio_msg_tag_t);
1731ae08745Sheppo static void vsw_process_data_dring_pkt(vsw_ldc_t *, void *);
1741ae08745Sheppo static void vsw_process_data_raw_pkt(vsw_ldc_t *, void *);
1751ae08745Sheppo static void vsw_process_data_ibnd_pkt(vsw_ldc_t *, void *);
1761ae08745Sheppo static void vsw_process_err_pkt(vsw_ldc_t *, void *, vio_msg_tag_t);
1771ae08745Sheppo 
1781ae08745Sheppo /* Switching/data transmit routines */
1791ae08745Sheppo static	void vsw_switch_l2_frame(vsw_t *vswp, mblk_t *mp, int caller,
1801ae08745Sheppo 	    vsw_port_t *port, mac_resource_handle_t);
1811ae08745Sheppo static	void vsw_switch_l3_frame(vsw_t *vswp, mblk_t *mp, int caller,
1821ae08745Sheppo 	    vsw_port_t *port, mac_resource_handle_t);
1831ae08745Sheppo static	int vsw_forward_all(vsw_t *vswp, mblk_t *mp, int caller,
1841ae08745Sheppo 	    vsw_port_t *port);
1851ae08745Sheppo static	int vsw_forward_grp(vsw_t *vswp, mblk_t *mp, int caller,
1861ae08745Sheppo 	    vsw_port_t *port);
1871ae08745Sheppo static	int vsw_portsend(vsw_port_t *, mblk_t *);
1881ae08745Sheppo static	int vsw_dringsend(vsw_ldc_t *, mblk_t *);
1891ae08745Sheppo static	int vsw_descrsend(vsw_ldc_t *, mblk_t *);
1901ae08745Sheppo 
1911ae08745Sheppo /* Packet creation routines */
1923af08d82Slm66018 static void vsw_send_ver(void *);
1931ae08745Sheppo static void vsw_send_attr(vsw_ldc_t *);
1941ae08745Sheppo static vio_dring_reg_msg_t *vsw_create_dring_info_pkt(vsw_ldc_t *);
1951ae08745Sheppo static void vsw_send_dring_info(vsw_ldc_t *);
1961ae08745Sheppo static void vsw_send_rdx(vsw_ldc_t *);
1971ae08745Sheppo 
198b071742bSsg70180 static int vsw_send_msg(vsw_ldc_t *, void *, int, boolean_t);
1991ae08745Sheppo 
2001ae08745Sheppo /* Forwarding database (FDB) routines */
2011ae08745Sheppo static	int vsw_add_fdb(vsw_t *vswp, vsw_port_t *port);
2021ae08745Sheppo static	int vsw_del_fdb(vsw_t *vswp, vsw_port_t *port);
2031ae08745Sheppo static	vsw_port_t *vsw_lookup_fdb(vsw_t *vswp, struct ether_header *);
2041ae08745Sheppo static	int vsw_add_rem_mcst(vnet_mcast_msg_t *, vsw_port_t *);
2051ae08745Sheppo static	int vsw_add_mcst(vsw_t *, uint8_t, uint64_t, void *);
2061ae08745Sheppo static	int vsw_del_mcst(vsw_t *, uint8_t, uint64_t, void *);
2071ae08745Sheppo static	void vsw_del_addr(uint8_t, void *, uint64_t);
2081ae08745Sheppo static	void vsw_del_mcst_port(vsw_port_t *);
2091ae08745Sheppo static	void vsw_del_mcst_vsw(vsw_t *);
2101ae08745Sheppo 
2111ae08745Sheppo /* Dring routines */
2121ae08745Sheppo static dring_info_t *vsw_create_dring(vsw_ldc_t *);
2131ae08745Sheppo static void vsw_create_privring(vsw_ldc_t *);
2141ae08745Sheppo static int vsw_setup_ring(vsw_ldc_t *ldcp, dring_info_t *dp);
2151ae08745Sheppo static int vsw_dring_find_free_desc(dring_info_t *, vsw_private_desc_t **,
2161ae08745Sheppo     int *);
2171ae08745Sheppo static dring_info_t *vsw_ident2dring(lane_t *, uint64_t);
2181ae08745Sheppo 
2191ae08745Sheppo static void vsw_set_lane_attr(vsw_t *, lane_t *);
2201ae08745Sheppo static int vsw_check_attr(vnet_attr_msg_t *, vsw_port_t *);
2211ae08745Sheppo static int vsw_dring_match(dring_info_t *dp, vio_dring_reg_msg_t *msg);
2221ae08745Sheppo static int vsw_mem_cookie_match(ldc_mem_cookie_t *, ldc_mem_cookie_t *);
2231ae08745Sheppo static int vsw_check_dring_info(vio_dring_reg_msg_t *);
2241ae08745Sheppo 
2251ae08745Sheppo /* Misc support routines */
2261ae08745Sheppo static	caddr_t vsw_print_ethaddr(uint8_t *addr, char *ebuf);
2271ae08745Sheppo static void vsw_free_lane_resources(vsw_ldc_t *, uint64_t);
2281ae08745Sheppo static int vsw_free_ring(dring_info_t *);
2291ae08745Sheppo 
2301ae08745Sheppo /* Debugging routines */
2311ae08745Sheppo static void dump_flags(uint64_t);
2321ae08745Sheppo static void display_state(void);
2331ae08745Sheppo static void display_lane(lane_t *);
2341ae08745Sheppo static void display_ring(dring_info_t *);
2351ae08745Sheppo 
236445b4c2eSsb155480 int	vsw_num_handshakes = VNET_NUM_HANDSHAKES; /* # of handshake attempts */
2371ae08745Sheppo int	vsw_wretries = 100;		/* # of write attempts */
238d10e4ef2Snarayan int	vsw_chain_len = 150;		/* max # of mblks in msg chain */
239d10e4ef2Snarayan int	vsw_desc_delay = 0;		/* delay in us */
240d10e4ef2Snarayan int	vsw_read_attempts = 5;		/* # of reads of descriptor */
241d10e4ef2Snarayan 
242d10e4ef2Snarayan uint32_t	vsw_mblk_size = VSW_MBLK_SIZE;
243d10e4ef2Snarayan uint32_t	vsw_num_mblks = VSW_NUM_MBLKS;
244d10e4ef2Snarayan 
245ba2e4443Sseb static	mac_callbacks_t	vsw_m_callbacks = {
246ba2e4443Sseb 	0,
247ba2e4443Sseb 	vsw_m_stat,
248ba2e4443Sseb 	vsw_m_start,
249ba2e4443Sseb 	vsw_m_stop,
250ba2e4443Sseb 	vsw_m_promisc,
251ba2e4443Sseb 	vsw_m_multicst,
252ba2e4443Sseb 	vsw_m_unicst,
253ba2e4443Sseb 	vsw_m_tx,
254ba2e4443Sseb 	NULL,
255ba2e4443Sseb 	NULL,
256ba2e4443Sseb 	NULL
257ba2e4443Sseb };
258ba2e4443Sseb 
2591ae08745Sheppo static	struct	cb_ops	vsw_cb_ops = {
2601ae08745Sheppo 	nulldev,			/* cb_open */
2611ae08745Sheppo 	nulldev,			/* cb_close */
2621ae08745Sheppo 	nodev,				/* cb_strategy */
2631ae08745Sheppo 	nodev,				/* cb_print */
2641ae08745Sheppo 	nodev,				/* cb_dump */
2651ae08745Sheppo 	nodev,				/* cb_read */
2661ae08745Sheppo 	nodev,				/* cb_write */
2671ae08745Sheppo 	nodev,				/* cb_ioctl */
2681ae08745Sheppo 	nodev,				/* cb_devmap */
2691ae08745Sheppo 	nodev,				/* cb_mmap */
2701ae08745Sheppo 	nodev,				/* cb_segmap */
2711ae08745Sheppo 	nochpoll,			/* cb_chpoll */
2721ae08745Sheppo 	ddi_prop_op,			/* cb_prop_op */
2731ae08745Sheppo 	NULL,				/* cb_stream */
2741ae08745Sheppo 	D_MP,				/* cb_flag */
2751ae08745Sheppo 	CB_REV,				/* rev */
2761ae08745Sheppo 	nodev,				/* int (*cb_aread)() */
2771ae08745Sheppo 	nodev				/* int (*cb_awrite)() */
2781ae08745Sheppo };
2791ae08745Sheppo 
2801ae08745Sheppo static	struct	dev_ops	vsw_ops = {
2811ae08745Sheppo 	DEVO_REV,		/* devo_rev */
2821ae08745Sheppo 	0,			/* devo_refcnt */
2831ae08745Sheppo 	vsw_getinfo,		/* devo_getinfo */
2841ae08745Sheppo 	nulldev,		/* devo_identify */
2851ae08745Sheppo 	nulldev,		/* devo_probe */
2861ae08745Sheppo 	vsw_attach,		/* devo_attach */
2871ae08745Sheppo 	vsw_detach,		/* devo_detach */
2881ae08745Sheppo 	nodev,			/* devo_reset */
2891ae08745Sheppo 	&vsw_cb_ops,		/* devo_cb_ops */
2901ae08745Sheppo 	(struct bus_ops *)NULL,	/* devo_bus_ops */
2911ae08745Sheppo 	ddi_power		/* devo_power */
2921ae08745Sheppo };
2931ae08745Sheppo 
2941ae08745Sheppo extern	struct	mod_ops	mod_driverops;
2951ae08745Sheppo static struct modldrv vswmodldrv = {
2961ae08745Sheppo 	&mod_driverops,
297205eeb1aSlm66018 	"sun4v Virtual Switch",
2981ae08745Sheppo 	&vsw_ops,
2991ae08745Sheppo };
3001ae08745Sheppo 
3011ae08745Sheppo #define	LDC_ENTER_LOCK(ldcp)	\
3021ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_cblock));\
3031ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_txlock));
3041ae08745Sheppo #define	LDC_EXIT_LOCK(ldcp)	\
3051ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_txlock));\
3061ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_cblock));
3071ae08745Sheppo 
3081ae08745Sheppo /* Driver soft state ptr  */
3091ae08745Sheppo static void	*vsw_state;
3101ae08745Sheppo 
3111ae08745Sheppo /*
3121ae08745Sheppo  * Linked list of "vsw_t" structures - one per instance.
3131ae08745Sheppo  */
3141ae08745Sheppo vsw_t		*vsw_head = NULL;
3151ae08745Sheppo krwlock_t	vsw_rw;
3161ae08745Sheppo 
3171ae08745Sheppo /*
3181ae08745Sheppo  * Property names
3191ae08745Sheppo  */
3201ae08745Sheppo static char vdev_propname[] = "virtual-device";
3211ae08745Sheppo static char vsw_propname[] = "virtual-network-switch";
3221ae08745Sheppo static char physdev_propname[] = "vsw-phys-dev";
3231ae08745Sheppo static char smode_propname[] = "vsw-switch-mode";
3241ae08745Sheppo static char macaddr_propname[] = "local-mac-address";
3251ae08745Sheppo static char remaddr_propname[] = "remote-mac-address";
3261ae08745Sheppo static char ldcids_propname[] = "ldc-ids";
3271ae08745Sheppo static char chan_propname[] = "channel-endpoint";
3281ae08745Sheppo static char id_propname[] = "id";
3291ae08745Sheppo static char reg_propname[] = "reg";
3301ae08745Sheppo 
3311ae08745Sheppo /* supported versions */
3321ae08745Sheppo static	ver_sup_t	vsw_versions[] = { {1, 0} };
3331ae08745Sheppo 
3341ae08745Sheppo /*
3351ae08745Sheppo  * Matching criteria passed to the MDEG to register interest
3361ae08745Sheppo  * in changes to 'virtual-device-port' nodes identified by their
3371ae08745Sheppo  * 'id' property.
3381ae08745Sheppo  */
3391ae08745Sheppo static md_prop_match_t vport_prop_match[] = {
3401ae08745Sheppo 	{ MDET_PROP_VAL,    "id"   },
3411ae08745Sheppo 	{ MDET_LIST_END,    NULL    }
3421ae08745Sheppo };
3431ae08745Sheppo 
3441ae08745Sheppo static mdeg_node_match_t vport_match = { "virtual-device-port",
3451ae08745Sheppo 						vport_prop_match };
3461ae08745Sheppo 
3471ae08745Sheppo /*
34834683adeSsg70180  * Matching criteria passed to the MDEG to register interest
34934683adeSsg70180  * in changes to 'virtual-device' nodes (i.e. vsw nodes) identified
35034683adeSsg70180  * by their 'name' and 'cfg-handle' properties.
35134683adeSsg70180  */
35234683adeSsg70180 static md_prop_match_t vdev_prop_match[] = {
35334683adeSsg70180 	{ MDET_PROP_STR,    "name"   },
35434683adeSsg70180 	{ MDET_PROP_VAL,    "cfg-handle" },
35534683adeSsg70180 	{ MDET_LIST_END,    NULL    }
35634683adeSsg70180 };
35734683adeSsg70180 
35834683adeSsg70180 static mdeg_node_match_t vdev_match = { "virtual-device",
35934683adeSsg70180 						vdev_prop_match };
36034683adeSsg70180 
36134683adeSsg70180 
36234683adeSsg70180 /*
3631ae08745Sheppo  * Specification of an MD node passed to the MDEG to filter any
3641ae08745Sheppo  * 'vport' nodes that do not belong to the specified node. This
3651ae08745Sheppo  * template is copied for each vsw instance and filled in with
3661ae08745Sheppo  * the appropriate 'cfg-handle' value before being passed to the MDEG.
3671ae08745Sheppo  */
3681ae08745Sheppo static mdeg_prop_spec_t vsw_prop_template[] = {
3691ae08745Sheppo 	{ MDET_PROP_STR,    "name",		vsw_propname },
3701ae08745Sheppo 	{ MDET_PROP_VAL,    "cfg-handle",	NULL	},
3711ae08745Sheppo 	{ MDET_LIST_END,    NULL,		NULL	}
3721ae08745Sheppo };
3731ae08745Sheppo 
3741ae08745Sheppo #define	VSW_SET_MDEG_PROP_INST(specp, val)	(specp)[1].ps_val = (val);
3751ae08745Sheppo 
3761ae08745Sheppo /*
3777636cb21Slm66018  * From /etc/system enable/disable thread per ring. This is a mode
3787636cb21Slm66018  * selection that is done a vsw driver attach time.
3797636cb21Slm66018  */
3807636cb21Slm66018 boolean_t vsw_multi_ring_enable = B_FALSE;
3817636cb21Slm66018 int vsw_mac_rx_rings = VSW_MAC_RX_RINGS;
3827636cb21Slm66018 
3837636cb21Slm66018 /*
3841ae08745Sheppo  * Print debug messages - set to 0x1f to enable all msgs
3851ae08745Sheppo  * or 0x0 to turn all off.
3861ae08745Sheppo  */
3871ae08745Sheppo int vswdbg = 0x0;
3881ae08745Sheppo 
3891ae08745Sheppo /*
3901ae08745Sheppo  * debug levels:
3911ae08745Sheppo  * 0x01:	Function entry/exit tracing
3921ae08745Sheppo  * 0x02:	Internal function messages
3931ae08745Sheppo  * 0x04:	Verbose internal messages
3941ae08745Sheppo  * 0x08:	Warning messages
3951ae08745Sheppo  * 0x10:	Error messages
3961ae08745Sheppo  */
3971ae08745Sheppo 
3981ae08745Sheppo static void
3991ae08745Sheppo vswdebug(vsw_t *vswp, const char *fmt, ...)
4001ae08745Sheppo {
4011ae08745Sheppo 	char buf[512];
4021ae08745Sheppo 	va_list ap;
4031ae08745Sheppo 
4041ae08745Sheppo 	va_start(ap, fmt);
4051ae08745Sheppo 	(void) vsprintf(buf, fmt, ap);
4061ae08745Sheppo 	va_end(ap);
4071ae08745Sheppo 
4081ae08745Sheppo 	if (vswp == NULL)
4091ae08745Sheppo 		cmn_err(CE_CONT, "%s\n", buf);
4101ae08745Sheppo 	else
4111ae08745Sheppo 		cmn_err(CE_CONT, "vsw%d: %s\n", vswp->instance, buf);
4121ae08745Sheppo }
4131ae08745Sheppo 
4141ae08745Sheppo /*
4151ae08745Sheppo  * For the moment the state dump routines have their own
4161ae08745Sheppo  * private flag.
4171ae08745Sheppo  */
4181ae08745Sheppo #define	DUMP_STATE	0
4191ae08745Sheppo 
4201ae08745Sheppo #if DUMP_STATE
4211ae08745Sheppo 
4221ae08745Sheppo #define	DUMP_TAG(tag) \
4231ae08745Sheppo {			\
4241ae08745Sheppo 	D1(NULL, "DUMP_TAG: type 0x%llx", (tag).vio_msgtype); \
4251ae08745Sheppo 	D1(NULL, "DUMP_TAG: stype 0x%llx", (tag).vio_subtype);	\
4261ae08745Sheppo 	D1(NULL, "DUMP_TAG: senv 0x%llx", (tag).vio_subtype_env);	\
4271ae08745Sheppo }
4281ae08745Sheppo 
4291ae08745Sheppo #define	DUMP_TAG_PTR(tag) \
4301ae08745Sheppo {			\
4311ae08745Sheppo 	D1(NULL, "DUMP_TAG: type 0x%llx", (tag)->vio_msgtype); \
4321ae08745Sheppo 	D1(NULL, "DUMP_TAG: stype 0x%llx", (tag)->vio_subtype);	\
4331ae08745Sheppo 	D1(NULL, "DUMP_TAG: senv 0x%llx", (tag)->vio_subtype_env);	\
4341ae08745Sheppo }
4351ae08745Sheppo 
4361ae08745Sheppo #define	DUMP_FLAGS(flags) dump_flags(flags);
4371ae08745Sheppo #define	DISPLAY_STATE()	display_state()
4381ae08745Sheppo 
4391ae08745Sheppo #else
4401ae08745Sheppo 
4411ae08745Sheppo #define	DUMP_TAG(tag)
4421ae08745Sheppo #define	DUMP_TAG_PTR(tag)
4431ae08745Sheppo #define	DUMP_FLAGS(state)
4441ae08745Sheppo #define	DISPLAY_STATE()
4451ae08745Sheppo 
4461ae08745Sheppo #endif	/* DUMP_STATE */
4471ae08745Sheppo 
4481ae08745Sheppo #ifdef DEBUG
4491ae08745Sheppo 
4501ae08745Sheppo #define	D1		\
4511ae08745Sheppo if (vswdbg & 0x01)	\
4521ae08745Sheppo 	vswdebug
4531ae08745Sheppo 
4541ae08745Sheppo #define	D2		\
4551ae08745Sheppo if (vswdbg & 0x02)	\
4561ae08745Sheppo 	vswdebug
4571ae08745Sheppo 
4581ae08745Sheppo #define	D3		\
4591ae08745Sheppo if (vswdbg & 0x04)	\
4601ae08745Sheppo 	vswdebug
4611ae08745Sheppo 
4621ae08745Sheppo #define	DWARN		\
4631ae08745Sheppo if (vswdbg & 0x08)	\
4641ae08745Sheppo 	vswdebug
4651ae08745Sheppo 
4661ae08745Sheppo #define	DERR		\
4671ae08745Sheppo if (vswdbg & 0x10)	\
4681ae08745Sheppo 	vswdebug
4691ae08745Sheppo 
4701ae08745Sheppo #else
4711ae08745Sheppo 
4721ae08745Sheppo #define	DERR		if (0)	vswdebug
4731ae08745Sheppo #define	DWARN		if (0)	vswdebug
4741ae08745Sheppo #define	D1		if (0)	vswdebug
4751ae08745Sheppo #define	D2		if (0)	vswdebug
4761ae08745Sheppo #define	D3		if (0)	vswdebug
4771ae08745Sheppo 
4781ae08745Sheppo #endif	/* DEBUG */
4791ae08745Sheppo 
4801ae08745Sheppo static struct modlinkage modlinkage = {
4811ae08745Sheppo 	MODREV_1,
4821ae08745Sheppo 	&vswmodldrv,
4831ae08745Sheppo 	NULL
4841ae08745Sheppo };
4851ae08745Sheppo 
4861ae08745Sheppo int
4871ae08745Sheppo _init(void)
4881ae08745Sheppo {
4891ae08745Sheppo 	int status;
4901ae08745Sheppo 
4911ae08745Sheppo 	rw_init(&vsw_rw, NULL, RW_DRIVER, NULL);
4921ae08745Sheppo 
4931ae08745Sheppo 	status = ddi_soft_state_init(&vsw_state, sizeof (vsw_t), 1);
4941ae08745Sheppo 	if (status != 0) {
4951ae08745Sheppo 		return (status);
4961ae08745Sheppo 	}
4971ae08745Sheppo 
4981ae08745Sheppo 	mac_init_ops(&vsw_ops, "vsw");
4991ae08745Sheppo 	status = mod_install(&modlinkage);
5001ae08745Sheppo 	if (status != 0) {
5011ae08745Sheppo 		ddi_soft_state_fini(&vsw_state);
5021ae08745Sheppo 	}
5031ae08745Sheppo 	return (status);
5041ae08745Sheppo }
5051ae08745Sheppo 
5061ae08745Sheppo int
5071ae08745Sheppo _fini(void)
5081ae08745Sheppo {
5091ae08745Sheppo 	int status;
5101ae08745Sheppo 
5111ae08745Sheppo 	status = mod_remove(&modlinkage);
5121ae08745Sheppo 	if (status != 0)
5131ae08745Sheppo 		return (status);
5141ae08745Sheppo 	mac_fini_ops(&vsw_ops);
5151ae08745Sheppo 	ddi_soft_state_fini(&vsw_state);
5161ae08745Sheppo 
5171ae08745Sheppo 	rw_destroy(&vsw_rw);
5181ae08745Sheppo 
5191ae08745Sheppo 	return (status);
5201ae08745Sheppo }
5211ae08745Sheppo 
5221ae08745Sheppo int
5231ae08745Sheppo _info(struct modinfo *modinfop)
5241ae08745Sheppo {
5251ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
5261ae08745Sheppo }
5271ae08745Sheppo 
5281ae08745Sheppo static int
5291ae08745Sheppo vsw_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5301ae08745Sheppo {
5311ae08745Sheppo 	vsw_t		*vswp;
53234683adeSsg70180 	int		instance;
5331ae08745Sheppo 	char		hashname[MAXNAMELEN];
5341ae08745Sheppo 	char		qname[TASKQ_NAMELEN];
5357636cb21Slm66018 	enum		{ PROG_init = 0x00,
5367636cb21Slm66018 				PROG_if_lock = 0x01,
5377636cb21Slm66018 				PROG_fdb = 0x02,
5387636cb21Slm66018 				PROG_mfdb = 0x04,
5397636cb21Slm66018 				PROG_report_dev = 0x08,
5407636cb21Slm66018 				PROG_plist = 0x10,
5411ae08745Sheppo 				PROG_taskq = 0x20}
5421ae08745Sheppo 			progress;
5431ae08745Sheppo 
5441ae08745Sheppo 	progress = PROG_init;
5451ae08745Sheppo 
5461ae08745Sheppo 	switch (cmd) {
5471ae08745Sheppo 	case DDI_ATTACH:
5481ae08745Sheppo 		break;
5491ae08745Sheppo 	case DDI_RESUME:
5501ae08745Sheppo 		/* nothing to do for this non-device */
5511ae08745Sheppo 		return (DDI_SUCCESS);
5521ae08745Sheppo 	case DDI_PM_RESUME:
5531ae08745Sheppo 	default:
5541ae08745Sheppo 		return (DDI_FAILURE);
5551ae08745Sheppo 	}
5561ae08745Sheppo 
5571ae08745Sheppo 	instance = ddi_get_instance(dip);
5581ae08745Sheppo 	if (ddi_soft_state_zalloc(vsw_state, instance) != DDI_SUCCESS) {
5591ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_soft_state_zalloc failed", instance);
5601ae08745Sheppo 		return (DDI_FAILURE);
5611ae08745Sheppo 	}
5621ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
5631ae08745Sheppo 
5641ae08745Sheppo 	if (vswp == NULL) {
5651ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_get_soft_state failed", instance);
5661ae08745Sheppo 		goto vsw_attach_fail;
5671ae08745Sheppo 	}
5681ae08745Sheppo 
5691ae08745Sheppo 	vswp->dip = dip;
5701ae08745Sheppo 	vswp->instance = instance;
5711ae08745Sheppo 	ddi_set_driver_private(dip, (caddr_t)vswp);
5721ae08745Sheppo 
5735f94e909Ssg70180 	mutex_init(&vswp->hw_lock, NULL, MUTEX_DRIVER, NULL);
57434683adeSsg70180 	mutex_init(&vswp->mac_lock, NULL, MUTEX_DRIVER, NULL);
5751ae08745Sheppo 	rw_init(&vswp->if_lockrw, NULL, RW_DRIVER, NULL);
5761ae08745Sheppo 	progress |= PROG_if_lock;
5771ae08745Sheppo 
5781ae08745Sheppo 	/* setup the unicast forwarding database  */
5791ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_unicst_table-%d",
5801ae08745Sheppo 	    vswp->instance);
5811ae08745Sheppo 	D2(vswp, "creating unicast hash table (%s)...", hashname);
5821ae08745Sheppo 	vswp->fdb = mod_hash_create_ptrhash(hashname, VSW_NCHAINS,
5831ae08745Sheppo 	    mod_hash_null_valdtor, sizeof (void *));
5841ae08745Sheppo 
5851ae08745Sheppo 	progress |= PROG_fdb;
5861ae08745Sheppo 
5871ae08745Sheppo 	/* setup the multicast fowarding database */
5881ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_mcst_table-%d",
5891ae08745Sheppo 	    vswp->instance);
5901ae08745Sheppo 	D2(vswp, "creating multicast hash table %s)...", hashname);
5911ae08745Sheppo 	rw_init(&vswp->mfdbrw, NULL, RW_DRIVER, NULL);
5921ae08745Sheppo 	vswp->mfdb = mod_hash_create_ptrhash(hashname, VSW_NCHAINS,
5931ae08745Sheppo 	    mod_hash_null_valdtor, sizeof (void *));
5941ae08745Sheppo 
5951ae08745Sheppo 	progress |= PROG_mfdb;
5961ae08745Sheppo 
5971ae08745Sheppo 	/*
5981ae08745Sheppo 	 * create lock protecting list of multicast addresses
5991ae08745Sheppo 	 * which could come via m_multicst() entry point when plumbed.
6001ae08745Sheppo 	 */
6011ae08745Sheppo 	mutex_init(&vswp->mca_lock, NULL, MUTEX_DRIVER, NULL);
6021ae08745Sheppo 	vswp->mcap = NULL;
6031ae08745Sheppo 
6041ae08745Sheppo 	ddi_report_dev(vswp->dip);
6051ae08745Sheppo 
6061ae08745Sheppo 	progress |= PROG_report_dev;
6071ae08745Sheppo 
6081ae08745Sheppo 	WRITE_ENTER(&vsw_rw);
6091ae08745Sheppo 	vswp->next = vsw_head;
6101ae08745Sheppo 	vsw_head = vswp;
6111ae08745Sheppo 	RW_EXIT(&vsw_rw);
6121ae08745Sheppo 
6131ae08745Sheppo 	/* setup the port list */
6141ae08745Sheppo 	rw_init(&vswp->plist.lockrw, NULL, RW_DRIVER, NULL);
6151ae08745Sheppo 	vswp->plist.head = NULL;
6161ae08745Sheppo 
6171ae08745Sheppo 	progress |= PROG_plist;
6181ae08745Sheppo 
6191ae08745Sheppo 	/*
6201ae08745Sheppo 	 * Create the taskq which will process all the VIO
6211ae08745Sheppo 	 * control messages.
6221ae08745Sheppo 	 */
6231ae08745Sheppo 	(void) snprintf(qname, TASKQ_NAMELEN, "vsw_taskq%d", vswp->instance);
6241ae08745Sheppo 	if ((vswp->taskq_p = ddi_taskq_create(vswp->dip, qname, 1,
6251ae08745Sheppo 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
62634683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to create task queue",
62734683adeSsg70180 		    vswp->instance);
6281ae08745Sheppo 		goto vsw_attach_fail;
6291ae08745Sheppo 	}
6301ae08745Sheppo 
6311ae08745Sheppo 	progress |= PROG_taskq;
6321ae08745Sheppo 
633d10e4ef2Snarayan 	/* prevent auto-detaching */
634d10e4ef2Snarayan 	if (ddi_prop_update_int(DDI_DEV_T_NONE, vswp->dip,
635d10e4ef2Snarayan 	    DDI_NO_AUTODETACH, 1) != DDI_SUCCESS) {
63634683adeSsg70180 		cmn_err(CE_NOTE, "!Unable to set \"%s\" property for "
637d10e4ef2Snarayan 		    "instance %u", DDI_NO_AUTODETACH, instance);
638d10e4ef2Snarayan 	}
639d10e4ef2Snarayan 
6401ae08745Sheppo 	/*
64134683adeSsg70180 	 * Now we have everything setup, register an interest in
64234683adeSsg70180 	 * specific MD nodes.
64334683adeSsg70180 	 *
64434683adeSsg70180 	 * The callback is invoked in 2 cases, firstly if upon mdeg
64534683adeSsg70180 	 * registration there are existing nodes which match our specified
64634683adeSsg70180 	 * criteria, and secondly if the MD is changed (and again, there
64734683adeSsg70180 	 * are nodes which we are interested in present within it. Note
64834683adeSsg70180 	 * that our callback will be invoked even if our specified nodes
64934683adeSsg70180 	 * have not actually changed).
65034683adeSsg70180 	 *
65134683adeSsg70180 	 * Until the callback is invoked we cannot switch any pkts as
65234683adeSsg70180 	 * we don't know basic information such as what mode we are
65334683adeSsg70180 	 * operating in. However we expect the callback to be invoked
65434683adeSsg70180 	 * immediately upon registration as this driver should only
65534683adeSsg70180 	 * be attaching if there are vsw nodes in the MD.
6561ae08745Sheppo 	 */
65734683adeSsg70180 	if (vsw_mdeg_register(vswp))
65834683adeSsg70180 		goto vsw_attach_fail;
6591ae08745Sheppo 
6601ae08745Sheppo 	return (DDI_SUCCESS);
6611ae08745Sheppo 
6621ae08745Sheppo vsw_attach_fail:
6631ae08745Sheppo 	DERR(NULL, "vsw_attach: failed");
6641ae08745Sheppo 
6651ae08745Sheppo 	if (progress & PROG_taskq)
6661ae08745Sheppo 		ddi_taskq_destroy(vswp->taskq_p);
6671ae08745Sheppo 
6681ae08745Sheppo 	if (progress & PROG_plist)
6691ae08745Sheppo 		rw_destroy(&vswp->plist.lockrw);
6701ae08745Sheppo 
6711ae08745Sheppo 	if (progress & PROG_report_dev) {
6721ae08745Sheppo 		ddi_remove_minor_node(dip, NULL);
6731ae08745Sheppo 		mutex_destroy(&vswp->mca_lock);
6741ae08745Sheppo 	}
6751ae08745Sheppo 
6761ae08745Sheppo 	if (progress & PROG_mfdb) {
6771ae08745Sheppo 		mod_hash_destroy_hash(vswp->mfdb);
6781ae08745Sheppo 		vswp->mfdb = NULL;
6791ae08745Sheppo 		rw_destroy(&vswp->mfdbrw);
6801ae08745Sheppo 	}
6811ae08745Sheppo 
6821ae08745Sheppo 	if (progress & PROG_fdb) {
6831ae08745Sheppo 		mod_hash_destroy_hash(vswp->fdb);
6841ae08745Sheppo 		vswp->fdb = NULL;
6851ae08745Sheppo 	}
6861ae08745Sheppo 
68734683adeSsg70180 	if (progress & PROG_if_lock) {
6881ae08745Sheppo 		rw_destroy(&vswp->if_lockrw);
68934683adeSsg70180 		mutex_destroy(&vswp->mac_lock);
6905f94e909Ssg70180 		mutex_destroy(&vswp->hw_lock);
69134683adeSsg70180 	}
6921ae08745Sheppo 
6931ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
6941ae08745Sheppo 	return (DDI_FAILURE);
6951ae08745Sheppo }
6961ae08745Sheppo 
6971ae08745Sheppo static int
6981ae08745Sheppo vsw_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6991ae08745Sheppo {
700d10e4ef2Snarayan 	vio_mblk_pool_t		*poolp, *npoolp;
7011ae08745Sheppo 	vsw_t			**vswpp, *vswp;
7021ae08745Sheppo 	int 			instance;
7031ae08745Sheppo 
7041ae08745Sheppo 	instance = ddi_get_instance(dip);
7051ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
7061ae08745Sheppo 
7071ae08745Sheppo 	if (vswp == NULL) {
7081ae08745Sheppo 		return (DDI_FAILURE);
7091ae08745Sheppo 	}
7101ae08745Sheppo 
7111ae08745Sheppo 	switch (cmd) {
7121ae08745Sheppo 	case DDI_DETACH:
7131ae08745Sheppo 		break;
7141ae08745Sheppo 	case DDI_SUSPEND:
7151ae08745Sheppo 	case DDI_PM_SUSPEND:
7161ae08745Sheppo 	default:
7171ae08745Sheppo 		return (DDI_FAILURE);
7181ae08745Sheppo 	}
7191ae08745Sheppo 
7201ae08745Sheppo 	D2(vswp, "detaching instance %d", instance);
7211ae08745Sheppo 
72234683adeSsg70180 	if (vswp->if_state & VSW_IF_REG) {
7231ae08745Sheppo 		if (vsw_mac_unregister(vswp) != 0) {
72434683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to detach from "
72534683adeSsg70180 			    "MAC layer", vswp->instance);
7261ae08745Sheppo 			return (DDI_FAILURE);
7271ae08745Sheppo 		}
728d10e4ef2Snarayan 	}
7291ae08745Sheppo 
7301ae08745Sheppo 	vsw_mdeg_unregister(vswp);
7311ae08745Sheppo 
732e1ebb9ecSlm66018 	/* remove mac layer callback */
73334683adeSsg70180 	mutex_enter(&vswp->mac_lock);
734e1ebb9ecSlm66018 	if ((vswp->mh != NULL) && (vswp->mrh != NULL)) {
735e1ebb9ecSlm66018 		mac_rx_remove(vswp->mh, vswp->mrh);
736e1ebb9ecSlm66018 		vswp->mrh = NULL;
7371ae08745Sheppo 	}
73834683adeSsg70180 	mutex_exit(&vswp->mac_lock);
7391ae08745Sheppo 
7401ae08745Sheppo 	if (vsw_detach_ports(vswp) != 0) {
74134683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to detach ports",
74234683adeSsg70180 		    vswp->instance);
7431ae08745Sheppo 		return (DDI_FAILURE);
7441ae08745Sheppo 	}
7451ae08745Sheppo 
74634683adeSsg70180 	rw_destroy(&vswp->if_lockrw);
74734683adeSsg70180 
7485f94e909Ssg70180 	mutex_destroy(&vswp->hw_lock);
7495f94e909Ssg70180 
7501ae08745Sheppo 	/*
751e1ebb9ecSlm66018 	 * Now that the ports have been deleted, stop and close
752e1ebb9ecSlm66018 	 * the physical device.
753e1ebb9ecSlm66018 	 */
75434683adeSsg70180 	mutex_enter(&vswp->mac_lock);
755e1ebb9ecSlm66018 	if (vswp->mh != NULL) {
7567636cb21Slm66018 		if (vswp->mstarted)
757e1ebb9ecSlm66018 			mac_stop(vswp->mh);
7587636cb21Slm66018 		if (vswp->mresources)
7597636cb21Slm66018 			mac_resource_set(vswp->mh, NULL, NULL);
760e1ebb9ecSlm66018 		mac_close(vswp->mh);
761e1ebb9ecSlm66018 
762e1ebb9ecSlm66018 		vswp->mh = NULL;
763e1ebb9ecSlm66018 		vswp->txinfo = NULL;
764e1ebb9ecSlm66018 	}
76534683adeSsg70180 	mutex_exit(&vswp->mac_lock);
76634683adeSsg70180 	mutex_destroy(&vswp->mac_lock);
767e1ebb9ecSlm66018 
768e1ebb9ecSlm66018 	/*
769d10e4ef2Snarayan 	 * Destroy any free pools that may still exist.
770d10e4ef2Snarayan 	 */
771d10e4ef2Snarayan 	poolp = vswp->rxh;
772d10e4ef2Snarayan 	while (poolp != NULL) {
773d10e4ef2Snarayan 		npoolp = vswp->rxh = poolp->nextp;
774d10e4ef2Snarayan 		if (vio_destroy_mblks(poolp) != 0) {
775d10e4ef2Snarayan 			vswp->rxh = poolp;
776d10e4ef2Snarayan 			return (DDI_FAILURE);
777d10e4ef2Snarayan 		}
778d10e4ef2Snarayan 		poolp = npoolp;
779d10e4ef2Snarayan 	}
780d10e4ef2Snarayan 
781d10e4ef2Snarayan 	/*
7821ae08745Sheppo 	 * Remove this instance from any entries it may be on in
7831ae08745Sheppo 	 * the hash table by using the list of addresses maintained
7841ae08745Sheppo 	 * in the vsw_t structure.
7851ae08745Sheppo 	 */
7861ae08745Sheppo 	vsw_del_mcst_vsw(vswp);
7871ae08745Sheppo 
7881ae08745Sheppo 	vswp->mcap = NULL;
7891ae08745Sheppo 	mutex_destroy(&vswp->mca_lock);
7901ae08745Sheppo 
7911ae08745Sheppo 	/*
7921ae08745Sheppo 	 * By now any pending tasks have finished and the underlying
7931ae08745Sheppo 	 * ldc's have been destroyed, so its safe to delete the control
7941ae08745Sheppo 	 * message taskq.
7951ae08745Sheppo 	 */
7961ae08745Sheppo 	if (vswp->taskq_p != NULL)
7971ae08745Sheppo 		ddi_taskq_destroy(vswp->taskq_p);
7981ae08745Sheppo 
7991ae08745Sheppo 	/*
8001ae08745Sheppo 	 * At this stage all the data pointers in the hash table
8011ae08745Sheppo 	 * should be NULL, as all the ports have been removed and will
8021ae08745Sheppo 	 * have deleted themselves from the port lists which the data
8031ae08745Sheppo 	 * pointers point to. Hence we can destroy the table using the
8041ae08745Sheppo 	 * default destructors.
8051ae08745Sheppo 	 */
8061ae08745Sheppo 	D2(vswp, "vsw_detach: destroying hash tables..");
8071ae08745Sheppo 	mod_hash_destroy_hash(vswp->fdb);
8081ae08745Sheppo 	vswp->fdb = NULL;
8091ae08745Sheppo 
8101ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
8111ae08745Sheppo 	mod_hash_destroy_hash(vswp->mfdb);
8121ae08745Sheppo 	vswp->mfdb = NULL;
8131ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
8141ae08745Sheppo 	rw_destroy(&vswp->mfdbrw);
8151ae08745Sheppo 
8161ae08745Sheppo 	ddi_remove_minor_node(dip, NULL);
8171ae08745Sheppo 
8181ae08745Sheppo 	rw_destroy(&vswp->plist.lockrw);
8191ae08745Sheppo 	WRITE_ENTER(&vsw_rw);
8201ae08745Sheppo 	for (vswpp = &vsw_head; *vswpp; vswpp = &(*vswpp)->next) {
8211ae08745Sheppo 		if (*vswpp == vswp) {
8221ae08745Sheppo 			*vswpp = vswp->next;
8231ae08745Sheppo 			break;
8241ae08745Sheppo 		}
8251ae08745Sheppo 	}
8261ae08745Sheppo 	RW_EXIT(&vsw_rw);
8271ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
8281ae08745Sheppo 
8291ae08745Sheppo 	return (DDI_SUCCESS);
8301ae08745Sheppo }
8311ae08745Sheppo 
8321ae08745Sheppo static int
8331ae08745Sheppo vsw_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
8341ae08745Sheppo {
8351ae08745Sheppo 	_NOTE(ARGUNUSED(dip))
8361ae08745Sheppo 
8371ae08745Sheppo 	vsw_t	*vswp = NULL;
8381ae08745Sheppo 	dev_t	dev = (dev_t)arg;
8391ae08745Sheppo 	int	instance;
8401ae08745Sheppo 
8411ae08745Sheppo 	instance = getminor(dev);
8421ae08745Sheppo 
8431ae08745Sheppo 	switch (infocmd) {
8441ae08745Sheppo 	case DDI_INFO_DEVT2DEVINFO:
8451ae08745Sheppo 		if ((vswp = ddi_get_soft_state(vsw_state, instance)) == NULL) {
8461ae08745Sheppo 			*result = NULL;
8471ae08745Sheppo 			return (DDI_FAILURE);
8481ae08745Sheppo 		}
8491ae08745Sheppo 		*result = vswp->dip;
8501ae08745Sheppo 		return (DDI_SUCCESS);
8511ae08745Sheppo 
8521ae08745Sheppo 	case DDI_INFO_DEVT2INSTANCE:
8531ae08745Sheppo 		*result = (void *)(uintptr_t)instance;
8541ae08745Sheppo 		return (DDI_SUCCESS);
8551ae08745Sheppo 
8561ae08745Sheppo 	default:
8571ae08745Sheppo 		*result = NULL;
8581ae08745Sheppo 		return (DDI_FAILURE);
8591ae08745Sheppo 	}
8601ae08745Sheppo }
8611ae08745Sheppo 
8621ae08745Sheppo /*
86334683adeSsg70180  * Get the value of the "vsw-phys-dev" property in the specified
86434683adeSsg70180  * node. This property is the name of the physical device that
86534683adeSsg70180  * the virtual switch will use to talk to the outside world.
86634683adeSsg70180  *
86734683adeSsg70180  * Note it is valid for this property to be NULL (but the property
86834683adeSsg70180  * itself must exist). Callers of this routine should verify that
86934683adeSsg70180  * the value returned is what they expected (i.e. either NULL or non NULL).
87034683adeSsg70180  *
87134683adeSsg70180  * On success returns value of the property in region pointed to by
87234683adeSsg70180  * the 'name' argument, and with return value of 0. Otherwise returns 1.
8731ae08745Sheppo  */
87434683adeSsg70180 static int
87534683adeSsg70180 vsw_get_md_physname(vsw_t *vswp, md_t *mdp, mde_cookie_t node, char *name)
8761ae08745Sheppo {
87734683adeSsg70180 	int	len = 0;
8781ae08745Sheppo 	char	*physname = NULL;
8791ae08745Sheppo 	char	*dev;
8801ae08745Sheppo 
88134683adeSsg70180 	if (md_get_prop_data(mdp, node, physdev_propname,
8821ae08745Sheppo 	    (uint8_t **)(&physname), &len) != 0) {
88334683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get name(s) of physical "
88434683adeSsg70180 		    "device(s) from MD", vswp->instance);
88534683adeSsg70180 		return (1);
8861ae08745Sheppo 	} else if ((strlen(physname) + 1) > LIFNAMSIZ) {
88734683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: %s is too long a device name",
88834683adeSsg70180 		    vswp->instance, physname);
88934683adeSsg70180 		return (1);
8901ae08745Sheppo 	} else {
89134683adeSsg70180 		(void) strncpy(name, physname, strlen(physname) + 1);
8921ae08745Sheppo 		D2(vswp, "%s: using first device specified (%s)",
89334683adeSsg70180 		    __func__, physname);
8941ae08745Sheppo 	}
8951ae08745Sheppo 
8961ae08745Sheppo #ifdef DEBUG
8971ae08745Sheppo 	/*
8981ae08745Sheppo 	 * As a temporary measure to aid testing we check to see if there
8991ae08745Sheppo 	 * is a vsw.conf file present. If there is we use the value of the
9001ae08745Sheppo 	 * vsw_physname property in the file as the name of the physical
9011ae08745Sheppo 	 * device, overriding the value from the MD.
9021ae08745Sheppo 	 *
9031ae08745Sheppo 	 * There may be multiple devices listed, but for the moment
9041ae08745Sheppo 	 * we just use the first one.
9051ae08745Sheppo 	 */
9061ae08745Sheppo 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vswp->dip, 0,
9071ae08745Sheppo 	    "vsw_physname", &dev) == DDI_PROP_SUCCESS) {
9081ae08745Sheppo 		if ((strlen(dev) + 1) > LIFNAMSIZ) {
90934683adeSsg70180 			cmn_err(CE_WARN, "vsw%d: %s is too long a device name",
91034683adeSsg70180 			    vswp->instance, dev);
91134683adeSsg70180 			ddi_prop_free(dev);
91234683adeSsg70180 			return (1);
9131ae08745Sheppo 		} else {
91434683adeSsg70180 			cmn_err(CE_NOTE, "vsw%d: Using device name (%s) from "
91534683adeSsg70180 			    "config file", vswp->instance, dev);
9161ae08745Sheppo 
91734683adeSsg70180 			(void) strncpy(name, dev, strlen(dev) + 1);
9181ae08745Sheppo 		}
9191ae08745Sheppo 
9201ae08745Sheppo 		ddi_prop_free(dev);
9211ae08745Sheppo 	}
9221ae08745Sheppo #endif
9231ae08745Sheppo 
92434683adeSsg70180 	return (0);
92534683adeSsg70180 }
926e1ebb9ecSlm66018 
927e1ebb9ecSlm66018 /*
92834683adeSsg70180  * Read the 'vsw-switch-mode' property from the specified MD node.
92934683adeSsg70180  *
93034683adeSsg70180  * Returns 0 on success and the number of modes found in 'found',
93134683adeSsg70180  * otherwise returns 1.
932e1ebb9ecSlm66018  */
93334683adeSsg70180 static int
93434683adeSsg70180 vsw_get_md_smodes(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
93534683adeSsg70180 						uint8_t *modes, int *found)
93634683adeSsg70180 {
93734683adeSsg70180 	int		len = 0;
93834683adeSsg70180 	int		smode_num = 0;
93934683adeSsg70180 	char		*smode = NULL;
94034683adeSsg70180 	char		*curr_mode = NULL;
94134683adeSsg70180 
94234683adeSsg70180 	D1(vswp, "%s: enter", __func__);
9431ae08745Sheppo 
9441ae08745Sheppo 	/*
9451ae08745Sheppo 	 * Get the switch-mode property. The modes are listed in
9461ae08745Sheppo 	 * decreasing order of preference, i.e. prefered mode is
9471ae08745Sheppo 	 * first item in list.
9481ae08745Sheppo 	 */
9491ae08745Sheppo 	len = 0;
95034683adeSsg70180 	smode_num = 0;
95134683adeSsg70180 	if (md_get_prop_data(mdp, node, smode_propname,
9521ae08745Sheppo 	    (uint8_t **)(&smode), &len) != 0) {
9531ae08745Sheppo 		/*
954e1ebb9ecSlm66018 		 * Unable to get switch-mode property from MD, nothing
955e1ebb9ecSlm66018 		 * more we can do.
9561ae08745Sheppo 		 */
95734683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get switch mode property"
95834683adeSsg70180 		    " from the MD", vswp->instance);
95934683adeSsg70180 		*found = 0;
96034683adeSsg70180 		return (1);
961e1ebb9ecSlm66018 	}
962e1ebb9ecSlm66018 
9631ae08745Sheppo 	curr_mode = smode;
9641ae08745Sheppo 	/*
9651ae08745Sheppo 	 * Modes of operation:
9661ae08745Sheppo 	 * 'switched'	 - layer 2 switching, underlying HW in
967e1ebb9ecSlm66018 	 *			programmed mode.
9681ae08745Sheppo 	 * 'promiscuous' - layer 2 switching, underlying HW in
9691ae08745Sheppo 	 *			promiscuous mode.
9701ae08745Sheppo 	 * 'routed'	 - layer 3 (i.e. IP) routing, underlying HW
9711ae08745Sheppo 	 *			in non-promiscuous mode.
9721ae08745Sheppo 	 */
97334683adeSsg70180 	while ((curr_mode < (smode + len)) && (smode_num < NUM_SMODES)) {
9741ae08745Sheppo 		D2(vswp, "%s: curr_mode = [%s]", __func__, curr_mode);
975e1ebb9ecSlm66018 		if (strcmp(curr_mode, "switched") == 0) {
97634683adeSsg70180 			modes[smode_num++] = VSW_LAYER2;
977e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "promiscuous") == 0) {
97834683adeSsg70180 			modes[smode_num++] = VSW_LAYER2_PROMISC;
979e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "routed") == 0) {
98034683adeSsg70180 			modes[smode_num++] = VSW_LAYER3;
981e1ebb9ecSlm66018 		} else {
98234683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unknown switch mode %s, "
98334683adeSsg70180 			    "setting to default switched mode",
98434683adeSsg70180 			    vswp->instance, curr_mode);
98534683adeSsg70180 			modes[smode_num++] = VSW_LAYER2;
9861ae08745Sheppo 		}
9871ae08745Sheppo 		curr_mode += strlen(curr_mode) + 1;
9881ae08745Sheppo 	}
98934683adeSsg70180 	*found = smode_num;
9901ae08745Sheppo 
99134683adeSsg70180 	D2(vswp, "%s: %d modes found", __func__, smode_num);
9921ae08745Sheppo 
9931ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
99434683adeSsg70180 
99534683adeSsg70180 	return (0);
9961ae08745Sheppo }
9971ae08745Sheppo 
998e1ebb9ecSlm66018 /*
999e1ebb9ecSlm66018  * Get the mac address of the physical device.
1000e1ebb9ecSlm66018  *
1001e1ebb9ecSlm66018  * Returns 0 on success, 1 on failure.
1002e1ebb9ecSlm66018  */
1003e1ebb9ecSlm66018 static int
1004e1ebb9ecSlm66018 vsw_get_physaddr(vsw_t *vswp)
1005e1ebb9ecSlm66018 {
1006e1ebb9ecSlm66018 	mac_handle_t	mh;
1007e1ebb9ecSlm66018 	char		drv[LIFNAMSIZ];
1008e1ebb9ecSlm66018 	uint_t		ddi_instance;
1009e1ebb9ecSlm66018 
1010e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1011e1ebb9ecSlm66018 
1012e1ebb9ecSlm66018 	if (ddi_parse(vswp->physname, drv, &ddi_instance) != DDI_SUCCESS)
1013e1ebb9ecSlm66018 		return (1);
1014e1ebb9ecSlm66018 
1015e1ebb9ecSlm66018 	if (mac_open(vswp->physname, ddi_instance, &mh) != 0) {
101634683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: mac_open %s failed",
101734683adeSsg70180 		    vswp->instance, vswp->physname);
1018e1ebb9ecSlm66018 		return (1);
1019e1ebb9ecSlm66018 	}
1020e1ebb9ecSlm66018 
1021e1ebb9ecSlm66018 	READ_ENTER(&vswp->if_lockrw);
1022e1ebb9ecSlm66018 	mac_unicst_get(mh, vswp->if_addr.ether_addr_octet);
1023e1ebb9ecSlm66018 	RW_EXIT(&vswp->if_lockrw);
1024e1ebb9ecSlm66018 
1025e1ebb9ecSlm66018 	mac_close(mh);
1026e1ebb9ecSlm66018 
1027e1ebb9ecSlm66018 	vswp->mdprops |= VSW_DEV_MACADDR;
1028e1ebb9ecSlm66018 
1029e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1030e1ebb9ecSlm66018 
1031e1ebb9ecSlm66018 	return (0);
1032e1ebb9ecSlm66018 }
1033e1ebb9ecSlm66018 
1034e1ebb9ecSlm66018 /*
1035e1ebb9ecSlm66018  * Check to see if the card supports the setting of multiple unicst
1036e1ebb9ecSlm66018  * addresses.
1037e1ebb9ecSlm66018  *
10385f94e909Ssg70180  * Returns 0 if card supports the programming of multiple unicast addresses,
10395f94e909Ssg70180  * otherwise returns 1.
1040e1ebb9ecSlm66018  */
1041e1ebb9ecSlm66018 static int
1042e1ebb9ecSlm66018 vsw_get_hw_maddr(vsw_t *vswp)
1043e1ebb9ecSlm66018 {
1044e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1045e1ebb9ecSlm66018 
104634683adeSsg70180 	mutex_enter(&vswp->mac_lock);
1047e1ebb9ecSlm66018 	if (vswp->mh == NULL) {
104834683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1049e1ebb9ecSlm66018 		return (1);
1050e1ebb9ecSlm66018 	}
1051e1ebb9ecSlm66018 
1052e1ebb9ecSlm66018 	if (!mac_capab_get(vswp->mh, MAC_CAPAB_MULTIADDRESS, &vswp->maddr)) {
10535f94e909Ssg70180 		cmn_err(CE_WARN, "!vsw%d: device (%s) does not support "
10545f94e909Ssg70180 		    "setting multiple unicast addresses", vswp->instance,
10555f94e909Ssg70180 		    vswp->physname);
105634683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1057e1ebb9ecSlm66018 		return (1);
1058e1ebb9ecSlm66018 	}
105934683adeSsg70180 	mutex_exit(&vswp->mac_lock);
1060e1ebb9ecSlm66018 
1061e1ebb9ecSlm66018 	D2(vswp, "%s: %d addrs : %d free", __func__,
1062e1ebb9ecSlm66018 	    vswp->maddr.maddr_naddr, vswp->maddr.maddr_naddrfree);
1063e1ebb9ecSlm66018 
1064e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1065e1ebb9ecSlm66018 
1066e1ebb9ecSlm66018 	return (0);
1067e1ebb9ecSlm66018 }
1068e1ebb9ecSlm66018 
1069e1ebb9ecSlm66018 /*
107034683adeSsg70180  * Setup the required switching mode.
107134683adeSsg70180  *
107234683adeSsg70180  * Returns 0 on success, 1 on failure.
107334683adeSsg70180  */
107434683adeSsg70180 static int
107534683adeSsg70180 vsw_setup_switching(vsw_t *vswp)
107634683adeSsg70180 {
107734683adeSsg70180 	int	i, rv = 1;
107834683adeSsg70180 
107934683adeSsg70180 	D1(vswp, "%s: enter", __func__);
108034683adeSsg70180 
108134683adeSsg70180 	/* select best switching mode */
108234683adeSsg70180 	for (i = 0; i < vswp->smode_num; i++) {
108334683adeSsg70180 		vswp->smode_idx = i;
108434683adeSsg70180 		switch (vswp->smode[i]) {
108534683adeSsg70180 		case VSW_LAYER2:
108634683adeSsg70180 		case VSW_LAYER2_PROMISC:
108734683adeSsg70180 			rv = vsw_setup_layer2(vswp);
108834683adeSsg70180 			break;
108934683adeSsg70180 
109034683adeSsg70180 		case VSW_LAYER3:
109134683adeSsg70180 			rv = vsw_setup_layer3(vswp);
109234683adeSsg70180 			break;
109334683adeSsg70180 
109434683adeSsg70180 		default:
109534683adeSsg70180 			DERR(vswp, "unknown switch mode");
109634683adeSsg70180 			rv = 1;
109734683adeSsg70180 			break;
109834683adeSsg70180 		}
109934683adeSsg70180 
110034683adeSsg70180 		if (rv == 0)
110134683adeSsg70180 			break;
110234683adeSsg70180 	}
110334683adeSsg70180 
110434683adeSsg70180 	if (rv == 1) {
110534683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to setup specified "
110634683adeSsg70180 		    "switching mode", vswp->instance);
110734683adeSsg70180 		return (rv);
110834683adeSsg70180 	}
110934683adeSsg70180 
111034683adeSsg70180 	D2(vswp, "%s: Operating in mode %d", __func__,
111134683adeSsg70180 	    vswp->smode[vswp->smode_idx]);
111234683adeSsg70180 
111334683adeSsg70180 	D1(vswp, "%s: exit", __func__);
111434683adeSsg70180 
111534683adeSsg70180 	return (0);
111634683adeSsg70180 }
111734683adeSsg70180 
111834683adeSsg70180 /*
1119e1ebb9ecSlm66018  * Setup for layer 2 switching.
1120e1ebb9ecSlm66018  *
1121e1ebb9ecSlm66018  * Returns 0 on success, 1 on failure.
1122e1ebb9ecSlm66018  */
11231ae08745Sheppo static int
11241ae08745Sheppo vsw_setup_layer2(vsw_t *vswp)
11251ae08745Sheppo {
11261ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11271ae08745Sheppo 
112834683adeSsg70180 	vswp->vsw_switch_frame = vsw_switch_l2_frame;
11291ae08745Sheppo 
11301ae08745Sheppo 	/*
11311ae08745Sheppo 	 * Attempt to link into the MAC layer so we can get
11321ae08745Sheppo 	 * and send packets out over the physical adapter.
11331ae08745Sheppo 	 */
11341ae08745Sheppo 	if (vswp->mdprops & VSW_MD_PHYSNAME) {
11351ae08745Sheppo 		if (vsw_mac_attach(vswp) != 0) {
11361ae08745Sheppo 			/*
11371ae08745Sheppo 			 * Registration with the MAC layer has failed,
11381ae08745Sheppo 			 * so return 1 so that can fall back to next
11391ae08745Sheppo 			 * prefered switching method.
11401ae08745Sheppo 			 */
114134683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to join as MAC layer "
114234683adeSsg70180 			    "client", vswp->instance);
1143e1ebb9ecSlm66018 			return (1);
11441ae08745Sheppo 		}
1145e1ebb9ecSlm66018 
1146e1ebb9ecSlm66018 		if (vswp->smode[vswp->smode_idx] == VSW_LAYER2) {
1147e1ebb9ecSlm66018 			/*
1148e1ebb9ecSlm66018 			 * Verify that underlying device can support multiple
11495f94e909Ssg70180 			 * unicast mac addresses.
1150e1ebb9ecSlm66018 			 */
1151e1ebb9ecSlm66018 			if (vsw_get_hw_maddr(vswp) != 0) {
115234683adeSsg70180 				cmn_err(CE_WARN, "!vsw%d: Unable to setup "
11535f94e909Ssg70180 				    "layer2 switching", vswp->instance);
1154e1ebb9ecSlm66018 				vsw_mac_detach(vswp);
1155e1ebb9ecSlm66018 				return (1);
1156e1ebb9ecSlm66018 			}
1157e1ebb9ecSlm66018 		}
1158e1ebb9ecSlm66018 
11591ae08745Sheppo 	} else {
1160e1ebb9ecSlm66018 		/*
1161e1ebb9ecSlm66018 		 * No physical device name found in MD which is
1162e1ebb9ecSlm66018 		 * required for layer 2.
1163e1ebb9ecSlm66018 		 */
116434683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: no physical device name specified",
116534683adeSsg70180 		    vswp->instance);
1166e1ebb9ecSlm66018 		return (1);
11671ae08745Sheppo 	}
11681ae08745Sheppo 
11691ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
11701ae08745Sheppo 
1171e1ebb9ecSlm66018 	return (0);
11721ae08745Sheppo }
11731ae08745Sheppo 
11741ae08745Sheppo static int
11751ae08745Sheppo vsw_setup_layer3(vsw_t *vswp)
11761ae08745Sheppo {
11771ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11781ae08745Sheppo 
11791ae08745Sheppo 	D2(vswp, "%s: operating in layer 3 mode", __func__);
118034683adeSsg70180 	vswp->vsw_switch_frame = vsw_switch_l3_frame;
11811ae08745Sheppo 
11821ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
11831ae08745Sheppo 
11841ae08745Sheppo 	return (0);
11851ae08745Sheppo }
11861ae08745Sheppo 
11871ae08745Sheppo /*
11881ae08745Sheppo  * Link into the MAC layer to gain access to the services provided by
11891ae08745Sheppo  * the underlying physical device driver (which should also have
11901ae08745Sheppo  * registered with the MAC layer).
11911ae08745Sheppo  *
11921ae08745Sheppo  * Only when in layer 2 mode.
11931ae08745Sheppo  */
11941ae08745Sheppo static int
11951ae08745Sheppo vsw_mac_attach(vsw_t *vswp)
11961ae08745Sheppo {
1197ba2e4443Sseb 	char	drv[LIFNAMSIZ];
1198ba2e4443Sseb 	uint_t	ddi_instance;
1199ba2e4443Sseb 
12007636cb21Slm66018 	D1(vswp, "%s: enter", __func__);
12011ae08745Sheppo 
120234683adeSsg70180 	ASSERT(vswp->mh == NULL);
120334683adeSsg70180 	ASSERT(vswp->mrh == NULL);
120434683adeSsg70180 	ASSERT(vswp->mstarted == B_FALSE);
120534683adeSsg70180 	ASSERT(vswp->mresources == B_FALSE);
12061ae08745Sheppo 
12071ae08745Sheppo 	ASSERT(vswp->mdprops & VSW_MD_PHYSNAME);
12081ae08745Sheppo 
120934683adeSsg70180 	mutex_enter(&vswp->mac_lock);
1210ba2e4443Sseb 	if (ddi_parse(vswp->physname, drv, &ddi_instance) != DDI_SUCCESS) {
121134683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: invalid device name: %s",
121234683adeSsg70180 		    vswp->instance, vswp->physname);
1213ba2e4443Sseb 		goto mac_fail_exit;
1214ba2e4443Sseb 	}
121534683adeSsg70180 
1216ba2e4443Sseb 	if ((mac_open(vswp->physname, ddi_instance, &vswp->mh)) != 0) {
121734683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: mac_open %s failed",
121834683adeSsg70180 		    vswp->instance, vswp->physname);
12191ae08745Sheppo 		goto mac_fail_exit;
12201ae08745Sheppo 	}
12211ae08745Sheppo 
12227636cb21Slm66018 	ASSERT(vswp->mh != NULL);
12237636cb21Slm66018 
12241ae08745Sheppo 	D2(vswp, "vsw_mac_attach: using device %s", vswp->physname);
12251ae08745Sheppo 
12267636cb21Slm66018 	if (vsw_multi_ring_enable) {
122734683adeSsg70180 		/*
122834683adeSsg70180 		 * Initialize the ring table.
122934683adeSsg70180 		 */
12307636cb21Slm66018 		vsw_mac_ring_tbl_init(vswp);
12311ae08745Sheppo 
12327636cb21Slm66018 		/*
123334683adeSsg70180 		 * Register our rx callback function.
12347636cb21Slm66018 		 */
12357636cb21Slm66018 		vswp->mrh = mac_rx_add(vswp->mh,
12367636cb21Slm66018 		    vsw_rx_queue_cb, (void *)vswp);
123734683adeSsg70180 		ASSERT(vswp->mrh != NULL);
12387636cb21Slm66018 
12397636cb21Slm66018 		/*
12407636cb21Slm66018 		 * Register our mac resource callback.
12417636cb21Slm66018 		 */
12427636cb21Slm66018 		mac_resource_set(vswp->mh, vsw_mac_ring_add_cb, (void *)vswp);
12437636cb21Slm66018 		vswp->mresources = B_TRUE;
12447636cb21Slm66018 
12457636cb21Slm66018 		/*
12467636cb21Slm66018 		 * Get the ring resources available to us from
12477636cb21Slm66018 		 * the mac below us.
12487636cb21Slm66018 		 */
12497636cb21Slm66018 		mac_resources(vswp->mh);
12507636cb21Slm66018 	} else {
12517636cb21Slm66018 		/*
12527636cb21Slm66018 		 * Just register our rx callback function
12537636cb21Slm66018 		 */
12547636cb21Slm66018 		vswp->mrh = mac_rx_add(vswp->mh, vsw_rx_cb, (void *)vswp);
12557636cb21Slm66018 		ASSERT(vswp->mrh != NULL);
125634683adeSsg70180 	}
12577636cb21Slm66018 
12587636cb21Slm66018 	/* Get the MAC tx fn */
12591ae08745Sheppo 	vswp->txinfo = mac_tx_get(vswp->mh);
12601ae08745Sheppo 
12611ae08745Sheppo 	/* start the interface */
12621ae08745Sheppo 	if (mac_start(vswp->mh) != 0) {
126334683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Could not start mac interface",
126434683adeSsg70180 		    vswp->instance);
12651ae08745Sheppo 		goto mac_fail_exit;
12661ae08745Sheppo 	}
12671ae08745Sheppo 
126834683adeSsg70180 	mutex_exit(&vswp->mac_lock);
126934683adeSsg70180 
12707636cb21Slm66018 	vswp->mstarted = B_TRUE;
12717636cb21Slm66018 
12727636cb21Slm66018 	D1(vswp, "%s: exit", __func__);
12731ae08745Sheppo 	return (0);
12741ae08745Sheppo 
12751ae08745Sheppo mac_fail_exit:
127634683adeSsg70180 	mutex_exit(&vswp->mac_lock);
12777636cb21Slm66018 	vsw_mac_detach(vswp);
12781ae08745Sheppo 
12797636cb21Slm66018 	D1(vswp, "%s: exit", __func__);
12801ae08745Sheppo 	return (1);
12811ae08745Sheppo }
12821ae08745Sheppo 
12831ae08745Sheppo static void
12841ae08745Sheppo vsw_mac_detach(vsw_t *vswp)
12851ae08745Sheppo {
12861ae08745Sheppo 	D1(vswp, "vsw_mac_detach: enter");
12871ae08745Sheppo 
12887636cb21Slm66018 	ASSERT(vswp != NULL);
12897636cb21Slm66018 
12907636cb21Slm66018 	if (vsw_multi_ring_enable) {
12917636cb21Slm66018 		vsw_mac_ring_tbl_destroy(vswp);
12927636cb21Slm66018 	}
12937636cb21Slm66018 
129434683adeSsg70180 	mutex_enter(&vswp->mac_lock);
129534683adeSsg70180 
1296b9a6d57aSsg70180 	if (vswp->mh != NULL) {
12977636cb21Slm66018 		if (vswp->mstarted)
12987636cb21Slm66018 			mac_stop(vswp->mh);
12991ae08745Sheppo 		if (vswp->mrh != NULL)
13001ae08745Sheppo 			mac_rx_remove(vswp->mh, vswp->mrh);
13017636cb21Slm66018 		if (vswp->mresources)
13027636cb21Slm66018 			mac_resource_set(vswp->mh, NULL, NULL);
13031ae08745Sheppo 		mac_close(vswp->mh);
1304b9a6d57aSsg70180 	}
13051ae08745Sheppo 
13061ae08745Sheppo 	vswp->mrh = NULL;
13071ae08745Sheppo 	vswp->mh = NULL;
13081ae08745Sheppo 	vswp->txinfo = NULL;
13097636cb21Slm66018 	vswp->mstarted = B_FALSE;
13101ae08745Sheppo 
131134683adeSsg70180 	mutex_exit(&vswp->mac_lock);
131234683adeSsg70180 
13131ae08745Sheppo 	D1(vswp, "vsw_mac_detach: exit");
13141ae08745Sheppo }
13151ae08745Sheppo 
13161ae08745Sheppo /*
1317e1ebb9ecSlm66018  * Depending on the mode specified, the capabilites and capacity
1318e1ebb9ecSlm66018  * of the underlying device setup the physical device.
13191ae08745Sheppo  *
1320e1ebb9ecSlm66018  * If in layer 3 mode, then do nothing.
1321e1ebb9ecSlm66018  *
1322e1ebb9ecSlm66018  * If in layer 2 programmed mode attempt to program the unicast address
1323e1ebb9ecSlm66018  * associated with the port into the physical device. If this is not
1324e1ebb9ecSlm66018  * possible due to resource exhaustion or simply because the device does
1325e1ebb9ecSlm66018  * not support multiple unicast addresses then if required fallback onto
1326e1ebb9ecSlm66018  * putting the card into promisc mode.
1327e1ebb9ecSlm66018  *
1328e1ebb9ecSlm66018  * If in promisc mode then simply set the card into promisc mode.
1329e1ebb9ecSlm66018  *
1330e1ebb9ecSlm66018  * Returns 0 success, 1 on failure.
13311ae08745Sheppo  */
1332e1ebb9ecSlm66018 static int
13335f94e909Ssg70180 vsw_set_hw(vsw_t *vswp, vsw_port_t *port, int type)
13341ae08745Sheppo {
1335e1ebb9ecSlm66018 	mac_multi_addr_t	mac_addr;
1336e1ebb9ecSlm66018 	int			err;
13371ae08745Sheppo 
1338e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1339e1ebb9ecSlm66018 
13405f94e909Ssg70180 	ASSERT(MUTEX_HELD(&vswp->hw_lock));
13415f94e909Ssg70180 	ASSERT((type == VSW_LOCALDEV) || (type == VSW_VNETPORT));
13425f94e909Ssg70180 
1343e1ebb9ecSlm66018 	if (vswp->smode[vswp->smode_idx] == VSW_LAYER3)
1344e1ebb9ecSlm66018 		return (0);
1345e1ebb9ecSlm66018 
1346e1ebb9ecSlm66018 	if (vswp->smode[vswp->smode_idx] == VSW_LAYER2_PROMISC) {
13475f94e909Ssg70180 		return (vsw_set_hw_promisc(vswp, port, type));
1348e1ebb9ecSlm66018 	}
1349e1ebb9ecSlm66018 
1350e1ebb9ecSlm66018 	/*
1351e1ebb9ecSlm66018 	 * Attempt to program the unicast address into the HW.
1352e1ebb9ecSlm66018 	 */
1353e1ebb9ecSlm66018 	mac_addr.mma_addrlen = ETHERADDRL;
13545f94e909Ssg70180 	if (type == VSW_VNETPORT) {
13555f94e909Ssg70180 		ASSERT(port != NULL);
1356e1ebb9ecSlm66018 		ether_copy(&port->p_macaddr, &mac_addr.mma_addr);
13575f94e909Ssg70180 	} else {
13585f94e909Ssg70180 		READ_ENTER(&vswp->if_lockrw);
13595f94e909Ssg70180 		/*
13605f94e909Ssg70180 		 * Don't program if the interface is not UP. This
13615f94e909Ssg70180 		 * is possible if the address has just been changed
13625f94e909Ssg70180 		 * in the MD node, but the interface has not yet been
13635f94e909Ssg70180 		 * plumbed.
13645f94e909Ssg70180 		 */
13655f94e909Ssg70180 		if (!(vswp->if_state & VSW_IF_UP)) {
13665f94e909Ssg70180 			RW_EXIT(&vswp->if_lockrw);
13675f94e909Ssg70180 			return (0);
13685f94e909Ssg70180 		}
13695f94e909Ssg70180 		ether_copy(&vswp->if_addr, &mac_addr.mma_addr);
13705f94e909Ssg70180 		RW_EXIT(&vswp->if_lockrw);
13715f94e909Ssg70180 	}
1372e1ebb9ecSlm66018 
13735f94e909Ssg70180 	err = vsw_set_hw_addr(vswp, &mac_addr);
1374e1ebb9ecSlm66018 	if (err != 0) {
1375e1ebb9ecSlm66018 		/*
1376e1ebb9ecSlm66018 		 * Mark that attempt should be made to re-config sometime
1377e1ebb9ecSlm66018 		 * in future if a port is deleted.
1378e1ebb9ecSlm66018 		 */
1379e1ebb9ecSlm66018 		vswp->recfg_reqd = B_TRUE;
1380e1ebb9ecSlm66018 
1381e1ebb9ecSlm66018 		/*
1382e1ebb9ecSlm66018 		 * Only 1 mode specified, nothing more to do.
1383e1ebb9ecSlm66018 		 */
1384e1ebb9ecSlm66018 		if (vswp->smode_num == 1)
1385e1ebb9ecSlm66018 			return (err);
1386e1ebb9ecSlm66018 
1387e1ebb9ecSlm66018 		/*
1388e1ebb9ecSlm66018 		 * If promiscuous was next mode specified try to
1389e1ebb9ecSlm66018 		 * set the card into that mode.
1390e1ebb9ecSlm66018 		 */
1391e1ebb9ecSlm66018 		if ((vswp->smode_idx <= (vswp->smode_num - 2)) &&
1392205eeb1aSlm66018 		    (vswp->smode[vswp->smode_idx + 1] ==
1393205eeb1aSlm66018 		    VSW_LAYER2_PROMISC)) {
1394e1ebb9ecSlm66018 			vswp->smode_idx += 1;
13955f94e909Ssg70180 			return (vsw_set_hw_promisc(vswp, port, type));
1396e1ebb9ecSlm66018 		}
1397e1ebb9ecSlm66018 		return (err);
1398e1ebb9ecSlm66018 	}
1399e1ebb9ecSlm66018 
14005f94e909Ssg70180 	if (type == VSW_VNETPORT) {
1401e1ebb9ecSlm66018 		port->addr_slot = mac_addr.mma_slot;
1402e1ebb9ecSlm66018 		port->addr_set = VSW_ADDR_HW;
14035f94e909Ssg70180 	} else {
14045f94e909Ssg70180 		vswp->addr_slot = mac_addr.mma_slot;
14055f94e909Ssg70180 		vswp->addr_set = VSW_ADDR_HW;
14065f94e909Ssg70180 	}
1407e1ebb9ecSlm66018 
14085f94e909Ssg70180 	D2(vswp, "programmed addr %x:%x:%x:%x:%x:%x into slot %d "
14095f94e909Ssg70180 	    "of device %s",
14105f94e909Ssg70180 	    mac_addr.mma_addr[0], mac_addr.mma_addr[1],
14115f94e909Ssg70180 	    mac_addr.mma_addr[2], mac_addr.mma_addr[3],
14125f94e909Ssg70180 	    mac_addr.mma_addr[4], mac_addr.mma_addr[5],
14135f94e909Ssg70180 	    mac_addr.mma_slot, vswp->physname);
1414e1ebb9ecSlm66018 
1415e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1416e1ebb9ecSlm66018 
1417e1ebb9ecSlm66018 	return (0);
1418e1ebb9ecSlm66018 }
1419e1ebb9ecSlm66018 
1420e1ebb9ecSlm66018 /*
1421e1ebb9ecSlm66018  * If in layer 3 mode do nothing.
1422e1ebb9ecSlm66018  *
1423e1ebb9ecSlm66018  * If in layer 2 switched mode remove the address from the physical
1424e1ebb9ecSlm66018  * device.
1425e1ebb9ecSlm66018  *
1426e1ebb9ecSlm66018  * If in layer 2 promiscuous mode disable promisc mode.
1427e1ebb9ecSlm66018  *
1428e1ebb9ecSlm66018  * Returns 0 on success.
1429e1ebb9ecSlm66018  */
1430e1ebb9ecSlm66018 static int
14315f94e909Ssg70180 vsw_unset_hw(vsw_t *vswp, vsw_port_t *port, int type)
1432e1ebb9ecSlm66018 {
14335f94e909Ssg70180 	mac_addr_slot_t	slot;
14345f94e909Ssg70180 	int		rv;
1435e1ebb9ecSlm66018 
1436e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1437e1ebb9ecSlm66018 
14385f94e909Ssg70180 	ASSERT(MUTEX_HELD(&vswp->hw_lock));
14395f94e909Ssg70180 
1440e1ebb9ecSlm66018 	if (vswp->smode[vswp->smode_idx] == VSW_LAYER3)
1441e1ebb9ecSlm66018 		return (0);
1442e1ebb9ecSlm66018 
14435f94e909Ssg70180 	switch (type) {
14445f94e909Ssg70180 	case VSW_VNETPORT:
14455f94e909Ssg70180 		ASSERT(port != NULL);
14465f94e909Ssg70180 
1447e1ebb9ecSlm66018 		if (port->addr_set == VSW_ADDR_PROMISC) {
14485f94e909Ssg70180 			return (vsw_unset_hw_promisc(vswp, port, type));
14495f94e909Ssg70180 
14505f94e909Ssg70180 		} else if (port->addr_set == VSW_ADDR_HW) {
14515f94e909Ssg70180 			slot = port->addr_slot;
14525f94e909Ssg70180 			if ((rv = vsw_unset_hw_addr(vswp, slot)) == 0)
14535f94e909Ssg70180 				port->addr_set = VSW_ADDR_UNSET;
1454e1ebb9ecSlm66018 		}
1455e1ebb9ecSlm66018 
14565f94e909Ssg70180 		break;
14575f94e909Ssg70180 
14585f94e909Ssg70180 	case VSW_LOCALDEV:
14595f94e909Ssg70180 		if (vswp->addr_set == VSW_ADDR_PROMISC) {
14605f94e909Ssg70180 			return (vsw_unset_hw_promisc(vswp, NULL, type));
14615f94e909Ssg70180 
14625f94e909Ssg70180 		} else if (vswp->addr_set == VSW_ADDR_HW) {
14635f94e909Ssg70180 			slot = vswp->addr_slot;
14645f94e909Ssg70180 			if ((rv = vsw_unset_hw_addr(vswp, slot)) == 0)
14655f94e909Ssg70180 				vswp->addr_set = VSW_ADDR_UNSET;
14665f94e909Ssg70180 		}
14675f94e909Ssg70180 
14685f94e909Ssg70180 		break;
14695f94e909Ssg70180 
14705f94e909Ssg70180 	default:
14715f94e909Ssg70180 		/* should never happen */
14725f94e909Ssg70180 		DERR(vswp, "%s: unknown type %d", __func__, type);
14735f94e909Ssg70180 		ASSERT(0);
14745f94e909Ssg70180 		return (1);
14755f94e909Ssg70180 	}
14765f94e909Ssg70180 
14775f94e909Ssg70180 	D1(vswp, "%s: exit", __func__);
14785f94e909Ssg70180 	return (rv);
14795f94e909Ssg70180 }
14805f94e909Ssg70180 
14815f94e909Ssg70180 /*
14825f94e909Ssg70180  * Attempt to program a unicast address into HW.
14835f94e909Ssg70180  *
14845f94e909Ssg70180  * Returns 0 on sucess, 1 on failure.
14855f94e909Ssg70180  */
14865f94e909Ssg70180 static int
14875f94e909Ssg70180 vsw_set_hw_addr(vsw_t *vswp, mac_multi_addr_t *mac)
14885f94e909Ssg70180 {
14895f94e909Ssg70180 	void	*mah;
14905f94e909Ssg70180 	int	rv;
14915f94e909Ssg70180 
14925f94e909Ssg70180 	D1(vswp, "%s: enter", __func__);
14935f94e909Ssg70180 
14945f94e909Ssg70180 	ASSERT(MUTEX_HELD(&vswp->hw_lock));
14955f94e909Ssg70180 
1496e1ebb9ecSlm66018 	if (vswp->maddr.maddr_handle == NULL)
1497e1ebb9ecSlm66018 		return (1);
1498e1ebb9ecSlm66018 
1499e1ebb9ecSlm66018 	mah = vswp->maddr.maddr_handle;
1500e1ebb9ecSlm66018 
15015f94e909Ssg70180 	rv = vswp->maddr.maddr_add(mah, mac);
15025f94e909Ssg70180 
15035f94e909Ssg70180 	if (rv == 0)
15045f94e909Ssg70180 		return (0);
15055f94e909Ssg70180 
15065f94e909Ssg70180 	/*
15075f94e909Ssg70180 	 * Its okay for the add to fail because we have exhausted
15085f94e909Ssg70180 	 * all the resouces in the hardware device. Any other error
15095f94e909Ssg70180 	 * we want to flag.
15105f94e909Ssg70180 	 */
15115f94e909Ssg70180 	if (rv != ENOSPC) {
15125f94e909Ssg70180 		cmn_err(CE_WARN, "!vsw%d: error programming "
15135f94e909Ssg70180 		    "address %x:%x:%x:%x:%x:%x into HW "
15145f94e909Ssg70180 		    "err (%d)", vswp->instance,
15155f94e909Ssg70180 		    mac->mma_addr[0], mac->mma_addr[1],
15165f94e909Ssg70180 		    mac->mma_addr[2], mac->mma_addr[3],
15175f94e909Ssg70180 		    mac->mma_addr[4], mac->mma_addr[5], rv);
15185f94e909Ssg70180 	}
15195f94e909Ssg70180 	D1(vswp, "%s: exit", __func__);
15205f94e909Ssg70180 	return (1);
1521e1ebb9ecSlm66018 }
1522e1ebb9ecSlm66018 
15235f94e909Ssg70180 /*
15245f94e909Ssg70180  * Remove a unicast mac address which has previously been programmed
15255f94e909Ssg70180  * into HW.
15265f94e909Ssg70180  *
15275f94e909Ssg70180  * Returns 0 on sucess, 1 on failure.
15285f94e909Ssg70180  */
15295f94e909Ssg70180 static int
15305f94e909Ssg70180 vsw_unset_hw_addr(vsw_t *vswp, int slot)
15315f94e909Ssg70180 {
15325f94e909Ssg70180 	void	*mah;
15335f94e909Ssg70180 	int	rv;
1534e1ebb9ecSlm66018 
15355f94e909Ssg70180 	D1(vswp, "%s: enter", __func__);
15365f94e909Ssg70180 
15375f94e909Ssg70180 	ASSERT(MUTEX_HELD(&vswp->hw_lock));
15385f94e909Ssg70180 	ASSERT(slot >= 0);
15395f94e909Ssg70180 
15405f94e909Ssg70180 	if (vswp->maddr.maddr_handle == NULL)
15415f94e909Ssg70180 		return (1);
15425f94e909Ssg70180 
15435f94e909Ssg70180 	mah = vswp->maddr.maddr_handle;
15445f94e909Ssg70180 
15455f94e909Ssg70180 	rv = vswp->maddr.maddr_remove(mah, slot);
15465f94e909Ssg70180 	if (rv != 0) {
15475f94e909Ssg70180 		cmn_err(CE_WARN, "!vsw%d: unable to remove address "
15485f94e909Ssg70180 		    "from slot %d in device %s (err %d)",
15495f94e909Ssg70180 		    vswp->instance, slot, vswp->physname, rv);
15505f94e909Ssg70180 		return (1);
1551e1ebb9ecSlm66018 	}
1552e1ebb9ecSlm66018 
15535f94e909Ssg70180 	D2(vswp, "removed addr from slot %d in device %s",
15545f94e909Ssg70180 	    slot, vswp->physname);
15555f94e909Ssg70180 
1556e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1557e1ebb9ecSlm66018 	return (0);
1558e1ebb9ecSlm66018 }
1559e1ebb9ecSlm66018 
1560e1ebb9ecSlm66018 /*
1561e1ebb9ecSlm66018  * Set network card into promisc mode.
1562e1ebb9ecSlm66018  *
1563e1ebb9ecSlm66018  * Returns 0 on success, 1 on failure.
1564e1ebb9ecSlm66018  */
1565e1ebb9ecSlm66018 static int
15665f94e909Ssg70180 vsw_set_hw_promisc(vsw_t *vswp, vsw_port_t *port, int type)
1567e1ebb9ecSlm66018 {
1568e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1569e1ebb9ecSlm66018 
15705f94e909Ssg70180 	ASSERT(MUTEX_HELD(&vswp->hw_lock));
15715f94e909Ssg70180 	ASSERT((type == VSW_LOCALDEV) || (type == VSW_VNETPORT));
15725f94e909Ssg70180 
157334683adeSsg70180 	mutex_enter(&vswp->mac_lock);
157434683adeSsg70180 	if (vswp->mh == NULL) {
157534683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1576e1ebb9ecSlm66018 		return (1);
157734683adeSsg70180 	}
1578e1ebb9ecSlm66018 
1579e1ebb9ecSlm66018 	if (vswp->promisc_cnt++ == 0) {
1580e1ebb9ecSlm66018 		if (mac_promisc_set(vswp->mh, B_TRUE, MAC_DEVPROMISC) != 0) {
1581e1ebb9ecSlm66018 			vswp->promisc_cnt--;
158234683adeSsg70180 			mutex_exit(&vswp->mac_lock);
1583e1ebb9ecSlm66018 			return (1);
1584e1ebb9ecSlm66018 		}
158534683adeSsg70180 		cmn_err(CE_NOTE, "!vsw%d: switching device %s into "
158634683adeSsg70180 		    "promiscuous mode", vswp->instance, vswp->physname);
1587e1ebb9ecSlm66018 	}
158834683adeSsg70180 	mutex_exit(&vswp->mac_lock);
15895f94e909Ssg70180 
15905f94e909Ssg70180 	if (type == VSW_VNETPORT) {
15915f94e909Ssg70180 		ASSERT(port != NULL);
1592e1ebb9ecSlm66018 		port->addr_set = VSW_ADDR_PROMISC;
15935f94e909Ssg70180 	} else {
15945f94e909Ssg70180 		vswp->addr_set = VSW_ADDR_PROMISC;
15955f94e909Ssg70180 	}
1596e1ebb9ecSlm66018 
1597e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1598e1ebb9ecSlm66018 
1599e1ebb9ecSlm66018 	return (0);
1600e1ebb9ecSlm66018 }
1601e1ebb9ecSlm66018 
1602e1ebb9ecSlm66018 /*
1603e1ebb9ecSlm66018  * Turn off promiscuous mode on network card.
1604e1ebb9ecSlm66018  *
1605e1ebb9ecSlm66018  * Returns 0 on success, 1 on failure.
1606e1ebb9ecSlm66018  */
1607e1ebb9ecSlm66018 static int
16085f94e909Ssg70180 vsw_unset_hw_promisc(vsw_t *vswp, vsw_port_t *port, int type)
1609e1ebb9ecSlm66018 {
1610e1ebb9ecSlm66018 	vsw_port_list_t 	*plist = &vswp->plist;
1611e1ebb9ecSlm66018 
161234683adeSsg70180 	D2(vswp, "%s: enter", __func__);
1613e1ebb9ecSlm66018 
16145f94e909Ssg70180 	ASSERT(MUTEX_HELD(&vswp->hw_lock));
16155f94e909Ssg70180 	ASSERT((type == VSW_LOCALDEV) || (type == VSW_VNETPORT));
16165f94e909Ssg70180 
161734683adeSsg70180 	mutex_enter(&vswp->mac_lock);
161834683adeSsg70180 	if (vswp->mh == NULL) {
161934683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1620e1ebb9ecSlm66018 		return (1);
162134683adeSsg70180 	}
1622e1ebb9ecSlm66018 
1623e1ebb9ecSlm66018 	if (--vswp->promisc_cnt == 0) {
1624e1ebb9ecSlm66018 		if (mac_promisc_set(vswp->mh, B_FALSE, MAC_DEVPROMISC) != 0) {
1625e1ebb9ecSlm66018 			vswp->promisc_cnt++;
162634683adeSsg70180 			mutex_exit(&vswp->mac_lock);
1627e1ebb9ecSlm66018 			return (1);
1628e1ebb9ecSlm66018 		}
1629e1ebb9ecSlm66018 
1630e1ebb9ecSlm66018 		/*
1631e1ebb9ecSlm66018 		 * We are exiting promisc mode either because we were
1632e1ebb9ecSlm66018 		 * only in promisc mode because we had failed over from
1633e1ebb9ecSlm66018 		 * switched mode due to HW resource issues, or the user
1634e1ebb9ecSlm66018 		 * wanted the card in promisc mode for all the ports and
1635e1ebb9ecSlm66018 		 * the last port is now being deleted. Tweak the message
1636e1ebb9ecSlm66018 		 * accordingly.
1637e1ebb9ecSlm66018 		 */
1638e1ebb9ecSlm66018 		if (plist->num_ports != 0) {
163934683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: switching device %s back to "
1640205eeb1aSlm66018 			    "programmed mode", vswp->instance, vswp->physname);
16411ae08745Sheppo 		} else {
164234683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: switching device %s out of "
1643205eeb1aSlm66018 			    "promiscuous mode", vswp->instance, vswp->physname);
16441ae08745Sheppo 		}
16451ae08745Sheppo 	}
164634683adeSsg70180 	mutex_exit(&vswp->mac_lock);
16475f94e909Ssg70180 
16485f94e909Ssg70180 	if (type == VSW_VNETPORT) {
16495f94e909Ssg70180 		ASSERT(port != NULL);
16505f94e909Ssg70180 		ASSERT(port->addr_set == VSW_ADDR_PROMISC);
1651e1ebb9ecSlm66018 		port->addr_set = VSW_ADDR_UNSET;
16525f94e909Ssg70180 	} else {
16535f94e909Ssg70180 		ASSERT(vswp->addr_set == VSW_ADDR_PROMISC);
16545f94e909Ssg70180 		vswp->addr_set = VSW_ADDR_UNSET;
16555f94e909Ssg70180 	}
1656e1ebb9ecSlm66018 
1657e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1658e1ebb9ecSlm66018 	return (0);
1659e1ebb9ecSlm66018 }
1660e1ebb9ecSlm66018 
1661e1ebb9ecSlm66018 /*
1662e1ebb9ecSlm66018  * Determine whether or not we are operating in our prefered
1663e1ebb9ecSlm66018  * mode and if not whether the physical resources now allow us
1664e1ebb9ecSlm66018  * to operate in it.
1665e1ebb9ecSlm66018  *
16665f94e909Ssg70180  * If a port is being removed should only be invoked after port has been
1667e1ebb9ecSlm66018  * removed from the port list.
1668e1ebb9ecSlm66018  */
16695f94e909Ssg70180 static void
1670e1ebb9ecSlm66018 vsw_reconfig_hw(vsw_t *vswp)
1671e1ebb9ecSlm66018 {
1672e1ebb9ecSlm66018 	int			s_idx;
1673e1ebb9ecSlm66018 
1674e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1675e1ebb9ecSlm66018 
16765f94e909Ssg70180 	ASSERT(MUTEX_HELD(&vswp->hw_lock));
1677e1ebb9ecSlm66018 
16785f94e909Ssg70180 	if (vswp->maddr.maddr_handle == NULL) {
16795f94e909Ssg70180 		return;
16805f94e909Ssg70180 	}
1681e1ebb9ecSlm66018 
1682e1ebb9ecSlm66018 	/*
1683e1ebb9ecSlm66018 	 * If we are in layer 2 (i.e. switched) or would like to be
16845f94e909Ssg70180 	 * in layer 2 then check if any ports or the vswitch itself
16855f94e909Ssg70180 	 * need to be programmed into the HW.
1686e1ebb9ecSlm66018 	 *
1687e1ebb9ecSlm66018 	 * This can happen in two cases - switched was specified as
1688e1ebb9ecSlm66018 	 * the prefered mode of operation but we exhausted the HW
1689e1ebb9ecSlm66018 	 * resources and so failed over to the next specifed mode,
1690e1ebb9ecSlm66018 	 * or switched was the only mode specified so after HW
1691e1ebb9ecSlm66018 	 * resources were exhausted there was nothing more we
1692e1ebb9ecSlm66018 	 * could do.
1693e1ebb9ecSlm66018 	 */
1694e1ebb9ecSlm66018 	if (vswp->smode_idx > 0)
1695e1ebb9ecSlm66018 		s_idx = vswp->smode_idx - 1;
1696e1ebb9ecSlm66018 	else
1697e1ebb9ecSlm66018 		s_idx = vswp->smode_idx;
1698e1ebb9ecSlm66018 
16995f94e909Ssg70180 	if (vswp->smode[s_idx] != VSW_LAYER2) {
17005f94e909Ssg70180 		return;
17015f94e909Ssg70180 	}
1702e1ebb9ecSlm66018 
1703e1ebb9ecSlm66018 	D2(vswp, "%s: attempting reconfig..", __func__);
1704e1ebb9ecSlm66018 
1705e1ebb9ecSlm66018 	/*
17065f94e909Ssg70180 	 * First, attempt to set the vswitch mac address into HW,
17075f94e909Ssg70180 	 * if required.
1708e1ebb9ecSlm66018 	 */
17095f94e909Ssg70180 	if (vsw_prog_if(vswp)) {
17105f94e909Ssg70180 		return;
1711e1ebb9ecSlm66018 	}
1712e1ebb9ecSlm66018 
1713e1ebb9ecSlm66018 	/*
17145f94e909Ssg70180 	 * Next, attempt to set any ports which have not yet been
17155f94e909Ssg70180 	 * programmed into HW.
1716e1ebb9ecSlm66018 	 */
17175f94e909Ssg70180 	if (vsw_prog_ports(vswp)) {
17185f94e909Ssg70180 		return;
1719e1ebb9ecSlm66018 	}
1720e1ebb9ecSlm66018 
17215f94e909Ssg70180 	/*
17225f94e909Ssg70180 	 * By now we know that have programmed all desired ports etc
17235f94e909Ssg70180 	 * into HW, so safe to mark reconfiguration as complete.
17245f94e909Ssg70180 	 */
1725e1ebb9ecSlm66018 	vswp->recfg_reqd = B_FALSE;
1726e1ebb9ecSlm66018 
1727e1ebb9ecSlm66018 	vswp->smode_idx = s_idx;
1728e1ebb9ecSlm66018 
17295f94e909Ssg70180 	D1(vswp, "%s: exit", __func__);
17305f94e909Ssg70180 }
17315f94e909Ssg70180 
17325f94e909Ssg70180 /*
17335f94e909Ssg70180  * Check to see if vsw itself is plumbed, and if so whether or not
17345f94e909Ssg70180  * its mac address should be written into HW.
17355f94e909Ssg70180  *
17365f94e909Ssg70180  * Returns 0 if could set address, or didn't have to set it.
17375f94e909Ssg70180  * Returns 1 if failed to set address.
17385f94e909Ssg70180  */
17395f94e909Ssg70180 static int
17405f94e909Ssg70180 vsw_prog_if(vsw_t *vswp)
17415f94e909Ssg70180 {
17425f94e909Ssg70180 	mac_multi_addr_t	addr;
17435f94e909Ssg70180 
17445f94e909Ssg70180 	D1(vswp, "%s: enter", __func__);
17455f94e909Ssg70180 
17465f94e909Ssg70180 	ASSERT(MUTEX_HELD(&vswp->hw_lock));
17475f94e909Ssg70180 
17485f94e909Ssg70180 	READ_ENTER(&vswp->if_lockrw);
17495f94e909Ssg70180 	if ((vswp->if_state & VSW_IF_UP) &&
17505f94e909Ssg70180 	    (vswp->addr_set != VSW_ADDR_HW)) {
17515f94e909Ssg70180 
17525f94e909Ssg70180 		addr.mma_addrlen = ETHERADDRL;
17535f94e909Ssg70180 		ether_copy(&vswp->if_addr, &addr.mma_addr);
17545f94e909Ssg70180 
17555f94e909Ssg70180 		if (vsw_set_hw_addr(vswp, &addr) != 0) {
17565f94e909Ssg70180 			RW_EXIT(&vswp->if_lockrw);
17575f94e909Ssg70180 			return (1);
17585f94e909Ssg70180 		}
17595f94e909Ssg70180 
17605f94e909Ssg70180 		vswp->addr_slot = addr.mma_slot;
17615f94e909Ssg70180 
17625f94e909Ssg70180 		/*
17635f94e909Ssg70180 		 * If previously when plumbed had had to place
17645f94e909Ssg70180 		 * interface into promisc mode, now reverse that.
17655f94e909Ssg70180 		 *
17665f94e909Ssg70180 		 * Note that interface will only actually be set into
17675f94e909Ssg70180 		 * non-promisc mode when last port/interface has been
17685f94e909Ssg70180 		 * programmed into HW.
17695f94e909Ssg70180 		 */
17705f94e909Ssg70180 		if (vswp->addr_set == VSW_ADDR_PROMISC)
17715f94e909Ssg70180 			(void) vsw_unset_hw_promisc(vswp, NULL, VSW_LOCALDEV);
17725f94e909Ssg70180 
17735f94e909Ssg70180 		vswp->addr_set = VSW_ADDR_HW;
17745f94e909Ssg70180 	}
17755f94e909Ssg70180 	RW_EXIT(&vswp->if_lockrw);
17765f94e909Ssg70180 
17775f94e909Ssg70180 	D1(vswp, "%s: exit", __func__);
1778e1ebb9ecSlm66018 	return (0);
1779e1ebb9ecSlm66018 }
1780e1ebb9ecSlm66018 
17815f94e909Ssg70180 /*
17825f94e909Ssg70180  * Scan the port list for any ports which have not yet been set
17835f94e909Ssg70180  * into HW. For those found attempt to program their mac addresses
17845f94e909Ssg70180  * into the physical device.
17855f94e909Ssg70180  *
17865f94e909Ssg70180  * Returns 0 if able to program all required ports (can be 0) into HW.
17875f94e909Ssg70180  * Returns 1 if failed to set at least one mac address.
17885f94e909Ssg70180  */
17895f94e909Ssg70180 static int
17905f94e909Ssg70180 vsw_prog_ports(vsw_t *vswp)
17915f94e909Ssg70180 {
17925f94e909Ssg70180 	mac_multi_addr_t	addr;
17935f94e909Ssg70180 	vsw_port_list_t		*plist = &vswp->plist;
17945f94e909Ssg70180 	vsw_port_t		*tp;
17955f94e909Ssg70180 	int			rv = 0;
17965f94e909Ssg70180 
17975f94e909Ssg70180 	D1(vswp, "%s: enter", __func__);
17985f94e909Ssg70180 
17995f94e909Ssg70180 	ASSERT(MUTEX_HELD(&vswp->hw_lock));
18005f94e909Ssg70180 
18015f94e909Ssg70180 	READ_ENTER(&plist->lockrw);
18025f94e909Ssg70180 	for (tp = plist->head; tp != NULL; tp = tp->p_next) {
18035f94e909Ssg70180 		if (tp->addr_set != VSW_ADDR_HW) {
18045f94e909Ssg70180 			addr.mma_addrlen = ETHERADDRL;
18055f94e909Ssg70180 			ether_copy(&tp->p_macaddr, &addr.mma_addr);
18065f94e909Ssg70180 
18075f94e909Ssg70180 			if (vsw_set_hw_addr(vswp, &addr) != 0) {
18085f94e909Ssg70180 				rv = 1;
18095f94e909Ssg70180 				break;
18105f94e909Ssg70180 			}
18115f94e909Ssg70180 
18125f94e909Ssg70180 			tp->addr_slot = addr.mma_slot;
18135f94e909Ssg70180 
18145f94e909Ssg70180 			/*
18155f94e909Ssg70180 			 * If when this port had first attached we had
18165f94e909Ssg70180 			 * had to place the interface into promisc mode,
18175f94e909Ssg70180 			 * then now reverse that.
18185f94e909Ssg70180 			 *
18195f94e909Ssg70180 			 * Note that the interface will not actually
18205f94e909Ssg70180 			 * change to non-promisc mode until all ports
18215f94e909Ssg70180 			 * have been programmed.
18225f94e909Ssg70180 			 */
18235f94e909Ssg70180 			if (tp->addr_set == VSW_ADDR_PROMISC)
18245f94e909Ssg70180 				(void) vsw_unset_hw_promisc(vswp,
18255f94e909Ssg70180 				    tp, VSW_VNETPORT);
18265f94e909Ssg70180 
18275f94e909Ssg70180 			tp->addr_set = VSW_ADDR_HW;
18285f94e909Ssg70180 		}
18295f94e909Ssg70180 	}
18305f94e909Ssg70180 	RW_EXIT(&plist->lockrw);
18315f94e909Ssg70180 
18325f94e909Ssg70180 	D1(vswp, "%s: exit", __func__);
1833e1ebb9ecSlm66018 	return (rv);
18341ae08745Sheppo }
18351ae08745Sheppo 
18367636cb21Slm66018 static void
18377636cb21Slm66018 vsw_mac_ring_tbl_entry_init(vsw_t *vswp, vsw_mac_ring_t *ringp)
18387636cb21Slm66018 {
18397636cb21Slm66018 	ringp->ring_state = VSW_MAC_RING_FREE;
18407636cb21Slm66018 	ringp->ring_arg = NULL;
18417636cb21Slm66018 	ringp->ring_blank = NULL;
18427636cb21Slm66018 	ringp->ring_vqp = NULL;
18437636cb21Slm66018 	ringp->ring_vswp = vswp;
18447636cb21Slm66018 }
18457636cb21Slm66018 
18467636cb21Slm66018 static void
18477636cb21Slm66018 vsw_mac_ring_tbl_init(vsw_t *vswp)
18487636cb21Slm66018 {
18497636cb21Slm66018 	int		i;
18507636cb21Slm66018 
18517636cb21Slm66018 	mutex_init(&vswp->mac_ring_lock, NULL, MUTEX_DRIVER, NULL);
18527636cb21Slm66018 
18537636cb21Slm66018 	vswp->mac_ring_tbl_sz = vsw_mac_rx_rings;
18547636cb21Slm66018 	vswp->mac_ring_tbl  =
1855205eeb1aSlm66018 	    kmem_alloc(vsw_mac_rx_rings * sizeof (vsw_mac_ring_t), KM_SLEEP);
18567636cb21Slm66018 
18577636cb21Slm66018 	for (i = 0; i < vswp->mac_ring_tbl_sz; i++)
18587636cb21Slm66018 		vsw_mac_ring_tbl_entry_init(vswp, &vswp->mac_ring_tbl[i]);
18597636cb21Slm66018 }
18607636cb21Slm66018 
18617636cb21Slm66018 static void
18627636cb21Slm66018 vsw_mac_ring_tbl_destroy(vsw_t *vswp)
18637636cb21Slm66018 {
18647636cb21Slm66018 	int		i;
186534683adeSsg70180 	vsw_mac_ring_t	*ringp;
18667636cb21Slm66018 
18677636cb21Slm66018 	mutex_enter(&vswp->mac_ring_lock);
18687636cb21Slm66018 	for (i = 0; i < vswp->mac_ring_tbl_sz; i++) {
186934683adeSsg70180 		ringp = &vswp->mac_ring_tbl[i];
187034683adeSsg70180 
187134683adeSsg70180 		if (ringp->ring_state != VSW_MAC_RING_FREE) {
18727636cb21Slm66018 			/*
18737636cb21Slm66018 			 * Destroy the queue.
18747636cb21Slm66018 			 */
187534683adeSsg70180 			vsw_queue_stop(ringp->ring_vqp);
187634683adeSsg70180 			vsw_queue_destroy(ringp->ring_vqp);
18777636cb21Slm66018 
18787636cb21Slm66018 			/*
18797636cb21Slm66018 			 * Re-initialize the structure.
18807636cb21Slm66018 			 */
188134683adeSsg70180 			vsw_mac_ring_tbl_entry_init(vswp, ringp);
18827636cb21Slm66018 		}
18837636cb21Slm66018 	}
18847636cb21Slm66018 	mutex_exit(&vswp->mac_ring_lock);
18857636cb21Slm66018 
18867636cb21Slm66018 	mutex_destroy(&vswp->mac_ring_lock);
18877636cb21Slm66018 	kmem_free(vswp->mac_ring_tbl,
18887636cb21Slm66018 	    vswp->mac_ring_tbl_sz * sizeof (vsw_mac_ring_t));
18897636cb21Slm66018 	vswp->mac_ring_tbl_sz = 0;
18907636cb21Slm66018 }
18917636cb21Slm66018 
18927636cb21Slm66018 /*
18937636cb21Slm66018  * Handle resource add callbacks from the driver below.
18947636cb21Slm66018  */
18957636cb21Slm66018 static mac_resource_handle_t
18967636cb21Slm66018 vsw_mac_ring_add_cb(void *arg, mac_resource_t *mrp)
18977636cb21Slm66018 {
18987636cb21Slm66018 	vsw_t		*vswp = (vsw_t *)arg;
18997636cb21Slm66018 	mac_rx_fifo_t	*mrfp = (mac_rx_fifo_t *)mrp;
19007636cb21Slm66018 	vsw_mac_ring_t	*ringp;
19017636cb21Slm66018 	vsw_queue_t	*vqp;
19027636cb21Slm66018 	int		i;
19037636cb21Slm66018 
19047636cb21Slm66018 	ASSERT(vswp != NULL);
19057636cb21Slm66018 	ASSERT(mrp != NULL);
19067636cb21Slm66018 	ASSERT(vswp->mac_ring_tbl != NULL);
19077636cb21Slm66018 
19087636cb21Slm66018 	D1(vswp, "%s: enter", __func__);
19097636cb21Slm66018 
19107636cb21Slm66018 	/*
19117636cb21Slm66018 	 * Check to make sure we have the correct resource type.
19127636cb21Slm66018 	 */
19137636cb21Slm66018 	if (mrp->mr_type != MAC_RX_FIFO)
19147636cb21Slm66018 		return (NULL);
19157636cb21Slm66018 
19167636cb21Slm66018 	/*
19177636cb21Slm66018 	 * Find a open entry in the ring table.
19187636cb21Slm66018 	 */
19197636cb21Slm66018 	mutex_enter(&vswp->mac_ring_lock);
19207636cb21Slm66018 	for (i = 0; i < vswp->mac_ring_tbl_sz; i++) {
19217636cb21Slm66018 		ringp = &vswp->mac_ring_tbl[i];
19227636cb21Slm66018 
19237636cb21Slm66018 		/*
19247636cb21Slm66018 		 * Check for an empty slot, if found, then setup queue
19257636cb21Slm66018 		 * and thread.
19267636cb21Slm66018 		 */
19277636cb21Slm66018 		if (ringp->ring_state == VSW_MAC_RING_FREE) {
19287636cb21Slm66018 			/*
19297636cb21Slm66018 			 * Create the queue for this ring.
19307636cb21Slm66018 			 */
19317636cb21Slm66018 			vqp = vsw_queue_create();
19327636cb21Slm66018 
19337636cb21Slm66018 			/*
19347636cb21Slm66018 			 * Initialize the ring data structure.
19357636cb21Slm66018 			 */
19367636cb21Slm66018 			ringp->ring_vqp = vqp;
19377636cb21Slm66018 			ringp->ring_arg = mrfp->mrf_arg;
19387636cb21Slm66018 			ringp->ring_blank = mrfp->mrf_blank;
19397636cb21Slm66018 			ringp->ring_state = VSW_MAC_RING_INUSE;
19407636cb21Slm66018 
19417636cb21Slm66018 			/*
19427636cb21Slm66018 			 * Create the worker thread.
19437636cb21Slm66018 			 */
19447636cb21Slm66018 			vqp->vq_worker = thread_create(NULL, 0,
19457636cb21Slm66018 			    vsw_queue_worker, ringp, 0, &p0,
19467636cb21Slm66018 			    TS_RUN, minclsyspri);
19477636cb21Slm66018 			if (vqp->vq_worker == NULL) {
19487636cb21Slm66018 				vsw_queue_destroy(vqp);
19497636cb21Slm66018 				vsw_mac_ring_tbl_entry_init(vswp, ringp);
19507636cb21Slm66018 				ringp = NULL;
19517636cb21Slm66018 			}
19527636cb21Slm66018 
195334683adeSsg70180 			if (ringp != NULL) {
195434683adeSsg70180 				/*
195534683adeSsg70180 				 * Make sure thread get's running state for
195634683adeSsg70180 				 * this ring.
195734683adeSsg70180 				 */
195834683adeSsg70180 				mutex_enter(&vqp->vq_lock);
195934683adeSsg70180 				while ((vqp->vq_state != VSW_QUEUE_RUNNING) &&
196034683adeSsg70180 				    (vqp->vq_state != VSW_QUEUE_DRAINED)) {
196134683adeSsg70180 					cv_wait(&vqp->vq_cv, &vqp->vq_lock);
196234683adeSsg70180 				}
196334683adeSsg70180 
196434683adeSsg70180 				/*
196534683adeSsg70180 				 * If the thread is not running, cleanup.
196634683adeSsg70180 				 */
196734683adeSsg70180 				if (vqp->vq_state == VSW_QUEUE_DRAINED) {
196834683adeSsg70180 					vsw_queue_destroy(vqp);
196934683adeSsg70180 					vsw_mac_ring_tbl_entry_init(vswp,
197034683adeSsg70180 					    ringp);
197134683adeSsg70180 					ringp = NULL;
197234683adeSsg70180 				}
197334683adeSsg70180 				mutex_exit(&vqp->vq_lock);
197434683adeSsg70180 			}
197534683adeSsg70180 
19767636cb21Slm66018 			mutex_exit(&vswp->mac_ring_lock);
19777636cb21Slm66018 			D1(vswp, "%s: exit", __func__);
19787636cb21Slm66018 			return ((mac_resource_handle_t)ringp);
19797636cb21Slm66018 		}
19807636cb21Slm66018 	}
19817636cb21Slm66018 	mutex_exit(&vswp->mac_ring_lock);
19827636cb21Slm66018 
19837636cb21Slm66018 	/*
19847636cb21Slm66018 	 * No slots in the ring table available.
19857636cb21Slm66018 	 */
19867636cb21Slm66018 	D1(vswp, "%s: exit", __func__);
19877636cb21Slm66018 	return (NULL);
19887636cb21Slm66018 }
19897636cb21Slm66018 
19907636cb21Slm66018 static void
19917636cb21Slm66018 vsw_queue_stop(vsw_queue_t *vqp)
19927636cb21Slm66018 {
19937636cb21Slm66018 	mutex_enter(&vqp->vq_lock);
19947636cb21Slm66018 
19957636cb21Slm66018 	if (vqp->vq_state == VSW_QUEUE_RUNNING) {
19967636cb21Slm66018 		vqp->vq_state = VSW_QUEUE_STOP;
19977636cb21Slm66018 		cv_signal(&vqp->vq_cv);
19987636cb21Slm66018 
19997636cb21Slm66018 		while (vqp->vq_state != VSW_QUEUE_DRAINED)
20007636cb21Slm66018 			cv_wait(&vqp->vq_cv, &vqp->vq_lock);
20017636cb21Slm66018 	}
20027636cb21Slm66018 
200334683adeSsg70180 	vqp->vq_state = VSW_QUEUE_STOPPED;
200434683adeSsg70180 
20057636cb21Slm66018 	mutex_exit(&vqp->vq_lock);
20067636cb21Slm66018 }
20077636cb21Slm66018 
20087636cb21Slm66018 static vsw_queue_t *
20097636cb21Slm66018 vsw_queue_create()
20107636cb21Slm66018 {
20117636cb21Slm66018 	vsw_queue_t *vqp;
20127636cb21Slm66018 
20137636cb21Slm66018 	vqp = kmem_zalloc(sizeof (vsw_queue_t), KM_SLEEP);
20147636cb21Slm66018 
20157636cb21Slm66018 	mutex_init(&vqp->vq_lock, NULL, MUTEX_DRIVER, NULL);
20167636cb21Slm66018 	cv_init(&vqp->vq_cv, NULL, CV_DRIVER, NULL);
20177636cb21Slm66018 	vqp->vq_first = NULL;
20187636cb21Slm66018 	vqp->vq_last = NULL;
201934683adeSsg70180 	vqp->vq_state = VSW_QUEUE_STOPPED;
20207636cb21Slm66018 
20217636cb21Slm66018 	return (vqp);
20227636cb21Slm66018 }
20237636cb21Slm66018 
20247636cb21Slm66018 static void
20257636cb21Slm66018 vsw_queue_destroy(vsw_queue_t *vqp)
20267636cb21Slm66018 {
20277636cb21Slm66018 	cv_destroy(&vqp->vq_cv);
20287636cb21Slm66018 	mutex_destroy(&vqp->vq_lock);
20297636cb21Slm66018 	kmem_free(vqp, sizeof (vsw_queue_t));
20307636cb21Slm66018 }
20317636cb21Slm66018 
20327636cb21Slm66018 static void
20337636cb21Slm66018 vsw_queue_worker(vsw_mac_ring_t *rrp)
20347636cb21Slm66018 {
20357636cb21Slm66018 	mblk_t		*mp;
20367636cb21Slm66018 	vsw_queue_t	*vqp = rrp->ring_vqp;
20377636cb21Slm66018 	vsw_t		*vswp = rrp->ring_vswp;
20387636cb21Slm66018 
20397636cb21Slm66018 	mutex_enter(&vqp->vq_lock);
20407636cb21Slm66018 
204134683adeSsg70180 	ASSERT(vqp->vq_state == VSW_QUEUE_STOPPED);
20427636cb21Slm66018 
20437636cb21Slm66018 	/*
20447636cb21Slm66018 	 * Set the state to running, since the thread is now active.
20457636cb21Slm66018 	 */
20467636cb21Slm66018 	vqp->vq_state = VSW_QUEUE_RUNNING;
204734683adeSsg70180 	cv_signal(&vqp->vq_cv);
20487636cb21Slm66018 
20497636cb21Slm66018 	while (vqp->vq_state == VSW_QUEUE_RUNNING) {
20507636cb21Slm66018 		/*
20517636cb21Slm66018 		 * Wait for work to do or the state has changed
20527636cb21Slm66018 		 * to not running.
20537636cb21Slm66018 		 */
20547636cb21Slm66018 		while ((vqp->vq_state == VSW_QUEUE_RUNNING) &&
20557636cb21Slm66018 		    (vqp->vq_first == NULL)) {
20567636cb21Slm66018 			cv_wait(&vqp->vq_cv, &vqp->vq_lock);
20577636cb21Slm66018 		}
20587636cb21Slm66018 
20597636cb21Slm66018 		/*
20607636cb21Slm66018 		 * Process packets that we received from the interface.
20617636cb21Slm66018 		 */
20627636cb21Slm66018 		if (vqp->vq_first != NULL) {
20637636cb21Slm66018 			mp = vqp->vq_first;
20647636cb21Slm66018 
20657636cb21Slm66018 			vqp->vq_first = NULL;
20667636cb21Slm66018 			vqp->vq_last = NULL;
20677636cb21Slm66018 
20687636cb21Slm66018 			mutex_exit(&vqp->vq_lock);
20697636cb21Slm66018 
20707636cb21Slm66018 			/* switch the chain of packets received */
207134683adeSsg70180 			vswp->vsw_switch_frame(vswp, mp,
207234683adeSsg70180 			    VSW_PHYSDEV, NULL, NULL);
20737636cb21Slm66018 
20747636cb21Slm66018 			mutex_enter(&vqp->vq_lock);
20757636cb21Slm66018 		}
20767636cb21Slm66018 	}
20777636cb21Slm66018 
20787636cb21Slm66018 	/*
20797636cb21Slm66018 	 * We are drained and signal we are done.
20807636cb21Slm66018 	 */
20817636cb21Slm66018 	vqp->vq_state = VSW_QUEUE_DRAINED;
20827636cb21Slm66018 	cv_signal(&vqp->vq_cv);
20837636cb21Slm66018 
20847636cb21Slm66018 	/*
20857636cb21Slm66018 	 * Exit lock and drain the remaining packets.
20867636cb21Slm66018 	 */
20877636cb21Slm66018 	mutex_exit(&vqp->vq_lock);
20887636cb21Slm66018 
20897636cb21Slm66018 	/*
20907636cb21Slm66018 	 * Exit the thread
20917636cb21Slm66018 	 */
20927636cb21Slm66018 	thread_exit();
20937636cb21Slm66018 }
20947636cb21Slm66018 
20957636cb21Slm66018 /*
20967636cb21Slm66018  * static void
20977636cb21Slm66018  * vsw_rx_queue_cb() - Receive callback routine when
20987636cb21Slm66018  *	vsw_multi_ring_enable is non-zero.  Queue the packets
20997636cb21Slm66018  *	to a packet queue for a worker thread to process.
21007636cb21Slm66018  */
21017636cb21Slm66018 static void
21027636cb21Slm66018 vsw_rx_queue_cb(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
21037636cb21Slm66018 {
21047636cb21Slm66018 	vsw_mac_ring_t	*ringp = (vsw_mac_ring_t *)mrh;
21057636cb21Slm66018 	vsw_t		*vswp = (vsw_t *)arg;
21067636cb21Slm66018 	vsw_queue_t	*vqp;
21077636cb21Slm66018 	mblk_t		*bp, *last;
21087636cb21Slm66018 
21097636cb21Slm66018 	ASSERT(mrh != NULL);
21107636cb21Slm66018 	ASSERT(vswp != NULL);
21117636cb21Slm66018 	ASSERT(mp != NULL);
21127636cb21Slm66018 
21137636cb21Slm66018 	D1(vswp, "%s: enter", __func__);
21147636cb21Slm66018 
21157636cb21Slm66018 	/*
21167636cb21Slm66018 	 * Find the last element in the mblk chain.
21177636cb21Slm66018 	 */
21187636cb21Slm66018 	bp = mp;
21197636cb21Slm66018 	do {
21207636cb21Slm66018 		last = bp;
21217636cb21Slm66018 		bp = bp->b_next;
21227636cb21Slm66018 	} while (bp != NULL);
21237636cb21Slm66018 
21247636cb21Slm66018 	/* Get the queue for the packets */
21257636cb21Slm66018 	vqp = ringp->ring_vqp;
21267636cb21Slm66018 
21277636cb21Slm66018 	/*
21287636cb21Slm66018 	 * Grab the lock such we can queue the packets.
21297636cb21Slm66018 	 */
21307636cb21Slm66018 	mutex_enter(&vqp->vq_lock);
21317636cb21Slm66018 
21327636cb21Slm66018 	if (vqp->vq_state != VSW_QUEUE_RUNNING) {
21337636cb21Slm66018 		freemsg(mp);
213434683adeSsg70180 		mutex_exit(&vqp->vq_lock);
21357636cb21Slm66018 		goto vsw_rx_queue_cb_exit;
21367636cb21Slm66018 	}
21377636cb21Slm66018 
21387636cb21Slm66018 	/*
21397636cb21Slm66018 	 * Add the mblk chain to the queue.  If there
21407636cb21Slm66018 	 * is some mblks in the queue, then add the new
21417636cb21Slm66018 	 * chain to the end.
21427636cb21Slm66018 	 */
21437636cb21Slm66018 	if (vqp->vq_first == NULL)
21447636cb21Slm66018 		vqp->vq_first = mp;
21457636cb21Slm66018 	else
21467636cb21Slm66018 		vqp->vq_last->b_next = mp;
21477636cb21Slm66018 
21487636cb21Slm66018 	vqp->vq_last = last;
21497636cb21Slm66018 
21507636cb21Slm66018 	/*
21517636cb21Slm66018 	 * Signal the worker thread that there is work to
21527636cb21Slm66018 	 * do.
21537636cb21Slm66018 	 */
21547636cb21Slm66018 	cv_signal(&vqp->vq_cv);
21557636cb21Slm66018 
21567636cb21Slm66018 	/*
21577636cb21Slm66018 	 * Let go of the lock and exit.
21587636cb21Slm66018 	 */
21597636cb21Slm66018 	mutex_exit(&vqp->vq_lock);
216034683adeSsg70180 
216134683adeSsg70180 vsw_rx_queue_cb_exit:
21627636cb21Slm66018 	D1(vswp, "%s: exit", __func__);
21637636cb21Slm66018 }
21647636cb21Slm66018 
21651ae08745Sheppo /*
21661ae08745Sheppo  * receive callback routine. Invoked by MAC layer when there
21671ae08745Sheppo  * are pkts being passed up from physical device.
21681ae08745Sheppo  *
21691ae08745Sheppo  * PERF: It may be more efficient when the card is in promisc
21701ae08745Sheppo  * mode to check the dest address of the pkts here (against
21711ae08745Sheppo  * the FDB) rather than checking later. Needs to be investigated.
21721ae08745Sheppo  */
21731ae08745Sheppo static void
21741ae08745Sheppo vsw_rx_cb(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
21751ae08745Sheppo {
21761ae08745Sheppo 	_NOTE(ARGUNUSED(mrh))
21771ae08745Sheppo 
21781ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
21791ae08745Sheppo 
21801ae08745Sheppo 	ASSERT(vswp != NULL);
21811ae08745Sheppo 
21821ae08745Sheppo 	D1(vswp, "vsw_rx_cb: enter");
21831ae08745Sheppo 
21841ae08745Sheppo 	/* switch the chain of packets received */
218534683adeSsg70180 	vswp->vsw_switch_frame(vswp, mp, VSW_PHYSDEV, NULL, NULL);
21861ae08745Sheppo 
21871ae08745Sheppo 	D1(vswp, "vsw_rx_cb: exit");
21881ae08745Sheppo }
21891ae08745Sheppo 
21901ae08745Sheppo /*
21911ae08745Sheppo  * Send a message out over the physical device via the MAC layer.
21921ae08745Sheppo  *
21931ae08745Sheppo  * Returns any mblks that it was unable to transmit.
21941ae08745Sheppo  */
21951ae08745Sheppo static mblk_t *
21961ae08745Sheppo vsw_tx_msg(vsw_t *vswp, mblk_t *mp)
21971ae08745Sheppo {
21981ae08745Sheppo 	const mac_txinfo_t	*mtp;
21991ae08745Sheppo 	mblk_t			*nextp;
22001ae08745Sheppo 
220134683adeSsg70180 	mutex_enter(&vswp->mac_lock);
22021ae08745Sheppo 	if (vswp->mh == NULL) {
22031ae08745Sheppo 		DERR(vswp, "vsw_tx_msg: dropping pkts: no tx routine avail");
220434683adeSsg70180 		mutex_exit(&vswp->mac_lock);
22051ae08745Sheppo 		return (mp);
22061ae08745Sheppo 	} else {
22071ae08745Sheppo 		for (;;) {
22081ae08745Sheppo 			nextp = mp->b_next;
22091ae08745Sheppo 			mp->b_next = NULL;
22101ae08745Sheppo 
22111ae08745Sheppo 			mtp = vswp->txinfo;
221234683adeSsg70180 
22131ae08745Sheppo 			if ((mp = mtp->mt_fn(mtp->mt_arg, mp)) != NULL) {
22141ae08745Sheppo 				mp->b_next = nextp;
22151ae08745Sheppo 				break;
22161ae08745Sheppo 			}
22171ae08745Sheppo 
22181ae08745Sheppo 			if ((mp = nextp) == NULL)
22191ae08745Sheppo 				break;
22201ae08745Sheppo 		}
22211ae08745Sheppo 	}
222234683adeSsg70180 	mutex_exit(&vswp->mac_lock);
22231ae08745Sheppo 
22241ae08745Sheppo 	return (mp);
22251ae08745Sheppo }
22261ae08745Sheppo 
22271ae08745Sheppo /*
22281ae08745Sheppo  * Register with the MAC layer as a network device, so we
22291ae08745Sheppo  * can be plumbed if necessary.
22301ae08745Sheppo  */
22311ae08745Sheppo static int
22321ae08745Sheppo vsw_mac_register(vsw_t *vswp)
22331ae08745Sheppo {
2234ba2e4443Sseb 	mac_register_t	*macp;
2235ba2e4443Sseb 	int		rv;
22361ae08745Sheppo 
22371ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
22381ae08745Sheppo 
2239ba2e4443Sseb 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
2240ba2e4443Sseb 		return (EINVAL);
2241ba2e4443Sseb 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
22421ae08745Sheppo 	macp->m_driver = vswp;
2243ba2e4443Sseb 	macp->m_dip = vswp->dip;
2244ba2e4443Sseb 	macp->m_src_addr = (uint8_t *)&vswp->if_addr;
2245ba2e4443Sseb 	macp->m_callbacks = &vsw_m_callbacks;
2246ba2e4443Sseb 	macp->m_min_sdu = 0;
2247ba2e4443Sseb 	macp->m_max_sdu = ETHERMTU;
2248ba2e4443Sseb 	rv = mac_register(macp, &vswp->if_mh);
2249ba2e4443Sseb 	mac_free(macp);
2250ba2e4443Sseb 	if (rv == 0)
2251ba2e4443Sseb 		vswp->if_state |= VSW_IF_REG;
22521ae08745Sheppo 
22531ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
22541ae08745Sheppo 
22551ae08745Sheppo 	return (rv);
22561ae08745Sheppo }
22571ae08745Sheppo 
22581ae08745Sheppo static int
22591ae08745Sheppo vsw_mac_unregister(vsw_t *vswp)
22601ae08745Sheppo {
22611ae08745Sheppo 	int		rv = 0;
22621ae08745Sheppo 
22631ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
22641ae08745Sheppo 
22651ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
22661ae08745Sheppo 
2267ba2e4443Sseb 	if (vswp->if_state & VSW_IF_REG) {
2268ba2e4443Sseb 		rv = mac_unregister(vswp->if_mh);
22691ae08745Sheppo 		if (rv != 0) {
22701ae08745Sheppo 			DWARN(vswp, "%s: unable to unregister from MAC "
22711ae08745Sheppo 			    "framework", __func__);
22721ae08745Sheppo 
22731ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
22741ae08745Sheppo 			D1(vswp, "%s: fail exit", __func__);
22751ae08745Sheppo 			return (rv);
22761ae08745Sheppo 		}
22771ae08745Sheppo 
2278ba2e4443Sseb 		/* mark i/f as down and unregistered */
2279ba2e4443Sseb 		vswp->if_state &= ~(VSW_IF_UP | VSW_IF_REG);
22801ae08745Sheppo 	}
22811ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
22821ae08745Sheppo 
22831ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
22841ae08745Sheppo 
22851ae08745Sheppo 	return (rv);
22861ae08745Sheppo }
22871ae08745Sheppo 
2288ba2e4443Sseb static int
2289ba2e4443Sseb vsw_m_stat(void *arg, uint_t stat, uint64_t *val)
22901ae08745Sheppo {
22911ae08745Sheppo 	vsw_t			*vswp = (vsw_t *)arg;
22921ae08745Sheppo 
22931ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
22941ae08745Sheppo 
229534683adeSsg70180 	mutex_enter(&vswp->mac_lock);
229634683adeSsg70180 	if (vswp->mh == NULL) {
229734683adeSsg70180 		mutex_exit(&vswp->mac_lock);
2298ba2e4443Sseb 		return (EINVAL);
229934683adeSsg70180 	}
23001ae08745Sheppo 
23011ae08745Sheppo 	/* return stats from underlying device */
2302ba2e4443Sseb 	*val = mac_stat_get(vswp->mh, stat);
230334683adeSsg70180 
230434683adeSsg70180 	mutex_exit(&vswp->mac_lock);
230534683adeSsg70180 
2306ba2e4443Sseb 	return (0);
23071ae08745Sheppo }
23081ae08745Sheppo 
23091ae08745Sheppo static void
23101ae08745Sheppo vsw_m_stop(void *arg)
23111ae08745Sheppo {
23121ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
23131ae08745Sheppo 
23141ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
23151ae08745Sheppo 
23161ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
23171ae08745Sheppo 	vswp->if_state &= ~VSW_IF_UP;
23181ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
23191ae08745Sheppo 
23205f94e909Ssg70180 	mutex_enter(&vswp->hw_lock);
23215f94e909Ssg70180 
23225f94e909Ssg70180 	(void) vsw_unset_hw(vswp, NULL, VSW_LOCALDEV);
23235f94e909Ssg70180 
23245f94e909Ssg70180 	if (vswp->recfg_reqd)
23255f94e909Ssg70180 		vsw_reconfig_hw(vswp);
23265f94e909Ssg70180 
23275f94e909Ssg70180 	mutex_exit(&vswp->hw_lock);
23285f94e909Ssg70180 
23291ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
23301ae08745Sheppo }
23311ae08745Sheppo 
23321ae08745Sheppo static int
23331ae08745Sheppo vsw_m_start(void *arg)
23341ae08745Sheppo {
23351ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
23361ae08745Sheppo 
23371ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
23381ae08745Sheppo 
23391ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
23401ae08745Sheppo 	vswp->if_state |= VSW_IF_UP;
23411ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
23421ae08745Sheppo 
23435f94e909Ssg70180 	mutex_enter(&vswp->hw_lock);
23445f94e909Ssg70180 	(void) vsw_set_hw(vswp, NULL, VSW_LOCALDEV);
23455f94e909Ssg70180 	mutex_exit(&vswp->hw_lock);
23465f94e909Ssg70180 
23471ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
23481ae08745Sheppo 	return (0);
23491ae08745Sheppo }
23501ae08745Sheppo 
23511ae08745Sheppo /*
23521ae08745Sheppo  * Change the local interface address.
23535f94e909Ssg70180  *
23545f94e909Ssg70180  * Note: we don't support this entry point. The local
23555f94e909Ssg70180  * mac address of the switch can only be changed via its
23565f94e909Ssg70180  * MD node properties.
23571ae08745Sheppo  */
23581ae08745Sheppo static int
23591ae08745Sheppo vsw_m_unicst(void *arg, const uint8_t *macaddr)
23601ae08745Sheppo {
23615f94e909Ssg70180 	_NOTE(ARGUNUSED(arg, macaddr))
23621ae08745Sheppo 
23635f94e909Ssg70180 	return (DDI_FAILURE);
23641ae08745Sheppo }
23651ae08745Sheppo 
23661ae08745Sheppo static int
23671ae08745Sheppo vsw_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
23681ae08745Sheppo {
23691ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
23701ae08745Sheppo 	mcst_addr_t	*mcst_p = NULL;
23711ae08745Sheppo 	uint64_t	addr = 0x0;
2372e1ebb9ecSlm66018 	int		i, ret = 0;
23731ae08745Sheppo 
23741ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
23751ae08745Sheppo 
23761ae08745Sheppo 	/*
23771ae08745Sheppo 	 * Convert address into form that can be used
23781ae08745Sheppo 	 * as hash table key.
23791ae08745Sheppo 	 */
23801ae08745Sheppo 	for (i = 0; i < ETHERADDRL; i++) {
23811ae08745Sheppo 		addr = (addr << 8) | mca[i];
23821ae08745Sheppo 	}
23831ae08745Sheppo 
23841ae08745Sheppo 	D2(vswp, "%s: addr = 0x%llx", __func__, addr);
23851ae08745Sheppo 
23861ae08745Sheppo 	if (add) {
23871ae08745Sheppo 		D2(vswp, "%s: adding multicast", __func__);
23881ae08745Sheppo 		if (vsw_add_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
23891ae08745Sheppo 			/*
23901ae08745Sheppo 			 * Update the list of multicast addresses
23911ae08745Sheppo 			 * contained within the vsw_t structure to
23921ae08745Sheppo 			 * include this new one.
23931ae08745Sheppo 			 */
23941ae08745Sheppo 			mcst_p = kmem_zalloc(sizeof (mcst_addr_t), KM_NOSLEEP);
23951ae08745Sheppo 			if (mcst_p == NULL) {
23961ae08745Sheppo 				DERR(vswp, "%s unable to alloc mem", __func__);
23971ae08745Sheppo 				return (1);
23981ae08745Sheppo 			}
23991ae08745Sheppo 			mcst_p->addr = addr;
24001ae08745Sheppo 
24011ae08745Sheppo 			mutex_enter(&vswp->mca_lock);
24021ae08745Sheppo 			mcst_p->nextp = vswp->mcap;
24031ae08745Sheppo 			vswp->mcap = mcst_p;
24041ae08745Sheppo 			mutex_exit(&vswp->mca_lock);
24051ae08745Sheppo 
24061ae08745Sheppo 			/*
24071ae08745Sheppo 			 * Call into the underlying driver to program the
24081ae08745Sheppo 			 * address into HW.
24091ae08745Sheppo 			 */
241034683adeSsg70180 			mutex_enter(&vswp->mac_lock);
2411e1ebb9ecSlm66018 			if (vswp->mh != NULL) {
2412e1ebb9ecSlm66018 				ret = mac_multicst_add(vswp->mh, mca);
2413e1ebb9ecSlm66018 				if (ret != 0) {
241434683adeSsg70180 					cmn_err(CE_WARN, "!vsw%d: unable to "
241534683adeSsg70180 					    "add multicast address",
241634683adeSsg70180 					    vswp->instance);
241734683adeSsg70180 					mutex_exit(&vswp->mac_lock);
2418e1ebb9ecSlm66018 					goto vsw_remove_addr;
2419e1ebb9ecSlm66018 				}
24201ae08745Sheppo 			}
242134683adeSsg70180 			mutex_exit(&vswp->mac_lock);
24221ae08745Sheppo 		} else {
242334683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: unable to add multicast "
242434683adeSsg70180 			    "address", vswp->instance);
2425e1ebb9ecSlm66018 		}
2426e1ebb9ecSlm66018 		return (ret);
2427e1ebb9ecSlm66018 	}
2428e1ebb9ecSlm66018 
2429e1ebb9ecSlm66018 vsw_remove_addr:
2430e1ebb9ecSlm66018 
24311ae08745Sheppo 	D2(vswp, "%s: removing multicast", __func__);
24321ae08745Sheppo 	/*
24331ae08745Sheppo 	 * Remove the address from the hash table..
24341ae08745Sheppo 	 */
24351ae08745Sheppo 	if (vsw_del_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
24361ae08745Sheppo 
24371ae08745Sheppo 		/*
24381ae08745Sheppo 		 * ..and then from the list maintained in the
24391ae08745Sheppo 		 * vsw_t structure.
24401ae08745Sheppo 		 */
24411ae08745Sheppo 		vsw_del_addr(VSW_LOCALDEV, vswp, addr);
24421ae08745Sheppo 
244334683adeSsg70180 		mutex_enter(&vswp->mac_lock);
24441ae08745Sheppo 		if (vswp->mh != NULL)
24451ae08745Sheppo 			(void) mac_multicst_remove(vswp->mh, mca);
244634683adeSsg70180 		mutex_exit(&vswp->mac_lock);
24471ae08745Sheppo 	}
24481ae08745Sheppo 
24491ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
24501ae08745Sheppo 
24511ae08745Sheppo 	return (0);
24521ae08745Sheppo }
24531ae08745Sheppo 
24541ae08745Sheppo static int
24551ae08745Sheppo vsw_m_promisc(void *arg, boolean_t on)
24561ae08745Sheppo {
24571ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
24581ae08745Sheppo 
24591ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
24601ae08745Sheppo 
24611ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
24621ae08745Sheppo 	if (on)
24631ae08745Sheppo 		vswp->if_state |= VSW_IF_PROMISC;
24641ae08745Sheppo 	else
24651ae08745Sheppo 		vswp->if_state &= ~VSW_IF_PROMISC;
24661ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
24671ae08745Sheppo 
24681ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
24691ae08745Sheppo 
24701ae08745Sheppo 	return (0);
24711ae08745Sheppo }
24721ae08745Sheppo 
24731ae08745Sheppo static mblk_t *
24741ae08745Sheppo vsw_m_tx(void *arg, mblk_t *mp)
24751ae08745Sheppo {
24761ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
24771ae08745Sheppo 
24781ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
24791ae08745Sheppo 
248034683adeSsg70180 	vswp->vsw_switch_frame(vswp, mp, VSW_LOCALDEV, NULL, NULL);
24811ae08745Sheppo 
24821ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
24831ae08745Sheppo 
24841ae08745Sheppo 	return (NULL);
24851ae08745Sheppo }
24861ae08745Sheppo 
24871ae08745Sheppo /*
24881ae08745Sheppo  * Register for machine description (MD) updates.
248934683adeSsg70180  *
249034683adeSsg70180  * Returns 0 on success, 1 on failure.
24911ae08745Sheppo  */
249234683adeSsg70180 static int
24931ae08745Sheppo vsw_mdeg_register(vsw_t *vswp)
24941ae08745Sheppo {
24951ae08745Sheppo 	mdeg_prop_spec_t	*pspecp;
24961ae08745Sheppo 	mdeg_node_spec_t	*inst_specp;
249734683adeSsg70180 	mdeg_handle_t		mdeg_hdl, mdeg_port_hdl;
24981ae08745Sheppo 	size_t			templatesz;
24991ae08745Sheppo 	int			inst, rv;
25001ae08745Sheppo 
25011ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
25021ae08745Sheppo 
250334683adeSsg70180 	/*
250434683adeSsg70180 	 * In each 'virtual-device' node in the MD there is a
250534683adeSsg70180 	 * 'cfg-handle' property which is the MD's concept of
250634683adeSsg70180 	 * an instance number (this may be completely different from
250734683adeSsg70180 	 * the device drivers instance #). OBP reads that value and
250834683adeSsg70180 	 * stores it in the 'reg' property of the appropriate node in
250934683adeSsg70180 	 * the device tree. So we use the 'reg' value when registering
251034683adeSsg70180 	 * with the mdeg framework, to ensure we get events for the
251134683adeSsg70180 	 * correct nodes.
251234683adeSsg70180 	 */
25131ae08745Sheppo 	inst = ddi_prop_get_int(DDI_DEV_T_ANY, vswp->dip,
25141ae08745Sheppo 	    DDI_PROP_DONTPASS, reg_propname, -1);
25151ae08745Sheppo 	if (inst == -1) {
251634683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read %s property from "
251734683adeSsg70180 		    "OBP device tree", vswp->instance, reg_propname);
251834683adeSsg70180 		return (1);
25191ae08745Sheppo 	}
25201ae08745Sheppo 
25211ae08745Sheppo 	D2(vswp, "%s: instance %d registering with mdeg", __func__, inst);
25221ae08745Sheppo 
25231ae08745Sheppo 	/*
25241ae08745Sheppo 	 * Allocate and initialize a per-instance copy
25251ae08745Sheppo 	 * of the global property spec array that will
25261ae08745Sheppo 	 * uniquely identify this vsw instance.
25271ae08745Sheppo 	 */
25281ae08745Sheppo 	templatesz = sizeof (vsw_prop_template);
25291ae08745Sheppo 	pspecp = kmem_zalloc(templatesz, KM_SLEEP);
25301ae08745Sheppo 
25311ae08745Sheppo 	bcopy(vsw_prop_template, pspecp, templatesz);
25321ae08745Sheppo 
25331ae08745Sheppo 	VSW_SET_MDEG_PROP_INST(pspecp, inst);
25341ae08745Sheppo 
25351ae08745Sheppo 	/* initialize the complete prop spec structure */
25361ae08745Sheppo 	inst_specp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
25371ae08745Sheppo 	inst_specp->namep = "virtual-device";
25381ae08745Sheppo 	inst_specp->specp = pspecp;
25391ae08745Sheppo 
254034683adeSsg70180 	/*
254134683adeSsg70180 	 * Register an interest in 'virtual-device' nodes with a
254234683adeSsg70180 	 * 'name' property of 'virtual-network-switch'
254334683adeSsg70180 	 */
254434683adeSsg70180 	rv = mdeg_register(inst_specp, &vdev_match, vsw_mdeg_cb,
25451ae08745Sheppo 	    (void *)vswp, &mdeg_hdl);
254634683adeSsg70180 	if (rv != MDEG_SUCCESS) {
254734683adeSsg70180 		DERR(vswp, "%s: mdeg_register failed (%d) for vsw node",
254834683adeSsg70180 		    __func__, rv);
254934683adeSsg70180 		goto mdeg_reg_fail;
255034683adeSsg70180 	}
25511ae08745Sheppo 
255234683adeSsg70180 	/*
255334683adeSsg70180 	 * Register an interest in 'vsw-port' nodes.
255434683adeSsg70180 	 */
255534683adeSsg70180 	rv = mdeg_register(inst_specp, &vport_match, vsw_port_mdeg_cb,
255634683adeSsg70180 	    (void *)vswp, &mdeg_port_hdl);
25571ae08745Sheppo 	if (rv != MDEG_SUCCESS) {
25581ae08745Sheppo 		DERR(vswp, "%s: mdeg_register failed (%d)\n", __func__, rv);
255934683adeSsg70180 		(void) mdeg_unregister(mdeg_hdl);
256034683adeSsg70180 		goto mdeg_reg_fail;
25611ae08745Sheppo 	}
25621ae08745Sheppo 
25631ae08745Sheppo 	/* save off data that will be needed later */
25641ae08745Sheppo 	vswp->inst_spec = inst_specp;
25651ae08745Sheppo 	vswp->mdeg_hdl = mdeg_hdl;
256634683adeSsg70180 	vswp->mdeg_port_hdl = mdeg_port_hdl;
25671ae08745Sheppo 
25681ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
256934683adeSsg70180 	return (0);
257034683adeSsg70180 
257134683adeSsg70180 mdeg_reg_fail:
257234683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: Unable to register MDEG callbacks",
257334683adeSsg70180 	    vswp->instance);
257434683adeSsg70180 	kmem_free(pspecp, templatesz);
257534683adeSsg70180 	kmem_free(inst_specp, sizeof (mdeg_node_spec_t));
257634683adeSsg70180 
257734683adeSsg70180 	vswp->mdeg_hdl = NULL;
257834683adeSsg70180 	vswp->mdeg_port_hdl = NULL;
257934683adeSsg70180 
258034683adeSsg70180 	return (1);
25811ae08745Sheppo }
25821ae08745Sheppo 
25831ae08745Sheppo static void
25841ae08745Sheppo vsw_mdeg_unregister(vsw_t *vswp)
25851ae08745Sheppo {
25861ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: enter");
25871ae08745Sheppo 
258834683adeSsg70180 	if (vswp->mdeg_hdl != NULL)
25891ae08745Sheppo 		(void) mdeg_unregister(vswp->mdeg_hdl);
25901ae08745Sheppo 
259134683adeSsg70180 	if (vswp->mdeg_port_hdl != NULL)
259234683adeSsg70180 		(void) mdeg_unregister(vswp->mdeg_port_hdl);
259334683adeSsg70180 
259434683adeSsg70180 	if (vswp->inst_spec != NULL) {
25951ae08745Sheppo 		if (vswp->inst_spec->specp != NULL) {
25961ae08745Sheppo 			(void) kmem_free(vswp->inst_spec->specp,
25971ae08745Sheppo 			    sizeof (vsw_prop_template));
25981ae08745Sheppo 			vswp->inst_spec->specp = NULL;
25991ae08745Sheppo 		}
26001ae08745Sheppo 
2601205eeb1aSlm66018 		(void) kmem_free(vswp->inst_spec, sizeof (mdeg_node_spec_t));
26021ae08745Sheppo 		vswp->inst_spec = NULL;
26031ae08745Sheppo 	}
26041ae08745Sheppo 
26051ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: exit");
26061ae08745Sheppo }
26071ae08745Sheppo 
260834683adeSsg70180 /*
260934683adeSsg70180  * Mdeg callback invoked for the vsw node itself.
261034683adeSsg70180  */
26111ae08745Sheppo static int
26121ae08745Sheppo vsw_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
26131ae08745Sheppo {
26141ae08745Sheppo 	vsw_t		*vswp;
26151ae08745Sheppo 	int		idx;
26161ae08745Sheppo 	md_t		*mdp;
26171ae08745Sheppo 	mde_cookie_t	node;
26181ae08745Sheppo 	uint64_t	inst;
261934683adeSsg70180 	char		*node_name = NULL;
26201ae08745Sheppo 
26211ae08745Sheppo 	if (resp == NULL)
26221ae08745Sheppo 		return (MDEG_FAILURE);
26231ae08745Sheppo 
26241ae08745Sheppo 	vswp = (vsw_t *)cb_argp;
26251ae08745Sheppo 
262634683adeSsg70180 	D1(vswp, "%s: added %d : removed %d : curr matched %d"
262734683adeSsg70180 	    " : prev matched %d", __func__, resp->added.nelem,
262834683adeSsg70180 	    resp->removed.nelem, resp->match_curr.nelem,
262934683adeSsg70180 	    resp->match_prev.nelem);
263034683adeSsg70180 
263134683adeSsg70180 	/*
263234683adeSsg70180 	 * Expect 'added' to be non-zero if virtual-network-switch
263334683adeSsg70180 	 * nodes exist in the MD when the driver attaches.
263434683adeSsg70180 	 */
263534683adeSsg70180 	for (idx = 0; idx < resp->added.nelem; idx++) {
263634683adeSsg70180 		mdp = resp->added.mdp;
263734683adeSsg70180 		node = resp->added.mdep[idx];
263834683adeSsg70180 
263934683adeSsg70180 		if (md_get_prop_str(mdp, node, "name", &node_name) != 0) {
264034683adeSsg70180 			DERR(vswp, "%s: unable to get node name for "
264134683adeSsg70180 			    "node(%d) 0x%lx", __func__, idx, node);
264234683adeSsg70180 			continue;
264334683adeSsg70180 		}
264434683adeSsg70180 
264534683adeSsg70180 		if (md_get_prop_val(mdp, node, "cfg-handle", &inst)) {
264634683adeSsg70180 			DERR(vswp, "%s: prop(cfg-handle) not found port(%d)",
264734683adeSsg70180 			    __func__, idx);
264834683adeSsg70180 			continue;
264934683adeSsg70180 		}
265034683adeSsg70180 
265134683adeSsg70180 		D2(vswp, "%s: added node(%d) 0x%lx with name %s "
265234683adeSsg70180 		    "and inst %d", __func__, idx, node, node_name, inst);
265334683adeSsg70180 
265434683adeSsg70180 		vsw_get_initial_md_properties(vswp, mdp, node);
265534683adeSsg70180 	}
265634683adeSsg70180 
265734683adeSsg70180 	/*
265834683adeSsg70180 	 * A non-zero 'match' value indicates that the MD has been
265934683adeSsg70180 	 * updated and that a virtual-network-switch node is present
266034683adeSsg70180 	 * which may or may not have been updated. It is up to the clients
266134683adeSsg70180 	 * to examine their own nodes and determine if they have changed.
266234683adeSsg70180 	 */
266334683adeSsg70180 	for (idx = 0; idx < resp->match_curr.nelem; idx++) {
266434683adeSsg70180 		mdp = resp->match_curr.mdp;
266534683adeSsg70180 		node = resp->match_curr.mdep[idx];
266634683adeSsg70180 
266734683adeSsg70180 		if (md_get_prop_str(mdp, node, "name", &node_name) != 0) {
266834683adeSsg70180 			DERR(vswp, "%s: unable to get node name for "
266934683adeSsg70180 			    "node(%d) 0x%lx", __func__, idx, node);
267034683adeSsg70180 			continue;
267134683adeSsg70180 		}
267234683adeSsg70180 
267334683adeSsg70180 		if (md_get_prop_val(mdp, node, "cfg-handle", &inst)) {
267434683adeSsg70180 			DERR(vswp, "%s: prop(cfg-handle) not found port(%d)",
267534683adeSsg70180 			    __func__, idx);
267634683adeSsg70180 			continue;
267734683adeSsg70180 		}
267834683adeSsg70180 
267934683adeSsg70180 		D2(vswp, "%s: changed node(%d) 0x%lx with name %s "
268034683adeSsg70180 		    "and inst %d", __func__, idx, node, node_name, inst);
268134683adeSsg70180 
268234683adeSsg70180 		vsw_update_md_prop(vswp, mdp, node);
268334683adeSsg70180 	}
268434683adeSsg70180 
268534683adeSsg70180 	return (MDEG_SUCCESS);
268634683adeSsg70180 }
268734683adeSsg70180 
268834683adeSsg70180 /*
268934683adeSsg70180  * Mdeg callback invoked for changes to the vsw-port nodes
269034683adeSsg70180  * under the vsw node.
269134683adeSsg70180  */
269234683adeSsg70180 static int
269334683adeSsg70180 vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
269434683adeSsg70180 {
269534683adeSsg70180 	vsw_t		*vswp;
269634683adeSsg70180 	int		idx;
269734683adeSsg70180 	md_t		*mdp;
269834683adeSsg70180 	mde_cookie_t	node;
269934683adeSsg70180 	uint64_t	inst;
270034683adeSsg70180 
270134683adeSsg70180 	if ((resp == NULL) || (cb_argp == NULL))
270234683adeSsg70180 		return (MDEG_FAILURE);
270334683adeSsg70180 
270434683adeSsg70180 	vswp = (vsw_t *)cb_argp;
270534683adeSsg70180 
270634683adeSsg70180 	D2(vswp, "%s: added %d : removed %d : curr matched %d"
270734683adeSsg70180 	    " : prev matched %d", __func__, resp->added.nelem,
270834683adeSsg70180 	    resp->removed.nelem, resp->match_curr.nelem,
27091ae08745Sheppo 	    resp->match_prev.nelem);
27101ae08745Sheppo 
27111ae08745Sheppo 	/* process added ports */
27121ae08745Sheppo 	for (idx = 0; idx < resp->added.nelem; idx++) {
27131ae08745Sheppo 		mdp = resp->added.mdp;
27141ae08745Sheppo 		node = resp->added.mdep[idx];
27151ae08745Sheppo 
27161ae08745Sheppo 		D2(vswp, "%s: adding node(%d) 0x%lx", __func__, idx, node);
27171ae08745Sheppo 
27181ae08745Sheppo 		if (vsw_port_add(vswp, mdp, &node) != 0) {
271934683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to add new port "
272034683adeSsg70180 			    "(0x%lx)", vswp->instance, node);
27211ae08745Sheppo 		}
27221ae08745Sheppo 	}
27231ae08745Sheppo 
27241ae08745Sheppo 	/* process removed ports */
27251ae08745Sheppo 	for (idx = 0; idx < resp->removed.nelem; idx++) {
27261ae08745Sheppo 		mdp = resp->removed.mdp;
27271ae08745Sheppo 		node = resp->removed.mdep[idx];
27281ae08745Sheppo 
27291ae08745Sheppo 		if (md_get_prop_val(mdp, node, id_propname, &inst)) {
273034683adeSsg70180 			DERR(vswp, "%s: prop(%s) not found in port(%d)",
27311ae08745Sheppo 			    __func__, id_propname, idx);
27321ae08745Sheppo 			continue;
27331ae08745Sheppo 		}
27341ae08745Sheppo 
27351ae08745Sheppo 		D2(vswp, "%s: removing node(%d) 0x%lx", __func__, idx, node);
27361ae08745Sheppo 
27371ae08745Sheppo 		if (vsw_port_detach(vswp, inst) != 0) {
273834683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to remove port %ld",
273934683adeSsg70180 			    vswp->instance, inst);
27401ae08745Sheppo 		}
27411ae08745Sheppo 	}
27421ae08745Sheppo 
27431ae08745Sheppo 	/*
27441ae08745Sheppo 	 * Currently no support for updating already active ports.
27451ae08745Sheppo 	 * So, ignore the match_curr and match_priv arrays for now.
27461ae08745Sheppo 	 */
27471ae08745Sheppo 
27481ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
27491ae08745Sheppo 
27501ae08745Sheppo 	return (MDEG_SUCCESS);
27511ae08745Sheppo }
27521ae08745Sheppo 
27531ae08745Sheppo /*
275434683adeSsg70180  * Read the initial start-of-day values from the specified MD node.
275534683adeSsg70180  */
275634683adeSsg70180 static void
275734683adeSsg70180 vsw_get_initial_md_properties(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
275834683adeSsg70180 {
275934683adeSsg70180 	int		i;
276034683adeSsg70180 	uint64_t 	macaddr = 0;
276134683adeSsg70180 
276234683adeSsg70180 	D1(vswp, "%s: enter", __func__);
276334683adeSsg70180 
276434683adeSsg70180 	if (vsw_get_md_physname(vswp, mdp, node, vswp->physname) == 0) {
276534683adeSsg70180 		/*
276634683adeSsg70180 		 * Note it is valid for the physname property to
276734683adeSsg70180 		 * be NULL so check actual name length to determine
276834683adeSsg70180 		 * if we have a actual device name.
276934683adeSsg70180 		 */
277034683adeSsg70180 		if (strlen(vswp->physname) > 0)
277134683adeSsg70180 			vswp->mdprops |= VSW_MD_PHYSNAME;
277234683adeSsg70180 	} else {
277334683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read name of physical "
277434683adeSsg70180 		    "device from MD", vswp->instance);
277534683adeSsg70180 		return;
277634683adeSsg70180 	}
277734683adeSsg70180 
277834683adeSsg70180 	/* mac address for vswitch device itself */
277934683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
278034683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
278134683adeSsg70180 		    vswp->instance);
278234683adeSsg70180 
278334683adeSsg70180 		/*
278434683adeSsg70180 		 * Fallback to using the mac address of the physical
278534683adeSsg70180 		 * device.
278634683adeSsg70180 		 */
278734683adeSsg70180 		if (vsw_get_physaddr(vswp) == 0) {
278834683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: Using MAC address from "
278934683adeSsg70180 			    "physical device (%s)", vswp->instance,
279034683adeSsg70180 			    vswp->physname);
279134683adeSsg70180 		} else {
279234683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address"
2793205eeb1aSlm66018 			    "from device %s", vswp->instance, vswp->physname);
279434683adeSsg70180 		}
279534683adeSsg70180 	} else {
279634683adeSsg70180 		WRITE_ENTER(&vswp->if_lockrw);
279734683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
279834683adeSsg70180 			vswp->if_addr.ether_addr_octet[i] = macaddr & 0xFF;
279934683adeSsg70180 			macaddr >>= 8;
280034683adeSsg70180 		}
280134683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
280234683adeSsg70180 		vswp->mdprops |= VSW_MD_MACADDR;
280334683adeSsg70180 	}
280434683adeSsg70180 
2805205eeb1aSlm66018 	if (vsw_get_md_smodes(vswp, mdp, node, vswp->smode, &vswp->smode_num)) {
280634683adeSsg70180 		cmn_err(CE_WARN, "vsw%d: Unable to read %s property from "
280734683adeSsg70180 		    "MD, defaulting to programmed mode", vswp->instance,
280834683adeSsg70180 		    smode_propname);
280934683adeSsg70180 
281034683adeSsg70180 		for (i = 0; i < NUM_SMODES; i++)
281134683adeSsg70180 			vswp->smode[i] = VSW_LAYER2;
281234683adeSsg70180 
281334683adeSsg70180 		vswp->smode_num = NUM_SMODES;
281434683adeSsg70180 	} else {
281534683adeSsg70180 		ASSERT(vswp->smode_num != 0);
281634683adeSsg70180 		vswp->mdprops |= VSW_MD_SMODE;
281734683adeSsg70180 	}
281834683adeSsg70180 
281934683adeSsg70180 	/*
282034683adeSsg70180 	 * Unable to setup any switching mode, nothing more
282134683adeSsg70180 	 * we can do.
282234683adeSsg70180 	 */
282334683adeSsg70180 	if (vsw_setup_switching(vswp))
282434683adeSsg70180 		return;
282534683adeSsg70180 
282634683adeSsg70180 	WRITE_ENTER(&vswp->if_lockrw);
282734683adeSsg70180 	vswp->if_state &= ~VSW_IF_UP;
282834683adeSsg70180 	RW_EXIT(&vswp->if_lockrw);
282934683adeSsg70180 	if (vswp->mdprops & (VSW_MD_MACADDR | VSW_DEV_MACADDR)) {
283034683adeSsg70180 		if (vsw_mac_register(vswp) != 0) {
283134683adeSsg70180 			/*
283234683adeSsg70180 			 * Treat this as a non-fatal error as we may be
283334683adeSsg70180 			 * able to operate in some other mode.
283434683adeSsg70180 			 */
283534683adeSsg70180 			cmn_err(CE_WARN, "vsw%d: Unable to register as "
283634683adeSsg70180 			    "provider with MAC layer", vswp->instance);
283734683adeSsg70180 		}
283834683adeSsg70180 	}
283934683adeSsg70180 
284034683adeSsg70180 	D1(vswp, "%s: exit", __func__);
284134683adeSsg70180 }
284234683adeSsg70180 
284334683adeSsg70180 /*
284434683adeSsg70180  * Check to see if the relevant properties in the specified node have
284534683adeSsg70180  * changed, and if so take the appropriate action.
284634683adeSsg70180  *
284734683adeSsg70180  * If any of the properties are missing or invalid we don't take
284834683adeSsg70180  * any action, as this function should only be invoked when modifications
284934683adeSsg70180  * have been made to what we assume is a working configuration, which
285034683adeSsg70180  * we leave active.
285134683adeSsg70180  *
285234683adeSsg70180  * Note it is legal for this routine to be invoked even if none of the
285334683adeSsg70180  * properties in the port node within the MD have actually changed.
285434683adeSsg70180  */
285534683adeSsg70180 static void
285634683adeSsg70180 vsw_update_md_prop(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
285734683adeSsg70180 {
285834683adeSsg70180 	char		physname[LIFNAMSIZ];
285934683adeSsg70180 	char		drv[LIFNAMSIZ];
286034683adeSsg70180 	uint_t		ddi_instance;
286134683adeSsg70180 	uint8_t		new_smode[NUM_SMODES];
286234683adeSsg70180 	int		i, smode_num = 0;
286334683adeSsg70180 	uint64_t 	macaddr = 0;
286434683adeSsg70180 	vsw_port_list_t *plist = &vswp->plist;
286534683adeSsg70180 	vsw_port_t	*port = NULL;
286634683adeSsg70180 	enum		{MD_init = 0x1,
286734683adeSsg70180 				MD_physname = 0x2,
286834683adeSsg70180 				MD_macaddr = 0x4,
286934683adeSsg70180 				MD_smode = 0x8} updated;
287034683adeSsg70180 
287134683adeSsg70180 	updated = MD_init;
287234683adeSsg70180 
287334683adeSsg70180 	D1(vswp, "%s: enter", __func__);
287434683adeSsg70180 
287534683adeSsg70180 	/*
287634683adeSsg70180 	 * Check if name of physical device in MD has changed.
287734683adeSsg70180 	 */
287834683adeSsg70180 	if (vsw_get_md_physname(vswp, mdp, node, (char *)&physname) == 0) {
287934683adeSsg70180 		/*
288034683adeSsg70180 		 * Do basic sanity check on new device name/instance,
288134683adeSsg70180 		 * if its non NULL. It is valid for the device name to
288234683adeSsg70180 		 * have changed from a non NULL to a NULL value, i.e.
288334683adeSsg70180 		 * the vsw is being changed to 'routed' mode.
288434683adeSsg70180 		 */
288534683adeSsg70180 		if ((strlen(physname) != 0) &&
2886205eeb1aSlm66018 		    (ddi_parse(physname, drv, &ddi_instance) != DDI_SUCCESS)) {
288734683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: new device name %s is not"
288834683adeSsg70180 			    " a valid device name/instance",
288934683adeSsg70180 			    vswp->instance, physname);
289034683adeSsg70180 			goto fail_reconf;
289134683adeSsg70180 		}
289234683adeSsg70180 
289334683adeSsg70180 		if (strcmp(physname, vswp->physname)) {
289434683adeSsg70180 			D2(vswp, "%s: device name changed from %s to %s",
289534683adeSsg70180 			    __func__, vswp->physname, physname);
289634683adeSsg70180 
289734683adeSsg70180 			updated |= MD_physname;
289834683adeSsg70180 		} else {
289934683adeSsg70180 			D2(vswp, "%s: device name unchanged at %s",
290034683adeSsg70180 			    __func__, vswp->physname);
290134683adeSsg70180 		}
290234683adeSsg70180 	} else {
290334683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read name of physical "
290434683adeSsg70180 		    "device from updated MD.", vswp->instance);
290534683adeSsg70180 		goto fail_reconf;
290634683adeSsg70180 	}
290734683adeSsg70180 
290834683adeSsg70180 	/*
290934683adeSsg70180 	 * Check if MAC address has changed.
291034683adeSsg70180 	 */
291134683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
291234683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
291334683adeSsg70180 		    vswp->instance);
291434683adeSsg70180 		goto fail_reconf;
291534683adeSsg70180 	} else {
291634683adeSsg70180 		READ_ENTER(&vswp->if_lockrw);
291734683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
2918205eeb1aSlm66018 			if (vswp->if_addr.ether_addr_octet[i] !=
2919205eeb1aSlm66018 			    (macaddr & 0xFF)) {
292034683adeSsg70180 				D2(vswp, "%s: octet[%d] 0x%x != 0x%x",
292134683adeSsg70180 				    __func__, i,
292234683adeSsg70180 				    vswp->if_addr.ether_addr_octet[i],
292334683adeSsg70180 				    (macaddr & 0xFF));
292434683adeSsg70180 				updated |= MD_macaddr;
292534683adeSsg70180 				break;
292634683adeSsg70180 			}
292734683adeSsg70180 			macaddr >>= 8;
292834683adeSsg70180 		}
292934683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
293034683adeSsg70180 	}
293134683adeSsg70180 
293234683adeSsg70180 	/*
293334683adeSsg70180 	 * Check if switching modes have changed.
293434683adeSsg70180 	 */
2935205eeb1aSlm66018 	if (vsw_get_md_smodes(vswp, mdp, node, new_smode, &smode_num)) {
293634683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read %s property from MD",
293734683adeSsg70180 		    vswp->instance, smode_propname);
293834683adeSsg70180 		goto fail_reconf;
293934683adeSsg70180 	} else {
294034683adeSsg70180 		ASSERT(smode_num != 0);
294134683adeSsg70180 		if (smode_num != vswp->smode_num) {
294234683adeSsg70180 			D2(vswp, "%s: number of modes changed from %d to %d",
294334683adeSsg70180 			    __func__, vswp->smode_num, smode_num);
294434683adeSsg70180 		}
294534683adeSsg70180 
294634683adeSsg70180 		for (i = 0; i < smode_num; i++) {
294734683adeSsg70180 			if (new_smode[i] != vswp->smode[i]) {
294834683adeSsg70180 				D2(vswp, "%s: mode changed from %d to %d",
294934683adeSsg70180 				    __func__, vswp->smode[i], new_smode[i]);
295034683adeSsg70180 				updated |= MD_smode;
295134683adeSsg70180 				break;
295234683adeSsg70180 			}
295334683adeSsg70180 		}
295434683adeSsg70180 	}
295534683adeSsg70180 
295634683adeSsg70180 	/*
295734683adeSsg70180 	 * Now make any changes which are needed...
295834683adeSsg70180 	 */
295934683adeSsg70180 
296034683adeSsg70180 	if (updated & (MD_physname | MD_smode)) {
296134683adeSsg70180 		/*
296234683adeSsg70180 		 * Disconnect all ports from the current card
296334683adeSsg70180 		 */
296434683adeSsg70180 		WRITE_ENTER(&plist->lockrw);
296534683adeSsg70180 		for (port = plist->head; port != NULL; port = port->p_next) {
296634683adeSsg70180 			/* Remove address if was programmed into HW. */
29675f94e909Ssg70180 			mutex_enter(&vswp->hw_lock);
29685f94e909Ssg70180 			if (vsw_unset_hw(vswp, port, VSW_VNETPORT)) {
29695f94e909Ssg70180 				mutex_exit(&vswp->hw_lock);
297034683adeSsg70180 				RW_EXIT(&plist->lockrw);
297134683adeSsg70180 				goto fail_update;
297234683adeSsg70180 			}
29735f94e909Ssg70180 			mutex_exit(&vswp->hw_lock);
297434683adeSsg70180 		}
297534683adeSsg70180 		RW_EXIT(&plist->lockrw);
297634683adeSsg70180 
297734683adeSsg70180 		/*
297834683adeSsg70180 		 * Stop, detach the old device..
297934683adeSsg70180 		 */
298034683adeSsg70180 		vsw_mac_detach(vswp);
298134683adeSsg70180 
298234683adeSsg70180 		/*
298334683adeSsg70180 		 * Update phys name.
298434683adeSsg70180 		 */
298534683adeSsg70180 		if (updated & MD_physname) {
298634683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: changing from %s to %s",
298734683adeSsg70180 			    vswp->instance, vswp->physname, physname);
298834683adeSsg70180 			(void) strncpy(vswp->physname,
298934683adeSsg70180 			    physname, strlen(physname) + 1);
299034683adeSsg70180 
299134683adeSsg70180 			if (strlen(vswp->physname) > 0)
299234683adeSsg70180 				vswp->mdprops |= VSW_MD_PHYSNAME;
299334683adeSsg70180 		}
299434683adeSsg70180 
299534683adeSsg70180 		/*
299634683adeSsg70180 		 * Update array with the new switch mode values.
299734683adeSsg70180 		 */
299834683adeSsg70180 		if (updated & MD_smode) {
299934683adeSsg70180 			for (i = 0; i < smode_num; i++)
300034683adeSsg70180 				vswp->smode[i] = new_smode[i];
300134683adeSsg70180 
300234683adeSsg70180 			vswp->smode_num = smode_num;
300334683adeSsg70180 			vswp->smode_idx = 0;
300434683adeSsg70180 		}
300534683adeSsg70180 
300634683adeSsg70180 		/*
300734683adeSsg70180 		 * ..and attach, start the new device.
300834683adeSsg70180 		 */
300934683adeSsg70180 		if (vsw_setup_switching(vswp))
301034683adeSsg70180 			goto fail_update;
301134683adeSsg70180 
301234683adeSsg70180 		/*
301334683adeSsg70180 		 * Connect ports to new card.
301434683adeSsg70180 		 */
301534683adeSsg70180 		WRITE_ENTER(&plist->lockrw);
301634683adeSsg70180 		for (port = plist->head; port != NULL; port = port->p_next) {
30175f94e909Ssg70180 			mutex_enter(&vswp->hw_lock);
30185f94e909Ssg70180 			if (vsw_set_hw(vswp, port, VSW_VNETPORT)) {
30195f94e909Ssg70180 				mutex_exit(&vswp->hw_lock);
302034683adeSsg70180 				RW_EXIT(&plist->lockrw);
302134683adeSsg70180 				goto fail_update;
302234683adeSsg70180 			}
30235f94e909Ssg70180 			mutex_exit(&vswp->hw_lock);
302434683adeSsg70180 		}
302534683adeSsg70180 		RW_EXIT(&plist->lockrw);
302634683adeSsg70180 	}
302734683adeSsg70180 
302834683adeSsg70180 	if (updated & MD_macaddr) {
302934683adeSsg70180 		cmn_err(CE_NOTE, "!vsw%d: changing mac address to 0x%lx",
303034683adeSsg70180 		    vswp->instance, macaddr);
303134683adeSsg70180 
303234683adeSsg70180 		WRITE_ENTER(&vswp->if_lockrw);
303334683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
303434683adeSsg70180 			vswp->if_addr.ether_addr_octet[i] = macaddr & 0xFF;
303534683adeSsg70180 			macaddr >>= 8;
303634683adeSsg70180 		}
303734683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
303834683adeSsg70180 
303934683adeSsg70180 		/*
30405f94e909Ssg70180 		 * Remove old address from HW (if programmed) and set
30415f94e909Ssg70180 		 * new address.
30425f94e909Ssg70180 		 */
30435f94e909Ssg70180 		mutex_enter(&vswp->hw_lock);
30445f94e909Ssg70180 		(void) vsw_unset_hw(vswp, NULL, VSW_LOCALDEV);
30455f94e909Ssg70180 		(void) vsw_set_hw(vswp, NULL, VSW_LOCALDEV);
30465f94e909Ssg70180 		mutex_exit(&vswp->hw_lock);
30475f94e909Ssg70180 
30485f94e909Ssg70180 		/*
304934683adeSsg70180 		 * Notify the MAC layer of the changed address.
305034683adeSsg70180 		 */
305134683adeSsg70180 		mac_unicst_update(vswp->if_mh, (uint8_t *)&vswp->if_addr);
305234683adeSsg70180 	}
305334683adeSsg70180 
305434683adeSsg70180 	return;
305534683adeSsg70180 
305634683adeSsg70180 fail_reconf:
305734683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: configuration unchanged", vswp->instance);
305834683adeSsg70180 	return;
305934683adeSsg70180 
306034683adeSsg70180 fail_update:
306134683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: update of configuration failed",
306234683adeSsg70180 	    vswp->instance);
306334683adeSsg70180 }
306434683adeSsg70180 
306534683adeSsg70180 /*
30661ae08745Sheppo  * Add a new port to the system.
30671ae08745Sheppo  *
30681ae08745Sheppo  * Returns 0 on success, 1 on failure.
30691ae08745Sheppo  */
30701ae08745Sheppo int
30711ae08745Sheppo vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node)
30721ae08745Sheppo {
30731ae08745Sheppo 	uint64_t		ldc_id;
30741ae08745Sheppo 	uint8_t			*addrp;
30751ae08745Sheppo 	int			i, addrsz;
30761ae08745Sheppo 	int			num_nodes = 0, nchan = 0;
30771ae08745Sheppo 	int			listsz = 0;
30781ae08745Sheppo 	mde_cookie_t		*listp = NULL;
30791ae08745Sheppo 	struct ether_addr	ea;
30801ae08745Sheppo 	uint64_t		macaddr;
30811ae08745Sheppo 	uint64_t		inst = 0;
30821ae08745Sheppo 	vsw_port_t		*port;
30831ae08745Sheppo 
30841ae08745Sheppo 	if (md_get_prop_val(mdp, *node, id_propname, &inst)) {
30851ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found", __func__,
30861ae08745Sheppo 		    id_propname);
30871ae08745Sheppo 		return (1);
30881ae08745Sheppo 	}
30891ae08745Sheppo 
30901ae08745Sheppo 	/*
30911ae08745Sheppo 	 * Find the channel endpoint node(s) (which should be under this
30921ae08745Sheppo 	 * port node) which contain the channel id(s).
30931ae08745Sheppo 	 */
30941ae08745Sheppo 	if ((num_nodes = md_node_count(mdp)) <= 0) {
30951ae08745Sheppo 		DERR(vswp, "%s: invalid number of nodes found (%d)",
30961ae08745Sheppo 		    __func__, num_nodes);
30971ae08745Sheppo 		return (1);
30981ae08745Sheppo 	}
30991ae08745Sheppo 
310034683adeSsg70180 	D2(vswp, "%s: %d nodes found", __func__, num_nodes);
310134683adeSsg70180 
31021ae08745Sheppo 	/* allocate enough space for node list */
31031ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
31041ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
31051ae08745Sheppo 
3106205eeb1aSlm66018 	nchan = md_scan_dag(mdp, *node, md_find_name(mdp, chan_propname),
31071ae08745Sheppo 	    md_find_name(mdp, "fwd"), listp);
31081ae08745Sheppo 
31091ae08745Sheppo 	if (nchan <= 0) {
31101ae08745Sheppo 		DWARN(vswp, "%s: no %s nodes found", __func__, chan_propname);
31111ae08745Sheppo 		kmem_free(listp, listsz);
31121ae08745Sheppo 		return (1);
31131ae08745Sheppo 	}
31141ae08745Sheppo 
31151ae08745Sheppo 	D2(vswp, "%s: %d %s nodes found", __func__, nchan, chan_propname);
31161ae08745Sheppo 
31171ae08745Sheppo 	/* use property from first node found */
31181ae08745Sheppo 	if (md_get_prop_val(mdp, listp[0], id_propname, &ldc_id)) {
31191ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found\n", __func__,
31201ae08745Sheppo 		    id_propname);
31211ae08745Sheppo 		kmem_free(listp, listsz);
31221ae08745Sheppo 		return (1);
31231ae08745Sheppo 	}
31241ae08745Sheppo 
31251ae08745Sheppo 	/* don't need list any more */
31261ae08745Sheppo 	kmem_free(listp, listsz);
31271ae08745Sheppo 
31281ae08745Sheppo 	D2(vswp, "%s: ldc_id 0x%llx", __func__, ldc_id);
31291ae08745Sheppo 
31301ae08745Sheppo 	/* read mac-address property */
31311ae08745Sheppo 	if (md_get_prop_data(mdp, *node, remaddr_propname,
31321ae08745Sheppo 	    &addrp, &addrsz)) {
31331ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found",
31341ae08745Sheppo 		    __func__, remaddr_propname);
31351ae08745Sheppo 		return (1);
31361ae08745Sheppo 	}
31371ae08745Sheppo 
31381ae08745Sheppo 	if (addrsz < ETHERADDRL) {
31391ae08745Sheppo 		DWARN(vswp, "%s: invalid address size", __func__);
31401ae08745Sheppo 		return (1);
31411ae08745Sheppo 	}
31421ae08745Sheppo 
31431ae08745Sheppo 	macaddr = *((uint64_t *)addrp);
31441ae08745Sheppo 	D2(vswp, "%s: remote mac address 0x%llx", __func__, macaddr);
31451ae08745Sheppo 
31461ae08745Sheppo 	for (i = ETHERADDRL - 1; i >= 0; i--) {
31471ae08745Sheppo 		ea.ether_addr_octet[i] = macaddr & 0xFF;
31481ae08745Sheppo 		macaddr >>= 8;
31491ae08745Sheppo 	}
31501ae08745Sheppo 
31511ae08745Sheppo 	if (vsw_port_attach(vswp, (int)inst, &ldc_id, 1, &ea) != 0) {
31521ae08745Sheppo 		DERR(vswp, "%s: failed to attach port", __func__);
31531ae08745Sheppo 		return (1);
31541ae08745Sheppo 	}
31551ae08745Sheppo 
31561ae08745Sheppo 	port = vsw_lookup_port(vswp, (int)inst);
31571ae08745Sheppo 
31581ae08745Sheppo 	/* just successfuly created the port, so it should exist */
31591ae08745Sheppo 	ASSERT(port != NULL);
31601ae08745Sheppo 
31611ae08745Sheppo 	return (0);
31621ae08745Sheppo }
31631ae08745Sheppo 
31641ae08745Sheppo /*
31651ae08745Sheppo  * Attach the specified port.
31661ae08745Sheppo  *
31671ae08745Sheppo  * Returns 0 on success, 1 on failure.
31681ae08745Sheppo  */
31691ae08745Sheppo static int
31701ae08745Sheppo vsw_port_attach(vsw_t *vswp, int p_instance, uint64_t *ldcids, int nids,
31711ae08745Sheppo struct ether_addr *macaddr)
31721ae08745Sheppo {
31731ae08745Sheppo 	vsw_port_list_t		*plist = &vswp->plist;
31741ae08745Sheppo 	vsw_port_t		*port, **prev_port;
31751ae08745Sheppo 	int			i;
31761ae08745Sheppo 
31771ae08745Sheppo 	D1(vswp, "%s: enter : port %d", __func__, p_instance);
31781ae08745Sheppo 
31791ae08745Sheppo 	/* port already exists? */
31801ae08745Sheppo 	READ_ENTER(&plist->lockrw);
31811ae08745Sheppo 	for (port = plist->head; port != NULL; port = port->p_next) {
31821ae08745Sheppo 		if (port->p_instance == p_instance) {
31831ae08745Sheppo 			DWARN(vswp, "%s: port instance %d already attached",
31841ae08745Sheppo 			    __func__, p_instance);
31851ae08745Sheppo 			RW_EXIT(&plist->lockrw);
31861ae08745Sheppo 			return (1);
31871ae08745Sheppo 		}
31881ae08745Sheppo 	}
31891ae08745Sheppo 	RW_EXIT(&plist->lockrw);
31901ae08745Sheppo 
31911ae08745Sheppo 	port = kmem_zalloc(sizeof (vsw_port_t), KM_SLEEP);
31921ae08745Sheppo 	port->p_vswp = vswp;
31931ae08745Sheppo 	port->p_instance = p_instance;
31941ae08745Sheppo 	port->p_ldclist.num_ldcs = 0;
31951ae08745Sheppo 	port->p_ldclist.head = NULL;
3196e1ebb9ecSlm66018 	port->addr_set = VSW_ADDR_UNSET;
31971ae08745Sheppo 
31981ae08745Sheppo 	rw_init(&port->p_ldclist.lockrw, NULL, RW_DRIVER, NULL);
31991ae08745Sheppo 
32001ae08745Sheppo 	mutex_init(&port->tx_lock, NULL, MUTEX_DRIVER, NULL);
32011ae08745Sheppo 	mutex_init(&port->mca_lock, NULL, MUTEX_DRIVER, NULL);
32021ae08745Sheppo 
32031ae08745Sheppo 	mutex_init(&port->ref_lock, NULL, MUTEX_DRIVER, NULL);
32041ae08745Sheppo 	cv_init(&port->ref_cv, NULL, CV_DRIVER, NULL);
32051ae08745Sheppo 
32061ae08745Sheppo 	mutex_init(&port->state_lock, NULL, MUTEX_DRIVER, NULL);
32071ae08745Sheppo 	cv_init(&port->state_cv, NULL, CV_DRIVER, NULL);
32081ae08745Sheppo 	port->state = VSW_PORT_INIT;
32091ae08745Sheppo 
32101ae08745Sheppo 	if (nids > VSW_PORT_MAX_LDCS) {
3211205eeb1aSlm66018 		D2(vswp, "%s: using first of %d ldc ids", __func__, nids);
32121ae08745Sheppo 		nids = VSW_PORT_MAX_LDCS;
32131ae08745Sheppo 	}
32141ae08745Sheppo 
32151ae08745Sheppo 	D2(vswp, "%s: %d nids", __func__, nids);
32161ae08745Sheppo 	for (i = 0; i < nids; i++) {
32171ae08745Sheppo 		D2(vswp, "%s: ldcid (%llx)", __func__, (uint64_t)ldcids[i]);
32181ae08745Sheppo 		if (vsw_ldc_attach(port, (uint64_t)ldcids[i]) != 0) {
32191ae08745Sheppo 			DERR(vswp, "%s: ldc_attach failed", __func__);
32201ae08745Sheppo 
32211ae08745Sheppo 			rw_destroy(&port->p_ldclist.lockrw);
32221ae08745Sheppo 
32231ae08745Sheppo 			cv_destroy(&port->ref_cv);
32241ae08745Sheppo 			mutex_destroy(&port->ref_lock);
32251ae08745Sheppo 
32261ae08745Sheppo 			cv_destroy(&port->state_cv);
32271ae08745Sheppo 			mutex_destroy(&port->state_lock);
32281ae08745Sheppo 
32291ae08745Sheppo 			mutex_destroy(&port->tx_lock);
32301ae08745Sheppo 			mutex_destroy(&port->mca_lock);
32311ae08745Sheppo 			kmem_free(port, sizeof (vsw_port_t));
32321ae08745Sheppo 			return (1);
32331ae08745Sheppo 		}
32341ae08745Sheppo 	}
32351ae08745Sheppo 
32361ae08745Sheppo 	ether_copy(macaddr, &port->p_macaddr);
32371ae08745Sheppo 
32381ae08745Sheppo 	WRITE_ENTER(&plist->lockrw);
32391ae08745Sheppo 
32401ae08745Sheppo 	/* create the fdb entry for this port/mac address */
32411ae08745Sheppo 	(void) vsw_add_fdb(vswp, port);
32421ae08745Sheppo 
32435f94e909Ssg70180 	mutex_enter(&vswp->hw_lock);
32445f94e909Ssg70180 	(void) vsw_set_hw(vswp, port, VSW_VNETPORT);
32455f94e909Ssg70180 	mutex_exit(&vswp->hw_lock);
3246e1ebb9ecSlm66018 
32471ae08745Sheppo 	/* link it into the list of ports for this vsw instance */
32481ae08745Sheppo 	prev_port = (vsw_port_t **)(&plist->head);
32491ae08745Sheppo 	port->p_next = *prev_port;
32501ae08745Sheppo 	*prev_port = port;
32511ae08745Sheppo 	plist->num_ports++;
32521ae08745Sheppo 	RW_EXIT(&plist->lockrw);
32531ae08745Sheppo 
32541ae08745Sheppo 	/*
32551ae08745Sheppo 	 * Initialise the port and any ldc's under it.
32561ae08745Sheppo 	 */
32571ae08745Sheppo 	(void) vsw_init_ldcs(port);
32581ae08745Sheppo 
32591ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
32601ae08745Sheppo 	return (0);
32611ae08745Sheppo }
32621ae08745Sheppo 
32631ae08745Sheppo /*
32641ae08745Sheppo  * Detach the specified port.
32651ae08745Sheppo  *
32661ae08745Sheppo  * Returns 0 on success, 1 on failure.
32671ae08745Sheppo  */
32681ae08745Sheppo static int
32691ae08745Sheppo vsw_port_detach(vsw_t *vswp, int p_instance)
32701ae08745Sheppo {
32711ae08745Sheppo 	vsw_port_t	*port = NULL;
32721ae08745Sheppo 	vsw_port_list_t	*plist = &vswp->plist;
32731ae08745Sheppo 
32741ae08745Sheppo 	D1(vswp, "%s: enter: port id %d", __func__, p_instance);
32751ae08745Sheppo 
32761ae08745Sheppo 	WRITE_ENTER(&plist->lockrw);
32771ae08745Sheppo 
32781ae08745Sheppo 	if ((port = vsw_lookup_port(vswp, p_instance)) == NULL) {
32791ae08745Sheppo 		RW_EXIT(&plist->lockrw);
32801ae08745Sheppo 		return (1);
32811ae08745Sheppo 	}
32821ae08745Sheppo 
32831ae08745Sheppo 	if (vsw_plist_del_node(vswp, port)) {
32841ae08745Sheppo 		RW_EXIT(&plist->lockrw);
32851ae08745Sheppo 		return (1);
32861ae08745Sheppo 	}
32871ae08745Sheppo 
32881ae08745Sheppo 	/* Remove the fdb entry for this port/mac address */
32891ae08745Sheppo 	(void) vsw_del_fdb(vswp, port);
32901ae08745Sheppo 
32911ae08745Sheppo 	/* Remove any multicast addresses.. */
32921ae08745Sheppo 	vsw_del_mcst_port(port);
32931ae08745Sheppo 
32941ae08745Sheppo 	/*
3295e1ebb9ecSlm66018 	 * No longer need to hold writer lock on port list now
3296e1ebb9ecSlm66018 	 * that we have unlinked the target port from the list.
32971ae08745Sheppo 	 */
32981ae08745Sheppo 	RW_EXIT(&plist->lockrw);
32991ae08745Sheppo 
33005f94e909Ssg70180 	/* Remove address if was programmed into HW. */
33015f94e909Ssg70180 	mutex_enter(&vswp->hw_lock);
33025f94e909Ssg70180 	(void) vsw_unset_hw(vswp, port, VSW_VNETPORT);
3303e1ebb9ecSlm66018 	if (vswp->recfg_reqd)
33045f94e909Ssg70180 		vsw_reconfig_hw(vswp);
33055f94e909Ssg70180 	mutex_exit(&vswp->hw_lock);
3306e1ebb9ecSlm66018 
33071ae08745Sheppo 	if (vsw_port_delete(port)) {
33081ae08745Sheppo 		return (1);
33091ae08745Sheppo 	}
33101ae08745Sheppo 
33111ae08745Sheppo 	D1(vswp, "%s: exit: p_instance(%d)", __func__, p_instance);
33121ae08745Sheppo 	return (0);
33131ae08745Sheppo }
33141ae08745Sheppo 
33151ae08745Sheppo /*
33161ae08745Sheppo  * Detach all active ports.
33171ae08745Sheppo  *
33181ae08745Sheppo  * Returns 0 on success, 1 on failure.
33191ae08745Sheppo  */
33201ae08745Sheppo static int
33211ae08745Sheppo vsw_detach_ports(vsw_t *vswp)
33221ae08745Sheppo {
33231ae08745Sheppo 	vsw_port_list_t 	*plist = &vswp->plist;
33241ae08745Sheppo 	vsw_port_t		*port = NULL;
33251ae08745Sheppo 
33261ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
33271ae08745Sheppo 
33281ae08745Sheppo 	WRITE_ENTER(&plist->lockrw);
33291ae08745Sheppo 
33301ae08745Sheppo 	while ((port = plist->head) != NULL) {
33311ae08745Sheppo 		if (vsw_plist_del_node(vswp, port)) {
33321ae08745Sheppo 			DERR(vswp, "%s: Error deleting port %d"
3333205eeb1aSlm66018 			    " from port list", __func__, port->p_instance);
33341ae08745Sheppo 			RW_EXIT(&plist->lockrw);
33351ae08745Sheppo 			return (1);
33361ae08745Sheppo 		}
33371ae08745Sheppo 
3338e1ebb9ecSlm66018 		/* Remove address if was programmed into HW. */
33395f94e909Ssg70180 		mutex_enter(&vswp->hw_lock);
33405f94e909Ssg70180 		(void) vsw_unset_hw(vswp, port, VSW_VNETPORT);
33415f94e909Ssg70180 		mutex_exit(&vswp->hw_lock);
3342e1ebb9ecSlm66018 
33431ae08745Sheppo 		/* Remove the fdb entry for this port/mac address */
33441ae08745Sheppo 		(void) vsw_del_fdb(vswp, port);
33451ae08745Sheppo 
33461ae08745Sheppo 		/* Remove any multicast addresses.. */
33471ae08745Sheppo 		vsw_del_mcst_port(port);
33481ae08745Sheppo 
33491ae08745Sheppo 		/*
33501ae08745Sheppo 		 * No longer need to hold the lock on the port list
33511ae08745Sheppo 		 * now that we have unlinked the target port from the
33521ae08745Sheppo 		 * list.
33531ae08745Sheppo 		 */
33541ae08745Sheppo 		RW_EXIT(&plist->lockrw);
33551ae08745Sheppo 		if (vsw_port_delete(port)) {
33561ae08745Sheppo 			DERR(vswp, "%s: Error deleting port %d",
33571ae08745Sheppo 			    __func__, port->p_instance);
33581ae08745Sheppo 			return (1);
33591ae08745Sheppo 		}
33601ae08745Sheppo 		WRITE_ENTER(&plist->lockrw);
33611ae08745Sheppo 	}
33621ae08745Sheppo 	RW_EXIT(&plist->lockrw);
33631ae08745Sheppo 
33641ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
33651ae08745Sheppo 
33661ae08745Sheppo 	return (0);
33671ae08745Sheppo }
33681ae08745Sheppo 
33691ae08745Sheppo /*
33701ae08745Sheppo  * Delete the specified port.
33711ae08745Sheppo  *
33721ae08745Sheppo  * Returns 0 on success, 1 on failure.
33731ae08745Sheppo  */
33741ae08745Sheppo static int
33751ae08745Sheppo vsw_port_delete(vsw_port_t *port)
33761ae08745Sheppo {
33771ae08745Sheppo 	vsw_ldc_list_t 		*ldcl;
33781ae08745Sheppo 	vsw_t			*vswp = port->p_vswp;
33791ae08745Sheppo 
33801ae08745Sheppo 	D1(vswp, "%s: enter : port id %d", __func__, port->p_instance);
33811ae08745Sheppo 
33821ae08745Sheppo 	(void) vsw_uninit_ldcs(port);
33831ae08745Sheppo 
33841ae08745Sheppo 	/*
33851ae08745Sheppo 	 * Wait for any pending ctrl msg tasks which reference this
33861ae08745Sheppo 	 * port to finish.
33871ae08745Sheppo 	 */
33881ae08745Sheppo 	if (vsw_drain_port_taskq(port))
33891ae08745Sheppo 		return (1);
33901ae08745Sheppo 
33911ae08745Sheppo 	/*
33921ae08745Sheppo 	 * Wait for port reference count to hit zero.
33931ae08745Sheppo 	 */
33941ae08745Sheppo 	mutex_enter(&port->ref_lock);
33951ae08745Sheppo 	while (port->ref_cnt != 0)
33961ae08745Sheppo 		cv_wait(&port->ref_cv, &port->ref_lock);
33971ae08745Sheppo 	mutex_exit(&port->ref_lock);
33981ae08745Sheppo 
33991ae08745Sheppo 	/*
34001ae08745Sheppo 	 * Wait for any active callbacks to finish
34011ae08745Sheppo 	 */
34021ae08745Sheppo 	if (vsw_drain_ldcs(port))
34031ae08745Sheppo 		return (1);
34041ae08745Sheppo 
34051ae08745Sheppo 	ldcl = &port->p_ldclist;
34061ae08745Sheppo 	WRITE_ENTER(&ldcl->lockrw);
34071ae08745Sheppo 	while (ldcl->num_ldcs > 0) {
3408205eeb1aSlm66018 		if (vsw_ldc_detach(port, ldcl->head->ldc_id) != 0) {
340934683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: unable to detach ldc %ld",
341034683adeSsg70180 			    vswp->instance, ldcl->head->ldc_id);
34111ae08745Sheppo 			RW_EXIT(&ldcl->lockrw);
34121ae08745Sheppo 			return (1);
34131ae08745Sheppo 		}
34141ae08745Sheppo 	}
34151ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
34161ae08745Sheppo 
34171ae08745Sheppo 	rw_destroy(&port->p_ldclist.lockrw);
34181ae08745Sheppo 
34191ae08745Sheppo 	mutex_destroy(&port->mca_lock);
34201ae08745Sheppo 	mutex_destroy(&port->tx_lock);
34211ae08745Sheppo 	cv_destroy(&port->ref_cv);
34221ae08745Sheppo 	mutex_destroy(&port->ref_lock);
34231ae08745Sheppo 
34241ae08745Sheppo 	cv_destroy(&port->state_cv);
34251ae08745Sheppo 	mutex_destroy(&port->state_lock);
34261ae08745Sheppo 
34271ae08745Sheppo 	kmem_free(port, sizeof (vsw_port_t));
34281ae08745Sheppo 
34291ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
34301ae08745Sheppo 
34311ae08745Sheppo 	return (0);
34321ae08745Sheppo }
34331ae08745Sheppo 
34341ae08745Sheppo /*
34351ae08745Sheppo  * Attach a logical domain channel (ldc) under a specified port.
34361ae08745Sheppo  *
34371ae08745Sheppo  * Returns 0 on success, 1 on failure.
34381ae08745Sheppo  */
34391ae08745Sheppo static int
34401ae08745Sheppo vsw_ldc_attach(vsw_port_t *port, uint64_t ldc_id)
34411ae08745Sheppo {
34421ae08745Sheppo 	vsw_t 		*vswp = port->p_vswp;
34431ae08745Sheppo 	vsw_ldc_list_t *ldcl = &port->p_ldclist;
34441ae08745Sheppo 	vsw_ldc_t 	*ldcp = NULL;
34451ae08745Sheppo 	ldc_attr_t 	attr;
34461ae08745Sheppo 	ldc_status_t	istatus;
34471ae08745Sheppo 	int 		status = DDI_FAILURE;
3448d10e4ef2Snarayan 	int		rv;
34493af08d82Slm66018 	enum		{ PROG_init = 0x0, PROG_mblks = 0x1,
34503af08d82Slm66018 				PROG_callback = 0x2}
34513af08d82Slm66018 			progress;
34523af08d82Slm66018 
34533af08d82Slm66018 	progress = PROG_init;
34541ae08745Sheppo 
34551ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
34561ae08745Sheppo 
34571ae08745Sheppo 	ldcp = kmem_zalloc(sizeof (vsw_ldc_t), KM_NOSLEEP);
34581ae08745Sheppo 	if (ldcp == NULL) {
34591ae08745Sheppo 		DERR(vswp, "%s: kmem_zalloc failed", __func__);
34601ae08745Sheppo 		return (1);
34611ae08745Sheppo 	}
34621ae08745Sheppo 	ldcp->ldc_id = ldc_id;
34631ae08745Sheppo 
3464d10e4ef2Snarayan 	/* allocate pool of receive mblks */
3465d10e4ef2Snarayan 	rv = vio_create_mblks(vsw_num_mblks, vsw_mblk_size, &(ldcp->rxh));
3466d10e4ef2Snarayan 	if (rv) {
3467d10e4ef2Snarayan 		DWARN(vswp, "%s: unable to create free mblk pool for"
3468d10e4ef2Snarayan 		    " channel %ld (rv %d)", __func__, ldc_id, rv);
3469d10e4ef2Snarayan 		kmem_free(ldcp, sizeof (vsw_ldc_t));
3470d10e4ef2Snarayan 		return (1);
3471d10e4ef2Snarayan 	}
3472d10e4ef2Snarayan 
34733af08d82Slm66018 	progress |= PROG_mblks;
34743af08d82Slm66018 
34751ae08745Sheppo 	mutex_init(&ldcp->ldc_txlock, NULL, MUTEX_DRIVER, NULL);
34761ae08745Sheppo 	mutex_init(&ldcp->ldc_cblock, NULL, MUTEX_DRIVER, NULL);
34771ae08745Sheppo 	mutex_init(&ldcp->drain_cv_lock, NULL, MUTEX_DRIVER, NULL);
34781ae08745Sheppo 	cv_init(&ldcp->drain_cv, NULL, CV_DRIVER, NULL);
3479445b4c2eSsb155480 	rw_init(&ldcp->lane_in.dlistrw, NULL, RW_DRIVER, NULL);
3480445b4c2eSsb155480 	rw_init(&ldcp->lane_out.dlistrw, NULL, RW_DRIVER, NULL);
34811ae08745Sheppo 
34821ae08745Sheppo 	/* required for handshake with peer */
34831ae08745Sheppo 	ldcp->local_session = (uint64_t)ddi_get_lbolt();
34841ae08745Sheppo 	ldcp->peer_session = 0;
34851ae08745Sheppo 	ldcp->session_status = 0;
34861ae08745Sheppo 
34871ae08745Sheppo 	mutex_init(&ldcp->hss_lock, NULL, MUTEX_DRIVER, NULL);
34881ae08745Sheppo 	ldcp->hss_id = 1;	/* Initial handshake session id */
34891ae08745Sheppo 
34901ae08745Sheppo 	/* only set for outbound lane, inbound set by peer */
3491d10e4ef2Snarayan 	mutex_init(&ldcp->lane_in.seq_lock, NULL, MUTEX_DRIVER, NULL);
3492d10e4ef2Snarayan 	mutex_init(&ldcp->lane_out.seq_lock, NULL, MUTEX_DRIVER, NULL);
34931ae08745Sheppo 	vsw_set_lane_attr(vswp, &ldcp->lane_out);
34941ae08745Sheppo 
34951ae08745Sheppo 	attr.devclass = LDC_DEV_NT_SVC;
34961ae08745Sheppo 	attr.instance = ddi_get_instance(vswp->dip);
34971ae08745Sheppo 	attr.mode = LDC_MODE_UNRELIABLE;
3498e1ebb9ecSlm66018 	attr.mtu = VSW_LDC_MTU;
34991ae08745Sheppo 	status = ldc_init(ldc_id, &attr, &ldcp->ldc_handle);
35001ae08745Sheppo 	if (status != 0) {
35011ae08745Sheppo 		DERR(vswp, "%s(%lld): ldc_init failed, rv (%d)",
35021ae08745Sheppo 		    __func__, ldc_id, status);
3503d10e4ef2Snarayan 		goto ldc_attach_fail;
35041ae08745Sheppo 	}
35051ae08745Sheppo 
35061ae08745Sheppo 	status = ldc_reg_callback(ldcp->ldc_handle, vsw_ldc_cb, (caddr_t)ldcp);
35071ae08745Sheppo 	if (status != 0) {
35081ae08745Sheppo 		DERR(vswp, "%s(%lld): ldc_reg_callback failed, rv (%d)",
35091ae08745Sheppo 		    __func__, ldc_id, status);
35101ae08745Sheppo 		(void) ldc_fini(ldcp->ldc_handle);
3511d10e4ef2Snarayan 		goto ldc_attach_fail;
35121ae08745Sheppo 	}
35131ae08745Sheppo 
35143af08d82Slm66018 	progress |= PROG_callback;
35153af08d82Slm66018 
35163af08d82Slm66018 	mutex_init(&ldcp->status_lock, NULL, MUTEX_DRIVER, NULL);
35171ae08745Sheppo 
35181ae08745Sheppo 	if (ldc_status(ldcp->ldc_handle, &istatus) != 0) {
35191ae08745Sheppo 		DERR(vswp, "%s: ldc_status failed", __func__);
35203af08d82Slm66018 		mutex_destroy(&ldcp->status_lock);
35213af08d82Slm66018 		goto ldc_attach_fail;
35221ae08745Sheppo 	}
35231ae08745Sheppo 
35241ae08745Sheppo 	ldcp->ldc_status = istatus;
35251ae08745Sheppo 	ldcp->ldc_port = port;
35261ae08745Sheppo 	ldcp->ldc_vswp = vswp;
35271ae08745Sheppo 
35281ae08745Sheppo 	/* link it into the list of channels for this port */
35291ae08745Sheppo 	WRITE_ENTER(&ldcl->lockrw);
35301ae08745Sheppo 	ldcp->ldc_next = ldcl->head;
35311ae08745Sheppo 	ldcl->head = ldcp;
35321ae08745Sheppo 	ldcl->num_ldcs++;
35331ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
35341ae08745Sheppo 
35351ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
35361ae08745Sheppo 	return (0);
3537d10e4ef2Snarayan 
3538d10e4ef2Snarayan ldc_attach_fail:
3539d10e4ef2Snarayan 	mutex_destroy(&ldcp->ldc_txlock);
3540d10e4ef2Snarayan 	mutex_destroy(&ldcp->ldc_cblock);
3541d10e4ef2Snarayan 
3542d10e4ef2Snarayan 	cv_destroy(&ldcp->drain_cv);
3543d10e4ef2Snarayan 
3544445b4c2eSsb155480 	rw_destroy(&ldcp->lane_in.dlistrw);
3545445b4c2eSsb155480 	rw_destroy(&ldcp->lane_out.dlistrw);
3546445b4c2eSsb155480 
35473af08d82Slm66018 	if (progress & PROG_callback) {
35483af08d82Slm66018 		(void) ldc_unreg_callback(ldcp->ldc_handle);
35493af08d82Slm66018 	}
35503af08d82Slm66018 
35513af08d82Slm66018 	if ((progress & PROG_mblks) && (ldcp->rxh != NULL)) {
3552d10e4ef2Snarayan 		if (vio_destroy_mblks(ldcp->rxh) != 0) {
3553d10e4ef2Snarayan 			/*
3554d10e4ef2Snarayan 			 * Something odd has happened, as the destroy
3555d10e4ef2Snarayan 			 * will only fail if some mblks have been allocated
3556d10e4ef2Snarayan 			 * from the pool already (which shouldn't happen)
3557d10e4ef2Snarayan 			 * and have not been returned.
3558d10e4ef2Snarayan 			 *
3559d10e4ef2Snarayan 			 * Add the pool pointer to a list maintained in
3560d10e4ef2Snarayan 			 * the device instance. Another attempt will be made
3561d10e4ef2Snarayan 			 * to free the pool when the device itself detaches.
3562d10e4ef2Snarayan 			 */
356334683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Creation of ldc channel %ld "
356434683adeSsg70180 			    "failed and cannot destroy associated mblk "
356534683adeSsg70180 			    "pool", vswp->instance, ldc_id);
3566d10e4ef2Snarayan 			ldcp->rxh->nextp =  vswp->rxh;
3567d10e4ef2Snarayan 			vswp->rxh = ldcp->rxh;
3568d10e4ef2Snarayan 		}
3569d10e4ef2Snarayan 	}
3570d10e4ef2Snarayan 	mutex_destroy(&ldcp->drain_cv_lock);
3571d10e4ef2Snarayan 	mutex_destroy(&ldcp->hss_lock);
3572d10e4ef2Snarayan 
3573d10e4ef2Snarayan 	mutex_destroy(&ldcp->lane_in.seq_lock);
3574d10e4ef2Snarayan 	mutex_destroy(&ldcp->lane_out.seq_lock);
3575d10e4ef2Snarayan 	kmem_free(ldcp, sizeof (vsw_ldc_t));
3576d10e4ef2Snarayan 
3577d10e4ef2Snarayan 	return (1);
35781ae08745Sheppo }
35791ae08745Sheppo 
35801ae08745Sheppo /*
35811ae08745Sheppo  * Detach a logical domain channel (ldc) belonging to a
35821ae08745Sheppo  * particular port.
35831ae08745Sheppo  *
35841ae08745Sheppo  * Returns 0 on success, 1 on failure.
35851ae08745Sheppo  */
35861ae08745Sheppo static int
35871ae08745Sheppo vsw_ldc_detach(vsw_port_t *port, uint64_t ldc_id)
35881ae08745Sheppo {
35891ae08745Sheppo 	vsw_t 		*vswp = port->p_vswp;
35901ae08745Sheppo 	vsw_ldc_t 	*ldcp, *prev_ldcp;
35911ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
35921ae08745Sheppo 	int 		rv;
35931ae08745Sheppo 
35941ae08745Sheppo 	prev_ldcp = ldcl->head;
35951ae08745Sheppo 	for (; (ldcp = prev_ldcp) != NULL; prev_ldcp = ldcp->ldc_next) {
35961ae08745Sheppo 		if (ldcp->ldc_id == ldc_id) {
35971ae08745Sheppo 			break;
35981ae08745Sheppo 		}
35991ae08745Sheppo 	}
36001ae08745Sheppo 
36011ae08745Sheppo 	/* specified ldc id not found */
36021ae08745Sheppo 	if (ldcp == NULL) {
36031ae08745Sheppo 		DERR(vswp, "%s: ldcp = NULL", __func__);
36041ae08745Sheppo 		return (1);
36051ae08745Sheppo 	}
36061ae08745Sheppo 
36071ae08745Sheppo 	D2(vswp, "%s: detaching channel %lld", __func__, ldcp->ldc_id);
36081ae08745Sheppo 
36091ae08745Sheppo 	/*
36101ae08745Sheppo 	 * Before we can close the channel we must release any mapped
36111ae08745Sheppo 	 * resources (e.g. drings).
36121ae08745Sheppo 	 */
36131ae08745Sheppo 	vsw_free_lane_resources(ldcp, INBOUND);
36141ae08745Sheppo 	vsw_free_lane_resources(ldcp, OUTBOUND);
36151ae08745Sheppo 
36161ae08745Sheppo 	/*
36171ae08745Sheppo 	 * If the close fails we are in serious trouble, as won't
36181ae08745Sheppo 	 * be able to delete the parent port.
36191ae08745Sheppo 	 */
36201ae08745Sheppo 	if ((rv = ldc_close(ldcp->ldc_handle)) != 0) {
36211ae08745Sheppo 		DERR(vswp, "%s: error %d closing channel %lld",
36221ae08745Sheppo 		    __func__, rv, ldcp->ldc_id);
36231ae08745Sheppo 		return (1);
36241ae08745Sheppo 	}
36251ae08745Sheppo 
36261ae08745Sheppo 	(void) ldc_fini(ldcp->ldc_handle);
36271ae08745Sheppo 
36281ae08745Sheppo 	ldcp->ldc_status = LDC_INIT;
36291ae08745Sheppo 	ldcp->ldc_handle = NULL;
36301ae08745Sheppo 	ldcp->ldc_vswp = NULL;
3631d10e4ef2Snarayan 
3632d10e4ef2Snarayan 	if (ldcp->rxh != NULL) {
3633d10e4ef2Snarayan 		if (vio_destroy_mblks(ldcp->rxh)) {
3634d10e4ef2Snarayan 			/*
3635d10e4ef2Snarayan 			 * Mostly likely some mblks are still in use and
3636d10e4ef2Snarayan 			 * have not been returned to the pool. Add the pool
3637d10e4ef2Snarayan 			 * to the list maintained in the device instance.
3638d10e4ef2Snarayan 			 * Another attempt will be made to destroy the pool
3639d10e4ef2Snarayan 			 * when the device detaches.
3640d10e4ef2Snarayan 			 */
3641d10e4ef2Snarayan 			ldcp->rxh->nextp =  vswp->rxh;
3642d10e4ef2Snarayan 			vswp->rxh = ldcp->rxh;
3643d10e4ef2Snarayan 		}
3644d10e4ef2Snarayan 	}
3645d10e4ef2Snarayan 
36463af08d82Slm66018 	/* unlink it from the list */
36473af08d82Slm66018 	prev_ldcp = ldcp->ldc_next;
36483af08d82Slm66018 	ldcl->num_ldcs--;
36493af08d82Slm66018 
36501ae08745Sheppo 	mutex_destroy(&ldcp->ldc_txlock);
36511ae08745Sheppo 	mutex_destroy(&ldcp->ldc_cblock);
36521ae08745Sheppo 	cv_destroy(&ldcp->drain_cv);
36531ae08745Sheppo 	mutex_destroy(&ldcp->drain_cv_lock);
36541ae08745Sheppo 	mutex_destroy(&ldcp->hss_lock);
3655d10e4ef2Snarayan 	mutex_destroy(&ldcp->lane_in.seq_lock);
3656d10e4ef2Snarayan 	mutex_destroy(&ldcp->lane_out.seq_lock);
36573af08d82Slm66018 	mutex_destroy(&ldcp->status_lock);
3658445b4c2eSsb155480 	rw_destroy(&ldcp->lane_in.dlistrw);
3659445b4c2eSsb155480 	rw_destroy(&ldcp->lane_out.dlistrw);
36601ae08745Sheppo 
36611ae08745Sheppo 	kmem_free(ldcp, sizeof (vsw_ldc_t));
36621ae08745Sheppo 
36631ae08745Sheppo 	return (0);
36641ae08745Sheppo }
36651ae08745Sheppo 
36661ae08745Sheppo /*
36671ae08745Sheppo  * Open and attempt to bring up the channel. Note that channel
36681ae08745Sheppo  * can only be brought up if peer has also opened channel.
36691ae08745Sheppo  *
36701ae08745Sheppo  * Returns 0 if can open and bring up channel, otherwise
36711ae08745Sheppo  * returns 1.
36721ae08745Sheppo  */
36731ae08745Sheppo static int
36741ae08745Sheppo vsw_ldc_init(vsw_ldc_t *ldcp)
36751ae08745Sheppo {
36761ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
36771ae08745Sheppo 	ldc_status_t	istatus = 0;
36781ae08745Sheppo 	int		rv;
36791ae08745Sheppo 
36801ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
36811ae08745Sheppo 
36821ae08745Sheppo 	LDC_ENTER_LOCK(ldcp);
36831ae08745Sheppo 
36841ae08745Sheppo 	/* don't start at 0 in case clients don't like that */
36851ae08745Sheppo 	ldcp->next_ident = 1;
36861ae08745Sheppo 
36871ae08745Sheppo 	rv = ldc_open(ldcp->ldc_handle);
36881ae08745Sheppo 	if (rv != 0) {
36891ae08745Sheppo 		DERR(vswp, "%s: ldc_open failed: id(%lld) rv(%d)",
36901ae08745Sheppo 		    __func__, ldcp->ldc_id, rv);
36911ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
36921ae08745Sheppo 		return (1);
36931ae08745Sheppo 	}
36941ae08745Sheppo 
36951ae08745Sheppo 	if (ldc_status(ldcp->ldc_handle, &istatus) != 0) {
36961ae08745Sheppo 		DERR(vswp, "%s: unable to get status", __func__);
36971ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
36981ae08745Sheppo 		return (1);
36991ae08745Sheppo 
37001ae08745Sheppo 	} else if (istatus != LDC_OPEN && istatus != LDC_READY) {
37011ae08745Sheppo 		DERR(vswp, "%s: id (%lld) status(%d) is not OPEN/READY",
37021ae08745Sheppo 		    __func__, ldcp->ldc_id, istatus);
37031ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
37041ae08745Sheppo 		return (1);
37051ae08745Sheppo 	}
37061ae08745Sheppo 
37073af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
37081ae08745Sheppo 	ldcp->ldc_status = istatus;
37093af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
37103af08d82Slm66018 
37111ae08745Sheppo 	rv = ldc_up(ldcp->ldc_handle);
37121ae08745Sheppo 	if (rv != 0) {
37131ae08745Sheppo 		/*
37141ae08745Sheppo 		 * Not a fatal error for ldc_up() to fail, as peer
37151ae08745Sheppo 		 * end point may simply not be ready yet.
37161ae08745Sheppo 		 */
37171ae08745Sheppo 		D2(vswp, "%s: ldc_up err id(%lld) rv(%d)", __func__,
37181ae08745Sheppo 		    ldcp->ldc_id, rv);
37191ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
37201ae08745Sheppo 		return (1);
37211ae08745Sheppo 	}
37221ae08745Sheppo 
37231ae08745Sheppo 	/*
37241ae08745Sheppo 	 * ldc_up() call is non-blocking so need to explicitly
37251ae08745Sheppo 	 * check channel status to see if in fact the channel
37261ae08745Sheppo 	 * is UP.
37271ae08745Sheppo 	 */
37283af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
37293af08d82Slm66018 	if (ldc_status(ldcp->ldc_handle, &ldcp->ldc_status) != 0) {
37301ae08745Sheppo 		DERR(vswp, "%s: unable to get status", __func__);
37313af08d82Slm66018 		mutex_exit(&ldcp->status_lock);
37321ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
37331ae08745Sheppo 		return (1);
37341ae08745Sheppo 
37351ae08745Sheppo 	}
3736b071742bSsg70180 
3737b071742bSsg70180 	if (ldcp->ldc_status == LDC_UP) {
3738b071742bSsg70180 		D2(vswp, "%s: channel %ld now UP (%ld)", __func__,
3739b071742bSsg70180 		    ldcp->ldc_id, istatus);
37403af08d82Slm66018 		mutex_exit(&ldcp->status_lock);
37411ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
37421ae08745Sheppo 
3743b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_UP);
3744b071742bSsg70180 		return (0);
37453af08d82Slm66018 	}
37463af08d82Slm66018 
3747b071742bSsg70180 	mutex_exit(&ldcp->status_lock);
3748b071742bSsg70180 	LDC_EXIT_LOCK(ldcp);
3749b071742bSsg70180 
37501ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
37511ae08745Sheppo 	return (0);
37521ae08745Sheppo }
37531ae08745Sheppo 
37541ae08745Sheppo /* disable callbacks on the channel */
37551ae08745Sheppo static int
37561ae08745Sheppo vsw_ldc_uninit(vsw_ldc_t *ldcp)
37571ae08745Sheppo {
37581ae08745Sheppo 	vsw_t	*vswp = ldcp->ldc_vswp;
37591ae08745Sheppo 	int	rv;
37601ae08745Sheppo 
37611ae08745Sheppo 	D1(vswp, "vsw_ldc_uninit: enter: id(%lx)\n", ldcp->ldc_id);
37621ae08745Sheppo 
37631ae08745Sheppo 	LDC_ENTER_LOCK(ldcp);
37641ae08745Sheppo 
37651ae08745Sheppo 	rv = ldc_set_cb_mode(ldcp->ldc_handle, LDC_CB_DISABLE);
37661ae08745Sheppo 	if (rv != 0) {
37671ae08745Sheppo 		DERR(vswp, "vsw_ldc_uninit(%lld): error disabling "
37681ae08745Sheppo 		    "interrupts (rv = %d)\n", ldcp->ldc_id, rv);
37691ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
37701ae08745Sheppo 		return (1);
37711ae08745Sheppo 	}
37721ae08745Sheppo 
37733af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
37741ae08745Sheppo 	ldcp->ldc_status = LDC_INIT;
37753af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
37761ae08745Sheppo 
37771ae08745Sheppo 	LDC_EXIT_LOCK(ldcp);
37781ae08745Sheppo 
37791ae08745Sheppo 	D1(vswp, "vsw_ldc_uninit: exit: id(%lx)", ldcp->ldc_id);
37801ae08745Sheppo 
37811ae08745Sheppo 	return (0);
37821ae08745Sheppo }
37831ae08745Sheppo 
37841ae08745Sheppo static int
37851ae08745Sheppo vsw_init_ldcs(vsw_port_t *port)
37861ae08745Sheppo {
37871ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
37881ae08745Sheppo 	vsw_ldc_t	*ldcp;
37891ae08745Sheppo 
37901ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
37911ae08745Sheppo 	ldcp =  ldcl->head;
37921ae08745Sheppo 	for (; ldcp  != NULL; ldcp = ldcp->ldc_next) {
37931ae08745Sheppo 		(void) vsw_ldc_init(ldcp);
37941ae08745Sheppo 	}
37951ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
37961ae08745Sheppo 
37971ae08745Sheppo 	return (0);
37981ae08745Sheppo }
37991ae08745Sheppo 
38001ae08745Sheppo static int
38011ae08745Sheppo vsw_uninit_ldcs(vsw_port_t *port)
38021ae08745Sheppo {
38031ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
38041ae08745Sheppo 	vsw_ldc_t	*ldcp;
38051ae08745Sheppo 
38061ae08745Sheppo 	D1(NULL, "vsw_uninit_ldcs: enter\n");
38071ae08745Sheppo 
38081ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
38091ae08745Sheppo 	ldcp =  ldcl->head;
38101ae08745Sheppo 	for (; ldcp  != NULL; ldcp = ldcp->ldc_next) {
38111ae08745Sheppo 		(void) vsw_ldc_uninit(ldcp);
38121ae08745Sheppo 	}
38131ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
38141ae08745Sheppo 
38151ae08745Sheppo 	D1(NULL, "vsw_uninit_ldcs: exit\n");
38161ae08745Sheppo 
38171ae08745Sheppo 	return (0);
38181ae08745Sheppo }
38191ae08745Sheppo 
38201ae08745Sheppo /*
38211ae08745Sheppo  * Wait until the callback(s) associated with the ldcs under the specified
38221ae08745Sheppo  * port have completed.
38231ae08745Sheppo  *
38241ae08745Sheppo  * Prior to this function being invoked each channel under this port
38251ae08745Sheppo  * should have been quiesced via ldc_set_cb_mode(DISABLE).
38261ae08745Sheppo  *
38271ae08745Sheppo  * A short explaination of what we are doing below..
38281ae08745Sheppo  *
38291ae08745Sheppo  * The simplest approach would be to have a reference counter in
38301ae08745Sheppo  * the ldc structure which is increment/decremented by the callbacks as
38311ae08745Sheppo  * they use the channel. The drain function could then simply disable any
38321ae08745Sheppo  * further callbacks and do a cv_wait for the ref to hit zero. Unfortunately
38331ae08745Sheppo  * there is a tiny window here - before the callback is able to get the lock
38341ae08745Sheppo  * on the channel it is interrupted and this function gets to execute. It
38351ae08745Sheppo  * sees that the ref count is zero and believes its free to delete the
38361ae08745Sheppo  * associated data structures.
38371ae08745Sheppo  *
38381ae08745Sheppo  * We get around this by taking advantage of the fact that before the ldc
38391ae08745Sheppo  * framework invokes a callback it sets a flag to indicate that there is a
38401ae08745Sheppo  * callback active (or about to become active). If when we attempt to
38411ae08745Sheppo  * unregister a callback when this active flag is set then the unregister
38421ae08745Sheppo  * will fail with EWOULDBLOCK.
38431ae08745Sheppo  *
38441ae08745Sheppo  * If the unregister fails we do a cv_timedwait. We will either be signaled
38451ae08745Sheppo  * by the callback as it is exiting (note we have to wait a short period to
38461ae08745Sheppo  * allow the callback to return fully to the ldc framework and it to clear
38471ae08745Sheppo  * the active flag), or by the timer expiring. In either case we again attempt
38481ae08745Sheppo  * the unregister. We repeat this until we can succesfully unregister the
38491ae08745Sheppo  * callback.
38501ae08745Sheppo  *
38511ae08745Sheppo  * The reason we use a cv_timedwait rather than a simple cv_wait is to catch
38521ae08745Sheppo  * the case where the callback has finished but the ldc framework has not yet
38531ae08745Sheppo  * cleared the active flag. In this case we would never get a cv_signal.
38541ae08745Sheppo  */
38551ae08745Sheppo static int
38561ae08745Sheppo vsw_drain_ldcs(vsw_port_t *port)
38571ae08745Sheppo {
38581ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
38591ae08745Sheppo 	vsw_ldc_t	*ldcp;
38601ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
38611ae08745Sheppo 
38621ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
38631ae08745Sheppo 
38641ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
38651ae08745Sheppo 
38661ae08745Sheppo 	ldcp = ldcl->head;
38671ae08745Sheppo 
38681ae08745Sheppo 	for (; ldcp  != NULL; ldcp = ldcp->ldc_next) {
38691ae08745Sheppo 		/*
38701ae08745Sheppo 		 * If we can unregister the channel callback then we
38711ae08745Sheppo 		 * know that there is no callback either running or
38721ae08745Sheppo 		 * scheduled to run for this channel so move on to next
38731ae08745Sheppo 		 * channel in the list.
38741ae08745Sheppo 		 */
38751ae08745Sheppo 		mutex_enter(&ldcp->drain_cv_lock);
38761ae08745Sheppo 
38771ae08745Sheppo 		/* prompt active callbacks to quit */
38781ae08745Sheppo 		ldcp->drain_state = VSW_LDC_DRAINING;
38791ae08745Sheppo 
38801ae08745Sheppo 		if ((ldc_unreg_callback(ldcp->ldc_handle)) == 0) {
38811ae08745Sheppo 			D2(vswp, "%s: unreg callback for chan %ld", __func__,
38821ae08745Sheppo 			    ldcp->ldc_id);
38831ae08745Sheppo 			mutex_exit(&ldcp->drain_cv_lock);
38841ae08745Sheppo 			continue;
38851ae08745Sheppo 		} else {
38861ae08745Sheppo 			/*
38871ae08745Sheppo 			 * If we end up here we know that either 1) a callback
38881ae08745Sheppo 			 * is currently executing, 2) is about to start (i.e.
38891ae08745Sheppo 			 * the ldc framework has set the active flag but
38901ae08745Sheppo 			 * has not actually invoked the callback yet, or 3)
38911ae08745Sheppo 			 * has finished and has returned to the ldc framework
38921ae08745Sheppo 			 * but the ldc framework has not yet cleared the
38931ae08745Sheppo 			 * active bit.
38941ae08745Sheppo 			 *
38951ae08745Sheppo 			 * Wait for it to finish.
38961ae08745Sheppo 			 */
38971ae08745Sheppo 			while (ldc_unreg_callback(ldcp->ldc_handle)
38981ae08745Sheppo 			    == EWOULDBLOCK)
38991ae08745Sheppo 				(void) cv_timedwait(&ldcp->drain_cv,
39001ae08745Sheppo 				    &ldcp->drain_cv_lock, lbolt + hz);
39011ae08745Sheppo 
39021ae08745Sheppo 			mutex_exit(&ldcp->drain_cv_lock);
39031ae08745Sheppo 			D2(vswp, "%s: unreg callback for chan %ld after "
39041ae08745Sheppo 			    "timeout", __func__, ldcp->ldc_id);
39051ae08745Sheppo 		}
39061ae08745Sheppo 	}
39071ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
39081ae08745Sheppo 
39091ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
39101ae08745Sheppo 	return (0);
39111ae08745Sheppo }
39121ae08745Sheppo 
39131ae08745Sheppo /*
39141ae08745Sheppo  * Wait until all tasks which reference this port have completed.
39151ae08745Sheppo  *
39161ae08745Sheppo  * Prior to this function being invoked each channel under this port
39171ae08745Sheppo  * should have been quiesced via ldc_set_cb_mode(DISABLE).
39181ae08745Sheppo  */
39191ae08745Sheppo static int
39201ae08745Sheppo vsw_drain_port_taskq(vsw_port_t *port)
39211ae08745Sheppo {
39221ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
39231ae08745Sheppo 
39241ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
39251ae08745Sheppo 
39261ae08745Sheppo 	/*
39271ae08745Sheppo 	 * Mark the port as in the process of being detached, and
39281ae08745Sheppo 	 * dispatch a marker task to the queue so we know when all
39291ae08745Sheppo 	 * relevant tasks have completed.
39301ae08745Sheppo 	 */
39311ae08745Sheppo 	mutex_enter(&port->state_lock);
39321ae08745Sheppo 	port->state = VSW_PORT_DETACHING;
39331ae08745Sheppo 
39341ae08745Sheppo 	if ((vswp->taskq_p == NULL) ||
39351ae08745Sheppo 	    (ddi_taskq_dispatch(vswp->taskq_p, vsw_marker_task,
39361ae08745Sheppo 	    port, DDI_NOSLEEP) != DDI_SUCCESS)) {
39371ae08745Sheppo 		DERR(vswp, "%s: unable to dispatch marker task",
39381ae08745Sheppo 		    __func__);
39391ae08745Sheppo 		mutex_exit(&port->state_lock);
39401ae08745Sheppo 		return (1);
39411ae08745Sheppo 	}
39421ae08745Sheppo 
39431ae08745Sheppo 	/*
39441ae08745Sheppo 	 * Wait for the marker task to finish.
39451ae08745Sheppo 	 */
39461ae08745Sheppo 	while (port->state != VSW_PORT_DETACHABLE)
39471ae08745Sheppo 		cv_wait(&port->state_cv, &port->state_lock);
39481ae08745Sheppo 
39491ae08745Sheppo 	mutex_exit(&port->state_lock);
39501ae08745Sheppo 
39511ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
39521ae08745Sheppo 
39531ae08745Sheppo 	return (0);
39541ae08745Sheppo }
39551ae08745Sheppo 
39561ae08745Sheppo static void
39571ae08745Sheppo vsw_marker_task(void *arg)
39581ae08745Sheppo {
39591ae08745Sheppo 	vsw_port_t	*port = arg;
39601ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
39611ae08745Sheppo 
39621ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
39631ae08745Sheppo 
39641ae08745Sheppo 	mutex_enter(&port->state_lock);
39651ae08745Sheppo 
39661ae08745Sheppo 	/*
39671ae08745Sheppo 	 * No further tasks should be dispatched which reference
39681ae08745Sheppo 	 * this port so ok to mark it as safe to detach.
39691ae08745Sheppo 	 */
39701ae08745Sheppo 	port->state = VSW_PORT_DETACHABLE;
39711ae08745Sheppo 
39721ae08745Sheppo 	cv_signal(&port->state_cv);
39731ae08745Sheppo 
39741ae08745Sheppo 	mutex_exit(&port->state_lock);
39751ae08745Sheppo 
39761ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
39771ae08745Sheppo }
39781ae08745Sheppo 
39791ae08745Sheppo static vsw_port_t *
39801ae08745Sheppo vsw_lookup_port(vsw_t *vswp, int p_instance)
39811ae08745Sheppo {
39821ae08745Sheppo 	vsw_port_list_t *plist = &vswp->plist;
39831ae08745Sheppo 	vsw_port_t	*port;
39841ae08745Sheppo 
39851ae08745Sheppo 	for (port = plist->head; port != NULL; port = port->p_next) {
39861ae08745Sheppo 		if (port->p_instance == p_instance) {
39871ae08745Sheppo 			D2(vswp, "vsw_lookup_port: found p_instance\n");
39881ae08745Sheppo 			return (port);
39891ae08745Sheppo 		}
39901ae08745Sheppo 	}
39911ae08745Sheppo 
39921ae08745Sheppo 	return (NULL);
39931ae08745Sheppo }
39941ae08745Sheppo 
39951ae08745Sheppo /*
39961ae08745Sheppo  * Search for and remove the specified port from the port
39971ae08745Sheppo  * list. Returns 0 if able to locate and remove port, otherwise
39981ae08745Sheppo  * returns 1.
39991ae08745Sheppo  */
40001ae08745Sheppo static int
40011ae08745Sheppo vsw_plist_del_node(vsw_t *vswp, vsw_port_t *port)
40021ae08745Sheppo {
40031ae08745Sheppo 	vsw_port_list_t *plist = &vswp->plist;
40041ae08745Sheppo 	vsw_port_t	*curr_p, *prev_p;
40051ae08745Sheppo 
40061ae08745Sheppo 	if (plist->head == NULL)
40071ae08745Sheppo 		return (1);
40081ae08745Sheppo 
40091ae08745Sheppo 	curr_p = prev_p = plist->head;
40101ae08745Sheppo 
40111ae08745Sheppo 	while (curr_p != NULL) {
40121ae08745Sheppo 		if (curr_p == port) {
40131ae08745Sheppo 			if (prev_p == curr_p) {
40141ae08745Sheppo 				plist->head = curr_p->p_next;
40151ae08745Sheppo 			} else {
40161ae08745Sheppo 				prev_p->p_next = curr_p->p_next;
40171ae08745Sheppo 			}
40181ae08745Sheppo 			plist->num_ports--;
40191ae08745Sheppo 			break;
40201ae08745Sheppo 		} else {
40211ae08745Sheppo 			prev_p = curr_p;
40221ae08745Sheppo 			curr_p = curr_p->p_next;
40231ae08745Sheppo 		}
40241ae08745Sheppo 	}
40251ae08745Sheppo 	return (0);
40261ae08745Sheppo }
40271ae08745Sheppo 
40281ae08745Sheppo /*
40291ae08745Sheppo  * Interrupt handler for ldc messages.
40301ae08745Sheppo  */
40311ae08745Sheppo static uint_t
40321ae08745Sheppo vsw_ldc_cb(uint64_t event, caddr_t arg)
40331ae08745Sheppo {
40341ae08745Sheppo 	vsw_ldc_t	*ldcp = (vsw_ldc_t  *)arg;
40351ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
40361ae08745Sheppo 
40371ae08745Sheppo 	D1(vswp, "%s: enter: ldcid (%lld)\n", __func__, ldcp->ldc_id);
40381ae08745Sheppo 
40391ae08745Sheppo 	mutex_enter(&ldcp->ldc_cblock);
40401ae08745Sheppo 
4041b071742bSsg70180 	mutex_enter(&ldcp->status_lock);
40421ae08745Sheppo 	if ((ldcp->ldc_status == LDC_INIT) || (ldcp->ldc_handle == NULL)) {
4043b071742bSsg70180 		mutex_exit(&ldcp->status_lock);
40441ae08745Sheppo 		mutex_exit(&ldcp->ldc_cblock);
40451ae08745Sheppo 		return (LDC_SUCCESS);
40461ae08745Sheppo 	}
40473af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
40483af08d82Slm66018 
40491ae08745Sheppo 	if (event & LDC_EVT_UP) {
40501ae08745Sheppo 		/*
4051b071742bSsg70180 		 * Channel has come up.
40521ae08745Sheppo 		 */
40531ae08745Sheppo 		D2(vswp, "%s: id(%ld) event(%llx) UP: status(%ld)",
4054b071742bSsg70180 		    __func__, ldcp->ldc_id, event, ldcp->ldc_status);
4055b071742bSsg70180 
4056b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_UP);
40571ae08745Sheppo 
40581ae08745Sheppo 		ASSERT((event & (LDC_EVT_RESET | LDC_EVT_DOWN)) == 0);
40591ae08745Sheppo 	}
40601ae08745Sheppo 
40611ae08745Sheppo 	if (event & LDC_EVT_READ) {
40621ae08745Sheppo 		/*
40631ae08745Sheppo 		 * Data available for reading.
40641ae08745Sheppo 		 */
40651ae08745Sheppo 		D2(vswp, "%s: id(ld) event(%llx) data READ",
40661ae08745Sheppo 		    __func__, ldcp->ldc_id, event);
40671ae08745Sheppo 
40681ae08745Sheppo 		vsw_process_pkt(ldcp);
40691ae08745Sheppo 
40701ae08745Sheppo 		ASSERT((event & (LDC_EVT_RESET | LDC_EVT_DOWN)) == 0);
40711ae08745Sheppo 
40721ae08745Sheppo 		goto vsw_cb_exit;
40731ae08745Sheppo 	}
40741ae08745Sheppo 
40753af08d82Slm66018 	if (event & (LDC_EVT_DOWN | LDC_EVT_RESET)) {
4076b071742bSsg70180 		D2(vswp, "%s: id(%ld) event (%lx) DOWN/RESET: status(%ld)",
4077b071742bSsg70180 		    __func__, ldcp->ldc_id, event, ldcp->ldc_status);
40783af08d82Slm66018 
4079b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
40801ae08745Sheppo 	}
40811ae08745Sheppo 
40821ae08745Sheppo 	/*
40831ae08745Sheppo 	 * Catch either LDC_EVT_WRITE which we don't support or any
40841ae08745Sheppo 	 * unknown event.
40851ae08745Sheppo 	 */
4086205eeb1aSlm66018 	if (event &
4087205eeb1aSlm66018 	    ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ)) {
40881ae08745Sheppo 		DERR(vswp, "%s: id(%ld) Unexpected event=(%llx) status(%ld)",
40891ae08745Sheppo 		    __func__, ldcp->ldc_id, event, ldcp->ldc_status);
40901ae08745Sheppo 	}
40911ae08745Sheppo 
40921ae08745Sheppo vsw_cb_exit:
40931ae08745Sheppo 	mutex_exit(&ldcp->ldc_cblock);
40941ae08745Sheppo 
40951ae08745Sheppo 	/*
40961ae08745Sheppo 	 * Let the drain function know we are finishing if it
40971ae08745Sheppo 	 * is waiting.
40981ae08745Sheppo 	 */
40991ae08745Sheppo 	mutex_enter(&ldcp->drain_cv_lock);
41001ae08745Sheppo 	if (ldcp->drain_state == VSW_LDC_DRAINING)
41011ae08745Sheppo 		cv_signal(&ldcp->drain_cv);
41021ae08745Sheppo 	mutex_exit(&ldcp->drain_cv_lock);
41031ae08745Sheppo 
41041ae08745Sheppo 	return (LDC_SUCCESS);
41051ae08745Sheppo }
41061ae08745Sheppo 
41071ae08745Sheppo /*
4108b071742bSsg70180  * Reinitialise data structures associated with the channel.
41091ae08745Sheppo  */
41101ae08745Sheppo static void
4111b071742bSsg70180 vsw_ldc_reinit(vsw_ldc_t *ldcp)
41121ae08745Sheppo {
41131ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
41141ae08745Sheppo 	vsw_port_t	*port;
41151ae08745Sheppo 	vsw_ldc_list_t	*ldcl;
41161ae08745Sheppo 
41173af08d82Slm66018 	D1(vswp, "%s: enter", __func__);
41181ae08745Sheppo 
41191ae08745Sheppo 	port = ldcp->ldc_port;
41201ae08745Sheppo 	ldcl = &port->p_ldclist;
41211ae08745Sheppo 
41223af08d82Slm66018 	READ_ENTER(&ldcl->lockrw);
41231ae08745Sheppo 
41241ae08745Sheppo 	D2(vswp, "%s: in 0x%llx : out 0x%llx", __func__,
41251ae08745Sheppo 	    ldcp->lane_in.lstate, ldcp->lane_out.lstate);
41261ae08745Sheppo 
41271ae08745Sheppo 	vsw_free_lane_resources(ldcp, INBOUND);
41281ae08745Sheppo 	vsw_free_lane_resources(ldcp, OUTBOUND);
41291ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
41301ae08745Sheppo 
41311ae08745Sheppo 	ldcp->lane_in.lstate = 0;
41321ae08745Sheppo 	ldcp->lane_out.lstate = 0;
41331ae08745Sheppo 
41341ae08745Sheppo 	/*
41351ae08745Sheppo 	 * Remove parent port from any multicast groups
41361ae08745Sheppo 	 * it may have registered with. Client must resend
41371ae08745Sheppo 	 * multicast add command after handshake completes.
41381ae08745Sheppo 	 */
41391ae08745Sheppo 	(void) vsw_del_fdb(vswp, port);
41401ae08745Sheppo 
41411ae08745Sheppo 	vsw_del_mcst_port(port);
41421ae08745Sheppo 
41431ae08745Sheppo 	ldcp->peer_session = 0;
41441ae08745Sheppo 	ldcp->session_status = 0;
41453af08d82Slm66018 	ldcp->hcnt = 0;
41463af08d82Slm66018 	ldcp->hphase = VSW_MILESTONE0;
41473af08d82Slm66018 
41483af08d82Slm66018 	D1(vswp, "%s: exit", __func__);
41493af08d82Slm66018 }
41503af08d82Slm66018 
41513af08d82Slm66018 /*
4152b071742bSsg70180  * Process a connection event.
4153b071742bSsg70180  *
4154b071742bSsg70180  * Note - care must be taken to ensure that this function is
4155b071742bSsg70180  * not called with the dlistrw lock held.
41563af08d82Slm66018  */
41573af08d82Slm66018 static void
4158b071742bSsg70180 vsw_process_conn_evt(vsw_ldc_t *ldcp, uint16_t evt)
41593af08d82Slm66018 {
41603af08d82Slm66018 	vsw_t		*vswp = ldcp->ldc_vswp;
4161b071742bSsg70180 	vsw_conn_evt_t	*conn = NULL;
41623af08d82Slm66018 
4163b071742bSsg70180 	D1(vswp, "%s: enter", __func__);
41641ae08745Sheppo 
41651ae08745Sheppo 	/*
4166b071742bSsg70180 	 * Check if either a reset or restart event is pending
4167b071742bSsg70180 	 * or in progress. If so just return.
4168b071742bSsg70180 	 *
4169b071742bSsg70180 	 * A VSW_CONN_RESET event originates either with a LDC_RESET_EVT
4170b071742bSsg70180 	 * being received by the callback handler, or a ECONNRESET error
4171b071742bSsg70180 	 * code being returned from a ldc_read() or ldc_write() call.
4172b071742bSsg70180 	 *
4173b071742bSsg70180 	 * A VSW_CONN_RESTART event occurs when some error checking code
4174b071742bSsg70180 	 * decides that there is a problem with data from the channel,
4175b071742bSsg70180 	 * and that the handshake should be restarted.
4176b071742bSsg70180 	 */
4177b071742bSsg70180 	if (((evt == VSW_CONN_RESET) || (evt == VSW_CONN_RESTART)) &&
4178b071742bSsg70180 	    (ldstub((uint8_t *)&ldcp->reset_active)))
4179b071742bSsg70180 		return;
4180b071742bSsg70180 
4181b071742bSsg70180 	/*
4182b071742bSsg70180 	 * If it is an LDC_UP event we first check the recorded
4183b071742bSsg70180 	 * state of the channel. If this is UP then we know that
4184b071742bSsg70180 	 * the channel moving to the UP state has already been dealt
4185b071742bSsg70180 	 * with and don't need to dispatch a  new task.
4186b071742bSsg70180 	 *
4187b071742bSsg70180 	 * The reason for this check is that when we do a ldc_up(),
4188b071742bSsg70180 	 * depending on the state of the peer, we may or may not get
4189b071742bSsg70180 	 * a LDC_UP event. As we can't depend on getting a LDC_UP evt
4190b071742bSsg70180 	 * every time we do ldc_up() we explicitly check the channel
4191b071742bSsg70180 	 * status to see has it come up (ldc_up() is asynch and will
4192b071742bSsg70180 	 * complete at some undefined time), and take the appropriate
4193b071742bSsg70180 	 * action.
4194b071742bSsg70180 	 *
4195b071742bSsg70180 	 * The flip side of this is that we may get a LDC_UP event
4196b071742bSsg70180 	 * when we have already seen that the channel is up and have
4197b071742bSsg70180 	 * dealt with that.
4198b071742bSsg70180 	 */
4199b071742bSsg70180 	mutex_enter(&ldcp->status_lock);
4200b071742bSsg70180 	if (evt == VSW_CONN_UP) {
4201205eeb1aSlm66018 		if ((ldcp->ldc_status == LDC_UP) || (ldcp->reset_active != 0)) {
4202b071742bSsg70180 			mutex_exit(&ldcp->status_lock);
4203b071742bSsg70180 			return;
4204b071742bSsg70180 		}
4205b071742bSsg70180 	}
4206b071742bSsg70180 	mutex_exit(&ldcp->status_lock);
4207b071742bSsg70180 
4208b071742bSsg70180 	/*
4209b071742bSsg70180 	 * The transaction group id allows us to identify and discard
4210b071742bSsg70180 	 * any tasks which are still pending on the taskq and refer
4211b071742bSsg70180 	 * to the handshake session we are about to restart or reset.
4212b071742bSsg70180 	 * These stale messages no longer have any real meaning.
42131ae08745Sheppo 	 */
42141ae08745Sheppo 	mutex_enter(&ldcp->hss_lock);
42151ae08745Sheppo 	ldcp->hss_id++;
42161ae08745Sheppo 	mutex_exit(&ldcp->hss_lock);
42171ae08745Sheppo 
4218b071742bSsg70180 	ASSERT(vswp->taskq_p != NULL);
4219b071742bSsg70180 
4220b071742bSsg70180 	if ((conn = kmem_zalloc(sizeof (vsw_conn_evt_t), KM_NOSLEEP)) == NULL) {
4221b071742bSsg70180 		cmn_err(CE_WARN, "!vsw%d: unable to allocate memory for"
4222b071742bSsg70180 		    " connection event", vswp->instance);
4223b071742bSsg70180 		goto err_exit;
4224b071742bSsg70180 	}
4225b071742bSsg70180 
4226b071742bSsg70180 	conn->evt = evt;
4227b071742bSsg70180 	conn->ldcp = ldcp;
4228b071742bSsg70180 
4229b071742bSsg70180 	if (ddi_taskq_dispatch(vswp->taskq_p, vsw_conn_task, conn,
4230b071742bSsg70180 	    DDI_NOSLEEP) != DDI_SUCCESS) {
4231b071742bSsg70180 		cmn_err(CE_WARN, "!vsw%d: Can't dispatch connection task",
4232b071742bSsg70180 		    vswp->instance);
4233b071742bSsg70180 
4234b071742bSsg70180 		kmem_free(conn, sizeof (vsw_conn_evt_t));
4235b071742bSsg70180 		goto err_exit;
4236b071742bSsg70180 	}
4237b071742bSsg70180 
4238b071742bSsg70180 	D1(vswp, "%s: exit", __func__);
4239b071742bSsg70180 	return;
4240b071742bSsg70180 
4241b071742bSsg70180 err_exit:
4242b071742bSsg70180 	/*
4243b071742bSsg70180 	 * Have mostly likely failed due to memory shortage. Clear the flag so
4244b071742bSsg70180 	 * that future requests will at least be attempted and will hopefully
4245b071742bSsg70180 	 * succeed.
4246b071742bSsg70180 	 */
4247b071742bSsg70180 	if ((evt == VSW_CONN_RESET) || (evt == VSW_CONN_RESTART))
4248b071742bSsg70180 		ldcp->reset_active = 0;
4249b071742bSsg70180 }
4250b071742bSsg70180 
4251b071742bSsg70180 /*
4252b071742bSsg70180  * Deal with events relating to a connection. Invoked from a taskq.
4253b071742bSsg70180  */
4254b071742bSsg70180 static void
4255b071742bSsg70180 vsw_conn_task(void *arg)
4256b071742bSsg70180 {
4257b071742bSsg70180 	vsw_conn_evt_t	*conn = (vsw_conn_evt_t *)arg;
4258b071742bSsg70180 	vsw_ldc_t	*ldcp = NULL;
4259b071742bSsg70180 	vsw_t		*vswp = NULL;
4260b071742bSsg70180 	uint16_t	evt;
4261b071742bSsg70180 	ldc_status_t	curr_status;
4262b071742bSsg70180 
4263b071742bSsg70180 	ldcp = conn->ldcp;
4264b071742bSsg70180 	evt = conn->evt;
4265b071742bSsg70180 	vswp = ldcp->ldc_vswp;
4266b071742bSsg70180 
4267b071742bSsg70180 	D1(vswp, "%s: enter", __func__);
4268b071742bSsg70180 
4269b071742bSsg70180 	/* can safely free now have copied out data */
4270b071742bSsg70180 	kmem_free(conn, sizeof (vsw_conn_evt_t));
4271b071742bSsg70180 
4272b071742bSsg70180 	mutex_enter(&ldcp->status_lock);
4273b071742bSsg70180 	if (ldc_status(ldcp->ldc_handle, &curr_status) != 0) {
4274b071742bSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read status of "
4275b071742bSsg70180 		    "channel %ld", vswp->instance, ldcp->ldc_id);
4276b071742bSsg70180 		mutex_exit(&ldcp->status_lock);
4277b071742bSsg70180 		return;
4278b071742bSsg70180 	}
4279b071742bSsg70180 
4280b071742bSsg70180 	/*
4281b071742bSsg70180 	 * If we wish to restart the handshake on this channel, then if
4282b071742bSsg70180 	 * the channel is UP we bring it DOWN to flush the underlying
4283b071742bSsg70180 	 * ldc queue.
4284b071742bSsg70180 	 */
4285b071742bSsg70180 	if ((evt == VSW_CONN_RESTART) && (curr_status == LDC_UP))
4286b071742bSsg70180 		(void) ldc_down(ldcp->ldc_handle);
4287b071742bSsg70180 
4288b071742bSsg70180 	/*
4289b071742bSsg70180 	 * re-init all the associated data structures.
4290b071742bSsg70180 	 */
4291b071742bSsg70180 	vsw_ldc_reinit(ldcp);
4292b071742bSsg70180 
4293b071742bSsg70180 	/*
4294b071742bSsg70180 	 * Bring the channel back up (note it does no harm to
4295b071742bSsg70180 	 * do this even if the channel is already UP, Just
4296b071742bSsg70180 	 * becomes effectively a no-op).
4297b071742bSsg70180 	 */
4298b071742bSsg70180 	(void) ldc_up(ldcp->ldc_handle);
4299b071742bSsg70180 
4300b071742bSsg70180 	/*
4301b071742bSsg70180 	 * Check if channel is now UP. This will only happen if
4302b071742bSsg70180 	 * peer has also done a ldc_up().
4303b071742bSsg70180 	 */
4304b071742bSsg70180 	if (ldc_status(ldcp->ldc_handle, &curr_status) != 0) {
4305b071742bSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read status of "
4306b071742bSsg70180 		    "channel %ld", vswp->instance, ldcp->ldc_id);
4307b071742bSsg70180 		mutex_exit(&ldcp->status_lock);
4308b071742bSsg70180 		return;
4309b071742bSsg70180 	}
4310b071742bSsg70180 
4311b071742bSsg70180 	ldcp->ldc_status = curr_status;
4312b071742bSsg70180 
4313b071742bSsg70180 	/* channel UP so restart handshake by sending version info */
4314b071742bSsg70180 	if (curr_status == LDC_UP) {
43151ae08745Sheppo 		if (ldcp->hcnt++ > vsw_num_handshakes) {
431634683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: exceeded number of permitted"
43171ae08745Sheppo 			    " handshake attempts (%d) on channel %ld",
431834683adeSsg70180 			    vswp->instance, ldcp->hcnt, ldcp->ldc_id);
43193af08d82Slm66018 			mutex_exit(&ldcp->status_lock);
43203af08d82Slm66018 			return;
43213af08d82Slm66018 		}
4322b071742bSsg70180 
4323b071742bSsg70180 		if (ddi_taskq_dispatch(vswp->taskq_p, vsw_send_ver, ldcp,
4324b071742bSsg70180 		    DDI_NOSLEEP) != DDI_SUCCESS) {
4325b071742bSsg70180 			cmn_err(CE_WARN, "!vsw%d: Can't dispatch version task",
4326b071742bSsg70180 			    vswp->instance);
43273af08d82Slm66018 
43283af08d82Slm66018 			/*
4329b071742bSsg70180 			 * Don't count as valid restart attempt if couldn't
4330b071742bSsg70180 			 * send version msg.
43313af08d82Slm66018 			 */
4332b071742bSsg70180 			if (ldcp->hcnt > 0)
4333b071742bSsg70180 				ldcp->hcnt--;
4334b071742bSsg70180 		}
43353af08d82Slm66018 	}
43363af08d82Slm66018 
43373af08d82Slm66018 	/*
4338b071742bSsg70180 	 * Mark that the process is complete by clearing the flag.
4339b071742bSsg70180 	 *
4340b071742bSsg70180 	 * Note is it possible that the taskq dispatch above may have failed,
4341b071742bSsg70180 	 * most likely due to memory shortage. We still clear the flag so
4342b071742bSsg70180 	 * future attempts will at least be attempted and will hopefully
4343b071742bSsg70180 	 * succeed.
43443af08d82Slm66018 	 */
4345b071742bSsg70180 	if ((evt == VSW_CONN_RESET) || (evt == VSW_CONN_RESTART))
4346445b4c2eSsb155480 		ldcp->reset_active = 0;
4347b071742bSsg70180 
4348b071742bSsg70180 	mutex_exit(&ldcp->status_lock);
43493af08d82Slm66018 
43503af08d82Slm66018 	D1(vswp, "%s: exit", __func__);
43513af08d82Slm66018 }
43523af08d82Slm66018 
43533af08d82Slm66018 /*
43541ae08745Sheppo  * returns 0 if legal for event signified by flag to have
43551ae08745Sheppo  * occured at the time it did. Otherwise returns 1.
43561ae08745Sheppo  */
43571ae08745Sheppo int
43581ae08745Sheppo vsw_check_flag(vsw_ldc_t *ldcp, int dir, uint64_t flag)
43591ae08745Sheppo {
43601ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
43611ae08745Sheppo 	uint64_t	state;
43621ae08745Sheppo 	uint64_t	phase;
43631ae08745Sheppo 
43641ae08745Sheppo 	if (dir == INBOUND)
43651ae08745Sheppo 		state = ldcp->lane_in.lstate;
43661ae08745Sheppo 	else
43671ae08745Sheppo 		state = ldcp->lane_out.lstate;
43681ae08745Sheppo 
43691ae08745Sheppo 	phase = ldcp->hphase;
43701ae08745Sheppo 
43711ae08745Sheppo 	switch (flag) {
43721ae08745Sheppo 	case VSW_VER_INFO_RECV:
43731ae08745Sheppo 		if (phase > VSW_MILESTONE0) {
43741ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): VER_INFO_RECV"
43751ae08745Sheppo 			    " when in state %d\n", ldcp->ldc_id, phase);
4376b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
43771ae08745Sheppo 			return (1);
43781ae08745Sheppo 		}
43791ae08745Sheppo 		break;
43801ae08745Sheppo 
43811ae08745Sheppo 	case VSW_VER_ACK_RECV:
43821ae08745Sheppo 	case VSW_VER_NACK_RECV:
43831ae08745Sheppo 		if (!(state & VSW_VER_INFO_SENT)) {
4384205eeb1aSlm66018 			DERR(vswp, "vsw_check_flag (%d): spurious VER_ACK or "
4385205eeb1aSlm66018 			    "VER_NACK when in state %d\n", ldcp->ldc_id, phase);
4386b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
43871ae08745Sheppo 			return (1);
43881ae08745Sheppo 		} else
43891ae08745Sheppo 			state &= ~VSW_VER_INFO_SENT;
43901ae08745Sheppo 		break;
43911ae08745Sheppo 
43921ae08745Sheppo 	case VSW_ATTR_INFO_RECV:
43931ae08745Sheppo 		if ((phase < VSW_MILESTONE1) || (phase >= VSW_MILESTONE2)) {
43941ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): ATTR_INFO_RECV"
43951ae08745Sheppo 			    " when in state %d\n", ldcp->ldc_id, phase);
4396b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
43971ae08745Sheppo 			return (1);
43981ae08745Sheppo 		}
43991ae08745Sheppo 		break;
44001ae08745Sheppo 
44011ae08745Sheppo 	case VSW_ATTR_ACK_RECV:
44021ae08745Sheppo 	case VSW_ATTR_NACK_RECV:
44031ae08745Sheppo 		if (!(state & VSW_ATTR_INFO_SENT)) {
44041ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious ATTR_ACK"
44051ae08745Sheppo 			    " or ATTR_NACK when in state %d\n",
44061ae08745Sheppo 			    ldcp->ldc_id, phase);
4407b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
44081ae08745Sheppo 			return (1);
44091ae08745Sheppo 		} else
44101ae08745Sheppo 			state &= ~VSW_ATTR_INFO_SENT;
44111ae08745Sheppo 		break;
44121ae08745Sheppo 
44131ae08745Sheppo 	case VSW_DRING_INFO_RECV:
44141ae08745Sheppo 		if (phase < VSW_MILESTONE1) {
44151ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): DRING_INFO_RECV"
44161ae08745Sheppo 			    " when in state %d\n", ldcp->ldc_id, phase);
4417b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
44181ae08745Sheppo 			return (1);
44191ae08745Sheppo 		}
44201ae08745Sheppo 		break;
44211ae08745Sheppo 
44221ae08745Sheppo 	case VSW_DRING_ACK_RECV:
44231ae08745Sheppo 	case VSW_DRING_NACK_RECV:
44241ae08745Sheppo 		if (!(state & VSW_DRING_INFO_SENT)) {
44251ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious DRING_ACK "
44261ae08745Sheppo 			    " or DRING_NACK when in state %d\n",
44271ae08745Sheppo 			    ldcp->ldc_id, phase);
4428b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
44291ae08745Sheppo 			return (1);
44301ae08745Sheppo 		} else
44311ae08745Sheppo 			state &= ~VSW_DRING_INFO_SENT;
44321ae08745Sheppo 		break;
44331ae08745Sheppo 
44341ae08745Sheppo 	case VSW_RDX_INFO_RECV:
44351ae08745Sheppo 		if (phase < VSW_MILESTONE3) {
44361ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): RDX_INFO_RECV"
44371ae08745Sheppo 			    " when in state %d\n", ldcp->ldc_id, phase);
4438b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
44391ae08745Sheppo 			return (1);
44401ae08745Sheppo 		}
44411ae08745Sheppo 		break;
44421ae08745Sheppo 
44431ae08745Sheppo 	case VSW_RDX_ACK_RECV:
44441ae08745Sheppo 	case VSW_RDX_NACK_RECV:
44451ae08745Sheppo 		if (!(state & VSW_RDX_INFO_SENT)) {
4446205eeb1aSlm66018 			DERR(vswp, "vsw_check_flag (%d): spurious RDX_ACK or "
4447205eeb1aSlm66018 			    "RDX_NACK when in state %d\n", ldcp->ldc_id, phase);
4448b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
44491ae08745Sheppo 			return (1);
44501ae08745Sheppo 		} else
44511ae08745Sheppo 			state &= ~VSW_RDX_INFO_SENT;
44521ae08745Sheppo 		break;
44531ae08745Sheppo 
44541ae08745Sheppo 	case VSW_MCST_INFO_RECV:
44551ae08745Sheppo 		if (phase < VSW_MILESTONE3) {
44561ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): VSW_MCST_INFO_RECV"
44571ae08745Sheppo 			    " when in state %d\n", ldcp->ldc_id, phase);
4458b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
44591ae08745Sheppo 			return (1);
44601ae08745Sheppo 		}
44611ae08745Sheppo 		break;
44621ae08745Sheppo 
44631ae08745Sheppo 	default:
44641ae08745Sheppo 		DERR(vswp, "vsw_check_flag (%lld): unknown flag (%llx)",
44651ae08745Sheppo 		    ldcp->ldc_id, flag);
44661ae08745Sheppo 		return (1);
44671ae08745Sheppo 	}
44681ae08745Sheppo 
44691ae08745Sheppo 	if (dir == INBOUND)
44701ae08745Sheppo 		ldcp->lane_in.lstate = state;
44711ae08745Sheppo 	else
44721ae08745Sheppo 		ldcp->lane_out.lstate = state;
44731ae08745Sheppo 
44741ae08745Sheppo 	D1(vswp, "vsw_check_flag (chan %lld): exit", ldcp->ldc_id);
44751ae08745Sheppo 
44761ae08745Sheppo 	return (0);
44771ae08745Sheppo }
44781ae08745Sheppo 
44791ae08745Sheppo void
44801ae08745Sheppo vsw_next_milestone(vsw_ldc_t *ldcp)
44811ae08745Sheppo {
44821ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
44831ae08745Sheppo 
44841ae08745Sheppo 	D1(vswp, "%s (chan %lld): enter (phase %ld)", __func__,
44851ae08745Sheppo 	    ldcp->ldc_id, ldcp->hphase);
44861ae08745Sheppo 
44871ae08745Sheppo 	DUMP_FLAGS(ldcp->lane_in.lstate);
44881ae08745Sheppo 	DUMP_FLAGS(ldcp->lane_out.lstate);
44891ae08745Sheppo 
44901ae08745Sheppo 	switch (ldcp->hphase) {
44911ae08745Sheppo 
44921ae08745Sheppo 	case VSW_MILESTONE0:
44931ae08745Sheppo 		/*
44941ae08745Sheppo 		 * If we haven't started to handshake with our peer,
44951ae08745Sheppo 		 * start to do so now.
44961ae08745Sheppo 		 */
44971ae08745Sheppo 		if (ldcp->lane_out.lstate == 0) {
44981ae08745Sheppo 			D2(vswp, "%s: (chan %lld) starting handshake "
44991ae08745Sheppo 			    "with peer", __func__, ldcp->ldc_id);
4500b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_UP);
45011ae08745Sheppo 		}
45021ae08745Sheppo 
45031ae08745Sheppo 		/*
45041ae08745Sheppo 		 * Only way to pass this milestone is to have successfully
45051ae08745Sheppo 		 * negotiated version info.
45061ae08745Sheppo 		 */
45071ae08745Sheppo 		if ((ldcp->lane_in.lstate & VSW_VER_ACK_SENT) &&
45081ae08745Sheppo 		    (ldcp->lane_out.lstate & VSW_VER_ACK_RECV)) {
45091ae08745Sheppo 
45101ae08745Sheppo 			D2(vswp, "%s: (chan %lld) leaving milestone 0",
45111ae08745Sheppo 			    __func__, ldcp->ldc_id);
45121ae08745Sheppo 
45131ae08745Sheppo 			/*
45141ae08745Sheppo 			 * Next milestone is passed when attribute
45151ae08745Sheppo 			 * information has been successfully exchanged.
45161ae08745Sheppo 			 */
45171ae08745Sheppo 			ldcp->hphase = VSW_MILESTONE1;
45181ae08745Sheppo 			vsw_send_attr(ldcp);
45191ae08745Sheppo 
45201ae08745Sheppo 		}
45211ae08745Sheppo 		break;
45221ae08745Sheppo 
45231ae08745Sheppo 	case VSW_MILESTONE1:
45241ae08745Sheppo 		/*
45251ae08745Sheppo 		 * Only way to pass this milestone is to have successfully
45261ae08745Sheppo 		 * negotiated attribute information.
45271ae08745Sheppo 		 */
45281ae08745Sheppo 		if (ldcp->lane_in.lstate & VSW_ATTR_ACK_SENT) {
45291ae08745Sheppo 
45301ae08745Sheppo 			ldcp->hphase = VSW_MILESTONE2;
45311ae08745Sheppo 
45321ae08745Sheppo 			/*
45331ae08745Sheppo 			 * If the peer device has said it wishes to
45341ae08745Sheppo 			 * use descriptor rings then we send it our ring
45351ae08745Sheppo 			 * info, otherwise we just set up a private ring
45361ae08745Sheppo 			 * which we use an internal buffer
45371ae08745Sheppo 			 */
45381ae08745Sheppo 			if (ldcp->lane_in.xfer_mode == VIO_DRING_MODE)
45391ae08745Sheppo 				vsw_send_dring_info(ldcp);
45401ae08745Sheppo 		}
45411ae08745Sheppo 		break;
45421ae08745Sheppo 
45431ae08745Sheppo 	case VSW_MILESTONE2:
45441ae08745Sheppo 		/*
45451ae08745Sheppo 		 * If peer has indicated in its attribute message that
45461ae08745Sheppo 		 * it wishes to use descriptor rings then the only way
45471ae08745Sheppo 		 * to pass this milestone is for us to have received
45481ae08745Sheppo 		 * valid dring info.
45491ae08745Sheppo 		 *
45501ae08745Sheppo 		 * If peer is not using descriptor rings then just fall
45511ae08745Sheppo 		 * through.
45521ae08745Sheppo 		 */
45531ae08745Sheppo 		if ((ldcp->lane_in.xfer_mode == VIO_DRING_MODE) &&
45541ae08745Sheppo 		    (!(ldcp->lane_in.lstate & VSW_DRING_ACK_SENT)))
45551ae08745Sheppo 			break;
45561ae08745Sheppo 
45571ae08745Sheppo 		D2(vswp, "%s: (chan %lld) leaving milestone 2",
45581ae08745Sheppo 		    __func__, ldcp->ldc_id);
45591ae08745Sheppo 
45601ae08745Sheppo 		ldcp->hphase = VSW_MILESTONE3;
45611ae08745Sheppo 		vsw_send_rdx(ldcp);
45621ae08745Sheppo 		break;
45631ae08745Sheppo 
45641ae08745Sheppo 	case VSW_MILESTONE3:
45651ae08745Sheppo 		/*
45661ae08745Sheppo 		 * Pass this milestone when all paramaters have been
45671ae08745Sheppo 		 * successfully exchanged and RDX sent in both directions.
45681ae08745Sheppo 		 *
45691ae08745Sheppo 		 * Mark outbound lane as available to transmit data.
45701ae08745Sheppo 		 */
4571b071742bSsg70180 		if ((ldcp->lane_out.lstate & VSW_RDX_ACK_SENT) &&
4572b071742bSsg70180 		    (ldcp->lane_in.lstate & VSW_RDX_ACK_RECV)) {
45731ae08745Sheppo 
45741ae08745Sheppo 			D2(vswp, "%s: (chan %lld) leaving milestone 3",
45751ae08745Sheppo 			    __func__, ldcp->ldc_id);
45763af08d82Slm66018 			D2(vswp, "%s: ** handshake complete (0x%llx : "
45773af08d82Slm66018 			    "0x%llx) **", __func__, ldcp->lane_in.lstate,
45783af08d82Slm66018 			    ldcp->lane_out.lstate);
45791ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_LANE_ACTIVE;
45801ae08745Sheppo 			ldcp->hphase = VSW_MILESTONE4;
45811ae08745Sheppo 			ldcp->hcnt = 0;
45821ae08745Sheppo 			DISPLAY_STATE();
45833af08d82Slm66018 		} else {
4584205eeb1aSlm66018 			D2(vswp, "%s: still in milestone 3 (0x%llx : 0x%llx)",
4585205eeb1aSlm66018 			    __func__, ldcp->lane_in.lstate,
45863af08d82Slm66018 			    ldcp->lane_out.lstate);
45871ae08745Sheppo 		}
45881ae08745Sheppo 		break;
45891ae08745Sheppo 
45901ae08745Sheppo 	case VSW_MILESTONE4:
45911ae08745Sheppo 		D2(vswp, "%s: (chan %lld) in milestone 4", __func__,
45921ae08745Sheppo 		    ldcp->ldc_id);
45931ae08745Sheppo 		break;
45941ae08745Sheppo 
45951ae08745Sheppo 	default:
45961ae08745Sheppo 		DERR(vswp, "%s: (chan %lld) Unknown Phase %x", __func__,
45971ae08745Sheppo 		    ldcp->ldc_id, ldcp->hphase);
45981ae08745Sheppo 	}
45991ae08745Sheppo 
46001ae08745Sheppo 	D1(vswp, "%s (chan %lld): exit (phase %ld)", __func__, ldcp->ldc_id,
46011ae08745Sheppo 	    ldcp->hphase);
46021ae08745Sheppo }
46031ae08745Sheppo 
46041ae08745Sheppo /*
46051ae08745Sheppo  * Check if major version is supported.
46061ae08745Sheppo  *
46071ae08745Sheppo  * Returns 0 if finds supported major number, and if necessary
46081ae08745Sheppo  * adjusts the minor field.
46091ae08745Sheppo  *
46101ae08745Sheppo  * Returns 1 if can't match major number exactly. Sets mjor/minor
46111ae08745Sheppo  * to next lowest support values, or to zero if no other values possible.
46121ae08745Sheppo  */
46131ae08745Sheppo static int
46141ae08745Sheppo vsw_supported_version(vio_ver_msg_t *vp)
46151ae08745Sheppo {
46161ae08745Sheppo 	int	i;
46171ae08745Sheppo 
46181ae08745Sheppo 	D1(NULL, "vsw_supported_version: enter");
46191ae08745Sheppo 
46201ae08745Sheppo 	for (i = 0; i < VSW_NUM_VER; i++) {
46211ae08745Sheppo 		if (vsw_versions[i].ver_major == vp->ver_major) {
46221ae08745Sheppo 			/*
46231ae08745Sheppo 			 * Matching or lower major version found. Update
46241ae08745Sheppo 			 * minor number if necessary.
46251ae08745Sheppo 			 */
46261ae08745Sheppo 			if (vp->ver_minor > vsw_versions[i].ver_minor) {
4627205eeb1aSlm66018 				D2(NULL, "%s: adjusting minor value from %d "
4628205eeb1aSlm66018 				    "to %d", __func__, vp->ver_minor,
46291ae08745Sheppo 				    vsw_versions[i].ver_minor);
46301ae08745Sheppo 				vp->ver_minor = vsw_versions[i].ver_minor;
46311ae08745Sheppo 			}
46321ae08745Sheppo 
46331ae08745Sheppo 			return (0);
46341ae08745Sheppo 		}
46351ae08745Sheppo 
46361ae08745Sheppo 		if (vsw_versions[i].ver_major < vp->ver_major) {
46371ae08745Sheppo 			if (vp->ver_minor > vsw_versions[i].ver_minor) {
4638205eeb1aSlm66018 				D2(NULL, "%s: adjusting minor value from %d "
4639205eeb1aSlm66018 				    "to %d", __func__, vp->ver_minor,
46401ae08745Sheppo 				    vsw_versions[i].ver_minor);
46411ae08745Sheppo 				vp->ver_minor = vsw_versions[i].ver_minor;
46421ae08745Sheppo 			}
46431ae08745Sheppo 			return (1);
46441ae08745Sheppo 		}
46451ae08745Sheppo 	}
46461ae08745Sheppo 
46471ae08745Sheppo 	/* No match was possible, zero out fields */
46481ae08745Sheppo 	vp->ver_major = 0;
46491ae08745Sheppo 	vp->ver_minor = 0;
46501ae08745Sheppo 
46511ae08745Sheppo 	D1(NULL, "vsw_supported_version: exit");
46521ae08745Sheppo 
46531ae08745Sheppo 	return (1);
46541ae08745Sheppo }
46551ae08745Sheppo 
46561ae08745Sheppo /*
46571ae08745Sheppo  * Main routine for processing messages received over LDC.
46581ae08745Sheppo  */
46591ae08745Sheppo static void
46601ae08745Sheppo vsw_process_pkt(void *arg)
46611ae08745Sheppo {
46621ae08745Sheppo 	vsw_ldc_t	*ldcp = (vsw_ldc_t  *)arg;
46631ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
46641ae08745Sheppo 	size_t		msglen;
46651ae08745Sheppo 	vio_msg_tag_t	tag;
46661ae08745Sheppo 	def_msg_t	dmsg;
46671ae08745Sheppo 	int 		rv = 0;
46681ae08745Sheppo 
46693af08d82Slm66018 
46701ae08745Sheppo 	D1(vswp, "%s enter: ldcid (%lld)\n", __func__, ldcp->ldc_id);
46711ae08745Sheppo 
46721ae08745Sheppo 	/*
46731ae08745Sheppo 	 * If channel is up read messages until channel is empty.
46741ae08745Sheppo 	 */
46751ae08745Sheppo 	do {
46761ae08745Sheppo 		msglen = sizeof (dmsg);
46771ae08745Sheppo 		rv = ldc_read(ldcp->ldc_handle, (caddr_t)&dmsg, &msglen);
46781ae08745Sheppo 
46791ae08745Sheppo 		if (rv != 0) {
4680205eeb1aSlm66018 			DERR(vswp, "%s :ldc_read err id(%lld) rv(%d) len(%d)\n",
4681205eeb1aSlm66018 			    __func__, ldcp->ldc_id, rv, msglen);
46823af08d82Slm66018 		}
46833af08d82Slm66018 
46843af08d82Slm66018 		/* channel has been reset */
46853af08d82Slm66018 		if (rv == ECONNRESET) {
4686b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
46871ae08745Sheppo 			break;
46881ae08745Sheppo 		}
46891ae08745Sheppo 
46901ae08745Sheppo 		if (msglen == 0) {
46911ae08745Sheppo 			D2(vswp, "%s: ldc_read id(%lld) NODATA", __func__,
46921ae08745Sheppo 			    ldcp->ldc_id);
46931ae08745Sheppo 			break;
46941ae08745Sheppo 		}
46951ae08745Sheppo 
46961ae08745Sheppo 		D2(vswp, "%s: ldc_read id(%lld): msglen(%d)", __func__,
46971ae08745Sheppo 		    ldcp->ldc_id, msglen);
46981ae08745Sheppo 
46991ae08745Sheppo 		/*
47001ae08745Sheppo 		 * Figure out what sort of packet we have gotten by
47011ae08745Sheppo 		 * examining the msg tag, and then switch it appropriately.
47021ae08745Sheppo 		 */
47031ae08745Sheppo 		bcopy(&dmsg, &tag, sizeof (vio_msg_tag_t));
47041ae08745Sheppo 
47051ae08745Sheppo 		switch (tag.vio_msgtype) {
47061ae08745Sheppo 		case VIO_TYPE_CTRL:
47071ae08745Sheppo 			vsw_dispatch_ctrl_task(ldcp, &dmsg, tag);
47081ae08745Sheppo 			break;
47091ae08745Sheppo 		case VIO_TYPE_DATA:
47101ae08745Sheppo 			vsw_process_data_pkt(ldcp, &dmsg, tag);
47111ae08745Sheppo 			break;
47121ae08745Sheppo 		case VIO_TYPE_ERR:
47131ae08745Sheppo 			vsw_process_err_pkt(ldcp, &dmsg, tag);
47141ae08745Sheppo 			break;
47151ae08745Sheppo 		default:
47161ae08745Sheppo 			DERR(vswp, "%s: Unknown tag(%lx) ", __func__,
47171ae08745Sheppo 			    "id(%lx)\n", tag.vio_msgtype, ldcp->ldc_id);
47181ae08745Sheppo 			break;
47191ae08745Sheppo 		}
47201ae08745Sheppo 	} while (msglen);
47211ae08745Sheppo 
47221ae08745Sheppo 	D1(vswp, "%s exit: ldcid (%lld)\n", __func__, ldcp->ldc_id);
47231ae08745Sheppo }
47241ae08745Sheppo 
47251ae08745Sheppo /*
47261ae08745Sheppo  * Dispatch a task to process a VIO control message.
47271ae08745Sheppo  */
47281ae08745Sheppo static void
47291ae08745Sheppo vsw_dispatch_ctrl_task(vsw_ldc_t *ldcp, void *cpkt, vio_msg_tag_t tag)
47301ae08745Sheppo {
47311ae08745Sheppo 	vsw_ctrl_task_t		*ctaskp = NULL;
47321ae08745Sheppo 	vsw_port_t		*port = ldcp->ldc_port;
47331ae08745Sheppo 	vsw_t			*vswp = port->p_vswp;
47341ae08745Sheppo 
47351ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
47361ae08745Sheppo 
47371ae08745Sheppo 	/*
47381ae08745Sheppo 	 * We need to handle RDX ACK messages in-band as once they
47391ae08745Sheppo 	 * are exchanged it is possible that we will get an
47401ae08745Sheppo 	 * immediate (legitimate) data packet.
47411ae08745Sheppo 	 */
47421ae08745Sheppo 	if ((tag.vio_subtype_env == VIO_RDX) &&
47431ae08745Sheppo 	    (tag.vio_subtype == VIO_SUBTYPE_ACK)) {
47443af08d82Slm66018 
4745b071742bSsg70180 		if (vsw_check_flag(ldcp, INBOUND, VSW_RDX_ACK_RECV))
47461ae08745Sheppo 			return;
47471ae08745Sheppo 
4748b071742bSsg70180 		ldcp->lane_in.lstate |= VSW_RDX_ACK_RECV;
47493af08d82Slm66018 		D2(vswp, "%s (%ld) handling RDX_ACK in place "
47503af08d82Slm66018 		    "(ostate 0x%llx : hphase %d)", __func__,
4751b071742bSsg70180 		    ldcp->ldc_id, ldcp->lane_in.lstate, ldcp->hphase);
47521ae08745Sheppo 		vsw_next_milestone(ldcp);
47531ae08745Sheppo 		return;
47541ae08745Sheppo 	}
47551ae08745Sheppo 
47561ae08745Sheppo 	ctaskp = kmem_alloc(sizeof (vsw_ctrl_task_t), KM_NOSLEEP);
47571ae08745Sheppo 
47581ae08745Sheppo 	if (ctaskp == NULL) {
4759205eeb1aSlm66018 		DERR(vswp, "%s: unable to alloc space for ctrl msg", __func__);
4760b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
47611ae08745Sheppo 		return;
47621ae08745Sheppo 	}
47631ae08745Sheppo 
47641ae08745Sheppo 	ctaskp->ldcp = ldcp;
47651ae08745Sheppo 	bcopy((def_msg_t *)cpkt, &ctaskp->pktp, sizeof (def_msg_t));
47661ae08745Sheppo 	mutex_enter(&ldcp->hss_lock);
47671ae08745Sheppo 	ctaskp->hss_id = ldcp->hss_id;
47681ae08745Sheppo 	mutex_exit(&ldcp->hss_lock);
47691ae08745Sheppo 
47701ae08745Sheppo 	/*
47711ae08745Sheppo 	 * Dispatch task to processing taskq if port is not in
47721ae08745Sheppo 	 * the process of being detached.
47731ae08745Sheppo 	 */
47741ae08745Sheppo 	mutex_enter(&port->state_lock);
47751ae08745Sheppo 	if (port->state == VSW_PORT_INIT) {
47761ae08745Sheppo 		if ((vswp->taskq_p == NULL) ||
4777205eeb1aSlm66018 		    (ddi_taskq_dispatch(vswp->taskq_p, vsw_process_ctrl_pkt,
4778205eeb1aSlm66018 		    ctaskp, DDI_NOSLEEP) != DDI_SUCCESS)) {
47791ae08745Sheppo 			DERR(vswp, "%s: unable to dispatch task to taskq",
47801ae08745Sheppo 			    __func__);
47811ae08745Sheppo 			kmem_free(ctaskp, sizeof (vsw_ctrl_task_t));
47821ae08745Sheppo 			mutex_exit(&port->state_lock);
4783b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
47841ae08745Sheppo 			return;
47851ae08745Sheppo 		}
47861ae08745Sheppo 	} else {
47871ae08745Sheppo 		DWARN(vswp, "%s: port %d detaching, not dispatching "
47881ae08745Sheppo 		    "task", __func__, port->p_instance);
47891ae08745Sheppo 	}
47901ae08745Sheppo 
47911ae08745Sheppo 	mutex_exit(&port->state_lock);
47921ae08745Sheppo 
47931ae08745Sheppo 	D2(vswp, "%s: dispatched task to taskq for chan %d", __func__,
47941ae08745Sheppo 	    ldcp->ldc_id);
47951ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
47961ae08745Sheppo }
47971ae08745Sheppo 
47981ae08745Sheppo /*
47991ae08745Sheppo  * Process a VIO ctrl message. Invoked from taskq.
48001ae08745Sheppo  */
48011ae08745Sheppo static void
48021ae08745Sheppo vsw_process_ctrl_pkt(void *arg)
48031ae08745Sheppo {
48041ae08745Sheppo 	vsw_ctrl_task_t	*ctaskp = (vsw_ctrl_task_t *)arg;
48051ae08745Sheppo 	vsw_ldc_t	*ldcp = ctaskp->ldcp;
48061ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
48071ae08745Sheppo 	vio_msg_tag_t	tag;
48081ae08745Sheppo 	uint16_t	env;
48091ae08745Sheppo 
48101ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
48111ae08745Sheppo 
48121ae08745Sheppo 	bcopy(&ctaskp->pktp, &tag, sizeof (vio_msg_tag_t));
48131ae08745Sheppo 	env = tag.vio_subtype_env;
48141ae08745Sheppo 
48151ae08745Sheppo 	/* stale pkt check */
48161ae08745Sheppo 	mutex_enter(&ldcp->hss_lock);
48171ae08745Sheppo 	if (ctaskp->hss_id < ldcp->hss_id) {
4818205eeb1aSlm66018 		DWARN(vswp, "%s: discarding stale packet belonging to earlier"
4819205eeb1aSlm66018 		    " (%ld) handshake session", __func__, ctaskp->hss_id);
48201ae08745Sheppo 		mutex_exit(&ldcp->hss_lock);
48211ae08745Sheppo 		return;
48221ae08745Sheppo 	}
48231ae08745Sheppo 	mutex_exit(&ldcp->hss_lock);
48241ae08745Sheppo 
48251ae08745Sheppo 	/* session id check */
48261ae08745Sheppo 	if (ldcp->session_status & VSW_PEER_SESSION) {
48271ae08745Sheppo 		if (ldcp->peer_session != tag.vio_sid) {
48281ae08745Sheppo 			DERR(vswp, "%s (chan %d): invalid session id (%llx)",
48291ae08745Sheppo 			    __func__, ldcp->ldc_id, tag.vio_sid);
48301ae08745Sheppo 			kmem_free(ctaskp, sizeof (vsw_ctrl_task_t));
4831b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
48321ae08745Sheppo 			return;
48331ae08745Sheppo 		}
48341ae08745Sheppo 	}
48351ae08745Sheppo 
48361ae08745Sheppo 	/*
48371ae08745Sheppo 	 * Switch on vio_subtype envelope, then let lower routines
48381ae08745Sheppo 	 * decide if its an INFO, ACK or NACK packet.
48391ae08745Sheppo 	 */
48401ae08745Sheppo 	switch (env) {
48411ae08745Sheppo 	case VIO_VER_INFO:
48421ae08745Sheppo 		vsw_process_ctrl_ver_pkt(ldcp, &ctaskp->pktp);
48431ae08745Sheppo 		break;
48441ae08745Sheppo 	case VIO_DRING_REG:
48451ae08745Sheppo 		vsw_process_ctrl_dring_reg_pkt(ldcp, &ctaskp->pktp);
48461ae08745Sheppo 		break;
48471ae08745Sheppo 	case VIO_DRING_UNREG:
48481ae08745Sheppo 		vsw_process_ctrl_dring_unreg_pkt(ldcp, &ctaskp->pktp);
48491ae08745Sheppo 		break;
48501ae08745Sheppo 	case VIO_ATTR_INFO:
48511ae08745Sheppo 		vsw_process_ctrl_attr_pkt(ldcp, &ctaskp->pktp);
48521ae08745Sheppo 		break;
48531ae08745Sheppo 	case VNET_MCAST_INFO:
48541ae08745Sheppo 		vsw_process_ctrl_mcst_pkt(ldcp, &ctaskp->pktp);
48551ae08745Sheppo 		break;
48561ae08745Sheppo 	case VIO_RDX:
48571ae08745Sheppo 		vsw_process_ctrl_rdx_pkt(ldcp, &ctaskp->pktp);
48581ae08745Sheppo 		break;
48591ae08745Sheppo 	default:
4860205eeb1aSlm66018 		DERR(vswp, "%s: unknown vio_subtype_env (%x)\n", __func__, env);
48611ae08745Sheppo 	}
48621ae08745Sheppo 
48631ae08745Sheppo 	kmem_free(ctaskp, sizeof (vsw_ctrl_task_t));
48641ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
48651ae08745Sheppo }
48661ae08745Sheppo 
48671ae08745Sheppo /*
48681ae08745Sheppo  * Version negotiation. We can end up here either because our peer
48691ae08745Sheppo  * has responded to a handshake message we have sent it, or our peer
48701ae08745Sheppo  * has initiated a handshake with us. If its the former then can only
48711ae08745Sheppo  * be ACK or NACK, if its the later can only be INFO.
48721ae08745Sheppo  *
48731ae08745Sheppo  * If its an ACK we move to the next stage of the handshake, namely
48741ae08745Sheppo  * attribute exchange. If its a NACK we see if we can specify another
48751ae08745Sheppo  * version, if we can't we stop.
48761ae08745Sheppo  *
48771ae08745Sheppo  * If it is an INFO we reset all params associated with communication
48781ae08745Sheppo  * in that direction over this channel (remember connection is
48791ae08745Sheppo  * essentially 2 independent simplex channels).
48801ae08745Sheppo  */
48811ae08745Sheppo void
48821ae08745Sheppo vsw_process_ctrl_ver_pkt(vsw_ldc_t *ldcp, void *pkt)
48831ae08745Sheppo {
48841ae08745Sheppo 	vio_ver_msg_t	*ver_pkt;
48851ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
48861ae08745Sheppo 
48871ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
48881ae08745Sheppo 
48891ae08745Sheppo 	/*
48901ae08745Sheppo 	 * We know this is a ctrl/version packet so
48911ae08745Sheppo 	 * cast it into the correct structure.
48921ae08745Sheppo 	 */
48931ae08745Sheppo 	ver_pkt = (vio_ver_msg_t *)pkt;
48941ae08745Sheppo 
48951ae08745Sheppo 	switch (ver_pkt->tag.vio_subtype) {
48961ae08745Sheppo 	case VIO_SUBTYPE_INFO:
48971ae08745Sheppo 		D2(vswp, "vsw_process_ctrl_ver_pkt: VIO_SUBTYPE_INFO\n");
48981ae08745Sheppo 
48991ae08745Sheppo 		/*
49001ae08745Sheppo 		 * Record the session id, which we will use from now
49011ae08745Sheppo 		 * until we see another VER_INFO msg. Even then the
49021ae08745Sheppo 		 * session id in most cases will be unchanged, execpt
49031ae08745Sheppo 		 * if channel was reset.
49041ae08745Sheppo 		 */
49051ae08745Sheppo 		if ((ldcp->session_status & VSW_PEER_SESSION) &&
49061ae08745Sheppo 		    (ldcp->peer_session != ver_pkt->tag.vio_sid)) {
49071ae08745Sheppo 			DERR(vswp, "%s: updating session id for chan %lld "
49081ae08745Sheppo 			    "from %llx to %llx", __func__, ldcp->ldc_id,
49091ae08745Sheppo 			    ldcp->peer_session, ver_pkt->tag.vio_sid);
49101ae08745Sheppo 		}
49111ae08745Sheppo 
49121ae08745Sheppo 		ldcp->peer_session = ver_pkt->tag.vio_sid;
49131ae08745Sheppo 		ldcp->session_status |= VSW_PEER_SESSION;
49141ae08745Sheppo 
49151ae08745Sheppo 		/* Legal message at this time ? */
49161ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_VER_INFO_RECV))
49171ae08745Sheppo 			return;
49181ae08745Sheppo 
49191ae08745Sheppo 		/*
49201ae08745Sheppo 		 * First check the device class. Currently only expect
49211ae08745Sheppo 		 * to be talking to a network device. In the future may
49221ae08745Sheppo 		 * also talk to another switch.
49231ae08745Sheppo 		 */
49241ae08745Sheppo 		if (ver_pkt->dev_class != VDEV_NETWORK) {
49251ae08745Sheppo 			DERR(vswp, "%s: illegal device class %d", __func__,
49261ae08745Sheppo 			    ver_pkt->dev_class);
49271ae08745Sheppo 
49281ae08745Sheppo 			ver_pkt->tag.vio_sid = ldcp->local_session;
49291ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
49301ae08745Sheppo 
49311ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)ver_pkt);
49321ae08745Sheppo 
4933b071742bSsg70180 			(void) vsw_send_msg(ldcp, (void *)ver_pkt,
4934b071742bSsg70180 			    sizeof (vio_ver_msg_t), B_TRUE);
49351ae08745Sheppo 
49361ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_VER_NACK_SENT;
49371ae08745Sheppo 			vsw_next_milestone(ldcp);
49381ae08745Sheppo 			return;
49391ae08745Sheppo 		} else {
49401ae08745Sheppo 			ldcp->dev_class = ver_pkt->dev_class;
49411ae08745Sheppo 		}
49421ae08745Sheppo 
49431ae08745Sheppo 		/*
49441ae08745Sheppo 		 * Now check the version.
49451ae08745Sheppo 		 */
49461ae08745Sheppo 		if (vsw_supported_version(ver_pkt) == 0) {
49471ae08745Sheppo 			/*
49481ae08745Sheppo 			 * Support this major version and possibly
49491ae08745Sheppo 			 * adjusted minor version.
49501ae08745Sheppo 			 */
49511ae08745Sheppo 
49521ae08745Sheppo 			D2(vswp, "%s: accepted ver %d:%d", __func__,
49531ae08745Sheppo 			    ver_pkt->ver_major, ver_pkt->ver_minor);
49541ae08745Sheppo 
49551ae08745Sheppo 			/* Store accepted values */
49561ae08745Sheppo 			ldcp->lane_in.ver_major = ver_pkt->ver_major;
49571ae08745Sheppo 			ldcp->lane_in.ver_minor = ver_pkt->ver_minor;
49581ae08745Sheppo 
49591ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
49601ae08745Sheppo 
49611ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_VER_ACK_SENT;
49621ae08745Sheppo 		} else {
49631ae08745Sheppo 			/*
49641ae08745Sheppo 			 * NACK back with the next lower major/minor
49651ae08745Sheppo 			 * pairing we support (if don't suuport any more
49661ae08745Sheppo 			 * versions then they will be set to zero.
49671ae08745Sheppo 			 */
49681ae08745Sheppo 
49691ae08745Sheppo 			D2(vswp, "%s: replying with ver %d:%d", __func__,
49701ae08745Sheppo 			    ver_pkt->ver_major, ver_pkt->ver_minor);
49711ae08745Sheppo 
49721ae08745Sheppo 			/* Store updated values */
49731ae08745Sheppo 			ldcp->lane_in.ver_major = ver_pkt->ver_major;
49741ae08745Sheppo 			ldcp->lane_in.ver_minor = ver_pkt->ver_minor;
49751ae08745Sheppo 
49761ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
49771ae08745Sheppo 
49781ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_VER_NACK_SENT;
49791ae08745Sheppo 		}
49801ae08745Sheppo 
49811ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)ver_pkt);
49821ae08745Sheppo 		ver_pkt->tag.vio_sid = ldcp->local_session;
4983b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)ver_pkt,
4984b071742bSsg70180 		    sizeof (vio_ver_msg_t), B_TRUE);
49851ae08745Sheppo 
49861ae08745Sheppo 		vsw_next_milestone(ldcp);
49871ae08745Sheppo 		break;
49881ae08745Sheppo 
49891ae08745Sheppo 	case VIO_SUBTYPE_ACK:
49901ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK\n", __func__);
49911ae08745Sheppo 
49921ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_VER_ACK_RECV))
49931ae08745Sheppo 			return;
49941ae08745Sheppo 
49951ae08745Sheppo 		/* Store updated values */
49961ae08745Sheppo 		ldcp->lane_in.ver_major = ver_pkt->ver_major;
49971ae08745Sheppo 		ldcp->lane_in.ver_minor = ver_pkt->ver_minor;
49981ae08745Sheppo 
49991ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_VER_ACK_RECV;
50001ae08745Sheppo 		vsw_next_milestone(ldcp);
50011ae08745Sheppo 
50021ae08745Sheppo 		break;
50031ae08745Sheppo 
50041ae08745Sheppo 	case VIO_SUBTYPE_NACK:
50051ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK\n", __func__);
50061ae08745Sheppo 
50071ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_VER_NACK_RECV))
50081ae08745Sheppo 			return;
50091ae08745Sheppo 
50101ae08745Sheppo 		/*
50111ae08745Sheppo 		 * If our peer sent us a NACK with the ver fields set to
50121ae08745Sheppo 		 * zero then there is nothing more we can do. Otherwise see
50131ae08745Sheppo 		 * if we support either the version suggested, or a lesser
50141ae08745Sheppo 		 * one.
50151ae08745Sheppo 		 */
50161ae08745Sheppo 		if ((ver_pkt->ver_major == 0) && (ver_pkt->ver_minor == 0)) {
50171ae08745Sheppo 			DERR(vswp, "%s: peer unable to negotiate any "
50181ae08745Sheppo 			    "further.", __func__);
50191ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_VER_NACK_RECV;
50201ae08745Sheppo 			vsw_next_milestone(ldcp);
50211ae08745Sheppo 			return;
50221ae08745Sheppo 		}
50231ae08745Sheppo 
50241ae08745Sheppo 		/*
50251ae08745Sheppo 		 * Check to see if we support this major version or
50261ae08745Sheppo 		 * a lower one. If we don't then maj/min will be set
50271ae08745Sheppo 		 * to zero.
50281ae08745Sheppo 		 */
50291ae08745Sheppo 		(void) vsw_supported_version(ver_pkt);
50301ae08745Sheppo 		if ((ver_pkt->ver_major == 0) && (ver_pkt->ver_minor == 0)) {
50311ae08745Sheppo 			/* Nothing more we can do */
50321ae08745Sheppo 			DERR(vswp, "%s: version negotiation failed.\n",
50331ae08745Sheppo 			    __func__);
50341ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_VER_NACK_RECV;
50351ae08745Sheppo 			vsw_next_milestone(ldcp);
50361ae08745Sheppo 		} else {
50371ae08745Sheppo 			/* found a supported major version */
50381ae08745Sheppo 			ldcp->lane_out.ver_major = ver_pkt->ver_major;
50391ae08745Sheppo 			ldcp->lane_out.ver_minor = ver_pkt->ver_minor;
50401ae08745Sheppo 
50411ae08745Sheppo 			D2(vswp, "%s: resending with updated values (%x, %x)",
5042205eeb1aSlm66018 			    __func__, ver_pkt->ver_major, ver_pkt->ver_minor);
50431ae08745Sheppo 
50441ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_VER_INFO_SENT;
50451ae08745Sheppo 			ver_pkt->tag.vio_sid = ldcp->local_session;
50461ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_INFO;
50471ae08745Sheppo 
50481ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)ver_pkt);
50491ae08745Sheppo 
5050b071742bSsg70180 			(void) vsw_send_msg(ldcp, (void *)ver_pkt,
5051b071742bSsg70180 			    sizeof (vio_ver_msg_t), B_TRUE);
50521ae08745Sheppo 
50531ae08745Sheppo 			vsw_next_milestone(ldcp);
50541ae08745Sheppo 
50551ae08745Sheppo 		}
50561ae08745Sheppo 		break;
50571ae08745Sheppo 
50581ae08745Sheppo 	default:
50591ae08745Sheppo 		DERR(vswp, "%s: unknown vio_subtype %x\n", __func__,
50601ae08745Sheppo 		    ver_pkt->tag.vio_subtype);
50611ae08745Sheppo 	}
50621ae08745Sheppo 
50631ae08745Sheppo 	D1(vswp, "%s(%lld): exit\n", __func__, ldcp->ldc_id);
50641ae08745Sheppo }
50651ae08745Sheppo 
50661ae08745Sheppo /*
50671ae08745Sheppo  * Process an attribute packet. We can end up here either because our peer
50681ae08745Sheppo  * has ACK/NACK'ed back to an earlier ATTR msg we had sent it, or our
50691ae08745Sheppo  * peer has sent us an attribute INFO message
50701ae08745Sheppo  *
50711ae08745Sheppo  * If its an ACK we then move to the next stage of the handshake which
50721ae08745Sheppo  * is to send our descriptor ring info to our peer. If its a NACK then
50731ae08745Sheppo  * there is nothing more we can (currently) do.
50741ae08745Sheppo  *
50751ae08745Sheppo  * If we get a valid/acceptable INFO packet (and we have already negotiated
50761ae08745Sheppo  * a version) we ACK back and set channel state to ATTR_RECV, otherwise we
50771ae08745Sheppo  * NACK back and reset channel state to INACTIV.
50781ae08745Sheppo  *
50791ae08745Sheppo  * FUTURE: in time we will probably negotiate over attributes, but for
50801ae08745Sheppo  * the moment unacceptable attributes are regarded as a fatal error.
50811ae08745Sheppo  *
50821ae08745Sheppo  */
50831ae08745Sheppo void
50841ae08745Sheppo vsw_process_ctrl_attr_pkt(vsw_ldc_t *ldcp, void *pkt)
50851ae08745Sheppo {
50861ae08745Sheppo 	vnet_attr_msg_t		*attr_pkt;
50871ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
50881ae08745Sheppo 	vsw_port_t		*port = ldcp->ldc_port;
50891ae08745Sheppo 	uint64_t		macaddr = 0;
50901ae08745Sheppo 	int			i;
50911ae08745Sheppo 
50921ae08745Sheppo 	D1(vswp, "%s(%lld) enter", __func__, ldcp->ldc_id);
50931ae08745Sheppo 
50941ae08745Sheppo 	/*
50951ae08745Sheppo 	 * We know this is a ctrl/attr packet so
50961ae08745Sheppo 	 * cast it into the correct structure.
50971ae08745Sheppo 	 */
50981ae08745Sheppo 	attr_pkt = (vnet_attr_msg_t *)pkt;
50991ae08745Sheppo 
51001ae08745Sheppo 	switch (attr_pkt->tag.vio_subtype) {
51011ae08745Sheppo 	case VIO_SUBTYPE_INFO:
51021ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
51031ae08745Sheppo 
51041ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_ATTR_INFO_RECV))
51051ae08745Sheppo 			return;
51061ae08745Sheppo 
51071ae08745Sheppo 		/*
51081ae08745Sheppo 		 * If the attributes are unacceptable then we NACK back.
51091ae08745Sheppo 		 */
51101ae08745Sheppo 		if (vsw_check_attr(attr_pkt, ldcp->ldc_port)) {
51111ae08745Sheppo 
51121ae08745Sheppo 			DERR(vswp, "%s (chan %d): invalid attributes",
51131ae08745Sheppo 			    __func__, ldcp->ldc_id);
51141ae08745Sheppo 
51151ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
51161ae08745Sheppo 
51171ae08745Sheppo 			attr_pkt->tag.vio_sid = ldcp->local_session;
51181ae08745Sheppo 			attr_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
51191ae08745Sheppo 
51201ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)attr_pkt);
51211ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_ATTR_NACK_SENT;
5122b071742bSsg70180 			(void) vsw_send_msg(ldcp, (void *)attr_pkt,
5123b071742bSsg70180 			    sizeof (vnet_attr_msg_t), B_TRUE);
51241ae08745Sheppo 
51251ae08745Sheppo 			vsw_next_milestone(ldcp);
51261ae08745Sheppo 			return;
51271ae08745Sheppo 		}
51281ae08745Sheppo 
51291ae08745Sheppo 		/*
51301ae08745Sheppo 		 * Otherwise store attributes for this lane and update
51311ae08745Sheppo 		 * lane state.
51321ae08745Sheppo 		 */
51331ae08745Sheppo 		ldcp->lane_in.mtu = attr_pkt->mtu;
51341ae08745Sheppo 		ldcp->lane_in.addr = attr_pkt->addr;
51351ae08745Sheppo 		ldcp->lane_in.addr_type = attr_pkt->addr_type;
51361ae08745Sheppo 		ldcp->lane_in.xfer_mode = attr_pkt->xfer_mode;
51371ae08745Sheppo 		ldcp->lane_in.ack_freq = attr_pkt->ack_freq;
51381ae08745Sheppo 
51391ae08745Sheppo 		macaddr = ldcp->lane_in.addr;
51401ae08745Sheppo 		for (i = ETHERADDRL - 1; i >= 0; i--) {
51411ae08745Sheppo 			port->p_macaddr.ether_addr_octet[i] = macaddr & 0xFF;
51421ae08745Sheppo 			macaddr >>= 8;
51431ae08745Sheppo 		}
51441ae08745Sheppo 
51451ae08745Sheppo 		/* create the fdb entry for this port/mac address */
51461ae08745Sheppo 		(void) vsw_add_fdb(vswp, port);
51471ae08745Sheppo 
51481ae08745Sheppo 		/* setup device specifc xmit routines */
51491ae08745Sheppo 		mutex_enter(&port->tx_lock);
51501ae08745Sheppo 		if (ldcp->lane_in.xfer_mode == VIO_DRING_MODE) {
51511ae08745Sheppo 			D2(vswp, "%s: mode = VIO_DRING_MODE", __func__);
51521ae08745Sheppo 			port->transmit = vsw_dringsend;
51531ae08745Sheppo 		} else if (ldcp->lane_in.xfer_mode == VIO_DESC_MODE) {
51541ae08745Sheppo 			D2(vswp, "%s: mode = VIO_DESC_MODE", __func__);
51551ae08745Sheppo 			vsw_create_privring(ldcp);
51561ae08745Sheppo 			port->transmit = vsw_descrsend;
51571ae08745Sheppo 		}
51581ae08745Sheppo 		mutex_exit(&port->tx_lock);
51591ae08745Sheppo 
51601ae08745Sheppo 		attr_pkt->tag.vio_sid = ldcp->local_session;
51611ae08745Sheppo 		attr_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
51621ae08745Sheppo 
51631ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)attr_pkt);
51641ae08745Sheppo 
51651ae08745Sheppo 		ldcp->lane_in.lstate |= VSW_ATTR_ACK_SENT;
51661ae08745Sheppo 
5167b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)attr_pkt,
5168b071742bSsg70180 		    sizeof (vnet_attr_msg_t), B_TRUE);
51691ae08745Sheppo 
51701ae08745Sheppo 		vsw_next_milestone(ldcp);
51711ae08745Sheppo 		break;
51721ae08745Sheppo 
51731ae08745Sheppo 	case VIO_SUBTYPE_ACK:
51741ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
51751ae08745Sheppo 
51761ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_ATTR_ACK_RECV))
51771ae08745Sheppo 			return;
51781ae08745Sheppo 
51791ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_ATTR_ACK_RECV;
51801ae08745Sheppo 		vsw_next_milestone(ldcp);
51811ae08745Sheppo 		break;
51821ae08745Sheppo 
51831ae08745Sheppo 	case VIO_SUBTYPE_NACK:
51841ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
51851ae08745Sheppo 
51861ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_ATTR_NACK_RECV))
51871ae08745Sheppo 			return;
51881ae08745Sheppo 
51891ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_ATTR_NACK_RECV;
51901ae08745Sheppo 		vsw_next_milestone(ldcp);
51911ae08745Sheppo 		break;
51921ae08745Sheppo 
51931ae08745Sheppo 	default:
51941ae08745Sheppo 		DERR(vswp, "%s: unknown vio_subtype %x\n", __func__,
51951ae08745Sheppo 		    attr_pkt->tag.vio_subtype);
51961ae08745Sheppo 	}
51971ae08745Sheppo 
51981ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
51991ae08745Sheppo }
52001ae08745Sheppo 
52011ae08745Sheppo /*
52021ae08745Sheppo  * Process a dring info packet. We can end up here either because our peer
52031ae08745Sheppo  * has ACK/NACK'ed back to an earlier DRING msg we had sent it, or our
52041ae08745Sheppo  * peer has sent us a dring INFO message.
52051ae08745Sheppo  *
52061ae08745Sheppo  * If we get a valid/acceptable INFO packet (and we have already negotiated
52071ae08745Sheppo  * a version) we ACK back and update the lane state, otherwise we NACK back.
52081ae08745Sheppo  *
52091ae08745Sheppo  * FUTURE: nothing to stop client from sending us info on multiple dring's
52101ae08745Sheppo  * but for the moment we will just use the first one we are given.
52111ae08745Sheppo  *
52121ae08745Sheppo  */
52131ae08745Sheppo void
52141ae08745Sheppo vsw_process_ctrl_dring_reg_pkt(vsw_ldc_t *ldcp, void *pkt)
52151ae08745Sheppo {
52161ae08745Sheppo 	vio_dring_reg_msg_t	*dring_pkt;
52171ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
52181ae08745Sheppo 	ldc_mem_info_t		minfo;
52191ae08745Sheppo 	dring_info_t		*dp, *dbp;
52201ae08745Sheppo 	int			dring_found = 0;
52211ae08745Sheppo 
52221ae08745Sheppo 	/*
52231ae08745Sheppo 	 * We know this is a ctrl/dring packet so
52241ae08745Sheppo 	 * cast it into the correct structure.
52251ae08745Sheppo 	 */
52261ae08745Sheppo 	dring_pkt = (vio_dring_reg_msg_t *)pkt;
52271ae08745Sheppo 
52281ae08745Sheppo 	D1(vswp, "%s(%lld) enter", __func__, ldcp->ldc_id);
52291ae08745Sheppo 
52301ae08745Sheppo 	switch (dring_pkt->tag.vio_subtype) {
52311ae08745Sheppo 	case VIO_SUBTYPE_INFO:
52321ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
52331ae08745Sheppo 
52341ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_DRING_INFO_RECV))
52351ae08745Sheppo 			return;
52361ae08745Sheppo 
52371ae08745Sheppo 		/*
52381ae08745Sheppo 		 * If the dring params are unacceptable then we NACK back.
52391ae08745Sheppo 		 */
52401ae08745Sheppo 		if (vsw_check_dring_info(dring_pkt)) {
52411ae08745Sheppo 
52421ae08745Sheppo 			DERR(vswp, "%s (%lld): invalid dring info",
52431ae08745Sheppo 			    __func__, ldcp->ldc_id);
52441ae08745Sheppo 
52451ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
52461ae08745Sheppo 
52471ae08745Sheppo 			dring_pkt->tag.vio_sid = ldcp->local_session;
52481ae08745Sheppo 			dring_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
52491ae08745Sheppo 
52501ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)dring_pkt);
52511ae08745Sheppo 
52521ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_DRING_NACK_SENT;
52531ae08745Sheppo 
5254b071742bSsg70180 			(void) vsw_send_msg(ldcp, (void *)dring_pkt,
5255b071742bSsg70180 			    sizeof (vio_dring_reg_msg_t), B_TRUE);
52561ae08745Sheppo 
52571ae08745Sheppo 			vsw_next_milestone(ldcp);
52581ae08745Sheppo 			return;
52591ae08745Sheppo 		}
52601ae08745Sheppo 
52611ae08745Sheppo 		/*
52621ae08745Sheppo 		 * Otherwise, attempt to map in the dring using the
52631ae08745Sheppo 		 * cookie. If that succeeds we send back a unique dring
52641ae08745Sheppo 		 * identifier that the sending side will use in future
52651ae08745Sheppo 		 * to refer to this descriptor ring.
52661ae08745Sheppo 		 */
52671ae08745Sheppo 		dp = kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
52681ae08745Sheppo 
52691ae08745Sheppo 		dp->num_descriptors = dring_pkt->num_descriptors;
52701ae08745Sheppo 		dp->descriptor_size = dring_pkt->descriptor_size;
52711ae08745Sheppo 		dp->options = dring_pkt->options;
52721ae08745Sheppo 		dp->ncookies = dring_pkt->ncookies;
52731ae08745Sheppo 
52741ae08745Sheppo 		/*
52751ae08745Sheppo 		 * Note: should only get one cookie. Enforced in
52761ae08745Sheppo 		 * the ldc layer.
52771ae08745Sheppo 		 */
52781ae08745Sheppo 		bcopy(&dring_pkt->cookie[0], &dp->cookie[0],
52791ae08745Sheppo 		    sizeof (ldc_mem_cookie_t));
52801ae08745Sheppo 
52811ae08745Sheppo 		D2(vswp, "%s: num_desc %ld : desc_size %ld", __func__,
52821ae08745Sheppo 		    dp->num_descriptors, dp->descriptor_size);
52831ae08745Sheppo 		D2(vswp, "%s: options 0x%lx: ncookies %ld", __func__,
52841ae08745Sheppo 		    dp->options, dp->ncookies);
52851ae08745Sheppo 
52861ae08745Sheppo 		if ((ldc_mem_dring_map(ldcp->ldc_handle, &dp->cookie[0],
5287205eeb1aSlm66018 		    dp->ncookies, dp->num_descriptors, dp->descriptor_size,
5288205eeb1aSlm66018 		    LDC_SHADOW_MAP, &(dp->handle))) != 0) {
52891ae08745Sheppo 
52901ae08745Sheppo 			DERR(vswp, "%s: dring_map failed\n", __func__);
52911ae08745Sheppo 
52921ae08745Sheppo 			kmem_free(dp, sizeof (dring_info_t));
52931ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
52941ae08745Sheppo 
52951ae08745Sheppo 			dring_pkt->tag.vio_sid = ldcp->local_session;
52961ae08745Sheppo 			dring_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
52971ae08745Sheppo 
52981ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)dring_pkt);
52991ae08745Sheppo 
53001ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_DRING_NACK_SENT;
5301b071742bSsg70180 			(void) vsw_send_msg(ldcp, (void *)dring_pkt,
5302b071742bSsg70180 			    sizeof (vio_dring_reg_msg_t), B_TRUE);
53031ae08745Sheppo 
53041ae08745Sheppo 			vsw_next_milestone(ldcp);
53051ae08745Sheppo 			return;
53061ae08745Sheppo 		}
53071ae08745Sheppo 
53081ae08745Sheppo 		if ((ldc_mem_dring_info(dp->handle, &minfo)) != 0) {
53091ae08745Sheppo 
53101ae08745Sheppo 			DERR(vswp, "%s: dring_addr failed\n", __func__);
53111ae08745Sheppo 
53121ae08745Sheppo 			kmem_free(dp, sizeof (dring_info_t));
53131ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
53141ae08745Sheppo 
53151ae08745Sheppo 			dring_pkt->tag.vio_sid = ldcp->local_session;
53161ae08745Sheppo 			dring_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
53171ae08745Sheppo 
53181ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)dring_pkt);
53191ae08745Sheppo 
53201ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_DRING_NACK_SENT;
5321b071742bSsg70180 			(void) vsw_send_msg(ldcp, (void *)dring_pkt,
5322b071742bSsg70180 			    sizeof (vio_dring_reg_msg_t), B_TRUE);
53231ae08745Sheppo 
53241ae08745Sheppo 			vsw_next_milestone(ldcp);
53251ae08745Sheppo 			return;
53261ae08745Sheppo 		} else {
53271ae08745Sheppo 			/* store the address of the pub part of ring */
53281ae08745Sheppo 			dp->pub_addr = minfo.vaddr;
53291ae08745Sheppo 		}
53301ae08745Sheppo 
53311ae08745Sheppo 		/* no private section as we are importing */
53321ae08745Sheppo 		dp->priv_addr = NULL;
53331ae08745Sheppo 
53341ae08745Sheppo 		/*
53351ae08745Sheppo 		 * Using simple mono increasing int for ident at
53361ae08745Sheppo 		 * the moment.
53371ae08745Sheppo 		 */
53381ae08745Sheppo 		dp->ident = ldcp->next_ident;
53391ae08745Sheppo 		ldcp->next_ident++;
53401ae08745Sheppo 
53411ae08745Sheppo 		dp->end_idx = 0;
53421ae08745Sheppo 		dp->next = NULL;
53431ae08745Sheppo 
53441ae08745Sheppo 		/*
53451ae08745Sheppo 		 * Link it onto the end of the list of drings
53461ae08745Sheppo 		 * for this lane.
53471ae08745Sheppo 		 */
53481ae08745Sheppo 		if (ldcp->lane_in.dringp == NULL) {
53491ae08745Sheppo 			D2(vswp, "%s: adding first INBOUND dring", __func__);
53501ae08745Sheppo 			ldcp->lane_in.dringp = dp;
53511ae08745Sheppo 		} else {
53521ae08745Sheppo 			dbp = ldcp->lane_in.dringp;
53531ae08745Sheppo 
53541ae08745Sheppo 			while (dbp->next != NULL)
53551ae08745Sheppo 				dbp = dbp->next;
53561ae08745Sheppo 
53571ae08745Sheppo 			dbp->next = dp;
53581ae08745Sheppo 		}
53591ae08745Sheppo 
53601ae08745Sheppo 		/* acknowledge it */
53611ae08745Sheppo 		dring_pkt->tag.vio_sid = ldcp->local_session;
53621ae08745Sheppo 		dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
53631ae08745Sheppo 		dring_pkt->dring_ident = dp->ident;
53641ae08745Sheppo 
5365b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)dring_pkt,
5366b071742bSsg70180 		    sizeof (vio_dring_reg_msg_t), B_TRUE);
53671ae08745Sheppo 
53681ae08745Sheppo 		ldcp->lane_in.lstate |= VSW_DRING_ACK_SENT;
53691ae08745Sheppo 		vsw_next_milestone(ldcp);
53701ae08745Sheppo 		break;
53711ae08745Sheppo 
53721ae08745Sheppo 	case VIO_SUBTYPE_ACK:
53731ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
53741ae08745Sheppo 
53751ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_DRING_ACK_RECV))
53761ae08745Sheppo 			return;
53771ae08745Sheppo 
53781ae08745Sheppo 		/*
53791ae08745Sheppo 		 * Peer is acknowledging our dring info and will have
53801ae08745Sheppo 		 * sent us a dring identifier which we will use to
53811ae08745Sheppo 		 * refer to this ring w.r.t. our peer.
53821ae08745Sheppo 		 */
53831ae08745Sheppo 		dp = ldcp->lane_out.dringp;
53841ae08745Sheppo 		if (dp != NULL) {
53851ae08745Sheppo 			/*
53861ae08745Sheppo 			 * Find the ring this ident should be associated
53871ae08745Sheppo 			 * with.
53881ae08745Sheppo 			 */
53891ae08745Sheppo 			if (vsw_dring_match(dp, dring_pkt)) {
53901ae08745Sheppo 				dring_found = 1;
53911ae08745Sheppo 
53921ae08745Sheppo 			} else while (dp != NULL) {
53931ae08745Sheppo 				if (vsw_dring_match(dp, dring_pkt)) {
53941ae08745Sheppo 					dring_found = 1;
53951ae08745Sheppo 					break;
53961ae08745Sheppo 				}
53971ae08745Sheppo 				dp = dp->next;
53981ae08745Sheppo 			}
53991ae08745Sheppo 
54001ae08745Sheppo 			if (dring_found == 0) {
54011ae08745Sheppo 				DERR(NULL, "%s: unrecognised ring cookie",
54021ae08745Sheppo 				    __func__);
5403b071742bSsg70180 				vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
54041ae08745Sheppo 				return;
54051ae08745Sheppo 			}
54061ae08745Sheppo 
54071ae08745Sheppo 		} else {
54081ae08745Sheppo 			DERR(vswp, "%s: DRING ACK received but no drings "
54091ae08745Sheppo 			    "allocated", __func__);
5410b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
54111ae08745Sheppo 			return;
54121ae08745Sheppo 		}
54131ae08745Sheppo 
54141ae08745Sheppo 		/* store ident */
54151ae08745Sheppo 		dp->ident = dring_pkt->dring_ident;
54161ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_DRING_ACK_RECV;
54171ae08745Sheppo 		vsw_next_milestone(ldcp);
54181ae08745Sheppo 		break;
54191ae08745Sheppo 
54201ae08745Sheppo 	case VIO_SUBTYPE_NACK:
54211ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
54221ae08745Sheppo 
54231ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_DRING_NACK_RECV))
54241ae08745Sheppo 			return;
54251ae08745Sheppo 
54261ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_DRING_NACK_RECV;
54271ae08745Sheppo 		vsw_next_milestone(ldcp);
54281ae08745Sheppo 		break;
54291ae08745Sheppo 
54301ae08745Sheppo 	default:
54311ae08745Sheppo 		DERR(vswp, "%s: Unknown vio_subtype %x\n", __func__,
54321ae08745Sheppo 		    dring_pkt->tag.vio_subtype);
54331ae08745Sheppo 	}
54341ae08745Sheppo 
54351ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
54361ae08745Sheppo }
54371ae08745Sheppo 
54381ae08745Sheppo /*
54391ae08745Sheppo  * Process a request from peer to unregister a dring.
54401ae08745Sheppo  *
54411ae08745Sheppo  * For the moment we just restart the handshake if our
54421ae08745Sheppo  * peer endpoint attempts to unregister a dring.
54431ae08745Sheppo  */
54441ae08745Sheppo void
54451ae08745Sheppo vsw_process_ctrl_dring_unreg_pkt(vsw_ldc_t *ldcp, void *pkt)
54461ae08745Sheppo {
54471ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
54481ae08745Sheppo 	vio_dring_unreg_msg_t	*dring_pkt;
54491ae08745Sheppo 
54501ae08745Sheppo 	/*
54511ae08745Sheppo 	 * We know this is a ctrl/dring packet so
54521ae08745Sheppo 	 * cast it into the correct structure.
54531ae08745Sheppo 	 */
54541ae08745Sheppo 	dring_pkt = (vio_dring_unreg_msg_t *)pkt;
54551ae08745Sheppo 
54561ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
54571ae08745Sheppo 
54581ae08745Sheppo 	switch (dring_pkt->tag.vio_subtype) {
54591ae08745Sheppo 	case VIO_SUBTYPE_INFO:
54601ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
54611ae08745Sheppo 
54621ae08745Sheppo 		DWARN(vswp, "%s: restarting handshake..", __func__);
54631ae08745Sheppo 		break;
54641ae08745Sheppo 
54651ae08745Sheppo 	case VIO_SUBTYPE_ACK:
54661ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
54671ae08745Sheppo 
54681ae08745Sheppo 		DWARN(vswp, "%s: restarting handshake..", __func__);
54691ae08745Sheppo 		break;
54701ae08745Sheppo 
54711ae08745Sheppo 	case VIO_SUBTYPE_NACK:
54721ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
54731ae08745Sheppo 
54741ae08745Sheppo 		DWARN(vswp, "%s: restarting handshake..", __func__);
54751ae08745Sheppo 		break;
54761ae08745Sheppo 
54771ae08745Sheppo 	default:
54781ae08745Sheppo 		DERR(vswp, "%s: Unknown vio_subtype %x\n", __func__,
54791ae08745Sheppo 		    dring_pkt->tag.vio_subtype);
54801ae08745Sheppo 	}
54811ae08745Sheppo 
5482b071742bSsg70180 	vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
5483b071742bSsg70180 
54841ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
54851ae08745Sheppo }
54861ae08745Sheppo 
54871ae08745Sheppo #define	SND_MCST_NACK(ldcp, pkt) \
54881ae08745Sheppo 	pkt->tag.vio_subtype = VIO_SUBTYPE_NACK; \
54891ae08745Sheppo 	pkt->tag.vio_sid = ldcp->local_session; \
5490b071742bSsg70180 	(void) vsw_send_msg(ldcp, (void *)pkt, \
5491b071742bSsg70180 			sizeof (vnet_mcast_msg_t), B_TRUE);
54921ae08745Sheppo 
54931ae08745Sheppo /*
54941ae08745Sheppo  * Process a multicast request from a vnet.
54951ae08745Sheppo  *
54961ae08745Sheppo  * Vnet's specify a multicast address that they are interested in. This
54971ae08745Sheppo  * address is used as a key into the hash table which forms the multicast
54981ae08745Sheppo  * forwarding database (mFDB).
54991ae08745Sheppo  *
55001ae08745Sheppo  * The table keys are the multicast addresses, while the table entries
55011ae08745Sheppo  * are pointers to lists of ports which wish to receive packets for the
55021ae08745Sheppo  * specified multicast address.
55031ae08745Sheppo  *
55041ae08745Sheppo  * When a multicast packet is being switched we use the address as a key
55051ae08745Sheppo  * into the hash table, and then walk the appropriate port list forwarding
55061ae08745Sheppo  * the pkt to each port in turn.
55071ae08745Sheppo  *
55081ae08745Sheppo  * If a vnet is no longer interested in a particular multicast grouping
55091ae08745Sheppo  * we simply find the correct location in the hash table and then delete
55101ae08745Sheppo  * the relevant port from the port list.
55111ae08745Sheppo  *
55121ae08745Sheppo  * To deal with the case whereby a port is being deleted without first
55131ae08745Sheppo  * removing itself from the lists in the hash table, we maintain a list
55141ae08745Sheppo  * of multicast addresses the port has registered an interest in, within
55151ae08745Sheppo  * the port structure itself. We then simply walk that list of addresses
55161ae08745Sheppo  * using them as keys into the hash table and remove the port from the
55171ae08745Sheppo  * appropriate lists.
55181ae08745Sheppo  */
55191ae08745Sheppo static void
55201ae08745Sheppo vsw_process_ctrl_mcst_pkt(vsw_ldc_t *ldcp, void *pkt)
55211ae08745Sheppo {
55221ae08745Sheppo 	vnet_mcast_msg_t	*mcst_pkt;
55231ae08745Sheppo 	vsw_port_t		*port = ldcp->ldc_port;
55241ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
55251ae08745Sheppo 	int			i;
55261ae08745Sheppo 
55271ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
55281ae08745Sheppo 
55291ae08745Sheppo 	/*
55301ae08745Sheppo 	 * We know this is a ctrl/mcast packet so
55311ae08745Sheppo 	 * cast it into the correct structure.
55321ae08745Sheppo 	 */
55331ae08745Sheppo 	mcst_pkt = (vnet_mcast_msg_t *)pkt;
55341ae08745Sheppo 
55351ae08745Sheppo 	switch (mcst_pkt->tag.vio_subtype) {
55361ae08745Sheppo 	case VIO_SUBTYPE_INFO:
55371ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
55381ae08745Sheppo 
55391ae08745Sheppo 		/*
55401ae08745Sheppo 		 * Check if in correct state to receive a multicast
55411ae08745Sheppo 		 * message (i.e. handshake complete). If not reset
55421ae08745Sheppo 		 * the handshake.
55431ae08745Sheppo 		 */
55441ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_MCST_INFO_RECV))
55451ae08745Sheppo 			return;
55461ae08745Sheppo 
55471ae08745Sheppo 		/*
55481ae08745Sheppo 		 * Before attempting to add or remove address check
55491ae08745Sheppo 		 * that they are valid multicast addresses.
55501ae08745Sheppo 		 * If not, then NACK back.
55511ae08745Sheppo 		 */
55521ae08745Sheppo 		for (i = 0; i < mcst_pkt->count; i++) {
55531ae08745Sheppo 			if ((mcst_pkt->mca[i].ether_addr_octet[0] & 01) != 1) {
55541ae08745Sheppo 				DERR(vswp, "%s: invalid multicast address",
55551ae08745Sheppo 				    __func__);
55561ae08745Sheppo 				SND_MCST_NACK(ldcp, mcst_pkt);
55571ae08745Sheppo 				return;
55581ae08745Sheppo 			}
55591ae08745Sheppo 		}
55601ae08745Sheppo 
55611ae08745Sheppo 		/*
55621ae08745Sheppo 		 * Now add/remove the addresses. If this fails we
55631ae08745Sheppo 		 * NACK back.
55641ae08745Sheppo 		 */
55651ae08745Sheppo 		if (vsw_add_rem_mcst(mcst_pkt, port) != 0) {
55661ae08745Sheppo 			SND_MCST_NACK(ldcp, mcst_pkt);
55671ae08745Sheppo 			return;
55681ae08745Sheppo 		}
55691ae08745Sheppo 
55701ae08745Sheppo 		mcst_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
55711ae08745Sheppo 		mcst_pkt->tag.vio_sid = ldcp->local_session;
55721ae08745Sheppo 
55731ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)mcst_pkt);
55741ae08745Sheppo 
5575b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)mcst_pkt,
5576b071742bSsg70180 		    sizeof (vnet_mcast_msg_t), B_TRUE);
55771ae08745Sheppo 		break;
55781ae08745Sheppo 
55791ae08745Sheppo 	case VIO_SUBTYPE_ACK:
55801ae08745Sheppo 		DWARN(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
55811ae08745Sheppo 
55821ae08745Sheppo 		/*
55831ae08745Sheppo 		 * We shouldn't ever get a multicast ACK message as
55841ae08745Sheppo 		 * at the moment we never request multicast addresses
55851ae08745Sheppo 		 * to be set on some other device. This may change in
55861ae08745Sheppo 		 * the future if we have cascading switches.
55871ae08745Sheppo 		 */
55881ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_MCST_ACK_RECV))
55891ae08745Sheppo 			return;
55901ae08745Sheppo 
55911ae08745Sheppo 				/* Do nothing */
55921ae08745Sheppo 		break;
55931ae08745Sheppo 
55941ae08745Sheppo 	case VIO_SUBTYPE_NACK:
55951ae08745Sheppo 		DWARN(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
55961ae08745Sheppo 
55971ae08745Sheppo 		/*
55981ae08745Sheppo 		 * We shouldn't get a multicast NACK packet for the
55991ae08745Sheppo 		 * same reasons as we shouldn't get a ACK packet.
56001ae08745Sheppo 		 */
56011ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_MCST_NACK_RECV))
56021ae08745Sheppo 			return;
56031ae08745Sheppo 
56041ae08745Sheppo 				/* Do nothing */
56051ae08745Sheppo 		break;
56061ae08745Sheppo 
56071ae08745Sheppo 	default:
56081ae08745Sheppo 		DERR(vswp, "%s: unknown vio_subtype %x\n", __func__,
56091ae08745Sheppo 		    mcst_pkt->tag.vio_subtype);
56101ae08745Sheppo 	}
56111ae08745Sheppo 
56121ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
56131ae08745Sheppo }
56141ae08745Sheppo 
56151ae08745Sheppo static void
56161ae08745Sheppo vsw_process_ctrl_rdx_pkt(vsw_ldc_t *ldcp, void *pkt)
56171ae08745Sheppo {
56181ae08745Sheppo 	vio_rdx_msg_t	*rdx_pkt;
56191ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
56201ae08745Sheppo 
56211ae08745Sheppo 	/*
56221ae08745Sheppo 	 * We know this is a ctrl/rdx packet so
56231ae08745Sheppo 	 * cast it into the correct structure.
56241ae08745Sheppo 	 */
56251ae08745Sheppo 	rdx_pkt = (vio_rdx_msg_t *)pkt;
56261ae08745Sheppo 
56271ae08745Sheppo 	D1(vswp, "%s(%lld) enter", __func__, ldcp->ldc_id);
56281ae08745Sheppo 
56291ae08745Sheppo 	switch (rdx_pkt->tag.vio_subtype) {
56301ae08745Sheppo 	case VIO_SUBTYPE_INFO:
56311ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
56321ae08745Sheppo 
5633b071742bSsg70180 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_RDX_INFO_RECV))
56341ae08745Sheppo 			return;
56351ae08745Sheppo 
56361ae08745Sheppo 		rdx_pkt->tag.vio_sid = ldcp->local_session;
56371ae08745Sheppo 		rdx_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
56381ae08745Sheppo 
56391ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)rdx_pkt);
56401ae08745Sheppo 
5641b071742bSsg70180 		ldcp->lane_out.lstate |= VSW_RDX_ACK_SENT;
56421ae08745Sheppo 
5643b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)rdx_pkt,
5644b071742bSsg70180 		    sizeof (vio_rdx_msg_t), B_TRUE);
56451ae08745Sheppo 
56461ae08745Sheppo 		vsw_next_milestone(ldcp);
56471ae08745Sheppo 		break;
56481ae08745Sheppo 
56491ae08745Sheppo 	case VIO_SUBTYPE_ACK:
56501ae08745Sheppo 		/*
56511ae08745Sheppo 		 * Should be handled in-band by callback handler.
56521ae08745Sheppo 		 */
56531ae08745Sheppo 		DERR(vswp, "%s: Unexpected VIO_SUBTYPE_ACK", __func__);
5654b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
56551ae08745Sheppo 		break;
56561ae08745Sheppo 
56571ae08745Sheppo 	case VIO_SUBTYPE_NACK:
56581ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
56591ae08745Sheppo 
5660b071742bSsg70180 		if (vsw_check_flag(ldcp, INBOUND, VSW_RDX_NACK_RECV))
56611ae08745Sheppo 			return;
56621ae08745Sheppo 
5663b071742bSsg70180 		ldcp->lane_in.lstate |= VSW_RDX_NACK_RECV;
56641ae08745Sheppo 		vsw_next_milestone(ldcp);
56651ae08745Sheppo 		break;
56661ae08745Sheppo 
56671ae08745Sheppo 	default:
56681ae08745Sheppo 		DERR(vswp, "%s: Unknown vio_subtype %x\n", __func__,
56691ae08745Sheppo 		    rdx_pkt->tag.vio_subtype);
56701ae08745Sheppo 	}
56711ae08745Sheppo 
56721ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
56731ae08745Sheppo }
56741ae08745Sheppo 
56751ae08745Sheppo static void
56761ae08745Sheppo vsw_process_data_pkt(vsw_ldc_t *ldcp, void *dpkt, vio_msg_tag_t tag)
56771ae08745Sheppo {
56781ae08745Sheppo 	uint16_t	env = tag.vio_subtype_env;
56791ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
56801ae08745Sheppo 
56811ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
56821ae08745Sheppo 
56831ae08745Sheppo 	/* session id check */
56841ae08745Sheppo 	if (ldcp->session_status & VSW_PEER_SESSION) {
56851ae08745Sheppo 		if (ldcp->peer_session != tag.vio_sid) {
56861ae08745Sheppo 			DERR(vswp, "%s (chan %d): invalid session id (%llx)",
56871ae08745Sheppo 			    __func__, ldcp->ldc_id, tag.vio_sid);
5688b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
56891ae08745Sheppo 			return;
56901ae08745Sheppo 		}
56911ae08745Sheppo 	}
56921ae08745Sheppo 
56931ae08745Sheppo 	/*
56941ae08745Sheppo 	 * It is an error for us to be getting data packets
56951ae08745Sheppo 	 * before the handshake has completed.
56961ae08745Sheppo 	 */
56971ae08745Sheppo 	if (ldcp->hphase != VSW_MILESTONE4) {
56981ae08745Sheppo 		DERR(vswp, "%s: got data packet before handshake complete "
56991ae08745Sheppo 		    "hphase %d (%x: %x)", __func__, ldcp->hphase,
57001ae08745Sheppo 		    ldcp->lane_in.lstate, ldcp->lane_out.lstate);
57011ae08745Sheppo 		DUMP_FLAGS(ldcp->lane_in.lstate);
57021ae08745Sheppo 		DUMP_FLAGS(ldcp->lane_out.lstate);
5703b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
57041ae08745Sheppo 		return;
57051ae08745Sheppo 	}
57061ae08745Sheppo 
57071ae08745Sheppo 	/*
57081ae08745Sheppo 	 * Switch on vio_subtype envelope, then let lower routines
57091ae08745Sheppo 	 * decide if its an INFO, ACK or NACK packet.
57101ae08745Sheppo 	 */
57111ae08745Sheppo 	if (env == VIO_DRING_DATA) {
57121ae08745Sheppo 		vsw_process_data_dring_pkt(ldcp, dpkt);
57131ae08745Sheppo 	} else if (env == VIO_PKT_DATA) {
57141ae08745Sheppo 		vsw_process_data_raw_pkt(ldcp, dpkt);
57151ae08745Sheppo 	} else if (env == VIO_DESC_DATA) {
57161ae08745Sheppo 		vsw_process_data_ibnd_pkt(ldcp, dpkt);
57171ae08745Sheppo 	} else {
5718205eeb1aSlm66018 		DERR(vswp, "%s: unknown vio_subtype_env (%x)\n", __func__, env);
57191ae08745Sheppo 	}
57201ae08745Sheppo 
57211ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
57221ae08745Sheppo }
57231ae08745Sheppo 
57241ae08745Sheppo #define	SND_DRING_NACK(ldcp, pkt) \
57251ae08745Sheppo 	pkt->tag.vio_subtype = VIO_SUBTYPE_NACK; \
57261ae08745Sheppo 	pkt->tag.vio_sid = ldcp->local_session; \
5727b071742bSsg70180 	(void) vsw_send_msg(ldcp, (void *)pkt, \
5728b071742bSsg70180 			sizeof (vio_dring_msg_t), B_TRUE);
57291ae08745Sheppo 
57301ae08745Sheppo static void
57311ae08745Sheppo vsw_process_data_dring_pkt(vsw_ldc_t *ldcp, void *dpkt)
57321ae08745Sheppo {
57331ae08745Sheppo 	vio_dring_msg_t		*dring_pkt;
57341ae08745Sheppo 	vnet_public_desc_t	*pub_addr = NULL;
57351ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
57361ae08745Sheppo 	dring_info_t		*dp = NULL;
57371ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
57381ae08745Sheppo 	mblk_t			*mp = NULL;
57391ae08745Sheppo 	mblk_t			*bp = NULL;
57401ae08745Sheppo 	mblk_t			*bpt = NULL;
57411ae08745Sheppo 	size_t			nbytes = 0;
57421ae08745Sheppo 	size_t			off = 0;
57431ae08745Sheppo 	uint64_t		ncookies = 0;
57441ae08745Sheppo 	uint64_t		chain = 0;
5745d10e4ef2Snarayan 	uint64_t		j, len;
5746d10e4ef2Snarayan 	uint32_t		pos, start, datalen;
5747d10e4ef2Snarayan 	uint32_t		range_start, range_end;
5748d10e4ef2Snarayan 	int32_t			end, num, cnt = 0;
5749b071742bSsg70180 	int			i, rv, msg_rv = 0;
57501ae08745Sheppo 	boolean_t		ack_needed = B_FALSE;
5751d10e4ef2Snarayan 	boolean_t		prev_desc_ack = B_FALSE;
5752d10e4ef2Snarayan 	int			read_attempts = 0;
57531ae08745Sheppo 
57541ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
57551ae08745Sheppo 
57561ae08745Sheppo 	/*
57571ae08745Sheppo 	 * We know this is a data/dring packet so
57581ae08745Sheppo 	 * cast it into the correct structure.
57591ae08745Sheppo 	 */
57601ae08745Sheppo 	dring_pkt = (vio_dring_msg_t *)dpkt;
57611ae08745Sheppo 
57621ae08745Sheppo 	/*
57631ae08745Sheppo 	 * Switch on the vio_subtype. If its INFO then we need to
57641ae08745Sheppo 	 * process the data. If its an ACK we need to make sure
57651ae08745Sheppo 	 * it makes sense (i.e did we send an earlier data/info),
57661ae08745Sheppo 	 * and if its a NACK then we maybe attempt a retry.
57671ae08745Sheppo 	 */
57681ae08745Sheppo 	switch (dring_pkt->tag.vio_subtype) {
57691ae08745Sheppo 	case VIO_SUBTYPE_INFO:
57701ae08745Sheppo 		D2(vswp, "%s(%lld): VIO_SUBTYPE_INFO", __func__, ldcp->ldc_id);
57711ae08745Sheppo 
5772445b4c2eSsb155480 		READ_ENTER(&ldcp->lane_in.dlistrw);
57731ae08745Sheppo 		if ((dp = vsw_ident2dring(&ldcp->lane_in,
57741ae08745Sheppo 		    dring_pkt->dring_ident)) == NULL) {
5775445b4c2eSsb155480 			RW_EXIT(&ldcp->lane_in.dlistrw);
57761ae08745Sheppo 
57771ae08745Sheppo 			DERR(vswp, "%s(%lld): unable to find dring from "
57781ae08745Sheppo 			    "ident 0x%llx", __func__, ldcp->ldc_id,
57791ae08745Sheppo 			    dring_pkt->dring_ident);
57801ae08745Sheppo 
57811ae08745Sheppo 			SND_DRING_NACK(ldcp, dring_pkt);
57821ae08745Sheppo 			return;
57831ae08745Sheppo 		}
57841ae08745Sheppo 
5785d10e4ef2Snarayan 		start = pos = dring_pkt->start_idx;
57861ae08745Sheppo 		end = dring_pkt->end_idx;
5787d10e4ef2Snarayan 		len = dp->num_descriptors;
57881ae08745Sheppo 
5789d10e4ef2Snarayan 		range_start = range_end = pos;
5790d10e4ef2Snarayan 
5791d10e4ef2Snarayan 		D2(vswp, "%s(%lld): start index %ld : end %ld\n",
57921ae08745Sheppo 		    __func__, ldcp->ldc_id, start, end);
57931ae08745Sheppo 
5794d10e4ef2Snarayan 		if (end == -1) {
5795d10e4ef2Snarayan 			num = -1;
57964bac2208Snarayan 		} else if (end >= 0) {
5797205eeb1aSlm66018 			num = end >= pos ? end - pos + 1: (len - pos + 1) + end;
5798d10e4ef2Snarayan 
57991ae08745Sheppo 			/* basic sanity check */
58001ae08745Sheppo 			if (end > len) {
5801445b4c2eSsb155480 				RW_EXIT(&ldcp->lane_in.dlistrw);
5802d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): endpoint %lld outside "
5803d10e4ef2Snarayan 				    "ring length %lld", __func__,
5804d10e4ef2Snarayan 				    ldcp->ldc_id, end, len);
58051ae08745Sheppo 
58061ae08745Sheppo 				SND_DRING_NACK(ldcp, dring_pkt);
58071ae08745Sheppo 				return;
58081ae08745Sheppo 			}
5809d10e4ef2Snarayan 		} else {
5810445b4c2eSsb155480 			RW_EXIT(&ldcp->lane_in.dlistrw);
5811d10e4ef2Snarayan 			DERR(vswp, "%s(%lld): invalid endpoint %lld",
5812d10e4ef2Snarayan 			    __func__, ldcp->ldc_id, end);
5813d10e4ef2Snarayan 			SND_DRING_NACK(ldcp, dring_pkt);
58141ae08745Sheppo 			return;
58151ae08745Sheppo 		}
58161ae08745Sheppo 
5817d10e4ef2Snarayan 		while (cnt != num) {
5818d10e4ef2Snarayan vsw_recheck_desc:
5819d10e4ef2Snarayan 			if ((rv = ldc_mem_dring_acquire(dp->handle,
5820d10e4ef2Snarayan 			    pos, pos)) != 0) {
5821445b4c2eSsb155480 				RW_EXIT(&ldcp->lane_in.dlistrw);
5822d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): unable to acquire "
5823d10e4ef2Snarayan 				    "descriptor at pos %d: err %d",
5824d10e4ef2Snarayan 				    __func__, pos, ldcp->ldc_id, rv);
5825d10e4ef2Snarayan 				SND_DRING_NACK(ldcp, dring_pkt);
5826d10e4ef2Snarayan 				return;
5827d10e4ef2Snarayan 			}
58281ae08745Sheppo 
5829d10e4ef2Snarayan 			pub_addr = (vnet_public_desc_t *)dp->pub_addr + pos;
58301ae08745Sheppo 
5831d10e4ef2Snarayan 			/*
5832d10e4ef2Snarayan 			 * When given a bounded range of descriptors
5833d10e4ef2Snarayan 			 * to process, its an error to hit a descriptor
5834d10e4ef2Snarayan 			 * which is not ready. In the non-bounded case
5835d10e4ef2Snarayan 			 * (end_idx == -1) this simply indicates we have
5836d10e4ef2Snarayan 			 * reached the end of the current active range.
5837d10e4ef2Snarayan 			 */
5838d10e4ef2Snarayan 			if (pub_addr->hdr.dstate != VIO_DESC_READY) {
5839d10e4ef2Snarayan 				/* unbound - no error */
5840d10e4ef2Snarayan 				if (end == -1) {
5841d10e4ef2Snarayan 					if (read_attempts == vsw_read_attempts)
5842d10e4ef2Snarayan 						break;
58431ae08745Sheppo 
5844d10e4ef2Snarayan 					delay(drv_usectohz(vsw_desc_delay));
5845d10e4ef2Snarayan 					read_attempts++;
5846d10e4ef2Snarayan 					goto vsw_recheck_desc;
5847d10e4ef2Snarayan 				}
58481ae08745Sheppo 
5849d10e4ef2Snarayan 				/* bounded - error - so NACK back */
5850445b4c2eSsb155480 				RW_EXIT(&ldcp->lane_in.dlistrw);
5851d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): descriptor not READY "
5852d10e4ef2Snarayan 				    "(%d)", __func__, ldcp->ldc_id,
5853d10e4ef2Snarayan 				    pub_addr->hdr.dstate);
5854d10e4ef2Snarayan 				SND_DRING_NACK(ldcp, dring_pkt);
5855d10e4ef2Snarayan 				return;
5856d10e4ef2Snarayan 			}
5857d10e4ef2Snarayan 
5858d10e4ef2Snarayan 			DTRACE_PROBE1(read_attempts, int, read_attempts);
5859d10e4ef2Snarayan 
5860d10e4ef2Snarayan 			range_end = pos;
5861d10e4ef2Snarayan 
5862d10e4ef2Snarayan 			/*
5863d10e4ef2Snarayan 			 * If we ACK'd the previous descriptor then now
5864d10e4ef2Snarayan 			 * record the new range start position for later
5865d10e4ef2Snarayan 			 * ACK's.
5866d10e4ef2Snarayan 			 */
5867d10e4ef2Snarayan 			if (prev_desc_ack) {
5868d10e4ef2Snarayan 				range_start = pos;
5869d10e4ef2Snarayan 
5870205eeb1aSlm66018 				D2(vswp, "%s(%lld): updating range start to be "
5871205eeb1aSlm66018 				    "%d", __func__, ldcp->ldc_id, range_start);
5872d10e4ef2Snarayan 
5873d10e4ef2Snarayan 				prev_desc_ack = B_FALSE;
5874d10e4ef2Snarayan 			}
58751ae08745Sheppo 
58761ae08745Sheppo 			/*
58771ae08745Sheppo 			 * Data is padded to align on 8 byte boundary,
58781ae08745Sheppo 			 * datalen is actual data length, i.e. minus that
58791ae08745Sheppo 			 * padding.
58801ae08745Sheppo 			 */
58811ae08745Sheppo 			datalen = pub_addr->nbytes;
58821ae08745Sheppo 
58831ae08745Sheppo 			/*
58841ae08745Sheppo 			 * Does peer wish us to ACK when we have finished
58851ae08745Sheppo 			 * with this descriptor ?
58861ae08745Sheppo 			 */
58871ae08745Sheppo 			if (pub_addr->hdr.ack)
58881ae08745Sheppo 				ack_needed = B_TRUE;
58891ae08745Sheppo 
58901ae08745Sheppo 			D2(vswp, "%s(%lld): processing desc %lld at pos"
58911ae08745Sheppo 			    " 0x%llx : dstate 0x%lx : datalen 0x%lx",
5892d10e4ef2Snarayan 			    __func__, ldcp->ldc_id, pos, pub_addr,
58931ae08745Sheppo 			    pub_addr->hdr.dstate, datalen);
58941ae08745Sheppo 
58951ae08745Sheppo 			/*
58961ae08745Sheppo 			 * Mark that we are starting to process descriptor.
58971ae08745Sheppo 			 */
58981ae08745Sheppo 			pub_addr->hdr.dstate = VIO_DESC_ACCEPTED;
58991ae08745Sheppo 
5900d10e4ef2Snarayan 			mp = vio_allocb(ldcp->rxh);
5901d10e4ef2Snarayan 			if (mp == NULL) {
59021ae08745Sheppo 				/*
5903d10e4ef2Snarayan 				 * No free receive buffers available, so
5904d10e4ef2Snarayan 				 * fallback onto allocb(9F). Make sure that
5905d10e4ef2Snarayan 				 * we get a data buffer which is a multiple
5906d10e4ef2Snarayan 				 * of 8 as this is required by ldc_mem_copy.
59071ae08745Sheppo 				 */
5908d10e4ef2Snarayan 				DTRACE_PROBE(allocb);
5909205eeb1aSlm66018 				if ((mp = allocb(datalen + VNET_IPALIGN + 8,
5910205eeb1aSlm66018 				    BPRI_MED)) == NULL) {
5911205eeb1aSlm66018 					DERR(vswp, "%s(%ld): allocb failed",
5912205eeb1aSlm66018 					    __func__, ldcp->ldc_id);
5913205eeb1aSlm66018 					pub_addr->hdr.dstate = VIO_DESC_DONE;
5914205eeb1aSlm66018 					(void) ldc_mem_dring_release(dp->handle,
5915205eeb1aSlm66018 					    pos, pos);
5916205eeb1aSlm66018 					break;
5917205eeb1aSlm66018 				}
5918d10e4ef2Snarayan 			}
5919d10e4ef2Snarayan 
5920d10e4ef2Snarayan 			/*
5921d10e4ef2Snarayan 			 * Ensure that we ask ldc for an aligned
5922d10e4ef2Snarayan 			 * number of bytes.
5923d10e4ef2Snarayan 			 */
5924d10e4ef2Snarayan 			nbytes = datalen + VNET_IPALIGN;
59251ae08745Sheppo 			if (nbytes & 0x7) {
59261ae08745Sheppo 				off = 8 - (nbytes & 0x7);
59271ae08745Sheppo 				nbytes += off;
59281ae08745Sheppo 			}
59291ae08745Sheppo 
59301ae08745Sheppo 			ncookies = pub_addr->ncookies;
59311ae08745Sheppo 			rv = ldc_mem_copy(ldcp->ldc_handle,
59321ae08745Sheppo 			    (caddr_t)mp->b_rptr, 0, &nbytes,
5933205eeb1aSlm66018 			    pub_addr->memcookie, ncookies, LDC_COPY_IN);
59341ae08745Sheppo 
59351ae08745Sheppo 			if (rv != 0) {
5936205eeb1aSlm66018 				DERR(vswp, "%s(%d): unable to copy in data "
5937205eeb1aSlm66018 				    "from %d cookies in desc %d (rv %d)",
5938205eeb1aSlm66018 				    __func__, ldcp->ldc_id, ncookies, pos, rv);
59391ae08745Sheppo 				freemsg(mp);
5940d10e4ef2Snarayan 
5941d10e4ef2Snarayan 				pub_addr->hdr.dstate = VIO_DESC_DONE;
59421ae08745Sheppo 				(void) ldc_mem_dring_release(dp->handle,
5943d10e4ef2Snarayan 				    pos, pos);
5944d10e4ef2Snarayan 				break;
59451ae08745Sheppo 			} else {
59461ae08745Sheppo 				D2(vswp, "%s(%d): copied in %ld bytes"
59471ae08745Sheppo 				    " using %d cookies", __func__,
59481ae08745Sheppo 				    ldcp->ldc_id, nbytes, ncookies);
59491ae08745Sheppo 			}
59501ae08745Sheppo 
5951d10e4ef2Snarayan 			/* adjust the read pointer to skip over the padding */
5952d10e4ef2Snarayan 			mp->b_rptr += VNET_IPALIGN;
5953d10e4ef2Snarayan 
59541ae08745Sheppo 			/* point to the actual end of data */
59551ae08745Sheppo 			mp->b_wptr = mp->b_rptr + datalen;
59561ae08745Sheppo 
59571ae08745Sheppo 			/* build a chain of received packets */
59581ae08745Sheppo 			if (bp == NULL) {
59591ae08745Sheppo 				/* first pkt */
59601ae08745Sheppo 				bp = mp;
59611ae08745Sheppo 				bp->b_next = bp->b_prev = NULL;
59621ae08745Sheppo 				bpt = bp;
59631ae08745Sheppo 				chain = 1;
59641ae08745Sheppo 			} else {
59651ae08745Sheppo 				mp->b_next = NULL;
59661ae08745Sheppo 				mp->b_prev = bpt;
59671ae08745Sheppo 				bpt->b_next = mp;
59681ae08745Sheppo 				bpt = mp;
59691ae08745Sheppo 				chain++;
59701ae08745Sheppo 			}
59711ae08745Sheppo 
59721ae08745Sheppo 			/* mark we are finished with this descriptor */
59731ae08745Sheppo 			pub_addr->hdr.dstate = VIO_DESC_DONE;
59741ae08745Sheppo 
5975d10e4ef2Snarayan 			(void) ldc_mem_dring_release(dp->handle, pos, pos);
5976d10e4ef2Snarayan 
59771ae08745Sheppo 			/*
5978d10e4ef2Snarayan 			 * Send an ACK back to peer if requested.
59791ae08745Sheppo 			 */
59801ae08745Sheppo 			if (ack_needed) {
59811ae08745Sheppo 				ack_needed = B_FALSE;
59821ae08745Sheppo 
5983d10e4ef2Snarayan 				dring_pkt->start_idx = range_start;
5984d10e4ef2Snarayan 				dring_pkt->end_idx = range_end;
59851ae08745Sheppo 
5986d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): processed %d %d, ACK"
5987d10e4ef2Snarayan 				    " requested", __func__, ldcp->ldc_id,
5988205eeb1aSlm66018 				    dring_pkt->start_idx, dring_pkt->end_idx);
59891ae08745Sheppo 
5990d10e4ef2Snarayan 				dring_pkt->dring_process_state = VIO_DP_ACTIVE;
59911ae08745Sheppo 				dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
59921ae08745Sheppo 				dring_pkt->tag.vio_sid = ldcp->local_session;
5993205eeb1aSlm66018 
5994b071742bSsg70180 				msg_rv = vsw_send_msg(ldcp, (void *)dring_pkt,
5995205eeb1aSlm66018 				    sizeof (vio_dring_msg_t), B_FALSE);
5996b071742bSsg70180 
5997b071742bSsg70180 				/*
5998b071742bSsg70180 				 * Check if ACK was successfully sent. If not
5999b071742bSsg70180 				 * we break and deal with that below.
6000b071742bSsg70180 				 */
6001b071742bSsg70180 				if (msg_rv != 0)
6002b071742bSsg70180 					break;
6003d10e4ef2Snarayan 
6004d10e4ef2Snarayan 				prev_desc_ack = B_TRUE;
6005d10e4ef2Snarayan 				range_start = pos;
60061ae08745Sheppo 			}
60071ae08745Sheppo 
6008d10e4ef2Snarayan 			/* next descriptor */
6009d10e4ef2Snarayan 			pos = (pos + 1) % len;
6010d10e4ef2Snarayan 			cnt++;
6011d10e4ef2Snarayan 
6012d10e4ef2Snarayan 			/*
6013d10e4ef2Snarayan 			 * Break out of loop here and stop processing to
6014d10e4ef2Snarayan 			 * allow some other network device (or disk) to
6015d10e4ef2Snarayan 			 * get access to the cpu.
6016d10e4ef2Snarayan 			 */
6017d10e4ef2Snarayan 			if (chain > vsw_chain_len) {
6018d10e4ef2Snarayan 				D3(vswp, "%s(%lld): switching chain of %d "
6019d10e4ef2Snarayan 				    "msgs", __func__, ldcp->ldc_id, chain);
6020d10e4ef2Snarayan 				break;
60211ae08745Sheppo 			}
60221ae08745Sheppo 		}
6023445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_in.dlistrw);
60241ae08745Sheppo 
6025b071742bSsg70180 		/*
6026b071742bSsg70180 		 * If when we attempted to send the ACK we found that the
6027b071742bSsg70180 		 * channel had been reset then now handle this. We deal with
6028b071742bSsg70180 		 * it here as we cannot reset the channel while holding the
6029b071742bSsg70180 		 * dlistrw lock, and we don't want to acquire/release it
6030b071742bSsg70180 		 * continuously in the above loop, as a channel reset should
6031b071742bSsg70180 		 * be a rare event.
6032b071742bSsg70180 		 */
6033b071742bSsg70180 		if (msg_rv == ECONNRESET) {
6034b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
6035b071742bSsg70180 			break;
6036b071742bSsg70180 		}
6037b071742bSsg70180 
60381ae08745Sheppo 		/* send the chain of packets to be switched */
6039d10e4ef2Snarayan 		if (bp != NULL) {
6040d10e4ef2Snarayan 			D3(vswp, "%s(%lld): switching chain of %d msgs",
6041d10e4ef2Snarayan 			    __func__, ldcp->ldc_id, chain);
604234683adeSsg70180 			vswp->vsw_switch_frame(vswp, bp, VSW_VNETPORT,
60431ae08745Sheppo 			    ldcp->ldc_port, NULL);
6044d10e4ef2Snarayan 		}
60451ae08745Sheppo 
6046d10e4ef2Snarayan 		DTRACE_PROBE1(msg_cnt, int, cnt);
6047d10e4ef2Snarayan 
6048d10e4ef2Snarayan 		/*
6049d10e4ef2Snarayan 		 * We are now finished so ACK back with the state
6050d10e4ef2Snarayan 		 * set to STOPPING so our peer knows we are finished
6051d10e4ef2Snarayan 		 */
6052d10e4ef2Snarayan 		dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
6053d10e4ef2Snarayan 		dring_pkt->tag.vio_sid = ldcp->local_session;
6054d10e4ef2Snarayan 
6055d10e4ef2Snarayan 		dring_pkt->dring_process_state = VIO_DP_STOPPED;
6056d10e4ef2Snarayan 
6057d10e4ef2Snarayan 		DTRACE_PROBE(stop_process_sent);
6058d10e4ef2Snarayan 
6059d10e4ef2Snarayan 		/*
6060d10e4ef2Snarayan 		 * We have not processed any more descriptors beyond
6061d10e4ef2Snarayan 		 * the last one we ACK'd.
6062d10e4ef2Snarayan 		 */
6063d10e4ef2Snarayan 		if (prev_desc_ack)
6064d10e4ef2Snarayan 			range_start = range_end;
6065d10e4ef2Snarayan 
6066d10e4ef2Snarayan 		dring_pkt->start_idx = range_start;
6067d10e4ef2Snarayan 		dring_pkt->end_idx = range_end;
6068d10e4ef2Snarayan 
6069d10e4ef2Snarayan 		D2(vswp, "%s(%lld) processed : %d : %d, now stopping",
6070d10e4ef2Snarayan 		    __func__, ldcp->ldc_id, dring_pkt->start_idx,
6071d10e4ef2Snarayan 		    dring_pkt->end_idx);
6072d10e4ef2Snarayan 
6073b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)dring_pkt,
6074b071742bSsg70180 		    sizeof (vio_dring_msg_t), B_TRUE);
60751ae08745Sheppo 		break;
60761ae08745Sheppo 
60771ae08745Sheppo 	case VIO_SUBTYPE_ACK:
60781ae08745Sheppo 		D2(vswp, "%s(%lld): VIO_SUBTYPE_ACK", __func__, ldcp->ldc_id);
60791ae08745Sheppo 		/*
60801ae08745Sheppo 		 * Verify that the relevant descriptors are all
60811ae08745Sheppo 		 * marked as DONE
60821ae08745Sheppo 		 */
6083445b4c2eSsb155480 		READ_ENTER(&ldcp->lane_out.dlistrw);
60841ae08745Sheppo 		if ((dp = vsw_ident2dring(&ldcp->lane_out,
60851ae08745Sheppo 		    dring_pkt->dring_ident)) == NULL) {
6086445b4c2eSsb155480 			RW_EXIT(&ldcp->lane_out.dlistrw);
60871ae08745Sheppo 			DERR(vswp, "%s: unknown ident in ACK", __func__);
60881ae08745Sheppo 			return;
60891ae08745Sheppo 		}
60901ae08745Sheppo 
60911ae08745Sheppo 		pub_addr = (vnet_public_desc_t *)dp->pub_addr;
60921ae08745Sheppo 		priv_addr = (vsw_private_desc_t *)dp->priv_addr;
60931ae08745Sheppo 
60941ae08745Sheppo 		start = end = 0;
60951ae08745Sheppo 		start = dring_pkt->start_idx;
60961ae08745Sheppo 		end = dring_pkt->end_idx;
60971ae08745Sheppo 		len = dp->num_descriptors;
60981ae08745Sheppo 
60991ae08745Sheppo 		j = num = 0;
61001ae08745Sheppo 		/* calculate # descriptors taking into a/c wrap around */
61011ae08745Sheppo 		num = end >= start ? end - start + 1: (len - start + 1) + end;
61021ae08745Sheppo 
61031ae08745Sheppo 		D2(vswp, "%s(%lld): start index %ld : end %ld : num %ld\n",
61041ae08745Sheppo 		    __func__, ldcp->ldc_id, start, end, num);
61051ae08745Sheppo 
6106d10e4ef2Snarayan 		mutex_enter(&dp->dlock);
6107d10e4ef2Snarayan 		dp->last_ack_recv = end;
6108d10e4ef2Snarayan 		mutex_exit(&dp->dlock);
6109d10e4ef2Snarayan 
61101ae08745Sheppo 		for (i = start; j < num; i = (i + 1) % len, j++) {
61111ae08745Sheppo 			pub_addr = (vnet_public_desc_t *)dp->pub_addr + i;
61121ae08745Sheppo 			priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
61131ae08745Sheppo 
6114d10e4ef2Snarayan 			/*
6115d10e4ef2Snarayan 			 * If the last descriptor in a range has the ACK
6116d10e4ef2Snarayan 			 * bit set then we will get two messages from our
6117d10e4ef2Snarayan 			 * peer relating to it. The normal ACK msg and then
6118d10e4ef2Snarayan 			 * a subsequent STOP msg. The first message will have
6119d10e4ef2Snarayan 			 * resulted in the descriptor being reclaimed and
6120d10e4ef2Snarayan 			 * its state set to FREE so when we encounter a non
6121d10e4ef2Snarayan 			 * DONE descriptor we need to check to see if its
6122d10e4ef2Snarayan 			 * because we have just reclaimed it.
6123d10e4ef2Snarayan 			 */
6124d10e4ef2Snarayan 			mutex_enter(&priv_addr->dstate_lock);
6125d10e4ef2Snarayan 			if (pub_addr->hdr.dstate == VIO_DESC_DONE) {
61261ae08745Sheppo 				/* clear all the fields */
61271ae08745Sheppo 				bzero(priv_addr->datap, priv_addr->datalen);
61281ae08745Sheppo 				priv_addr->datalen = 0;
61291ae08745Sheppo 
61301ae08745Sheppo 				pub_addr->hdr.dstate = VIO_DESC_FREE;
61311ae08745Sheppo 				pub_addr->hdr.ack = 0;
6132d10e4ef2Snarayan 
61331ae08745Sheppo 				priv_addr->dstate = VIO_DESC_FREE;
6134d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
61351ae08745Sheppo 
61361ae08745Sheppo 				D3(vswp, "clearing descp %d : pub state "
61371ae08745Sheppo 				    "0x%llx : priv state 0x%llx", i,
6138205eeb1aSlm66018 				    pub_addr->hdr.dstate, priv_addr->dstate);
6139d10e4ef2Snarayan 
6140d10e4ef2Snarayan 			} else {
6141d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
6142d10e4ef2Snarayan 
6143d10e4ef2Snarayan 				if (dring_pkt->dring_process_state !=
6144d10e4ef2Snarayan 				    VIO_DP_STOPPED) {
6145d10e4ef2Snarayan 					DERR(vswp, "%s: descriptor %lld at pos "
6146d10e4ef2Snarayan 					    " 0x%llx not DONE (0x%lx)\n",
6147d10e4ef2Snarayan 					    __func__, i, pub_addr,
6148d10e4ef2Snarayan 					    pub_addr->hdr.dstate);
6149445b4c2eSsb155480 					RW_EXIT(&ldcp->lane_out.dlistrw);
6150d10e4ef2Snarayan 					return;
6151d10e4ef2Snarayan 				}
61521ae08745Sheppo 			}
61531ae08745Sheppo 		}
61541ae08745Sheppo 
6155d10e4ef2Snarayan 		/*
6156d10e4ef2Snarayan 		 * If our peer is stopping processing descriptors then
6157d10e4ef2Snarayan 		 * we check to make sure it has processed all the descriptors
6158d10e4ef2Snarayan 		 * we have updated. If not then we send it a new message
6159d10e4ef2Snarayan 		 * to prompt it to restart.
6160d10e4ef2Snarayan 		 */
6161d10e4ef2Snarayan 		if (dring_pkt->dring_process_state == VIO_DP_STOPPED) {
6162d10e4ef2Snarayan 			DTRACE_PROBE(stop_process_recv);
6163d10e4ef2Snarayan 			D2(vswp, "%s(%lld): got stopping msg : %d : %d",
6164d10e4ef2Snarayan 			    __func__, ldcp->ldc_id, dring_pkt->start_idx,
6165d10e4ef2Snarayan 			    dring_pkt->end_idx);
6166d10e4ef2Snarayan 
6167d10e4ef2Snarayan 			/*
6168d10e4ef2Snarayan 			 * Check next descriptor in public section of ring.
6169d10e4ef2Snarayan 			 * If its marked as READY then we need to prompt our
6170d10e4ef2Snarayan 			 * peer to start processing the ring again.
6171d10e4ef2Snarayan 			 */
6172d10e4ef2Snarayan 			i = (end + 1) % len;
6173d10e4ef2Snarayan 			pub_addr = (vnet_public_desc_t *)dp->pub_addr + i;
6174d10e4ef2Snarayan 			priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
6175d10e4ef2Snarayan 
6176d10e4ef2Snarayan 			/*
6177d10e4ef2Snarayan 			 * Hold the restart lock across all of this to
6178d10e4ef2Snarayan 			 * make sure that its not possible for us to
6179d10e4ef2Snarayan 			 * decide that a msg needs to be sent in the future
6180d10e4ef2Snarayan 			 * but the sending code having already checked is
6181d10e4ef2Snarayan 			 * about to exit.
6182d10e4ef2Snarayan 			 */
6183d10e4ef2Snarayan 			mutex_enter(&dp->restart_lock);
6184d10e4ef2Snarayan 			mutex_enter(&priv_addr->dstate_lock);
6185d10e4ef2Snarayan 			if (pub_addr->hdr.dstate == VIO_DESC_READY) {
6186d10e4ef2Snarayan 
6187d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
6188d10e4ef2Snarayan 
6189d10e4ef2Snarayan 				dring_pkt->tag.vio_subtype = VIO_SUBTYPE_INFO;
6190d10e4ef2Snarayan 				dring_pkt->tag.vio_sid = ldcp->local_session;
6191d10e4ef2Snarayan 
6192d10e4ef2Snarayan 				mutex_enter(&ldcp->lane_out.seq_lock);
6193d10e4ef2Snarayan 				dring_pkt->seq_num = ldcp->lane_out.seq_num++;
6194d10e4ef2Snarayan 				mutex_exit(&ldcp->lane_out.seq_lock);
6195d10e4ef2Snarayan 
6196d10e4ef2Snarayan 				dring_pkt->start_idx = (end + 1) % len;
6197d10e4ef2Snarayan 				dring_pkt->end_idx = -1;
6198d10e4ef2Snarayan 
6199d10e4ef2Snarayan 				D2(vswp, "%s(%lld) : sending restart msg:"
6200d10e4ef2Snarayan 				    " %d : %d", __func__, ldcp->ldc_id,
6201205eeb1aSlm66018 				    dring_pkt->start_idx, dring_pkt->end_idx);
6202d10e4ef2Snarayan 
6203b071742bSsg70180 				msg_rv = vsw_send_msg(ldcp, (void *)dring_pkt,
6204b071742bSsg70180 				    sizeof (vio_dring_msg_t), B_FALSE);
6205b071742bSsg70180 
6206d10e4ef2Snarayan 			} else {
6207d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
6208d10e4ef2Snarayan 				dp->restart_reqd = B_TRUE;
6209d10e4ef2Snarayan 			}
6210d10e4ef2Snarayan 			mutex_exit(&dp->restart_lock);
6211d10e4ef2Snarayan 		}
6212445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_out.dlistrw);
6213b071742bSsg70180 
6214b071742bSsg70180 		/* only do channel reset after dropping dlistrw lock */
6215b071742bSsg70180 		if (msg_rv == ECONNRESET)
6216b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
6217b071742bSsg70180 
62181ae08745Sheppo 		break;
62191ae08745Sheppo 
62201ae08745Sheppo 	case VIO_SUBTYPE_NACK:
62211ae08745Sheppo 		DWARN(vswp, "%s(%lld): VIO_SUBTYPE_NACK",
62221ae08745Sheppo 		    __func__, ldcp->ldc_id);
62231ae08745Sheppo 		/*
62241ae08745Sheppo 		 * Something is badly wrong if we are getting NACK's
62251ae08745Sheppo 		 * for our data pkts. So reset the channel.
62261ae08745Sheppo 		 */
6227b071742bSsg70180 		vsw_process_conn_evt(ldcp, VSW_CONN_RESTART);
62281ae08745Sheppo 
62291ae08745Sheppo 		break;
62301ae08745Sheppo 
62311ae08745Sheppo 	default:
62321ae08745Sheppo 		DERR(vswp, "%s(%lld): Unknown vio_subtype %x\n", __func__,
62331ae08745Sheppo 		    ldcp->ldc_id, dring_pkt->tag.vio_subtype);
62341ae08745Sheppo 	}
62351ae08745Sheppo 
62361ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
62371ae08745Sheppo }
62381ae08745Sheppo 
62391ae08745Sheppo /*
62401ae08745Sheppo  * VIO_PKT_DATA (a.k.a raw data mode )
62411ae08745Sheppo  *
62421ae08745Sheppo  * Note - currently not supported. Do nothing.
62431ae08745Sheppo  */
62441ae08745Sheppo static void
62451ae08745Sheppo vsw_process_data_raw_pkt(vsw_ldc_t *ldcp, void *dpkt)
62461ae08745Sheppo {
62471ae08745Sheppo 	_NOTE(ARGUNUSED(dpkt))
62481ae08745Sheppo 
62491ae08745Sheppo 	D1(NULL, "%s (%lld): enter\n", __func__, ldcp->ldc_id);
6250205eeb1aSlm66018 	DERR(NULL, "%s (%lld): currently unsupported", __func__, ldcp->ldc_id);
62511ae08745Sheppo 	D1(NULL, "%s (%lld): exit\n", __func__, ldcp->ldc_id);
62521ae08745Sheppo }
62531ae08745Sheppo 
62541ae08745Sheppo /*
62551ae08745Sheppo  * Process an in-band descriptor message (most likely from
62561ae08745Sheppo  * OBP).
62571ae08745Sheppo  */
62581ae08745Sheppo static void
62591ae08745Sheppo vsw_process_data_ibnd_pkt(vsw_ldc_t *ldcp, void *pkt)
62601ae08745Sheppo {
6261445b4c2eSsb155480 	vnet_ibnd_desc_t	*ibnd_desc;
62621ae08745Sheppo 	dring_info_t		*dp = NULL;
62631ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
62641ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
62651ae08745Sheppo 	mblk_t			*mp = NULL;
62661ae08745Sheppo 	size_t			nbytes = 0;
62671ae08745Sheppo 	size_t			off = 0;
62681ae08745Sheppo 	uint64_t		idx = 0;
62694bac2208Snarayan 	uint32_t		num = 1, len, datalen = 0;
62701ae08745Sheppo 	uint64_t		ncookies = 0;
62714bac2208Snarayan 	int			i, rv;
62724bac2208Snarayan 	int			j = 0;
62731ae08745Sheppo 
62741ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
62751ae08745Sheppo 
6276445b4c2eSsb155480 	ibnd_desc = (vnet_ibnd_desc_t *)pkt;
62771ae08745Sheppo 
62781ae08745Sheppo 	switch (ibnd_desc->hdr.tag.vio_subtype) {
62791ae08745Sheppo 	case VIO_SUBTYPE_INFO:
62801ae08745Sheppo 		D1(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
62811ae08745Sheppo 
62821ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_DRING_INFO_RECV))
62831ae08745Sheppo 			return;
62841ae08745Sheppo 
62851ae08745Sheppo 		/*
62861ae08745Sheppo 		 * Data is padded to align on a 8 byte boundary,
62871ae08745Sheppo 		 * nbytes is actual data length, i.e. minus that
62881ae08745Sheppo 		 * padding.
62891ae08745Sheppo 		 */
62901ae08745Sheppo 		datalen = ibnd_desc->nbytes;
62911ae08745Sheppo 
62921ae08745Sheppo 		D2(vswp, "%s(%lld): processing inband desc : "
62931ae08745Sheppo 		    ": datalen 0x%lx", __func__, ldcp->ldc_id, datalen);
62941ae08745Sheppo 
62951ae08745Sheppo 		ncookies = ibnd_desc->ncookies;
62961ae08745Sheppo 
62971ae08745Sheppo 		/*
62981ae08745Sheppo 		 * allocb(9F) returns an aligned data block. We
62991ae08745Sheppo 		 * need to ensure that we ask ldc for an aligned
63001ae08745Sheppo 		 * number of bytes also.
63011ae08745Sheppo 		 */
63021ae08745Sheppo 		nbytes = datalen;
63031ae08745Sheppo 		if (nbytes & 0x7) {
63041ae08745Sheppo 			off = 8 - (nbytes & 0x7);
63051ae08745Sheppo 			nbytes += off;
63061ae08745Sheppo 		}
63071ae08745Sheppo 
63081ae08745Sheppo 		mp = allocb(datalen, BPRI_MED);
63091ae08745Sheppo 		if (mp == NULL) {
63101ae08745Sheppo 			DERR(vswp, "%s(%lld): allocb failed",
63111ae08745Sheppo 			    __func__, ldcp->ldc_id);
63121ae08745Sheppo 			return;
63131ae08745Sheppo 		}
63141ae08745Sheppo 
63151ae08745Sheppo 		rv = ldc_mem_copy(ldcp->ldc_handle, (caddr_t)mp->b_rptr,
63161ae08745Sheppo 		    0, &nbytes, ibnd_desc->memcookie, (uint64_t)ncookies,
63171ae08745Sheppo 		    LDC_COPY_IN);
63181ae08745Sheppo 
63191ae08745Sheppo 		if (rv != 0) {
63201ae08745Sheppo 			DERR(vswp, "%s(%d): unable to copy in data from "
6321205eeb1aSlm66018 			    "%d cookie(s)", __func__, ldcp->ldc_id, ncookies);
63221ae08745Sheppo 			freemsg(mp);
63231ae08745Sheppo 			return;
6324023505bcSraghuram 		}
6325023505bcSraghuram 
6326205eeb1aSlm66018 		D2(vswp, "%s(%d): copied in %ld bytes using %d cookies",
6327205eeb1aSlm66018 		    __func__, ldcp->ldc_id, nbytes, ncookies);
6328023505bcSraghuram 
63291ae08745Sheppo 		/* point to the actual end of data */
6330*da86a4daSrf157361 		mp->b_wptr = mp->b_rptr + datalen;
63311ae08745Sheppo 
63321ae08745Sheppo 		/*
63331ae08745Sheppo 		 * We ACK back every in-band descriptor message we process
63341ae08745Sheppo 		 */
63351ae08745Sheppo 		ibnd_desc->hdr.tag.vio_subtype = VIO_SUBTYPE_ACK;
63361ae08745Sheppo 		ibnd_desc->hdr.tag.vio_sid = ldcp->local_session;
6337b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)ibnd_desc,
6338b071742bSsg70180 		    sizeof (vnet_ibnd_desc_t), B_TRUE);
63391ae08745Sheppo 
63401ae08745Sheppo 		/* send the packet to be switched */
6341*da86a4daSrf157361 		vswp->vsw_switch_frame(vswp, mp, VSW_VNETPORT,
63421ae08745Sheppo 		    ldcp->ldc_port, NULL);
63431ae08745Sheppo 
63441ae08745Sheppo 		break;
63451ae08745Sheppo 
63461ae08745Sheppo 	case VIO_SUBTYPE_ACK:
63471ae08745Sheppo 		D1(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
63481ae08745Sheppo 
63491ae08745Sheppo 		/* Verify the ACK is valid */
63501ae08745Sheppo 		idx = ibnd_desc->hdr.desc_handle;
63511ae08745Sheppo 
63521ae08745Sheppo 		if (idx >= VSW_RING_NUM_EL) {
635334683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: corrupted ACK received "
635434683adeSsg70180 			    "(idx %ld)", vswp->instance, idx);
63551ae08745Sheppo 			return;
63561ae08745Sheppo 		}
63571ae08745Sheppo 
63581ae08745Sheppo 		if ((dp = ldcp->lane_out.dringp) == NULL) {
63591ae08745Sheppo 			DERR(vswp, "%s: no dring found", __func__);
63601ae08745Sheppo 			return;
63611ae08745Sheppo 		}
63621ae08745Sheppo 
63634bac2208Snarayan 		len = dp->num_descriptors;
63644bac2208Snarayan 		/*
63654bac2208Snarayan 		 * If the descriptor we are being ACK'ed for is not the
63664bac2208Snarayan 		 * one we expected, then pkts were lost somwhere, either
63674bac2208Snarayan 		 * when we tried to send a msg, or a previous ACK msg from
63684bac2208Snarayan 		 * our peer. In either case we now reclaim the descriptors
63694bac2208Snarayan 		 * in the range from the last ACK we received up to the
63704bac2208Snarayan 		 * current ACK.
63714bac2208Snarayan 		 */
63724bac2208Snarayan 		if (idx != dp->last_ack_recv) {
63734bac2208Snarayan 			DWARN(vswp, "%s: dropped pkts detected, (%ld, %ld)",
63744bac2208Snarayan 			    __func__, dp->last_ack_recv, idx);
63754bac2208Snarayan 			num = idx >= dp->last_ack_recv ?
63764bac2208Snarayan 			    idx - dp->last_ack_recv + 1:
63774bac2208Snarayan 			    (len - dp->last_ack_recv + 1) + idx;
63784bac2208Snarayan 		}
63791ae08745Sheppo 
63801ae08745Sheppo 		/*
63811ae08745Sheppo 		 * When we sent the in-band message to our peer we
63821ae08745Sheppo 		 * marked the copy in our private ring as READY. We now
63831ae08745Sheppo 		 * check that the descriptor we are being ACK'ed for is in
63841ae08745Sheppo 		 * fact READY, i.e. it is one we have shared with our peer.
63854bac2208Snarayan 		 *
63864bac2208Snarayan 		 * If its not we flag an error, but still reset the descr
63874bac2208Snarayan 		 * back to FREE.
63881ae08745Sheppo 		 */
63894bac2208Snarayan 		for (i = dp->last_ack_recv; j < num; i = (i + 1) % len, j++) {
63904bac2208Snarayan 			priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
6391d10e4ef2Snarayan 			mutex_enter(&priv_addr->dstate_lock);
63921ae08745Sheppo 			if (priv_addr->dstate != VIO_DESC_READY) {
63934bac2208Snarayan 				DERR(vswp, "%s: (%ld) desc at index %ld not "
63944bac2208Snarayan 				    "READY (0x%lx)", __func__,
63954bac2208Snarayan 				    ldcp->ldc_id, idx, priv_addr->dstate);
63964bac2208Snarayan 				DERR(vswp, "%s: bound %d: ncookies %ld : "
63974bac2208Snarayan 				    "datalen %ld", __func__,
63984bac2208Snarayan 				    priv_addr->bound, priv_addr->ncookies,
63994bac2208Snarayan 				    priv_addr->datalen);
64004bac2208Snarayan 			}
64011ae08745Sheppo 			D2(vswp, "%s: (%lld) freeing descp at %lld", __func__,
64021ae08745Sheppo 			    ldcp->ldc_id, idx);
64031ae08745Sheppo 			/* release resources associated with sent msg */
64041ae08745Sheppo 			bzero(priv_addr->datap, priv_addr->datalen);
64051ae08745Sheppo 			priv_addr->datalen = 0;
64061ae08745Sheppo 			priv_addr->dstate = VIO_DESC_FREE;
6407d10e4ef2Snarayan 			mutex_exit(&priv_addr->dstate_lock);
64081ae08745Sheppo 		}
64094bac2208Snarayan 		/* update to next expected value */
64104bac2208Snarayan 		dp->last_ack_recv = (idx + 1) % dp->num_descriptors;
64114bac2208Snarayan 
64121ae08745Sheppo 		break;
64131ae08745Sheppo 
64141ae08745Sheppo 	case VIO_SUBTYPE_NACK:
64151ae08745Sheppo 		DERR(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
64161ae08745Sheppo 
64171ae08745Sheppo 		/*
64181ae08745Sheppo 		 * We should only get a NACK if our peer doesn't like
64191ae08745Sheppo 		 * something about a message we have sent it. If this
64201ae08745Sheppo 		 * happens we just release the resources associated with
64211ae08745Sheppo 		 * the message. (We are relying on higher layers to decide
64221ae08745Sheppo 		 * whether or not to resend.
64231ae08745Sheppo 		 */
64241ae08745Sheppo 
64251ae08745Sheppo 		/* limit check */
64261ae08745Sheppo 		idx = ibnd_desc->hdr.desc_handle;
64271ae08745Sheppo 
64281ae08745Sheppo 		if (idx >= VSW_RING_NUM_EL) {
64291ae08745Sheppo 			DERR(vswp, "%s: corrupted NACK received (idx %lld)",
64301ae08745Sheppo 			    __func__, idx);
64311ae08745Sheppo 			return;
64321ae08745Sheppo 		}
64331ae08745Sheppo 
64341ae08745Sheppo 		if ((dp = ldcp->lane_out.dringp) == NULL) {
64351ae08745Sheppo 			DERR(vswp, "%s: no dring found", __func__);
64361ae08745Sheppo 			return;
64371ae08745Sheppo 		}
64381ae08745Sheppo 
64391ae08745Sheppo 		priv_addr = (vsw_private_desc_t *)dp->priv_addr;
64401ae08745Sheppo 
64411ae08745Sheppo 		/* move to correct location in ring */
64421ae08745Sheppo 		priv_addr += idx;
64431ae08745Sheppo 
64441ae08745Sheppo 		/* release resources associated with sent msg */
6445d10e4ef2Snarayan 		mutex_enter(&priv_addr->dstate_lock);
64461ae08745Sheppo 		bzero(priv_addr->datap, priv_addr->datalen);
64471ae08745Sheppo 		priv_addr->datalen = 0;
64481ae08745Sheppo 		priv_addr->dstate = VIO_DESC_FREE;
6449d10e4ef2Snarayan 		mutex_exit(&priv_addr->dstate_lock);
64501ae08745Sheppo 
64511ae08745Sheppo 		break;
64521ae08745Sheppo 
64531ae08745Sheppo 	default:
64541ae08745Sheppo 		DERR(vswp, "%s(%lld): Unknown vio_subtype %x\n", __func__,
64551ae08745Sheppo 		    ldcp->ldc_id, ibnd_desc->hdr.tag.vio_subtype);
64561ae08745Sheppo 	}
64571ae08745Sheppo 
64581ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
64591ae08745Sheppo }
64601ae08745Sheppo 
64611ae08745Sheppo static void
64621ae08745Sheppo vsw_process_err_pkt(vsw_ldc_t *ldcp, void *epkt, vio_msg_tag_t tag)
64631ae08745Sheppo {
64641ae08745Sheppo 	_NOTE(ARGUNUSED(epkt))
64651ae08745Sheppo 
64661ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
64671ae08745Sheppo 	uint16_t	env = tag.vio_subtype_env;
64681ae08745Sheppo 
64691ae08745Sheppo 	D1(vswp, "%s (%lld): enter\n", __func__, ldcp->ldc_id);
64701ae08745Sheppo 
64711ae08745Sheppo 	/*
64721ae08745Sheppo 	 * Error vio_subtypes have yet to be defined. So for
64731ae08745Sheppo 	 * the moment we can't do anything.
64741ae08745Sheppo 	 */
64751ae08745Sheppo 	D2(vswp, "%s: (%x) vio_subtype env", __func__, env);
64761ae08745Sheppo 
64771ae08745Sheppo 	D1(vswp, "%s (%lld): exit\n", __func__, ldcp->ldc_id);
64781ae08745Sheppo }
64791ae08745Sheppo 
64801ae08745Sheppo /*
64811ae08745Sheppo  * Switch the given ethernet frame when operating in layer 2 mode.
64821ae08745Sheppo  *
64831ae08745Sheppo  * vswp: pointer to the vsw instance
64841ae08745Sheppo  * mp: pointer to chain of ethernet frame(s) to be switched
64851ae08745Sheppo  * caller: identifies the source of this frame as:
64861ae08745Sheppo  * 		1. VSW_VNETPORT - a vsw port (connected to a vnet).
64871ae08745Sheppo  *		2. VSW_PHYSDEV - the physical ethernet device
64881ae08745Sheppo  *		3. VSW_LOCALDEV - vsw configured as a virtual interface
64891ae08745Sheppo  * arg: argument provided by the caller.
64901ae08745Sheppo  *		1. for VNETPORT - pointer to the corresponding vsw_port_t.
64911ae08745Sheppo  *		2. for PHYSDEV - NULL
64921ae08745Sheppo  *		3. for LOCALDEV - pointer to to this vsw_t(self)
64931ae08745Sheppo  */
64941ae08745Sheppo void
64951ae08745Sheppo vsw_switch_l2_frame(vsw_t *vswp, mblk_t *mp, int caller,
64961ae08745Sheppo 			vsw_port_t *arg, mac_resource_handle_t mrh)
64971ae08745Sheppo {
64981ae08745Sheppo 	struct ether_header	*ehp;
64991ae08745Sheppo 	vsw_port_t		*port = NULL;
65001ae08745Sheppo 	mblk_t			*bp, *ret_m;
65011ae08745Sheppo 	mblk_t			*nmp = NULL;
65021ae08745Sheppo 	vsw_port_list_t		*plist = &vswp->plist;
65031ae08745Sheppo 
65041ae08745Sheppo 	D1(vswp, "%s: enter (caller %d)", __func__, caller);
65051ae08745Sheppo 
65061ae08745Sheppo 	/*
65071ae08745Sheppo 	 * PERF: rather than breaking up the chain here, scan it
65081ae08745Sheppo 	 * to find all mblks heading to same destination and then
65091ae08745Sheppo 	 * pass that sub-chain to the lower transmit functions.
65101ae08745Sheppo 	 */
65111ae08745Sheppo 
65121ae08745Sheppo 	/* process the chain of packets */
65131ae08745Sheppo 	bp = mp;
65141ae08745Sheppo 	while (bp) {
65151ae08745Sheppo 		mp = bp;
65161ae08745Sheppo 		bp = bp->b_next;
65171ae08745Sheppo 		mp->b_next = mp->b_prev = NULL;
65181ae08745Sheppo 		ehp = (struct ether_header *)mp->b_rptr;
65191ae08745Sheppo 
65201ae08745Sheppo 		D2(vswp, "%s: mblk data buffer %lld : actual data size %lld",
65211ae08745Sheppo 		    __func__, MBLKSIZE(mp), MBLKL(mp));
65221ae08745Sheppo 
65231ae08745Sheppo 		READ_ENTER(&vswp->if_lockrw);
65241ae08745Sheppo 		if (ether_cmp(&ehp->ether_dhost, &vswp->if_addr) == 0) {
65251ae08745Sheppo 			/*
65261ae08745Sheppo 			 * If destination is VSW_LOCALDEV (vsw as an eth
65271ae08745Sheppo 			 * interface) and if the device is up & running,
65281ae08745Sheppo 			 * send the packet up the stack on this host.
65291ae08745Sheppo 			 * If the virtual interface is down, drop the packet.
65301ae08745Sheppo 			 */
65311ae08745Sheppo 			if (caller != VSW_LOCALDEV) {
65321ae08745Sheppo 				if (vswp->if_state & VSW_IF_UP) {
65331ae08745Sheppo 					RW_EXIT(&vswp->if_lockrw);
6534ba2e4443Sseb 					mac_rx(vswp->if_mh, mrh, mp);
65351ae08745Sheppo 				} else {
65361ae08745Sheppo 					RW_EXIT(&vswp->if_lockrw);
65371ae08745Sheppo 					/* Interface down, drop pkt */
65381ae08745Sheppo 					freemsg(mp);
65391ae08745Sheppo 				}
65401ae08745Sheppo 			} else {
65411ae08745Sheppo 				RW_EXIT(&vswp->if_lockrw);
65421ae08745Sheppo 				freemsg(mp);
65431ae08745Sheppo 			}
65441ae08745Sheppo 			continue;
65451ae08745Sheppo 		}
65461ae08745Sheppo 		RW_EXIT(&vswp->if_lockrw);
65471ae08745Sheppo 
65481ae08745Sheppo 		READ_ENTER(&plist->lockrw);
65491ae08745Sheppo 		port = vsw_lookup_fdb(vswp, ehp);
65501ae08745Sheppo 		if (port) {
65511ae08745Sheppo 			/*
65521ae08745Sheppo 			 * Mark the port as in-use.
65531ae08745Sheppo 			 */
65541ae08745Sheppo 			mutex_enter(&port->ref_lock);
65551ae08745Sheppo 			port->ref_cnt++;
65561ae08745Sheppo 			mutex_exit(&port->ref_lock);
65571ae08745Sheppo 			RW_EXIT(&plist->lockrw);
65581ae08745Sheppo 
65591ae08745Sheppo 			/*
65601ae08745Sheppo 			 * If plumbed and in promisc mode then copy msg
65611ae08745Sheppo 			 * and send up the stack.
65621ae08745Sheppo 			 */
65631ae08745Sheppo 			READ_ENTER(&vswp->if_lockrw);
65641ae08745Sheppo 			if (VSW_U_P(vswp->if_state)) {
65651ae08745Sheppo 				RW_EXIT(&vswp->if_lockrw);
65661ae08745Sheppo 				nmp = copymsg(mp);
65671ae08745Sheppo 				if (nmp)
6568ba2e4443Sseb 					mac_rx(vswp->if_mh, mrh, nmp);
65691ae08745Sheppo 			} else {
65701ae08745Sheppo 				RW_EXIT(&vswp->if_lockrw);
65711ae08745Sheppo 			}
65721ae08745Sheppo 
65731ae08745Sheppo 			/*
65741ae08745Sheppo 			 * If the destination is in FDB, the packet
65751ae08745Sheppo 			 * should be forwarded to the correponding
65761ae08745Sheppo 			 * vsw_port (connected to a vnet device -
65771ae08745Sheppo 			 * VSW_VNETPORT)
65781ae08745Sheppo 			 */
65791ae08745Sheppo 			(void) vsw_portsend(port, mp);
65801ae08745Sheppo 
65811ae08745Sheppo 			/*
65821ae08745Sheppo 			 * Decrement use count in port and check if
65831ae08745Sheppo 			 * should wake delete thread.
65841ae08745Sheppo 			 */
65851ae08745Sheppo 			mutex_enter(&port->ref_lock);
65861ae08745Sheppo 			port->ref_cnt--;
65871ae08745Sheppo 			if (port->ref_cnt == 0)
65881ae08745Sheppo 				cv_signal(&port->ref_cv);
65891ae08745Sheppo 			mutex_exit(&port->ref_lock);
65901ae08745Sheppo 		} else {
65911ae08745Sheppo 			RW_EXIT(&plist->lockrw);
65921ae08745Sheppo 			/*
65931ae08745Sheppo 			 * Destination not in FDB.
65941ae08745Sheppo 			 *
65951ae08745Sheppo 			 * If the destination is broadcast or
65961ae08745Sheppo 			 * multicast forward the packet to all
65971ae08745Sheppo 			 * (VNETPORTs, PHYSDEV, LOCALDEV),
65981ae08745Sheppo 			 * except the caller.
65991ae08745Sheppo 			 */
66001ae08745Sheppo 			if (IS_BROADCAST(ehp)) {
66011ae08745Sheppo 				D3(vswp, "%s: BROADCAST pkt", __func__);
6602205eeb1aSlm66018 				(void) vsw_forward_all(vswp, mp, caller, arg);
66031ae08745Sheppo 			} else if (IS_MULTICAST(ehp)) {
66041ae08745Sheppo 				D3(vswp, "%s: MULTICAST pkt", __func__);
6605205eeb1aSlm66018 				(void) vsw_forward_grp(vswp, mp, caller, arg);
66061ae08745Sheppo 			} else {
66071ae08745Sheppo 				/*
66081ae08745Sheppo 				 * If the destination is unicast, and came
66091ae08745Sheppo 				 * from either a logical network device or
66101ae08745Sheppo 				 * the switch itself when it is plumbed, then
66111ae08745Sheppo 				 * send it out on the physical device and also
66121ae08745Sheppo 				 * up the stack if the logical interface is
66131ae08745Sheppo 				 * in promiscious mode.
66141ae08745Sheppo 				 *
66151ae08745Sheppo 				 * NOTE:  The assumption here is that if we
66161ae08745Sheppo 				 * cannot find the destination in our fdb, its
66171ae08745Sheppo 				 * a unicast address, and came from either a
66181ae08745Sheppo 				 * vnet or down the stack (when plumbed) it
66191ae08745Sheppo 				 * must be destinded for an ethernet device
66201ae08745Sheppo 				 * outside our ldoms.
66211ae08745Sheppo 				 */
66221ae08745Sheppo 				if (caller == VSW_VNETPORT) {
66231ae08745Sheppo 					READ_ENTER(&vswp->if_lockrw);
66241ae08745Sheppo 					if (VSW_U_P(vswp->if_state)) {
66251ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
66261ae08745Sheppo 						nmp = copymsg(mp);
66271ae08745Sheppo 						if (nmp)
6628ba2e4443Sseb 							mac_rx(vswp->if_mh,
66291ae08745Sheppo 							    mrh, nmp);
66301ae08745Sheppo 					} else {
66311ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
66321ae08745Sheppo 					}
66331ae08745Sheppo 					if ((ret_m = vsw_tx_msg(vswp, mp))
66341ae08745Sheppo 					    != NULL) {
66351ae08745Sheppo 						DERR(vswp, "%s: drop mblks to "
66361ae08745Sheppo 						    "phys dev", __func__);
66371ae08745Sheppo 						freemsg(ret_m);
66381ae08745Sheppo 					}
66391ae08745Sheppo 
66401ae08745Sheppo 				} else if (caller == VSW_PHYSDEV) {
66411ae08745Sheppo 					/*
66421ae08745Sheppo 					 * Pkt seen because card in promisc
66431ae08745Sheppo 					 * mode. Send up stack if plumbed in
66441ae08745Sheppo 					 * promisc mode, else drop it.
66451ae08745Sheppo 					 */
66461ae08745Sheppo 					READ_ENTER(&vswp->if_lockrw);
66471ae08745Sheppo 					if (VSW_U_P(vswp->if_state)) {
66481ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
6649ba2e4443Sseb 						mac_rx(vswp->if_mh, mrh, mp);
66501ae08745Sheppo 					} else {
66511ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
66521ae08745Sheppo 						freemsg(mp);
66531ae08745Sheppo 					}
66541ae08745Sheppo 
66551ae08745Sheppo 				} else if (caller == VSW_LOCALDEV) {
66561ae08745Sheppo 					/*
66571ae08745Sheppo 					 * Pkt came down the stack, send out
66581ae08745Sheppo 					 * over physical device.
66591ae08745Sheppo 					 */
66601ae08745Sheppo 					if ((ret_m = vsw_tx_msg(vswp, mp))
66611ae08745Sheppo 					    != NULL) {
66621ae08745Sheppo 						DERR(vswp, "%s: drop mblks to "
66631ae08745Sheppo 						    "phys dev", __func__);
66641ae08745Sheppo 						freemsg(ret_m);
66651ae08745Sheppo 					}
66661ae08745Sheppo 				}
66671ae08745Sheppo 			}
66681ae08745Sheppo 		}
66691ae08745Sheppo 	}
66701ae08745Sheppo 	D1(vswp, "%s: exit\n", __func__);
66711ae08745Sheppo }
66721ae08745Sheppo 
66731ae08745Sheppo /*
66741ae08745Sheppo  * Switch ethernet frame when in layer 3 mode (i.e. using IP
66751ae08745Sheppo  * layer to do the routing).
66761ae08745Sheppo  *
66771ae08745Sheppo  * There is a large amount of overlap between this function and
66781ae08745Sheppo  * vsw_switch_l2_frame. At some stage we need to revisit and refactor
66791ae08745Sheppo  * both these functions.
66801ae08745Sheppo  */
66811ae08745Sheppo void
66821ae08745Sheppo vsw_switch_l3_frame(vsw_t *vswp, mblk_t *mp, int caller,
66831ae08745Sheppo 			vsw_port_t *arg, mac_resource_handle_t mrh)
66841ae08745Sheppo {
66851ae08745Sheppo 	struct ether_header	*ehp;
66861ae08745Sheppo 	vsw_port_t		*port = NULL;
66871ae08745Sheppo 	mblk_t			*bp = NULL;
66881ae08745Sheppo 	vsw_port_list_t		*plist = &vswp->plist;
66891ae08745Sheppo 
66901ae08745Sheppo 	D1(vswp, "%s: enter (caller %d)", __func__, caller);
66911ae08745Sheppo 
66921ae08745Sheppo 	/*
66931ae08745Sheppo 	 * In layer 3 mode should only ever be switching packets
66941ae08745Sheppo 	 * between IP layer and vnet devices. So make sure thats
66951ae08745Sheppo 	 * who is invoking us.
66961ae08745Sheppo 	 */
66971ae08745Sheppo 	if ((caller != VSW_LOCALDEV) && (caller != VSW_VNETPORT)) {
66981ae08745Sheppo 		DERR(vswp, "%s: unexpected caller (%d)", __func__, caller);
66991ae08745Sheppo 		freemsgchain(mp);
67001ae08745Sheppo 		return;
67011ae08745Sheppo 	}
67021ae08745Sheppo 
67031ae08745Sheppo 	/* process the chain of packets */
67041ae08745Sheppo 	bp = mp;
67051ae08745Sheppo 	while (bp) {
67061ae08745Sheppo 		mp = bp;
67071ae08745Sheppo 		bp = bp->b_next;
67081ae08745Sheppo 		mp->b_next = mp->b_prev = NULL;
67091ae08745Sheppo 		ehp = (struct ether_header *)mp->b_rptr;
67101ae08745Sheppo 
67111ae08745Sheppo 		D2(vswp, "%s: mblk data buffer %lld : actual data size %lld",
67121ae08745Sheppo 		    __func__, MBLKSIZE(mp), MBLKL(mp));
67131ae08745Sheppo 
67141ae08745Sheppo 		READ_ENTER(&plist->lockrw);
67151ae08745Sheppo 		port = vsw_lookup_fdb(vswp, ehp);
67161ae08745Sheppo 		if (port) {
67171ae08745Sheppo 			/*
67181ae08745Sheppo 			 * Mark port as in-use.
67191ae08745Sheppo 			 */
67201ae08745Sheppo 			mutex_enter(&port->ref_lock);
67211ae08745Sheppo 			port->ref_cnt++;
67221ae08745Sheppo 			mutex_exit(&port->ref_lock);
67231ae08745Sheppo 			RW_EXIT(&plist->lockrw);
67241ae08745Sheppo 
67251ae08745Sheppo 			D2(vswp, "%s: sending to target port", __func__);
67261ae08745Sheppo 			(void) vsw_portsend(port, mp);
67271ae08745Sheppo 
67281ae08745Sheppo 			/*
67291ae08745Sheppo 			 * Finished with port so decrement ref count and
67301ae08745Sheppo 			 * check if should wake delete thread.
67311ae08745Sheppo 			 */
67321ae08745Sheppo 			mutex_enter(&port->ref_lock);
67331ae08745Sheppo 			port->ref_cnt--;
67341ae08745Sheppo 			if (port->ref_cnt == 0)
67351ae08745Sheppo 				cv_signal(&port->ref_cv);
67361ae08745Sheppo 			mutex_exit(&port->ref_lock);
67371ae08745Sheppo 		} else {
67381ae08745Sheppo 			RW_EXIT(&plist->lockrw);
67391ae08745Sheppo 			/*
67401ae08745Sheppo 			 * Destination not in FDB
67411ae08745Sheppo 			 *
67421ae08745Sheppo 			 * If the destination is broadcast or
67431ae08745Sheppo 			 * multicast forward the packet to all
67441ae08745Sheppo 			 * (VNETPORTs, PHYSDEV, LOCALDEV),
67451ae08745Sheppo 			 * except the caller.
67461ae08745Sheppo 			 */
67471ae08745Sheppo 			if (IS_BROADCAST(ehp)) {
67481ae08745Sheppo 				D2(vswp, "%s: BROADCAST pkt", __func__);
6749205eeb1aSlm66018 				(void) vsw_forward_all(vswp, mp, caller, arg);
67501ae08745Sheppo 			} else if (IS_MULTICAST(ehp)) {
67511ae08745Sheppo 				D2(vswp, "%s: MULTICAST pkt", __func__);
6752205eeb1aSlm66018 				(void) vsw_forward_grp(vswp, mp, caller, arg);
67531ae08745Sheppo 			} else {
67541ae08745Sheppo 				/*
67551ae08745Sheppo 				 * Unicast pkt from vnet that we don't have
67561ae08745Sheppo 				 * an FDB entry for, so must be destinded for
67571ae08745Sheppo 				 * the outside world. Attempt to send up to the
67581ae08745Sheppo 				 * IP layer to allow it to deal with it.
67591ae08745Sheppo 				 */
67601ae08745Sheppo 				if (caller == VSW_VNETPORT) {
67611ae08745Sheppo 					READ_ENTER(&vswp->if_lockrw);
67621ae08745Sheppo 					if (vswp->if_state & VSW_IF_UP) {
67631ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
67641ae08745Sheppo 						D2(vswp, "%s: sending up",
67651ae08745Sheppo 						    __func__);
6766ba2e4443Sseb 						mac_rx(vswp->if_mh, mrh, mp);
67671ae08745Sheppo 					} else {
67681ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
67691ae08745Sheppo 						/* Interface down, drop pkt */
67701ae08745Sheppo 						D2(vswp, "%s I/F down",
67711ae08745Sheppo 						    __func__);
67721ae08745Sheppo 						freemsg(mp);
67731ae08745Sheppo 					}
67741ae08745Sheppo 				}
67751ae08745Sheppo 			}
67761ae08745Sheppo 		}
67771ae08745Sheppo 	}
67781ae08745Sheppo 
67791ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
67801ae08745Sheppo }
67811ae08745Sheppo 
67821ae08745Sheppo /*
67831ae08745Sheppo  * Forward the ethernet frame to all ports (VNETPORTs, PHYSDEV, LOCALDEV),
67841ae08745Sheppo  * except the caller (port on which frame arrived).
67851ae08745Sheppo  */
67861ae08745Sheppo static int
67871ae08745Sheppo vsw_forward_all(vsw_t *vswp, mblk_t *mp, int caller, vsw_port_t *arg)
67881ae08745Sheppo {
67891ae08745Sheppo 	vsw_port_list_t	*plist = &vswp->plist;
67901ae08745Sheppo 	vsw_port_t	*portp;
67911ae08745Sheppo 	mblk_t		*nmp = NULL;
67921ae08745Sheppo 	mblk_t		*ret_m = NULL;
67931ae08745Sheppo 	int		skip_port = 0;
67941ae08745Sheppo 
67951ae08745Sheppo 	D1(vswp, "vsw_forward_all: enter\n");
67961ae08745Sheppo 
67971ae08745Sheppo 	/*
67981ae08745Sheppo 	 * Broadcast message from inside ldoms so send to outside
67991ae08745Sheppo 	 * world if in either of layer 2 modes.
68001ae08745Sheppo 	 */
68011ae08745Sheppo 	if (((vswp->smode[vswp->smode_idx] == VSW_LAYER2) ||
68021ae08745Sheppo 	    (vswp->smode[vswp->smode_idx] == VSW_LAYER2_PROMISC)) &&
68031ae08745Sheppo 	    ((caller == VSW_LOCALDEV) || (caller == VSW_VNETPORT))) {
68041ae08745Sheppo 
68051ae08745Sheppo 		nmp = dupmsg(mp);
68061ae08745Sheppo 		if (nmp) {
68071ae08745Sheppo 			if ((ret_m = vsw_tx_msg(vswp, nmp)) != NULL) {
68081ae08745Sheppo 				DERR(vswp, "%s: dropping pkt(s) "
68091ae08745Sheppo 				    "consisting of %ld bytes of data for"
68101ae08745Sheppo 				    " physical device", __func__, MBLKL(ret_m));
68111ae08745Sheppo 				freemsg(ret_m);
68121ae08745Sheppo 			}
68131ae08745Sheppo 		}
68141ae08745Sheppo 	}
68151ae08745Sheppo 
68161ae08745Sheppo 	if (caller == VSW_VNETPORT)
68171ae08745Sheppo 		skip_port = 1;
68181ae08745Sheppo 
68191ae08745Sheppo 	/*
68201ae08745Sheppo 	 * Broadcast message from other vnet (layer 2 or 3) or outside
68211ae08745Sheppo 	 * world (layer 2 only), send up stack if plumbed.
68221ae08745Sheppo 	 */
68231ae08745Sheppo 	if ((caller == VSW_PHYSDEV) || (caller == VSW_VNETPORT)) {
68241ae08745Sheppo 		READ_ENTER(&vswp->if_lockrw);
68251ae08745Sheppo 		if (vswp->if_state & VSW_IF_UP) {
68261ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
68271ae08745Sheppo 			nmp = copymsg(mp);
68281ae08745Sheppo 			if (nmp)
6829ba2e4443Sseb 				mac_rx(vswp->if_mh, NULL, nmp);
68301ae08745Sheppo 		} else {
68311ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
68321ae08745Sheppo 		}
68331ae08745Sheppo 	}
68341ae08745Sheppo 
68351ae08745Sheppo 	/* send it to all VNETPORTs */
68361ae08745Sheppo 	READ_ENTER(&plist->lockrw);
68371ae08745Sheppo 	for (portp = plist->head; portp != NULL; portp = portp->p_next) {
68381ae08745Sheppo 		D2(vswp, "vsw_forward_all: port %d", portp->p_instance);
68391ae08745Sheppo 		/*
68401ae08745Sheppo 		 * Caution ! - don't reorder these two checks as arg
68411ae08745Sheppo 		 * will be NULL if the caller is PHYSDEV. skip_port is
68421ae08745Sheppo 		 * only set if caller is VNETPORT.
68431ae08745Sheppo 		 */
68441ae08745Sheppo 		if ((skip_port) && (portp == arg))
68451ae08745Sheppo 			continue;
68461ae08745Sheppo 		else {
68471ae08745Sheppo 			nmp = dupmsg(mp);
68481ae08745Sheppo 			if (nmp) {
68491ae08745Sheppo 				(void) vsw_portsend(portp, nmp);
68501ae08745Sheppo 			} else {
68511ae08745Sheppo 				DERR(vswp, "vsw_forward_all: nmp NULL");
68521ae08745Sheppo 			}
68531ae08745Sheppo 		}
68541ae08745Sheppo 	}
68551ae08745Sheppo 	RW_EXIT(&plist->lockrw);
68561ae08745Sheppo 
68571ae08745Sheppo 	freemsg(mp);
68581ae08745Sheppo 
68591ae08745Sheppo 	D1(vswp, "vsw_forward_all: exit\n");
68601ae08745Sheppo 	return (0);
68611ae08745Sheppo }
68621ae08745Sheppo 
68631ae08745Sheppo /*
68641ae08745Sheppo  * Forward pkts to any devices or interfaces which have registered
68651ae08745Sheppo  * an interest in them (i.e. multicast groups).
68661ae08745Sheppo  */
68671ae08745Sheppo static int
68681ae08745Sheppo vsw_forward_grp(vsw_t *vswp, mblk_t *mp, int caller, vsw_port_t *arg)
68691ae08745Sheppo {
68701ae08745Sheppo 	struct ether_header	*ehp = (struct ether_header *)mp->b_rptr;
68711ae08745Sheppo 	mfdb_ent_t		*entp = NULL;
68721ae08745Sheppo 	mfdb_ent_t		*tpp = NULL;
68731ae08745Sheppo 	vsw_port_t 		*port;
68741ae08745Sheppo 	uint64_t		key = 0;
68751ae08745Sheppo 	mblk_t			*nmp = NULL;
68761ae08745Sheppo 	mblk_t			*ret_m = NULL;
68771ae08745Sheppo 	boolean_t		check_if = B_TRUE;
68781ae08745Sheppo 
68791ae08745Sheppo 	/*
68801ae08745Sheppo 	 * Convert address to hash table key
68811ae08745Sheppo 	 */
68821ae08745Sheppo 	KEY_HASH(key, ehp->ether_dhost);
68831ae08745Sheppo 
68841ae08745Sheppo 	D1(vswp, "%s: key 0x%llx", __func__, key);
68851ae08745Sheppo 
68861ae08745Sheppo 	/*
68871ae08745Sheppo 	 * If pkt came from either a vnet or down the stack (if we are
68881ae08745Sheppo 	 * plumbed) and we are in layer 2 mode, then we send the pkt out
68891ae08745Sheppo 	 * over the physical adapter, and then check to see if any other
68901ae08745Sheppo 	 * vnets are interested in it.
68911ae08745Sheppo 	 */
68921ae08745Sheppo 	if (((vswp->smode[vswp->smode_idx] == VSW_LAYER2) ||
68931ae08745Sheppo 	    (vswp->smode[vswp->smode_idx] == VSW_LAYER2_PROMISC)) &&
68941ae08745Sheppo 	    ((caller == VSW_VNETPORT) || (caller == VSW_LOCALDEV))) {
68951ae08745Sheppo 		nmp = dupmsg(mp);
68961ae08745Sheppo 		if (nmp) {
68971ae08745Sheppo 			if ((ret_m = vsw_tx_msg(vswp, nmp)) != NULL) {
6898205eeb1aSlm66018 				DERR(vswp, "%s: dropping pkt(s) consisting of "
6899205eeb1aSlm66018 				    "%ld bytes of data for physical device",
69001ae08745Sheppo 				    __func__, MBLKL(ret_m));
69011ae08745Sheppo 				freemsg(ret_m);
69021ae08745Sheppo 			}
69031ae08745Sheppo 		}
69041ae08745Sheppo 	}
69051ae08745Sheppo 
69061ae08745Sheppo 	READ_ENTER(&vswp->mfdbrw);
69071ae08745Sheppo 	if (mod_hash_find(vswp->mfdb, (mod_hash_key_t)key,
69081ae08745Sheppo 	    (mod_hash_val_t *)&entp) != 0) {
69091ae08745Sheppo 		D3(vswp, "%s: no table entry found for addr 0x%llx",
69101ae08745Sheppo 		    __func__, key);
69111ae08745Sheppo 	} else {
69121ae08745Sheppo 		/*
69131ae08745Sheppo 		 * Send to list of devices associated with this address...
69141ae08745Sheppo 		 */
69151ae08745Sheppo 		for (tpp = entp; tpp != NULL; tpp = tpp->nextp) {
69161ae08745Sheppo 
69171ae08745Sheppo 			/* dont send to ourselves */
69181ae08745Sheppo 			if ((caller == VSW_VNETPORT) &&
69191ae08745Sheppo 			    (tpp->d_addr == (void *)arg)) {
69201ae08745Sheppo 				port = (vsw_port_t *)tpp->d_addr;
69211ae08745Sheppo 				D3(vswp, "%s: not sending to ourselves"
6922205eeb1aSlm66018 				    " : port %d", __func__, port->p_instance);
69231ae08745Sheppo 				continue;
69241ae08745Sheppo 
69251ae08745Sheppo 			} else if ((caller == VSW_LOCALDEV) &&
69261ae08745Sheppo 			    (tpp->d_type == VSW_LOCALDEV)) {
69271ae08745Sheppo 				D3(vswp, "%s: not sending back up stack",
69281ae08745Sheppo 				    __func__);
69291ae08745Sheppo 				continue;
69301ae08745Sheppo 			}
69311ae08745Sheppo 
69321ae08745Sheppo 			if (tpp->d_type == VSW_VNETPORT) {
69331ae08745Sheppo 				port = (vsw_port_t *)tpp->d_addr;
6934205eeb1aSlm66018 				D3(vswp, "%s: sending to port %ld for addr "
6935205eeb1aSlm66018 				    "0x%llx", __func__, port->p_instance, key);
69361ae08745Sheppo 
69371ae08745Sheppo 				nmp = dupmsg(mp);
69381ae08745Sheppo 				if (nmp)
69391ae08745Sheppo 					(void) vsw_portsend(port, nmp);
69401ae08745Sheppo 			} else {
69411ae08745Sheppo 				if (vswp->if_state & VSW_IF_UP) {
69421ae08745Sheppo 					nmp = copymsg(mp);
69431ae08745Sheppo 					if (nmp)
6944ba2e4443Sseb 						mac_rx(vswp->if_mh, NULL, nmp);
69451ae08745Sheppo 					check_if = B_FALSE;
69461ae08745Sheppo 					D3(vswp, "%s: sending up stack"
6947205eeb1aSlm66018 					    " for addr 0x%llx", __func__, key);
69481ae08745Sheppo 				}
69491ae08745Sheppo 			}
69501ae08745Sheppo 		}
69511ae08745Sheppo 	}
69521ae08745Sheppo 
69531ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
69541ae08745Sheppo 
69551ae08745Sheppo 	/*
69561ae08745Sheppo 	 * If the pkt came from either a vnet or from physical device,
69571ae08745Sheppo 	 * and if we havent already sent the pkt up the stack then we
69581ae08745Sheppo 	 * check now if we can/should (i.e. the interface is plumbed
69591ae08745Sheppo 	 * and in promisc mode).
69601ae08745Sheppo 	 */
69611ae08745Sheppo 	if ((check_if) &&
69621ae08745Sheppo 	    ((caller == VSW_VNETPORT) || (caller == VSW_PHYSDEV))) {
69631ae08745Sheppo 		READ_ENTER(&vswp->if_lockrw);
69641ae08745Sheppo 		if (VSW_U_P(vswp->if_state)) {
69651ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
69661ae08745Sheppo 			D3(vswp, "%s: (caller %d) finally sending up stack"
69671ae08745Sheppo 			    " for addr 0x%llx", __func__, caller, key);
69681ae08745Sheppo 			nmp = copymsg(mp);
69691ae08745Sheppo 			if (nmp)
6970ba2e4443Sseb 				mac_rx(vswp->if_mh, NULL, nmp);
69711ae08745Sheppo 		} else {
69721ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
69731ae08745Sheppo 		}
69741ae08745Sheppo 	}
69751ae08745Sheppo 
69761ae08745Sheppo 	freemsg(mp);
69771ae08745Sheppo 
69781ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
69791ae08745Sheppo 
69801ae08745Sheppo 	return (0);
69811ae08745Sheppo }
69821ae08745Sheppo 
69831ae08745Sheppo /* transmit the packet over the given port */
69841ae08745Sheppo static int
69851ae08745Sheppo vsw_portsend(vsw_port_t *port, mblk_t *mp)
69861ae08745Sheppo {
69871ae08745Sheppo 	vsw_ldc_list_t 	*ldcl = &port->p_ldclist;
69881ae08745Sheppo 	vsw_ldc_t 	*ldcp;
69891ae08745Sheppo 	int		status = 0;
69901ae08745Sheppo 
69911ae08745Sheppo 
69921ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
69931ae08745Sheppo 	/*
69941ae08745Sheppo 	 * Note for now, we have a single channel.
69951ae08745Sheppo 	 */
69961ae08745Sheppo 	ldcp = ldcl->head;
69971ae08745Sheppo 	if (ldcp == NULL) {
69981ae08745Sheppo 		DERR(port->p_vswp, "vsw_portsend: no ldc: dropping packet\n");
69991ae08745Sheppo 		freemsg(mp);
70001ae08745Sheppo 		RW_EXIT(&ldcl->lockrw);
70011ae08745Sheppo 		return (1);
70021ae08745Sheppo 	}
70031ae08745Sheppo 
70041ae08745Sheppo 	/*
70051ae08745Sheppo 	 * Send the message out using the appropriate
70061ae08745Sheppo 	 * transmit function which will free mblock when it
70071ae08745Sheppo 	 * is finished with it.
70081ae08745Sheppo 	 */
70091ae08745Sheppo 	mutex_enter(&port->tx_lock);
70101ae08745Sheppo 	if (port->transmit != NULL)
70111ae08745Sheppo 		status = (*port->transmit)(ldcp, mp);
70121ae08745Sheppo 	else {
70131ae08745Sheppo 		freemsg(mp);
70141ae08745Sheppo 	}
70151ae08745Sheppo 	mutex_exit(&port->tx_lock);
70161ae08745Sheppo 
70171ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
70181ae08745Sheppo 
70191ae08745Sheppo 	return (status);
70201ae08745Sheppo }
70211ae08745Sheppo 
70221ae08745Sheppo /*
70231ae08745Sheppo  * Send packet out via descriptor ring to a logical device.
70241ae08745Sheppo  */
70251ae08745Sheppo static int
70261ae08745Sheppo vsw_dringsend(vsw_ldc_t *ldcp, mblk_t *mp)
70271ae08745Sheppo {
70281ae08745Sheppo 	vio_dring_msg_t		dring_pkt;
70291ae08745Sheppo 	dring_info_t		*dp = NULL;
70301ae08745Sheppo 	vsw_private_desc_t	*priv_desc = NULL;
7031d10e4ef2Snarayan 	vnet_public_desc_t	*pub = NULL;
70321ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
70331ae08745Sheppo 	mblk_t			*bp;
70341ae08745Sheppo 	size_t			n, size;
70351ae08745Sheppo 	caddr_t			bufp;
70361ae08745Sheppo 	int			idx;
70371ae08745Sheppo 	int			status = LDC_TX_SUCCESS;
70381ae08745Sheppo 
70391ae08745Sheppo 	D1(vswp, "%s(%lld): enter\n", __func__, ldcp->ldc_id);
70401ae08745Sheppo 
70411ae08745Sheppo 	/* TODO: make test a macro */
70421ae08745Sheppo 	if ((!(ldcp->lane_out.lstate & VSW_LANE_ACTIVE)) ||
70431ae08745Sheppo 	    (ldcp->ldc_status != LDC_UP) || (ldcp->ldc_handle == NULL)) {
70441ae08745Sheppo 		DWARN(vswp, "%s(%lld) status(%d) lstate(0x%llx), dropping "
70451ae08745Sheppo 		    "packet\n", __func__, ldcp->ldc_id, ldcp->ldc_status,
70461ae08745Sheppo 		    ldcp->lane_out.lstate);
70471ae08745Sheppo 		freemsg(mp);
70481ae08745Sheppo 		return (LDC_TX_FAILURE);
70491ae08745Sheppo 	}
70501ae08745Sheppo 
70511ae08745Sheppo 	/*
70521ae08745Sheppo 	 * Note - using first ring only, this may change
70531ae08745Sheppo 	 * in the future.
70541ae08745Sheppo 	 */
7055445b4c2eSsb155480 	READ_ENTER(&ldcp->lane_out.dlistrw);
70561ae08745Sheppo 	if ((dp = ldcp->lane_out.dringp) == NULL) {
7057445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_out.dlistrw);
70581ae08745Sheppo 		DERR(vswp, "%s(%lld): no dring for outbound lane on"
70591ae08745Sheppo 		    " channel %d", __func__, ldcp->ldc_id, ldcp->ldc_id);
70601ae08745Sheppo 		freemsg(mp);
70611ae08745Sheppo 		return (LDC_TX_FAILURE);
70621ae08745Sheppo 	}
70631ae08745Sheppo 
70641ae08745Sheppo 	size = msgsize(mp);
70651ae08745Sheppo 	if (size > (size_t)ETHERMAX) {
7066445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_out.dlistrw);
70671ae08745Sheppo 		DERR(vswp, "%s(%lld) invalid size (%ld)\n", __func__,
70681ae08745Sheppo 		    ldcp->ldc_id, size);
7069d10e4ef2Snarayan 		freemsg(mp);
7070d10e4ef2Snarayan 		return (LDC_TX_FAILURE);
70711ae08745Sheppo 	}
70721ae08745Sheppo 
70731ae08745Sheppo 	/*
70741ae08745Sheppo 	 * Find a free descriptor
70751ae08745Sheppo 	 *
70761ae08745Sheppo 	 * Note: for the moment we are assuming that we will only
70771ae08745Sheppo 	 * have one dring going from the switch to each of its
70781ae08745Sheppo 	 * peers. This may change in the future.
70791ae08745Sheppo 	 */
70801ae08745Sheppo 	if (vsw_dring_find_free_desc(dp, &priv_desc, &idx) != 0) {
7081d10e4ef2Snarayan 		D2(vswp, "%s(%lld): no descriptor available for ring "
70821ae08745Sheppo 		    "at 0x%llx", __func__, ldcp->ldc_id, dp);
70831ae08745Sheppo 
70841ae08745Sheppo 		/* nothing more we can do */
70851ae08745Sheppo 		status = LDC_TX_NORESOURCES;
70861ae08745Sheppo 		goto vsw_dringsend_free_exit;
70871ae08745Sheppo 	} else {
7088205eeb1aSlm66018 		D2(vswp, "%s(%lld): free private descriptor found at pos %ld "
7089205eeb1aSlm66018 		    "addr 0x%llx\n", __func__, ldcp->ldc_id, idx, priv_desc);
70901ae08745Sheppo 	}
70911ae08745Sheppo 
70921ae08745Sheppo 	/* copy data into the descriptor */
70931ae08745Sheppo 	bufp = priv_desc->datap;
7094d10e4ef2Snarayan 	bufp += VNET_IPALIGN;
70951ae08745Sheppo 	for (bp = mp, n = 0; bp != NULL; bp = bp->b_cont) {
70961ae08745Sheppo 		n = MBLKL(bp);
70971ae08745Sheppo 		bcopy(bp->b_rptr, bufp, n);
70981ae08745Sheppo 		bufp += n;
70991ae08745Sheppo 	}
71001ae08745Sheppo 
71011ae08745Sheppo 	priv_desc->datalen = (size < (size_t)ETHERMIN) ? ETHERMIN : size;
7102d10e4ef2Snarayan 
7103d10e4ef2Snarayan 	pub = priv_desc->descp;
7104d10e4ef2Snarayan 	pub->nbytes = priv_desc->datalen;
7105d10e4ef2Snarayan 
7106d10e4ef2Snarayan 	mutex_enter(&priv_desc->dstate_lock);
7107d10e4ef2Snarayan 	pub->hdr.dstate = VIO_DESC_READY;
7108d10e4ef2Snarayan 	mutex_exit(&priv_desc->dstate_lock);
71091ae08745Sheppo 
71101ae08745Sheppo 	/*
7111d10e4ef2Snarayan 	 * Determine whether or not we need to send a message to our
7112d10e4ef2Snarayan 	 * peer prompting them to read our newly updated descriptor(s).
71131ae08745Sheppo 	 */
7114d10e4ef2Snarayan 	mutex_enter(&dp->restart_lock);
7115d10e4ef2Snarayan 	if (dp->restart_reqd) {
7116d10e4ef2Snarayan 		dp->restart_reqd = B_FALSE;
7117d10e4ef2Snarayan 		mutex_exit(&dp->restart_lock);
71181ae08745Sheppo 
71191ae08745Sheppo 		/*
71201ae08745Sheppo 		 * Send a vio_dring_msg to peer to prompt them to read
71211ae08745Sheppo 		 * the updated descriptor ring.
71221ae08745Sheppo 		 */
71231ae08745Sheppo 		dring_pkt.tag.vio_msgtype = VIO_TYPE_DATA;
71241ae08745Sheppo 		dring_pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
71251ae08745Sheppo 		dring_pkt.tag.vio_subtype_env = VIO_DRING_DATA;
71261ae08745Sheppo 		dring_pkt.tag.vio_sid = ldcp->local_session;
71271ae08745Sheppo 
71281ae08745Sheppo 		/* Note - for now using first ring */
71291ae08745Sheppo 		dring_pkt.dring_ident = dp->ident;
71301ae08745Sheppo 
7131d10e4ef2Snarayan 		mutex_enter(&ldcp->lane_out.seq_lock);
71321ae08745Sheppo 		dring_pkt.seq_num = ldcp->lane_out.seq_num++;
7133d10e4ef2Snarayan 		mutex_exit(&ldcp->lane_out.seq_lock);
71341ae08745Sheppo 
7135d10e4ef2Snarayan 		/*
7136d10e4ef2Snarayan 		 * If last_ack_recv is -1 then we know we've not
7137d10e4ef2Snarayan 		 * received any ack's yet, so this must be the first
7138d10e4ef2Snarayan 		 * msg sent, so set the start to the begining of the ring.
7139d10e4ef2Snarayan 		 */
7140d10e4ef2Snarayan 		mutex_enter(&dp->dlock);
7141d10e4ef2Snarayan 		if (dp->last_ack_recv == -1) {
7142d10e4ef2Snarayan 			dring_pkt.start_idx = 0;
7143d10e4ef2Snarayan 		} else {
7144205eeb1aSlm66018 			dring_pkt.start_idx =
7145205eeb1aSlm66018 			    (dp->last_ack_recv + 1) % dp->num_descriptors;
7146d10e4ef2Snarayan 		}
7147d10e4ef2Snarayan 		dring_pkt.end_idx = -1;
7148d10e4ef2Snarayan 		mutex_exit(&dp->dlock);
71491ae08745Sheppo 
71501ae08745Sheppo 		D3(vswp, "%s(%lld): dring 0x%llx : ident 0x%llx\n", __func__,
71511ae08745Sheppo 		    ldcp->ldc_id, dp, dring_pkt.dring_ident);
7152d10e4ef2Snarayan 		D3(vswp, "%s(%lld): start %lld : end %lld : seq %lld\n",
7153d10e4ef2Snarayan 		    __func__, ldcp->ldc_id, dring_pkt.start_idx,
7154d10e4ef2Snarayan 		    dring_pkt.end_idx, dring_pkt.seq_num);
71551ae08745Sheppo 
7156b071742bSsg70180 		RW_EXIT(&ldcp->lane_out.dlistrw);
7157b071742bSsg70180 
7158b071742bSsg70180 		(void) vsw_send_msg(ldcp, (void *)&dring_pkt,
7159b071742bSsg70180 		    sizeof (vio_dring_msg_t), B_TRUE);
7160b071742bSsg70180 
7161b071742bSsg70180 		/* free the message block */
7162b071742bSsg70180 		freemsg(mp);
7163b071742bSsg70180 		return (status);
7164b071742bSsg70180 
7165d10e4ef2Snarayan 	} else {
7166d10e4ef2Snarayan 		mutex_exit(&dp->restart_lock);
7167d10e4ef2Snarayan 		D2(vswp, "%s(%lld): updating descp %d", __func__,
7168d10e4ef2Snarayan 		    ldcp->ldc_id, idx);
7169d10e4ef2Snarayan 	}
71701ae08745Sheppo 
71711ae08745Sheppo vsw_dringsend_free_exit:
71721ae08745Sheppo 
7173445b4c2eSsb155480 	RW_EXIT(&ldcp->lane_out.dlistrw);
7174445b4c2eSsb155480 
71751ae08745Sheppo 	/* free the message block */
71761ae08745Sheppo 	freemsg(mp);
71771ae08745Sheppo 
71781ae08745Sheppo 	D1(vswp, "%s(%lld): exit\n", __func__, ldcp->ldc_id);
71791ae08745Sheppo 	return (status);
71801ae08745Sheppo }
71811ae08745Sheppo 
71821ae08745Sheppo /*
71831ae08745Sheppo  * Send an in-band descriptor message over ldc.
71841ae08745Sheppo  */
71851ae08745Sheppo static int
71861ae08745Sheppo vsw_descrsend(vsw_ldc_t *ldcp, mblk_t *mp)
71871ae08745Sheppo {
71881ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
7189445b4c2eSsb155480 	vnet_ibnd_desc_t	ibnd_msg;
71901ae08745Sheppo 	vsw_private_desc_t	*priv_desc = NULL;
71911ae08745Sheppo 	dring_info_t		*dp = NULL;
71921ae08745Sheppo 	size_t			n, size = 0;
71931ae08745Sheppo 	caddr_t			bufp;
71941ae08745Sheppo 	mblk_t			*bp;
71951ae08745Sheppo 	int			idx, i;
71961ae08745Sheppo 	int			status = LDC_TX_SUCCESS;
71971ae08745Sheppo 	static int		warn_msg = 1;
71981ae08745Sheppo 
71991ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
72001ae08745Sheppo 
72011ae08745Sheppo 	ASSERT(mp != NULL);
72021ae08745Sheppo 
72031ae08745Sheppo 	if ((!(ldcp->lane_out.lstate & VSW_LANE_ACTIVE)) ||
72041ae08745Sheppo 	    (ldcp->ldc_status != LDC_UP) || (ldcp->ldc_handle == NULL)) {
72051ae08745Sheppo 		DERR(vswp, "%s(%lld) status(%d) state (0x%llx), dropping pkt",
72061ae08745Sheppo 		    __func__, ldcp->ldc_id, ldcp->ldc_status,
72071ae08745Sheppo 		    ldcp->lane_out.lstate);
72081ae08745Sheppo 		freemsg(mp);
72091ae08745Sheppo 		return (LDC_TX_FAILURE);
72101ae08745Sheppo 	}
72111ae08745Sheppo 
72121ae08745Sheppo 	/*
72131ae08745Sheppo 	 * only expect single dring to exist, which we use
72141ae08745Sheppo 	 * as an internal buffer, rather than a transfer channel.
72151ae08745Sheppo 	 */
7216445b4c2eSsb155480 	READ_ENTER(&ldcp->lane_out.dlistrw);
72171ae08745Sheppo 	if ((dp = ldcp->lane_out.dringp) == NULL) {
72181ae08745Sheppo 		DERR(vswp, "%s(%lld): no dring for outbound lane",
72191ae08745Sheppo 		    __func__, ldcp->ldc_id);
7220205eeb1aSlm66018 		DERR(vswp, "%s(%lld) status(%d) state (0x%llx)", __func__,
7221205eeb1aSlm66018 		    ldcp->ldc_id, ldcp->ldc_status, ldcp->lane_out.lstate);
7222445b4c2eSsb155480 		RW_EXIT(&ldcp->lane_out.dlistrw);
72231ae08745Sheppo 		freemsg(mp);
72241ae08745Sheppo 		return (LDC_TX_FAILURE);
72251ae08745Sheppo 	}
72261ae08745Sheppo 
72271ae08745Sheppo 	size = msgsize(mp);
72281ae08745Sheppo 	if (size > (size_t)ETHERMAX) {
7229b071742bSsg70180 		RW_EXIT(&ldcp->lane_out.dlistrw);
72301ae08745Sheppo 		DERR(vswp, "%s(%lld) invalid size (%ld)\n", __func__,
72311ae08745Sheppo 		    ldcp->ldc_id, size);
7232d10e4ef2Snarayan 		freemsg(mp);
7233d10e4ef2Snarayan 		return (LDC_TX_FAILURE);
72341ae08745Sheppo 	}
72351ae08745Sheppo 
72361ae08745Sheppo 	/*
72371ae08745Sheppo 	 * Find a free descriptor in our buffer ring
72381ae08745Sheppo 	 */
72391ae08745Sheppo 	if (vsw_dring_find_free_desc(dp, &priv_desc, &idx) != 0) {
7240b071742bSsg70180 		RW_EXIT(&ldcp->lane_out.dlistrw);
72411ae08745Sheppo 		if (warn_msg) {
72421ae08745Sheppo 			DERR(vswp, "%s(%lld): no descriptor available for ring "
72431ae08745Sheppo 			    "at 0x%llx", __func__, ldcp->ldc_id, dp);
72441ae08745Sheppo 			warn_msg = 0;
72451ae08745Sheppo 		}
72461ae08745Sheppo 
72471ae08745Sheppo 		/* nothing more we can do */
72481ae08745Sheppo 		status = LDC_TX_NORESOURCES;
72491ae08745Sheppo 		goto vsw_descrsend_free_exit;
72501ae08745Sheppo 	} else {
72511ae08745Sheppo 		D2(vswp, "%s(%lld): free private descriptor found at pos "
7252205eeb1aSlm66018 		    "%ld addr 0x%x\n", __func__, ldcp->ldc_id, idx, priv_desc);
72531ae08745Sheppo 		warn_msg = 1;
72541ae08745Sheppo 	}
72551ae08745Sheppo 
72561ae08745Sheppo 	/* copy data into the descriptor */
72571ae08745Sheppo 	bufp = priv_desc->datap;
72581ae08745Sheppo 	for (bp = mp, n = 0; bp != NULL; bp = bp->b_cont) {
72591ae08745Sheppo 		n = MBLKL(bp);
72601ae08745Sheppo 		bcopy(bp->b_rptr, bufp, n);
72611ae08745Sheppo 		bufp += n;
72621ae08745Sheppo 	}
72631ae08745Sheppo 
72641ae08745Sheppo 	priv_desc->datalen = (size < (size_t)ETHERMIN) ? ETHERMIN : size;
72651ae08745Sheppo 
72661ae08745Sheppo 	/* create and send the in-band descp msg */
72671ae08745Sheppo 	ibnd_msg.hdr.tag.vio_msgtype = VIO_TYPE_DATA;
72681ae08745Sheppo 	ibnd_msg.hdr.tag.vio_subtype = VIO_SUBTYPE_INFO;
72691ae08745Sheppo 	ibnd_msg.hdr.tag.vio_subtype_env = VIO_DESC_DATA;
72701ae08745Sheppo 	ibnd_msg.hdr.tag.vio_sid = ldcp->local_session;
72711ae08745Sheppo 
7272d10e4ef2Snarayan 	mutex_enter(&ldcp->lane_out.seq_lock);
72731ae08745Sheppo 	ibnd_msg.hdr.seq_num = ldcp->lane_out.seq_num++;
7274d10e4ef2Snarayan 	mutex_exit(&ldcp->lane_out.seq_lock);
72751ae08745Sheppo 
72761ae08745Sheppo 	/*
72771ae08745Sheppo 	 * Copy the mem cookies describing the data from the
72781ae08745Sheppo 	 * private region of the descriptor ring into the inband
72791ae08745Sheppo 	 * descriptor.
72801ae08745Sheppo 	 */
72811ae08745Sheppo 	for (i = 0; i < priv_desc->ncookies; i++) {
72821ae08745Sheppo 		bcopy(&priv_desc->memcookie[i], &ibnd_msg.memcookie[i],
72831ae08745Sheppo 		    sizeof (ldc_mem_cookie_t));
72841ae08745Sheppo 	}
72851ae08745Sheppo 
72861ae08745Sheppo 	ibnd_msg.hdr.desc_handle = idx;
72871ae08745Sheppo 	ibnd_msg.ncookies = priv_desc->ncookies;
72881ae08745Sheppo 	ibnd_msg.nbytes = size;
72891ae08745Sheppo 
7290b071742bSsg70180 	RW_EXIT(&ldcp->lane_out.dlistrw);
7291b071742bSsg70180 
7292b071742bSsg70180 	(void) vsw_send_msg(ldcp, (void *)&ibnd_msg,
7293b071742bSsg70180 	    sizeof (vnet_ibnd_desc_t), B_TRUE);
72941ae08745Sheppo 
72951ae08745Sheppo vsw_descrsend_free_exit:
72961ae08745Sheppo 
72971ae08745Sheppo 	/* free the allocated message blocks */
72981ae08745Sheppo 	freemsg(mp);
72991ae08745Sheppo 
73001ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
73011ae08745Sheppo 	return (status);
73021ae08745Sheppo }
73031ae08745Sheppo 
73041ae08745Sheppo static void
73053af08d82Slm66018 vsw_send_ver(void *arg)
73061ae08745Sheppo {
73073af08d82Slm66018 	vsw_ldc_t	*ldcp = (vsw_ldc_t *)arg;
73081ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
73091ae08745Sheppo 	lane_t		*lp = &ldcp->lane_out;
73101ae08745Sheppo 	vio_ver_msg_t	ver_msg;
73111ae08745Sheppo 
73121ae08745Sheppo 	D1(vswp, "%s enter", __func__);
73131ae08745Sheppo 
73141ae08745Sheppo 	ver_msg.tag.vio_msgtype = VIO_TYPE_CTRL;
73151ae08745Sheppo 	ver_msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
73161ae08745Sheppo 	ver_msg.tag.vio_subtype_env = VIO_VER_INFO;
73171ae08745Sheppo 	ver_msg.tag.vio_sid = ldcp->local_session;
73181ae08745Sheppo 
73191ae08745Sheppo 	ver_msg.ver_major = vsw_versions[0].ver_major;
73201ae08745Sheppo 	ver_msg.ver_minor = vsw_versions[0].ver_minor;
73211ae08745Sheppo 	ver_msg.dev_class = VDEV_NETWORK_SWITCH;
73221ae08745Sheppo 
73231ae08745Sheppo 	lp->lstate |= VSW_VER_INFO_SENT;
73241ae08745Sheppo 	lp->ver_major = ver_msg.ver_major;
73251ae08745Sheppo 	lp->ver_minor = ver_msg.ver_minor;
73261ae08745Sheppo 
73271ae08745Sheppo 	DUMP_TAG(ver_msg.tag);
73281ae08745Sheppo 
7329b071742bSsg70180 	(void) vsw_send_msg(ldcp, &ver_msg, sizeof (vio_ver_msg_t), B_TRUE);
73301ae08745Sheppo 
73311ae08745Sheppo 	D1(vswp, "%s (%d): exit", __func__, ldcp->ldc_id);
73321ae08745Sheppo }
73331ae08745Sheppo 
73341ae08745Sheppo static void
73351ae08745Sheppo vsw_send_attr(vsw_ldc_t *ldcp)
73361ae08745Sheppo {
73371ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
73381ae08745Sheppo 	lane_t			*lp = &ldcp->lane_out;
73391ae08745Sheppo 	vnet_attr_msg_t		attr_msg;
73401ae08745Sheppo 
73411ae08745Sheppo 	D1(vswp, "%s (%ld) enter", __func__, ldcp->ldc_id);
73421ae08745Sheppo 
73431ae08745Sheppo 	/*
73441ae08745Sheppo 	 * Subtype is set to INFO by default
73451ae08745Sheppo 	 */
73461ae08745Sheppo 	attr_msg.tag.vio_msgtype = VIO_TYPE_CTRL;
73471ae08745Sheppo 	attr_msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
73481ae08745Sheppo 	attr_msg.tag.vio_subtype_env = VIO_ATTR_INFO;
73491ae08745Sheppo 	attr_msg.tag.vio_sid = ldcp->local_session;
73501ae08745Sheppo 
73511ae08745Sheppo 	/* payload copied from default settings for lane */
73521ae08745Sheppo 	attr_msg.mtu = lp->mtu;
73531ae08745Sheppo 	attr_msg.addr_type = lp->addr_type;
73541ae08745Sheppo 	attr_msg.xfer_mode = lp->xfer_mode;
73551ae08745Sheppo 	attr_msg.ack_freq = lp->xfer_mode;
73561ae08745Sheppo 
73571ae08745Sheppo 	READ_ENTER(&vswp->if_lockrw);
73581ae08745Sheppo 	bcopy(&(vswp->if_addr), &(attr_msg.addr), ETHERADDRL);
73591ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
73601ae08745Sheppo 
73611ae08745Sheppo 	ldcp->lane_out.lstate |= VSW_ATTR_INFO_SENT;
73621ae08745Sheppo 
73631ae08745Sheppo 	DUMP_TAG(attr_msg.tag);
73641ae08745Sheppo 
7365b071742bSsg70180 	(void) vsw_send_msg(ldcp, &attr_msg, sizeof (vnet_attr_msg_t), B_TRUE);
73661ae08745Sheppo 
7367b071742bSsg70180 	D1(vswp, "%s (%ld) exit", __func__, ldcp->ldc_id);
73681ae08745Sheppo }
73691ae08745Sheppo 
73701ae08745Sheppo /*
73711ae08745Sheppo  * Create dring info msg (which also results in the creation of
73721ae08745Sheppo  * a dring).
73731ae08745Sheppo  */
73741ae08745Sheppo static vio_dring_reg_msg_t *
73751ae08745Sheppo vsw_create_dring_info_pkt(vsw_ldc_t *ldcp)
73761ae08745Sheppo {
73771ae08745Sheppo 	vio_dring_reg_msg_t	*mp;
73781ae08745Sheppo 	dring_info_t		*dp;
73791ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
73801ae08745Sheppo 
73811ae08745Sheppo 	D1(vswp, "vsw_create_dring_info_pkt enter\n");
73821ae08745Sheppo 
73831ae08745Sheppo 	/*
73841ae08745Sheppo 	 * If we can't create a dring, obviously no point sending
73851ae08745Sheppo 	 * a message.
73861ae08745Sheppo 	 */
73871ae08745Sheppo 	if ((dp = vsw_create_dring(ldcp)) == NULL)
73881ae08745Sheppo 		return (NULL);
73891ae08745Sheppo 
73901ae08745Sheppo 	mp = kmem_zalloc(sizeof (vio_dring_reg_msg_t), KM_SLEEP);
73911ae08745Sheppo 
73921ae08745Sheppo 	mp->tag.vio_msgtype = VIO_TYPE_CTRL;
73931ae08745Sheppo 	mp->tag.vio_subtype = VIO_SUBTYPE_INFO;
73941ae08745Sheppo 	mp->tag.vio_subtype_env = VIO_DRING_REG;
73951ae08745Sheppo 	mp->tag.vio_sid = ldcp->local_session;
73961ae08745Sheppo 
73971ae08745Sheppo 	/* payload */
73981ae08745Sheppo 	mp->num_descriptors = dp->num_descriptors;
73991ae08745Sheppo 	mp->descriptor_size = dp->descriptor_size;
74001ae08745Sheppo 	mp->options = dp->options;
74011ae08745Sheppo 	mp->ncookies = dp->ncookies;
74021ae08745Sheppo 	bcopy(&dp->cookie[0], &mp->cookie[0], sizeof (ldc_mem_cookie_t));
74031ae08745Sheppo 
74041ae08745Sheppo 	mp->dring_ident = 0;
74051ae08745Sheppo 
74061ae08745Sheppo 	D1(vswp, "vsw_create_dring_info_pkt exit\n");
74071ae08745Sheppo 
74081ae08745Sheppo 	return (mp);
74091ae08745Sheppo }
74101ae08745Sheppo 
74111ae08745Sheppo static void
74121ae08745Sheppo vsw_send_dring_info(vsw_ldc_t *ldcp)
74131ae08745Sheppo {
74141ae08745Sheppo 	vio_dring_reg_msg_t	*dring_msg;
74151ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
74161ae08745Sheppo 
74171ae08745Sheppo 	D1(vswp, "%s: (%ld) enter", __func__, ldcp->ldc_id);
74181ae08745Sheppo 
74191ae08745Sheppo 	dring_msg = vsw_create_dring_info_pkt(ldcp);
74201ae08745Sheppo 	if (dring_msg == NULL) {
742134683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: %s: error creating msg",
742234683adeSsg70180 		    vswp->instance, __func__);
74231ae08745Sheppo 		return;
74241ae08745Sheppo 	}
74251ae08745Sheppo 
74261ae08745Sheppo 	ldcp->lane_out.lstate |= VSW_DRING_INFO_SENT;
74271ae08745Sheppo 
74281ae08745Sheppo 	DUMP_TAG_PTR((vio_msg_tag_t *)dring_msg);
74291ae08745Sheppo 
7430b071742bSsg70180 	(void) vsw_send_msg(ldcp, dring_msg,
7431b071742bSsg70180 	    sizeof (vio_dring_reg_msg_t), B_TRUE);
74321ae08745Sheppo 
74331ae08745Sheppo 	kmem_free(dring_msg, sizeof (vio_dring_reg_msg_t));
74341ae08745Sheppo 
74351ae08745Sheppo 	D1(vswp, "%s: (%ld) exit", __func__, ldcp->ldc_id);
74361ae08745Sheppo }
74371ae08745Sheppo 
74381ae08745Sheppo static void
74391ae08745Sheppo vsw_send_rdx(vsw_ldc_t *ldcp)
74401ae08745Sheppo {
74411ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
74421ae08745Sheppo 	vio_rdx_msg_t	rdx_msg;
74431ae08745Sheppo 
74441ae08745Sheppo 	D1(vswp, "%s (%ld) enter", __func__, ldcp->ldc_id);
74451ae08745Sheppo 
74461ae08745Sheppo 	rdx_msg.tag.vio_msgtype = VIO_TYPE_CTRL;
74471ae08745Sheppo 	rdx_msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
74481ae08745Sheppo 	rdx_msg.tag.vio_subtype_env = VIO_RDX;
74491ae08745Sheppo 	rdx_msg.tag.vio_sid = ldcp->local_session;
74501ae08745Sheppo 
7451b071742bSsg70180 	ldcp->lane_in.lstate |= VSW_RDX_INFO_SENT;
74521ae08745Sheppo 
74531ae08745Sheppo 	DUMP_TAG(rdx_msg.tag);
74541ae08745Sheppo 
7455b071742bSsg70180 	(void) vsw_send_msg(ldcp, &rdx_msg, sizeof (vio_rdx_msg_t), B_TRUE);
74561ae08745Sheppo 
74571ae08745Sheppo 	D1(vswp, "%s (%ld) exit", __func__, ldcp->ldc_id);
74581ae08745Sheppo }
74591ae08745Sheppo 
74601ae08745Sheppo /*
74611ae08745Sheppo  * Generic routine to send message out over ldc channel.
7462b071742bSsg70180  *
7463b071742bSsg70180  * It is possible that when we attempt to write over the ldc channel
7464b071742bSsg70180  * that we get notified that it has been reset. Depending on the value
7465b071742bSsg70180  * of the handle_reset flag we either handle that event here or simply
7466b071742bSsg70180  * notify the caller that the channel was reset.
74671ae08745Sheppo  */
7468b071742bSsg70180 static int
7469b071742bSsg70180 vsw_send_msg(vsw_ldc_t *ldcp, void *msgp, int size, boolean_t handle_reset)
74701ae08745Sheppo {
74711ae08745Sheppo 	int		rv;
74721ae08745Sheppo 	size_t		msglen = size;
74731ae08745Sheppo 	vio_msg_tag_t	*tag = (vio_msg_tag_t *)msgp;
74741ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
74751ae08745Sheppo 
74761ae08745Sheppo 	D1(vswp, "vsw_send_msg (%lld) enter : sending %d bytes",
74771ae08745Sheppo 	    ldcp->ldc_id, size);
74781ae08745Sheppo 
74791ae08745Sheppo 	D2(vswp, "send_msg: type 0x%llx", tag->vio_msgtype);
74801ae08745Sheppo 	D2(vswp, "send_msg: stype 0x%llx", tag->vio_subtype);
74811ae08745Sheppo 	D2(vswp, "send_msg: senv 0x%llx", tag->vio_subtype_env);
74821ae08745Sheppo 
74831ae08745Sheppo 	mutex_enter(&ldcp->ldc_txlock);
74841ae08745Sheppo 	do {
74851ae08745Sheppo 		msglen = size;
74861ae08745Sheppo 		rv = ldc_write(ldcp->ldc_handle, (caddr_t)msgp, &msglen);
74871ae08745Sheppo 	} while (rv == EWOULDBLOCK && --vsw_wretries > 0);
74881ae08745Sheppo 
74891ae08745Sheppo 	if ((rv != 0) || (msglen != size)) {
7490205eeb1aSlm66018 		DERR(vswp, "vsw_send_msg:ldc_write failed: chan(%lld) rv(%d) "
7491205eeb1aSlm66018 		    "size (%d) msglen(%d)\n", ldcp->ldc_id, rv, size, msglen);
74921ae08745Sheppo 	}
74933af08d82Slm66018 	mutex_exit(&ldcp->ldc_txlock);
74943af08d82Slm66018 
7495b071742bSsg70180 	/*
7496b071742bSsg70180 	 * If channel has been reset we either handle it here or
7497b071742bSsg70180 	 * simply report back that it has been reset and let caller
7498b071742bSsg70180 	 * decide what to do.
7499b071742bSsg70180 	 */
75003af08d82Slm66018 	if (rv == ECONNRESET) {
7501205eeb1aSlm66018 		DWARN(vswp, "%s (%lld) channel reset", __func__, ldcp->ldc_id);
7502b071742bSsg70180 
7503b071742bSsg70180 		/*
7504b071742bSsg70180 		 * N.B - must never be holding the dlistrw lock when
7505b071742bSsg70180 		 * we do a reset of the channel.
7506b071742bSsg70180 		 */
7507b071742bSsg70180 		if (handle_reset) {
7508b071742bSsg70180 			vsw_process_conn_evt(ldcp, VSW_CONN_RESET);
7509b071742bSsg70180 		}
75103af08d82Slm66018 	}
75111ae08745Sheppo 
7512b071742bSsg70180 	return (rv);
75131ae08745Sheppo }
75141ae08745Sheppo 
75151ae08745Sheppo /*
75161ae08745Sheppo  * Add an entry into FDB, for the given mac address and port_id.
75171ae08745Sheppo  * Returns 0 on success, 1 on failure.
75181ae08745Sheppo  *
75191ae08745Sheppo  * Lock protecting FDB must be held by calling process.
75201ae08745Sheppo  */
75211ae08745Sheppo static int
75221ae08745Sheppo vsw_add_fdb(vsw_t *vswp, vsw_port_t *port)
75231ae08745Sheppo {
75241ae08745Sheppo 	uint64_t	addr = 0;
75251ae08745Sheppo 
75261ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
75271ae08745Sheppo 
75281ae08745Sheppo 	KEY_HASH(addr, port->p_macaddr);
75291ae08745Sheppo 
75301ae08745Sheppo 	D2(vswp, "%s: key = 0x%llx", __func__, addr);
75311ae08745Sheppo 
75321ae08745Sheppo 	/*
75331ae08745Sheppo 	 * Note: duplicate keys will be rejected by mod_hash.
75341ae08745Sheppo 	 */
75351ae08745Sheppo 	if (mod_hash_insert(vswp->fdb, (mod_hash_key_t)addr,
75361ae08745Sheppo 	    (mod_hash_val_t)port) != 0) {
75371ae08745Sheppo 		DERR(vswp, "%s: unable to add entry into fdb.", __func__);
75381ae08745Sheppo 		return (1);
75391ae08745Sheppo 	}
75401ae08745Sheppo 
75411ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
75421ae08745Sheppo 	return (0);
75431ae08745Sheppo }
75441ae08745Sheppo 
75451ae08745Sheppo /*
75461ae08745Sheppo  * Remove an entry from FDB.
75471ae08745Sheppo  * Returns 0 on success, 1 on failure.
75481ae08745Sheppo  */
75491ae08745Sheppo static int
75501ae08745Sheppo vsw_del_fdb(vsw_t *vswp, vsw_port_t *port)
75511ae08745Sheppo {
75521ae08745Sheppo 	uint64_t	addr = 0;
75531ae08745Sheppo 
75541ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
75551ae08745Sheppo 
75561ae08745Sheppo 	KEY_HASH(addr, port->p_macaddr);
75571ae08745Sheppo 
75581ae08745Sheppo 	D2(vswp, "%s: key = 0x%llx", __func__, addr);
75591ae08745Sheppo 
75601ae08745Sheppo 	(void) mod_hash_destroy(vswp->fdb, (mod_hash_val_t)addr);
75611ae08745Sheppo 
75621ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
75631ae08745Sheppo 
75641ae08745Sheppo 	return (0);
75651ae08745Sheppo }
75661ae08745Sheppo 
75671ae08745Sheppo /*
75681ae08745Sheppo  * Search fdb for a given mac address.
75691ae08745Sheppo  * Returns pointer to the entry if found, else returns NULL.
75701ae08745Sheppo  */
75711ae08745Sheppo static vsw_port_t *
75721ae08745Sheppo vsw_lookup_fdb(vsw_t *vswp, struct ether_header *ehp)
75731ae08745Sheppo {
75741ae08745Sheppo 	uint64_t	key = 0;
75751ae08745Sheppo 	vsw_port_t	*port = NULL;
75761ae08745Sheppo 
75771ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
75781ae08745Sheppo 
75791ae08745Sheppo 	KEY_HASH(key, ehp->ether_dhost);
75801ae08745Sheppo 
75811ae08745Sheppo 	D2(vswp, "%s: key = 0x%llx", __func__, key);
75821ae08745Sheppo 
75831ae08745Sheppo 	if (mod_hash_find(vswp->fdb, (mod_hash_key_t)key,
75841ae08745Sheppo 	    (mod_hash_val_t *)&port) != 0) {
758534683adeSsg70180 		D2(vswp, "%s: no port found", __func__);
75861ae08745Sheppo 		return (NULL);
75871ae08745Sheppo 	}
75881ae08745Sheppo 
75891ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
75901ae08745Sheppo 
75911ae08745Sheppo 	return (port);
75921ae08745Sheppo }
75931ae08745Sheppo 
75941ae08745Sheppo /*
75951ae08745Sheppo  * Add or remove multicast address(es).
75961ae08745Sheppo  *
75971ae08745Sheppo  * Returns 0 on success, 1 on failure.
75981ae08745Sheppo  */
75991ae08745Sheppo static int
76001ae08745Sheppo vsw_add_rem_mcst(vnet_mcast_msg_t *mcst_pkt, vsw_port_t *port)
76011ae08745Sheppo {
76021ae08745Sheppo 	mcst_addr_t		*mcst_p = NULL;
76031ae08745Sheppo 	vsw_t			*vswp = port->p_vswp;
76041ae08745Sheppo 	uint64_t		addr = 0x0;
760534683adeSsg70180 	int			i;
76061ae08745Sheppo 
76071ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
76081ae08745Sheppo 
76091ae08745Sheppo 	D2(vswp, "%s: %d addresses", __func__, mcst_pkt->count);
76101ae08745Sheppo 
761134683adeSsg70180 	mutex_enter(&vswp->mac_lock);
761234683adeSsg70180 	if (vswp->mh == NULL) {
761334683adeSsg70180 		mutex_exit(&vswp->mac_lock);
7614e1ebb9ecSlm66018 		return (1);
761534683adeSsg70180 	}
761634683adeSsg70180 	mutex_exit(&vswp->mac_lock);
7617e1ebb9ecSlm66018 
76181ae08745Sheppo 	for (i = 0; i < mcst_pkt->count; i++) {
76191ae08745Sheppo 		/*
76201ae08745Sheppo 		 * Convert address into form that can be used
76211ae08745Sheppo 		 * as hash table key.
76221ae08745Sheppo 		 */
76231ae08745Sheppo 		KEY_HASH(addr, mcst_pkt->mca[i]);
76241ae08745Sheppo 
76251ae08745Sheppo 		/*
76261ae08745Sheppo 		 * Add or delete the specified address/port combination.
76271ae08745Sheppo 		 */
76281ae08745Sheppo 		if (mcst_pkt->set == 0x1) {
76291ae08745Sheppo 			D3(vswp, "%s: adding multicast address 0x%llx for "
76301ae08745Sheppo 			    "port %ld", __func__, addr, port->p_instance);
76311ae08745Sheppo 			if (vsw_add_mcst(vswp, VSW_VNETPORT, addr, port) == 0) {
76321ae08745Sheppo 				/*
76331ae08745Sheppo 				 * Update the list of multicast
76341ae08745Sheppo 				 * addresses contained within the
76351ae08745Sheppo 				 * port structure to include this new
76361ae08745Sheppo 				 * one.
76371ae08745Sheppo 				 */
7638205eeb1aSlm66018 				mcst_p = kmem_alloc(
7639205eeb1aSlm66018 				    sizeof (mcst_addr_t), KM_NOSLEEP);
76401ae08745Sheppo 				if (mcst_p == NULL) {
76411ae08745Sheppo 					DERR(vswp, "%s: unable to alloc mem",
76421ae08745Sheppo 					    __func__);
76431ae08745Sheppo 					return (1);
76441ae08745Sheppo 				}
76451ae08745Sheppo 
76461ae08745Sheppo 				mcst_p->nextp = NULL;
76471ae08745Sheppo 				mcst_p->addr = addr;
76481ae08745Sheppo 
76491ae08745Sheppo 				mutex_enter(&port->mca_lock);
76501ae08745Sheppo 				mcst_p->nextp = port->mcap;
76511ae08745Sheppo 				port->mcap = mcst_p;
76521ae08745Sheppo 				mutex_exit(&port->mca_lock);
76531ae08745Sheppo 
76541ae08745Sheppo 				/*
76551ae08745Sheppo 				 * Program the address into HW. If the addr
76561ae08745Sheppo 				 * has already been programmed then the MAC
76571ae08745Sheppo 				 * just increments a ref counter (which is
76581ae08745Sheppo 				 * used when the address is being deleted)
76591ae08745Sheppo 				 */
766034683adeSsg70180 				mutex_enter(&vswp->mac_lock);
766134683adeSsg70180 				if ((vswp->mh == NULL) ||
766234683adeSsg70180 				    mac_multicst_add(vswp->mh,
766334683adeSsg70180 				    (uchar_t *)&mcst_pkt->mca[i])) {
766434683adeSsg70180 					mutex_exit(&vswp->mac_lock);
766534683adeSsg70180 					cmn_err(CE_WARN, "!vsw%d: unable to "
766634683adeSsg70180 					    "add multicast address",
766734683adeSsg70180 					    vswp->instance);
7668e1ebb9ecSlm66018 					(void) vsw_del_mcst(vswp, VSW_VNETPORT,
7669e1ebb9ecSlm66018 					    addr, port);
7670e1ebb9ecSlm66018 					vsw_del_addr(VSW_VNETPORT, port, addr);
767134683adeSsg70180 					return (1);
7672e1ebb9ecSlm66018 				}
767334683adeSsg70180 				mutex_exit(&vswp->mac_lock);
76741ae08745Sheppo 
76751ae08745Sheppo 			} else {
76761ae08745Sheppo 				DERR(vswp, "%s: error adding multicast "
76771ae08745Sheppo 				    "address 0x%llx for port %ld",
76781ae08745Sheppo 				    __func__, addr, port->p_instance);
76791ae08745Sheppo 				return (1);
76801ae08745Sheppo 			}
76811ae08745Sheppo 		} else {
76821ae08745Sheppo 			/*
76831ae08745Sheppo 			 * Delete an entry from the multicast hash
76841ae08745Sheppo 			 * table and update the address list
76851ae08745Sheppo 			 * appropriately.
76861ae08745Sheppo 			 */
76871ae08745Sheppo 			if (vsw_del_mcst(vswp, VSW_VNETPORT, addr, port) == 0) {
76881ae08745Sheppo 				D3(vswp, "%s: deleting multicast address "
76891ae08745Sheppo 				    "0x%llx for port %ld", __func__, addr,
76901ae08745Sheppo 				    port->p_instance);
76911ae08745Sheppo 
76921ae08745Sheppo 				vsw_del_addr(VSW_VNETPORT, port, addr);
76931ae08745Sheppo 
76941ae08745Sheppo 				/*
76951ae08745Sheppo 				 * Remove the address from HW. The address
76961ae08745Sheppo 				 * will actually only be removed once the ref
76971ae08745Sheppo 				 * count within the MAC layer has dropped to
76981ae08745Sheppo 				 * zero. I.e. we can safely call this fn even
76991ae08745Sheppo 				 * if other ports are interested in this
77001ae08745Sheppo 				 * address.
77011ae08745Sheppo 				 */
770234683adeSsg70180 				mutex_enter(&vswp->mac_lock);
770334683adeSsg70180 				if ((vswp->mh == NULL) ||
770434683adeSsg70180 				    mac_multicst_remove(vswp->mh,
770534683adeSsg70180 				    (uchar_t *)&mcst_pkt->mca[i])) {
770634683adeSsg70180 					mutex_exit(&vswp->mac_lock);
770734683adeSsg70180 					cmn_err(CE_WARN, "!vsw%d: unable to "
770834683adeSsg70180 					    "remove multicast address",
770934683adeSsg70180 					    vswp->instance);
771034683adeSsg70180 					return (1);
771134683adeSsg70180 				}
771234683adeSsg70180 				mutex_exit(&vswp->mac_lock);
77131ae08745Sheppo 
77141ae08745Sheppo 			} else {
77151ae08745Sheppo 				DERR(vswp, "%s: error deleting multicast "
77161ae08745Sheppo 				    "addr 0x%llx for port %ld",
77171ae08745Sheppo 				    __func__, addr, port->p_instance);
77181ae08745Sheppo 				return (1);
77191ae08745Sheppo 			}
77201ae08745Sheppo 		}
77211ae08745Sheppo 	}
77221ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
77231ae08745Sheppo 	return (0);
77241ae08745Sheppo }
77251ae08745Sheppo 
77261ae08745Sheppo /*
77271ae08745Sheppo  * Add a new multicast entry.
77281ae08745Sheppo  *
77291ae08745Sheppo  * Search hash table based on address. If match found then
77301ae08745Sheppo  * update associated val (which is chain of ports), otherwise
77311ae08745Sheppo  * create new key/val (addr/port) pair and insert into table.
77321ae08745Sheppo  */
77331ae08745Sheppo static int
77341ae08745Sheppo vsw_add_mcst(vsw_t *vswp, uint8_t devtype, uint64_t addr, void *arg)
77351ae08745Sheppo {
77361ae08745Sheppo 	int		dup = 0;
77371ae08745Sheppo 	int		rv = 0;
77381ae08745Sheppo 	mfdb_ent_t	*ment = NULL;
77391ae08745Sheppo 	mfdb_ent_t	*tmp_ent = NULL;
77401ae08745Sheppo 	mfdb_ent_t	*new_ent = NULL;
77411ae08745Sheppo 	void		*tgt = NULL;
77421ae08745Sheppo 
77431ae08745Sheppo 	if (devtype == VSW_VNETPORT) {
77441ae08745Sheppo 		/*
77451ae08745Sheppo 		 * Being invoked from a vnet.
77461ae08745Sheppo 		 */
77471ae08745Sheppo 		ASSERT(arg != NULL);
77481ae08745Sheppo 		tgt = arg;
77491ae08745Sheppo 		D2(NULL, "%s: port %d : address 0x%llx", __func__,
77501ae08745Sheppo 		    ((vsw_port_t *)arg)->p_instance, addr);
77511ae08745Sheppo 	} else {
77521ae08745Sheppo 		/*
77531ae08745Sheppo 		 * We are being invoked via the m_multicst mac entry
77541ae08745Sheppo 		 * point.
77551ae08745Sheppo 		 */
77561ae08745Sheppo 		D2(NULL, "%s: address 0x%llx", __func__, addr);
77571ae08745Sheppo 		tgt = (void *)vswp;
77581ae08745Sheppo 	}
77591ae08745Sheppo 
77601ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
77611ae08745Sheppo 	if (mod_hash_find(vswp->mfdb, (mod_hash_key_t)addr,
77621ae08745Sheppo 	    (mod_hash_val_t *)&ment) != 0) {
77631ae08745Sheppo 
77641ae08745Sheppo 		/* address not currently in table */
77651ae08745Sheppo 		ment = kmem_alloc(sizeof (mfdb_ent_t), KM_SLEEP);
77661ae08745Sheppo 		ment->d_addr = (void *)tgt;
77671ae08745Sheppo 		ment->d_type = devtype;
77681ae08745Sheppo 		ment->nextp = NULL;
77691ae08745Sheppo 
77701ae08745Sheppo 		if (mod_hash_insert(vswp->mfdb, (mod_hash_key_t)addr,
77711ae08745Sheppo 		    (mod_hash_val_t)ment) != 0) {
77721ae08745Sheppo 			DERR(vswp, "%s: hash table insertion failed", __func__);
77731ae08745Sheppo 			kmem_free(ment, sizeof (mfdb_ent_t));
77741ae08745Sheppo 			rv = 1;
77751ae08745Sheppo 		} else {
77761ae08745Sheppo 			D2(vswp, "%s: added initial entry for 0x%llx to "
77771ae08745Sheppo 			    "table", __func__, addr);
77781ae08745Sheppo 		}
77791ae08745Sheppo 	} else {
77801ae08745Sheppo 		/*
77811ae08745Sheppo 		 * Address in table. Check to see if specified port
77821ae08745Sheppo 		 * is already associated with the address. If not add
77831ae08745Sheppo 		 * it now.
77841ae08745Sheppo 		 */
77851ae08745Sheppo 		tmp_ent = ment;
77861ae08745Sheppo 		while (tmp_ent != NULL) {
77871ae08745Sheppo 			if (tmp_ent->d_addr == (void *)tgt) {
77881ae08745Sheppo 				if (devtype == VSW_VNETPORT) {
77891ae08745Sheppo 					DERR(vswp, "%s: duplicate port entry "
77901ae08745Sheppo 					    "found for portid %ld and key "
77911ae08745Sheppo 					    "0x%llx", __func__,
77921ae08745Sheppo 					    ((vsw_port_t *)arg)->p_instance,
77931ae08745Sheppo 					    addr);
77941ae08745Sheppo 				} else {
77951ae08745Sheppo 					DERR(vswp, "%s: duplicate entry found"
7796205eeb1aSlm66018 					    "for key 0x%llx", __func__, addr);
77971ae08745Sheppo 				}
77981ae08745Sheppo 				rv = 1;
77991ae08745Sheppo 				dup = 1;
78001ae08745Sheppo 				break;
78011ae08745Sheppo 			}
78021ae08745Sheppo 			tmp_ent = tmp_ent->nextp;
78031ae08745Sheppo 		}
78041ae08745Sheppo 
78051ae08745Sheppo 		/*
78061ae08745Sheppo 		 * Port not on list so add it to end now.
78071ae08745Sheppo 		 */
78081ae08745Sheppo 		if (0 == dup) {
78091ae08745Sheppo 			D2(vswp, "%s: added entry for 0x%llx to table",
78101ae08745Sheppo 			    __func__, addr);
78111ae08745Sheppo 			new_ent = kmem_alloc(sizeof (mfdb_ent_t), KM_SLEEP);
78121ae08745Sheppo 			new_ent->d_addr = (void *)tgt;
78131ae08745Sheppo 			new_ent->d_type = devtype;
78141ae08745Sheppo 			new_ent->nextp = NULL;
78151ae08745Sheppo 
78161ae08745Sheppo 			tmp_ent = ment;
78171ae08745Sheppo 			while (tmp_ent->nextp != NULL)
78181ae08745Sheppo 				tmp_ent = tmp_ent->nextp;
78191ae08745Sheppo 
78201ae08745Sheppo 			tmp_ent->nextp = new_ent;
78211ae08745Sheppo 		}
78221ae08745Sheppo 	}
78231ae08745Sheppo 
78241ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
78251ae08745Sheppo 	return (rv);
78261ae08745Sheppo }
78271ae08745Sheppo 
78281ae08745Sheppo /*
78291ae08745Sheppo  * Remove a multicast entry from the hashtable.
78301ae08745Sheppo  *
78311ae08745Sheppo  * Search hash table based on address. If match found, scan
78321ae08745Sheppo  * list of ports associated with address. If specified port
78331ae08745Sheppo  * found remove it from list.
78341ae08745Sheppo  */
78351ae08745Sheppo static int
78361ae08745Sheppo vsw_del_mcst(vsw_t *vswp, uint8_t devtype, uint64_t addr, void *arg)
78371ae08745Sheppo {
78381ae08745Sheppo 	mfdb_ent_t	*ment = NULL;
78391ae08745Sheppo 	mfdb_ent_t	*curr_p, *prev_p;
78401ae08745Sheppo 	void		*tgt = NULL;
78411ae08745Sheppo 
78421ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
78431ae08745Sheppo 
78441ae08745Sheppo 	if (devtype == VSW_VNETPORT) {
78451ae08745Sheppo 		tgt = (vsw_port_t *)arg;
78461ae08745Sheppo 		D2(vswp, "%s: removing port %d from mFDB for address"
7847205eeb1aSlm66018 		    " 0x%llx", __func__, ((vsw_port_t *)tgt)->p_instance, addr);
78481ae08745Sheppo 	} else {
78491ae08745Sheppo 		D2(vswp, "%s: removing entry", __func__);
78501ae08745Sheppo 		tgt = (void *)vswp;
78511ae08745Sheppo 	}
78521ae08745Sheppo 
78531ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
78541ae08745Sheppo 	if (mod_hash_find(vswp->mfdb, (mod_hash_key_t)addr,
78551ae08745Sheppo 	    (mod_hash_val_t *)&ment) != 0) {
78561ae08745Sheppo 		D2(vswp, "%s: address 0x%llx not in table", __func__, addr);
78571ae08745Sheppo 		RW_EXIT(&vswp->mfdbrw);
78581ae08745Sheppo 		return (1);
78591ae08745Sheppo 	}
78601ae08745Sheppo 
78611ae08745Sheppo 	prev_p = curr_p = ment;
78621ae08745Sheppo 
78631ae08745Sheppo 	while (curr_p != NULL) {
78641ae08745Sheppo 		if (curr_p->d_addr == (void *)tgt) {
78651ae08745Sheppo 			if (devtype == VSW_VNETPORT) {
78661ae08745Sheppo 				D2(vswp, "%s: port %d found", __func__,
78671ae08745Sheppo 				    ((vsw_port_t *)tgt)->p_instance);
78681ae08745Sheppo 			} else {
78691ae08745Sheppo 				D2(vswp, "%s: instance found", __func__);
78701ae08745Sheppo 			}
78711ae08745Sheppo 
78721ae08745Sheppo 			if (prev_p == curr_p) {
78731ae08745Sheppo 				/*
78741ae08745Sheppo 				 * head of list, if no other element is in
78751ae08745Sheppo 				 * list then destroy this entry, otherwise
78761ae08745Sheppo 				 * just replace it with updated value.
78771ae08745Sheppo 				 */
78781ae08745Sheppo 				ment = curr_p->nextp;
78791ae08745Sheppo 				kmem_free(curr_p, sizeof (mfdb_ent_t));
78801ae08745Sheppo 				if (ment == NULL) {
78811ae08745Sheppo 					(void) mod_hash_destroy(vswp->mfdb,
78821ae08745Sheppo 					    (mod_hash_val_t)addr);
78831ae08745Sheppo 				} else {
78841ae08745Sheppo 					(void) mod_hash_replace(vswp->mfdb,
78851ae08745Sheppo 					    (mod_hash_key_t)addr,
78861ae08745Sheppo 					    (mod_hash_val_t)ment);
78871ae08745Sheppo 				}
78881ae08745Sheppo 			} else {
78891ae08745Sheppo 				/*
78901ae08745Sheppo 				 * Not head of list, no need to do
78911ae08745Sheppo 				 * replacement, just adjust list pointers.
78921ae08745Sheppo 				 */
78931ae08745Sheppo 				prev_p->nextp = curr_p->nextp;
78941ae08745Sheppo 				kmem_free(curr_p, sizeof (mfdb_ent_t));
78951ae08745Sheppo 			}
78961ae08745Sheppo 			break;
78971ae08745Sheppo 		}
78981ae08745Sheppo 
78991ae08745Sheppo 		prev_p = curr_p;
79001ae08745Sheppo 		curr_p = curr_p->nextp;
79011ae08745Sheppo 	}
79021ae08745Sheppo 
79031ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
79041ae08745Sheppo 
79051ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
79061ae08745Sheppo 
79071ae08745Sheppo 	return (0);
79081ae08745Sheppo }
79091ae08745Sheppo 
79101ae08745Sheppo /*
79111ae08745Sheppo  * Port is being deleted, but has registered an interest in one
79121ae08745Sheppo  * or more multicast groups. Using the list of addresses maintained
79131ae08745Sheppo  * within the port structure find the appropriate entry in the hash
79141ae08745Sheppo  * table and remove this port from the list of interested ports.
79151ae08745Sheppo  */
79161ae08745Sheppo static void
79171ae08745Sheppo vsw_del_mcst_port(vsw_port_t *port)
79181ae08745Sheppo {
79191ae08745Sheppo 	mcst_addr_t	*mcst_p = NULL;
79201ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
79211ae08745Sheppo 
79221ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
79231ae08745Sheppo 
79241ae08745Sheppo 	mutex_enter(&port->mca_lock);
79251ae08745Sheppo 	while (port->mcap != NULL) {
79261ae08745Sheppo 		(void) vsw_del_mcst(vswp, VSW_VNETPORT,
79271ae08745Sheppo 		    port->mcap->addr, port);
79281ae08745Sheppo 
79291ae08745Sheppo 		mcst_p = port->mcap->nextp;
79301ae08745Sheppo 		kmem_free(port->mcap, sizeof (mcst_addr_t));
79311ae08745Sheppo 		port->mcap = mcst_p;
79321ae08745Sheppo 	}
79331ae08745Sheppo 	mutex_exit(&port->mca_lock);
79341ae08745Sheppo 
79351ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
79361ae08745Sheppo }
79371ae08745Sheppo 
79381ae08745Sheppo /*
79391ae08745Sheppo  * This vsw instance is detaching, but has registered an interest in one
79401ae08745Sheppo  * or more multicast groups. Using the list of addresses maintained
79411ae08745Sheppo  * within the vsw structure find the appropriate entry in the hash
79421ae08745Sheppo  * table and remove this instance from the list of interested ports.
79431ae08745Sheppo  */
79441ae08745Sheppo static void
79451ae08745Sheppo vsw_del_mcst_vsw(vsw_t *vswp)
79461ae08745Sheppo {
79471ae08745Sheppo 	mcst_addr_t	*next_p = NULL;
79481ae08745Sheppo 
79491ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
79501ae08745Sheppo 
79511ae08745Sheppo 	mutex_enter(&vswp->mca_lock);
79521ae08745Sheppo 
79531ae08745Sheppo 	while (vswp->mcap != NULL) {
79541ae08745Sheppo 		DERR(vswp, "%s: deleting addr 0x%llx",
79551ae08745Sheppo 		    __func__, vswp->mcap->addr);
7956205eeb1aSlm66018 		(void) vsw_del_mcst(vswp, VSW_LOCALDEV, vswp->mcap->addr, NULL);
79571ae08745Sheppo 
79581ae08745Sheppo 		next_p = vswp->mcap->nextp;
79591ae08745Sheppo 		kmem_free(vswp->mcap, sizeof (mcst_addr_t));
79601ae08745Sheppo 		vswp->mcap = next_p;
79611ae08745Sheppo 	}
79621ae08745Sheppo 
79631ae08745Sheppo 	vswp->mcap = NULL;
79641ae08745Sheppo 	mutex_exit(&vswp->mca_lock);
79651ae08745Sheppo 
79661ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
79671ae08745Sheppo }
79681ae08745Sheppo 
79691ae08745Sheppo 
79701ae08745Sheppo /*
79711ae08745Sheppo  * Remove the specified address from the list of address maintained
79721ae08745Sheppo  * in this port node.
79731ae08745Sheppo  */
79741ae08745Sheppo static void
79751ae08745Sheppo vsw_del_addr(uint8_t devtype, void *arg, uint64_t addr)
79761ae08745Sheppo {
79771ae08745Sheppo 	vsw_t		*vswp = NULL;
79781ae08745Sheppo 	vsw_port_t	*port = NULL;
79791ae08745Sheppo 	mcst_addr_t	*prev_p = NULL;
79801ae08745Sheppo 	mcst_addr_t	*curr_p = NULL;
79811ae08745Sheppo 
79821ae08745Sheppo 	D1(NULL, "%s: enter : devtype %d : addr 0x%llx",
79831ae08745Sheppo 	    __func__, devtype, addr);
79841ae08745Sheppo 
79851ae08745Sheppo 	if (devtype == VSW_VNETPORT) {
79861ae08745Sheppo 		port = (vsw_port_t *)arg;
79871ae08745Sheppo 		mutex_enter(&port->mca_lock);
79881ae08745Sheppo 		prev_p = curr_p = port->mcap;
79891ae08745Sheppo 	} else {
79901ae08745Sheppo 		vswp = (vsw_t *)arg;
79911ae08745Sheppo 		mutex_enter(&vswp->mca_lock);
79921ae08745Sheppo 		prev_p = curr_p = vswp->mcap;
79931ae08745Sheppo 	}
79941ae08745Sheppo 
79951ae08745Sheppo 	while (curr_p != NULL) {
79961ae08745Sheppo 		if (curr_p->addr == addr) {
79971ae08745Sheppo 			D2(NULL, "%s: address found", __func__);
79981ae08745Sheppo 			/* match found */
79991ae08745Sheppo 			if (prev_p == curr_p) {
80001ae08745Sheppo 				/* list head */
80011ae08745Sheppo 				if (devtype == VSW_VNETPORT)
80021ae08745Sheppo 					port->mcap = curr_p->nextp;
80031ae08745Sheppo 				else
80041ae08745Sheppo 					vswp->mcap = curr_p->nextp;
80051ae08745Sheppo 			} else {
80061ae08745Sheppo 				prev_p->nextp = curr_p->nextp;
80071ae08745Sheppo 			}
80081ae08745Sheppo 			kmem_free(curr_p, sizeof (mcst_addr_t));
80091ae08745Sheppo 			break;
80101ae08745Sheppo 		} else {
80111ae08745Sheppo 			prev_p = curr_p;
80121ae08745Sheppo 			curr_p = curr_p->nextp;
80131ae08745Sheppo 		}
80141ae08745Sheppo 	}
80151ae08745Sheppo 
80161ae08745Sheppo 	if (devtype == VSW_VNETPORT)
80171ae08745Sheppo 		mutex_exit(&port->mca_lock);
80181ae08745Sheppo 	else
80191ae08745Sheppo 		mutex_exit(&vswp->mca_lock);
80201ae08745Sheppo 
80211ae08745Sheppo 	D1(NULL, "%s: exit", __func__);
80221ae08745Sheppo }
80231ae08745Sheppo 
80241ae08745Sheppo /*
80251ae08745Sheppo  * Creates a descriptor ring (dring) and links it into the
80261ae08745Sheppo  * link of outbound drings for this channel.
80271ae08745Sheppo  *
80281ae08745Sheppo  * Returns NULL if creation failed.
80291ae08745Sheppo  */
80301ae08745Sheppo static dring_info_t *
80311ae08745Sheppo vsw_create_dring(vsw_ldc_t *ldcp)
80321ae08745Sheppo {
80331ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
80341ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
80351ae08745Sheppo 	ldc_mem_info_t		minfo;
80361ae08745Sheppo 	dring_info_t		*dp, *tp;
80371ae08745Sheppo 	int			i;
80381ae08745Sheppo 
80391ae08745Sheppo 	dp = (dring_info_t *)kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
80401ae08745Sheppo 
80411ae08745Sheppo 	mutex_init(&dp->dlock, NULL, MUTEX_DRIVER, NULL);
80421ae08745Sheppo 
80431ae08745Sheppo 	/* create public section of ring */
80441ae08745Sheppo 	if ((ldc_mem_dring_create(VSW_RING_NUM_EL,
80451ae08745Sheppo 	    VSW_PUB_SIZE, &dp->handle)) != 0) {
80461ae08745Sheppo 
80471ae08745Sheppo 		DERR(vswp, "vsw_create_dring(%lld): ldc dring create "
80481ae08745Sheppo 		    "failed", ldcp->ldc_id);
80491ae08745Sheppo 		goto create_fail_exit;
80501ae08745Sheppo 	}
80511ae08745Sheppo 
80521ae08745Sheppo 	ASSERT(dp->handle != NULL);
80531ae08745Sheppo 
80541ae08745Sheppo 	/*
80551ae08745Sheppo 	 * Get the base address of the public section of the ring.
80561ae08745Sheppo 	 */
80571ae08745Sheppo 	if ((ldc_mem_dring_info(dp->handle, &minfo)) != 0) {
80581ae08745Sheppo 		DERR(vswp, "vsw_create_dring(%lld): dring info failed\n",
80591ae08745Sheppo 		    ldcp->ldc_id);
80601ae08745Sheppo 		goto dring_fail_exit;
80611ae08745Sheppo 	} else {
80621ae08745Sheppo 		ASSERT(minfo.vaddr != 0);
80631ae08745Sheppo 		dp->pub_addr = minfo.vaddr;
80641ae08745Sheppo 	}
80651ae08745Sheppo 
80661ae08745Sheppo 	dp->num_descriptors = VSW_RING_NUM_EL;
80671ae08745Sheppo 	dp->descriptor_size = VSW_PUB_SIZE;
80681ae08745Sheppo 	dp->options = VIO_TX_DRING;
80691ae08745Sheppo 	dp->ncookies = 1;	/* guaranteed by ldc */
80701ae08745Sheppo 
80711ae08745Sheppo 	/*
80721ae08745Sheppo 	 * create private portion of ring
80731ae08745Sheppo 	 */
80741ae08745Sheppo 	dp->priv_addr = (vsw_private_desc_t *)kmem_zalloc(
80751ae08745Sheppo 	    (sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL), KM_SLEEP);
80761ae08745Sheppo 
80771ae08745Sheppo 	if (vsw_setup_ring(ldcp, dp)) {
80781ae08745Sheppo 		DERR(vswp, "%s: unable to setup ring", __func__);
80791ae08745Sheppo 		goto dring_fail_exit;
80801ae08745Sheppo 	}
80811ae08745Sheppo 
80821ae08745Sheppo 	/* haven't used any descriptors yet */
80831ae08745Sheppo 	dp->end_idx = 0;
8084d10e4ef2Snarayan 	dp->last_ack_recv = -1;
80851ae08745Sheppo 
80861ae08745Sheppo 	/* bind dring to the channel */
80871ae08745Sheppo 	if ((ldc_mem_dring_bind(ldcp->ldc_handle, dp->handle,
80881ae08745Sheppo 	    LDC_SHADOW_MAP, LDC_MEM_RW,
80891ae08745Sheppo 	    &dp->cookie[0], &dp->ncookies)) != 0) {
80901ae08745Sheppo 		DERR(vswp, "vsw_create_dring: unable to bind to channel "
80911ae08745Sheppo 		    "%lld", ldcp->ldc_id);
80921ae08745Sheppo 		goto dring_fail_exit;
80931ae08745Sheppo 	}
80941ae08745Sheppo 
8095d10e4ef2Snarayan 	mutex_init(&dp->restart_lock, NULL, MUTEX_DRIVER, NULL);
8096d10e4ef2Snarayan 	dp->restart_reqd = B_TRUE;
8097d10e4ef2Snarayan 
80981ae08745Sheppo 	/*
80991ae08745Sheppo 	 * Only ever create rings for outgoing lane. Link it onto
81001ae08745Sheppo 	 * end of list.
81011ae08745Sheppo 	 */
8102445b4c2eSsb155480 	WRITE_ENTER(&ldcp->lane_out.dlistrw);
81031ae08745Sheppo 	if (ldcp->lane_out.dringp == NULL) {
81041ae08745Sheppo 		D2(vswp, "vsw_create_dring: adding first outbound ring");
81051ae08745Sheppo 		ldcp->lane_out.dringp = dp;
81061ae08745Sheppo 	} else {
81071ae08745Sheppo 		tp = ldcp->lane_out.dringp;
81081ae08745Sheppo 		while (tp->next != NULL)
81091ae08745Sheppo 			tp = tp->next;
81101ae08745Sheppo 
81111ae08745Sheppo 		tp->next = dp;
81121ae08745Sheppo 	}
8113445b4c2eSsb155480 	RW_EXIT(&ldcp->lane_out.dlistrw);
81141ae08745Sheppo 
81151ae08745Sheppo 	return (dp);
81161ae08745Sheppo 
81171ae08745Sheppo dring_fail_exit:
81181ae08745Sheppo 	(void) ldc_mem_dring_destroy(dp->handle);
81191ae08745Sheppo 
81201ae08745Sheppo create_fail_exit:
81211ae08745Sheppo 	if (dp->priv_addr != NULL) {
81221ae08745Sheppo 		priv_addr = dp->priv_addr;
81231ae08745Sheppo 		for (i = 0; i < VSW_RING_NUM_EL; i++) {
81241ae08745Sheppo 			if (priv_addr->memhandle != NULL)
81251ae08745Sheppo 				(void) ldc_mem_free_handle(
81261ae08745Sheppo 				    priv_addr->memhandle);
81271ae08745Sheppo 			priv_addr++;
81281ae08745Sheppo 		}
81291ae08745Sheppo 		kmem_free(dp->priv_addr,
81301ae08745Sheppo 		    (sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL));
81311ae08745Sheppo 	}
81321ae08745Sheppo 	mutex_destroy(&dp->dlock);
81331ae08745Sheppo 
81341ae08745Sheppo 	kmem_free(dp, sizeof (dring_info_t));
81351ae08745Sheppo 	return (NULL);
81361ae08745Sheppo }
81371ae08745Sheppo 
81381ae08745Sheppo /*
81391ae08745Sheppo  * Create a ring consisting of just a private portion and link
81401ae08745Sheppo  * it into the list of rings for the outbound lane.
81411ae08745Sheppo  *
81421ae08745Sheppo  * These type of rings are used primarily for temporary data
81431ae08745Sheppo  * storage (i.e. as data buffers).
81441ae08745Sheppo  */
81451ae08745Sheppo void
81461ae08745Sheppo vsw_create_privring(vsw_ldc_t *ldcp)
81471ae08745Sheppo {
81481ae08745Sheppo 	dring_info_t		*dp, *tp;
81491ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
81501ae08745Sheppo 
81511ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
81521ae08745Sheppo 
81531ae08745Sheppo 	dp = kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
81541ae08745Sheppo 
81551ae08745Sheppo 	mutex_init(&dp->dlock, NULL, MUTEX_DRIVER, NULL);
81561ae08745Sheppo 
81571ae08745Sheppo 	/* no public section */
81581ae08745Sheppo 	dp->pub_addr = NULL;
81591ae08745Sheppo 
8160205eeb1aSlm66018 	dp->priv_addr = kmem_zalloc(
8161205eeb1aSlm66018 	    (sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL), KM_SLEEP);
81621ae08745Sheppo 
81634bac2208Snarayan 	dp->num_descriptors = VSW_RING_NUM_EL;
81644bac2208Snarayan 
81651ae08745Sheppo 	if (vsw_setup_ring(ldcp, dp)) {
81661ae08745Sheppo 		DERR(vswp, "%s: setup of ring failed", __func__);
81671ae08745Sheppo 		kmem_free(dp->priv_addr,
81681ae08745Sheppo 		    (sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL));
81691ae08745Sheppo 		mutex_destroy(&dp->dlock);
81701ae08745Sheppo 		kmem_free(dp, sizeof (dring_info_t));
81711ae08745Sheppo 		return;
81721ae08745Sheppo 	}
81731ae08745Sheppo 
81741ae08745Sheppo 	/* haven't used any descriptors yet */
81751ae08745Sheppo 	dp->end_idx = 0;
81761ae08745Sheppo 
8177d10e4ef2Snarayan 	mutex_init(&dp->restart_lock, NULL, MUTEX_DRIVER, NULL);
8178d10e4ef2Snarayan 	dp->restart_reqd = B_TRUE;
8179d10e4ef2Snarayan 
81801ae08745Sheppo 	/*
81811ae08745Sheppo 	 * Only ever create rings for outgoing lane. Link it onto
81821ae08745Sheppo 	 * end of list.
81831ae08745Sheppo 	 */
8184445b4c2eSsb155480 	WRITE_ENTER(&ldcp->lane_out.dlistrw);
81851ae08745Sheppo 	if (ldcp->lane_out.dringp == NULL) {
81861ae08745Sheppo 		D2(vswp, "%s: adding first outbound privring", __func__);
81871ae08745Sheppo 		ldcp->lane_out.dringp = dp;
81881ae08745Sheppo 	} else {
81891ae08745Sheppo 		tp = ldcp->lane_out.dringp;
81901ae08745Sheppo 		while (tp->next != NULL)
81911ae08745Sheppo 			tp = tp->next;
81921ae08745Sheppo 
81931ae08745Sheppo 		tp->next = dp;
81941ae08745Sheppo 	}
8195445b4c2eSsb155480 	RW_EXIT(&ldcp->lane_out.dlistrw);
81961ae08745Sheppo 
81971ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
81981ae08745Sheppo }
81991ae08745Sheppo 
82001ae08745Sheppo /*
82011ae08745Sheppo  * Setup the descriptors in the dring. Returns 0 on success, 1 on
82021ae08745Sheppo  * failure.
82031ae08745Sheppo  */
82041ae08745Sheppo int
82051ae08745Sheppo vsw_setup_ring(vsw_ldc_t *ldcp, dring_info_t *dp)
82061ae08745Sheppo {
82071ae08745Sheppo 	vnet_public_desc_t	*pub_addr = NULL;
82081ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
82091ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
82101ae08745Sheppo 	uint64_t		*tmpp;
82111ae08745Sheppo 	uint64_t		offset = 0;
82121ae08745Sheppo 	uint32_t		ncookies = 0;
82131ae08745Sheppo 	static char		*name = "vsw_setup_ring";
8214d10e4ef2Snarayan 	int			i, j, nc, rv;
82151ae08745Sheppo 
82161ae08745Sheppo 	priv_addr = dp->priv_addr;
82171ae08745Sheppo 	pub_addr = dp->pub_addr;
82181ae08745Sheppo 
8219d10e4ef2Snarayan 	/* public section may be null but private should never be */
8220d10e4ef2Snarayan 	ASSERT(priv_addr != NULL);
8221d10e4ef2Snarayan 
82221ae08745Sheppo 	/*
82231ae08745Sheppo 	 * Allocate the region of memory which will be used to hold
82241ae08745Sheppo 	 * the data the descriptors will refer to.
82251ae08745Sheppo 	 */
82261ae08745Sheppo 	dp->data_sz = (VSW_RING_NUM_EL * VSW_RING_EL_DATA_SZ);
82271ae08745Sheppo 	dp->data_addr = kmem_alloc(dp->data_sz, KM_SLEEP);
82281ae08745Sheppo 
82291ae08745Sheppo 	D2(vswp, "%s: allocated %lld bytes at 0x%llx\n", name,
82301ae08745Sheppo 	    dp->data_sz, dp->data_addr);
82311ae08745Sheppo 
82321ae08745Sheppo 	tmpp = (uint64_t *)dp->data_addr;
82331ae08745Sheppo 	offset = VSW_RING_EL_DATA_SZ / sizeof (tmpp);
82341ae08745Sheppo 
82351ae08745Sheppo 	/*
82361ae08745Sheppo 	 * Initialise some of the private and public (if they exist)
82371ae08745Sheppo 	 * descriptor fields.
82381ae08745Sheppo 	 */
82391ae08745Sheppo 	for (i = 0; i < VSW_RING_NUM_EL; i++) {
8240d10e4ef2Snarayan 		mutex_init(&priv_addr->dstate_lock, NULL, MUTEX_DRIVER, NULL);
8241d10e4ef2Snarayan 
82421ae08745Sheppo 		if ((ldc_mem_alloc_handle(ldcp->ldc_handle,
82431ae08745Sheppo 		    &priv_addr->memhandle)) != 0) {
82441ae08745Sheppo 			DERR(vswp, "%s: alloc mem handle failed", name);
82451ae08745Sheppo 			goto setup_ring_cleanup;
82461ae08745Sheppo 		}
82471ae08745Sheppo 
82481ae08745Sheppo 		priv_addr->datap = (void *)tmpp;
82491ae08745Sheppo 
82501ae08745Sheppo 		rv = ldc_mem_bind_handle(priv_addr->memhandle,
82511ae08745Sheppo 		    (caddr_t)priv_addr->datap, VSW_RING_EL_DATA_SZ,
82521ae08745Sheppo 		    LDC_SHADOW_MAP, LDC_MEM_R|LDC_MEM_W,
82531ae08745Sheppo 		    &(priv_addr->memcookie[0]), &ncookies);
82541ae08745Sheppo 		if (rv != 0) {
82551ae08745Sheppo 			DERR(vswp, "%s(%lld): ldc_mem_bind_handle failed "
82561ae08745Sheppo 			    "(rv %d)", name, ldcp->ldc_id, rv);
82571ae08745Sheppo 			goto setup_ring_cleanup;
82581ae08745Sheppo 		}
82591ae08745Sheppo 		priv_addr->bound = 1;
82601ae08745Sheppo 
82611ae08745Sheppo 		D2(vswp, "%s: %d: memcookie 0 : addr 0x%llx : size 0x%llx",
82621ae08745Sheppo 		    name, i, priv_addr->memcookie[0].addr,
82631ae08745Sheppo 		    priv_addr->memcookie[0].size);
82641ae08745Sheppo 
82651ae08745Sheppo 		if (ncookies >= (uint32_t)(VSW_MAX_COOKIES + 1)) {
82661ae08745Sheppo 			DERR(vswp, "%s(%lld) ldc_mem_bind_handle returned "
82671ae08745Sheppo 			    "invalid num of cookies (%d) for size 0x%llx",
8268205eeb1aSlm66018 			    name, ldcp->ldc_id, ncookies, VSW_RING_EL_DATA_SZ);
82691ae08745Sheppo 
82701ae08745Sheppo 			goto setup_ring_cleanup;
82711ae08745Sheppo 		} else {
82721ae08745Sheppo 			for (j = 1; j < ncookies; j++) {
82731ae08745Sheppo 				rv = ldc_mem_nextcookie(priv_addr->memhandle,
82741ae08745Sheppo 				    &(priv_addr->memcookie[j]));
82751ae08745Sheppo 				if (rv != 0) {
82761ae08745Sheppo 					DERR(vswp, "%s: ldc_mem_nextcookie "
82771ae08745Sheppo 					    "failed rv (%d)", name, rv);
82781ae08745Sheppo 					goto setup_ring_cleanup;
82791ae08745Sheppo 				}
82801ae08745Sheppo 				D3(vswp, "%s: memcookie %d : addr 0x%llx : "
82811ae08745Sheppo 				    "size 0x%llx", name, j,
82821ae08745Sheppo 				    priv_addr->memcookie[j].addr,
82831ae08745Sheppo 				    priv_addr->memcookie[j].size);
82841ae08745Sheppo 			}
82851ae08745Sheppo 
82861ae08745Sheppo 		}
82871ae08745Sheppo 		priv_addr->ncookies = ncookies;
82881ae08745Sheppo 		priv_addr->dstate = VIO_DESC_FREE;
82891ae08745Sheppo 
82901ae08745Sheppo 		if (pub_addr != NULL) {
82911ae08745Sheppo 
82921ae08745Sheppo 			/* link pub and private sides */
82931ae08745Sheppo 			priv_addr->descp = pub_addr;
82941ae08745Sheppo 
8295d10e4ef2Snarayan 			pub_addr->ncookies = priv_addr->ncookies;
8296d10e4ef2Snarayan 
8297d10e4ef2Snarayan 			for (nc = 0; nc < pub_addr->ncookies; nc++) {
8298d10e4ef2Snarayan 				bcopy(&priv_addr->memcookie[nc],
8299d10e4ef2Snarayan 				    &pub_addr->memcookie[nc],
8300d10e4ef2Snarayan 				    sizeof (ldc_mem_cookie_t));
8301d10e4ef2Snarayan 			}
8302d10e4ef2Snarayan 
83031ae08745Sheppo 			pub_addr->hdr.dstate = VIO_DESC_FREE;
83041ae08745Sheppo 			pub_addr++;
83051ae08745Sheppo 		}
83061ae08745Sheppo 
83071ae08745Sheppo 		/*
83081ae08745Sheppo 		 * move to next element in the dring and the next
83091ae08745Sheppo 		 * position in the data buffer.
83101ae08745Sheppo 		 */
83111ae08745Sheppo 		priv_addr++;
83121ae08745Sheppo 		tmpp += offset;
83131ae08745Sheppo 	}
83141ae08745Sheppo 
83151ae08745Sheppo 	return (0);
83161ae08745Sheppo 
83171ae08745Sheppo setup_ring_cleanup:
83181ae08745Sheppo 	priv_addr = dp->priv_addr;
83191ae08745Sheppo 
8320d10e4ef2Snarayan 	for (j = 0; j < i; j++) {
83211ae08745Sheppo 		(void) ldc_mem_unbind_handle(priv_addr->memhandle);
83221ae08745Sheppo 		(void) ldc_mem_free_handle(priv_addr->memhandle);
83231ae08745Sheppo 
8324d10e4ef2Snarayan 		mutex_destroy(&priv_addr->dstate_lock);
8325d10e4ef2Snarayan 
83261ae08745Sheppo 		priv_addr++;
83271ae08745Sheppo 	}
83281ae08745Sheppo 	kmem_free(dp->data_addr, dp->data_sz);
83291ae08745Sheppo 
83301ae08745Sheppo 	return (1);
83311ae08745Sheppo }
83321ae08745Sheppo 
83331ae08745Sheppo /*
83341ae08745Sheppo  * Searches the private section of a ring for a free descriptor,
83351ae08745Sheppo  * starting at the location of the last free descriptor found
83361ae08745Sheppo  * previously.
83371ae08745Sheppo  *
8338d10e4ef2Snarayan  * Returns 0 if free descriptor is available, and updates state
8339d10e4ef2Snarayan  * of private descriptor to VIO_DESC_READY,  otherwise returns 1.
83401ae08745Sheppo  *
83411ae08745Sheppo  * FUTURE: might need to return contiguous range of descriptors
83421ae08745Sheppo  * as dring info msg assumes all will be contiguous.
83431ae08745Sheppo  */
83441ae08745Sheppo static int
83451ae08745Sheppo vsw_dring_find_free_desc(dring_info_t *dringp,
83461ae08745Sheppo 		vsw_private_desc_t **priv_p, int *idx)
83471ae08745Sheppo {
8348d10e4ef2Snarayan 	vsw_private_desc_t	*addr = NULL;
83491ae08745Sheppo 	int			num = VSW_RING_NUM_EL;
83501ae08745Sheppo 	int			ret = 1;
83511ae08745Sheppo 
83521ae08745Sheppo 	D1(NULL, "%s enter\n", __func__);
83531ae08745Sheppo 
8354d10e4ef2Snarayan 	ASSERT(dringp->priv_addr != NULL);
83551ae08745Sheppo 
83561ae08745Sheppo 	D2(NULL, "%s: searching ring, dringp 0x%llx : start pos %lld",
8357d10e4ef2Snarayan 	    __func__, dringp, dringp->end_idx);
83581ae08745Sheppo 
8359d10e4ef2Snarayan 	addr = (vsw_private_desc_t *)dringp->priv_addr + dringp->end_idx;
8360d10e4ef2Snarayan 
8361d10e4ef2Snarayan 	mutex_enter(&addr->dstate_lock);
83621ae08745Sheppo 	if (addr->dstate == VIO_DESC_FREE) {
8363d10e4ef2Snarayan 		addr->dstate = VIO_DESC_READY;
83641ae08745Sheppo 		*priv_p = addr;
8365d10e4ef2Snarayan 		*idx = dringp->end_idx;
8366d10e4ef2Snarayan 		dringp->end_idx = (dringp->end_idx + 1) % num;
83671ae08745Sheppo 		ret = 0;
8368d10e4ef2Snarayan 
83691ae08745Sheppo 	}
8370d10e4ef2Snarayan 	mutex_exit(&addr->dstate_lock);
83711ae08745Sheppo 
83721ae08745Sheppo 	/* ring full */
83731ae08745Sheppo 	if (ret == 1) {
8374d10e4ef2Snarayan 		D2(NULL, "%s: no desp free: started at %d", __func__,
8375d10e4ef2Snarayan 		    dringp->end_idx);
83761ae08745Sheppo 	}
83771ae08745Sheppo 
83781ae08745Sheppo 	D1(NULL, "%s: exit\n", __func__);
83791ae08745Sheppo 
83801ae08745Sheppo 	return (ret);
83811ae08745Sheppo }
83821ae08745Sheppo 
83831ae08745Sheppo /*
83841ae08745Sheppo  * Map from a dring identifier to the ring itself. Returns
83851ae08745Sheppo  * pointer to ring or NULL if no match found.
8386445b4c2eSsb155480  *
8387445b4c2eSsb155480  * Should be called with dlistrw rwlock held as reader.
83881ae08745Sheppo  */
83891ae08745Sheppo static dring_info_t *
83901ae08745Sheppo vsw_ident2dring(lane_t *lane, uint64_t ident)
83911ae08745Sheppo {
83921ae08745Sheppo 	dring_info_t	*dp = NULL;
83931ae08745Sheppo 
83941ae08745Sheppo 	if ((dp = lane->dringp) == NULL) {
83951ae08745Sheppo 		return (NULL);
83961ae08745Sheppo 	} else {
83971ae08745Sheppo 		if (dp->ident == ident)
83981ae08745Sheppo 			return (dp);
83991ae08745Sheppo 
84001ae08745Sheppo 		while (dp != NULL) {
84011ae08745Sheppo 			if (dp->ident == ident)
84021ae08745Sheppo 				break;
84031ae08745Sheppo 			dp = dp->next;
84041ae08745Sheppo 		}
84051ae08745Sheppo 	}
84061ae08745Sheppo 
84071ae08745Sheppo 	return (dp);
84081ae08745Sheppo }
84091ae08745Sheppo 
84101ae08745Sheppo /*
84111ae08745Sheppo  * Set the default lane attributes. These are copied into
84121ae08745Sheppo  * the attr msg we send to our peer. If they are not acceptable
84131ae08745Sheppo  * then (currently) the handshake ends.
84141ae08745Sheppo  */
84151ae08745Sheppo static void
84161ae08745Sheppo vsw_set_lane_attr(vsw_t *vswp, lane_t *lp)
84171ae08745Sheppo {
84181ae08745Sheppo 	bzero(lp, sizeof (lane_t));
84191ae08745Sheppo 
84201ae08745Sheppo 	READ_ENTER(&vswp->if_lockrw);
84211ae08745Sheppo 	ether_copy(&(vswp->if_addr), &(lp->addr));
84221ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
84231ae08745Sheppo 
84241ae08745Sheppo 	lp->mtu = VSW_MTU;
84251ae08745Sheppo 	lp->addr_type = ADDR_TYPE_MAC;
84261ae08745Sheppo 	lp->xfer_mode = VIO_DRING_MODE;
84271ae08745Sheppo 	lp->ack_freq = 0;	/* for shared mode */
8428d10e4ef2Snarayan 
8429d10e4ef2Snarayan 	mutex_enter(&lp->seq_lock);
84301ae08745Sheppo 	lp->seq_num = VNET_ISS;
8431d10e4ef2Snarayan 	mutex_exit(&lp->seq_lock);
84321ae08745Sheppo }
84331ae08745Sheppo 
84341ae08745Sheppo /*
84351ae08745Sheppo  * Verify that the attributes are acceptable.
84361ae08745Sheppo  *
84371ae08745Sheppo  * FUTURE: If some attributes are not acceptable, change them
84381ae08745Sheppo  * our desired values.
84391ae08745Sheppo  */
84401ae08745Sheppo static int
84411ae08745Sheppo vsw_check_attr(vnet_attr_msg_t *pkt, vsw_port_t *port)
84421ae08745Sheppo {
84431ae08745Sheppo 	int	ret = 0;
84441ae08745Sheppo 
84451ae08745Sheppo 	D1(NULL, "vsw_check_attr enter\n");
84461ae08745Sheppo 
84471ae08745Sheppo 	/*
84481ae08745Sheppo 	 * Note we currently only support in-band descriptors
84491ae08745Sheppo 	 * and descriptor rings, not packet based transfer (VIO_PKT_MODE)
84501ae08745Sheppo 	 */
84511ae08745Sheppo 	if ((pkt->xfer_mode != VIO_DESC_MODE) &&
84521ae08745Sheppo 	    (pkt->xfer_mode != VIO_DRING_MODE)) {
8453205eeb1aSlm66018 		D2(NULL, "vsw_check_attr: unknown mode %x\n", pkt->xfer_mode);
84541ae08745Sheppo 		ret = 1;
84551ae08745Sheppo 	}
84561ae08745Sheppo 
84571ae08745Sheppo 	/* Only support MAC addresses at moment. */
84581ae08745Sheppo 	if ((pkt->addr_type != ADDR_TYPE_MAC) || (pkt->addr == 0)) {
84591ae08745Sheppo 		D2(NULL, "vsw_check_attr: invalid addr_type %x, "
8460205eeb1aSlm66018 		    "or address 0x%llx\n", pkt->addr_type, pkt->addr);
84611ae08745Sheppo 		ret = 1;
84621ae08745Sheppo 	}
84631ae08745Sheppo 
84641ae08745Sheppo 	/*
84651ae08745Sheppo 	 * MAC address supplied by device should match that stored
84661ae08745Sheppo 	 * in the vsw-port OBP node. Need to decide what to do if they
84671ae08745Sheppo 	 * don't match, for the moment just warn but don't fail.
84681ae08745Sheppo 	 */
84691ae08745Sheppo 	if (bcmp(&pkt->addr, &port->p_macaddr, ETHERADDRL) != 0) {
84701ae08745Sheppo 		DERR(NULL, "vsw_check_attr: device supplied address "
84711ae08745Sheppo 		    "0x%llx doesn't match node address 0x%llx\n",
84721ae08745Sheppo 		    pkt->addr, port->p_macaddr);
84731ae08745Sheppo 	}
84741ae08745Sheppo 
84751ae08745Sheppo 	/*
84761ae08745Sheppo 	 * Ack freq only makes sense in pkt mode, in shared
84771ae08745Sheppo 	 * mode the ring descriptors say whether or not to
84781ae08745Sheppo 	 * send back an ACK.
84791ae08745Sheppo 	 */
84801ae08745Sheppo 	if ((pkt->xfer_mode == VIO_DRING_MODE) &&
84811ae08745Sheppo 	    (pkt->ack_freq > 0)) {
84821ae08745Sheppo 		D2(NULL, "vsw_check_attr: non zero ack freq "
84831ae08745Sheppo 		    " in SHM mode\n");
84841ae08745Sheppo 		ret = 1;
84851ae08745Sheppo 	}
84861ae08745Sheppo 
84871ae08745Sheppo 	/*
84881ae08745Sheppo 	 * Note: for the moment we only support ETHER
84891ae08745Sheppo 	 * frames. This may change in the future.
84901ae08745Sheppo 	 */
84911ae08745Sheppo 	if ((pkt->mtu > VSW_MTU) || (pkt->mtu <= 0)) {
84921ae08745Sheppo 		D2(NULL, "vsw_check_attr: invalid MTU (0x%llx)\n",
84931ae08745Sheppo 		    pkt->mtu);
84941ae08745Sheppo 		ret = 1;
84951ae08745Sheppo 	}
84961ae08745Sheppo 
84971ae08745Sheppo 	D1(NULL, "vsw_check_attr exit\n");
84981ae08745Sheppo 
84991ae08745Sheppo 	return (ret);
85001ae08745Sheppo }
85011ae08745Sheppo 
85021ae08745Sheppo /*
85031ae08745Sheppo  * Returns 1 if there is a problem, 0 otherwise.
85041ae08745Sheppo  */
85051ae08745Sheppo static int
85061ae08745Sheppo vsw_check_dring_info(vio_dring_reg_msg_t *pkt)
85071ae08745Sheppo {
85081ae08745Sheppo 	_NOTE(ARGUNUSED(pkt))
85091ae08745Sheppo 
85101ae08745Sheppo 	int	ret = 0;
85111ae08745Sheppo 
85121ae08745Sheppo 	D1(NULL, "vsw_check_dring_info enter\n");
85131ae08745Sheppo 
85141ae08745Sheppo 	if ((pkt->num_descriptors == 0) ||
85151ae08745Sheppo 	    (pkt->descriptor_size == 0) ||
85161ae08745Sheppo 	    (pkt->ncookies != 1)) {
85171ae08745Sheppo 		DERR(NULL, "vsw_check_dring_info: invalid dring msg");
85181ae08745Sheppo 		ret = 1;
85191ae08745Sheppo 	}
85201ae08745Sheppo 
85211ae08745Sheppo 	D1(NULL, "vsw_check_dring_info exit\n");
85221ae08745Sheppo 
85231ae08745Sheppo 	return (ret);
85241ae08745Sheppo }
85251ae08745Sheppo 
85261ae08745Sheppo /*
85271ae08745Sheppo  * Returns 1 if two memory cookies match. Otherwise returns 0.
85281ae08745Sheppo  */
85291ae08745Sheppo static int
85301ae08745Sheppo vsw_mem_cookie_match(ldc_mem_cookie_t *m1, ldc_mem_cookie_t *m2)
85311ae08745Sheppo {
85321ae08745Sheppo 	if ((m1->addr != m2->addr) ||
85331ae08745Sheppo 	    (m2->size != m2->size)) {
85341ae08745Sheppo 		return (0);
85351ae08745Sheppo 	} else {
85361ae08745Sheppo 		return (1);
85371ae08745Sheppo 	}
85381ae08745Sheppo }
85391ae08745Sheppo 
85401ae08745Sheppo /*
85411ae08745Sheppo  * Returns 1 if ring described in reg message matches that
85421ae08745Sheppo  * described by dring_info structure. Otherwise returns 0.
85431ae08745Sheppo  */
85441ae08745Sheppo static int
85451ae08745Sheppo vsw_dring_match(dring_info_t *dp, vio_dring_reg_msg_t *msg)
85461ae08745Sheppo {
85471ae08745Sheppo 	if ((msg->descriptor_size != dp->descriptor_size) ||
85481ae08745Sheppo 	    (msg->num_descriptors != dp->num_descriptors) ||
85491ae08745Sheppo 	    (msg->ncookies != dp->ncookies) ||
85501ae08745Sheppo 	    !(vsw_mem_cookie_match(&msg->cookie[0], &dp->cookie[0]))) {
85511ae08745Sheppo 		return (0);
85521ae08745Sheppo 	} else {
85531ae08745Sheppo 		return (1);
85541ae08745Sheppo 	}
85551ae08745Sheppo 
85561ae08745Sheppo }
85571ae08745Sheppo 
85581ae08745Sheppo static caddr_t
85591ae08745Sheppo vsw_print_ethaddr(uint8_t *a, char *ebuf)
85601ae08745Sheppo {
85611ae08745Sheppo 	(void) sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
85621ae08745Sheppo 	    a[0], a[1], a[2], a[3], a[4], a[5]);
85631ae08745Sheppo 	return (ebuf);
85641ae08745Sheppo }
85651ae08745Sheppo 
85661ae08745Sheppo /*
85671ae08745Sheppo  * Reset and free all the resources associated with
85681ae08745Sheppo  * the channel.
85691ae08745Sheppo  */
85701ae08745Sheppo static void
85711ae08745Sheppo vsw_free_lane_resources(vsw_ldc_t *ldcp, uint64_t dir)
85721ae08745Sheppo {
85731ae08745Sheppo 	dring_info_t		*dp, *dpp;
85741ae08745Sheppo 	lane_t			*lp = NULL;
85751ae08745Sheppo 	int			rv = 0;
85761ae08745Sheppo 
85771ae08745Sheppo 	ASSERT(ldcp != NULL);
85781ae08745Sheppo 
85791ae08745Sheppo 	D1(ldcp->ldc_vswp, "%s (%lld): enter", __func__, ldcp->ldc_id);
85801ae08745Sheppo 
85811ae08745Sheppo 	if (dir == INBOUND) {
85821ae08745Sheppo 		D2(ldcp->ldc_vswp, "%s: freeing INBOUND lane"
85831ae08745Sheppo 		    " of channel %lld", __func__, ldcp->ldc_id);
85841ae08745Sheppo 		lp = &ldcp->lane_in;
85851ae08745Sheppo 	} else {
85861ae08745Sheppo 		D2(ldcp->ldc_vswp, "%s: freeing OUTBOUND lane"
85871ae08745Sheppo 		    " of channel %lld", __func__, ldcp->ldc_id);
85881ae08745Sheppo 		lp = &ldcp->lane_out;
85891ae08745Sheppo 	}
85901ae08745Sheppo 
85911ae08745Sheppo 	lp->lstate = VSW_LANE_INACTIV;
8592d10e4ef2Snarayan 	mutex_enter(&lp->seq_lock);
85931ae08745Sheppo 	lp->seq_num = VNET_ISS;
8594d10e4ef2Snarayan 	mutex_exit(&lp->seq_lock);
85951ae08745Sheppo 	if (lp->dringp) {
85961ae08745Sheppo 		if (dir == INBOUND) {
8597445b4c2eSsb155480 			WRITE_ENTER(&lp->dlistrw);
85981ae08745Sheppo 			dp = lp->dringp;
85991ae08745Sheppo 			while (dp != NULL) {
86001ae08745Sheppo 				dpp = dp->next;
86011ae08745Sheppo 				if (dp->handle != NULL)
86021ae08745Sheppo 					(void) ldc_mem_dring_unmap(dp->handle);
86031ae08745Sheppo 				kmem_free(dp, sizeof (dring_info_t));
86041ae08745Sheppo 				dp = dpp;
86051ae08745Sheppo 			}
8606445b4c2eSsb155480 			RW_EXIT(&lp->dlistrw);
86071ae08745Sheppo 		} else {
86081ae08745Sheppo 			/*
86091ae08745Sheppo 			 * unbind, destroy exported dring, free dring struct
86101ae08745Sheppo 			 */
8611445b4c2eSsb155480 			WRITE_ENTER(&lp->dlistrw);
86121ae08745Sheppo 			dp = lp->dringp;
86131ae08745Sheppo 			rv = vsw_free_ring(dp);
8614445b4c2eSsb155480 			RW_EXIT(&lp->dlistrw);
86151ae08745Sheppo 		}
86161ae08745Sheppo 		if (rv == 0) {
86171ae08745Sheppo 			lp->dringp = NULL;
86181ae08745Sheppo 		}
86191ae08745Sheppo 	}
86201ae08745Sheppo 
86211ae08745Sheppo 	D1(ldcp->ldc_vswp, "%s (%lld): exit", __func__, ldcp->ldc_id);
86221ae08745Sheppo }
86231ae08745Sheppo 
86241ae08745Sheppo /*
86251ae08745Sheppo  * Free ring and all associated resources.
8626445b4c2eSsb155480  *
8627445b4c2eSsb155480  * Should be called with dlistrw rwlock held as writer.
86281ae08745Sheppo  */
86291ae08745Sheppo static int
86301ae08745Sheppo vsw_free_ring(dring_info_t *dp)
86311ae08745Sheppo {
86321ae08745Sheppo 	vsw_private_desc_t	*paddr = NULL;
86331ae08745Sheppo 	dring_info_t		*dpp;
86341ae08745Sheppo 	int			i, rv = 1;
86351ae08745Sheppo 
86361ae08745Sheppo 	while (dp != NULL) {
86371ae08745Sheppo 		mutex_enter(&dp->dlock);
86381ae08745Sheppo 		dpp = dp->next;
86391ae08745Sheppo 		if (dp->priv_addr != NULL) {
86401ae08745Sheppo 			/*
86411ae08745Sheppo 			 * First unbind and free the memory handles
86421ae08745Sheppo 			 * stored in each descriptor within the ring.
86431ae08745Sheppo 			 */
86441ae08745Sheppo 			for (i = 0; i < VSW_RING_NUM_EL; i++) {
86451ae08745Sheppo 				paddr = (vsw_private_desc_t *)
86461ae08745Sheppo 				    dp->priv_addr + i;
86471ae08745Sheppo 				if (paddr->memhandle != NULL) {
86481ae08745Sheppo 					if (paddr->bound == 1) {
86491ae08745Sheppo 						rv = ldc_mem_unbind_handle(
86501ae08745Sheppo 						    paddr->memhandle);
86511ae08745Sheppo 
86521ae08745Sheppo 						if (rv != 0) {
86531ae08745Sheppo 							DERR(NULL, "error "
86541ae08745Sheppo 							"unbinding handle for "
86551ae08745Sheppo 							"ring 0x%llx at pos %d",
86561ae08745Sheppo 							    dp, i);
86571ae08745Sheppo 							mutex_exit(&dp->dlock);
86581ae08745Sheppo 							return (rv);
86591ae08745Sheppo 						}
86601ae08745Sheppo 						paddr->bound = 0;
86611ae08745Sheppo 					}
86621ae08745Sheppo 
86631ae08745Sheppo 					rv = ldc_mem_free_handle(
86641ae08745Sheppo 					    paddr->memhandle);
86651ae08745Sheppo 					if (rv != 0) {
86661ae08745Sheppo 						DERR(NULL, "error freeing "
8667205eeb1aSlm66018 						    "handle for ring 0x%llx "
8668205eeb1aSlm66018 						    "at pos %d", dp, i);
86691ae08745Sheppo 						mutex_exit(&dp->dlock);
86701ae08745Sheppo 						return (rv);
86711ae08745Sheppo 					}
86721ae08745Sheppo 					paddr->memhandle = NULL;
86731ae08745Sheppo 				}
8674d10e4ef2Snarayan 				mutex_destroy(&paddr->dstate_lock);
86751ae08745Sheppo 			}
8676205eeb1aSlm66018 			kmem_free(dp->priv_addr,
8677205eeb1aSlm66018 			    (sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL));
86781ae08745Sheppo 		}
86791ae08745Sheppo 
86801ae08745Sheppo 		/*
86811ae08745Sheppo 		 * Now unbind and destroy the ring itself.
86821ae08745Sheppo 		 */
86831ae08745Sheppo 		if (dp->handle != NULL) {
86841ae08745Sheppo 			(void) ldc_mem_dring_unbind(dp->handle);
86851ae08745Sheppo 			(void) ldc_mem_dring_destroy(dp->handle);
86861ae08745Sheppo 		}
86871ae08745Sheppo 
86881ae08745Sheppo 		if (dp->data_addr != NULL) {
86891ae08745Sheppo 			kmem_free(dp->data_addr, dp->data_sz);
86901ae08745Sheppo 		}
86911ae08745Sheppo 
86921ae08745Sheppo 		mutex_exit(&dp->dlock);
86931ae08745Sheppo 		mutex_destroy(&dp->dlock);
8694d10e4ef2Snarayan 		mutex_destroy(&dp->restart_lock);
86951ae08745Sheppo 		kmem_free(dp, sizeof (dring_info_t));
86961ae08745Sheppo 
86971ae08745Sheppo 		dp = dpp;
86981ae08745Sheppo 	}
86991ae08745Sheppo 	return (0);
87001ae08745Sheppo }
87011ae08745Sheppo 
87021ae08745Sheppo /*
87031ae08745Sheppo  * Debugging routines
87041ae08745Sheppo  */
87051ae08745Sheppo static void
87061ae08745Sheppo display_state(void)
87071ae08745Sheppo {
87081ae08745Sheppo 	vsw_t		*vswp;
87091ae08745Sheppo 	vsw_port_list_t	*plist;
87101ae08745Sheppo 	vsw_port_t 	*port;
87111ae08745Sheppo 	vsw_ldc_list_t	*ldcl;
87121ae08745Sheppo 	vsw_ldc_t 	*ldcp;
87131ae08745Sheppo 
87141ae08745Sheppo 	cmn_err(CE_NOTE, "***** system state *****");
87151ae08745Sheppo 
87161ae08745Sheppo 	for (vswp = vsw_head; vswp; vswp = vswp->next) {
87171ae08745Sheppo 		plist = &vswp->plist;
87181ae08745Sheppo 		READ_ENTER(&plist->lockrw);
87191ae08745Sheppo 		cmn_err(CE_CONT, "vsw instance %d has %d ports attached\n",
87201ae08745Sheppo 		    vswp->instance, plist->num_ports);
87211ae08745Sheppo 
87221ae08745Sheppo 		for (port = plist->head; port != NULL; port = port->p_next) {
87231ae08745Sheppo 			ldcl = &port->p_ldclist;
87241ae08745Sheppo 			cmn_err(CE_CONT, "port %d : %d ldcs attached\n",
87251ae08745Sheppo 			    port->p_instance, ldcl->num_ldcs);
87261ae08745Sheppo 			READ_ENTER(&ldcl->lockrw);
87271ae08745Sheppo 			ldcp = ldcl->head;
87281ae08745Sheppo 			for (; ldcp != NULL; ldcp = ldcp->ldc_next) {
87291ae08745Sheppo 				cmn_err(CE_CONT, "chan %lu : dev %d : "
87301ae08745Sheppo 				    "status %d : phase %u\n",
87311ae08745Sheppo 				    ldcp->ldc_id, ldcp->dev_class,
87321ae08745Sheppo 				    ldcp->ldc_status, ldcp->hphase);
87331ae08745Sheppo 				cmn_err(CE_CONT, "chan %lu : lsession %lu : "
8734205eeb1aSlm66018 				    "psession %lu\n", ldcp->ldc_id,
8735205eeb1aSlm66018 				    ldcp->local_session, ldcp->peer_session);
87361ae08745Sheppo 
87371ae08745Sheppo 				cmn_err(CE_CONT, "Inbound lane:\n");
87381ae08745Sheppo 				display_lane(&ldcp->lane_in);
87391ae08745Sheppo 				cmn_err(CE_CONT, "Outbound lane:\n");
87401ae08745Sheppo 				display_lane(&ldcp->lane_out);
87411ae08745Sheppo 			}
87421ae08745Sheppo 			RW_EXIT(&ldcl->lockrw);
87431ae08745Sheppo 		}
87441ae08745Sheppo 		RW_EXIT(&plist->lockrw);
87451ae08745Sheppo 	}
87461ae08745Sheppo 	cmn_err(CE_NOTE, "***** system state *****");
87471ae08745Sheppo }
87481ae08745Sheppo 
87491ae08745Sheppo static void
87501ae08745Sheppo display_lane(lane_t *lp)
87511ae08745Sheppo {
87521ae08745Sheppo 	dring_info_t	*drp;
87531ae08745Sheppo 
87541ae08745Sheppo 	cmn_err(CE_CONT, "ver 0x%x:0x%x : state %lx : mtu 0x%lx\n",
87551ae08745Sheppo 	    lp->ver_major, lp->ver_minor, lp->lstate, lp->mtu);
87561ae08745Sheppo 	cmn_err(CE_CONT, "addr_type %d : addr 0x%lx : xmode %d\n",
87571ae08745Sheppo 	    lp->addr_type, lp->addr, lp->xfer_mode);
87581ae08745Sheppo 	cmn_err(CE_CONT, "dringp 0x%lx\n", (uint64_t)lp->dringp);
87591ae08745Sheppo 
87601ae08745Sheppo 	cmn_err(CE_CONT, "Dring info:\n");
87611ae08745Sheppo 	for (drp = lp->dringp; drp != NULL; drp = drp->next) {
87621ae08745Sheppo 		cmn_err(CE_CONT, "\tnum_desc %u : dsize %u\n",
87631ae08745Sheppo 		    drp->num_descriptors, drp->descriptor_size);
87641ae08745Sheppo 		cmn_err(CE_CONT, "\thandle 0x%lx\n", drp->handle);
87651ae08745Sheppo 		cmn_err(CE_CONT, "\tpub_addr 0x%lx : priv_addr 0x%lx\n",
87661ae08745Sheppo 		    (uint64_t)drp->pub_addr, (uint64_t)drp->priv_addr);
87671ae08745Sheppo 		cmn_err(CE_CONT, "\tident 0x%lx : end_idx %lu\n",
87681ae08745Sheppo 		    drp->ident, drp->end_idx);
87691ae08745Sheppo 		display_ring(drp);
87701ae08745Sheppo 	}
87711ae08745Sheppo }
87721ae08745Sheppo 
87731ae08745Sheppo static void
87741ae08745Sheppo display_ring(dring_info_t *dringp)
87751ae08745Sheppo {
87761ae08745Sheppo 	uint64_t		i;
87771ae08745Sheppo 	uint64_t		priv_count = 0;
87781ae08745Sheppo 	uint64_t		pub_count = 0;
87791ae08745Sheppo 	vnet_public_desc_t	*pub_addr = NULL;
87801ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
87811ae08745Sheppo 
87821ae08745Sheppo 	for (i = 0; i < VSW_RING_NUM_EL; i++) {
87831ae08745Sheppo 		if (dringp->pub_addr != NULL) {
87841ae08745Sheppo 			pub_addr = (vnet_public_desc_t *)dringp->pub_addr + i;
87851ae08745Sheppo 
87861ae08745Sheppo 			if (pub_addr->hdr.dstate == VIO_DESC_FREE)
87871ae08745Sheppo 				pub_count++;
87881ae08745Sheppo 		}
87891ae08745Sheppo 
87901ae08745Sheppo 		if (dringp->priv_addr != NULL) {
8791205eeb1aSlm66018 			priv_addr = (vsw_private_desc_t *)dringp->priv_addr + i;
87921ae08745Sheppo 
87931ae08745Sheppo 			if (priv_addr->dstate == VIO_DESC_FREE)
87941ae08745Sheppo 				priv_count++;
87951ae08745Sheppo 		}
87961ae08745Sheppo 	}
87971ae08745Sheppo 	cmn_err(CE_CONT, "\t%lu elements: %lu priv free: %lu pub free\n",
87981ae08745Sheppo 	    i, priv_count, pub_count);
87991ae08745Sheppo }
88001ae08745Sheppo 
88011ae08745Sheppo static void
88021ae08745Sheppo dump_flags(uint64_t state)
88031ae08745Sheppo {
88041ae08745Sheppo 	int	i;
88051ae08745Sheppo 
88061ae08745Sheppo 	typedef struct flag_name {
88071ae08745Sheppo 		int	flag_val;
88081ae08745Sheppo 		char	*flag_name;
88091ae08745Sheppo 	} flag_name_t;
88101ae08745Sheppo 
88111ae08745Sheppo 	flag_name_t	flags[] = {
88121ae08745Sheppo 		VSW_VER_INFO_SENT, "VSW_VER_INFO_SENT",
88131ae08745Sheppo 		VSW_VER_INFO_RECV, "VSW_VER_INFO_RECV",
88141ae08745Sheppo 		VSW_VER_ACK_RECV, "VSW_VER_ACK_RECV",
88151ae08745Sheppo 		VSW_VER_ACK_SENT, "VSW_VER_ACK_SENT",
88161ae08745Sheppo 		VSW_VER_NACK_RECV, "VSW_VER_NACK_RECV",
88171ae08745Sheppo 		VSW_VER_NACK_SENT, "VSW_VER_NACK_SENT",
88181ae08745Sheppo 		VSW_ATTR_INFO_SENT, "VSW_ATTR_INFO_SENT",
88191ae08745Sheppo 		VSW_ATTR_INFO_RECV, "VSW_ATTR_INFO_RECV",
88201ae08745Sheppo 		VSW_ATTR_ACK_SENT, "VSW_ATTR_ACK_SENT",
88211ae08745Sheppo 		VSW_ATTR_ACK_RECV, "VSW_ATTR_ACK_RECV",
88221ae08745Sheppo 		VSW_ATTR_NACK_SENT, "VSW_ATTR_NACK_SENT",
88231ae08745Sheppo 		VSW_ATTR_NACK_RECV, "VSW_ATTR_NACK_RECV",
88241ae08745Sheppo 		VSW_DRING_INFO_SENT, "VSW_DRING_INFO_SENT",
88251ae08745Sheppo 		VSW_DRING_INFO_RECV, "VSW_DRING_INFO_RECV",
88261ae08745Sheppo 		VSW_DRING_ACK_SENT, "VSW_DRING_ACK_SENT",
88271ae08745Sheppo 		VSW_DRING_ACK_RECV, "VSW_DRING_ACK_RECV",
88281ae08745Sheppo 		VSW_DRING_NACK_SENT, "VSW_DRING_NACK_SENT",
88291ae08745Sheppo 		VSW_DRING_NACK_RECV, "VSW_DRING_NACK_RECV",
88301ae08745Sheppo 		VSW_RDX_INFO_SENT, "VSW_RDX_INFO_SENT",
88311ae08745Sheppo 		VSW_RDX_INFO_RECV, "VSW_RDX_INFO_RECV",
88321ae08745Sheppo 		VSW_RDX_ACK_SENT, "VSW_RDX_ACK_SENT",
88331ae08745Sheppo 		VSW_RDX_ACK_RECV, "VSW_RDX_ACK_RECV",
88341ae08745Sheppo 		VSW_RDX_NACK_SENT, "VSW_RDX_NACK_SENT",
88351ae08745Sheppo 		VSW_RDX_NACK_RECV, "VSW_RDX_NACK_RECV",
88361ae08745Sheppo 		VSW_MCST_INFO_SENT, "VSW_MCST_INFO_SENT",
88371ae08745Sheppo 		VSW_MCST_INFO_RECV, "VSW_MCST_INFO_RECV",
88381ae08745Sheppo 		VSW_MCST_ACK_SENT, "VSW_MCST_ACK_SENT",
88391ae08745Sheppo 		VSW_MCST_ACK_RECV, "VSW_MCST_ACK_RECV",
88401ae08745Sheppo 		VSW_MCST_NACK_SENT, "VSW_MCST_NACK_SENT",
88411ae08745Sheppo 		VSW_MCST_NACK_RECV, "VSW_MCST_NACK_RECV",
88421ae08745Sheppo 		VSW_LANE_ACTIVE, "VSW_LANE_ACTIVE"};
88431ae08745Sheppo 
88441ae08745Sheppo 	DERR(NULL, "DUMP_FLAGS: %llx\n", state);
88451ae08745Sheppo 	for (i = 0; i < sizeof (flags)/sizeof (flag_name_t); i++) {
88461ae08745Sheppo 		if (state & flags[i].flag_val)
88471ae08745Sheppo 			DERR(NULL, "DUMP_FLAGS %s", flags[i].flag_name);
88481ae08745Sheppo 	}
88491ae08745Sheppo }
8850