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