xref: /titanic_50/usr/src/uts/sun4v/io/vsw.c (revision 34683adecebe88ca2c857e28be4749f3a083f9fc)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo 
221ae08745Sheppo /*
231ae08745Sheppo  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
241ae08745Sheppo  * Use is subject to license terms.
251ae08745Sheppo  */
261ae08745Sheppo 
271ae08745Sheppo #pragma ident	"%Z%%M%	%I%	%E% SMI"
281ae08745Sheppo 
291ae08745Sheppo #include <sys/types.h>
301ae08745Sheppo #include <sys/errno.h>
311ae08745Sheppo #include <sys/debug.h>
321ae08745Sheppo #include <sys/time.h>
331ae08745Sheppo #include <sys/sysmacros.h>
341ae08745Sheppo #include <sys/systm.h>
351ae08745Sheppo #include <sys/user.h>
361ae08745Sheppo #include <sys/stropts.h>
371ae08745Sheppo #include <sys/stream.h>
381ae08745Sheppo #include <sys/strlog.h>
391ae08745Sheppo #include <sys/strsubr.h>
401ae08745Sheppo #include <sys/cmn_err.h>
411ae08745Sheppo #include <sys/cpu.h>
421ae08745Sheppo #include <sys/kmem.h>
431ae08745Sheppo #include <sys/conf.h>
441ae08745Sheppo #include <sys/ddi.h>
451ae08745Sheppo #include <sys/sunddi.h>
461ae08745Sheppo #include <sys/ksynch.h>
471ae08745Sheppo #include <sys/stat.h>
481ae08745Sheppo #include <sys/kstat.h>
491ae08745Sheppo #include <sys/vtrace.h>
501ae08745Sheppo #include <sys/strsun.h>
511ae08745Sheppo #include <sys/dlpi.h>
521ae08745Sheppo #include <sys/ethernet.h>
531ae08745Sheppo #include <net/if.h>
541ae08745Sheppo #include <sys/varargs.h>
551ae08745Sheppo #include <sys/machsystm.h>
561ae08745Sheppo #include <sys/modctl.h>
571ae08745Sheppo #include <sys/modhash.h>
581ae08745Sheppo #include <sys/mac.h>
59ba2e4443Sseb #include <sys/mac_ether.h>
601ae08745Sheppo #include <sys/taskq.h>
611ae08745Sheppo #include <sys/note.h>
621ae08745Sheppo #include <sys/mach_descrip.h>
631ae08745Sheppo #include <sys/mac.h>
641ae08745Sheppo #include <sys/mdeg.h>
651ae08745Sheppo #include <sys/ldc.h>
661ae08745Sheppo #include <sys/vsw_fdb.h>
671ae08745Sheppo #include <sys/vsw.h>
681ae08745Sheppo #include <sys/vio_mailbox.h>
691ae08745Sheppo #include <sys/vnet_mailbox.h>
701ae08745Sheppo #include <sys/vnet_common.h>
71d10e4ef2Snarayan #include <sys/vio_util.h>
72d10e4ef2Snarayan #include <sys/sdt.h>
731ae08745Sheppo 
741ae08745Sheppo /*
751ae08745Sheppo  * Function prototypes.
761ae08745Sheppo  */
771ae08745Sheppo static	int vsw_attach(dev_info_t *, ddi_attach_cmd_t);
781ae08745Sheppo static	int vsw_detach(dev_info_t *, ddi_detach_cmd_t);
791ae08745Sheppo static	int vsw_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
80*34683adeSsg70180 static	int vsw_get_md_physname(vsw_t *, md_t *, mde_cookie_t, char *);
81*34683adeSsg70180 static	int vsw_get_md_smodes(vsw_t *, md_t *, mde_cookie_t, uint8_t *, int *);
82e1ebb9ecSlm66018 static	int vsw_get_physaddr(vsw_t *);
83*34683adeSsg70180 static	int vsw_setup_switching(vsw_t *);
841ae08745Sheppo static	int vsw_setup_layer2(vsw_t *);
851ae08745Sheppo static	int vsw_setup_layer3(vsw_t *);
861ae08745Sheppo 
877636cb21Slm66018 /* MAC Ring table functions. */
887636cb21Slm66018 static void vsw_mac_ring_tbl_init(vsw_t *vswp);
897636cb21Slm66018 static void vsw_mac_ring_tbl_destroy(vsw_t *vswp);
907636cb21Slm66018 static void vsw_queue_worker(vsw_mac_ring_t *rrp);
917636cb21Slm66018 static void vsw_queue_stop(vsw_queue_t *vqp);
927636cb21Slm66018 static vsw_queue_t *vsw_queue_create();
937636cb21Slm66018 static void vsw_queue_destroy(vsw_queue_t *vqp);
947636cb21Slm66018 
951ae08745Sheppo /* MAC layer routines */
967636cb21Slm66018 static mac_resource_handle_t vsw_mac_ring_add_cb(void *arg,
977636cb21Slm66018 		mac_resource_t *mrp);
98e1ebb9ecSlm66018 static	int vsw_get_hw_maddr(vsw_t *);
99e1ebb9ecSlm66018 static	int vsw_set_hw(vsw_t *, vsw_port_t *);
100e1ebb9ecSlm66018 static	int vsw_set_hw_promisc(vsw_t *, vsw_port_t *);
101e1ebb9ecSlm66018 static	int vsw_unset_hw(vsw_t *, vsw_port_t *);
102e1ebb9ecSlm66018 static	int vsw_unset_hw_promisc(vsw_t *, vsw_port_t *);
103e1ebb9ecSlm66018 static	int vsw_reconfig_hw(vsw_t *);
1047636cb21Slm66018 static int vsw_mac_attach(vsw_t *vswp);
1057636cb21Slm66018 static void vsw_mac_detach(vsw_t *vswp);
1067636cb21Slm66018 
1077636cb21Slm66018 static void vsw_rx_queue_cb(void *, mac_resource_handle_t, mblk_t *);
1081ae08745Sheppo static void vsw_rx_cb(void *, mac_resource_handle_t, mblk_t *);
1091ae08745Sheppo static mblk_t *vsw_tx_msg(vsw_t *, mblk_t *);
1101ae08745Sheppo static int vsw_mac_register(vsw_t *);
1111ae08745Sheppo static int vsw_mac_unregister(vsw_t *);
112ba2e4443Sseb static int vsw_m_stat(void *, uint_t, uint64_t *);
1131ae08745Sheppo static void vsw_m_stop(void *arg);
1141ae08745Sheppo static int vsw_m_start(void *arg);
1151ae08745Sheppo static int vsw_m_unicst(void *arg, const uint8_t *);
1161ae08745Sheppo static int vsw_m_multicst(void *arg, boolean_t, const uint8_t *);
1171ae08745Sheppo static int vsw_m_promisc(void *arg, boolean_t);
1181ae08745Sheppo static mblk_t *vsw_m_tx(void *arg, mblk_t *);
1191ae08745Sheppo 
1201ae08745Sheppo /* MDEG routines */
121*34683adeSsg70180 static	int vsw_mdeg_register(vsw_t *vswp);
1221ae08745Sheppo static	void vsw_mdeg_unregister(vsw_t *vswp);
1231ae08745Sheppo static	int vsw_mdeg_cb(void *cb_argp, mdeg_result_t *);
124*34683adeSsg70180 static	int vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *);
125*34683adeSsg70180 static	void vsw_get_initial_md_properties(vsw_t *vswp, md_t *, mde_cookie_t);
126*34683adeSsg70180 static	void vsw_update_md_prop(vsw_t *, md_t *, mde_cookie_t);
1271ae08745Sheppo 
1281ae08745Sheppo /* Port add/deletion routines */
1291ae08745Sheppo static	int vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node);
1301ae08745Sheppo static	int vsw_port_attach(vsw_t *vswp, int p_instance,
1311ae08745Sheppo 	uint64_t *ldcids, int nids, struct ether_addr *macaddr);
1321ae08745Sheppo static	int vsw_detach_ports(vsw_t *vswp);
1331ae08745Sheppo static	int vsw_port_detach(vsw_t *vswp, int p_instance);
1341ae08745Sheppo static	int vsw_port_delete(vsw_port_t *port);
1351ae08745Sheppo static	int vsw_ldc_attach(vsw_port_t *port, uint64_t ldc_id);
1361ae08745Sheppo static	int vsw_ldc_detach(vsw_port_t *port, uint64_t ldc_id);
1371ae08745Sheppo static	int vsw_init_ldcs(vsw_port_t *port);
1381ae08745Sheppo static	int vsw_uninit_ldcs(vsw_port_t *port);
1391ae08745Sheppo static	int vsw_ldc_init(vsw_ldc_t *ldcp);
1401ae08745Sheppo static	int vsw_ldc_uninit(vsw_ldc_t *ldcp);
1411ae08745Sheppo static	int vsw_drain_ldcs(vsw_port_t *port);
1421ae08745Sheppo static	int vsw_drain_port_taskq(vsw_port_t *port);
1431ae08745Sheppo static	void vsw_marker_task(void *);
1441ae08745Sheppo static	vsw_port_t *vsw_lookup_port(vsw_t *vswp, int p_instance);
1451ae08745Sheppo static	int vsw_plist_del_node(vsw_t *, vsw_port_t *port);
1461ae08745Sheppo 
1471ae08745Sheppo /* Interrupt routines */
1481ae08745Sheppo static	uint_t vsw_ldc_cb(uint64_t cb, caddr_t arg);
1491ae08745Sheppo 
1501ae08745Sheppo /* Handshake routines */
1513af08d82Slm66018 static	void vsw_restart_ldc(vsw_ldc_t *);
1521ae08745Sheppo static	void vsw_restart_handshake(vsw_ldc_t *);
1533af08d82Slm66018 static	void vsw_handle_reset(vsw_ldc_t *);
1541ae08745Sheppo static	int vsw_check_flag(vsw_ldc_t *, int, uint64_t);
1551ae08745Sheppo static	void vsw_next_milestone(vsw_ldc_t *);
1561ae08745Sheppo static	int vsw_supported_version(vio_ver_msg_t *);
1571ae08745Sheppo 
1581ae08745Sheppo /* Data processing routines */
1591ae08745Sheppo static void vsw_process_pkt(void *);
1601ae08745Sheppo static void vsw_dispatch_ctrl_task(vsw_ldc_t *, void *, vio_msg_tag_t);
1611ae08745Sheppo static void vsw_process_ctrl_pkt(void *);
1621ae08745Sheppo static void vsw_process_ctrl_ver_pkt(vsw_ldc_t *, void *);
1631ae08745Sheppo static void vsw_process_ctrl_attr_pkt(vsw_ldc_t *, void *);
1641ae08745Sheppo static void vsw_process_ctrl_mcst_pkt(vsw_ldc_t *, void *);
1651ae08745Sheppo static void vsw_process_ctrl_dring_reg_pkt(vsw_ldc_t *, void *);
1661ae08745Sheppo static void vsw_process_ctrl_dring_unreg_pkt(vsw_ldc_t *, void *);
1671ae08745Sheppo static void vsw_process_ctrl_rdx_pkt(vsw_ldc_t *, void *);
1681ae08745Sheppo static void vsw_process_data_pkt(vsw_ldc_t *, void *, vio_msg_tag_t);
1691ae08745Sheppo static void vsw_process_data_dring_pkt(vsw_ldc_t *, void *);
1701ae08745Sheppo static void vsw_process_data_raw_pkt(vsw_ldc_t *, void *);
1711ae08745Sheppo static void vsw_process_data_ibnd_pkt(vsw_ldc_t *, void *);
1721ae08745Sheppo static void vsw_process_err_pkt(vsw_ldc_t *, void *, vio_msg_tag_t);
1731ae08745Sheppo 
1741ae08745Sheppo /* Switching/data transmit routines */
1751ae08745Sheppo static	void vsw_switch_l2_frame(vsw_t *vswp, mblk_t *mp, int caller,
1761ae08745Sheppo 	    vsw_port_t *port, mac_resource_handle_t);
1771ae08745Sheppo static	void vsw_switch_l3_frame(vsw_t *vswp, mblk_t *mp, int caller,
1781ae08745Sheppo 	    vsw_port_t *port, mac_resource_handle_t);
1791ae08745Sheppo static	int vsw_forward_all(vsw_t *vswp, mblk_t *mp, int caller,
1801ae08745Sheppo 	    vsw_port_t *port);
1811ae08745Sheppo static	int vsw_forward_grp(vsw_t *vswp, mblk_t *mp, int caller,
1821ae08745Sheppo 	    vsw_port_t *port);
1831ae08745Sheppo static	int vsw_portsend(vsw_port_t *, mblk_t *);
1841ae08745Sheppo static	int vsw_dringsend(vsw_ldc_t *, mblk_t *);
1851ae08745Sheppo static	int vsw_descrsend(vsw_ldc_t *, mblk_t *);
1861ae08745Sheppo 
1871ae08745Sheppo /* Packet creation routines */
1883af08d82Slm66018 static void vsw_send_ver(void *);
1891ae08745Sheppo static void vsw_send_attr(vsw_ldc_t *);
1901ae08745Sheppo static vio_dring_reg_msg_t *vsw_create_dring_info_pkt(vsw_ldc_t *);
1911ae08745Sheppo static void vsw_send_dring_info(vsw_ldc_t *);
1921ae08745Sheppo static void vsw_send_rdx(vsw_ldc_t *);
1931ae08745Sheppo 
1941ae08745Sheppo static void vsw_send_msg(vsw_ldc_t *, void *, int);
1951ae08745Sheppo 
1961ae08745Sheppo /* Forwarding database (FDB) routines */
1971ae08745Sheppo static	int vsw_add_fdb(vsw_t *vswp, vsw_port_t *port);
1981ae08745Sheppo static	int vsw_del_fdb(vsw_t *vswp, vsw_port_t *port);
1991ae08745Sheppo static	vsw_port_t *vsw_lookup_fdb(vsw_t *vswp, struct ether_header *);
2001ae08745Sheppo static	int vsw_add_rem_mcst(vnet_mcast_msg_t *, vsw_port_t *);
2011ae08745Sheppo static	int vsw_add_mcst(vsw_t *, uint8_t, uint64_t, void *);
2021ae08745Sheppo static	int vsw_del_mcst(vsw_t *, uint8_t, uint64_t, void *);
2031ae08745Sheppo static	void vsw_del_addr(uint8_t, void *, uint64_t);
2041ae08745Sheppo static	void vsw_del_mcst_port(vsw_port_t *);
2051ae08745Sheppo static	void vsw_del_mcst_vsw(vsw_t *);
2061ae08745Sheppo 
2071ae08745Sheppo /* Dring routines */
2081ae08745Sheppo static dring_info_t *vsw_create_dring(vsw_ldc_t *);
2091ae08745Sheppo static void vsw_create_privring(vsw_ldc_t *);
2101ae08745Sheppo static int vsw_setup_ring(vsw_ldc_t *ldcp, dring_info_t *dp);
2111ae08745Sheppo static int vsw_dring_find_free_desc(dring_info_t *, vsw_private_desc_t **,
2121ae08745Sheppo     int *);
2131ae08745Sheppo static dring_info_t *vsw_ident2dring(lane_t *, uint64_t);
2141ae08745Sheppo 
2151ae08745Sheppo static void vsw_set_lane_attr(vsw_t *, lane_t *);
2161ae08745Sheppo static int vsw_check_attr(vnet_attr_msg_t *, vsw_port_t *);
2171ae08745Sheppo static int vsw_dring_match(dring_info_t *dp, vio_dring_reg_msg_t *msg);
2181ae08745Sheppo static int vsw_mem_cookie_match(ldc_mem_cookie_t *, ldc_mem_cookie_t *);
2191ae08745Sheppo static int vsw_check_dring_info(vio_dring_reg_msg_t *);
2201ae08745Sheppo 
2211ae08745Sheppo /* Misc support routines */
2221ae08745Sheppo static	caddr_t vsw_print_ethaddr(uint8_t *addr, char *ebuf);
2231ae08745Sheppo static void vsw_free_lane_resources(vsw_ldc_t *, uint64_t);
2241ae08745Sheppo static int vsw_free_ring(dring_info_t *);
2251ae08745Sheppo 
2261ae08745Sheppo /* Debugging routines */
2271ae08745Sheppo static void dump_flags(uint64_t);
2281ae08745Sheppo static void display_state(void);
2291ae08745Sheppo static void display_lane(lane_t *);
2301ae08745Sheppo static void display_ring(dring_info_t *);
2311ae08745Sheppo 
2321ae08745Sheppo int	vsw_num_handshakes = 3;		/* # of handshake attempts */
2331ae08745Sheppo int	vsw_wretries = 100;		/* # of write attempts */
234d10e4ef2Snarayan int	vsw_chain_len = 150;		/* max # of mblks in msg chain */
235d10e4ef2Snarayan int	vsw_desc_delay = 0;		/* delay in us */
236d10e4ef2Snarayan int	vsw_read_attempts = 5;		/* # of reads of descriptor */
237d10e4ef2Snarayan 
238d10e4ef2Snarayan uint32_t	vsw_mblk_size = VSW_MBLK_SIZE;
239d10e4ef2Snarayan uint32_t	vsw_num_mblks = VSW_NUM_MBLKS;
240d10e4ef2Snarayan 
241ba2e4443Sseb static	mac_callbacks_t	vsw_m_callbacks = {
242ba2e4443Sseb 	0,
243ba2e4443Sseb 	vsw_m_stat,
244ba2e4443Sseb 	vsw_m_start,
245ba2e4443Sseb 	vsw_m_stop,
246ba2e4443Sseb 	vsw_m_promisc,
247ba2e4443Sseb 	vsw_m_multicst,
248ba2e4443Sseb 	vsw_m_unicst,
249ba2e4443Sseb 	vsw_m_tx,
250ba2e4443Sseb 	NULL,
251ba2e4443Sseb 	NULL,
252ba2e4443Sseb 	NULL
253ba2e4443Sseb };
254ba2e4443Sseb 
2551ae08745Sheppo static	struct	cb_ops	vsw_cb_ops = {
2561ae08745Sheppo 	nulldev,			/* cb_open */
2571ae08745Sheppo 	nulldev,			/* cb_close */
2581ae08745Sheppo 	nodev,				/* cb_strategy */
2591ae08745Sheppo 	nodev,				/* cb_print */
2601ae08745Sheppo 	nodev,				/* cb_dump */
2611ae08745Sheppo 	nodev,				/* cb_read */
2621ae08745Sheppo 	nodev,				/* cb_write */
2631ae08745Sheppo 	nodev,				/* cb_ioctl */
2641ae08745Sheppo 	nodev,				/* cb_devmap */
2651ae08745Sheppo 	nodev,				/* cb_mmap */
2661ae08745Sheppo 	nodev,				/* cb_segmap */
2671ae08745Sheppo 	nochpoll,			/* cb_chpoll */
2681ae08745Sheppo 	ddi_prop_op,			/* cb_prop_op */
2691ae08745Sheppo 	NULL,				/* cb_stream */
2701ae08745Sheppo 	D_MP,				/* cb_flag */
2711ae08745Sheppo 	CB_REV,				/* rev */
2721ae08745Sheppo 	nodev,				/* int (*cb_aread)() */
2731ae08745Sheppo 	nodev				/* int (*cb_awrite)() */
2741ae08745Sheppo };
2751ae08745Sheppo 
2761ae08745Sheppo static	struct	dev_ops	vsw_ops = {
2771ae08745Sheppo 	DEVO_REV,		/* devo_rev */
2781ae08745Sheppo 	0,			/* devo_refcnt */
2791ae08745Sheppo 	vsw_getinfo,		/* devo_getinfo */
2801ae08745Sheppo 	nulldev,		/* devo_identify */
2811ae08745Sheppo 	nulldev,		/* devo_probe */
2821ae08745Sheppo 	vsw_attach,		/* devo_attach */
2831ae08745Sheppo 	vsw_detach,		/* devo_detach */
2841ae08745Sheppo 	nodev,			/* devo_reset */
2851ae08745Sheppo 	&vsw_cb_ops,		/* devo_cb_ops */
2861ae08745Sheppo 	(struct bus_ops *)NULL,	/* devo_bus_ops */
2871ae08745Sheppo 	ddi_power		/* devo_power */
2881ae08745Sheppo };
2891ae08745Sheppo 
2901ae08745Sheppo extern	struct	mod_ops	mod_driverops;
2911ae08745Sheppo static struct modldrv vswmodldrv = {
2921ae08745Sheppo 	&mod_driverops,
2931ae08745Sheppo 	"sun4v Virtual Switch Driver %I%",
2941ae08745Sheppo 	&vsw_ops,
2951ae08745Sheppo };
2961ae08745Sheppo 
2971ae08745Sheppo #define	LDC_ENTER_LOCK(ldcp)	\
2981ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_cblock));\
2991ae08745Sheppo 				mutex_enter(&((ldcp)->ldc_txlock));
3001ae08745Sheppo #define	LDC_EXIT_LOCK(ldcp)	\
3011ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_txlock));\
3021ae08745Sheppo 				mutex_exit(&((ldcp)->ldc_cblock));
3031ae08745Sheppo 
3041ae08745Sheppo /* Driver soft state ptr  */
3051ae08745Sheppo static void	*vsw_state;
3061ae08745Sheppo 
3071ae08745Sheppo /*
3081ae08745Sheppo  * Linked list of "vsw_t" structures - one per instance.
3091ae08745Sheppo  */
3101ae08745Sheppo vsw_t		*vsw_head = NULL;
3111ae08745Sheppo krwlock_t	vsw_rw;
3121ae08745Sheppo 
3131ae08745Sheppo /*
3141ae08745Sheppo  * Property names
3151ae08745Sheppo  */
3161ae08745Sheppo static char vdev_propname[] = "virtual-device";
3171ae08745Sheppo static char vsw_propname[] = "virtual-network-switch";
3181ae08745Sheppo static char physdev_propname[] = "vsw-phys-dev";
3191ae08745Sheppo static char smode_propname[] = "vsw-switch-mode";
3201ae08745Sheppo static char macaddr_propname[] = "local-mac-address";
3211ae08745Sheppo static char remaddr_propname[] = "remote-mac-address";
3221ae08745Sheppo static char ldcids_propname[] = "ldc-ids";
3231ae08745Sheppo static char chan_propname[] = "channel-endpoint";
3241ae08745Sheppo static char id_propname[] = "id";
3251ae08745Sheppo static char reg_propname[] = "reg";
3261ae08745Sheppo 
3271ae08745Sheppo /* supported versions */
3281ae08745Sheppo static	ver_sup_t	vsw_versions[] = { {1, 0} };
3291ae08745Sheppo 
3301ae08745Sheppo /*
3311ae08745Sheppo  * Matching criteria passed to the MDEG to register interest
3321ae08745Sheppo  * in changes to 'virtual-device-port' nodes identified by their
3331ae08745Sheppo  * 'id' property.
3341ae08745Sheppo  */
3351ae08745Sheppo static md_prop_match_t vport_prop_match[] = {
3361ae08745Sheppo 	{ MDET_PROP_VAL,    "id"   },
3371ae08745Sheppo 	{ MDET_LIST_END,    NULL    }
3381ae08745Sheppo };
3391ae08745Sheppo 
3401ae08745Sheppo static mdeg_node_match_t vport_match = { "virtual-device-port",
3411ae08745Sheppo 						vport_prop_match };
3421ae08745Sheppo 
3431ae08745Sheppo /*
344*34683adeSsg70180  * Matching criteria passed to the MDEG to register interest
345*34683adeSsg70180  * in changes to 'virtual-device' nodes (i.e. vsw nodes) identified
346*34683adeSsg70180  * by their 'name' and 'cfg-handle' properties.
347*34683adeSsg70180  */
348*34683adeSsg70180 static md_prop_match_t vdev_prop_match[] = {
349*34683adeSsg70180 	{ MDET_PROP_STR,    "name"   },
350*34683adeSsg70180 	{ MDET_PROP_VAL,    "cfg-handle" },
351*34683adeSsg70180 	{ MDET_LIST_END,    NULL    }
352*34683adeSsg70180 };
353*34683adeSsg70180 
354*34683adeSsg70180 static mdeg_node_match_t vdev_match = { "virtual-device",
355*34683adeSsg70180 						vdev_prop_match };
356*34683adeSsg70180 
357*34683adeSsg70180 
358*34683adeSsg70180 /*
3591ae08745Sheppo  * Specification of an MD node passed to the MDEG to filter any
3601ae08745Sheppo  * 'vport' nodes that do not belong to the specified node. This
3611ae08745Sheppo  * template is copied for each vsw instance and filled in with
3621ae08745Sheppo  * the appropriate 'cfg-handle' value before being passed to the MDEG.
3631ae08745Sheppo  */
3641ae08745Sheppo static mdeg_prop_spec_t vsw_prop_template[] = {
3651ae08745Sheppo 	{ MDET_PROP_STR,    "name",		vsw_propname },
3661ae08745Sheppo 	{ MDET_PROP_VAL,    "cfg-handle",	NULL	},
3671ae08745Sheppo 	{ MDET_LIST_END,    NULL,		NULL	}
3681ae08745Sheppo };
3691ae08745Sheppo 
3701ae08745Sheppo #define	VSW_SET_MDEG_PROP_INST(specp, val)	(specp)[1].ps_val = (val);
3711ae08745Sheppo 
3721ae08745Sheppo /*
3737636cb21Slm66018  * From /etc/system enable/disable thread per ring. This is a mode
3747636cb21Slm66018  * selection that is done a vsw driver attach time.
3757636cb21Slm66018  */
3767636cb21Slm66018 boolean_t vsw_multi_ring_enable = B_FALSE;
3777636cb21Slm66018 int vsw_mac_rx_rings = VSW_MAC_RX_RINGS;
3787636cb21Slm66018 
3797636cb21Slm66018 /*
3801ae08745Sheppo  * Print debug messages - set to 0x1f to enable all msgs
3811ae08745Sheppo  * or 0x0 to turn all off.
3821ae08745Sheppo  */
3831ae08745Sheppo int vswdbg = 0x0;
3841ae08745Sheppo 
3851ae08745Sheppo /*
3861ae08745Sheppo  * debug levels:
3871ae08745Sheppo  * 0x01:	Function entry/exit tracing
3881ae08745Sheppo  * 0x02:	Internal function messages
3891ae08745Sheppo  * 0x04:	Verbose internal messages
3901ae08745Sheppo  * 0x08:	Warning messages
3911ae08745Sheppo  * 0x10:	Error messages
3921ae08745Sheppo  */
3931ae08745Sheppo 
3941ae08745Sheppo static void
3951ae08745Sheppo vswdebug(vsw_t *vswp, const char *fmt, ...)
3961ae08745Sheppo {
3971ae08745Sheppo 	char buf[512];
3981ae08745Sheppo 	va_list ap;
3991ae08745Sheppo 
4001ae08745Sheppo 	va_start(ap, fmt);
4011ae08745Sheppo 	(void) vsprintf(buf, fmt, ap);
4021ae08745Sheppo 	va_end(ap);
4031ae08745Sheppo 
4041ae08745Sheppo 	if (vswp == NULL)
4051ae08745Sheppo 		cmn_err(CE_CONT, "%s\n", buf);
4061ae08745Sheppo 	else
4071ae08745Sheppo 		cmn_err(CE_CONT, "vsw%d: %s\n", vswp->instance, buf);
4081ae08745Sheppo }
4091ae08745Sheppo 
4101ae08745Sheppo /*
4111ae08745Sheppo  * For the moment the state dump routines have their own
4121ae08745Sheppo  * private flag.
4131ae08745Sheppo  */
4141ae08745Sheppo #define	DUMP_STATE	0
4151ae08745Sheppo 
4161ae08745Sheppo #if DUMP_STATE
4171ae08745Sheppo 
4181ae08745Sheppo #define	DUMP_TAG(tag) \
4191ae08745Sheppo {			\
4201ae08745Sheppo 	D1(NULL, "DUMP_TAG: type 0x%llx", (tag).vio_msgtype); \
4211ae08745Sheppo 	D1(NULL, "DUMP_TAG: stype 0x%llx", (tag).vio_subtype);	\
4221ae08745Sheppo 	D1(NULL, "DUMP_TAG: senv 0x%llx", (tag).vio_subtype_env);	\
4231ae08745Sheppo }
4241ae08745Sheppo 
4251ae08745Sheppo #define	DUMP_TAG_PTR(tag) \
4261ae08745Sheppo {			\
4271ae08745Sheppo 	D1(NULL, "DUMP_TAG: type 0x%llx", (tag)->vio_msgtype); \
4281ae08745Sheppo 	D1(NULL, "DUMP_TAG: stype 0x%llx", (tag)->vio_subtype);	\
4291ae08745Sheppo 	D1(NULL, "DUMP_TAG: senv 0x%llx", (tag)->vio_subtype_env);	\
4301ae08745Sheppo }
4311ae08745Sheppo 
4321ae08745Sheppo #define	DUMP_FLAGS(flags) dump_flags(flags);
4331ae08745Sheppo #define	DISPLAY_STATE()	display_state()
4341ae08745Sheppo 
4351ae08745Sheppo #else
4361ae08745Sheppo 
4371ae08745Sheppo #define	DUMP_TAG(tag)
4381ae08745Sheppo #define	DUMP_TAG_PTR(tag)
4391ae08745Sheppo #define	DUMP_FLAGS(state)
4401ae08745Sheppo #define	DISPLAY_STATE()
4411ae08745Sheppo 
4421ae08745Sheppo #endif	/* DUMP_STATE */
4431ae08745Sheppo 
4441ae08745Sheppo #ifdef DEBUG
4451ae08745Sheppo 
4461ae08745Sheppo #define	D1		\
4471ae08745Sheppo if (vswdbg & 0x01)	\
4481ae08745Sheppo 	vswdebug
4491ae08745Sheppo 
4501ae08745Sheppo #define	D2		\
4511ae08745Sheppo if (vswdbg & 0x02)	\
4521ae08745Sheppo 	vswdebug
4531ae08745Sheppo 
4541ae08745Sheppo #define	D3		\
4551ae08745Sheppo if (vswdbg & 0x04)	\
4561ae08745Sheppo 	vswdebug
4571ae08745Sheppo 
4581ae08745Sheppo #define	DWARN		\
4591ae08745Sheppo if (vswdbg & 0x08)	\
4601ae08745Sheppo 	vswdebug
4611ae08745Sheppo 
4621ae08745Sheppo #define	DERR		\
4631ae08745Sheppo if (vswdbg & 0x10)	\
4641ae08745Sheppo 	vswdebug
4651ae08745Sheppo 
4661ae08745Sheppo #else
4671ae08745Sheppo 
4681ae08745Sheppo #define	DERR		if (0)	vswdebug
4691ae08745Sheppo #define	DWARN		if (0)	vswdebug
4701ae08745Sheppo #define	D1		if (0)	vswdebug
4711ae08745Sheppo #define	D2		if (0)	vswdebug
4721ae08745Sheppo #define	D3		if (0)	vswdebug
4731ae08745Sheppo 
4741ae08745Sheppo #endif	/* DEBUG */
4751ae08745Sheppo 
4761ae08745Sheppo static struct modlinkage modlinkage = {
4771ae08745Sheppo 	MODREV_1,
4781ae08745Sheppo 	&vswmodldrv,
4791ae08745Sheppo 	NULL
4801ae08745Sheppo };
4811ae08745Sheppo 
4821ae08745Sheppo int
4831ae08745Sheppo _init(void)
4841ae08745Sheppo {
4851ae08745Sheppo 	int status;
4861ae08745Sheppo 
4871ae08745Sheppo 	rw_init(&vsw_rw, NULL, RW_DRIVER, NULL);
4881ae08745Sheppo 
4891ae08745Sheppo 	status = ddi_soft_state_init(&vsw_state, sizeof (vsw_t), 1);
4901ae08745Sheppo 	if (status != 0) {
4911ae08745Sheppo 		return (status);
4921ae08745Sheppo 	}
4931ae08745Sheppo 
4941ae08745Sheppo 	mac_init_ops(&vsw_ops, "vsw");
4951ae08745Sheppo 	status = mod_install(&modlinkage);
4961ae08745Sheppo 	if (status != 0) {
4971ae08745Sheppo 		ddi_soft_state_fini(&vsw_state);
4981ae08745Sheppo 	}
4991ae08745Sheppo 	return (status);
5001ae08745Sheppo }
5011ae08745Sheppo 
5021ae08745Sheppo int
5031ae08745Sheppo _fini(void)
5041ae08745Sheppo {
5051ae08745Sheppo 	int status;
5061ae08745Sheppo 
5071ae08745Sheppo 	status = mod_remove(&modlinkage);
5081ae08745Sheppo 	if (status != 0)
5091ae08745Sheppo 		return (status);
5101ae08745Sheppo 	mac_fini_ops(&vsw_ops);
5111ae08745Sheppo 	ddi_soft_state_fini(&vsw_state);
5121ae08745Sheppo 
5131ae08745Sheppo 	rw_destroy(&vsw_rw);
5141ae08745Sheppo 
5151ae08745Sheppo 	return (status);
5161ae08745Sheppo }
5171ae08745Sheppo 
5181ae08745Sheppo int
5191ae08745Sheppo _info(struct modinfo *modinfop)
5201ae08745Sheppo {
5211ae08745Sheppo 	return (mod_info(&modlinkage, modinfop));
5221ae08745Sheppo }
5231ae08745Sheppo 
5241ae08745Sheppo static int
5251ae08745Sheppo vsw_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5261ae08745Sheppo {
5271ae08745Sheppo 	vsw_t		*vswp;
528*34683adeSsg70180 	int		instance;
5291ae08745Sheppo 	char		hashname[MAXNAMELEN];
5301ae08745Sheppo 	char		qname[TASKQ_NAMELEN];
5317636cb21Slm66018 	enum		{ PROG_init = 0x00,
5327636cb21Slm66018 				PROG_if_lock = 0x01,
5337636cb21Slm66018 				PROG_fdb = 0x02,
5347636cb21Slm66018 				PROG_mfdb = 0x04,
5357636cb21Slm66018 				PROG_report_dev = 0x08,
5367636cb21Slm66018 				PROG_plist = 0x10,
5371ae08745Sheppo 				PROG_taskq = 0x20}
5381ae08745Sheppo 			progress;
5391ae08745Sheppo 
5401ae08745Sheppo 	progress = PROG_init;
5411ae08745Sheppo 
5421ae08745Sheppo 	switch (cmd) {
5431ae08745Sheppo 	case DDI_ATTACH:
5441ae08745Sheppo 		break;
5451ae08745Sheppo 	case DDI_RESUME:
5461ae08745Sheppo 		/* nothing to do for this non-device */
5471ae08745Sheppo 		return (DDI_SUCCESS);
5481ae08745Sheppo 	case DDI_PM_RESUME:
5491ae08745Sheppo 	default:
5501ae08745Sheppo 		return (DDI_FAILURE);
5511ae08745Sheppo 	}
5521ae08745Sheppo 
5531ae08745Sheppo 	instance = ddi_get_instance(dip);
5541ae08745Sheppo 	if (ddi_soft_state_zalloc(vsw_state, instance) != DDI_SUCCESS) {
5551ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_soft_state_zalloc failed", instance);
5561ae08745Sheppo 		return (DDI_FAILURE);
5571ae08745Sheppo 	}
5581ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
5591ae08745Sheppo 
5601ae08745Sheppo 	if (vswp == NULL) {
5611ae08745Sheppo 		DERR(NULL, "vsw%d: ddi_get_soft_state failed", instance);
5621ae08745Sheppo 		goto vsw_attach_fail;
5631ae08745Sheppo 	}
5641ae08745Sheppo 
5651ae08745Sheppo 	vswp->dip = dip;
5661ae08745Sheppo 	vswp->instance = instance;
5671ae08745Sheppo 	ddi_set_driver_private(dip, (caddr_t)vswp);
5681ae08745Sheppo 
569*34683adeSsg70180 	mutex_init(&vswp->mac_lock, NULL, MUTEX_DRIVER, NULL);
5701ae08745Sheppo 	rw_init(&vswp->if_lockrw, NULL, RW_DRIVER, NULL);
5711ae08745Sheppo 	progress |= PROG_if_lock;
5721ae08745Sheppo 
5731ae08745Sheppo 	/* setup the unicast forwarding database  */
5741ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_unicst_table-%d",
5751ae08745Sheppo 							vswp->instance);
5761ae08745Sheppo 	D2(vswp, "creating unicast hash table (%s)...", hashname);
5771ae08745Sheppo 	vswp->fdb = mod_hash_create_ptrhash(hashname, VSW_NCHAINS,
5781ae08745Sheppo 		mod_hash_null_valdtor, sizeof (void *));
5791ae08745Sheppo 
5801ae08745Sheppo 	progress |= PROG_fdb;
5811ae08745Sheppo 
5821ae08745Sheppo 	/* setup the multicast fowarding database */
5831ae08745Sheppo 	(void) snprintf(hashname, MAXNAMELEN, "vsw_mcst_table-%d",
5841ae08745Sheppo 							vswp->instance);
5851ae08745Sheppo 	D2(vswp, "creating multicast hash table %s)...", hashname);
5861ae08745Sheppo 	rw_init(&vswp->mfdbrw, NULL, RW_DRIVER, NULL);
5871ae08745Sheppo 	vswp->mfdb = mod_hash_create_ptrhash(hashname, VSW_NCHAINS,
5881ae08745Sheppo 			mod_hash_null_valdtor, sizeof (void *));
5891ae08745Sheppo 
5901ae08745Sheppo 	progress |= PROG_mfdb;
5911ae08745Sheppo 
5921ae08745Sheppo 	/*
5931ae08745Sheppo 	 * create lock protecting list of multicast addresses
5941ae08745Sheppo 	 * which could come via m_multicst() entry point when plumbed.
5951ae08745Sheppo 	 */
5961ae08745Sheppo 	mutex_init(&vswp->mca_lock, NULL, MUTEX_DRIVER, NULL);
5971ae08745Sheppo 	vswp->mcap = NULL;
5981ae08745Sheppo 
5991ae08745Sheppo 	ddi_report_dev(vswp->dip);
6001ae08745Sheppo 
6011ae08745Sheppo 	progress |= PROG_report_dev;
6021ae08745Sheppo 
6031ae08745Sheppo 	WRITE_ENTER(&vsw_rw);
6041ae08745Sheppo 	vswp->next = vsw_head;
6051ae08745Sheppo 	vsw_head = vswp;
6061ae08745Sheppo 	RW_EXIT(&vsw_rw);
6071ae08745Sheppo 
6081ae08745Sheppo 	/* setup the port list */
6091ae08745Sheppo 	rw_init(&vswp->plist.lockrw, NULL, RW_DRIVER, NULL);
6101ae08745Sheppo 	vswp->plist.head = NULL;
6111ae08745Sheppo 
6121ae08745Sheppo 	progress |= PROG_plist;
6131ae08745Sheppo 
6141ae08745Sheppo 	/*
6151ae08745Sheppo 	 * Create the taskq which will process all the VIO
6161ae08745Sheppo 	 * control messages.
6171ae08745Sheppo 	 */
6181ae08745Sheppo 	(void) snprintf(qname, TASKQ_NAMELEN, "vsw_taskq%d", vswp->instance);
6191ae08745Sheppo 	if ((vswp->taskq_p = ddi_taskq_create(vswp->dip, qname, 1,
6201ae08745Sheppo 					TASKQ_DEFAULTPRI, 0)) == NULL) {
621*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to create task queue",
622*34683adeSsg70180 			vswp->instance);
6231ae08745Sheppo 		goto vsw_attach_fail;
6241ae08745Sheppo 	}
6251ae08745Sheppo 
6261ae08745Sheppo 	progress |= PROG_taskq;
6271ae08745Sheppo 
628d10e4ef2Snarayan 	/* prevent auto-detaching */
629d10e4ef2Snarayan 	if (ddi_prop_update_int(DDI_DEV_T_NONE, vswp->dip,
630d10e4ef2Snarayan 				DDI_NO_AUTODETACH, 1) != DDI_SUCCESS) {
631*34683adeSsg70180 		cmn_err(CE_NOTE, "!Unable to set \"%s\" property for "
632d10e4ef2Snarayan 			"instance %u", DDI_NO_AUTODETACH, instance);
633d10e4ef2Snarayan 	}
634d10e4ef2Snarayan 
6351ae08745Sheppo 	/*
636*34683adeSsg70180 	 * Now we have everything setup, register an interest in
637*34683adeSsg70180 	 * specific MD nodes.
638*34683adeSsg70180 	 *
639*34683adeSsg70180 	 * The callback is invoked in 2 cases, firstly if upon mdeg
640*34683adeSsg70180 	 * registration there are existing nodes which match our specified
641*34683adeSsg70180 	 * criteria, and secondly if the MD is changed (and again, there
642*34683adeSsg70180 	 * are nodes which we are interested in present within it. Note
643*34683adeSsg70180 	 * that our callback will be invoked even if our specified nodes
644*34683adeSsg70180 	 * have not actually changed).
645*34683adeSsg70180 	 *
646*34683adeSsg70180 	 * Until the callback is invoked we cannot switch any pkts as
647*34683adeSsg70180 	 * we don't know basic information such as what mode we are
648*34683adeSsg70180 	 * operating in. However we expect the callback to be invoked
649*34683adeSsg70180 	 * immediately upon registration as this driver should only
650*34683adeSsg70180 	 * be attaching if there are vsw nodes in the MD.
6511ae08745Sheppo 	 */
652*34683adeSsg70180 	if (vsw_mdeg_register(vswp))
653*34683adeSsg70180 		goto vsw_attach_fail;
6541ae08745Sheppo 
6551ae08745Sheppo 	return (DDI_SUCCESS);
6561ae08745Sheppo 
6571ae08745Sheppo vsw_attach_fail:
6581ae08745Sheppo 	DERR(NULL, "vsw_attach: failed");
6591ae08745Sheppo 
6601ae08745Sheppo 	if (progress & PROG_taskq)
6611ae08745Sheppo 		ddi_taskq_destroy(vswp->taskq_p);
6621ae08745Sheppo 
6631ae08745Sheppo 	if (progress & PROG_plist)
6641ae08745Sheppo 		rw_destroy(&vswp->plist.lockrw);
6651ae08745Sheppo 
6661ae08745Sheppo 	if (progress & PROG_report_dev) {
6671ae08745Sheppo 		ddi_remove_minor_node(dip, NULL);
6681ae08745Sheppo 		mutex_destroy(&vswp->mca_lock);
6691ae08745Sheppo 	}
6701ae08745Sheppo 
6711ae08745Sheppo 	if (progress & PROG_mfdb) {
6721ae08745Sheppo 		mod_hash_destroy_hash(vswp->mfdb);
6731ae08745Sheppo 		vswp->mfdb = NULL;
6741ae08745Sheppo 		rw_destroy(&vswp->mfdbrw);
6751ae08745Sheppo 	}
6761ae08745Sheppo 
6771ae08745Sheppo 	if (progress & PROG_fdb) {
6781ae08745Sheppo 		mod_hash_destroy_hash(vswp->fdb);
6791ae08745Sheppo 		vswp->fdb = NULL;
6801ae08745Sheppo 	}
6811ae08745Sheppo 
682*34683adeSsg70180 	if (progress & PROG_if_lock) {
6831ae08745Sheppo 		rw_destroy(&vswp->if_lockrw);
684*34683adeSsg70180 		mutex_destroy(&vswp->mac_lock);
685*34683adeSsg70180 	}
6861ae08745Sheppo 
6871ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
6881ae08745Sheppo 	return (DDI_FAILURE);
6891ae08745Sheppo }
6901ae08745Sheppo 
6911ae08745Sheppo static int
6921ae08745Sheppo vsw_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
6931ae08745Sheppo {
694d10e4ef2Snarayan 	vio_mblk_pool_t		*poolp, *npoolp;
6951ae08745Sheppo 	vsw_t			**vswpp, *vswp;
6961ae08745Sheppo 	int 			instance;
6971ae08745Sheppo 
6981ae08745Sheppo 	instance = ddi_get_instance(dip);
6991ae08745Sheppo 	vswp = ddi_get_soft_state(vsw_state, instance);
7001ae08745Sheppo 
7011ae08745Sheppo 	if (vswp == NULL) {
7021ae08745Sheppo 		return (DDI_FAILURE);
7031ae08745Sheppo 	}
7041ae08745Sheppo 
7051ae08745Sheppo 	switch (cmd) {
7061ae08745Sheppo 	case DDI_DETACH:
7071ae08745Sheppo 		break;
7081ae08745Sheppo 	case DDI_SUSPEND:
7091ae08745Sheppo 	case DDI_PM_SUSPEND:
7101ae08745Sheppo 	default:
7111ae08745Sheppo 		return (DDI_FAILURE);
7121ae08745Sheppo 	}
7131ae08745Sheppo 
7141ae08745Sheppo 	D2(vswp, "detaching instance %d", instance);
7151ae08745Sheppo 
716*34683adeSsg70180 	if (vswp->if_state & VSW_IF_REG) {
7171ae08745Sheppo 		if (vsw_mac_unregister(vswp) != 0) {
718*34683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to detach from "
719*34683adeSsg70180 				"MAC layer", vswp->instance);
7201ae08745Sheppo 			return (DDI_FAILURE);
7211ae08745Sheppo 		}
722d10e4ef2Snarayan 	}
7231ae08745Sheppo 
7241ae08745Sheppo 	vsw_mdeg_unregister(vswp);
7251ae08745Sheppo 
726e1ebb9ecSlm66018 	/* remove mac layer callback */
727*34683adeSsg70180 	mutex_enter(&vswp->mac_lock);
728e1ebb9ecSlm66018 	if ((vswp->mh != NULL) && (vswp->mrh != NULL)) {
729e1ebb9ecSlm66018 		mac_rx_remove(vswp->mh, vswp->mrh);
730e1ebb9ecSlm66018 		vswp->mrh = NULL;
7311ae08745Sheppo 	}
732*34683adeSsg70180 	mutex_exit(&vswp->mac_lock);
7331ae08745Sheppo 
7341ae08745Sheppo 	if (vsw_detach_ports(vswp) != 0) {
735*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to detach ports",
736*34683adeSsg70180 							vswp->instance);
7371ae08745Sheppo 		return (DDI_FAILURE);
7381ae08745Sheppo 	}
7391ae08745Sheppo 
740*34683adeSsg70180 	rw_destroy(&vswp->if_lockrw);
741*34683adeSsg70180 
7421ae08745Sheppo 	/*
743e1ebb9ecSlm66018 	 * Now that the ports have been deleted, stop and close
744e1ebb9ecSlm66018 	 * the physical device.
745e1ebb9ecSlm66018 	 */
746*34683adeSsg70180 	mutex_enter(&vswp->mac_lock);
747e1ebb9ecSlm66018 	if (vswp->mh != NULL) {
7487636cb21Slm66018 		if (vswp->mstarted)
749e1ebb9ecSlm66018 			mac_stop(vswp->mh);
7507636cb21Slm66018 		if (vswp->mresources)
7517636cb21Slm66018 			mac_resource_set(vswp->mh, NULL, NULL);
752e1ebb9ecSlm66018 		mac_close(vswp->mh);
753e1ebb9ecSlm66018 
754e1ebb9ecSlm66018 		vswp->mh = NULL;
755e1ebb9ecSlm66018 		vswp->txinfo = NULL;
756e1ebb9ecSlm66018 	}
757*34683adeSsg70180 	mutex_exit(&vswp->mac_lock);
758*34683adeSsg70180 	mutex_destroy(&vswp->mac_lock);
759e1ebb9ecSlm66018 
760e1ebb9ecSlm66018 	/*
761d10e4ef2Snarayan 	 * Destroy any free pools that may still exist.
762d10e4ef2Snarayan 	 */
763d10e4ef2Snarayan 	poolp = vswp->rxh;
764d10e4ef2Snarayan 	while (poolp != NULL) {
765d10e4ef2Snarayan 		npoolp = vswp->rxh = poolp->nextp;
766d10e4ef2Snarayan 		if (vio_destroy_mblks(poolp) != 0) {
767d10e4ef2Snarayan 			vswp->rxh = poolp;
768d10e4ef2Snarayan 			return (DDI_FAILURE);
769d10e4ef2Snarayan 		}
770d10e4ef2Snarayan 		poolp = npoolp;
771d10e4ef2Snarayan 	}
772d10e4ef2Snarayan 
773d10e4ef2Snarayan 	/*
7741ae08745Sheppo 	 * Remove this instance from any entries it may be on in
7751ae08745Sheppo 	 * the hash table by using the list of addresses maintained
7761ae08745Sheppo 	 * in the vsw_t structure.
7771ae08745Sheppo 	 */
7781ae08745Sheppo 	vsw_del_mcst_vsw(vswp);
7791ae08745Sheppo 
7801ae08745Sheppo 	vswp->mcap = NULL;
7811ae08745Sheppo 	mutex_destroy(&vswp->mca_lock);
7821ae08745Sheppo 
7831ae08745Sheppo 	/*
7841ae08745Sheppo 	 * By now any pending tasks have finished and the underlying
7851ae08745Sheppo 	 * ldc's have been destroyed, so its safe to delete the control
7861ae08745Sheppo 	 * message taskq.
7871ae08745Sheppo 	 */
7881ae08745Sheppo 	if (vswp->taskq_p != NULL)
7891ae08745Sheppo 		ddi_taskq_destroy(vswp->taskq_p);
7901ae08745Sheppo 
7911ae08745Sheppo 	/*
7921ae08745Sheppo 	 * At this stage all the data pointers in the hash table
7931ae08745Sheppo 	 * should be NULL, as all the ports have been removed and will
7941ae08745Sheppo 	 * have deleted themselves from the port lists which the data
7951ae08745Sheppo 	 * pointers point to. Hence we can destroy the table using the
7961ae08745Sheppo 	 * default destructors.
7971ae08745Sheppo 	 */
7981ae08745Sheppo 	D2(vswp, "vsw_detach: destroying hash tables..");
7991ae08745Sheppo 	mod_hash_destroy_hash(vswp->fdb);
8001ae08745Sheppo 	vswp->fdb = NULL;
8011ae08745Sheppo 
8021ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
8031ae08745Sheppo 	mod_hash_destroy_hash(vswp->mfdb);
8041ae08745Sheppo 	vswp->mfdb = NULL;
8051ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
8061ae08745Sheppo 	rw_destroy(&vswp->mfdbrw);
8071ae08745Sheppo 
8081ae08745Sheppo 	ddi_remove_minor_node(dip, NULL);
8091ae08745Sheppo 
8101ae08745Sheppo 	rw_destroy(&vswp->plist.lockrw);
8111ae08745Sheppo 	WRITE_ENTER(&vsw_rw);
8121ae08745Sheppo 	for (vswpp = &vsw_head; *vswpp; vswpp = &(*vswpp)->next) {
8131ae08745Sheppo 		if (*vswpp == vswp) {
8141ae08745Sheppo 			*vswpp = vswp->next;
8151ae08745Sheppo 			break;
8161ae08745Sheppo 		}
8171ae08745Sheppo 	}
8181ae08745Sheppo 	RW_EXIT(&vsw_rw);
8191ae08745Sheppo 	ddi_soft_state_free(vsw_state, instance);
8201ae08745Sheppo 
8211ae08745Sheppo 	return (DDI_SUCCESS);
8221ae08745Sheppo }
8231ae08745Sheppo 
8241ae08745Sheppo static int
8251ae08745Sheppo vsw_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
8261ae08745Sheppo {
8271ae08745Sheppo 	_NOTE(ARGUNUSED(dip))
8281ae08745Sheppo 
8291ae08745Sheppo 	vsw_t	*vswp = NULL;
8301ae08745Sheppo 	dev_t	dev = (dev_t)arg;
8311ae08745Sheppo 	int	instance;
8321ae08745Sheppo 
8331ae08745Sheppo 	instance = getminor(dev);
8341ae08745Sheppo 
8351ae08745Sheppo 	switch (infocmd) {
8361ae08745Sheppo 	case DDI_INFO_DEVT2DEVINFO:
8371ae08745Sheppo 		if ((vswp = ddi_get_soft_state(vsw_state, instance)) == NULL) {
8381ae08745Sheppo 			*result = NULL;
8391ae08745Sheppo 			return (DDI_FAILURE);
8401ae08745Sheppo 		}
8411ae08745Sheppo 		*result = vswp->dip;
8421ae08745Sheppo 		return (DDI_SUCCESS);
8431ae08745Sheppo 
8441ae08745Sheppo 	case DDI_INFO_DEVT2INSTANCE:
8451ae08745Sheppo 		*result = (void *)(uintptr_t)instance;
8461ae08745Sheppo 		return (DDI_SUCCESS);
8471ae08745Sheppo 
8481ae08745Sheppo 	default:
8491ae08745Sheppo 		*result = NULL;
8501ae08745Sheppo 		return (DDI_FAILURE);
8511ae08745Sheppo 	}
8521ae08745Sheppo }
8531ae08745Sheppo 
8541ae08745Sheppo /*
855*34683adeSsg70180  * Get the value of the "vsw-phys-dev" property in the specified
856*34683adeSsg70180  * node. This property is the name of the physical device that
857*34683adeSsg70180  * the virtual switch will use to talk to the outside world.
858*34683adeSsg70180  *
859*34683adeSsg70180  * Note it is valid for this property to be NULL (but the property
860*34683adeSsg70180  * itself must exist). Callers of this routine should verify that
861*34683adeSsg70180  * the value returned is what they expected (i.e. either NULL or non NULL).
862*34683adeSsg70180  *
863*34683adeSsg70180  * On success returns value of the property in region pointed to by
864*34683adeSsg70180  * the 'name' argument, and with return value of 0. Otherwise returns 1.
8651ae08745Sheppo  */
866*34683adeSsg70180 static int
867*34683adeSsg70180 vsw_get_md_physname(vsw_t *vswp, md_t *mdp, mde_cookie_t node, char *name)
8681ae08745Sheppo {
869*34683adeSsg70180 	int	len = 0;
8701ae08745Sheppo 	char	*physname = NULL;
8711ae08745Sheppo 	char	*dev;
8721ae08745Sheppo 
873*34683adeSsg70180 	if (md_get_prop_data(mdp, node, physdev_propname,
8741ae08745Sheppo 				(uint8_t **)(&physname), &len) != 0) {
875*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get name(s) of physical "
876*34683adeSsg70180 				"device(s) from MD", vswp->instance);
877*34683adeSsg70180 		return (1);
8781ae08745Sheppo 	} else if ((strlen(physname) + 1) > LIFNAMSIZ) {
879*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: %s is too long a device name",
880*34683adeSsg70180 			vswp->instance, physname);
881*34683adeSsg70180 		return (1);
8821ae08745Sheppo 	} else {
883*34683adeSsg70180 		(void) strncpy(name, physname, strlen(physname) + 1);
8841ae08745Sheppo 		D2(vswp, "%s: using first device specified (%s)",
885*34683adeSsg70180 			__func__, physname);
8861ae08745Sheppo 	}
8871ae08745Sheppo 
8881ae08745Sheppo #ifdef DEBUG
8891ae08745Sheppo 	/*
8901ae08745Sheppo 	 * As a temporary measure to aid testing we check to see if there
8911ae08745Sheppo 	 * is a vsw.conf file present. If there is we use the value of the
8921ae08745Sheppo 	 * vsw_physname property in the file as the name of the physical
8931ae08745Sheppo 	 * device, overriding the value from the MD.
8941ae08745Sheppo 	 *
8951ae08745Sheppo 	 * There may be multiple devices listed, but for the moment
8961ae08745Sheppo 	 * we just use the first one.
8971ae08745Sheppo 	 */
8981ae08745Sheppo 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vswp->dip, 0,
8991ae08745Sheppo 		"vsw_physname", &dev) == DDI_PROP_SUCCESS) {
9001ae08745Sheppo 		if ((strlen(dev) + 1) > LIFNAMSIZ) {
901*34683adeSsg70180 			cmn_err(CE_WARN, "vsw%d: %s is too long a device name",
902*34683adeSsg70180 				vswp->instance, dev);
903*34683adeSsg70180 			ddi_prop_free(dev);
904*34683adeSsg70180 			return (1);
9051ae08745Sheppo 		} else {
906*34683adeSsg70180 			cmn_err(CE_NOTE, "vsw%d: Using device name (%s) from "
907*34683adeSsg70180 				"config file", vswp->instance, dev);
9081ae08745Sheppo 
909*34683adeSsg70180 			(void) strncpy(name, dev, strlen(dev) + 1);
9101ae08745Sheppo 		}
9111ae08745Sheppo 
9121ae08745Sheppo 		ddi_prop_free(dev);
9131ae08745Sheppo 	}
9141ae08745Sheppo #endif
9151ae08745Sheppo 
916*34683adeSsg70180 	return (0);
917*34683adeSsg70180 }
918e1ebb9ecSlm66018 
919e1ebb9ecSlm66018 /*
920*34683adeSsg70180  * Read the 'vsw-switch-mode' property from the specified MD node.
921*34683adeSsg70180  *
922*34683adeSsg70180  * Returns 0 on success and the number of modes found in 'found',
923*34683adeSsg70180  * otherwise returns 1.
924e1ebb9ecSlm66018  */
925*34683adeSsg70180 static int
926*34683adeSsg70180 vsw_get_md_smodes(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
927*34683adeSsg70180 						uint8_t *modes, int *found)
928*34683adeSsg70180 {
929*34683adeSsg70180 	int		len = 0;
930*34683adeSsg70180 	int		smode_num = 0;
931*34683adeSsg70180 	char		*smode = NULL;
932*34683adeSsg70180 	char		*curr_mode = NULL;
933*34683adeSsg70180 
934*34683adeSsg70180 	D1(vswp, "%s: enter", __func__);
9351ae08745Sheppo 
9361ae08745Sheppo 	/*
9371ae08745Sheppo 	 * Get the switch-mode property. The modes are listed in
9381ae08745Sheppo 	 * decreasing order of preference, i.e. prefered mode is
9391ae08745Sheppo 	 * first item in list.
9401ae08745Sheppo 	 */
9411ae08745Sheppo 	len = 0;
942*34683adeSsg70180 	smode_num = 0;
943*34683adeSsg70180 	if (md_get_prop_data(mdp, node, smode_propname,
9441ae08745Sheppo 				(uint8_t **)(&smode), &len) != 0) {
9451ae08745Sheppo 		/*
946e1ebb9ecSlm66018 		 * Unable to get switch-mode property from MD, nothing
947e1ebb9ecSlm66018 		 * more we can do.
9481ae08745Sheppo 		 */
949*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get switch mode property"
950*34683adeSsg70180 			" from the MD", vswp->instance);
951*34683adeSsg70180 		*found = 0;
952*34683adeSsg70180 		return (1);
953e1ebb9ecSlm66018 	}
954e1ebb9ecSlm66018 
9551ae08745Sheppo 	curr_mode = smode;
9561ae08745Sheppo 	/*
9571ae08745Sheppo 	 * Modes of operation:
9581ae08745Sheppo 	 * 'switched'	 - layer 2 switching, underlying HW in
959e1ebb9ecSlm66018 	 *			programmed mode.
9601ae08745Sheppo 	 * 'promiscuous' - layer 2 switching, underlying HW in
9611ae08745Sheppo 	 *			promiscuous mode.
9621ae08745Sheppo 	 * 'routed'	 - layer 3 (i.e. IP) routing, underlying HW
9631ae08745Sheppo 	 *			in non-promiscuous mode.
9641ae08745Sheppo 	 */
965*34683adeSsg70180 	while ((curr_mode < (smode + len)) && (smode_num < NUM_SMODES)) {
9661ae08745Sheppo 		D2(vswp, "%s: curr_mode = [%s]", __func__, curr_mode);
967e1ebb9ecSlm66018 		if (strcmp(curr_mode, "switched") == 0) {
968*34683adeSsg70180 			modes[smode_num++] = VSW_LAYER2;
969e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "promiscuous") == 0) {
970*34683adeSsg70180 			modes[smode_num++] = VSW_LAYER2_PROMISC;
971e1ebb9ecSlm66018 		} else if (strcmp(curr_mode, "routed") == 0) {
972*34683adeSsg70180 			modes[smode_num++] = VSW_LAYER3;
973e1ebb9ecSlm66018 		} else {
974*34683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unknown switch mode %s, "
975*34683adeSsg70180 				"setting to default switched mode",
976*34683adeSsg70180 				vswp->instance, curr_mode);
977*34683adeSsg70180 			modes[smode_num++] = VSW_LAYER2;
9781ae08745Sheppo 		}
9791ae08745Sheppo 		curr_mode += strlen(curr_mode) + 1;
9801ae08745Sheppo 	}
981*34683adeSsg70180 	*found = smode_num;
9821ae08745Sheppo 
983*34683adeSsg70180 	D2(vswp, "%s: %d modes found", __func__, smode_num);
9841ae08745Sheppo 
9851ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
986*34683adeSsg70180 
987*34683adeSsg70180 	return (0);
9881ae08745Sheppo }
9891ae08745Sheppo 
990e1ebb9ecSlm66018 /*
991e1ebb9ecSlm66018  * Get the mac address of the physical device.
992e1ebb9ecSlm66018  *
993e1ebb9ecSlm66018  * Returns 0 on success, 1 on failure.
994e1ebb9ecSlm66018  */
995e1ebb9ecSlm66018 static int
996e1ebb9ecSlm66018 vsw_get_physaddr(vsw_t *vswp)
997e1ebb9ecSlm66018 {
998e1ebb9ecSlm66018 	mac_handle_t	mh;
999e1ebb9ecSlm66018 	char		drv[LIFNAMSIZ];
1000e1ebb9ecSlm66018 	uint_t		ddi_instance;
1001e1ebb9ecSlm66018 
1002e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1003e1ebb9ecSlm66018 
1004e1ebb9ecSlm66018 	if (ddi_parse(vswp->physname, drv, &ddi_instance) != DDI_SUCCESS)
1005e1ebb9ecSlm66018 		return (1);
1006e1ebb9ecSlm66018 
1007e1ebb9ecSlm66018 	if (mac_open(vswp->physname, ddi_instance, &mh) != 0) {
1008*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: mac_open %s failed",
1009*34683adeSsg70180 				vswp->instance, vswp->physname);
1010e1ebb9ecSlm66018 		return (1);
1011e1ebb9ecSlm66018 	}
1012e1ebb9ecSlm66018 
1013e1ebb9ecSlm66018 	READ_ENTER(&vswp->if_lockrw);
1014e1ebb9ecSlm66018 	mac_unicst_get(mh, vswp->if_addr.ether_addr_octet);
1015e1ebb9ecSlm66018 	RW_EXIT(&vswp->if_lockrw);
1016e1ebb9ecSlm66018 
1017e1ebb9ecSlm66018 	mac_close(mh);
1018e1ebb9ecSlm66018 
1019e1ebb9ecSlm66018 	vswp->mdprops |= VSW_DEV_MACADDR;
1020e1ebb9ecSlm66018 
1021e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1022e1ebb9ecSlm66018 
1023e1ebb9ecSlm66018 	return (0);
1024e1ebb9ecSlm66018 }
1025e1ebb9ecSlm66018 
1026e1ebb9ecSlm66018 /*
1027e1ebb9ecSlm66018  * Check to see if the card supports the setting of multiple unicst
1028e1ebb9ecSlm66018  * addresses.
1029e1ebb9ecSlm66018  *
1030e1ebb9ecSlm66018  * Returns 0 if card supports the programming of multiple unicast addresses
1031e1ebb9ecSlm66018  * and there are free address slots available, otherwise returns 1.
1032e1ebb9ecSlm66018  */
1033e1ebb9ecSlm66018 static int
1034e1ebb9ecSlm66018 vsw_get_hw_maddr(vsw_t *vswp)
1035e1ebb9ecSlm66018 {
1036e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1037e1ebb9ecSlm66018 
1038*34683adeSsg70180 	mutex_enter(&vswp->mac_lock);
1039e1ebb9ecSlm66018 	if (vswp->mh == NULL) {
1040*34683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1041e1ebb9ecSlm66018 		return (1);
1042e1ebb9ecSlm66018 	}
1043e1ebb9ecSlm66018 
1044e1ebb9ecSlm66018 	if (!mac_capab_get(vswp->mh, MAC_CAPAB_MULTIADDRESS, &vswp->maddr)) {
1045e1ebb9ecSlm66018 		DWARN(vswp, "Unable to get capabilities of"
1046e1ebb9ecSlm66018 			" underlying device (%s)", vswp->physname);
1047*34683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1048e1ebb9ecSlm66018 		return (1);
1049e1ebb9ecSlm66018 	}
1050*34683adeSsg70180 	mutex_exit(&vswp->mac_lock);
1051e1ebb9ecSlm66018 
1052e1ebb9ecSlm66018 	if (vswp->maddr.maddr_naddrfree == 0) {
10537636cb21Slm66018 		cmn_err(CE_WARN,
1054*34683adeSsg70180 			"!vsw%d: device %s has no free unicast address slots",
1055*34683adeSsg70180 				vswp->instance, vswp->physname);
1056e1ebb9ecSlm66018 		return (1);
1057e1ebb9ecSlm66018 	}
1058e1ebb9ecSlm66018 
1059e1ebb9ecSlm66018 	D2(vswp, "%s: %d addrs : %d free", __func__,
1060e1ebb9ecSlm66018 		vswp->maddr.maddr_naddr, vswp->maddr.maddr_naddrfree);
1061e1ebb9ecSlm66018 
1062e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1063e1ebb9ecSlm66018 
1064e1ebb9ecSlm66018 	return (0);
1065e1ebb9ecSlm66018 }
1066e1ebb9ecSlm66018 
1067e1ebb9ecSlm66018 /*
1068*34683adeSsg70180  * Setup the required switching mode.
1069*34683adeSsg70180  *
1070*34683adeSsg70180  * Returns 0 on success, 1 on failure.
1071*34683adeSsg70180  */
1072*34683adeSsg70180 static int
1073*34683adeSsg70180 vsw_setup_switching(vsw_t *vswp)
1074*34683adeSsg70180 {
1075*34683adeSsg70180 	int	i, rv = 1;
1076*34683adeSsg70180 
1077*34683adeSsg70180 	D1(vswp, "%s: enter", __func__);
1078*34683adeSsg70180 
1079*34683adeSsg70180 	/* select best switching mode */
1080*34683adeSsg70180 	for (i = 0; i < vswp->smode_num; i++) {
1081*34683adeSsg70180 		vswp->smode_idx = i;
1082*34683adeSsg70180 		switch (vswp->smode[i]) {
1083*34683adeSsg70180 		case VSW_LAYER2:
1084*34683adeSsg70180 		case VSW_LAYER2_PROMISC:
1085*34683adeSsg70180 			rv = vsw_setup_layer2(vswp);
1086*34683adeSsg70180 			break;
1087*34683adeSsg70180 
1088*34683adeSsg70180 		case VSW_LAYER3:
1089*34683adeSsg70180 			rv = vsw_setup_layer3(vswp);
1090*34683adeSsg70180 			break;
1091*34683adeSsg70180 
1092*34683adeSsg70180 		default:
1093*34683adeSsg70180 			DERR(vswp, "unknown switch mode");
1094*34683adeSsg70180 			rv = 1;
1095*34683adeSsg70180 			break;
1096*34683adeSsg70180 		}
1097*34683adeSsg70180 
1098*34683adeSsg70180 		if (rv == 0)
1099*34683adeSsg70180 			break;
1100*34683adeSsg70180 	}
1101*34683adeSsg70180 
1102*34683adeSsg70180 	if (rv == 1) {
1103*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to setup specified "
1104*34683adeSsg70180 			"switching mode", vswp->instance);
1105*34683adeSsg70180 		return (rv);
1106*34683adeSsg70180 	}
1107*34683adeSsg70180 
1108*34683adeSsg70180 	D2(vswp, "%s: Operating in mode %d", __func__,
1109*34683adeSsg70180 					vswp->smode[vswp->smode_idx]);
1110*34683adeSsg70180 
1111*34683adeSsg70180 	D1(vswp, "%s: exit", __func__);
1112*34683adeSsg70180 
1113*34683adeSsg70180 	return (0);
1114*34683adeSsg70180 }
1115*34683adeSsg70180 
1116*34683adeSsg70180 /*
1117e1ebb9ecSlm66018  * Setup for layer 2 switching.
1118e1ebb9ecSlm66018  *
1119e1ebb9ecSlm66018  * Returns 0 on success, 1 on failure.
1120e1ebb9ecSlm66018  */
11211ae08745Sheppo static int
11221ae08745Sheppo vsw_setup_layer2(vsw_t *vswp)
11231ae08745Sheppo {
11241ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11251ae08745Sheppo 
1126*34683adeSsg70180 	vswp->vsw_switch_frame = vsw_switch_l2_frame;
11271ae08745Sheppo 
11281ae08745Sheppo 	/*
11291ae08745Sheppo 	 * Attempt to link into the MAC layer so we can get
11301ae08745Sheppo 	 * and send packets out over the physical adapter.
11311ae08745Sheppo 	 */
11321ae08745Sheppo 	if (vswp->mdprops & VSW_MD_PHYSNAME) {
11331ae08745Sheppo 		if (vsw_mac_attach(vswp) != 0) {
11341ae08745Sheppo 			/*
11351ae08745Sheppo 			 * Registration with the MAC layer has failed,
11361ae08745Sheppo 			 * so return 1 so that can fall back to next
11371ae08745Sheppo 			 * prefered switching method.
11381ae08745Sheppo 			 */
1139*34683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to join as MAC layer "
1140*34683adeSsg70180 				"client", vswp->instance);
1141e1ebb9ecSlm66018 			return (1);
11421ae08745Sheppo 		}
1143e1ebb9ecSlm66018 
1144e1ebb9ecSlm66018 		if (vswp->smode[vswp->smode_idx] == VSW_LAYER2) {
1145e1ebb9ecSlm66018 			/*
1146e1ebb9ecSlm66018 			 * Verify that underlying device can support multiple
1147e1ebb9ecSlm66018 			 * unicast mac addresses, and has free capacity.
1148e1ebb9ecSlm66018 			 */
1149e1ebb9ecSlm66018 			if (vsw_get_hw_maddr(vswp) != 0) {
1150*34683adeSsg70180 				cmn_err(CE_WARN, "!vsw%d: Unable to setup "
1151*34683adeSsg70180 					"switching", vswp->instance);
1152e1ebb9ecSlm66018 				vsw_mac_detach(vswp);
1153e1ebb9ecSlm66018 				return (1);
1154e1ebb9ecSlm66018 			}
1155e1ebb9ecSlm66018 		}
1156e1ebb9ecSlm66018 
11571ae08745Sheppo 	} else {
1158e1ebb9ecSlm66018 		/*
1159e1ebb9ecSlm66018 		 * No physical device name found in MD which is
1160e1ebb9ecSlm66018 		 * required for layer 2.
1161e1ebb9ecSlm66018 		 */
1162*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: no physical device name specified",
1163*34683adeSsg70180 			vswp->instance);
1164e1ebb9ecSlm66018 		return (1);
11651ae08745Sheppo 	}
11661ae08745Sheppo 
11671ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
11681ae08745Sheppo 
1169e1ebb9ecSlm66018 	return (0);
11701ae08745Sheppo }
11711ae08745Sheppo 
11721ae08745Sheppo static int
11731ae08745Sheppo vsw_setup_layer3(vsw_t *vswp)
11741ae08745Sheppo {
11751ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
11761ae08745Sheppo 
11771ae08745Sheppo 	D2(vswp, "%s: operating in layer 3 mode", __func__);
1178*34683adeSsg70180 	vswp->vsw_switch_frame = vsw_switch_l3_frame;
11791ae08745Sheppo 
11801ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
11811ae08745Sheppo 
11821ae08745Sheppo 	return (0);
11831ae08745Sheppo }
11841ae08745Sheppo 
11851ae08745Sheppo /*
11861ae08745Sheppo  * Link into the MAC layer to gain access to the services provided by
11871ae08745Sheppo  * the underlying physical device driver (which should also have
11881ae08745Sheppo  * registered with the MAC layer).
11891ae08745Sheppo  *
11901ae08745Sheppo  * Only when in layer 2 mode.
11911ae08745Sheppo  */
11921ae08745Sheppo static int
11931ae08745Sheppo vsw_mac_attach(vsw_t *vswp)
11941ae08745Sheppo {
1195ba2e4443Sseb 	char	drv[LIFNAMSIZ];
1196ba2e4443Sseb 	uint_t	ddi_instance;
1197ba2e4443Sseb 
11987636cb21Slm66018 	D1(vswp, "%s: enter", __func__);
11991ae08745Sheppo 
1200*34683adeSsg70180 	ASSERT(vswp->mh == NULL);
1201*34683adeSsg70180 	ASSERT(vswp->mrh == NULL);
1202*34683adeSsg70180 	ASSERT(vswp->mstarted == B_FALSE);
1203*34683adeSsg70180 	ASSERT(vswp->mresources == B_FALSE);
12041ae08745Sheppo 
12051ae08745Sheppo 	ASSERT(vswp->mdprops & VSW_MD_PHYSNAME);
12061ae08745Sheppo 
1207*34683adeSsg70180 	mutex_enter(&vswp->mac_lock);
1208ba2e4443Sseb 	if (ddi_parse(vswp->physname, drv, &ddi_instance) != DDI_SUCCESS) {
1209*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: invalid device name: %s",
1210*34683adeSsg70180 			vswp->instance, vswp->physname);
1211ba2e4443Sseb 		goto mac_fail_exit;
1212ba2e4443Sseb 	}
1213*34683adeSsg70180 
1214ba2e4443Sseb 	if ((mac_open(vswp->physname, ddi_instance, &vswp->mh)) != 0) {
1215*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: mac_open %s failed",
1216*34683adeSsg70180 			vswp->instance, vswp->physname);
12171ae08745Sheppo 		goto mac_fail_exit;
12181ae08745Sheppo 	}
12191ae08745Sheppo 
12207636cb21Slm66018 	ASSERT(vswp->mh != NULL);
12217636cb21Slm66018 
12221ae08745Sheppo 	D2(vswp, "vsw_mac_attach: using device %s", vswp->physname);
12231ae08745Sheppo 
12247636cb21Slm66018 	if (vsw_multi_ring_enable) {
1225*34683adeSsg70180 		/*
1226*34683adeSsg70180 		 * Initialize the ring table.
1227*34683adeSsg70180 		 */
12287636cb21Slm66018 		vsw_mac_ring_tbl_init(vswp);
12291ae08745Sheppo 
12307636cb21Slm66018 		/*
1231*34683adeSsg70180 		 * Register our rx callback function.
12327636cb21Slm66018 		 */
12337636cb21Slm66018 		vswp->mrh = mac_rx_add(vswp->mh,
12347636cb21Slm66018 			vsw_rx_queue_cb, (void *)vswp);
1235*34683adeSsg70180 		ASSERT(vswp->mrh != NULL);
12367636cb21Slm66018 
12377636cb21Slm66018 		/*
12387636cb21Slm66018 		 * Register our mac resource callback.
12397636cb21Slm66018 		 */
12407636cb21Slm66018 		mac_resource_set(vswp->mh, vsw_mac_ring_add_cb, (void *)vswp);
12417636cb21Slm66018 		vswp->mresources = B_TRUE;
12427636cb21Slm66018 
12437636cb21Slm66018 		/*
12447636cb21Slm66018 		 * Get the ring resources available to us from
12457636cb21Slm66018 		 * the mac below us.
12467636cb21Slm66018 		 */
12477636cb21Slm66018 		mac_resources(vswp->mh);
12487636cb21Slm66018 	} else {
12497636cb21Slm66018 		/*
12507636cb21Slm66018 		 * Just register our rx callback function
12517636cb21Slm66018 		 */
12527636cb21Slm66018 		vswp->mrh = mac_rx_add(vswp->mh, vsw_rx_cb, (void *)vswp);
12537636cb21Slm66018 		ASSERT(vswp->mrh != NULL);
1254*34683adeSsg70180 	}
12557636cb21Slm66018 
12567636cb21Slm66018 	/* Get the MAC tx fn */
12571ae08745Sheppo 	vswp->txinfo = mac_tx_get(vswp->mh);
12581ae08745Sheppo 
12591ae08745Sheppo 	/* start the interface */
12601ae08745Sheppo 	if (mac_start(vswp->mh) != 0) {
1261*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Could not start mac interface",
1262*34683adeSsg70180 			vswp->instance);
12631ae08745Sheppo 		goto mac_fail_exit;
12641ae08745Sheppo 	}
12651ae08745Sheppo 
1266*34683adeSsg70180 	mutex_exit(&vswp->mac_lock);
1267*34683adeSsg70180 
12687636cb21Slm66018 	vswp->mstarted = B_TRUE;
12697636cb21Slm66018 
12707636cb21Slm66018 	D1(vswp, "%s: exit", __func__);
12711ae08745Sheppo 	return (0);
12721ae08745Sheppo 
12731ae08745Sheppo mac_fail_exit:
1274*34683adeSsg70180 	mutex_exit(&vswp->mac_lock);
12757636cb21Slm66018 	vsw_mac_detach(vswp);
12761ae08745Sheppo 
12777636cb21Slm66018 	D1(vswp, "%s: exit", __func__);
12781ae08745Sheppo 	return (1);
12791ae08745Sheppo }
12801ae08745Sheppo 
12811ae08745Sheppo static void
12821ae08745Sheppo vsw_mac_detach(vsw_t *vswp)
12831ae08745Sheppo {
12841ae08745Sheppo 	D1(vswp, "vsw_mac_detach: enter");
12851ae08745Sheppo 
12867636cb21Slm66018 	ASSERT(vswp != NULL);
12877636cb21Slm66018 
12887636cb21Slm66018 	if (vsw_multi_ring_enable) {
12897636cb21Slm66018 		vsw_mac_ring_tbl_destroy(vswp);
12907636cb21Slm66018 	}
12917636cb21Slm66018 
1292*34683adeSsg70180 	mutex_enter(&vswp->mac_lock);
1293*34683adeSsg70180 
1294b9a6d57aSsg70180 	if (vswp->mh != NULL) {
12957636cb21Slm66018 		if (vswp->mstarted)
12967636cb21Slm66018 			mac_stop(vswp->mh);
12971ae08745Sheppo 		if (vswp->mrh != NULL)
12981ae08745Sheppo 			mac_rx_remove(vswp->mh, vswp->mrh);
12997636cb21Slm66018 		if (vswp->mresources)
13007636cb21Slm66018 			mac_resource_set(vswp->mh, NULL, NULL);
13011ae08745Sheppo 		mac_close(vswp->mh);
1302b9a6d57aSsg70180 	}
13031ae08745Sheppo 
13041ae08745Sheppo 	vswp->mrh = NULL;
13051ae08745Sheppo 	vswp->mh = NULL;
13061ae08745Sheppo 	vswp->txinfo = NULL;
13077636cb21Slm66018 	vswp->mstarted = B_FALSE;
13081ae08745Sheppo 
1309*34683adeSsg70180 	mutex_exit(&vswp->mac_lock);
1310*34683adeSsg70180 
13111ae08745Sheppo 	D1(vswp, "vsw_mac_detach: exit");
13121ae08745Sheppo }
13131ae08745Sheppo 
13141ae08745Sheppo /*
1315e1ebb9ecSlm66018  * Depending on the mode specified, the capabilites and capacity
1316e1ebb9ecSlm66018  * of the underlying device setup the physical device.
13171ae08745Sheppo  *
1318e1ebb9ecSlm66018  * If in layer 3 mode, then do nothing.
1319e1ebb9ecSlm66018  *
1320e1ebb9ecSlm66018  * If in layer 2 programmed mode attempt to program the unicast address
1321e1ebb9ecSlm66018  * associated with the port into the physical device. If this is not
1322e1ebb9ecSlm66018  * possible due to resource exhaustion or simply because the device does
1323e1ebb9ecSlm66018  * not support multiple unicast addresses then if required fallback onto
1324e1ebb9ecSlm66018  * putting the card into promisc mode.
1325e1ebb9ecSlm66018  *
1326e1ebb9ecSlm66018  * If in promisc mode then simply set the card into promisc mode.
1327e1ebb9ecSlm66018  *
1328e1ebb9ecSlm66018  * Returns 0 success, 1 on failure.
13291ae08745Sheppo  */
1330e1ebb9ecSlm66018 static int
1331e1ebb9ecSlm66018 vsw_set_hw(vsw_t *vswp, vsw_port_t *port)
13321ae08745Sheppo {
1333e1ebb9ecSlm66018 	mac_multi_addr_t	mac_addr;
1334e1ebb9ecSlm66018 	void			*mah;
1335e1ebb9ecSlm66018 	int			err;
13361ae08745Sheppo 
1337e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1338e1ebb9ecSlm66018 
1339e1ebb9ecSlm66018 	if (vswp->smode[vswp->smode_idx] == VSW_LAYER3)
1340e1ebb9ecSlm66018 		return (0);
1341e1ebb9ecSlm66018 
1342e1ebb9ecSlm66018 	if (vswp->smode[vswp->smode_idx] == VSW_LAYER2_PROMISC) {
1343e1ebb9ecSlm66018 		return (vsw_set_hw_promisc(vswp, port));
1344e1ebb9ecSlm66018 	}
1345e1ebb9ecSlm66018 
1346e1ebb9ecSlm66018 	if (vswp->maddr.maddr_handle == NULL)
1347e1ebb9ecSlm66018 		return (1);
1348e1ebb9ecSlm66018 
1349e1ebb9ecSlm66018 	mah = vswp->maddr.maddr_handle;
1350e1ebb9ecSlm66018 
1351e1ebb9ecSlm66018 	/*
1352e1ebb9ecSlm66018 	 * Attempt to program the unicast address into the HW.
1353e1ebb9ecSlm66018 	 */
1354e1ebb9ecSlm66018 	mac_addr.mma_addrlen = ETHERADDRL;
1355e1ebb9ecSlm66018 	ether_copy(&port->p_macaddr, &mac_addr.mma_addr);
1356e1ebb9ecSlm66018 
1357e1ebb9ecSlm66018 	err = vswp->maddr.maddr_add(mah, &mac_addr);
1358e1ebb9ecSlm66018 	if (err != 0) {
1359*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: failed to program addr "
1360e1ebb9ecSlm66018 			"%x:%x:%x:%x:%x:%x for port %d into device %s "
1361*34683adeSsg70180 			": err %d", vswp->instance,
1362*34683adeSsg70180 			port->p_macaddr.ether_addr_octet[0],
1363e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[1],
1364e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[2],
1365e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[3],
1366e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[4],
1367e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[5],
1368e1ebb9ecSlm66018 			port->p_instance, vswp->physname, err);
1369e1ebb9ecSlm66018 
1370e1ebb9ecSlm66018 		/*
1371e1ebb9ecSlm66018 		 * Mark that attempt should be made to re-config sometime
1372e1ebb9ecSlm66018 		 * in future if a port is deleted.
1373e1ebb9ecSlm66018 		 */
1374e1ebb9ecSlm66018 		vswp->recfg_reqd = B_TRUE;
1375e1ebb9ecSlm66018 
1376e1ebb9ecSlm66018 		/*
1377e1ebb9ecSlm66018 		 * Only 1 mode specified, nothing more to do.
1378e1ebb9ecSlm66018 		 */
1379e1ebb9ecSlm66018 		if (vswp->smode_num == 1)
1380e1ebb9ecSlm66018 			return (err);
1381e1ebb9ecSlm66018 
1382e1ebb9ecSlm66018 		/*
1383e1ebb9ecSlm66018 		 * If promiscuous was next mode specified try to
1384e1ebb9ecSlm66018 		 * set the card into that mode.
1385e1ebb9ecSlm66018 		 */
1386e1ebb9ecSlm66018 		if ((vswp->smode_idx <= (vswp->smode_num - 2)) &&
1387e1ebb9ecSlm66018 			(vswp->smode[vswp->smode_idx + 1]
1388e1ebb9ecSlm66018 					== VSW_LAYER2_PROMISC)) {
1389e1ebb9ecSlm66018 			vswp->smode_idx += 1;
1390e1ebb9ecSlm66018 			return (vsw_set_hw_promisc(vswp, port));
1391e1ebb9ecSlm66018 		}
1392e1ebb9ecSlm66018 		return (err);
1393e1ebb9ecSlm66018 	}
1394e1ebb9ecSlm66018 
1395e1ebb9ecSlm66018 	port->addr_slot = mac_addr.mma_slot;
1396e1ebb9ecSlm66018 	port->addr_set = VSW_ADDR_HW;
1397e1ebb9ecSlm66018 
1398e1ebb9ecSlm66018 	D2(vswp, "programmed addr %x:%x:%x:%x:%x:%x for port %d "
1399e1ebb9ecSlm66018 		"into slot %d of device %s",
1400e1ebb9ecSlm66018 		port->p_macaddr.ether_addr_octet[0],
1401e1ebb9ecSlm66018 		port->p_macaddr.ether_addr_octet[1],
1402e1ebb9ecSlm66018 		port->p_macaddr.ether_addr_octet[2],
1403e1ebb9ecSlm66018 		port->p_macaddr.ether_addr_octet[3],
1404e1ebb9ecSlm66018 		port->p_macaddr.ether_addr_octet[4],
1405e1ebb9ecSlm66018 		port->p_macaddr.ether_addr_octet[5],
1406e1ebb9ecSlm66018 		port->p_instance, port->addr_slot, vswp->physname);
1407e1ebb9ecSlm66018 
1408e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1409e1ebb9ecSlm66018 
1410e1ebb9ecSlm66018 	return (0);
1411e1ebb9ecSlm66018 }
1412e1ebb9ecSlm66018 
1413e1ebb9ecSlm66018 /*
1414e1ebb9ecSlm66018  * If in layer 3 mode do nothing.
1415e1ebb9ecSlm66018  *
1416e1ebb9ecSlm66018  * If in layer 2 switched mode remove the address from the physical
1417e1ebb9ecSlm66018  * device.
1418e1ebb9ecSlm66018  *
1419e1ebb9ecSlm66018  * If in layer 2 promiscuous mode disable promisc mode.
1420e1ebb9ecSlm66018  *
1421e1ebb9ecSlm66018  * Returns 0 on success.
1422e1ebb9ecSlm66018  */
1423e1ebb9ecSlm66018 static int
1424e1ebb9ecSlm66018 vsw_unset_hw(vsw_t *vswp, vsw_port_t *port)
1425e1ebb9ecSlm66018 {
1426e1ebb9ecSlm66018 	int		err;
1427e1ebb9ecSlm66018 	void		*mah;
1428e1ebb9ecSlm66018 
1429e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1430e1ebb9ecSlm66018 
1431e1ebb9ecSlm66018 	if (vswp->smode[vswp->smode_idx] == VSW_LAYER3)
1432e1ebb9ecSlm66018 		return (0);
1433e1ebb9ecSlm66018 
1434e1ebb9ecSlm66018 	if (port->addr_set == VSW_ADDR_PROMISC) {
1435e1ebb9ecSlm66018 		return (vsw_unset_hw_promisc(vswp, port));
1436e1ebb9ecSlm66018 	}
1437e1ebb9ecSlm66018 
1438e1ebb9ecSlm66018 	if (port->addr_set == VSW_ADDR_HW) {
1439e1ebb9ecSlm66018 		if (vswp->maddr.maddr_handle == NULL)
1440e1ebb9ecSlm66018 			return (1);
1441e1ebb9ecSlm66018 
1442e1ebb9ecSlm66018 		mah = vswp->maddr.maddr_handle;
1443e1ebb9ecSlm66018 
1444e1ebb9ecSlm66018 		err = vswp->maddr.maddr_remove(mah, port->addr_slot);
1445e1ebb9ecSlm66018 		if (err != 0) {
1446*34683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to remove addr "
1447e1ebb9ecSlm66018 				"%x:%x:%x:%x:%x:%x for port %d from device %s"
1448*34683adeSsg70180 				" : (err %d)", vswp->instance,
1449e1ebb9ecSlm66018 				port->p_macaddr.ether_addr_octet[0],
1450e1ebb9ecSlm66018 				port->p_macaddr.ether_addr_octet[1],
1451e1ebb9ecSlm66018 				port->p_macaddr.ether_addr_octet[2],
1452e1ebb9ecSlm66018 				port->p_macaddr.ether_addr_octet[3],
1453e1ebb9ecSlm66018 				port->p_macaddr.ether_addr_octet[4],
1454e1ebb9ecSlm66018 				port->p_macaddr.ether_addr_octet[5],
1455e1ebb9ecSlm66018 				port->p_instance, vswp->physname, err);
1456e1ebb9ecSlm66018 			return (err);
1457e1ebb9ecSlm66018 		}
1458e1ebb9ecSlm66018 
1459e1ebb9ecSlm66018 		port->addr_set = VSW_ADDR_UNSET;
1460e1ebb9ecSlm66018 
1461e1ebb9ecSlm66018 		D2(vswp, "removed addr %x:%x:%x:%x:%x:%x for "
1462e1ebb9ecSlm66018 			"port %d from device %s",
1463e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[0],
1464e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[1],
1465e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[2],
1466e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[3],
1467e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[4],
1468e1ebb9ecSlm66018 			port->p_macaddr.ether_addr_octet[5],
1469e1ebb9ecSlm66018 			port->p_instance, vswp->physname);
1470e1ebb9ecSlm66018 	}
1471e1ebb9ecSlm66018 
1472e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1473e1ebb9ecSlm66018 	return (0);
1474e1ebb9ecSlm66018 }
1475e1ebb9ecSlm66018 
1476e1ebb9ecSlm66018 /*
1477e1ebb9ecSlm66018  * Set network card into promisc mode.
1478e1ebb9ecSlm66018  *
1479e1ebb9ecSlm66018  * Returns 0 on success, 1 on failure.
1480e1ebb9ecSlm66018  */
1481e1ebb9ecSlm66018 static int
1482e1ebb9ecSlm66018 vsw_set_hw_promisc(vsw_t *vswp, vsw_port_t *port)
1483e1ebb9ecSlm66018 {
1484e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1485e1ebb9ecSlm66018 
1486*34683adeSsg70180 	mutex_enter(&vswp->mac_lock);
1487*34683adeSsg70180 	if (vswp->mh == NULL) {
1488*34683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1489e1ebb9ecSlm66018 		return (1);
1490*34683adeSsg70180 	}
1491e1ebb9ecSlm66018 
1492e1ebb9ecSlm66018 	if (vswp->promisc_cnt++ == 0) {
1493e1ebb9ecSlm66018 		if (mac_promisc_set(vswp->mh, B_TRUE, MAC_DEVPROMISC) != 0) {
1494e1ebb9ecSlm66018 			vswp->promisc_cnt--;
1495*34683adeSsg70180 			mutex_exit(&vswp->mac_lock);
1496e1ebb9ecSlm66018 			return (1);
1497e1ebb9ecSlm66018 		}
1498*34683adeSsg70180 		cmn_err(CE_NOTE, "!vsw%d: switching device %s into "
1499*34683adeSsg70180 			"promiscuous mode", vswp->instance, vswp->physname);
1500e1ebb9ecSlm66018 	}
1501*34683adeSsg70180 	mutex_exit(&vswp->mac_lock);
1502e1ebb9ecSlm66018 	port->addr_set = VSW_ADDR_PROMISC;
1503e1ebb9ecSlm66018 
1504e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1505e1ebb9ecSlm66018 
1506e1ebb9ecSlm66018 	return (0);
1507e1ebb9ecSlm66018 }
1508e1ebb9ecSlm66018 
1509e1ebb9ecSlm66018 /*
1510e1ebb9ecSlm66018  * Turn off promiscuous mode on network card.
1511e1ebb9ecSlm66018  *
1512e1ebb9ecSlm66018  * Returns 0 on success, 1 on failure.
1513e1ebb9ecSlm66018  */
1514e1ebb9ecSlm66018 static int
1515e1ebb9ecSlm66018 vsw_unset_hw_promisc(vsw_t *vswp, vsw_port_t *port)
1516e1ebb9ecSlm66018 {
1517e1ebb9ecSlm66018 	vsw_port_list_t 	*plist = &vswp->plist;
1518e1ebb9ecSlm66018 
1519*34683adeSsg70180 	D2(vswp, "%s: enter", __func__);
1520e1ebb9ecSlm66018 
1521*34683adeSsg70180 	mutex_enter(&vswp->mac_lock);
1522*34683adeSsg70180 	if (vswp->mh == NULL) {
1523*34683adeSsg70180 		mutex_exit(&vswp->mac_lock);
1524e1ebb9ecSlm66018 		return (1);
1525*34683adeSsg70180 	}
1526e1ebb9ecSlm66018 
1527e1ebb9ecSlm66018 	ASSERT(port->addr_set == VSW_ADDR_PROMISC);
1528e1ebb9ecSlm66018 
1529e1ebb9ecSlm66018 	if (--vswp->promisc_cnt == 0) {
1530e1ebb9ecSlm66018 		if (mac_promisc_set(vswp->mh, B_FALSE, MAC_DEVPROMISC) != 0) {
1531e1ebb9ecSlm66018 			vswp->promisc_cnt++;
1532*34683adeSsg70180 			mutex_exit(&vswp->mac_lock);
1533e1ebb9ecSlm66018 			return (1);
1534e1ebb9ecSlm66018 		}
1535e1ebb9ecSlm66018 
1536e1ebb9ecSlm66018 		/*
1537e1ebb9ecSlm66018 		 * We are exiting promisc mode either because we were
1538e1ebb9ecSlm66018 		 * only in promisc mode because we had failed over from
1539e1ebb9ecSlm66018 		 * switched mode due to HW resource issues, or the user
1540e1ebb9ecSlm66018 		 * wanted the card in promisc mode for all the ports and
1541e1ebb9ecSlm66018 		 * the last port is now being deleted. Tweak the message
1542e1ebb9ecSlm66018 		 * accordingly.
1543e1ebb9ecSlm66018 		 */
1544e1ebb9ecSlm66018 		if (plist->num_ports != 0) {
1545*34683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: switching device %s back to "
1546*34683adeSsg70180 				"programmed mode", vswp->instance,
1547*34683adeSsg70180 				vswp->physname);
15481ae08745Sheppo 		} else {
1549*34683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: switching device %s out of "
1550*34683adeSsg70180 				"promiscuous mode", vswp->instance,
1551*34683adeSsg70180 				vswp->physname);
15521ae08745Sheppo 		}
15531ae08745Sheppo 	}
1554*34683adeSsg70180 	mutex_exit(&vswp->mac_lock);
1555e1ebb9ecSlm66018 	port->addr_set = VSW_ADDR_UNSET;
1556e1ebb9ecSlm66018 
1557e1ebb9ecSlm66018 	D1(vswp, "%s: exit", __func__);
1558e1ebb9ecSlm66018 	return (0);
1559e1ebb9ecSlm66018 }
1560e1ebb9ecSlm66018 
1561e1ebb9ecSlm66018 /*
1562e1ebb9ecSlm66018  * Determine whether or not we are operating in our prefered
1563e1ebb9ecSlm66018  * mode and if not whether the physical resources now allow us
1564e1ebb9ecSlm66018  * to operate in it.
1565e1ebb9ecSlm66018  *
1566e1ebb9ecSlm66018  * Should only be invoked after port which is being deleted has been
1567e1ebb9ecSlm66018  * removed from the port list.
1568e1ebb9ecSlm66018  */
1569e1ebb9ecSlm66018 static int
1570e1ebb9ecSlm66018 vsw_reconfig_hw(vsw_t *vswp)
1571e1ebb9ecSlm66018 {
1572e1ebb9ecSlm66018 	vsw_port_list_t 	*plist = &vswp->plist;
1573e1ebb9ecSlm66018 	mac_multi_addr_t	mac_addr;
1574e1ebb9ecSlm66018 	vsw_port_t		*tp;
1575e1ebb9ecSlm66018 	void			*mah;
1576e1ebb9ecSlm66018 	int			rv = 0;
1577e1ebb9ecSlm66018 	int			s_idx;
1578e1ebb9ecSlm66018 
1579e1ebb9ecSlm66018 	D1(vswp, "%s: enter", __func__);
1580e1ebb9ecSlm66018 
1581e1ebb9ecSlm66018 	if (vswp->maddr.maddr_handle == NULL)
1582e1ebb9ecSlm66018 		return (1);
1583e1ebb9ecSlm66018 
1584e1ebb9ecSlm66018 	/*
1585e1ebb9ecSlm66018 	 * Check if there are now sufficient HW resources to
1586e1ebb9ecSlm66018 	 * attempt a re-config.
1587e1ebb9ecSlm66018 	 */
1588e1ebb9ecSlm66018 	if (plist->num_ports > vswp->maddr.maddr_naddrfree)
1589e1ebb9ecSlm66018 		return (1);
1590e1ebb9ecSlm66018 
1591e1ebb9ecSlm66018 	/*
1592e1ebb9ecSlm66018 	 * If we are in layer 2 (i.e. switched) or would like to be
1593e1ebb9ecSlm66018 	 * in layer 2 then check if any ports need to be programmed
1594e1ebb9ecSlm66018 	 * into the HW.
1595e1ebb9ecSlm66018 	 *
1596e1ebb9ecSlm66018 	 * This can happen in two cases - switched was specified as
1597e1ebb9ecSlm66018 	 * the prefered mode of operation but we exhausted the HW
1598e1ebb9ecSlm66018 	 * resources and so failed over to the next specifed mode,
1599e1ebb9ecSlm66018 	 * or switched was the only mode specified so after HW
1600e1ebb9ecSlm66018 	 * resources were exhausted there was nothing more we
1601e1ebb9ecSlm66018 	 * could do.
1602e1ebb9ecSlm66018 	 */
1603e1ebb9ecSlm66018 	if (vswp->smode_idx > 0)
1604e1ebb9ecSlm66018 		s_idx = vswp->smode_idx - 1;
1605e1ebb9ecSlm66018 	else
1606e1ebb9ecSlm66018 		s_idx = vswp->smode_idx;
1607e1ebb9ecSlm66018 
1608e1ebb9ecSlm66018 	if (vswp->smode[s_idx] == VSW_LAYER2) {
1609e1ebb9ecSlm66018 		mah = vswp->maddr.maddr_handle;
1610e1ebb9ecSlm66018 
1611e1ebb9ecSlm66018 		D2(vswp, "%s: attempting reconfig..", __func__);
1612e1ebb9ecSlm66018 
1613e1ebb9ecSlm66018 		/*
1614e1ebb9ecSlm66018 		 * Scan the port list for any port whose address has not
1615e1ebb9ecSlm66018 		 * be programmed in HW - there should be a max of one.
1616e1ebb9ecSlm66018 		 */
1617e1ebb9ecSlm66018 		for (tp = plist->head; tp != NULL; tp = tp->p_next) {
1618e1ebb9ecSlm66018 			if (tp->addr_set != VSW_ADDR_HW) {
1619e1ebb9ecSlm66018 				mac_addr.mma_addrlen = ETHERADDRL;
1620e1ebb9ecSlm66018 				ether_copy(&tp->p_macaddr, &mac_addr.mma_addr);
1621e1ebb9ecSlm66018 
1622e1ebb9ecSlm66018 				rv = vswp->maddr.maddr_add(mah, &mac_addr);
1623e1ebb9ecSlm66018 				if (rv != 0) {
1624e1ebb9ecSlm66018 					DWARN(vswp, "Error setting addr in "
1625e1ebb9ecSlm66018 						"HW for port %d err %d",
1626e1ebb9ecSlm66018 						tp->p_instance, rv);
1627e1ebb9ecSlm66018 					goto reconfig_err_exit;
1628e1ebb9ecSlm66018 				}
1629e1ebb9ecSlm66018 				tp->addr_slot = mac_addr.mma_slot;
1630e1ebb9ecSlm66018 
1631e1ebb9ecSlm66018 				D2(vswp, "re-programmed port %d "
1632e1ebb9ecSlm66018 					"addr %x:%x:%x:%x:%x:%x into slot %d"
1633e1ebb9ecSlm66018 					" of device %s", tp->p_instance,
1634e1ebb9ecSlm66018 					tp->p_macaddr.ether_addr_octet[0],
1635e1ebb9ecSlm66018 					tp->p_macaddr.ether_addr_octet[1],
1636e1ebb9ecSlm66018 					tp->p_macaddr.ether_addr_octet[2],
1637e1ebb9ecSlm66018 					tp->p_macaddr.ether_addr_octet[3],
1638e1ebb9ecSlm66018 					tp->p_macaddr.ether_addr_octet[4],
1639e1ebb9ecSlm66018 					tp->p_macaddr.ether_addr_octet[5],
1640e1ebb9ecSlm66018 					tp->addr_slot, vswp->physname);
1641e1ebb9ecSlm66018 
1642e1ebb9ecSlm66018 				/*
1643e1ebb9ecSlm66018 				 * If up to now we had to put the card into
1644e1ebb9ecSlm66018 				 * promisc mode to see this address, we
1645e1ebb9ecSlm66018 				 * can now safely disable promisc mode.
1646e1ebb9ecSlm66018 				 */
1647e1ebb9ecSlm66018 				if (tp->addr_set == VSW_ADDR_PROMISC)
1648e1ebb9ecSlm66018 					(void) vsw_unset_hw_promisc(vswp, tp);
1649e1ebb9ecSlm66018 
1650e1ebb9ecSlm66018 				tp->addr_set = VSW_ADDR_HW;
1651e1ebb9ecSlm66018 			}
1652e1ebb9ecSlm66018 		}
1653e1ebb9ecSlm66018 
1654e1ebb9ecSlm66018 		/* no further re-config needed */
1655e1ebb9ecSlm66018 		vswp->recfg_reqd = B_FALSE;
1656e1ebb9ecSlm66018 
1657e1ebb9ecSlm66018 		vswp->smode_idx = s_idx;
1658e1ebb9ecSlm66018 
1659e1ebb9ecSlm66018 		return (0);
1660e1ebb9ecSlm66018 	}
1661e1ebb9ecSlm66018 
1662e1ebb9ecSlm66018 reconfig_err_exit:
1663e1ebb9ecSlm66018 	return (rv);
16641ae08745Sheppo }
16651ae08745Sheppo 
16667636cb21Slm66018 static void
16677636cb21Slm66018 vsw_mac_ring_tbl_entry_init(vsw_t *vswp, vsw_mac_ring_t *ringp)
16687636cb21Slm66018 {
16697636cb21Slm66018 	ringp->ring_state = VSW_MAC_RING_FREE;
16707636cb21Slm66018 	ringp->ring_arg = NULL;
16717636cb21Slm66018 	ringp->ring_blank = NULL;
16727636cb21Slm66018 	ringp->ring_vqp = NULL;
16737636cb21Slm66018 	ringp->ring_vswp = vswp;
16747636cb21Slm66018 }
16757636cb21Slm66018 
16767636cb21Slm66018 static void
16777636cb21Slm66018 vsw_mac_ring_tbl_init(vsw_t *vswp)
16787636cb21Slm66018 {
16797636cb21Slm66018 	int		i;
16807636cb21Slm66018 
16817636cb21Slm66018 	mutex_init(&vswp->mac_ring_lock, NULL, MUTEX_DRIVER, NULL);
16827636cb21Slm66018 
16837636cb21Slm66018 	vswp->mac_ring_tbl_sz = vsw_mac_rx_rings;
16847636cb21Slm66018 	vswp->mac_ring_tbl  =
16857636cb21Slm66018 		kmem_alloc(vsw_mac_rx_rings * sizeof (vsw_mac_ring_t),
16867636cb21Slm66018 		KM_SLEEP);
16877636cb21Slm66018 
16887636cb21Slm66018 	for (i = 0; i < vswp->mac_ring_tbl_sz; i++)
16897636cb21Slm66018 		vsw_mac_ring_tbl_entry_init(vswp, &vswp->mac_ring_tbl[i]);
16907636cb21Slm66018 }
16917636cb21Slm66018 
16927636cb21Slm66018 static void
16937636cb21Slm66018 vsw_mac_ring_tbl_destroy(vsw_t *vswp)
16947636cb21Slm66018 {
16957636cb21Slm66018 	int		i;
1696*34683adeSsg70180 	vsw_mac_ring_t	*ringp;
16977636cb21Slm66018 
16987636cb21Slm66018 	mutex_enter(&vswp->mac_ring_lock);
16997636cb21Slm66018 	for (i = 0; i < vswp->mac_ring_tbl_sz; i++) {
1700*34683adeSsg70180 		ringp = &vswp->mac_ring_tbl[i];
1701*34683adeSsg70180 
1702*34683adeSsg70180 		if (ringp->ring_state != VSW_MAC_RING_FREE) {
17037636cb21Slm66018 			/*
17047636cb21Slm66018 			 * Destroy the queue.
17057636cb21Slm66018 			 */
1706*34683adeSsg70180 			vsw_queue_stop(ringp->ring_vqp);
1707*34683adeSsg70180 			vsw_queue_destroy(ringp->ring_vqp);
17087636cb21Slm66018 
17097636cb21Slm66018 			/*
17107636cb21Slm66018 			 * Re-initialize the structure.
17117636cb21Slm66018 			 */
1712*34683adeSsg70180 			vsw_mac_ring_tbl_entry_init(vswp, ringp);
17137636cb21Slm66018 		}
17147636cb21Slm66018 	}
17157636cb21Slm66018 	mutex_exit(&vswp->mac_ring_lock);
17167636cb21Slm66018 
17177636cb21Slm66018 	mutex_destroy(&vswp->mac_ring_lock);
17187636cb21Slm66018 	kmem_free(vswp->mac_ring_tbl,
17197636cb21Slm66018 		vswp->mac_ring_tbl_sz * sizeof (vsw_mac_ring_t));
17207636cb21Slm66018 	vswp->mac_ring_tbl_sz = 0;
17217636cb21Slm66018 }
17227636cb21Slm66018 
17237636cb21Slm66018 /*
17247636cb21Slm66018  * Handle resource add callbacks from the driver below.
17257636cb21Slm66018  */
17267636cb21Slm66018 static mac_resource_handle_t
17277636cb21Slm66018 vsw_mac_ring_add_cb(void *arg, mac_resource_t *mrp)
17287636cb21Slm66018 {
17297636cb21Slm66018 	vsw_t		*vswp = (vsw_t *)arg;
17307636cb21Slm66018 	mac_rx_fifo_t	*mrfp = (mac_rx_fifo_t *)mrp;
17317636cb21Slm66018 	vsw_mac_ring_t	*ringp;
17327636cb21Slm66018 	vsw_queue_t	*vqp;
17337636cb21Slm66018 	int		i;
17347636cb21Slm66018 
17357636cb21Slm66018 	ASSERT(vswp != NULL);
17367636cb21Slm66018 	ASSERT(mrp != NULL);
17377636cb21Slm66018 	ASSERT(vswp->mac_ring_tbl != NULL);
17387636cb21Slm66018 
17397636cb21Slm66018 	D1(vswp, "%s: enter", __func__);
17407636cb21Slm66018 
17417636cb21Slm66018 	/*
17427636cb21Slm66018 	 * Check to make sure we have the correct resource type.
17437636cb21Slm66018 	 */
17447636cb21Slm66018 	if (mrp->mr_type != MAC_RX_FIFO)
17457636cb21Slm66018 		return (NULL);
17467636cb21Slm66018 
17477636cb21Slm66018 	/*
17487636cb21Slm66018 	 * Find a open entry in the ring table.
17497636cb21Slm66018 	 */
17507636cb21Slm66018 	mutex_enter(&vswp->mac_ring_lock);
17517636cb21Slm66018 	for (i = 0; i < vswp->mac_ring_tbl_sz; i++) {
17527636cb21Slm66018 		ringp = &vswp->mac_ring_tbl[i];
17537636cb21Slm66018 
17547636cb21Slm66018 		/*
17557636cb21Slm66018 		 * Check for an empty slot, if found, then setup queue
17567636cb21Slm66018 		 * and thread.
17577636cb21Slm66018 		 */
17587636cb21Slm66018 		if (ringp->ring_state == VSW_MAC_RING_FREE) {
17597636cb21Slm66018 			/*
17607636cb21Slm66018 			 * Create the queue for this ring.
17617636cb21Slm66018 			 */
17627636cb21Slm66018 			vqp = vsw_queue_create();
17637636cb21Slm66018 
17647636cb21Slm66018 			/*
17657636cb21Slm66018 			 * Initialize the ring data structure.
17667636cb21Slm66018 			 */
17677636cb21Slm66018 			ringp->ring_vqp = vqp;
17687636cb21Slm66018 			ringp->ring_arg = mrfp->mrf_arg;
17697636cb21Slm66018 			ringp->ring_blank = mrfp->mrf_blank;
17707636cb21Slm66018 			ringp->ring_state = VSW_MAC_RING_INUSE;
17717636cb21Slm66018 
17727636cb21Slm66018 			/*
17737636cb21Slm66018 			 * Create the worker thread.
17747636cb21Slm66018 			 */
17757636cb21Slm66018 			vqp->vq_worker = thread_create(NULL, 0,
17767636cb21Slm66018 				vsw_queue_worker, ringp, 0, &p0,
17777636cb21Slm66018 				TS_RUN, minclsyspri);
17787636cb21Slm66018 			if (vqp->vq_worker == NULL) {
17797636cb21Slm66018 				vsw_queue_destroy(vqp);
17807636cb21Slm66018 				vsw_mac_ring_tbl_entry_init(vswp, ringp);
17817636cb21Slm66018 				ringp = NULL;
17827636cb21Slm66018 			}
17837636cb21Slm66018 
1784*34683adeSsg70180 			if (ringp != NULL) {
1785*34683adeSsg70180 				/*
1786*34683adeSsg70180 				 * Make sure thread get's running state for
1787*34683adeSsg70180 				 * this ring.
1788*34683adeSsg70180 				 */
1789*34683adeSsg70180 				mutex_enter(&vqp->vq_lock);
1790*34683adeSsg70180 				while ((vqp->vq_state != VSW_QUEUE_RUNNING) &&
1791*34683adeSsg70180 					(vqp->vq_state != VSW_QUEUE_DRAINED)) {
1792*34683adeSsg70180 					cv_wait(&vqp->vq_cv, &vqp->vq_lock);
1793*34683adeSsg70180 				}
1794*34683adeSsg70180 
1795*34683adeSsg70180 				/*
1796*34683adeSsg70180 				 * If the thread is not running, cleanup.
1797*34683adeSsg70180 				 */
1798*34683adeSsg70180 				if (vqp->vq_state == VSW_QUEUE_DRAINED) {
1799*34683adeSsg70180 					vsw_queue_destroy(vqp);
1800*34683adeSsg70180 					vsw_mac_ring_tbl_entry_init(vswp,
1801*34683adeSsg70180 						ringp);
1802*34683adeSsg70180 					ringp = NULL;
1803*34683adeSsg70180 				}
1804*34683adeSsg70180 				mutex_exit(&vqp->vq_lock);
1805*34683adeSsg70180 			}
1806*34683adeSsg70180 
18077636cb21Slm66018 			mutex_exit(&vswp->mac_ring_lock);
18087636cb21Slm66018 			D1(vswp, "%s: exit", __func__);
18097636cb21Slm66018 			return ((mac_resource_handle_t)ringp);
18107636cb21Slm66018 		}
18117636cb21Slm66018 	}
18127636cb21Slm66018 	mutex_exit(&vswp->mac_ring_lock);
18137636cb21Slm66018 
18147636cb21Slm66018 	/*
18157636cb21Slm66018 	 * No slots in the ring table available.
18167636cb21Slm66018 	 */
18177636cb21Slm66018 	D1(vswp, "%s: exit", __func__);
18187636cb21Slm66018 	return (NULL);
18197636cb21Slm66018 }
18207636cb21Slm66018 
18217636cb21Slm66018 static void
18227636cb21Slm66018 vsw_queue_stop(vsw_queue_t *vqp)
18237636cb21Slm66018 {
18247636cb21Slm66018 	mutex_enter(&vqp->vq_lock);
18257636cb21Slm66018 
18267636cb21Slm66018 	if (vqp->vq_state == VSW_QUEUE_RUNNING) {
18277636cb21Slm66018 		vqp->vq_state = VSW_QUEUE_STOP;
18287636cb21Slm66018 		cv_signal(&vqp->vq_cv);
18297636cb21Slm66018 
18307636cb21Slm66018 		while (vqp->vq_state != VSW_QUEUE_DRAINED)
18317636cb21Slm66018 			cv_wait(&vqp->vq_cv, &vqp->vq_lock);
18327636cb21Slm66018 	}
18337636cb21Slm66018 
1834*34683adeSsg70180 	vqp->vq_state = VSW_QUEUE_STOPPED;
1835*34683adeSsg70180 
18367636cb21Slm66018 	mutex_exit(&vqp->vq_lock);
18377636cb21Slm66018 }
18387636cb21Slm66018 
18397636cb21Slm66018 static vsw_queue_t *
18407636cb21Slm66018 vsw_queue_create()
18417636cb21Slm66018 {
18427636cb21Slm66018 	vsw_queue_t *vqp;
18437636cb21Slm66018 
18447636cb21Slm66018 	vqp = kmem_zalloc(sizeof (vsw_queue_t), KM_SLEEP);
18457636cb21Slm66018 
18467636cb21Slm66018 	mutex_init(&vqp->vq_lock, NULL, MUTEX_DRIVER, NULL);
18477636cb21Slm66018 	cv_init(&vqp->vq_cv, NULL, CV_DRIVER, NULL);
18487636cb21Slm66018 	vqp->vq_first = NULL;
18497636cb21Slm66018 	vqp->vq_last = NULL;
1850*34683adeSsg70180 	vqp->vq_state = VSW_QUEUE_STOPPED;
18517636cb21Slm66018 
18527636cb21Slm66018 	return (vqp);
18537636cb21Slm66018 }
18547636cb21Slm66018 
18557636cb21Slm66018 static void
18567636cb21Slm66018 vsw_queue_destroy(vsw_queue_t *vqp)
18577636cb21Slm66018 {
18587636cb21Slm66018 	cv_destroy(&vqp->vq_cv);
18597636cb21Slm66018 	mutex_destroy(&vqp->vq_lock);
18607636cb21Slm66018 	kmem_free(vqp, sizeof (vsw_queue_t));
18617636cb21Slm66018 }
18627636cb21Slm66018 
18637636cb21Slm66018 static void
18647636cb21Slm66018 vsw_queue_worker(vsw_mac_ring_t *rrp)
18657636cb21Slm66018 {
18667636cb21Slm66018 	mblk_t		*mp;
18677636cb21Slm66018 	vsw_queue_t	*vqp = rrp->ring_vqp;
18687636cb21Slm66018 	vsw_t		*vswp = rrp->ring_vswp;
18697636cb21Slm66018 
18707636cb21Slm66018 	mutex_enter(&vqp->vq_lock);
18717636cb21Slm66018 
1872*34683adeSsg70180 	ASSERT(vqp->vq_state == VSW_QUEUE_STOPPED);
18737636cb21Slm66018 
18747636cb21Slm66018 	/*
18757636cb21Slm66018 	 * Set the state to running, since the thread is now active.
18767636cb21Slm66018 	 */
18777636cb21Slm66018 	vqp->vq_state = VSW_QUEUE_RUNNING;
1878*34683adeSsg70180 	cv_signal(&vqp->vq_cv);
18797636cb21Slm66018 
18807636cb21Slm66018 	while (vqp->vq_state == VSW_QUEUE_RUNNING) {
18817636cb21Slm66018 		/*
18827636cb21Slm66018 		 * Wait for work to do or the state has changed
18837636cb21Slm66018 		 * to not running.
18847636cb21Slm66018 		 */
18857636cb21Slm66018 		while ((vqp->vq_state == VSW_QUEUE_RUNNING) &&
18867636cb21Slm66018 				(vqp->vq_first == NULL)) {
18877636cb21Slm66018 			cv_wait(&vqp->vq_cv, &vqp->vq_lock);
18887636cb21Slm66018 		}
18897636cb21Slm66018 
18907636cb21Slm66018 		/*
18917636cb21Slm66018 		 * Process packets that we received from the interface.
18927636cb21Slm66018 		 */
18937636cb21Slm66018 		if (vqp->vq_first != NULL) {
18947636cb21Slm66018 			mp = vqp->vq_first;
18957636cb21Slm66018 
18967636cb21Slm66018 			vqp->vq_first = NULL;
18977636cb21Slm66018 			vqp->vq_last = NULL;
18987636cb21Slm66018 
18997636cb21Slm66018 			mutex_exit(&vqp->vq_lock);
19007636cb21Slm66018 
19017636cb21Slm66018 			/* switch the chain of packets received */
1902*34683adeSsg70180 			vswp->vsw_switch_frame(vswp, mp,
1903*34683adeSsg70180 						VSW_PHYSDEV, NULL, NULL);
19047636cb21Slm66018 
19057636cb21Slm66018 			mutex_enter(&vqp->vq_lock);
19067636cb21Slm66018 		}
19077636cb21Slm66018 	}
19087636cb21Slm66018 
19097636cb21Slm66018 	/*
19107636cb21Slm66018 	 * We are drained and signal we are done.
19117636cb21Slm66018 	 */
19127636cb21Slm66018 	vqp->vq_state = VSW_QUEUE_DRAINED;
19137636cb21Slm66018 	cv_signal(&vqp->vq_cv);
19147636cb21Slm66018 
19157636cb21Slm66018 	/*
19167636cb21Slm66018 	 * Exit lock and drain the remaining packets.
19177636cb21Slm66018 	 */
19187636cb21Slm66018 	mutex_exit(&vqp->vq_lock);
19197636cb21Slm66018 
19207636cb21Slm66018 	/*
19217636cb21Slm66018 	 * Exit the thread
19227636cb21Slm66018 	 */
19237636cb21Slm66018 	thread_exit();
19247636cb21Slm66018 }
19257636cb21Slm66018 
19267636cb21Slm66018 /*
19277636cb21Slm66018  * static void
19287636cb21Slm66018  * vsw_rx_queue_cb() - Receive callback routine when
19297636cb21Slm66018  *	vsw_multi_ring_enable is non-zero.  Queue the packets
19307636cb21Slm66018  *	to a packet queue for a worker thread to process.
19317636cb21Slm66018  */
19327636cb21Slm66018 static void
19337636cb21Slm66018 vsw_rx_queue_cb(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
19347636cb21Slm66018 {
19357636cb21Slm66018 	vsw_mac_ring_t	*ringp = (vsw_mac_ring_t *)mrh;
19367636cb21Slm66018 	vsw_t		*vswp = (vsw_t *)arg;
19377636cb21Slm66018 	vsw_queue_t	*vqp;
19387636cb21Slm66018 	mblk_t		*bp, *last;
19397636cb21Slm66018 
19407636cb21Slm66018 	ASSERT(mrh != NULL);
19417636cb21Slm66018 	ASSERT(vswp != NULL);
19427636cb21Slm66018 	ASSERT(mp != NULL);
19437636cb21Slm66018 
19447636cb21Slm66018 	D1(vswp, "%s: enter", __func__);
19457636cb21Slm66018 
19467636cb21Slm66018 	/*
19477636cb21Slm66018 	 * Find the last element in the mblk chain.
19487636cb21Slm66018 	 */
19497636cb21Slm66018 	bp = mp;
19507636cb21Slm66018 	do {
19517636cb21Slm66018 		last = bp;
19527636cb21Slm66018 		bp = bp->b_next;
19537636cb21Slm66018 	} while (bp != NULL);
19547636cb21Slm66018 
19557636cb21Slm66018 	/* Get the queue for the packets */
19567636cb21Slm66018 	vqp = ringp->ring_vqp;
19577636cb21Slm66018 
19587636cb21Slm66018 	/*
19597636cb21Slm66018 	 * Grab the lock such we can queue the packets.
19607636cb21Slm66018 	 */
19617636cb21Slm66018 	mutex_enter(&vqp->vq_lock);
19627636cb21Slm66018 
19637636cb21Slm66018 	if (vqp->vq_state != VSW_QUEUE_RUNNING) {
19647636cb21Slm66018 		freemsg(mp);
1965*34683adeSsg70180 		mutex_exit(&vqp->vq_lock);
19667636cb21Slm66018 		goto vsw_rx_queue_cb_exit;
19677636cb21Slm66018 	}
19687636cb21Slm66018 
19697636cb21Slm66018 	/*
19707636cb21Slm66018 	 * Add the mblk chain to the queue.  If there
19717636cb21Slm66018 	 * is some mblks in the queue, then add the new
19727636cb21Slm66018 	 * chain to the end.
19737636cb21Slm66018 	 */
19747636cb21Slm66018 	if (vqp->vq_first == NULL)
19757636cb21Slm66018 		vqp->vq_first = mp;
19767636cb21Slm66018 	else
19777636cb21Slm66018 		vqp->vq_last->b_next = mp;
19787636cb21Slm66018 
19797636cb21Slm66018 	vqp->vq_last = last;
19807636cb21Slm66018 
19817636cb21Slm66018 	/*
19827636cb21Slm66018 	 * Signal the worker thread that there is work to
19837636cb21Slm66018 	 * do.
19847636cb21Slm66018 	 */
19857636cb21Slm66018 	cv_signal(&vqp->vq_cv);
19867636cb21Slm66018 
19877636cb21Slm66018 	/*
19887636cb21Slm66018 	 * Let go of the lock and exit.
19897636cb21Slm66018 	 */
19907636cb21Slm66018 	mutex_exit(&vqp->vq_lock);
1991*34683adeSsg70180 
1992*34683adeSsg70180 vsw_rx_queue_cb_exit:
19937636cb21Slm66018 	D1(vswp, "%s: exit", __func__);
19947636cb21Slm66018 }
19957636cb21Slm66018 
19961ae08745Sheppo /*
19971ae08745Sheppo  * receive callback routine. Invoked by MAC layer when there
19981ae08745Sheppo  * are pkts being passed up from physical device.
19991ae08745Sheppo  *
20001ae08745Sheppo  * PERF: It may be more efficient when the card is in promisc
20011ae08745Sheppo  * mode to check the dest address of the pkts here (against
20021ae08745Sheppo  * the FDB) rather than checking later. Needs to be investigated.
20031ae08745Sheppo  */
20041ae08745Sheppo static void
20051ae08745Sheppo vsw_rx_cb(void *arg, mac_resource_handle_t mrh, mblk_t *mp)
20061ae08745Sheppo {
20071ae08745Sheppo 	_NOTE(ARGUNUSED(mrh))
20081ae08745Sheppo 
20091ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
20101ae08745Sheppo 
20111ae08745Sheppo 	ASSERT(vswp != NULL);
20121ae08745Sheppo 
20131ae08745Sheppo 	D1(vswp, "vsw_rx_cb: enter");
20141ae08745Sheppo 
20151ae08745Sheppo 	/* switch the chain of packets received */
2016*34683adeSsg70180 	vswp->vsw_switch_frame(vswp, mp, VSW_PHYSDEV, NULL, NULL);
20171ae08745Sheppo 
20181ae08745Sheppo 	D1(vswp, "vsw_rx_cb: exit");
20191ae08745Sheppo }
20201ae08745Sheppo 
20211ae08745Sheppo /*
20221ae08745Sheppo  * Send a message out over the physical device via the MAC layer.
20231ae08745Sheppo  *
20241ae08745Sheppo  * Returns any mblks that it was unable to transmit.
20251ae08745Sheppo  */
20261ae08745Sheppo static mblk_t *
20271ae08745Sheppo vsw_tx_msg(vsw_t *vswp, mblk_t *mp)
20281ae08745Sheppo {
20291ae08745Sheppo 	const mac_txinfo_t	*mtp;
20301ae08745Sheppo 	mblk_t			*nextp;
20311ae08745Sheppo 
2032*34683adeSsg70180 	mutex_enter(&vswp->mac_lock);
20331ae08745Sheppo 	if (vswp->mh == NULL) {
20341ae08745Sheppo 		DERR(vswp, "vsw_tx_msg: dropping pkts: no tx routine avail");
2035*34683adeSsg70180 		mutex_exit(&vswp->mac_lock);
20361ae08745Sheppo 		return (mp);
20371ae08745Sheppo 	} else {
20381ae08745Sheppo 		for (;;) {
20391ae08745Sheppo 			nextp = mp->b_next;
20401ae08745Sheppo 			mp->b_next = NULL;
20411ae08745Sheppo 
20421ae08745Sheppo 			mtp = vswp->txinfo;
2043*34683adeSsg70180 
20441ae08745Sheppo 			if ((mp = mtp->mt_fn(mtp->mt_arg, mp)) != NULL) {
20451ae08745Sheppo 				mp->b_next = nextp;
20461ae08745Sheppo 				break;
20471ae08745Sheppo 			}
20481ae08745Sheppo 
20491ae08745Sheppo 			if ((mp = nextp) == NULL)
20501ae08745Sheppo 				break;
20511ae08745Sheppo 		}
20521ae08745Sheppo 	}
2053*34683adeSsg70180 	mutex_exit(&vswp->mac_lock);
20541ae08745Sheppo 
20551ae08745Sheppo 	return (mp);
20561ae08745Sheppo }
20571ae08745Sheppo 
20581ae08745Sheppo /*
20591ae08745Sheppo  * Register with the MAC layer as a network device, so we
20601ae08745Sheppo  * can be plumbed if necessary.
20611ae08745Sheppo  */
20621ae08745Sheppo static int
20631ae08745Sheppo vsw_mac_register(vsw_t *vswp)
20641ae08745Sheppo {
2065ba2e4443Sseb 	mac_register_t	*macp;
2066ba2e4443Sseb 	int		rv;
20671ae08745Sheppo 
20681ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
20691ae08745Sheppo 
2070ba2e4443Sseb 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
2071ba2e4443Sseb 		return (EINVAL);
2072ba2e4443Sseb 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
20731ae08745Sheppo 	macp->m_driver = vswp;
2074ba2e4443Sseb 	macp->m_dip = vswp->dip;
2075ba2e4443Sseb 	macp->m_src_addr = (uint8_t *)&vswp->if_addr;
2076ba2e4443Sseb 	macp->m_callbacks = &vsw_m_callbacks;
2077ba2e4443Sseb 	macp->m_min_sdu = 0;
2078ba2e4443Sseb 	macp->m_max_sdu = ETHERMTU;
2079ba2e4443Sseb 	rv = mac_register(macp, &vswp->if_mh);
2080ba2e4443Sseb 	mac_free(macp);
2081ba2e4443Sseb 	if (rv == 0)
2082ba2e4443Sseb 		vswp->if_state |= VSW_IF_REG;
20831ae08745Sheppo 
20841ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
20851ae08745Sheppo 
20861ae08745Sheppo 	return (rv);
20871ae08745Sheppo }
20881ae08745Sheppo 
20891ae08745Sheppo static int
20901ae08745Sheppo vsw_mac_unregister(vsw_t *vswp)
20911ae08745Sheppo {
20921ae08745Sheppo 	int		rv = 0;
20931ae08745Sheppo 
20941ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
20951ae08745Sheppo 
20961ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
20971ae08745Sheppo 
2098ba2e4443Sseb 	if (vswp->if_state & VSW_IF_REG) {
2099ba2e4443Sseb 		rv = mac_unregister(vswp->if_mh);
21001ae08745Sheppo 		if (rv != 0) {
21011ae08745Sheppo 			DWARN(vswp, "%s: unable to unregister from MAC "
21021ae08745Sheppo 				"framework", __func__);
21031ae08745Sheppo 
21041ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
21051ae08745Sheppo 			D1(vswp, "%s: fail exit", __func__);
21061ae08745Sheppo 			return (rv);
21071ae08745Sheppo 		}
21081ae08745Sheppo 
2109ba2e4443Sseb 		/* mark i/f as down and unregistered */
2110ba2e4443Sseb 		vswp->if_state &= ~(VSW_IF_UP | VSW_IF_REG);
21111ae08745Sheppo 	}
21121ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
21131ae08745Sheppo 
21141ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
21151ae08745Sheppo 
21161ae08745Sheppo 	return (rv);
21171ae08745Sheppo }
21181ae08745Sheppo 
2119ba2e4443Sseb static int
2120ba2e4443Sseb vsw_m_stat(void *arg, uint_t stat, uint64_t *val)
21211ae08745Sheppo {
21221ae08745Sheppo 	vsw_t			*vswp = (vsw_t *)arg;
21231ae08745Sheppo 
21241ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
21251ae08745Sheppo 
2126*34683adeSsg70180 	mutex_enter(&vswp->mac_lock);
2127*34683adeSsg70180 	if (vswp->mh == NULL) {
2128*34683adeSsg70180 		mutex_exit(&vswp->mac_lock);
2129ba2e4443Sseb 		return (EINVAL);
2130*34683adeSsg70180 	}
21311ae08745Sheppo 
21321ae08745Sheppo 	/* return stats from underlying device */
2133ba2e4443Sseb 	*val = mac_stat_get(vswp->mh, stat);
2134*34683adeSsg70180 
2135*34683adeSsg70180 	mutex_exit(&vswp->mac_lock);
2136*34683adeSsg70180 
2137ba2e4443Sseb 	return (0);
21381ae08745Sheppo }
21391ae08745Sheppo 
21401ae08745Sheppo static void
21411ae08745Sheppo vsw_m_stop(void *arg)
21421ae08745Sheppo {
21431ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
21441ae08745Sheppo 
21451ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
21461ae08745Sheppo 
21471ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
21481ae08745Sheppo 	vswp->if_state &= ~VSW_IF_UP;
21491ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
21501ae08745Sheppo 
21511ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
21521ae08745Sheppo }
21531ae08745Sheppo 
21541ae08745Sheppo static int
21551ae08745Sheppo vsw_m_start(void *arg)
21561ae08745Sheppo {
21571ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
21581ae08745Sheppo 
21591ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
21601ae08745Sheppo 
21611ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
21621ae08745Sheppo 	vswp->if_state |= VSW_IF_UP;
21631ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
21641ae08745Sheppo 
21651ae08745Sheppo 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
21661ae08745Sheppo 	return (0);
21671ae08745Sheppo }
21681ae08745Sheppo 
21691ae08745Sheppo /*
21701ae08745Sheppo  * Change the local interface address.
21711ae08745Sheppo  */
21721ae08745Sheppo static int
21731ae08745Sheppo vsw_m_unicst(void *arg, const uint8_t *macaddr)
21741ae08745Sheppo {
21751ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
21761ae08745Sheppo 
21771ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
21781ae08745Sheppo 
21791ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
21801ae08745Sheppo 	ether_copy(macaddr, &vswp->if_addr);
21811ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
21821ae08745Sheppo 
21831ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
21841ae08745Sheppo 
21851ae08745Sheppo 	return (0);
21861ae08745Sheppo }
21871ae08745Sheppo 
21881ae08745Sheppo static int
21891ae08745Sheppo vsw_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
21901ae08745Sheppo {
21911ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
21921ae08745Sheppo 	mcst_addr_t	*mcst_p = NULL;
21931ae08745Sheppo 	uint64_t	addr = 0x0;
2194e1ebb9ecSlm66018 	int		i, ret = 0;
21951ae08745Sheppo 
21961ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
21971ae08745Sheppo 
21981ae08745Sheppo 	/*
21991ae08745Sheppo 	 * Convert address into form that can be used
22001ae08745Sheppo 	 * as hash table key.
22011ae08745Sheppo 	 */
22021ae08745Sheppo 	for (i = 0; i < ETHERADDRL; i++) {
22031ae08745Sheppo 		addr = (addr << 8) | mca[i];
22041ae08745Sheppo 	}
22051ae08745Sheppo 
22061ae08745Sheppo 	D2(vswp, "%s: addr = 0x%llx", __func__, addr);
22071ae08745Sheppo 
22081ae08745Sheppo 	if (add) {
22091ae08745Sheppo 		D2(vswp, "%s: adding multicast", __func__);
22101ae08745Sheppo 		if (vsw_add_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
22111ae08745Sheppo 			/*
22121ae08745Sheppo 			 * Update the list of multicast addresses
22131ae08745Sheppo 			 * contained within the vsw_t structure to
22141ae08745Sheppo 			 * include this new one.
22151ae08745Sheppo 			 */
22161ae08745Sheppo 			mcst_p = kmem_zalloc(sizeof (mcst_addr_t), KM_NOSLEEP);
22171ae08745Sheppo 			if (mcst_p == NULL) {
22181ae08745Sheppo 				DERR(vswp, "%s unable to alloc mem", __func__);
22191ae08745Sheppo 				return (1);
22201ae08745Sheppo 			}
22211ae08745Sheppo 			mcst_p->addr = addr;
22221ae08745Sheppo 
22231ae08745Sheppo 			mutex_enter(&vswp->mca_lock);
22241ae08745Sheppo 			mcst_p->nextp = vswp->mcap;
22251ae08745Sheppo 			vswp->mcap = mcst_p;
22261ae08745Sheppo 			mutex_exit(&vswp->mca_lock);
22271ae08745Sheppo 
22281ae08745Sheppo 			/*
22291ae08745Sheppo 			 * Call into the underlying driver to program the
22301ae08745Sheppo 			 * address into HW.
22311ae08745Sheppo 			 */
2232*34683adeSsg70180 			mutex_enter(&vswp->mac_lock);
2233e1ebb9ecSlm66018 			if (vswp->mh != NULL) {
2234e1ebb9ecSlm66018 				ret = mac_multicst_add(vswp->mh, mca);
2235e1ebb9ecSlm66018 				if (ret != 0) {
2236*34683adeSsg70180 					cmn_err(CE_WARN, "!vsw%d: unable to "
2237*34683adeSsg70180 						"add multicast address",
2238*34683adeSsg70180 						vswp->instance);
2239*34683adeSsg70180 					mutex_exit(&vswp->mac_lock);
2240e1ebb9ecSlm66018 					goto vsw_remove_addr;
2241e1ebb9ecSlm66018 				}
22421ae08745Sheppo 			}
2243*34683adeSsg70180 			mutex_exit(&vswp->mac_lock);
22441ae08745Sheppo 		} else {
2245*34683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: unable to add multicast "
2246*34683adeSsg70180 				"address", vswp->instance);
2247e1ebb9ecSlm66018 		}
2248e1ebb9ecSlm66018 		return (ret);
2249e1ebb9ecSlm66018 	}
2250e1ebb9ecSlm66018 
2251e1ebb9ecSlm66018 vsw_remove_addr:
2252e1ebb9ecSlm66018 
22531ae08745Sheppo 	D2(vswp, "%s: removing multicast", __func__);
22541ae08745Sheppo 	/*
22551ae08745Sheppo 	 * Remove the address from the hash table..
22561ae08745Sheppo 	 */
22571ae08745Sheppo 	if (vsw_del_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
22581ae08745Sheppo 
22591ae08745Sheppo 		/*
22601ae08745Sheppo 		 * ..and then from the list maintained in the
22611ae08745Sheppo 		 * vsw_t structure.
22621ae08745Sheppo 		 */
22631ae08745Sheppo 		vsw_del_addr(VSW_LOCALDEV, vswp, addr);
22641ae08745Sheppo 
2265*34683adeSsg70180 		mutex_enter(&vswp->mac_lock);
22661ae08745Sheppo 		if (vswp->mh != NULL)
22671ae08745Sheppo 			(void) mac_multicst_remove(vswp->mh, mca);
2268*34683adeSsg70180 		mutex_exit(&vswp->mac_lock);
22691ae08745Sheppo 	}
22701ae08745Sheppo 
22711ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
22721ae08745Sheppo 
22731ae08745Sheppo 	return (0);
22741ae08745Sheppo }
22751ae08745Sheppo 
22761ae08745Sheppo static int
22771ae08745Sheppo vsw_m_promisc(void *arg, boolean_t on)
22781ae08745Sheppo {
22791ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
22801ae08745Sheppo 
22811ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
22821ae08745Sheppo 
22831ae08745Sheppo 	WRITE_ENTER(&vswp->if_lockrw);
22841ae08745Sheppo 	if (on)
22851ae08745Sheppo 		vswp->if_state |= VSW_IF_PROMISC;
22861ae08745Sheppo 	else
22871ae08745Sheppo 		vswp->if_state &= ~VSW_IF_PROMISC;
22881ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
22891ae08745Sheppo 
22901ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
22911ae08745Sheppo 
22921ae08745Sheppo 	return (0);
22931ae08745Sheppo }
22941ae08745Sheppo 
22951ae08745Sheppo static mblk_t *
22961ae08745Sheppo vsw_m_tx(void *arg, mblk_t *mp)
22971ae08745Sheppo {
22981ae08745Sheppo 	vsw_t		*vswp = (vsw_t *)arg;
22991ae08745Sheppo 
23001ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
23011ae08745Sheppo 
2302*34683adeSsg70180 	vswp->vsw_switch_frame(vswp, mp, VSW_LOCALDEV, NULL, NULL);
23031ae08745Sheppo 
23041ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
23051ae08745Sheppo 
23061ae08745Sheppo 	return (NULL);
23071ae08745Sheppo }
23081ae08745Sheppo 
23091ae08745Sheppo /*
23101ae08745Sheppo  * Register for machine description (MD) updates.
2311*34683adeSsg70180  *
2312*34683adeSsg70180  * Returns 0 on success, 1 on failure.
23131ae08745Sheppo  */
2314*34683adeSsg70180 static int
23151ae08745Sheppo vsw_mdeg_register(vsw_t *vswp)
23161ae08745Sheppo {
23171ae08745Sheppo 	mdeg_prop_spec_t	*pspecp;
23181ae08745Sheppo 	mdeg_node_spec_t	*inst_specp;
2319*34683adeSsg70180 	mdeg_handle_t		mdeg_hdl, mdeg_port_hdl;
23201ae08745Sheppo 	size_t			templatesz;
23211ae08745Sheppo 	int			inst, rv;
23221ae08745Sheppo 
23231ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
23241ae08745Sheppo 
2325*34683adeSsg70180 	/*
2326*34683adeSsg70180 	 * In each 'virtual-device' node in the MD there is a
2327*34683adeSsg70180 	 * 'cfg-handle' property which is the MD's concept of
2328*34683adeSsg70180 	 * an instance number (this may be completely different from
2329*34683adeSsg70180 	 * the device drivers instance #). OBP reads that value and
2330*34683adeSsg70180 	 * stores it in the 'reg' property of the appropriate node in
2331*34683adeSsg70180 	 * the device tree. So we use the 'reg' value when registering
2332*34683adeSsg70180 	 * with the mdeg framework, to ensure we get events for the
2333*34683adeSsg70180 	 * correct nodes.
2334*34683adeSsg70180 	 */
23351ae08745Sheppo 	inst = ddi_prop_get_int(DDI_DEV_T_ANY, vswp->dip,
23361ae08745Sheppo 		DDI_PROP_DONTPASS, reg_propname, -1);
23371ae08745Sheppo 	if (inst == -1) {
2338*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read %s property from "
2339*34683adeSsg70180 			"OBP device tree", vswp->instance, reg_propname);
2340*34683adeSsg70180 		return (1);
23411ae08745Sheppo 	}
23421ae08745Sheppo 
23431ae08745Sheppo 	D2(vswp, "%s: instance %d registering with mdeg", __func__, inst);
23441ae08745Sheppo 
23451ae08745Sheppo 	/*
23461ae08745Sheppo 	 * Allocate and initialize a per-instance copy
23471ae08745Sheppo 	 * of the global property spec array that will
23481ae08745Sheppo 	 * uniquely identify this vsw instance.
23491ae08745Sheppo 	 */
23501ae08745Sheppo 	templatesz = sizeof (vsw_prop_template);
23511ae08745Sheppo 	pspecp = kmem_zalloc(templatesz, KM_SLEEP);
23521ae08745Sheppo 
23531ae08745Sheppo 	bcopy(vsw_prop_template, pspecp, templatesz);
23541ae08745Sheppo 
23551ae08745Sheppo 	VSW_SET_MDEG_PROP_INST(pspecp, inst);
23561ae08745Sheppo 
23571ae08745Sheppo 	/* initialize the complete prop spec structure */
23581ae08745Sheppo 	inst_specp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
23591ae08745Sheppo 	inst_specp->namep = "virtual-device";
23601ae08745Sheppo 	inst_specp->specp = pspecp;
23611ae08745Sheppo 
2362*34683adeSsg70180 	/*
2363*34683adeSsg70180 	 * Register an interest in 'virtual-device' nodes with a
2364*34683adeSsg70180 	 * 'name' property of 'virtual-network-switch'
2365*34683adeSsg70180 	 */
2366*34683adeSsg70180 	rv = mdeg_register(inst_specp, &vdev_match, vsw_mdeg_cb,
23671ae08745Sheppo 	    (void *)vswp, &mdeg_hdl);
2368*34683adeSsg70180 	if (rv != MDEG_SUCCESS) {
2369*34683adeSsg70180 		DERR(vswp, "%s: mdeg_register failed (%d) for vsw node",
2370*34683adeSsg70180 			__func__, rv);
2371*34683adeSsg70180 		goto mdeg_reg_fail;
2372*34683adeSsg70180 	}
23731ae08745Sheppo 
2374*34683adeSsg70180 	/*
2375*34683adeSsg70180 	 * Register an interest in 'vsw-port' nodes.
2376*34683adeSsg70180 	 */
2377*34683adeSsg70180 	rv = mdeg_register(inst_specp, &vport_match, vsw_port_mdeg_cb,
2378*34683adeSsg70180 	    (void *)vswp, &mdeg_port_hdl);
23791ae08745Sheppo 	if (rv != MDEG_SUCCESS) {
23801ae08745Sheppo 		DERR(vswp, "%s: mdeg_register failed (%d)\n", __func__, rv);
2381*34683adeSsg70180 		(void) mdeg_unregister(mdeg_hdl);
2382*34683adeSsg70180 		goto mdeg_reg_fail;
23831ae08745Sheppo 	}
23841ae08745Sheppo 
23851ae08745Sheppo 	/* save off data that will be needed later */
23861ae08745Sheppo 	vswp->inst_spec = inst_specp;
23871ae08745Sheppo 	vswp->mdeg_hdl = mdeg_hdl;
2388*34683adeSsg70180 	vswp->mdeg_port_hdl = mdeg_port_hdl;
23891ae08745Sheppo 
23901ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
2391*34683adeSsg70180 	return (0);
2392*34683adeSsg70180 
2393*34683adeSsg70180 mdeg_reg_fail:
2394*34683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: Unable to register MDEG callbacks",
2395*34683adeSsg70180 				vswp->instance);
2396*34683adeSsg70180 	kmem_free(pspecp, templatesz);
2397*34683adeSsg70180 	kmem_free(inst_specp, sizeof (mdeg_node_spec_t));
2398*34683adeSsg70180 
2399*34683adeSsg70180 	vswp->mdeg_hdl = NULL;
2400*34683adeSsg70180 	vswp->mdeg_port_hdl = NULL;
2401*34683adeSsg70180 
2402*34683adeSsg70180 	return (1);
24031ae08745Sheppo }
24041ae08745Sheppo 
24051ae08745Sheppo static void
24061ae08745Sheppo vsw_mdeg_unregister(vsw_t *vswp)
24071ae08745Sheppo {
24081ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: enter");
24091ae08745Sheppo 
2410*34683adeSsg70180 	if (vswp->mdeg_hdl != NULL)
24111ae08745Sheppo 		(void) mdeg_unregister(vswp->mdeg_hdl);
24121ae08745Sheppo 
2413*34683adeSsg70180 	if (vswp->mdeg_port_hdl != NULL)
2414*34683adeSsg70180 		(void) mdeg_unregister(vswp->mdeg_port_hdl);
2415*34683adeSsg70180 
2416*34683adeSsg70180 	if (vswp->inst_spec != NULL) {
24171ae08745Sheppo 		if (vswp->inst_spec->specp != NULL) {
24181ae08745Sheppo 			(void) kmem_free(vswp->inst_spec->specp,
24191ae08745Sheppo 				sizeof (vsw_prop_template));
24201ae08745Sheppo 			vswp->inst_spec->specp = NULL;
24211ae08745Sheppo 		}
24221ae08745Sheppo 
24231ae08745Sheppo 		(void) kmem_free(vswp->inst_spec,
24241ae08745Sheppo 			sizeof (mdeg_node_spec_t));
24251ae08745Sheppo 		vswp->inst_spec = NULL;
24261ae08745Sheppo 	}
24271ae08745Sheppo 
24281ae08745Sheppo 	D1(vswp, "vsw_mdeg_unregister: exit");
24291ae08745Sheppo }
24301ae08745Sheppo 
2431*34683adeSsg70180 /*
2432*34683adeSsg70180  * Mdeg callback invoked for the vsw node itself.
2433*34683adeSsg70180  */
24341ae08745Sheppo static int
24351ae08745Sheppo vsw_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
24361ae08745Sheppo {
24371ae08745Sheppo 	vsw_t		*vswp;
24381ae08745Sheppo 	int		idx;
24391ae08745Sheppo 	md_t		*mdp;
24401ae08745Sheppo 	mde_cookie_t	node;
24411ae08745Sheppo 	uint64_t	inst;
2442*34683adeSsg70180 	char		*node_name = NULL;
24431ae08745Sheppo 
24441ae08745Sheppo 	if (resp == NULL)
24451ae08745Sheppo 		return (MDEG_FAILURE);
24461ae08745Sheppo 
24471ae08745Sheppo 	vswp = (vsw_t *)cb_argp;
24481ae08745Sheppo 
2449*34683adeSsg70180 	D1(vswp, "%s: added %d : removed %d : curr matched %d"
2450*34683adeSsg70180 		" : prev matched %d", __func__, resp->added.nelem,
2451*34683adeSsg70180 		resp->removed.nelem, resp->match_curr.nelem,
2452*34683adeSsg70180 		resp->match_prev.nelem);
2453*34683adeSsg70180 
2454*34683adeSsg70180 	/*
2455*34683adeSsg70180 	 * Expect 'added' to be non-zero if virtual-network-switch
2456*34683adeSsg70180 	 * nodes exist in the MD when the driver attaches.
2457*34683adeSsg70180 	 */
2458*34683adeSsg70180 	for (idx = 0; idx < resp->added.nelem; idx++) {
2459*34683adeSsg70180 		mdp = resp->added.mdp;
2460*34683adeSsg70180 		node = resp->added.mdep[idx];
2461*34683adeSsg70180 
2462*34683adeSsg70180 		if (md_get_prop_str(mdp, node, "name", &node_name) != 0) {
2463*34683adeSsg70180 			DERR(vswp, "%s: unable to get node name for "
2464*34683adeSsg70180 				"node(%d) 0x%lx", __func__, idx, node);
2465*34683adeSsg70180 			continue;
2466*34683adeSsg70180 		}
2467*34683adeSsg70180 
2468*34683adeSsg70180 		if (md_get_prop_val(mdp, node, "cfg-handle", &inst)) {
2469*34683adeSsg70180 			DERR(vswp, "%s: prop(cfg-handle) not found port(%d)",
2470*34683adeSsg70180 				__func__, idx);
2471*34683adeSsg70180 			continue;
2472*34683adeSsg70180 		}
2473*34683adeSsg70180 
2474*34683adeSsg70180 		D2(vswp, "%s: added node(%d) 0x%lx with name %s "
2475*34683adeSsg70180 			"and inst %d", __func__, idx, node, node_name, inst);
2476*34683adeSsg70180 
2477*34683adeSsg70180 		vsw_get_initial_md_properties(vswp, mdp, node);
2478*34683adeSsg70180 	}
2479*34683adeSsg70180 
2480*34683adeSsg70180 	/*
2481*34683adeSsg70180 	 * A non-zero 'match' value indicates that the MD has been
2482*34683adeSsg70180 	 * updated and that a virtual-network-switch node is present
2483*34683adeSsg70180 	 * which may or may not have been updated. It is up to the clients
2484*34683adeSsg70180 	 * to examine their own nodes and determine if they have changed.
2485*34683adeSsg70180 	 */
2486*34683adeSsg70180 	for (idx = 0; idx < resp->match_curr.nelem; idx++) {
2487*34683adeSsg70180 		mdp = resp->match_curr.mdp;
2488*34683adeSsg70180 		node = resp->match_curr.mdep[idx];
2489*34683adeSsg70180 
2490*34683adeSsg70180 		if (md_get_prop_str(mdp, node, "name", &node_name) != 0) {
2491*34683adeSsg70180 			DERR(vswp, "%s: unable to get node name for "
2492*34683adeSsg70180 				"node(%d) 0x%lx", __func__, idx, node);
2493*34683adeSsg70180 			continue;
2494*34683adeSsg70180 		}
2495*34683adeSsg70180 
2496*34683adeSsg70180 		if (md_get_prop_val(mdp, node, "cfg-handle", &inst)) {
2497*34683adeSsg70180 			DERR(vswp, "%s: prop(cfg-handle) not found port(%d)",
2498*34683adeSsg70180 				__func__, idx);
2499*34683adeSsg70180 			continue;
2500*34683adeSsg70180 		}
2501*34683adeSsg70180 
2502*34683adeSsg70180 		D2(vswp, "%s: changed node(%d) 0x%lx with name %s "
2503*34683adeSsg70180 			"and inst %d", __func__, idx, node, node_name, inst);
2504*34683adeSsg70180 
2505*34683adeSsg70180 		vsw_update_md_prop(vswp, mdp, node);
2506*34683adeSsg70180 	}
2507*34683adeSsg70180 
2508*34683adeSsg70180 	return (MDEG_SUCCESS);
2509*34683adeSsg70180 }
2510*34683adeSsg70180 
2511*34683adeSsg70180 /*
2512*34683adeSsg70180  * Mdeg callback invoked for changes to the vsw-port nodes
2513*34683adeSsg70180  * under the vsw node.
2514*34683adeSsg70180  */
2515*34683adeSsg70180 static int
2516*34683adeSsg70180 vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
2517*34683adeSsg70180 {
2518*34683adeSsg70180 	vsw_t		*vswp;
2519*34683adeSsg70180 	int		idx;
2520*34683adeSsg70180 	md_t		*mdp;
2521*34683adeSsg70180 	mde_cookie_t	node;
2522*34683adeSsg70180 	uint64_t	inst;
2523*34683adeSsg70180 
2524*34683adeSsg70180 	if ((resp == NULL) || (cb_argp == NULL))
2525*34683adeSsg70180 		return (MDEG_FAILURE);
2526*34683adeSsg70180 
2527*34683adeSsg70180 	vswp = (vsw_t *)cb_argp;
2528*34683adeSsg70180 
2529*34683adeSsg70180 	D2(vswp, "%s: added %d : removed %d : curr matched %d"
2530*34683adeSsg70180 		" : prev matched %d", __func__, resp->added.nelem,
2531*34683adeSsg70180 		resp->removed.nelem, resp->match_curr.nelem,
25321ae08745Sheppo 		resp->match_prev.nelem);
25331ae08745Sheppo 
25341ae08745Sheppo 	/* process added ports */
25351ae08745Sheppo 	for (idx = 0; idx < resp->added.nelem; idx++) {
25361ae08745Sheppo 		mdp = resp->added.mdp;
25371ae08745Sheppo 		node = resp->added.mdep[idx];
25381ae08745Sheppo 
25391ae08745Sheppo 		D2(vswp, "%s: adding node(%d) 0x%lx", __func__, idx, node);
25401ae08745Sheppo 
25411ae08745Sheppo 		if (vsw_port_add(vswp, mdp, &node) != 0) {
2542*34683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to add new port "
2543*34683adeSsg70180 				"(0x%lx)", vswp->instance, node);
25441ae08745Sheppo 		}
25451ae08745Sheppo 	}
25461ae08745Sheppo 
25471ae08745Sheppo 	/* process removed ports */
25481ae08745Sheppo 	for (idx = 0; idx < resp->removed.nelem; idx++) {
25491ae08745Sheppo 		mdp = resp->removed.mdp;
25501ae08745Sheppo 		node = resp->removed.mdep[idx];
25511ae08745Sheppo 
25521ae08745Sheppo 		if (md_get_prop_val(mdp, node, id_propname, &inst)) {
2553*34683adeSsg70180 			DERR(vswp, "%s: prop(%s) not found in port(%d)",
25541ae08745Sheppo 				__func__, id_propname, idx);
25551ae08745Sheppo 			continue;
25561ae08745Sheppo 		}
25571ae08745Sheppo 
25581ae08745Sheppo 		D2(vswp, "%s: removing node(%d) 0x%lx", __func__, idx, node);
25591ae08745Sheppo 
25601ae08745Sheppo 		if (vsw_port_detach(vswp, inst) != 0) {
2561*34683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to remove port %ld",
2562*34683adeSsg70180 				vswp->instance, inst);
25631ae08745Sheppo 		}
25641ae08745Sheppo 	}
25651ae08745Sheppo 
25661ae08745Sheppo 	/*
25671ae08745Sheppo 	 * Currently no support for updating already active ports.
25681ae08745Sheppo 	 * So, ignore the match_curr and match_priv arrays for now.
25691ae08745Sheppo 	 */
25701ae08745Sheppo 
25711ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
25721ae08745Sheppo 
25731ae08745Sheppo 	return (MDEG_SUCCESS);
25741ae08745Sheppo }
25751ae08745Sheppo 
25761ae08745Sheppo /*
2577*34683adeSsg70180  * Read the initial start-of-day values from the specified MD node.
2578*34683adeSsg70180  */
2579*34683adeSsg70180 static void
2580*34683adeSsg70180 vsw_get_initial_md_properties(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
2581*34683adeSsg70180 {
2582*34683adeSsg70180 	int		i;
2583*34683adeSsg70180 	uint64_t 	macaddr = 0;
2584*34683adeSsg70180 
2585*34683adeSsg70180 	D1(vswp, "%s: enter", __func__);
2586*34683adeSsg70180 
2587*34683adeSsg70180 	if (vsw_get_md_physname(vswp, mdp, node, vswp->physname) == 0) {
2588*34683adeSsg70180 		/*
2589*34683adeSsg70180 		 * Note it is valid for the physname property to
2590*34683adeSsg70180 		 * be NULL so check actual name length to determine
2591*34683adeSsg70180 		 * if we have a actual device name.
2592*34683adeSsg70180 		 */
2593*34683adeSsg70180 		if (strlen(vswp->physname) > 0)
2594*34683adeSsg70180 			vswp->mdprops |= VSW_MD_PHYSNAME;
2595*34683adeSsg70180 	} else {
2596*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read name of physical "
2597*34683adeSsg70180 			"device from MD", vswp->instance);
2598*34683adeSsg70180 		return;
2599*34683adeSsg70180 	}
2600*34683adeSsg70180 
2601*34683adeSsg70180 	/* mac address for vswitch device itself */
2602*34683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
2603*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
2604*34683adeSsg70180 			vswp->instance);
2605*34683adeSsg70180 
2606*34683adeSsg70180 		/*
2607*34683adeSsg70180 		 * Fallback to using the mac address of the physical
2608*34683adeSsg70180 		 * device.
2609*34683adeSsg70180 		 */
2610*34683adeSsg70180 		if (vsw_get_physaddr(vswp) == 0) {
2611*34683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: Using MAC address from "
2612*34683adeSsg70180 				"physical device (%s)", vswp->instance,
2613*34683adeSsg70180 				vswp->physname);
2614*34683adeSsg70180 		} else {
2615*34683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address"
2616*34683adeSsg70180 				"from device %s", vswp->instance,
2617*34683adeSsg70180 				vswp->physname);
2618*34683adeSsg70180 		}
2619*34683adeSsg70180 	} else {
2620*34683adeSsg70180 		WRITE_ENTER(&vswp->if_lockrw);
2621*34683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
2622*34683adeSsg70180 			vswp->if_addr.ether_addr_octet[i] = macaddr & 0xFF;
2623*34683adeSsg70180 			macaddr >>= 8;
2624*34683adeSsg70180 		}
2625*34683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
2626*34683adeSsg70180 		vswp->mdprops |= VSW_MD_MACADDR;
2627*34683adeSsg70180 	}
2628*34683adeSsg70180 
2629*34683adeSsg70180 	if (vsw_get_md_smodes(vswp, mdp, node,
2630*34683adeSsg70180 				vswp->smode, &vswp->smode_num)) {
2631*34683adeSsg70180 		cmn_err(CE_WARN, "vsw%d: Unable to read %s property from "
2632*34683adeSsg70180 			"MD, defaulting to programmed mode", vswp->instance,
2633*34683adeSsg70180 			smode_propname);
2634*34683adeSsg70180 
2635*34683adeSsg70180 		for (i = 0; i < NUM_SMODES; i++)
2636*34683adeSsg70180 			vswp->smode[i] = VSW_LAYER2;
2637*34683adeSsg70180 
2638*34683adeSsg70180 		vswp->smode_num = NUM_SMODES;
2639*34683adeSsg70180 	} else {
2640*34683adeSsg70180 		ASSERT(vswp->smode_num != 0);
2641*34683adeSsg70180 		vswp->mdprops |= VSW_MD_SMODE;
2642*34683adeSsg70180 	}
2643*34683adeSsg70180 
2644*34683adeSsg70180 	/*
2645*34683adeSsg70180 	 * Unable to setup any switching mode, nothing more
2646*34683adeSsg70180 	 * we can do.
2647*34683adeSsg70180 	 */
2648*34683adeSsg70180 	if (vsw_setup_switching(vswp))
2649*34683adeSsg70180 		return;
2650*34683adeSsg70180 
2651*34683adeSsg70180 	WRITE_ENTER(&vswp->if_lockrw);
2652*34683adeSsg70180 	vswp->if_state &= ~VSW_IF_UP;
2653*34683adeSsg70180 	RW_EXIT(&vswp->if_lockrw);
2654*34683adeSsg70180 	if (vswp->mdprops & (VSW_MD_MACADDR | VSW_DEV_MACADDR)) {
2655*34683adeSsg70180 		if (vsw_mac_register(vswp) != 0) {
2656*34683adeSsg70180 			/*
2657*34683adeSsg70180 			 * Treat this as a non-fatal error as we may be
2658*34683adeSsg70180 			 * able to operate in some other mode.
2659*34683adeSsg70180 			 */
2660*34683adeSsg70180 			cmn_err(CE_WARN, "vsw%d: Unable to register as "
2661*34683adeSsg70180 				"provider with MAC layer", vswp->instance);
2662*34683adeSsg70180 		}
2663*34683adeSsg70180 	}
2664*34683adeSsg70180 
2665*34683adeSsg70180 	D1(vswp, "%s: exit", __func__);
2666*34683adeSsg70180 }
2667*34683adeSsg70180 
2668*34683adeSsg70180 /*
2669*34683adeSsg70180  * Check to see if the relevant properties in the specified node have
2670*34683adeSsg70180  * changed, and if so take the appropriate action.
2671*34683adeSsg70180  *
2672*34683adeSsg70180  * If any of the properties are missing or invalid we don't take
2673*34683adeSsg70180  * any action, as this function should only be invoked when modifications
2674*34683adeSsg70180  * have been made to what we assume is a working configuration, which
2675*34683adeSsg70180  * we leave active.
2676*34683adeSsg70180  *
2677*34683adeSsg70180  * Note it is legal for this routine to be invoked even if none of the
2678*34683adeSsg70180  * properties in the port node within the MD have actually changed.
2679*34683adeSsg70180  */
2680*34683adeSsg70180 static void
2681*34683adeSsg70180 vsw_update_md_prop(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
2682*34683adeSsg70180 {
2683*34683adeSsg70180 	char		physname[LIFNAMSIZ];
2684*34683adeSsg70180 	char		drv[LIFNAMSIZ];
2685*34683adeSsg70180 	uint_t		ddi_instance;
2686*34683adeSsg70180 	uint8_t		new_smode[NUM_SMODES];
2687*34683adeSsg70180 	int		i, smode_num = 0;
2688*34683adeSsg70180 	uint64_t 	macaddr = 0;
2689*34683adeSsg70180 	vsw_port_list_t *plist = &vswp->plist;
2690*34683adeSsg70180 	vsw_port_t	*port = NULL;
2691*34683adeSsg70180 	enum		{MD_init = 0x1,
2692*34683adeSsg70180 				MD_physname = 0x2,
2693*34683adeSsg70180 				MD_macaddr = 0x4,
2694*34683adeSsg70180 				MD_smode = 0x8} updated;
2695*34683adeSsg70180 
2696*34683adeSsg70180 	updated = MD_init;
2697*34683adeSsg70180 
2698*34683adeSsg70180 	D1(vswp, "%s: enter", __func__);
2699*34683adeSsg70180 
2700*34683adeSsg70180 	/*
2701*34683adeSsg70180 	 * Check if name of physical device in MD has changed.
2702*34683adeSsg70180 	 */
2703*34683adeSsg70180 	if (vsw_get_md_physname(vswp, mdp, node, (char *)&physname) == 0) {
2704*34683adeSsg70180 		/*
2705*34683adeSsg70180 		 * Do basic sanity check on new device name/instance,
2706*34683adeSsg70180 		 * if its non NULL. It is valid for the device name to
2707*34683adeSsg70180 		 * have changed from a non NULL to a NULL value, i.e.
2708*34683adeSsg70180 		 * the vsw is being changed to 'routed' mode.
2709*34683adeSsg70180 		 */
2710*34683adeSsg70180 		if ((strlen(physname) != 0) &&
2711*34683adeSsg70180 			(ddi_parse(physname, drv,
2712*34683adeSsg70180 				&ddi_instance) != DDI_SUCCESS)) {
2713*34683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: new device name %s is not"
2714*34683adeSsg70180 				" a valid device name/instance",
2715*34683adeSsg70180 				vswp->instance, physname);
2716*34683adeSsg70180 			goto fail_reconf;
2717*34683adeSsg70180 		}
2718*34683adeSsg70180 
2719*34683adeSsg70180 		if (strcmp(physname, vswp->physname)) {
2720*34683adeSsg70180 			D2(vswp, "%s: device name changed from %s to %s",
2721*34683adeSsg70180 					__func__, vswp->physname, physname);
2722*34683adeSsg70180 
2723*34683adeSsg70180 			updated |= MD_physname;
2724*34683adeSsg70180 		} else {
2725*34683adeSsg70180 			D2(vswp, "%s: device name unchanged at %s",
2726*34683adeSsg70180 					__func__, vswp->physname);
2727*34683adeSsg70180 		}
2728*34683adeSsg70180 	} else {
2729*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read name of physical "
2730*34683adeSsg70180 			"device from updated MD.", vswp->instance);
2731*34683adeSsg70180 		goto fail_reconf;
2732*34683adeSsg70180 	}
2733*34683adeSsg70180 
2734*34683adeSsg70180 	/*
2735*34683adeSsg70180 	 * Check if MAC address has changed.
2736*34683adeSsg70180 	 */
2737*34683adeSsg70180 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
2738*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
2739*34683adeSsg70180 			vswp->instance);
2740*34683adeSsg70180 		goto fail_reconf;
2741*34683adeSsg70180 	} else {
2742*34683adeSsg70180 		READ_ENTER(&vswp->if_lockrw);
2743*34683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
2744*34683adeSsg70180 			if (vswp->if_addr.ether_addr_octet[i]
2745*34683adeSsg70180 							!= (macaddr & 0xFF)) {
2746*34683adeSsg70180 				D2(vswp, "%s: octet[%d] 0x%x != 0x%x",
2747*34683adeSsg70180 					__func__, i,
2748*34683adeSsg70180 					vswp->if_addr.ether_addr_octet[i],
2749*34683adeSsg70180 					(macaddr & 0xFF));
2750*34683adeSsg70180 				updated |= MD_macaddr;
2751*34683adeSsg70180 				break;
2752*34683adeSsg70180 			}
2753*34683adeSsg70180 			macaddr >>= 8;
2754*34683adeSsg70180 		}
2755*34683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
2756*34683adeSsg70180 	}
2757*34683adeSsg70180 
2758*34683adeSsg70180 	/*
2759*34683adeSsg70180 	 * Check if switching modes have changed.
2760*34683adeSsg70180 	 */
2761*34683adeSsg70180 	if (vsw_get_md_smodes(vswp, mdp, node,
2762*34683adeSsg70180 				new_smode, &smode_num)) {
2763*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read %s property from MD",
2764*34683adeSsg70180 					vswp->instance, smode_propname);
2765*34683adeSsg70180 		goto fail_reconf;
2766*34683adeSsg70180 	} else {
2767*34683adeSsg70180 		ASSERT(smode_num != 0);
2768*34683adeSsg70180 		if (smode_num != vswp->smode_num) {
2769*34683adeSsg70180 			D2(vswp, "%s: number of modes changed from %d to %d",
2770*34683adeSsg70180 				__func__, vswp->smode_num, smode_num);
2771*34683adeSsg70180 		}
2772*34683adeSsg70180 
2773*34683adeSsg70180 		for (i = 0; i < smode_num; i++) {
2774*34683adeSsg70180 			if (new_smode[i] != vswp->smode[i]) {
2775*34683adeSsg70180 				D2(vswp, "%s: mode changed from %d to %d",
2776*34683adeSsg70180 					__func__, vswp->smode[i], new_smode[i]);
2777*34683adeSsg70180 				updated |= MD_smode;
2778*34683adeSsg70180 				break;
2779*34683adeSsg70180 			}
2780*34683adeSsg70180 		}
2781*34683adeSsg70180 	}
2782*34683adeSsg70180 
2783*34683adeSsg70180 	/*
2784*34683adeSsg70180 	 * Now make any changes which are needed...
2785*34683adeSsg70180 	 */
2786*34683adeSsg70180 
2787*34683adeSsg70180 	if (updated & (MD_physname | MD_smode)) {
2788*34683adeSsg70180 		/*
2789*34683adeSsg70180 		 * Disconnect all ports from the current card
2790*34683adeSsg70180 		 */
2791*34683adeSsg70180 		WRITE_ENTER(&plist->lockrw);
2792*34683adeSsg70180 		for (port = plist->head; port != NULL; port = port->p_next) {
2793*34683adeSsg70180 			/* Remove address if was programmed into HW. */
2794*34683adeSsg70180 			if (vsw_unset_hw(vswp, port)) {
2795*34683adeSsg70180 				RW_EXIT(&plist->lockrw);
2796*34683adeSsg70180 				goto fail_update;
2797*34683adeSsg70180 			}
2798*34683adeSsg70180 		}
2799*34683adeSsg70180 		RW_EXIT(&plist->lockrw);
2800*34683adeSsg70180 
2801*34683adeSsg70180 		/*
2802*34683adeSsg70180 		 * Stop, detach the old device..
2803*34683adeSsg70180 		 */
2804*34683adeSsg70180 		vsw_mac_detach(vswp);
2805*34683adeSsg70180 
2806*34683adeSsg70180 		/*
2807*34683adeSsg70180 		 * Update phys name.
2808*34683adeSsg70180 		 */
2809*34683adeSsg70180 		if (updated & MD_physname) {
2810*34683adeSsg70180 			cmn_err(CE_NOTE, "!vsw%d: changing from %s to %s",
2811*34683adeSsg70180 				vswp->instance, vswp->physname, physname);
2812*34683adeSsg70180 			(void) strncpy(vswp->physname,
2813*34683adeSsg70180 					physname, strlen(physname) + 1);
2814*34683adeSsg70180 
2815*34683adeSsg70180 			if (strlen(vswp->physname) > 0)
2816*34683adeSsg70180 				vswp->mdprops |= VSW_MD_PHYSNAME;
2817*34683adeSsg70180 		}
2818*34683adeSsg70180 
2819*34683adeSsg70180 		/*
2820*34683adeSsg70180 		 * Update array with the new switch mode values.
2821*34683adeSsg70180 		 */
2822*34683adeSsg70180 		if (updated & MD_smode) {
2823*34683adeSsg70180 			for (i = 0; i < smode_num; i++)
2824*34683adeSsg70180 				vswp->smode[i] = new_smode[i];
2825*34683adeSsg70180 
2826*34683adeSsg70180 			vswp->smode_num = smode_num;
2827*34683adeSsg70180 			vswp->smode_idx = 0;
2828*34683adeSsg70180 		}
2829*34683adeSsg70180 
2830*34683adeSsg70180 		/*
2831*34683adeSsg70180 		 * ..and attach, start the new device.
2832*34683adeSsg70180 		 */
2833*34683adeSsg70180 		if (vsw_setup_switching(vswp))
2834*34683adeSsg70180 			goto fail_update;
2835*34683adeSsg70180 
2836*34683adeSsg70180 		/*
2837*34683adeSsg70180 		 * Connect ports to new card.
2838*34683adeSsg70180 		 */
2839*34683adeSsg70180 		WRITE_ENTER(&plist->lockrw);
2840*34683adeSsg70180 		for (port = plist->head; port != NULL; port = port->p_next) {
2841*34683adeSsg70180 			if (vsw_set_hw(vswp, port)) {
2842*34683adeSsg70180 				RW_EXIT(&plist->lockrw);
2843*34683adeSsg70180 				goto fail_update;
2844*34683adeSsg70180 			}
2845*34683adeSsg70180 		}
2846*34683adeSsg70180 		RW_EXIT(&plist->lockrw);
2847*34683adeSsg70180 	}
2848*34683adeSsg70180 
2849*34683adeSsg70180 	if (updated & MD_macaddr) {
2850*34683adeSsg70180 		cmn_err(CE_NOTE, "!vsw%d: changing mac address to 0x%lx",
2851*34683adeSsg70180 				vswp->instance, macaddr);
2852*34683adeSsg70180 
2853*34683adeSsg70180 		WRITE_ENTER(&vswp->if_lockrw);
2854*34683adeSsg70180 		for (i = ETHERADDRL - 1; i >= 0; i--) {
2855*34683adeSsg70180 			vswp->if_addr.ether_addr_octet[i] = macaddr & 0xFF;
2856*34683adeSsg70180 			macaddr >>= 8;
2857*34683adeSsg70180 		}
2858*34683adeSsg70180 		RW_EXIT(&vswp->if_lockrw);
2859*34683adeSsg70180 
2860*34683adeSsg70180 		/*
2861*34683adeSsg70180 		 * Notify the MAC layer of the changed address.
2862*34683adeSsg70180 		 */
2863*34683adeSsg70180 		mac_unicst_update(vswp->if_mh, (uint8_t *)&vswp->if_addr);
2864*34683adeSsg70180 	}
2865*34683adeSsg70180 
2866*34683adeSsg70180 	return;
2867*34683adeSsg70180 
2868*34683adeSsg70180 fail_reconf:
2869*34683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: configuration unchanged", vswp->instance);
2870*34683adeSsg70180 	return;
2871*34683adeSsg70180 
2872*34683adeSsg70180 fail_update:
2873*34683adeSsg70180 	cmn_err(CE_WARN, "!vsw%d: update of configuration failed",
2874*34683adeSsg70180 			vswp->instance);
2875*34683adeSsg70180 }
2876*34683adeSsg70180 
2877*34683adeSsg70180 /*
28781ae08745Sheppo  * Add a new port to the system.
28791ae08745Sheppo  *
28801ae08745Sheppo  * Returns 0 on success, 1 on failure.
28811ae08745Sheppo  */
28821ae08745Sheppo int
28831ae08745Sheppo vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node)
28841ae08745Sheppo {
28851ae08745Sheppo 	uint64_t		ldc_id;
28861ae08745Sheppo 	uint8_t			*addrp;
28871ae08745Sheppo 	int			i, addrsz;
28881ae08745Sheppo 	int			num_nodes = 0, nchan = 0;
28891ae08745Sheppo 	int			listsz = 0;
28901ae08745Sheppo 	mde_cookie_t		*listp = NULL;
28911ae08745Sheppo 	struct ether_addr	ea;
28921ae08745Sheppo 	uint64_t		macaddr;
28931ae08745Sheppo 	uint64_t		inst = 0;
28941ae08745Sheppo 	vsw_port_t		*port;
28951ae08745Sheppo 
28961ae08745Sheppo 	if (md_get_prop_val(mdp, *node, id_propname, &inst)) {
28971ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found", __func__,
28981ae08745Sheppo 			id_propname);
28991ae08745Sheppo 		return (1);
29001ae08745Sheppo 	}
29011ae08745Sheppo 
29021ae08745Sheppo 	/*
29031ae08745Sheppo 	 * Find the channel endpoint node(s) (which should be under this
29041ae08745Sheppo 	 * port node) which contain the channel id(s).
29051ae08745Sheppo 	 */
29061ae08745Sheppo 	if ((num_nodes = md_node_count(mdp)) <= 0) {
29071ae08745Sheppo 		DERR(vswp, "%s: invalid number of nodes found (%d)",
29081ae08745Sheppo 			__func__, num_nodes);
29091ae08745Sheppo 		return (1);
29101ae08745Sheppo 	}
29111ae08745Sheppo 
2912*34683adeSsg70180 	D2(vswp, "%s: %d nodes found", __func__, num_nodes);
2913*34683adeSsg70180 
29141ae08745Sheppo 	/* allocate enough space for node list */
29151ae08745Sheppo 	listsz = num_nodes * sizeof (mde_cookie_t);
29161ae08745Sheppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
29171ae08745Sheppo 
29181ae08745Sheppo 	nchan = md_scan_dag(mdp, *node,
29191ae08745Sheppo 		md_find_name(mdp, chan_propname),
29201ae08745Sheppo 		md_find_name(mdp, "fwd"), listp);
29211ae08745Sheppo 
29221ae08745Sheppo 	if (nchan <= 0) {
29231ae08745Sheppo 		DWARN(vswp, "%s: no %s nodes found", __func__, chan_propname);
29241ae08745Sheppo 		kmem_free(listp, listsz);
29251ae08745Sheppo 		return (1);
29261ae08745Sheppo 	}
29271ae08745Sheppo 
29281ae08745Sheppo 	D2(vswp, "%s: %d %s nodes found", __func__, nchan, chan_propname);
29291ae08745Sheppo 
29301ae08745Sheppo 	/* use property from first node found */
29311ae08745Sheppo 	if (md_get_prop_val(mdp, listp[0], id_propname, &ldc_id)) {
29321ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found\n", __func__,
29331ae08745Sheppo 			id_propname);
29341ae08745Sheppo 		kmem_free(listp, listsz);
29351ae08745Sheppo 		return (1);
29361ae08745Sheppo 	}
29371ae08745Sheppo 
29381ae08745Sheppo 	/* don't need list any more */
29391ae08745Sheppo 	kmem_free(listp, listsz);
29401ae08745Sheppo 
29411ae08745Sheppo 	D2(vswp, "%s: ldc_id 0x%llx", __func__, ldc_id);
29421ae08745Sheppo 
29431ae08745Sheppo 	/* read mac-address property */
29441ae08745Sheppo 	if (md_get_prop_data(mdp, *node, remaddr_propname,
29451ae08745Sheppo 					&addrp, &addrsz)) {
29461ae08745Sheppo 		DWARN(vswp, "%s: prop(%s) not found",
29471ae08745Sheppo 				__func__, remaddr_propname);
29481ae08745Sheppo 		return (1);
29491ae08745Sheppo 	}
29501ae08745Sheppo 
29511ae08745Sheppo 	if (addrsz < ETHERADDRL) {
29521ae08745Sheppo 		DWARN(vswp, "%s: invalid address size", __func__);
29531ae08745Sheppo 		return (1);
29541ae08745Sheppo 	}
29551ae08745Sheppo 
29561ae08745Sheppo 	macaddr = *((uint64_t *)addrp);
29571ae08745Sheppo 	D2(vswp, "%s: remote mac address 0x%llx", __func__, macaddr);
29581ae08745Sheppo 
29591ae08745Sheppo 	for (i = ETHERADDRL - 1; i >= 0; i--) {
29601ae08745Sheppo 		ea.ether_addr_octet[i] = macaddr & 0xFF;
29611ae08745Sheppo 		macaddr >>= 8;
29621ae08745Sheppo 	}
29631ae08745Sheppo 
29641ae08745Sheppo 	if (vsw_port_attach(vswp, (int)inst, &ldc_id, 1, &ea) != 0) {
29651ae08745Sheppo 		DERR(vswp, "%s: failed to attach port", __func__);
29661ae08745Sheppo 		return (1);
29671ae08745Sheppo 	}
29681ae08745Sheppo 
29691ae08745Sheppo 	port = vsw_lookup_port(vswp, (int)inst);
29701ae08745Sheppo 
29711ae08745Sheppo 	/* just successfuly created the port, so it should exist */
29721ae08745Sheppo 	ASSERT(port != NULL);
29731ae08745Sheppo 
29741ae08745Sheppo 	return (0);
29751ae08745Sheppo }
29761ae08745Sheppo 
29771ae08745Sheppo /*
29781ae08745Sheppo  * Attach the specified port.
29791ae08745Sheppo  *
29801ae08745Sheppo  * Returns 0 on success, 1 on failure.
29811ae08745Sheppo  */
29821ae08745Sheppo static int
29831ae08745Sheppo vsw_port_attach(vsw_t *vswp, int p_instance, uint64_t *ldcids, int nids,
29841ae08745Sheppo struct ether_addr *macaddr)
29851ae08745Sheppo {
29861ae08745Sheppo 	vsw_port_list_t		*plist = &vswp->plist;
29871ae08745Sheppo 	vsw_port_t		*port, **prev_port;
29881ae08745Sheppo 	int			i;
29891ae08745Sheppo 
29901ae08745Sheppo 	D1(vswp, "%s: enter : port %d", __func__, p_instance);
29911ae08745Sheppo 
29921ae08745Sheppo 	/* port already exists? */
29931ae08745Sheppo 	READ_ENTER(&plist->lockrw);
29941ae08745Sheppo 	for (port = plist->head; port != NULL; port = port->p_next) {
29951ae08745Sheppo 		if (port->p_instance == p_instance) {
29961ae08745Sheppo 			DWARN(vswp, "%s: port instance %d already attached",
29971ae08745Sheppo 				__func__, p_instance);
29981ae08745Sheppo 			RW_EXIT(&plist->lockrw);
29991ae08745Sheppo 			return (1);
30001ae08745Sheppo 		}
30011ae08745Sheppo 	}
30021ae08745Sheppo 	RW_EXIT(&plist->lockrw);
30031ae08745Sheppo 
30041ae08745Sheppo 	port = kmem_zalloc(sizeof (vsw_port_t), KM_SLEEP);
30051ae08745Sheppo 	port->p_vswp = vswp;
30061ae08745Sheppo 	port->p_instance = p_instance;
30071ae08745Sheppo 	port->p_ldclist.num_ldcs = 0;
30081ae08745Sheppo 	port->p_ldclist.head = NULL;
3009e1ebb9ecSlm66018 	port->addr_set = VSW_ADDR_UNSET;
30101ae08745Sheppo 
30111ae08745Sheppo 	rw_init(&port->p_ldclist.lockrw, NULL, RW_DRIVER, NULL);
30121ae08745Sheppo 
30131ae08745Sheppo 	mutex_init(&port->tx_lock, NULL, MUTEX_DRIVER, NULL);
30141ae08745Sheppo 	mutex_init(&port->mca_lock, NULL, MUTEX_DRIVER, NULL);
30151ae08745Sheppo 
30161ae08745Sheppo 	mutex_init(&port->ref_lock, NULL, MUTEX_DRIVER, NULL);
30171ae08745Sheppo 	cv_init(&port->ref_cv, NULL, CV_DRIVER, NULL);
30181ae08745Sheppo 
30191ae08745Sheppo 	mutex_init(&port->state_lock, NULL, MUTEX_DRIVER, NULL);
30201ae08745Sheppo 	cv_init(&port->state_cv, NULL, CV_DRIVER, NULL);
30211ae08745Sheppo 	port->state = VSW_PORT_INIT;
30221ae08745Sheppo 
30231ae08745Sheppo 	if (nids > VSW_PORT_MAX_LDCS) {
30241ae08745Sheppo 		D2(vswp, "%s: using first of %d ldc ids",
30251ae08745Sheppo 			__func__, nids);
30261ae08745Sheppo 		nids = VSW_PORT_MAX_LDCS;
30271ae08745Sheppo 	}
30281ae08745Sheppo 
30291ae08745Sheppo 	D2(vswp, "%s: %d nids", __func__, nids);
30301ae08745Sheppo 	for (i = 0; i < nids; i++) {
30311ae08745Sheppo 		D2(vswp, "%s: ldcid (%llx)", __func__, (uint64_t)ldcids[i]);
30321ae08745Sheppo 		if (vsw_ldc_attach(port, (uint64_t)ldcids[i]) != 0) {
30331ae08745Sheppo 			DERR(vswp, "%s: ldc_attach failed", __func__);
30341ae08745Sheppo 
30351ae08745Sheppo 			rw_destroy(&port->p_ldclist.lockrw);
30361ae08745Sheppo 
30371ae08745Sheppo 			cv_destroy(&port->ref_cv);
30381ae08745Sheppo 			mutex_destroy(&port->ref_lock);
30391ae08745Sheppo 
30401ae08745Sheppo 			cv_destroy(&port->state_cv);
30411ae08745Sheppo 			mutex_destroy(&port->state_lock);
30421ae08745Sheppo 
30431ae08745Sheppo 			mutex_destroy(&port->tx_lock);
30441ae08745Sheppo 			mutex_destroy(&port->mca_lock);
30451ae08745Sheppo 			kmem_free(port, sizeof (vsw_port_t));
30461ae08745Sheppo 			return (1);
30471ae08745Sheppo 		}
30481ae08745Sheppo 	}
30491ae08745Sheppo 
30501ae08745Sheppo 	ether_copy(macaddr, &port->p_macaddr);
30511ae08745Sheppo 
30521ae08745Sheppo 	WRITE_ENTER(&plist->lockrw);
30531ae08745Sheppo 
30541ae08745Sheppo 	/* create the fdb entry for this port/mac address */
30551ae08745Sheppo 	(void) vsw_add_fdb(vswp, port);
30561ae08745Sheppo 
3057e1ebb9ecSlm66018 	(void) vsw_set_hw(vswp, port);
3058e1ebb9ecSlm66018 
30591ae08745Sheppo 	/* link it into the list of ports for this vsw instance */
30601ae08745Sheppo 	prev_port = (vsw_port_t **)(&plist->head);
30611ae08745Sheppo 	port->p_next = *prev_port;
30621ae08745Sheppo 	*prev_port = port;
30631ae08745Sheppo 	plist->num_ports++;
30641ae08745Sheppo 	RW_EXIT(&plist->lockrw);
30651ae08745Sheppo 
30661ae08745Sheppo 	/*
30671ae08745Sheppo 	 * Initialise the port and any ldc's under it.
30681ae08745Sheppo 	 */
30691ae08745Sheppo 	(void) vsw_init_ldcs(port);
30701ae08745Sheppo 
30711ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
30721ae08745Sheppo 	return (0);
30731ae08745Sheppo }
30741ae08745Sheppo 
30751ae08745Sheppo /*
30761ae08745Sheppo  * Detach the specified port.
30771ae08745Sheppo  *
30781ae08745Sheppo  * Returns 0 on success, 1 on failure.
30791ae08745Sheppo  */
30801ae08745Sheppo static int
30811ae08745Sheppo vsw_port_detach(vsw_t *vswp, int p_instance)
30821ae08745Sheppo {
30831ae08745Sheppo 	vsw_port_t	*port = NULL;
30841ae08745Sheppo 	vsw_port_list_t	*plist = &vswp->plist;
30851ae08745Sheppo 
30861ae08745Sheppo 	D1(vswp, "%s: enter: port id %d", __func__, p_instance);
30871ae08745Sheppo 
30881ae08745Sheppo 	WRITE_ENTER(&plist->lockrw);
30891ae08745Sheppo 
30901ae08745Sheppo 	if ((port = vsw_lookup_port(vswp, p_instance)) == NULL) {
30911ae08745Sheppo 		RW_EXIT(&plist->lockrw);
30921ae08745Sheppo 		return (1);
30931ae08745Sheppo 	}
30941ae08745Sheppo 
30951ae08745Sheppo 	if (vsw_plist_del_node(vswp, port)) {
30961ae08745Sheppo 		RW_EXIT(&plist->lockrw);
30971ae08745Sheppo 		return (1);
30981ae08745Sheppo 	}
30991ae08745Sheppo 
3100e1ebb9ecSlm66018 	/* Remove address if was programmed into HW. */
3101e1ebb9ecSlm66018 	(void) vsw_unset_hw(vswp, port);
3102e1ebb9ecSlm66018 
31031ae08745Sheppo 	/* Remove the fdb entry for this port/mac address */
31041ae08745Sheppo 	(void) vsw_del_fdb(vswp, port);
31051ae08745Sheppo 
31061ae08745Sheppo 	/* Remove any multicast addresses.. */
31071ae08745Sheppo 	vsw_del_mcst_port(port);
31081ae08745Sheppo 
31091ae08745Sheppo 	/*
3110e1ebb9ecSlm66018 	 * No longer need to hold writer lock on port list now
3111e1ebb9ecSlm66018 	 * that we have unlinked the target port from the list.
31121ae08745Sheppo 	 */
31131ae08745Sheppo 	RW_EXIT(&plist->lockrw);
31141ae08745Sheppo 
3115e1ebb9ecSlm66018 	READ_ENTER(&plist->lockrw);
3116e1ebb9ecSlm66018 
3117e1ebb9ecSlm66018 	if (vswp->recfg_reqd)
3118e1ebb9ecSlm66018 		(void) vsw_reconfig_hw(vswp);
3119e1ebb9ecSlm66018 
3120e1ebb9ecSlm66018 	RW_EXIT(&plist->lockrw);
3121e1ebb9ecSlm66018 
31221ae08745Sheppo 	if (vsw_port_delete(port)) {
31231ae08745Sheppo 		return (1);
31241ae08745Sheppo 	}
31251ae08745Sheppo 
31261ae08745Sheppo 	D1(vswp, "%s: exit: p_instance(%d)", __func__, p_instance);
31271ae08745Sheppo 	return (0);
31281ae08745Sheppo }
31291ae08745Sheppo 
31301ae08745Sheppo /*
31311ae08745Sheppo  * Detach all active ports.
31321ae08745Sheppo  *
31331ae08745Sheppo  * Returns 0 on success, 1 on failure.
31341ae08745Sheppo  */
31351ae08745Sheppo static int
31361ae08745Sheppo vsw_detach_ports(vsw_t *vswp)
31371ae08745Sheppo {
31381ae08745Sheppo 	vsw_port_list_t 	*plist = &vswp->plist;
31391ae08745Sheppo 	vsw_port_t		*port = NULL;
31401ae08745Sheppo 
31411ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
31421ae08745Sheppo 
31431ae08745Sheppo 	WRITE_ENTER(&plist->lockrw);
31441ae08745Sheppo 
31451ae08745Sheppo 	while ((port = plist->head) != NULL) {
31461ae08745Sheppo 		if (vsw_plist_del_node(vswp, port)) {
31471ae08745Sheppo 			DERR(vswp, "%s: Error deleting port %d"
31481ae08745Sheppo 				" from port list", __func__,
31491ae08745Sheppo 				port->p_instance);
31501ae08745Sheppo 			RW_EXIT(&plist->lockrw);
31511ae08745Sheppo 			return (1);
31521ae08745Sheppo 		}
31531ae08745Sheppo 
3154e1ebb9ecSlm66018 		/* Remove address if was programmed into HW. */
3155e1ebb9ecSlm66018 		(void) vsw_unset_hw(vswp, port);
3156e1ebb9ecSlm66018 
31571ae08745Sheppo 		/* Remove the fdb entry for this port/mac address */
31581ae08745Sheppo 		(void) vsw_del_fdb(vswp, port);
31591ae08745Sheppo 
31601ae08745Sheppo 		/* Remove any multicast addresses.. */
31611ae08745Sheppo 		vsw_del_mcst_port(port);
31621ae08745Sheppo 
31631ae08745Sheppo 		/*
31641ae08745Sheppo 		 * No longer need to hold the lock on the port list
31651ae08745Sheppo 		 * now that we have unlinked the target port from the
31661ae08745Sheppo 		 * list.
31671ae08745Sheppo 		 */
31681ae08745Sheppo 		RW_EXIT(&plist->lockrw);
31691ae08745Sheppo 		if (vsw_port_delete(port)) {
31701ae08745Sheppo 			DERR(vswp, "%s: Error deleting port %d",
31711ae08745Sheppo 				__func__, port->p_instance);
31721ae08745Sheppo 			return (1);
31731ae08745Sheppo 		}
31741ae08745Sheppo 		WRITE_ENTER(&plist->lockrw);
31751ae08745Sheppo 	}
31761ae08745Sheppo 	RW_EXIT(&plist->lockrw);
31771ae08745Sheppo 
31781ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
31791ae08745Sheppo 
31801ae08745Sheppo 	return (0);
31811ae08745Sheppo }
31821ae08745Sheppo 
31831ae08745Sheppo /*
31841ae08745Sheppo  * Delete the specified port.
31851ae08745Sheppo  *
31861ae08745Sheppo  * Returns 0 on success, 1 on failure.
31871ae08745Sheppo  */
31881ae08745Sheppo static int
31891ae08745Sheppo vsw_port_delete(vsw_port_t *port)
31901ae08745Sheppo {
31911ae08745Sheppo 	vsw_ldc_list_t 		*ldcl;
31921ae08745Sheppo 	vsw_t			*vswp = port->p_vswp;
31931ae08745Sheppo 
31941ae08745Sheppo 	D1(vswp, "%s: enter : port id %d", __func__, port->p_instance);
31951ae08745Sheppo 
31961ae08745Sheppo 	(void) vsw_uninit_ldcs(port);
31971ae08745Sheppo 
31981ae08745Sheppo 	/*
31991ae08745Sheppo 	 * Wait for any pending ctrl msg tasks which reference this
32001ae08745Sheppo 	 * port to finish.
32011ae08745Sheppo 	 */
32021ae08745Sheppo 	if (vsw_drain_port_taskq(port))
32031ae08745Sheppo 		return (1);
32041ae08745Sheppo 
32051ae08745Sheppo 	/*
32061ae08745Sheppo 	 * Wait for port reference count to hit zero.
32071ae08745Sheppo 	 */
32081ae08745Sheppo 	mutex_enter(&port->ref_lock);
32091ae08745Sheppo 	while (port->ref_cnt != 0)
32101ae08745Sheppo 		cv_wait(&port->ref_cv, &port->ref_lock);
32111ae08745Sheppo 	mutex_exit(&port->ref_lock);
32121ae08745Sheppo 
32131ae08745Sheppo 	/*
32141ae08745Sheppo 	 * Wait for any active callbacks to finish
32151ae08745Sheppo 	 */
32161ae08745Sheppo 	if (vsw_drain_ldcs(port))
32171ae08745Sheppo 		return (1);
32181ae08745Sheppo 
32191ae08745Sheppo 	ldcl = &port->p_ldclist;
32201ae08745Sheppo 	WRITE_ENTER(&ldcl->lockrw);
32211ae08745Sheppo 	while (ldcl->num_ldcs > 0) {
32221ae08745Sheppo 		if (vsw_ldc_detach(port, ldcl->head->ldc_id) != 0) {;
3223*34683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: unable to detach ldc %ld",
3224*34683adeSsg70180 					vswp->instance, ldcl->head->ldc_id);
32251ae08745Sheppo 			RW_EXIT(&ldcl->lockrw);
32261ae08745Sheppo 			return (1);
32271ae08745Sheppo 		}
32281ae08745Sheppo 	}
32291ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
32301ae08745Sheppo 
32311ae08745Sheppo 	rw_destroy(&port->p_ldclist.lockrw);
32321ae08745Sheppo 
32331ae08745Sheppo 	mutex_destroy(&port->mca_lock);
32341ae08745Sheppo 	mutex_destroy(&port->tx_lock);
32351ae08745Sheppo 	cv_destroy(&port->ref_cv);
32361ae08745Sheppo 	mutex_destroy(&port->ref_lock);
32371ae08745Sheppo 
32381ae08745Sheppo 	cv_destroy(&port->state_cv);
32391ae08745Sheppo 	mutex_destroy(&port->state_lock);
32401ae08745Sheppo 
32411ae08745Sheppo 	kmem_free(port, sizeof (vsw_port_t));
32421ae08745Sheppo 
32431ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
32441ae08745Sheppo 
32451ae08745Sheppo 	return (0);
32461ae08745Sheppo }
32471ae08745Sheppo 
32481ae08745Sheppo /*
32491ae08745Sheppo  * Attach a logical domain channel (ldc) under a specified port.
32501ae08745Sheppo  *
32511ae08745Sheppo  * Returns 0 on success, 1 on failure.
32521ae08745Sheppo  */
32531ae08745Sheppo static int
32541ae08745Sheppo vsw_ldc_attach(vsw_port_t *port, uint64_t ldc_id)
32551ae08745Sheppo {
32561ae08745Sheppo 	vsw_t 		*vswp = port->p_vswp;
32571ae08745Sheppo 	vsw_ldc_list_t *ldcl = &port->p_ldclist;
32581ae08745Sheppo 	vsw_ldc_t 	*ldcp = NULL;
32591ae08745Sheppo 	ldc_attr_t 	attr;
32601ae08745Sheppo 	ldc_status_t	istatus;
32611ae08745Sheppo 	int 		status = DDI_FAILURE;
3262d10e4ef2Snarayan 	int		rv;
32633af08d82Slm66018 	enum		{ PROG_init = 0x0, PROG_mblks = 0x1,
32643af08d82Slm66018 				PROG_callback = 0x2}
32653af08d82Slm66018 			progress;
32663af08d82Slm66018 
32673af08d82Slm66018 	progress = PROG_init;
32681ae08745Sheppo 
32691ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
32701ae08745Sheppo 
32711ae08745Sheppo 	ldcp = kmem_zalloc(sizeof (vsw_ldc_t), KM_NOSLEEP);
32721ae08745Sheppo 	if (ldcp == NULL) {
32731ae08745Sheppo 		DERR(vswp, "%s: kmem_zalloc failed", __func__);
32741ae08745Sheppo 		return (1);
32751ae08745Sheppo 	}
32761ae08745Sheppo 	ldcp->ldc_id = ldc_id;
32771ae08745Sheppo 
3278d10e4ef2Snarayan 	/* allocate pool of receive mblks */
3279d10e4ef2Snarayan 	rv = vio_create_mblks(vsw_num_mblks, vsw_mblk_size, &(ldcp->rxh));
3280d10e4ef2Snarayan 	if (rv) {
3281d10e4ef2Snarayan 		DWARN(vswp, "%s: unable to create free mblk pool for"
3282d10e4ef2Snarayan 			" channel %ld (rv %d)", __func__, ldc_id, rv);
3283d10e4ef2Snarayan 		kmem_free(ldcp, sizeof (vsw_ldc_t));
3284d10e4ef2Snarayan 		return (1);
3285d10e4ef2Snarayan 	}
3286d10e4ef2Snarayan 
32873af08d82Slm66018 	progress |= PROG_mblks;
32883af08d82Slm66018 
32891ae08745Sheppo 	mutex_init(&ldcp->ldc_txlock, NULL, MUTEX_DRIVER, NULL);
32901ae08745Sheppo 	mutex_init(&ldcp->ldc_cblock, NULL, MUTEX_DRIVER, NULL);
32911ae08745Sheppo 	mutex_init(&ldcp->drain_cv_lock, NULL, MUTEX_DRIVER, NULL);
32921ae08745Sheppo 	cv_init(&ldcp->drain_cv, NULL, CV_DRIVER, NULL);
32931ae08745Sheppo 
32941ae08745Sheppo 	/* required for handshake with peer */
32951ae08745Sheppo 	ldcp->local_session = (uint64_t)ddi_get_lbolt();
32961ae08745Sheppo 	ldcp->peer_session = 0;
32971ae08745Sheppo 	ldcp->session_status = 0;
32981ae08745Sheppo 
32991ae08745Sheppo 	mutex_init(&ldcp->hss_lock, NULL, MUTEX_DRIVER, NULL);
33001ae08745Sheppo 	ldcp->hss_id = 1;	/* Initial handshake session id */
33011ae08745Sheppo 
33021ae08745Sheppo 	/* only set for outbound lane, inbound set by peer */
3303d10e4ef2Snarayan 	mutex_init(&ldcp->lane_in.seq_lock, NULL, MUTEX_DRIVER, NULL);
3304d10e4ef2Snarayan 	mutex_init(&ldcp->lane_out.seq_lock, NULL, MUTEX_DRIVER, NULL);
33051ae08745Sheppo 	vsw_set_lane_attr(vswp, &ldcp->lane_out);
33061ae08745Sheppo 
33071ae08745Sheppo 	attr.devclass = LDC_DEV_NT_SVC;
33081ae08745Sheppo 	attr.instance = ddi_get_instance(vswp->dip);
33091ae08745Sheppo 	attr.mode = LDC_MODE_UNRELIABLE;
3310e1ebb9ecSlm66018 	attr.mtu = VSW_LDC_MTU;
33111ae08745Sheppo 	status = ldc_init(ldc_id, &attr, &ldcp->ldc_handle);
33121ae08745Sheppo 	if (status != 0) {
33131ae08745Sheppo 		DERR(vswp, "%s(%lld): ldc_init failed, rv (%d)",
33141ae08745Sheppo 		    __func__, ldc_id, status);
3315d10e4ef2Snarayan 		goto ldc_attach_fail;
33161ae08745Sheppo 	}
33171ae08745Sheppo 
33181ae08745Sheppo 	status = ldc_reg_callback(ldcp->ldc_handle, vsw_ldc_cb, (caddr_t)ldcp);
33191ae08745Sheppo 	if (status != 0) {
33201ae08745Sheppo 		DERR(vswp, "%s(%lld): ldc_reg_callback failed, rv (%d)",
33211ae08745Sheppo 		    __func__, ldc_id, status);
33221ae08745Sheppo 		(void) ldc_fini(ldcp->ldc_handle);
3323d10e4ef2Snarayan 		goto ldc_attach_fail;
33241ae08745Sheppo 	}
33251ae08745Sheppo 
33263af08d82Slm66018 	progress |= PROG_callback;
33273af08d82Slm66018 
33283af08d82Slm66018 	mutex_init(&ldcp->status_lock, NULL, MUTEX_DRIVER, NULL);
33291ae08745Sheppo 
33301ae08745Sheppo 	if (ldc_status(ldcp->ldc_handle, &istatus) != 0) {
33311ae08745Sheppo 		DERR(vswp, "%s: ldc_status failed", __func__);
33323af08d82Slm66018 		mutex_destroy(&ldcp->status_lock);
33333af08d82Slm66018 		goto ldc_attach_fail;
33341ae08745Sheppo 	}
33351ae08745Sheppo 
33361ae08745Sheppo 	ldcp->ldc_status = istatus;
33371ae08745Sheppo 	ldcp->ldc_port = port;
33381ae08745Sheppo 	ldcp->ldc_vswp = vswp;
33391ae08745Sheppo 
33401ae08745Sheppo 	/* link it into the list of channels for this port */
33411ae08745Sheppo 	WRITE_ENTER(&ldcl->lockrw);
33421ae08745Sheppo 	ldcp->ldc_next = ldcl->head;
33431ae08745Sheppo 	ldcl->head = ldcp;
33441ae08745Sheppo 	ldcl->num_ldcs++;
33451ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
33461ae08745Sheppo 
33471ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
33481ae08745Sheppo 	return (0);
3349d10e4ef2Snarayan 
3350d10e4ef2Snarayan ldc_attach_fail:
3351d10e4ef2Snarayan 	mutex_destroy(&ldcp->ldc_txlock);
3352d10e4ef2Snarayan 	mutex_destroy(&ldcp->ldc_cblock);
3353d10e4ef2Snarayan 
3354d10e4ef2Snarayan 	cv_destroy(&ldcp->drain_cv);
3355d10e4ef2Snarayan 
33563af08d82Slm66018 	if (progress & PROG_callback) {
33573af08d82Slm66018 		(void) ldc_unreg_callback(ldcp->ldc_handle);
33583af08d82Slm66018 	}
33593af08d82Slm66018 
33603af08d82Slm66018 	if ((progress & PROG_mblks) && (ldcp->rxh != NULL)) {
3361d10e4ef2Snarayan 		if (vio_destroy_mblks(ldcp->rxh) != 0) {
3362d10e4ef2Snarayan 			/*
3363d10e4ef2Snarayan 			 * Something odd has happened, as the destroy
3364d10e4ef2Snarayan 			 * will only fail if some mblks have been allocated
3365d10e4ef2Snarayan 			 * from the pool already (which shouldn't happen)
3366d10e4ef2Snarayan 			 * and have not been returned.
3367d10e4ef2Snarayan 			 *
3368d10e4ef2Snarayan 			 * Add the pool pointer to a list maintained in
3369d10e4ef2Snarayan 			 * the device instance. Another attempt will be made
3370d10e4ef2Snarayan 			 * to free the pool when the device itself detaches.
3371d10e4ef2Snarayan 			 */
3372*34683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: Creation of ldc channel %ld "
3373*34683adeSsg70180 				"failed and cannot destroy associated mblk "
3374*34683adeSsg70180 				"pool", vswp->instance, ldc_id);
3375d10e4ef2Snarayan 			ldcp->rxh->nextp =  vswp->rxh;
3376d10e4ef2Snarayan 			vswp->rxh = ldcp->rxh;
3377d10e4ef2Snarayan 		}
3378d10e4ef2Snarayan 	}
3379d10e4ef2Snarayan 	mutex_destroy(&ldcp->drain_cv_lock);
3380d10e4ef2Snarayan 	mutex_destroy(&ldcp->hss_lock);
3381d10e4ef2Snarayan 
3382d10e4ef2Snarayan 	mutex_destroy(&ldcp->lane_in.seq_lock);
3383d10e4ef2Snarayan 	mutex_destroy(&ldcp->lane_out.seq_lock);
3384d10e4ef2Snarayan 	kmem_free(ldcp, sizeof (vsw_ldc_t));
3385d10e4ef2Snarayan 
3386d10e4ef2Snarayan 	return (1);
33871ae08745Sheppo }
33881ae08745Sheppo 
33891ae08745Sheppo /*
33901ae08745Sheppo  * Detach a logical domain channel (ldc) belonging to a
33911ae08745Sheppo  * particular port.
33921ae08745Sheppo  *
33931ae08745Sheppo  * Returns 0 on success, 1 on failure.
33941ae08745Sheppo  */
33951ae08745Sheppo static int
33961ae08745Sheppo vsw_ldc_detach(vsw_port_t *port, uint64_t ldc_id)
33971ae08745Sheppo {
33981ae08745Sheppo 	vsw_t 		*vswp = port->p_vswp;
33991ae08745Sheppo 	vsw_ldc_t 	*ldcp, *prev_ldcp;
34001ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
34011ae08745Sheppo 	int 		rv;
34021ae08745Sheppo 
34031ae08745Sheppo 	prev_ldcp = ldcl->head;
34041ae08745Sheppo 	for (; (ldcp = prev_ldcp) != NULL; prev_ldcp = ldcp->ldc_next) {
34051ae08745Sheppo 		if (ldcp->ldc_id == ldc_id) {
34061ae08745Sheppo 			break;
34071ae08745Sheppo 		}
34081ae08745Sheppo 	}
34091ae08745Sheppo 
34101ae08745Sheppo 	/* specified ldc id not found */
34111ae08745Sheppo 	if (ldcp == NULL) {
34121ae08745Sheppo 		DERR(vswp, "%s: ldcp = NULL", __func__);
34131ae08745Sheppo 		return (1);
34141ae08745Sheppo 	}
34151ae08745Sheppo 
34161ae08745Sheppo 	D2(vswp, "%s: detaching channel %lld", __func__, ldcp->ldc_id);
34171ae08745Sheppo 
34181ae08745Sheppo 	/*
34191ae08745Sheppo 	 * Before we can close the channel we must release any mapped
34201ae08745Sheppo 	 * resources (e.g. drings).
34211ae08745Sheppo 	 */
34221ae08745Sheppo 	vsw_free_lane_resources(ldcp, INBOUND);
34231ae08745Sheppo 	vsw_free_lane_resources(ldcp, OUTBOUND);
34241ae08745Sheppo 
34251ae08745Sheppo 	/*
34261ae08745Sheppo 	 * If the close fails we are in serious trouble, as won't
34271ae08745Sheppo 	 * be able to delete the parent port.
34281ae08745Sheppo 	 */
34291ae08745Sheppo 	if ((rv = ldc_close(ldcp->ldc_handle)) != 0) {
34301ae08745Sheppo 		DERR(vswp, "%s: error %d closing channel %lld",
34311ae08745Sheppo 			__func__, rv, ldcp->ldc_id);
34321ae08745Sheppo 		return (1);
34331ae08745Sheppo 	}
34341ae08745Sheppo 
34351ae08745Sheppo 	(void) ldc_fini(ldcp->ldc_handle);
34361ae08745Sheppo 
34371ae08745Sheppo 	ldcp->ldc_status = LDC_INIT;
34381ae08745Sheppo 	ldcp->ldc_handle = NULL;
34391ae08745Sheppo 	ldcp->ldc_vswp = NULL;
3440d10e4ef2Snarayan 
3441d10e4ef2Snarayan 	if (ldcp->rxh != NULL) {
3442d10e4ef2Snarayan 		if (vio_destroy_mblks(ldcp->rxh)) {
3443d10e4ef2Snarayan 			/*
3444d10e4ef2Snarayan 			 * Mostly likely some mblks are still in use and
3445d10e4ef2Snarayan 			 * have not been returned to the pool. Add the pool
3446d10e4ef2Snarayan 			 * to the list maintained in the device instance.
3447d10e4ef2Snarayan 			 * Another attempt will be made to destroy the pool
3448d10e4ef2Snarayan 			 * when the device detaches.
3449d10e4ef2Snarayan 			 */
3450d10e4ef2Snarayan 			ldcp->rxh->nextp =  vswp->rxh;
3451d10e4ef2Snarayan 			vswp->rxh = ldcp->rxh;
3452d10e4ef2Snarayan 		}
3453d10e4ef2Snarayan 	}
3454d10e4ef2Snarayan 
34553af08d82Slm66018 	/* unlink it from the list */
34563af08d82Slm66018 	prev_ldcp = ldcp->ldc_next;
34573af08d82Slm66018 	ldcl->num_ldcs--;
34583af08d82Slm66018 
34591ae08745Sheppo 	mutex_destroy(&ldcp->ldc_txlock);
34601ae08745Sheppo 	mutex_destroy(&ldcp->ldc_cblock);
34611ae08745Sheppo 	cv_destroy(&ldcp->drain_cv);
34621ae08745Sheppo 	mutex_destroy(&ldcp->drain_cv_lock);
34631ae08745Sheppo 	mutex_destroy(&ldcp->hss_lock);
3464d10e4ef2Snarayan 	mutex_destroy(&ldcp->lane_in.seq_lock);
3465d10e4ef2Snarayan 	mutex_destroy(&ldcp->lane_out.seq_lock);
34663af08d82Slm66018 	mutex_destroy(&ldcp->status_lock);
34671ae08745Sheppo 
34681ae08745Sheppo 	kmem_free(ldcp, sizeof (vsw_ldc_t));
34691ae08745Sheppo 
34701ae08745Sheppo 	return (0);
34711ae08745Sheppo }
34721ae08745Sheppo 
34731ae08745Sheppo /*
34741ae08745Sheppo  * Open and attempt to bring up the channel. Note that channel
34751ae08745Sheppo  * can only be brought up if peer has also opened channel.
34761ae08745Sheppo  *
34771ae08745Sheppo  * Returns 0 if can open and bring up channel, otherwise
34781ae08745Sheppo  * returns 1.
34791ae08745Sheppo  */
34801ae08745Sheppo static int
34811ae08745Sheppo vsw_ldc_init(vsw_ldc_t *ldcp)
34821ae08745Sheppo {
34831ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
34841ae08745Sheppo 	ldc_status_t	istatus = 0;
34851ae08745Sheppo 	int		rv;
34861ae08745Sheppo 
34871ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
34881ae08745Sheppo 
34891ae08745Sheppo 	LDC_ENTER_LOCK(ldcp);
34901ae08745Sheppo 
34911ae08745Sheppo 	/* don't start at 0 in case clients don't like that */
34921ae08745Sheppo 	ldcp->next_ident = 1;
34931ae08745Sheppo 
34941ae08745Sheppo 	rv = ldc_open(ldcp->ldc_handle);
34951ae08745Sheppo 	if (rv != 0) {
34961ae08745Sheppo 		DERR(vswp, "%s: ldc_open failed: id(%lld) rv(%d)",
34971ae08745Sheppo 		    __func__, ldcp->ldc_id, rv);
34981ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
34991ae08745Sheppo 		return (1);
35001ae08745Sheppo 	}
35011ae08745Sheppo 
35021ae08745Sheppo 	if (ldc_status(ldcp->ldc_handle, &istatus) != 0) {
35031ae08745Sheppo 		DERR(vswp, "%s: unable to get status", __func__);
35041ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35051ae08745Sheppo 		return (1);
35061ae08745Sheppo 
35071ae08745Sheppo 	} else if (istatus != LDC_OPEN && istatus != LDC_READY) {
35081ae08745Sheppo 		DERR(vswp, "%s: id (%lld) status(%d) is not OPEN/READY",
35091ae08745Sheppo 		    __func__, ldcp->ldc_id, istatus);
35101ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35111ae08745Sheppo 		return (1);
35121ae08745Sheppo 	}
35131ae08745Sheppo 
35143af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
35151ae08745Sheppo 	ldcp->ldc_status = istatus;
35163af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
35173af08d82Slm66018 
35181ae08745Sheppo 	rv = ldc_up(ldcp->ldc_handle);
35191ae08745Sheppo 	if (rv != 0) {
35201ae08745Sheppo 		/*
35211ae08745Sheppo 		 * Not a fatal error for ldc_up() to fail, as peer
35221ae08745Sheppo 		 * end point may simply not be ready yet.
35231ae08745Sheppo 		 */
35241ae08745Sheppo 		D2(vswp, "%s: ldc_up err id(%lld) rv(%d)", __func__,
35251ae08745Sheppo 			ldcp->ldc_id, rv);
35261ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35271ae08745Sheppo 		return (1);
35281ae08745Sheppo 	}
35291ae08745Sheppo 
35301ae08745Sheppo 	/*
35311ae08745Sheppo 	 * ldc_up() call is non-blocking so need to explicitly
35321ae08745Sheppo 	 * check channel status to see if in fact the channel
35331ae08745Sheppo 	 * is UP.
35341ae08745Sheppo 	 */
35353af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
35363af08d82Slm66018 	istatus = ldcp->ldc_status;
35373af08d82Slm66018 	if (ldc_status(ldcp->ldc_handle, &ldcp->ldc_status) != 0) {
35381ae08745Sheppo 		DERR(vswp, "%s: unable to get status", __func__);
35393af08d82Slm66018 		mutex_exit(&ldcp->status_lock);
35401ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35411ae08745Sheppo 		return (1);
35421ae08745Sheppo 
35431ae08745Sheppo 	}
35443af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
35451ae08745Sheppo 	LDC_EXIT_LOCK(ldcp);
35461ae08745Sheppo 
35473af08d82Slm66018 	if ((istatus != LDC_UP) && (ldcp->ldc_status == LDC_UP)) {
35483af08d82Slm66018 		D2(vswp, "%s: channel %ld now UP (%ld)", __func__,
35493af08d82Slm66018 			ldcp->ldc_id, istatus);
35503af08d82Slm66018 		vsw_restart_handshake(ldcp);
35513af08d82Slm66018 	}
35523af08d82Slm66018 
35531ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
35541ae08745Sheppo 	return (0);
35551ae08745Sheppo }
35561ae08745Sheppo 
35571ae08745Sheppo /* disable callbacks on the channel */
35581ae08745Sheppo static int
35591ae08745Sheppo vsw_ldc_uninit(vsw_ldc_t *ldcp)
35601ae08745Sheppo {
35611ae08745Sheppo 	vsw_t	*vswp = ldcp->ldc_vswp;
35621ae08745Sheppo 	int	rv;
35631ae08745Sheppo 
35641ae08745Sheppo 	D1(vswp, "vsw_ldc_uninit: enter: id(%lx)\n", ldcp->ldc_id);
35651ae08745Sheppo 
35661ae08745Sheppo 	LDC_ENTER_LOCK(ldcp);
35671ae08745Sheppo 
35681ae08745Sheppo 	rv = ldc_set_cb_mode(ldcp->ldc_handle, LDC_CB_DISABLE);
35691ae08745Sheppo 	if (rv != 0) {
35701ae08745Sheppo 		DERR(vswp, "vsw_ldc_uninit(%lld): error disabling "
35711ae08745Sheppo 			"interrupts (rv = %d)\n", ldcp->ldc_id, rv);
35721ae08745Sheppo 		LDC_EXIT_LOCK(ldcp);
35731ae08745Sheppo 		return (1);
35741ae08745Sheppo 	}
35751ae08745Sheppo 
35763af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
35771ae08745Sheppo 	ldcp->ldc_status = LDC_INIT;
35783af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
35791ae08745Sheppo 
35801ae08745Sheppo 	LDC_EXIT_LOCK(ldcp);
35811ae08745Sheppo 
35821ae08745Sheppo 	D1(vswp, "vsw_ldc_uninit: exit: id(%lx)", ldcp->ldc_id);
35831ae08745Sheppo 
35841ae08745Sheppo 	return (0);
35851ae08745Sheppo }
35861ae08745Sheppo 
35871ae08745Sheppo static int
35881ae08745Sheppo vsw_init_ldcs(vsw_port_t *port)
35891ae08745Sheppo {
35901ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
35911ae08745Sheppo 	vsw_ldc_t	*ldcp;
35921ae08745Sheppo 
35931ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
35941ae08745Sheppo 	ldcp =  ldcl->head;
35951ae08745Sheppo 	for (; ldcp  != NULL; ldcp = ldcp->ldc_next) {
35961ae08745Sheppo 		(void) vsw_ldc_init(ldcp);
35971ae08745Sheppo 	}
35981ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
35991ae08745Sheppo 
36001ae08745Sheppo 	return (0);
36011ae08745Sheppo }
36021ae08745Sheppo 
36031ae08745Sheppo static int
36041ae08745Sheppo vsw_uninit_ldcs(vsw_port_t *port)
36051ae08745Sheppo {
36061ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
36071ae08745Sheppo 	vsw_ldc_t	*ldcp;
36081ae08745Sheppo 
36091ae08745Sheppo 	D1(NULL, "vsw_uninit_ldcs: enter\n");
36101ae08745Sheppo 
36111ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
36121ae08745Sheppo 	ldcp =  ldcl->head;
36131ae08745Sheppo 	for (; ldcp  != NULL; ldcp = ldcp->ldc_next) {
36141ae08745Sheppo 		(void) vsw_ldc_uninit(ldcp);
36151ae08745Sheppo 	}
36161ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
36171ae08745Sheppo 
36181ae08745Sheppo 	D1(NULL, "vsw_uninit_ldcs: exit\n");
36191ae08745Sheppo 
36201ae08745Sheppo 	return (0);
36211ae08745Sheppo }
36221ae08745Sheppo 
36231ae08745Sheppo /*
36241ae08745Sheppo  * Wait until the callback(s) associated with the ldcs under the specified
36251ae08745Sheppo  * port have completed.
36261ae08745Sheppo  *
36271ae08745Sheppo  * Prior to this function being invoked each channel under this port
36281ae08745Sheppo  * should have been quiesced via ldc_set_cb_mode(DISABLE).
36291ae08745Sheppo  *
36301ae08745Sheppo  * A short explaination of what we are doing below..
36311ae08745Sheppo  *
36321ae08745Sheppo  * The simplest approach would be to have a reference counter in
36331ae08745Sheppo  * the ldc structure which is increment/decremented by the callbacks as
36341ae08745Sheppo  * they use the channel. The drain function could then simply disable any
36351ae08745Sheppo  * further callbacks and do a cv_wait for the ref to hit zero. Unfortunately
36361ae08745Sheppo  * there is a tiny window here - before the callback is able to get the lock
36371ae08745Sheppo  * on the channel it is interrupted and this function gets to execute. It
36381ae08745Sheppo  * sees that the ref count is zero and believes its free to delete the
36391ae08745Sheppo  * associated data structures.
36401ae08745Sheppo  *
36411ae08745Sheppo  * We get around this by taking advantage of the fact that before the ldc
36421ae08745Sheppo  * framework invokes a callback it sets a flag to indicate that there is a
36431ae08745Sheppo  * callback active (or about to become active). If when we attempt to
36441ae08745Sheppo  * unregister a callback when this active flag is set then the unregister
36451ae08745Sheppo  * will fail with EWOULDBLOCK.
36461ae08745Sheppo  *
36471ae08745Sheppo  * If the unregister fails we do a cv_timedwait. We will either be signaled
36481ae08745Sheppo  * by the callback as it is exiting (note we have to wait a short period to
36491ae08745Sheppo  * allow the callback to return fully to the ldc framework and it to clear
36501ae08745Sheppo  * the active flag), or by the timer expiring. In either case we again attempt
36511ae08745Sheppo  * the unregister. We repeat this until we can succesfully unregister the
36521ae08745Sheppo  * callback.
36531ae08745Sheppo  *
36541ae08745Sheppo  * The reason we use a cv_timedwait rather than a simple cv_wait is to catch
36551ae08745Sheppo  * the case where the callback has finished but the ldc framework has not yet
36561ae08745Sheppo  * cleared the active flag. In this case we would never get a cv_signal.
36571ae08745Sheppo  */
36581ae08745Sheppo static int
36591ae08745Sheppo vsw_drain_ldcs(vsw_port_t *port)
36601ae08745Sheppo {
36611ae08745Sheppo 	vsw_ldc_list_t	*ldcl = &port->p_ldclist;
36621ae08745Sheppo 	vsw_ldc_t	*ldcp;
36631ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
36641ae08745Sheppo 
36651ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
36661ae08745Sheppo 
36671ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
36681ae08745Sheppo 
36691ae08745Sheppo 	ldcp = ldcl->head;
36701ae08745Sheppo 
36711ae08745Sheppo 	for (; ldcp  != NULL; ldcp = ldcp->ldc_next) {
36721ae08745Sheppo 		/*
36731ae08745Sheppo 		 * If we can unregister the channel callback then we
36741ae08745Sheppo 		 * know that there is no callback either running or
36751ae08745Sheppo 		 * scheduled to run for this channel so move on to next
36761ae08745Sheppo 		 * channel in the list.
36771ae08745Sheppo 		 */
36781ae08745Sheppo 		mutex_enter(&ldcp->drain_cv_lock);
36791ae08745Sheppo 
36801ae08745Sheppo 		/* prompt active callbacks to quit */
36811ae08745Sheppo 		ldcp->drain_state = VSW_LDC_DRAINING;
36821ae08745Sheppo 
36831ae08745Sheppo 		if ((ldc_unreg_callback(ldcp->ldc_handle)) == 0) {
36841ae08745Sheppo 			D2(vswp, "%s: unreg callback for chan %ld", __func__,
36851ae08745Sheppo 				ldcp->ldc_id);
36861ae08745Sheppo 			mutex_exit(&ldcp->drain_cv_lock);
36871ae08745Sheppo 			continue;
36881ae08745Sheppo 		} else {
36891ae08745Sheppo 			/*
36901ae08745Sheppo 			 * If we end up here we know that either 1) a callback
36911ae08745Sheppo 			 * is currently executing, 2) is about to start (i.e.
36921ae08745Sheppo 			 * the ldc framework has set the active flag but
36931ae08745Sheppo 			 * has not actually invoked the callback yet, or 3)
36941ae08745Sheppo 			 * has finished and has returned to the ldc framework
36951ae08745Sheppo 			 * but the ldc framework has not yet cleared the
36961ae08745Sheppo 			 * active bit.
36971ae08745Sheppo 			 *
36981ae08745Sheppo 			 * Wait for it to finish.
36991ae08745Sheppo 			 */
37001ae08745Sheppo 			while (ldc_unreg_callback(ldcp->ldc_handle)
37011ae08745Sheppo 								== EWOULDBLOCK)
37021ae08745Sheppo 				(void) cv_timedwait(&ldcp->drain_cv,
37031ae08745Sheppo 					&ldcp->drain_cv_lock, lbolt + hz);
37041ae08745Sheppo 
37051ae08745Sheppo 			mutex_exit(&ldcp->drain_cv_lock);
37061ae08745Sheppo 			D2(vswp, "%s: unreg callback for chan %ld after "
37071ae08745Sheppo 				"timeout", __func__, ldcp->ldc_id);
37081ae08745Sheppo 		}
37091ae08745Sheppo 	}
37101ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
37111ae08745Sheppo 
37121ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
37131ae08745Sheppo 	return (0);
37141ae08745Sheppo }
37151ae08745Sheppo 
37161ae08745Sheppo /*
37171ae08745Sheppo  * Wait until all tasks which reference this port have completed.
37181ae08745Sheppo  *
37191ae08745Sheppo  * Prior to this function being invoked each channel under this port
37201ae08745Sheppo  * should have been quiesced via ldc_set_cb_mode(DISABLE).
37211ae08745Sheppo  */
37221ae08745Sheppo static int
37231ae08745Sheppo vsw_drain_port_taskq(vsw_port_t *port)
37241ae08745Sheppo {
37251ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
37261ae08745Sheppo 
37271ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
37281ae08745Sheppo 
37291ae08745Sheppo 	/*
37301ae08745Sheppo 	 * Mark the port as in the process of being detached, and
37311ae08745Sheppo 	 * dispatch a marker task to the queue so we know when all
37321ae08745Sheppo 	 * relevant tasks have completed.
37331ae08745Sheppo 	 */
37341ae08745Sheppo 	mutex_enter(&port->state_lock);
37351ae08745Sheppo 	port->state = VSW_PORT_DETACHING;
37361ae08745Sheppo 
37371ae08745Sheppo 	if ((vswp->taskq_p == NULL) ||
37381ae08745Sheppo 		(ddi_taskq_dispatch(vswp->taskq_p, vsw_marker_task,
37391ae08745Sheppo 			port, DDI_NOSLEEP) != DDI_SUCCESS)) {
37401ae08745Sheppo 		DERR(vswp, "%s: unable to dispatch marker task",
37411ae08745Sheppo 			__func__);
37421ae08745Sheppo 		mutex_exit(&port->state_lock);
37431ae08745Sheppo 		return (1);
37441ae08745Sheppo 	}
37451ae08745Sheppo 
37461ae08745Sheppo 	/*
37471ae08745Sheppo 	 * Wait for the marker task to finish.
37481ae08745Sheppo 	 */
37491ae08745Sheppo 	while (port->state != VSW_PORT_DETACHABLE)
37501ae08745Sheppo 		cv_wait(&port->state_cv, &port->state_lock);
37511ae08745Sheppo 
37521ae08745Sheppo 	mutex_exit(&port->state_lock);
37531ae08745Sheppo 
37541ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
37551ae08745Sheppo 
37561ae08745Sheppo 	return (0);
37571ae08745Sheppo }
37581ae08745Sheppo 
37591ae08745Sheppo static void
37601ae08745Sheppo vsw_marker_task(void *arg)
37611ae08745Sheppo {
37621ae08745Sheppo 	vsw_port_t	*port = arg;
37631ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
37641ae08745Sheppo 
37651ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
37661ae08745Sheppo 
37671ae08745Sheppo 	mutex_enter(&port->state_lock);
37681ae08745Sheppo 
37691ae08745Sheppo 	/*
37701ae08745Sheppo 	 * No further tasks should be dispatched which reference
37711ae08745Sheppo 	 * this port so ok to mark it as safe to detach.
37721ae08745Sheppo 	 */
37731ae08745Sheppo 	port->state = VSW_PORT_DETACHABLE;
37741ae08745Sheppo 
37751ae08745Sheppo 	cv_signal(&port->state_cv);
37761ae08745Sheppo 
37771ae08745Sheppo 	mutex_exit(&port->state_lock);
37781ae08745Sheppo 
37791ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
37801ae08745Sheppo }
37811ae08745Sheppo 
37821ae08745Sheppo static vsw_port_t *
37831ae08745Sheppo vsw_lookup_port(vsw_t *vswp, int p_instance)
37841ae08745Sheppo {
37851ae08745Sheppo 	vsw_port_list_t *plist = &vswp->plist;
37861ae08745Sheppo 	vsw_port_t	*port;
37871ae08745Sheppo 
37881ae08745Sheppo 	for (port = plist->head; port != NULL; port = port->p_next) {
37891ae08745Sheppo 		if (port->p_instance == p_instance) {
37901ae08745Sheppo 			D2(vswp, "vsw_lookup_port: found p_instance\n");
37911ae08745Sheppo 			return (port);
37921ae08745Sheppo 		}
37931ae08745Sheppo 	}
37941ae08745Sheppo 
37951ae08745Sheppo 	return (NULL);
37961ae08745Sheppo }
37971ae08745Sheppo 
37981ae08745Sheppo /*
37991ae08745Sheppo  * Search for and remove the specified port from the port
38001ae08745Sheppo  * list. Returns 0 if able to locate and remove port, otherwise
38011ae08745Sheppo  * returns 1.
38021ae08745Sheppo  */
38031ae08745Sheppo static int
38041ae08745Sheppo vsw_plist_del_node(vsw_t *vswp, vsw_port_t *port)
38051ae08745Sheppo {
38061ae08745Sheppo 	vsw_port_list_t *plist = &vswp->plist;
38071ae08745Sheppo 	vsw_port_t	*curr_p, *prev_p;
38081ae08745Sheppo 
38091ae08745Sheppo 	if (plist->head == NULL)
38101ae08745Sheppo 		return (1);
38111ae08745Sheppo 
38121ae08745Sheppo 	curr_p = prev_p = plist->head;
38131ae08745Sheppo 
38141ae08745Sheppo 	while (curr_p != NULL) {
38151ae08745Sheppo 		if (curr_p == port) {
38161ae08745Sheppo 			if (prev_p == curr_p) {
38171ae08745Sheppo 				plist->head = curr_p->p_next;
38181ae08745Sheppo 			} else {
38191ae08745Sheppo 				prev_p->p_next = curr_p->p_next;
38201ae08745Sheppo 			}
38211ae08745Sheppo 			plist->num_ports--;
38221ae08745Sheppo 			break;
38231ae08745Sheppo 		} else {
38241ae08745Sheppo 			prev_p = curr_p;
38251ae08745Sheppo 			curr_p = curr_p->p_next;
38261ae08745Sheppo 		}
38271ae08745Sheppo 	}
38281ae08745Sheppo 	return (0);
38291ae08745Sheppo }
38301ae08745Sheppo 
38311ae08745Sheppo /*
38321ae08745Sheppo  * Interrupt handler for ldc messages.
38331ae08745Sheppo  */
38341ae08745Sheppo static uint_t
38351ae08745Sheppo vsw_ldc_cb(uint64_t event, caddr_t arg)
38361ae08745Sheppo {
38371ae08745Sheppo 	vsw_ldc_t	*ldcp = (vsw_ldc_t  *)arg;
38381ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
38391ae08745Sheppo 	ldc_status_t	lstatus;
38401ae08745Sheppo 	int		rv;
38411ae08745Sheppo 
38421ae08745Sheppo 	D1(vswp, "%s: enter: ldcid (%lld)\n", __func__, ldcp->ldc_id);
38431ae08745Sheppo 
38441ae08745Sheppo 	mutex_enter(&ldcp->ldc_cblock);
38451ae08745Sheppo 
38461ae08745Sheppo 	if ((ldcp->ldc_status == LDC_INIT) || (ldcp->ldc_handle == NULL)) {
38471ae08745Sheppo 		mutex_exit(&ldcp->ldc_cblock);
38481ae08745Sheppo 		return (LDC_SUCCESS);
38491ae08745Sheppo 	}
38501ae08745Sheppo 
38513af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
38523af08d82Slm66018 	lstatus = ldcp->ldc_status;
38533af08d82Slm66018 	rv = ldc_status(ldcp->ldc_handle, &ldcp->ldc_status);
38543af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
38553af08d82Slm66018 	if (rv != 0) {
3856*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Unable to read channel state",
3857*34683adeSsg70180 			vswp->instance);
38583af08d82Slm66018 		goto vsw_cb_exit;
38593af08d82Slm66018 	}
38603af08d82Slm66018 
38611ae08745Sheppo 	if (event & LDC_EVT_UP) {
38621ae08745Sheppo 		/*
38631ae08745Sheppo 		 * Channel has come up, get the state and then start
38641ae08745Sheppo 		 * the handshake.
38651ae08745Sheppo 		 */
38661ae08745Sheppo 		D2(vswp, "%s: id(%ld) event(%llx) UP: status(%ld)",
38673af08d82Slm66018 			__func__, ldcp->ldc_id, event, lstatus);
38683af08d82Slm66018 		D2(vswp, "%s: UP: old status %ld : cur status %ld",
38693af08d82Slm66018 			__func__, lstatus, ldcp->ldc_status);
38703af08d82Slm66018 		if ((ldcp->ldc_status != lstatus) &&
38713af08d82Slm66018 					(ldcp->ldc_status == LDC_UP)) {
38721ae08745Sheppo 				vsw_restart_handshake(ldcp);
38733af08d82Slm66018 		}
38741ae08745Sheppo 
38751ae08745Sheppo 		ASSERT((event & (LDC_EVT_RESET | LDC_EVT_DOWN)) == 0);
38761ae08745Sheppo 	}
38771ae08745Sheppo 
38781ae08745Sheppo 	if (event & LDC_EVT_READ) {
38791ae08745Sheppo 		/*
38801ae08745Sheppo 		 * Data available for reading.
38811ae08745Sheppo 		 */
38821ae08745Sheppo 		D2(vswp, "%s: id(ld) event(%llx) data READ",
38831ae08745Sheppo 				__func__, ldcp->ldc_id, event);
38841ae08745Sheppo 
38851ae08745Sheppo 		vsw_process_pkt(ldcp);
38861ae08745Sheppo 
38871ae08745Sheppo 		ASSERT((event & (LDC_EVT_RESET | LDC_EVT_DOWN)) == 0);
38881ae08745Sheppo 
38891ae08745Sheppo 		goto vsw_cb_exit;
38901ae08745Sheppo 	}
38911ae08745Sheppo 
38923af08d82Slm66018 	if (event & (LDC_EVT_DOWN | LDC_EVT_RESET)) {
38933af08d82Slm66018 		D2(vswp, "%s: id(%ld) event(%llx) DOWN/RESET",
38943af08d82Slm66018 					__func__, ldcp->ldc_id, event);
38953af08d82Slm66018 
38963af08d82Slm66018 		/* attempt to restart the connection */
38973af08d82Slm66018 		vsw_restart_ldc(ldcp);
38983af08d82Slm66018 
38993af08d82Slm66018 		/*
39003af08d82Slm66018 		 * vsw_restart_ldc() will attempt to bring the channel
39013af08d82Slm66018 		 * back up. Check here to see if that succeeded.
39023af08d82Slm66018 		 */
39033af08d82Slm66018 		mutex_enter(&ldcp->status_lock);
39043af08d82Slm66018 		lstatus = ldcp->ldc_status;
39053af08d82Slm66018 		rv = ldc_status(ldcp->ldc_handle, &ldcp->ldc_status);
39063af08d82Slm66018 		mutex_exit(&ldcp->status_lock);
39071ae08745Sheppo 		if (rv != 0) {
39083af08d82Slm66018 			DERR(vswp, "%s: unable to read status for channel %ld",
39093af08d82Slm66018 				__func__, ldcp->ldc_id);
39103af08d82Slm66018 			goto vsw_cb_exit;
39111ae08745Sheppo 		}
39121ae08745Sheppo 
39133af08d82Slm66018 		D2(vswp, "%s: id(%ld) event(%llx) DOWN/RESET event:"
39143af08d82Slm66018 			" old status %ld : cur status %ld", __func__,
39153af08d82Slm66018 			ldcp->ldc_id, event, lstatus, ldcp->ldc_status);
39163af08d82Slm66018 
39173af08d82Slm66018 		/*
39183af08d82Slm66018 		 * If channel was not previously UP then (re)start the
39193af08d82Slm66018 		 * handshake.
39203af08d82Slm66018 		 */
39213af08d82Slm66018 		if ((ldcp->ldc_status == LDC_UP) && (lstatus != LDC_UP)) {
39223af08d82Slm66018 			D2(vswp, "%s: channel %ld now UP, restarting "
39233af08d82Slm66018 				"handshake", __func__, ldcp->ldc_id);
39243af08d82Slm66018 			vsw_restart_handshake(ldcp);
39251ae08745Sheppo 		}
39261ae08745Sheppo 	}
39271ae08745Sheppo 
39281ae08745Sheppo 	/*
39291ae08745Sheppo 	 * Catch either LDC_EVT_WRITE which we don't support or any
39301ae08745Sheppo 	 * unknown event.
39311ae08745Sheppo 	 */
39321ae08745Sheppo 	if (event & ~(LDC_EVT_UP | LDC_EVT_RESET
39331ae08745Sheppo 					| LDC_EVT_DOWN | LDC_EVT_READ)) {
39341ae08745Sheppo 
39351ae08745Sheppo 		DERR(vswp, "%s: id(%ld) Unexpected event=(%llx) status(%ld)",
39361ae08745Sheppo 			__func__, ldcp->ldc_id, event, ldcp->ldc_status);
39371ae08745Sheppo 	}
39381ae08745Sheppo 
39391ae08745Sheppo vsw_cb_exit:
39401ae08745Sheppo 	mutex_exit(&ldcp->ldc_cblock);
39411ae08745Sheppo 
39421ae08745Sheppo 	/*
39431ae08745Sheppo 	 * Let the drain function know we are finishing if it
39441ae08745Sheppo 	 * is waiting.
39451ae08745Sheppo 	 */
39461ae08745Sheppo 	mutex_enter(&ldcp->drain_cv_lock);
39471ae08745Sheppo 	if (ldcp->drain_state == VSW_LDC_DRAINING)
39481ae08745Sheppo 		cv_signal(&ldcp->drain_cv);
39491ae08745Sheppo 	mutex_exit(&ldcp->drain_cv_lock);
39501ae08745Sheppo 
39511ae08745Sheppo 	return (LDC_SUCCESS);
39521ae08745Sheppo }
39531ae08745Sheppo 
39541ae08745Sheppo /*
39553af08d82Slm66018  * Restart the connection with our peer. Free any existing
39563af08d82Slm66018  * data structures and then attempt to bring channel back
39573af08d82Slm66018  * up.
39581ae08745Sheppo  */
39591ae08745Sheppo static void
39603af08d82Slm66018 vsw_restart_ldc(vsw_ldc_t *ldcp)
39611ae08745Sheppo {
39623af08d82Slm66018 	int		rv;
39631ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
39641ae08745Sheppo 	vsw_port_t	*port;
39651ae08745Sheppo 	vsw_ldc_list_t	*ldcl;
39661ae08745Sheppo 
39673af08d82Slm66018 	D1(vswp, "%s: enter", __func__);
39681ae08745Sheppo 
39691ae08745Sheppo 	port = ldcp->ldc_port;
39701ae08745Sheppo 	ldcl = &port->p_ldclist;
39711ae08745Sheppo 
39723af08d82Slm66018 	READ_ENTER(&ldcl->lockrw);
39731ae08745Sheppo 
39741ae08745Sheppo 	D2(vswp, "%s: in 0x%llx : out 0x%llx", __func__,
39751ae08745Sheppo 		ldcp->lane_in.lstate, ldcp->lane_out.lstate);
39761ae08745Sheppo 
39771ae08745Sheppo 	vsw_free_lane_resources(ldcp, INBOUND);
39781ae08745Sheppo 	vsw_free_lane_resources(ldcp, OUTBOUND);
39791ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
39801ae08745Sheppo 
39811ae08745Sheppo 	ldcp->lane_in.lstate = 0;
39821ae08745Sheppo 	ldcp->lane_out.lstate = 0;
39831ae08745Sheppo 
39841ae08745Sheppo 	/*
39851ae08745Sheppo 	 * Remove parent port from any multicast groups
39861ae08745Sheppo 	 * it may have registered with. Client must resend
39871ae08745Sheppo 	 * multicast add command after handshake completes.
39881ae08745Sheppo 	 */
39891ae08745Sheppo 	(void) vsw_del_fdb(vswp, port);
39901ae08745Sheppo 
39911ae08745Sheppo 	vsw_del_mcst_port(port);
39921ae08745Sheppo 
39931ae08745Sheppo 	ldcp->peer_session = 0;
39941ae08745Sheppo 	ldcp->session_status = 0;
39953af08d82Slm66018 	ldcp->hcnt = 0;
39963af08d82Slm66018 	ldcp->hphase = VSW_MILESTONE0;
39973af08d82Slm66018 
39983af08d82Slm66018 	rv = ldc_up(ldcp->ldc_handle);
39993af08d82Slm66018 	if (rv != 0) {
40003af08d82Slm66018 		/*
40013af08d82Slm66018 		 * Not a fatal error for ldc_up() to fail, as peer
40023af08d82Slm66018 		 * end point may simply not be ready yet.
40033af08d82Slm66018 		 */
40043af08d82Slm66018 		D2(vswp, "%s: ldc_up err id(%lld) rv(%d)", __func__,
40053af08d82Slm66018 			ldcp->ldc_id, rv);
40063af08d82Slm66018 	}
40073af08d82Slm66018 
40083af08d82Slm66018 	D1(vswp, "%s: exit", __func__);
40093af08d82Slm66018 }
40103af08d82Slm66018 
40113af08d82Slm66018 /*
40123af08d82Slm66018  * (Re)start a handshake with our peer by sending them
40133af08d82Slm66018  * our version info.
40143af08d82Slm66018  */
40153af08d82Slm66018 static void
40163af08d82Slm66018 vsw_restart_handshake(vsw_ldc_t *ldcp)
40173af08d82Slm66018 {
40183af08d82Slm66018 	vsw_t		*vswp = ldcp->ldc_vswp;
40193af08d82Slm66018 
40203af08d82Slm66018 	D1(vswp, "vsw_restart_handshake: enter");
40213af08d82Slm66018 
40223af08d82Slm66018 	if (ldcp->hphase != VSW_MILESTONE0) {
40233af08d82Slm66018 		vsw_restart_ldc(ldcp);
40243af08d82Slm66018 	}
40251ae08745Sheppo 
40261ae08745Sheppo 	/*
40271ae08745Sheppo 	 * We now increment the transaction group id. This allows
40281ae08745Sheppo 	 * us to identify and disard any tasks which are still pending
40291ae08745Sheppo 	 * on the taskq and refer to the handshake session we are about
40301ae08745Sheppo 	 * to restart. These stale messages no longer have any real
40311ae08745Sheppo 	 * meaning.
40321ae08745Sheppo 	 */
40331ae08745Sheppo 	mutex_enter(&ldcp->hss_lock);
40341ae08745Sheppo 	ldcp->hss_id++;
40351ae08745Sheppo 	mutex_exit(&ldcp->hss_lock);
40361ae08745Sheppo 
40371ae08745Sheppo 	if (ldcp->hcnt++ > vsw_num_handshakes) {
4038*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: exceeded number of permitted "
40391ae08745Sheppo 			"handshake attempts (%d) on channel %ld",
4040*34683adeSsg70180 			vswp->instance, ldcp->hcnt, ldcp->ldc_id);
40411ae08745Sheppo 		return;
40421ae08745Sheppo 	}
40431ae08745Sheppo 
40443af08d82Slm66018 	if ((vswp->taskq_p == NULL) ||
40453af08d82Slm66018 		(ddi_taskq_dispatch(vswp->taskq_p, vsw_send_ver, ldcp,
40463af08d82Slm66018 			DDI_NOSLEEP) != DDI_SUCCESS)) {
4047*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: Can't dispatch version handshake "
4048*34683adeSsg70180 			"task", vswp->instance);
40493af08d82Slm66018 	}
40501ae08745Sheppo 
40511ae08745Sheppo 	D1(vswp, "vsw_restart_handshake: exit");
40521ae08745Sheppo }
40531ae08745Sheppo 
40541ae08745Sheppo /*
40553af08d82Slm66018  * Deal appropriately with a ECONNRESET event encountered in a ldc_*
40563af08d82Slm66018  * call.
40573af08d82Slm66018  */
40583af08d82Slm66018 static void
40593af08d82Slm66018 vsw_handle_reset(vsw_ldc_t *ldcp)
40603af08d82Slm66018 {
40613af08d82Slm66018 	vsw_t		*vswp = ldcp->ldc_vswp;
40623af08d82Slm66018 	ldc_status_t	lstatus;
40633af08d82Slm66018 
40643af08d82Slm66018 	D1(vswp, "%s: enter", __func__);
40653af08d82Slm66018 
40663af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
40673af08d82Slm66018 	lstatus = ldcp->ldc_status;
40683af08d82Slm66018 	if (ldc_status(ldcp->ldc_handle, &ldcp->ldc_status) != 0) {
40693af08d82Slm66018 		DERR(vswp, "%s: unable to read status for channel %ld",
40703af08d82Slm66018 			__func__, ldcp->ldc_id);
40713af08d82Slm66018 		mutex_exit(&ldcp->status_lock);
40723af08d82Slm66018 		return;
40733af08d82Slm66018 	}
40743af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
40753af08d82Slm66018 
40763af08d82Slm66018 	/*
40773af08d82Slm66018 	 * Check the channel's previous recorded state to
40783af08d82Slm66018 	 * determine if this is the first ECONNRESET event
40793af08d82Slm66018 	 * we've gotten for this particular channel (i.e. was
40803af08d82Slm66018 	 * previously up but is no longer). If so, terminate
40813af08d82Slm66018 	 * the channel.
40823af08d82Slm66018 	 */
40833af08d82Slm66018 	if ((ldcp->ldc_status != LDC_UP) && (lstatus == LDC_UP)) {
40843af08d82Slm66018 		vsw_restart_ldc(ldcp);
40853af08d82Slm66018 	}
40863af08d82Slm66018 
40873af08d82Slm66018 	/*
40883af08d82Slm66018 	 * vsw_restart_ldc() will also attempt to bring channel
40893af08d82Slm66018 	 * back up. Check here if that succeeds.
40903af08d82Slm66018 	 */
40913af08d82Slm66018 	mutex_enter(&ldcp->status_lock);
40923af08d82Slm66018 	lstatus = ldcp->ldc_status;
40933af08d82Slm66018 	if (ldc_status(ldcp->ldc_handle, &ldcp->ldc_status) != 0) {
40943af08d82Slm66018 		DERR(vswp, "%s: unable to read status for channel %ld",
40953af08d82Slm66018 			__func__, ldcp->ldc_id);
40963af08d82Slm66018 		mutex_exit(&ldcp->status_lock);
40973af08d82Slm66018 		return;
40983af08d82Slm66018 	}
40993af08d82Slm66018 	mutex_exit(&ldcp->status_lock);
41003af08d82Slm66018 
41013af08d82Slm66018 	/*
41023af08d82Slm66018 	 * If channel is now up and no one else (i.e. the callback routine)
41033af08d82Slm66018 	 * has dealt with it then we restart the handshake here.
41043af08d82Slm66018 	 */
41053af08d82Slm66018 	if ((lstatus != LDC_UP) && (ldcp->ldc_status == LDC_UP)) {
41063af08d82Slm66018 		vsw_restart_handshake(ldcp);
41073af08d82Slm66018 	}
41083af08d82Slm66018 
41093af08d82Slm66018 	D1(vswp, "%s: exit", __func__);
41103af08d82Slm66018 }
41113af08d82Slm66018 
41123af08d82Slm66018 /*
41131ae08745Sheppo  * returns 0 if legal for event signified by flag to have
41141ae08745Sheppo  * occured at the time it did. Otherwise returns 1.
41151ae08745Sheppo  */
41161ae08745Sheppo int
41171ae08745Sheppo vsw_check_flag(vsw_ldc_t *ldcp, int dir, uint64_t flag)
41181ae08745Sheppo {
41191ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
41201ae08745Sheppo 	uint64_t	state;
41211ae08745Sheppo 	uint64_t	phase;
41221ae08745Sheppo 
41231ae08745Sheppo 	if (dir == INBOUND)
41241ae08745Sheppo 		state = ldcp->lane_in.lstate;
41251ae08745Sheppo 	else
41261ae08745Sheppo 		state = ldcp->lane_out.lstate;
41271ae08745Sheppo 
41281ae08745Sheppo 	phase = ldcp->hphase;
41291ae08745Sheppo 
41301ae08745Sheppo 	switch (flag) {
41311ae08745Sheppo 	case VSW_VER_INFO_RECV:
41321ae08745Sheppo 		if (phase > VSW_MILESTONE0) {
41331ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): VER_INFO_RECV"
41341ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
41351ae08745Sheppo 			vsw_restart_handshake(ldcp);
41361ae08745Sheppo 			return (1);
41371ae08745Sheppo 		}
41381ae08745Sheppo 		break;
41391ae08745Sheppo 
41401ae08745Sheppo 	case VSW_VER_ACK_RECV:
41411ae08745Sheppo 	case VSW_VER_NACK_RECV:
41421ae08745Sheppo 		if (!(state & VSW_VER_INFO_SENT)) {
41431ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious VER_ACK"
41441ae08745Sheppo 				" or VER_NACK when in state %d\n",
41451ae08745Sheppo 				ldcp->ldc_id, phase);
41461ae08745Sheppo 			vsw_restart_handshake(ldcp);
41471ae08745Sheppo 			return (1);
41481ae08745Sheppo 		} else
41491ae08745Sheppo 			state &= ~VSW_VER_INFO_SENT;
41501ae08745Sheppo 		break;
41511ae08745Sheppo 
41521ae08745Sheppo 	case VSW_ATTR_INFO_RECV:
41531ae08745Sheppo 		if ((phase < VSW_MILESTONE1) || (phase >= VSW_MILESTONE2)) {
41541ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): ATTR_INFO_RECV"
41551ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
41561ae08745Sheppo 			vsw_restart_handshake(ldcp);
41571ae08745Sheppo 			return (1);
41581ae08745Sheppo 		}
41591ae08745Sheppo 		break;
41601ae08745Sheppo 
41611ae08745Sheppo 	case VSW_ATTR_ACK_RECV:
41621ae08745Sheppo 	case VSW_ATTR_NACK_RECV:
41631ae08745Sheppo 		if (!(state & VSW_ATTR_INFO_SENT)) {
41641ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious ATTR_ACK"
41651ae08745Sheppo 				" or ATTR_NACK when in state %d\n",
41661ae08745Sheppo 				ldcp->ldc_id, phase);
41671ae08745Sheppo 			vsw_restart_handshake(ldcp);
41681ae08745Sheppo 			return (1);
41691ae08745Sheppo 		} else
41701ae08745Sheppo 			state &= ~VSW_ATTR_INFO_SENT;
41711ae08745Sheppo 		break;
41721ae08745Sheppo 
41731ae08745Sheppo 	case VSW_DRING_INFO_RECV:
41741ae08745Sheppo 		if (phase < VSW_MILESTONE1) {
41751ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): DRING_INFO_RECV"
41761ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
41771ae08745Sheppo 			vsw_restart_handshake(ldcp);
41781ae08745Sheppo 			return (1);
41791ae08745Sheppo 		}
41801ae08745Sheppo 		break;
41811ae08745Sheppo 
41821ae08745Sheppo 	case VSW_DRING_ACK_RECV:
41831ae08745Sheppo 	case VSW_DRING_NACK_RECV:
41841ae08745Sheppo 		if (!(state & VSW_DRING_INFO_SENT)) {
41851ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious DRING_ACK"
41861ae08745Sheppo 				" or DRING_NACK when in state %d\n",
41871ae08745Sheppo 				ldcp->ldc_id, phase);
41881ae08745Sheppo 			vsw_restart_handshake(ldcp);
41891ae08745Sheppo 			return (1);
41901ae08745Sheppo 		} else
41911ae08745Sheppo 			state &= ~VSW_DRING_INFO_SENT;
41921ae08745Sheppo 		break;
41931ae08745Sheppo 
41941ae08745Sheppo 	case VSW_RDX_INFO_RECV:
41951ae08745Sheppo 		if (phase < VSW_MILESTONE3) {
41961ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): RDX_INFO_RECV"
41971ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
41981ae08745Sheppo 			vsw_restart_handshake(ldcp);
41991ae08745Sheppo 			return (1);
42001ae08745Sheppo 		}
42011ae08745Sheppo 		break;
42021ae08745Sheppo 
42031ae08745Sheppo 	case VSW_RDX_ACK_RECV:
42041ae08745Sheppo 	case VSW_RDX_NACK_RECV:
42051ae08745Sheppo 		if (!(state & VSW_RDX_INFO_SENT)) {
42061ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): spurious RDX_ACK"
42071ae08745Sheppo 				" or RDX_NACK when in state %d\n",
42081ae08745Sheppo 				ldcp->ldc_id, phase);
42091ae08745Sheppo 			vsw_restart_handshake(ldcp);
42101ae08745Sheppo 			return (1);
42111ae08745Sheppo 		} else
42121ae08745Sheppo 			state &= ~VSW_RDX_INFO_SENT;
42131ae08745Sheppo 		break;
42141ae08745Sheppo 
42151ae08745Sheppo 	case VSW_MCST_INFO_RECV:
42161ae08745Sheppo 		if (phase < VSW_MILESTONE3) {
42171ae08745Sheppo 			DERR(vswp, "vsw_check_flag (%d): VSW_MCST_INFO_RECV"
42181ae08745Sheppo 				" when in state %d\n", ldcp->ldc_id, phase);
42191ae08745Sheppo 			vsw_restart_handshake(ldcp);
42201ae08745Sheppo 			return (1);
42211ae08745Sheppo 		}
42221ae08745Sheppo 		break;
42231ae08745Sheppo 
42241ae08745Sheppo 	default:
42251ae08745Sheppo 		DERR(vswp, "vsw_check_flag (%lld): unknown flag (%llx)",
42261ae08745Sheppo 				ldcp->ldc_id, flag);
42271ae08745Sheppo 		return (1);
42281ae08745Sheppo 	}
42291ae08745Sheppo 
42301ae08745Sheppo 	if (dir == INBOUND)
42311ae08745Sheppo 		ldcp->lane_in.lstate = state;
42321ae08745Sheppo 	else
42331ae08745Sheppo 		ldcp->lane_out.lstate = state;
42341ae08745Sheppo 
42351ae08745Sheppo 	D1(vswp, "vsw_check_flag (chan %lld): exit", ldcp->ldc_id);
42361ae08745Sheppo 
42371ae08745Sheppo 	return (0);
42381ae08745Sheppo }
42391ae08745Sheppo 
42401ae08745Sheppo void
42411ae08745Sheppo vsw_next_milestone(vsw_ldc_t *ldcp)
42421ae08745Sheppo {
42431ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
42441ae08745Sheppo 
42451ae08745Sheppo 	D1(vswp, "%s (chan %lld): enter (phase %ld)", __func__,
42461ae08745Sheppo 		ldcp->ldc_id, ldcp->hphase);
42471ae08745Sheppo 
42481ae08745Sheppo 	DUMP_FLAGS(ldcp->lane_in.lstate);
42491ae08745Sheppo 	DUMP_FLAGS(ldcp->lane_out.lstate);
42501ae08745Sheppo 
42511ae08745Sheppo 	switch (ldcp->hphase) {
42521ae08745Sheppo 
42531ae08745Sheppo 	case VSW_MILESTONE0:
42541ae08745Sheppo 		/*
42551ae08745Sheppo 		 * If we haven't started to handshake with our peer,
42561ae08745Sheppo 		 * start to do so now.
42571ae08745Sheppo 		 */
42581ae08745Sheppo 		if (ldcp->lane_out.lstate == 0) {
42591ae08745Sheppo 			D2(vswp, "%s: (chan %lld) starting handshake "
42601ae08745Sheppo 				"with peer", __func__, ldcp->ldc_id);
42611ae08745Sheppo 			vsw_restart_handshake(ldcp);
42621ae08745Sheppo 		}
42631ae08745Sheppo 
42641ae08745Sheppo 		/*
42651ae08745Sheppo 		 * Only way to pass this milestone is to have successfully
42661ae08745Sheppo 		 * negotiated version info.
42671ae08745Sheppo 		 */
42681ae08745Sheppo 		if ((ldcp->lane_in.lstate & VSW_VER_ACK_SENT) &&
42691ae08745Sheppo 			(ldcp->lane_out.lstate & VSW_VER_ACK_RECV)) {
42701ae08745Sheppo 
42711ae08745Sheppo 			D2(vswp, "%s: (chan %lld) leaving milestone 0",
42721ae08745Sheppo 				__func__, ldcp->ldc_id);
42731ae08745Sheppo 
42741ae08745Sheppo 			/*
42751ae08745Sheppo 			 * Next milestone is passed when attribute
42761ae08745Sheppo 			 * information has been successfully exchanged.
42771ae08745Sheppo 			 */
42781ae08745Sheppo 			ldcp->hphase = VSW_MILESTONE1;
42791ae08745Sheppo 			vsw_send_attr(ldcp);
42801ae08745Sheppo 
42811ae08745Sheppo 		}
42821ae08745Sheppo 		break;
42831ae08745Sheppo 
42841ae08745Sheppo 	case VSW_MILESTONE1:
42851ae08745Sheppo 		/*
42861ae08745Sheppo 		 * Only way to pass this milestone is to have successfully
42871ae08745Sheppo 		 * negotiated attribute information.
42881ae08745Sheppo 		 */
42891ae08745Sheppo 		if (ldcp->lane_in.lstate & VSW_ATTR_ACK_SENT) {
42901ae08745Sheppo 
42911ae08745Sheppo 			ldcp->hphase = VSW_MILESTONE2;
42921ae08745Sheppo 
42931ae08745Sheppo 			/*
42941ae08745Sheppo 			 * If the peer device has said it wishes to
42951ae08745Sheppo 			 * use descriptor rings then we send it our ring
42961ae08745Sheppo 			 * info, otherwise we just set up a private ring
42971ae08745Sheppo 			 * which we use an internal buffer
42981ae08745Sheppo 			 */
42991ae08745Sheppo 			if (ldcp->lane_in.xfer_mode == VIO_DRING_MODE)
43001ae08745Sheppo 				vsw_send_dring_info(ldcp);
43011ae08745Sheppo 		}
43021ae08745Sheppo 		break;
43031ae08745Sheppo 
43041ae08745Sheppo 
43051ae08745Sheppo 	case VSW_MILESTONE2:
43061ae08745Sheppo 		/*
43071ae08745Sheppo 		 * If peer has indicated in its attribute message that
43081ae08745Sheppo 		 * it wishes to use descriptor rings then the only way
43091ae08745Sheppo 		 * to pass this milestone is for us to have received
43101ae08745Sheppo 		 * valid dring info.
43111ae08745Sheppo 		 *
43121ae08745Sheppo 		 * If peer is not using descriptor rings then just fall
43131ae08745Sheppo 		 * through.
43141ae08745Sheppo 		 */
43151ae08745Sheppo 		if ((ldcp->lane_in.xfer_mode == VIO_DRING_MODE) &&
43161ae08745Sheppo 			(!(ldcp->lane_in.lstate & VSW_DRING_ACK_SENT)))
43171ae08745Sheppo 			break;
43181ae08745Sheppo 
43191ae08745Sheppo 		D2(vswp, "%s: (chan %lld) leaving milestone 2",
43201ae08745Sheppo 				__func__, ldcp->ldc_id);
43211ae08745Sheppo 
43221ae08745Sheppo 		ldcp->hphase = VSW_MILESTONE3;
43231ae08745Sheppo 		vsw_send_rdx(ldcp);
43241ae08745Sheppo 		break;
43251ae08745Sheppo 
43261ae08745Sheppo 	case VSW_MILESTONE3:
43271ae08745Sheppo 		/*
43281ae08745Sheppo 		 * Pass this milestone when all paramaters have been
43291ae08745Sheppo 		 * successfully exchanged and RDX sent in both directions.
43301ae08745Sheppo 		 *
43311ae08745Sheppo 		 * Mark outbound lane as available to transmit data.
43321ae08745Sheppo 		 */
43331ae08745Sheppo 		if ((ldcp->lane_in.lstate & VSW_RDX_ACK_SENT) &&
43341ae08745Sheppo 			(ldcp->lane_out.lstate & VSW_RDX_ACK_RECV)) {
43351ae08745Sheppo 
43361ae08745Sheppo 			D2(vswp, "%s: (chan %lld) leaving milestone 3",
43371ae08745Sheppo 				__func__, ldcp->ldc_id);
43383af08d82Slm66018 			D2(vswp, "%s: ** handshake complete (0x%llx : "
43393af08d82Slm66018 				"0x%llx) **", __func__, ldcp->lane_in.lstate,
43403af08d82Slm66018 				ldcp->lane_out.lstate);
43411ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_LANE_ACTIVE;
43421ae08745Sheppo 			ldcp->hphase = VSW_MILESTONE4;
43431ae08745Sheppo 			ldcp->hcnt = 0;
43441ae08745Sheppo 			DISPLAY_STATE();
43453af08d82Slm66018 		} else {
43463af08d82Slm66018 			D2(vswp, "%s: still in milestone 3 (0x%llx :"
43473af08d82Slm66018 				" 0x%llx", __func__, ldcp->lane_in.lstate,
43483af08d82Slm66018 				ldcp->lane_out.lstate);
43491ae08745Sheppo 		}
43501ae08745Sheppo 		break;
43511ae08745Sheppo 
43521ae08745Sheppo 	case VSW_MILESTONE4:
43531ae08745Sheppo 		D2(vswp, "%s: (chan %lld) in milestone 4", __func__,
43541ae08745Sheppo 							ldcp->ldc_id);
43551ae08745Sheppo 		break;
43561ae08745Sheppo 
43571ae08745Sheppo 	default:
43581ae08745Sheppo 		DERR(vswp, "%s: (chan %lld) Unknown Phase %x", __func__,
43591ae08745Sheppo 			ldcp->ldc_id, ldcp->hphase);
43601ae08745Sheppo 	}
43611ae08745Sheppo 
43621ae08745Sheppo 	D1(vswp, "%s (chan %lld): exit (phase %ld)", __func__, ldcp->ldc_id,
43631ae08745Sheppo 		ldcp->hphase);
43641ae08745Sheppo }
43651ae08745Sheppo 
43661ae08745Sheppo /*
43671ae08745Sheppo  * Check if major version is supported.
43681ae08745Sheppo  *
43691ae08745Sheppo  * Returns 0 if finds supported major number, and if necessary
43701ae08745Sheppo  * adjusts the minor field.
43711ae08745Sheppo  *
43721ae08745Sheppo  * Returns 1 if can't match major number exactly. Sets mjor/minor
43731ae08745Sheppo  * to next lowest support values, or to zero if no other values possible.
43741ae08745Sheppo  */
43751ae08745Sheppo static int
43761ae08745Sheppo vsw_supported_version(vio_ver_msg_t *vp)
43771ae08745Sheppo {
43781ae08745Sheppo 	int	i;
43791ae08745Sheppo 
43801ae08745Sheppo 	D1(NULL, "vsw_supported_version: enter");
43811ae08745Sheppo 
43821ae08745Sheppo 	for (i = 0; i < VSW_NUM_VER; i++) {
43831ae08745Sheppo 		if (vsw_versions[i].ver_major == vp->ver_major) {
43841ae08745Sheppo 			/*
43851ae08745Sheppo 			 * Matching or lower major version found. Update
43861ae08745Sheppo 			 * minor number if necessary.
43871ae08745Sheppo 			 */
43881ae08745Sheppo 			if (vp->ver_minor > vsw_versions[i].ver_minor) {
43891ae08745Sheppo 				D2(NULL, "%s: adjusting minor value"
43901ae08745Sheppo 					" from %d to %d", __func__,
43911ae08745Sheppo 					vp->ver_minor,
43921ae08745Sheppo 					vsw_versions[i].ver_minor);
43931ae08745Sheppo 				vp->ver_minor = vsw_versions[i].ver_minor;
43941ae08745Sheppo 			}
43951ae08745Sheppo 
43961ae08745Sheppo 			return (0);
43971ae08745Sheppo 		}
43981ae08745Sheppo 
43991ae08745Sheppo 		if (vsw_versions[i].ver_major < vp->ver_major) {
44001ae08745Sheppo 			if (vp->ver_minor > vsw_versions[i].ver_minor) {
44011ae08745Sheppo 				D2(NULL, "%s: adjusting minor value"
44021ae08745Sheppo 					" from %d to %d", __func__,
44031ae08745Sheppo 					vp->ver_minor,
44041ae08745Sheppo 					vsw_versions[i].ver_minor);
44051ae08745Sheppo 				vp->ver_minor = vsw_versions[i].ver_minor;
44061ae08745Sheppo 			}
44071ae08745Sheppo 			return (1);
44081ae08745Sheppo 		}
44091ae08745Sheppo 	}
44101ae08745Sheppo 
44111ae08745Sheppo 	/* No match was possible, zero out fields */
44121ae08745Sheppo 	vp->ver_major = 0;
44131ae08745Sheppo 	vp->ver_minor = 0;
44141ae08745Sheppo 
44151ae08745Sheppo 	D1(NULL, "vsw_supported_version: exit");
44161ae08745Sheppo 
44171ae08745Sheppo 	return (1);
44181ae08745Sheppo }
44191ae08745Sheppo 
44201ae08745Sheppo /*
44211ae08745Sheppo  * Main routine for processing messages received over LDC.
44221ae08745Sheppo  */
44231ae08745Sheppo static void
44241ae08745Sheppo vsw_process_pkt(void *arg)
44251ae08745Sheppo {
44261ae08745Sheppo 	vsw_ldc_t	*ldcp = (vsw_ldc_t  *)arg;
44271ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
44281ae08745Sheppo 	size_t		msglen;
44291ae08745Sheppo 	vio_msg_tag_t	tag;
44301ae08745Sheppo 	def_msg_t	dmsg;
44311ae08745Sheppo 	int 		rv = 0;
44321ae08745Sheppo 
44333af08d82Slm66018 
44341ae08745Sheppo 	D1(vswp, "%s enter: ldcid (%lld)\n", __func__, ldcp->ldc_id);
44351ae08745Sheppo 
44361ae08745Sheppo 	/*
44371ae08745Sheppo 	 * If channel is up read messages until channel is empty.
44381ae08745Sheppo 	 */
44391ae08745Sheppo 	do {
44401ae08745Sheppo 		msglen = sizeof (dmsg);
44411ae08745Sheppo 		rv = ldc_read(ldcp->ldc_handle, (caddr_t)&dmsg, &msglen);
44421ae08745Sheppo 
44431ae08745Sheppo 		if (rv != 0) {
44441ae08745Sheppo 			DERR(vswp, "%s :ldc_read err id(%lld) rv(%d) "
44451ae08745Sheppo 				"len(%d)\n", __func__, ldcp->ldc_id,
44461ae08745Sheppo 							rv, msglen);
44473af08d82Slm66018 		}
44483af08d82Slm66018 
44493af08d82Slm66018 		/* channel has been reset */
44503af08d82Slm66018 		if (rv == ECONNRESET) {
44513af08d82Slm66018 			vsw_handle_reset(ldcp);
44521ae08745Sheppo 			break;
44531ae08745Sheppo 		}
44541ae08745Sheppo 
44551ae08745Sheppo 		if (msglen == 0) {
44561ae08745Sheppo 			D2(vswp, "%s: ldc_read id(%lld) NODATA", __func__,
44571ae08745Sheppo 			ldcp->ldc_id);
44581ae08745Sheppo 			break;
44591ae08745Sheppo 		}
44601ae08745Sheppo 
44611ae08745Sheppo 		D2(vswp, "%s: ldc_read id(%lld): msglen(%d)", __func__,
44621ae08745Sheppo 		    ldcp->ldc_id, msglen);
44631ae08745Sheppo 
44641ae08745Sheppo 		/*
44651ae08745Sheppo 		 * Figure out what sort of packet we have gotten by
44661ae08745Sheppo 		 * examining the msg tag, and then switch it appropriately.
44671ae08745Sheppo 		 */
44681ae08745Sheppo 		bcopy(&dmsg, &tag, sizeof (vio_msg_tag_t));
44691ae08745Sheppo 
44701ae08745Sheppo 		switch (tag.vio_msgtype) {
44711ae08745Sheppo 		case VIO_TYPE_CTRL:
44721ae08745Sheppo 			vsw_dispatch_ctrl_task(ldcp, &dmsg, tag);
44731ae08745Sheppo 			break;
44741ae08745Sheppo 		case VIO_TYPE_DATA:
44751ae08745Sheppo 			vsw_process_data_pkt(ldcp, &dmsg, tag);
44761ae08745Sheppo 			break;
44771ae08745Sheppo 		case VIO_TYPE_ERR:
44781ae08745Sheppo 			vsw_process_err_pkt(ldcp, &dmsg, tag);
44791ae08745Sheppo 			break;
44801ae08745Sheppo 		default:
44811ae08745Sheppo 			DERR(vswp, "%s: Unknown tag(%lx) ", __func__,
44821ae08745Sheppo 				"id(%lx)\n", tag.vio_msgtype, ldcp->ldc_id);
44831ae08745Sheppo 			break;
44841ae08745Sheppo 		}
44851ae08745Sheppo 	} while (msglen);
44861ae08745Sheppo 
44871ae08745Sheppo 	D1(vswp, "%s exit: ldcid (%lld)\n", __func__, ldcp->ldc_id);
44881ae08745Sheppo }
44891ae08745Sheppo 
44901ae08745Sheppo /*
44911ae08745Sheppo  * Dispatch a task to process a VIO control message.
44921ae08745Sheppo  */
44931ae08745Sheppo static void
44941ae08745Sheppo vsw_dispatch_ctrl_task(vsw_ldc_t *ldcp, void *cpkt, vio_msg_tag_t tag)
44951ae08745Sheppo {
44961ae08745Sheppo 	vsw_ctrl_task_t		*ctaskp = NULL;
44971ae08745Sheppo 	vsw_port_t		*port = ldcp->ldc_port;
44981ae08745Sheppo 	vsw_t			*vswp = port->p_vswp;
44991ae08745Sheppo 
45001ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
45011ae08745Sheppo 
45021ae08745Sheppo 	/*
45031ae08745Sheppo 	 * We need to handle RDX ACK messages in-band as once they
45041ae08745Sheppo 	 * are exchanged it is possible that we will get an
45051ae08745Sheppo 	 * immediate (legitimate) data packet.
45061ae08745Sheppo 	 */
45071ae08745Sheppo 	if ((tag.vio_subtype_env == VIO_RDX) &&
45081ae08745Sheppo 		(tag.vio_subtype == VIO_SUBTYPE_ACK)) {
45093af08d82Slm66018 
45101ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_RDX_ACK_RECV))
45111ae08745Sheppo 			return;
45121ae08745Sheppo 
45131ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_RDX_ACK_RECV;
45143af08d82Slm66018 		D2(vswp, "%s (%ld) handling RDX_ACK in place "
45153af08d82Slm66018 			"(ostate 0x%llx : hphase %d)", __func__,
45163af08d82Slm66018 			ldcp->ldc_id, ldcp->lane_out.lstate, ldcp->hphase);
45171ae08745Sheppo 		vsw_next_milestone(ldcp);
45181ae08745Sheppo 		return;
45191ae08745Sheppo 	}
45201ae08745Sheppo 
45211ae08745Sheppo 	ctaskp = kmem_alloc(sizeof (vsw_ctrl_task_t), KM_NOSLEEP);
45221ae08745Sheppo 
45231ae08745Sheppo 	if (ctaskp == NULL) {
45241ae08745Sheppo 		DERR(vswp, "%s: unable to alloc space for ctrl"
45251ae08745Sheppo 			" msg", __func__);
45261ae08745Sheppo 		vsw_restart_handshake(ldcp);
45271ae08745Sheppo 		return;
45281ae08745Sheppo 	}
45291ae08745Sheppo 
45301ae08745Sheppo 	ctaskp->ldcp = ldcp;
45311ae08745Sheppo 	bcopy((def_msg_t *)cpkt, &ctaskp->pktp, sizeof (def_msg_t));
45321ae08745Sheppo 	mutex_enter(&ldcp->hss_lock);
45331ae08745Sheppo 	ctaskp->hss_id = ldcp->hss_id;
45341ae08745Sheppo 	mutex_exit(&ldcp->hss_lock);
45351ae08745Sheppo 
45361ae08745Sheppo 	/*
45371ae08745Sheppo 	 * Dispatch task to processing taskq if port is not in
45381ae08745Sheppo 	 * the process of being detached.
45391ae08745Sheppo 	 */
45401ae08745Sheppo 	mutex_enter(&port->state_lock);
45411ae08745Sheppo 	if (port->state == VSW_PORT_INIT) {
45421ae08745Sheppo 		if ((vswp->taskq_p == NULL) ||
45431ae08745Sheppo 			(ddi_taskq_dispatch(vswp->taskq_p,
45441ae08745Sheppo 			vsw_process_ctrl_pkt, ctaskp, DDI_NOSLEEP)
45451ae08745Sheppo 							!= DDI_SUCCESS)) {
45461ae08745Sheppo 			DERR(vswp, "%s: unable to dispatch task to taskq",
45471ae08745Sheppo 				__func__);
45481ae08745Sheppo 			kmem_free(ctaskp, sizeof (vsw_ctrl_task_t));
45491ae08745Sheppo 			mutex_exit(&port->state_lock);
45501ae08745Sheppo 			vsw_restart_handshake(ldcp);
45511ae08745Sheppo 			return;
45521ae08745Sheppo 		}
45531ae08745Sheppo 	} else {
45541ae08745Sheppo 		DWARN(vswp, "%s: port %d detaching, not dispatching "
45551ae08745Sheppo 			"task", __func__, port->p_instance);
45561ae08745Sheppo 	}
45571ae08745Sheppo 
45581ae08745Sheppo 	mutex_exit(&port->state_lock);
45591ae08745Sheppo 
45601ae08745Sheppo 	D2(vswp, "%s: dispatched task to taskq for chan %d", __func__,
45611ae08745Sheppo 			ldcp->ldc_id);
45621ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
45631ae08745Sheppo }
45641ae08745Sheppo 
45651ae08745Sheppo /*
45661ae08745Sheppo  * Process a VIO ctrl message. Invoked from taskq.
45671ae08745Sheppo  */
45681ae08745Sheppo static void
45691ae08745Sheppo vsw_process_ctrl_pkt(void *arg)
45701ae08745Sheppo {
45711ae08745Sheppo 	vsw_ctrl_task_t	*ctaskp = (vsw_ctrl_task_t *)arg;
45721ae08745Sheppo 	vsw_ldc_t	*ldcp = ctaskp->ldcp;
45731ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
45741ae08745Sheppo 	vio_msg_tag_t	tag;
45751ae08745Sheppo 	uint16_t	env;
45761ae08745Sheppo 
45771ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
45781ae08745Sheppo 
45791ae08745Sheppo 	bcopy(&ctaskp->pktp, &tag, sizeof (vio_msg_tag_t));
45801ae08745Sheppo 	env = tag.vio_subtype_env;
45811ae08745Sheppo 
45821ae08745Sheppo 	/* stale pkt check */
45831ae08745Sheppo 	mutex_enter(&ldcp->hss_lock);
45841ae08745Sheppo 	if (ctaskp->hss_id < ldcp->hss_id) {
45851ae08745Sheppo 		DWARN(vswp, "%s: discarding stale packet belonging to"
45861ae08745Sheppo 			" earlier (%ld) handshake session", __func__,
45871ae08745Sheppo 			ctaskp->hss_id);
45881ae08745Sheppo 		mutex_exit(&ldcp->hss_lock);
45891ae08745Sheppo 		return;
45901ae08745Sheppo 	}
45911ae08745Sheppo 	mutex_exit(&ldcp->hss_lock);
45921ae08745Sheppo 
45931ae08745Sheppo 	/* session id check */
45941ae08745Sheppo 	if (ldcp->session_status & VSW_PEER_SESSION) {
45951ae08745Sheppo 		if (ldcp->peer_session != tag.vio_sid) {
45961ae08745Sheppo 			DERR(vswp, "%s (chan %d): invalid session id (%llx)",
45971ae08745Sheppo 				__func__, ldcp->ldc_id, tag.vio_sid);
45981ae08745Sheppo 			kmem_free(ctaskp, sizeof (vsw_ctrl_task_t));
45991ae08745Sheppo 			vsw_restart_handshake(ldcp);
46001ae08745Sheppo 			return;
46011ae08745Sheppo 		}
46021ae08745Sheppo 	}
46031ae08745Sheppo 
46041ae08745Sheppo 	/*
46051ae08745Sheppo 	 * Switch on vio_subtype envelope, then let lower routines
46061ae08745Sheppo 	 * decide if its an INFO, ACK or NACK packet.
46071ae08745Sheppo 	 */
46081ae08745Sheppo 	switch (env) {
46091ae08745Sheppo 	case VIO_VER_INFO:
46101ae08745Sheppo 		vsw_process_ctrl_ver_pkt(ldcp, &ctaskp->pktp);
46111ae08745Sheppo 		break;
46121ae08745Sheppo 	case VIO_DRING_REG:
46131ae08745Sheppo 		vsw_process_ctrl_dring_reg_pkt(ldcp, &ctaskp->pktp);
46141ae08745Sheppo 		break;
46151ae08745Sheppo 	case VIO_DRING_UNREG:
46161ae08745Sheppo 		vsw_process_ctrl_dring_unreg_pkt(ldcp, &ctaskp->pktp);
46171ae08745Sheppo 		break;
46181ae08745Sheppo 	case VIO_ATTR_INFO:
46191ae08745Sheppo 		vsw_process_ctrl_attr_pkt(ldcp, &ctaskp->pktp);
46201ae08745Sheppo 		break;
46211ae08745Sheppo 	case VNET_MCAST_INFO:
46221ae08745Sheppo 		vsw_process_ctrl_mcst_pkt(ldcp, &ctaskp->pktp);
46231ae08745Sheppo 		break;
46241ae08745Sheppo 	case VIO_RDX:
46251ae08745Sheppo 		vsw_process_ctrl_rdx_pkt(ldcp, &ctaskp->pktp);
46261ae08745Sheppo 		break;
46271ae08745Sheppo 	default:
46281ae08745Sheppo 		DERR(vswp, "%s : unknown vio_subtype_env (%x)\n",
46291ae08745Sheppo 							__func__, env);
46301ae08745Sheppo 	}
46311ae08745Sheppo 
46321ae08745Sheppo 	kmem_free(ctaskp, sizeof (vsw_ctrl_task_t));
46331ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
46341ae08745Sheppo }
46351ae08745Sheppo 
46361ae08745Sheppo /*
46371ae08745Sheppo  * Version negotiation. We can end up here either because our peer
46381ae08745Sheppo  * has responded to a handshake message we have sent it, or our peer
46391ae08745Sheppo  * has initiated a handshake with us. If its the former then can only
46401ae08745Sheppo  * be ACK or NACK, if its the later can only be INFO.
46411ae08745Sheppo  *
46421ae08745Sheppo  * If its an ACK we move to the next stage of the handshake, namely
46431ae08745Sheppo  * attribute exchange. If its a NACK we see if we can specify another
46441ae08745Sheppo  * version, if we can't we stop.
46451ae08745Sheppo  *
46461ae08745Sheppo  * If it is an INFO we reset all params associated with communication
46471ae08745Sheppo  * in that direction over this channel (remember connection is
46481ae08745Sheppo  * essentially 2 independent simplex channels).
46491ae08745Sheppo  */
46501ae08745Sheppo void
46511ae08745Sheppo vsw_process_ctrl_ver_pkt(vsw_ldc_t *ldcp, void *pkt)
46521ae08745Sheppo {
46531ae08745Sheppo 	vio_ver_msg_t	*ver_pkt;
46541ae08745Sheppo 	vsw_t 		*vswp = ldcp->ldc_vswp;
46551ae08745Sheppo 
46561ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
46571ae08745Sheppo 
46581ae08745Sheppo 	/*
46591ae08745Sheppo 	 * We know this is a ctrl/version packet so
46601ae08745Sheppo 	 * cast it into the correct structure.
46611ae08745Sheppo 	 */
46621ae08745Sheppo 	ver_pkt = (vio_ver_msg_t *)pkt;
46631ae08745Sheppo 
46641ae08745Sheppo 	switch (ver_pkt->tag.vio_subtype) {
46651ae08745Sheppo 	case VIO_SUBTYPE_INFO:
46661ae08745Sheppo 		D2(vswp, "vsw_process_ctrl_ver_pkt: VIO_SUBTYPE_INFO\n");
46671ae08745Sheppo 
46681ae08745Sheppo 		/*
46691ae08745Sheppo 		 * Record the session id, which we will use from now
46701ae08745Sheppo 		 * until we see another VER_INFO msg. Even then the
46711ae08745Sheppo 		 * session id in most cases will be unchanged, execpt
46721ae08745Sheppo 		 * if channel was reset.
46731ae08745Sheppo 		 */
46741ae08745Sheppo 		if ((ldcp->session_status & VSW_PEER_SESSION) &&
46751ae08745Sheppo 			(ldcp->peer_session != ver_pkt->tag.vio_sid)) {
46761ae08745Sheppo 			DERR(vswp, "%s: updating session id for chan %lld "
46771ae08745Sheppo 				"from %llx to %llx", __func__, ldcp->ldc_id,
46781ae08745Sheppo 				ldcp->peer_session, ver_pkt->tag.vio_sid);
46791ae08745Sheppo 		}
46801ae08745Sheppo 
46811ae08745Sheppo 		ldcp->peer_session = ver_pkt->tag.vio_sid;
46821ae08745Sheppo 		ldcp->session_status |= VSW_PEER_SESSION;
46831ae08745Sheppo 
46841ae08745Sheppo 		/* Legal message at this time ? */
46851ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_VER_INFO_RECV))
46861ae08745Sheppo 			return;
46871ae08745Sheppo 
46881ae08745Sheppo 		/*
46891ae08745Sheppo 		 * First check the device class. Currently only expect
46901ae08745Sheppo 		 * to be talking to a network device. In the future may
46911ae08745Sheppo 		 * also talk to another switch.
46921ae08745Sheppo 		 */
46931ae08745Sheppo 		if (ver_pkt->dev_class != VDEV_NETWORK) {
46941ae08745Sheppo 			DERR(vswp, "%s: illegal device class %d", __func__,
46951ae08745Sheppo 				ver_pkt->dev_class);
46961ae08745Sheppo 
46971ae08745Sheppo 			ver_pkt->tag.vio_sid = ldcp->local_session;
46981ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
46991ae08745Sheppo 
47001ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)ver_pkt);
47011ae08745Sheppo 
47021ae08745Sheppo 			vsw_send_msg(ldcp, (void *)ver_pkt,
47031ae08745Sheppo 					sizeof (vio_ver_msg_t));
47041ae08745Sheppo 
47051ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_VER_NACK_SENT;
47061ae08745Sheppo 			vsw_next_milestone(ldcp);
47071ae08745Sheppo 			return;
47081ae08745Sheppo 		} else {
47091ae08745Sheppo 			ldcp->dev_class = ver_pkt->dev_class;
47101ae08745Sheppo 		}
47111ae08745Sheppo 
47121ae08745Sheppo 		/*
47131ae08745Sheppo 		 * Now check the version.
47141ae08745Sheppo 		 */
47151ae08745Sheppo 		if (vsw_supported_version(ver_pkt) == 0) {
47161ae08745Sheppo 			/*
47171ae08745Sheppo 			 * Support this major version and possibly
47181ae08745Sheppo 			 * adjusted minor version.
47191ae08745Sheppo 			 */
47201ae08745Sheppo 
47211ae08745Sheppo 			D2(vswp, "%s: accepted ver %d:%d", __func__,
47221ae08745Sheppo 				ver_pkt->ver_major, ver_pkt->ver_minor);
47231ae08745Sheppo 
47241ae08745Sheppo 			/* Store accepted values */
47251ae08745Sheppo 			ldcp->lane_in.ver_major = ver_pkt->ver_major;
47261ae08745Sheppo 			ldcp->lane_in.ver_minor = ver_pkt->ver_minor;
47271ae08745Sheppo 
47281ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
47291ae08745Sheppo 
47301ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_VER_ACK_SENT;
47311ae08745Sheppo 		} else {
47321ae08745Sheppo 			/*
47331ae08745Sheppo 			 * NACK back with the next lower major/minor
47341ae08745Sheppo 			 * pairing we support (if don't suuport any more
47351ae08745Sheppo 			 * versions then they will be set to zero.
47361ae08745Sheppo 			 */
47371ae08745Sheppo 
47381ae08745Sheppo 			D2(vswp, "%s: replying with ver %d:%d", __func__,
47391ae08745Sheppo 				ver_pkt->ver_major, ver_pkt->ver_minor);
47401ae08745Sheppo 
47411ae08745Sheppo 			/* Store updated values */
47421ae08745Sheppo 			ldcp->lane_in.ver_major = ver_pkt->ver_major;
47431ae08745Sheppo 			ldcp->lane_in.ver_minor = ver_pkt->ver_minor;
47441ae08745Sheppo 
47451ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
47461ae08745Sheppo 
47471ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_VER_NACK_SENT;
47481ae08745Sheppo 		}
47491ae08745Sheppo 
47501ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)ver_pkt);
47511ae08745Sheppo 		ver_pkt->tag.vio_sid = ldcp->local_session;
47521ae08745Sheppo 		vsw_send_msg(ldcp, (void *)ver_pkt, sizeof (vio_ver_msg_t));
47531ae08745Sheppo 
47541ae08745Sheppo 		vsw_next_milestone(ldcp);
47551ae08745Sheppo 		break;
47561ae08745Sheppo 
47571ae08745Sheppo 	case VIO_SUBTYPE_ACK:
47581ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK\n", __func__);
47591ae08745Sheppo 
47601ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_VER_ACK_RECV))
47611ae08745Sheppo 			return;
47621ae08745Sheppo 
47631ae08745Sheppo 		/* Store updated values */
47641ae08745Sheppo 		ldcp->lane_in.ver_major = ver_pkt->ver_major;
47651ae08745Sheppo 		ldcp->lane_in.ver_minor = ver_pkt->ver_minor;
47661ae08745Sheppo 
47671ae08745Sheppo 
47681ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_VER_ACK_RECV;
47691ae08745Sheppo 		vsw_next_milestone(ldcp);
47701ae08745Sheppo 
47711ae08745Sheppo 		break;
47721ae08745Sheppo 
47731ae08745Sheppo 	case VIO_SUBTYPE_NACK:
47741ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK\n", __func__);
47751ae08745Sheppo 
47761ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_VER_NACK_RECV))
47771ae08745Sheppo 			return;
47781ae08745Sheppo 
47791ae08745Sheppo 		/*
47801ae08745Sheppo 		 * If our peer sent us a NACK with the ver fields set to
47811ae08745Sheppo 		 * zero then there is nothing more we can do. Otherwise see
47821ae08745Sheppo 		 * if we support either the version suggested, or a lesser
47831ae08745Sheppo 		 * one.
47841ae08745Sheppo 		 */
47851ae08745Sheppo 		if ((ver_pkt->ver_major == 0) && (ver_pkt->ver_minor == 0)) {
47861ae08745Sheppo 			DERR(vswp, "%s: peer unable to negotiate any "
47871ae08745Sheppo 				"further.", __func__);
47881ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_VER_NACK_RECV;
47891ae08745Sheppo 			vsw_next_milestone(ldcp);
47901ae08745Sheppo 			return;
47911ae08745Sheppo 		}
47921ae08745Sheppo 
47931ae08745Sheppo 		/*
47941ae08745Sheppo 		 * Check to see if we support this major version or
47951ae08745Sheppo 		 * a lower one. If we don't then maj/min will be set
47961ae08745Sheppo 		 * to zero.
47971ae08745Sheppo 		 */
47981ae08745Sheppo 		(void) vsw_supported_version(ver_pkt);
47991ae08745Sheppo 		if ((ver_pkt->ver_major == 0) && (ver_pkt->ver_minor == 0)) {
48001ae08745Sheppo 			/* Nothing more we can do */
48011ae08745Sheppo 			DERR(vswp, "%s: version negotiation failed.\n",
48021ae08745Sheppo 								__func__);
48031ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_VER_NACK_RECV;
48041ae08745Sheppo 			vsw_next_milestone(ldcp);
48051ae08745Sheppo 		} else {
48061ae08745Sheppo 			/* found a supported major version */
48071ae08745Sheppo 			ldcp->lane_out.ver_major = ver_pkt->ver_major;
48081ae08745Sheppo 			ldcp->lane_out.ver_minor = ver_pkt->ver_minor;
48091ae08745Sheppo 
48101ae08745Sheppo 			D2(vswp, "%s: resending with updated values (%x, %x)",
48111ae08745Sheppo 				__func__, ver_pkt->ver_major,
48121ae08745Sheppo 				ver_pkt->ver_minor);
48131ae08745Sheppo 
48141ae08745Sheppo 			ldcp->lane_out.lstate |= VSW_VER_INFO_SENT;
48151ae08745Sheppo 			ver_pkt->tag.vio_sid = ldcp->local_session;
48161ae08745Sheppo 			ver_pkt->tag.vio_subtype = VIO_SUBTYPE_INFO;
48171ae08745Sheppo 
48181ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)ver_pkt);
48191ae08745Sheppo 
48201ae08745Sheppo 			vsw_send_msg(ldcp, (void *)ver_pkt,
48211ae08745Sheppo 					sizeof (vio_ver_msg_t));
48221ae08745Sheppo 
48231ae08745Sheppo 			vsw_next_milestone(ldcp);
48241ae08745Sheppo 
48251ae08745Sheppo 		}
48261ae08745Sheppo 		break;
48271ae08745Sheppo 
48281ae08745Sheppo 	default:
48291ae08745Sheppo 		DERR(vswp, "%s: unknown vio_subtype %x\n", __func__,
48301ae08745Sheppo 			ver_pkt->tag.vio_subtype);
48311ae08745Sheppo 	}
48321ae08745Sheppo 
48331ae08745Sheppo 	D1(vswp, "%s(%lld): exit\n", __func__, ldcp->ldc_id);
48341ae08745Sheppo }
48351ae08745Sheppo 
48361ae08745Sheppo /*
48371ae08745Sheppo  * Process an attribute packet. We can end up here either because our peer
48381ae08745Sheppo  * has ACK/NACK'ed back to an earlier ATTR msg we had sent it, or our
48391ae08745Sheppo  * peer has sent us an attribute INFO message
48401ae08745Sheppo  *
48411ae08745Sheppo  * If its an ACK we then move to the next stage of the handshake which
48421ae08745Sheppo  * is to send our descriptor ring info to our peer. If its a NACK then
48431ae08745Sheppo  * there is nothing more we can (currently) do.
48441ae08745Sheppo  *
48451ae08745Sheppo  * If we get a valid/acceptable INFO packet (and we have already negotiated
48461ae08745Sheppo  * a version) we ACK back and set channel state to ATTR_RECV, otherwise we
48471ae08745Sheppo  * NACK back and reset channel state to INACTIV.
48481ae08745Sheppo  *
48491ae08745Sheppo  * FUTURE: in time we will probably negotiate over attributes, but for
48501ae08745Sheppo  * the moment unacceptable attributes are regarded as a fatal error.
48511ae08745Sheppo  *
48521ae08745Sheppo  */
48531ae08745Sheppo void
48541ae08745Sheppo vsw_process_ctrl_attr_pkt(vsw_ldc_t *ldcp, void *pkt)
48551ae08745Sheppo {
48561ae08745Sheppo 	vnet_attr_msg_t		*attr_pkt;
48571ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
48581ae08745Sheppo 	vsw_port_t		*port = ldcp->ldc_port;
48591ae08745Sheppo 	uint64_t		macaddr = 0;
48601ae08745Sheppo 	int			i;
48611ae08745Sheppo 
48621ae08745Sheppo 	D1(vswp, "%s(%lld) enter", __func__, ldcp->ldc_id);
48631ae08745Sheppo 
48641ae08745Sheppo 	/*
48651ae08745Sheppo 	 * We know this is a ctrl/attr packet so
48661ae08745Sheppo 	 * cast it into the correct structure.
48671ae08745Sheppo 	 */
48681ae08745Sheppo 	attr_pkt = (vnet_attr_msg_t *)pkt;
48691ae08745Sheppo 
48701ae08745Sheppo 	switch (attr_pkt->tag.vio_subtype) {
48711ae08745Sheppo 	case VIO_SUBTYPE_INFO:
48721ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
48731ae08745Sheppo 
48741ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_ATTR_INFO_RECV))
48751ae08745Sheppo 			return;
48761ae08745Sheppo 
48771ae08745Sheppo 		/*
48781ae08745Sheppo 		 * If the attributes are unacceptable then we NACK back.
48791ae08745Sheppo 		 */
48801ae08745Sheppo 		if (vsw_check_attr(attr_pkt, ldcp->ldc_port)) {
48811ae08745Sheppo 
48821ae08745Sheppo 			DERR(vswp, "%s (chan %d): invalid attributes",
48831ae08745Sheppo 				__func__, ldcp->ldc_id);
48841ae08745Sheppo 
48851ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
48861ae08745Sheppo 
48871ae08745Sheppo 			attr_pkt->tag.vio_sid = ldcp->local_session;
48881ae08745Sheppo 			attr_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
48891ae08745Sheppo 
48901ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)attr_pkt);
48911ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_ATTR_NACK_SENT;
48921ae08745Sheppo 			vsw_send_msg(ldcp, (void *)attr_pkt,
48931ae08745Sheppo 					sizeof (vnet_attr_msg_t));
48941ae08745Sheppo 
48951ae08745Sheppo 			vsw_next_milestone(ldcp);
48961ae08745Sheppo 			return;
48971ae08745Sheppo 		}
48981ae08745Sheppo 
48991ae08745Sheppo 		/*
49001ae08745Sheppo 		 * Otherwise store attributes for this lane and update
49011ae08745Sheppo 		 * lane state.
49021ae08745Sheppo 		 */
49031ae08745Sheppo 		ldcp->lane_in.mtu = attr_pkt->mtu;
49041ae08745Sheppo 		ldcp->lane_in.addr = attr_pkt->addr;
49051ae08745Sheppo 		ldcp->lane_in.addr_type = attr_pkt->addr_type;
49061ae08745Sheppo 		ldcp->lane_in.xfer_mode = attr_pkt->xfer_mode;
49071ae08745Sheppo 		ldcp->lane_in.ack_freq = attr_pkt->ack_freq;
49081ae08745Sheppo 
49091ae08745Sheppo 		macaddr = ldcp->lane_in.addr;
49101ae08745Sheppo 		for (i = ETHERADDRL - 1; i >= 0; i--) {
49111ae08745Sheppo 			port->p_macaddr.ether_addr_octet[i] = macaddr & 0xFF;
49121ae08745Sheppo 			macaddr >>= 8;
49131ae08745Sheppo 		}
49141ae08745Sheppo 
49151ae08745Sheppo 		/* create the fdb entry for this port/mac address */
49161ae08745Sheppo 		(void) vsw_add_fdb(vswp, port);
49171ae08745Sheppo 
49181ae08745Sheppo 		/* setup device specifc xmit routines */
49191ae08745Sheppo 		mutex_enter(&port->tx_lock);
49201ae08745Sheppo 		if (ldcp->lane_in.xfer_mode == VIO_DRING_MODE) {
49211ae08745Sheppo 			D2(vswp, "%s: mode = VIO_DRING_MODE", __func__);
49221ae08745Sheppo 			port->transmit = vsw_dringsend;
49231ae08745Sheppo 		} else if (ldcp->lane_in.xfer_mode == VIO_DESC_MODE) {
49241ae08745Sheppo 			D2(vswp, "%s: mode = VIO_DESC_MODE", __func__);
49251ae08745Sheppo 			vsw_create_privring(ldcp);
49261ae08745Sheppo 			port->transmit = vsw_descrsend;
49271ae08745Sheppo 		}
49281ae08745Sheppo 		mutex_exit(&port->tx_lock);
49291ae08745Sheppo 
49301ae08745Sheppo 		attr_pkt->tag.vio_sid = ldcp->local_session;
49311ae08745Sheppo 		attr_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
49321ae08745Sheppo 
49331ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)attr_pkt);
49341ae08745Sheppo 
49351ae08745Sheppo 		ldcp->lane_in.lstate |= VSW_ATTR_ACK_SENT;
49361ae08745Sheppo 
49371ae08745Sheppo 		vsw_send_msg(ldcp, (void *)attr_pkt,
49381ae08745Sheppo 					sizeof (vnet_attr_msg_t));
49391ae08745Sheppo 
49401ae08745Sheppo 		vsw_next_milestone(ldcp);
49411ae08745Sheppo 		break;
49421ae08745Sheppo 
49431ae08745Sheppo 	case VIO_SUBTYPE_ACK:
49441ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
49451ae08745Sheppo 
49461ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_ATTR_ACK_RECV))
49471ae08745Sheppo 			return;
49481ae08745Sheppo 
49491ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_ATTR_ACK_RECV;
49501ae08745Sheppo 		vsw_next_milestone(ldcp);
49511ae08745Sheppo 		break;
49521ae08745Sheppo 
49531ae08745Sheppo 	case VIO_SUBTYPE_NACK:
49541ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
49551ae08745Sheppo 
49561ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_ATTR_NACK_RECV))
49571ae08745Sheppo 			return;
49581ae08745Sheppo 
49591ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_ATTR_NACK_RECV;
49601ae08745Sheppo 		vsw_next_milestone(ldcp);
49611ae08745Sheppo 		break;
49621ae08745Sheppo 
49631ae08745Sheppo 	default:
49641ae08745Sheppo 		DERR(vswp, "%s: unknown vio_subtype %x\n", __func__,
49651ae08745Sheppo 			attr_pkt->tag.vio_subtype);
49661ae08745Sheppo 	}
49671ae08745Sheppo 
49681ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
49691ae08745Sheppo }
49701ae08745Sheppo 
49711ae08745Sheppo /*
49721ae08745Sheppo  * Process a dring info packet. We can end up here either because our peer
49731ae08745Sheppo  * has ACK/NACK'ed back to an earlier DRING msg we had sent it, or our
49741ae08745Sheppo  * peer has sent us a dring INFO message.
49751ae08745Sheppo  *
49761ae08745Sheppo  * If we get a valid/acceptable INFO packet (and we have already negotiated
49771ae08745Sheppo  * a version) we ACK back and update the lane state, otherwise we NACK back.
49781ae08745Sheppo  *
49791ae08745Sheppo  * FUTURE: nothing to stop client from sending us info on multiple dring's
49801ae08745Sheppo  * but for the moment we will just use the first one we are given.
49811ae08745Sheppo  *
49821ae08745Sheppo  */
49831ae08745Sheppo void
49841ae08745Sheppo vsw_process_ctrl_dring_reg_pkt(vsw_ldc_t *ldcp, void *pkt)
49851ae08745Sheppo {
49861ae08745Sheppo 	vio_dring_reg_msg_t	*dring_pkt;
49871ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
49881ae08745Sheppo 	ldc_mem_info_t		minfo;
49891ae08745Sheppo 	dring_info_t		*dp, *dbp;
49901ae08745Sheppo 	int			dring_found = 0;
49911ae08745Sheppo 
49921ae08745Sheppo 	/*
49931ae08745Sheppo 	 * We know this is a ctrl/dring packet so
49941ae08745Sheppo 	 * cast it into the correct structure.
49951ae08745Sheppo 	 */
49961ae08745Sheppo 	dring_pkt = (vio_dring_reg_msg_t *)pkt;
49971ae08745Sheppo 
49981ae08745Sheppo 	D1(vswp, "%s(%lld) enter", __func__, ldcp->ldc_id);
49991ae08745Sheppo 
50001ae08745Sheppo 	switch (dring_pkt->tag.vio_subtype) {
50011ae08745Sheppo 	case VIO_SUBTYPE_INFO:
50021ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
50031ae08745Sheppo 
50041ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_DRING_INFO_RECV))
50051ae08745Sheppo 			return;
50061ae08745Sheppo 
50071ae08745Sheppo 		/*
50081ae08745Sheppo 		 * If the dring params are unacceptable then we NACK back.
50091ae08745Sheppo 		 */
50101ae08745Sheppo 		if (vsw_check_dring_info(dring_pkt)) {
50111ae08745Sheppo 
50121ae08745Sheppo 			DERR(vswp, "%s (%lld): invalid dring info",
50131ae08745Sheppo 				__func__, ldcp->ldc_id);
50141ae08745Sheppo 
50151ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
50161ae08745Sheppo 
50171ae08745Sheppo 			dring_pkt->tag.vio_sid = ldcp->local_session;
50181ae08745Sheppo 			dring_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
50191ae08745Sheppo 
50201ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)dring_pkt);
50211ae08745Sheppo 
50221ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_DRING_NACK_SENT;
50231ae08745Sheppo 
50241ae08745Sheppo 			vsw_send_msg(ldcp, (void *)dring_pkt,
50251ae08745Sheppo 					sizeof (vio_dring_reg_msg_t));
50261ae08745Sheppo 
50271ae08745Sheppo 			vsw_next_milestone(ldcp);
50281ae08745Sheppo 			return;
50291ae08745Sheppo 		}
50301ae08745Sheppo 
50311ae08745Sheppo 		/*
50321ae08745Sheppo 		 * Otherwise, attempt to map in the dring using the
50331ae08745Sheppo 		 * cookie. If that succeeds we send back a unique dring
50341ae08745Sheppo 		 * identifier that the sending side will use in future
50351ae08745Sheppo 		 * to refer to this descriptor ring.
50361ae08745Sheppo 		 */
50371ae08745Sheppo 		dp = kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
50381ae08745Sheppo 
50391ae08745Sheppo 		dp->num_descriptors = dring_pkt->num_descriptors;
50401ae08745Sheppo 		dp->descriptor_size = dring_pkt->descriptor_size;
50411ae08745Sheppo 		dp->options = dring_pkt->options;
50421ae08745Sheppo 		dp->ncookies = dring_pkt->ncookies;
50431ae08745Sheppo 
50441ae08745Sheppo 		/*
50451ae08745Sheppo 		 * Note: should only get one cookie. Enforced in
50461ae08745Sheppo 		 * the ldc layer.
50471ae08745Sheppo 		 */
50481ae08745Sheppo 		bcopy(&dring_pkt->cookie[0], &dp->cookie[0],
50491ae08745Sheppo 			sizeof (ldc_mem_cookie_t));
50501ae08745Sheppo 
50511ae08745Sheppo 		D2(vswp, "%s: num_desc %ld : desc_size %ld", __func__,
50521ae08745Sheppo 			dp->num_descriptors, dp->descriptor_size);
50531ae08745Sheppo 		D2(vswp, "%s: options 0x%lx: ncookies %ld", __func__,
50541ae08745Sheppo 			dp->options, dp->ncookies);
50551ae08745Sheppo 
50561ae08745Sheppo 		if ((ldc_mem_dring_map(ldcp->ldc_handle, &dp->cookie[0],
50571ae08745Sheppo 			dp->ncookies, dp->num_descriptors,
50581ae08745Sheppo 			dp->descriptor_size, LDC_SHADOW_MAP,
50591ae08745Sheppo 			&(dp->handle))) != 0) {
50601ae08745Sheppo 
50611ae08745Sheppo 			DERR(vswp, "%s: dring_map failed\n", __func__);
50621ae08745Sheppo 
50631ae08745Sheppo 			kmem_free(dp, sizeof (dring_info_t));
50641ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
50651ae08745Sheppo 
50661ae08745Sheppo 			dring_pkt->tag.vio_sid = ldcp->local_session;
50671ae08745Sheppo 			dring_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
50681ae08745Sheppo 
50691ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)dring_pkt);
50701ae08745Sheppo 
50711ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_DRING_NACK_SENT;
50721ae08745Sheppo 			vsw_send_msg(ldcp, (void *)dring_pkt,
50731ae08745Sheppo 				sizeof (vio_dring_reg_msg_t));
50741ae08745Sheppo 
50751ae08745Sheppo 			vsw_next_milestone(ldcp);
50761ae08745Sheppo 			return;
50771ae08745Sheppo 		}
50781ae08745Sheppo 
50791ae08745Sheppo 		if ((ldc_mem_dring_info(dp->handle, &minfo)) != 0) {
50801ae08745Sheppo 
50811ae08745Sheppo 			DERR(vswp, "%s: dring_addr failed\n", __func__);
50821ae08745Sheppo 
50831ae08745Sheppo 			kmem_free(dp, sizeof (dring_info_t));
50841ae08745Sheppo 			vsw_free_lane_resources(ldcp, INBOUND);
50851ae08745Sheppo 
50861ae08745Sheppo 			dring_pkt->tag.vio_sid = ldcp->local_session;
50871ae08745Sheppo 			dring_pkt->tag.vio_subtype = VIO_SUBTYPE_NACK;
50881ae08745Sheppo 
50891ae08745Sheppo 			DUMP_TAG_PTR((vio_msg_tag_t *)dring_pkt);
50901ae08745Sheppo 
50911ae08745Sheppo 			ldcp->lane_in.lstate |= VSW_DRING_NACK_SENT;
50921ae08745Sheppo 			vsw_send_msg(ldcp, (void *)dring_pkt,
50931ae08745Sheppo 				sizeof (vio_dring_reg_msg_t));
50941ae08745Sheppo 
50951ae08745Sheppo 			vsw_next_milestone(ldcp);
50961ae08745Sheppo 			return;
50971ae08745Sheppo 		} else {
50981ae08745Sheppo 			/* store the address of the pub part of ring */
50991ae08745Sheppo 			dp->pub_addr = minfo.vaddr;
51001ae08745Sheppo 		}
51011ae08745Sheppo 
51021ae08745Sheppo 		/* no private section as we are importing */
51031ae08745Sheppo 		dp->priv_addr = NULL;
51041ae08745Sheppo 
51051ae08745Sheppo 		/*
51061ae08745Sheppo 		 * Using simple mono increasing int for ident at
51071ae08745Sheppo 		 * the moment.
51081ae08745Sheppo 		 */
51091ae08745Sheppo 		dp->ident = ldcp->next_ident;
51101ae08745Sheppo 		ldcp->next_ident++;
51111ae08745Sheppo 
51121ae08745Sheppo 		dp->end_idx = 0;
51131ae08745Sheppo 		dp->next = NULL;
51141ae08745Sheppo 
51151ae08745Sheppo 		/*
51161ae08745Sheppo 		 * Link it onto the end of the list of drings
51171ae08745Sheppo 		 * for this lane.
51181ae08745Sheppo 		 */
51191ae08745Sheppo 		if (ldcp->lane_in.dringp == NULL) {
51201ae08745Sheppo 			D2(vswp, "%s: adding first INBOUND dring", __func__);
51211ae08745Sheppo 			ldcp->lane_in.dringp = dp;
51221ae08745Sheppo 		} else {
51231ae08745Sheppo 			dbp = ldcp->lane_in.dringp;
51241ae08745Sheppo 
51251ae08745Sheppo 			while (dbp->next != NULL)
51261ae08745Sheppo 				dbp = dbp->next;
51271ae08745Sheppo 
51281ae08745Sheppo 			dbp->next = dp;
51291ae08745Sheppo 		}
51301ae08745Sheppo 
51311ae08745Sheppo 		/* acknowledge it */
51321ae08745Sheppo 		dring_pkt->tag.vio_sid = ldcp->local_session;
51331ae08745Sheppo 		dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
51341ae08745Sheppo 		dring_pkt->dring_ident = dp->ident;
51351ae08745Sheppo 
51361ae08745Sheppo 		vsw_send_msg(ldcp, (void *)dring_pkt,
51371ae08745Sheppo 				sizeof (vio_dring_reg_msg_t));
51381ae08745Sheppo 
51391ae08745Sheppo 		ldcp->lane_in.lstate |= VSW_DRING_ACK_SENT;
51401ae08745Sheppo 		vsw_next_milestone(ldcp);
51411ae08745Sheppo 		break;
51421ae08745Sheppo 
51431ae08745Sheppo 	case VIO_SUBTYPE_ACK:
51441ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
51451ae08745Sheppo 
51461ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_DRING_ACK_RECV))
51471ae08745Sheppo 			return;
51481ae08745Sheppo 
51491ae08745Sheppo 		/*
51501ae08745Sheppo 		 * Peer is acknowledging our dring info and will have
51511ae08745Sheppo 		 * sent us a dring identifier which we will use to
51521ae08745Sheppo 		 * refer to this ring w.r.t. our peer.
51531ae08745Sheppo 		 */
51541ae08745Sheppo 		dp = ldcp->lane_out.dringp;
51551ae08745Sheppo 		if (dp != NULL) {
51561ae08745Sheppo 			/*
51571ae08745Sheppo 			 * Find the ring this ident should be associated
51581ae08745Sheppo 			 * with.
51591ae08745Sheppo 			 */
51601ae08745Sheppo 			if (vsw_dring_match(dp, dring_pkt)) {
51611ae08745Sheppo 				dring_found = 1;
51621ae08745Sheppo 
51631ae08745Sheppo 			} else while (dp != NULL) {
51641ae08745Sheppo 				if (vsw_dring_match(dp, dring_pkt)) {
51651ae08745Sheppo 					dring_found = 1;
51661ae08745Sheppo 					break;
51671ae08745Sheppo 				}
51681ae08745Sheppo 				dp = dp->next;
51691ae08745Sheppo 			}
51701ae08745Sheppo 
51711ae08745Sheppo 			if (dring_found == 0) {
51721ae08745Sheppo 				DERR(NULL, "%s: unrecognised ring cookie",
51731ae08745Sheppo 					__func__);
51741ae08745Sheppo 				vsw_restart_handshake(ldcp);
51751ae08745Sheppo 				return;
51761ae08745Sheppo 			}
51771ae08745Sheppo 
51781ae08745Sheppo 		} else {
51791ae08745Sheppo 			DERR(vswp, "%s: DRING ACK received but no drings "
51801ae08745Sheppo 				"allocated", __func__);
51811ae08745Sheppo 			vsw_restart_handshake(ldcp);
51821ae08745Sheppo 			return;
51831ae08745Sheppo 		}
51841ae08745Sheppo 
51851ae08745Sheppo 		/* store ident */
51861ae08745Sheppo 		dp->ident = dring_pkt->dring_ident;
51871ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_DRING_ACK_RECV;
51881ae08745Sheppo 		vsw_next_milestone(ldcp);
51891ae08745Sheppo 		break;
51901ae08745Sheppo 
51911ae08745Sheppo 	case VIO_SUBTYPE_NACK:
51921ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
51931ae08745Sheppo 
51941ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_DRING_NACK_RECV))
51951ae08745Sheppo 			return;
51961ae08745Sheppo 
51971ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_DRING_NACK_RECV;
51981ae08745Sheppo 		vsw_next_milestone(ldcp);
51991ae08745Sheppo 		break;
52001ae08745Sheppo 
52011ae08745Sheppo 	default:
52021ae08745Sheppo 		DERR(vswp, "%s: Unknown vio_subtype %x\n", __func__,
52031ae08745Sheppo 			dring_pkt->tag.vio_subtype);
52041ae08745Sheppo 	}
52051ae08745Sheppo 
52061ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
52071ae08745Sheppo }
52081ae08745Sheppo 
52091ae08745Sheppo /*
52101ae08745Sheppo  * Process a request from peer to unregister a dring.
52111ae08745Sheppo  *
52121ae08745Sheppo  * For the moment we just restart the handshake if our
52131ae08745Sheppo  * peer endpoint attempts to unregister a dring.
52141ae08745Sheppo  */
52151ae08745Sheppo void
52161ae08745Sheppo vsw_process_ctrl_dring_unreg_pkt(vsw_ldc_t *ldcp, void *pkt)
52171ae08745Sheppo {
52181ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
52191ae08745Sheppo 	vio_dring_unreg_msg_t	*dring_pkt;
52201ae08745Sheppo 
52211ae08745Sheppo 	/*
52221ae08745Sheppo 	 * We know this is a ctrl/dring packet so
52231ae08745Sheppo 	 * cast it into the correct structure.
52241ae08745Sheppo 	 */
52251ae08745Sheppo 	dring_pkt = (vio_dring_unreg_msg_t *)pkt;
52261ae08745Sheppo 
52271ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
52281ae08745Sheppo 
52291ae08745Sheppo 	switch (dring_pkt->tag.vio_subtype) {
52301ae08745Sheppo 	case VIO_SUBTYPE_INFO:
52311ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
52321ae08745Sheppo 
52331ae08745Sheppo 		DWARN(vswp, "%s: restarting handshake..", __func__);
52341ae08745Sheppo 		vsw_restart_handshake(ldcp);
52351ae08745Sheppo 		break;
52361ae08745Sheppo 
52371ae08745Sheppo 	case VIO_SUBTYPE_ACK:
52381ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
52391ae08745Sheppo 
52401ae08745Sheppo 		DWARN(vswp, "%s: restarting handshake..", __func__);
52411ae08745Sheppo 		vsw_restart_handshake(ldcp);
52421ae08745Sheppo 		break;
52431ae08745Sheppo 
52441ae08745Sheppo 	case VIO_SUBTYPE_NACK:
52451ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
52461ae08745Sheppo 
52471ae08745Sheppo 		DWARN(vswp, "%s: restarting handshake..", __func__);
52481ae08745Sheppo 		vsw_restart_handshake(ldcp);
52491ae08745Sheppo 		break;
52501ae08745Sheppo 
52511ae08745Sheppo 	default:
52521ae08745Sheppo 		DERR(vswp, "%s: Unknown vio_subtype %x\n", __func__,
52531ae08745Sheppo 			dring_pkt->tag.vio_subtype);
52541ae08745Sheppo 		vsw_restart_handshake(ldcp);
52551ae08745Sheppo 	}
52561ae08745Sheppo 
52571ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
52581ae08745Sheppo }
52591ae08745Sheppo 
52601ae08745Sheppo #define	SND_MCST_NACK(ldcp, pkt) \
52611ae08745Sheppo 	pkt->tag.vio_subtype = VIO_SUBTYPE_NACK; \
52621ae08745Sheppo 	pkt->tag.vio_sid = ldcp->local_session; \
52631ae08745Sheppo 	vsw_send_msg(ldcp, (void *)pkt, sizeof (vnet_mcast_msg_t));
52641ae08745Sheppo 
52651ae08745Sheppo /*
52661ae08745Sheppo  * Process a multicast request from a vnet.
52671ae08745Sheppo  *
52681ae08745Sheppo  * Vnet's specify a multicast address that they are interested in. This
52691ae08745Sheppo  * address is used as a key into the hash table which forms the multicast
52701ae08745Sheppo  * forwarding database (mFDB).
52711ae08745Sheppo  *
52721ae08745Sheppo  * The table keys are the multicast addresses, while the table entries
52731ae08745Sheppo  * are pointers to lists of ports which wish to receive packets for the
52741ae08745Sheppo  * specified multicast address.
52751ae08745Sheppo  *
52761ae08745Sheppo  * When a multicast packet is being switched we use the address as a key
52771ae08745Sheppo  * into the hash table, and then walk the appropriate port list forwarding
52781ae08745Sheppo  * the pkt to each port in turn.
52791ae08745Sheppo  *
52801ae08745Sheppo  * If a vnet is no longer interested in a particular multicast grouping
52811ae08745Sheppo  * we simply find the correct location in the hash table and then delete
52821ae08745Sheppo  * the relevant port from the port list.
52831ae08745Sheppo  *
52841ae08745Sheppo  * To deal with the case whereby a port is being deleted without first
52851ae08745Sheppo  * removing itself from the lists in the hash table, we maintain a list
52861ae08745Sheppo  * of multicast addresses the port has registered an interest in, within
52871ae08745Sheppo  * the port structure itself. We then simply walk that list of addresses
52881ae08745Sheppo  * using them as keys into the hash table and remove the port from the
52891ae08745Sheppo  * appropriate lists.
52901ae08745Sheppo  */
52911ae08745Sheppo static void
52921ae08745Sheppo vsw_process_ctrl_mcst_pkt(vsw_ldc_t *ldcp, void *pkt)
52931ae08745Sheppo {
52941ae08745Sheppo 	vnet_mcast_msg_t	*mcst_pkt;
52951ae08745Sheppo 	vsw_port_t		*port = ldcp->ldc_port;
52961ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
52971ae08745Sheppo 	int			i;
52981ae08745Sheppo 
52991ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
53001ae08745Sheppo 
53011ae08745Sheppo 	/*
53021ae08745Sheppo 	 * We know this is a ctrl/mcast packet so
53031ae08745Sheppo 	 * cast it into the correct structure.
53041ae08745Sheppo 	 */
53051ae08745Sheppo 	mcst_pkt = (vnet_mcast_msg_t *)pkt;
53061ae08745Sheppo 
53071ae08745Sheppo 	switch (mcst_pkt->tag.vio_subtype) {
53081ae08745Sheppo 	case VIO_SUBTYPE_INFO:
53091ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
53101ae08745Sheppo 
53111ae08745Sheppo 		/*
53121ae08745Sheppo 		 * Check if in correct state to receive a multicast
53131ae08745Sheppo 		 * message (i.e. handshake complete). If not reset
53141ae08745Sheppo 		 * the handshake.
53151ae08745Sheppo 		 */
53161ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_MCST_INFO_RECV))
53171ae08745Sheppo 			return;
53181ae08745Sheppo 
53191ae08745Sheppo 		/*
53201ae08745Sheppo 		 * Before attempting to add or remove address check
53211ae08745Sheppo 		 * that they are valid multicast addresses.
53221ae08745Sheppo 		 * If not, then NACK back.
53231ae08745Sheppo 		 */
53241ae08745Sheppo 		for (i = 0; i < mcst_pkt->count; i++) {
53251ae08745Sheppo 			if ((mcst_pkt->mca[i].ether_addr_octet[0] & 01) != 1) {
53261ae08745Sheppo 				DERR(vswp, "%s: invalid multicast address",
53271ae08745Sheppo 								__func__);
53281ae08745Sheppo 				SND_MCST_NACK(ldcp, mcst_pkt);
53291ae08745Sheppo 				return;
53301ae08745Sheppo 			}
53311ae08745Sheppo 		}
53321ae08745Sheppo 
53331ae08745Sheppo 		/*
53341ae08745Sheppo 		 * Now add/remove the addresses. If this fails we
53351ae08745Sheppo 		 * NACK back.
53361ae08745Sheppo 		 */
53371ae08745Sheppo 		if (vsw_add_rem_mcst(mcst_pkt, port) != 0) {
53381ae08745Sheppo 			SND_MCST_NACK(ldcp, mcst_pkt);
53391ae08745Sheppo 			return;
53401ae08745Sheppo 		}
53411ae08745Sheppo 
53421ae08745Sheppo 		mcst_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
53431ae08745Sheppo 		mcst_pkt->tag.vio_sid = ldcp->local_session;
53441ae08745Sheppo 
53451ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)mcst_pkt);
53461ae08745Sheppo 
53471ae08745Sheppo 		vsw_send_msg(ldcp, (void *)mcst_pkt,
53481ae08745Sheppo 					sizeof (vnet_mcast_msg_t));
53491ae08745Sheppo 		break;
53501ae08745Sheppo 
53511ae08745Sheppo 	case VIO_SUBTYPE_ACK:
53521ae08745Sheppo 		DWARN(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
53531ae08745Sheppo 
53541ae08745Sheppo 		/*
53551ae08745Sheppo 		 * We shouldn't ever get a multicast ACK message as
53561ae08745Sheppo 		 * at the moment we never request multicast addresses
53571ae08745Sheppo 		 * to be set on some other device. This may change in
53581ae08745Sheppo 		 * the future if we have cascading switches.
53591ae08745Sheppo 		 */
53601ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_MCST_ACK_RECV))
53611ae08745Sheppo 			return;
53621ae08745Sheppo 
53631ae08745Sheppo 				/* Do nothing */
53641ae08745Sheppo 		break;
53651ae08745Sheppo 
53661ae08745Sheppo 	case VIO_SUBTYPE_NACK:
53671ae08745Sheppo 		DWARN(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
53681ae08745Sheppo 
53691ae08745Sheppo 		/*
53701ae08745Sheppo 		 * We shouldn't get a multicast NACK packet for the
53711ae08745Sheppo 		 * same reasons as we shouldn't get a ACK packet.
53721ae08745Sheppo 		 */
53731ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_MCST_NACK_RECV))
53741ae08745Sheppo 			return;
53751ae08745Sheppo 
53761ae08745Sheppo 				/* Do nothing */
53771ae08745Sheppo 		break;
53781ae08745Sheppo 
53791ae08745Sheppo 	default:
53801ae08745Sheppo 		DERR(vswp, "%s: unknown vio_subtype %x\n", __func__,
53811ae08745Sheppo 			mcst_pkt->tag.vio_subtype);
53821ae08745Sheppo 	}
53831ae08745Sheppo 
53841ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
53851ae08745Sheppo }
53861ae08745Sheppo 
53871ae08745Sheppo static void
53881ae08745Sheppo vsw_process_ctrl_rdx_pkt(vsw_ldc_t *ldcp, void *pkt)
53891ae08745Sheppo {
53901ae08745Sheppo 	vio_rdx_msg_t	*rdx_pkt;
53911ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
53921ae08745Sheppo 
53931ae08745Sheppo 	/*
53941ae08745Sheppo 	 * We know this is a ctrl/rdx packet so
53951ae08745Sheppo 	 * cast it into the correct structure.
53961ae08745Sheppo 	 */
53971ae08745Sheppo 	rdx_pkt = (vio_rdx_msg_t *)pkt;
53981ae08745Sheppo 
53991ae08745Sheppo 	D1(vswp, "%s(%lld) enter", __func__, ldcp->ldc_id);
54001ae08745Sheppo 
54011ae08745Sheppo 	switch (rdx_pkt->tag.vio_subtype) {
54021ae08745Sheppo 	case VIO_SUBTYPE_INFO:
54031ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
54041ae08745Sheppo 
54051ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_RDX_INFO_RECV))
54061ae08745Sheppo 			return;
54071ae08745Sheppo 
54081ae08745Sheppo 		rdx_pkt->tag.vio_sid = ldcp->local_session;
54091ae08745Sheppo 		rdx_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
54101ae08745Sheppo 
54111ae08745Sheppo 		DUMP_TAG_PTR((vio_msg_tag_t *)rdx_pkt);
54121ae08745Sheppo 
54131ae08745Sheppo 		ldcp->lane_in.lstate |= VSW_RDX_ACK_SENT;
54141ae08745Sheppo 
54151ae08745Sheppo 		vsw_send_msg(ldcp, (void *)rdx_pkt,
54161ae08745Sheppo 				sizeof (vio_rdx_msg_t));
54171ae08745Sheppo 
54181ae08745Sheppo 		vsw_next_milestone(ldcp);
54191ae08745Sheppo 		break;
54201ae08745Sheppo 
54211ae08745Sheppo 	case VIO_SUBTYPE_ACK:
54221ae08745Sheppo 		/*
54231ae08745Sheppo 		 * Should be handled in-band by callback handler.
54241ae08745Sheppo 		 */
54251ae08745Sheppo 		DERR(vswp, "%s: Unexpected VIO_SUBTYPE_ACK", __func__);
54261ae08745Sheppo 		vsw_restart_handshake(ldcp);
54271ae08745Sheppo 		break;
54281ae08745Sheppo 
54291ae08745Sheppo 	case VIO_SUBTYPE_NACK:
54301ae08745Sheppo 		D2(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
54311ae08745Sheppo 
54321ae08745Sheppo 		if (vsw_check_flag(ldcp, OUTBOUND, VSW_RDX_NACK_RECV))
54331ae08745Sheppo 			return;
54341ae08745Sheppo 
54351ae08745Sheppo 		ldcp->lane_out.lstate |= VSW_RDX_NACK_RECV;
54361ae08745Sheppo 		vsw_next_milestone(ldcp);
54371ae08745Sheppo 		break;
54381ae08745Sheppo 
54391ae08745Sheppo 	default:
54401ae08745Sheppo 		DERR(vswp, "%s: Unknown vio_subtype %x\n", __func__,
54411ae08745Sheppo 			rdx_pkt->tag.vio_subtype);
54421ae08745Sheppo 	}
54431ae08745Sheppo 
54441ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
54451ae08745Sheppo }
54461ae08745Sheppo 
54471ae08745Sheppo static void
54481ae08745Sheppo vsw_process_data_pkt(vsw_ldc_t *ldcp, void *dpkt, vio_msg_tag_t tag)
54491ae08745Sheppo {
54501ae08745Sheppo 	uint16_t	env = tag.vio_subtype_env;
54511ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
54521ae08745Sheppo 
54531ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
54541ae08745Sheppo 
54551ae08745Sheppo 	/* session id check */
54561ae08745Sheppo 	if (ldcp->session_status & VSW_PEER_SESSION) {
54571ae08745Sheppo 		if (ldcp->peer_session != tag.vio_sid) {
54581ae08745Sheppo 			DERR(vswp, "%s (chan %d): invalid session id (%llx)",
54591ae08745Sheppo 				__func__, ldcp->ldc_id, tag.vio_sid);
54601ae08745Sheppo 			vsw_restart_handshake(ldcp);
54611ae08745Sheppo 			return;
54621ae08745Sheppo 		}
54631ae08745Sheppo 	}
54641ae08745Sheppo 
54651ae08745Sheppo 	/*
54661ae08745Sheppo 	 * It is an error for us to be getting data packets
54671ae08745Sheppo 	 * before the handshake has completed.
54681ae08745Sheppo 	 */
54691ae08745Sheppo 	if (ldcp->hphase != VSW_MILESTONE4) {
54701ae08745Sheppo 		DERR(vswp, "%s: got data packet before handshake complete "
54711ae08745Sheppo 			"hphase %d (%x: %x)", __func__, ldcp->hphase,
54721ae08745Sheppo 			ldcp->lane_in.lstate, ldcp->lane_out.lstate);
54731ae08745Sheppo 		DUMP_FLAGS(ldcp->lane_in.lstate);
54741ae08745Sheppo 		DUMP_FLAGS(ldcp->lane_out.lstate);
54751ae08745Sheppo 		vsw_restart_handshake(ldcp);
54761ae08745Sheppo 		return;
54771ae08745Sheppo 	}
54781ae08745Sheppo 
54791ae08745Sheppo 	/*
54801ae08745Sheppo 	 * Switch on vio_subtype envelope, then let lower routines
54811ae08745Sheppo 	 * decide if its an INFO, ACK or NACK packet.
54821ae08745Sheppo 	 */
54831ae08745Sheppo 	if (env == VIO_DRING_DATA) {
54841ae08745Sheppo 		vsw_process_data_dring_pkt(ldcp, dpkt);
54851ae08745Sheppo 	} else if (env == VIO_PKT_DATA) {
54861ae08745Sheppo 		vsw_process_data_raw_pkt(ldcp, dpkt);
54871ae08745Sheppo 	} else if (env == VIO_DESC_DATA) {
54881ae08745Sheppo 		vsw_process_data_ibnd_pkt(ldcp, dpkt);
54891ae08745Sheppo 	} else {
54901ae08745Sheppo 		DERR(vswp, "%s : unknown vio_subtype_env (%x)\n",
54911ae08745Sheppo 							__func__, env);
54921ae08745Sheppo 	}
54931ae08745Sheppo 
54941ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
54951ae08745Sheppo }
54961ae08745Sheppo 
54971ae08745Sheppo #define	SND_DRING_NACK(ldcp, pkt) \
54981ae08745Sheppo 	pkt->tag.vio_subtype = VIO_SUBTYPE_NACK; \
54991ae08745Sheppo 	pkt->tag.vio_sid = ldcp->local_session; \
55001ae08745Sheppo 	vsw_send_msg(ldcp, (void *)pkt, sizeof (vio_dring_msg_t));
55011ae08745Sheppo 
55021ae08745Sheppo static void
55031ae08745Sheppo vsw_process_data_dring_pkt(vsw_ldc_t *ldcp, void *dpkt)
55041ae08745Sheppo {
55051ae08745Sheppo 	vio_dring_msg_t		*dring_pkt;
55061ae08745Sheppo 	vnet_public_desc_t	*pub_addr = NULL;
55071ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
55081ae08745Sheppo 	dring_info_t		*dp = NULL;
55091ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
55101ae08745Sheppo 	mblk_t			*mp = NULL;
55111ae08745Sheppo 	mblk_t			*bp = NULL;
55121ae08745Sheppo 	mblk_t			*bpt = NULL;
55131ae08745Sheppo 	size_t			nbytes = 0;
55141ae08745Sheppo 	size_t			off = 0;
55151ae08745Sheppo 	uint64_t		ncookies = 0;
55161ae08745Sheppo 	uint64_t		chain = 0;
5517d10e4ef2Snarayan 	uint64_t		j, len;
5518d10e4ef2Snarayan 	uint32_t		pos, start, datalen;
5519d10e4ef2Snarayan 	uint32_t		range_start, range_end;
5520d10e4ef2Snarayan 	int32_t			end, num, cnt = 0;
5521d10e4ef2Snarayan 	int			i, rv;
55221ae08745Sheppo 	boolean_t		ack_needed = B_FALSE;
5523d10e4ef2Snarayan 	boolean_t		prev_desc_ack = B_FALSE;
5524d10e4ef2Snarayan 	int			read_attempts = 0;
55251ae08745Sheppo 
55261ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
55271ae08745Sheppo 
55281ae08745Sheppo 	/*
55291ae08745Sheppo 	 * We know this is a data/dring packet so
55301ae08745Sheppo 	 * cast it into the correct structure.
55311ae08745Sheppo 	 */
55321ae08745Sheppo 	dring_pkt = (vio_dring_msg_t *)dpkt;
55331ae08745Sheppo 
55341ae08745Sheppo 	/*
55351ae08745Sheppo 	 * Switch on the vio_subtype. If its INFO then we need to
55361ae08745Sheppo 	 * process the data. If its an ACK we need to make sure
55371ae08745Sheppo 	 * it makes sense (i.e did we send an earlier data/info),
55381ae08745Sheppo 	 * and if its a NACK then we maybe attempt a retry.
55391ae08745Sheppo 	 */
55401ae08745Sheppo 	switch (dring_pkt->tag.vio_subtype) {
55411ae08745Sheppo 	case VIO_SUBTYPE_INFO:
55421ae08745Sheppo 		D2(vswp, "%s(%lld): VIO_SUBTYPE_INFO", __func__, ldcp->ldc_id);
55431ae08745Sheppo 
55441ae08745Sheppo 		if ((dp = vsw_ident2dring(&ldcp->lane_in,
55451ae08745Sheppo 				dring_pkt->dring_ident)) == NULL) {
55461ae08745Sheppo 
55471ae08745Sheppo 			DERR(vswp, "%s(%lld): unable to find dring from "
55481ae08745Sheppo 				"ident 0x%llx", __func__, ldcp->ldc_id,
55491ae08745Sheppo 				dring_pkt->dring_ident);
55501ae08745Sheppo 
55511ae08745Sheppo 			SND_DRING_NACK(ldcp, dring_pkt);
55521ae08745Sheppo 			return;
55531ae08745Sheppo 		}
55541ae08745Sheppo 
5555d10e4ef2Snarayan 		start = pos = dring_pkt->start_idx;
55561ae08745Sheppo 		end = dring_pkt->end_idx;
5557d10e4ef2Snarayan 		len = dp->num_descriptors;
55581ae08745Sheppo 
5559d10e4ef2Snarayan 		range_start = range_end = pos;
5560d10e4ef2Snarayan 
5561d10e4ef2Snarayan 		D2(vswp, "%s(%lld): start index %ld : end %ld\n",
55621ae08745Sheppo 			__func__, ldcp->ldc_id, start, end);
55631ae08745Sheppo 
5564d10e4ef2Snarayan 		if (end == -1) {
5565d10e4ef2Snarayan 			num = -1;
55664bac2208Snarayan 		} else if (end >= 0) {
5567d10e4ef2Snarayan 			num = end >= pos ?
5568d10e4ef2Snarayan 				end - pos + 1: (len - pos + 1) + end;
5569d10e4ef2Snarayan 
55701ae08745Sheppo 			/* basic sanity check */
55711ae08745Sheppo 			if (end > len) {
5572d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): endpoint %lld outside "
5573d10e4ef2Snarayan 					"ring length %lld", __func__,
5574d10e4ef2Snarayan 					ldcp->ldc_id, end, len);
55751ae08745Sheppo 
55761ae08745Sheppo 				SND_DRING_NACK(ldcp, dring_pkt);
55771ae08745Sheppo 				return;
55781ae08745Sheppo 			}
5579d10e4ef2Snarayan 		} else {
5580d10e4ef2Snarayan 			DERR(vswp, "%s(%lld): invalid endpoint %lld",
5581d10e4ef2Snarayan 				__func__, ldcp->ldc_id, end);
5582d10e4ef2Snarayan 			SND_DRING_NACK(ldcp, dring_pkt);
55831ae08745Sheppo 			return;
55841ae08745Sheppo 		}
55851ae08745Sheppo 
5586d10e4ef2Snarayan 		while (cnt != num) {
5587d10e4ef2Snarayan vsw_recheck_desc:
5588d10e4ef2Snarayan 			if ((rv = ldc_mem_dring_acquire(dp->handle,
5589d10e4ef2Snarayan 							pos, pos)) != 0) {
5590d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): unable to acquire "
5591d10e4ef2Snarayan 					"descriptor at pos %d: err %d",
5592d10e4ef2Snarayan 					__func__, pos, ldcp->ldc_id, rv);
5593d10e4ef2Snarayan 				SND_DRING_NACK(ldcp, dring_pkt);
5594d10e4ef2Snarayan 				return;
5595d10e4ef2Snarayan 			}
55961ae08745Sheppo 
5597d10e4ef2Snarayan 			pub_addr = (vnet_public_desc_t *)dp->pub_addr + pos;
55981ae08745Sheppo 
5599d10e4ef2Snarayan 			/*
5600d10e4ef2Snarayan 			 * When given a bounded range of descriptors
5601d10e4ef2Snarayan 			 * to process, its an error to hit a descriptor
5602d10e4ef2Snarayan 			 * which is not ready. In the non-bounded case
5603d10e4ef2Snarayan 			 * (end_idx == -1) this simply indicates we have
5604d10e4ef2Snarayan 			 * reached the end of the current active range.
5605d10e4ef2Snarayan 			 */
5606d10e4ef2Snarayan 			if (pub_addr->hdr.dstate != VIO_DESC_READY) {
5607d10e4ef2Snarayan 				/* unbound - no error */
5608d10e4ef2Snarayan 				if (end == -1) {
5609d10e4ef2Snarayan 					if (read_attempts == vsw_read_attempts)
5610d10e4ef2Snarayan 						break;
56111ae08745Sheppo 
5612d10e4ef2Snarayan 					delay(drv_usectohz(vsw_desc_delay));
5613d10e4ef2Snarayan 					read_attempts++;
5614d10e4ef2Snarayan 					goto vsw_recheck_desc;
5615d10e4ef2Snarayan 				}
56161ae08745Sheppo 
5617d10e4ef2Snarayan 				/* bounded - error - so NACK back */
5618d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): descriptor not READY "
5619d10e4ef2Snarayan 					"(%d)", __func__, ldcp->ldc_id,
5620d10e4ef2Snarayan 					pub_addr->hdr.dstate);
5621d10e4ef2Snarayan 				SND_DRING_NACK(ldcp, dring_pkt);
5622d10e4ef2Snarayan 				return;
5623d10e4ef2Snarayan 			}
5624d10e4ef2Snarayan 
5625d10e4ef2Snarayan 			DTRACE_PROBE1(read_attempts, int, read_attempts);
5626d10e4ef2Snarayan 
5627d10e4ef2Snarayan 			range_end = pos;
5628d10e4ef2Snarayan 
5629d10e4ef2Snarayan 			/*
5630d10e4ef2Snarayan 			 * If we ACK'd the previous descriptor then now
5631d10e4ef2Snarayan 			 * record the new range start position for later
5632d10e4ef2Snarayan 			 * ACK's.
5633d10e4ef2Snarayan 			 */
5634d10e4ef2Snarayan 			if (prev_desc_ack) {
5635d10e4ef2Snarayan 				range_start = pos;
5636d10e4ef2Snarayan 
5637d10e4ef2Snarayan 				D2(vswp, "%s(%lld): updating range start "
5638d10e4ef2Snarayan 					"to be %d", __func__, ldcp->ldc_id,
5639d10e4ef2Snarayan 					range_start);
5640d10e4ef2Snarayan 
5641d10e4ef2Snarayan 				prev_desc_ack = B_FALSE;
5642d10e4ef2Snarayan 			}
56431ae08745Sheppo 
56441ae08745Sheppo 			/*
56451ae08745Sheppo 			 * Data is padded to align on 8 byte boundary,
56461ae08745Sheppo 			 * datalen is actual data length, i.e. minus that
56471ae08745Sheppo 			 * padding.
56481ae08745Sheppo 			 */
56491ae08745Sheppo 			datalen = pub_addr->nbytes;
56501ae08745Sheppo 
56511ae08745Sheppo 			/*
56521ae08745Sheppo 			 * Does peer wish us to ACK when we have finished
56531ae08745Sheppo 			 * with this descriptor ?
56541ae08745Sheppo 			 */
56551ae08745Sheppo 			if (pub_addr->hdr.ack)
56561ae08745Sheppo 				ack_needed = B_TRUE;
56571ae08745Sheppo 
56581ae08745Sheppo 			D2(vswp, "%s(%lld): processing desc %lld at pos"
56591ae08745Sheppo 				" 0x%llx : dstate 0x%lx : datalen 0x%lx",
5660d10e4ef2Snarayan 				__func__, ldcp->ldc_id, pos, pub_addr,
56611ae08745Sheppo 				pub_addr->hdr.dstate, datalen);
56621ae08745Sheppo 
56631ae08745Sheppo 			/*
56641ae08745Sheppo 			 * Mark that we are starting to process descriptor.
56651ae08745Sheppo 			 */
56661ae08745Sheppo 			pub_addr->hdr.dstate = VIO_DESC_ACCEPTED;
56671ae08745Sheppo 
5668d10e4ef2Snarayan 			mp = vio_allocb(ldcp->rxh);
5669d10e4ef2Snarayan 			if (mp == NULL) {
56701ae08745Sheppo 				/*
5671d10e4ef2Snarayan 				 * No free receive buffers available, so
5672d10e4ef2Snarayan 				 * fallback onto allocb(9F). Make sure that
5673d10e4ef2Snarayan 				 * we get a data buffer which is a multiple
5674d10e4ef2Snarayan 				 * of 8 as this is required by ldc_mem_copy.
56751ae08745Sheppo 				 */
5676d10e4ef2Snarayan 				DTRACE_PROBE(allocb);
5677d10e4ef2Snarayan 				mp = allocb(datalen + VNET_IPALIGN + 8,
5678d10e4ef2Snarayan 								BPRI_MED);
5679d10e4ef2Snarayan 			}
5680d10e4ef2Snarayan 
5681d10e4ef2Snarayan 			/*
5682d10e4ef2Snarayan 			 * Ensure that we ask ldc for an aligned
5683d10e4ef2Snarayan 			 * number of bytes.
5684d10e4ef2Snarayan 			 */
5685d10e4ef2Snarayan 			nbytes = datalen + VNET_IPALIGN;
56861ae08745Sheppo 			if (nbytes & 0x7) {
56871ae08745Sheppo 				off = 8 - (nbytes & 0x7);
56881ae08745Sheppo 				nbytes += off;
56891ae08745Sheppo 			}
56901ae08745Sheppo 
56911ae08745Sheppo 			ncookies = pub_addr->ncookies;
56921ae08745Sheppo 			rv = ldc_mem_copy(ldcp->ldc_handle,
56931ae08745Sheppo 				(caddr_t)mp->b_rptr, 0, &nbytes,
56941ae08745Sheppo 				pub_addr->memcookie, ncookies,
56951ae08745Sheppo 				LDC_COPY_IN);
56961ae08745Sheppo 
56971ae08745Sheppo 			if (rv != 0) {
56981ae08745Sheppo 				DERR(vswp, "%s(%d): unable to copy in "
5699d10e4ef2Snarayan 					"data from %d cookies in desc %d"
5700d10e4ef2Snarayan 					" (rv %d)", __func__, ldcp->ldc_id,
5701d10e4ef2Snarayan 					ncookies, pos, rv);
57021ae08745Sheppo 				freemsg(mp);
5703d10e4ef2Snarayan 
5704d10e4ef2Snarayan 				pub_addr->hdr.dstate = VIO_DESC_DONE;
57051ae08745Sheppo 				(void) ldc_mem_dring_release(dp->handle,
5706d10e4ef2Snarayan 								pos, pos);
5707d10e4ef2Snarayan 				break;
57081ae08745Sheppo 			} else {
57091ae08745Sheppo 				D2(vswp, "%s(%d): copied in %ld bytes"
57101ae08745Sheppo 					" using %d cookies", __func__,
57111ae08745Sheppo 					ldcp->ldc_id, nbytes, ncookies);
57121ae08745Sheppo 			}
57131ae08745Sheppo 
5714d10e4ef2Snarayan 			/* adjust the read pointer to skip over the padding */
5715d10e4ef2Snarayan 			mp->b_rptr += VNET_IPALIGN;
5716d10e4ef2Snarayan 
57171ae08745Sheppo 			/* point to the actual end of data */
57181ae08745Sheppo 			mp->b_wptr = mp->b_rptr + datalen;
57191ae08745Sheppo 
57201ae08745Sheppo 			/* build a chain of received packets */
57211ae08745Sheppo 			if (bp == NULL) {
57221ae08745Sheppo 				/* first pkt */
57231ae08745Sheppo 				bp = mp;
57241ae08745Sheppo 				bp->b_next = bp->b_prev = NULL;
57251ae08745Sheppo 				bpt = bp;
57261ae08745Sheppo 				chain = 1;
57271ae08745Sheppo 			} else {
57281ae08745Sheppo 				mp->b_next = NULL;
57291ae08745Sheppo 				mp->b_prev = bpt;
57301ae08745Sheppo 				bpt->b_next = mp;
57311ae08745Sheppo 				bpt = mp;
57321ae08745Sheppo 				chain++;
57331ae08745Sheppo 			}
57341ae08745Sheppo 
57351ae08745Sheppo 			/* mark we are finished with this descriptor */
57361ae08745Sheppo 			pub_addr->hdr.dstate = VIO_DESC_DONE;
57371ae08745Sheppo 
5738d10e4ef2Snarayan 			(void) ldc_mem_dring_release(dp->handle, pos, pos);
5739d10e4ef2Snarayan 
57401ae08745Sheppo 			/*
5741d10e4ef2Snarayan 			 * Send an ACK back to peer if requested.
57421ae08745Sheppo 			 */
57431ae08745Sheppo 			if (ack_needed) {
57441ae08745Sheppo 				ack_needed = B_FALSE;
57451ae08745Sheppo 
5746d10e4ef2Snarayan 				dring_pkt->start_idx = range_start;
5747d10e4ef2Snarayan 				dring_pkt->end_idx = range_end;
57481ae08745Sheppo 
5749d10e4ef2Snarayan 				DERR(vswp, "%s(%lld): processed %d %d, ACK"
5750d10e4ef2Snarayan 					" requested", __func__, ldcp->ldc_id,
5751d10e4ef2Snarayan 					dring_pkt->start_idx,
5752d10e4ef2Snarayan 					dring_pkt->end_idx);
57531ae08745Sheppo 
5754d10e4ef2Snarayan 				dring_pkt->dring_process_state = VIO_DP_ACTIVE;
57551ae08745Sheppo 				dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
57561ae08745Sheppo 				dring_pkt->tag.vio_sid = ldcp->local_session;
57571ae08745Sheppo 				vsw_send_msg(ldcp, (void *)dring_pkt,
57581ae08745Sheppo 					sizeof (vio_dring_msg_t));
5759d10e4ef2Snarayan 
5760d10e4ef2Snarayan 				prev_desc_ack = B_TRUE;
5761d10e4ef2Snarayan 				range_start = pos;
57621ae08745Sheppo 			}
57631ae08745Sheppo 
5764d10e4ef2Snarayan 			/* next descriptor */
5765d10e4ef2Snarayan 			pos = (pos + 1) % len;
5766d10e4ef2Snarayan 			cnt++;
5767d10e4ef2Snarayan 
5768d10e4ef2Snarayan 			/*
5769d10e4ef2Snarayan 			 * Break out of loop here and stop processing to
5770d10e4ef2Snarayan 			 * allow some other network device (or disk) to
5771d10e4ef2Snarayan 			 * get access to the cpu.
5772d10e4ef2Snarayan 			 */
5773d10e4ef2Snarayan 			/* send the chain of packets to be switched */
5774d10e4ef2Snarayan 			if (chain > vsw_chain_len) {
5775d10e4ef2Snarayan 				D3(vswp, "%s(%lld): switching chain of %d "
5776d10e4ef2Snarayan 					"msgs", __func__, ldcp->ldc_id, chain);
5777*34683adeSsg70180 				vswp->vsw_switch_frame(vswp, bp, VSW_VNETPORT,
5778d10e4ef2Snarayan 							ldcp->ldc_port, NULL);
5779d10e4ef2Snarayan 				bp = NULL;
5780d10e4ef2Snarayan 				break;
57811ae08745Sheppo 			}
57821ae08745Sheppo 		}
57831ae08745Sheppo 
57841ae08745Sheppo 		/* send the chain of packets to be switched */
5785d10e4ef2Snarayan 		if (bp != NULL) {
5786d10e4ef2Snarayan 			D3(vswp, "%s(%lld): switching chain of %d msgs",
5787d10e4ef2Snarayan 					__func__, ldcp->ldc_id, chain);
5788*34683adeSsg70180 			vswp->vsw_switch_frame(vswp, bp, VSW_VNETPORT,
57891ae08745Sheppo 							ldcp->ldc_port, NULL);
5790d10e4ef2Snarayan 		}
57911ae08745Sheppo 
5792d10e4ef2Snarayan 		DTRACE_PROBE1(msg_cnt, int, cnt);
5793d10e4ef2Snarayan 
5794d10e4ef2Snarayan 		/*
5795d10e4ef2Snarayan 		 * We are now finished so ACK back with the state
5796d10e4ef2Snarayan 		 * set to STOPPING so our peer knows we are finished
5797d10e4ef2Snarayan 		 */
5798d10e4ef2Snarayan 		dring_pkt->tag.vio_subtype = VIO_SUBTYPE_ACK;
5799d10e4ef2Snarayan 		dring_pkt->tag.vio_sid = ldcp->local_session;
5800d10e4ef2Snarayan 
5801d10e4ef2Snarayan 		dring_pkt->dring_process_state = VIO_DP_STOPPED;
5802d10e4ef2Snarayan 
5803d10e4ef2Snarayan 		DTRACE_PROBE(stop_process_sent);
5804d10e4ef2Snarayan 
5805d10e4ef2Snarayan 		/*
5806d10e4ef2Snarayan 		 * We have not processed any more descriptors beyond
5807d10e4ef2Snarayan 		 * the last one we ACK'd.
5808d10e4ef2Snarayan 		 */
5809d10e4ef2Snarayan 		if (prev_desc_ack)
5810d10e4ef2Snarayan 			range_start = range_end;
5811d10e4ef2Snarayan 
5812d10e4ef2Snarayan 		dring_pkt->start_idx = range_start;
5813d10e4ef2Snarayan 		dring_pkt->end_idx = range_end;
5814d10e4ef2Snarayan 
5815d10e4ef2Snarayan 		D2(vswp, "%s(%lld) processed : %d : %d, now stopping",
5816d10e4ef2Snarayan 			__func__, ldcp->ldc_id, dring_pkt->start_idx,
5817d10e4ef2Snarayan 			dring_pkt->end_idx);
5818d10e4ef2Snarayan 
5819d10e4ef2Snarayan 		vsw_send_msg(ldcp, (void *)dring_pkt,
5820d10e4ef2Snarayan 					sizeof (vio_dring_msg_t));
58211ae08745Sheppo 		break;
58221ae08745Sheppo 
58231ae08745Sheppo 	case VIO_SUBTYPE_ACK:
58241ae08745Sheppo 		D2(vswp, "%s(%lld): VIO_SUBTYPE_ACK", __func__, ldcp->ldc_id);
58251ae08745Sheppo 		/*
58261ae08745Sheppo 		 * Verify that the relevant descriptors are all
58271ae08745Sheppo 		 * marked as DONE
58281ae08745Sheppo 		 */
58291ae08745Sheppo 		if ((dp = vsw_ident2dring(&ldcp->lane_out,
58301ae08745Sheppo 			dring_pkt->dring_ident)) == NULL) {
58311ae08745Sheppo 			DERR(vswp, "%s: unknown ident in ACK", __func__);
58321ae08745Sheppo 			return;
58331ae08745Sheppo 		}
58341ae08745Sheppo 
58351ae08745Sheppo 		pub_addr = (vnet_public_desc_t *)dp->pub_addr;
58361ae08745Sheppo 		priv_addr = (vsw_private_desc_t *)dp->priv_addr;
58371ae08745Sheppo 
58381ae08745Sheppo 		start = end = 0;
58391ae08745Sheppo 		start = dring_pkt->start_idx;
58401ae08745Sheppo 		end = dring_pkt->end_idx;
58411ae08745Sheppo 		len = dp->num_descriptors;
58421ae08745Sheppo 
58431ae08745Sheppo 		j = num = 0;
58441ae08745Sheppo 		/* calculate # descriptors taking into a/c wrap around */
58451ae08745Sheppo 		num = end >= start ? end - start + 1: (len - start + 1) + end;
58461ae08745Sheppo 
58471ae08745Sheppo 		D2(vswp, "%s(%lld): start index %ld : end %ld : num %ld\n",
58481ae08745Sheppo 			__func__, ldcp->ldc_id, start, end, num);
58491ae08745Sheppo 
5850d10e4ef2Snarayan 		mutex_enter(&dp->dlock);
5851d10e4ef2Snarayan 		dp->last_ack_recv = end;
5852d10e4ef2Snarayan 		mutex_exit(&dp->dlock);
5853d10e4ef2Snarayan 
58541ae08745Sheppo 		for (i = start; j < num; i = (i + 1) % len, j++) {
58551ae08745Sheppo 			pub_addr = (vnet_public_desc_t *)dp->pub_addr + i;
58561ae08745Sheppo 			priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
58571ae08745Sheppo 
5858d10e4ef2Snarayan 			/*
5859d10e4ef2Snarayan 			 * If the last descriptor in a range has the ACK
5860d10e4ef2Snarayan 			 * bit set then we will get two messages from our
5861d10e4ef2Snarayan 			 * peer relating to it. The normal ACK msg and then
5862d10e4ef2Snarayan 			 * a subsequent STOP msg. The first message will have
5863d10e4ef2Snarayan 			 * resulted in the descriptor being reclaimed and
5864d10e4ef2Snarayan 			 * its state set to FREE so when we encounter a non
5865d10e4ef2Snarayan 			 * DONE descriptor we need to check to see if its
5866d10e4ef2Snarayan 			 * because we have just reclaimed it.
5867d10e4ef2Snarayan 			 */
5868d10e4ef2Snarayan 			mutex_enter(&priv_addr->dstate_lock);
5869d10e4ef2Snarayan 			if (pub_addr->hdr.dstate == VIO_DESC_DONE) {
58701ae08745Sheppo 				/* clear all the fields */
58711ae08745Sheppo 				bzero(priv_addr->datap, priv_addr->datalen);
58721ae08745Sheppo 				priv_addr->datalen = 0;
58731ae08745Sheppo 
58741ae08745Sheppo 				pub_addr->hdr.dstate = VIO_DESC_FREE;
58751ae08745Sheppo 				pub_addr->hdr.ack = 0;
5876d10e4ef2Snarayan 
58771ae08745Sheppo 				priv_addr->dstate = VIO_DESC_FREE;
5878d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
58791ae08745Sheppo 
58801ae08745Sheppo 				D3(vswp, "clearing descp %d : pub state "
58811ae08745Sheppo 					"0x%llx : priv state 0x%llx", i,
58821ae08745Sheppo 					pub_addr->hdr.dstate,
58831ae08745Sheppo 					priv_addr->dstate);
5884d10e4ef2Snarayan 
5885d10e4ef2Snarayan 			} else {
5886d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
5887d10e4ef2Snarayan 
5888d10e4ef2Snarayan 				if (dring_pkt->dring_process_state !=
5889d10e4ef2Snarayan 							VIO_DP_STOPPED) {
5890d10e4ef2Snarayan 					DERR(vswp, "%s: descriptor %lld at pos "
5891d10e4ef2Snarayan 						" 0x%llx not DONE (0x%lx)\n",
5892d10e4ef2Snarayan 						__func__, i, pub_addr,
5893d10e4ef2Snarayan 						pub_addr->hdr.dstate);
5894d10e4ef2Snarayan 					return;
5895d10e4ef2Snarayan 				}
58961ae08745Sheppo 			}
58971ae08745Sheppo 		}
58981ae08745Sheppo 
5899d10e4ef2Snarayan 		/*
5900d10e4ef2Snarayan 		 * If our peer is stopping processing descriptors then
5901d10e4ef2Snarayan 		 * we check to make sure it has processed all the descriptors
5902d10e4ef2Snarayan 		 * we have updated. If not then we send it a new message
5903d10e4ef2Snarayan 		 * to prompt it to restart.
5904d10e4ef2Snarayan 		 */
5905d10e4ef2Snarayan 		if (dring_pkt->dring_process_state == VIO_DP_STOPPED) {
5906d10e4ef2Snarayan 			DTRACE_PROBE(stop_process_recv);
5907d10e4ef2Snarayan 			D2(vswp, "%s(%lld): got stopping msg : %d : %d",
5908d10e4ef2Snarayan 				__func__, ldcp->ldc_id, dring_pkt->start_idx,
5909d10e4ef2Snarayan 				dring_pkt->end_idx);
5910d10e4ef2Snarayan 
5911d10e4ef2Snarayan 			/*
5912d10e4ef2Snarayan 			 * Check next descriptor in public section of ring.
5913d10e4ef2Snarayan 			 * If its marked as READY then we need to prompt our
5914d10e4ef2Snarayan 			 * peer to start processing the ring again.
5915d10e4ef2Snarayan 			 */
5916d10e4ef2Snarayan 			i = (end + 1) % len;
5917d10e4ef2Snarayan 			pub_addr = (vnet_public_desc_t *)dp->pub_addr + i;
5918d10e4ef2Snarayan 			priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
5919d10e4ef2Snarayan 
5920d10e4ef2Snarayan 			/*
5921d10e4ef2Snarayan 			 * Hold the restart lock across all of this to
5922d10e4ef2Snarayan 			 * make sure that its not possible for us to
5923d10e4ef2Snarayan 			 * decide that a msg needs to be sent in the future
5924d10e4ef2Snarayan 			 * but the sending code having already checked is
5925d10e4ef2Snarayan 			 * about to exit.
5926d10e4ef2Snarayan 			 */
5927d10e4ef2Snarayan 			mutex_enter(&dp->restart_lock);
5928d10e4ef2Snarayan 			mutex_enter(&priv_addr->dstate_lock);
5929d10e4ef2Snarayan 			if (pub_addr->hdr.dstate == VIO_DESC_READY) {
5930d10e4ef2Snarayan 
5931d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
5932d10e4ef2Snarayan 
5933d10e4ef2Snarayan 				dring_pkt->tag.vio_subtype = VIO_SUBTYPE_INFO;
5934d10e4ef2Snarayan 				dring_pkt->tag.vio_sid = ldcp->local_session;
5935d10e4ef2Snarayan 
5936d10e4ef2Snarayan 				mutex_enter(&ldcp->lane_out.seq_lock);
5937d10e4ef2Snarayan 				dring_pkt->seq_num = ldcp->lane_out.seq_num++;
5938d10e4ef2Snarayan 				mutex_exit(&ldcp->lane_out.seq_lock);
5939d10e4ef2Snarayan 
5940d10e4ef2Snarayan 				dring_pkt->start_idx = (end + 1) % len;
5941d10e4ef2Snarayan 				dring_pkt->end_idx = -1;
5942d10e4ef2Snarayan 
5943d10e4ef2Snarayan 				D2(vswp, "%s(%lld) : sending restart msg:"
5944d10e4ef2Snarayan 					" %d : %d", __func__, ldcp->ldc_id,
5945d10e4ef2Snarayan 					dring_pkt->start_idx,
5946d10e4ef2Snarayan 					dring_pkt->end_idx);
5947d10e4ef2Snarayan 
5948d10e4ef2Snarayan 				vsw_send_msg(ldcp, (void *)dring_pkt,
5949d10e4ef2Snarayan 						sizeof (vio_dring_msg_t));
5950d10e4ef2Snarayan 			} else {
5951d10e4ef2Snarayan 				mutex_exit(&priv_addr->dstate_lock);
5952d10e4ef2Snarayan 				dp->restart_reqd = B_TRUE;
5953d10e4ef2Snarayan 			}
5954d10e4ef2Snarayan 			mutex_exit(&dp->restart_lock);
5955d10e4ef2Snarayan 		}
59561ae08745Sheppo 		break;
59571ae08745Sheppo 
59581ae08745Sheppo 	case VIO_SUBTYPE_NACK:
59591ae08745Sheppo 		DWARN(vswp, "%s(%lld): VIO_SUBTYPE_NACK",
59601ae08745Sheppo 						__func__, ldcp->ldc_id);
59611ae08745Sheppo 		/*
59621ae08745Sheppo 		 * Something is badly wrong if we are getting NACK's
59631ae08745Sheppo 		 * for our data pkts. So reset the channel.
59641ae08745Sheppo 		 */
59651ae08745Sheppo 		vsw_restart_handshake(ldcp);
59661ae08745Sheppo 
59671ae08745Sheppo 		break;
59681ae08745Sheppo 
59691ae08745Sheppo 	default:
59701ae08745Sheppo 		DERR(vswp, "%s(%lld): Unknown vio_subtype %x\n", __func__,
59711ae08745Sheppo 			ldcp->ldc_id, dring_pkt->tag.vio_subtype);
59721ae08745Sheppo 	}
59731ae08745Sheppo 
59741ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
59751ae08745Sheppo }
59761ae08745Sheppo 
59771ae08745Sheppo /*
59781ae08745Sheppo  * VIO_PKT_DATA (a.k.a raw data mode )
59791ae08745Sheppo  *
59801ae08745Sheppo  * Note - currently not supported. Do nothing.
59811ae08745Sheppo  */
59821ae08745Sheppo static void
59831ae08745Sheppo vsw_process_data_raw_pkt(vsw_ldc_t *ldcp, void *dpkt)
59841ae08745Sheppo {
59851ae08745Sheppo 	_NOTE(ARGUNUSED(dpkt))
59861ae08745Sheppo 
59871ae08745Sheppo 	D1(NULL, "%s (%lld): enter\n", __func__, ldcp->ldc_id);
59881ae08745Sheppo 
59891ae08745Sheppo 	DERR(NULL, "%s (%lld): currently  not supported",
59901ae08745Sheppo 						__func__, ldcp->ldc_id);
59911ae08745Sheppo 
59921ae08745Sheppo 	D1(NULL, "%s (%lld): exit\n", __func__, ldcp->ldc_id);
59931ae08745Sheppo }
59941ae08745Sheppo 
59951ae08745Sheppo #define	SND_IBND_DESC_NACK(ldcp, pkt) \
59961ae08745Sheppo 	pkt->tag.vio_subtype = VIO_SUBTYPE_NACK; \
59971ae08745Sheppo 	pkt->tag.vio_sid = ldcp->local_session; \
59981ae08745Sheppo 	vsw_send_msg(ldcp, (void *)pkt, sizeof (vio_ibnd_desc_t));
59991ae08745Sheppo 
60001ae08745Sheppo /*
60011ae08745Sheppo  * Process an in-band descriptor message (most likely from
60021ae08745Sheppo  * OBP).
60031ae08745Sheppo  */
60041ae08745Sheppo static void
60051ae08745Sheppo vsw_process_data_ibnd_pkt(vsw_ldc_t *ldcp, void *pkt)
60061ae08745Sheppo {
60071ae08745Sheppo 	vio_ibnd_desc_t		*ibnd_desc;
60081ae08745Sheppo 	dring_info_t		*dp = NULL;
60091ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
60101ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
60111ae08745Sheppo 	mblk_t			*mp = NULL;
60121ae08745Sheppo 	size_t			nbytes = 0;
60131ae08745Sheppo 	size_t			off = 0;
60141ae08745Sheppo 	uint64_t		idx = 0;
60154bac2208Snarayan 	uint32_t		num = 1, len, datalen = 0;
60161ae08745Sheppo 	uint64_t		ncookies = 0;
60174bac2208Snarayan 	int			i, rv;
60184bac2208Snarayan 	int			j = 0;
60191ae08745Sheppo 
60201ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
60211ae08745Sheppo 
60221ae08745Sheppo 	ibnd_desc = (vio_ibnd_desc_t *)pkt;
60231ae08745Sheppo 
60241ae08745Sheppo 	switch (ibnd_desc->hdr.tag.vio_subtype) {
60251ae08745Sheppo 	case VIO_SUBTYPE_INFO:
60261ae08745Sheppo 		D1(vswp, "%s: VIO_SUBTYPE_INFO", __func__);
60271ae08745Sheppo 
60281ae08745Sheppo 		if (vsw_check_flag(ldcp, INBOUND, VSW_DRING_INFO_RECV))
60291ae08745Sheppo 			return;
60301ae08745Sheppo 
60311ae08745Sheppo 		/*
60321ae08745Sheppo 		 * Data is padded to align on a 8 byte boundary,
60331ae08745Sheppo 		 * nbytes is actual data length, i.e. minus that
60341ae08745Sheppo 		 * padding.
60351ae08745Sheppo 		 */
60361ae08745Sheppo 		datalen = ibnd_desc->nbytes;
60371ae08745Sheppo 
60381ae08745Sheppo 		D2(vswp, "%s(%lld): processing inband desc : "
60391ae08745Sheppo 			": datalen 0x%lx", __func__, ldcp->ldc_id, datalen);
60401ae08745Sheppo 
60411ae08745Sheppo 		ncookies = ibnd_desc->ncookies;
60421ae08745Sheppo 
60431ae08745Sheppo 		/*
60441ae08745Sheppo 		 * allocb(9F) returns an aligned data block. We
60451ae08745Sheppo 		 * need to ensure that we ask ldc for an aligned
60461ae08745Sheppo 		 * number of bytes also.
60471ae08745Sheppo 		 */
60481ae08745Sheppo 		nbytes = datalen;
60491ae08745Sheppo 		if (nbytes & 0x7) {
60501ae08745Sheppo 			off = 8 - (nbytes & 0x7);
60511ae08745Sheppo 			nbytes += off;
60521ae08745Sheppo 		}
60531ae08745Sheppo 
60541ae08745Sheppo 		mp = allocb(datalen, BPRI_MED);
60551ae08745Sheppo 		if (mp == NULL) {
60561ae08745Sheppo 			DERR(vswp, "%s(%lld): allocb failed",
60571ae08745Sheppo 					__func__, ldcp->ldc_id);
60581ae08745Sheppo 			return;
60591ae08745Sheppo 		}
60601ae08745Sheppo 
60611ae08745Sheppo 		rv = ldc_mem_copy(ldcp->ldc_handle, (caddr_t)mp->b_rptr,
60621ae08745Sheppo 			0, &nbytes, ibnd_desc->memcookie, (uint64_t)ncookies,
60631ae08745Sheppo 			LDC_COPY_IN);
60641ae08745Sheppo 
60651ae08745Sheppo 		if (rv != 0) {
60661ae08745Sheppo 			DERR(vswp, "%s(%d): unable to copy in data from "
60671ae08745Sheppo 				"%d cookie(s)", __func__,
60681ae08745Sheppo 				ldcp->ldc_id, ncookies);
60691ae08745Sheppo 			freemsg(mp);
60701ae08745Sheppo 			return;
60711ae08745Sheppo 		} else {
60721ae08745Sheppo 			D2(vswp, "%s(%d): copied in %ld bytes using %d "
60731ae08745Sheppo 				"cookies", __func__, ldcp->ldc_id, nbytes,
60741ae08745Sheppo 				ncookies);
60751ae08745Sheppo 		}
60761ae08745Sheppo 
60771ae08745Sheppo 		/* point to the actual end of data */
60781ae08745Sheppo 		mp->b_wptr = mp->b_rptr + datalen;
60791ae08745Sheppo 
60801ae08745Sheppo 		/*
60811ae08745Sheppo 		 * We ACK back every in-band descriptor message we process
60821ae08745Sheppo 		 */
60831ae08745Sheppo 		ibnd_desc->hdr.tag.vio_subtype = VIO_SUBTYPE_ACK;
60841ae08745Sheppo 		ibnd_desc->hdr.tag.vio_sid = ldcp->local_session;
60851ae08745Sheppo 		vsw_send_msg(ldcp, (void *)ibnd_desc,
60861ae08745Sheppo 				sizeof (vio_ibnd_desc_t));
60871ae08745Sheppo 
60881ae08745Sheppo 		/* send the packet to be switched */
6089*34683adeSsg70180 		vswp->vsw_switch_frame(vswp, mp, VSW_VNETPORT,
60901ae08745Sheppo 					ldcp->ldc_port, NULL);
60911ae08745Sheppo 
60921ae08745Sheppo 		break;
60931ae08745Sheppo 
60941ae08745Sheppo 	case VIO_SUBTYPE_ACK:
60951ae08745Sheppo 		D1(vswp, "%s: VIO_SUBTYPE_ACK", __func__);
60961ae08745Sheppo 
60971ae08745Sheppo 		/* Verify the ACK is valid */
60981ae08745Sheppo 		idx = ibnd_desc->hdr.desc_handle;
60991ae08745Sheppo 
61001ae08745Sheppo 		if (idx >= VSW_RING_NUM_EL) {
6101*34683adeSsg70180 			cmn_err(CE_WARN, "!vsw%d: corrupted ACK received "
6102*34683adeSsg70180 				"(idx %ld)", vswp->instance, idx);
61031ae08745Sheppo 			return;
61041ae08745Sheppo 		}
61051ae08745Sheppo 
61061ae08745Sheppo 		if ((dp = ldcp->lane_out.dringp) == NULL) {
61071ae08745Sheppo 			DERR(vswp, "%s: no dring found", __func__);
61081ae08745Sheppo 			return;
61091ae08745Sheppo 		}
61101ae08745Sheppo 
61114bac2208Snarayan 		len = dp->num_descriptors;
61124bac2208Snarayan 		/*
61134bac2208Snarayan 		 * If the descriptor we are being ACK'ed for is not the
61144bac2208Snarayan 		 * one we expected, then pkts were lost somwhere, either
61154bac2208Snarayan 		 * when we tried to send a msg, or a previous ACK msg from
61164bac2208Snarayan 		 * our peer. In either case we now reclaim the descriptors
61174bac2208Snarayan 		 * in the range from the last ACK we received up to the
61184bac2208Snarayan 		 * current ACK.
61194bac2208Snarayan 		 */
61204bac2208Snarayan 		if (idx != dp->last_ack_recv) {
61214bac2208Snarayan 			DWARN(vswp, "%s: dropped pkts detected, (%ld, %ld)",
61224bac2208Snarayan 				__func__, dp->last_ack_recv, idx);
61234bac2208Snarayan 			num = idx >= dp->last_ack_recv ?
61244bac2208Snarayan 				idx - dp->last_ack_recv + 1:
61254bac2208Snarayan 				(len - dp->last_ack_recv + 1) + idx;
61264bac2208Snarayan 		}
61271ae08745Sheppo 
61281ae08745Sheppo 		/*
61291ae08745Sheppo 		 * When we sent the in-band message to our peer we
61301ae08745Sheppo 		 * marked the copy in our private ring as READY. We now
61311ae08745Sheppo 		 * check that the descriptor we are being ACK'ed for is in
61321ae08745Sheppo 		 * fact READY, i.e. it is one we have shared with our peer.
61334bac2208Snarayan 		 *
61344bac2208Snarayan 		 * If its not we flag an error, but still reset the descr
61354bac2208Snarayan 		 * back to FREE.
61361ae08745Sheppo 		 */
61374bac2208Snarayan 		for (i = dp->last_ack_recv; j < num; i = (i + 1) % len, j++) {
61384bac2208Snarayan 			priv_addr = (vsw_private_desc_t *)dp->priv_addr + i;
6139d10e4ef2Snarayan 			mutex_enter(&priv_addr->dstate_lock);
61401ae08745Sheppo 			if (priv_addr->dstate != VIO_DESC_READY) {
61414bac2208Snarayan 				DERR(vswp, "%s: (%ld) desc at index %ld not "
61424bac2208Snarayan 					"READY (0x%lx)", __func__,
61434bac2208Snarayan 					ldcp->ldc_id, idx, priv_addr->dstate);
61444bac2208Snarayan 				DERR(vswp, "%s: bound %d: ncookies %ld : "
61454bac2208Snarayan 					"datalen %ld", __func__,
61464bac2208Snarayan 					priv_addr->bound, priv_addr->ncookies,
61474bac2208Snarayan 					priv_addr->datalen);
61484bac2208Snarayan 			}
61491ae08745Sheppo 			D2(vswp, "%s: (%lld) freeing descp at %lld", __func__,
61501ae08745Sheppo 				ldcp->ldc_id, idx);
61511ae08745Sheppo 			/* release resources associated with sent msg */
61521ae08745Sheppo 			bzero(priv_addr->datap, priv_addr->datalen);
61531ae08745Sheppo 			priv_addr->datalen = 0;
61541ae08745Sheppo 			priv_addr->dstate = VIO_DESC_FREE;
6155d10e4ef2Snarayan 			mutex_exit(&priv_addr->dstate_lock);
61561ae08745Sheppo 		}
61574bac2208Snarayan 		/* update to next expected value */
61584bac2208Snarayan 		dp->last_ack_recv = (idx + 1) % dp->num_descriptors;
61594bac2208Snarayan 
61601ae08745Sheppo 		break;
61611ae08745Sheppo 
61621ae08745Sheppo 	case VIO_SUBTYPE_NACK:
61631ae08745Sheppo 		DERR(vswp, "%s: VIO_SUBTYPE_NACK", __func__);
61641ae08745Sheppo 
61651ae08745Sheppo 		/*
61661ae08745Sheppo 		 * We should only get a NACK if our peer doesn't like
61671ae08745Sheppo 		 * something about a message we have sent it. If this
61681ae08745Sheppo 		 * happens we just release the resources associated with
61691ae08745Sheppo 		 * the message. (We are relying on higher layers to decide
61701ae08745Sheppo 		 * whether or not to resend.
61711ae08745Sheppo 		 */
61721ae08745Sheppo 
61731ae08745Sheppo 		/* limit check */
61741ae08745Sheppo 		idx = ibnd_desc->hdr.desc_handle;
61751ae08745Sheppo 
61761ae08745Sheppo 		if (idx >= VSW_RING_NUM_EL) {
61771ae08745Sheppo 			DERR(vswp, "%s: corrupted NACK received (idx %lld)",
61781ae08745Sheppo 				__func__, idx);
61791ae08745Sheppo 			return;
61801ae08745Sheppo 		}
61811ae08745Sheppo 
61821ae08745Sheppo 		if ((dp = ldcp->lane_out.dringp) == NULL) {
61831ae08745Sheppo 			DERR(vswp, "%s: no dring found", __func__);
61841ae08745Sheppo 			return;
61851ae08745Sheppo 		}
61861ae08745Sheppo 
61871ae08745Sheppo 		priv_addr = (vsw_private_desc_t *)dp->priv_addr;
61881ae08745Sheppo 
61891ae08745Sheppo 		/* move to correct location in ring */
61901ae08745Sheppo 		priv_addr += idx;
61911ae08745Sheppo 
61921ae08745Sheppo 		/* release resources associated with sent msg */
6193d10e4ef2Snarayan 		mutex_enter(&priv_addr->dstate_lock);
61941ae08745Sheppo 		bzero(priv_addr->datap, priv_addr->datalen);
61951ae08745Sheppo 		priv_addr->datalen = 0;
61961ae08745Sheppo 		priv_addr->dstate = VIO_DESC_FREE;
6197d10e4ef2Snarayan 		mutex_exit(&priv_addr->dstate_lock);
61981ae08745Sheppo 
61991ae08745Sheppo 		break;
62001ae08745Sheppo 
62011ae08745Sheppo 	default:
62021ae08745Sheppo 		DERR(vswp, "%s(%lld): Unknown vio_subtype %x\n", __func__,
62031ae08745Sheppo 			ldcp->ldc_id, ibnd_desc->hdr.tag.vio_subtype);
62041ae08745Sheppo 	}
62051ae08745Sheppo 
62061ae08745Sheppo 	D1(vswp, "%s(%lld) exit", __func__, ldcp->ldc_id);
62071ae08745Sheppo }
62081ae08745Sheppo 
62091ae08745Sheppo static void
62101ae08745Sheppo vsw_process_err_pkt(vsw_ldc_t *ldcp, void *epkt, vio_msg_tag_t tag)
62111ae08745Sheppo {
62121ae08745Sheppo 	_NOTE(ARGUNUSED(epkt))
62131ae08745Sheppo 
62141ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
62151ae08745Sheppo 	uint16_t	env = tag.vio_subtype_env;
62161ae08745Sheppo 
62171ae08745Sheppo 	D1(vswp, "%s (%lld): enter\n", __func__, ldcp->ldc_id);
62181ae08745Sheppo 
62191ae08745Sheppo 	/*
62201ae08745Sheppo 	 * Error vio_subtypes have yet to be defined. So for
62211ae08745Sheppo 	 * the moment we can't do anything.
62221ae08745Sheppo 	 */
62231ae08745Sheppo 	D2(vswp, "%s: (%x) vio_subtype env", __func__, env);
62241ae08745Sheppo 
62251ae08745Sheppo 	D1(vswp, "%s (%lld): exit\n", __func__, ldcp->ldc_id);
62261ae08745Sheppo }
62271ae08745Sheppo 
62281ae08745Sheppo /*
62291ae08745Sheppo  * Switch the given ethernet frame when operating in layer 2 mode.
62301ae08745Sheppo  *
62311ae08745Sheppo  * vswp: pointer to the vsw instance
62321ae08745Sheppo  * mp: pointer to chain of ethernet frame(s) to be switched
62331ae08745Sheppo  * caller: identifies the source of this frame as:
62341ae08745Sheppo  * 		1. VSW_VNETPORT - a vsw port (connected to a vnet).
62351ae08745Sheppo  *		2. VSW_PHYSDEV - the physical ethernet device
62361ae08745Sheppo  *		3. VSW_LOCALDEV - vsw configured as a virtual interface
62371ae08745Sheppo  * arg: argument provided by the caller.
62381ae08745Sheppo  *		1. for VNETPORT - pointer to the corresponding vsw_port_t.
62391ae08745Sheppo  *		2. for PHYSDEV - NULL
62401ae08745Sheppo  *		3. for LOCALDEV - pointer to to this vsw_t(self)
62411ae08745Sheppo  */
62421ae08745Sheppo void
62431ae08745Sheppo vsw_switch_l2_frame(vsw_t *vswp, mblk_t *mp, int caller,
62441ae08745Sheppo 			vsw_port_t *arg, mac_resource_handle_t mrh)
62451ae08745Sheppo {
62461ae08745Sheppo 	struct ether_header	*ehp;
62471ae08745Sheppo 	vsw_port_t		*port = NULL;
62481ae08745Sheppo 	mblk_t			*bp, *ret_m;
62491ae08745Sheppo 	mblk_t			*nmp = NULL;
62501ae08745Sheppo 	vsw_port_list_t		*plist = &vswp->plist;
62511ae08745Sheppo 
62521ae08745Sheppo 	D1(vswp, "%s: enter (caller %d)", __func__, caller);
62531ae08745Sheppo 
62541ae08745Sheppo 	/*
62551ae08745Sheppo 	 * PERF: rather than breaking up the chain here, scan it
62561ae08745Sheppo 	 * to find all mblks heading to same destination and then
62571ae08745Sheppo 	 * pass that sub-chain to the lower transmit functions.
62581ae08745Sheppo 	 */
62591ae08745Sheppo 
62601ae08745Sheppo 	/* process the chain of packets */
62611ae08745Sheppo 	bp = mp;
62621ae08745Sheppo 	while (bp) {
62631ae08745Sheppo 		mp = bp;
62641ae08745Sheppo 		bp = bp->b_next;
62651ae08745Sheppo 		mp->b_next = mp->b_prev = NULL;
62661ae08745Sheppo 		ehp = (struct ether_header *)mp->b_rptr;
62671ae08745Sheppo 
62681ae08745Sheppo 		D2(vswp, "%s: mblk data buffer %lld : actual data size %lld",
62691ae08745Sheppo 			__func__, MBLKSIZE(mp), MBLKL(mp));
62701ae08745Sheppo 
62711ae08745Sheppo 		READ_ENTER(&vswp->if_lockrw);
62721ae08745Sheppo 		if (ether_cmp(&ehp->ether_dhost, &vswp->if_addr) == 0) {
62731ae08745Sheppo 			/*
62741ae08745Sheppo 			 * If destination is VSW_LOCALDEV (vsw as an eth
62751ae08745Sheppo 			 * interface) and if the device is up & running,
62761ae08745Sheppo 			 * send the packet up the stack on this host.
62771ae08745Sheppo 			 * If the virtual interface is down, drop the packet.
62781ae08745Sheppo 			 */
62791ae08745Sheppo 			if (caller != VSW_LOCALDEV) {
62801ae08745Sheppo 				if (vswp->if_state & VSW_IF_UP) {
62811ae08745Sheppo 					RW_EXIT(&vswp->if_lockrw);
6282ba2e4443Sseb 					mac_rx(vswp->if_mh, mrh, mp);
62831ae08745Sheppo 				} else {
62841ae08745Sheppo 					RW_EXIT(&vswp->if_lockrw);
62851ae08745Sheppo 					/* Interface down, drop pkt */
62861ae08745Sheppo 					freemsg(mp);
62871ae08745Sheppo 				}
62881ae08745Sheppo 			} else {
62891ae08745Sheppo 				RW_EXIT(&vswp->if_lockrw);
62901ae08745Sheppo 				freemsg(mp);
62911ae08745Sheppo 			}
62921ae08745Sheppo 			continue;
62931ae08745Sheppo 		}
62941ae08745Sheppo 		RW_EXIT(&vswp->if_lockrw);
62951ae08745Sheppo 
62961ae08745Sheppo 		READ_ENTER(&plist->lockrw);
62971ae08745Sheppo 		port = vsw_lookup_fdb(vswp, ehp);
62981ae08745Sheppo 		if (port) {
62991ae08745Sheppo 			/*
63001ae08745Sheppo 			 * Mark the port as in-use.
63011ae08745Sheppo 			 */
63021ae08745Sheppo 			mutex_enter(&port->ref_lock);
63031ae08745Sheppo 			port->ref_cnt++;
63041ae08745Sheppo 			mutex_exit(&port->ref_lock);
63051ae08745Sheppo 			RW_EXIT(&plist->lockrw);
63061ae08745Sheppo 
63071ae08745Sheppo 			/*
63081ae08745Sheppo 			 * If plumbed and in promisc mode then copy msg
63091ae08745Sheppo 			 * and send up the stack.
63101ae08745Sheppo 			 */
63111ae08745Sheppo 			READ_ENTER(&vswp->if_lockrw);
63121ae08745Sheppo 			if (VSW_U_P(vswp->if_state)) {
63131ae08745Sheppo 				RW_EXIT(&vswp->if_lockrw);
63141ae08745Sheppo 				nmp = copymsg(mp);
63151ae08745Sheppo 				if (nmp)
6316ba2e4443Sseb 					mac_rx(vswp->if_mh, mrh, nmp);
63171ae08745Sheppo 			} else {
63181ae08745Sheppo 				RW_EXIT(&vswp->if_lockrw);
63191ae08745Sheppo 			}
63201ae08745Sheppo 
63211ae08745Sheppo 			/*
63221ae08745Sheppo 			 * If the destination is in FDB, the packet
63231ae08745Sheppo 			 * should be forwarded to the correponding
63241ae08745Sheppo 			 * vsw_port (connected to a vnet device -
63251ae08745Sheppo 			 * VSW_VNETPORT)
63261ae08745Sheppo 			 */
63271ae08745Sheppo 			(void) vsw_portsend(port, mp);
63281ae08745Sheppo 
63291ae08745Sheppo 			/*
63301ae08745Sheppo 			 * Decrement use count in port and check if
63311ae08745Sheppo 			 * should wake delete thread.
63321ae08745Sheppo 			 */
63331ae08745Sheppo 			mutex_enter(&port->ref_lock);
63341ae08745Sheppo 			port->ref_cnt--;
63351ae08745Sheppo 			if (port->ref_cnt == 0)
63361ae08745Sheppo 				cv_signal(&port->ref_cv);
63371ae08745Sheppo 			mutex_exit(&port->ref_lock);
63381ae08745Sheppo 		} else {
63391ae08745Sheppo 			RW_EXIT(&plist->lockrw);
63401ae08745Sheppo 			/*
63411ae08745Sheppo 			 * Destination not in FDB.
63421ae08745Sheppo 			 *
63431ae08745Sheppo 			 * If the destination is broadcast or
63441ae08745Sheppo 			 * multicast forward the packet to all
63451ae08745Sheppo 			 * (VNETPORTs, PHYSDEV, LOCALDEV),
63461ae08745Sheppo 			 * except the caller.
63471ae08745Sheppo 			 */
63481ae08745Sheppo 			if (IS_BROADCAST(ehp)) {
63491ae08745Sheppo 				D3(vswp, "%s: BROADCAST pkt", __func__);
63501ae08745Sheppo 				(void) vsw_forward_all(vswp, mp,
63511ae08745Sheppo 								caller, arg);
63521ae08745Sheppo 			} else if (IS_MULTICAST(ehp)) {
63531ae08745Sheppo 				D3(vswp, "%s: MULTICAST pkt", __func__);
63541ae08745Sheppo 				(void) vsw_forward_grp(vswp, mp,
63551ae08745Sheppo 							caller, arg);
63561ae08745Sheppo 			} else {
63571ae08745Sheppo 				/*
63581ae08745Sheppo 				 * If the destination is unicast, and came
63591ae08745Sheppo 				 * from either a logical network device or
63601ae08745Sheppo 				 * the switch itself when it is plumbed, then
63611ae08745Sheppo 				 * send it out on the physical device and also
63621ae08745Sheppo 				 * up the stack if the logical interface is
63631ae08745Sheppo 				 * in promiscious mode.
63641ae08745Sheppo 				 *
63651ae08745Sheppo 				 * NOTE:  The assumption here is that if we
63661ae08745Sheppo 				 * cannot find the destination in our fdb, its
63671ae08745Sheppo 				 * a unicast address, and came from either a
63681ae08745Sheppo 				 * vnet or down the stack (when plumbed) it
63691ae08745Sheppo 				 * must be destinded for an ethernet device
63701ae08745Sheppo 				 * outside our ldoms.
63711ae08745Sheppo 				 */
63721ae08745Sheppo 				if (caller == VSW_VNETPORT) {
63731ae08745Sheppo 					READ_ENTER(&vswp->if_lockrw);
63741ae08745Sheppo 					if (VSW_U_P(vswp->if_state)) {
63751ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
63761ae08745Sheppo 						nmp = copymsg(mp);
63771ae08745Sheppo 						if (nmp)
6378ba2e4443Sseb 							mac_rx(vswp->if_mh,
63791ae08745Sheppo 								mrh, nmp);
63801ae08745Sheppo 					} else {
63811ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
63821ae08745Sheppo 					}
63831ae08745Sheppo 					if ((ret_m = vsw_tx_msg(vswp, mp))
63841ae08745Sheppo 								!= NULL) {
63851ae08745Sheppo 						DERR(vswp, "%s: drop mblks to "
63861ae08745Sheppo 							"phys dev", __func__);
63871ae08745Sheppo 						freemsg(ret_m);
63881ae08745Sheppo 					}
63891ae08745Sheppo 
63901ae08745Sheppo 				} else if (caller == VSW_PHYSDEV) {
63911ae08745Sheppo 					/*
63921ae08745Sheppo 					 * Pkt seen because card in promisc
63931ae08745Sheppo 					 * mode. Send up stack if plumbed in
63941ae08745Sheppo 					 * promisc mode, else drop it.
63951ae08745Sheppo 					 */
63961ae08745Sheppo 					READ_ENTER(&vswp->if_lockrw);
63971ae08745Sheppo 					if (VSW_U_P(vswp->if_state)) {
63981ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
6399ba2e4443Sseb 						mac_rx(vswp->if_mh, mrh, mp);
64001ae08745Sheppo 					} else {
64011ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
64021ae08745Sheppo 						freemsg(mp);
64031ae08745Sheppo 					}
64041ae08745Sheppo 
64051ae08745Sheppo 				} else if (caller == VSW_LOCALDEV) {
64061ae08745Sheppo 					/*
64071ae08745Sheppo 					 * Pkt came down the stack, send out
64081ae08745Sheppo 					 * over physical device.
64091ae08745Sheppo 					 */
64101ae08745Sheppo 					if ((ret_m = vsw_tx_msg(vswp, mp))
64111ae08745Sheppo 								!= NULL) {
64121ae08745Sheppo 						DERR(vswp, "%s: drop mblks to "
64131ae08745Sheppo 							"phys dev", __func__);
64141ae08745Sheppo 						freemsg(ret_m);
64151ae08745Sheppo 					}
64161ae08745Sheppo 				}
64171ae08745Sheppo 			}
64181ae08745Sheppo 		}
64191ae08745Sheppo 	}
64201ae08745Sheppo 	D1(vswp, "%s: exit\n", __func__);
64211ae08745Sheppo }
64221ae08745Sheppo 
64231ae08745Sheppo /*
64241ae08745Sheppo  * Switch ethernet frame when in layer 3 mode (i.e. using IP
64251ae08745Sheppo  * layer to do the routing).
64261ae08745Sheppo  *
64271ae08745Sheppo  * There is a large amount of overlap between this function and
64281ae08745Sheppo  * vsw_switch_l2_frame. At some stage we need to revisit and refactor
64291ae08745Sheppo  * both these functions.
64301ae08745Sheppo  */
64311ae08745Sheppo void
64321ae08745Sheppo vsw_switch_l3_frame(vsw_t *vswp, mblk_t *mp, int caller,
64331ae08745Sheppo 			vsw_port_t *arg, mac_resource_handle_t mrh)
64341ae08745Sheppo {
64351ae08745Sheppo 	struct ether_header	*ehp;
64361ae08745Sheppo 	vsw_port_t		*port = NULL;
64371ae08745Sheppo 	mblk_t			*bp = NULL;
64381ae08745Sheppo 	vsw_port_list_t		*plist = &vswp->plist;
64391ae08745Sheppo 
64401ae08745Sheppo 	D1(vswp, "%s: enter (caller %d)", __func__, caller);
64411ae08745Sheppo 
64421ae08745Sheppo 	/*
64431ae08745Sheppo 	 * In layer 3 mode should only ever be switching packets
64441ae08745Sheppo 	 * between IP layer and vnet devices. So make sure thats
64451ae08745Sheppo 	 * who is invoking us.
64461ae08745Sheppo 	 */
64471ae08745Sheppo 	if ((caller != VSW_LOCALDEV) && (caller != VSW_VNETPORT)) {
64481ae08745Sheppo 		DERR(vswp, "%s: unexpected caller (%d)", __func__, caller);
64491ae08745Sheppo 		freemsgchain(mp);
64501ae08745Sheppo 		return;
64511ae08745Sheppo 	}
64521ae08745Sheppo 
64531ae08745Sheppo 	/* process the chain of packets */
64541ae08745Sheppo 	bp = mp;
64551ae08745Sheppo 	while (bp) {
64561ae08745Sheppo 		mp = bp;
64571ae08745Sheppo 		bp = bp->b_next;
64581ae08745Sheppo 		mp->b_next = mp->b_prev = NULL;
64591ae08745Sheppo 		ehp = (struct ether_header *)mp->b_rptr;
64601ae08745Sheppo 
64611ae08745Sheppo 		D2(vswp, "%s: mblk data buffer %lld : actual data size %lld",
64621ae08745Sheppo 			__func__, MBLKSIZE(mp), MBLKL(mp));
64631ae08745Sheppo 
64641ae08745Sheppo 		READ_ENTER(&plist->lockrw);
64651ae08745Sheppo 		port = vsw_lookup_fdb(vswp, ehp);
64661ae08745Sheppo 		if (port) {
64671ae08745Sheppo 			/*
64681ae08745Sheppo 			 * Mark port as in-use.
64691ae08745Sheppo 			 */
64701ae08745Sheppo 			mutex_enter(&port->ref_lock);
64711ae08745Sheppo 			port->ref_cnt++;
64721ae08745Sheppo 			mutex_exit(&port->ref_lock);
64731ae08745Sheppo 			RW_EXIT(&plist->lockrw);
64741ae08745Sheppo 
64751ae08745Sheppo 			D2(vswp, "%s: sending to target port", __func__);
64761ae08745Sheppo 			(void) vsw_portsend(port, mp);
64771ae08745Sheppo 
64781ae08745Sheppo 			/*
64791ae08745Sheppo 			 * Finished with port so decrement ref count and
64801ae08745Sheppo 			 * check if should wake delete thread.
64811ae08745Sheppo 			 */
64821ae08745Sheppo 			mutex_enter(&port->ref_lock);
64831ae08745Sheppo 			port->ref_cnt--;
64841ae08745Sheppo 			if (port->ref_cnt == 0)
64851ae08745Sheppo 				cv_signal(&port->ref_cv);
64861ae08745Sheppo 			mutex_exit(&port->ref_lock);
64871ae08745Sheppo 		} else {
64881ae08745Sheppo 			RW_EXIT(&plist->lockrw);
64891ae08745Sheppo 			/*
64901ae08745Sheppo 			 * Destination not in FDB
64911ae08745Sheppo 			 *
64921ae08745Sheppo 			 * If the destination is broadcast or
64931ae08745Sheppo 			 * multicast forward the packet to all
64941ae08745Sheppo 			 * (VNETPORTs, PHYSDEV, LOCALDEV),
64951ae08745Sheppo 			 * except the caller.
64961ae08745Sheppo 			 */
64971ae08745Sheppo 			if (IS_BROADCAST(ehp)) {
64981ae08745Sheppo 				D2(vswp, "%s: BROADCAST pkt", __func__);
64991ae08745Sheppo 				(void) vsw_forward_all(vswp, mp,
65001ae08745Sheppo 								caller, arg);
65011ae08745Sheppo 			} else if (IS_MULTICAST(ehp)) {
65021ae08745Sheppo 				D2(vswp, "%s: MULTICAST pkt", __func__);
65031ae08745Sheppo 				(void) vsw_forward_grp(vswp, mp,
65041ae08745Sheppo 							caller, arg);
65051ae08745Sheppo 			} else {
65061ae08745Sheppo 				/*
65071ae08745Sheppo 				 * Unicast pkt from vnet that we don't have
65081ae08745Sheppo 				 * an FDB entry for, so must be destinded for
65091ae08745Sheppo 				 * the outside world. Attempt to send up to the
65101ae08745Sheppo 				 * IP layer to allow it to deal with it.
65111ae08745Sheppo 				 */
65121ae08745Sheppo 				if (caller == VSW_VNETPORT) {
65131ae08745Sheppo 					READ_ENTER(&vswp->if_lockrw);
65141ae08745Sheppo 					if (vswp->if_state & VSW_IF_UP) {
65151ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
65161ae08745Sheppo 						D2(vswp, "%s: sending up",
65171ae08745Sheppo 							__func__);
6518ba2e4443Sseb 						mac_rx(vswp->if_mh, mrh, mp);
65191ae08745Sheppo 					} else {
65201ae08745Sheppo 						RW_EXIT(&vswp->if_lockrw);
65211ae08745Sheppo 						/* Interface down, drop pkt */
65221ae08745Sheppo 						D2(vswp, "%s I/F down",
65231ae08745Sheppo 								__func__);
65241ae08745Sheppo 						freemsg(mp);
65251ae08745Sheppo 					}
65261ae08745Sheppo 				}
65271ae08745Sheppo 			}
65281ae08745Sheppo 		}
65291ae08745Sheppo 	}
65301ae08745Sheppo 
65311ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
65321ae08745Sheppo }
65331ae08745Sheppo 
65341ae08745Sheppo /*
65351ae08745Sheppo  * Forward the ethernet frame to all ports (VNETPORTs, PHYSDEV, LOCALDEV),
65361ae08745Sheppo  * except the caller (port on which frame arrived).
65371ae08745Sheppo  */
65381ae08745Sheppo static int
65391ae08745Sheppo vsw_forward_all(vsw_t *vswp, mblk_t *mp, int caller, vsw_port_t *arg)
65401ae08745Sheppo {
65411ae08745Sheppo 	vsw_port_list_t	*plist = &vswp->plist;
65421ae08745Sheppo 	vsw_port_t	*portp;
65431ae08745Sheppo 	mblk_t		*nmp = NULL;
65441ae08745Sheppo 	mblk_t		*ret_m = NULL;
65451ae08745Sheppo 	int		skip_port = 0;
65461ae08745Sheppo 
65471ae08745Sheppo 	D1(vswp, "vsw_forward_all: enter\n");
65481ae08745Sheppo 
65491ae08745Sheppo 	/*
65501ae08745Sheppo 	 * Broadcast message from inside ldoms so send to outside
65511ae08745Sheppo 	 * world if in either of layer 2 modes.
65521ae08745Sheppo 	 */
65531ae08745Sheppo 	if (((vswp->smode[vswp->smode_idx] == VSW_LAYER2) ||
65541ae08745Sheppo 		(vswp->smode[vswp->smode_idx] == VSW_LAYER2_PROMISC)) &&
65551ae08745Sheppo 		((caller == VSW_LOCALDEV) || (caller == VSW_VNETPORT))) {
65561ae08745Sheppo 
65571ae08745Sheppo 		nmp = dupmsg(mp);
65581ae08745Sheppo 		if (nmp) {
65591ae08745Sheppo 			if ((ret_m = vsw_tx_msg(vswp, nmp)) != NULL) {
65601ae08745Sheppo 				DERR(vswp, "%s: dropping pkt(s) "
65611ae08745Sheppo 				"consisting of %ld bytes of data for"
65621ae08745Sheppo 				" physical device", __func__, MBLKL(ret_m));
65631ae08745Sheppo 			freemsg(ret_m);
65641ae08745Sheppo 			}
65651ae08745Sheppo 		}
65661ae08745Sheppo 	}
65671ae08745Sheppo 
65681ae08745Sheppo 	if (caller == VSW_VNETPORT)
65691ae08745Sheppo 		skip_port = 1;
65701ae08745Sheppo 
65711ae08745Sheppo 	/*
65721ae08745Sheppo 	 * Broadcast message from other vnet (layer 2 or 3) or outside
65731ae08745Sheppo 	 * world (layer 2 only), send up stack if plumbed.
65741ae08745Sheppo 	 */
65751ae08745Sheppo 	if ((caller == VSW_PHYSDEV) || (caller == VSW_VNETPORT)) {
65761ae08745Sheppo 		READ_ENTER(&vswp->if_lockrw);
65771ae08745Sheppo 		if (vswp->if_state & VSW_IF_UP) {
65781ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
65791ae08745Sheppo 			nmp = copymsg(mp);
65801ae08745Sheppo 			if (nmp)
6581ba2e4443Sseb 				mac_rx(vswp->if_mh, NULL, nmp);
65821ae08745Sheppo 		} else {
65831ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
65841ae08745Sheppo 		}
65851ae08745Sheppo 	}
65861ae08745Sheppo 
65871ae08745Sheppo 	/* send it to all VNETPORTs */
65881ae08745Sheppo 	READ_ENTER(&plist->lockrw);
65891ae08745Sheppo 	for (portp = plist->head; portp != NULL; portp = portp->p_next) {
65901ae08745Sheppo 		D2(vswp, "vsw_forward_all: port %d", portp->p_instance);
65911ae08745Sheppo 		/*
65921ae08745Sheppo 		 * Caution ! - don't reorder these two checks as arg
65931ae08745Sheppo 		 * will be NULL if the caller is PHYSDEV. skip_port is
65941ae08745Sheppo 		 * only set if caller is VNETPORT.
65951ae08745Sheppo 		 */
65961ae08745Sheppo 		if ((skip_port) && (portp == arg))
65971ae08745Sheppo 			continue;
65981ae08745Sheppo 		else {
65991ae08745Sheppo 			nmp = dupmsg(mp);
66001ae08745Sheppo 			if (nmp) {
66011ae08745Sheppo 				(void) vsw_portsend(portp, nmp);
66021ae08745Sheppo 			} else {
66031ae08745Sheppo 				DERR(vswp, "vsw_forward_all: nmp NULL");
66041ae08745Sheppo 			}
66051ae08745Sheppo 		}
66061ae08745Sheppo 	}
66071ae08745Sheppo 	RW_EXIT(&plist->lockrw);
66081ae08745Sheppo 
66091ae08745Sheppo 	freemsg(mp);
66101ae08745Sheppo 
66111ae08745Sheppo 	D1(vswp, "vsw_forward_all: exit\n");
66121ae08745Sheppo 	return (0);
66131ae08745Sheppo }
66141ae08745Sheppo 
66151ae08745Sheppo /*
66161ae08745Sheppo  * Forward pkts to any devices or interfaces which have registered
66171ae08745Sheppo  * an interest in them (i.e. multicast groups).
66181ae08745Sheppo  */
66191ae08745Sheppo static int
66201ae08745Sheppo vsw_forward_grp(vsw_t *vswp, mblk_t *mp, int caller, vsw_port_t *arg)
66211ae08745Sheppo {
66221ae08745Sheppo 	struct ether_header	*ehp = (struct ether_header *)mp->b_rptr;
66231ae08745Sheppo 	mfdb_ent_t		*entp = NULL;
66241ae08745Sheppo 	mfdb_ent_t		*tpp = NULL;
66251ae08745Sheppo 	vsw_port_t 		*port;
66261ae08745Sheppo 	uint64_t		key = 0;
66271ae08745Sheppo 	mblk_t			*nmp = NULL;
66281ae08745Sheppo 	mblk_t			*ret_m = NULL;
66291ae08745Sheppo 	boolean_t		check_if = B_TRUE;
66301ae08745Sheppo 
66311ae08745Sheppo 	/*
66321ae08745Sheppo 	 * Convert address to hash table key
66331ae08745Sheppo 	 */
66341ae08745Sheppo 	KEY_HASH(key, ehp->ether_dhost);
66351ae08745Sheppo 
66361ae08745Sheppo 	D1(vswp, "%s: key 0x%llx", __func__, key);
66371ae08745Sheppo 
66381ae08745Sheppo 	/*
66391ae08745Sheppo 	 * If pkt came from either a vnet or down the stack (if we are
66401ae08745Sheppo 	 * plumbed) and we are in layer 2 mode, then we send the pkt out
66411ae08745Sheppo 	 * over the physical adapter, and then check to see if any other
66421ae08745Sheppo 	 * vnets are interested in it.
66431ae08745Sheppo 	 */
66441ae08745Sheppo 	if (((vswp->smode[vswp->smode_idx] == VSW_LAYER2) ||
66451ae08745Sheppo 		(vswp->smode[vswp->smode_idx] == VSW_LAYER2_PROMISC)) &&
66461ae08745Sheppo 		((caller == VSW_VNETPORT) || (caller == VSW_LOCALDEV))) {
66471ae08745Sheppo 		nmp = dupmsg(mp);
66481ae08745Sheppo 		if (nmp) {
66491ae08745Sheppo 			if ((ret_m = vsw_tx_msg(vswp, nmp)) != NULL) {
66501ae08745Sheppo 				DERR(vswp, "%s: dropping pkt(s) "
66511ae08745Sheppo 					"consisting of %ld bytes of "
66521ae08745Sheppo 					"data for physical device",
66531ae08745Sheppo 					__func__, MBLKL(ret_m));
66541ae08745Sheppo 				freemsg(ret_m);
66551ae08745Sheppo 			}
66561ae08745Sheppo 		}
66571ae08745Sheppo 	}
66581ae08745Sheppo 
66591ae08745Sheppo 	READ_ENTER(&vswp->mfdbrw);
66601ae08745Sheppo 	if (mod_hash_find(vswp->mfdb, (mod_hash_key_t)key,
66611ae08745Sheppo 				(mod_hash_val_t *)&entp) != 0) {
66621ae08745Sheppo 		D3(vswp, "%s: no table entry found for addr 0x%llx",
66631ae08745Sheppo 								__func__, key);
66641ae08745Sheppo 	} else {
66651ae08745Sheppo 		/*
66661ae08745Sheppo 		 * Send to list of devices associated with this address...
66671ae08745Sheppo 		 */
66681ae08745Sheppo 		for (tpp = entp; tpp != NULL; tpp = tpp->nextp) {
66691ae08745Sheppo 
66701ae08745Sheppo 			/* dont send to ourselves */
66711ae08745Sheppo 			if ((caller == VSW_VNETPORT) &&
66721ae08745Sheppo 				(tpp->d_addr == (void *)arg)) {
66731ae08745Sheppo 				port = (vsw_port_t *)tpp->d_addr;
66741ae08745Sheppo 				D3(vswp, "%s: not sending to ourselves"
66751ae08745Sheppo 					" : port %d", __func__,
66761ae08745Sheppo 					port->p_instance);
66771ae08745Sheppo 				continue;
66781ae08745Sheppo 
66791ae08745Sheppo 			} else if ((caller == VSW_LOCALDEV) &&
66801ae08745Sheppo 				(tpp->d_type == VSW_LOCALDEV)) {
66811ae08745Sheppo 				D3(vswp, "%s: not sending back up stack",
66821ae08745Sheppo 					__func__);
66831ae08745Sheppo 				continue;
66841ae08745Sheppo 			}
66851ae08745Sheppo 
66861ae08745Sheppo 			if (tpp->d_type == VSW_VNETPORT) {
66871ae08745Sheppo 				port = (vsw_port_t *)tpp->d_addr;
66881ae08745Sheppo 				D3(vswp, "%s: sending to port %ld for "
66891ae08745Sheppo 					" addr 0x%llx", __func__,
66901ae08745Sheppo 					port->p_instance, key);
66911ae08745Sheppo 
66921ae08745Sheppo 				nmp = dupmsg(mp);
66931ae08745Sheppo 				if (nmp)
66941ae08745Sheppo 					(void) vsw_portsend(port, nmp);
66951ae08745Sheppo 			} else {
66961ae08745Sheppo 				if (vswp->if_state & VSW_IF_UP) {
66971ae08745Sheppo 					nmp = copymsg(mp);
66981ae08745Sheppo 					if (nmp)
6699ba2e4443Sseb 						mac_rx(vswp->if_mh, NULL, nmp);
67001ae08745Sheppo 					check_if = B_FALSE;
67011ae08745Sheppo 					D3(vswp, "%s: sending up stack"
67021ae08745Sheppo 						" for addr 0x%llx", __func__,
67031ae08745Sheppo 						key);
67041ae08745Sheppo 				}
67051ae08745Sheppo 			}
67061ae08745Sheppo 		}
67071ae08745Sheppo 	}
67081ae08745Sheppo 
67091ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
67101ae08745Sheppo 
67111ae08745Sheppo 	/*
67121ae08745Sheppo 	 * If the pkt came from either a vnet or from physical device,
67131ae08745Sheppo 	 * and if we havent already sent the pkt up the stack then we
67141ae08745Sheppo 	 * check now if we can/should (i.e. the interface is plumbed
67151ae08745Sheppo 	 * and in promisc mode).
67161ae08745Sheppo 	 */
67171ae08745Sheppo 	if ((check_if) &&
67181ae08745Sheppo 		((caller == VSW_VNETPORT) || (caller == VSW_PHYSDEV))) {
67191ae08745Sheppo 		READ_ENTER(&vswp->if_lockrw);
67201ae08745Sheppo 		if (VSW_U_P(vswp->if_state)) {
67211ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
67221ae08745Sheppo 			D3(vswp, "%s: (caller %d) finally sending up stack"
67231ae08745Sheppo 				" for addr 0x%llx", __func__, caller, key);
67241ae08745Sheppo 			nmp = copymsg(mp);
67251ae08745Sheppo 			if (nmp)
6726ba2e4443Sseb 				mac_rx(vswp->if_mh, NULL, nmp);
67271ae08745Sheppo 		} else {
67281ae08745Sheppo 			RW_EXIT(&vswp->if_lockrw);
67291ae08745Sheppo 		}
67301ae08745Sheppo 	}
67311ae08745Sheppo 
67321ae08745Sheppo 	freemsg(mp);
67331ae08745Sheppo 
67341ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
67351ae08745Sheppo 
67361ae08745Sheppo 	return (0);
67371ae08745Sheppo }
67381ae08745Sheppo 
67391ae08745Sheppo /* transmit the packet over the given port */
67401ae08745Sheppo static int
67411ae08745Sheppo vsw_portsend(vsw_port_t *port, mblk_t *mp)
67421ae08745Sheppo {
67431ae08745Sheppo 	vsw_ldc_list_t 	*ldcl = &port->p_ldclist;
67441ae08745Sheppo 	vsw_ldc_t 	*ldcp;
67451ae08745Sheppo 	int		status = 0;
67461ae08745Sheppo 
67471ae08745Sheppo 
67481ae08745Sheppo 	READ_ENTER(&ldcl->lockrw);
67491ae08745Sheppo 	/*
67501ae08745Sheppo 	 * Note for now, we have a single channel.
67511ae08745Sheppo 	 */
67521ae08745Sheppo 	ldcp = ldcl->head;
67531ae08745Sheppo 	if (ldcp == NULL) {
67541ae08745Sheppo 		DERR(port->p_vswp, "vsw_portsend: no ldc: dropping packet\n");
67551ae08745Sheppo 		freemsg(mp);
67561ae08745Sheppo 		RW_EXIT(&ldcl->lockrw);
67571ae08745Sheppo 		return (1);
67581ae08745Sheppo 	}
67591ae08745Sheppo 
67601ae08745Sheppo 	/*
67611ae08745Sheppo 	 * Send the message out using the appropriate
67621ae08745Sheppo 	 * transmit function which will free mblock when it
67631ae08745Sheppo 	 * is finished with it.
67641ae08745Sheppo 	 */
67651ae08745Sheppo 	mutex_enter(&port->tx_lock);
67661ae08745Sheppo 	if (port->transmit != NULL)
67671ae08745Sheppo 		status = (*port->transmit)(ldcp, mp);
67681ae08745Sheppo 	else {
67691ae08745Sheppo 		freemsg(mp);
67701ae08745Sheppo 	}
67711ae08745Sheppo 	mutex_exit(&port->tx_lock);
67721ae08745Sheppo 
67731ae08745Sheppo 	RW_EXIT(&ldcl->lockrw);
67741ae08745Sheppo 
67751ae08745Sheppo 	return (status);
67761ae08745Sheppo }
67771ae08745Sheppo 
67781ae08745Sheppo /*
67791ae08745Sheppo  * Send packet out via descriptor ring to a logical device.
67801ae08745Sheppo  */
67811ae08745Sheppo static int
67821ae08745Sheppo vsw_dringsend(vsw_ldc_t *ldcp, mblk_t *mp)
67831ae08745Sheppo {
67841ae08745Sheppo 	vio_dring_msg_t		dring_pkt;
67851ae08745Sheppo 	dring_info_t		*dp = NULL;
67861ae08745Sheppo 	vsw_private_desc_t	*priv_desc = NULL;
6787d10e4ef2Snarayan 	vnet_public_desc_t	*pub = NULL;
67881ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
67891ae08745Sheppo 	mblk_t			*bp;
67901ae08745Sheppo 	size_t			n, size;
67911ae08745Sheppo 	caddr_t			bufp;
67921ae08745Sheppo 	int			idx;
67931ae08745Sheppo 	int			status = LDC_TX_SUCCESS;
67941ae08745Sheppo 
67951ae08745Sheppo 	D1(vswp, "%s(%lld): enter\n", __func__, ldcp->ldc_id);
67961ae08745Sheppo 
67971ae08745Sheppo 	/* TODO: make test a macro */
67981ae08745Sheppo 	if ((!(ldcp->lane_out.lstate & VSW_LANE_ACTIVE)) ||
67991ae08745Sheppo 		(ldcp->ldc_status != LDC_UP) || (ldcp->ldc_handle == NULL)) {
68001ae08745Sheppo 		DWARN(vswp, "%s(%lld) status(%d) lstate(0x%llx), dropping "
68011ae08745Sheppo 			"packet\n", __func__, ldcp->ldc_id, ldcp->ldc_status,
68021ae08745Sheppo 			ldcp->lane_out.lstate);
68031ae08745Sheppo 		freemsg(mp);
68041ae08745Sheppo 		return (LDC_TX_FAILURE);
68051ae08745Sheppo 	}
68061ae08745Sheppo 
68071ae08745Sheppo 	/*
68081ae08745Sheppo 	 * Note - using first ring only, this may change
68091ae08745Sheppo 	 * in the future.
68101ae08745Sheppo 	 */
68111ae08745Sheppo 	if ((dp = ldcp->lane_out.dringp) == NULL) {
68121ae08745Sheppo 		DERR(vswp, "%s(%lld): no dring for outbound lane on"
68131ae08745Sheppo 			" channel %d", __func__, ldcp->ldc_id, ldcp->ldc_id);
68141ae08745Sheppo 		freemsg(mp);
68151ae08745Sheppo 		return (LDC_TX_FAILURE);
68161ae08745Sheppo 	}
68171ae08745Sheppo 
68181ae08745Sheppo 	size = msgsize(mp);
68191ae08745Sheppo 	if (size > (size_t)ETHERMAX) {
68201ae08745Sheppo 		DERR(vswp, "%s(%lld) invalid size (%ld)\n", __func__,
68211ae08745Sheppo 		    ldcp->ldc_id, size);
6822d10e4ef2Snarayan 		freemsg(mp);
6823d10e4ef2Snarayan 		return (LDC_TX_FAILURE);
68241ae08745Sheppo 	}
68251ae08745Sheppo 
68261ae08745Sheppo 	/*
68271ae08745Sheppo 	 * Find a free descriptor
68281ae08745Sheppo 	 *
68291ae08745Sheppo 	 * Note: for the moment we are assuming that we will only
68301ae08745Sheppo 	 * have one dring going from the switch to each of its
68311ae08745Sheppo 	 * peers. This may change in the future.
68321ae08745Sheppo 	 */
68331ae08745Sheppo 	if (vsw_dring_find_free_desc(dp, &priv_desc, &idx) != 0) {
6834d10e4ef2Snarayan 		D2(vswp, "%s(%lld): no descriptor available for ring "
68351ae08745Sheppo 			"at 0x%llx", __func__, ldcp->ldc_id, dp);
68361ae08745Sheppo 
68371ae08745Sheppo 		/* nothing more we can do */
68381ae08745Sheppo 		status = LDC_TX_NORESOURCES;
68391ae08745Sheppo 		goto vsw_dringsend_free_exit;
68401ae08745Sheppo 	} else {
68411ae08745Sheppo 		D2(vswp, "%s(%lld): free private descriptor found at pos "
68421ae08745Sheppo 			"%ld addr 0x%llx\n", __func__, ldcp->ldc_id, idx,
68431ae08745Sheppo 			priv_desc);
68441ae08745Sheppo 	}
68451ae08745Sheppo 
68461ae08745Sheppo 	/* copy data into the descriptor */
68471ae08745Sheppo 	bufp = priv_desc->datap;
6848d10e4ef2Snarayan 	bufp += VNET_IPALIGN;
68491ae08745Sheppo 	for (bp = mp, n = 0; bp != NULL; bp = bp->b_cont) {
68501ae08745Sheppo 		n = MBLKL(bp);
68511ae08745Sheppo 		bcopy(bp->b_rptr, bufp, n);
68521ae08745Sheppo 		bufp += n;
68531ae08745Sheppo 	}
68541ae08745Sheppo 
68551ae08745Sheppo 	priv_desc->datalen = (size < (size_t)ETHERMIN) ? ETHERMIN : size;
6856d10e4ef2Snarayan 
6857d10e4ef2Snarayan 	pub = priv_desc->descp;
6858d10e4ef2Snarayan 	pub->nbytes = priv_desc->datalen;
6859d10e4ef2Snarayan 
6860d10e4ef2Snarayan 	mutex_enter(&priv_desc->dstate_lock);
6861d10e4ef2Snarayan 	pub->hdr.dstate = VIO_DESC_READY;
6862d10e4ef2Snarayan 	mutex_exit(&priv_desc->dstate_lock);
68631ae08745Sheppo 
68641ae08745Sheppo 	/*
6865d10e4ef2Snarayan 	 * Determine whether or not we need to send a message to our
6866d10e4ef2Snarayan 	 * peer prompting them to read our newly updated descriptor(s).
68671ae08745Sheppo 	 */
6868d10e4ef2Snarayan 	mutex_enter(&dp->restart_lock);
6869d10e4ef2Snarayan 	if (dp->restart_reqd) {
6870d10e4ef2Snarayan 		dp->restart_reqd = B_FALSE;
6871d10e4ef2Snarayan 		mutex_exit(&dp->restart_lock);
68721ae08745Sheppo 
68731ae08745Sheppo 		/*
68741ae08745Sheppo 		 * Send a vio_dring_msg to peer to prompt them to read
68751ae08745Sheppo 		 * the updated descriptor ring.
68761ae08745Sheppo 		 */
68771ae08745Sheppo 		dring_pkt.tag.vio_msgtype = VIO_TYPE_DATA;
68781ae08745Sheppo 		dring_pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
68791ae08745Sheppo 		dring_pkt.tag.vio_subtype_env = VIO_DRING_DATA;
68801ae08745Sheppo 		dring_pkt.tag.vio_sid = ldcp->local_session;
68811ae08745Sheppo 
68821ae08745Sheppo 		/* Note - for now using first ring */
68831ae08745Sheppo 		dring_pkt.dring_ident = dp->ident;
68841ae08745Sheppo 
6885d10e4ef2Snarayan 		mutex_enter(&ldcp->lane_out.seq_lock);
68861ae08745Sheppo 		dring_pkt.seq_num = ldcp->lane_out.seq_num++;
6887d10e4ef2Snarayan 		mutex_exit(&ldcp->lane_out.seq_lock);
68881ae08745Sheppo 
6889d10e4ef2Snarayan 		/*
6890d10e4ef2Snarayan 		 * If last_ack_recv is -1 then we know we've not
6891d10e4ef2Snarayan 		 * received any ack's yet, so this must be the first
6892d10e4ef2Snarayan 		 * msg sent, so set the start to the begining of the ring.
6893d10e4ef2Snarayan 		 */
6894d10e4ef2Snarayan 		mutex_enter(&dp->dlock);
6895d10e4ef2Snarayan 		if (dp->last_ack_recv == -1) {
6896d10e4ef2Snarayan 			dring_pkt.start_idx = 0;
6897d10e4ef2Snarayan 		} else {
6898d10e4ef2Snarayan 			dring_pkt.start_idx = (dp->last_ack_recv + 1) %
6899d10e4ef2Snarayan 						dp->num_descriptors;
6900d10e4ef2Snarayan 		}
6901d10e4ef2Snarayan 		dring_pkt.end_idx = -1;
6902d10e4ef2Snarayan 		mutex_exit(&dp->dlock);
69031ae08745Sheppo 
69041ae08745Sheppo 		D3(vswp, "%s(%lld): dring 0x%llx : ident 0x%llx\n", __func__,
69051ae08745Sheppo 			ldcp->ldc_id, dp, dring_pkt.dring_ident);
6906d10e4ef2Snarayan 		D3(vswp, "%s(%lld): start %lld : end %lld : seq %lld\n",
6907d10e4ef2Snarayan 			__func__, ldcp->ldc_id, dring_pkt.start_idx,
6908d10e4ef2Snarayan 			dring_pkt.end_idx, dring_pkt.seq_num);
69091ae08745Sheppo 
6910d10e4ef2Snarayan 		vsw_send_msg(ldcp, (void *)&dring_pkt,
6911d10e4ef2Snarayan 						sizeof (vio_dring_msg_t));
6912d10e4ef2Snarayan 	} else {
6913d10e4ef2Snarayan 		mutex_exit(&dp->restart_lock);
6914d10e4ef2Snarayan 		D2(vswp, "%s(%lld): updating descp %d", __func__,
6915d10e4ef2Snarayan 			ldcp->ldc_id, idx);
6916d10e4ef2Snarayan 	}
69171ae08745Sheppo 
69181ae08745Sheppo vsw_dringsend_free_exit:
69191ae08745Sheppo 
69201ae08745Sheppo 	/* free the message block */
69211ae08745Sheppo 	freemsg(mp);
69221ae08745Sheppo 
69231ae08745Sheppo 	D1(vswp, "%s(%lld): exit\n", __func__, ldcp->ldc_id);
69241ae08745Sheppo 	return (status);
69251ae08745Sheppo }
69261ae08745Sheppo 
69271ae08745Sheppo /*
69281ae08745Sheppo  * Send an in-band descriptor message over ldc.
69291ae08745Sheppo  */
69301ae08745Sheppo static int
69311ae08745Sheppo vsw_descrsend(vsw_ldc_t *ldcp, mblk_t *mp)
69321ae08745Sheppo {
69331ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
69341ae08745Sheppo 	vio_ibnd_desc_t		ibnd_msg;
69351ae08745Sheppo 	vsw_private_desc_t	*priv_desc = NULL;
69361ae08745Sheppo 	dring_info_t		*dp = NULL;
69371ae08745Sheppo 	size_t			n, size = 0;
69381ae08745Sheppo 	caddr_t			bufp;
69391ae08745Sheppo 	mblk_t			*bp;
69401ae08745Sheppo 	int			idx, i;
69411ae08745Sheppo 	int			status = LDC_TX_SUCCESS;
69421ae08745Sheppo 	static int		warn_msg = 1;
69431ae08745Sheppo 
69441ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
69451ae08745Sheppo 
69461ae08745Sheppo 	ASSERT(mp != NULL);
69471ae08745Sheppo 
69481ae08745Sheppo 	if ((!(ldcp->lane_out.lstate & VSW_LANE_ACTIVE)) ||
69491ae08745Sheppo 		(ldcp->ldc_status != LDC_UP) || (ldcp->ldc_handle == NULL)) {
69501ae08745Sheppo 		DERR(vswp, "%s(%lld) status(%d) state (0x%llx), dropping pkt",
69511ae08745Sheppo 			__func__, ldcp->ldc_id, ldcp->ldc_status,
69521ae08745Sheppo 			ldcp->lane_out.lstate);
69531ae08745Sheppo 		freemsg(mp);
69541ae08745Sheppo 		return (LDC_TX_FAILURE);
69551ae08745Sheppo 	}
69561ae08745Sheppo 
69571ae08745Sheppo 	/*
69581ae08745Sheppo 	 * only expect single dring to exist, which we use
69591ae08745Sheppo 	 * as an internal buffer, rather than a transfer channel.
69601ae08745Sheppo 	 */
69611ae08745Sheppo 	if ((dp = ldcp->lane_out.dringp) == NULL) {
69621ae08745Sheppo 		DERR(vswp, "%s(%lld): no dring for outbound lane",
69631ae08745Sheppo 			__func__, ldcp->ldc_id);
69641ae08745Sheppo 		DERR(vswp, "%s(%lld) status(%d) state (0x%llx)",
69651ae08745Sheppo 			__func__, ldcp->ldc_id, ldcp->ldc_status,
69661ae08745Sheppo 			ldcp->lane_out.lstate);
69671ae08745Sheppo 		freemsg(mp);
69681ae08745Sheppo 		return (LDC_TX_FAILURE);
69691ae08745Sheppo 	}
69701ae08745Sheppo 
69711ae08745Sheppo 	size = msgsize(mp);
69721ae08745Sheppo 	if (size > (size_t)ETHERMAX) {
69731ae08745Sheppo 		DERR(vswp, "%s(%lld) invalid size (%ld)\n", __func__,
69741ae08745Sheppo 		    ldcp->ldc_id, size);
6975d10e4ef2Snarayan 		freemsg(mp);
6976d10e4ef2Snarayan 		return (LDC_TX_FAILURE);
69771ae08745Sheppo 	}
69781ae08745Sheppo 
69791ae08745Sheppo 	/*
69801ae08745Sheppo 	 * Find a free descriptor in our buffer ring
69811ae08745Sheppo 	 */
69821ae08745Sheppo 	if (vsw_dring_find_free_desc(dp, &priv_desc, &idx) != 0) {
69831ae08745Sheppo 		if (warn_msg) {
69841ae08745Sheppo 			DERR(vswp, "%s(%lld): no descriptor available for ring "
69851ae08745Sheppo 			"at 0x%llx", __func__, ldcp->ldc_id, dp);
69861ae08745Sheppo 			warn_msg = 0;
69871ae08745Sheppo 		}
69881ae08745Sheppo 
69891ae08745Sheppo 		/* nothing more we can do */
69901ae08745Sheppo 		status = LDC_TX_NORESOURCES;
69911ae08745Sheppo 		goto vsw_descrsend_free_exit;
69921ae08745Sheppo 	} else {
69931ae08745Sheppo 		D2(vswp, "%s(%lld): free private descriptor found at pos "
69941ae08745Sheppo 			"%ld addr 0x%x\n", __func__, ldcp->ldc_id, idx,
69951ae08745Sheppo 			priv_desc);
69961ae08745Sheppo 		warn_msg = 1;
69971ae08745Sheppo 	}
69981ae08745Sheppo 
69991ae08745Sheppo 	/* copy data into the descriptor */
70001ae08745Sheppo 	bufp = priv_desc->datap;
70011ae08745Sheppo 	for (bp = mp, n = 0; bp != NULL; bp = bp->b_cont) {
70021ae08745Sheppo 		n = MBLKL(bp);
70031ae08745Sheppo 		bcopy(bp->b_rptr, bufp, n);
70041ae08745Sheppo 		bufp += n;
70051ae08745Sheppo 	}
70061ae08745Sheppo 
70071ae08745Sheppo 	priv_desc->datalen = (size < (size_t)ETHERMIN) ? ETHERMIN : size;
70081ae08745Sheppo 
70091ae08745Sheppo 	/* create and send the in-band descp msg */
70101ae08745Sheppo 	ibnd_msg.hdr.tag.vio_msgtype = VIO_TYPE_DATA;
70111ae08745Sheppo 	ibnd_msg.hdr.tag.vio_subtype = VIO_SUBTYPE_INFO;
70121ae08745Sheppo 	ibnd_msg.hdr.tag.vio_subtype_env = VIO_DESC_DATA;
70131ae08745Sheppo 	ibnd_msg.hdr.tag.vio_sid = ldcp->local_session;
70141ae08745Sheppo 
7015d10e4ef2Snarayan 	mutex_enter(&ldcp->lane_out.seq_lock);
70161ae08745Sheppo 	ibnd_msg.hdr.seq_num = ldcp->lane_out.seq_num++;
7017d10e4ef2Snarayan 	mutex_exit(&ldcp->lane_out.seq_lock);
70181ae08745Sheppo 
70191ae08745Sheppo 	/*
70201ae08745Sheppo 	 * Copy the mem cookies describing the data from the
70211ae08745Sheppo 	 * private region of the descriptor ring into the inband
70221ae08745Sheppo 	 * descriptor.
70231ae08745Sheppo 	 */
70241ae08745Sheppo 	for (i = 0; i < priv_desc->ncookies; i++) {
70251ae08745Sheppo 		bcopy(&priv_desc->memcookie[i], &ibnd_msg.memcookie[i],
70261ae08745Sheppo 			sizeof (ldc_mem_cookie_t));
70271ae08745Sheppo 	}
70281ae08745Sheppo 
70291ae08745Sheppo 	ibnd_msg.hdr.desc_handle = idx;
70301ae08745Sheppo 	ibnd_msg.ncookies = priv_desc->ncookies;
70311ae08745Sheppo 	ibnd_msg.nbytes = size;
70321ae08745Sheppo 
70331ae08745Sheppo 	vsw_send_msg(ldcp, (void *)&ibnd_msg, sizeof (vio_ibnd_desc_t));
70341ae08745Sheppo 
70351ae08745Sheppo vsw_descrsend_free_exit:
70361ae08745Sheppo 
70371ae08745Sheppo 	/* free the allocated message blocks */
70381ae08745Sheppo 	freemsg(mp);
70391ae08745Sheppo 
70401ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
70411ae08745Sheppo 	return (status);
70421ae08745Sheppo }
70431ae08745Sheppo 
70441ae08745Sheppo static void
70453af08d82Slm66018 vsw_send_ver(void *arg)
70461ae08745Sheppo {
70473af08d82Slm66018 	vsw_ldc_t	*ldcp = (vsw_ldc_t *)arg;
70481ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
70491ae08745Sheppo 	lane_t		*lp = &ldcp->lane_out;
70501ae08745Sheppo 	vio_ver_msg_t	ver_msg;
70511ae08745Sheppo 
70521ae08745Sheppo 	D1(vswp, "%s enter", __func__);
70531ae08745Sheppo 
70541ae08745Sheppo 	ver_msg.tag.vio_msgtype = VIO_TYPE_CTRL;
70551ae08745Sheppo 	ver_msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
70561ae08745Sheppo 	ver_msg.tag.vio_subtype_env = VIO_VER_INFO;
70571ae08745Sheppo 	ver_msg.tag.vio_sid = ldcp->local_session;
70581ae08745Sheppo 
70591ae08745Sheppo 	ver_msg.ver_major = vsw_versions[0].ver_major;
70601ae08745Sheppo 	ver_msg.ver_minor = vsw_versions[0].ver_minor;
70611ae08745Sheppo 	ver_msg.dev_class = VDEV_NETWORK_SWITCH;
70621ae08745Sheppo 
70631ae08745Sheppo 	lp->lstate |= VSW_VER_INFO_SENT;
70641ae08745Sheppo 	lp->ver_major = ver_msg.ver_major;
70651ae08745Sheppo 	lp->ver_minor = ver_msg.ver_minor;
70661ae08745Sheppo 
70671ae08745Sheppo 	DUMP_TAG(ver_msg.tag);
70681ae08745Sheppo 
70691ae08745Sheppo 	vsw_send_msg(ldcp, &ver_msg, sizeof (vio_ver_msg_t));
70701ae08745Sheppo 
70711ae08745Sheppo 	D1(vswp, "%s (%d): exit", __func__, ldcp->ldc_id);
70721ae08745Sheppo }
70731ae08745Sheppo 
70741ae08745Sheppo static void
70751ae08745Sheppo vsw_send_attr(vsw_ldc_t *ldcp)
70761ae08745Sheppo {
70771ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
70781ae08745Sheppo 	lane_t			*lp = &ldcp->lane_out;
70791ae08745Sheppo 	vnet_attr_msg_t		attr_msg;
70801ae08745Sheppo 
70811ae08745Sheppo 	D1(vswp, "%s (%ld) enter", __func__, ldcp->ldc_id);
70821ae08745Sheppo 
70831ae08745Sheppo 	/*
70841ae08745Sheppo 	 * Subtype is set to INFO by default
70851ae08745Sheppo 	 */
70861ae08745Sheppo 	attr_msg.tag.vio_msgtype = VIO_TYPE_CTRL;
70871ae08745Sheppo 	attr_msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
70881ae08745Sheppo 	attr_msg.tag.vio_subtype_env = VIO_ATTR_INFO;
70891ae08745Sheppo 	attr_msg.tag.vio_sid = ldcp->local_session;
70901ae08745Sheppo 
70911ae08745Sheppo 	/* payload copied from default settings for lane */
70921ae08745Sheppo 	attr_msg.mtu = lp->mtu;
70931ae08745Sheppo 	attr_msg.addr_type = lp->addr_type;
70941ae08745Sheppo 	attr_msg.xfer_mode = lp->xfer_mode;
70951ae08745Sheppo 	attr_msg.ack_freq = lp->xfer_mode;
70961ae08745Sheppo 
70971ae08745Sheppo 	READ_ENTER(&vswp->if_lockrw);
70981ae08745Sheppo 	bcopy(&(vswp->if_addr), &(attr_msg.addr), ETHERADDRL);
70991ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
71001ae08745Sheppo 
71011ae08745Sheppo 	ldcp->lane_out.lstate |= VSW_ATTR_INFO_SENT;
71021ae08745Sheppo 
71031ae08745Sheppo 	DUMP_TAG(attr_msg.tag);
71041ae08745Sheppo 
71051ae08745Sheppo 	vsw_send_msg(ldcp, &attr_msg, sizeof (vnet_attr_msg_t));
71061ae08745Sheppo 
71071ae08745Sheppo 	D1(vswp, "%s (%ld) enter", __func__, ldcp->ldc_id);
71081ae08745Sheppo }
71091ae08745Sheppo 
71101ae08745Sheppo /*
71111ae08745Sheppo  * Create dring info msg (which also results in the creation of
71121ae08745Sheppo  * a dring).
71131ae08745Sheppo  */
71141ae08745Sheppo static vio_dring_reg_msg_t *
71151ae08745Sheppo vsw_create_dring_info_pkt(vsw_ldc_t *ldcp)
71161ae08745Sheppo {
71171ae08745Sheppo 	vio_dring_reg_msg_t	*mp;
71181ae08745Sheppo 	dring_info_t		*dp;
71191ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
71201ae08745Sheppo 
71211ae08745Sheppo 	D1(vswp, "vsw_create_dring_info_pkt enter\n");
71221ae08745Sheppo 
71231ae08745Sheppo 	/*
71241ae08745Sheppo 	 * If we can't create a dring, obviously no point sending
71251ae08745Sheppo 	 * a message.
71261ae08745Sheppo 	 */
71271ae08745Sheppo 	if ((dp = vsw_create_dring(ldcp)) == NULL)
71281ae08745Sheppo 		return (NULL);
71291ae08745Sheppo 
71301ae08745Sheppo 	mp = kmem_zalloc(sizeof (vio_dring_reg_msg_t), KM_SLEEP);
71311ae08745Sheppo 
71321ae08745Sheppo 	mp->tag.vio_msgtype = VIO_TYPE_CTRL;
71331ae08745Sheppo 	mp->tag.vio_subtype = VIO_SUBTYPE_INFO;
71341ae08745Sheppo 	mp->tag.vio_subtype_env = VIO_DRING_REG;
71351ae08745Sheppo 	mp->tag.vio_sid = ldcp->local_session;
71361ae08745Sheppo 
71371ae08745Sheppo 	/* payload */
71381ae08745Sheppo 	mp->num_descriptors = dp->num_descriptors;
71391ae08745Sheppo 	mp->descriptor_size = dp->descriptor_size;
71401ae08745Sheppo 	mp->options = dp->options;
71411ae08745Sheppo 	mp->ncookies = dp->ncookies;
71421ae08745Sheppo 	bcopy(&dp->cookie[0], &mp->cookie[0], sizeof (ldc_mem_cookie_t));
71431ae08745Sheppo 
71441ae08745Sheppo 	mp->dring_ident = 0;
71451ae08745Sheppo 
71461ae08745Sheppo 	D1(vswp, "vsw_create_dring_info_pkt exit\n");
71471ae08745Sheppo 
71481ae08745Sheppo 	return (mp);
71491ae08745Sheppo }
71501ae08745Sheppo 
71511ae08745Sheppo static void
71521ae08745Sheppo vsw_send_dring_info(vsw_ldc_t *ldcp)
71531ae08745Sheppo {
71541ae08745Sheppo 	vio_dring_reg_msg_t	*dring_msg;
71551ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
71561ae08745Sheppo 
71571ae08745Sheppo 	D1(vswp, "%s: (%ld) enter", __func__, ldcp->ldc_id);
71581ae08745Sheppo 
71591ae08745Sheppo 	dring_msg = vsw_create_dring_info_pkt(ldcp);
71601ae08745Sheppo 	if (dring_msg == NULL) {
7161*34683adeSsg70180 		cmn_err(CE_WARN, "!vsw%d: %s: error creating msg",
7162*34683adeSsg70180 			vswp->instance, __func__);
71631ae08745Sheppo 		return;
71641ae08745Sheppo 	}
71651ae08745Sheppo 
71661ae08745Sheppo 	ldcp->lane_out.lstate |= VSW_DRING_INFO_SENT;
71671ae08745Sheppo 
71681ae08745Sheppo 	DUMP_TAG_PTR((vio_msg_tag_t *)dring_msg);
71691ae08745Sheppo 
71701ae08745Sheppo 	vsw_send_msg(ldcp, dring_msg,
71711ae08745Sheppo 		sizeof (vio_dring_reg_msg_t));
71721ae08745Sheppo 
71731ae08745Sheppo 	kmem_free(dring_msg, sizeof (vio_dring_reg_msg_t));
71741ae08745Sheppo 
71751ae08745Sheppo 	D1(vswp, "%s: (%ld) exit", __func__, ldcp->ldc_id);
71761ae08745Sheppo }
71771ae08745Sheppo 
71781ae08745Sheppo static void
71791ae08745Sheppo vsw_send_rdx(vsw_ldc_t *ldcp)
71801ae08745Sheppo {
71811ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
71821ae08745Sheppo 	vio_rdx_msg_t	rdx_msg;
71831ae08745Sheppo 
71841ae08745Sheppo 	D1(vswp, "%s (%ld) enter", __func__, ldcp->ldc_id);
71851ae08745Sheppo 
71861ae08745Sheppo 	rdx_msg.tag.vio_msgtype = VIO_TYPE_CTRL;
71871ae08745Sheppo 	rdx_msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
71881ae08745Sheppo 	rdx_msg.tag.vio_subtype_env = VIO_RDX;
71891ae08745Sheppo 	rdx_msg.tag.vio_sid = ldcp->local_session;
71901ae08745Sheppo 
71911ae08745Sheppo 	ldcp->lane_out.lstate |= VSW_RDX_INFO_SENT;
71921ae08745Sheppo 
71931ae08745Sheppo 	DUMP_TAG(rdx_msg.tag);
71941ae08745Sheppo 
71951ae08745Sheppo 	vsw_send_msg(ldcp, &rdx_msg, sizeof (vio_rdx_msg_t));
71961ae08745Sheppo 
71971ae08745Sheppo 	D1(vswp, "%s (%ld) exit", __func__, ldcp->ldc_id);
71981ae08745Sheppo }
71991ae08745Sheppo 
72001ae08745Sheppo /*
72011ae08745Sheppo  * Generic routine to send message out over ldc channel.
72021ae08745Sheppo  */
72031ae08745Sheppo static void
72041ae08745Sheppo vsw_send_msg(vsw_ldc_t *ldcp, void *msgp, int size)
72051ae08745Sheppo {
72061ae08745Sheppo 	int		rv;
72071ae08745Sheppo 	size_t		msglen = size;
72081ae08745Sheppo 	vio_msg_tag_t	*tag = (vio_msg_tag_t *)msgp;
72091ae08745Sheppo 	vsw_t		*vswp = ldcp->ldc_vswp;
72101ae08745Sheppo 
72111ae08745Sheppo 	D1(vswp, "vsw_send_msg (%lld) enter : sending %d bytes",
72121ae08745Sheppo 			ldcp->ldc_id, size);
72131ae08745Sheppo 
72141ae08745Sheppo 	D2(vswp, "send_msg: type 0x%llx", tag->vio_msgtype);
72151ae08745Sheppo 	D2(vswp, "send_msg: stype 0x%llx", tag->vio_subtype);
72161ae08745Sheppo 	D2(vswp, "send_msg: senv 0x%llx", tag->vio_subtype_env);
72171ae08745Sheppo 
72181ae08745Sheppo 	mutex_enter(&ldcp->ldc_txlock);
72191ae08745Sheppo 	do {
72201ae08745Sheppo 		msglen = size;
72211ae08745Sheppo 		rv = ldc_write(ldcp->ldc_handle, (caddr_t)msgp, &msglen);
72221ae08745Sheppo 	} while (rv == EWOULDBLOCK && --vsw_wretries > 0);
72231ae08745Sheppo 
72241ae08745Sheppo 	if ((rv != 0) || (msglen != size)) {
72251ae08745Sheppo 		DERR(vswp, "vsw_send_msg:ldc_write failed: chan(%lld) "
72261ae08745Sheppo 			"rv(%d) size (%d) msglen(%d)\n", ldcp->ldc_id,
72271ae08745Sheppo 			rv, size, msglen);
72281ae08745Sheppo 	}
72293af08d82Slm66018 	mutex_exit(&ldcp->ldc_txlock);
72303af08d82Slm66018 
72313af08d82Slm66018 	/* channel has been reset */
72323af08d82Slm66018 	if (rv == ECONNRESET) {
72333af08d82Slm66018 		vsw_handle_reset(ldcp);
72343af08d82Slm66018 	}
72351ae08745Sheppo 
72361ae08745Sheppo 	D1(vswp, "vsw_send_msg (%lld) exit : sent %d bytes",
72371ae08745Sheppo 			ldcp->ldc_id, msglen);
72381ae08745Sheppo }
72391ae08745Sheppo 
72401ae08745Sheppo /*
72411ae08745Sheppo  * Add an entry into FDB, for the given mac address and port_id.
72421ae08745Sheppo  * Returns 0 on success, 1 on failure.
72431ae08745Sheppo  *
72441ae08745Sheppo  * Lock protecting FDB must be held by calling process.
72451ae08745Sheppo  */
72461ae08745Sheppo static int
72471ae08745Sheppo vsw_add_fdb(vsw_t *vswp, vsw_port_t *port)
72481ae08745Sheppo {
72491ae08745Sheppo 	uint64_t	addr = 0;
72501ae08745Sheppo 
72511ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
72521ae08745Sheppo 
72531ae08745Sheppo 	KEY_HASH(addr, port->p_macaddr);
72541ae08745Sheppo 
72551ae08745Sheppo 	D2(vswp, "%s: key = 0x%llx", __func__, addr);
72561ae08745Sheppo 
72571ae08745Sheppo 	/*
72581ae08745Sheppo 	 * Note: duplicate keys will be rejected by mod_hash.
72591ae08745Sheppo 	 */
72601ae08745Sheppo 	if (mod_hash_insert(vswp->fdb, (mod_hash_key_t)addr,
72611ae08745Sheppo 				(mod_hash_val_t)port) != 0) {
72621ae08745Sheppo 		DERR(vswp, "%s: unable to add entry into fdb.", __func__);
72631ae08745Sheppo 		return (1);
72641ae08745Sheppo 	}
72651ae08745Sheppo 
72661ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
72671ae08745Sheppo 	return (0);
72681ae08745Sheppo }
72691ae08745Sheppo 
72701ae08745Sheppo /*
72711ae08745Sheppo  * Remove an entry from FDB.
72721ae08745Sheppo  * Returns 0 on success, 1 on failure.
72731ae08745Sheppo  */
72741ae08745Sheppo static int
72751ae08745Sheppo vsw_del_fdb(vsw_t *vswp, vsw_port_t *port)
72761ae08745Sheppo {
72771ae08745Sheppo 	uint64_t	addr = 0;
72781ae08745Sheppo 
72791ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
72801ae08745Sheppo 
72811ae08745Sheppo 	KEY_HASH(addr, port->p_macaddr);
72821ae08745Sheppo 
72831ae08745Sheppo 	D2(vswp, "%s: key = 0x%llx", __func__, addr);
72841ae08745Sheppo 
72851ae08745Sheppo 	(void) mod_hash_destroy(vswp->fdb, (mod_hash_val_t)addr);
72861ae08745Sheppo 
72871ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
72881ae08745Sheppo 
72891ae08745Sheppo 	return (0);
72901ae08745Sheppo }
72911ae08745Sheppo 
72921ae08745Sheppo /*
72931ae08745Sheppo  * Search fdb for a given mac address.
72941ae08745Sheppo  * Returns pointer to the entry if found, else returns NULL.
72951ae08745Sheppo  */
72961ae08745Sheppo static vsw_port_t *
72971ae08745Sheppo vsw_lookup_fdb(vsw_t *vswp, struct ether_header *ehp)
72981ae08745Sheppo {
72991ae08745Sheppo 	uint64_t	key = 0;
73001ae08745Sheppo 	vsw_port_t	*port = NULL;
73011ae08745Sheppo 
73021ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
73031ae08745Sheppo 
73041ae08745Sheppo 	KEY_HASH(key, ehp->ether_dhost);
73051ae08745Sheppo 
73061ae08745Sheppo 	D2(vswp, "%s: key = 0x%llx", __func__, key);
73071ae08745Sheppo 
73081ae08745Sheppo 	if (mod_hash_find(vswp->fdb, (mod_hash_key_t)key,
73091ae08745Sheppo 				(mod_hash_val_t *)&port) != 0) {
7310*34683adeSsg70180 		D2(vswp, "%s: no port found", __func__);
73111ae08745Sheppo 		return (NULL);
73121ae08745Sheppo 	}
73131ae08745Sheppo 
73141ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
73151ae08745Sheppo 
73161ae08745Sheppo 	return (port);
73171ae08745Sheppo }
73181ae08745Sheppo 
73191ae08745Sheppo /*
73201ae08745Sheppo  * Add or remove multicast address(es).
73211ae08745Sheppo  *
73221ae08745Sheppo  * Returns 0 on success, 1 on failure.
73231ae08745Sheppo  */
73241ae08745Sheppo static int
73251ae08745Sheppo vsw_add_rem_mcst(vnet_mcast_msg_t *mcst_pkt, vsw_port_t *port)
73261ae08745Sheppo {
73271ae08745Sheppo 	mcst_addr_t		*mcst_p = NULL;
73281ae08745Sheppo 	vsw_t			*vswp = port->p_vswp;
73291ae08745Sheppo 	uint64_t		addr = 0x0;
7330*34683adeSsg70180 	int			i;
73311ae08745Sheppo 
73321ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
73331ae08745Sheppo 
73341ae08745Sheppo 	D2(vswp, "%s: %d addresses", __func__, mcst_pkt->count);
73351ae08745Sheppo 
7336*34683adeSsg70180 	mutex_enter(&vswp->mac_lock);
7337*34683adeSsg70180 	if (vswp->mh == NULL) {
7338*34683adeSsg70180 		mutex_exit(&vswp->mac_lock);
7339e1ebb9ecSlm66018 		return (1);
7340*34683adeSsg70180 	}
7341*34683adeSsg70180 	mutex_exit(&vswp->mac_lock);
7342e1ebb9ecSlm66018 
73431ae08745Sheppo 	for (i = 0; i < mcst_pkt->count; i++) {
73441ae08745Sheppo 		/*
73451ae08745Sheppo 		 * Convert address into form that can be used
73461ae08745Sheppo 		 * as hash table key.
73471ae08745Sheppo 		 */
73481ae08745Sheppo 		KEY_HASH(addr, mcst_pkt->mca[i]);
73491ae08745Sheppo 
73501ae08745Sheppo 		/*
73511ae08745Sheppo 		 * Add or delete the specified address/port combination.
73521ae08745Sheppo 		 */
73531ae08745Sheppo 		if (mcst_pkt->set == 0x1) {
73541ae08745Sheppo 			D3(vswp, "%s: adding multicast address 0x%llx for "
73551ae08745Sheppo 				"port %ld", __func__, addr, port->p_instance);
73561ae08745Sheppo 			if (vsw_add_mcst(vswp, VSW_VNETPORT, addr, port) == 0) {
73571ae08745Sheppo 				/*
73581ae08745Sheppo 				 * Update the list of multicast
73591ae08745Sheppo 				 * addresses contained within the
73601ae08745Sheppo 				 * port structure to include this new
73611ae08745Sheppo 				 * one.
73621ae08745Sheppo 				 */
73631ae08745Sheppo 				mcst_p = kmem_alloc(sizeof (mcst_addr_t),
73641ae08745Sheppo 								KM_NOSLEEP);
73651ae08745Sheppo 				if (mcst_p == NULL) {
73661ae08745Sheppo 					DERR(vswp, "%s: unable to alloc mem",
73671ae08745Sheppo 						__func__);
73681ae08745Sheppo 					return (1);
73691ae08745Sheppo 				}
73701ae08745Sheppo 
73711ae08745Sheppo 				mcst_p->nextp = NULL;
73721ae08745Sheppo 				mcst_p->addr = addr;
73731ae08745Sheppo 
73741ae08745Sheppo 				mutex_enter(&port->mca_lock);
73751ae08745Sheppo 				mcst_p->nextp = port->mcap;
73761ae08745Sheppo 				port->mcap = mcst_p;
73771ae08745Sheppo 				mutex_exit(&port->mca_lock);
73781ae08745Sheppo 
73791ae08745Sheppo 				/*
73801ae08745Sheppo 				 * Program the address into HW. If the addr
73811ae08745Sheppo 				 * has already been programmed then the MAC
73821ae08745Sheppo 				 * just increments a ref counter (which is
73831ae08745Sheppo 				 * used when the address is being deleted)
73841ae08745Sheppo 				 */
7385*34683adeSsg70180 				mutex_enter(&vswp->mac_lock);
7386*34683adeSsg70180 				if ((vswp->mh == NULL) ||
7387*34683adeSsg70180 					mac_multicst_add(vswp->mh,
7388*34683adeSsg70180 						(uchar_t *)&mcst_pkt->mca[i])) {
7389*34683adeSsg70180 					mutex_exit(&vswp->mac_lock);
7390*34683adeSsg70180 					cmn_err(CE_WARN, "!vsw%d: unable to "
7391*34683adeSsg70180 						"add multicast address",
7392*34683adeSsg70180 						vswp->instance);
7393e1ebb9ecSlm66018 					(void) vsw_del_mcst(vswp, VSW_VNETPORT,
7394e1ebb9ecSlm66018 						addr, port);
7395e1ebb9ecSlm66018 					vsw_del_addr(VSW_VNETPORT, port, addr);
7396*34683adeSsg70180 					return (1);
7397e1ebb9ecSlm66018 				}
7398*34683adeSsg70180 				mutex_exit(&vswp->mac_lock);
73991ae08745Sheppo 
74001ae08745Sheppo 			} else {
74011ae08745Sheppo 				DERR(vswp, "%s: error adding multicast "
74021ae08745Sheppo 					"address 0x%llx for port %ld",
74031ae08745Sheppo 					__func__, addr, port->p_instance);
74041ae08745Sheppo 				return (1);
74051ae08745Sheppo 			}
74061ae08745Sheppo 		} else {
74071ae08745Sheppo 			/*
74081ae08745Sheppo 			 * Delete an entry from the multicast hash
74091ae08745Sheppo 			 * table and update the address list
74101ae08745Sheppo 			 * appropriately.
74111ae08745Sheppo 			 */
74121ae08745Sheppo 			if (vsw_del_mcst(vswp, VSW_VNETPORT, addr, port) == 0) {
74131ae08745Sheppo 				D3(vswp, "%s: deleting multicast address "
74141ae08745Sheppo 					"0x%llx for port %ld", __func__, addr,
74151ae08745Sheppo 					port->p_instance);
74161ae08745Sheppo 
74171ae08745Sheppo 				vsw_del_addr(VSW_VNETPORT, port, addr);
74181ae08745Sheppo 
74191ae08745Sheppo 				/*
74201ae08745Sheppo 				 * Remove the address from HW. The address
74211ae08745Sheppo 				 * will actually only be removed once the ref
74221ae08745Sheppo 				 * count within the MAC layer has dropped to
74231ae08745Sheppo 				 * zero. I.e. we can safely call this fn even
74241ae08745Sheppo 				 * if other ports are interested in this
74251ae08745Sheppo 				 * address.
74261ae08745Sheppo 				 */
7427*34683adeSsg70180 				mutex_enter(&vswp->mac_lock);
7428*34683adeSsg70180 				if ((vswp->mh == NULL) ||
7429*34683adeSsg70180 					mac_multicst_remove(vswp->mh,
7430*34683adeSsg70180 						(uchar_t *)&mcst_pkt->mca[i])) {
7431*34683adeSsg70180 					mutex_exit(&vswp->mac_lock);
7432*34683adeSsg70180 					cmn_err(CE_WARN, "!vsw%d: unable to "
7433*34683adeSsg70180 						"remove multicast address",
7434*34683adeSsg70180 						vswp->instance);
7435*34683adeSsg70180 					return (1);
7436*34683adeSsg70180 				}
7437*34683adeSsg70180 				mutex_exit(&vswp->mac_lock);
74381ae08745Sheppo 
74391ae08745Sheppo 			} else {
74401ae08745Sheppo 				DERR(vswp, "%s: error deleting multicast "
74411ae08745Sheppo 					"addr 0x%llx for port %ld",
74421ae08745Sheppo 					__func__, addr, port->p_instance);
74431ae08745Sheppo 				return (1);
74441ae08745Sheppo 			}
74451ae08745Sheppo 		}
74461ae08745Sheppo 	}
74471ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
74481ae08745Sheppo 	return (0);
74491ae08745Sheppo }
74501ae08745Sheppo 
74511ae08745Sheppo /*
74521ae08745Sheppo  * Add a new multicast entry.
74531ae08745Sheppo  *
74541ae08745Sheppo  * Search hash table based on address. If match found then
74551ae08745Sheppo  * update associated val (which is chain of ports), otherwise
74561ae08745Sheppo  * create new key/val (addr/port) pair and insert into table.
74571ae08745Sheppo  */
74581ae08745Sheppo static int
74591ae08745Sheppo vsw_add_mcst(vsw_t *vswp, uint8_t devtype, uint64_t addr, void *arg)
74601ae08745Sheppo {
74611ae08745Sheppo 	int		dup = 0;
74621ae08745Sheppo 	int		rv = 0;
74631ae08745Sheppo 	mfdb_ent_t	*ment = NULL;
74641ae08745Sheppo 	mfdb_ent_t	*tmp_ent = NULL;
74651ae08745Sheppo 	mfdb_ent_t	*new_ent = NULL;
74661ae08745Sheppo 	void		*tgt = NULL;
74671ae08745Sheppo 
74681ae08745Sheppo 	if (devtype == VSW_VNETPORT) {
74691ae08745Sheppo 		/*
74701ae08745Sheppo 		 * Being invoked from a vnet.
74711ae08745Sheppo 		 */
74721ae08745Sheppo 		ASSERT(arg != NULL);
74731ae08745Sheppo 		tgt = arg;
74741ae08745Sheppo 		D2(NULL, "%s: port %d : address 0x%llx", __func__,
74751ae08745Sheppo 			((vsw_port_t *)arg)->p_instance, addr);
74761ae08745Sheppo 	} else {
74771ae08745Sheppo 		/*
74781ae08745Sheppo 		 * We are being invoked via the m_multicst mac entry
74791ae08745Sheppo 		 * point.
74801ae08745Sheppo 		 */
74811ae08745Sheppo 		D2(NULL, "%s: address 0x%llx", __func__, addr);
74821ae08745Sheppo 		tgt = (void *)vswp;
74831ae08745Sheppo 	}
74841ae08745Sheppo 
74851ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
74861ae08745Sheppo 	if (mod_hash_find(vswp->mfdb, (mod_hash_key_t)addr,
74871ae08745Sheppo 				(mod_hash_val_t *)&ment) != 0) {
74881ae08745Sheppo 
74891ae08745Sheppo 		/* address not currently in table */
74901ae08745Sheppo 		ment = kmem_alloc(sizeof (mfdb_ent_t), KM_SLEEP);
74911ae08745Sheppo 		ment->d_addr = (void *)tgt;
74921ae08745Sheppo 		ment->d_type = devtype;
74931ae08745Sheppo 		ment->nextp = NULL;
74941ae08745Sheppo 
74951ae08745Sheppo 		if (mod_hash_insert(vswp->mfdb, (mod_hash_key_t)addr,
74961ae08745Sheppo 			(mod_hash_val_t)ment) != 0) {
74971ae08745Sheppo 			DERR(vswp, "%s: hash table insertion failed", __func__);
74981ae08745Sheppo 			kmem_free(ment, sizeof (mfdb_ent_t));
74991ae08745Sheppo 			rv = 1;
75001ae08745Sheppo 		} else {
75011ae08745Sheppo 			D2(vswp, "%s: added initial entry for 0x%llx to "
75021ae08745Sheppo 				"table", __func__, addr);
75031ae08745Sheppo 		}
75041ae08745Sheppo 	} else {
75051ae08745Sheppo 		/*
75061ae08745Sheppo 		 * Address in table. Check to see if specified port
75071ae08745Sheppo 		 * is already associated with the address. If not add
75081ae08745Sheppo 		 * it now.
75091ae08745Sheppo 		 */
75101ae08745Sheppo 		tmp_ent = ment;
75111ae08745Sheppo 		while (tmp_ent != NULL) {
75121ae08745Sheppo 			if (tmp_ent->d_addr == (void *)tgt) {
75131ae08745Sheppo 				if (devtype == VSW_VNETPORT) {
75141ae08745Sheppo 					DERR(vswp, "%s: duplicate port entry "
75151ae08745Sheppo 						"found for portid %ld and key "
75161ae08745Sheppo 						"0x%llx", __func__,
75171ae08745Sheppo 						((vsw_port_t *)arg)->p_instance,
75181ae08745Sheppo 						addr);
75191ae08745Sheppo 				} else {
75201ae08745Sheppo 					DERR(vswp, "%s: duplicate entry found"
75211ae08745Sheppo 						"for key 0x%llx",
75221ae08745Sheppo 						__func__, addr);
75231ae08745Sheppo 				}
75241ae08745Sheppo 				rv = 1;
75251ae08745Sheppo 				dup = 1;
75261ae08745Sheppo 				break;
75271ae08745Sheppo 			}
75281ae08745Sheppo 			tmp_ent = tmp_ent->nextp;
75291ae08745Sheppo 		}
75301ae08745Sheppo 
75311ae08745Sheppo 		/*
75321ae08745Sheppo 		 * Port not on list so add it to end now.
75331ae08745Sheppo 		 */
75341ae08745Sheppo 		if (0 == dup) {
75351ae08745Sheppo 			D2(vswp, "%s: added entry for 0x%llx to table",
75361ae08745Sheppo 				__func__, addr);
75371ae08745Sheppo 			new_ent = kmem_alloc(sizeof (mfdb_ent_t), KM_SLEEP);
75381ae08745Sheppo 			new_ent->d_addr = (void *)tgt;
75391ae08745Sheppo 			new_ent->d_type = devtype;
75401ae08745Sheppo 			new_ent->nextp = NULL;
75411ae08745Sheppo 
75421ae08745Sheppo 			tmp_ent = ment;
75431ae08745Sheppo 			while (tmp_ent->nextp != NULL)
75441ae08745Sheppo 				tmp_ent = tmp_ent->nextp;
75451ae08745Sheppo 
75461ae08745Sheppo 			tmp_ent->nextp = new_ent;
75471ae08745Sheppo 		}
75481ae08745Sheppo 	}
75491ae08745Sheppo 
75501ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
75511ae08745Sheppo 	return (rv);
75521ae08745Sheppo }
75531ae08745Sheppo 
75541ae08745Sheppo /*
75551ae08745Sheppo  * Remove a multicast entry from the hashtable.
75561ae08745Sheppo  *
75571ae08745Sheppo  * Search hash table based on address. If match found, scan
75581ae08745Sheppo  * list of ports associated with address. If specified port
75591ae08745Sheppo  * found remove it from list.
75601ae08745Sheppo  */
75611ae08745Sheppo static int
75621ae08745Sheppo vsw_del_mcst(vsw_t *vswp, uint8_t devtype, uint64_t addr, void *arg)
75631ae08745Sheppo {
75641ae08745Sheppo 	mfdb_ent_t	*ment = NULL;
75651ae08745Sheppo 	mfdb_ent_t	*curr_p, *prev_p;
75661ae08745Sheppo 	void		*tgt = NULL;
75671ae08745Sheppo 
75681ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
75691ae08745Sheppo 
75701ae08745Sheppo 	if (devtype == VSW_VNETPORT) {
75711ae08745Sheppo 		tgt = (vsw_port_t *)arg;
75721ae08745Sheppo 		D2(vswp, "%s: removing port %d from mFDB for address"
75731ae08745Sheppo 			" 0x%llx", __func__, ((vsw_port_t *)tgt)->p_instance,
75741ae08745Sheppo 			addr);
75751ae08745Sheppo 	} else {
75761ae08745Sheppo 		D2(vswp, "%s: removing entry", __func__);
75771ae08745Sheppo 		tgt = (void *)vswp;
75781ae08745Sheppo 	}
75791ae08745Sheppo 
75801ae08745Sheppo 	WRITE_ENTER(&vswp->mfdbrw);
75811ae08745Sheppo 	if (mod_hash_find(vswp->mfdb, (mod_hash_key_t)addr,
75821ae08745Sheppo 				(mod_hash_val_t *)&ment) != 0) {
75831ae08745Sheppo 		D2(vswp, "%s: address 0x%llx not in table", __func__, addr);
75841ae08745Sheppo 		RW_EXIT(&vswp->mfdbrw);
75851ae08745Sheppo 		return (1);
75861ae08745Sheppo 	}
75871ae08745Sheppo 
75881ae08745Sheppo 	prev_p = curr_p = ment;
75891ae08745Sheppo 
75901ae08745Sheppo 	while (curr_p != NULL) {
75911ae08745Sheppo 		if (curr_p->d_addr == (void *)tgt) {
75921ae08745Sheppo 			if (devtype == VSW_VNETPORT) {
75931ae08745Sheppo 				D2(vswp, "%s: port %d found", __func__,
75941ae08745Sheppo 					((vsw_port_t *)tgt)->p_instance);
75951ae08745Sheppo 			} else {
75961ae08745Sheppo 				D2(vswp, "%s: instance found", __func__);
75971ae08745Sheppo 			}
75981ae08745Sheppo 
75991ae08745Sheppo 			if (prev_p == curr_p) {
76001ae08745Sheppo 				/*
76011ae08745Sheppo 				 * head of list, if no other element is in
76021ae08745Sheppo 				 * list then destroy this entry, otherwise
76031ae08745Sheppo 				 * just replace it with updated value.
76041ae08745Sheppo 				 */
76051ae08745Sheppo 				ment = curr_p->nextp;
76061ae08745Sheppo 				kmem_free(curr_p, sizeof (mfdb_ent_t));
76071ae08745Sheppo 				if (ment == NULL) {
76081ae08745Sheppo 					(void) mod_hash_destroy(vswp->mfdb,
76091ae08745Sheppo 							(mod_hash_val_t)addr);
76101ae08745Sheppo 				} else {
76111ae08745Sheppo 					(void) mod_hash_replace(vswp->mfdb,
76121ae08745Sheppo 							(mod_hash_key_t)addr,
76131ae08745Sheppo 							(mod_hash_val_t)ment);
76141ae08745Sheppo 				}
76151ae08745Sheppo 			} else {
76161ae08745Sheppo 				/*
76171ae08745Sheppo 				 * Not head of list, no need to do
76181ae08745Sheppo 				 * replacement, just adjust list pointers.
76191ae08745Sheppo 				 */
76201ae08745Sheppo 				prev_p->nextp = curr_p->nextp;
76211ae08745Sheppo 				kmem_free(curr_p, sizeof (mfdb_ent_t));
76221ae08745Sheppo 			}
76231ae08745Sheppo 			break;
76241ae08745Sheppo 		}
76251ae08745Sheppo 
76261ae08745Sheppo 		prev_p = curr_p;
76271ae08745Sheppo 		curr_p = curr_p->nextp;
76281ae08745Sheppo 	}
76291ae08745Sheppo 
76301ae08745Sheppo 	RW_EXIT(&vswp->mfdbrw);
76311ae08745Sheppo 
76321ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
76331ae08745Sheppo 
76341ae08745Sheppo 	return (0);
76351ae08745Sheppo }
76361ae08745Sheppo 
76371ae08745Sheppo /*
76381ae08745Sheppo  * Port is being deleted, but has registered an interest in one
76391ae08745Sheppo  * or more multicast groups. Using the list of addresses maintained
76401ae08745Sheppo  * within the port structure find the appropriate entry in the hash
76411ae08745Sheppo  * table and remove this port from the list of interested ports.
76421ae08745Sheppo  */
76431ae08745Sheppo static void
76441ae08745Sheppo vsw_del_mcst_port(vsw_port_t *port)
76451ae08745Sheppo {
76461ae08745Sheppo 	mcst_addr_t	*mcst_p = NULL;
76471ae08745Sheppo 	vsw_t		*vswp = port->p_vswp;
76481ae08745Sheppo 
76491ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
76501ae08745Sheppo 
76511ae08745Sheppo 	mutex_enter(&port->mca_lock);
76521ae08745Sheppo 	while (port->mcap != NULL) {
76531ae08745Sheppo 		(void) vsw_del_mcst(vswp, VSW_VNETPORT,
76541ae08745Sheppo 					port->mcap->addr, port);
76551ae08745Sheppo 
76561ae08745Sheppo 		mcst_p = port->mcap->nextp;
76571ae08745Sheppo 		kmem_free(port->mcap, sizeof (mcst_addr_t));
76581ae08745Sheppo 		port->mcap = mcst_p;
76591ae08745Sheppo 	}
76601ae08745Sheppo 	mutex_exit(&port->mca_lock);
76611ae08745Sheppo 
76621ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
76631ae08745Sheppo }
76641ae08745Sheppo 
76651ae08745Sheppo /*
76661ae08745Sheppo  * This vsw instance is detaching, but has registered an interest in one
76671ae08745Sheppo  * or more multicast groups. Using the list of addresses maintained
76681ae08745Sheppo  * within the vsw structure find the appropriate entry in the hash
76691ae08745Sheppo  * table and remove this instance from the list of interested ports.
76701ae08745Sheppo  */
76711ae08745Sheppo static void
76721ae08745Sheppo vsw_del_mcst_vsw(vsw_t *vswp)
76731ae08745Sheppo {
76741ae08745Sheppo 	mcst_addr_t	*next_p = NULL;
76751ae08745Sheppo 
76761ae08745Sheppo 	D1(vswp, "%s: enter", __func__);
76771ae08745Sheppo 
76781ae08745Sheppo 	mutex_enter(&vswp->mca_lock);
76791ae08745Sheppo 
76801ae08745Sheppo 	while (vswp->mcap != NULL) {
76811ae08745Sheppo 		DERR(vswp, "%s: deleting addr 0x%llx",
76821ae08745Sheppo 			__func__, vswp->mcap->addr);
76831ae08745Sheppo 		(void) vsw_del_mcst(vswp, VSW_LOCALDEV,
76841ae08745Sheppo 				vswp->mcap->addr, NULL);
76851ae08745Sheppo 
76861ae08745Sheppo 		next_p = vswp->mcap->nextp;
76871ae08745Sheppo 		kmem_free(vswp->mcap, sizeof (mcst_addr_t));
76881ae08745Sheppo 		vswp->mcap = next_p;
76891ae08745Sheppo 	}
76901ae08745Sheppo 
76911ae08745Sheppo 	vswp->mcap = NULL;
76921ae08745Sheppo 	mutex_exit(&vswp->mca_lock);
76931ae08745Sheppo 
76941ae08745Sheppo 	D1(vswp, "%s: exit", __func__);
76951ae08745Sheppo }
76961ae08745Sheppo 
76971ae08745Sheppo 
76981ae08745Sheppo /*
76991ae08745Sheppo  * Remove the specified address from the list of address maintained
77001ae08745Sheppo  * in this port node.
77011ae08745Sheppo  */
77021ae08745Sheppo static void
77031ae08745Sheppo vsw_del_addr(uint8_t devtype, void *arg, uint64_t addr)
77041ae08745Sheppo {
77051ae08745Sheppo 	vsw_t		*vswp = NULL;
77061ae08745Sheppo 	vsw_port_t	*port = NULL;
77071ae08745Sheppo 	mcst_addr_t	*prev_p = NULL;
77081ae08745Sheppo 	mcst_addr_t	*curr_p = NULL;
77091ae08745Sheppo 
77101ae08745Sheppo 	D1(NULL, "%s: enter : devtype %d : addr 0x%llx",
77111ae08745Sheppo 		__func__, devtype, addr);
77121ae08745Sheppo 
77131ae08745Sheppo 	if (devtype == VSW_VNETPORT) {
77141ae08745Sheppo 		port = (vsw_port_t *)arg;
77151ae08745Sheppo 		mutex_enter(&port->mca_lock);
77161ae08745Sheppo 		prev_p = curr_p = port->mcap;
77171ae08745Sheppo 	} else {
77181ae08745Sheppo 		vswp = (vsw_t *)arg;
77191ae08745Sheppo 		mutex_enter(&vswp->mca_lock);
77201ae08745Sheppo 		prev_p = curr_p = vswp->mcap;
77211ae08745Sheppo 	}
77221ae08745Sheppo 
77231ae08745Sheppo 	while (curr_p != NULL) {
77241ae08745Sheppo 		if (curr_p->addr == addr) {
77251ae08745Sheppo 			D2(NULL, "%s: address found", __func__);
77261ae08745Sheppo 			/* match found */
77271ae08745Sheppo 			if (prev_p == curr_p) {
77281ae08745Sheppo 				/* list head */
77291ae08745Sheppo 				if (devtype == VSW_VNETPORT)
77301ae08745Sheppo 					port->mcap = curr_p->nextp;
77311ae08745Sheppo 				else
77321ae08745Sheppo 					vswp->mcap = curr_p->nextp;
77331ae08745Sheppo 			} else {
77341ae08745Sheppo 				prev_p->nextp = curr_p->nextp;
77351ae08745Sheppo 			}
77361ae08745Sheppo 			kmem_free(curr_p, sizeof (mcst_addr_t));
77371ae08745Sheppo 			break;
77381ae08745Sheppo 		} else {
77391ae08745Sheppo 			prev_p = curr_p;
77401ae08745Sheppo 			curr_p = curr_p->nextp;
77411ae08745Sheppo 		}
77421ae08745Sheppo 	}
77431ae08745Sheppo 
77441ae08745Sheppo 	if (devtype == VSW_VNETPORT)
77451ae08745Sheppo 		mutex_exit(&port->mca_lock);
77461ae08745Sheppo 	else
77471ae08745Sheppo 		mutex_exit(&vswp->mca_lock);
77481ae08745Sheppo 
77491ae08745Sheppo 	D1(NULL, "%s: exit", __func__);
77501ae08745Sheppo }
77511ae08745Sheppo 
77521ae08745Sheppo /*
77531ae08745Sheppo  * Creates a descriptor ring (dring) and links it into the
77541ae08745Sheppo  * link of outbound drings for this channel.
77551ae08745Sheppo  *
77561ae08745Sheppo  * Returns NULL if creation failed.
77571ae08745Sheppo  */
77581ae08745Sheppo static dring_info_t *
77591ae08745Sheppo vsw_create_dring(vsw_ldc_t *ldcp)
77601ae08745Sheppo {
77611ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
77621ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
77631ae08745Sheppo 	ldc_mem_info_t		minfo;
77641ae08745Sheppo 	dring_info_t		*dp, *tp;
77651ae08745Sheppo 	int			i;
77661ae08745Sheppo 
77671ae08745Sheppo 	dp = (dring_info_t *)kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
77681ae08745Sheppo 
77691ae08745Sheppo 	mutex_init(&dp->dlock, NULL, MUTEX_DRIVER, NULL);
77701ae08745Sheppo 
77711ae08745Sheppo 	/* create public section of ring */
77721ae08745Sheppo 	if ((ldc_mem_dring_create(VSW_RING_NUM_EL,
77731ae08745Sheppo 			VSW_PUB_SIZE, &dp->handle)) != 0) {
77741ae08745Sheppo 
77751ae08745Sheppo 		DERR(vswp, "vsw_create_dring(%lld): ldc dring create "
77761ae08745Sheppo 			"failed", ldcp->ldc_id);
77771ae08745Sheppo 		goto create_fail_exit;
77781ae08745Sheppo 	}
77791ae08745Sheppo 
77801ae08745Sheppo 	ASSERT(dp->handle != NULL);
77811ae08745Sheppo 
77821ae08745Sheppo 	/*
77831ae08745Sheppo 	 * Get the base address of the public section of the ring.
77841ae08745Sheppo 	 */
77851ae08745Sheppo 	if ((ldc_mem_dring_info(dp->handle, &minfo)) != 0) {
77861ae08745Sheppo 		DERR(vswp, "vsw_create_dring(%lld): dring info failed\n",
77871ae08745Sheppo 			ldcp->ldc_id);
77881ae08745Sheppo 		goto dring_fail_exit;
77891ae08745Sheppo 	} else {
77901ae08745Sheppo 		ASSERT(minfo.vaddr != 0);
77911ae08745Sheppo 		dp->pub_addr = minfo.vaddr;
77921ae08745Sheppo 	}
77931ae08745Sheppo 
77941ae08745Sheppo 	dp->num_descriptors = VSW_RING_NUM_EL;
77951ae08745Sheppo 	dp->descriptor_size = VSW_PUB_SIZE;
77961ae08745Sheppo 	dp->options = VIO_TX_DRING;
77971ae08745Sheppo 	dp->ncookies = 1;	/* guaranteed by ldc */
77981ae08745Sheppo 
77991ae08745Sheppo 	/*
78001ae08745Sheppo 	 * create private portion of ring
78011ae08745Sheppo 	 */
78021ae08745Sheppo 	dp->priv_addr = (vsw_private_desc_t *)kmem_zalloc(
78031ae08745Sheppo 		(sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL), KM_SLEEP);
78041ae08745Sheppo 
78051ae08745Sheppo 	if (vsw_setup_ring(ldcp, dp)) {
78061ae08745Sheppo 		DERR(vswp, "%s: unable to setup ring", __func__);
78071ae08745Sheppo 		goto dring_fail_exit;
78081ae08745Sheppo 	}
78091ae08745Sheppo 
78101ae08745Sheppo 	/* haven't used any descriptors yet */
78111ae08745Sheppo 	dp->end_idx = 0;
7812d10e4ef2Snarayan 	dp->last_ack_recv = -1;
78131ae08745Sheppo 
78141ae08745Sheppo 	/* bind dring to the channel */
78151ae08745Sheppo 	if ((ldc_mem_dring_bind(ldcp->ldc_handle, dp->handle,
78161ae08745Sheppo 		LDC_SHADOW_MAP, LDC_MEM_RW,
78171ae08745Sheppo 		&dp->cookie[0], &dp->ncookies)) != 0) {
78181ae08745Sheppo 		DERR(vswp, "vsw_create_dring: unable to bind to channel "
78191ae08745Sheppo 			"%lld", ldcp->ldc_id);
78201ae08745Sheppo 		goto dring_fail_exit;
78211ae08745Sheppo 	}
78221ae08745Sheppo 
7823d10e4ef2Snarayan 	mutex_init(&dp->restart_lock, NULL, MUTEX_DRIVER, NULL);
7824d10e4ef2Snarayan 	dp->restart_reqd = B_TRUE;
7825d10e4ef2Snarayan 
78261ae08745Sheppo 	/*
78271ae08745Sheppo 	 * Only ever create rings for outgoing lane. Link it onto
78281ae08745Sheppo 	 * end of list.
78291ae08745Sheppo 	 */
78301ae08745Sheppo 	if (ldcp->lane_out.dringp == NULL) {
78311ae08745Sheppo 		D2(vswp, "vsw_create_dring: adding first outbound ring");
78321ae08745Sheppo 		ldcp->lane_out.dringp = dp;
78331ae08745Sheppo 	} else {
78341ae08745Sheppo 		tp = ldcp->lane_out.dringp;
78351ae08745Sheppo 		while (tp->next != NULL)
78361ae08745Sheppo 			tp = tp->next;
78371ae08745Sheppo 
78381ae08745Sheppo 		tp->next = dp;
78391ae08745Sheppo 	}
78401ae08745Sheppo 
78411ae08745Sheppo 	return (dp);
78421ae08745Sheppo 
78431ae08745Sheppo dring_fail_exit:
78441ae08745Sheppo 	(void) ldc_mem_dring_destroy(dp->handle);
78451ae08745Sheppo 
78461ae08745Sheppo create_fail_exit:
78471ae08745Sheppo 	if (dp->priv_addr != NULL) {
78481ae08745Sheppo 		priv_addr = dp->priv_addr;
78491ae08745Sheppo 		for (i = 0; i < VSW_RING_NUM_EL; i++) {
78501ae08745Sheppo 			if (priv_addr->memhandle != NULL)
78511ae08745Sheppo 				(void) ldc_mem_free_handle(
78521ae08745Sheppo 						priv_addr->memhandle);
78531ae08745Sheppo 			priv_addr++;
78541ae08745Sheppo 		}
78551ae08745Sheppo 		kmem_free(dp->priv_addr,
78561ae08745Sheppo 			(sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL));
78571ae08745Sheppo 	}
78581ae08745Sheppo 	mutex_destroy(&dp->dlock);
78591ae08745Sheppo 
78601ae08745Sheppo 	kmem_free(dp, sizeof (dring_info_t));
78611ae08745Sheppo 	return (NULL);
78621ae08745Sheppo }
78631ae08745Sheppo 
78641ae08745Sheppo /*
78651ae08745Sheppo  * Create a ring consisting of just a private portion and link
78661ae08745Sheppo  * it into the list of rings for the outbound lane.
78671ae08745Sheppo  *
78681ae08745Sheppo  * These type of rings are used primarily for temporary data
78691ae08745Sheppo  * storage (i.e. as data buffers).
78701ae08745Sheppo  */
78711ae08745Sheppo void
78721ae08745Sheppo vsw_create_privring(vsw_ldc_t *ldcp)
78731ae08745Sheppo {
78741ae08745Sheppo 	dring_info_t		*dp, *tp;
78751ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
78761ae08745Sheppo 
78771ae08745Sheppo 	D1(vswp, "%s(%lld): enter", __func__, ldcp->ldc_id);
78781ae08745Sheppo 
78791ae08745Sheppo 	dp = kmem_zalloc(sizeof (dring_info_t), KM_SLEEP);
78801ae08745Sheppo 
78811ae08745Sheppo 	mutex_init(&dp->dlock, NULL, MUTEX_DRIVER, NULL);
78821ae08745Sheppo 
78831ae08745Sheppo 	/* no public section */
78841ae08745Sheppo 	dp->pub_addr = NULL;
78851ae08745Sheppo 
78861ae08745Sheppo 	dp->priv_addr = kmem_zalloc((sizeof (vsw_private_desc_t) *
78871ae08745Sheppo 					VSW_RING_NUM_EL), KM_SLEEP);
78881ae08745Sheppo 
78894bac2208Snarayan 	dp->num_descriptors = VSW_RING_NUM_EL;
78904bac2208Snarayan 
78911ae08745Sheppo 	if (vsw_setup_ring(ldcp, dp)) {
78921ae08745Sheppo 		DERR(vswp, "%s: setup of ring failed", __func__);
78931ae08745Sheppo 		kmem_free(dp->priv_addr,
78941ae08745Sheppo 			(sizeof (vsw_private_desc_t) * VSW_RING_NUM_EL));
78951ae08745Sheppo 		mutex_destroy(&dp->dlock);
78961ae08745Sheppo 		kmem_free(dp, sizeof (dring_info_t));
78971ae08745Sheppo 		return;
78981ae08745Sheppo 	}
78991ae08745Sheppo 
79001ae08745Sheppo 	/* haven't used any descriptors yet */
79011ae08745Sheppo 	dp->end_idx = 0;
79021ae08745Sheppo 
7903d10e4ef2Snarayan 	mutex_init(&dp->restart_lock, NULL, MUTEX_DRIVER, NULL);
7904d10e4ef2Snarayan 	dp->restart_reqd = B_TRUE;
7905d10e4ef2Snarayan 
79061ae08745Sheppo 	/*
79071ae08745Sheppo 	 * Only ever create rings for outgoing lane. Link it onto
79081ae08745Sheppo 	 * end of list.
79091ae08745Sheppo 	 */
79101ae08745Sheppo 	if (ldcp->lane_out.dringp == NULL) {
79111ae08745Sheppo 		D2(vswp, "%s: adding first outbound privring", __func__);
79121ae08745Sheppo 		ldcp->lane_out.dringp = dp;
79131ae08745Sheppo 	} else {
79141ae08745Sheppo 		tp = ldcp->lane_out.dringp;
79151ae08745Sheppo 		while (tp->next != NULL)
79161ae08745Sheppo 			tp = tp->next;
79171ae08745Sheppo 
79181ae08745Sheppo 		tp->next = dp;
79191ae08745Sheppo 	}
79201ae08745Sheppo 
79211ae08745Sheppo 	D1(vswp, "%s(%lld): exit", __func__, ldcp->ldc_id);
79221ae08745Sheppo }
79231ae08745Sheppo 
79241ae08745Sheppo /*
79251ae08745Sheppo  * Setup the descriptors in the dring. Returns 0 on success, 1 on
79261ae08745Sheppo  * failure.
79271ae08745Sheppo  */
79281ae08745Sheppo int
79291ae08745Sheppo vsw_setup_ring(vsw_ldc_t *ldcp, dring_info_t *dp)
79301ae08745Sheppo {
79311ae08745Sheppo 	vnet_public_desc_t	*pub_addr = NULL;
79321ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
79331ae08745Sheppo 	vsw_t			*vswp = ldcp->ldc_vswp;
79341ae08745Sheppo 	uint64_t		*tmpp;
79351ae08745Sheppo 	uint64_t		offset = 0;
79361ae08745Sheppo 	uint32_t		ncookies = 0;
79371ae08745Sheppo 	static char		*name = "vsw_setup_ring";
7938d10e4ef2Snarayan 	int			i, j, nc, rv;
79391ae08745Sheppo 
79401ae08745Sheppo 	priv_addr = dp->priv_addr;
79411ae08745Sheppo 	pub_addr = dp->pub_addr;
79421ae08745Sheppo 
7943d10e4ef2Snarayan 	/* public section may be null but private should never be */
7944d10e4ef2Snarayan 	ASSERT(priv_addr != NULL);
7945d10e4ef2Snarayan 
79461ae08745Sheppo 	/*
79471ae08745Sheppo 	 * Allocate the region of memory which will be used to hold
79481ae08745Sheppo 	 * the data the descriptors will refer to.
79491ae08745Sheppo 	 */
79501ae08745Sheppo 	dp->data_sz = (VSW_RING_NUM_EL * VSW_RING_EL_DATA_SZ);
79511ae08745Sheppo 	dp->data_addr = kmem_alloc(dp->data_sz, KM_SLEEP);
79521ae08745Sheppo 
79531ae08745Sheppo 	D2(vswp, "%s: allocated %lld bytes at 0x%llx\n", name,
79541ae08745Sheppo 		dp->data_sz, dp->data_addr);
79551ae08745Sheppo 
79561ae08745Sheppo 	tmpp = (uint64_t *)dp->data_addr;
79571ae08745Sheppo 	offset = VSW_RING_EL_DATA_SZ / sizeof (tmpp);
79581ae08745Sheppo 
79591ae08745Sheppo 	/*
79601ae08745Sheppo 	 * Initialise some of the private and public (if they exist)
79611ae08745Sheppo 	 * descriptor fields.
79621ae08745Sheppo 	 */
79631ae08745Sheppo 	for (i = 0; i < VSW_RING_NUM_EL; i++) {
7964d10e4ef2Snarayan 		mutex_init(&priv_addr->dstate_lock, NULL, MUTEX_DRIVER, NULL);
7965d10e4ef2Snarayan 
79661ae08745Sheppo 		if ((ldc_mem_alloc_handle(ldcp->ldc_handle,
79671ae08745Sheppo 			&priv_addr->memhandle)) != 0) {
79681ae08745Sheppo 			DERR(vswp, "%s: alloc mem handle failed", name);
79691ae08745Sheppo 			goto setup_ring_cleanup;
79701ae08745Sheppo 		}
79711ae08745Sheppo 
79721ae08745Sheppo 		priv_addr->datap = (void *)tmpp;
79731ae08745Sheppo 
79741ae08745Sheppo 		rv = ldc_mem_bind_handle(priv_addr->memhandle,
79751ae08745Sheppo 			(caddr_t)priv_addr->datap, VSW_RING_EL_DATA_SZ,
79761ae08745Sheppo 			LDC_SHADOW_MAP, LDC_MEM_R|LDC_MEM_W,
79771ae08745Sheppo 			&(priv_addr->memcookie[0]), &ncookies);
79781ae08745Sheppo 		if (rv != 0) {
79791ae08745Sheppo 			DERR(vswp, "%s(%lld): ldc_mem_bind_handle failed "
79801ae08745Sheppo 				"(rv %d)", name, ldcp->ldc_id, rv);
79811ae08745Sheppo 			goto setup_ring_cleanup;
79821ae08745Sheppo 		}
79831ae08745Sheppo 		priv_addr->bound = 1;
79841ae08745Sheppo 
79851ae08745Sheppo 		D2(vswp, "%s: %d: memcookie 0 : addr 0x%llx : size 0x%llx",
79861ae08745Sheppo 			name, i, priv_addr->memcookie[0].addr,
79871ae08745Sheppo 			priv_addr->memcookie[0].size);
79881ae08745Sheppo 
79891ae08745Sheppo 		if (ncookies >= (uint32_t)(VSW_MAX_COOKIES + 1)) {
79901ae08745Sheppo 			DERR(vswp, "%s(%lld) ldc_mem_bind_handle returned "
79911ae08745Sheppo 				"invalid num of cookies (%d) for size 0x%llx",
79921ae08745Sheppo 				name, ldcp->ldc_id, ncookies,
79931ae08745Sheppo 				VSW_RING_EL_DATA_SZ);
79941ae08745Sheppo 
79951ae08745Sheppo 			goto setup_ring_cleanup;
79961ae08745Sheppo 		} else {
79971ae08745Sheppo 			for (j = 1; j < ncookies; j++) {
79981ae08745Sheppo 				rv = ldc_mem_nextcookie(priv_addr->memhandle,
79991ae08745Sheppo 					&(priv_addr->memcookie[j]));
80001ae08745Sheppo 				if (rv != 0) {
80011ae08745Sheppo 					DERR(vswp, "%s: ldc_mem_nextcookie "
80021ae08745Sheppo 						"failed rv (%d)", name, rv);
80031ae08745Sheppo 					goto setup_ring_cleanup;
80041ae08745Sheppo 				}
80051ae08745Sheppo 				D3(vswp, "%s: memcookie %d : addr 0x%llx : "
80061ae08745Sheppo 					"size 0x%llx", name, j,
80071ae08745Sheppo 					priv_addr->memcookie[j].addr,
80081ae08745Sheppo 					priv_addr->memcookie[j].size);
80091ae08745Sheppo 			}
80101ae08745Sheppo 
80111ae08745Sheppo 		}
80121ae08745Sheppo 		priv_addr->ncookies = ncookies;
80131ae08745Sheppo 		priv_addr->dstate = VIO_DESC_FREE;
80141ae08745Sheppo 
80151ae08745Sheppo 		if (pub_addr != NULL) {
80161ae08745Sheppo 
80171ae08745Sheppo 			/* link pub and private sides */
80181ae08745Sheppo 			priv_addr->descp = pub_addr;
80191ae08745Sheppo 
8020d10e4ef2Snarayan 			pub_addr->ncookies = priv_addr->ncookies;
8021d10e4ef2Snarayan 
8022d10e4ef2Snarayan 			for (nc = 0; nc < pub_addr->ncookies; nc++) {
8023d10e4ef2Snarayan 				bcopy(&priv_addr->memcookie[nc],
8024d10e4ef2Snarayan 					&pub_addr->memcookie[nc],
8025d10e4ef2Snarayan 					sizeof (ldc_mem_cookie_t));
8026d10e4ef2Snarayan 			}
8027d10e4ef2Snarayan 
80281ae08745Sheppo 			pub_addr->hdr.dstate = VIO_DESC_FREE;
80291ae08745Sheppo 			pub_addr++;
80301ae08745Sheppo 		}
80311ae08745Sheppo 
80321ae08745Sheppo 		/*
80331ae08745Sheppo 		 * move to next element in the dring and the next
80341ae08745Sheppo 		 * position in the data buffer.
80351ae08745Sheppo 		 */
80361ae08745Sheppo 		priv_addr++;
80371ae08745Sheppo 		tmpp += offset;
80381ae08745Sheppo 	}
80391ae08745Sheppo 
80401ae08745Sheppo 	return (0);
80411ae08745Sheppo 
80421ae08745Sheppo setup_ring_cleanup:
80431ae08745Sheppo 	priv_addr = dp->priv_addr;
80441ae08745Sheppo 
8045d10e4ef2Snarayan 	for (j = 0; j < i; j++) {
80461ae08745Sheppo 		(void) ldc_mem_unbind_handle(priv_addr->memhandle);
80471ae08745Sheppo 		(void) ldc_mem_free_handle(priv_addr->memhandle);
80481ae08745Sheppo 
8049d10e4ef2Snarayan 		mutex_destroy(&priv_addr->dstate_lock);
8050d10e4ef2Snarayan 
80511ae08745Sheppo 		priv_addr++;
80521ae08745Sheppo 	}
80531ae08745Sheppo 	kmem_free(dp->data_addr, dp->data_sz);
80541ae08745Sheppo 
80551ae08745Sheppo 	return (1);
80561ae08745Sheppo }
80571ae08745Sheppo 
80581ae08745Sheppo /*
80591ae08745Sheppo  * Searches the private section of a ring for a free descriptor,
80601ae08745Sheppo  * starting at the location of the last free descriptor found
80611ae08745Sheppo  * previously.
80621ae08745Sheppo  *
8063d10e4ef2Snarayan  * Returns 0 if free descriptor is available, and updates state
8064d10e4ef2Snarayan  * of private descriptor to VIO_DESC_READY,  otherwise returns 1.
80651ae08745Sheppo  *
80661ae08745Sheppo  * FUTURE: might need to return contiguous range of descriptors
80671ae08745Sheppo  * as dring info msg assumes all will be contiguous.
80681ae08745Sheppo  */
80691ae08745Sheppo static int
80701ae08745Sheppo vsw_dring_find_free_desc(dring_info_t *dringp,
80711ae08745Sheppo 		vsw_private_desc_t **priv_p, int *idx)
80721ae08745Sheppo {
8073d10e4ef2Snarayan 	vsw_private_desc_t	*addr = NULL;
80741ae08745Sheppo 	int			num = VSW_RING_NUM_EL;
80751ae08745Sheppo 	int			ret = 1;
80761ae08745Sheppo 
80771ae08745Sheppo 	D1(NULL, "%s enter\n", __func__);
80781ae08745Sheppo 
8079d10e4ef2Snarayan 	ASSERT(dringp->priv_addr != NULL);
80801ae08745Sheppo 
80811ae08745Sheppo 	D2(NULL, "%s: searching ring, dringp 0x%llx : start pos %lld",
8082d10e4ef2Snarayan 			__func__, dringp, dringp->end_idx);
80831ae08745Sheppo 
8084d10e4ef2Snarayan 	addr = (vsw_private_desc_t *)dringp->priv_addr + dringp->end_idx;
8085d10e4ef2Snarayan 
8086d10e4ef2Snarayan 	mutex_enter(&addr->dstate_lock);
80871ae08745Sheppo 	if (addr->dstate == VIO_DESC_FREE) {
8088d10e4ef2Snarayan 		addr->dstate = VIO_DESC_READY;
80891ae08745Sheppo 		*priv_p = addr;
8090d10e4ef2Snarayan 		*idx = dringp->end_idx;
8091d10e4ef2Snarayan 		dringp->end_idx = (dringp->end_idx + 1) % num;
80921ae08745Sheppo 		ret = 0;
8093d10e4ef2Snarayan 
80941ae08745Sheppo 	}
8095d10e4ef2Snarayan 	mutex_exit(&addr->dstate_lock);
80961ae08745Sheppo 
80971ae08745Sheppo 	/* ring full */
80981ae08745Sheppo 	if (ret == 1) {
8099d10e4ef2Snarayan 		D2(NULL, "%s: no desp free: started at %d", __func__,
8100d10e4ef2Snarayan 			dringp->end_idx);
81011ae08745Sheppo 	}
81021ae08745Sheppo 
81031ae08745Sheppo 	D1(NULL, "%s: exit\n", __func__);
81041ae08745Sheppo 
81051ae08745Sheppo 	return (ret);
81061ae08745Sheppo }
81071ae08745Sheppo 
81081ae08745Sheppo /*
81091ae08745Sheppo  * Map from a dring identifier to the ring itself. Returns
81101ae08745Sheppo  * pointer to ring or NULL if no match found.
81111ae08745Sheppo  */
81121ae08745Sheppo static dring_info_t *
81131ae08745Sheppo vsw_ident2dring(lane_t *lane, uint64_t ident)
81141ae08745Sheppo {
81151ae08745Sheppo 	dring_info_t	*dp = NULL;
81161ae08745Sheppo 
81171ae08745Sheppo 	if ((dp = lane->dringp) == NULL) {
81181ae08745Sheppo 		return (NULL);
81191ae08745Sheppo 	} else {
81201ae08745Sheppo 		if (dp->ident == ident)
81211ae08745Sheppo 			return (dp);
81221ae08745Sheppo 
81231ae08745Sheppo 		while (dp != NULL) {
81241ae08745Sheppo 			if (dp->ident == ident)
81251ae08745Sheppo 				break;
81261ae08745Sheppo 			dp = dp->next;
81271ae08745Sheppo 		}
81281ae08745Sheppo 	}
81291ae08745Sheppo 
81301ae08745Sheppo 	return (dp);
81311ae08745Sheppo }
81321ae08745Sheppo 
81331ae08745Sheppo /*
81341ae08745Sheppo  * Set the default lane attributes. These are copied into
81351ae08745Sheppo  * the attr msg we send to our peer. If they are not acceptable
81361ae08745Sheppo  * then (currently) the handshake ends.
81371ae08745Sheppo  */
81381ae08745Sheppo static void
81391ae08745Sheppo vsw_set_lane_attr(vsw_t *vswp, lane_t *lp)
81401ae08745Sheppo {
81411ae08745Sheppo 	bzero(lp, sizeof (lane_t));
81421ae08745Sheppo 
81431ae08745Sheppo 	READ_ENTER(&vswp->if_lockrw);
81441ae08745Sheppo 	ether_copy(&(vswp->if_addr), &(lp->addr));
81451ae08745Sheppo 	RW_EXIT(&vswp->if_lockrw);
81461ae08745Sheppo 
81471ae08745Sheppo 	lp->mtu = VSW_MTU;
81481ae08745Sheppo 	lp->addr_type = ADDR_TYPE_MAC;
81491ae08745Sheppo 	lp->xfer_mode = VIO_DRING_MODE;
81501ae08745Sheppo 	lp->ack_freq = 0;	/* for shared mode */
8151d10e4ef2Snarayan 
8152d10e4ef2Snarayan 	mutex_enter(&lp->seq_lock);
81531ae08745Sheppo 	lp->seq_num = VNET_ISS;
8154d10e4ef2Snarayan 	mutex_exit(&lp->seq_lock);
81551ae08745Sheppo }
81561ae08745Sheppo 
81571ae08745Sheppo /*
81581ae08745Sheppo  * Verify that the attributes are acceptable.
81591ae08745Sheppo  *
81601ae08745Sheppo  * FUTURE: If some attributes are not acceptable, change them
81611ae08745Sheppo  * our desired values.
81621ae08745Sheppo  */
81631ae08745Sheppo static int
81641ae08745Sheppo vsw_check_attr(vnet_attr_msg_t *pkt, vsw_port_t *port)
81651ae08745Sheppo {
81661ae08745Sheppo 	int	ret = 0;
81671ae08745Sheppo 
81681ae08745Sheppo 	D1(NULL, "vsw_check_attr enter\n");
81691ae08745Sheppo 
81701ae08745Sheppo 	/*
81711ae08745Sheppo 	 * Note we currently only support in-band descriptors
81721ae08745Sheppo 	 * and descriptor rings, not packet based transfer (VIO_PKT_MODE)
81731ae08745Sheppo 	 */
81741ae08745Sheppo 	if ((pkt->xfer_mode != VIO_DESC_MODE) &&
81751ae08745Sheppo 			(pkt->xfer_mode != VIO_DRING_MODE)) {
81761ae08745Sheppo 		D2(NULL, "vsw_check_attr: unknown mode %x\n",
81771ae08745Sheppo 			pkt->xfer_mode);
81781ae08745Sheppo 		ret = 1;
81791ae08745Sheppo 	}
81801ae08745Sheppo 
81811ae08745Sheppo 	/* Only support MAC addresses at moment. */
81821ae08745Sheppo 	if ((pkt->addr_type != ADDR_TYPE_MAC) || (pkt->addr == 0)) {
81831ae08745Sheppo 		D2(NULL, "vsw_check_attr: invalid addr_type %x, "
81841ae08745Sheppo 			"or address 0x%llx\n", pkt->addr_type,
81851ae08745Sheppo 			pkt->addr);
81861ae08745Sheppo 		ret = 1;
81871ae08745Sheppo 	}
81881ae08745Sheppo 
81891ae08745Sheppo 	/*
81901ae08745Sheppo 	 * MAC address supplied by device should match that stored
81911ae08745Sheppo 	 * in the vsw-port OBP node. Need to decide what to do if they
81921ae08745Sheppo 	 * don't match, for the moment just warn but don't fail.
81931ae08745Sheppo 	 */
81941ae08745Sheppo 	if (bcmp(&pkt->addr, &port->p_macaddr, ETHERADDRL) != 0) {
81951ae08745Sheppo 		DERR(NULL, "vsw_check_attr: device supplied address "
81961ae08745Sheppo 			"0x%llx doesn't match node address 0x%llx\n",
81971ae08745Sheppo 			pkt->addr, port->p_macaddr);
81981ae08745Sheppo 	}
81991ae08745Sheppo 
82001ae08745Sheppo 	/*
82011ae08745Sheppo 	 * Ack freq only makes sense in pkt mode, in shared
82021ae08745Sheppo 	 * mode the ring descriptors say whether or not to
82031ae08745Sheppo 	 * send back an ACK.
82041ae08745Sheppo 	 */
82051ae08745Sheppo 	if ((pkt->xfer_mode == VIO_DRING_MODE) &&
82061ae08745Sheppo 				(pkt->ack_freq > 0)) {
82071ae08745Sheppo 		D2(NULL, "vsw_check_attr: non zero ack freq "
82081ae08745Sheppo 			" in SHM mode\n");
82091ae08745Sheppo 		ret = 1;
82101ae08745Sheppo 	}
82111ae08745Sheppo 
82121ae08745Sheppo 	/*
82131ae08745Sheppo 	 * Note: for the moment we only support ETHER
82141ae08745Sheppo 	 * frames. This may change in the future.
82151ae08745Sheppo 	 */
82161ae08745Sheppo 	if ((pkt->mtu > VSW_MTU) || (pkt->mtu <= 0)) {
82171ae08745Sheppo 		D2(NULL, "vsw_check_attr: invalid MTU (0x%llx)\n",
82181ae08745Sheppo 			pkt->mtu);
82191ae08745Sheppo 		ret = 1;
82201ae08745Sheppo 	}
82211ae08745Sheppo 
82221ae08745Sheppo 	D1(NULL, "vsw_check_attr exit\n");
82231ae08745Sheppo 
82241ae08745Sheppo 	return (ret);
82251ae08745Sheppo }
82261ae08745Sheppo 
82271ae08745Sheppo /*
82281ae08745Sheppo  * Returns 1 if there is a problem, 0 otherwise.
82291ae08745Sheppo  */
82301ae08745Sheppo static int
82311ae08745Sheppo vsw_check_dring_info(vio_dring_reg_msg_t *pkt)
82321ae08745Sheppo {
82331ae08745Sheppo 	_NOTE(ARGUNUSED(pkt))
82341ae08745Sheppo 
82351ae08745Sheppo 	int	ret = 0;
82361ae08745Sheppo 
82371ae08745Sheppo 	D1(NULL, "vsw_check_dring_info enter\n");
82381ae08745Sheppo 
82391ae08745Sheppo 	if ((pkt->num_descriptors == 0) ||
82401ae08745Sheppo 		(pkt->descriptor_size == 0) ||
82411ae08745Sheppo 		(pkt->ncookies != 1)) {
82421ae08745Sheppo 		DERR(NULL, "vsw_check_dring_info: invalid dring msg");
82431ae08745Sheppo 		ret = 1;
82441ae08745Sheppo 	}
82451ae08745Sheppo 
82461ae08745Sheppo 	D1(NULL, "vsw_check_dring_info exit\n");
82471ae08745Sheppo 
82481ae08745Sheppo 	return (ret);
82491ae08745Sheppo }
82501ae08745Sheppo 
82511ae08745Sheppo /*
82521ae08745Sheppo  * Returns 1 if two memory cookies match. Otherwise returns 0.
82531ae08745Sheppo  */
82541ae08745Sheppo static int
82551ae08745Sheppo vsw_mem_cookie_match(ldc_mem_cookie_t *m1, ldc_mem_cookie_t *m2)
82561ae08745Sheppo {
82571ae08745Sheppo 	if ((m1->addr != m2->addr) ||
82581ae08745Sheppo 		(m2->size != m2->size)) {
82591ae08745Sheppo 		return (0);
82601ae08745Sheppo 	} else {
82611ae08745Sheppo 		return (1);
82621ae08745Sheppo 	}
82631ae08745Sheppo }
82641ae08745Sheppo 
82651ae08745Sheppo /*
82661ae08745Sheppo  * Returns 1 if ring described in reg message matches that
82671ae08745Sheppo  * described by dring_info structure. Otherwise returns 0.
82681ae08745Sheppo  */
82691ae08745Sheppo static int
82701ae08745Sheppo vsw_dring_match(dring_info_t *dp, vio_dring_reg_msg_t *msg)
82711ae08745Sheppo {
82721ae08745Sheppo 	if ((msg->descriptor_size != dp->descriptor_size) ||
82731ae08745Sheppo 		(msg->num_descriptors != dp->num_descriptors) ||
82741ae08745Sheppo 		(msg->ncookies != dp->ncookies) ||
82751ae08745Sheppo 		!(vsw_mem_cookie_match(&msg->cookie[0], &dp->cookie[0]))) {
82761ae08745Sheppo 		return (0);
82771ae08745Sheppo 	} else {
82781ae08745Sheppo 		return (1);
82791ae08745Sheppo 	}
82801ae08745Sheppo 
82811ae08745Sheppo }
82821ae08745Sheppo 
82831ae08745Sheppo static caddr_t
82841ae08745Sheppo vsw_print_ethaddr(uint8_t *a, char *ebuf)
82851ae08745Sheppo {
82861ae08745Sheppo 	(void) sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
82871ae08745Sheppo 	    a[0], a[1], a[2], a[3], a[4], a[5]);
82881ae08745Sheppo 	return (ebuf);
82891ae08745Sheppo }
82901ae08745Sheppo 
82911ae08745Sheppo /*
82921ae08745Sheppo  * Reset and free all the resources associated with
82931ae08745Sheppo  * the channel.
82941ae08745Sheppo  */
82951ae08745Sheppo static void
82961ae08745Sheppo vsw_free_lane_resources(vsw_ldc_t *ldcp, uint64_t dir)
82971ae08745Sheppo {
82981ae08745Sheppo 	dring_info_t		*dp, *dpp;
82991ae08745Sheppo 	lane_t			*lp = NULL;
83001ae08745Sheppo 	int			rv = 0;
83011ae08745Sheppo 
83021ae08745Sheppo 	ASSERT(ldcp != NULL);
83031ae08745Sheppo 
83041ae08745Sheppo 	D1(ldcp->ldc_vswp, "%s (%lld): enter", __func__, ldcp->ldc_id);
83051ae08745Sheppo 
83061ae08745Sheppo 	if (dir == INBOUND) {
83071ae08745Sheppo 		D2(ldcp->ldc_vswp, "%s: freeing INBOUND lane"
83081ae08745Sheppo 			" of channel %lld", __func__, ldcp->ldc_id);
83091ae08745Sheppo 		lp = &ldcp->lane_in;
83101ae08745Sheppo 	} else {
83111ae08745Sheppo 		D2(ldcp->ldc_vswp, "%s: freeing OUTBOUND lane"
83121ae08745Sheppo 			" of channel %lld", __func__, ldcp->ldc_id);
83131ae08745Sheppo 		lp = &ldcp->lane_out;
83141ae08745Sheppo 	}
83151ae08745Sheppo 
83161ae08745Sheppo 	lp->lstate = VSW_LANE_INACTIV;
8317d10e4ef2Snarayan 	mutex_enter(&lp->seq_lock);
83181ae08745Sheppo 	lp->seq_num = VNET_ISS;
8319d10e4ef2Snarayan 	mutex_exit(&lp->seq_lock);
83201ae08745Sheppo 	if (lp->dringp) {
83211ae08745Sheppo 		if (dir == INBOUND) {
83221ae08745Sheppo 			dp = lp->dringp;
83231ae08745Sheppo 			while (dp != NULL) {
83241ae08745Sheppo 				dpp = dp->next;
83251ae08745Sheppo 				if (dp->handle != NULL)
83261ae08745Sheppo 					(void) ldc_mem_dring_unmap(dp->handle);
83271ae08745Sheppo 				kmem_free(dp, sizeof (dring_info_t));
83281ae08745Sheppo 				dp = dpp;
83291ae08745Sheppo 			}
83301ae08745Sheppo 		} else {
83311ae08745Sheppo 			/*
83321ae08745Sheppo 			 * unbind, destroy exported dring, free dring struct
83331ae08745Sheppo 			 */
83341ae08745Sheppo 			dp = lp->dringp;
83351ae08745Sheppo 			rv = vsw_free_ring(dp);
83361ae08745Sheppo 		}
83371ae08745Sheppo 		if (rv == 0) {
83381ae08745Sheppo 			lp->dringp = NULL;
83391ae08745Sheppo 		}
83401ae08745Sheppo 	}
83411ae08745Sheppo 
83421ae08745Sheppo 	D1(ldcp->ldc_vswp, "%s (%lld): exit", __func__, ldcp->ldc_id);
83431ae08745Sheppo }
83441ae08745Sheppo 
83451ae08745Sheppo /*
83461ae08745Sheppo  * Free ring and all associated resources.
83471ae08745Sheppo  */
83481ae08745Sheppo static int
83491ae08745Sheppo vsw_free_ring(dring_info_t *dp)
83501ae08745Sheppo {
83511ae08745Sheppo 	vsw_private_desc_t	*paddr = NULL;
83521ae08745Sheppo 	dring_info_t		*dpp;
83531ae08745Sheppo 	int			i, rv = 1;
83541ae08745Sheppo 
83551ae08745Sheppo 	while (dp != NULL) {
83561ae08745Sheppo 		mutex_enter(&dp->dlock);
83571ae08745Sheppo 		dpp = dp->next;
83581ae08745Sheppo 		if (dp->priv_addr != NULL) {
83591ae08745Sheppo 			/*
83601ae08745Sheppo 			 * First unbind and free the memory handles
83611ae08745Sheppo 			 * stored in each descriptor within the ring.
83621ae08745Sheppo 			 */
83631ae08745Sheppo 			for (i = 0; i < VSW_RING_NUM_EL; i++) {
83641ae08745Sheppo 				paddr = (vsw_private_desc_t *)
83651ae08745Sheppo 						dp->priv_addr + i;
83661ae08745Sheppo 				if (paddr->memhandle != NULL) {
83671ae08745Sheppo 					if (paddr->bound == 1) {
83681ae08745Sheppo 						rv = ldc_mem_unbind_handle(
83691ae08745Sheppo 							paddr->memhandle);
83701ae08745Sheppo 
83711ae08745Sheppo 						if (rv != 0) {
83721ae08745Sheppo 							DERR(NULL, "error "
83731ae08745Sheppo 							"unbinding handle for "
83741ae08745Sheppo 							"ring 0x%llx at pos %d",
83751ae08745Sheppo 							dp, i);
83761ae08745Sheppo 							mutex_exit(&dp->dlock);
83771ae08745Sheppo 							return (rv);
83781ae08745Sheppo 						}
83791ae08745Sheppo 						paddr->bound = 0;
83801ae08745Sheppo 					}
83811ae08745Sheppo 
83821ae08745Sheppo 					rv = ldc_mem_free_handle(
83831ae08745Sheppo 							paddr->memhandle);
83841ae08745Sheppo 					if (rv != 0) {
83851ae08745Sheppo 						DERR(NULL, "error freeing "
83861ae08745Sheppo 							"handle for ring "
83871ae08745Sheppo 							"0x%llx at pos %d",
83881ae08745Sheppo 							dp, i);
83891ae08745Sheppo 						mutex_exit(&dp->dlock);
83901ae08745Sheppo 						return (rv);
83911ae08745Sheppo 					}
83921ae08745Sheppo 					paddr->memhandle = NULL;
83931ae08745Sheppo 				}
8394d10e4ef2Snarayan 				mutex_destroy(&paddr->dstate_lock);
83951ae08745Sheppo 			}
83961ae08745Sheppo 			kmem_free(dp->priv_addr, (sizeof (vsw_private_desc_t)
83971ae08745Sheppo 					* VSW_RING_NUM_EL));
83981ae08745Sheppo 		}
83991ae08745Sheppo 
84001ae08745Sheppo 		/*
84011ae08745Sheppo 		 * Now unbind and destroy the ring itself.
84021ae08745Sheppo 		 */
84031ae08745Sheppo 		if (dp->handle != NULL) {
84041ae08745Sheppo 			(void) ldc_mem_dring_unbind(dp->handle);
84051ae08745Sheppo 			(void) ldc_mem_dring_destroy(dp->handle);
84061ae08745Sheppo 		}
84071ae08745Sheppo 
84081ae08745Sheppo 		if (dp->data_addr != NULL) {
84091ae08745Sheppo 			kmem_free(dp->data_addr, dp->data_sz);
84101ae08745Sheppo 		}
84111ae08745Sheppo 
84121ae08745Sheppo 		mutex_exit(&dp->dlock);
84131ae08745Sheppo 		mutex_destroy(&dp->dlock);
8414d10e4ef2Snarayan 		mutex_destroy(&dp->restart_lock);
84151ae08745Sheppo 		kmem_free(dp, sizeof (dring_info_t));
84161ae08745Sheppo 
84171ae08745Sheppo 		dp = dpp;
84181ae08745Sheppo 	}
84191ae08745Sheppo 	return (0);
84201ae08745Sheppo }
84211ae08745Sheppo 
84221ae08745Sheppo /*
84231ae08745Sheppo  * Debugging routines
84241ae08745Sheppo  */
84251ae08745Sheppo static void
84261ae08745Sheppo display_state(void)
84271ae08745Sheppo {
84281ae08745Sheppo 	vsw_t		*vswp;
84291ae08745Sheppo 	vsw_port_list_t	*plist;
84301ae08745Sheppo 	vsw_port_t 	*port;
84311ae08745Sheppo 	vsw_ldc_list_t	*ldcl;
84321ae08745Sheppo 	vsw_ldc_t 	*ldcp;
84331ae08745Sheppo 
84341ae08745Sheppo 	cmn_err(CE_NOTE, "***** system state *****");
84351ae08745Sheppo 
84361ae08745Sheppo 	for (vswp = vsw_head; vswp; vswp = vswp->next) {
84371ae08745Sheppo 		plist = &vswp->plist;
84381ae08745Sheppo 		READ_ENTER(&plist->lockrw);
84391ae08745Sheppo 		cmn_err(CE_CONT, "vsw instance %d has %d ports attached\n",
84401ae08745Sheppo 			vswp->instance, plist->num_ports);
84411ae08745Sheppo 
84421ae08745Sheppo 		for (port = plist->head; port != NULL; port = port->p_next) {
84431ae08745Sheppo 			ldcl = &port->p_ldclist;
84441ae08745Sheppo 			cmn_err(CE_CONT, "port %d : %d ldcs attached\n",
84451ae08745Sheppo 				port->p_instance, ldcl->num_ldcs);
84461ae08745Sheppo 			READ_ENTER(&ldcl->lockrw);
84471ae08745Sheppo 			ldcp = ldcl->head;
84481ae08745Sheppo 			for (; ldcp != NULL; ldcp = ldcp->ldc_next) {
84491ae08745Sheppo 				cmn_err(CE_CONT, "chan %lu : dev %d : "
84501ae08745Sheppo 					"status %d : phase %u\n",
84511ae08745Sheppo 					ldcp->ldc_id, ldcp->dev_class,
84521ae08745Sheppo 					ldcp->ldc_status, ldcp->hphase);
84531ae08745Sheppo 				cmn_err(CE_CONT, "chan %lu : lsession %lu : "
84541ae08745Sheppo 					"psession %lu\n",
84551ae08745Sheppo 					ldcp->ldc_id,
84561ae08745Sheppo 					ldcp->local_session,
84571ae08745Sheppo 					ldcp->peer_session);
84581ae08745Sheppo 
84591ae08745Sheppo 				cmn_err(CE_CONT, "Inbound lane:\n");
84601ae08745Sheppo 				display_lane(&ldcp->lane_in);
84611ae08745Sheppo 				cmn_err(CE_CONT, "Outbound lane:\n");
84621ae08745Sheppo 				display_lane(&ldcp->lane_out);
84631ae08745Sheppo 			}
84641ae08745Sheppo 			RW_EXIT(&ldcl->lockrw);
84651ae08745Sheppo 		}
84661ae08745Sheppo 		RW_EXIT(&plist->lockrw);
84671ae08745Sheppo 	}
84681ae08745Sheppo 	cmn_err(CE_NOTE, "***** system state *****");
84691ae08745Sheppo }
84701ae08745Sheppo 
84711ae08745Sheppo static void
84721ae08745Sheppo display_lane(lane_t *lp)
84731ae08745Sheppo {
84741ae08745Sheppo 	dring_info_t	*drp;
84751ae08745Sheppo 
84761ae08745Sheppo 	cmn_err(CE_CONT, "ver 0x%x:0x%x : state %lx : mtu 0x%lx\n",
84771ae08745Sheppo 		lp->ver_major, lp->ver_minor, lp->lstate, lp->mtu);
84781ae08745Sheppo 	cmn_err(CE_CONT, "addr_type %d : addr 0x%lx : xmode %d\n",
84791ae08745Sheppo 		lp->addr_type, lp->addr, lp->xfer_mode);
84801ae08745Sheppo 	cmn_err(CE_CONT, "dringp 0x%lx\n", (uint64_t)lp->dringp);
84811ae08745Sheppo 
84821ae08745Sheppo 	cmn_err(CE_CONT, "Dring info:\n");
84831ae08745Sheppo 	for (drp = lp->dringp; drp != NULL; drp = drp->next) {
84841ae08745Sheppo 		cmn_err(CE_CONT, "\tnum_desc %u : dsize %u\n",
84851ae08745Sheppo 			drp->num_descriptors, drp->descriptor_size);
84861ae08745Sheppo 		cmn_err(CE_CONT, "\thandle 0x%lx\n", drp->handle);
84871ae08745Sheppo 		cmn_err(CE_CONT, "\tpub_addr 0x%lx : priv_addr 0x%lx\n",
84881ae08745Sheppo 			(uint64_t)drp->pub_addr, (uint64_t)drp->priv_addr);
84891ae08745Sheppo 		cmn_err(CE_CONT, "\tident 0x%lx : end_idx %lu\n",
84901ae08745Sheppo 			drp->ident, drp->end_idx);
84911ae08745Sheppo 		display_ring(drp);
84921ae08745Sheppo 	}
84931ae08745Sheppo }
84941ae08745Sheppo 
84951ae08745Sheppo static void
84961ae08745Sheppo display_ring(dring_info_t *dringp)
84971ae08745Sheppo {
84981ae08745Sheppo 	uint64_t		i;
84991ae08745Sheppo 	uint64_t		priv_count = 0;
85001ae08745Sheppo 	uint64_t		pub_count = 0;
85011ae08745Sheppo 	vnet_public_desc_t	*pub_addr = NULL;
85021ae08745Sheppo 	vsw_private_desc_t	*priv_addr = NULL;
85031ae08745Sheppo 
85041ae08745Sheppo 	for (i = 0; i < VSW_RING_NUM_EL; i++) {
85051ae08745Sheppo 		if (dringp->pub_addr != NULL) {
85061ae08745Sheppo 			pub_addr = (vnet_public_desc_t *)dringp->pub_addr + i;
85071ae08745Sheppo 
85081ae08745Sheppo 			if (pub_addr->hdr.dstate == VIO_DESC_FREE)
85091ae08745Sheppo 				pub_count++;
85101ae08745Sheppo 		}
85111ae08745Sheppo 
85121ae08745Sheppo 		if (dringp->priv_addr != NULL) {
85131ae08745Sheppo 			priv_addr =
85141ae08745Sheppo 				(vsw_private_desc_t *)dringp->priv_addr + i;
85151ae08745Sheppo 
85161ae08745Sheppo 			if (priv_addr->dstate == VIO_DESC_FREE)
85171ae08745Sheppo 				priv_count++;
85181ae08745Sheppo 		}
85191ae08745Sheppo 	}
85201ae08745Sheppo 	cmn_err(CE_CONT, "\t%lu elements: %lu priv free: %lu pub free\n",
85211ae08745Sheppo 			i, priv_count, pub_count);
85221ae08745Sheppo }
85231ae08745Sheppo 
85241ae08745Sheppo static void
85251ae08745Sheppo dump_flags(uint64_t state)
85261ae08745Sheppo {
85271ae08745Sheppo 	int	i;
85281ae08745Sheppo 
85291ae08745Sheppo 	typedef struct flag_name {
85301ae08745Sheppo 		int	flag_val;
85311ae08745Sheppo 		char	*flag_name;
85321ae08745Sheppo 	} flag_name_t;
85331ae08745Sheppo 
85341ae08745Sheppo 	flag_name_t	flags[] = {
85351ae08745Sheppo 		VSW_VER_INFO_SENT, "VSW_VER_INFO_SENT",
85361ae08745Sheppo 		VSW_VER_INFO_RECV, "VSW_VER_INFO_RECV",
85371ae08745Sheppo 		VSW_VER_ACK_RECV, "VSW_VER_ACK_RECV",
85381ae08745Sheppo 		VSW_VER_ACK_SENT, "VSW_VER_ACK_SENT",
85391ae08745Sheppo 		VSW_VER_NACK_RECV, "VSW_VER_NACK_RECV",
85401ae08745Sheppo 		VSW_VER_NACK_SENT, "VSW_VER_NACK_SENT",
85411ae08745Sheppo 		VSW_ATTR_INFO_SENT, "VSW_ATTR_INFO_SENT",
85421ae08745Sheppo 		VSW_ATTR_INFO_RECV, "VSW_ATTR_INFO_RECV",
85431ae08745Sheppo 		VSW_ATTR_ACK_SENT, "VSW_ATTR_ACK_SENT",
85441ae08745Sheppo 		VSW_ATTR_ACK_RECV, "VSW_ATTR_ACK_RECV",
85451ae08745Sheppo 		VSW_ATTR_NACK_SENT, "VSW_ATTR_NACK_SENT",
85461ae08745Sheppo 		VSW_ATTR_NACK_RECV, "VSW_ATTR_NACK_RECV",
85471ae08745Sheppo 		VSW_DRING_INFO_SENT, "VSW_DRING_INFO_SENT",
85481ae08745Sheppo 		VSW_DRING_INFO_RECV, "VSW_DRING_INFO_RECV",
85491ae08745Sheppo 		VSW_DRING_ACK_SENT, "VSW_DRING_ACK_SENT",
85501ae08745Sheppo 		VSW_DRING_ACK_RECV, "VSW_DRING_ACK_RECV",
85511ae08745Sheppo 		VSW_DRING_NACK_SENT, "VSW_DRING_NACK_SENT",
85521ae08745Sheppo 		VSW_DRING_NACK_RECV, "VSW_DRING_NACK_RECV",
85531ae08745Sheppo 		VSW_RDX_INFO_SENT, "VSW_RDX_INFO_SENT",
85541ae08745Sheppo 		VSW_RDX_INFO_RECV, "VSW_RDX_INFO_RECV",
85551ae08745Sheppo 		VSW_RDX_ACK_SENT, "VSW_RDX_ACK_SENT",
85561ae08745Sheppo 		VSW_RDX_ACK_RECV, "VSW_RDX_ACK_RECV",
85571ae08745Sheppo 		VSW_RDX_NACK_SENT, "VSW_RDX_NACK_SENT",
85581ae08745Sheppo 		VSW_RDX_NACK_RECV, "VSW_RDX_NACK_RECV",
85591ae08745Sheppo 		VSW_MCST_INFO_SENT, "VSW_MCST_INFO_SENT",
85601ae08745Sheppo 		VSW_MCST_INFO_RECV, "VSW_MCST_INFO_RECV",
85611ae08745Sheppo 		VSW_MCST_ACK_SENT, "VSW_MCST_ACK_SENT",
85621ae08745Sheppo 		VSW_MCST_ACK_RECV, "VSW_MCST_ACK_RECV",
85631ae08745Sheppo 		VSW_MCST_NACK_SENT, "VSW_MCST_NACK_SENT",
85641ae08745Sheppo 		VSW_MCST_NACK_RECV, "VSW_MCST_NACK_RECV",
85651ae08745Sheppo 		VSW_LANE_ACTIVE, "VSW_LANE_ACTIVE"};
85661ae08745Sheppo 
85671ae08745Sheppo 	DERR(NULL, "DUMP_FLAGS: %llx\n", state);
85681ae08745Sheppo 	for (i = 0; i < sizeof (flags)/sizeof (flag_name_t); i++) {
85691ae08745Sheppo 		if (state & flags[i].flag_val)
85701ae08745Sheppo 			DERR(NULL, "DUMP_FLAGS %s", flags[i].flag_name);
85711ae08745Sheppo 	}
85721ae08745Sheppo }
8573