xref: /illumos-gate/usr/src/uts/common/io/mac/mac_provider.c (revision c211fc479225fa54805cf480633bf6689ca9a2db)
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 2009 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 	} else {
672 		flow_entry_t *flent;
673 		int err;
674 
675 		rw_enter(&mip->mi_rw_lock, RW_READER);
676 		if (mip->mi_single_active_client != NULL) {
677 			flent = mip->mi_single_active_client->mci_flent_list;
678 			FLOW_TRY_REFHOLD(flent, err);
679 			rw_exit(&mip->mi_rw_lock);
680 			if (err == 0) {
681 				(flent->fe_cb_fn)(flent->fe_cb_arg1,
682 				    flent->fe_cb_arg2, mp_chain, B_FALSE);
683 				FLOW_REFRELE(flent);
684 				return;
685 			}
686 		} else {
687 			rw_exit(&mip->mi_rw_lock);
688 		}
689 	}
690 
691 	if (!FLOW_TAB_EMPTY(mip->mi_flow_tab)) {
692 		if ((bp = mac_rx_flow(mh, mrh, bp)) == NULL)
693 			return;
694 	}
695 
696 	freemsgchain(bp);
697 }
698 
699 /* DATA TRANSMISSION */
700 
701 /*
702  * A driver's notification to resume transmission, in case of a provider
703  * without TX rings.
704  */
705 void
706 mac_tx_update(mac_handle_t mh)
707 {
708 	/*
709 	 * Walk the list of MAC clients (mac_client_handle)
710 	 * and update
711 	 */
712 	i_mac_tx_srs_notify((mac_impl_t *)mh, NULL);
713 }
714 
715 /*
716  * A driver's notification to resume transmission on the specified TX ring.
717  */
718 void
719 mac_tx_ring_update(mac_handle_t mh, mac_ring_handle_t rh)
720 {
721 	i_mac_tx_srs_notify((mac_impl_t *)mh, rh);
722 }
723 
724 /* LINK STATE */
725 /*
726  * Notify the MAC layer about a link state change
727  */
728 void
729 mac_link_update(mac_handle_t mh, link_state_t link)
730 {
731 	mac_impl_t	*mip = (mac_impl_t *)mh;
732 
733 	/*
734 	 * Save the link state.
735 	 */
736 	mip->mi_linkstate = link;
737 
738 	/*
739 	 * Send a MAC_NOTE_LINK notification.
740 	 */
741 	i_mac_notify(mip, MAC_NOTE_LINK);
742 }
743 
744 /* OTHER CONTROL INFORMATION */
745 
746 /*
747  * A driver notified us that its primary MAC address has changed.
748  */
749 void
750 mac_unicst_update(mac_handle_t mh, const uint8_t *addr)
751 {
752 	mac_impl_t	*mip = (mac_impl_t *)mh;
753 
754 	if (mip->mi_type->mt_addr_length == 0)
755 		return;
756 
757 	i_mac_perim_enter(mip);
758 	/*
759 	 * If address doesn't change, do nothing.
760 	 */
761 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) {
762 		i_mac_perim_exit(mip);
763 		return;
764 	}
765 
766 	/*
767 	 * Freshen the MAC address value and update all MAC clients that
768 	 * share this MAC address.
769 	 */
770 	mac_freshen_macaddr(mac_find_macaddr(mip, mip->mi_addr),
771 	    (uint8_t *)addr);
772 
773 	i_mac_perim_exit(mip);
774 
775 	/*
776 	 * Send a MAC_NOTE_UNICST notification.
777 	 */
778 	i_mac_notify(mip, MAC_NOTE_UNICST);
779 }
780 
781 /*
782  * The provider's hw resources (e.g. rings grouping) has changed.
783  * Notify the MAC framework to trigger a re-negotiation of the capabilities.
784  */
785 void
786 mac_resource_update(mac_handle_t mh)
787 {
788 	/*
789 	 * Send a MAC_NOTE_RESOURCE notification.
790 	 */
791 	i_mac_notify((mac_impl_t *)mh, MAC_NOTE_RESOURCE);
792 }
793 
794 /*
795  * MAC plugin information changed.
796  */
797 int
798 mac_pdata_update(mac_handle_t mh, void *mac_pdata, size_t dsize)
799 {
800 	mac_impl_t	*mip = (mac_impl_t *)mh;
801 
802 	/*
803 	 * Verify that the plugin supports MAC plugin data and that the
804 	 * supplied data is valid.
805 	 */
806 	if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY))
807 		return (EINVAL);
808 	if (!mip->mi_type->mt_ops.mtops_pdata_verify(mac_pdata, dsize))
809 		return (EINVAL);
810 
811 	if (mip->mi_pdata != NULL)
812 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
813 
814 	mip->mi_pdata = kmem_alloc(dsize, KM_SLEEP);
815 	bcopy(mac_pdata, mip->mi_pdata, dsize);
816 	mip->mi_pdata_size = dsize;
817 
818 	/*
819 	 * Since the MAC plugin data is used to construct MAC headers that
820 	 * were cached in fast-path headers, we need to flush fast-path
821 	 * information for links associated with this mac.
822 	 */
823 	i_mac_notify(mip, MAC_NOTE_FASTPATH_FLUSH);
824 	return (0);
825 }
826 
827 /*
828  * Invoked by driver as well as the framework to notify its capability change.
829  */
830 void
831 mac_capab_update(mac_handle_t mh)
832 {
833 	/* Send MAC_NOTE_CAPAB_CHG notification */
834 	i_mac_notify((mac_impl_t *)mh, MAC_NOTE_CAPAB_CHG);
835 }
836 
837 int
838 mac_maxsdu_update(mac_handle_t mh, uint_t sdu_max)
839 {
840 	mac_impl_t	*mip = (mac_impl_t *)mh;
841 
842 	if (sdu_max <= mip->mi_sdu_min)
843 		return (EINVAL);
844 	mip->mi_sdu_max = sdu_max;
845 
846 	/* Send a MAC_NOTE_SDU_SIZE notification. */
847 	i_mac_notify(mip, MAC_NOTE_SDU_SIZE);
848 	return (0);
849 }
850 
851 /* PRIVATE FUNCTIONS, FOR INTERNAL USE ONLY */
852 
853 /*
854  * Updates the mac_impl structure with the current state of the link
855  */
856 static void
857 i_mac_log_link_state(mac_impl_t *mip)
858 {
859 	/*
860 	 * If no change, then it is not interesting.
861 	 */
862 	if (mip->mi_lastlinkstate == mip->mi_linkstate)
863 		return;
864 
865 	switch (mip->mi_linkstate) {
866 	case LINK_STATE_UP:
867 		if (mip->mi_type->mt_ops.mtops_ops & MTOPS_LINK_DETAILS) {
868 			char det[200];
869 
870 			mip->mi_type->mt_ops.mtops_link_details(det,
871 			    sizeof (det), (mac_handle_t)mip, mip->mi_pdata);
872 
873 			cmn_err(CE_NOTE, "!%s link up, %s", mip->mi_name, det);
874 		} else {
875 			cmn_err(CE_NOTE, "!%s link up", mip->mi_name);
876 		}
877 		break;
878 
879 	case LINK_STATE_DOWN:
880 		/*
881 		 * Only transitions from UP to DOWN are interesting
882 		 */
883 		if (mip->mi_lastlinkstate != LINK_STATE_UNKNOWN)
884 			cmn_err(CE_NOTE, "!%s link down", mip->mi_name);
885 		break;
886 
887 	case LINK_STATE_UNKNOWN:
888 		/*
889 		 * This case is normally not interesting.
890 		 */
891 		break;
892 	}
893 	mip->mi_lastlinkstate = mip->mi_linkstate;
894 }
895 
896 /*
897  * Main routine for the callbacks notifications thread
898  */
899 static void
900 i_mac_notify_thread(void *arg)
901 {
902 	mac_impl_t	*mip = arg;
903 	callb_cpr_t	cprinfo;
904 	mac_cb_t	*mcb;
905 	mac_cb_info_t	*mcbi;
906 	mac_notify_cb_t	*mncb;
907 
908 	mcbi = &mip->mi_notify_cb_info;
909 	CALLB_CPR_INIT(&cprinfo, mcbi->mcbi_lockp, callb_generic_cpr,
910 	    "i_mac_notify_thread");
911 
912 	mutex_enter(mcbi->mcbi_lockp);
913 
914 	for (;;) {
915 		uint32_t	bits;
916 		uint32_t	type;
917 
918 		bits = mip->mi_notify_bits;
919 		if (bits == 0) {
920 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
921 			cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
922 			CALLB_CPR_SAFE_END(&cprinfo, mcbi->mcbi_lockp);
923 			continue;
924 		}
925 		mip->mi_notify_bits = 0;
926 		if ((bits & (1 << MAC_NNOTE)) != 0) {
927 			/* request to quit */
928 			ASSERT(mip->mi_state_flags & MIS_DISABLED);
929 			break;
930 		}
931 
932 		mutex_exit(mcbi->mcbi_lockp);
933 
934 		/*
935 		 * Log link changes.
936 		 */
937 		if ((bits & (1 << MAC_NOTE_LINK)) != 0)
938 			i_mac_log_link_state(mip);
939 
940 		/*
941 		 * Do notification callbacks for each notification type.
942 		 */
943 		for (type = 0; type < MAC_NNOTE; type++) {
944 			if ((bits & (1 << type)) == 0) {
945 				continue;
946 			}
947 
948 			if (mac_notify_cb_list[type].mac_notify_cb_fn)
949 				mac_notify_cb_list[type].mac_notify_cb_fn(mip);
950 
951 			/*
952 			 * Walk the list of notifications.
953 			 */
954 			MAC_CALLBACK_WALKER_INC(&mip->mi_notify_cb_info);
955 			for (mcb = mip->mi_notify_cb_list; mcb != NULL;
956 			    mcb = mcb->mcb_nextp) {
957 				mncb = (mac_notify_cb_t *)mcb->mcb_objp;
958 				mncb->mncb_fn(mncb->mncb_arg, type);
959 			}
960 			MAC_CALLBACK_WALKER_DCR(&mip->mi_notify_cb_info,
961 			    &mip->mi_notify_cb_list);
962 		}
963 
964 		mutex_enter(mcbi->mcbi_lockp);
965 	}
966 
967 	mip->mi_state_flags |= MIS_NOTIFY_DONE;
968 	cv_broadcast(&mcbi->mcbi_cv);
969 
970 	/* CALLB_CPR_EXIT drops the lock */
971 	CALLB_CPR_EXIT(&cprinfo);
972 	thread_exit();
973 }
974 
975 /*
976  * Signal the i_mac_notify_thread asking it to quit.
977  * Then wait till it is done.
978  */
979 void
980 i_mac_notify_exit(mac_impl_t *mip)
981 {
982 	mac_cb_info_t	*mcbi;
983 
984 	mcbi = &mip->mi_notify_cb_info;
985 
986 	mutex_enter(mcbi->mcbi_lockp);
987 	mip->mi_notify_bits = (1 << MAC_NNOTE);
988 	cv_broadcast(&mcbi->mcbi_cv);
989 
990 
991 	while ((mip->mi_notify_thread != NULL) &&
992 	    !(mip->mi_state_flags & MIS_NOTIFY_DONE)) {
993 		cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
994 	}
995 
996 	/* Necessary clean up before doing kmem_cache_free */
997 	mip->mi_state_flags &= ~MIS_NOTIFY_DONE;
998 	mip->mi_notify_bits = 0;
999 	mip->mi_notify_thread = NULL;
1000 	mutex_exit(mcbi->mcbi_lockp);
1001 }
1002 
1003 /*
1004  * Entry point invoked by drivers to dynamically add a ring to an
1005  * existing group.
1006  */
1007 int
1008 mac_group_add_ring(mac_group_handle_t gh, int index)
1009 {
1010 	mac_group_t *group = (mac_group_t *)gh;
1011 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
1012 	int ret;
1013 
1014 	i_mac_perim_enter(mip);
1015 
1016 	/*
1017 	 * Only RX rings can be added or removed by drivers currently.
1018 	 */
1019 	ASSERT(group->mrg_type == MAC_RING_TYPE_RX);
1020 
1021 	ret = i_mac_group_add_ring(group, NULL, index);
1022 
1023 	i_mac_perim_exit(mip);
1024 
1025 	return (ret);
1026 }
1027 
1028 /*
1029  * Entry point invoked by drivers to dynamically remove a ring
1030  * from an existing group. The specified ring handle must no longer
1031  * be used by the driver after a call to this function.
1032  */
1033 void
1034 mac_group_rem_ring(mac_group_handle_t gh, mac_ring_handle_t rh)
1035 {
1036 	mac_group_t *group = (mac_group_t *)gh;
1037 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
1038 
1039 	i_mac_perim_enter(mip);
1040 
1041 	/*
1042 	 * Only RX rings can be added or removed by drivers currently.
1043 	 */
1044 	ASSERT(group->mrg_type == MAC_RING_TYPE_RX);
1045 
1046 	i_mac_group_rem_ring(group, (mac_ring_t *)rh, B_TRUE);
1047 
1048 	i_mac_perim_exit(mip);
1049 }
1050