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