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