xref: /titanic_41/usr/src/uts/common/io/mac/mac_provider.c (revision a98ca9da1bd21dd8b5b0161eb59d550bf26e70e2)
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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/conf.h>
28 #include <sys/id_space.h>
29 #include <sys/esunddi.h>
30 #include <sys/stat.h>
31 #include <sys/mkdev.h>
32 #include <sys/stream.h>
33 #include <sys/strsubr.h>
34 #include <sys/dlpi.h>
35 #include <sys/modhash.h>
36 #include <sys/mac.h>
37 #include <sys/mac_provider.h>
38 #include <sys/mac_impl.h>
39 #include <sys/mac_client_impl.h>
40 #include <sys/mac_client_priv.h>
41 #include <sys/mac_soft_ring.h>
42 #include <sys/mac_stat.h>
43 #include <sys/dld.h>
44 #include <sys/modctl.h>
45 #include <sys/fs/dv_node.h>
46 #include <sys/thread.h>
47 #include <sys/proc.h>
48 #include <sys/callb.h>
49 #include <sys/cpuvar.h>
50 #include <sys/atomic.h>
51 #include <sys/sdt.h>
52 #include <sys/mac_flow.h>
53 #include <sys/ddi_intr_impl.h>
54 #include <sys/disp.h>
55 #include <sys/sdt.h>
56 #include <sys/pattr.h>
57 #include <sys/strsun.h>
58 
59 /*
60  * MAC Provider Interface.
61  *
62  * Interface for GLDv3 compatible NIC drivers.
63  */
64 
65 static void i_mac_notify_thread(void *);
66 
67 typedef void (*mac_notify_default_cb_fn_t)(mac_impl_t *);
68 
69 static const mac_notify_default_cb_fn_t mac_notify_cb_list[MAC_NNOTE] = {
70 	mac_fanout_recompute,	/* MAC_NOTE_LINK */
71 	NULL,		/* MAC_NOTE_UNICST */
72 	NULL,		/* MAC_NOTE_TX */
73 	NULL,		/* MAC_NOTE_DEVPROMISC */
74 	NULL,		/* MAC_NOTE_FASTPATH_FLUSH */
75 	NULL,		/* MAC_NOTE_SDU_SIZE */
76 	NULL,		/* MAC_NOTE_MARGIN */
77 	NULL,		/* MAC_NOTE_CAPAB_CHG */
78 	NULL		/* MAC_NOTE_LOWLINK */
79 };
80 
81 /*
82  * Driver support functions.
83  */
84 
85 /* REGISTRATION */
86 
87 mac_register_t *
88 mac_alloc(uint_t mac_version)
89 {
90 	mac_register_t *mregp;
91 
92 	/*
93 	 * Make sure there isn't a version mismatch between the driver and
94 	 * the framework.  In the future, if multiple versions are
95 	 * supported, this check could become more sophisticated.
96 	 */
97 	if (mac_version != MAC_VERSION)
98 		return (NULL);
99 
100 	mregp = kmem_zalloc(sizeof (mac_register_t), KM_SLEEP);
101 	mregp->m_version = mac_version;
102 	return (mregp);
103 }
104 
105 void
106 mac_free(mac_register_t *mregp)
107 {
108 	kmem_free(mregp, sizeof (mac_register_t));
109 }
110 
111 /*
112  * mac_register() is how drivers register new MACs with the GLDv3
113  * framework.  The mregp argument is allocated by drivers using the
114  * mac_alloc() function, and can be freed using mac_free() immediately upon
115  * return from mac_register().  Upon success (0 return value), the mhp
116  * opaque pointer becomes the driver's handle to its MAC interface, and is
117  * the argument to all other mac module entry points.
118  */
119 /* ARGSUSED */
120 int
121 mac_register(mac_register_t *mregp, mac_handle_t *mhp)
122 {
123 	mac_impl_t		*mip;
124 	mactype_t		*mtype;
125 	int			err = EINVAL;
126 	struct devnames		*dnp = NULL;
127 	uint_t			instance;
128 	boolean_t		style1_created = B_FALSE;
129 	boolean_t		style2_created = B_FALSE;
130 	char			*driver;
131 	minor_t			minor = 0;
132 
133 	/* A successful call to mac_init_ops() sets the DN_GLDV3_DRIVER flag. */
134 	if (!GLDV3_DRV(ddi_driver_major(mregp->m_dip)))
135 		return (EINVAL);
136 
137 	/* Find the required MAC-Type plugin. */
138 	if ((mtype = mactype_getplugin(mregp->m_type_ident)) == NULL)
139 		return (EINVAL);
140 
141 	/* Create a mac_impl_t to represent this MAC. */
142 	mip = kmem_cache_alloc(i_mac_impl_cachep, KM_SLEEP);
143 
144 	/*
145 	 * The mac is not ready for open yet.
146 	 */
147 	mip->mi_state_flags |= MIS_DISABLED;
148 
149 	/*
150 	 * When a mac is registered, the m_instance field can be set to:
151 	 *
152 	 *  0:	Get the mac's instance number from m_dip.
153 	 *	This is usually used for physical device dips.
154 	 *
155 	 *  [1 .. MAC_MAX_MINOR-1]: Use the value as the mac's instance number.
156 	 *	For example, when an aggregation is created with the key option,
157 	 *	"key" will be used as the instance number.
158 	 *
159 	 *  -1: Assign an instance number from [MAC_MAX_MINOR .. MAXMIN-1].
160 	 *	This is often used when a MAC of a virtual link is registered
161 	 *	(e.g., aggregation when "key" is not specified, or vnic).
162 	 *
163 	 * Note that the instance number is used to derive the mi_minor field
164 	 * of mac_impl_t, which will then be used to derive the name of kstats
165 	 * and the devfs nodes.  The first 2 cases are needed to preserve
166 	 * backward compatibility.
167 	 */
168 	switch (mregp->m_instance) {
169 	case 0:
170 		instance = ddi_get_instance(mregp->m_dip);
171 		break;
172 	case ((uint_t)-1):
173 		minor = mac_minor_hold(B_TRUE);
174 		if (minor == 0) {
175 			err = ENOSPC;
176 			goto fail;
177 		}
178 		instance = minor - 1;
179 		break;
180 	default:
181 		instance = mregp->m_instance;
182 		if (instance >= MAC_MAX_MINOR) {
183 			err = EINVAL;
184 			goto fail;
185 		}
186 		break;
187 	}
188 
189 	mip->mi_minor = (minor_t)(instance + 1);
190 	mip->mi_dip = mregp->m_dip;
191 	mip->mi_clients_list = NULL;
192 	mip->mi_nclients = 0;
193 
194 	/* Set the default IEEE Port VLAN Identifier */
195 	mip->mi_pvid = 1;
196 
197 	/* Default bridge link learning protection values */
198 	mip->mi_llimit = 1000;
199 	mip->mi_ldecay = 200;
200 
201 	driver = (char *)ddi_driver_name(mip->mi_dip);
202 
203 	/* Construct the MAC name as <drvname><instance> */
204 	(void) snprintf(mip->mi_name, sizeof (mip->mi_name), "%s%d",
205 	    driver, instance);
206 
207 	mip->mi_driver = mregp->m_driver;
208 
209 	mip->mi_type = mtype;
210 	mip->mi_margin = mregp->m_margin;
211 	mip->mi_info.mi_media = mtype->mt_type;
212 	mip->mi_info.mi_nativemedia = mtype->mt_nativetype;
213 	if (mregp->m_max_sdu <= mregp->m_min_sdu)
214 		goto fail;
215 	mip->mi_sdu_min = mregp->m_min_sdu;
216 	mip->mi_sdu_max = mregp->m_max_sdu;
217 	mip->mi_info.mi_addr_length = mip->mi_type->mt_addr_length;
218 	/*
219 	 * If the media supports a broadcast address, cache a pointer to it
220 	 * in the mac_info_t so that upper layers can use it.
221 	 */
222 	mip->mi_info.mi_brdcst_addr = mip->mi_type->mt_brdcst_addr;
223 
224 	mip->mi_v12n_level = mregp->m_v12n;
225 
226 	/*
227 	 * Copy the unicast source address into the mac_info_t, but only if
228 	 * the MAC-Type defines a non-zero address length.  We need to
229 	 * handle MAC-Types that have an address length of 0
230 	 * (point-to-point protocol MACs for example).
231 	 */
232 	if (mip->mi_type->mt_addr_length > 0) {
233 		if (mregp->m_src_addr == NULL)
234 			goto fail;
235 		mip->mi_info.mi_unicst_addr =
236 		    kmem_alloc(mip->mi_type->mt_addr_length, KM_SLEEP);
237 		bcopy(mregp->m_src_addr, mip->mi_info.mi_unicst_addr,
238 		    mip->mi_type->mt_addr_length);
239 
240 		/*
241 		 * Copy the fixed 'factory' MAC address from the immutable
242 		 * info.  This is taken to be the MAC address currently in
243 		 * use.
244 		 */
245 		bcopy(mip->mi_info.mi_unicst_addr, mip->mi_addr,
246 		    mip->mi_type->mt_addr_length);
247 
248 		/*
249 		 * At this point, we should set up the classification
250 		 * rules etc but we delay it till mac_open() so that
251 		 * the resource discovery has taken place and we
252 		 * know someone wants to use the device. Otherwise
253 		 * memory gets allocated for Rx ring structures even
254 		 * during probe.
255 		 */
256 
257 		/* Copy the destination address if one is provided. */
258 		if (mregp->m_dst_addr != NULL) {
259 			bcopy(mregp->m_dst_addr, mip->mi_dstaddr,
260 			    mip->mi_type->mt_addr_length);
261 			mip->mi_dstaddr_set = B_TRUE;
262 		}
263 	} else if (mregp->m_src_addr != NULL) {
264 		goto fail;
265 	}
266 
267 	/*
268 	 * The format of the m_pdata is specific to the plugin.  It is
269 	 * passed in as an argument to all of the plugin callbacks.  The
270 	 * driver can update this information by calling
271 	 * mac_pdata_update().
272 	 */
273 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY) {
274 		/*
275 		 * Verify if the supplied plugin data is valid.  Note that
276 		 * even if the caller passed in a NULL pointer as plugin data,
277 		 * we still need to verify if that's valid as the plugin may
278 		 * require plugin data to function.
279 		 */
280 		if (!mip->mi_type->mt_ops.mtops_pdata_verify(mregp->m_pdata,
281 		    mregp->m_pdata_size)) {
282 			goto fail;
283 		}
284 		if (mregp->m_pdata != NULL) {
285 			mip->mi_pdata =
286 			    kmem_alloc(mregp->m_pdata_size, KM_SLEEP);
287 			bcopy(mregp->m_pdata, mip->mi_pdata,
288 			    mregp->m_pdata_size);
289 			mip->mi_pdata_size = mregp->m_pdata_size;
290 		}
291 	} else if (mregp->m_pdata != NULL) {
292 		/*
293 		 * The caller supplied non-NULL plugin data, but the plugin
294 		 * does not recognize plugin data.
295 		 */
296 		err = EINVAL;
297 		goto fail;
298 	}
299 
300 	/*
301 	 * Register the private properties.
302 	 */
303 	mac_register_priv_prop(mip, mregp->m_priv_props);
304 
305 	/*
306 	 * Stash the driver callbacks into the mac_impl_t, but first sanity
307 	 * check to make sure all mandatory callbacks are set.
308 	 */
309 	if (mregp->m_callbacks->mc_getstat == NULL ||
310 	    mregp->m_callbacks->mc_start == NULL ||
311 	    mregp->m_callbacks->mc_stop == NULL ||
312 	    mregp->m_callbacks->mc_setpromisc == NULL ||
313 	    mregp->m_callbacks->mc_multicst == NULL) {
314 		goto fail;
315 	}
316 	mip->mi_callbacks = mregp->m_callbacks;
317 
318 	if (mac_capab_get((mac_handle_t)mip, MAC_CAPAB_LEGACY,
319 	    &mip->mi_capab_legacy)) {
320 		mip->mi_state_flags |= MIS_LEGACY;
321 		mip->mi_phy_dev = mip->mi_capab_legacy.ml_dev;
322 	} else {
323 		mip->mi_phy_dev = makedevice(ddi_driver_major(mip->mi_dip),
324 		    mip->mi_minor);
325 	}
326 
327 	/*
328 	 * Allocate a notification thread. thread_create blocks for memory
329 	 * if needed, it never fails.
330 	 */
331 	mip->mi_notify_thread = thread_create(NULL, 0, i_mac_notify_thread,
332 	    mip, 0, &p0, TS_RUN, minclsyspri);
333 
334 	/*
335 	 * Initialize the capabilities
336 	 */
337 
338 	bzero(&mip->mi_rx_rings_cap, sizeof (mac_capab_rings_t));
339 	bzero(&mip->mi_tx_rings_cap, sizeof (mac_capab_rings_t));
340 
341 	if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, NULL))
342 		mip->mi_state_flags |= MIS_IS_VNIC;
343 
344 	if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR, NULL))
345 		mip->mi_state_flags |= MIS_IS_AGGR;
346 
347 	mac_addr_factory_init(mip);
348 
349 	/*
350 	 * Enforce the virtrualization level registered.
351 	 */
352 	if (mip->mi_v12n_level & MAC_VIRT_LEVEL1) {
353 		if (mac_init_rings(mip, MAC_RING_TYPE_RX) != 0 ||
354 		    mac_init_rings(mip, MAC_RING_TYPE_TX) != 0)
355 			goto fail;
356 
357 		/*
358 		 * The driver needs to register at least rx rings for this
359 		 * virtualization level.
360 		 */
361 		if (mip->mi_rx_groups == NULL)
362 			goto fail;
363 	}
364 
365 	/*
366 	 * The driver must set mc_unicst entry point to NULL when it advertises
367 	 * CAP_RINGS for rx groups.
368 	 */
369 	if (mip->mi_rx_groups != NULL) {
370 		if (mregp->m_callbacks->mc_unicst != NULL)
371 			goto fail;
372 	} else {
373 		if (mregp->m_callbacks->mc_unicst == NULL)
374 			goto fail;
375 	}
376 
377 	/*
378 	 * Initialize MAC addresses. Must be called after mac_init_rings().
379 	 */
380 	mac_init_macaddr(mip);
381 
382 	mip->mi_share_capab.ms_snum = 0;
383 	if (mip->mi_v12n_level & MAC_VIRT_HIO) {
384 		(void) mac_capab_get((mac_handle_t)mip, MAC_CAPAB_SHARES,
385 		    &mip->mi_share_capab);
386 	}
387 
388 	/*
389 	 * Initialize the kstats for this device.
390 	 */
391 	mac_driver_stat_create(mip);
392 
393 	/* Zero out any properties. */
394 	bzero(&mip->mi_resource_props, sizeof (mac_resource_props_t));
395 
396 	if (mip->mi_minor <= MAC_MAX_MINOR) {
397 		/* Create a style-2 DLPI device */
398 		if (ddi_create_minor_node(mip->mi_dip, driver, S_IFCHR, 0,
399 		    DDI_NT_NET, CLONE_DEV) != DDI_SUCCESS)
400 			goto fail;
401 		style2_created = B_TRUE;
402 
403 		/* Create a style-1 DLPI device */
404 		if (ddi_create_minor_node(mip->mi_dip, mip->mi_name, S_IFCHR,
405 		    mip->mi_minor, DDI_NT_NET, 0) != DDI_SUCCESS)
406 			goto fail;
407 		style1_created = B_TRUE;
408 	}
409 
410 	mac_flow_l2tab_create(mip, &mip->mi_flow_tab);
411 
412 	rw_enter(&i_mac_impl_lock, RW_WRITER);
413 	if (mod_hash_insert(i_mac_impl_hash,
414 	    (mod_hash_key_t)mip->mi_name, (mod_hash_val_t)mip) != 0) {
415 		rw_exit(&i_mac_impl_lock);
416 		err = EEXIST;
417 		goto fail;
418 	}
419 
420 	DTRACE_PROBE2(mac__register, struct devnames *, dnp,
421 	    (mac_impl_t *), mip);
422 
423 	/*
424 	 * Mark the MAC to be ready for open.
425 	 */
426 	mip->mi_state_flags &= ~MIS_DISABLED;
427 	rw_exit(&i_mac_impl_lock);
428 
429 	atomic_inc_32(&i_mac_impl_count);
430 
431 	cmn_err(CE_NOTE, "!%s registered", mip->mi_name);
432 	*mhp = (mac_handle_t)mip;
433 	return (0);
434 
435 fail:
436 	if (style1_created)
437 		ddi_remove_minor_node(mip->mi_dip, mip->mi_name);
438 
439 	if (style2_created)
440 		ddi_remove_minor_node(mip->mi_dip, driver);
441 
442 	mac_addr_factory_fini(mip);
443 
444 	/* Clean up registered MAC addresses */
445 	mac_fini_macaddr(mip);
446 
447 	/* Clean up registered rings */
448 	mac_free_rings(mip, MAC_RING_TYPE_RX);
449 	mac_free_rings(mip, MAC_RING_TYPE_TX);
450 
451 	/* Clean up notification thread */
452 	if (mip->mi_notify_thread != NULL)
453 		i_mac_notify_exit(mip);
454 
455 	if (mip->mi_info.mi_unicst_addr != NULL) {
456 		kmem_free(mip->mi_info.mi_unicst_addr,
457 		    mip->mi_type->mt_addr_length);
458 		mip->mi_info.mi_unicst_addr = NULL;
459 	}
460 
461 	mac_driver_stat_delete(mip);
462 
463 	if (mip->mi_type != NULL) {
464 		atomic_dec_32(&mip->mi_type->mt_ref);
465 		mip->mi_type = NULL;
466 	}
467 
468 	if (mip->mi_pdata != NULL) {
469 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
470 		mip->mi_pdata = NULL;
471 		mip->mi_pdata_size = 0;
472 	}
473 
474 	if (minor != 0) {
475 		ASSERT(minor > MAC_MAX_MINOR);
476 		mac_minor_rele(minor);
477 	}
478 
479 	mip->mi_state_flags = 0;
480 	mac_unregister_priv_prop(mip);
481 
482 	/*
483 	 * Clear the state before destroying the mac_impl_t
484 	 */
485 	mip->mi_state_flags = 0;
486 
487 	kmem_cache_free(i_mac_impl_cachep, mip);
488 	return (err);
489 }
490 
491 /*
492  * Unregister from the GLDv3 framework
493  */
494 int
495 mac_unregister(mac_handle_t mh)
496 {
497 	int			err;
498 	mac_impl_t		*mip = (mac_impl_t *)mh;
499 	mod_hash_val_t		val;
500 	mac_margin_req_t	*mmr, *nextmmr;
501 
502 	/* Fail the unregister if there are any open references to this mac. */
503 	if ((err = mac_disable_nowait(mh)) != 0)
504 		return (err);
505 
506 	/*
507 	 * Clean up notification thread and wait for it to exit.
508 	 */
509 	i_mac_notify_exit(mip);
510 
511 	i_mac_perim_enter(mip);
512 
513 	/*
514 	 * There is still resource properties configured over this mac.
515 	 */
516 	if (mip->mi_resource_props.mrp_mask != 0)
517 		mac_fastpath_enable((mac_handle_t)mip);
518 
519 	if (mip->mi_minor < MAC_MAX_MINOR + 1) {
520 		ddi_remove_minor_node(mip->mi_dip, mip->mi_name);
521 		ddi_remove_minor_node(mip->mi_dip,
522 		    (char *)ddi_driver_name(mip->mi_dip));
523 	}
524 
525 	ASSERT(mip->mi_nactiveclients == 0 && !(mip->mi_state_flags &
526 	    MIS_EXCLUSIVE));
527 
528 	mac_driver_stat_delete(mip);
529 
530 	(void) mod_hash_remove(i_mac_impl_hash,
531 	    (mod_hash_key_t)mip->mi_name, &val);
532 	ASSERT(mip == (mac_impl_t *)val);
533 
534 	ASSERT(i_mac_impl_count > 0);
535 	atomic_dec_32(&i_mac_impl_count);
536 
537 	if (mip->mi_pdata != NULL)
538 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
539 	mip->mi_pdata = NULL;
540 	mip->mi_pdata_size = 0;
541 
542 	/*
543 	 * Free the list of margin request.
544 	 */
545 	for (mmr = mip->mi_mmrp; mmr != NULL; mmr = nextmmr) {
546 		nextmmr = mmr->mmr_nextp;
547 		kmem_free(mmr, sizeof (mac_margin_req_t));
548 	}
549 	mip->mi_mmrp = NULL;
550 
551 	mip->mi_linkstate = mip->mi_lowlinkstate = LINK_STATE_UNKNOWN;
552 	kmem_free(mip->mi_info.mi_unicst_addr, mip->mi_type->mt_addr_length);
553 	mip->mi_info.mi_unicst_addr = NULL;
554 
555 	atomic_dec_32(&mip->mi_type->mt_ref);
556 	mip->mi_type = NULL;
557 
558 	/*
559 	 * Free the primary MAC address.
560 	 */
561 	mac_fini_macaddr(mip);
562 
563 	/*
564 	 * free all rings
565 	 */
566 	mac_free_rings(mip, MAC_RING_TYPE_RX);
567 	mac_free_rings(mip, MAC_RING_TYPE_TX);
568 
569 	mac_addr_factory_fini(mip);
570 
571 	bzero(mip->mi_addr, MAXMACADDRLEN);
572 	bzero(mip->mi_dstaddr, MAXMACADDRLEN);
573 
574 	/* and the flows */
575 	mac_flow_tab_destroy(mip->mi_flow_tab);
576 	mip->mi_flow_tab = NULL;
577 
578 	if (mip->mi_minor > MAC_MAX_MINOR)
579 		mac_minor_rele(mip->mi_minor);
580 
581 	cmn_err(CE_NOTE, "!%s unregistered", mip->mi_name);
582 
583 	/*
584 	 * Reset the perim related fields to default values before
585 	 * kmem_cache_free
586 	 */
587 	i_mac_perim_exit(mip);
588 	mip->mi_state_flags = 0;
589 
590 	mac_unregister_priv_prop(mip);
591 
592 	ASSERT(mip->mi_bridge_link == NULL);
593 	kmem_cache_free(i_mac_impl_cachep, mip);
594 
595 	return (0);
596 }
597 
598 /* DATA RECEPTION */
599 
600 /*
601  * This function is invoked for packets received by the MAC driver in
602  * interrupt context. The ring generation number provided by the driver
603  * is matched with the ring generation number held in MAC. If they do not
604  * match, received packets are considered stale packets coming from an older
605  * assignment of the ring. Drop them.
606  */
607 void
608 mac_rx_ring(mac_handle_t mh, mac_ring_handle_t mrh, mblk_t *mp_chain,
609     uint64_t mr_gen_num)
610 {
611 	mac_ring_t		*mr = (mac_ring_t *)mrh;
612 
613 	if ((mr != NULL) && (mr->mr_gen_num != mr_gen_num)) {
614 		DTRACE_PROBE2(mac__rx__rings__stale__packet, uint64_t,
615 		    mr->mr_gen_num, uint64_t, mr_gen_num);
616 		freemsgchain(mp_chain);
617 		return;
618 	}
619 	mac_rx(mh, (mac_resource_handle_t)mrh, mp_chain);
620 }
621 
622 /*
623  * This function is invoked for each packet received by the underlying driver.
624  */
625 void
626 mac_rx(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain)
627 {
628 	mac_impl_t *mip = (mac_impl_t *)mh;
629 
630 	/*
631 	 * Check if the link is part of a bridge.  If not, then we don't need
632 	 * to take the lock to remain consistent.  Make this common case
633 	 * lock-free and tail-call optimized.
634 	 */
635 	if (mip->mi_bridge_link == NULL) {
636 		mac_rx_common(mh, mrh, mp_chain);
637 	} else {
638 		/*
639 		 * Once we take a reference on the bridge link, the bridge
640 		 * module itself can't unload, so the callback pointers are
641 		 * stable.
642 		 */
643 		mutex_enter(&mip->mi_bridge_lock);
644 		if ((mh = mip->mi_bridge_link) != NULL)
645 			mac_bridge_ref_cb(mh, B_TRUE);
646 		mutex_exit(&mip->mi_bridge_lock);
647 		if (mh == NULL) {
648 			mac_rx_common((mac_handle_t)mip, mrh, mp_chain);
649 		} else {
650 			mac_bridge_rx_cb(mh, mrh, mp_chain);
651 			mac_bridge_ref_cb(mh, B_FALSE);
652 		}
653 	}
654 }
655 
656 /*
657  * Special case function: this allows snooping of packets transmitted and
658  * received by TRILL. By design, they go directly into the TRILL module.
659  */
660 void
661 mac_trill_snoop(mac_handle_t mh, mblk_t *mp)
662 {
663 	mac_impl_t *mip = (mac_impl_t *)mh;
664 
665 	if (mip->mi_promisc_list != NULL)
666 		mac_promisc_dispatch(mip, mp, NULL);
667 }
668 
669 /*
670  * This is the upward reentry point for packets arriving from the bridging
671  * module and from mac_rx for links not part of a bridge.
672  */
673 void
674 mac_rx_common(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain)
675 {
676 	mac_impl_t		*mip = (mac_impl_t *)mh;
677 	mac_ring_t		*mr = (mac_ring_t *)mrh;
678 	mac_soft_ring_set_t 	*mac_srs;
679 	mblk_t			*bp = mp_chain;
680 	boolean_t		hw_classified = B_FALSE;
681 
682 	/*
683 	 * If there are any promiscuous mode callbacks defined for
684 	 * this MAC, pass them a copy if appropriate.
685 	 */
686 	if (mip->mi_promisc_list != NULL)
687 		mac_promisc_dispatch(mip, mp_chain, NULL);
688 
689 	if (mr != NULL) {
690 		/*
691 		 * If the SRS teardown has started, just return. The 'mr'
692 		 * continues to be valid until the driver unregisters the mac.
693 		 * Hardware classified packets will not make their way up
694 		 * beyond this point once the teardown has started. The driver
695 		 * is never passed a pointer to a flow entry or SRS or any
696 		 * structure that can be freed much before mac_unregister.
697 		 */
698 		mutex_enter(&mr->mr_lock);
699 		if ((mr->mr_state != MR_INUSE) || (mr->mr_flag &
700 		    (MR_INCIPIENT | MR_CONDEMNED | MR_QUIESCE))) {
701 			mutex_exit(&mr->mr_lock);
702 			freemsgchain(mp_chain);
703 			return;
704 		}
705 		if (mr->mr_classify_type == MAC_HW_CLASSIFIER) {
706 			hw_classified = B_TRUE;
707 			MR_REFHOLD_LOCKED(mr);
708 		}
709 		mutex_exit(&mr->mr_lock);
710 
711 		/*
712 		 * We check if an SRS is controlling this ring.
713 		 * If so, we can directly call the srs_lower_proc
714 		 * routine otherwise we need to go through mac_rx_classify
715 		 * to reach the right place.
716 		 */
717 		if (hw_classified) {
718 			mac_srs = mr->mr_srs;
719 			/*
720 			 * This is supposed to be the fast path.
721 			 * All packets received though here were steered by
722 			 * the hardware classifier, and share the same
723 			 * MAC header info.
724 			 */
725 			mac_srs->srs_rx.sr_lower_proc(mh,
726 			    (mac_resource_handle_t)mac_srs, mp_chain, B_FALSE);
727 			MR_REFRELE(mr);
728 			return;
729 		}
730 		/* We'll fall through to software classification */
731 	} else {
732 		flow_entry_t *flent;
733 		int err;
734 
735 		rw_enter(&mip->mi_rw_lock, RW_READER);
736 		if (mip->mi_single_active_client != NULL) {
737 			flent = mip->mi_single_active_client->mci_flent_list;
738 			FLOW_TRY_REFHOLD(flent, err);
739 			rw_exit(&mip->mi_rw_lock);
740 			if (err == 0) {
741 				(flent->fe_cb_fn)(flent->fe_cb_arg1,
742 				    flent->fe_cb_arg2, mp_chain, B_FALSE);
743 				FLOW_REFRELE(flent);
744 				return;
745 			}
746 		} else {
747 			rw_exit(&mip->mi_rw_lock);
748 		}
749 	}
750 
751 	if (!FLOW_TAB_EMPTY(mip->mi_flow_tab)) {
752 		if ((bp = mac_rx_flow(mh, mrh, bp)) == NULL)
753 			return;
754 	}
755 
756 	freemsgchain(bp);
757 }
758 
759 /* DATA TRANSMISSION */
760 
761 /*
762  * A driver's notification to resume transmission, in case of a provider
763  * without TX rings.
764  */
765 void
766 mac_tx_update(mac_handle_t mh)
767 {
768 	mac_tx_ring_update(mh, NULL);
769 }
770 
771 /*
772  * A driver's notification to resume transmission on the specified TX ring.
773  */
774 void
775 mac_tx_ring_update(mac_handle_t mh, mac_ring_handle_t rh)
776 {
777 	i_mac_tx_srs_notify((mac_impl_t *)mh, rh);
778 }
779 
780 /* LINK STATE */
781 /*
782  * Notify the MAC layer about a link state change
783  */
784 void
785 mac_link_update(mac_handle_t mh, link_state_t link)
786 {
787 	mac_impl_t	*mip = (mac_impl_t *)mh;
788 
789 	/*
790 	 * Save the link state.
791 	 */
792 	mip->mi_lowlinkstate = link;
793 
794 	/*
795 	 * Send a MAC_NOTE_LOWLINK notification.  This tells the notification
796 	 * thread to deliver both lower and upper notifications.
797 	 */
798 	i_mac_notify(mip, MAC_NOTE_LOWLINK);
799 }
800 
801 /*
802  * Notify the MAC layer about a link state change due to bridging.
803  */
804 void
805 mac_link_redo(mac_handle_t mh, link_state_t link)
806 {
807 	mac_impl_t	*mip = (mac_impl_t *)mh;
808 
809 	/*
810 	 * Save the link state.
811 	 */
812 	mip->mi_linkstate = link;
813 
814 	/*
815 	 * Send a MAC_NOTE_LINK notification.  Only upper notifications are
816 	 * made.
817 	 */
818 	i_mac_notify(mip, MAC_NOTE_LINK);
819 }
820 
821 /* MINOR NODE HANDLING */
822 
823 /*
824  * Given a dev_t, return the instance number (PPA) associated with it.
825  * Drivers can use this in their getinfo(9e) implementation to lookup
826  * the instance number (i.e. PPA) of the device, to use as an index to
827  * their own array of soft state structures.
828  *
829  * Returns -1 on error.
830  */
831 int
832 mac_devt_to_instance(dev_t devt)
833 {
834 	return (dld_devt_to_instance(devt));
835 }
836 
837 /*
838  * This function returns the first minor number that is available for
839  * driver private use.  All minor numbers smaller than this are
840  * reserved for GLDv3 use.
841  */
842 minor_t
843 mac_private_minor(void)
844 {
845 	return (MAC_PRIVATE_MINOR);
846 }
847 
848 /* OTHER CONTROL INFORMATION */
849 
850 /*
851  * A driver notified us that its primary MAC address has changed.
852  */
853 void
854 mac_unicst_update(mac_handle_t mh, const uint8_t *addr)
855 {
856 	mac_impl_t	*mip = (mac_impl_t *)mh;
857 
858 	if (mip->mi_type->mt_addr_length == 0)
859 		return;
860 
861 	i_mac_perim_enter(mip);
862 
863 	/*
864 	 * If address changes, freshen the MAC address value and update
865 	 * all MAC clients that share this MAC address.
866 	 */
867 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) != 0) {
868 		mac_freshen_macaddr(mac_find_macaddr(mip, mip->mi_addr),
869 		    (uint8_t *)addr);
870 	}
871 
872 	i_mac_perim_exit(mip);
873 
874 	/*
875 	 * Send a MAC_NOTE_UNICST notification.
876 	 */
877 	i_mac_notify(mip, MAC_NOTE_UNICST);
878 }
879 
880 void
881 mac_dst_update(mac_handle_t mh, const uint8_t *addr)
882 {
883 	mac_impl_t	*mip = (mac_impl_t *)mh;
884 
885 	if (mip->mi_type->mt_addr_length == 0)
886 		return;
887 
888 	i_mac_perim_enter(mip);
889 	bcopy(addr, mip->mi_dstaddr, mip->mi_type->mt_addr_length);
890 	i_mac_perim_exit(mip);
891 	i_mac_notify(mip, MAC_NOTE_DEST);
892 }
893 
894 /*
895  * MAC plugin information changed.
896  */
897 int
898 mac_pdata_update(mac_handle_t mh, void *mac_pdata, size_t dsize)
899 {
900 	mac_impl_t	*mip = (mac_impl_t *)mh;
901 
902 	/*
903 	 * Verify that the plugin supports MAC plugin data and that the
904 	 * supplied data is valid.
905 	 */
906 	if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY))
907 		return (EINVAL);
908 	if (!mip->mi_type->mt_ops.mtops_pdata_verify(mac_pdata, dsize))
909 		return (EINVAL);
910 
911 	if (mip->mi_pdata != NULL)
912 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
913 
914 	mip->mi_pdata = kmem_alloc(dsize, KM_SLEEP);
915 	bcopy(mac_pdata, mip->mi_pdata, dsize);
916 	mip->mi_pdata_size = dsize;
917 
918 	/*
919 	 * Since the MAC plugin data is used to construct MAC headers that
920 	 * were cached in fast-path headers, we need to flush fast-path
921 	 * information for links associated with this mac.
922 	 */
923 	i_mac_notify(mip, MAC_NOTE_FASTPATH_FLUSH);
924 	return (0);
925 }
926 
927 /*
928  * Invoked by driver as well as the framework to notify its capability change.
929  */
930 void
931 mac_capab_update(mac_handle_t mh)
932 {
933 	/* Send MAC_NOTE_CAPAB_CHG notification */
934 	i_mac_notify((mac_impl_t *)mh, MAC_NOTE_CAPAB_CHG);
935 }
936 
937 int
938 mac_maxsdu_update(mac_handle_t mh, uint_t sdu_max)
939 {
940 	mac_impl_t	*mip = (mac_impl_t *)mh;
941 
942 	if (sdu_max == 0 || sdu_max < mip->mi_sdu_min)
943 		return (EINVAL);
944 	mip->mi_sdu_max = sdu_max;
945 
946 	/* Send a MAC_NOTE_SDU_SIZE notification. */
947 	i_mac_notify(mip, MAC_NOTE_SDU_SIZE);
948 	return (0);
949 }
950 
951 static void
952 mac_ring_intr_retarget(mac_group_t *group, mac_ring_t *ring)
953 {
954 	mac_client_impl_t *mcip;
955 	flow_entry_t *flent;
956 	mac_soft_ring_set_t *mac_rx_srs;
957 	mac_cpus_t *srs_cpu;
958 	int i;
959 
960 	if (((mcip = MAC_GROUP_ONLY_CLIENT(group)) != NULL) &&
961 	    (!ring->mr_info.mri_intr.mi_ddi_shared)) {
962 		/* interrupt can be re-targeted */
963 		ASSERT(group->mrg_state == MAC_GROUP_STATE_RESERVED);
964 		flent = mcip->mci_flent;
965 		if (ring->mr_type == MAC_RING_TYPE_RX) {
966 			for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
967 				mac_rx_srs = flent->fe_rx_srs[i];
968 				if (mac_rx_srs->srs_ring != ring)
969 					continue;
970 				srs_cpu = &mac_rx_srs->srs_cpu;
971 				mutex_enter(&cpu_lock);
972 				mac_rx_srs_retarget_intr(mac_rx_srs,
973 				    srs_cpu->mc_rx_intr_cpu);
974 				mutex_exit(&cpu_lock);
975 				break;
976 			}
977 		} else {
978 			if (flent->fe_tx_srs != NULL) {
979 				mutex_enter(&cpu_lock);
980 				mac_tx_srs_retarget_intr(
981 				    flent->fe_tx_srs);
982 				mutex_exit(&cpu_lock);
983 			}
984 		}
985 	}
986 }
987 
988 /*
989  * Clients like aggr create pseudo rings (mac_ring_t) and expose them to
990  * their clients. There is a 1-1 mapping pseudo ring and the hardware
991  * ring. ddi interrupt handles are exported from the hardware ring to
992  * the pseudo ring. Thus when the interrupt handle changes, clients of
993  * aggr that are using the handle need to use the new handle and
994  * re-target their interrupts.
995  */
996 static void
997 mac_pseudo_ring_intr_retarget(mac_impl_t *mip, mac_ring_t *ring,
998     ddi_intr_handle_t ddh)
999 {
1000 	mac_ring_t *pring;
1001 	mac_group_t *pgroup;
1002 	mac_impl_t *pmip;
1003 	char macname[MAXNAMELEN];
1004 	mac_perim_handle_t p_mph;
1005 	uint64_t saved_gen_num;
1006 
1007 again:
1008 	pring = (mac_ring_t *)ring->mr_prh;
1009 	pgroup = (mac_group_t *)pring->mr_gh;
1010 	pmip = (mac_impl_t *)pgroup->mrg_mh;
1011 	saved_gen_num = ring->mr_gen_num;
1012 	(void) strlcpy(macname, pmip->mi_name, MAXNAMELEN);
1013 	/*
1014 	 * We need to enter aggr's perimeter. The locking hierarchy
1015 	 * dictates that aggr's perimeter should be entered first
1016 	 * and then the port's perimeter. So drop the port's
1017 	 * perimeter, enter aggr's and then re-enter port's
1018 	 * perimeter.
1019 	 */
1020 	i_mac_perim_exit(mip);
1021 	/*
1022 	 * While we know pmip is the aggr's mip, there is a
1023 	 * possibility that aggr could have unregistered by
1024 	 * the time we exit port's perimeter (mip) and
1025 	 * enter aggr's perimeter (pmip). To avoid that
1026 	 * scenario, enter aggr's perimeter using its name.
1027 	 */
1028 	if (mac_perim_enter_by_macname(macname, &p_mph) != 0)
1029 		return;
1030 	i_mac_perim_enter(mip);
1031 	/*
1032 	 * Check if the ring got assigned to another aggregation before
1033 	 * be could enter aggr's and the port's perimeter. When a ring
1034 	 * gets deleted from an aggregation, it calls mac_stop_ring()
1035 	 * which increments the generation number. So checking
1036 	 * generation number will be enough.
1037 	 */
1038 	if (ring->mr_gen_num != saved_gen_num && ring->mr_prh != NULL) {
1039 		i_mac_perim_exit(mip);
1040 		mac_perim_exit(p_mph);
1041 		i_mac_perim_enter(mip);
1042 		goto again;
1043 	}
1044 
1045 	/* Check if pseudo ring is still present */
1046 	if (ring->mr_prh != NULL) {
1047 		pring->mr_info.mri_intr.mi_ddi_handle = ddh;
1048 		pring->mr_info.mri_intr.mi_ddi_shared =
1049 		    ring->mr_info.mri_intr.mi_ddi_shared;
1050 		if (ddh != NULL)
1051 			mac_ring_intr_retarget(pgroup, pring);
1052 	}
1053 	i_mac_perim_exit(mip);
1054 	mac_perim_exit(p_mph);
1055 }
1056 /*
1057  * API called by driver to provide new interrupt handle for TX/RX rings.
1058  * This usually happens when IRM (Interrupt Resource Manangement)
1059  * framework either gives the driver more MSI-x interrupts or takes
1060  * away MSI-x interrupts from the driver.
1061  */
1062 void
1063 mac_ring_intr_set(mac_ring_handle_t mrh, ddi_intr_handle_t ddh)
1064 {
1065 	mac_ring_t	*ring = (mac_ring_t *)mrh;
1066 	mac_group_t	*group = (mac_group_t *)ring->mr_gh;
1067 	mac_impl_t	*mip = (mac_impl_t *)group->mrg_mh;
1068 
1069 	i_mac_perim_enter(mip);
1070 	ring->mr_info.mri_intr.mi_ddi_handle = ddh;
1071 	if (ddh == NULL) {
1072 		/* Interrupts being reset */
1073 		ring->mr_info.mri_intr.mi_ddi_shared = B_FALSE;
1074 		if (ring->mr_prh != NULL) {
1075 			mac_pseudo_ring_intr_retarget(mip, ring, ddh);
1076 			return;
1077 		}
1078 	} else {
1079 		/* New interrupt handle */
1080 		mac_compare_ddi_handle(mip->mi_rx_groups,
1081 		    mip->mi_rx_group_count, ring);
1082 		if (!ring->mr_info.mri_intr.mi_ddi_shared) {
1083 			mac_compare_ddi_handle(mip->mi_tx_groups,
1084 			    mip->mi_tx_group_count, ring);
1085 		}
1086 		if (ring->mr_prh != NULL) {
1087 			mac_pseudo_ring_intr_retarget(mip, ring, ddh);
1088 			return;
1089 		} else {
1090 			mac_ring_intr_retarget(group, ring);
1091 		}
1092 	}
1093 	i_mac_perim_exit(mip);
1094 }
1095 
1096 /* PRIVATE FUNCTIONS, FOR INTERNAL USE ONLY */
1097 
1098 /*
1099  * Updates the mac_impl structure with the current state of the link
1100  */
1101 static void
1102 i_mac_log_link_state(mac_impl_t *mip)
1103 {
1104 	/*
1105 	 * If no change, then it is not interesting.
1106 	 */
1107 	if (mip->mi_lastlowlinkstate == mip->mi_lowlinkstate)
1108 		return;
1109 
1110 	switch (mip->mi_lowlinkstate) {
1111 	case LINK_STATE_UP:
1112 		if (mip->mi_type->mt_ops.mtops_ops & MTOPS_LINK_DETAILS) {
1113 			char det[200];
1114 
1115 			mip->mi_type->mt_ops.mtops_link_details(det,
1116 			    sizeof (det), (mac_handle_t)mip, mip->mi_pdata);
1117 
1118 			cmn_err(CE_NOTE, "!%s link up, %s", mip->mi_name, det);
1119 		} else {
1120 			cmn_err(CE_NOTE, "!%s link up", mip->mi_name);
1121 		}
1122 		break;
1123 
1124 	case LINK_STATE_DOWN:
1125 		/*
1126 		 * Only transitions from UP to DOWN are interesting
1127 		 */
1128 		if (mip->mi_lastlowlinkstate != LINK_STATE_UNKNOWN)
1129 			cmn_err(CE_NOTE, "!%s link down", mip->mi_name);
1130 		break;
1131 
1132 	case LINK_STATE_UNKNOWN:
1133 		/*
1134 		 * This case is normally not interesting.
1135 		 */
1136 		break;
1137 	}
1138 	mip->mi_lastlowlinkstate = mip->mi_lowlinkstate;
1139 }
1140 
1141 /*
1142  * Main routine for the callbacks notifications thread
1143  */
1144 static void
1145 i_mac_notify_thread(void *arg)
1146 {
1147 	mac_impl_t	*mip = arg;
1148 	callb_cpr_t	cprinfo;
1149 	mac_cb_t	*mcb;
1150 	mac_cb_info_t	*mcbi;
1151 	mac_notify_cb_t	*mncb;
1152 
1153 	mcbi = &mip->mi_notify_cb_info;
1154 	CALLB_CPR_INIT(&cprinfo, mcbi->mcbi_lockp, callb_generic_cpr,
1155 	    "i_mac_notify_thread");
1156 
1157 	mutex_enter(mcbi->mcbi_lockp);
1158 
1159 	for (;;) {
1160 		uint32_t	bits;
1161 		uint32_t	type;
1162 
1163 		bits = mip->mi_notify_bits;
1164 		if (bits == 0) {
1165 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
1166 			cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
1167 			CALLB_CPR_SAFE_END(&cprinfo, mcbi->mcbi_lockp);
1168 			continue;
1169 		}
1170 		mip->mi_notify_bits = 0;
1171 		if ((bits & (1 << MAC_NNOTE)) != 0) {
1172 			/* request to quit */
1173 			ASSERT(mip->mi_state_flags & MIS_DISABLED);
1174 			break;
1175 		}
1176 
1177 		mutex_exit(mcbi->mcbi_lockp);
1178 
1179 		/*
1180 		 * Log link changes on the actual link, but then do reports on
1181 		 * synthetic state (if part of a bridge).
1182 		 */
1183 		if ((bits & (1 << MAC_NOTE_LOWLINK)) != 0) {
1184 			link_state_t newstate;
1185 			mac_handle_t mh;
1186 
1187 			i_mac_log_link_state(mip);
1188 			newstate = mip->mi_lowlinkstate;
1189 			if (mip->mi_bridge_link != NULL) {
1190 				mutex_enter(&mip->mi_bridge_lock);
1191 				if ((mh = mip->mi_bridge_link) != NULL) {
1192 					newstate = mac_bridge_ls_cb(mh,
1193 					    newstate);
1194 				}
1195 				mutex_exit(&mip->mi_bridge_lock);
1196 			}
1197 			if (newstate != mip->mi_linkstate) {
1198 				mip->mi_linkstate = newstate;
1199 				bits |= 1 << MAC_NOTE_LINK;
1200 			}
1201 		}
1202 
1203 		/*
1204 		 * Do notification callbacks for each notification type.
1205 		 */
1206 		for (type = 0; type < MAC_NNOTE; type++) {
1207 			if ((bits & (1 << type)) == 0) {
1208 				continue;
1209 			}
1210 
1211 			if (mac_notify_cb_list[type] != NULL)
1212 				(*mac_notify_cb_list[type])(mip);
1213 
1214 			/*
1215 			 * Walk the list of notifications.
1216 			 */
1217 			MAC_CALLBACK_WALKER_INC(&mip->mi_notify_cb_info);
1218 			for (mcb = mip->mi_notify_cb_list; mcb != NULL;
1219 			    mcb = mcb->mcb_nextp) {
1220 				mncb = (mac_notify_cb_t *)mcb->mcb_objp;
1221 				mncb->mncb_fn(mncb->mncb_arg, type);
1222 			}
1223 			MAC_CALLBACK_WALKER_DCR(&mip->mi_notify_cb_info,
1224 			    &mip->mi_notify_cb_list);
1225 		}
1226 
1227 		mutex_enter(mcbi->mcbi_lockp);
1228 	}
1229 
1230 	mip->mi_state_flags |= MIS_NOTIFY_DONE;
1231 	cv_broadcast(&mcbi->mcbi_cv);
1232 
1233 	/* CALLB_CPR_EXIT drops the lock */
1234 	CALLB_CPR_EXIT(&cprinfo);
1235 	thread_exit();
1236 }
1237 
1238 /*
1239  * Signal the i_mac_notify_thread asking it to quit.
1240  * Then wait till it is done.
1241  */
1242 void
1243 i_mac_notify_exit(mac_impl_t *mip)
1244 {
1245 	mac_cb_info_t	*mcbi;
1246 
1247 	mcbi = &mip->mi_notify_cb_info;
1248 
1249 	mutex_enter(mcbi->mcbi_lockp);
1250 	mip->mi_notify_bits = (1 << MAC_NNOTE);
1251 	cv_broadcast(&mcbi->mcbi_cv);
1252 
1253 
1254 	while ((mip->mi_notify_thread != NULL) &&
1255 	    !(mip->mi_state_flags & MIS_NOTIFY_DONE)) {
1256 		cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
1257 	}
1258 
1259 	/* Necessary clean up before doing kmem_cache_free */
1260 	mip->mi_state_flags &= ~MIS_NOTIFY_DONE;
1261 	mip->mi_notify_bits = 0;
1262 	mip->mi_notify_thread = NULL;
1263 	mutex_exit(mcbi->mcbi_lockp);
1264 }
1265 
1266 /*
1267  * Entry point invoked by drivers to dynamically add a ring to an
1268  * existing group.
1269  */
1270 int
1271 mac_group_add_ring(mac_group_handle_t gh, int index)
1272 {
1273 	mac_group_t *group = (mac_group_t *)gh;
1274 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
1275 	int ret;
1276 
1277 	i_mac_perim_enter(mip);
1278 	ret = i_mac_group_add_ring(group, NULL, index);
1279 	i_mac_perim_exit(mip);
1280 	return (ret);
1281 }
1282 
1283 /*
1284  * Entry point invoked by drivers to dynamically remove a ring
1285  * from an existing group. The specified ring handle must no longer
1286  * be used by the driver after a call to this function.
1287  */
1288 void
1289 mac_group_rem_ring(mac_group_handle_t gh, mac_ring_handle_t rh)
1290 {
1291 	mac_group_t *group = (mac_group_t *)gh;
1292 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
1293 
1294 	i_mac_perim_enter(mip);
1295 	i_mac_group_rem_ring(group, (mac_ring_t *)rh, B_TRUE);
1296 	i_mac_perim_exit(mip);
1297 }
1298 
1299 /*
1300  * mac_prop_info_*() callbacks called from the driver's prefix_propinfo()
1301  * entry points.
1302  */
1303 
1304 void
1305 mac_prop_info_set_default_uint8(mac_prop_info_handle_t ph, uint8_t val)
1306 {
1307 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
1308 
1309 	/* nothing to do if the caller doesn't want the default value */
1310 	if (pr->pr_default == NULL)
1311 		return;
1312 
1313 	ASSERT(pr->pr_default_size >= sizeof (uint8_t));
1314 
1315 	*(uint8_t *)(pr->pr_default) = val;
1316 	pr->pr_flags |= MAC_PROP_INFO_DEFAULT;
1317 }
1318 
1319 void
1320 mac_prop_info_set_default_uint64(mac_prop_info_handle_t ph, uint64_t val)
1321 {
1322 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
1323 
1324 	/* nothing to do if the caller doesn't want the default value */
1325 	if (pr->pr_default == NULL)
1326 		return;
1327 
1328 	ASSERT(pr->pr_default_size >= sizeof (uint64_t));
1329 
1330 	bcopy(&val, pr->pr_default, sizeof (val));
1331 
1332 	pr->pr_flags |= MAC_PROP_INFO_DEFAULT;
1333 }
1334 
1335 void
1336 mac_prop_info_set_default_uint32(mac_prop_info_handle_t ph, uint32_t val)
1337 {
1338 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
1339 
1340 	/* nothing to do if the caller doesn't want the default value */
1341 	if (pr->pr_default == NULL)
1342 		return;
1343 
1344 	ASSERT(pr->pr_default_size >= sizeof (uint32_t));
1345 
1346 	bcopy(&val, pr->pr_default, sizeof (val));
1347 
1348 	pr->pr_flags |= MAC_PROP_INFO_DEFAULT;
1349 }
1350 
1351 void
1352 mac_prop_info_set_default_str(mac_prop_info_handle_t ph, const char *str)
1353 {
1354 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
1355 
1356 	/* nothing to do if the caller doesn't want the default value */
1357 	if (pr->pr_default == NULL)
1358 		return;
1359 
1360 	if (strlen(str) >= pr->pr_default_size)
1361 		pr->pr_errno = ENOBUFS;
1362 	else
1363 		(void) strlcpy(pr->pr_default, str, pr->pr_default_size);
1364 	pr->pr_flags |= MAC_PROP_INFO_DEFAULT;
1365 }
1366 
1367 void
1368 mac_prop_info_set_default_link_flowctrl(mac_prop_info_handle_t ph,
1369     link_flowctrl_t val)
1370 {
1371 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
1372 
1373 	/* nothing to do if the caller doesn't want the default value */
1374 	if (pr->pr_default == NULL)
1375 		return;
1376 
1377 	ASSERT(pr->pr_default_size >= sizeof (link_flowctrl_t));
1378 
1379 	bcopy(&val, pr->pr_default, sizeof (val));
1380 
1381 	pr->pr_flags |= MAC_PROP_INFO_DEFAULT;
1382 }
1383 
1384 void
1385 mac_prop_info_set_range_uint32(mac_prop_info_handle_t ph, uint32_t min,
1386     uint32_t max)
1387 {
1388 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
1389 	mac_propval_range_t *range = pr->pr_range;
1390 	mac_propval_uint32_range_t *range32;
1391 
1392 	/* nothing to do if the caller doesn't want the range info */
1393 	if (range == NULL)
1394 		return;
1395 
1396 	if (pr->pr_range_cur_count++ == 0) {
1397 		/* first range */
1398 		pr->pr_flags |= MAC_PROP_INFO_RANGE;
1399 		range->mpr_type = MAC_PROPVAL_UINT32;
1400 	} else {
1401 		/* all ranges of a property should be of the same type */
1402 		ASSERT(range->mpr_type == MAC_PROPVAL_UINT32);
1403 		if (pr->pr_range_cur_count > range->mpr_count) {
1404 			pr->pr_errno = ENOSPC;
1405 			return;
1406 		}
1407 	}
1408 
1409 	range32 = range->mpr_range_uint32;
1410 	range32[pr->pr_range_cur_count - 1].mpur_min = min;
1411 	range32[pr->pr_range_cur_count - 1].mpur_max = max;
1412 }
1413 
1414 void
1415 mac_prop_info_set_perm(mac_prop_info_handle_t ph, uint8_t perm)
1416 {
1417 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
1418 
1419 	pr->pr_perm = perm;
1420 	pr->pr_flags |= MAC_PROP_INFO_PERM;
1421 }
1422 
1423 void mac_hcksum_get(mblk_t *mp, uint32_t *start, uint32_t *stuff,
1424     uint32_t *end, uint32_t *value, uint32_t *flags_ptr)
1425 {
1426 	uint32_t flags;
1427 
1428 	ASSERT(DB_TYPE(mp) == M_DATA);
1429 
1430 	flags = DB_CKSUMFLAGS(mp) & HCK_FLAGS;
1431 	if ((flags & (HCK_PARTIALCKSUM | HCK_FULLCKSUM)) != 0) {
1432 		if (value != NULL)
1433 			*value = (uint32_t)DB_CKSUM16(mp);
1434 		if ((flags & HCK_PARTIALCKSUM) != 0) {
1435 			if (start != NULL)
1436 				*start = (uint32_t)DB_CKSUMSTART(mp);
1437 			if (stuff != NULL)
1438 				*stuff = (uint32_t)DB_CKSUMSTUFF(mp);
1439 			if (end != NULL)
1440 				*end = (uint32_t)DB_CKSUMEND(mp);
1441 		}
1442 	}
1443 
1444 	if (flags_ptr != NULL)
1445 		*flags_ptr = flags;
1446 }
1447 
1448 void mac_hcksum_set(mblk_t *mp, uint32_t start, uint32_t stuff,
1449     uint32_t end, uint32_t value, uint32_t flags)
1450 {
1451 	ASSERT(DB_TYPE(mp) == M_DATA);
1452 
1453 	DB_CKSUMSTART(mp) = (intptr_t)start;
1454 	DB_CKSUMSTUFF(mp) = (intptr_t)stuff;
1455 	DB_CKSUMEND(mp) = (intptr_t)end;
1456 	DB_CKSUMFLAGS(mp) = (uint16_t)flags;
1457 	DB_CKSUM16(mp) = (uint16_t)value;
1458 }
1459 
1460 void
1461 mac_lso_get(mblk_t *mp, uint32_t *mss, uint32_t *flags)
1462 {
1463 	ASSERT(DB_TYPE(mp) == M_DATA);
1464 
1465 	if (flags != NULL) {
1466 		*flags = DB_CKSUMFLAGS(mp) & HW_LSO;
1467 		if ((*flags != 0) && (mss != NULL))
1468 			*mss = (uint32_t)DB_LSOMSS(mp);
1469 	}
1470 }
1471