xref: /titanic_41/usr/src/uts/common/io/dld/dld_proto.c (revision 88e8a7f2124e61277dc5f6bf9718881bfb8b724e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Data-Link Driver
28  */
29 #include <sys/sysmacros.h>
30 #include <sys/strsubr.h>
31 #include <sys/strsun.h>
32 #include <sys/vlan.h>
33 #include <sys/dld_impl.h>
34 #include <sys/mac_client.h>
35 #include <sys/mac_client_impl.h>
36 #include <sys/mac_client_priv.h>
37 
38 typedef void proto_reqfunc_t(dld_str_t *, mblk_t *);
39 
40 static proto_reqfunc_t proto_info_req, proto_attach_req, proto_detach_req,
41     proto_bind_req, proto_unbind_req, proto_promiscon_req, proto_promiscoff_req,
42     proto_enabmulti_req, proto_disabmulti_req, proto_physaddr_req,
43     proto_setphysaddr_req, proto_udqos_req, proto_req, proto_capability_req,
44     proto_notify_req, proto_passive_req;
45 
46 static void proto_capability_advertise(dld_str_t *, mblk_t *);
47 static int dld_capab_poll_disable(dld_str_t *, dld_capab_poll_t *);
48 static boolean_t check_mod_above(queue_t *, const char *);
49 
50 #define	DL_ACK_PENDING(state) \
51 	((state) == DL_ATTACH_PENDING || \
52 	(state) == DL_DETACH_PENDING || \
53 	(state) == DL_BIND_PENDING || \
54 	(state) == DL_UNBIND_PENDING)
55 
56 /*
57  * Process a DLPI protocol message.
58  * The primitives DL_BIND_REQ, DL_ENABMULTI_REQ, DL_PROMISCON_REQ,
59  * DL_SET_PHYS_ADDR_REQ put the data link below our dld_str_t into an
60  * 'active' state. The primitive DL_PASSIVE_REQ marks our dld_str_t
61  * as 'passive' and forbids it from being subsequently made 'active'
62  * by the above primitives.
63  */
64 void
65 dld_proto(dld_str_t *dsp, mblk_t *mp)
66 {
67 	t_uscalar_t		prim;
68 
69 	if (MBLKL(mp) < sizeof (t_uscalar_t)) {
70 		freemsg(mp);
71 		return;
72 	}
73 	prim = ((union DL_primitives *)mp->b_rptr)->dl_primitive;
74 
75 	switch (prim) {
76 	case DL_INFO_REQ:
77 		proto_info_req(dsp, mp);
78 		break;
79 	case DL_BIND_REQ:
80 		proto_bind_req(dsp, mp);
81 		break;
82 	case DL_UNBIND_REQ:
83 		proto_unbind_req(dsp, mp);
84 		break;
85 	case DL_UNITDATA_REQ:
86 		proto_unitdata_req(dsp, mp);
87 		break;
88 	case DL_UDQOS_REQ:
89 		proto_udqos_req(dsp, mp);
90 		break;
91 	case DL_ATTACH_REQ:
92 		proto_attach_req(dsp, mp);
93 		break;
94 	case DL_DETACH_REQ:
95 		proto_detach_req(dsp, mp);
96 		break;
97 	case DL_ENABMULTI_REQ:
98 		proto_enabmulti_req(dsp, mp);
99 		break;
100 	case DL_DISABMULTI_REQ:
101 		proto_disabmulti_req(dsp, mp);
102 		break;
103 	case DL_PROMISCON_REQ:
104 		proto_promiscon_req(dsp, mp);
105 		break;
106 	case DL_PROMISCOFF_REQ:
107 		proto_promiscoff_req(dsp, mp);
108 		break;
109 	case DL_PHYS_ADDR_REQ:
110 		proto_physaddr_req(dsp, mp);
111 		break;
112 	case DL_SET_PHYS_ADDR_REQ:
113 		proto_setphysaddr_req(dsp, mp);
114 		break;
115 	case DL_NOTIFY_REQ:
116 		proto_notify_req(dsp, mp);
117 		break;
118 	case DL_CAPABILITY_REQ:
119 		proto_capability_req(dsp, mp);
120 		break;
121 	case DL_PASSIVE_REQ:
122 		proto_passive_req(dsp, mp);
123 		break;
124 	default:
125 		proto_req(dsp, mp);
126 		break;
127 	}
128 }
129 
130 #define	NEG(x)	-(x)
131 typedef struct dl_info_ack_wrapper {
132 	dl_info_ack_t		dl_info;
133 	uint8_t			dl_addr[MAXMACADDRLEN + sizeof (uint16_t)];
134 	uint8_t			dl_brdcst_addr[MAXMACADDRLEN];
135 	dl_qos_cl_range1_t	dl_qos_range1;
136 	dl_qos_cl_sel1_t	dl_qos_sel1;
137 } dl_info_ack_wrapper_t;
138 
139 /*
140  * DL_INFO_REQ
141  */
142 static void
143 proto_info_req(dld_str_t *dsp, mblk_t *mp)
144 {
145 	dl_info_ack_wrapper_t	*dlwp;
146 	dl_info_ack_t		*dlp;
147 	dl_qos_cl_sel1_t	*selp;
148 	dl_qos_cl_range1_t	*rangep;
149 	uint8_t			*addr;
150 	uint8_t			*brdcst_addr;
151 	uint_t			addr_length;
152 	uint_t			sap_length;
153 	mac_info_t		minfo;
154 	mac_info_t		*minfop;
155 	queue_t			*q = dsp->ds_wq;
156 
157 	/*
158 	 * Swap the request message for one large enough to contain the
159 	 * wrapper structure defined above.
160 	 */
161 	if ((mp = mexchange(q, mp, sizeof (dl_info_ack_wrapper_t),
162 	    M_PCPROTO, 0)) == NULL)
163 		return;
164 
165 	bzero(mp->b_rptr, sizeof (dl_info_ack_wrapper_t));
166 	dlwp = (dl_info_ack_wrapper_t *)mp->b_rptr;
167 
168 	dlp = &(dlwp->dl_info);
169 	ASSERT(dlp == (dl_info_ack_t *)mp->b_rptr);
170 
171 	dlp->dl_primitive = DL_INFO_ACK;
172 
173 	/*
174 	 * Set up the sub-structure pointers.
175 	 */
176 	addr = dlwp->dl_addr;
177 	brdcst_addr = dlwp->dl_brdcst_addr;
178 	rangep = &(dlwp->dl_qos_range1);
179 	selp = &(dlwp->dl_qos_sel1);
180 
181 	/*
182 	 * This driver supports only version 2 connectionless DLPI provider
183 	 * nodes.
184 	 */
185 	dlp->dl_service_mode = DL_CLDLS;
186 	dlp->dl_version = DL_VERSION_2;
187 
188 	/*
189 	 * Set the style of the provider
190 	 */
191 	dlp->dl_provider_style = dsp->ds_style;
192 	ASSERT(dlp->dl_provider_style == DL_STYLE1 ||
193 	    dlp->dl_provider_style == DL_STYLE2);
194 
195 	/*
196 	 * Set the current DLPI state.
197 	 */
198 	dlp->dl_current_state = dsp->ds_dlstate;
199 
200 	/*
201 	 * Gratuitously set the media type. This is to deal with modules
202 	 * that assume the media type is known prior to DL_ATTACH_REQ
203 	 * being completed.
204 	 */
205 	dlp->dl_mac_type = DL_ETHER;
206 
207 	/*
208 	 * If the stream is not at least attached we try to retrieve the
209 	 * mac_info using mac_info_get()
210 	 */
211 	if (dsp->ds_dlstate == DL_UNATTACHED ||
212 	    dsp->ds_dlstate == DL_ATTACH_PENDING ||
213 	    dsp->ds_dlstate == DL_DETACH_PENDING) {
214 		if (!mac_info_get(ddi_major_to_name(dsp->ds_major), &minfo)) {
215 			/*
216 			 * Cannot find mac_info. giving up.
217 			 */
218 			goto done;
219 		}
220 		minfop = &minfo;
221 	} else {
222 		minfop = (mac_info_t *)dsp->ds_mip;
223 		/* We can only get the sdu if we're attached. */
224 		mac_sdu_get(dsp->ds_mh, &dlp->dl_min_sdu, &dlp->dl_max_sdu);
225 	}
226 
227 	/*
228 	 * Set the media type (properly this time).
229 	 */
230 	if (dsp->ds_native)
231 		dlp->dl_mac_type = minfop->mi_nativemedia;
232 	else
233 		dlp->dl_mac_type = minfop->mi_media;
234 
235 	/*
236 	 * Set the DLSAP length. We only support 16 bit values and they
237 	 * appear after the MAC address portion of DLSAP addresses.
238 	 */
239 	sap_length = sizeof (uint16_t);
240 	dlp->dl_sap_length = NEG(sap_length);
241 
242 	addr_length = minfop->mi_addr_length;
243 
244 	/*
245 	 * Copy in the media broadcast address.
246 	 */
247 	if (minfop->mi_brdcst_addr != NULL) {
248 		dlp->dl_brdcst_addr_offset =
249 		    (uintptr_t)brdcst_addr - (uintptr_t)dlp;
250 		bcopy(minfop->mi_brdcst_addr, brdcst_addr, addr_length);
251 		dlp->dl_brdcst_addr_length = addr_length;
252 	}
253 
254 	/* Only VLAN links and links that have a normal tag mode support QOS. */
255 	if ((dsp->ds_mch != NULL &&
256 	    mac_client_vid(dsp->ds_mch) != VLAN_ID_NONE) ||
257 	    (dsp->ds_dlp != NULL &&
258 	    dsp->ds_dlp->dl_tagmode == LINK_TAGMODE_NORMAL)) {
259 		dlp->dl_qos_range_offset = (uintptr_t)rangep - (uintptr_t)dlp;
260 		dlp->dl_qos_range_length = sizeof (dl_qos_cl_range1_t);
261 
262 		rangep->dl_qos_type = DL_QOS_CL_RANGE1;
263 		rangep->dl_trans_delay.dl_target_value = DL_UNKNOWN;
264 		rangep->dl_trans_delay.dl_accept_value = DL_UNKNOWN;
265 		rangep->dl_protection.dl_min = DL_UNKNOWN;
266 		rangep->dl_protection.dl_max = DL_UNKNOWN;
267 		rangep->dl_residual_error = DL_UNKNOWN;
268 
269 		/*
270 		 * Specify the supported range of priorities.
271 		 */
272 		rangep->dl_priority.dl_min = 0;
273 		rangep->dl_priority.dl_max = (1 << VLAN_PRI_SIZE) - 1;
274 
275 		dlp->dl_qos_offset = (uintptr_t)selp - (uintptr_t)dlp;
276 		dlp->dl_qos_length = sizeof (dl_qos_cl_sel1_t);
277 
278 		selp->dl_qos_type = DL_QOS_CL_SEL1;
279 		selp->dl_trans_delay = DL_UNKNOWN;
280 		selp->dl_protection = DL_UNKNOWN;
281 		selp->dl_residual_error = DL_UNKNOWN;
282 
283 		/*
284 		 * Specify the current priority (which can be changed by
285 		 * the DL_UDQOS_REQ primitive).
286 		 */
287 		selp->dl_priority = dsp->ds_pri;
288 	}
289 
290 	dlp->dl_addr_length = addr_length + sizeof (uint16_t);
291 	if (dsp->ds_dlstate == DL_IDLE) {
292 		/*
293 		 * The stream is bound. Therefore we can formulate a valid
294 		 * DLSAP address.
295 		 */
296 		dlp->dl_addr_offset = (uintptr_t)addr - (uintptr_t)dlp;
297 		if (addr_length > 0)
298 			mac_unicast_primary_get(dsp->ds_mh, addr);
299 
300 		*(uint16_t *)(addr + addr_length) = dsp->ds_sap;
301 	}
302 
303 done:
304 	IMPLY(dlp->dl_qos_offset != 0, dlp->dl_qos_length != 0);
305 	IMPLY(dlp->dl_qos_range_offset != 0,
306 	    dlp->dl_qos_range_length != 0);
307 	IMPLY(dlp->dl_addr_offset != 0, dlp->dl_addr_length != 0);
308 	IMPLY(dlp->dl_brdcst_addr_offset != 0,
309 	    dlp->dl_brdcst_addr_length != 0);
310 
311 	qreply(q, mp);
312 }
313 
314 /*
315  * DL_ATTACH_REQ
316  */
317 static void
318 proto_attach_req(dld_str_t *dsp, mblk_t *mp)
319 {
320 	dl_attach_req_t	*dlp = (dl_attach_req_t *)mp->b_rptr;
321 	int		err = 0;
322 	t_uscalar_t	dl_err;
323 	queue_t		*q = dsp->ds_wq;
324 
325 	if (MBLKL(mp) < sizeof (dl_attach_req_t) ||
326 	    dlp->dl_ppa < 0 || dsp->ds_style == DL_STYLE1) {
327 		dl_err = DL_BADPRIM;
328 		goto failed;
329 	}
330 
331 	if (dsp->ds_dlstate != DL_UNATTACHED) {
332 		dl_err = DL_OUTSTATE;
333 		goto failed;
334 	}
335 
336 	dsp->ds_dlstate = DL_ATTACH_PENDING;
337 
338 	err = dld_str_attach(dsp, dlp->dl_ppa);
339 	if (err != 0) {
340 		switch (err) {
341 		case ENOENT:
342 			dl_err = DL_BADPPA;
343 			err = 0;
344 			break;
345 		default:
346 			dl_err = DL_SYSERR;
347 			break;
348 		}
349 		dsp->ds_dlstate = DL_UNATTACHED;
350 		goto failed;
351 	}
352 	ASSERT(dsp->ds_dlstate == DL_UNBOUND);
353 	dlokack(q, mp, DL_ATTACH_REQ);
354 	return;
355 
356 failed:
357 	dlerrorack(q, mp, DL_ATTACH_REQ, dl_err, (t_uscalar_t)err);
358 }
359 
360 /*
361  * DL_DETACH_REQ
362  */
363 static void
364 proto_detach_req(dld_str_t *dsp, mblk_t *mp)
365 {
366 	queue_t		*q = dsp->ds_wq;
367 	t_uscalar_t	dl_err;
368 
369 	if (MBLKL(mp) < sizeof (dl_detach_req_t)) {
370 		dl_err = DL_BADPRIM;
371 		goto failed;
372 	}
373 
374 	if (dsp->ds_dlstate != DL_UNBOUND) {
375 		dl_err = DL_OUTSTATE;
376 		goto failed;
377 	}
378 
379 	if (dsp->ds_style == DL_STYLE1) {
380 		dl_err = DL_BADPRIM;
381 		goto failed;
382 	}
383 
384 	ASSERT(dsp->ds_datathr_cnt == 0);
385 	dsp->ds_dlstate = DL_DETACH_PENDING;
386 
387 	dld_str_detach(dsp);
388 	dlokack(dsp->ds_wq, mp, DL_DETACH_REQ);
389 	return;
390 
391 failed:
392 	dlerrorack(q, mp, DL_DETACH_REQ, dl_err, 0);
393 }
394 
395 /*
396  * DL_BIND_REQ
397  */
398 static void
399 proto_bind_req(dld_str_t *dsp, mblk_t *mp)
400 {
401 	dl_bind_req_t	*dlp = (dl_bind_req_t *)mp->b_rptr;
402 	int		err = 0;
403 	uint8_t		dlsap_addr[MAXMACADDRLEN + sizeof (uint16_t)];
404 	uint_t		dlsap_addr_length;
405 	t_uscalar_t	dl_err;
406 	t_scalar_t	sap;
407 	queue_t		*q = dsp->ds_wq;
408 	mac_perim_handle_t	mph;
409 	void		*mdip;
410 	int32_t		intr_cpu;
411 
412 	if (MBLKL(mp) < sizeof (dl_bind_req_t)) {
413 		dl_err = DL_BADPRIM;
414 		goto failed;
415 	}
416 
417 	if (dlp->dl_xidtest_flg != 0) {
418 		dl_err = DL_NOAUTO;
419 		goto failed;
420 	}
421 
422 	if (dlp->dl_service_mode != DL_CLDLS) {
423 		dl_err = DL_UNSUPPORTED;
424 		goto failed;
425 	}
426 
427 	if (dsp->ds_dlstate != DL_UNBOUND) {
428 		dl_err = DL_OUTSTATE;
429 		goto failed;
430 	}
431 
432 	mac_perim_enter_by_mh(dsp->ds_mh, &mph);
433 
434 	if ((err = dls_active_set(dsp)) != 0) {
435 		dl_err = DL_SYSERR;
436 		goto failed2;
437 	}
438 
439 	dsp->ds_dlstate = DL_BIND_PENDING;
440 	/*
441 	 * Set the receive callback.
442 	 */
443 	dls_rx_set(dsp, (dsp->ds_mode == DLD_RAW) ?
444 	    dld_str_rx_raw : dld_str_rx_unitdata, dsp);
445 
446 	/*
447 	 * Bind the channel such that it can receive packets.
448 	 */
449 	sap = dlp->dl_sap;
450 	dsp->ds_nonip = !check_mod_above(dsp->ds_rq, "ip") &&
451 	    !check_mod_above(dsp->ds_rq, "arp");
452 
453 	err = dls_bind(dsp, sap);
454 	if (err != 0) {
455 		switch (err) {
456 		case EINVAL:
457 			dl_err = DL_BADADDR;
458 			err = 0;
459 			break;
460 		default:
461 			dl_err = DL_SYSERR;
462 			break;
463 		}
464 
465 		dsp->ds_dlstate = DL_UNBOUND;
466 		dls_active_clear(dsp, B_FALSE);
467 		goto failed2;
468 	}
469 
470 	intr_cpu = mac_client_intr_cpu(dsp->ds_mch);
471 	mdip = mac_get_devinfo(dsp->ds_mh);
472 	mac_perim_exit(mph);
473 
474 	/*
475 	 * We do this after we get out of the perim to avoid deadlocks
476 	 * etc. since part of mac_client_retarget_intr is to walk the
477 	 * device tree in order to find and retarget the interrupts.
478 	 */
479 	if (intr_cpu != -1)
480 		mac_client_set_intr_cpu(mdip, dsp->ds_mch, intr_cpu);
481 
482 	/*
483 	 * Copy in MAC address.
484 	 */
485 	dlsap_addr_length = dsp->ds_mip->mi_addr_length;
486 	mac_unicast_primary_get(dsp->ds_mh, dlsap_addr);
487 
488 	/*
489 	 * Copy in the SAP.
490 	 */
491 	*(uint16_t *)(dlsap_addr + dlsap_addr_length) = sap;
492 	dlsap_addr_length += sizeof (uint16_t);
493 
494 	dsp->ds_dlstate = DL_IDLE;
495 	dlbindack(q, mp, sap, dlsap_addr, dlsap_addr_length, 0, 0);
496 	return;
497 
498 failed2:
499 	mac_perim_exit(mph);
500 failed:
501 	dlerrorack(q, mp, DL_BIND_REQ, dl_err, (t_uscalar_t)err);
502 }
503 
504 /*
505  * DL_UNBIND_REQ
506  */
507 static void
508 proto_unbind_req(dld_str_t *dsp, mblk_t *mp)
509 {
510 	queue_t		*q = dsp->ds_wq;
511 	t_uscalar_t	dl_err;
512 	mac_perim_handle_t	mph;
513 
514 	if (MBLKL(mp) < sizeof (dl_unbind_req_t)) {
515 		dl_err = DL_BADPRIM;
516 		goto failed;
517 	}
518 
519 	if (dsp->ds_dlstate != DL_IDLE) {
520 		dl_err = DL_OUTSTATE;
521 		goto failed;
522 	}
523 
524 	mutex_enter(&dsp->ds_lock);
525 	while (dsp->ds_datathr_cnt != 0)
526 		cv_wait(&dsp->ds_datathr_cv, &dsp->ds_lock);
527 
528 	dsp->ds_dlstate = DL_UNBIND_PENDING;
529 	mutex_exit(&dsp->ds_lock);
530 
531 	mac_perim_enter_by_mh(dsp->ds_mh, &mph);
532 	/*
533 	 * Unbind the channel to stop packets being received.
534 	 */
535 	dls_unbind(dsp);
536 
537 	/*
538 	 * Disable polling mode, if it is enabled.
539 	 */
540 	(void) dld_capab_poll_disable(dsp, NULL);
541 
542 	/*
543 	 * Clear LSO flags.
544 	 */
545 	dsp->ds_lso = B_FALSE;
546 	dsp->ds_lso_max = 0;
547 
548 	/*
549 	 * Clear the receive callback.
550 	 */
551 	dls_rx_set(dsp, NULL, NULL);
552 	dsp->ds_direct = B_FALSE;
553 
554 	/*
555 	 * Set the mode back to the default (unitdata).
556 	 */
557 	dsp->ds_mode = DLD_UNITDATA;
558 	dsp->ds_dlstate = DL_UNBOUND;
559 
560 	dls_active_clear(dsp, B_FALSE);
561 	mac_perim_exit(mph);
562 	dlokack(dsp->ds_wq, mp, DL_UNBIND_REQ);
563 	return;
564 failed:
565 	dlerrorack(q, mp, DL_UNBIND_REQ, dl_err, 0);
566 }
567 
568 /*
569  * DL_PROMISCON_REQ
570  */
571 static void
572 proto_promiscon_req(dld_str_t *dsp, mblk_t *mp)
573 {
574 	dl_promiscon_req_t *dlp = (dl_promiscon_req_t *)mp->b_rptr;
575 	int		err = 0;
576 	t_uscalar_t	dl_err;
577 	uint32_t	promisc_saved;
578 	queue_t		*q = dsp->ds_wq;
579 	mac_perim_handle_t	mph;
580 
581 	if (MBLKL(mp) < sizeof (dl_promiscon_req_t)) {
582 		dl_err = DL_BADPRIM;
583 		goto failed;
584 	}
585 
586 	if (dsp->ds_dlstate == DL_UNATTACHED ||
587 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
588 		dl_err = DL_OUTSTATE;
589 		goto failed;
590 	}
591 
592 	promisc_saved = dsp->ds_promisc;
593 	switch (dlp->dl_level) {
594 	case DL_PROMISC_SAP:
595 		dsp->ds_promisc |= DLS_PROMISC_SAP;
596 		break;
597 
598 	case DL_PROMISC_MULTI:
599 		dsp->ds_promisc |= DLS_PROMISC_MULTI;
600 		break;
601 
602 	case DL_PROMISC_PHYS:
603 		dsp->ds_promisc |= DLS_PROMISC_PHYS;
604 		break;
605 
606 	default:
607 		dl_err = DL_NOTSUPPORTED;
608 		goto failed;
609 	}
610 
611 	mac_perim_enter_by_mh(dsp->ds_mh, &mph);
612 
613 	if ((promisc_saved == 0) && (err = dls_active_set(dsp)) != 0) {
614 		dsp->ds_promisc = promisc_saved;
615 		dl_err = DL_SYSERR;
616 		goto failed2;
617 	}
618 
619 	/*
620 	 * Adjust channel promiscuity.
621 	 */
622 	err = dls_promisc(dsp, promisc_saved);
623 
624 	if (err != 0) {
625 		dl_err = DL_SYSERR;
626 		dsp->ds_promisc = promisc_saved;
627 		if (promisc_saved == 0)
628 			dls_active_clear(dsp, B_FALSE);
629 		goto failed2;
630 	}
631 
632 	mac_perim_exit(mph);
633 
634 	dlokack(q, mp, DL_PROMISCON_REQ);
635 	return;
636 
637 failed2:
638 	mac_perim_exit(mph);
639 failed:
640 	dlerrorack(q, mp, DL_PROMISCON_REQ, dl_err, (t_uscalar_t)err);
641 }
642 
643 /*
644  * DL_PROMISCOFF_REQ
645  */
646 static void
647 proto_promiscoff_req(dld_str_t *dsp, mblk_t *mp)
648 {
649 	dl_promiscoff_req_t *dlp = (dl_promiscoff_req_t *)mp->b_rptr;
650 	int		err = 0;
651 	t_uscalar_t	dl_err;
652 	uint32_t	promisc_saved;
653 	queue_t		*q = dsp->ds_wq;
654 	mac_perim_handle_t	mph;
655 
656 	if (MBLKL(mp) < sizeof (dl_promiscoff_req_t)) {
657 		dl_err = DL_BADPRIM;
658 		goto failed;
659 	}
660 
661 	if (dsp->ds_dlstate == DL_UNATTACHED ||
662 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
663 		dl_err = DL_OUTSTATE;
664 		goto failed;
665 	}
666 
667 	promisc_saved = dsp->ds_promisc;
668 	switch (dlp->dl_level) {
669 	case DL_PROMISC_SAP:
670 		if (!(dsp->ds_promisc & DLS_PROMISC_SAP)) {
671 			dl_err = DL_NOTENAB;
672 			goto failed;
673 		}
674 		dsp->ds_promisc &= ~DLS_PROMISC_SAP;
675 		break;
676 
677 	case DL_PROMISC_MULTI:
678 		if (!(dsp->ds_promisc & DLS_PROMISC_MULTI)) {
679 			dl_err = DL_NOTENAB;
680 			goto failed;
681 		}
682 		dsp->ds_promisc &= ~DLS_PROMISC_MULTI;
683 		break;
684 
685 	case DL_PROMISC_PHYS:
686 		if (!(dsp->ds_promisc & DLS_PROMISC_PHYS)) {
687 			dl_err = DL_NOTENAB;
688 			goto failed;
689 		}
690 		dsp->ds_promisc &= ~DLS_PROMISC_PHYS;
691 		break;
692 
693 	default:
694 		dl_err = DL_NOTSUPPORTED;
695 		goto failed;
696 	}
697 
698 	mac_perim_enter_by_mh(dsp->ds_mh, &mph);
699 	/*
700 	 * Adjust channel promiscuity.
701 	 */
702 	err = dls_promisc(dsp, promisc_saved);
703 
704 	if (err != 0) {
705 		mac_perim_exit(mph);
706 		dl_err = DL_SYSERR;
707 		goto failed;
708 	}
709 
710 	if (dsp->ds_promisc == 0)
711 		dls_active_clear(dsp, B_FALSE);
712 
713 	mac_perim_exit(mph);
714 
715 	dlokack(q, mp, DL_PROMISCOFF_REQ);
716 	return;
717 failed:
718 	dlerrorack(q, mp, DL_PROMISCOFF_REQ, dl_err, (t_uscalar_t)err);
719 }
720 
721 /*
722  * DL_ENABMULTI_REQ
723  */
724 static void
725 proto_enabmulti_req(dld_str_t *dsp, mblk_t *mp)
726 {
727 	dl_enabmulti_req_t *dlp = (dl_enabmulti_req_t *)mp->b_rptr;
728 	int		err = 0;
729 	t_uscalar_t	dl_err;
730 	queue_t		*q = dsp->ds_wq;
731 	mac_perim_handle_t	mph;
732 
733 	if (dsp->ds_dlstate == DL_UNATTACHED ||
734 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
735 		dl_err = DL_OUTSTATE;
736 		goto failed;
737 	}
738 
739 	if (MBLKL(mp) < sizeof (dl_enabmulti_req_t) ||
740 	    !MBLKIN(mp, dlp->dl_addr_offset, dlp->dl_addr_length) ||
741 	    dlp->dl_addr_length != dsp->ds_mip->mi_addr_length) {
742 		dl_err = DL_BADPRIM;
743 		goto failed;
744 	}
745 
746 	mac_perim_enter_by_mh(dsp->ds_mh, &mph);
747 
748 	if ((dsp->ds_dmap == NULL) && (err = dls_active_set(dsp)) != 0) {
749 		dl_err = DL_SYSERR;
750 		goto failed2;
751 	}
752 
753 	err = dls_multicst_add(dsp, mp->b_rptr + dlp->dl_addr_offset);
754 	if (err != 0) {
755 		switch (err) {
756 		case EINVAL:
757 			dl_err = DL_BADADDR;
758 			err = 0;
759 			break;
760 		case ENOSPC:
761 			dl_err = DL_TOOMANY;
762 			err = 0;
763 			break;
764 		default:
765 			dl_err = DL_SYSERR;
766 			break;
767 		}
768 		if (dsp->ds_dmap == NULL)
769 			dls_active_clear(dsp, B_FALSE);
770 		goto failed2;
771 	}
772 
773 	mac_perim_exit(mph);
774 
775 	dlokack(q, mp, DL_ENABMULTI_REQ);
776 	return;
777 
778 failed2:
779 	mac_perim_exit(mph);
780 failed:
781 	dlerrorack(q, mp, DL_ENABMULTI_REQ, dl_err, (t_uscalar_t)err);
782 }
783 
784 /*
785  * DL_DISABMULTI_REQ
786  */
787 static void
788 proto_disabmulti_req(dld_str_t *dsp, mblk_t *mp)
789 {
790 	dl_disabmulti_req_t *dlp = (dl_disabmulti_req_t *)mp->b_rptr;
791 	int		err = 0;
792 	t_uscalar_t	dl_err;
793 	queue_t		*q = dsp->ds_wq;
794 	mac_perim_handle_t	mph;
795 
796 	if (dsp->ds_dlstate == DL_UNATTACHED ||
797 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
798 		dl_err = DL_OUTSTATE;
799 		goto failed;
800 	}
801 
802 	if (MBLKL(mp) < sizeof (dl_disabmulti_req_t) ||
803 	    !MBLKIN(mp, dlp->dl_addr_offset, dlp->dl_addr_length) ||
804 	    dlp->dl_addr_length != dsp->ds_mip->mi_addr_length) {
805 		dl_err = DL_BADPRIM;
806 		goto failed;
807 	}
808 
809 	mac_perim_enter_by_mh(dsp->ds_mh, &mph);
810 	err = dls_multicst_remove(dsp, mp->b_rptr + dlp->dl_addr_offset);
811 	if ((err == 0) && (dsp->ds_dmap == NULL))
812 		dls_active_clear(dsp, B_FALSE);
813 	mac_perim_exit(mph);
814 
815 	if (err != 0) {
816 	switch (err) {
817 		case EINVAL:
818 			dl_err = DL_BADADDR;
819 			err = 0;
820 			break;
821 
822 		case ENOENT:
823 			dl_err = DL_NOTENAB;
824 			err = 0;
825 			break;
826 
827 		default:
828 			dl_err = DL_SYSERR;
829 			break;
830 		}
831 		goto failed;
832 	}
833 	dlokack(q, mp, DL_DISABMULTI_REQ);
834 	return;
835 failed:
836 	dlerrorack(q, mp, DL_DISABMULTI_REQ, dl_err, (t_uscalar_t)err);
837 }
838 
839 /*
840  * DL_PHYS_ADDR_REQ
841  */
842 static void
843 proto_physaddr_req(dld_str_t *dsp, mblk_t *mp)
844 {
845 	dl_phys_addr_req_t *dlp = (dl_phys_addr_req_t *)mp->b_rptr;
846 	queue_t		*q = dsp->ds_wq;
847 	t_uscalar_t	dl_err = 0;
848 	char		*addr = NULL;
849 	uint_t		addr_length;
850 
851 	if (MBLKL(mp) < sizeof (dl_phys_addr_req_t)) {
852 		dl_err = DL_BADPRIM;
853 		goto done;
854 	}
855 
856 	if (dsp->ds_dlstate == DL_UNATTACHED ||
857 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
858 		dl_err = DL_OUTSTATE;
859 		goto done;
860 	}
861 
862 	addr_length = dsp->ds_mip->mi_addr_length;
863 	if (addr_length > 0) {
864 		addr = kmem_alloc(addr_length, KM_SLEEP);
865 		switch (dlp->dl_addr_type) {
866 		case DL_CURR_PHYS_ADDR:
867 			mac_unicast_primary_get(dsp->ds_mh, (uint8_t *)addr);
868 			break;
869 		case DL_FACT_PHYS_ADDR:
870 			bcopy(dsp->ds_mip->mi_unicst_addr, addr, addr_length);
871 			break;
872 		case DL_CURR_DEST_ADDR:
873 			if (!mac_dst_get(dsp->ds_mh, (uint8_t *)addr))
874 				dl_err = DL_NOTSUPPORTED;
875 			break;
876 		default:
877 			dl_err = DL_UNSUPPORTED;
878 		}
879 	}
880 done:
881 	if (dl_err == 0)
882 		dlphysaddrack(q, mp, addr, (t_uscalar_t)addr_length);
883 	else
884 		dlerrorack(q, mp, DL_PHYS_ADDR_REQ, dl_err, 0);
885 	if (addr != NULL)
886 		kmem_free(addr, addr_length);
887 }
888 
889 /*
890  * DL_SET_PHYS_ADDR_REQ
891  */
892 static void
893 proto_setphysaddr_req(dld_str_t *dsp, mblk_t *mp)
894 {
895 	dl_set_phys_addr_req_t *dlp = (dl_set_phys_addr_req_t *)mp->b_rptr;
896 	int		err = 0;
897 	t_uscalar_t	dl_err;
898 	queue_t		*q = dsp->ds_wq;
899 	mac_perim_handle_t	mph;
900 
901 	if (dsp->ds_dlstate == DL_UNATTACHED ||
902 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
903 		dl_err = DL_OUTSTATE;
904 		goto failed;
905 	}
906 
907 	if (MBLKL(mp) < sizeof (dl_set_phys_addr_req_t) ||
908 	    !MBLKIN(mp, dlp->dl_addr_offset, dlp->dl_addr_length) ||
909 	    dlp->dl_addr_length != dsp->ds_mip->mi_addr_length) {
910 		dl_err = DL_BADPRIM;
911 		goto failed;
912 	}
913 
914 	mac_perim_enter_by_mh(dsp->ds_mh, &mph);
915 
916 	if ((err = dls_active_set(dsp)) != 0) {
917 		dl_err = DL_SYSERR;
918 		goto failed2;
919 	}
920 
921 	/*
922 	 * If mac-nospoof is enabled and the link is owned by a
923 	 * non-global zone, changing the mac address is not allowed.
924 	 */
925 	if (dsp->ds_dlp->dl_zid != GLOBAL_ZONEID &&
926 	    mac_protect_enabled(dsp->ds_mch, MPT_MACNOSPOOF)) {
927 		dls_active_clear(dsp, B_FALSE);
928 		err = EACCES;
929 		goto failed2;
930 	}
931 
932 	err = mac_unicast_primary_set(dsp->ds_mh,
933 	    mp->b_rptr + dlp->dl_addr_offset);
934 	if (err != 0) {
935 		switch (err) {
936 		case EINVAL:
937 			dl_err = DL_BADADDR;
938 			err = 0;
939 			break;
940 
941 		default:
942 			dl_err = DL_SYSERR;
943 			break;
944 		}
945 		dls_active_clear(dsp, B_FALSE);
946 		goto failed2;
947 
948 	}
949 
950 	mac_perim_exit(mph);
951 
952 	dlokack(q, mp, DL_SET_PHYS_ADDR_REQ);
953 	return;
954 
955 failed2:
956 	mac_perim_exit(mph);
957 failed:
958 	dlerrorack(q, mp, DL_SET_PHYS_ADDR_REQ, dl_err, (t_uscalar_t)err);
959 }
960 
961 /*
962  * DL_UDQOS_REQ
963  */
964 static void
965 proto_udqos_req(dld_str_t *dsp, mblk_t *mp)
966 {
967 	dl_udqos_req_t *dlp = (dl_udqos_req_t *)mp->b_rptr;
968 	dl_qos_cl_sel1_t *selp;
969 	int		off, len;
970 	t_uscalar_t	dl_err;
971 	queue_t		*q = dsp->ds_wq;
972 
973 	off = dlp->dl_qos_offset;
974 	len = dlp->dl_qos_length;
975 
976 	if (MBLKL(mp) < sizeof (dl_udqos_req_t) || !MBLKIN(mp, off, len)) {
977 		dl_err = DL_BADPRIM;
978 		goto failed;
979 	}
980 
981 	selp = (dl_qos_cl_sel1_t *)(mp->b_rptr + off);
982 	if (selp->dl_qos_type != DL_QOS_CL_SEL1) {
983 		dl_err = DL_BADQOSTYPE;
984 		goto failed;
985 	}
986 
987 	if (selp->dl_priority > (1 << VLAN_PRI_SIZE) - 1 ||
988 	    selp->dl_priority < 0) {
989 		dl_err = DL_BADQOSPARAM;
990 		goto failed;
991 	}
992 
993 	dsp->ds_pri = selp->dl_priority;
994 	dlokack(q, mp, DL_UDQOS_REQ);
995 	return;
996 failed:
997 	dlerrorack(q, mp, DL_UDQOS_REQ, dl_err, 0);
998 }
999 
1000 static boolean_t
1001 check_mod_above(queue_t *q, const char *mod)
1002 {
1003 	queue_t		*next_q;
1004 	boolean_t	ret = B_TRUE;
1005 
1006 	claimstr(q);
1007 	next_q = q->q_next;
1008 	if (strcmp(next_q->q_qinfo->qi_minfo->mi_idname, mod) != 0)
1009 		ret = B_FALSE;
1010 	releasestr(q);
1011 	return (ret);
1012 }
1013 
1014 /*
1015  * DL_CAPABILITY_REQ
1016  */
1017 static void
1018 proto_capability_req(dld_str_t *dsp, mblk_t *mp)
1019 {
1020 	dl_capability_req_t *dlp = (dl_capability_req_t *)mp->b_rptr;
1021 	dl_capability_sub_t *sp;
1022 	size_t		size, len;
1023 	offset_t	off, end;
1024 	t_uscalar_t	dl_err;
1025 	queue_t		*q = dsp->ds_wq;
1026 
1027 	if (MBLKL(mp) < sizeof (dl_capability_req_t)) {
1028 		dl_err = DL_BADPRIM;
1029 		goto failed;
1030 	}
1031 
1032 	if (dsp->ds_dlstate == DL_UNATTACHED ||
1033 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
1034 		dl_err = DL_OUTSTATE;
1035 		goto failed;
1036 	}
1037 
1038 	/*
1039 	 * This request is overloaded. If there are no requested capabilities
1040 	 * then we just want to acknowledge with all the capabilities we
1041 	 * support. Otherwise we enable the set of capabilities requested.
1042 	 */
1043 	if (dlp->dl_sub_length == 0) {
1044 		proto_capability_advertise(dsp, mp);
1045 		return;
1046 	}
1047 
1048 	if (!MBLKIN(mp, dlp->dl_sub_offset, dlp->dl_sub_length)) {
1049 		dl_err = DL_BADPRIM;
1050 		goto failed;
1051 	}
1052 
1053 	dlp->dl_primitive = DL_CAPABILITY_ACK;
1054 
1055 	off = dlp->dl_sub_offset;
1056 	len = dlp->dl_sub_length;
1057 
1058 	/*
1059 	 * Walk the list of capabilities to be enabled.
1060 	 */
1061 	for (end = off + len; off < end; ) {
1062 		sp = (dl_capability_sub_t *)(mp->b_rptr + off);
1063 		size = sizeof (dl_capability_sub_t) + sp->dl_length;
1064 
1065 		if (off + size > end ||
1066 		    !IS_P2ALIGNED(off, sizeof (uint32_t))) {
1067 			dl_err = DL_BADPRIM;
1068 			goto failed;
1069 		}
1070 
1071 		switch (sp->dl_cap) {
1072 		/*
1073 		 * TCP/IP checksum offload to hardware.
1074 		 */
1075 		case DL_CAPAB_HCKSUM: {
1076 			dl_capab_hcksum_t *hcksump;
1077 			dl_capab_hcksum_t hcksum;
1078 
1079 			hcksump = (dl_capab_hcksum_t *)&sp[1];
1080 			/*
1081 			 * Copy for alignment.
1082 			 */
1083 			bcopy(hcksump, &hcksum, sizeof (dl_capab_hcksum_t));
1084 			dlcapabsetqid(&(hcksum.hcksum_mid), dsp->ds_rq);
1085 			bcopy(&hcksum, hcksump, sizeof (dl_capab_hcksum_t));
1086 			break;
1087 		}
1088 
1089 		case DL_CAPAB_DLD: {
1090 			dl_capab_dld_t	*dldp;
1091 			dl_capab_dld_t	dld;
1092 
1093 			dldp = (dl_capab_dld_t *)&sp[1];
1094 			/*
1095 			 * Copy for alignment.
1096 			 */
1097 			bcopy(dldp, &dld, sizeof (dl_capab_dld_t));
1098 			dlcapabsetqid(&(dld.dld_mid), dsp->ds_rq);
1099 			bcopy(&dld, dldp, sizeof (dl_capab_dld_t));
1100 			break;
1101 		}
1102 		default:
1103 			break;
1104 		}
1105 		off += size;
1106 	}
1107 	qreply(q, mp);
1108 	return;
1109 failed:
1110 	dlerrorack(q, mp, DL_CAPABILITY_REQ, dl_err, 0);
1111 }
1112 
1113 /*
1114  * DL_NOTIFY_REQ
1115  */
1116 static void
1117 proto_notify_req(dld_str_t *dsp, mblk_t *mp)
1118 {
1119 	dl_notify_req_t	*dlp = (dl_notify_req_t *)mp->b_rptr;
1120 	t_uscalar_t	dl_err;
1121 	queue_t		*q = dsp->ds_wq;
1122 	uint_t		note =
1123 	    DL_NOTE_PROMISC_ON_PHYS |
1124 	    DL_NOTE_PROMISC_OFF_PHYS |
1125 	    DL_NOTE_PHYS_ADDR |
1126 	    DL_NOTE_LINK_UP |
1127 	    DL_NOTE_LINK_DOWN |
1128 	    DL_NOTE_CAPAB_RENEG |
1129 	    DL_NOTE_FASTPATH_FLUSH |
1130 	    DL_NOTE_SPEED |
1131 	    DL_NOTE_SDU_SIZE;
1132 
1133 	if (MBLKL(mp) < sizeof (dl_notify_req_t)) {
1134 		dl_err = DL_BADPRIM;
1135 		goto failed;
1136 	}
1137 
1138 	if (dsp->ds_dlstate == DL_UNATTACHED ||
1139 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
1140 		dl_err = DL_OUTSTATE;
1141 		goto failed;
1142 	}
1143 
1144 	note &= ~(mac_no_notification(dsp->ds_mh));
1145 
1146 	/*
1147 	 * Cache the notifications that are being enabled.
1148 	 */
1149 	dsp->ds_notifications = dlp->dl_notifications & note;
1150 	/*
1151 	 * The ACK carries all notifications regardless of which set is
1152 	 * being enabled.
1153 	 */
1154 	dlnotifyack(q, mp, note);
1155 
1156 	/*
1157 	 * Generate DL_NOTIFY_IND messages for each enabled notification.
1158 	 */
1159 	if (dsp->ds_notifications != 0) {
1160 		dld_str_notify_ind(dsp);
1161 	}
1162 	return;
1163 failed:
1164 	dlerrorack(q, mp, DL_NOTIFY_REQ, dl_err, 0);
1165 }
1166 
1167 /*
1168  * DL_UINTDATA_REQ
1169  */
1170 void
1171 proto_unitdata_req(dld_str_t *dsp, mblk_t *mp)
1172 {
1173 	queue_t			*q = dsp->ds_wq;
1174 	dl_unitdata_req_t	*dlp = (dl_unitdata_req_t *)mp->b_rptr;
1175 	off_t			off;
1176 	size_t			len, size;
1177 	const uint8_t		*addr;
1178 	uint16_t		sap;
1179 	uint_t			addr_length;
1180 	mblk_t			*bp, *payload;
1181 	uint32_t		start, stuff, end, value, flags;
1182 	t_uscalar_t		dl_err;
1183 	uint_t			max_sdu;
1184 
1185 	if (MBLKL(mp) < sizeof (dl_unitdata_req_t) || mp->b_cont == NULL) {
1186 		dlerrorack(q, mp, DL_UNITDATA_REQ, DL_BADPRIM, 0);
1187 		return;
1188 	}
1189 
1190 	mutex_enter(&dsp->ds_lock);
1191 	if (dsp->ds_dlstate != DL_IDLE) {
1192 		mutex_exit(&dsp->ds_lock);
1193 		dlerrorack(q, mp, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
1194 		return;
1195 	}
1196 	DLD_DATATHR_INC(dsp);
1197 	mutex_exit(&dsp->ds_lock);
1198 
1199 	addr_length = dsp->ds_mip->mi_addr_length;
1200 
1201 	off = dlp->dl_dest_addr_offset;
1202 	len = dlp->dl_dest_addr_length;
1203 
1204 	if (!MBLKIN(mp, off, len) || !IS_P2ALIGNED(off, sizeof (uint16_t))) {
1205 		dl_err = DL_BADPRIM;
1206 		goto failed;
1207 	}
1208 
1209 	if (len != addr_length + sizeof (uint16_t)) {
1210 		dl_err = DL_BADADDR;
1211 		goto failed;
1212 	}
1213 
1214 	addr = mp->b_rptr + off;
1215 	sap = *(uint16_t *)(mp->b_rptr + off + addr_length);
1216 
1217 	/*
1218 	 * Check the length of the packet and the block types.
1219 	 */
1220 	size = 0;
1221 	payload = mp->b_cont;
1222 	for (bp = payload; bp != NULL; bp = bp->b_cont) {
1223 		if (DB_TYPE(bp) != M_DATA)
1224 			goto baddata;
1225 
1226 		size += MBLKL(bp);
1227 	}
1228 
1229 	mac_sdu_get(dsp->ds_mh, NULL, &max_sdu);
1230 	if (size > max_sdu)
1231 		goto baddata;
1232 
1233 	/*
1234 	 * Build a packet header.
1235 	 */
1236 	if ((bp = dls_header(dsp, addr, sap, dlp->dl_priority.dl_max,
1237 	    &payload)) == NULL) {
1238 		dl_err = DL_BADADDR;
1239 		goto failed;
1240 	}
1241 
1242 	/*
1243 	 * We no longer need the M_PROTO header, so free it.
1244 	 */
1245 	freeb(mp);
1246 
1247 	/*
1248 	 * Transfer the checksum offload information if it is present.
1249 	 */
1250 	hcksum_retrieve(payload, NULL, NULL, &start, &stuff, &end, &value,
1251 	    &flags);
1252 	(void) hcksum_assoc(bp, NULL, NULL, start, stuff, end, value, flags, 0);
1253 
1254 	/*
1255 	 * Link the payload onto the new header.
1256 	 */
1257 	ASSERT(bp->b_cont == NULL);
1258 	bp->b_cont = payload;
1259 
1260 	/*
1261 	 * No lock can be held across modules and putnext()'s,
1262 	 * which can happen here with the call from DLD_TX().
1263 	 */
1264 	if (DLD_TX(dsp, bp, 0, 0) != NULL) {
1265 		/* flow-controlled */
1266 		DLD_SETQFULL(dsp);
1267 	}
1268 	DLD_DATATHR_DCR(dsp);
1269 	return;
1270 
1271 failed:
1272 	dlerrorack(q, mp, DL_UNITDATA_REQ, dl_err, 0);
1273 	DLD_DATATHR_DCR(dsp);
1274 	return;
1275 
1276 baddata:
1277 	dluderrorind(q, mp, (void *)addr, len, DL_BADDATA, 0);
1278 	DLD_DATATHR_DCR(dsp);
1279 }
1280 
1281 /*
1282  * DL_PASSIVE_REQ
1283  */
1284 static void
1285 proto_passive_req(dld_str_t *dsp, mblk_t *mp)
1286 {
1287 	t_uscalar_t dl_err;
1288 
1289 	/*
1290 	 * If we've already become active by issuing an active primitive,
1291 	 * then it's too late to try to become passive.
1292 	 */
1293 	if (dsp->ds_passivestate == DLD_ACTIVE) {
1294 		dl_err = DL_OUTSTATE;
1295 		goto failed;
1296 	}
1297 
1298 	if (MBLKL(mp) < sizeof (dl_passive_req_t)) {
1299 		dl_err = DL_BADPRIM;
1300 		goto failed;
1301 	}
1302 
1303 	dsp->ds_passivestate = DLD_PASSIVE;
1304 	dlokack(dsp->ds_wq, mp, DL_PASSIVE_REQ);
1305 	return;
1306 failed:
1307 	dlerrorack(dsp->ds_wq, mp, DL_PASSIVE_REQ, dl_err, 0);
1308 }
1309 
1310 
1311 /*
1312  * Catch-all handler.
1313  */
1314 static void
1315 proto_req(dld_str_t *dsp, mblk_t *mp)
1316 {
1317 	union DL_primitives	*dlp = (union DL_primitives *)mp->b_rptr;
1318 
1319 	dlerrorack(dsp->ds_wq, mp, dlp->dl_primitive, DL_UNSUPPORTED, 0);
1320 }
1321 
1322 static int
1323 dld_capab_perim(dld_str_t *dsp, void *data, uint_t flags)
1324 {
1325 	switch (flags) {
1326 	case DLD_ENABLE:
1327 		mac_perim_enter_by_mh(dsp->ds_mh, (mac_perim_handle_t *)data);
1328 		return (0);
1329 
1330 	case DLD_DISABLE:
1331 		mac_perim_exit((mac_perim_handle_t)data);
1332 		return (0);
1333 
1334 	case DLD_QUERY:
1335 		return (mac_perim_held(dsp->ds_mh));
1336 	}
1337 	return (0);
1338 }
1339 
1340 static int
1341 dld_capab_direct(dld_str_t *dsp, void *data, uint_t flags)
1342 {
1343 	dld_capab_direct_t	*direct = data;
1344 
1345 	ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
1346 
1347 	switch (flags) {
1348 	case DLD_ENABLE:
1349 		dls_rx_set(dsp, (dls_rx_t)direct->di_rx_cf,
1350 		    direct->di_rx_ch);
1351 
1352 		direct->di_tx_df = (uintptr_t)str_mdata_fastpath_put;
1353 		direct->di_tx_dh = dsp;
1354 		direct->di_tx_cb_df = (uintptr_t)mac_client_tx_notify;
1355 		direct->di_tx_cb_dh = dsp->ds_mch;
1356 		direct->di_tx_fctl_df = (uintptr_t)mac_tx_is_flow_blocked;
1357 		direct->di_tx_fctl_dh = dsp->ds_mch;
1358 
1359 		dsp->ds_direct = B_TRUE;
1360 
1361 		return (0);
1362 
1363 	case DLD_DISABLE:
1364 		dls_rx_set(dsp, (dsp->ds_mode == DLD_FASTPATH) ?
1365 		    dld_str_rx_fastpath : dld_str_rx_unitdata, (void *)dsp);
1366 		dsp->ds_direct = B_FALSE;
1367 
1368 		return (0);
1369 	}
1370 	return (ENOTSUP);
1371 }
1372 
1373 /*
1374  * dld_capab_poll_enable()
1375  *
1376  * This function is misnamed. All polling  and fanouts are run out of the
1377  * lower mac (in case of VNIC and the only mac in case of NICs). The
1378  * availability of Rx ring and promiscous mode is all taken care between
1379  * the soft ring set (mac_srs), the Rx ring, and S/W classifier. Any
1380  * fanout necessary is done by the soft rings that are part of the
1381  * mac_srs (by default mac_srs sends the packets up via a TCP and
1382  * non TCP soft ring).
1383  *
1384  * The mac_srs (or its associated soft rings) always store the ill_rx_ring
1385  * (the cookie returned when they registered with IP during plumb) as their
1386  * 2nd argument which is passed up as mac_resource_handle_t. The upcall
1387  * function and 1st argument is what the caller registered when they
1388  * called mac_rx_classify_flow_add() to register the flow. For VNIC,
1389  * the function is vnic_rx and argument is vnic_t. For regular NIC
1390  * case, it mac_rx_default and mac_handle_t. As explained above, the
1391  * mac_srs (or its soft ring) will add the ill_rx_ring (mac_resource_handle_t)
1392  * from its stored 2nd argument.
1393  */
1394 static int
1395 dld_capab_poll_enable(dld_str_t *dsp, dld_capab_poll_t *poll)
1396 {
1397 	if (dsp->ds_polling)
1398 		return (EINVAL);
1399 
1400 	if ((dld_opt & DLD_OPT_NO_POLL) != 0 || dsp->ds_mode == DLD_RAW)
1401 		return (ENOTSUP);
1402 
1403 	/*
1404 	 * Enable client polling if and only if DLS bypass is possible.
1405 	 * Special cases like VLANs need DLS processing in the Rx data path.
1406 	 * In such a case we can neither allow the client (IP) to directly
1407 	 * poll the softring (since DLS processing hasn't been done) nor can
1408 	 * we allow DLS bypass.
1409 	 */
1410 	if (!mac_rx_bypass_set(dsp->ds_mch, dsp->ds_rx, dsp->ds_rx_arg))
1411 		return (ENOTSUP);
1412 
1413 	/*
1414 	 * Register soft ring resources. This will come in handy later if
1415 	 * the user decides to modify CPU bindings to use more CPUs for the
1416 	 * device in which case we will switch to fanout using soft rings.
1417 	 */
1418 	mac_resource_set_common(dsp->ds_mch,
1419 	    (mac_resource_add_t)poll->poll_ring_add_cf,
1420 	    (mac_resource_remove_t)poll->poll_ring_remove_cf,
1421 	    (mac_resource_quiesce_t)poll->poll_ring_quiesce_cf,
1422 	    (mac_resource_restart_t)poll->poll_ring_restart_cf,
1423 	    (mac_resource_bind_t)poll->poll_ring_bind_cf,
1424 	    poll->poll_ring_ch);
1425 
1426 	mac_client_poll_enable(dsp->ds_mch);
1427 
1428 	dsp->ds_polling = B_TRUE;
1429 	return (0);
1430 }
1431 
1432 /* ARGSUSED */
1433 static int
1434 dld_capab_poll_disable(dld_str_t *dsp, dld_capab_poll_t *poll)
1435 {
1436 	if (!dsp->ds_polling)
1437 		return (EINVAL);
1438 
1439 	mac_client_poll_disable(dsp->ds_mch);
1440 	mac_resource_set(dsp->ds_mch, NULL, NULL);
1441 
1442 	dsp->ds_polling = B_FALSE;
1443 	return (0);
1444 }
1445 
1446 static int
1447 dld_capab_poll(dld_str_t *dsp, void *data, uint_t flags)
1448 {
1449 	dld_capab_poll_t	*poll = data;
1450 
1451 	ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
1452 
1453 	switch (flags) {
1454 	case DLD_ENABLE:
1455 		return (dld_capab_poll_enable(dsp, poll));
1456 	case DLD_DISABLE:
1457 		return (dld_capab_poll_disable(dsp, poll));
1458 	}
1459 	return (ENOTSUP);
1460 }
1461 
1462 static int
1463 dld_capab_lso(dld_str_t *dsp, void *data, uint_t flags)
1464 {
1465 	dld_capab_lso_t		*lso = data;
1466 
1467 	ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
1468 
1469 	switch (flags) {
1470 	case DLD_ENABLE: {
1471 		mac_capab_lso_t		mac_lso;
1472 
1473 		/*
1474 		 * Check if LSO is supported on this MAC & enable LSO
1475 		 * accordingly.
1476 		 */
1477 		if (mac_capab_get(dsp->ds_mh, MAC_CAPAB_LSO, &mac_lso)) {
1478 			lso->lso_max = mac_lso.lso_basic_tcp_ipv4.lso_max;
1479 			lso->lso_flags = 0;
1480 			/* translate the flag for mac clients */
1481 			if ((mac_lso.lso_flags & LSO_TX_BASIC_TCP_IPV4) != 0)
1482 				lso->lso_flags |= DLD_LSO_BASIC_TCP_IPV4;
1483 			dsp->ds_lso = B_TRUE;
1484 			dsp->ds_lso_max = lso->lso_max;
1485 		} else {
1486 			dsp->ds_lso = B_FALSE;
1487 			dsp->ds_lso_max = 0;
1488 			return (ENOTSUP);
1489 		}
1490 		return (0);
1491 	}
1492 	case DLD_DISABLE: {
1493 		dsp->ds_lso = B_FALSE;
1494 		dsp->ds_lso_max = 0;
1495 		return (0);
1496 	}
1497 	}
1498 	return (ENOTSUP);
1499 }
1500 
1501 static int
1502 dld_capab(dld_str_t *dsp, uint_t type, void *data, uint_t flags)
1503 {
1504 	int	err;
1505 
1506 	/*
1507 	 * Don't enable direct callback capabilities unless the caller is
1508 	 * the IP client. When a module is inserted in a stream (_I_INSERT)
1509 	 * the stack initiates capability disable, but due to races, the
1510 	 * module insertion may complete before the capability disable
1511 	 * completes. So we limit the check to DLD_ENABLE case.
1512 	 */
1513 	if ((flags == DLD_ENABLE && type != DLD_CAPAB_PERIM) &&
1514 	    (dsp->ds_sap != ETHERTYPE_IP ||
1515 	    !check_mod_above(dsp->ds_rq, "ip"))) {
1516 		return (ENOTSUP);
1517 	}
1518 
1519 	switch (type) {
1520 	case DLD_CAPAB_DIRECT:
1521 		err = dld_capab_direct(dsp, data, flags);
1522 		break;
1523 
1524 	case DLD_CAPAB_POLL:
1525 		err =  dld_capab_poll(dsp, data, flags);
1526 		break;
1527 
1528 	case DLD_CAPAB_PERIM:
1529 		err = dld_capab_perim(dsp, data, flags);
1530 		break;
1531 
1532 	case DLD_CAPAB_LSO:
1533 		err = dld_capab_lso(dsp, data, flags);
1534 		break;
1535 
1536 	default:
1537 		err = ENOTSUP;
1538 		break;
1539 	}
1540 
1541 	return (err);
1542 }
1543 
1544 /*
1545  * DL_CAPABILITY_ACK/DL_ERROR_ACK
1546  */
1547 static void
1548 proto_capability_advertise(dld_str_t *dsp, mblk_t *mp)
1549 {
1550 	dl_capability_ack_t	*dlap;
1551 	dl_capability_sub_t	*dlsp;
1552 	size_t			subsize;
1553 	dl_capab_dld_t		dld;
1554 	dl_capab_hcksum_t	hcksum;
1555 	dl_capab_zerocopy_t	zcopy;
1556 	dl_capab_vrrp_t		vrrp;
1557 	mac_capab_vrrp_t	vrrp_capab;
1558 	uint8_t			*ptr;
1559 	queue_t			*q = dsp->ds_wq;
1560 	mblk_t			*mp1;
1561 	boolean_t		is_vlan;
1562 	boolean_t		hcksum_capable = B_FALSE;
1563 	boolean_t		zcopy_capable = B_FALSE;
1564 	boolean_t		dld_capable = B_FALSE;
1565 	boolean_t		vrrp_capable = B_FALSE;
1566 
1567 	/*
1568 	 * Initially assume no capabilities.
1569 	 */
1570 	subsize = 0;
1571 	is_vlan = (mac_client_vid(dsp->ds_mch) != VLAN_ID_NONE);
1572 
1573 	/*
1574 	 * Check if checksum offload is supported on this MAC.  Don't
1575 	 * advertise DL_CAPAB_HCKSUM if the underlying MAC is VLAN incapable,
1576 	 * since it might not be able to do the hardware checksum offload
1577 	 * with the correct offset.
1578 	 */
1579 	bzero(&hcksum, sizeof (dl_capab_hcksum_t));
1580 	if ((!is_vlan || (!mac_capab_get(dsp->ds_mh, MAC_CAPAB_NO_NATIVEVLAN,
1581 	    NULL))) && mac_capab_get(dsp->ds_mh, MAC_CAPAB_HCKSUM,
1582 	    &hcksum.hcksum_txflags)) {
1583 		if (hcksum.hcksum_txflags != 0) {
1584 			hcksum_capable = B_TRUE;
1585 			subsize += sizeof (dl_capability_sub_t) +
1586 			    sizeof (dl_capab_hcksum_t);
1587 		}
1588 	}
1589 
1590 	/*
1591 	 * Check if zerocopy is supported on this interface.
1592 	 * If advertising DL_CAPAB_ZEROCOPY has not been explicitly disabled
1593 	 * then reserve space for that capability.
1594 	 */
1595 	if (!mac_capab_get(dsp->ds_mh, MAC_CAPAB_NO_ZCOPY, NULL) &&
1596 	    !(dld_opt & DLD_OPT_NO_ZEROCOPY)) {
1597 		zcopy_capable = B_TRUE;
1598 		subsize += sizeof (dl_capability_sub_t) +
1599 		    sizeof (dl_capab_zerocopy_t);
1600 	}
1601 
1602 	/*
1603 	 * Direct capability negotiation interface between IP and DLD
1604 	 */
1605 	if (dsp->ds_sap == ETHERTYPE_IP && check_mod_above(dsp->ds_rq, "ip")) {
1606 		dld_capable = B_TRUE;
1607 		subsize += sizeof (dl_capability_sub_t) +
1608 		    sizeof (dl_capab_dld_t);
1609 	}
1610 
1611 	/*
1612 	 * Check if vrrp is supported on this interface. If so, reserve
1613 	 * space for that capability.
1614 	 */
1615 	if (mac_capab_get(dsp->ds_mh, MAC_CAPAB_VRRP, &vrrp_capab)) {
1616 		vrrp_capable = B_TRUE;
1617 		subsize += sizeof (dl_capability_sub_t) +
1618 		    sizeof (dl_capab_vrrp_t);
1619 	}
1620 
1621 	/*
1622 	 * If there are no capabilities to advertise or if we
1623 	 * can't allocate a response, send a DL_ERROR_ACK.
1624 	 */
1625 	if ((mp1 = reallocb(mp,
1626 	    sizeof (dl_capability_ack_t) + subsize, 0)) == NULL) {
1627 		dlerrorack(q, mp, DL_CAPABILITY_REQ, DL_NOTSUPPORTED, 0);
1628 		return;
1629 	}
1630 
1631 	mp = mp1;
1632 	DB_TYPE(mp) = M_PROTO;
1633 	mp->b_wptr = mp->b_rptr + sizeof (dl_capability_ack_t) + subsize;
1634 	bzero(mp->b_rptr, MBLKL(mp));
1635 	dlap = (dl_capability_ack_t *)mp->b_rptr;
1636 	dlap->dl_primitive = DL_CAPABILITY_ACK;
1637 	dlap->dl_sub_offset = sizeof (dl_capability_ack_t);
1638 	dlap->dl_sub_length = subsize;
1639 	ptr = (uint8_t *)&dlap[1];
1640 
1641 	/*
1642 	 * TCP/IP checksum offload.
1643 	 */
1644 	if (hcksum_capable) {
1645 		dlsp = (dl_capability_sub_t *)ptr;
1646 
1647 		dlsp->dl_cap = DL_CAPAB_HCKSUM;
1648 		dlsp->dl_length = sizeof (dl_capab_hcksum_t);
1649 		ptr += sizeof (dl_capability_sub_t);
1650 
1651 		hcksum.hcksum_version = HCKSUM_VERSION_1;
1652 		dlcapabsetqid(&(hcksum.hcksum_mid), dsp->ds_rq);
1653 		bcopy(&hcksum, ptr, sizeof (dl_capab_hcksum_t));
1654 		ptr += sizeof (dl_capab_hcksum_t);
1655 	}
1656 
1657 	/*
1658 	 * Zero copy
1659 	 */
1660 	if (zcopy_capable) {
1661 		dlsp = (dl_capability_sub_t *)ptr;
1662 
1663 		dlsp->dl_cap = DL_CAPAB_ZEROCOPY;
1664 		dlsp->dl_length = sizeof (dl_capab_zerocopy_t);
1665 		ptr += sizeof (dl_capability_sub_t);
1666 
1667 		bzero(&zcopy, sizeof (dl_capab_zerocopy_t));
1668 		zcopy.zerocopy_version = ZEROCOPY_VERSION_1;
1669 		zcopy.zerocopy_flags = DL_CAPAB_VMSAFE_MEM;
1670 
1671 		dlcapabsetqid(&(zcopy.zerocopy_mid), dsp->ds_rq);
1672 		bcopy(&zcopy, ptr, sizeof (dl_capab_zerocopy_t));
1673 		ptr += sizeof (dl_capab_zerocopy_t);
1674 	}
1675 
1676 	/*
1677 	 * VRRP capability negotiation
1678 	 */
1679 	if (vrrp_capable) {
1680 		dlsp = (dl_capability_sub_t *)ptr;
1681 		dlsp->dl_cap = DL_CAPAB_VRRP;
1682 		dlsp->dl_length = sizeof (dl_capab_vrrp_t);
1683 		ptr += sizeof (dl_capability_sub_t);
1684 
1685 		bzero(&vrrp, sizeof (dl_capab_vrrp_t));
1686 		vrrp.vrrp_af = vrrp_capab.mcv_af;
1687 		bcopy(&vrrp, ptr, sizeof (dl_capab_vrrp_t));
1688 		ptr += sizeof (dl_capab_vrrp_t);
1689 	}
1690 
1691 	/*
1692 	 * Direct capability negotiation interface between IP and DLD.
1693 	 * Refer to dld.h for details.
1694 	 */
1695 	if (dld_capable) {
1696 		dlsp = (dl_capability_sub_t *)ptr;
1697 		dlsp->dl_cap = DL_CAPAB_DLD;
1698 		dlsp->dl_length = sizeof (dl_capab_dld_t);
1699 		ptr += sizeof (dl_capability_sub_t);
1700 
1701 		bzero(&dld, sizeof (dl_capab_dld_t));
1702 		dld.dld_version = DLD_CURRENT_VERSION;
1703 		dld.dld_capab = (uintptr_t)dld_capab;
1704 		dld.dld_capab_handle = (uintptr_t)dsp;
1705 
1706 		dlcapabsetqid(&(dld.dld_mid), dsp->ds_rq);
1707 		bcopy(&dld, ptr, sizeof (dl_capab_dld_t));
1708 		ptr += sizeof (dl_capab_dld_t);
1709 	}
1710 
1711 	ASSERT(ptr == mp->b_rptr + sizeof (dl_capability_ack_t) + subsize);
1712 	qreply(q, mp);
1713 }
1714 
1715 /*
1716  * Disable any enabled capabilities.
1717  */
1718 void
1719 dld_capabilities_disable(dld_str_t *dsp)
1720 {
1721 	if (dsp->ds_polling)
1722 		(void) dld_capab_poll_disable(dsp, NULL);
1723 }
1724