xref: /illumos-gate/usr/src/uts/common/io/mac/mac_provider.c (revision d77e6e0f12d19668c0e9068c0fcd7a2123da5373)
1da14cebeSEric Cheng /*
2da14cebeSEric Cheng  * CDDL HEADER START
3da14cebeSEric Cheng  *
4da14cebeSEric Cheng  * The contents of this file are subject to the terms of the
5da14cebeSEric Cheng  * Common Development and Distribution License (the "License").
6da14cebeSEric Cheng  * You may not use this file except in compliance with the License.
7da14cebeSEric Cheng  *
8da14cebeSEric Cheng  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da14cebeSEric Cheng  * or http://www.opensolaris.org/os/licensing.
10da14cebeSEric Cheng  * See the License for the specific language governing permissions
11da14cebeSEric Cheng  * and limitations under the License.
12da14cebeSEric Cheng  *
13da14cebeSEric Cheng  * When distributing Covered Code, include this CDDL HEADER in each
14da14cebeSEric Cheng  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da14cebeSEric Cheng  * If applicable, add the following below this CDDL HEADER, with the
16da14cebeSEric Cheng  * fields enclosed by brackets "[]" replaced with your own identifying
17da14cebeSEric Cheng  * information: Portions Copyright [yyyy] [name of copyright owner]
18da14cebeSEric Cheng  *
19da14cebeSEric Cheng  * CDDL HEADER END
20da14cebeSEric Cheng  */
21da14cebeSEric Cheng 
22da14cebeSEric Cheng /*
230591ddd0SPrakash Jalan  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24c61a1653SRyan Zezeski  * Copyright 2019 Joyent, Inc.
258241ccbbSDan McDonald  * Copyright 2017 OmniTI Computer Consulting, Inc. All rights reserved.
26*d77e6e0fSPaul Winder  * Copyright 2020 RackTop Systems, Inc.
27da14cebeSEric Cheng  */
28da14cebeSEric Cheng 
29da14cebeSEric Cheng #include <sys/types.h>
30da14cebeSEric Cheng #include <sys/conf.h>
31da14cebeSEric Cheng #include <sys/id_space.h>
32da14cebeSEric Cheng #include <sys/esunddi.h>
33da14cebeSEric Cheng #include <sys/stat.h>
34da14cebeSEric Cheng #include <sys/mkdev.h>
35da14cebeSEric Cheng #include <sys/stream.h>
36da14cebeSEric Cheng #include <sys/strsubr.h>
37da14cebeSEric Cheng #include <sys/dlpi.h>
38da14cebeSEric Cheng #include <sys/modhash.h>
39da14cebeSEric Cheng #include <sys/mac.h>
40da14cebeSEric Cheng #include <sys/mac_provider.h>
41da14cebeSEric Cheng #include <sys/mac_impl.h>
42da14cebeSEric Cheng #include <sys/mac_client_impl.h>
43da14cebeSEric Cheng #include <sys/mac_client_priv.h>
44da14cebeSEric Cheng #include <sys/mac_soft_ring.h>
450dc2366fSVenugopal Iyer #include <sys/mac_stat.h>
4661af1958SGarrett D'Amore #include <sys/dld.h>
47da14cebeSEric Cheng #include <sys/modctl.h>
48da14cebeSEric Cheng #include <sys/fs/dv_node.h>
49da14cebeSEric Cheng #include <sys/thread.h>
50da14cebeSEric Cheng #include <sys/proc.h>
51da14cebeSEric Cheng #include <sys/callb.h>
52da14cebeSEric Cheng #include <sys/cpuvar.h>
53da14cebeSEric Cheng #include <sys/atomic.h>
54da14cebeSEric Cheng #include <sys/sdt.h>
55da14cebeSEric Cheng #include <sys/mac_flow.h>
56da14cebeSEric Cheng #include <sys/ddi_intr_impl.h>
57da14cebeSEric Cheng #include <sys/disp.h>
58da14cebeSEric Cheng #include <sys/sdt.h>
590dc2366fSVenugopal Iyer #include <sys/pattr.h>
600dc2366fSVenugopal Iyer #include <sys/strsun.h>
6184de666eSRyan Zezeski #include <sys/vlan.h>
62d240edafSRobert Mustacchi #include <inet/ip.h>
63d240edafSRobert Mustacchi #include <inet/tcp.h>
64d240edafSRobert Mustacchi #include <netinet/udp.h>
65d240edafSRobert Mustacchi #include <netinet/sctp.h>
66da14cebeSEric Cheng 
67da14cebeSEric Cheng /*
68da14cebeSEric Cheng  * MAC Provider Interface.
69da14cebeSEric Cheng  *
70da14cebeSEric Cheng  * Interface for GLDv3 compatible NIC drivers.
71da14cebeSEric Cheng  */
72da14cebeSEric Cheng 
73da14cebeSEric Cheng static void i_mac_notify_thread(void *);
74da14cebeSEric Cheng 
75da14cebeSEric Cheng typedef void (*mac_notify_default_cb_fn_t)(mac_impl_t *);
76da14cebeSEric Cheng 
774eaa4710SRishi Srivatsavai static const mac_notify_default_cb_fn_t mac_notify_cb_list[MAC_NNOTE] = {
784eaa4710SRishi Srivatsavai 	mac_fanout_recompute,	/* MAC_NOTE_LINK */
794eaa4710SRishi Srivatsavai 	NULL,		/* MAC_NOTE_UNICST */
804eaa4710SRishi Srivatsavai 	NULL,		/* MAC_NOTE_TX */
814eaa4710SRishi Srivatsavai 	NULL,		/* MAC_NOTE_DEVPROMISC */
824eaa4710SRishi Srivatsavai 	NULL,		/* MAC_NOTE_FASTPATH_FLUSH */
834eaa4710SRishi Srivatsavai 	NULL,		/* MAC_NOTE_SDU_SIZE */
844eaa4710SRishi Srivatsavai 	NULL,		/* MAC_NOTE_MARGIN */
854eaa4710SRishi Srivatsavai 	NULL,		/* MAC_NOTE_CAPAB_CHG */
864eaa4710SRishi Srivatsavai 	NULL		/* MAC_NOTE_LOWLINK */
87da14cebeSEric Cheng };
88da14cebeSEric Cheng 
89da14cebeSEric Cheng /*
90da14cebeSEric Cheng  * Driver support functions.
91da14cebeSEric Cheng  */
92da14cebeSEric Cheng 
93da14cebeSEric Cheng /* REGISTRATION */
94da14cebeSEric Cheng 
95da14cebeSEric Cheng mac_register_t *
96da14cebeSEric Cheng mac_alloc(uint_t mac_version)
97da14cebeSEric Cheng {
98da14cebeSEric Cheng 	mac_register_t *mregp;
99da14cebeSEric Cheng 
100da14cebeSEric Cheng 	/*
101da14cebeSEric Cheng 	 * Make sure there isn't a version mismatch between the driver and
102da14cebeSEric Cheng 	 * the framework.  In the future, if multiple versions are
103da14cebeSEric Cheng 	 * supported, this check could become more sophisticated.
104da14cebeSEric Cheng 	 */
105da14cebeSEric Cheng 	if (mac_version != MAC_VERSION)
106da14cebeSEric Cheng 		return (NULL);
107da14cebeSEric Cheng 
108da14cebeSEric Cheng 	mregp = kmem_zalloc(sizeof (mac_register_t), KM_SLEEP);
109da14cebeSEric Cheng 	mregp->m_version = mac_version;
110da14cebeSEric Cheng 	return (mregp);
111da14cebeSEric Cheng }
112da14cebeSEric Cheng 
113da14cebeSEric Cheng void
114da14cebeSEric Cheng mac_free(mac_register_t *mregp)
115da14cebeSEric Cheng {
116da14cebeSEric Cheng 	kmem_free(mregp, sizeof (mac_register_t));
117da14cebeSEric Cheng }
118da14cebeSEric Cheng 
119c61a1653SRyan Zezeski /*
120c61a1653SRyan Zezeski  * Convert a MAC's offload features into the equivalent DB_CKSUMFLAGS
121c61a1653SRyan Zezeski  * value.
122c61a1653SRyan Zezeski  */
123c61a1653SRyan Zezeski static uint16_t
124c61a1653SRyan Zezeski mac_features_to_flags(mac_handle_t mh)
125c61a1653SRyan Zezeski {
126c61a1653SRyan Zezeski 	uint16_t flags = 0;
127c61a1653SRyan Zezeski 	uint32_t cap_sum = 0;
128c61a1653SRyan Zezeski 	mac_capab_lso_t cap_lso;
129c61a1653SRyan Zezeski 
130c61a1653SRyan Zezeski 	if (mac_capab_get(mh, MAC_CAPAB_HCKSUM, &cap_sum)) {
131c61a1653SRyan Zezeski 		if (cap_sum & HCKSUM_IPHDRCKSUM)
132c61a1653SRyan Zezeski 			flags |= HCK_IPV4_HDRCKSUM;
133c61a1653SRyan Zezeski 
134c61a1653SRyan Zezeski 		if (cap_sum & HCKSUM_INET_PARTIAL)
135c61a1653SRyan Zezeski 			flags |= HCK_PARTIALCKSUM;
136c61a1653SRyan Zezeski 		else if (cap_sum & (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6))
137c61a1653SRyan Zezeski 			flags |= HCK_FULLCKSUM;
138c61a1653SRyan Zezeski 	}
139c61a1653SRyan Zezeski 
140c61a1653SRyan Zezeski 	/*
141c61a1653SRyan Zezeski 	 * We don't need the information stored in 'cap_lso', but we
142c61a1653SRyan Zezeski 	 * need to pass a non-NULL pointer to appease the driver.
143c61a1653SRyan Zezeski 	 */
144c61a1653SRyan Zezeski 	if (mac_capab_get(mh, MAC_CAPAB_LSO, &cap_lso))
145c61a1653SRyan Zezeski 		flags |= HW_LSO;
146c61a1653SRyan Zezeski 
147c61a1653SRyan Zezeski 	return (flags);
148c61a1653SRyan Zezeski }
149c61a1653SRyan Zezeski 
150da14cebeSEric Cheng /*
151da14cebeSEric Cheng  * mac_register() is how drivers register new MACs with the GLDv3
152da14cebeSEric Cheng  * framework.  The mregp argument is allocated by drivers using the
153da14cebeSEric Cheng  * mac_alloc() function, and can be freed using mac_free() immediately upon
154da14cebeSEric Cheng  * return from mac_register().  Upon success (0 return value), the mhp
155da14cebeSEric Cheng  * opaque pointer becomes the driver's handle to its MAC interface, and is
156da14cebeSEric Cheng  * the argument to all other mac module entry points.
157da14cebeSEric Cheng  */
158da14cebeSEric Cheng /* ARGSUSED */
159da14cebeSEric Cheng int
160da14cebeSEric Cheng mac_register(mac_register_t *mregp, mac_handle_t *mhp)
161da14cebeSEric Cheng {
162da14cebeSEric Cheng 	mac_impl_t		*mip;
163da14cebeSEric Cheng 	mactype_t		*mtype;
164da14cebeSEric Cheng 	int			err = EINVAL;
165da14cebeSEric Cheng 	struct devnames		*dnp = NULL;
166da14cebeSEric Cheng 	uint_t			instance;
167da14cebeSEric Cheng 	boolean_t		style1_created = B_FALSE;
168da14cebeSEric Cheng 	boolean_t		style2_created = B_FALSE;
169da14cebeSEric Cheng 	char			*driver;
170da14cebeSEric Cheng 	minor_t			minor = 0;
171da14cebeSEric Cheng 
172ee94b1c3SSebastien Roy 	/* A successful call to mac_init_ops() sets the DN_GLDV3_DRIVER flag. */
173ee94b1c3SSebastien Roy 	if (!GLDV3_DRV(ddi_driver_major(mregp->m_dip)))
174ee94b1c3SSebastien Roy 		return (EINVAL);
175ee94b1c3SSebastien Roy 
176da14cebeSEric Cheng 	/* Find the required MAC-Type plugin. */
177da14cebeSEric Cheng 	if ((mtype = mactype_getplugin(mregp->m_type_ident)) == NULL)
178da14cebeSEric Cheng 		return (EINVAL);
179da14cebeSEric Cheng 
180da14cebeSEric Cheng 	/* Create a mac_impl_t to represent this MAC. */
181da14cebeSEric Cheng 	mip = kmem_cache_alloc(i_mac_impl_cachep, KM_SLEEP);
182da14cebeSEric Cheng 
183da14cebeSEric Cheng 	/*
184da14cebeSEric Cheng 	 * The mac is not ready for open yet.
185da14cebeSEric Cheng 	 */
186da14cebeSEric Cheng 	mip->mi_state_flags |= MIS_DISABLED;
187da14cebeSEric Cheng 
188da14cebeSEric Cheng 	/*
189da14cebeSEric Cheng 	 * When a mac is registered, the m_instance field can be set to:
190da14cebeSEric Cheng 	 *
191da14cebeSEric Cheng 	 *  0:	Get the mac's instance number from m_dip.
192da14cebeSEric Cheng 	 *	This is usually used for physical device dips.
193da14cebeSEric Cheng 	 *
194da14cebeSEric Cheng 	 *  [1 .. MAC_MAX_MINOR-1]: Use the value as the mac's instance number.
195da14cebeSEric Cheng 	 *	For example, when an aggregation is created with the key option,
196da14cebeSEric Cheng 	 *	"key" will be used as the instance number.
197da14cebeSEric Cheng 	 *
198da14cebeSEric Cheng 	 *  -1: Assign an instance number from [MAC_MAX_MINOR .. MAXMIN-1].
199da14cebeSEric Cheng 	 *	This is often used when a MAC of a virtual link is registered
200da14cebeSEric Cheng 	 *	(e.g., aggregation when "key" is not specified, or vnic).
201da14cebeSEric Cheng 	 *
202da14cebeSEric Cheng 	 * Note that the instance number is used to derive the mi_minor field
203da14cebeSEric Cheng 	 * of mac_impl_t, which will then be used to derive the name of kstats
204da14cebeSEric Cheng 	 * and the devfs nodes.  The first 2 cases are needed to preserve
205da14cebeSEric Cheng 	 * backward compatibility.
206da14cebeSEric Cheng 	 */
207da14cebeSEric Cheng 	switch (mregp->m_instance) {
208da14cebeSEric Cheng 	case 0:
209da14cebeSEric Cheng 		instance = ddi_get_instance(mregp->m_dip);
210da14cebeSEric Cheng 		break;
211da14cebeSEric Cheng 	case ((uint_t)-1):
212da14cebeSEric Cheng 		minor = mac_minor_hold(B_TRUE);
213da14cebeSEric Cheng 		if (minor == 0) {
214da14cebeSEric Cheng 			err = ENOSPC;
215da14cebeSEric Cheng 			goto fail;
216da14cebeSEric Cheng 		}
217da14cebeSEric Cheng 		instance = minor - 1;
218da14cebeSEric Cheng 		break;
219da14cebeSEric Cheng 	default:
220da14cebeSEric Cheng 		instance = mregp->m_instance;
221da14cebeSEric Cheng 		if (instance >= MAC_MAX_MINOR) {
222da14cebeSEric Cheng 			err = EINVAL;
223da14cebeSEric Cheng 			goto fail;
224da14cebeSEric Cheng 		}
225da14cebeSEric Cheng 		break;
226da14cebeSEric Cheng 	}
227da14cebeSEric Cheng 
228da14cebeSEric Cheng 	mip->mi_minor = (minor_t)(instance + 1);
229da14cebeSEric Cheng 	mip->mi_dip = mregp->m_dip;
230da14cebeSEric Cheng 	mip->mi_clients_list = NULL;
231da14cebeSEric Cheng 	mip->mi_nclients = 0;
232da14cebeSEric Cheng 
2334eaa4710SRishi Srivatsavai 	/* Set the default IEEE Port VLAN Identifier */
2344eaa4710SRishi Srivatsavai 	mip->mi_pvid = 1;
2354eaa4710SRishi Srivatsavai 
2364eaa4710SRishi Srivatsavai 	/* Default bridge link learning protection values */
2374eaa4710SRishi Srivatsavai 	mip->mi_llimit = 1000;
2384eaa4710SRishi Srivatsavai 	mip->mi_ldecay = 200;
2394eaa4710SRishi Srivatsavai 
240da14cebeSEric Cheng 	driver = (char *)ddi_driver_name(mip->mi_dip);
241da14cebeSEric Cheng 
242da14cebeSEric Cheng 	/* Construct the MAC name as <drvname><instance> */
243da14cebeSEric Cheng 	(void) snprintf(mip->mi_name, sizeof (mip->mi_name), "%s%d",
244da14cebeSEric Cheng 	    driver, instance);
245da14cebeSEric Cheng 
246da14cebeSEric Cheng 	mip->mi_driver = mregp->m_driver;
247da14cebeSEric Cheng 
248da14cebeSEric Cheng 	mip->mi_type = mtype;
249da14cebeSEric Cheng 	mip->mi_margin = mregp->m_margin;
250da14cebeSEric Cheng 	mip->mi_info.mi_media = mtype->mt_type;
251da14cebeSEric Cheng 	mip->mi_info.mi_nativemedia = mtype->mt_nativetype;
252da14cebeSEric Cheng 	if (mregp->m_max_sdu <= mregp->m_min_sdu)
253da14cebeSEric Cheng 		goto fail;
2541eee170aSErik Nordmark 	if (mregp->m_multicast_sdu == 0)
2551eee170aSErik Nordmark 		mregp->m_multicast_sdu = mregp->m_max_sdu;
2561eee170aSErik Nordmark 	if (mregp->m_multicast_sdu < mregp->m_min_sdu ||
2571eee170aSErik Nordmark 	    mregp->m_multicast_sdu > mregp->m_max_sdu)
2581eee170aSErik Nordmark 		goto fail;
259da14cebeSEric Cheng 	mip->mi_sdu_min = mregp->m_min_sdu;
260da14cebeSEric Cheng 	mip->mi_sdu_max = mregp->m_max_sdu;
2611eee170aSErik Nordmark 	mip->mi_sdu_multicast = mregp->m_multicast_sdu;
262da14cebeSEric Cheng 	mip->mi_info.mi_addr_length = mip->mi_type->mt_addr_length;
263da14cebeSEric Cheng 	/*
264da14cebeSEric Cheng 	 * If the media supports a broadcast address, cache a pointer to it
265da14cebeSEric Cheng 	 * in the mac_info_t so that upper layers can use it.
266da14cebeSEric Cheng 	 */
267da14cebeSEric Cheng 	mip->mi_info.mi_brdcst_addr = mip->mi_type->mt_brdcst_addr;
268da14cebeSEric Cheng 
269da14cebeSEric Cheng 	mip->mi_v12n_level = mregp->m_v12n;
270da14cebeSEric Cheng 
271da14cebeSEric Cheng 	/*
272da14cebeSEric Cheng 	 * Copy the unicast source address into the mac_info_t, but only if
273da14cebeSEric Cheng 	 * the MAC-Type defines a non-zero address length.  We need to
274da14cebeSEric Cheng 	 * handle MAC-Types that have an address length of 0
275da14cebeSEric Cheng 	 * (point-to-point protocol MACs for example).
276da14cebeSEric Cheng 	 */
277da14cebeSEric Cheng 	if (mip->mi_type->mt_addr_length > 0) {
278da14cebeSEric Cheng 		if (mregp->m_src_addr == NULL)
279da14cebeSEric Cheng 			goto fail;
280da14cebeSEric Cheng 		mip->mi_info.mi_unicst_addr =
281da14cebeSEric Cheng 		    kmem_alloc(mip->mi_type->mt_addr_length, KM_SLEEP);
282da14cebeSEric Cheng 		bcopy(mregp->m_src_addr, mip->mi_info.mi_unicst_addr,
283da14cebeSEric Cheng 		    mip->mi_type->mt_addr_length);
284da14cebeSEric Cheng 
285da14cebeSEric Cheng 		/*
286da14cebeSEric Cheng 		 * Copy the fixed 'factory' MAC address from the immutable
287da14cebeSEric Cheng 		 * info.  This is taken to be the MAC address currently in
288da14cebeSEric Cheng 		 * use.
289da14cebeSEric Cheng 		 */
290da14cebeSEric Cheng 		bcopy(mip->mi_info.mi_unicst_addr, mip->mi_addr,
291da14cebeSEric Cheng 		    mip->mi_type->mt_addr_length);
292da14cebeSEric Cheng 
293da14cebeSEric Cheng 		/*
294da14cebeSEric Cheng 		 * At this point, we should set up the classification
295da14cebeSEric Cheng 		 * rules etc but we delay it till mac_open() so that
296da14cebeSEric Cheng 		 * the resource discovery has taken place and we
297da14cebeSEric Cheng 		 * know someone wants to use the device. Otherwise
298da14cebeSEric Cheng 		 * memory gets allocated for Rx ring structures even
299da14cebeSEric Cheng 		 * during probe.
300da14cebeSEric Cheng 		 */
301da14cebeSEric Cheng 
302da14cebeSEric Cheng 		/* Copy the destination address if one is provided. */
303da14cebeSEric Cheng 		if (mregp->m_dst_addr != NULL) {
304da14cebeSEric Cheng 			bcopy(mregp->m_dst_addr, mip->mi_dstaddr,
305da14cebeSEric Cheng 			    mip->mi_type->mt_addr_length);
3062b24ab6bSSebastien Roy 			mip->mi_dstaddr_set = B_TRUE;
307da14cebeSEric Cheng 		}
308da14cebeSEric Cheng 	} else if (mregp->m_src_addr != NULL) {
309da14cebeSEric Cheng 		goto fail;
310da14cebeSEric Cheng 	}
311da14cebeSEric Cheng 
312da14cebeSEric Cheng 	/*
313da14cebeSEric Cheng 	 * The format of the m_pdata is specific to the plugin.  It is
314da14cebeSEric Cheng 	 * passed in as an argument to all of the plugin callbacks.  The
315da14cebeSEric Cheng 	 * driver can update this information by calling
316da14cebeSEric Cheng 	 * mac_pdata_update().
317da14cebeSEric Cheng 	 */
3182b24ab6bSSebastien Roy 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY) {
319da14cebeSEric Cheng 		/*
3202b24ab6bSSebastien Roy 		 * Verify if the supplied plugin data is valid.  Note that
3212b24ab6bSSebastien Roy 		 * even if the caller passed in a NULL pointer as plugin data,
3222b24ab6bSSebastien Roy 		 * we still need to verify if that's valid as the plugin may
3232b24ab6bSSebastien Roy 		 * require plugin data to function.
324da14cebeSEric Cheng 		 */
325da14cebeSEric Cheng 		if (!mip->mi_type->mt_ops.mtops_pdata_verify(mregp->m_pdata,
326da14cebeSEric Cheng 		    mregp->m_pdata_size)) {
327da14cebeSEric Cheng 			goto fail;
328da14cebeSEric Cheng 		}
3292b24ab6bSSebastien Roy 		if (mregp->m_pdata != NULL) {
3302b24ab6bSSebastien Roy 			mip->mi_pdata =
3312b24ab6bSSebastien Roy 			    kmem_alloc(mregp->m_pdata_size, KM_SLEEP);
3322b24ab6bSSebastien Roy 			bcopy(mregp->m_pdata, mip->mi_pdata,
3332b24ab6bSSebastien Roy 			    mregp->m_pdata_size);
3342b24ab6bSSebastien Roy 			mip->mi_pdata_size = mregp->m_pdata_size;
3352b24ab6bSSebastien Roy 		}
3362b24ab6bSSebastien Roy 	} else if (mregp->m_pdata != NULL) {
3372b24ab6bSSebastien Roy 		/*
3382b24ab6bSSebastien Roy 		 * The caller supplied non-NULL plugin data, but the plugin
3392b24ab6bSSebastien Roy 		 * does not recognize plugin data.
3402b24ab6bSSebastien Roy 		 */
3412b24ab6bSSebastien Roy 		err = EINVAL;
3422b24ab6bSSebastien Roy 		goto fail;
343da14cebeSEric Cheng 	}
344da14cebeSEric Cheng 
345da14cebeSEric Cheng 	/*
346da14cebeSEric Cheng 	 * Register the private properties.
347da14cebeSEric Cheng 	 */
3480dc2366fSVenugopal Iyer 	mac_register_priv_prop(mip, mregp->m_priv_props);
349da14cebeSEric Cheng 
350da14cebeSEric Cheng 	/*
351da14cebeSEric Cheng 	 * Stash the driver callbacks into the mac_impl_t, but first sanity
352da14cebeSEric Cheng 	 * check to make sure all mandatory callbacks are set.
353da14cebeSEric Cheng 	 */
354da14cebeSEric Cheng 	if (mregp->m_callbacks->mc_getstat == NULL ||
355da14cebeSEric Cheng 	    mregp->m_callbacks->mc_start == NULL ||
356da14cebeSEric Cheng 	    mregp->m_callbacks->mc_stop == NULL ||
357da14cebeSEric Cheng 	    mregp->m_callbacks->mc_setpromisc == NULL ||
358da14cebeSEric Cheng 	    mregp->m_callbacks->mc_multicst == NULL) {
359da14cebeSEric Cheng 		goto fail;
360da14cebeSEric Cheng 	}
361da14cebeSEric Cheng 	mip->mi_callbacks = mregp->m_callbacks;
362da14cebeSEric Cheng 
3635d460eafSCathy Zhou 	if (mac_capab_get((mac_handle_t)mip, MAC_CAPAB_LEGACY,
3645d460eafSCathy Zhou 	    &mip->mi_capab_legacy)) {
365da14cebeSEric Cheng 		mip->mi_state_flags |= MIS_LEGACY;
3665d460eafSCathy Zhou 		mip->mi_phy_dev = mip->mi_capab_legacy.ml_dev;
367da14cebeSEric Cheng 	} else {
368da14cebeSEric Cheng 		mip->mi_phy_dev = makedevice(ddi_driver_major(mip->mi_dip),
36961af1958SGarrett D'Amore 		    mip->mi_minor);
370da14cebeSEric Cheng 	}
371da14cebeSEric Cheng 
372da14cebeSEric Cheng 	/*
373da14cebeSEric Cheng 	 * Allocate a notification thread. thread_create blocks for memory
374da14cebeSEric Cheng 	 * if needed, it never fails.
375da14cebeSEric Cheng 	 */
376da14cebeSEric Cheng 	mip->mi_notify_thread = thread_create(NULL, 0, i_mac_notify_thread,
377da14cebeSEric Cheng 	    mip, 0, &p0, TS_RUN, minclsyspri);
378da14cebeSEric Cheng 
379da14cebeSEric Cheng 	/*
380c61a1653SRyan Zezeski 	 * Cache the DB_CKSUMFLAGS that this MAC supports.
381da14cebeSEric Cheng 	 */
382c61a1653SRyan Zezeski 	mip->mi_tx_cksum_flags = mac_features_to_flags((mac_handle_t)mip);
383da14cebeSEric Cheng 
384c61a1653SRyan Zezeski 	/*
385c61a1653SRyan Zezeski 	 * Initialize the capabilities
386c61a1653SRyan Zezeski 	 */
3870dc2366fSVenugopal Iyer 	bzero(&mip->mi_rx_rings_cap, sizeof (mac_capab_rings_t));
3880dc2366fSVenugopal Iyer 	bzero(&mip->mi_tx_rings_cap, sizeof (mac_capab_rings_t));
3890dc2366fSVenugopal Iyer 
390da14cebeSEric Cheng 	if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, NULL))
391da14cebeSEric Cheng 		mip->mi_state_flags |= MIS_IS_VNIC;
392da14cebeSEric Cheng 
393da14cebeSEric Cheng 	if (i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR, NULL))
394da14cebeSEric Cheng 		mip->mi_state_flags |= MIS_IS_AGGR;
395da14cebeSEric Cheng 
396da14cebeSEric Cheng 	mac_addr_factory_init(mip);
397da14cebeSEric Cheng 
39845d3dd98SRobert Mustacchi 	mac_transceiver_init(mip);
39945d3dd98SRobert Mustacchi 
400c1e9c696SRobert Mustacchi 	mac_led_init(mip);
401c1e9c696SRobert Mustacchi 
402da14cebeSEric Cheng 	/*
403da14cebeSEric Cheng 	 * Enforce the virtrualization level registered.
404da14cebeSEric Cheng 	 */
405da14cebeSEric Cheng 	if (mip->mi_v12n_level & MAC_VIRT_LEVEL1) {
406da14cebeSEric Cheng 		if (mac_init_rings(mip, MAC_RING_TYPE_RX) != 0 ||
407da14cebeSEric Cheng 		    mac_init_rings(mip, MAC_RING_TYPE_TX) != 0)
408da14cebeSEric Cheng 			goto fail;
409da14cebeSEric Cheng 
410da14cebeSEric Cheng 		/*
411da14cebeSEric Cheng 		 * The driver needs to register at least rx rings for this
412da14cebeSEric Cheng 		 * virtualization level.
413da14cebeSEric Cheng 		 */
414da14cebeSEric Cheng 		if (mip->mi_rx_groups == NULL)
415da14cebeSEric Cheng 			goto fail;
416da14cebeSEric Cheng 	}
417da14cebeSEric Cheng 
418da14cebeSEric Cheng 	/*
419da14cebeSEric Cheng 	 * The driver must set mc_unicst entry point to NULL when it advertises
420da14cebeSEric Cheng 	 * CAP_RINGS for rx groups.
421da14cebeSEric Cheng 	 */
422da14cebeSEric Cheng 	if (mip->mi_rx_groups != NULL) {
423da14cebeSEric Cheng 		if (mregp->m_callbacks->mc_unicst != NULL)
424da14cebeSEric Cheng 			goto fail;
425da14cebeSEric Cheng 	} else {
426da14cebeSEric Cheng 		if (mregp->m_callbacks->mc_unicst == NULL)
427da14cebeSEric Cheng 			goto fail;
428da14cebeSEric Cheng 	}
429da14cebeSEric Cheng 
430da14cebeSEric Cheng 	/*
431da14cebeSEric Cheng 	 * Initialize MAC addresses. Must be called after mac_init_rings().
432da14cebeSEric Cheng 	 */
433da14cebeSEric Cheng 	mac_init_macaddr(mip);
434da14cebeSEric Cheng 
435da14cebeSEric Cheng 	mip->mi_share_capab.ms_snum = 0;
436da14cebeSEric Cheng 	if (mip->mi_v12n_level & MAC_VIRT_HIO) {
437da14cebeSEric Cheng 		(void) mac_capab_get((mac_handle_t)mip, MAC_CAPAB_SHARES,
438da14cebeSEric Cheng 		    &mip->mi_share_capab);
439da14cebeSEric Cheng 	}
440da14cebeSEric Cheng 
441da14cebeSEric Cheng 	/*
442da14cebeSEric Cheng 	 * Initialize the kstats for this device.
443da14cebeSEric Cheng 	 */
4440dc2366fSVenugopal Iyer 	mac_driver_stat_create(mip);
445da14cebeSEric Cheng 
446da14cebeSEric Cheng 	/* Zero out any properties. */
447da14cebeSEric Cheng 	bzero(&mip->mi_resource_props, sizeof (mac_resource_props_t));
448da14cebeSEric Cheng 
4493ade6e84SGarrett D'Amore 	if (mip->mi_minor <= MAC_MAX_MINOR) {
450da14cebeSEric Cheng 		/* Create a style-2 DLPI device */
451da14cebeSEric Cheng 		if (ddi_create_minor_node(mip->mi_dip, driver, S_IFCHR, 0,
452da14cebeSEric Cheng 		    DDI_NT_NET, CLONE_DEV) != DDI_SUCCESS)
453da14cebeSEric Cheng 			goto fail;
454da14cebeSEric Cheng 		style2_created = B_TRUE;
455da14cebeSEric Cheng 
456da14cebeSEric Cheng 		/* Create a style-1 DLPI device */
457da14cebeSEric Cheng 		if (ddi_create_minor_node(mip->mi_dip, mip->mi_name, S_IFCHR,
458da14cebeSEric Cheng 		    mip->mi_minor, DDI_NT_NET, 0) != DDI_SUCCESS)
459da14cebeSEric Cheng 			goto fail;
460da14cebeSEric Cheng 		style1_created = B_TRUE;
461da14cebeSEric Cheng 	}
462da14cebeSEric Cheng 
463da14cebeSEric Cheng 	mac_flow_l2tab_create(mip, &mip->mi_flow_tab);
464da14cebeSEric Cheng 
465da14cebeSEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
466da14cebeSEric Cheng 	if (mod_hash_insert(i_mac_impl_hash,
467da14cebeSEric Cheng 	    (mod_hash_key_t)mip->mi_name, (mod_hash_val_t)mip) != 0) {
468da14cebeSEric Cheng 		rw_exit(&i_mac_impl_lock);
469da14cebeSEric Cheng 		err = EEXIST;
470da14cebeSEric Cheng 		goto fail;
471da14cebeSEric Cheng 	}
472da14cebeSEric Cheng 
473da14cebeSEric Cheng 	DTRACE_PROBE2(mac__register, struct devnames *, dnp,
474da14cebeSEric Cheng 	    (mac_impl_t *), mip);
475da14cebeSEric Cheng 
476da14cebeSEric Cheng 	/*
477da14cebeSEric Cheng 	 * Mark the MAC to be ready for open.
478da14cebeSEric Cheng 	 */
479da14cebeSEric Cheng 	mip->mi_state_flags &= ~MIS_DISABLED;
480da14cebeSEric Cheng 	rw_exit(&i_mac_impl_lock);
481da14cebeSEric Cheng 
482da14cebeSEric Cheng 	atomic_inc_32(&i_mac_impl_count);
483da14cebeSEric Cheng 
484da14cebeSEric Cheng 	cmn_err(CE_NOTE, "!%s registered", mip->mi_name);
485da14cebeSEric Cheng 	*mhp = (mac_handle_t)mip;
486da14cebeSEric Cheng 	return (0);
487da14cebeSEric Cheng 
488da14cebeSEric Cheng fail:
489da14cebeSEric Cheng 	if (style1_created)
490da14cebeSEric Cheng 		ddi_remove_minor_node(mip->mi_dip, mip->mi_name);
491da14cebeSEric Cheng 
492da14cebeSEric Cheng 	if (style2_created)
493da14cebeSEric Cheng 		ddi_remove_minor_node(mip->mi_dip, driver);
494da14cebeSEric Cheng 
495da14cebeSEric Cheng 	mac_addr_factory_fini(mip);
496da14cebeSEric Cheng 
497da14cebeSEric Cheng 	/* Clean up registered MAC addresses */
498da14cebeSEric Cheng 	mac_fini_macaddr(mip);
499da14cebeSEric Cheng 
500da14cebeSEric Cheng 	/* Clean up registered rings */
501da14cebeSEric Cheng 	mac_free_rings(mip, MAC_RING_TYPE_RX);
502da14cebeSEric Cheng 	mac_free_rings(mip, MAC_RING_TYPE_TX);
503da14cebeSEric Cheng 
504da14cebeSEric Cheng 	/* Clean up notification thread */
505da14cebeSEric Cheng 	if (mip->mi_notify_thread != NULL)
506da14cebeSEric Cheng 		i_mac_notify_exit(mip);
507da14cebeSEric Cheng 
508da14cebeSEric Cheng 	if (mip->mi_info.mi_unicst_addr != NULL) {
509da14cebeSEric Cheng 		kmem_free(mip->mi_info.mi_unicst_addr,
510da14cebeSEric Cheng 		    mip->mi_type->mt_addr_length);
511da14cebeSEric Cheng 		mip->mi_info.mi_unicst_addr = NULL;
512da14cebeSEric Cheng 	}
513da14cebeSEric Cheng 
5140dc2366fSVenugopal Iyer 	mac_driver_stat_delete(mip);
515da14cebeSEric Cheng 
516da14cebeSEric Cheng 	if (mip->mi_type != NULL) {
517da14cebeSEric Cheng 		atomic_dec_32(&mip->mi_type->mt_ref);
518da14cebeSEric Cheng 		mip->mi_type = NULL;
519da14cebeSEric Cheng 	}
520da14cebeSEric Cheng 
521da14cebeSEric Cheng 	if (mip->mi_pdata != NULL) {
522da14cebeSEric Cheng 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
523da14cebeSEric Cheng 		mip->mi_pdata = NULL;
524da14cebeSEric Cheng 		mip->mi_pdata_size = 0;
525da14cebeSEric Cheng 	}
526da14cebeSEric Cheng 
527da14cebeSEric Cheng 	if (minor != 0) {
528da14cebeSEric Cheng 		ASSERT(minor > MAC_MAX_MINOR);
529da14cebeSEric Cheng 		mac_minor_rele(minor);
530da14cebeSEric Cheng 	}
531da14cebeSEric Cheng 
5320dc2366fSVenugopal Iyer 	mip->mi_state_flags = 0;
533da14cebeSEric Cheng 	mac_unregister_priv_prop(mip);
534da14cebeSEric Cheng 
535783f25ceSMichael Speer 	/*
536783f25ceSMichael Speer 	 * Clear the state before destroying the mac_impl_t
537783f25ceSMichael Speer 	 */
538783f25ceSMichael Speer 	mip->mi_state_flags = 0;
539783f25ceSMichael Speer 
540da14cebeSEric Cheng 	kmem_cache_free(i_mac_impl_cachep, mip);
541da14cebeSEric Cheng 	return (err);
542da14cebeSEric Cheng }
543da14cebeSEric Cheng 
544da14cebeSEric Cheng /*
545da14cebeSEric Cheng  * Unregister from the GLDv3 framework
546da14cebeSEric Cheng  */
547da14cebeSEric Cheng int
548da14cebeSEric Cheng mac_unregister(mac_handle_t mh)
549da14cebeSEric Cheng {
550da14cebeSEric Cheng 	int			err;
551da14cebeSEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
552da14cebeSEric Cheng 	mod_hash_val_t		val;
553da14cebeSEric Cheng 	mac_margin_req_t	*mmr, *nextmmr;
554da14cebeSEric Cheng 
555da14cebeSEric Cheng 	/* Fail the unregister if there are any open references to this mac. */
556da14cebeSEric Cheng 	if ((err = mac_disable_nowait(mh)) != 0)
557da14cebeSEric Cheng 		return (err);
558da14cebeSEric Cheng 
559da14cebeSEric Cheng 	/*
560da14cebeSEric Cheng 	 * Clean up notification thread and wait for it to exit.
561da14cebeSEric Cheng 	 */
562da14cebeSEric Cheng 	i_mac_notify_exit(mip);
563da14cebeSEric Cheng 
5648241ccbbSDan McDonald 	/*
5658241ccbbSDan McDonald 	 * Prior to acquiring the MAC perimeter, remove the MAC instance from
5668241ccbbSDan McDonald 	 * the internal hash table. Such removal means table-walkers that
5678241ccbbSDan McDonald 	 * acquire the perimeter will not do so on behalf of what we are
5688241ccbbSDan McDonald 	 * unregistering, which prevents a deadlock.
5698241ccbbSDan McDonald 	 */
5708241ccbbSDan McDonald 	rw_enter(&i_mac_impl_lock, RW_WRITER);
5718241ccbbSDan McDonald 	(void) mod_hash_remove(i_mac_impl_hash,
5728241ccbbSDan McDonald 	    (mod_hash_key_t)mip->mi_name, &val);
5738241ccbbSDan McDonald 	rw_exit(&i_mac_impl_lock);
5748241ccbbSDan McDonald 	ASSERT(mip == (mac_impl_t *)val);
5758241ccbbSDan McDonald 
576da14cebeSEric Cheng 	i_mac_perim_enter(mip);
577da14cebeSEric Cheng 
5785d460eafSCathy Zhou 	/*
5795d460eafSCathy Zhou 	 * There is still resource properties configured over this mac.
5805d460eafSCathy Zhou 	 */
5815d460eafSCathy Zhou 	if (mip->mi_resource_props.mrp_mask != 0)
5825d460eafSCathy Zhou 		mac_fastpath_enable((mac_handle_t)mip);
5835d460eafSCathy Zhou 
584da14cebeSEric Cheng 	if (mip->mi_minor < MAC_MAX_MINOR + 1) {
585da14cebeSEric Cheng 		ddi_remove_minor_node(mip->mi_dip, mip->mi_name);
586da14cebeSEric Cheng 		ddi_remove_minor_node(mip->mi_dip,
587da14cebeSEric Cheng 		    (char *)ddi_driver_name(mip->mi_dip));
588da14cebeSEric Cheng 	}
589da14cebeSEric Cheng 
590da14cebeSEric Cheng 	ASSERT(mip->mi_nactiveclients == 0 && !(mip->mi_state_flags &
591da14cebeSEric Cheng 	    MIS_EXCLUSIVE));
592da14cebeSEric Cheng 
5930dc2366fSVenugopal Iyer 	mac_driver_stat_delete(mip);
594da14cebeSEric Cheng 
595da14cebeSEric Cheng 	ASSERT(i_mac_impl_count > 0);
596da14cebeSEric Cheng 	atomic_dec_32(&i_mac_impl_count);
597da14cebeSEric Cheng 
598da14cebeSEric Cheng 	if (mip->mi_pdata != NULL)
599da14cebeSEric Cheng 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
600da14cebeSEric Cheng 	mip->mi_pdata = NULL;
601da14cebeSEric Cheng 	mip->mi_pdata_size = 0;
602da14cebeSEric Cheng 
603da14cebeSEric Cheng 	/*
604da14cebeSEric Cheng 	 * Free the list of margin request.
605da14cebeSEric Cheng 	 */
606da14cebeSEric Cheng 	for (mmr = mip->mi_mmrp; mmr != NULL; mmr = nextmmr) {
607da14cebeSEric Cheng 		nextmmr = mmr->mmr_nextp;
608da14cebeSEric Cheng 		kmem_free(mmr, sizeof (mac_margin_req_t));
609da14cebeSEric Cheng 	}
610da14cebeSEric Cheng 	mip->mi_mmrp = NULL;
611da14cebeSEric Cheng 
6124eaa4710SRishi Srivatsavai 	mip->mi_linkstate = mip->mi_lowlinkstate = LINK_STATE_UNKNOWN;
613da14cebeSEric Cheng 	kmem_free(mip->mi_info.mi_unicst_addr, mip->mi_type->mt_addr_length);
614da14cebeSEric Cheng 	mip->mi_info.mi_unicst_addr = NULL;
615da14cebeSEric Cheng 
616da14cebeSEric Cheng 	atomic_dec_32(&mip->mi_type->mt_ref);
617da14cebeSEric Cheng 	mip->mi_type = NULL;
618da14cebeSEric Cheng 
619da14cebeSEric Cheng 	/*
620da14cebeSEric Cheng 	 * Free the primary MAC address.
621da14cebeSEric Cheng 	 */
622da14cebeSEric Cheng 	mac_fini_macaddr(mip);
623da14cebeSEric Cheng 
624da14cebeSEric Cheng 	/*
625da14cebeSEric Cheng 	 * free all rings
626da14cebeSEric Cheng 	 */
627da14cebeSEric Cheng 	mac_free_rings(mip, MAC_RING_TYPE_RX);
628da14cebeSEric Cheng 	mac_free_rings(mip, MAC_RING_TYPE_TX);
629da14cebeSEric Cheng 
630da14cebeSEric Cheng 	mac_addr_factory_fini(mip);
631da14cebeSEric Cheng 
632da14cebeSEric Cheng 	bzero(mip->mi_addr, MAXMACADDRLEN);
633da14cebeSEric Cheng 	bzero(mip->mi_dstaddr, MAXMACADDRLEN);
6349b41bdc4SRobert Mustacchi 	mip->mi_dstaddr_set = B_FALSE;
635da14cebeSEric Cheng 
636da14cebeSEric Cheng 	/* and the flows */
637da14cebeSEric Cheng 	mac_flow_tab_destroy(mip->mi_flow_tab);
638da14cebeSEric Cheng 	mip->mi_flow_tab = NULL;
639da14cebeSEric Cheng 
640da14cebeSEric Cheng 	if (mip->mi_minor > MAC_MAX_MINOR)
641da14cebeSEric Cheng 		mac_minor_rele(mip->mi_minor);
642da14cebeSEric Cheng 
643da14cebeSEric Cheng 	cmn_err(CE_NOTE, "!%s unregistered", mip->mi_name);
644da14cebeSEric Cheng 
645da14cebeSEric Cheng 	/*
646da14cebeSEric Cheng 	 * Reset the perim related fields to default values before
647da14cebeSEric Cheng 	 * kmem_cache_free
648da14cebeSEric Cheng 	 */
649da14cebeSEric Cheng 	i_mac_perim_exit(mip);
650da14cebeSEric Cheng 	mip->mi_state_flags = 0;
651da14cebeSEric Cheng 
652da14cebeSEric Cheng 	mac_unregister_priv_prop(mip);
6534eaa4710SRishi Srivatsavai 
6544eaa4710SRishi Srivatsavai 	ASSERT(mip->mi_bridge_link == NULL);
655da14cebeSEric Cheng 	kmem_cache_free(i_mac_impl_cachep, mip);
656da14cebeSEric Cheng 
657da14cebeSEric Cheng 	return (0);
658da14cebeSEric Cheng }
659da14cebeSEric Cheng 
660da14cebeSEric Cheng /* DATA RECEPTION */
661da14cebeSEric Cheng 
662da14cebeSEric Cheng /*
663da14cebeSEric Cheng  * This function is invoked for packets received by the MAC driver in
664da14cebeSEric Cheng  * interrupt context. The ring generation number provided by the driver
665da14cebeSEric Cheng  * is matched with the ring generation number held in MAC. If they do not
666da14cebeSEric Cheng  * match, received packets are considered stale packets coming from an older
667da14cebeSEric Cheng  * assignment of the ring. Drop them.
668da14cebeSEric Cheng  */
669da14cebeSEric Cheng void
670da14cebeSEric Cheng mac_rx_ring(mac_handle_t mh, mac_ring_handle_t mrh, mblk_t *mp_chain,
671da14cebeSEric Cheng     uint64_t mr_gen_num)
672da14cebeSEric Cheng {
673da14cebeSEric Cheng 	mac_ring_t		*mr = (mac_ring_t *)mrh;
674da14cebeSEric Cheng 
675da14cebeSEric Cheng 	if ((mr != NULL) && (mr->mr_gen_num != mr_gen_num)) {
676da14cebeSEric Cheng 		DTRACE_PROBE2(mac__rx__rings__stale__packet, uint64_t,
677da14cebeSEric Cheng 		    mr->mr_gen_num, uint64_t, mr_gen_num);
678da14cebeSEric Cheng 		freemsgchain(mp_chain);
679da14cebeSEric Cheng 		return;
680da14cebeSEric Cheng 	}
681da14cebeSEric Cheng 	mac_rx(mh, (mac_resource_handle_t)mrh, mp_chain);
682da14cebeSEric Cheng }
683da14cebeSEric Cheng 
684da14cebeSEric Cheng /*
6854eaa4710SRishi Srivatsavai  * This function is invoked for each packet received by the underlying driver.
686da14cebeSEric Cheng  */
687da14cebeSEric Cheng void
688da14cebeSEric Cheng mac_rx(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain)
6894eaa4710SRishi Srivatsavai {
6904eaa4710SRishi Srivatsavai 	mac_impl_t *mip = (mac_impl_t *)mh;
6914eaa4710SRishi Srivatsavai 
6924eaa4710SRishi Srivatsavai 	/*
6934eaa4710SRishi Srivatsavai 	 * Check if the link is part of a bridge.  If not, then we don't need
6944eaa4710SRishi Srivatsavai 	 * to take the lock to remain consistent.  Make this common case
6954eaa4710SRishi Srivatsavai 	 * lock-free and tail-call optimized.
6964eaa4710SRishi Srivatsavai 	 */
6974eaa4710SRishi Srivatsavai 	if (mip->mi_bridge_link == NULL) {
6984eaa4710SRishi Srivatsavai 		mac_rx_common(mh, mrh, mp_chain);
6994eaa4710SRishi Srivatsavai 	} else {
7004eaa4710SRishi Srivatsavai 		/*
7014eaa4710SRishi Srivatsavai 		 * Once we take a reference on the bridge link, the bridge
7024eaa4710SRishi Srivatsavai 		 * module itself can't unload, so the callback pointers are
7034eaa4710SRishi Srivatsavai 		 * stable.
7044eaa4710SRishi Srivatsavai 		 */
7054eaa4710SRishi Srivatsavai 		mutex_enter(&mip->mi_bridge_lock);
7064eaa4710SRishi Srivatsavai 		if ((mh = mip->mi_bridge_link) != NULL)
7074eaa4710SRishi Srivatsavai 			mac_bridge_ref_cb(mh, B_TRUE);
7084eaa4710SRishi Srivatsavai 		mutex_exit(&mip->mi_bridge_lock);
7094eaa4710SRishi Srivatsavai 		if (mh == NULL) {
7104eaa4710SRishi Srivatsavai 			mac_rx_common((mac_handle_t)mip, mrh, mp_chain);
7114eaa4710SRishi Srivatsavai 		} else {
7124eaa4710SRishi Srivatsavai 			mac_bridge_rx_cb(mh, mrh, mp_chain);
7134eaa4710SRishi Srivatsavai 			mac_bridge_ref_cb(mh, B_FALSE);
7144eaa4710SRishi Srivatsavai 		}
7154eaa4710SRishi Srivatsavai 	}
7164eaa4710SRishi Srivatsavai }
7174eaa4710SRishi Srivatsavai 
7184eaa4710SRishi Srivatsavai /*
7194eaa4710SRishi Srivatsavai  * Special case function: this allows snooping of packets transmitted and
7204eaa4710SRishi Srivatsavai  * received by TRILL. By design, they go directly into the TRILL module.
7214eaa4710SRishi Srivatsavai  */
7224eaa4710SRishi Srivatsavai void
7234eaa4710SRishi Srivatsavai mac_trill_snoop(mac_handle_t mh, mblk_t *mp)
7244eaa4710SRishi Srivatsavai {
7254eaa4710SRishi Srivatsavai 	mac_impl_t *mip = (mac_impl_t *)mh;
7264eaa4710SRishi Srivatsavai 
7274eaa4710SRishi Srivatsavai 	if (mip->mi_promisc_list != NULL)
728c61a1653SRyan Zezeski 		mac_promisc_dispatch(mip, mp, NULL, B_FALSE);
7294eaa4710SRishi Srivatsavai }
7304eaa4710SRishi Srivatsavai 
7314eaa4710SRishi Srivatsavai /*
7324eaa4710SRishi Srivatsavai  * This is the upward reentry point for packets arriving from the bridging
7334eaa4710SRishi Srivatsavai  * module and from mac_rx for links not part of a bridge.
7344eaa4710SRishi Srivatsavai  */
7354eaa4710SRishi Srivatsavai void
7364eaa4710SRishi Srivatsavai mac_rx_common(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain)
737da14cebeSEric Cheng {
738da14cebeSEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
739da14cebeSEric Cheng 	mac_ring_t		*mr = (mac_ring_t *)mrh;
74084de666eSRyan Zezeski 	mac_soft_ring_set_t	*mac_srs;
741da14cebeSEric Cheng 	mblk_t			*bp = mp_chain;
742da14cebeSEric Cheng 
743da14cebeSEric Cheng 	/*
744da14cebeSEric Cheng 	 * If there are any promiscuous mode callbacks defined for
745da14cebeSEric Cheng 	 * this MAC, pass them a copy if appropriate.
746da14cebeSEric Cheng 	 */
747da14cebeSEric Cheng 	if (mip->mi_promisc_list != NULL)
748c61a1653SRyan Zezeski 		mac_promisc_dispatch(mip, mp_chain, NULL, B_FALSE);
749da14cebeSEric Cheng 
750da14cebeSEric Cheng 	if (mr != NULL) {
751da14cebeSEric Cheng 		/*
752da14cebeSEric Cheng 		 * If the SRS teardown has started, just return. The 'mr'
75345948e49SRyan Zezeski 		 * continues to be valid until the driver unregisters the MAC.
754da14cebeSEric Cheng 		 * Hardware classified packets will not make their way up
755da14cebeSEric Cheng 		 * beyond this point once the teardown has started. The driver
756da14cebeSEric Cheng 		 * is never passed a pointer to a flow entry or SRS or any
757da14cebeSEric Cheng 		 * structure that can be freed much before mac_unregister.
758da14cebeSEric Cheng 		 */
759da14cebeSEric Cheng 		mutex_enter(&mr->mr_lock);
760da14cebeSEric Cheng 		if ((mr->mr_state != MR_INUSE) || (mr->mr_flag &
761da14cebeSEric Cheng 		    (MR_INCIPIENT | MR_CONDEMNED | MR_QUIESCE))) {
762da14cebeSEric Cheng 			mutex_exit(&mr->mr_lock);
763da14cebeSEric Cheng 			freemsgchain(mp_chain);
764da14cebeSEric Cheng 			return;
765da14cebeSEric Cheng 		}
76645948e49SRyan Zezeski 
76745948e49SRyan Zezeski 		/*
76845948e49SRyan Zezeski 		 * The ring is in passthru mode; pass the chain up to
76945948e49SRyan Zezeski 		 * the pseudo ring.
77045948e49SRyan Zezeski 		 */
77145948e49SRyan Zezeski 		if (mr->mr_classify_type == MAC_PASSTHRU_CLASSIFIER) {
772da14cebeSEric Cheng 			MR_REFHOLD_LOCKED(mr);
77345948e49SRyan Zezeski 			mutex_exit(&mr->mr_lock);
77445948e49SRyan Zezeski 			mr->mr_pt_fn(mr->mr_pt_arg1, mr->mr_pt_arg2, mp_chain,
77545948e49SRyan Zezeski 			    B_FALSE);
77645948e49SRyan Zezeski 			MR_REFRELE(mr);
77745948e49SRyan Zezeski 			return;
778da14cebeSEric Cheng 		}
77945948e49SRyan Zezeski 
78045948e49SRyan Zezeski 		/*
78145948e49SRyan Zezeski 		 * The passthru callback should only be set when in
78245948e49SRyan Zezeski 		 * MAC_PASSTHRU_CLASSIFIER mode.
78345948e49SRyan Zezeski 		 */
78445948e49SRyan Zezeski 		ASSERT3P(mr->mr_pt_fn, ==, NULL);
785da14cebeSEric Cheng 
786da14cebeSEric Cheng 		/*
787da14cebeSEric Cheng 		 * We check if an SRS is controlling this ring.
788da14cebeSEric Cheng 		 * If so, we can directly call the srs_lower_proc
789da14cebeSEric Cheng 		 * routine otherwise we need to go through mac_rx_classify
790da14cebeSEric Cheng 		 * to reach the right place.
791da14cebeSEric Cheng 		 */
79245948e49SRyan Zezeski 		if (mr->mr_classify_type == MAC_HW_CLASSIFIER) {
79345948e49SRyan Zezeski 			MR_REFHOLD_LOCKED(mr);
79445948e49SRyan Zezeski 			mutex_exit(&mr->mr_lock);
79545948e49SRyan Zezeski 			ASSERT3P(mr->mr_srs, !=, NULL);
796da14cebeSEric Cheng 			mac_srs = mr->mr_srs;
79745948e49SRyan Zezeski 
798da14cebeSEric Cheng 			/*
79945948e49SRyan Zezeski 			 * This is the fast path. All packets received
80045948e49SRyan Zezeski 			 * on this ring are hardware classified and
80145948e49SRyan Zezeski 			 * share the same MAC header info.
802da14cebeSEric Cheng 			 */
803da14cebeSEric Cheng 			mac_srs->srs_rx.sr_lower_proc(mh,
804da14cebeSEric Cheng 			    (mac_resource_handle_t)mac_srs, mp_chain, B_FALSE);
805da14cebeSEric Cheng 			MR_REFRELE(mr);
806da14cebeSEric Cheng 			return;
807da14cebeSEric Cheng 		}
80845948e49SRyan Zezeski 
80945948e49SRyan Zezeski 		mutex_exit(&mr->mr_lock);
810da14cebeSEric Cheng 		/* We'll fall through to software classification */
811ae6aa22aSVenugopal Iyer 	} else {
812ae6aa22aSVenugopal Iyer 		flow_entry_t *flent;
813ae6aa22aSVenugopal Iyer 		int err;
814ae6aa22aSVenugopal Iyer 
815ae6aa22aSVenugopal Iyer 		rw_enter(&mip->mi_rw_lock, RW_READER);
816ae6aa22aSVenugopal Iyer 		if (mip->mi_single_active_client != NULL) {
817ae6aa22aSVenugopal Iyer 			flent = mip->mi_single_active_client->mci_flent_list;
818ae6aa22aSVenugopal Iyer 			FLOW_TRY_REFHOLD(flent, err);
819ae6aa22aSVenugopal Iyer 			rw_exit(&mip->mi_rw_lock);
820ae6aa22aSVenugopal Iyer 			if (err == 0) {
821ae6aa22aSVenugopal Iyer 				(flent->fe_cb_fn)(flent->fe_cb_arg1,
822ae6aa22aSVenugopal Iyer 				    flent->fe_cb_arg2, mp_chain, B_FALSE);
823ae6aa22aSVenugopal Iyer 				FLOW_REFRELE(flent);
824ae6aa22aSVenugopal Iyer 				return;
825ae6aa22aSVenugopal Iyer 			}
826ae6aa22aSVenugopal Iyer 		} else {
827ae6aa22aSVenugopal Iyer 			rw_exit(&mip->mi_rw_lock);
828ae6aa22aSVenugopal Iyer 		}
829da14cebeSEric Cheng 	}
830da14cebeSEric Cheng 
831da14cebeSEric Cheng 	if (!FLOW_TAB_EMPTY(mip->mi_flow_tab)) {
832da14cebeSEric Cheng 		if ((bp = mac_rx_flow(mh, mrh, bp)) == NULL)
833da14cebeSEric Cheng 			return;
834da14cebeSEric Cheng 	}
835da14cebeSEric Cheng 
836da14cebeSEric Cheng 	freemsgchain(bp);
837da14cebeSEric Cheng }
838da14cebeSEric Cheng 
839da14cebeSEric Cheng /* DATA TRANSMISSION */
840da14cebeSEric Cheng 
841da14cebeSEric Cheng /*
842da14cebeSEric Cheng  * A driver's notification to resume transmission, in case of a provider
843da14cebeSEric Cheng  * without TX rings.
844da14cebeSEric Cheng  */
845da14cebeSEric Cheng void
846da14cebeSEric Cheng mac_tx_update(mac_handle_t mh)
847da14cebeSEric Cheng {
8480dc2366fSVenugopal Iyer 	mac_tx_ring_update(mh, NULL);
849da14cebeSEric Cheng }
850da14cebeSEric Cheng 
851da14cebeSEric Cheng /*
852da14cebeSEric Cheng  * A driver's notification to resume transmission on the specified TX ring.
853da14cebeSEric Cheng  */
854da14cebeSEric Cheng void
855da14cebeSEric Cheng mac_tx_ring_update(mac_handle_t mh, mac_ring_handle_t rh)
856da14cebeSEric Cheng {
857da14cebeSEric Cheng 	i_mac_tx_srs_notify((mac_impl_t *)mh, rh);
858da14cebeSEric Cheng }
859da14cebeSEric Cheng 
860da14cebeSEric Cheng /* LINK STATE */
861da14cebeSEric Cheng /*
862da14cebeSEric Cheng  * Notify the MAC layer about a link state change
863da14cebeSEric Cheng  */
864da14cebeSEric Cheng void
865da14cebeSEric Cheng mac_link_update(mac_handle_t mh, link_state_t link)
866da14cebeSEric Cheng {
867da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
868da14cebeSEric Cheng 
8694eaa4710SRishi Srivatsavai 	/*
8704eaa4710SRishi Srivatsavai 	 * Save the link state.
8714eaa4710SRishi Srivatsavai 	 */
8724eaa4710SRishi Srivatsavai 	mip->mi_lowlinkstate = link;
8734eaa4710SRishi Srivatsavai 
8744eaa4710SRishi Srivatsavai 	/*
8754eaa4710SRishi Srivatsavai 	 * Send a MAC_NOTE_LOWLINK notification.  This tells the notification
8764eaa4710SRishi Srivatsavai 	 * thread to deliver both lower and upper notifications.
8774eaa4710SRishi Srivatsavai 	 */
8784eaa4710SRishi Srivatsavai 	i_mac_notify(mip, MAC_NOTE_LOWLINK);
8794eaa4710SRishi Srivatsavai }
8804eaa4710SRishi Srivatsavai 
8814eaa4710SRishi Srivatsavai /*
8824eaa4710SRishi Srivatsavai  * Notify the MAC layer about a link state change due to bridging.
8834eaa4710SRishi Srivatsavai  */
8844eaa4710SRishi Srivatsavai void
8854eaa4710SRishi Srivatsavai mac_link_redo(mac_handle_t mh, link_state_t link)
8864eaa4710SRishi Srivatsavai {
8874eaa4710SRishi Srivatsavai 	mac_impl_t	*mip = (mac_impl_t *)mh;
8884eaa4710SRishi Srivatsavai 
889da14cebeSEric Cheng 	/*
890da14cebeSEric Cheng 	 * Save the link state.
891da14cebeSEric Cheng 	 */
892da14cebeSEric Cheng 	mip->mi_linkstate = link;
893da14cebeSEric Cheng 
894da14cebeSEric Cheng 	/*
8954eaa4710SRishi Srivatsavai 	 * Send a MAC_NOTE_LINK notification.  Only upper notifications are
8964eaa4710SRishi Srivatsavai 	 * made.
897da14cebeSEric Cheng 	 */
898da14cebeSEric Cheng 	i_mac_notify(mip, MAC_NOTE_LINK);
899da14cebeSEric Cheng }
900da14cebeSEric Cheng 
90161af1958SGarrett D'Amore /* MINOR NODE HANDLING */
90261af1958SGarrett D'Amore 
90361af1958SGarrett D'Amore /*
90461af1958SGarrett D'Amore  * Given a dev_t, return the instance number (PPA) associated with it.
90561af1958SGarrett D'Amore  * Drivers can use this in their getinfo(9e) implementation to lookup
90661af1958SGarrett D'Amore  * the instance number (i.e. PPA) of the device, to use as an index to
90761af1958SGarrett D'Amore  * their own array of soft state structures.
90861af1958SGarrett D'Amore  *
90961af1958SGarrett D'Amore  * Returns -1 on error.
91061af1958SGarrett D'Amore  */
91161af1958SGarrett D'Amore int
91261af1958SGarrett D'Amore mac_devt_to_instance(dev_t devt)
91361af1958SGarrett D'Amore {
91461af1958SGarrett D'Amore 	return (dld_devt_to_instance(devt));
91561af1958SGarrett D'Amore }
91661af1958SGarrett D'Amore 
91761af1958SGarrett D'Amore /*
91861af1958SGarrett D'Amore  * This function returns the first minor number that is available for
91961af1958SGarrett D'Amore  * driver private use.  All minor numbers smaller than this are
92061af1958SGarrett D'Amore  * reserved for GLDv3 use.
92161af1958SGarrett D'Amore  */
92261af1958SGarrett D'Amore minor_t
92361af1958SGarrett D'Amore mac_private_minor(void)
92461af1958SGarrett D'Amore {
92561af1958SGarrett D'Amore 	return (MAC_PRIVATE_MINOR);
92661af1958SGarrett D'Amore }
92761af1958SGarrett D'Amore 
928da14cebeSEric Cheng /* OTHER CONTROL INFORMATION */
929da14cebeSEric Cheng 
930da14cebeSEric Cheng /*
931da14cebeSEric Cheng  * A driver notified us that its primary MAC address has changed.
932da14cebeSEric Cheng  */
933da14cebeSEric Cheng void
934da14cebeSEric Cheng mac_unicst_update(mac_handle_t mh, const uint8_t *addr)
935da14cebeSEric Cheng {
936da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
937da14cebeSEric Cheng 
938da14cebeSEric Cheng 	if (mip->mi_type->mt_addr_length == 0)
939da14cebeSEric Cheng 		return;
940da14cebeSEric Cheng 
941da14cebeSEric Cheng 	i_mac_perim_enter(mip);
942da14cebeSEric Cheng 
943da14cebeSEric Cheng 	/*
944fd0939efSDavid Edmondson 	 * If address changes, freshen the MAC address value and update
945fd0939efSDavid Edmondson 	 * all MAC clients that share this MAC address.
946da14cebeSEric Cheng 	 */
947fd0939efSDavid Edmondson 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) != 0) {
948fd0939efSDavid Edmondson 		mac_freshen_macaddr(mac_find_macaddr(mip, mip->mi_addr),
949fd0939efSDavid Edmondson 		    (uint8_t *)addr);
950fd0939efSDavid Edmondson 	}
951da14cebeSEric Cheng 
952da14cebeSEric Cheng 	i_mac_perim_exit(mip);
953da14cebeSEric Cheng 
954da14cebeSEric Cheng 	/*
955da14cebeSEric Cheng 	 * Send a MAC_NOTE_UNICST notification.
956da14cebeSEric Cheng 	 */
957da14cebeSEric Cheng 	i_mac_notify(mip, MAC_NOTE_UNICST);
958da14cebeSEric Cheng }
959da14cebeSEric Cheng 
9602b24ab6bSSebastien Roy void
9612b24ab6bSSebastien Roy mac_dst_update(mac_handle_t mh, const uint8_t *addr)
9622b24ab6bSSebastien Roy {
9632b24ab6bSSebastien Roy 	mac_impl_t	*mip = (mac_impl_t *)mh;
9642b24ab6bSSebastien Roy 
9652b24ab6bSSebastien Roy 	if (mip->mi_type->mt_addr_length == 0)
9662b24ab6bSSebastien Roy 		return;
9672b24ab6bSSebastien Roy 
9682b24ab6bSSebastien Roy 	i_mac_perim_enter(mip);
9692b24ab6bSSebastien Roy 	bcopy(addr, mip->mi_dstaddr, mip->mi_type->mt_addr_length);
9702b24ab6bSSebastien Roy 	i_mac_perim_exit(mip);
9712b24ab6bSSebastien Roy 	i_mac_notify(mip, MAC_NOTE_DEST);
9722b24ab6bSSebastien Roy }
9732b24ab6bSSebastien Roy 
974da14cebeSEric Cheng /*
975da14cebeSEric Cheng  * MAC plugin information changed.
976da14cebeSEric Cheng  */
977da14cebeSEric Cheng int
978da14cebeSEric Cheng mac_pdata_update(mac_handle_t mh, void *mac_pdata, size_t dsize)
979da14cebeSEric Cheng {
980da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
981da14cebeSEric Cheng 
982da14cebeSEric Cheng 	/*
983da14cebeSEric Cheng 	 * Verify that the plugin supports MAC plugin data and that the
984da14cebeSEric Cheng 	 * supplied data is valid.
985da14cebeSEric Cheng 	 */
986da14cebeSEric Cheng 	if (!(mip->mi_type->mt_ops.mtops_ops & MTOPS_PDATA_VERIFY))
987da14cebeSEric Cheng 		return (EINVAL);
988da14cebeSEric Cheng 	if (!mip->mi_type->mt_ops.mtops_pdata_verify(mac_pdata, dsize))
989da14cebeSEric Cheng 		return (EINVAL);
990da14cebeSEric Cheng 
991da14cebeSEric Cheng 	if (mip->mi_pdata != NULL)
992da14cebeSEric Cheng 		kmem_free(mip->mi_pdata, mip->mi_pdata_size);
993da14cebeSEric Cheng 
994da14cebeSEric Cheng 	mip->mi_pdata = kmem_alloc(dsize, KM_SLEEP);
995da14cebeSEric Cheng 	bcopy(mac_pdata, mip->mi_pdata, dsize);
996da14cebeSEric Cheng 	mip->mi_pdata_size = dsize;
997da14cebeSEric Cheng 
998da14cebeSEric Cheng 	/*
999da14cebeSEric Cheng 	 * Since the MAC plugin data is used to construct MAC headers that
1000da14cebeSEric Cheng 	 * were cached in fast-path headers, we need to flush fast-path
1001da14cebeSEric Cheng 	 * information for links associated with this mac.
1002da14cebeSEric Cheng 	 */
1003da14cebeSEric Cheng 	i_mac_notify(mip, MAC_NOTE_FASTPATH_FLUSH);
1004da14cebeSEric Cheng 	return (0);
1005da14cebeSEric Cheng }
1006da14cebeSEric Cheng 
1007da14cebeSEric Cheng /*
1008c61a1653SRyan Zezeski  * The mac provider or mac frameowrk calls this function when it wants
1009c61a1653SRyan Zezeski  * to notify upstream consumers that the capabilities have changed and
1010c61a1653SRyan Zezeski  * that they should modify their own internal state accordingly.
1011c61a1653SRyan Zezeski  *
1012c61a1653SRyan Zezeski  * We currently have no regard for the fact that a provider could
1013c61a1653SRyan Zezeski  * decide to drop capabilities which would invalidate pending traffic.
1014c61a1653SRyan Zezeski  * For example, if one was to disable the Tx checksum offload while
1015c61a1653SRyan Zezeski  * TCP/IP traffic was being sent by mac clients relying on that
1016c61a1653SRyan Zezeski  * feature, then those packets would hit the write with missing or
1017c61a1653SRyan Zezeski  * partial checksums. A proper solution involves not only providing
1018c61a1653SRyan Zezeski  * notfication, but also performing client quiescing. That is, a capab
1019c61a1653SRyan Zezeski  * change should be treated as an atomic transaction that forms a
1020c61a1653SRyan Zezeski  * barrier between traffic relying on the current capabs and traffic
1021c61a1653SRyan Zezeski  * relying on the new capabs. In practice, simnet is currently the
1022c61a1653SRyan Zezeski  * only provider that could hit this, and it's an easily avoidable
1023c61a1653SRyan Zezeski  * situation (and at worst it should only lead to some dropped
1024c61a1653SRyan Zezeski  * packets). But if we ever want better on-the-fly capab change to
1025c61a1653SRyan Zezeski  * actual hardware providers, then we should give this update
1026c61a1653SRyan Zezeski  * mechanism a proper implementation.
1027da14cebeSEric Cheng  */
1028da14cebeSEric Cheng void
1029da14cebeSEric Cheng mac_capab_update(mac_handle_t mh)
1030da14cebeSEric Cheng {
1031c61a1653SRyan Zezeski 	/*
1032c61a1653SRyan Zezeski 	 * Send a MAC_NOTE_CAPAB_CHG notification to alert upstream
1033c61a1653SRyan Zezeski 	 * clients to renegotiate capabilities.
1034c61a1653SRyan Zezeski 	 */
1035da14cebeSEric Cheng 	i_mac_notify((mac_impl_t *)mh, MAC_NOTE_CAPAB_CHG);
1036da14cebeSEric Cheng }
1037da14cebeSEric Cheng 
10381eee170aSErik Nordmark /*
10391eee170aSErik Nordmark  * Used by normal drivers to update the max sdu size.
10401eee170aSErik Nordmark  * We need to handle the case of a smaller mi_sdu_multicast
10411eee170aSErik Nordmark  * since this is called by mac_set_mtu() even for drivers that
10421eee170aSErik Nordmark  * have differing unicast and multicast mtu and we don't want to
10431eee170aSErik Nordmark  * increase the multicast mtu by accident in that case.
10441eee170aSErik Nordmark  */
1045da14cebeSEric Cheng int
1046da14cebeSEric Cheng mac_maxsdu_update(mac_handle_t mh, uint_t sdu_max)
1047da14cebeSEric Cheng {
1048da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
1049da14cebeSEric Cheng 
1050f0f2c3a5SGirish Moodalbail 	if (sdu_max == 0 || sdu_max < mip->mi_sdu_min)
1051da14cebeSEric Cheng 		return (EINVAL);
1052da14cebeSEric Cheng 	mip->mi_sdu_max = sdu_max;
10531eee170aSErik Nordmark 	if (mip->mi_sdu_multicast > mip->mi_sdu_max)
10541eee170aSErik Nordmark 		mip->mi_sdu_multicast = mip->mi_sdu_max;
10551eee170aSErik Nordmark 
10561eee170aSErik Nordmark 	/* Send a MAC_NOTE_SDU_SIZE notification. */
10571eee170aSErik Nordmark 	i_mac_notify(mip, MAC_NOTE_SDU_SIZE);
10581eee170aSErik Nordmark 	return (0);
10591eee170aSErik Nordmark }
10601eee170aSErik Nordmark 
10611eee170aSErik Nordmark /*
10621eee170aSErik Nordmark  * Version of the above function that is used by drivers that have a different
10631eee170aSErik Nordmark  * max sdu size for multicast/broadcast vs. unicast.
10641eee170aSErik Nordmark  */
10651eee170aSErik Nordmark int
10661eee170aSErik Nordmark mac_maxsdu_update2(mac_handle_t mh, uint_t sdu_max, uint_t sdu_multicast)
10671eee170aSErik Nordmark {
10681eee170aSErik Nordmark 	mac_impl_t	*mip = (mac_impl_t *)mh;
10691eee170aSErik Nordmark 
10701eee170aSErik Nordmark 	if (sdu_max == 0 || sdu_max < mip->mi_sdu_min)
10711eee170aSErik Nordmark 		return (EINVAL);
10721eee170aSErik Nordmark 	if (sdu_multicast == 0)
10731eee170aSErik Nordmark 		sdu_multicast = sdu_max;
10741eee170aSErik Nordmark 	if (sdu_multicast > sdu_max || sdu_multicast < mip->mi_sdu_min)
10751eee170aSErik Nordmark 		return (EINVAL);
10761eee170aSErik Nordmark 	mip->mi_sdu_max = sdu_max;
10771eee170aSErik Nordmark 	mip->mi_sdu_multicast = sdu_multicast;
1078da14cebeSEric Cheng 
1079da14cebeSEric Cheng 	/* Send a MAC_NOTE_SDU_SIZE notification. */
1080da14cebeSEric Cheng 	i_mac_notify(mip, MAC_NOTE_SDU_SIZE);
1081da14cebeSEric Cheng 	return (0);
1082da14cebeSEric Cheng }
1083da14cebeSEric Cheng 
10840dc2366fSVenugopal Iyer static void
10850dc2366fSVenugopal Iyer mac_ring_intr_retarget(mac_group_t *group, mac_ring_t *ring)
10860dc2366fSVenugopal Iyer {
10870dc2366fSVenugopal Iyer 	mac_client_impl_t *mcip;
10880dc2366fSVenugopal Iyer 	flow_entry_t *flent;
10890dc2366fSVenugopal Iyer 	mac_soft_ring_set_t *mac_rx_srs;
10900dc2366fSVenugopal Iyer 	mac_cpus_t *srs_cpu;
10910dc2366fSVenugopal Iyer 	int i;
10920dc2366fSVenugopal Iyer 
10930dc2366fSVenugopal Iyer 	if (((mcip = MAC_GROUP_ONLY_CLIENT(group)) != NULL) &&
10940dc2366fSVenugopal Iyer 	    (!ring->mr_info.mri_intr.mi_ddi_shared)) {
10950dc2366fSVenugopal Iyer 		/* interrupt can be re-targeted */
10960dc2366fSVenugopal Iyer 		ASSERT(group->mrg_state == MAC_GROUP_STATE_RESERVED);
10970dc2366fSVenugopal Iyer 		flent = mcip->mci_flent;
10980dc2366fSVenugopal Iyer 		if (ring->mr_type == MAC_RING_TYPE_RX) {
10990dc2366fSVenugopal Iyer 			for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
11000dc2366fSVenugopal Iyer 				mac_rx_srs = flent->fe_rx_srs[i];
11010dc2366fSVenugopal Iyer 				if (mac_rx_srs->srs_ring != ring)
11020dc2366fSVenugopal Iyer 					continue;
11030dc2366fSVenugopal Iyer 				srs_cpu = &mac_rx_srs->srs_cpu;
11040dc2366fSVenugopal Iyer 				mutex_enter(&cpu_lock);
11050dc2366fSVenugopal Iyer 				mac_rx_srs_retarget_intr(mac_rx_srs,
11060dc2366fSVenugopal Iyer 				    srs_cpu->mc_rx_intr_cpu);
11070dc2366fSVenugopal Iyer 				mutex_exit(&cpu_lock);
11080dc2366fSVenugopal Iyer 				break;
11090dc2366fSVenugopal Iyer 			}
11100dc2366fSVenugopal Iyer 		} else {
11110dc2366fSVenugopal Iyer 			if (flent->fe_tx_srs != NULL) {
11120dc2366fSVenugopal Iyer 				mutex_enter(&cpu_lock);
11130dc2366fSVenugopal Iyer 				mac_tx_srs_retarget_intr(
11140dc2366fSVenugopal Iyer 				    flent->fe_tx_srs);
11150dc2366fSVenugopal Iyer 				mutex_exit(&cpu_lock);
11160dc2366fSVenugopal Iyer 			}
11170dc2366fSVenugopal Iyer 		}
11180dc2366fSVenugopal Iyer 	}
11190dc2366fSVenugopal Iyer }
11200dc2366fSVenugopal Iyer 
11210dc2366fSVenugopal Iyer /*
11220dc2366fSVenugopal Iyer  * Clients like aggr create pseudo rings (mac_ring_t) and expose them to
11230dc2366fSVenugopal Iyer  * their clients. There is a 1-1 mapping pseudo ring and the hardware
11240dc2366fSVenugopal Iyer  * ring. ddi interrupt handles are exported from the hardware ring to
11250dc2366fSVenugopal Iyer  * the pseudo ring. Thus when the interrupt handle changes, clients of
11260dc2366fSVenugopal Iyer  * aggr that are using the handle need to use the new handle and
11270dc2366fSVenugopal Iyer  * re-target their interrupts.
11280dc2366fSVenugopal Iyer  */
11290dc2366fSVenugopal Iyer static void
11300dc2366fSVenugopal Iyer mac_pseudo_ring_intr_retarget(mac_impl_t *mip, mac_ring_t *ring,
11310dc2366fSVenugopal Iyer     ddi_intr_handle_t ddh)
11320dc2366fSVenugopal Iyer {
11330dc2366fSVenugopal Iyer 	mac_ring_t *pring;
11340dc2366fSVenugopal Iyer 	mac_group_t *pgroup;
11350dc2366fSVenugopal Iyer 	mac_impl_t *pmip;
11360dc2366fSVenugopal Iyer 	char macname[MAXNAMELEN];
11370dc2366fSVenugopal Iyer 	mac_perim_handle_t p_mph;
11380dc2366fSVenugopal Iyer 	uint64_t saved_gen_num;
11390dc2366fSVenugopal Iyer 
11400dc2366fSVenugopal Iyer again:
11410dc2366fSVenugopal Iyer 	pring = (mac_ring_t *)ring->mr_prh;
11420dc2366fSVenugopal Iyer 	pgroup = (mac_group_t *)pring->mr_gh;
11430dc2366fSVenugopal Iyer 	pmip = (mac_impl_t *)pgroup->mrg_mh;
11440dc2366fSVenugopal Iyer 	saved_gen_num = ring->mr_gen_num;
11450dc2366fSVenugopal Iyer 	(void) strlcpy(macname, pmip->mi_name, MAXNAMELEN);
11460dc2366fSVenugopal Iyer 	/*
11470dc2366fSVenugopal Iyer 	 * We need to enter aggr's perimeter. The locking hierarchy
11480dc2366fSVenugopal Iyer 	 * dictates that aggr's perimeter should be entered first
11490dc2366fSVenugopal Iyer 	 * and then the port's perimeter. So drop the port's
11500dc2366fSVenugopal Iyer 	 * perimeter, enter aggr's and then re-enter port's
11510dc2366fSVenugopal Iyer 	 * perimeter.
11520dc2366fSVenugopal Iyer 	 */
11530dc2366fSVenugopal Iyer 	i_mac_perim_exit(mip);
11540dc2366fSVenugopal Iyer 	/*
11550dc2366fSVenugopal Iyer 	 * While we know pmip is the aggr's mip, there is a
11560dc2366fSVenugopal Iyer 	 * possibility that aggr could have unregistered by
11570dc2366fSVenugopal Iyer 	 * the time we exit port's perimeter (mip) and
11580dc2366fSVenugopal Iyer 	 * enter aggr's perimeter (pmip). To avoid that
11590dc2366fSVenugopal Iyer 	 * scenario, enter aggr's perimeter using its name.
11600dc2366fSVenugopal Iyer 	 */
11610dc2366fSVenugopal Iyer 	if (mac_perim_enter_by_macname(macname, &p_mph) != 0)
11620dc2366fSVenugopal Iyer 		return;
11630dc2366fSVenugopal Iyer 	i_mac_perim_enter(mip);
11640dc2366fSVenugopal Iyer 	/*
11650dc2366fSVenugopal Iyer 	 * Check if the ring got assigned to another aggregation before
11660dc2366fSVenugopal Iyer 	 * be could enter aggr's and the port's perimeter. When a ring
11670dc2366fSVenugopal Iyer 	 * gets deleted from an aggregation, it calls mac_stop_ring()
11680dc2366fSVenugopal Iyer 	 * which increments the generation number. So checking
11690dc2366fSVenugopal Iyer 	 * generation number will be enough.
11700dc2366fSVenugopal Iyer 	 */
11710dc2366fSVenugopal Iyer 	if (ring->mr_gen_num != saved_gen_num && ring->mr_prh != NULL) {
11720dc2366fSVenugopal Iyer 		i_mac_perim_exit(mip);
11730dc2366fSVenugopal Iyer 		mac_perim_exit(p_mph);
11740dc2366fSVenugopal Iyer 		i_mac_perim_enter(mip);
11750dc2366fSVenugopal Iyer 		goto again;
11760dc2366fSVenugopal Iyer 	}
11770dc2366fSVenugopal Iyer 
11780dc2366fSVenugopal Iyer 	/* Check if pseudo ring is still present */
11790dc2366fSVenugopal Iyer 	if (ring->mr_prh != NULL) {
11800dc2366fSVenugopal Iyer 		pring->mr_info.mri_intr.mi_ddi_handle = ddh;
11810dc2366fSVenugopal Iyer 		pring->mr_info.mri_intr.mi_ddi_shared =
11820dc2366fSVenugopal Iyer 		    ring->mr_info.mri_intr.mi_ddi_shared;
11830dc2366fSVenugopal Iyer 		if (ddh != NULL)
11840dc2366fSVenugopal Iyer 			mac_ring_intr_retarget(pgroup, pring);
11850dc2366fSVenugopal Iyer 	}
11860dc2366fSVenugopal Iyer 	i_mac_perim_exit(mip);
11870dc2366fSVenugopal Iyer 	mac_perim_exit(p_mph);
11880dc2366fSVenugopal Iyer }
11890dc2366fSVenugopal Iyer /*
11900dc2366fSVenugopal Iyer  * API called by driver to provide new interrupt handle for TX/RX rings.
11910dc2366fSVenugopal Iyer  * This usually happens when IRM (Interrupt Resource Manangement)
11920dc2366fSVenugopal Iyer  * framework either gives the driver more MSI-x interrupts or takes
11930dc2366fSVenugopal Iyer  * away MSI-x interrupts from the driver.
11940dc2366fSVenugopal Iyer  */
11950dc2366fSVenugopal Iyer void
11960dc2366fSVenugopal Iyer mac_ring_intr_set(mac_ring_handle_t mrh, ddi_intr_handle_t ddh)
11970dc2366fSVenugopal Iyer {
11980dc2366fSVenugopal Iyer 	mac_ring_t	*ring = (mac_ring_t *)mrh;
11990dc2366fSVenugopal Iyer 	mac_group_t	*group = (mac_group_t *)ring->mr_gh;
12000dc2366fSVenugopal Iyer 	mac_impl_t	*mip = (mac_impl_t *)group->mrg_mh;
12010dc2366fSVenugopal Iyer 
12020dc2366fSVenugopal Iyer 	i_mac_perim_enter(mip);
12030dc2366fSVenugopal Iyer 	ring->mr_info.mri_intr.mi_ddi_handle = ddh;
12040dc2366fSVenugopal Iyer 	if (ddh == NULL) {
12050dc2366fSVenugopal Iyer 		/* Interrupts being reset */
12060dc2366fSVenugopal Iyer 		ring->mr_info.mri_intr.mi_ddi_shared = B_FALSE;
12070dc2366fSVenugopal Iyer 		if (ring->mr_prh != NULL) {
12080dc2366fSVenugopal Iyer 			mac_pseudo_ring_intr_retarget(mip, ring, ddh);
12090dc2366fSVenugopal Iyer 			return;
12100dc2366fSVenugopal Iyer 		}
12110dc2366fSVenugopal Iyer 	} else {
12120dc2366fSVenugopal Iyer 		/* New interrupt handle */
12130dc2366fSVenugopal Iyer 		mac_compare_ddi_handle(mip->mi_rx_groups,
12140dc2366fSVenugopal Iyer 		    mip->mi_rx_group_count, ring);
12150dc2366fSVenugopal Iyer 		if (!ring->mr_info.mri_intr.mi_ddi_shared) {
12160dc2366fSVenugopal Iyer 			mac_compare_ddi_handle(mip->mi_tx_groups,
12170dc2366fSVenugopal Iyer 			    mip->mi_tx_group_count, ring);
12180dc2366fSVenugopal Iyer 		}
12190dc2366fSVenugopal Iyer 		if (ring->mr_prh != NULL) {
12200dc2366fSVenugopal Iyer 			mac_pseudo_ring_intr_retarget(mip, ring, ddh);
12210dc2366fSVenugopal Iyer 			return;
12220dc2366fSVenugopal Iyer 		} else {
12230dc2366fSVenugopal Iyer 			mac_ring_intr_retarget(group, ring);
12240dc2366fSVenugopal Iyer 		}
12250dc2366fSVenugopal Iyer 	}
12260dc2366fSVenugopal Iyer 	i_mac_perim_exit(mip);
12270dc2366fSVenugopal Iyer }
12280dc2366fSVenugopal Iyer 
1229da14cebeSEric Cheng /* PRIVATE FUNCTIONS, FOR INTERNAL USE ONLY */
1230da14cebeSEric Cheng 
1231da14cebeSEric Cheng /*
1232da14cebeSEric Cheng  * Updates the mac_impl structure with the current state of the link
1233da14cebeSEric Cheng  */
1234da14cebeSEric Cheng static void
1235da14cebeSEric Cheng i_mac_log_link_state(mac_impl_t *mip)
1236da14cebeSEric Cheng {
1237da14cebeSEric Cheng 	/*
1238da14cebeSEric Cheng 	 * If no change, then it is not interesting.
1239da14cebeSEric Cheng 	 */
12404eaa4710SRishi Srivatsavai 	if (mip->mi_lastlowlinkstate == mip->mi_lowlinkstate)
1241da14cebeSEric Cheng 		return;
1242da14cebeSEric Cheng 
12434eaa4710SRishi Srivatsavai 	switch (mip->mi_lowlinkstate) {
1244da14cebeSEric Cheng 	case LINK_STATE_UP:
1245da14cebeSEric Cheng 		if (mip->mi_type->mt_ops.mtops_ops & MTOPS_LINK_DETAILS) {
1246da14cebeSEric Cheng 			char det[200];
1247da14cebeSEric Cheng 
1248da14cebeSEric Cheng 			mip->mi_type->mt_ops.mtops_link_details(det,
1249da14cebeSEric Cheng 			    sizeof (det), (mac_handle_t)mip, mip->mi_pdata);
1250da14cebeSEric Cheng 
1251da14cebeSEric Cheng 			cmn_err(CE_NOTE, "!%s link up, %s", mip->mi_name, det);
1252da14cebeSEric Cheng 		} else {
1253da14cebeSEric Cheng 			cmn_err(CE_NOTE, "!%s link up", mip->mi_name);
1254da14cebeSEric Cheng 		}
1255da14cebeSEric Cheng 		break;
1256da14cebeSEric Cheng 
1257da14cebeSEric Cheng 	case LINK_STATE_DOWN:
1258da14cebeSEric Cheng 		/*
1259da14cebeSEric Cheng 		 * Only transitions from UP to DOWN are interesting
1260da14cebeSEric Cheng 		 */
12614eaa4710SRishi Srivatsavai 		if (mip->mi_lastlowlinkstate != LINK_STATE_UNKNOWN)
1262da14cebeSEric Cheng 			cmn_err(CE_NOTE, "!%s link down", mip->mi_name);
1263da14cebeSEric Cheng 		break;
1264da14cebeSEric Cheng 
1265da14cebeSEric Cheng 	case LINK_STATE_UNKNOWN:
1266da14cebeSEric Cheng 		/*
1267da14cebeSEric Cheng 		 * This case is normally not interesting.
1268da14cebeSEric Cheng 		 */
1269da14cebeSEric Cheng 		break;
1270da14cebeSEric Cheng 	}
12714eaa4710SRishi Srivatsavai 	mip->mi_lastlowlinkstate = mip->mi_lowlinkstate;
1272da14cebeSEric Cheng }
1273da14cebeSEric Cheng 
1274da14cebeSEric Cheng /*
1275da14cebeSEric Cheng  * Main routine for the callbacks notifications thread
1276da14cebeSEric Cheng  */
1277da14cebeSEric Cheng static void
1278da14cebeSEric Cheng i_mac_notify_thread(void *arg)
1279da14cebeSEric Cheng {
1280da14cebeSEric Cheng 	mac_impl_t	*mip = arg;
1281da14cebeSEric Cheng 	callb_cpr_t	cprinfo;
1282da14cebeSEric Cheng 	mac_cb_t	*mcb;
1283da14cebeSEric Cheng 	mac_cb_info_t	*mcbi;
1284da14cebeSEric Cheng 	mac_notify_cb_t	*mncb;
1285da14cebeSEric Cheng 
1286da14cebeSEric Cheng 	mcbi = &mip->mi_notify_cb_info;
1287da14cebeSEric Cheng 	CALLB_CPR_INIT(&cprinfo, mcbi->mcbi_lockp, callb_generic_cpr,
1288da14cebeSEric Cheng 	    "i_mac_notify_thread");
1289da14cebeSEric Cheng 
1290da14cebeSEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
1291da14cebeSEric Cheng 
1292da14cebeSEric Cheng 	for (;;) {
1293da14cebeSEric Cheng 		uint32_t	bits;
1294da14cebeSEric Cheng 		uint32_t	type;
1295da14cebeSEric Cheng 
1296da14cebeSEric Cheng 		bits = mip->mi_notify_bits;
1297da14cebeSEric Cheng 		if (bits == 0) {
1298da14cebeSEric Cheng 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
1299da14cebeSEric Cheng 			cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
1300da14cebeSEric Cheng 			CALLB_CPR_SAFE_END(&cprinfo, mcbi->mcbi_lockp);
1301da14cebeSEric Cheng 			continue;
1302da14cebeSEric Cheng 		}
1303da14cebeSEric Cheng 		mip->mi_notify_bits = 0;
1304da14cebeSEric Cheng 		if ((bits & (1 << MAC_NNOTE)) != 0) {
1305da14cebeSEric Cheng 			/* request to quit */
1306da14cebeSEric Cheng 			ASSERT(mip->mi_state_flags & MIS_DISABLED);
1307da14cebeSEric Cheng 			break;
1308da14cebeSEric Cheng 		}
1309da14cebeSEric Cheng 
1310da14cebeSEric Cheng 		mutex_exit(mcbi->mcbi_lockp);
1311da14cebeSEric Cheng 
1312da14cebeSEric Cheng 		/*
13134eaa4710SRishi Srivatsavai 		 * Log link changes on the actual link, but then do reports on
13144eaa4710SRishi Srivatsavai 		 * synthetic state (if part of a bridge).
1315da14cebeSEric Cheng 		 */
13164eaa4710SRishi Srivatsavai 		if ((bits & (1 << MAC_NOTE_LOWLINK)) != 0) {
13174eaa4710SRishi Srivatsavai 			link_state_t newstate;
13184eaa4710SRishi Srivatsavai 			mac_handle_t mh;
13194eaa4710SRishi Srivatsavai 
1320da14cebeSEric Cheng 			i_mac_log_link_state(mip);
13214eaa4710SRishi Srivatsavai 			newstate = mip->mi_lowlinkstate;
13224eaa4710SRishi Srivatsavai 			if (mip->mi_bridge_link != NULL) {
13234eaa4710SRishi Srivatsavai 				mutex_enter(&mip->mi_bridge_lock);
13244eaa4710SRishi Srivatsavai 				if ((mh = mip->mi_bridge_link) != NULL) {
13254eaa4710SRishi Srivatsavai 					newstate = mac_bridge_ls_cb(mh,
13264eaa4710SRishi Srivatsavai 					    newstate);
13274eaa4710SRishi Srivatsavai 				}
13284eaa4710SRishi Srivatsavai 				mutex_exit(&mip->mi_bridge_lock);
13294eaa4710SRishi Srivatsavai 			}
13304eaa4710SRishi Srivatsavai 			if (newstate != mip->mi_linkstate) {
13314eaa4710SRishi Srivatsavai 				mip->mi_linkstate = newstate;
13324eaa4710SRishi Srivatsavai 				bits |= 1 << MAC_NOTE_LINK;
13334eaa4710SRishi Srivatsavai 			}
13344eaa4710SRishi Srivatsavai 		}
1335da14cebeSEric Cheng 
1336c61a1653SRyan Zezeski 		/*
1337c61a1653SRyan Zezeski 		 * Depending on which capabs have changed, the Tx
1338c61a1653SRyan Zezeski 		 * checksum flags may also need to be updated.
1339c61a1653SRyan Zezeski 		 */
1340c61a1653SRyan Zezeski 		if ((bits & (1 << MAC_NOTE_CAPAB_CHG)) != 0) {
1341c61a1653SRyan Zezeski 			mac_perim_handle_t mph;
1342c61a1653SRyan Zezeski 			mac_handle_t mh = (mac_handle_t)mip;
1343c61a1653SRyan Zezeski 
1344c61a1653SRyan Zezeski 			mac_perim_enter_by_mh(mh, &mph);
1345c61a1653SRyan Zezeski 			mip->mi_tx_cksum_flags = mac_features_to_flags(mh);
1346c61a1653SRyan Zezeski 			mac_perim_exit(mph);
1347c61a1653SRyan Zezeski 		}
1348c61a1653SRyan Zezeski 
1349da14cebeSEric Cheng 		/*
1350da14cebeSEric Cheng 		 * Do notification callbacks for each notification type.
1351da14cebeSEric Cheng 		 */
1352da14cebeSEric Cheng 		for (type = 0; type < MAC_NNOTE; type++) {
1353da14cebeSEric Cheng 			if ((bits & (1 << type)) == 0) {
1354da14cebeSEric Cheng 				continue;
1355da14cebeSEric Cheng 			}
1356da14cebeSEric Cheng 
13574eaa4710SRishi Srivatsavai 			if (mac_notify_cb_list[type] != NULL)
13584eaa4710SRishi Srivatsavai 				(*mac_notify_cb_list[type])(mip);
1359da14cebeSEric Cheng 
1360da14cebeSEric Cheng 			/*
1361da14cebeSEric Cheng 			 * Walk the list of notifications.
1362da14cebeSEric Cheng 			 */
1363da14cebeSEric Cheng 			MAC_CALLBACK_WALKER_INC(&mip->mi_notify_cb_info);
1364da14cebeSEric Cheng 			for (mcb = mip->mi_notify_cb_list; mcb != NULL;
1365da14cebeSEric Cheng 			    mcb = mcb->mcb_nextp) {
1366da14cebeSEric Cheng 				mncb = (mac_notify_cb_t *)mcb->mcb_objp;
1367da14cebeSEric Cheng 				mncb->mncb_fn(mncb->mncb_arg, type);
1368da14cebeSEric Cheng 			}
1369da14cebeSEric Cheng 			MAC_CALLBACK_WALKER_DCR(&mip->mi_notify_cb_info,
1370da14cebeSEric Cheng 			    &mip->mi_notify_cb_list);
1371da14cebeSEric Cheng 		}
1372da14cebeSEric Cheng 
1373da14cebeSEric Cheng 		mutex_enter(mcbi->mcbi_lockp);
1374da14cebeSEric Cheng 	}
1375da14cebeSEric Cheng 
1376da14cebeSEric Cheng 	mip->mi_state_flags |= MIS_NOTIFY_DONE;
1377da14cebeSEric Cheng 	cv_broadcast(&mcbi->mcbi_cv);
1378da14cebeSEric Cheng 
1379da14cebeSEric Cheng 	/* CALLB_CPR_EXIT drops the lock */
1380da14cebeSEric Cheng 	CALLB_CPR_EXIT(&cprinfo);
1381da14cebeSEric Cheng 	thread_exit();
1382da14cebeSEric Cheng }
1383da14cebeSEric Cheng 
1384da14cebeSEric Cheng /*
1385da14cebeSEric Cheng  * Signal the i_mac_notify_thread asking it to quit.
1386da14cebeSEric Cheng  * Then wait till it is done.
1387da14cebeSEric Cheng  */
1388da14cebeSEric Cheng void
1389da14cebeSEric Cheng i_mac_notify_exit(mac_impl_t *mip)
1390da14cebeSEric Cheng {
1391da14cebeSEric Cheng 	mac_cb_info_t	*mcbi;
1392da14cebeSEric Cheng 
1393da14cebeSEric Cheng 	mcbi = &mip->mi_notify_cb_info;
1394da14cebeSEric Cheng 
1395da14cebeSEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
1396da14cebeSEric Cheng 	mip->mi_notify_bits = (1 << MAC_NNOTE);
1397da14cebeSEric Cheng 	cv_broadcast(&mcbi->mcbi_cv);
1398da14cebeSEric Cheng 
1399da14cebeSEric Cheng 
1400da14cebeSEric Cheng 	while ((mip->mi_notify_thread != NULL) &&
1401da14cebeSEric Cheng 	    !(mip->mi_state_flags & MIS_NOTIFY_DONE)) {
1402da14cebeSEric Cheng 		cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
1403da14cebeSEric Cheng 	}
1404da14cebeSEric Cheng 
1405da14cebeSEric Cheng 	/* Necessary clean up before doing kmem_cache_free */
1406da14cebeSEric Cheng 	mip->mi_state_flags &= ~MIS_NOTIFY_DONE;
1407da14cebeSEric Cheng 	mip->mi_notify_bits = 0;
1408da14cebeSEric Cheng 	mip->mi_notify_thread = NULL;
1409da14cebeSEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
1410da14cebeSEric Cheng }
1411da14cebeSEric Cheng 
1412da14cebeSEric Cheng /*
1413da14cebeSEric Cheng  * Entry point invoked by drivers to dynamically add a ring to an
1414da14cebeSEric Cheng  * existing group.
1415da14cebeSEric Cheng  */
1416da14cebeSEric Cheng int
1417da14cebeSEric Cheng mac_group_add_ring(mac_group_handle_t gh, int index)
1418da14cebeSEric Cheng {
1419da14cebeSEric Cheng 	mac_group_t *group = (mac_group_t *)gh;
1420da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
1421da14cebeSEric Cheng 	int ret;
1422da14cebeSEric Cheng 
1423da14cebeSEric Cheng 	i_mac_perim_enter(mip);
1424da14cebeSEric Cheng 	ret = i_mac_group_add_ring(group, NULL, index);
1425da14cebeSEric Cheng 	i_mac_perim_exit(mip);
1426da14cebeSEric Cheng 	return (ret);
1427da14cebeSEric Cheng }
1428da14cebeSEric Cheng 
1429da14cebeSEric Cheng /*
1430da14cebeSEric Cheng  * Entry point invoked by drivers to dynamically remove a ring
1431da14cebeSEric Cheng  * from an existing group. The specified ring handle must no longer
1432da14cebeSEric Cheng  * be used by the driver after a call to this function.
1433da14cebeSEric Cheng  */
1434da14cebeSEric Cheng void
1435da14cebeSEric Cheng mac_group_rem_ring(mac_group_handle_t gh, mac_ring_handle_t rh)
1436da14cebeSEric Cheng {
1437da14cebeSEric Cheng 	mac_group_t *group = (mac_group_t *)gh;
1438da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
1439da14cebeSEric Cheng 
1440da14cebeSEric Cheng 	i_mac_perim_enter(mip);
14410dc2366fSVenugopal Iyer 	i_mac_group_rem_ring(group, (mac_ring_t *)rh, B_TRUE);
14420dc2366fSVenugopal Iyer 	i_mac_perim_exit(mip);
14430dc2366fSVenugopal Iyer }
1444da14cebeSEric Cheng 
14450dc2366fSVenugopal Iyer /*
14460dc2366fSVenugopal Iyer  * mac_prop_info_*() callbacks called from the driver's prefix_propinfo()
14470dc2366fSVenugopal Iyer  * entry points.
14480dc2366fSVenugopal Iyer  */
1449da14cebeSEric Cheng 
14500dc2366fSVenugopal Iyer void
14510dc2366fSVenugopal Iyer mac_prop_info_set_default_uint8(mac_prop_info_handle_t ph, uint8_t val)
14520dc2366fSVenugopal Iyer {
14530dc2366fSVenugopal Iyer 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
1454da14cebeSEric Cheng 
14550dc2366fSVenugopal Iyer 	/* nothing to do if the caller doesn't want the default value */
14560dc2366fSVenugopal Iyer 	if (pr->pr_default == NULL)
14570dc2366fSVenugopal Iyer 		return;
14580dc2366fSVenugopal Iyer 
14590dc2366fSVenugopal Iyer 	ASSERT(pr->pr_default_size >= sizeof (uint8_t));
14600dc2366fSVenugopal Iyer 
14610dc2366fSVenugopal Iyer 	*(uint8_t *)(pr->pr_default) = val;
14620dc2366fSVenugopal Iyer 	pr->pr_flags |= MAC_PROP_INFO_DEFAULT;
14630dc2366fSVenugopal Iyer }
14640dc2366fSVenugopal Iyer 
14650dc2366fSVenugopal Iyer void
14660dc2366fSVenugopal Iyer mac_prop_info_set_default_uint64(mac_prop_info_handle_t ph, uint64_t val)
14670dc2366fSVenugopal Iyer {
14680dc2366fSVenugopal Iyer 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
14690dc2366fSVenugopal Iyer 
14700dc2366fSVenugopal Iyer 	/* nothing to do if the caller doesn't want the default value */
14710dc2366fSVenugopal Iyer 	if (pr->pr_default == NULL)
14720dc2366fSVenugopal Iyer 		return;
14730dc2366fSVenugopal Iyer 
14740dc2366fSVenugopal Iyer 	ASSERT(pr->pr_default_size >= sizeof (uint64_t));
14750dc2366fSVenugopal Iyer 
14760dc2366fSVenugopal Iyer 	bcopy(&val, pr->pr_default, sizeof (val));
14770dc2366fSVenugopal Iyer 
14780dc2366fSVenugopal Iyer 	pr->pr_flags |= MAC_PROP_INFO_DEFAULT;
14790dc2366fSVenugopal Iyer }
14800dc2366fSVenugopal Iyer 
14810dc2366fSVenugopal Iyer void
14820dc2366fSVenugopal Iyer mac_prop_info_set_default_uint32(mac_prop_info_handle_t ph, uint32_t val)
14830dc2366fSVenugopal Iyer {
14840dc2366fSVenugopal Iyer 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
14850dc2366fSVenugopal Iyer 
14860dc2366fSVenugopal Iyer 	/* nothing to do if the caller doesn't want the default value */
14870dc2366fSVenugopal Iyer 	if (pr->pr_default == NULL)
14880dc2366fSVenugopal Iyer 		return;
14890dc2366fSVenugopal Iyer 
14900dc2366fSVenugopal Iyer 	ASSERT(pr->pr_default_size >= sizeof (uint32_t));
14910dc2366fSVenugopal Iyer 
14920dc2366fSVenugopal Iyer 	bcopy(&val, pr->pr_default, sizeof (val));
14930dc2366fSVenugopal Iyer 
14940dc2366fSVenugopal Iyer 	pr->pr_flags |= MAC_PROP_INFO_DEFAULT;
14950dc2366fSVenugopal Iyer }
14960dc2366fSVenugopal Iyer 
14970dc2366fSVenugopal Iyer void
14980dc2366fSVenugopal Iyer mac_prop_info_set_default_str(mac_prop_info_handle_t ph, const char *str)
14990dc2366fSVenugopal Iyer {
15000dc2366fSVenugopal Iyer 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
15010dc2366fSVenugopal Iyer 
15020dc2366fSVenugopal Iyer 	/* nothing to do if the caller doesn't want the default value */
15030dc2366fSVenugopal Iyer 	if (pr->pr_default == NULL)
15040dc2366fSVenugopal Iyer 		return;
15050dc2366fSVenugopal Iyer 
1506f689bed1SRishi Srivatsavai 	if (strlen(str) >= pr->pr_default_size)
15070591ddd0SPrakash Jalan 		pr->pr_errno = ENOBUFS;
15080dc2366fSVenugopal Iyer 	else
1509f689bed1SRishi Srivatsavai 		(void) strlcpy(pr->pr_default, str, pr->pr_default_size);
15100dc2366fSVenugopal Iyer 	pr->pr_flags |= MAC_PROP_INFO_DEFAULT;
15110dc2366fSVenugopal Iyer }
15120dc2366fSVenugopal Iyer 
15130dc2366fSVenugopal Iyer void
15140dc2366fSVenugopal Iyer mac_prop_info_set_default_link_flowctrl(mac_prop_info_handle_t ph,
15150dc2366fSVenugopal Iyer     link_flowctrl_t val)
15160dc2366fSVenugopal Iyer {
15170dc2366fSVenugopal Iyer 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
15180dc2366fSVenugopal Iyer 
15190dc2366fSVenugopal Iyer 	/* nothing to do if the caller doesn't want the default value */
15200dc2366fSVenugopal Iyer 	if (pr->pr_default == NULL)
15210dc2366fSVenugopal Iyer 		return;
15220dc2366fSVenugopal Iyer 
15230dc2366fSVenugopal Iyer 	ASSERT(pr->pr_default_size >= sizeof (link_flowctrl_t));
15240dc2366fSVenugopal Iyer 
15250dc2366fSVenugopal Iyer 	bcopy(&val, pr->pr_default, sizeof (val));
15260dc2366fSVenugopal Iyer 
15270dc2366fSVenugopal Iyer 	pr->pr_flags |= MAC_PROP_INFO_DEFAULT;
15280dc2366fSVenugopal Iyer }
15290dc2366fSVenugopal Iyer 
1530*d77e6e0fSPaul Winder void
1531*d77e6e0fSPaul Winder mac_prop_info_set_default_fec(mac_prop_info_handle_t ph, link_fec_t val)
1532*d77e6e0fSPaul Winder {
1533*d77e6e0fSPaul Winder 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
1534*d77e6e0fSPaul Winder 
1535*d77e6e0fSPaul Winder 	/* nothing to do if the caller doesn't want the default value */
1536*d77e6e0fSPaul Winder 	if (pr->pr_default == NULL)
1537*d77e6e0fSPaul Winder 		return;
1538*d77e6e0fSPaul Winder 
1539*d77e6e0fSPaul Winder 	ASSERT(pr->pr_default_size >= sizeof (link_fec_t));
1540*d77e6e0fSPaul Winder 
1541*d77e6e0fSPaul Winder 	bcopy(&val, pr->pr_default, sizeof (val));
1542*d77e6e0fSPaul Winder 
1543*d77e6e0fSPaul Winder 	pr->pr_flags |= MAC_PROP_INFO_DEFAULT;
1544*d77e6e0fSPaul Winder }
1545*d77e6e0fSPaul Winder 
15460dc2366fSVenugopal Iyer void
15470dc2366fSVenugopal Iyer mac_prop_info_set_range_uint32(mac_prop_info_handle_t ph, uint32_t min,
15480dc2366fSVenugopal Iyer     uint32_t max)
15490dc2366fSVenugopal Iyer {
15500dc2366fSVenugopal Iyer 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
15510dc2366fSVenugopal Iyer 	mac_propval_range_t *range = pr->pr_range;
15520591ddd0SPrakash Jalan 	mac_propval_uint32_range_t *range32;
15530dc2366fSVenugopal Iyer 
15540dc2366fSVenugopal Iyer 	/* nothing to do if the caller doesn't want the range info */
15550dc2366fSVenugopal Iyer 	if (range == NULL)
15560dc2366fSVenugopal Iyer 		return;
15570dc2366fSVenugopal Iyer 
15580591ddd0SPrakash Jalan 	if (pr->pr_range_cur_count++ == 0) {
15590591ddd0SPrakash Jalan 		/* first range */
15600591ddd0SPrakash Jalan 		pr->pr_flags |= MAC_PROP_INFO_RANGE;
15610591ddd0SPrakash Jalan 		range->mpr_type = MAC_PROPVAL_UINT32;
15620591ddd0SPrakash Jalan 	} else {
15630591ddd0SPrakash Jalan 		/* all ranges of a property should be of the same type */
15640591ddd0SPrakash Jalan 		ASSERT(range->mpr_type == MAC_PROPVAL_UINT32);
15650591ddd0SPrakash Jalan 		if (pr->pr_range_cur_count > range->mpr_count) {
15660591ddd0SPrakash Jalan 			pr->pr_errno = ENOSPC;
15670591ddd0SPrakash Jalan 			return;
15680591ddd0SPrakash Jalan 		}
15690591ddd0SPrakash Jalan 	}
15700591ddd0SPrakash Jalan 
15710591ddd0SPrakash Jalan 	range32 = range->mpr_range_uint32;
15720591ddd0SPrakash Jalan 	range32[pr->pr_range_cur_count - 1].mpur_min = min;
15730591ddd0SPrakash Jalan 	range32[pr->pr_range_cur_count - 1].mpur_max = max;
15740dc2366fSVenugopal Iyer }
15750dc2366fSVenugopal Iyer 
15760dc2366fSVenugopal Iyer void
15770dc2366fSVenugopal Iyer mac_prop_info_set_perm(mac_prop_info_handle_t ph, uint8_t perm)
15780dc2366fSVenugopal Iyer {
15790dc2366fSVenugopal Iyer 	mac_prop_info_state_t *pr = (mac_prop_info_state_t *)ph;
15800dc2366fSVenugopal Iyer 
15810dc2366fSVenugopal Iyer 	pr->pr_perm = perm;
15820dc2366fSVenugopal Iyer 	pr->pr_flags |= MAC_PROP_INFO_PERM;
15830dc2366fSVenugopal Iyer }
15840dc2366fSVenugopal Iyer 
1585ec71f88eSPatrick Mooney void
1586ec71f88eSPatrick Mooney mac_hcksum_get(const mblk_t *mp, uint32_t *start, uint32_t *stuff,
15870dc2366fSVenugopal Iyer     uint32_t *end, uint32_t *value, uint32_t *flags_ptr)
15880dc2366fSVenugopal Iyer {
15890dc2366fSVenugopal Iyer 	uint32_t flags;
15900dc2366fSVenugopal Iyer 
15910dc2366fSVenugopal Iyer 	ASSERT(DB_TYPE(mp) == M_DATA);
15920dc2366fSVenugopal Iyer 
15930dc2366fSVenugopal Iyer 	flags = DB_CKSUMFLAGS(mp) & HCK_FLAGS;
15940dc2366fSVenugopal Iyer 	if ((flags & (HCK_PARTIALCKSUM | HCK_FULLCKSUM)) != 0) {
15950dc2366fSVenugopal Iyer 		if (value != NULL)
15960dc2366fSVenugopal Iyer 			*value = (uint32_t)DB_CKSUM16(mp);
15970dc2366fSVenugopal Iyer 		if ((flags & HCK_PARTIALCKSUM) != 0) {
15980dc2366fSVenugopal Iyer 			if (start != NULL)
15990dc2366fSVenugopal Iyer 				*start = (uint32_t)DB_CKSUMSTART(mp);
16000dc2366fSVenugopal Iyer 			if (stuff != NULL)
16010dc2366fSVenugopal Iyer 				*stuff = (uint32_t)DB_CKSUMSTUFF(mp);
16020dc2366fSVenugopal Iyer 			if (end != NULL)
16030dc2366fSVenugopal Iyer 				*end = (uint32_t)DB_CKSUMEND(mp);
16040dc2366fSVenugopal Iyer 		}
16050dc2366fSVenugopal Iyer 	}
16060dc2366fSVenugopal Iyer 
16070dc2366fSVenugopal Iyer 	if (flags_ptr != NULL)
16080dc2366fSVenugopal Iyer 		*flags_ptr = flags;
16090dc2366fSVenugopal Iyer }
16100dc2366fSVenugopal Iyer 
1611ec71f88eSPatrick Mooney void
1612ec71f88eSPatrick Mooney mac_hcksum_set(mblk_t *mp, uint32_t start, uint32_t stuff, uint32_t end,
1613ec71f88eSPatrick Mooney     uint32_t value, uint32_t flags)
16140dc2366fSVenugopal Iyer {
16150dc2366fSVenugopal Iyer 	ASSERT(DB_TYPE(mp) == M_DATA);
16160dc2366fSVenugopal Iyer 
16170dc2366fSVenugopal Iyer 	DB_CKSUMSTART(mp) = (intptr_t)start;
16180dc2366fSVenugopal Iyer 	DB_CKSUMSTUFF(mp) = (intptr_t)stuff;
16190dc2366fSVenugopal Iyer 	DB_CKSUMEND(mp) = (intptr_t)end;
16200dc2366fSVenugopal Iyer 	DB_CKSUMFLAGS(mp) = (uint16_t)flags;
16210dc2366fSVenugopal Iyer 	DB_CKSUM16(mp) = (uint16_t)value;
16220dc2366fSVenugopal Iyer }
16230dc2366fSVenugopal Iyer 
1624ec71f88eSPatrick Mooney void
1625ec71f88eSPatrick Mooney mac_hcksum_clone(const mblk_t *src, mblk_t *dst)
1626ec71f88eSPatrick Mooney {
1627ec71f88eSPatrick Mooney 	ASSERT3U(DB_TYPE(src), ==, M_DATA);
1628ec71f88eSPatrick Mooney 	ASSERT3U(DB_TYPE(dst), ==, M_DATA);
1629ec71f88eSPatrick Mooney 
1630ec71f88eSPatrick Mooney 	/*
1631c61a1653SRyan Zezeski 	 * Do these assignments unconditionally, rather than only when
1632c61a1653SRyan Zezeski 	 * flags is non-zero. This protects a situation where zeroed
1633c61a1653SRyan Zezeski 	 * hcksum data does not make the jump onto an mblk_t with
1634c61a1653SRyan Zezeski 	 * stale data in those fields. It's important to copy all
1635c61a1653SRyan Zezeski 	 * possible flags (HCK_* as well as HW_*) and not just the
1636c61a1653SRyan Zezeski 	 * checksum specific flags. Dropping flags during a clone
1637c61a1653SRyan Zezeski 	 * could result in dropped packets. If the caller has good
1638c61a1653SRyan Zezeski 	 * reason to drop those flags then it should do it manually,
1639c61a1653SRyan Zezeski 	 * after the clone.
1640ec71f88eSPatrick Mooney 	 */
1641c61a1653SRyan Zezeski 	DB_CKSUMFLAGS(dst) = DB_CKSUMFLAGS(src);
1642ec71f88eSPatrick Mooney 	DB_CKSUMSTART(dst) = DB_CKSUMSTART(src);
1643ec71f88eSPatrick Mooney 	DB_CKSUMSTUFF(dst) = DB_CKSUMSTUFF(src);
1644ec71f88eSPatrick Mooney 	DB_CKSUMEND(dst) = DB_CKSUMEND(src);
1645ec71f88eSPatrick Mooney 	DB_CKSUM16(dst) = DB_CKSUM16(src);
1646c61a1653SRyan Zezeski 	DB_LSOMSS(dst) = DB_LSOMSS(src);
1647ec71f88eSPatrick Mooney }
1648ec71f88eSPatrick Mooney 
16490dc2366fSVenugopal Iyer void
16500dc2366fSVenugopal Iyer mac_lso_get(mblk_t *mp, uint32_t *mss, uint32_t *flags)
16510dc2366fSVenugopal Iyer {
16520dc2366fSVenugopal Iyer 	ASSERT(DB_TYPE(mp) == M_DATA);
16530dc2366fSVenugopal Iyer 
16540dc2366fSVenugopal Iyer 	if (flags != NULL) {
16550dc2366fSVenugopal Iyer 		*flags = DB_CKSUMFLAGS(mp) & HW_LSO;
16560dc2366fSVenugopal Iyer 		if ((*flags != 0) && (mss != NULL))
16570dc2366fSVenugopal Iyer 			*mss = (uint32_t)DB_LSOMSS(mp);
16580dc2366fSVenugopal Iyer 	}
1659da14cebeSEric Cheng }
166045d3dd98SRobert Mustacchi 
166145d3dd98SRobert Mustacchi void
166245d3dd98SRobert Mustacchi mac_transceiver_info_set_present(mac_transceiver_info_t *infop,
166345d3dd98SRobert Mustacchi     boolean_t present)
166445d3dd98SRobert Mustacchi {
166545d3dd98SRobert Mustacchi 	infop->mti_present = present;
166645d3dd98SRobert Mustacchi }
166745d3dd98SRobert Mustacchi 
166845d3dd98SRobert Mustacchi void
166945d3dd98SRobert Mustacchi mac_transceiver_info_set_usable(mac_transceiver_info_t *infop,
167045d3dd98SRobert Mustacchi     boolean_t usable)
167145d3dd98SRobert Mustacchi {
167245d3dd98SRobert Mustacchi 	infop->mti_usable = usable;
167345d3dd98SRobert Mustacchi }
1674d240edafSRobert Mustacchi 
1675d240edafSRobert Mustacchi /*
1676d240edafSRobert Mustacchi  * We should really keep track of our offset and not walk everything every
1677d240edafSRobert Mustacchi  * time. I can't imagine that this will be kind to us at high packet rates;
1678d240edafSRobert Mustacchi  * however, for the moment, let's leave that.
1679d240edafSRobert Mustacchi  *
1680d240edafSRobert Mustacchi  * This walks a message block chain without pulling up to fill in the context
1681d240edafSRobert Mustacchi  * information. Note that the data we care about could be hidden across more
1682d240edafSRobert Mustacchi  * than one mblk_t.
1683d240edafSRobert Mustacchi  */
1684d240edafSRobert Mustacchi static int
1685d240edafSRobert Mustacchi mac_meoi_get_uint8(mblk_t *mp, off_t off, uint8_t *out)
1686d240edafSRobert Mustacchi {
1687d240edafSRobert Mustacchi 	size_t mpsize;
1688d240edafSRobert Mustacchi 	uint8_t *bp;
1689d240edafSRobert Mustacchi 
1690d240edafSRobert Mustacchi 	mpsize = msgsize(mp);
1691d240edafSRobert Mustacchi 	/* Check for overflow */
1692d240edafSRobert Mustacchi 	if (off + sizeof (uint16_t) > mpsize)
1693d240edafSRobert Mustacchi 		return (-1);
1694d240edafSRobert Mustacchi 
1695d240edafSRobert Mustacchi 	mpsize = MBLKL(mp);
1696d240edafSRobert Mustacchi 	while (off >= mpsize) {
1697d240edafSRobert Mustacchi 		mp = mp->b_cont;
1698d240edafSRobert Mustacchi 		off -= mpsize;
1699d240edafSRobert Mustacchi 		mpsize = MBLKL(mp);
1700d240edafSRobert Mustacchi 	}
1701d240edafSRobert Mustacchi 
1702d240edafSRobert Mustacchi 	bp = mp->b_rptr + off;
1703d240edafSRobert Mustacchi 	*out = *bp;
1704d240edafSRobert Mustacchi 	return (0);
1705d240edafSRobert Mustacchi 
1706d240edafSRobert Mustacchi }
1707d240edafSRobert Mustacchi 
1708d240edafSRobert Mustacchi static int
1709d240edafSRobert Mustacchi mac_meoi_get_uint16(mblk_t *mp, off_t off, uint16_t *out)
1710d240edafSRobert Mustacchi {
1711d240edafSRobert Mustacchi 	size_t mpsize;
1712d240edafSRobert Mustacchi 	uint8_t *bp;
1713d240edafSRobert Mustacchi 
1714d240edafSRobert Mustacchi 	mpsize = msgsize(mp);
1715d240edafSRobert Mustacchi 	/* Check for overflow */
1716d240edafSRobert Mustacchi 	if (off + sizeof (uint16_t) > mpsize)
1717d240edafSRobert Mustacchi 		return (-1);
1718d240edafSRobert Mustacchi 
1719d240edafSRobert Mustacchi 	mpsize = MBLKL(mp);
1720d240edafSRobert Mustacchi 	while (off >= mpsize) {
1721d240edafSRobert Mustacchi 		mp = mp->b_cont;
1722d240edafSRobert Mustacchi 		off -= mpsize;
1723d240edafSRobert Mustacchi 		mpsize = MBLKL(mp);
1724d240edafSRobert Mustacchi 	}
1725d240edafSRobert Mustacchi 
1726d240edafSRobert Mustacchi 	/*
1727d240edafSRobert Mustacchi 	 * Data is in network order. Note the second byte of data might be in
1728d240edafSRobert Mustacchi 	 * the next mp.
1729d240edafSRobert Mustacchi 	 */
1730d240edafSRobert Mustacchi 	bp = mp->b_rptr + off;
1731d240edafSRobert Mustacchi 	*out = *bp << 8;
1732d240edafSRobert Mustacchi 	if (off + 1 == mpsize) {
1733d240edafSRobert Mustacchi 		mp = mp->b_cont;
1734d240edafSRobert Mustacchi 		bp = mp->b_rptr;
1735d240edafSRobert Mustacchi 	} else {
1736d240edafSRobert Mustacchi 		bp++;
1737d240edafSRobert Mustacchi 	}
1738d240edafSRobert Mustacchi 
1739d240edafSRobert Mustacchi 	*out |= *bp;
1740d240edafSRobert Mustacchi 	return (0);
1741d240edafSRobert Mustacchi 
1742d240edafSRobert Mustacchi }
1743d240edafSRobert Mustacchi 
1744d240edafSRobert Mustacchi 
1745d240edafSRobert Mustacchi int
1746d240edafSRobert Mustacchi mac_ether_offload_info(mblk_t *mp, mac_ether_offload_info_t *meoi)
1747d240edafSRobert Mustacchi {
1748d240edafSRobert Mustacchi 	size_t off;
1749d240edafSRobert Mustacchi 	uint16_t ether;
1750d240edafSRobert Mustacchi 	uint8_t ipproto, iplen, l4len, maclen;
1751d240edafSRobert Mustacchi 
1752d240edafSRobert Mustacchi 	bzero(meoi, sizeof (mac_ether_offload_info_t));
1753d240edafSRobert Mustacchi 
1754d240edafSRobert Mustacchi 	meoi->meoi_len = msgsize(mp);
1755d240edafSRobert Mustacchi 	off = offsetof(struct ether_header, ether_type);
1756d240edafSRobert Mustacchi 	if (mac_meoi_get_uint16(mp, off, &ether) != 0)
1757d240edafSRobert Mustacchi 		return (-1);
1758d240edafSRobert Mustacchi 
1759d240edafSRobert Mustacchi 	if (ether == ETHERTYPE_VLAN) {
1760d240edafSRobert Mustacchi 		off = offsetof(struct ether_vlan_header, ether_type);
1761d240edafSRobert Mustacchi 		if (mac_meoi_get_uint16(mp, off, &ether) != 0)
1762d240edafSRobert Mustacchi 			return (-1);
1763d240edafSRobert Mustacchi 		meoi->meoi_flags |= MEOI_VLAN_TAGGED;
1764d240edafSRobert Mustacchi 		maclen = sizeof (struct ether_vlan_header);
1765d240edafSRobert Mustacchi 	} else {
1766d240edafSRobert Mustacchi 		maclen = sizeof (struct ether_header);
1767d240edafSRobert Mustacchi 	}
1768d240edafSRobert Mustacchi 	meoi->meoi_flags |= MEOI_L2INFO_SET;
1769d240edafSRobert Mustacchi 	meoi->meoi_l2hlen = maclen;
1770d240edafSRobert Mustacchi 	meoi->meoi_l3proto = ether;
1771d240edafSRobert Mustacchi 
1772d240edafSRobert Mustacchi 	switch (ether) {
1773d240edafSRobert Mustacchi 	case ETHERTYPE_IP:
1774d240edafSRobert Mustacchi 		/*
1775d240edafSRobert Mustacchi 		 * For IPv4 we need to get the length of the header, as it can
1776d240edafSRobert Mustacchi 		 * be variable.
1777d240edafSRobert Mustacchi 		 */
1778d240edafSRobert Mustacchi 		off = offsetof(ipha_t, ipha_version_and_hdr_length) + maclen;
1779d240edafSRobert Mustacchi 		if (mac_meoi_get_uint8(mp, off, &iplen) != 0)
1780d240edafSRobert Mustacchi 			return (-1);
1781d240edafSRobert Mustacchi 		iplen &= 0x0f;
1782d240edafSRobert Mustacchi 		if (iplen < 5 || iplen > 0x0f)
1783d240edafSRobert Mustacchi 			return (-1);
1784d240edafSRobert Mustacchi 		iplen *= 4;
1785d240edafSRobert Mustacchi 		off = offsetof(ipha_t, ipha_protocol) + maclen;
1786d240edafSRobert Mustacchi 		if (mac_meoi_get_uint8(mp, off, &ipproto) == -1)
1787d240edafSRobert Mustacchi 			return (-1);
1788d240edafSRobert Mustacchi 		break;
1789d240edafSRobert Mustacchi 	case ETHERTYPE_IPV6:
1790d240edafSRobert Mustacchi 		iplen = 40;
1791d240edafSRobert Mustacchi 		off = offsetof(ip6_t, ip6_nxt) + maclen;
1792d240edafSRobert Mustacchi 		if (mac_meoi_get_uint8(mp, off, &ipproto) == -1)
1793d240edafSRobert Mustacchi 			return (-1);
1794d240edafSRobert Mustacchi 		break;
1795d240edafSRobert Mustacchi 	default:
1796d240edafSRobert Mustacchi 		return (0);
1797d240edafSRobert Mustacchi 	}
1798d240edafSRobert Mustacchi 	meoi->meoi_l3hlen = iplen;
1799d240edafSRobert Mustacchi 	meoi->meoi_l4proto = ipproto;
1800d240edafSRobert Mustacchi 	meoi->meoi_flags |= MEOI_L3INFO_SET;
1801d240edafSRobert Mustacchi 
1802d240edafSRobert Mustacchi 	switch (ipproto) {
1803d240edafSRobert Mustacchi 	case IPPROTO_TCP:
1804d240edafSRobert Mustacchi 		off = offsetof(tcph_t, th_offset_and_rsrvd) + maclen + iplen;
1805d240edafSRobert Mustacchi 		if (mac_meoi_get_uint8(mp, off, &l4len) == -1)
1806d240edafSRobert Mustacchi 			return (-1);
1807d240edafSRobert Mustacchi 		l4len = (l4len & 0xf0) >> 4;
1808d240edafSRobert Mustacchi 		if (l4len < 5 || l4len > 0xf)
1809d240edafSRobert Mustacchi 			return (-1);
1810d240edafSRobert Mustacchi 		l4len *= 4;
1811d240edafSRobert Mustacchi 		break;
1812d240edafSRobert Mustacchi 	case IPPROTO_UDP:
1813d240edafSRobert Mustacchi 		l4len = sizeof (struct udphdr);
1814d240edafSRobert Mustacchi 		break;
1815d240edafSRobert Mustacchi 	case IPPROTO_SCTP:
1816d240edafSRobert Mustacchi 		l4len = sizeof (sctp_hdr_t);
1817d240edafSRobert Mustacchi 		break;
1818d240edafSRobert Mustacchi 	default:
1819d240edafSRobert Mustacchi 		return (0);
1820d240edafSRobert Mustacchi 	}
1821d240edafSRobert Mustacchi 
1822d240edafSRobert Mustacchi 	meoi->meoi_l4hlen = l4len;
1823d240edafSRobert Mustacchi 	meoi->meoi_flags |= MEOI_L4INFO_SET;
1824d240edafSRobert Mustacchi 	return (0);
1825d240edafSRobert Mustacchi }
1826