xref: /titanic_44/usr/src/uts/common/io/mac/mac_provider.c (revision 2d6b5ea734bb47d251c82670646fde46af15fd69)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/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/modctl.h>
44 #include <sys/fs/dv_node.h>
45 #include <sys/thread.h>
46 #include <sys/proc.h>
47 #include <sys/callb.h>
48 #include <sys/cpuvar.h>
49 #include <sys/atomic.h>
50 #include <sys/sdt.h>
51 #include <sys/mac_flow.h>
52 #include <sys/ddi_intr_impl.h>
53 #include <sys/disp.h>
54 #include <sys/sdt.h>
55 
56 /*
57  * MAC Provider Interface.
58  *
59  * Interface for GLDv3 compatible NIC drivers.
60  */
61 
62 static void i_mac_notify_thread(void *);
63 
64 typedef void (*mac_notify_default_cb_fn_t)(mac_impl_t *);
65 
66 typedef struct mac_notify_default_cb_s {
67 	mac_notify_type_t		mac_notify_type;
68 	mac_notify_default_cb_fn_t	mac_notify_cb_fn;
69 }mac_notify_default_cb_t;
70 
71 mac_notify_default_cb_t mac_notify_cb_list[] = {
72 	{ MAC_NOTE_LINK,		mac_fanout_recompute},
73 	{ MAC_NOTE_PROMISC,		NULL},
74 	{ MAC_NOTE_UNICST,		NULL},
75 	{ MAC_NOTE_TX,			NULL},
76 	{ MAC_NOTE_RESOURCE,		NULL},
77 	{ MAC_NOTE_DEVPROMISC,		NULL},
78 	{ MAC_NOTE_FASTPATH_FLUSH,	NULL},
79 	{ MAC_NOTE_SDU_SIZE,		NULL},
80 	{ MAC_NOTE_MARGIN,		NULL},
81 	{ MAC_NOTE_CAPAB_CHG,		NULL},
82 	{ MAC_NNOTE,			NULL},
83 };
84 
85 /*
86  * Driver support functions.
87  */
88 
89 /* REGISTRATION */
90 
91 mac_register_t *
92 mac_alloc(uint_t mac_version)
93 {
94 	mac_register_t *mregp;
95 
96 	/*
97 	 * Make sure there isn't a version mismatch between the driver and
98 	 * the framework.  In the future, if multiple versions are
99 	 * supported, this check could become more sophisticated.
100 	 */
101 	if (mac_version != MAC_VERSION)
102 		return (NULL);
103 
104 	mregp = kmem_zalloc(sizeof (mac_register_t), KM_SLEEP);
105 	mregp->m_version = mac_version;
106 	return (mregp);
107 }
108 
109 void
110 mac_free(mac_register_t *mregp)
111 {
112 	kmem_free(mregp, sizeof (mac_register_t));
113 }
114 
115 /*
116  * mac_register() is how drivers register new MACs with the GLDv3
117  * framework.  The mregp argument is allocated by drivers using the
118  * mac_alloc() function, and can be freed using mac_free() immediately upon
119  * return from mac_register().  Upon success (0 return value), the mhp
120  * opaque pointer becomes the driver's handle to its MAC interface, and is
121  * the argument to all other mac module entry points.
122  */
123 /* ARGSUSED */
124 int
125 mac_register(mac_register_t *mregp, mac_handle_t *mhp)
126 {
127 	mac_impl_t		*mip;
128 	mactype_t		*mtype;
129 	int			err = EINVAL;
130 	struct devnames		*dnp = NULL;
131 	uint_t			instance;
132 	boolean_t		style1_created = B_FALSE;
133 	boolean_t		style2_created = B_FALSE;
134 	mac_capab_legacy_t	legacy;
135 	char			*driver;
136 	minor_t			minor = 0;
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 	driver = (char *)ddi_driver_name(mip->mi_dip);
196 
197 	/* Construct the MAC name as <drvname><instance> */
198 	(void) snprintf(mip->mi_name, sizeof (mip->mi_name), "%s%d",
199 	    driver, instance);
200 
201 	mip->mi_driver = mregp->m_driver;
202 
203 	mip->mi_type = mtype;
204 	mip->mi_margin = mregp->m_margin;
205 	mip->mi_info.mi_media = mtype->mt_type;
206 	mip->mi_info.mi_nativemedia = mtype->mt_nativetype;
207 	if (mregp->m_max_sdu <= mregp->m_min_sdu)
208 		goto fail;
209 	mip->mi_sdu_min = mregp->m_min_sdu;
210 	mip->mi_sdu_max = mregp->m_max_sdu;
211 	mip->mi_info.mi_addr_length = mip->mi_type->mt_addr_length;
212 	/*
213 	 * If the media supports a broadcast address, cache a pointer to it
214 	 * in the mac_info_t so that upper layers can use it.
215 	 */
216 	mip->mi_info.mi_brdcst_addr = mip->mi_type->mt_brdcst_addr;
217 
218 	mip->mi_v12n_level = mregp->m_v12n;
219 
220 	/*
221 	 * Copy the unicast source address into the mac_info_t, but only if
222 	 * the MAC-Type defines a non-zero address length.  We need to
223 	 * handle MAC-Types that have an address length of 0
224 	 * (point-to-point protocol MACs for example).
225 	 */
226 	if (mip->mi_type->mt_addr_length > 0) {
227 		if (mregp->m_src_addr == NULL)
228 			goto fail;
229 		mip->mi_info.mi_unicst_addr =
230 		    kmem_alloc(mip->mi_type->mt_addr_length, KM_SLEEP);
231 		bcopy(mregp->m_src_addr, mip->mi_info.mi_unicst_addr,
232 		    mip->mi_type->mt_addr_length);
233 
234 		/*
235 		 * Copy the fixed 'factory' MAC address from the immutable
236 		 * info.  This is taken to be the MAC address currently in
237 		 * use.
238 		 */
239 		bcopy(mip->mi_info.mi_unicst_addr, mip->mi_addr,
240 		    mip->mi_type->mt_addr_length);
241 
242 		/*
243 		 * At this point, we should set up the classification
244 		 * rules etc but we delay it till mac_open() so that
245 		 * the resource discovery has taken place and we
246 		 * know someone wants to use the device. Otherwise
247 		 * memory gets allocated for Rx ring structures even
248 		 * during probe.
249 		 */
250 
251 		/* Copy the destination address if one is provided. */
252 		if (mregp->m_dst_addr != NULL) {
253 			bcopy(mregp->m_dst_addr, mip->mi_dstaddr,
254 			    mip->mi_type->mt_addr_length);
255 		}
256 	} else if (mregp->m_src_addr != NULL) {
257 		goto fail;
258 	}
259 
260 	/*
261 	 * The format of the m_pdata is specific to the plugin.  It is
262 	 * passed in as an argument to all of the plugin callbacks.  The
263 	 * driver can update this information by calling
264 	 * mac_pdata_update().
265 	 */
266 	if (mregp->m_pdata != NULL) {
267 		/*
268 		 * Verify that the plugin supports MAC plugin data and that
269 		 * the supplied data is valid.
270 		 */
271 		if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY))
272 			goto fail;
273 		if (!mip->mi_type->mt_ops.mtops_pdata_verify(mregp->m_pdata,
274 		    mregp->m_pdata_size)) {
275 			goto fail;
276 		}
277 		mip->mi_pdata = kmem_alloc(mregp->m_pdata_size, KM_SLEEP);
278 		bcopy(mregp->m_pdata, mip->mi_pdata, mregp->m_pdata_size);
279 		mip->mi_pdata_size = mregp->m_pdata_size;
280 	}
281 
282 	/*
283 	 * Register the private properties.
284 	 */
285 	mac_register_priv_prop(mip, mregp->m_priv_props,
286 	    mregp->m_priv_prop_count);
287 
288 	/*
289 	 * Stash the driver callbacks into the mac_impl_t, but first sanity
290 	 * check to make sure all mandatory callbacks are set.
291 	 */
292 	if (mregp->m_callbacks->mc_getstat == NULL ||
293 	    mregp->m_callbacks->mc_start == NULL ||
294 	    mregp->m_callbacks->mc_stop == NULL ||
295 	    mregp->m_callbacks->mc_setpromisc == NULL ||
296 	    mregp->m_callbacks->mc_multicst == NULL) {
297 		goto fail;
298 	}
299 	mip->mi_callbacks = mregp->m_callbacks;
300 
301 	if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_LEGACY, &legacy))
302 		mip->mi_state_flags |= MIS_LEGACY;
303 
304 	if (mip->mi_state_flags & MIS_LEGACY) {
305 		mip->mi_unsup_note = legacy.ml_unsup_note;
306 		mip->mi_phy_dev = legacy.ml_dev;
307 	} else {
308 		mip->mi_unsup_note = 0;
309 		mip->mi_phy_dev = makedevice(ddi_driver_major(mip->mi_dip),
310 		    ddi_get_instance(mip->mi_dip) + 1);
311 	}
312 
313 	/*
314 	 * Allocate a notification thread. thread_create blocks for memory
315 	 * if needed, it never fails.
316 	 */
317 	mip->mi_notify_thread = thread_create(NULL, 0, i_mac_notify_thread,
318 	    mip, 0, &p0, TS_RUN, minclsyspri);
319 
320 	/*
321 	 * Initialize the capabilities
322 	 */
323 
324 	if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, NULL))
325 		mip->mi_state_flags |= MIS_IS_VNIC;
326 
327 	if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR, NULL))
328 		mip->mi_state_flags |= MIS_IS_AGGR;
329 
330 	mac_addr_factory_init(mip);
331 
332 	/*
333 	 * Enforce the virtrualization level registered.
334 	 */
335 	if (mip->mi_v12n_level & MAC_VIRT_LEVEL1) {
336 		if (mac_init_rings(mip, MAC_RING_TYPE_RX) != 0 ||
337 		    mac_init_rings(mip, MAC_RING_TYPE_TX) != 0)
338 			goto fail;
339 
340 		/*
341 		 * The driver needs to register at least rx rings for this
342 		 * virtualization level.
343 		 */
344 		if (mip->mi_rx_groups == NULL)
345 			goto fail;
346 	}
347 
348 	/*
349 	 * The driver must set mc_unicst entry point to NULL when it advertises
350 	 * CAP_RINGS for rx groups.
351 	 */
352 	if (mip->mi_rx_groups != NULL) {
353 		if (mregp->m_callbacks->mc_unicst != NULL)
354 			goto fail;
355 	} else {
356 		if (mregp->m_callbacks->mc_unicst == NULL)
357 			goto fail;
358 	}
359 
360 	/*
361 	 * The driver must set mc_tx entry point to NULL when it advertises
362 	 * CAP_RINGS for tx rings.
363 	 */
364 	if (mip->mi_tx_groups != NULL) {
365 		if (mregp->m_callbacks->mc_tx != NULL)
366 			goto fail;
367 	} else {
368 		if (mregp->m_callbacks->mc_tx == NULL)
369 			goto fail;
370 	}
371 
372 	/*
373 	 * Initialize MAC addresses. Must be called after mac_init_rings().
374 	 */
375 	mac_init_macaddr(mip);
376 
377 	mip->mi_share_capab.ms_snum = 0;
378 	if (mip->mi_v12n_level & MAC_VIRT_HIO) {
379 		(void) mac_capab_get((mac_handle_t)mip, MAC_CAPAB_SHARES,
380 		    &mip->mi_share_capab);
381 	}
382 
383 	/*
384 	 * Initialize the kstats for this device.
385 	 */
386 	mac_stat_create(mip);
387 
388 	/* Zero out any properties. */
389 	bzero(&mip->mi_resource_props, sizeof (mac_resource_props_t));
390 
391 	/* set the gldv3 flag in dn_flags */
392 	dnp = &devnamesp[ddi_driver_major(mip->mi_dip)];
393 	LOCK_DEV_OPS(&dnp->dn_lock);
394 	dnp->dn_flags |= (DN_GLDV3_DRIVER | DN_NETWORK_DRIVER);
395 	UNLOCK_DEV_OPS(&dnp->dn_lock);
396 
397 	if (mip->mi_minor < MAC_MAX_MINOR + 1) {
398 		/* Create a style-2 DLPI device */
399 		if (ddi_create_minor_node(mip->mi_dip, driver, S_IFCHR, 0,
400 		    DDI_NT_NET, CLONE_DEV) != DDI_SUCCESS)
401 			goto fail;
402 		style2_created = B_TRUE;
403 
404 		/* Create a style-1 DLPI device */
405 		if (ddi_create_minor_node(mip->mi_dip, mip->mi_name, S_IFCHR,
406 		    mip->mi_minor, DDI_NT_NET, 0) != DDI_SUCCESS)
407 			goto fail;
408 		style1_created = B_TRUE;
409 	}
410 
411 	mac_flow_l2tab_create(mip, &mip->mi_flow_tab);
412 
413 	rw_enter(&i_mac_impl_lock, RW_WRITER);
414 	if (mod_hash_insert(i_mac_impl_hash,
415 	    (mod_hash_key_t)mip->mi_name, (mod_hash_val_t)mip) != 0) {
416 		rw_exit(&i_mac_impl_lock);
417 		err = EEXIST;
418 		goto fail;
419 	}
420 
421 	DTRACE_PROBE2(mac__register, struct devnames *, dnp,
422 	    (mac_impl_t *), mip);
423 
424 	/*
425 	 * Mark the MAC to be ready for open.
426 	 */
427 	mip->mi_state_flags &= ~MIS_DISABLED;
428 	rw_exit(&i_mac_impl_lock);
429 
430 	atomic_inc_32(&i_mac_impl_count);
431 
432 	cmn_err(CE_NOTE, "!%s registered", mip->mi_name);
433 	*mhp = (mac_handle_t)mip;
434 	return (0);
435 
436 fail:
437 	if (style1_created)
438 		ddi_remove_minor_node(mip->mi_dip, mip->mi_name);
439 
440 	if (style2_created)
441 		ddi_remove_minor_node(mip->mi_dip, driver);
442 
443 	mac_addr_factory_fini(mip);
444 
445 	/* Clean up registered MAC addresses */
446 	mac_fini_macaddr(mip);
447 
448 	/* Clean up registered rings */
449 	mac_free_rings(mip, MAC_RING_TYPE_RX);
450 	mac_free_rings(mip, MAC_RING_TYPE_TX);
451 
452 	/* Clean up notification thread */
453 	if (mip->mi_notify_thread != NULL)
454 		i_mac_notify_exit(mip);
455 
456 	if (mip->mi_info.mi_unicst_addr != NULL) {
457 		kmem_free(mip->mi_info.mi_unicst_addr,
458 		    mip->mi_type->mt_addr_length);
459 		mip->mi_info.mi_unicst_addr = NULL;
460 	}
461 
462 	mac_stat_destroy(mip);
463 
464 	if (mip->mi_type != NULL) {
465 		atomic_dec_32(&mip->mi_type->mt_ref);
466 		mip->mi_type = NULL;
467 	}
468 
469 	if (mip->mi_pdata != NULL) {
470 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
471 		mip->mi_pdata = NULL;
472 		mip->mi_pdata_size = 0;
473 	}
474 
475 	if (minor != 0) {
476 		ASSERT(minor > MAC_MAX_MINOR);
477 		mac_minor_rele(minor);
478 	}
479 
480 	mac_unregister_priv_prop(mip);
481 
482 	kmem_cache_free(i_mac_impl_cachep, mip);
483 	return (err);
484 }
485 
486 /*
487  * Unregister from the GLDv3 framework
488  */
489 int
490 mac_unregister(mac_handle_t mh)
491 {
492 	int			err;
493 	mac_impl_t		*mip = (mac_impl_t *)mh;
494 	mod_hash_val_t		val;
495 	mac_margin_req_t	*mmr, *nextmmr;
496 
497 	/* Fail the unregister if there are any open references to this mac. */
498 	if ((err = mac_disable_nowait(mh)) != 0)
499 		return (err);
500 
501 	/*
502 	 * Clean up notification thread and wait for it to exit.
503 	 */
504 	i_mac_notify_exit(mip);
505 
506 	i_mac_perim_enter(mip);
507 
508 	if (mip->mi_minor < MAC_MAX_MINOR + 1) {
509 		ddi_remove_minor_node(mip->mi_dip, mip->mi_name);
510 		ddi_remove_minor_node(mip->mi_dip,
511 		    (char *)ddi_driver_name(mip->mi_dip));
512 	}
513 
514 	ASSERT(mip->mi_nactiveclients == 0 && !(mip->mi_state_flags &
515 	    MIS_EXCLUSIVE));
516 
517 	mac_stat_destroy(mip);
518 
519 	(void) mod_hash_remove(i_mac_impl_hash,
520 	    (mod_hash_key_t)mip->mi_name, &val);
521 	ASSERT(mip == (mac_impl_t *)val);
522 
523 	ASSERT(i_mac_impl_count > 0);
524 	atomic_dec_32(&i_mac_impl_count);
525 
526 	if (mip->mi_pdata != NULL)
527 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
528 	mip->mi_pdata = NULL;
529 	mip->mi_pdata_size = 0;
530 
531 	/*
532 	 * Free the list of margin request.
533 	 */
534 	for (mmr = mip->mi_mmrp; mmr != NULL; mmr = nextmmr) {
535 		nextmmr = mmr->mmr_nextp;
536 		kmem_free(mmr, sizeof (mac_margin_req_t));
537 	}
538 	mip->mi_mmrp = NULL;
539 
540 	mip->mi_linkstate = LINK_STATE_UNKNOWN;
541 	kmem_free(mip->mi_info.mi_unicst_addr, mip->mi_type->mt_addr_length);
542 	mip->mi_info.mi_unicst_addr = NULL;
543 
544 	atomic_dec_32(&mip->mi_type->mt_ref);
545 	mip->mi_type = NULL;
546 
547 	/*
548 	 * Free the primary MAC address.
549 	 */
550 	mac_fini_macaddr(mip);
551 
552 	/*
553 	 * free all rings
554 	 */
555 	mac_free_rings(mip, MAC_RING_TYPE_RX);
556 	mac_free_rings(mip, MAC_RING_TYPE_TX);
557 
558 	mac_addr_factory_fini(mip);
559 
560 	bzero(mip->mi_addr, MAXMACADDRLEN);
561 	bzero(mip->mi_dstaddr, MAXMACADDRLEN);
562 
563 	/* and the flows */
564 	mac_flow_tab_destroy(mip->mi_flow_tab);
565 	mip->mi_flow_tab = NULL;
566 
567 	if (mip->mi_minor > MAC_MAX_MINOR)
568 		mac_minor_rele(mip->mi_minor);
569 
570 	cmn_err(CE_NOTE, "!%s unregistered", mip->mi_name);
571 
572 	/*
573 	 * Reset the perim related fields to default values before
574 	 * kmem_cache_free
575 	 */
576 	i_mac_perim_exit(mip);
577 	mip->mi_state_flags = 0;
578 
579 	mac_unregister_priv_prop(mip);
580 	kmem_cache_free(i_mac_impl_cachep, mip);
581 
582 	return (0);
583 }
584 
585 /* DATA RECEPTION */
586 
587 /*
588  * This function is invoked for packets received by the MAC driver in
589  * interrupt context. The ring generation number provided by the driver
590  * is matched with the ring generation number held in MAC. If they do not
591  * match, received packets are considered stale packets coming from an older
592  * assignment of the ring. Drop them.
593  */
594 void
595 mac_rx_ring(mac_handle_t mh, mac_ring_handle_t mrh, mblk_t *mp_chain,
596     uint64_t mr_gen_num)
597 {
598 	mac_ring_t		*mr = (mac_ring_t *)mrh;
599 
600 	if ((mr != NULL) && (mr->mr_gen_num != mr_gen_num)) {
601 		DTRACE_PROBE2(mac__rx__rings__stale__packet, uint64_t,
602 		    mr->mr_gen_num, uint64_t, mr_gen_num);
603 		freemsgchain(mp_chain);
604 		return;
605 	}
606 	mac_rx(mh, (mac_resource_handle_t)mrh, mp_chain);
607 }
608 
609 /*
610  * This function is invoked for each packet received by the underlying
611  * driver.
612  */
613 void
614 mac_rx(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain)
615 {
616 	mac_impl_t		*mip = (mac_impl_t *)mh;
617 	mac_ring_t		*mr = (mac_ring_t *)mrh;
618 	mac_soft_ring_set_t 	*mac_srs;
619 	mblk_t			*bp = mp_chain;
620 	boolean_t		hw_classified = B_FALSE;
621 
622 	/*
623 	 * If there are any promiscuous mode callbacks defined for
624 	 * this MAC, pass them a copy if appropriate.
625 	 */
626 	if (mip->mi_promisc_list != NULL)
627 		mac_promisc_dispatch(mip, mp_chain, NULL);
628 
629 	if (mr != NULL) {
630 		/*
631 		 * If the SRS teardown has started, just return. The 'mr'
632 		 * continues to be valid until the driver unregisters the mac.
633 		 * Hardware classified packets will not make their way up
634 		 * beyond this point once the teardown has started. The driver
635 		 * is never passed a pointer to a flow entry or SRS or any
636 		 * structure that can be freed much before mac_unregister.
637 		 */
638 		mutex_enter(&mr->mr_lock);
639 		if ((mr->mr_state != MR_INUSE) || (mr->mr_flag &
640 		    (MR_INCIPIENT | MR_CONDEMNED | MR_QUIESCE))) {
641 			mutex_exit(&mr->mr_lock);
642 			freemsgchain(mp_chain);
643 			return;
644 		}
645 		if (mr->mr_classify_type == MAC_HW_CLASSIFIER) {
646 			hw_classified = B_TRUE;
647 			MR_REFHOLD_LOCKED(mr);
648 		}
649 		mutex_exit(&mr->mr_lock);
650 
651 		/*
652 		 * We check if an SRS is controlling this ring.
653 		 * If so, we can directly call the srs_lower_proc
654 		 * routine otherwise we need to go through mac_rx_classify
655 		 * to reach the right place.
656 		 */
657 		if (hw_classified) {
658 			mac_srs = mr->mr_srs;
659 			/*
660 			 * This is supposed to be the fast path.
661 			 * All packets received though here were steered by
662 			 * the hardware classifier, and share the same
663 			 * MAC header info.
664 			 */
665 			mac_srs->srs_rx.sr_lower_proc(mh,
666 			    (mac_resource_handle_t)mac_srs, mp_chain, B_FALSE);
667 			MR_REFRELE(mr);
668 			return;
669 		}
670 		/* We'll fall through to software classification */
671 	}
672 
673 	if (!FLOW_TAB_EMPTY(mip->mi_flow_tab)) {
674 		if ((bp = mac_rx_flow(mh, mrh, bp)) == NULL)
675 			return;
676 	}
677 
678 	freemsgchain(bp);
679 }
680 
681 /* DATA TRANSMISSION */
682 
683 /*
684  * A driver's notification to resume transmission, in case of a provider
685  * without TX rings.
686  */
687 void
688 mac_tx_update(mac_handle_t mh)
689 {
690 	/*
691 	 * Walk the list of MAC clients (mac_client_handle)
692 	 * and update
693 	 */
694 	i_mac_tx_srs_notify((mac_impl_t *)mh, NULL);
695 }
696 
697 /*
698  * A driver's notification to resume transmission on the specified TX ring.
699  */
700 void
701 mac_tx_ring_update(mac_handle_t mh, mac_ring_handle_t rh)
702 {
703 	i_mac_tx_srs_notify((mac_impl_t *)mh, rh);
704 }
705 
706 /* LINK STATE */
707 /*
708  * Notify the MAC layer about a link state change
709  */
710 void
711 mac_link_update(mac_handle_t mh, link_state_t link)
712 {
713 	mac_impl_t	*mip = (mac_impl_t *)mh;
714 
715 	/*
716 	 * Save the link state.
717 	 */
718 	mip->mi_linkstate = link;
719 
720 	/*
721 	 * Send a MAC_NOTE_LINK notification.
722 	 */
723 	i_mac_notify(mip, MAC_NOTE_LINK);
724 }
725 
726 /* OTHER CONTROL INFORMATION */
727 
728 /*
729  * A driver notified us that its primary MAC address has changed.
730  */
731 void
732 mac_unicst_update(mac_handle_t mh, const uint8_t *addr)
733 {
734 	mac_impl_t	*mip = (mac_impl_t *)mh;
735 
736 	if (mip->mi_type->mt_addr_length == 0)
737 		return;
738 
739 	i_mac_perim_enter(mip);
740 	/*
741 	 * If address doesn't change, do nothing.
742 	 */
743 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) {
744 		i_mac_perim_exit(mip);
745 		return;
746 	}
747 
748 	/*
749 	 * Freshen the MAC address value and update all MAC clients that
750 	 * share this MAC address.
751 	 */
752 	mac_freshen_macaddr(mac_find_macaddr(mip, mip->mi_addr),
753 	    (uint8_t *)addr);
754 
755 	i_mac_perim_exit(mip);
756 
757 	/*
758 	 * Send a MAC_NOTE_UNICST notification.
759 	 */
760 	i_mac_notify(mip, MAC_NOTE_UNICST);
761 }
762 
763 /*
764  * The provider's hw resources (e.g. rings grouping) has changed.
765  * Notify the MAC framework to trigger a re-negotiation of the capabilities.
766  */
767 void
768 mac_resource_update(mac_handle_t mh)
769 {
770 	/*
771 	 * Send a MAC_NOTE_RESOURCE notification.
772 	 */
773 	i_mac_notify((mac_impl_t *)mh, MAC_NOTE_RESOURCE);
774 }
775 
776 /*
777  * MAC plugin information changed.
778  */
779 int
780 mac_pdata_update(mac_handle_t mh, void *mac_pdata, size_t dsize)
781 {
782 	mac_impl_t	*mip = (mac_impl_t *)mh;
783 
784 	/*
785 	 * Verify that the plugin supports MAC plugin data and that the
786 	 * supplied data is valid.
787 	 */
788 	if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY))
789 		return (EINVAL);
790 	if (!mip->mi_type->mt_ops.mtops_pdata_verify(mac_pdata, dsize))
791 		return (EINVAL);
792 
793 	if (mip->mi_pdata != NULL)
794 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
795 
796 	mip->mi_pdata = kmem_alloc(dsize, KM_SLEEP);
797 	bcopy(mac_pdata, mip->mi_pdata, dsize);
798 	mip->mi_pdata_size = dsize;
799 
800 	/*
801 	 * Since the MAC plugin data is used to construct MAC headers that
802 	 * were cached in fast-path headers, we need to flush fast-path
803 	 * information for links associated with this mac.
804 	 */
805 	i_mac_notify(mip, MAC_NOTE_FASTPATH_FLUSH);
806 	return (0);
807 }
808 
809 /*
810  * Invoked by driver as well as the framework to notify its capability change.
811  */
812 void
813 mac_capab_update(mac_handle_t mh)
814 {
815 	/* Send MAC_NOTE_CAPAB_CHG notification */
816 	i_mac_notify((mac_impl_t *)mh, MAC_NOTE_CAPAB_CHG);
817 }
818 
819 int
820 mac_maxsdu_update(mac_handle_t mh, uint_t sdu_max)
821 {
822 	mac_impl_t	*mip = (mac_impl_t *)mh;
823 
824 	if (sdu_max <= mip->mi_sdu_min)
825 		return (EINVAL);
826 	mip->mi_sdu_max = sdu_max;
827 
828 	/* Send a MAC_NOTE_SDU_SIZE notification. */
829 	i_mac_notify(mip, MAC_NOTE_SDU_SIZE);
830 	return (0);
831 }
832 
833 /* PRIVATE FUNCTIONS, FOR INTERNAL USE ONLY */
834 
835 /*
836  * Updates the mac_impl structure with the current state of the link
837  */
838 static void
839 i_mac_log_link_state(mac_impl_t *mip)
840 {
841 	/*
842 	 * If no change, then it is not interesting.
843 	 */
844 	if (mip->mi_lastlinkstate == mip->mi_linkstate)
845 		return;
846 
847 	switch (mip->mi_linkstate) {
848 	case LINK_STATE_UP:
849 		if (mip->mi_type->mt_ops.mtops_ops & MTOPS_LINK_DETAILS) {
850 			char det[200];
851 
852 			mip->mi_type->mt_ops.mtops_link_details(det,
853 			    sizeof (det), (mac_handle_t)mip, mip->mi_pdata);
854 
855 			cmn_err(CE_NOTE, "!%s link up, %s", mip->mi_name, det);
856 		} else {
857 			cmn_err(CE_NOTE, "!%s link up", mip->mi_name);
858 		}
859 		break;
860 
861 	case LINK_STATE_DOWN:
862 		/*
863 		 * Only transitions from UP to DOWN are interesting
864 		 */
865 		if (mip->mi_lastlinkstate != LINK_STATE_UNKNOWN)
866 			cmn_err(CE_NOTE, "!%s link down", mip->mi_name);
867 		break;
868 
869 	case LINK_STATE_UNKNOWN:
870 		/*
871 		 * This case is normally not interesting.
872 		 */
873 		break;
874 	}
875 	mip->mi_lastlinkstate = mip->mi_linkstate;
876 }
877 
878 /*
879  * Main routine for the callbacks notifications thread
880  */
881 static void
882 i_mac_notify_thread(void *arg)
883 {
884 	mac_impl_t	*mip = arg;
885 	callb_cpr_t	cprinfo;
886 	mac_cb_t	*mcb;
887 	mac_cb_info_t	*mcbi;
888 	mac_notify_cb_t	*mncb;
889 
890 	mcbi = &mip->mi_notify_cb_info;
891 	CALLB_CPR_INIT(&cprinfo, mcbi->mcbi_lockp, callb_generic_cpr,
892 	    "i_mac_notify_thread");
893 
894 	mutex_enter(mcbi->mcbi_lockp);
895 
896 	for (;;) {
897 		uint32_t	bits;
898 		uint32_t	type;
899 
900 		bits = mip->mi_notify_bits;
901 		if (bits == 0) {
902 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
903 			cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
904 			CALLB_CPR_SAFE_END(&cprinfo, mcbi->mcbi_lockp);
905 			continue;
906 		}
907 		mip->mi_notify_bits = 0;
908 		if ((bits & (1 << MAC_NNOTE)) != 0) {
909 			/* request to quit */
910 			ASSERT(mip->mi_state_flags & MIS_DISABLED);
911 			break;
912 		}
913 
914 		mutex_exit(mcbi->mcbi_lockp);
915 
916 		/*
917 		 * Log link changes.
918 		 */
919 		if ((bits & (1 << MAC_NOTE_LINK)) != 0)
920 			i_mac_log_link_state(mip);
921 
922 		/*
923 		 * Do notification callbacks for each notification type.
924 		 */
925 		for (type = 0; type < MAC_NNOTE; type++) {
926 			if ((bits & (1 << type)) == 0) {
927 				continue;
928 			}
929 
930 			if (mac_notify_cb_list[type].mac_notify_cb_fn)
931 				mac_notify_cb_list[type].mac_notify_cb_fn(mip);
932 
933 			/*
934 			 * Walk the list of notifications.
935 			 */
936 			MAC_CALLBACK_WALKER_INC(&mip->mi_notify_cb_info);
937 			for (mcb = mip->mi_notify_cb_list; mcb != NULL;
938 			    mcb = mcb->mcb_nextp) {
939 				mncb = (mac_notify_cb_t *)mcb->mcb_objp;
940 				mncb->mncb_fn(mncb->mncb_arg, type);
941 			}
942 			MAC_CALLBACK_WALKER_DCR(&mip->mi_notify_cb_info,
943 			    &mip->mi_notify_cb_list);
944 		}
945 
946 		mutex_enter(mcbi->mcbi_lockp);
947 	}
948 
949 	mip->mi_state_flags |= MIS_NOTIFY_DONE;
950 	cv_broadcast(&mcbi->mcbi_cv);
951 
952 	/* CALLB_CPR_EXIT drops the lock */
953 	CALLB_CPR_EXIT(&cprinfo);
954 	thread_exit();
955 }
956 
957 /*
958  * Signal the i_mac_notify_thread asking it to quit.
959  * Then wait till it is done.
960  */
961 void
962 i_mac_notify_exit(mac_impl_t *mip)
963 {
964 	mac_cb_info_t	*mcbi;
965 
966 	mcbi = &mip->mi_notify_cb_info;
967 
968 	mutex_enter(mcbi->mcbi_lockp);
969 	mip->mi_notify_bits = (1 << MAC_NNOTE);
970 	cv_broadcast(&mcbi->mcbi_cv);
971 
972 
973 	while ((mip->mi_notify_thread != NULL) &&
974 	    !(mip->mi_state_flags & MIS_NOTIFY_DONE)) {
975 		cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
976 	}
977 
978 	/* Necessary clean up before doing kmem_cache_free */
979 	mip->mi_state_flags &= ~MIS_NOTIFY_DONE;
980 	mip->mi_notify_bits = 0;
981 	mip->mi_notify_thread = NULL;
982 	mutex_exit(mcbi->mcbi_lockp);
983 }
984 
985 /*
986  * Entry point invoked by drivers to dynamically add a ring to an
987  * existing group.
988  */
989 int
990 mac_group_add_ring(mac_group_handle_t gh, int index)
991 {
992 	mac_group_t *group = (mac_group_t *)gh;
993 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
994 	int ret;
995 
996 	i_mac_perim_enter(mip);
997 
998 	/*
999 	 * Only RX rings can be added or removed by drivers currently.
1000 	 */
1001 	ASSERT(group->mrg_type == MAC_RING_TYPE_RX);
1002 
1003 	ret = i_mac_group_add_ring(group, NULL, index);
1004 
1005 	i_mac_perim_exit(mip);
1006 
1007 	return (ret);
1008 }
1009 
1010 /*
1011  * Entry point invoked by drivers to dynamically remove a ring
1012  * from an existing group. The specified ring handle must no longer
1013  * be used by the driver after a call to this function.
1014  */
1015 void
1016 mac_group_rem_ring(mac_group_handle_t gh, mac_ring_handle_t rh)
1017 {
1018 	mac_group_t *group = (mac_group_t *)gh;
1019 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
1020 
1021 	i_mac_perim_enter(mip);
1022 
1023 	/*
1024 	 * Only RX rings can be added or removed by drivers currently.
1025 	 */
1026 	ASSERT(group->mrg_type == MAC_RING_TYPE_RX);
1027 
1028 	i_mac_group_rem_ring(group, (mac_ring_t *)rh, B_TRUE);
1029 
1030 	i_mac_perim_exit(mip);
1031 }
1032