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