xref: /titanic_50/usr/src/uts/sun4v/io/vsw.c (revision 2654012f83cec5dc15b61dfe3e4a4915f186e7a6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/errno.h>
29 #include <sys/debug.h>
30 #include <sys/time.h>
31 #include <sys/sysmacros.h>
32 #include <sys/systm.h>
33 #include <sys/user.h>
34 #include <sys/stropts.h>
35 #include <sys/stream.h>
36 #include <sys/strlog.h>
37 #include <sys/strsubr.h>
38 #include <sys/cmn_err.h>
39 #include <sys/cpu.h>
40 #include <sys/kmem.h>
41 #include <sys/conf.h>
42 #include <sys/ddi.h>
43 #include <sys/sunddi.h>
44 #include <sys/ksynch.h>
45 #include <sys/stat.h>
46 #include <sys/kstat.h>
47 #include <sys/vtrace.h>
48 #include <sys/strsun.h>
49 #include <sys/dlpi.h>
50 #include <sys/ethernet.h>
51 #include <net/if.h>
52 #include <sys/varargs.h>
53 #include <sys/machsystm.h>
54 #include <sys/modctl.h>
55 #include <sys/modhash.h>
56 #include <sys/mac.h>
57 #include <sys/mac_ether.h>
58 #include <sys/taskq.h>
59 #include <sys/note.h>
60 #include <sys/mach_descrip.h>
61 #include <sys/mac.h>
62 #include <sys/mdeg.h>
63 #include <sys/ldc.h>
64 #include <sys/vsw_fdb.h>
65 #include <sys/vsw.h>
66 #include <sys/vio_mailbox.h>
67 #include <sys/vnet_mailbox.h>
68 #include <sys/vnet_common.h>
69 #include <sys/vio_util.h>
70 #include <sys/sdt.h>
71 #include <sys/atomic.h>
72 #include <sys/callb.h>
73 #include <sys/vlan.h>
74 
75 /*
76  * Function prototypes.
77  */
78 static	int vsw_attach(dev_info_t *, ddi_attach_cmd_t);
79 static	int vsw_detach(dev_info_t *, ddi_detach_cmd_t);
80 static	int vsw_get_md_physname(vsw_t *, md_t *, mde_cookie_t, char *);
81 static	int vsw_get_md_smodes(vsw_t *, md_t *, mde_cookie_t, uint8_t *, int *);
82 
83 /* MDEG routines */
84 static	int vsw_mdeg_register(vsw_t *vswp);
85 static	void vsw_mdeg_unregister(vsw_t *vswp);
86 static	int vsw_mdeg_cb(void *cb_argp, mdeg_result_t *);
87 static	int vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *);
88 static	int vsw_get_initial_md_properties(vsw_t *vswp, md_t *, mde_cookie_t);
89 static	int vsw_read_mdprops(vsw_t *vswp);
90 static	void vsw_vlan_read_ids(void *arg, int type, md_t *mdp,
91 	mde_cookie_t node, uint16_t *pvidp, uint16_t **vidspp,
92 	uint16_t *nvidsp, uint16_t *default_idp);
93 static	int vsw_port_read_props(vsw_port_t *portp, vsw_t *vswp,
94 	md_t *mdp, mde_cookie_t *node);
95 static	void vsw_read_pri_eth_types(vsw_t *vswp, md_t *mdp,
96 	mde_cookie_t node);
97 static	void vsw_mtu_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
98 	uint32_t *mtu);
99 static	int vsw_mtu_update(vsw_t *vswp, uint32_t mtu);
100 static	void vsw_update_md_prop(vsw_t *, md_t *, mde_cookie_t);
101 static void vsw_save_lmacaddr(vsw_t *vswp, uint64_t macaddr);
102 
103 /* Mac driver related routines */
104 static int vsw_mac_register(vsw_t *);
105 static int vsw_mac_unregister(vsw_t *);
106 static int vsw_m_stat(void *, uint_t, uint64_t *);
107 static void vsw_m_stop(void *arg);
108 static int vsw_m_start(void *arg);
109 static int vsw_m_unicst(void *arg, const uint8_t *);
110 static int vsw_m_multicst(void *arg, boolean_t, const uint8_t *);
111 static int vsw_m_promisc(void *arg, boolean_t);
112 static mblk_t *vsw_m_tx(void *arg, mblk_t *);
113 void vsw_mac_rx(vsw_t *vswp, mac_resource_handle_t mrh,
114     mblk_t *mp, vsw_macrx_flags_t flags);
115 
116 /*
117  * Functions imported from other files.
118  */
119 extern void vsw_setup_switching_timeout(void *arg);
120 extern void vsw_stop_switching_timeout(vsw_t *vswp);
121 extern int vsw_setup_switching(vsw_t *);
122 extern void vsw_switch_frame_nop(vsw_t *vswp, mblk_t *mp, int caller,
123     vsw_port_t *port, mac_resource_handle_t mrh);
124 extern int vsw_add_mcst(vsw_t *, uint8_t, uint64_t, void *);
125 extern int vsw_del_mcst(vsw_t *, uint8_t, uint64_t, void *);
126 extern void vsw_del_mcst_vsw(vsw_t *);
127 extern mcst_addr_t *vsw_del_addr(uint8_t devtype, void *arg, uint64_t addr);
128 extern int vsw_detach_ports(vsw_t *vswp);
129 extern int vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node);
130 extern int vsw_port_detach(vsw_t *vswp, int p_instance);
131 static int vsw_port_update(vsw_t *vswp, md_t *curr_mdp, mde_cookie_t curr_mdex,
132 	md_t *prev_mdp, mde_cookie_t prev_mdex);
133 extern	int vsw_port_attach(vsw_port_t *port);
134 extern vsw_port_t *vsw_lookup_port(vsw_t *vswp, int p_instance);
135 extern int vsw_mac_attach(vsw_t *vswp);
136 extern void vsw_mac_detach(vsw_t *vswp);
137 extern int vsw_mac_open(vsw_t *vswp);
138 extern void vsw_mac_close(vsw_t *vswp);
139 extern int vsw_set_hw(vsw_t *, vsw_port_t *, int);
140 extern int vsw_unset_hw(vsw_t *, vsw_port_t *, int);
141 extern void vsw_reconfig_hw(vsw_t *);
142 extern void vsw_unset_addrs(vsw_t *vswp);
143 extern void vsw_setup_layer2_post_process(vsw_t *vswp);
144 extern void vsw_create_vlans(void *arg, int type);
145 extern void vsw_destroy_vlans(void *arg, int type);
146 extern void vsw_vlan_add_ids(void *arg, int type);
147 extern void vsw_vlan_remove_ids(void *arg, int type);
148 extern void vsw_vlan_unaware_port_reset(vsw_port_t *portp);
149 extern uint32_t vsw_vlan_frame_untag(void *arg, int type, mblk_t **np,
150 	mblk_t **npt);
151 extern mblk_t *vsw_vlan_frame_pretag(void *arg, int type, mblk_t *mp);
152 extern void vsw_hio_cleanup(vsw_t *vswp);
153 extern void vsw_reset_ports(vsw_t *vswp);
154 extern void vsw_port_reset(vsw_port_t *portp);
155 void vsw_hio_port_update(vsw_port_t *portp, boolean_t hio_enabled);
156 
157 /*
158  * Internal tunables.
159  */
160 int	vsw_num_handshakes = VNET_NUM_HANDSHAKES; /* # of handshake attempts */
161 int	vsw_wretries = 100;		/* # of write attempts */
162 int	vsw_desc_delay = 0;		/* delay in us */
163 int	vsw_read_attempts = 5;		/* # of reads of descriptor */
164 int	vsw_setup_switching_delay = 3;	/* setup sw timeout interval in sec */
165 int	vsw_mac_open_retries = 300;	/* max # of mac_open() retries */
166 					/* 300*3 = 900sec(15min) of max tmout */
167 int	vsw_ldc_tx_delay = 5;		/* delay(ticks) for tx retries */
168 int	vsw_ldc_tx_retries = 10;	/* # of ldc tx retries */
169 boolean_t vsw_ldc_rxthr_enabled = B_TRUE;	/* LDC Rx thread enabled */
170 boolean_t vsw_ldc_txthr_enabled = B_TRUE;	/* LDC Tx thread enabled */
171 
172 uint32_t	vsw_fdb_nchains = 8;	/* # of chains in fdb hash table */
173 uint32_t	vsw_vlan_nchains = 4;	/* # of chains in vlan id hash table */
174 uint32_t	vsw_ethermtu = 1500;	/* mtu of the device */
175 
176 /* sw timeout for boot delay only, in milliseconds */
177 int vsw_setup_switching_boot_delay = 100 * MILLISEC;
178 
179 /* delay in usec to wait for all references on a fdb entry to be dropped */
180 uint32_t vsw_fdbe_refcnt_delay = 10;
181 
182 /*
183  * Default vlan id. This is only used internally when the "default-vlan-id"
184  * property is not present in the MD device node. Therefore, this should not be
185  * used as a tunable; if this value is changed, the corresponding variable
186  * should be updated to the same value in all vnets connected to this vsw.
187  */
188 uint16_t	vsw_default_vlan_id = 1;
189 
190 /*
191  * Workaround for a version handshake bug in obp's vnet.
192  * If vsw initiates version negotiation starting from the highest version,
193  * obp sends a nack and terminates version handshake. To workaround
194  * this, we do not initiate version handshake when the channel comes up.
195  * Instead, we wait for the peer to send its version info msg and go through
196  * the version protocol exchange. If we successfully negotiate a version,
197  * before sending the ack, we send our version info msg to the peer
198  * using the <major,minor> version that we are about to ack.
199  */
200 boolean_t vsw_obp_ver_proto_workaround = B_TRUE;
201 
202 /*
203  * In the absence of "priority-ether-types" property in MD, the following
204  * internal tunable can be set to specify a single priority ethertype.
205  */
206 uint64_t vsw_pri_eth_type = 0;
207 
208 /*
209  * Number of transmit priority buffers that are preallocated per device.
210  * This number is chosen to be a small value to throttle transmission
211  * of priority packets. Note: Must be a power of 2 for vio_create_mblks().
212  */
213 uint32_t vsw_pri_tx_nmblks = 64;
214 
215 /*
216  * Number of RARP packets sent to announce macaddr to the physical switch,
217  * after vsw's physical device is changed dynamically or after a guest (client
218  * vnet) is live migrated in.
219  */
220 uint32_t vsw_publish_macaddr_count = 3;
221 
222 boolean_t vsw_hio_enabled = B_TRUE;	/* Enable/disable HybridIO */
223 int vsw_hio_max_cleanup_retries = 10;	/* Max retries for HybridIO cleanp */
224 int vsw_hio_cleanup_delay = 10000;	/* 10ms */
225 
226 /*
227  * External tunables.
228  */
229 /*
230  * Enable/disable thread per ring. This is a mode selection
231  * that is done a vsw driver attach time.
232  */
233 boolean_t vsw_multi_ring_enable = B_FALSE;
234 int vsw_mac_rx_rings = VSW_MAC_RX_RINGS;
235 
236 /* Number of transmit descriptors -  must be power of 2 */
237 uint32_t vsw_ntxds = VSW_RING_NUM_EL;
238 
239 /*
240  * Max number of mblks received in one receive operation.
241  */
242 uint32_t vsw_chain_len = (VSW_NUM_MBLKS * 0.6);
243 
244 /*
245  * Internal tunables for receive buffer pools, that is,  the size and number of
246  * mblks for each pool. At least 3 sizes must be specified if these are used.
247  * The sizes must be specified in increasing order. Non-zero value of the first
248  * size will be used as a hint to use these values instead of the algorithm
249  * that determines the sizes based on MTU.
250  */
251 uint32_t vsw_mblk_size1 = 0;
252 uint32_t vsw_mblk_size2 = 0;
253 uint32_t vsw_mblk_size3 = 0;
254 uint32_t vsw_mblk_size4 = 0;
255 uint32_t vsw_num_mblks1 = VSW_NUM_MBLKS;	/* number of mblks for pool1 */
256 uint32_t vsw_num_mblks2 = VSW_NUM_MBLKS;	/* number of mblks for pool2 */
257 uint32_t vsw_num_mblks3 = VSW_NUM_MBLKS;	/* number of mblks for pool3 */
258 uint32_t vsw_num_mblks4 = VSW_NUM_MBLKS;	/* number of mblks for pool4 */
259 
260 /*
261  * Set this to non-zero to enable additional internal receive buffer pools
262  * based on the MTU of the device for better performance at the cost of more
263  * memory consumption. This is turned off by default, to use allocb(9F) for
264  * receive buffer allocations of sizes > 2K.
265  */
266 boolean_t vsw_jumbo_rxpools = B_FALSE;
267 
268 /*
269  * vsw_max_tx_qcount is the maximum # of packets that can be queued
270  * before the tx worker thread begins processing the queue. Its value
271  * is chosen to be 4x the default length of tx descriptor ring.
272  */
273 uint32_t vsw_max_tx_qcount = 4 * VSW_RING_NUM_EL;
274 
275 /*
276  * MAC callbacks
277  */
278 static	mac_callbacks_t	vsw_m_callbacks = {
279 	0,
280 	vsw_m_stat,
281 	vsw_m_start,
282 	vsw_m_stop,
283 	vsw_m_promisc,
284 	vsw_m_multicst,
285 	vsw_m_unicst,
286 	vsw_m_tx,
287 	NULL,
288 	NULL,
289 	NULL
290 };
291 
292 static	struct	cb_ops	vsw_cb_ops = {
293 	nulldev,			/* cb_open */
294 	nulldev,			/* cb_close */
295 	nodev,				/* cb_strategy */
296 	nodev,				/* cb_print */
297 	nodev,				/* cb_dump */
298 	nodev,				/* cb_read */
299 	nodev,				/* cb_write */
300 	nodev,				/* cb_ioctl */
301 	nodev,				/* cb_devmap */
302 	nodev,				/* cb_mmap */
303 	nodev,				/* cb_segmap */
304 	nochpoll,			/* cb_chpoll */
305 	ddi_prop_op,			/* cb_prop_op */
306 	NULL,				/* cb_stream */
307 	D_MP,				/* cb_flag */
308 	CB_REV,				/* rev */
309 	nodev,				/* int (*cb_aread)() */
310 	nodev				/* int (*cb_awrite)() */
311 };
312 
313 static	struct	dev_ops	vsw_ops = {
314 	DEVO_REV,		/* devo_rev */
315 	0,			/* devo_refcnt */
316 	NULL,			/* devo_getinfo */
317 	nulldev,		/* devo_identify */
318 	nulldev,		/* devo_probe */
319 	vsw_attach,		/* devo_attach */
320 	vsw_detach,		/* devo_detach */
321 	nodev,			/* devo_reset */
322 	&vsw_cb_ops,		/* devo_cb_ops */
323 	(struct bus_ops *)NULL,	/* devo_bus_ops */
324 	ddi_power		/* devo_power */
325 };
326 
327 extern	struct	mod_ops	mod_driverops;
328 static struct modldrv vswmodldrv = {
329 	&mod_driverops,
330 	"sun4v Virtual Switch",
331 	&vsw_ops,
332 };
333 
334 #define	LDC_ENTER_LOCK(ldcp)	\
335 				mutex_enter(&((ldcp)->ldc_cblock));\
336 				mutex_enter(&((ldcp)->ldc_rxlock));\
337 				mutex_enter(&((ldcp)->ldc_txlock));
338 #define	LDC_EXIT_LOCK(ldcp)	\
339 				mutex_exit(&((ldcp)->ldc_txlock));\
340 				mutex_exit(&((ldcp)->ldc_rxlock));\
341 				mutex_exit(&((ldcp)->ldc_cblock));
342 
343 /* Driver soft state ptr  */
344 static void	*vsw_state;
345 
346 /*
347  * Linked list of "vsw_t" structures - one per instance.
348  */
349 vsw_t		*vsw_head = NULL;
350 krwlock_t	vsw_rw;
351 
352 /*
353  * Property names
354  */
355 static char vdev_propname[] = "virtual-device";
356 static char vsw_propname[] = "virtual-network-switch";
357 static char physdev_propname[] = "vsw-phys-dev";
358 static char smode_propname[] = "vsw-switch-mode";
359 static char macaddr_propname[] = "local-mac-address";
360 static char remaddr_propname[] = "remote-mac-address";
361 static char ldcids_propname[] = "ldc-ids";
362 static char chan_propname[] = "channel-endpoint";
363 static char id_propname[] = "id";
364 static char reg_propname[] = "reg";
365 static char pri_types_propname[] = "priority-ether-types";
366 static char vsw_pvid_propname[] = "port-vlan-id";
367 static char vsw_vid_propname[] = "vlan-id";
368 static char vsw_dvid_propname[] = "default-vlan-id";
369 static char port_pvid_propname[] = "remote-port-vlan-id";
370 static char port_vid_propname[] = "remote-vlan-id";
371 static char hybrid_propname[] = "hybrid";
372 static char vsw_mtu_propname[] = "mtu";
373 
374 /*
375  * Matching criteria passed to the MDEG to register interest
376  * in changes to 'virtual-device-port' nodes identified by their
377  * 'id' property.
378  */
379 static md_prop_match_t vport_prop_match[] = {
380 	{ MDET_PROP_VAL,    "id"   },
381 	{ MDET_LIST_END,    NULL    }
382 };
383 
384 static mdeg_node_match_t vport_match = { "virtual-device-port",
385 						vport_prop_match };
386 
387 /*
388  * Matching criteria passed to the MDEG to register interest
389  * in changes to 'virtual-device' nodes (i.e. vsw nodes) identified
390  * by their 'name' and 'cfg-handle' properties.
391  */
392 static md_prop_match_t vdev_prop_match[] = {
393 	{ MDET_PROP_STR,    "name"   },
394 	{ MDET_PROP_VAL,    "cfg-handle" },
395 	{ MDET_LIST_END,    NULL    }
396 };
397 
398 static mdeg_node_match_t vdev_match = { "virtual-device",
399 						vdev_prop_match };
400 
401 
402 /*
403  * Specification of an MD node passed to the MDEG to filter any
404  * 'vport' nodes that do not belong to the specified node. This
405  * template is copied for each vsw instance and filled in with
406  * the appropriate 'cfg-handle' value before being passed to the MDEG.
407  */
408 static mdeg_prop_spec_t vsw_prop_template[] = {
409 	{ MDET_PROP_STR,    "name",		vsw_propname },
410 	{ MDET_PROP_VAL,    "cfg-handle",	NULL	},
411 	{ MDET_LIST_END,    NULL,		NULL	}
412 };
413 
414 #define	VSW_SET_MDEG_PROP_INST(specp, val)	(specp)[1].ps_val = (val);
415 
416 #ifdef	DEBUG
417 /*
418  * Print debug messages - set to 0x1f to enable all msgs
419  * or 0x0 to turn all off.
420  */
421 int vswdbg = 0x0;
422 
423 /*
424  * debug levels:
425  * 0x01:	Function entry/exit tracing
426  * 0x02:	Internal function messages
427  * 0x04:	Verbose internal messages
428  * 0x08:	Warning messages
429  * 0x10:	Error messages
430  */
431 
432 void
433 vswdebug(vsw_t *vswp, const char *fmt, ...)
434 {
435 	char buf[512];
436 	va_list ap;
437 
438 	va_start(ap, fmt);
439 	(void) vsprintf(buf, fmt, ap);
440 	va_end(ap);
441 
442 	if (vswp == NULL)
443 		cmn_err(CE_CONT, "%s\n", buf);
444 	else
445 		cmn_err(CE_CONT, "vsw%d: %s\n", vswp->instance, buf);
446 }
447 
448 #endif	/* DEBUG */
449 
450 static struct modlinkage modlinkage = {
451 	MODREV_1,
452 	&vswmodldrv,
453 	NULL
454 };
455 
456 int
457 _init(void)
458 {
459 	int status;
460 
461 	rw_init(&vsw_rw, NULL, RW_DRIVER, NULL);
462 
463 	status = ddi_soft_state_init(&vsw_state, sizeof (vsw_t), 1);
464 	if (status != 0) {
465 		return (status);
466 	}
467 
468 	mac_init_ops(&vsw_ops, DRV_NAME);
469 	status = mod_install(&modlinkage);
470 	if (status != 0) {
471 		ddi_soft_state_fini(&vsw_state);
472 	}
473 	return (status);
474 }
475 
476 int
477 _fini(void)
478 {
479 	int status;
480 
481 	status = mod_remove(&modlinkage);
482 	if (status != 0)
483 		return (status);
484 	mac_fini_ops(&vsw_ops);
485 	ddi_soft_state_fini(&vsw_state);
486 
487 	rw_destroy(&vsw_rw);
488 
489 	return (status);
490 }
491 
492 int
493 _info(struct modinfo *modinfop)
494 {
495 	return (mod_info(&modlinkage, modinfop));
496 }
497 
498 static int
499 vsw_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
500 {
501 	vsw_t		*vswp;
502 	int		instance;
503 	char		hashname[MAXNAMELEN];
504 	char		qname[TASKQ_NAMELEN];
505 	enum		{ PROG_init = 0x00,
506 				PROG_locks = 0x01,
507 				PROG_readmd = 0x02,
508 				PROG_fdb = 0x04,
509 				PROG_mfdb = 0x08,
510 				PROG_taskq = 0x10,
511 				PROG_swmode = 0x20,
512 				PROG_macreg = 0x40,
513 				PROG_mdreg = 0x80}
514 			progress;
515 
516 	progress = PROG_init;
517 	int		rv;
518 
519 	switch (cmd) {
520 	case DDI_ATTACH:
521 		break;
522 	case DDI_RESUME:
523 		/* nothing to do for this non-device */
524 		return (DDI_SUCCESS);
525 	case DDI_PM_RESUME:
526 	default:
527 		return (DDI_FAILURE);
528 	}
529 
530 	instance = ddi_get_instance(dip);
531 	if (ddi_soft_state_zalloc(vsw_state, instance) != DDI_SUCCESS) {
532 		DERR(NULL, "vsw%d: ddi_soft_state_zalloc failed", instance);
533 		return (DDI_FAILURE);
534 	}
535 	vswp = ddi_get_soft_state(vsw_state, instance);
536 
537 	if (vswp == NULL) {
538 		DERR(NULL, "vsw%d: ddi_get_soft_state failed", instance);
539 		goto vsw_attach_fail;
540 	}
541 
542 	vswp->dip = dip;
543 	vswp->instance = instance;
544 	ddi_set_driver_private(dip, (caddr_t)vswp);
545 
546 	mutex_init(&vswp->hw_lock, NULL, MUTEX_DRIVER, NULL);
547 	mutex_init(&vswp->mca_lock, NULL, MUTEX_DRIVER, NULL);
548 	mutex_init(&vswp->swtmout_lock, NULL, MUTEX_DRIVER, NULL);
549 	rw_init(&vswp->if_lockrw, NULL, RW_DRIVER, NULL);
550 	rw_init(&vswp->mac_rwlock, NULL, RW_DRIVER, NULL);
551 	rw_init(&vswp->mfdbrw, NULL, RW_DRIVER, NULL);
552 	rw_init(&vswp->plist.lockrw, NULL, RW_DRIVER, NULL);
553 
554 	progress |= PROG_locks;
555 
556 	rv = vsw_read_mdprops(vswp);
557 	if (rv != 0)
558 		goto vsw_attach_fail;
559 
560 	progress |= PROG_readmd;
561 
562 	/* setup the unicast forwarding database  */
563 	(void) snprintf(hashname, MAXNAMELEN, "vsw_unicst_table-%d",
564 	    vswp->instance);
565 	D2(vswp, "creating unicast hash table (%s)...", hashname);
566 	vswp->fdb_nchains = vsw_fdb_nchains;
567 	vswp->fdb_hashp = mod_hash_create_ptrhash(hashname, vswp->fdb_nchains,
568 	    mod_hash_null_valdtor, sizeof (void *));
569 	vsw_create_vlans((void *)vswp, VSW_LOCALDEV);
570 	progress |= PROG_fdb;
571 
572 	/* setup the multicast fowarding database */
573 	(void) snprintf(hashname, MAXNAMELEN, "vsw_mcst_table-%d",
574 	    vswp->instance);
575 	D2(vswp, "creating multicast hash table %s)...", hashname);
576 	vswp->mfdb = mod_hash_create_ptrhash(hashname, vsw_fdb_nchains,
577 	    mod_hash_null_valdtor, sizeof (void *));
578 
579 	progress |= PROG_mfdb;
580 
581 	/*
582 	 * Create the taskq which will process all the VIO
583 	 * control messages.
584 	 */
585 	(void) snprintf(qname, TASKQ_NAMELEN, "vsw_taskq%d", vswp->instance);
586 	if ((vswp->taskq_p = ddi_taskq_create(vswp->dip, qname, 1,
587 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
588 		cmn_err(CE_WARN, "!vsw%d: Unable to create task queue",
589 		    vswp->instance);
590 		goto vsw_attach_fail;
591 	}
592 
593 	progress |= PROG_taskq;
594 
595 	/* prevent auto-detaching */
596 	if (ddi_prop_update_int(DDI_DEV_T_NONE, vswp->dip,
597 	    DDI_NO_AUTODETACH, 1) != DDI_SUCCESS) {
598 		cmn_err(CE_NOTE, "!Unable to set \"%s\" property for "
599 		    "instance %u", DDI_NO_AUTODETACH, instance);
600 	}
601 
602 	/*
603 	 * The null switching function is set to avoid panic until
604 	 * switch mode is setup.
605 	 */
606 	vswp->vsw_switch_frame = vsw_switch_frame_nop;
607 
608 	/*
609 	 * Setup the required switching mode,
610 	 * based on the mdprops that we read earlier.
611 	 * schedule a short timeout (0.1 sec) for the first time
612 	 * setup and avoid calling mac_open() directly here,
613 	 * others are regular timeout 3 secs.
614 	 */
615 	mutex_enter(&vswp->swtmout_lock);
616 
617 	vswp->swtmout_enabled = B_TRUE;
618 	vswp->swtmout_id = timeout(vsw_setup_switching_timeout, vswp,
619 	    drv_usectohz(vsw_setup_switching_boot_delay));
620 
621 	mutex_exit(&vswp->swtmout_lock);
622 
623 	progress |= PROG_swmode;
624 
625 	/* Register with mac layer as a provider */
626 	rv = vsw_mac_register(vswp);
627 	if (rv != 0)
628 		goto vsw_attach_fail;
629 
630 	progress |= PROG_macreg;
631 
632 	/*
633 	 * Now we have everything setup, register an interest in
634 	 * specific MD nodes.
635 	 *
636 	 * The callback is invoked in 2 cases, firstly if upon mdeg
637 	 * registration there are existing nodes which match our specified
638 	 * criteria, and secondly if the MD is changed (and again, there
639 	 * are nodes which we are interested in present within it. Note
640 	 * that our callback will be invoked even if our specified nodes
641 	 * have not actually changed).
642 	 *
643 	 */
644 	rv = vsw_mdeg_register(vswp);
645 	if (rv != 0)
646 		goto vsw_attach_fail;
647 
648 	progress |= PROG_mdreg;
649 
650 	WRITE_ENTER(&vsw_rw);
651 	vswp->next = vsw_head;
652 	vsw_head = vswp;
653 	RW_EXIT(&vsw_rw);
654 
655 	ddi_report_dev(vswp->dip);
656 	return (DDI_SUCCESS);
657 
658 vsw_attach_fail:
659 	DERR(NULL, "vsw_attach: failed");
660 
661 	if (progress & PROG_mdreg) {
662 		vsw_mdeg_unregister(vswp);
663 		(void) vsw_detach_ports(vswp);
664 	}
665 
666 	if (progress & PROG_macreg)
667 		(void) vsw_mac_unregister(vswp);
668 
669 	if (progress & PROG_swmode) {
670 		vsw_stop_switching_timeout(vswp);
671 		vsw_hio_cleanup(vswp);
672 		WRITE_ENTER(&vswp->mac_rwlock);
673 		vsw_mac_detach(vswp);
674 		vsw_mac_close(vswp);
675 		RW_EXIT(&vswp->mac_rwlock);
676 	}
677 
678 	if (progress & PROG_taskq)
679 		ddi_taskq_destroy(vswp->taskq_p);
680 
681 	if (progress & PROG_mfdb)
682 		mod_hash_destroy_hash(vswp->mfdb);
683 
684 	if (progress & PROG_fdb) {
685 		vsw_destroy_vlans(vswp, VSW_LOCALDEV);
686 		mod_hash_destroy_hash(vswp->fdb_hashp);
687 	}
688 
689 	if (progress & PROG_readmd) {
690 		if (VSW_PRI_ETH_DEFINED(vswp)) {
691 			kmem_free(vswp->pri_types,
692 			    sizeof (uint16_t) * vswp->pri_num_types);
693 		}
694 		(void) vio_destroy_mblks(vswp->pri_tx_vmp);
695 	}
696 
697 	if (progress & PROG_locks) {
698 		rw_destroy(&vswp->plist.lockrw);
699 		rw_destroy(&vswp->mfdbrw);
700 		rw_destroy(&vswp->mac_rwlock);
701 		rw_destroy(&vswp->if_lockrw);
702 		mutex_destroy(&vswp->swtmout_lock);
703 		mutex_destroy(&vswp->mca_lock);
704 		mutex_destroy(&vswp->hw_lock);
705 	}
706 
707 	ddi_soft_state_free(vsw_state, instance);
708 	return (DDI_FAILURE);
709 }
710 
711 static int
712 vsw_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
713 {
714 	vio_mblk_pool_t		*poolp, *npoolp;
715 	vsw_t			**vswpp, *vswp;
716 	int 			instance;
717 
718 	instance = ddi_get_instance(dip);
719 	vswp = ddi_get_soft_state(vsw_state, instance);
720 
721 	if (vswp == NULL) {
722 		return (DDI_FAILURE);
723 	}
724 
725 	switch (cmd) {
726 	case DDI_DETACH:
727 		break;
728 	case DDI_SUSPEND:
729 	case DDI_PM_SUSPEND:
730 	default:
731 		return (DDI_FAILURE);
732 	}
733 
734 	D2(vswp, "detaching instance %d", instance);
735 
736 	/* Stop any pending timeout to setup switching mode. */
737 	vsw_stop_switching_timeout(vswp);
738 
739 	if (vswp->if_state & VSW_IF_REG) {
740 		if (vsw_mac_unregister(vswp) != 0) {
741 			cmn_err(CE_WARN, "!vsw%d: Unable to detach from "
742 			    "MAC layer", vswp->instance);
743 			return (DDI_FAILURE);
744 		}
745 	}
746 
747 	vsw_mdeg_unregister(vswp);
748 
749 	/* remove mac layer callback */
750 	WRITE_ENTER(&vswp->mac_rwlock);
751 	if ((vswp->mh != NULL) && (vswp->mrh != NULL)) {
752 		mac_rx_remove(vswp->mh, vswp->mrh, B_TRUE);
753 		vswp->mrh = NULL;
754 	}
755 	RW_EXIT(&vswp->mac_rwlock);
756 
757 	if (vsw_detach_ports(vswp) != 0) {
758 		cmn_err(CE_WARN, "!vsw%d: Unable to unconfigure ports",
759 		    vswp->instance);
760 		return (DDI_FAILURE);
761 	}
762 
763 	rw_destroy(&vswp->if_lockrw);
764 
765 	/* cleanup HybridIO */
766 	vsw_hio_cleanup(vswp);
767 
768 	mutex_destroy(&vswp->hw_lock);
769 
770 	/*
771 	 * Now that the ports have been deleted, stop and close
772 	 * the physical device.
773 	 */
774 	WRITE_ENTER(&vswp->mac_rwlock);
775 
776 	vsw_mac_detach(vswp);
777 	vsw_mac_close(vswp);
778 
779 	RW_EXIT(&vswp->mac_rwlock);
780 
781 	rw_destroy(&vswp->mac_rwlock);
782 	mutex_destroy(&vswp->swtmout_lock);
783 
784 	/*
785 	 * Destroy any free pools that may still exist.
786 	 */
787 	poolp = vswp->rxh;
788 	while (poolp != NULL) {
789 		npoolp = vswp->rxh = poolp->nextp;
790 		if (vio_destroy_mblks(poolp) != 0) {
791 			vswp->rxh = poolp;
792 			return (DDI_FAILURE);
793 		}
794 		poolp = npoolp;
795 	}
796 
797 	/*
798 	 * Remove this instance from any entries it may be on in
799 	 * the hash table by using the list of addresses maintained
800 	 * in the vsw_t structure.
801 	 */
802 	vsw_del_mcst_vsw(vswp);
803 
804 	vswp->mcap = NULL;
805 	mutex_destroy(&vswp->mca_lock);
806 
807 	/*
808 	 * By now any pending tasks have finished and the underlying
809 	 * ldc's have been destroyed, so its safe to delete the control
810 	 * message taskq.
811 	 */
812 	if (vswp->taskq_p != NULL)
813 		ddi_taskq_destroy(vswp->taskq_p);
814 
815 	/*
816 	 * At this stage all the data pointers in the hash table
817 	 * should be NULL, as all the ports have been removed and will
818 	 * have deleted themselves from the port lists which the data
819 	 * pointers point to. Hence we can destroy the table using the
820 	 * default destructors.
821 	 */
822 	D2(vswp, "vsw_detach: destroying hash tables..");
823 	vsw_destroy_vlans(vswp, VSW_LOCALDEV);
824 	mod_hash_destroy_hash(vswp->fdb_hashp);
825 	vswp->fdb_hashp = NULL;
826 
827 	WRITE_ENTER(&vswp->mfdbrw);
828 	mod_hash_destroy_hash(vswp->mfdb);
829 	vswp->mfdb = NULL;
830 	RW_EXIT(&vswp->mfdbrw);
831 	rw_destroy(&vswp->mfdbrw);
832 
833 	/* free pri_types table */
834 	if (VSW_PRI_ETH_DEFINED(vswp)) {
835 		kmem_free(vswp->pri_types,
836 		    sizeof (uint16_t) * vswp->pri_num_types);
837 		(void) vio_destroy_mblks(vswp->pri_tx_vmp);
838 	}
839 
840 	ddi_remove_minor_node(dip, NULL);
841 
842 	rw_destroy(&vswp->plist.lockrw);
843 	WRITE_ENTER(&vsw_rw);
844 	for (vswpp = &vsw_head; *vswpp; vswpp = &(*vswpp)->next) {
845 		if (*vswpp == vswp) {
846 			*vswpp = vswp->next;
847 			break;
848 		}
849 	}
850 	RW_EXIT(&vsw_rw);
851 	ddi_soft_state_free(vsw_state, instance);
852 
853 	return (DDI_SUCCESS);
854 }
855 
856 /*
857  * Get the value of the "vsw-phys-dev" property in the specified
858  * node. This property is the name of the physical device that
859  * the virtual switch will use to talk to the outside world.
860  *
861  * Note it is valid for this property to be NULL (but the property
862  * itself must exist). Callers of this routine should verify that
863  * the value returned is what they expected (i.e. either NULL or non NULL).
864  *
865  * On success returns value of the property in region pointed to by
866  * the 'name' argument, and with return value of 0. Otherwise returns 1.
867  */
868 static int
869 vsw_get_md_physname(vsw_t *vswp, md_t *mdp, mde_cookie_t node, char *name)
870 {
871 	int		len = 0;
872 	int		instance;
873 	char		*physname = NULL;
874 	char		*dev;
875 	const char	*dev_name;
876 	char		myname[MAXNAMELEN];
877 
878 	dev_name = ddi_driver_name(vswp->dip);
879 	instance = ddi_get_instance(vswp->dip);
880 	(void) snprintf(myname, MAXNAMELEN, "%s%d", dev_name, instance);
881 
882 	if (md_get_prop_data(mdp, node, physdev_propname,
883 	    (uint8_t **)(&physname), &len) != 0) {
884 		cmn_err(CE_WARN, "!vsw%d: Unable to get name(s) of physical "
885 		    "device(s) from MD", vswp->instance);
886 		return (1);
887 	} else if ((strlen(physname) + 1) > LIFNAMSIZ) {
888 		cmn_err(CE_WARN, "!vsw%d: %s is too long a device name",
889 		    vswp->instance, physname);
890 		return (1);
891 	} else if (strcmp(myname, physname) == 0) {
892 		/*
893 		 * Prevent the vswitch from opening itself as the
894 		 * network device.
895 		 */
896 		cmn_err(CE_WARN, "!vsw%d: %s is an invalid device name",
897 		    vswp->instance, physname);
898 		return (1);
899 	} else {
900 		(void) strncpy(name, physname, strlen(physname) + 1);
901 		D2(vswp, "%s: using first device specified (%s)",
902 		    __func__, physname);
903 	}
904 
905 #ifdef DEBUG
906 	/*
907 	 * As a temporary measure to aid testing we check to see if there
908 	 * is a vsw.conf file present. If there is we use the value of the
909 	 * vsw_physname property in the file as the name of the physical
910 	 * device, overriding the value from the MD.
911 	 *
912 	 * There may be multiple devices listed, but for the moment
913 	 * we just use the first one.
914 	 */
915 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, vswp->dip, 0,
916 	    "vsw_physname", &dev) == DDI_PROP_SUCCESS) {
917 		if ((strlen(dev) + 1) > LIFNAMSIZ) {
918 			cmn_err(CE_WARN, "vsw%d: %s is too long a device name",
919 			    vswp->instance, dev);
920 			ddi_prop_free(dev);
921 			return (1);
922 		} else {
923 			cmn_err(CE_NOTE, "vsw%d: Using device name (%s) from "
924 			    "config file", vswp->instance, dev);
925 
926 			(void) strncpy(name, dev, strlen(dev) + 1);
927 		}
928 
929 		ddi_prop_free(dev);
930 	}
931 #endif
932 
933 	return (0);
934 }
935 
936 /*
937  * Read the 'vsw-switch-mode' property from the specified MD node.
938  *
939  * Returns 0 on success and the number of modes found in 'found',
940  * otherwise returns 1.
941  */
942 static int
943 vsw_get_md_smodes(vsw_t *vswp, md_t *mdp, mde_cookie_t node,
944 						uint8_t *modes, int *found)
945 {
946 	int		len = 0;
947 	int		smode_num = 0;
948 	char		*smode = NULL;
949 	char		*curr_mode = NULL;
950 
951 	D1(vswp, "%s: enter", __func__);
952 
953 	/*
954 	 * Get the switch-mode property. The modes are listed in
955 	 * decreasing order of preference, i.e. prefered mode is
956 	 * first item in list.
957 	 */
958 	len = 0;
959 	smode_num = 0;
960 	if (md_get_prop_data(mdp, node, smode_propname,
961 	    (uint8_t **)(&smode), &len) != 0) {
962 		/*
963 		 * Unable to get switch-mode property from MD, nothing
964 		 * more we can do.
965 		 */
966 		cmn_err(CE_WARN, "!vsw%d: Unable to get switch mode property"
967 		    " from the MD", vswp->instance);
968 		*found = 0;
969 		return (1);
970 	}
971 
972 	curr_mode = smode;
973 	/*
974 	 * Modes of operation:
975 	 * 'switched'	 - layer 2 switching, underlying HW in
976 	 *			programmed mode.
977 	 * 'promiscuous' - layer 2 switching, underlying HW in
978 	 *			promiscuous mode.
979 	 * 'routed'	 - layer 3 (i.e. IP) routing, underlying HW
980 	 *			in non-promiscuous mode.
981 	 */
982 	while ((curr_mode < (smode + len)) && (smode_num < NUM_SMODES)) {
983 		D2(vswp, "%s: curr_mode = [%s]", __func__, curr_mode);
984 		if (strcmp(curr_mode, "switched") == 0) {
985 			modes[smode_num++] = VSW_LAYER2;
986 		} else if (strcmp(curr_mode, "promiscuous") == 0) {
987 			modes[smode_num++] = VSW_LAYER2_PROMISC;
988 		} else if (strcmp(curr_mode, "routed") == 0) {
989 			modes[smode_num++] = VSW_LAYER3;
990 		} else {
991 			DWARN(vswp, "%s: Unknown switch mode %s, "
992 			    "setting to default 'switched' mode",
993 			    __func__, curr_mode);
994 			modes[smode_num++] = VSW_LAYER2;
995 		}
996 		curr_mode += strlen(curr_mode) + 1;
997 	}
998 	*found = smode_num;
999 
1000 	D2(vswp, "%s: %d modes found", __func__, smode_num);
1001 
1002 	D1(vswp, "%s: exit", __func__);
1003 
1004 	return (0);
1005 }
1006 
1007 /*
1008  * Register with the MAC layer as a network device, so we
1009  * can be plumbed if necessary.
1010  */
1011 static int
1012 vsw_mac_register(vsw_t *vswp)
1013 {
1014 	mac_register_t	*macp;
1015 	int		rv;
1016 
1017 	D1(vswp, "%s: enter", __func__);
1018 
1019 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
1020 		return (EINVAL);
1021 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
1022 	macp->m_driver = vswp;
1023 	macp->m_dip = vswp->dip;
1024 	macp->m_src_addr = (uint8_t *)&vswp->if_addr;
1025 	macp->m_callbacks = &vsw_m_callbacks;
1026 	macp->m_min_sdu = 0;
1027 	macp->m_max_sdu = vswp->mtu;
1028 	macp->m_margin = VLAN_TAGSZ;
1029 	rv = mac_register(macp, &vswp->if_mh);
1030 	mac_free(macp);
1031 	if (rv != 0) {
1032 		/*
1033 		 * Treat this as a non-fatal error as we may be
1034 		 * able to operate in some other mode.
1035 		 */
1036 		cmn_err(CE_NOTE, "!vsw%d: Unable to register as "
1037 		    "a provider with MAC layer", vswp->instance);
1038 		return (rv);
1039 	}
1040 
1041 	vswp->if_state |= VSW_IF_REG;
1042 
1043 	D1(vswp, "%s: exit", __func__);
1044 
1045 	return (rv);
1046 }
1047 
1048 static int
1049 vsw_mac_unregister(vsw_t *vswp)
1050 {
1051 	int		rv = 0;
1052 
1053 	D1(vswp, "%s: enter", __func__);
1054 
1055 	WRITE_ENTER(&vswp->if_lockrw);
1056 
1057 	if (vswp->if_state & VSW_IF_REG) {
1058 		rv = mac_unregister(vswp->if_mh);
1059 		if (rv != 0) {
1060 			DWARN(vswp, "%s: unable to unregister from MAC "
1061 			    "framework", __func__);
1062 
1063 			RW_EXIT(&vswp->if_lockrw);
1064 			D1(vswp, "%s: fail exit", __func__);
1065 			return (rv);
1066 		}
1067 
1068 		/* mark i/f as down and unregistered */
1069 		vswp->if_state &= ~(VSW_IF_UP | VSW_IF_REG);
1070 	}
1071 	RW_EXIT(&vswp->if_lockrw);
1072 
1073 	D1(vswp, "%s: exit", __func__);
1074 
1075 	return (rv);
1076 }
1077 
1078 static int
1079 vsw_m_stat(void *arg, uint_t stat, uint64_t *val)
1080 {
1081 	vsw_t			*vswp = (vsw_t *)arg;
1082 
1083 	D1(vswp, "%s: enter", __func__);
1084 
1085 	WRITE_ENTER(&vswp->mac_rwlock);
1086 	if (vswp->mh == NULL) {
1087 		RW_EXIT(&vswp->mac_rwlock);
1088 		return (EINVAL);
1089 	}
1090 
1091 	/* return stats from underlying device */
1092 	*val = mac_stat_get(vswp->mh, stat);
1093 
1094 	RW_EXIT(&vswp->mac_rwlock);
1095 
1096 	return (0);
1097 }
1098 
1099 static void
1100 vsw_m_stop(void *arg)
1101 {
1102 	vsw_t	*vswp = (vsw_t *)arg;
1103 
1104 	D1(vswp, "%s: enter", __func__);
1105 
1106 	WRITE_ENTER(&vswp->if_lockrw);
1107 	vswp->if_state &= ~VSW_IF_UP;
1108 	RW_EXIT(&vswp->if_lockrw);
1109 
1110 	mutex_enter(&vswp->hw_lock);
1111 
1112 	(void) vsw_unset_hw(vswp, NULL, VSW_LOCALDEV);
1113 
1114 	if (vswp->recfg_reqd)
1115 		vsw_reconfig_hw(vswp);
1116 
1117 	mutex_exit(&vswp->hw_lock);
1118 
1119 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
1120 }
1121 
1122 static int
1123 vsw_m_start(void *arg)
1124 {
1125 	vsw_t		*vswp = (vsw_t *)arg;
1126 
1127 	D1(vswp, "%s: enter", __func__);
1128 
1129 	WRITE_ENTER(&vswp->if_lockrw);
1130 
1131 	vswp->if_state |= VSW_IF_UP;
1132 
1133 	if (vswp->switching_setup_done == B_FALSE) {
1134 		/*
1135 		 * If the switching mode has not been setup yet, just
1136 		 * return. The unicast address will be programmed
1137 		 * after the physical device is successfully setup by the
1138 		 * timeout handler.
1139 		 */
1140 		RW_EXIT(&vswp->if_lockrw);
1141 		return (0);
1142 	}
1143 
1144 	/* if in layer2 mode, program unicast address. */
1145 	if (vswp->mh != NULL) {
1146 		mutex_enter(&vswp->hw_lock);
1147 		(void) vsw_set_hw(vswp, NULL, VSW_LOCALDEV);
1148 		mutex_exit(&vswp->hw_lock);
1149 	}
1150 
1151 	RW_EXIT(&vswp->if_lockrw);
1152 
1153 	D1(vswp, "%s: exit (state = %d)", __func__, vswp->if_state);
1154 	return (0);
1155 }
1156 
1157 /*
1158  * Change the local interface address.
1159  *
1160  * Note: we don't support this entry point. The local
1161  * mac address of the switch can only be changed via its
1162  * MD node properties.
1163  */
1164 static int
1165 vsw_m_unicst(void *arg, const uint8_t *macaddr)
1166 {
1167 	_NOTE(ARGUNUSED(arg, macaddr))
1168 
1169 	return (DDI_FAILURE);
1170 }
1171 
1172 static int
1173 vsw_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
1174 {
1175 	vsw_t		*vswp = (vsw_t *)arg;
1176 	mcst_addr_t	*mcst_p = NULL;
1177 	uint64_t	addr = 0x0;
1178 	int		i, ret = 0;
1179 
1180 	D1(vswp, "%s: enter", __func__);
1181 
1182 	/*
1183 	 * Convert address into form that can be used
1184 	 * as hash table key.
1185 	 */
1186 	for (i = 0; i < ETHERADDRL; i++) {
1187 		addr = (addr << 8) | mca[i];
1188 	}
1189 
1190 	D2(vswp, "%s: addr = 0x%llx", __func__, addr);
1191 
1192 	if (add) {
1193 		D2(vswp, "%s: adding multicast", __func__);
1194 		if (vsw_add_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
1195 			/*
1196 			 * Update the list of multicast addresses
1197 			 * contained within the vsw_t structure to
1198 			 * include this new one.
1199 			 */
1200 			mcst_p = kmem_zalloc(sizeof (mcst_addr_t), KM_NOSLEEP);
1201 			if (mcst_p == NULL) {
1202 				DERR(vswp, "%s unable to alloc mem", __func__);
1203 				(void) vsw_del_mcst(vswp,
1204 				    VSW_LOCALDEV, addr, NULL);
1205 				return (1);
1206 			}
1207 			mcst_p->addr = addr;
1208 			ether_copy(mca, &mcst_p->mca);
1209 
1210 			/*
1211 			 * Call into the underlying driver to program the
1212 			 * address into HW.
1213 			 */
1214 			WRITE_ENTER(&vswp->mac_rwlock);
1215 			if (vswp->mh != NULL) {
1216 				ret = mac_multicst_add(vswp->mh, mca);
1217 				if (ret != 0) {
1218 					cmn_err(CE_NOTE, "!vsw%d: unable to "
1219 					    "add multicast address",
1220 					    vswp->instance);
1221 					RW_EXIT(&vswp->mac_rwlock);
1222 					(void) vsw_del_mcst(vswp,
1223 					    VSW_LOCALDEV, addr, NULL);
1224 					kmem_free(mcst_p, sizeof (*mcst_p));
1225 					return (ret);
1226 				}
1227 				mcst_p->mac_added = B_TRUE;
1228 			}
1229 			RW_EXIT(&vswp->mac_rwlock);
1230 
1231 			mutex_enter(&vswp->mca_lock);
1232 			mcst_p->nextp = vswp->mcap;
1233 			vswp->mcap = mcst_p;
1234 			mutex_exit(&vswp->mca_lock);
1235 		} else {
1236 			cmn_err(CE_NOTE, "!vsw%d: unable to add multicast "
1237 			    "address", vswp->instance);
1238 		}
1239 		return (ret);
1240 	}
1241 
1242 	D2(vswp, "%s: removing multicast", __func__);
1243 	/*
1244 	 * Remove the address from the hash table..
1245 	 */
1246 	if (vsw_del_mcst(vswp, VSW_LOCALDEV, addr, NULL) == 0) {
1247 
1248 		/*
1249 		 * ..and then from the list maintained in the
1250 		 * vsw_t structure.
1251 		 */
1252 		mcst_p = vsw_del_addr(VSW_LOCALDEV, vswp, addr);
1253 		ASSERT(mcst_p != NULL);
1254 
1255 		WRITE_ENTER(&vswp->mac_rwlock);
1256 		if (vswp->mh != NULL && mcst_p->mac_added) {
1257 			(void) mac_multicst_remove(vswp->mh, mca);
1258 			mcst_p->mac_added = B_FALSE;
1259 		}
1260 		RW_EXIT(&vswp->mac_rwlock);
1261 		kmem_free(mcst_p, sizeof (*mcst_p));
1262 	}
1263 
1264 	D1(vswp, "%s: exit", __func__);
1265 
1266 	return (0);
1267 }
1268 
1269 static int
1270 vsw_m_promisc(void *arg, boolean_t on)
1271 {
1272 	vsw_t		*vswp = (vsw_t *)arg;
1273 
1274 	D1(vswp, "%s: enter", __func__);
1275 
1276 	WRITE_ENTER(&vswp->if_lockrw);
1277 	if (on)
1278 		vswp->if_state |= VSW_IF_PROMISC;
1279 	else
1280 		vswp->if_state &= ~VSW_IF_PROMISC;
1281 	RW_EXIT(&vswp->if_lockrw);
1282 
1283 	D1(vswp, "%s: exit", __func__);
1284 
1285 	return (0);
1286 }
1287 
1288 static mblk_t *
1289 vsw_m_tx(void *arg, mblk_t *mp)
1290 {
1291 	vsw_t		*vswp = (vsw_t *)arg;
1292 
1293 	D1(vswp, "%s: enter", __func__);
1294 
1295 	mp = vsw_vlan_frame_pretag(vswp, VSW_LOCALDEV, mp);
1296 
1297 	if (mp == NULL) {
1298 		return (NULL);
1299 	}
1300 
1301 	vswp->vsw_switch_frame(vswp, mp, VSW_LOCALDEV, NULL, NULL);
1302 
1303 	D1(vswp, "%s: exit", __func__);
1304 
1305 	return (NULL);
1306 }
1307 
1308 /*
1309  * Register for machine description (MD) updates.
1310  *
1311  * Returns 0 on success, 1 on failure.
1312  */
1313 static int
1314 vsw_mdeg_register(vsw_t *vswp)
1315 {
1316 	mdeg_prop_spec_t	*pspecp;
1317 	mdeg_node_spec_t	*inst_specp;
1318 	mdeg_handle_t		mdeg_hdl, mdeg_port_hdl;
1319 	size_t			templatesz;
1320 	int			rv;
1321 
1322 	D1(vswp, "%s: enter", __func__);
1323 
1324 	/*
1325 	 * Allocate and initialize a per-instance copy
1326 	 * of the global property spec array that will
1327 	 * uniquely identify this vsw instance.
1328 	 */
1329 	templatesz = sizeof (vsw_prop_template);
1330 	pspecp = kmem_zalloc(templatesz, KM_SLEEP);
1331 
1332 	bcopy(vsw_prop_template, pspecp, templatesz);
1333 
1334 	VSW_SET_MDEG_PROP_INST(pspecp, vswp->regprop);
1335 
1336 	/* initialize the complete prop spec structure */
1337 	inst_specp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
1338 	inst_specp->namep = "virtual-device";
1339 	inst_specp->specp = pspecp;
1340 
1341 	D2(vswp, "%s: instance %d registering with mdeg", __func__,
1342 	    vswp->regprop);
1343 	/*
1344 	 * Register an interest in 'virtual-device' nodes with a
1345 	 * 'name' property of 'virtual-network-switch'
1346 	 */
1347 	rv = mdeg_register(inst_specp, &vdev_match, vsw_mdeg_cb,
1348 	    (void *)vswp, &mdeg_hdl);
1349 	if (rv != MDEG_SUCCESS) {
1350 		DERR(vswp, "%s: mdeg_register failed (%d) for vsw node",
1351 		    __func__, rv);
1352 		goto mdeg_reg_fail;
1353 	}
1354 
1355 	/*
1356 	 * Register an interest in 'vsw-port' nodes.
1357 	 */
1358 	rv = mdeg_register(inst_specp, &vport_match, vsw_port_mdeg_cb,
1359 	    (void *)vswp, &mdeg_port_hdl);
1360 	if (rv != MDEG_SUCCESS) {
1361 		DERR(vswp, "%s: mdeg_register failed (%d)\n", __func__, rv);
1362 		(void) mdeg_unregister(mdeg_hdl);
1363 		goto mdeg_reg_fail;
1364 	}
1365 
1366 	/* save off data that will be needed later */
1367 	vswp->inst_spec = inst_specp;
1368 	vswp->mdeg_hdl = mdeg_hdl;
1369 	vswp->mdeg_port_hdl = mdeg_port_hdl;
1370 
1371 	D1(vswp, "%s: exit", __func__);
1372 	return (0);
1373 
1374 mdeg_reg_fail:
1375 	cmn_err(CE_WARN, "!vsw%d: Unable to register MDEG callbacks",
1376 	    vswp->instance);
1377 	kmem_free(pspecp, templatesz);
1378 	kmem_free(inst_specp, sizeof (mdeg_node_spec_t));
1379 
1380 	vswp->mdeg_hdl = NULL;
1381 	vswp->mdeg_port_hdl = NULL;
1382 
1383 	return (1);
1384 }
1385 
1386 static void
1387 vsw_mdeg_unregister(vsw_t *vswp)
1388 {
1389 	D1(vswp, "vsw_mdeg_unregister: enter");
1390 
1391 	if (vswp->mdeg_hdl != NULL)
1392 		(void) mdeg_unregister(vswp->mdeg_hdl);
1393 
1394 	if (vswp->mdeg_port_hdl != NULL)
1395 		(void) mdeg_unregister(vswp->mdeg_port_hdl);
1396 
1397 	if (vswp->inst_spec != NULL) {
1398 		if (vswp->inst_spec->specp != NULL) {
1399 			(void) kmem_free(vswp->inst_spec->specp,
1400 			    sizeof (vsw_prop_template));
1401 			vswp->inst_spec->specp = NULL;
1402 		}
1403 
1404 		(void) kmem_free(vswp->inst_spec, sizeof (mdeg_node_spec_t));
1405 		vswp->inst_spec = NULL;
1406 	}
1407 
1408 	D1(vswp, "vsw_mdeg_unregister: exit");
1409 }
1410 
1411 /*
1412  * Mdeg callback invoked for the vsw node itself.
1413  */
1414 static int
1415 vsw_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
1416 {
1417 	vsw_t		*vswp;
1418 	md_t		*mdp;
1419 	mde_cookie_t	node;
1420 	uint64_t	inst;
1421 	char		*node_name = NULL;
1422 
1423 	if (resp == NULL)
1424 		return (MDEG_FAILURE);
1425 
1426 	vswp = (vsw_t *)cb_argp;
1427 
1428 	D1(vswp, "%s: added %d : removed %d : curr matched %d"
1429 	    " : prev matched %d", __func__, resp->added.nelem,
1430 	    resp->removed.nelem, resp->match_curr.nelem,
1431 	    resp->match_prev.nelem);
1432 
1433 	/*
1434 	 * We get an initial callback for this node as 'added'
1435 	 * after registering with mdeg. Note that we would have
1436 	 * already gathered information about this vsw node by
1437 	 * walking MD earlier during attach (in vsw_read_mdprops()).
1438 	 * So, there is a window where the properties of this
1439 	 * node might have changed when we get this initial 'added'
1440 	 * callback. We handle this as if an update occured
1441 	 * and invoke the same function which handles updates to
1442 	 * the properties of this vsw-node if any.
1443 	 *
1444 	 * A non-zero 'match' value indicates that the MD has been
1445 	 * updated and that a virtual-network-switch node is
1446 	 * present which may or may not have been updated. It is
1447 	 * up to the clients to examine their own nodes and
1448 	 * determine if they have changed.
1449 	 */
1450 	if (resp->added.nelem != 0) {
1451 
1452 		if (resp->added.nelem != 1) {
1453 			cmn_err(CE_NOTE, "!vsw%d: number of nodes added "
1454 			    "invalid: %d\n", vswp->instance, resp->added.nelem);
1455 			return (MDEG_FAILURE);
1456 		}
1457 
1458 		mdp = resp->added.mdp;
1459 		node = resp->added.mdep[0];
1460 
1461 	} else if (resp->match_curr.nelem != 0) {
1462 
1463 		if (resp->match_curr.nelem != 1) {
1464 			cmn_err(CE_NOTE, "!vsw%d: number of nodes updated "
1465 			    "invalid: %d\n", vswp->instance,
1466 			    resp->match_curr.nelem);
1467 			return (MDEG_FAILURE);
1468 		}
1469 
1470 		mdp = resp->match_curr.mdp;
1471 		node = resp->match_curr.mdep[0];
1472 
1473 	} else {
1474 		return (MDEG_FAILURE);
1475 	}
1476 
1477 	/* Validate name and instance */
1478 	if (md_get_prop_str(mdp, node, "name", &node_name) != 0) {
1479 		DERR(vswp, "%s: unable to get node name\n",  __func__);
1480 		return (MDEG_FAILURE);
1481 	}
1482 
1483 	/* is this a virtual-network-switch? */
1484 	if (strcmp(node_name, vsw_propname) != 0) {
1485 		DERR(vswp, "%s: Invalid node name: %s\n",
1486 		    __func__, node_name);
1487 		return (MDEG_FAILURE);
1488 	}
1489 
1490 	if (md_get_prop_val(mdp, node, "cfg-handle", &inst)) {
1491 		DERR(vswp, "%s: prop(cfg-handle) not found\n",
1492 		    __func__);
1493 		return (MDEG_FAILURE);
1494 	}
1495 
1496 	/* is this the right instance of vsw? */
1497 	if (inst != vswp->regprop) {
1498 		DERR(vswp, "%s: Invalid cfg-handle: %lx\n",
1499 		    __func__, inst);
1500 		return (MDEG_FAILURE);
1501 	}
1502 
1503 	vsw_update_md_prop(vswp, mdp, node);
1504 
1505 	return (MDEG_SUCCESS);
1506 }
1507 
1508 /*
1509  * Mdeg callback invoked for changes to the vsw-port nodes
1510  * under the vsw node.
1511  */
1512 static int
1513 vsw_port_mdeg_cb(void *cb_argp, mdeg_result_t *resp)
1514 {
1515 	vsw_t		*vswp;
1516 	int		idx;
1517 	md_t		*mdp;
1518 	mde_cookie_t	node;
1519 	uint64_t	inst;
1520 	int		rv;
1521 
1522 	if ((resp == NULL) || (cb_argp == NULL))
1523 		return (MDEG_FAILURE);
1524 
1525 	vswp = (vsw_t *)cb_argp;
1526 
1527 	D2(vswp, "%s: added %d : removed %d : curr matched %d"
1528 	    " : prev matched %d", __func__, resp->added.nelem,
1529 	    resp->removed.nelem, resp->match_curr.nelem,
1530 	    resp->match_prev.nelem);
1531 
1532 	/* process added ports */
1533 	for (idx = 0; idx < resp->added.nelem; idx++) {
1534 		mdp = resp->added.mdp;
1535 		node = resp->added.mdep[idx];
1536 
1537 		D2(vswp, "%s: adding node(%d) 0x%lx", __func__, idx, node);
1538 
1539 		if ((rv = vsw_port_add(vswp, mdp, &node)) != 0) {
1540 			cmn_err(CE_WARN, "!vsw%d: Unable to add new port "
1541 			    "(0x%lx), err=%d", vswp->instance, node, rv);
1542 		}
1543 	}
1544 
1545 	/* process removed ports */
1546 	for (idx = 0; idx < resp->removed.nelem; idx++) {
1547 		mdp = resp->removed.mdp;
1548 		node = resp->removed.mdep[idx];
1549 
1550 		if (md_get_prop_val(mdp, node, id_propname, &inst)) {
1551 			DERR(vswp, "%s: prop(%s) not found in port(%d)",
1552 			    __func__, id_propname, idx);
1553 			continue;
1554 		}
1555 
1556 		D2(vswp, "%s: removing node(%d) 0x%lx", __func__, idx, node);
1557 
1558 		if (vsw_port_detach(vswp, inst) != 0) {
1559 			cmn_err(CE_WARN, "!vsw%d: Unable to remove port %ld",
1560 			    vswp->instance, inst);
1561 		}
1562 	}
1563 
1564 	for (idx = 0; idx < resp->match_curr.nelem; idx++) {
1565 		(void) vsw_port_update(vswp, resp->match_curr.mdp,
1566 		    resp->match_curr.mdep[idx],
1567 		    resp->match_prev.mdp,
1568 		    resp->match_prev.mdep[idx]);
1569 	}
1570 
1571 	D1(vswp, "%s: exit", __func__);
1572 
1573 	return (MDEG_SUCCESS);
1574 }
1575 
1576 /*
1577  * Scan the machine description for this instance of vsw
1578  * and read its properties. Called only from vsw_attach().
1579  * Returns: 0 on success, 1 on failure.
1580  */
1581 static int
1582 vsw_read_mdprops(vsw_t *vswp)
1583 {
1584 	md_t		*mdp = NULL;
1585 	mde_cookie_t	rootnode;
1586 	mde_cookie_t	*listp = NULL;
1587 	uint64_t	inst;
1588 	uint64_t	cfgh;
1589 	char		*name;
1590 	int		rv = 1;
1591 	int		num_nodes = 0;
1592 	int		num_devs = 0;
1593 	int		listsz = 0;
1594 	int		i;
1595 
1596 	/*
1597 	 * In each 'virtual-device' node in the MD there is a
1598 	 * 'cfg-handle' property which is the MD's concept of
1599 	 * an instance number (this may be completely different from
1600 	 * the device drivers instance #). OBP reads that value and
1601 	 * stores it in the 'reg' property of the appropriate node in
1602 	 * the device tree. We first read this reg property and use this
1603 	 * to compare against the 'cfg-handle' property of vsw nodes
1604 	 * in MD to get to this specific vsw instance and then read
1605 	 * other properties that we are interested in.
1606 	 * We also cache the value of 'reg' property and use it later
1607 	 * to register callbacks with mdeg (see vsw_mdeg_register())
1608 	 */
1609 	inst = ddi_prop_get_int(DDI_DEV_T_ANY, vswp->dip,
1610 	    DDI_PROP_DONTPASS, reg_propname, -1);
1611 	if (inst == -1) {
1612 		cmn_err(CE_NOTE, "!vsw%d: Unable to read %s property from "
1613 		    "OBP device tree", vswp->instance, reg_propname);
1614 		return (rv);
1615 	}
1616 
1617 	vswp->regprop = inst;
1618 
1619 	if ((mdp = md_get_handle()) == NULL) {
1620 		DWARN(vswp, "%s: cannot init MD\n", __func__);
1621 		return (rv);
1622 	}
1623 
1624 	num_nodes = md_node_count(mdp);
1625 	ASSERT(num_nodes > 0);
1626 
1627 	listsz = num_nodes * sizeof (mde_cookie_t);
1628 	listp = (mde_cookie_t *)kmem_zalloc(listsz, KM_SLEEP);
1629 
1630 	rootnode = md_root_node(mdp);
1631 
1632 	/* search for all "virtual_device" nodes */
1633 	num_devs = md_scan_dag(mdp, rootnode,
1634 	    md_find_name(mdp, vdev_propname),
1635 	    md_find_name(mdp, "fwd"), listp);
1636 	if (num_devs <= 0) {
1637 		DWARN(vswp, "%s: invalid num_devs:%d\n", __func__, num_devs);
1638 		goto vsw_readmd_exit;
1639 	}
1640 
1641 	/*
1642 	 * Now loop through the list of virtual-devices looking for
1643 	 * devices with name "virtual-network-switch" and for each
1644 	 * such device compare its instance with what we have from
1645 	 * the 'reg' property to find the right node in MD and then
1646 	 * read all its properties.
1647 	 */
1648 	for (i = 0; i < num_devs; i++) {
1649 
1650 		if (md_get_prop_str(mdp, listp[i], "name", &name) != 0) {
1651 			DWARN(vswp, "%s: name property not found\n",
1652 			    __func__);
1653 			goto vsw_readmd_exit;
1654 		}
1655 
1656 		/* is this a virtual-network-switch? */
1657 		if (strcmp(name, vsw_propname) != 0)
1658 			continue;
1659 
1660 		if (md_get_prop_val(mdp, listp[i], "cfg-handle", &cfgh) != 0) {
1661 			DWARN(vswp, "%s: cfg-handle property not found\n",
1662 			    __func__);
1663 			goto vsw_readmd_exit;
1664 		}
1665 
1666 		/* is this the required instance of vsw? */
1667 		if (inst != cfgh)
1668 			continue;
1669 
1670 		/* now read all properties of this vsw instance */
1671 		rv = vsw_get_initial_md_properties(vswp, mdp, listp[i]);
1672 		break;
1673 	}
1674 
1675 vsw_readmd_exit:
1676 
1677 	kmem_free(listp, listsz);
1678 	(void) md_fini_handle(mdp);
1679 	return (rv);
1680 }
1681 
1682 /*
1683  * Read the initial start-of-day values from the specified MD node.
1684  */
1685 static int
1686 vsw_get_initial_md_properties(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
1687 {
1688 	int		i;
1689 	uint64_t 	macaddr = 0;
1690 
1691 	D1(vswp, "%s: enter", __func__);
1692 
1693 	if (vsw_get_md_physname(vswp, mdp, node, vswp->physname) != 0) {
1694 		return (1);
1695 	}
1696 
1697 	/* mac address for vswitch device itself */
1698 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
1699 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
1700 		    vswp->instance);
1701 		return (1);
1702 	}
1703 
1704 	vsw_save_lmacaddr(vswp, macaddr);
1705 
1706 	if (vsw_get_md_smodes(vswp, mdp, node, vswp->smode, &vswp->smode_num)) {
1707 		DWARN(vswp, "%s: Unable to read %s property from MD, "
1708 		    "defaulting to 'switched' mode",
1709 		    __func__, smode_propname);
1710 
1711 		for (i = 0; i < NUM_SMODES; i++)
1712 			vswp->smode[i] = VSW_LAYER2;
1713 
1714 		vswp->smode_num = NUM_SMODES;
1715 	} else {
1716 		ASSERT(vswp->smode_num != 0);
1717 	}
1718 
1719 	/* read mtu */
1720 	vsw_mtu_read(vswp, mdp, node, &vswp->mtu);
1721 	if (vswp->mtu < ETHERMTU || vswp->mtu > VNET_MAX_MTU) {
1722 		vswp->mtu = ETHERMTU;
1723 	}
1724 	vswp->max_frame_size = vswp->mtu + sizeof (struct ether_header) +
1725 	    VLAN_TAGSZ;
1726 
1727 	/* read vlan id properties of this vsw instance */
1728 	vsw_vlan_read_ids(vswp, VSW_LOCALDEV, mdp, node, &vswp->pvid,
1729 	    &vswp->vids, &vswp->nvids, &vswp->default_vlan_id);
1730 
1731 	/* read priority-ether-types */
1732 	vsw_read_pri_eth_types(vswp, mdp, node);
1733 
1734 	D1(vswp, "%s: exit", __func__);
1735 	return (0);
1736 }
1737 
1738 /*
1739  * Read vlan id properties of the given MD node.
1740  * Arguments:
1741  *   arg:          device argument(vsw device or a port)
1742  *   type:         type of arg; VSW_LOCALDEV(vsw device) or VSW_VNETPORT(port)
1743  *   mdp:          machine description
1744  *   node:         md node cookie
1745  *
1746  * Returns:
1747  *   pvidp:        port-vlan-id of the node
1748  *   vidspp:       list of vlan-ids of the node
1749  *   nvidsp:       # of vlan-ids in the list
1750  *   default_idp:  default-vlan-id of the node(if node is vsw device)
1751  */
1752 static void
1753 vsw_vlan_read_ids(void *arg, int type, md_t *mdp, mde_cookie_t node,
1754 	uint16_t *pvidp, uint16_t **vidspp, uint16_t *nvidsp,
1755 	uint16_t *default_idp)
1756 {
1757 	vsw_t		*vswp;
1758 	vsw_port_t	*portp;
1759 	char		*pvid_propname;
1760 	char		*vid_propname;
1761 	uint_t		nvids = 0;
1762 	uint32_t	vids_size;
1763 	int		rv;
1764 	int		i;
1765 	uint64_t	*data;
1766 	uint64_t	val;
1767 	int		size;
1768 	int		inst;
1769 
1770 	if (type == VSW_LOCALDEV) {
1771 
1772 		vswp = (vsw_t *)arg;
1773 		pvid_propname = vsw_pvid_propname;
1774 		vid_propname = vsw_vid_propname;
1775 		inst = vswp->instance;
1776 
1777 	} else if (type == VSW_VNETPORT) {
1778 
1779 		portp = (vsw_port_t *)arg;
1780 		vswp = portp->p_vswp;
1781 		pvid_propname = port_pvid_propname;
1782 		vid_propname = port_vid_propname;
1783 		inst = portp->p_instance;
1784 
1785 	} else {
1786 		return;
1787 	}
1788 
1789 	if (type == VSW_LOCALDEV && default_idp != NULL) {
1790 		rv = md_get_prop_val(mdp, node, vsw_dvid_propname, &val);
1791 		if (rv != 0) {
1792 			DWARN(vswp, "%s: prop(%s) not found", __func__,
1793 			    vsw_dvid_propname);
1794 
1795 			*default_idp = vsw_default_vlan_id;
1796 		} else {
1797 			*default_idp = val & 0xFFF;
1798 			D2(vswp, "%s: %s(%d): (%d)\n", __func__,
1799 			    vsw_dvid_propname, inst, *default_idp);
1800 		}
1801 	}
1802 
1803 	rv = md_get_prop_val(mdp, node, pvid_propname, &val);
1804 	if (rv != 0) {
1805 		DWARN(vswp, "%s: prop(%s) not found", __func__, pvid_propname);
1806 		*pvidp = vsw_default_vlan_id;
1807 	} else {
1808 
1809 		*pvidp = val & 0xFFF;
1810 		D2(vswp, "%s: %s(%d): (%d)\n", __func__,
1811 		    pvid_propname, inst, *pvidp);
1812 	}
1813 
1814 	rv = md_get_prop_data(mdp, node, vid_propname, (uint8_t **)&data,
1815 	    &size);
1816 	if (rv != 0) {
1817 		D2(vswp, "%s: prop(%s) not found", __func__, vid_propname);
1818 		size = 0;
1819 	} else {
1820 		size /= sizeof (uint64_t);
1821 	}
1822 	nvids = size;
1823 
1824 	if (nvids != 0) {
1825 		D2(vswp, "%s: %s(%d): ", __func__, vid_propname, inst);
1826 		vids_size = sizeof (uint16_t) * nvids;
1827 		*vidspp = kmem_zalloc(vids_size, KM_SLEEP);
1828 		for (i = 0; i < nvids; i++) {
1829 			(*vidspp)[i] = data[i] & 0xFFFF;
1830 			D2(vswp, " %d ", (*vidspp)[i]);
1831 		}
1832 		D2(vswp, "\n");
1833 	}
1834 
1835 	*nvidsp = nvids;
1836 }
1837 
1838 /*
1839  * This function reads "priority-ether-types" property from md. This property
1840  * is used to enable support for priority frames. Applications which need
1841  * guaranteed and timely delivery of certain high priority frames to/from
1842  * a vnet or vsw within ldoms, should configure this property by providing
1843  * the ether type(s) for which the priority facility is needed.
1844  * Normal data frames are delivered over a ldc channel using the descriptor
1845  * ring mechanism which is constrained by factors such as descriptor ring size,
1846  * the rate at which the ring is processed at the peer ldc end point, etc.
1847  * The priority mechanism provides an Out-Of-Band path to send/receive frames
1848  * as raw pkt data (VIO_PKT_DATA) messages over the channel, avoiding the
1849  * descriptor ring path and enables a more reliable and timely delivery of
1850  * frames to the peer.
1851  */
1852 static void
1853 vsw_read_pri_eth_types(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
1854 {
1855 	int		rv;
1856 	uint16_t	*types;
1857 	uint64_t	*data;
1858 	int		size;
1859 	int		i;
1860 	size_t		mblk_sz;
1861 
1862 	rv = md_get_prop_data(mdp, node, pri_types_propname,
1863 	    (uint8_t **)&data, &size);
1864 	if (rv != 0) {
1865 		/*
1866 		 * Property may not exist if we are running pre-ldoms1.1 f/w.
1867 		 * Check if 'vsw_pri_eth_type' has been set in that case.
1868 		 */
1869 		if (vsw_pri_eth_type != 0) {
1870 			size = sizeof (vsw_pri_eth_type);
1871 			data = &vsw_pri_eth_type;
1872 		} else {
1873 			D3(vswp, "%s: prop(%s) not found", __func__,
1874 			    pri_types_propname);
1875 			size = 0;
1876 		}
1877 	}
1878 
1879 	if (size == 0) {
1880 		vswp->pri_num_types = 0;
1881 		return;
1882 	}
1883 
1884 	/*
1885 	 * we have some priority-ether-types defined;
1886 	 * allocate a table of these types and also
1887 	 * allocate a pool of mblks to transmit these
1888 	 * priority packets.
1889 	 */
1890 	size /= sizeof (uint64_t);
1891 	vswp->pri_num_types = size;
1892 	vswp->pri_types = kmem_zalloc(size * sizeof (uint16_t), KM_SLEEP);
1893 	for (i = 0, types = vswp->pri_types; i < size; i++) {
1894 		types[i] = data[i] & 0xFFFF;
1895 	}
1896 	mblk_sz = (VIO_PKT_DATA_HDRSIZE + ETHERMAX + 7) & ~7;
1897 	(void) vio_create_mblks(vsw_pri_tx_nmblks, mblk_sz, &vswp->pri_tx_vmp);
1898 }
1899 
1900 static void
1901 vsw_mtu_read(vsw_t *vswp, md_t *mdp, mde_cookie_t node, uint32_t *mtu)
1902 {
1903 	int		rv;
1904 	int		inst;
1905 	uint64_t	val;
1906 	char		*mtu_propname;
1907 
1908 	mtu_propname = vsw_mtu_propname;
1909 	inst = vswp->instance;
1910 
1911 	rv = md_get_prop_val(mdp, node, mtu_propname, &val);
1912 	if (rv != 0) {
1913 		D3(vswp, "%s: prop(%s) not found", __func__, mtu_propname);
1914 		*mtu = vsw_ethermtu;
1915 	} else {
1916 
1917 		*mtu = val & 0xFFFF;
1918 		D2(vswp, "%s: %s(%d): (%d)\n", __func__,
1919 		    mtu_propname, inst, *mtu);
1920 	}
1921 }
1922 
1923 /*
1924  * Update the mtu of the vsw device. We first check if the device has been
1925  * plumbed and if so fail the mtu update. Otherwise, we continue to update the
1926  * new mtu and reset all ports to initiate handshake re-negotiation with peers
1927  * using the new mtu.
1928  */
1929 static int
1930 vsw_mtu_update(vsw_t *vswp, uint32_t mtu)
1931 {
1932 	int	rv;
1933 
1934 	WRITE_ENTER(&vswp->if_lockrw);
1935 
1936 	if (vswp->if_state & VSW_IF_UP) {
1937 
1938 		RW_EXIT(&vswp->if_lockrw);
1939 
1940 		cmn_err(CE_NOTE, "!vsw%d: Unable to process mtu update"
1941 		    " as the device is plumbed\n", vswp->instance);
1942 		return (EBUSY);
1943 
1944 	} else {
1945 
1946 		D2(vswp, "%s: curr_mtu(%d) new_mtu(%d)\n",
1947 		    __func__, vswp->mtu, mtu);
1948 
1949 		vswp->mtu = mtu;
1950 		vswp->max_frame_size = vswp->mtu +
1951 		    sizeof (struct ether_header) + VLAN_TAGSZ;
1952 
1953 		rv = mac_maxsdu_update(vswp->if_mh, mtu);
1954 		if (rv != 0) {
1955 			cmn_err(CE_NOTE,
1956 			    "!vsw%d: Unable to update mtu with mac"
1957 			    " layer\n", vswp->instance);
1958 		}
1959 
1960 		RW_EXIT(&vswp->if_lockrw);
1961 
1962 		WRITE_ENTER(&vswp->mac_rwlock);
1963 
1964 		if (vswp->mh == 0) {
1965 			/*
1966 			 * Physical device is not available yet; mtu will be
1967 			 * updated after we open it successfully, as we have
1968 			 * saved the new mtu.
1969 			 */
1970 			D2(vswp, "%s: Physical device:%s is not "
1971 			    "available yet; can't update its mtu\n",
1972 			    __func__, vswp->physname);
1973 
1974 		} else {
1975 
1976 			/*
1977 			 * Stop and restart to enable the
1978 			 * new mtu in the physical device.
1979 			 */
1980 			vsw_mac_detach(vswp);
1981 			rv = vsw_mac_attach(vswp);
1982 			if (rv != 0) {
1983 				RW_EXIT(&vswp->mac_rwlock);
1984 				return (EIO);
1985 			}
1986 
1987 		}
1988 
1989 		RW_EXIT(&vswp->mac_rwlock);
1990 
1991 		/* Reset ports to renegotiate with the new mtu */
1992 		vsw_reset_ports(vswp);
1993 
1994 	}
1995 
1996 	return (0);
1997 }
1998 
1999 /*
2000  * Check to see if the relevant properties in the specified node have
2001  * changed, and if so take the appropriate action.
2002  *
2003  * If any of the properties are missing or invalid we don't take
2004  * any action, as this function should only be invoked when modifications
2005  * have been made to what we assume is a working configuration, which
2006  * we leave active.
2007  *
2008  * Note it is legal for this routine to be invoked even if none of the
2009  * properties in the port node within the MD have actually changed.
2010  */
2011 static void
2012 vsw_update_md_prop(vsw_t *vswp, md_t *mdp, mde_cookie_t node)
2013 {
2014 	char		physname[LIFNAMSIZ];
2015 	char		drv[LIFNAMSIZ];
2016 	uint_t		ddi_instance;
2017 	uint8_t		new_smode[NUM_SMODES];
2018 	int		i, smode_num = 0;
2019 	uint64_t 	macaddr = 0;
2020 	enum		{MD_init = 0x1,
2021 				MD_physname = 0x2,
2022 				MD_macaddr = 0x4,
2023 				MD_smode = 0x8,
2024 				MD_vlans = 0x10,
2025 				MD_mtu = 0x20} updated;
2026 	int		rv;
2027 	uint16_t	pvid;
2028 	uint16_t	*vids;
2029 	uint16_t	nvids;
2030 	uint32_t	mtu;
2031 
2032 	updated = MD_init;
2033 
2034 	D1(vswp, "%s: enter", __func__);
2035 
2036 	/*
2037 	 * Check if name of physical device in MD has changed.
2038 	 */
2039 	if (vsw_get_md_physname(vswp, mdp, node, (char *)&physname) == 0) {
2040 		/*
2041 		 * Do basic sanity check on new device name/instance,
2042 		 * if its non NULL. It is valid for the device name to
2043 		 * have changed from a non NULL to a NULL value, i.e.
2044 		 * the vsw is being changed to 'routed' mode.
2045 		 */
2046 		if ((strlen(physname) != 0) &&
2047 		    (ddi_parse(physname, drv,
2048 		    &ddi_instance) != DDI_SUCCESS)) {
2049 			cmn_err(CE_WARN, "!vsw%d: physical device %s is not"
2050 			    " a valid device name/instance",
2051 			    vswp->instance, physname);
2052 			goto fail_reconf;
2053 		}
2054 
2055 		if (strcmp(physname, vswp->physname)) {
2056 			D2(vswp, "%s: device name changed from %s to %s",
2057 			    __func__, vswp->physname, physname);
2058 
2059 			updated |= MD_physname;
2060 		} else {
2061 			D2(vswp, "%s: device name unchanged at %s",
2062 			    __func__, vswp->physname);
2063 		}
2064 	} else {
2065 		cmn_err(CE_WARN, "!vsw%d: Unable to read name of physical "
2066 		    "device from updated MD.", vswp->instance);
2067 		goto fail_reconf;
2068 	}
2069 
2070 	/*
2071 	 * Check if MAC address has changed.
2072 	 */
2073 	if (md_get_prop_val(mdp, node, macaddr_propname, &macaddr) != 0) {
2074 		cmn_err(CE_WARN, "!vsw%d: Unable to get MAC address from MD",
2075 		    vswp->instance);
2076 		goto fail_reconf;
2077 	} else {
2078 		uint64_t maddr = macaddr;
2079 		READ_ENTER(&vswp->if_lockrw);
2080 		for (i = ETHERADDRL - 1; i >= 0; i--) {
2081 			if (vswp->if_addr.ether_addr_octet[i]
2082 			    != (macaddr & 0xFF)) {
2083 				D2(vswp, "%s: octet[%d] 0x%x != 0x%x",
2084 				    __func__, i,
2085 				    vswp->if_addr.ether_addr_octet[i],
2086 				    (macaddr & 0xFF));
2087 				updated |= MD_macaddr;
2088 				macaddr = maddr;
2089 				break;
2090 			}
2091 			macaddr >>= 8;
2092 		}
2093 		RW_EXIT(&vswp->if_lockrw);
2094 		if (updated & MD_macaddr) {
2095 			vsw_save_lmacaddr(vswp, macaddr);
2096 		}
2097 	}
2098 
2099 	/*
2100 	 * Check if switching modes have changed.
2101 	 */
2102 	if (vsw_get_md_smodes(vswp, mdp, node,
2103 	    new_smode, &smode_num)) {
2104 		cmn_err(CE_WARN, "!vsw%d: Unable to read %s property from MD",
2105 		    vswp->instance, smode_propname);
2106 		goto fail_reconf;
2107 	} else {
2108 		ASSERT(smode_num != 0);
2109 		if (smode_num != vswp->smode_num) {
2110 			D2(vswp, "%s: number of modes changed from %d to %d",
2111 			    __func__, vswp->smode_num, smode_num);
2112 		}
2113 
2114 		for (i = 0; i < smode_num; i++) {
2115 			if (new_smode[i] != vswp->smode[i]) {
2116 				D2(vswp, "%s: mode changed from %d to %d",
2117 				    __func__, vswp->smode[i], new_smode[i]);
2118 				updated |= MD_smode;
2119 				break;
2120 			}
2121 		}
2122 	}
2123 
2124 	/* Read the vlan ids */
2125 	vsw_vlan_read_ids(vswp, VSW_LOCALDEV, mdp, node, &pvid, &vids,
2126 	    &nvids, NULL);
2127 
2128 	/* Determine if there are any vlan id updates */
2129 	if ((pvid != vswp->pvid) ||		/* pvid changed? */
2130 	    (nvids != vswp->nvids) ||		/* # of vids changed? */
2131 	    ((nvids != 0) && (vswp->nvids != 0) &&	/* vids changed? */
2132 	    bcmp(vids, vswp->vids, sizeof (uint16_t) * nvids))) {
2133 		updated |= MD_vlans;
2134 	}
2135 
2136 	/* Read mtu */
2137 	vsw_mtu_read(vswp, mdp, node, &mtu);
2138 	if (mtu != vswp->mtu) {
2139 		if (mtu >= ETHERMTU && mtu <= VNET_MAX_MTU) {
2140 			updated |= MD_mtu;
2141 		} else {
2142 			cmn_err(CE_NOTE, "!vsw%d: Unable to process mtu update"
2143 			    " as the specified value:%d is invalid\n",
2144 			    vswp->instance, mtu);
2145 		}
2146 	}
2147 
2148 	/*
2149 	 * Now make any changes which are needed...
2150 	 */
2151 
2152 	if (updated & (MD_physname | MD_smode)) {
2153 
2154 		/*
2155 		 * Stop any pending timeout to setup switching mode.
2156 		 */
2157 		vsw_stop_switching_timeout(vswp);
2158 
2159 		/* Cleanup HybridIO */
2160 		vsw_hio_cleanup(vswp);
2161 
2162 		/*
2163 		 * Remove unicst, mcst addrs of vsw interface
2164 		 * and ports from the physdev.
2165 		 */
2166 		vsw_unset_addrs(vswp);
2167 
2168 		/*
2169 		 * Stop, detach and close the old device..
2170 		 */
2171 		WRITE_ENTER(&vswp->mac_rwlock);
2172 
2173 		vsw_mac_detach(vswp);
2174 		vsw_mac_close(vswp);
2175 
2176 		RW_EXIT(&vswp->mac_rwlock);
2177 
2178 		/*
2179 		 * Update phys name.
2180 		 */
2181 		if (updated & MD_physname) {
2182 			cmn_err(CE_NOTE, "!vsw%d: changing from %s to %s",
2183 			    vswp->instance, vswp->physname, physname);
2184 			(void) strncpy(vswp->physname,
2185 			    physname, strlen(physname) + 1);
2186 		}
2187 
2188 		/*
2189 		 * Update array with the new switch mode values.
2190 		 */
2191 		if (updated & MD_smode) {
2192 			for (i = 0; i < smode_num; i++)
2193 				vswp->smode[i] = new_smode[i];
2194 
2195 			vswp->smode_num = smode_num;
2196 			vswp->smode_idx = 0;
2197 		}
2198 
2199 		/*
2200 		 * ..and attach, start the new device.
2201 		 */
2202 		rv = vsw_setup_switching(vswp);
2203 		if (rv == EAGAIN) {
2204 			/*
2205 			 * Unable to setup switching mode.
2206 			 * As the error is EAGAIN, schedule a timeout to retry
2207 			 * and return. Programming addresses of ports and
2208 			 * vsw interface will be done when the timeout handler
2209 			 * completes successfully.
2210 			 */
2211 			mutex_enter(&vswp->swtmout_lock);
2212 
2213 			vswp->swtmout_enabled = B_TRUE;
2214 			vswp->swtmout_id =
2215 			    timeout(vsw_setup_switching_timeout, vswp,
2216 			    (vsw_setup_switching_delay *
2217 			    drv_usectohz(MICROSEC)));
2218 
2219 			mutex_exit(&vswp->swtmout_lock);
2220 
2221 			return;
2222 
2223 		} else if (rv) {
2224 			goto fail_update;
2225 		}
2226 
2227 		vsw_setup_layer2_post_process(vswp);
2228 	} else if (updated & MD_macaddr) {
2229 		/*
2230 		 * We enter here if only MD_macaddr is exclusively updated.
2231 		 * If MD_physname and/or MD_smode are also updated, then
2232 		 * as part of that, we would have implicitly processed
2233 		 * MD_macaddr update (above).
2234 		 */
2235 		cmn_err(CE_NOTE, "!vsw%d: changing mac address to 0x%lx",
2236 		    vswp->instance, macaddr);
2237 
2238 		READ_ENTER(&vswp->if_lockrw);
2239 		if (vswp->if_state & VSW_IF_UP) {
2240 
2241 			mutex_enter(&vswp->hw_lock);
2242 			/*
2243 			 * Remove old mac address of vsw interface
2244 			 * from the physdev
2245 			 */
2246 			(void) vsw_unset_hw(vswp, NULL, VSW_LOCALDEV);
2247 			/*
2248 			 * Program new mac address of vsw interface
2249 			 * in the physdev
2250 			 */
2251 			rv = vsw_set_hw(vswp, NULL, VSW_LOCALDEV);
2252 			mutex_exit(&vswp->hw_lock);
2253 			if (rv != 0) {
2254 				cmn_err(CE_NOTE,
2255 				    "!vsw%d: failed to program interface "
2256 				    "unicast address\n", vswp->instance);
2257 			}
2258 			/*
2259 			 * Notify the MAC layer of the changed address.
2260 			 */
2261 			mac_unicst_update(vswp->if_mh,
2262 			    (uint8_t *)&vswp->if_addr);
2263 
2264 		}
2265 		RW_EXIT(&vswp->if_lockrw);
2266 
2267 	}
2268 
2269 	if (updated & MD_vlans) {
2270 		/* Remove existing vlan ids from the hash table. */
2271 		vsw_vlan_remove_ids(vswp, VSW_LOCALDEV);
2272 
2273 		/* save the new vlan ids */
2274 		vswp->pvid = pvid;
2275 		if (vswp->nvids != 0) {
2276 			kmem_free(vswp->vids, sizeof (uint16_t) * vswp->nvids);
2277 			vswp->nvids = 0;
2278 		}
2279 		if (nvids != 0) {
2280 			vswp->nvids = nvids;
2281 			vswp->vids = vids;
2282 		}
2283 
2284 		/* add these new vlan ids into hash table */
2285 		vsw_vlan_add_ids(vswp, VSW_LOCALDEV);
2286 	} else {
2287 		if (nvids != 0) {
2288 			kmem_free(vids, sizeof (uint16_t) * nvids);
2289 		}
2290 	}
2291 
2292 	if (updated & MD_mtu) {
2293 
2294 		rv = vsw_mtu_update(vswp, mtu);
2295 		if (rv != 0) {
2296 			goto fail_update;
2297 		}
2298 
2299 	}
2300 
2301 	return;
2302 
2303 fail_reconf:
2304 	cmn_err(CE_WARN, "!vsw%d: configuration unchanged", vswp->instance);
2305 	return;
2306 
2307 fail_update:
2308 	cmn_err(CE_WARN, "!vsw%d: re-configuration failed",
2309 	    vswp->instance);
2310 }
2311 
2312 /*
2313  * Read the port's md properties.
2314  */
2315 static int
2316 vsw_port_read_props(vsw_port_t *portp, vsw_t *vswp,
2317 	md_t *mdp, mde_cookie_t *node)
2318 {
2319 	uint64_t		ldc_id;
2320 	uint8_t			*addrp;
2321 	int			i, addrsz;
2322 	int			num_nodes = 0, nchan = 0;
2323 	int			listsz = 0;
2324 	mde_cookie_t		*listp = NULL;
2325 	struct ether_addr	ea;
2326 	uint64_t		macaddr;
2327 	uint64_t		inst = 0;
2328 	uint64_t		val;
2329 
2330 	if (md_get_prop_val(mdp, *node, id_propname, &inst)) {
2331 		DWARN(vswp, "%s: prop(%s) not found", __func__,
2332 		    id_propname);
2333 		return (1);
2334 	}
2335 
2336 	/*
2337 	 * Find the channel endpoint node(s) (which should be under this
2338 	 * port node) which contain the channel id(s).
2339 	 */
2340 	if ((num_nodes = md_node_count(mdp)) <= 0) {
2341 		DERR(vswp, "%s: invalid number of nodes found (%d)",
2342 		    __func__, num_nodes);
2343 		return (1);
2344 	}
2345 
2346 	D2(vswp, "%s: %d nodes found", __func__, num_nodes);
2347 
2348 	/* allocate enough space for node list */
2349 	listsz = num_nodes * sizeof (mde_cookie_t);
2350 	listp = kmem_zalloc(listsz, KM_SLEEP);
2351 
2352 	nchan = md_scan_dag(mdp, *node, md_find_name(mdp, chan_propname),
2353 	    md_find_name(mdp, "fwd"), listp);
2354 
2355 	if (nchan <= 0) {
2356 		DWARN(vswp, "%s: no %s nodes found", __func__, chan_propname);
2357 		kmem_free(listp, listsz);
2358 		return (1);
2359 	}
2360 
2361 	D2(vswp, "%s: %d %s nodes found", __func__, nchan, chan_propname);
2362 
2363 	/* use property from first node found */
2364 	if (md_get_prop_val(mdp, listp[0], id_propname, &ldc_id)) {
2365 		DWARN(vswp, "%s: prop(%s) not found\n", __func__,
2366 		    id_propname);
2367 		kmem_free(listp, listsz);
2368 		return (1);
2369 	}
2370 
2371 	/* don't need list any more */
2372 	kmem_free(listp, listsz);
2373 
2374 	D2(vswp, "%s: ldc_id 0x%llx", __func__, ldc_id);
2375 
2376 	/* read mac-address property */
2377 	if (md_get_prop_data(mdp, *node, remaddr_propname,
2378 	    &addrp, &addrsz)) {
2379 		DWARN(vswp, "%s: prop(%s) not found",
2380 		    __func__, remaddr_propname);
2381 		return (1);
2382 	}
2383 
2384 	if (addrsz < ETHERADDRL) {
2385 		DWARN(vswp, "%s: invalid address size", __func__);
2386 		return (1);
2387 	}
2388 
2389 	macaddr = *((uint64_t *)addrp);
2390 	D2(vswp, "%s: remote mac address 0x%llx", __func__, macaddr);
2391 
2392 	for (i = ETHERADDRL - 1; i >= 0; i--) {
2393 		ea.ether_addr_octet[i] = macaddr & 0xFF;
2394 		macaddr >>= 8;
2395 	}
2396 
2397 	/* now update all properties into the port */
2398 	portp->p_vswp = vswp;
2399 	portp->p_instance = inst;
2400 	portp->addr_set = VSW_ADDR_UNSET;
2401 	ether_copy(&ea, &portp->p_macaddr);
2402 	if (nchan > VSW_PORT_MAX_LDCS) {
2403 		D2(vswp, "%s: using first of %d ldc ids",
2404 		    __func__, nchan);
2405 		nchan = VSW_PORT_MAX_LDCS;
2406 	}
2407 	portp->num_ldcs = nchan;
2408 	portp->ldc_ids =
2409 	    kmem_zalloc(sizeof (uint64_t) * nchan, KM_SLEEP);
2410 	bcopy(&ldc_id, (portp->ldc_ids), sizeof (uint64_t) * nchan);
2411 
2412 	/* read vlan id properties of this port node */
2413 	vsw_vlan_read_ids(portp, VSW_VNETPORT, mdp, *node, &portp->pvid,
2414 	    &portp->vids, &portp->nvids, NULL);
2415 
2416 	/* Check if hybrid property is present */
2417 	if (md_get_prop_val(mdp, *node, hybrid_propname, &val) == 0) {
2418 		D1(vswp, "%s: prop(%s) found\n", __func__, hybrid_propname);
2419 		portp->p_hio_enabled = B_TRUE;
2420 	} else {
2421 		portp->p_hio_enabled = B_FALSE;
2422 	}
2423 	/*
2424 	 * Port hio capability determined after version
2425 	 * negotiation, i.e., when we know the peer is HybridIO capable.
2426 	 */
2427 	portp->p_hio_capable = B_FALSE;
2428 	return (0);
2429 }
2430 
2431 /*
2432  * Add a new port to the system.
2433  *
2434  * Returns 0 on success, 1 on failure.
2435  */
2436 int
2437 vsw_port_add(vsw_t *vswp, md_t *mdp, mde_cookie_t *node)
2438 {
2439 	vsw_port_t	*portp;
2440 	int		rv;
2441 
2442 	portp = kmem_zalloc(sizeof (vsw_port_t), KM_SLEEP);
2443 
2444 	rv = vsw_port_read_props(portp, vswp, mdp, node);
2445 	if (rv != 0) {
2446 		kmem_free(portp, sizeof (*portp));
2447 		return (1);
2448 	}
2449 
2450 	rv = vsw_port_attach(portp);
2451 	if (rv != 0) {
2452 		DERR(vswp, "%s: failed to attach port", __func__);
2453 		return (1);
2454 	}
2455 
2456 	return (0);
2457 }
2458 
2459 static int
2460 vsw_port_update(vsw_t *vswp, md_t *curr_mdp, mde_cookie_t curr_mdex,
2461 	md_t *prev_mdp, mde_cookie_t prev_mdex)
2462 {
2463 	uint64_t	cport_num;
2464 	uint64_t	pport_num;
2465 	vsw_port_list_t	*plistp;
2466 	vsw_port_t	*portp;
2467 	boolean_t	updated_vlans = B_FALSE;
2468 	uint16_t	pvid;
2469 	uint16_t	*vids;
2470 	uint16_t	nvids;
2471 	uint64_t	val;
2472 	boolean_t	hio_enabled = B_FALSE;
2473 
2474 	/*
2475 	 * For now, we get port updates only if vlan ids changed.
2476 	 * We read the port num and do some sanity check.
2477 	 */
2478 	if (md_get_prop_val(curr_mdp, curr_mdex, id_propname, &cport_num)) {
2479 		return (1);
2480 	}
2481 
2482 	if (md_get_prop_val(prev_mdp, prev_mdex, id_propname, &pport_num)) {
2483 		return (1);
2484 	}
2485 	if (cport_num != pport_num)
2486 		return (1);
2487 
2488 	plistp = &(vswp->plist);
2489 
2490 	READ_ENTER(&plistp->lockrw);
2491 
2492 	portp = vsw_lookup_port(vswp, cport_num);
2493 	if (portp == NULL) {
2494 		RW_EXIT(&plistp->lockrw);
2495 		return (1);
2496 	}
2497 
2498 	/* Read the vlan ids */
2499 	vsw_vlan_read_ids(portp, VSW_VNETPORT, curr_mdp, curr_mdex, &pvid,
2500 	    &vids, &nvids, NULL);
2501 
2502 	/* Determine if there are any vlan id updates */
2503 	if ((pvid != portp->pvid) ||		/* pvid changed? */
2504 	    (nvids != portp->nvids) ||		/* # of vids changed? */
2505 	    ((nvids != 0) && (portp->nvids != 0) &&	/* vids changed? */
2506 	    bcmp(vids, portp->vids, sizeof (uint16_t) * nvids))) {
2507 		updated_vlans = B_TRUE;
2508 	}
2509 
2510 	if (updated_vlans == B_TRUE) {
2511 
2512 		/* Remove existing vlan ids from the hash table. */
2513 		vsw_vlan_remove_ids(portp, VSW_VNETPORT);
2514 
2515 		/* save the new vlan ids */
2516 		portp->pvid = pvid;
2517 		if (portp->nvids != 0) {
2518 			kmem_free(portp->vids,
2519 			    sizeof (uint16_t) * portp->nvids);
2520 			portp->nvids = 0;
2521 		}
2522 		if (nvids != 0) {
2523 			portp->vids = kmem_zalloc(sizeof (uint16_t) *
2524 			    nvids, KM_SLEEP);
2525 			bcopy(vids, portp->vids, sizeof (uint16_t) * nvids);
2526 			portp->nvids = nvids;
2527 			kmem_free(vids, sizeof (uint16_t) * nvids);
2528 		}
2529 
2530 		/* add these new vlan ids into hash table */
2531 		vsw_vlan_add_ids(portp, VSW_VNETPORT);
2532 
2533 		/* reset the port if it is vlan unaware (ver < 1.3) */
2534 		vsw_vlan_unaware_port_reset(portp);
2535 	}
2536 
2537 	/* Check if hybrid property is present */
2538 	if (md_get_prop_val(curr_mdp, curr_mdex, hybrid_propname, &val) == 0) {
2539 		D1(vswp, "%s: prop(%s) found\n", __func__, hybrid_propname);
2540 		hio_enabled = B_TRUE;
2541 	}
2542 
2543 	if (portp->p_hio_enabled != hio_enabled) {
2544 		vsw_hio_port_update(portp, hio_enabled);
2545 	}
2546 
2547 	RW_EXIT(&plistp->lockrw);
2548 
2549 	return (0);
2550 }
2551 
2552 /*
2553  * vsw_mac_rx -- A common function to send packets to the interface.
2554  * By default this function check if the interface is UP or not, the
2555  * rest of the behaviour depends on the flags as below:
2556  *
2557  *	VSW_MACRX_PROMISC -- Check if the promisc mode set or not.
2558  *	VSW_MACRX_COPYMSG -- Make a copy of the message(s).
2559  *	VSW_MACRX_FREEMSG -- Free if the messages cannot be sent up the stack.
2560  */
2561 void
2562 vsw_mac_rx(vsw_t *vswp, mac_resource_handle_t mrh,
2563     mblk_t *mp, vsw_macrx_flags_t flags)
2564 {
2565 	mblk_t		*mpt;
2566 
2567 	D1(vswp, "%s:enter\n", __func__);
2568 	READ_ENTER(&vswp->if_lockrw);
2569 	/* Check if the interface is up */
2570 	if (!(vswp->if_state & VSW_IF_UP)) {
2571 		RW_EXIT(&vswp->if_lockrw);
2572 		/* Free messages only if FREEMSG flag specified */
2573 		if (flags & VSW_MACRX_FREEMSG) {
2574 			freemsgchain(mp);
2575 		}
2576 		D1(vswp, "%s:exit\n", __func__);
2577 		return;
2578 	}
2579 	/*
2580 	 * If PROMISC flag is passed, then check if
2581 	 * the interface is in the PROMISC mode.
2582 	 * If not, drop the messages.
2583 	 */
2584 	if (flags & VSW_MACRX_PROMISC) {
2585 		if (!(vswp->if_state & VSW_IF_PROMISC)) {
2586 			RW_EXIT(&vswp->if_lockrw);
2587 			/* Free messages only if FREEMSG flag specified */
2588 			if (flags & VSW_MACRX_FREEMSG) {
2589 				freemsgchain(mp);
2590 			}
2591 			D1(vswp, "%s:exit\n", __func__);
2592 			return;
2593 		}
2594 	}
2595 	RW_EXIT(&vswp->if_lockrw);
2596 	/*
2597 	 * If COPYMSG flag is passed, then make a copy
2598 	 * of the message chain and send up the copy.
2599 	 */
2600 	if (flags & VSW_MACRX_COPYMSG) {
2601 		mp = copymsgchain(mp);
2602 		if (mp == NULL) {
2603 			D1(vswp, "%s:exit\n", __func__);
2604 			return;
2605 		}
2606 	}
2607 
2608 	D2(vswp, "%s: sending up stack", __func__);
2609 
2610 	mpt = NULL;
2611 	(void) vsw_vlan_frame_untag(vswp, VSW_LOCALDEV, &mp, &mpt);
2612 	if (mp != NULL) {
2613 		mac_rx(vswp->if_mh, mrh, mp);
2614 	}
2615 	D1(vswp, "%s:exit\n", __func__);
2616 }
2617 
2618 /* copy mac address of vsw into soft state structure */
2619 static void
2620 vsw_save_lmacaddr(vsw_t *vswp, uint64_t macaddr)
2621 {
2622 	int	i;
2623 
2624 	WRITE_ENTER(&vswp->if_lockrw);
2625 	for (i = ETHERADDRL - 1; i >= 0; i--) {
2626 		vswp->if_addr.ether_addr_octet[i] = macaddr & 0xFF;
2627 		macaddr >>= 8;
2628 	}
2629 	RW_EXIT(&vswp->if_lockrw);
2630 }
2631