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