xref: /titanic_50/usr/src/uts/common/io/dld/dld_proto.c (revision 07247649496653b0ece32ce7ea710057f85fa9da)
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 (dsp->ds_passivestate == DLD_UNINITIALIZED &&
434 	    ((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 	err = dls_bind(dsp, sap);
451 	if (err != 0) {
452 		switch (err) {
453 		case EINVAL:
454 			dl_err = DL_BADADDR;
455 			err = 0;
456 			break;
457 		default:
458 			dl_err = DL_SYSERR;
459 			break;
460 		}
461 
462 		dsp->ds_dlstate = DL_UNBOUND;
463 		if (dsp->ds_passivestate == DLD_UNINITIALIZED)
464 			dls_active_clear(dsp);
465 		goto failed2;
466 	}
467 
468 	intr_cpu = mac_client_intr_cpu(dsp->ds_mch);
469 	mdip = mac_get_devinfo(dsp->ds_mh);
470 	mac_perim_exit(mph);
471 
472 	/*
473 	 * We do this after we get out of the perim to avoid deadlocks
474 	 * etc. since part of mac_client_retarget_intr is to walk the
475 	 * device tree in order to find and retarget the interrupts.
476 	 */
477 	mac_client_set_intr_cpu(mdip, dsp->ds_mch, intr_cpu);
478 
479 	/*
480 	 * Copy in MAC address.
481 	 */
482 	dlsap_addr_length = dsp->ds_mip->mi_addr_length;
483 	mac_unicast_primary_get(dsp->ds_mh, dlsap_addr);
484 
485 	/*
486 	 * Copy in the SAP.
487 	 */
488 	*(uint16_t *)(dlsap_addr + dlsap_addr_length) = sap;
489 	dlsap_addr_length += sizeof (uint16_t);
490 
491 	dsp->ds_dlstate = DL_IDLE;
492 	if (dsp->ds_passivestate == DLD_UNINITIALIZED)
493 		dsp->ds_passivestate = DLD_ACTIVE;
494 
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 	if (dls_unbind(dsp) != 0) {
536 		dl_err = DL_OUTSTATE;
537 		mac_perim_exit(mph);
538 		goto failed;
539 	}
540 
541 	/*
542 	 * Disable polling mode, if it is enabled.
543 	 */
544 	(void) dld_capab_poll_disable(dsp, NULL);
545 
546 	/*
547 	 * Clear LSO flags.
548 	 */
549 	dsp->ds_lso = B_FALSE;
550 	dsp->ds_lso_max = 0;
551 
552 	/*
553 	 * Clear the receive callback.
554 	 */
555 	dls_rx_set(dsp, NULL, NULL);
556 	dsp->ds_direct = B_FALSE;
557 
558 	/*
559 	 * Set the mode back to the default (unitdata).
560 	 */
561 	dsp->ds_mode = DLD_UNITDATA;
562 	dsp->ds_dlstate = DL_UNBOUND;
563 
564 	mac_perim_exit(mph);
565 	dlokack(dsp->ds_wq, mp, DL_UNBIND_REQ);
566 	return;
567 failed:
568 	dlerrorack(q, mp, DL_UNBIND_REQ, dl_err, 0);
569 }
570 
571 /*
572  * DL_PROMISCON_REQ
573  */
574 static void
575 proto_promiscon_req(dld_str_t *dsp, mblk_t *mp)
576 {
577 	dl_promiscon_req_t *dlp = (dl_promiscon_req_t *)mp->b_rptr;
578 	int		err = 0;
579 	t_uscalar_t	dl_err;
580 	uint32_t	promisc_saved;
581 	queue_t		*q = dsp->ds_wq;
582 	mac_perim_handle_t	mph;
583 
584 	if (MBLKL(mp) < sizeof (dl_promiscon_req_t)) {
585 		dl_err = DL_BADPRIM;
586 		goto failed;
587 	}
588 
589 	if (dsp->ds_dlstate == DL_UNATTACHED ||
590 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
591 		dl_err = DL_OUTSTATE;
592 		goto failed;
593 	}
594 
595 	promisc_saved = dsp->ds_promisc;
596 	switch (dlp->dl_level) {
597 	case DL_PROMISC_SAP:
598 		dsp->ds_promisc |= DLS_PROMISC_SAP;
599 		break;
600 
601 	case DL_PROMISC_MULTI:
602 		dsp->ds_promisc |= DLS_PROMISC_MULTI;
603 		break;
604 
605 	case DL_PROMISC_PHYS:
606 		dsp->ds_promisc |= DLS_PROMISC_PHYS;
607 		break;
608 
609 	default:
610 		dl_err = DL_NOTSUPPORTED;
611 		goto failed;
612 	}
613 
614 	mac_perim_enter_by_mh(dsp->ds_mh, &mph);
615 
616 	if (dsp->ds_passivestate == DLD_UNINITIALIZED &&
617 	    ((err = dls_active_set(dsp)) != 0)) {
618 		dsp->ds_promisc = promisc_saved;
619 		dl_err = DL_SYSERR;
620 		goto failed2;
621 	}
622 
623 	/*
624 	 * Adjust channel promiscuity.
625 	 */
626 	err = dls_promisc(dsp, promisc_saved);
627 
628 	if (err != 0) {
629 		dl_err = DL_SYSERR;
630 		dsp->ds_promisc = promisc_saved;
631 		if (dsp->ds_passivestate == DLD_UNINITIALIZED)
632 			dls_active_clear(dsp);
633 		goto failed2;
634 	}
635 
636 	mac_perim_exit(mph);
637 
638 	if (dsp->ds_passivestate == DLD_UNINITIALIZED)
639 		dsp->ds_passivestate = DLD_ACTIVE;
640 	dlokack(q, mp, DL_PROMISCON_REQ);
641 	return;
642 
643 failed2:
644 	mac_perim_exit(mph);
645 failed:
646 	dlerrorack(q, mp, DL_PROMISCON_REQ, dl_err, (t_uscalar_t)err);
647 }
648 
649 /*
650  * DL_PROMISCOFF_REQ
651  */
652 static void
653 proto_promiscoff_req(dld_str_t *dsp, mblk_t *mp)
654 {
655 	dl_promiscoff_req_t *dlp = (dl_promiscoff_req_t *)mp->b_rptr;
656 	int		err = 0;
657 	t_uscalar_t	dl_err;
658 	uint32_t	promisc_saved;
659 	queue_t		*q = dsp->ds_wq;
660 	mac_perim_handle_t	mph;
661 
662 	if (MBLKL(mp) < sizeof (dl_promiscoff_req_t)) {
663 		dl_err = DL_BADPRIM;
664 		goto failed;
665 	}
666 
667 	if (dsp->ds_dlstate == DL_UNATTACHED ||
668 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
669 		dl_err = DL_OUTSTATE;
670 		goto failed;
671 	}
672 
673 	promisc_saved = dsp->ds_promisc;
674 	switch (dlp->dl_level) {
675 	case DL_PROMISC_SAP:
676 		if (!(dsp->ds_promisc & DLS_PROMISC_SAP)) {
677 			dl_err = DL_NOTENAB;
678 			goto failed;
679 		}
680 		dsp->ds_promisc &= ~DLS_PROMISC_SAP;
681 		break;
682 
683 	case DL_PROMISC_MULTI:
684 		if (!(dsp->ds_promisc & DLS_PROMISC_MULTI)) {
685 			dl_err = DL_NOTENAB;
686 			goto failed;
687 		}
688 		dsp->ds_promisc &= ~DLS_PROMISC_MULTI;
689 		break;
690 
691 	case DL_PROMISC_PHYS:
692 		if (!(dsp->ds_promisc & DLS_PROMISC_PHYS)) {
693 			dl_err = DL_NOTENAB;
694 			goto failed;
695 		}
696 		dsp->ds_promisc &= ~DLS_PROMISC_PHYS;
697 		break;
698 
699 	default:
700 		dl_err = DL_NOTSUPPORTED;
701 		goto failed;
702 	}
703 
704 	mac_perim_enter_by_mh(dsp->ds_mh, &mph);
705 	/*
706 	 * Adjust channel promiscuity.
707 	 */
708 	err = dls_promisc(dsp, promisc_saved);
709 	mac_perim_exit(mph);
710 
711 	if (err != 0) {
712 		dl_err = DL_SYSERR;
713 		goto failed;
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_passivestate == DLD_UNINITIALIZED &&
749 	    ((err = dls_active_set(dsp)) != 0)) {
750 		dl_err = DL_SYSERR;
751 		goto failed2;
752 	}
753 
754 	err = dls_multicst_add(dsp, mp->b_rptr + dlp->dl_addr_offset);
755 
756 	if (err != 0) {
757 		switch (err) {
758 		case EINVAL:
759 			dl_err = DL_BADADDR;
760 			err = 0;
761 			break;
762 		case ENOSPC:
763 			dl_err = DL_TOOMANY;
764 			err = 0;
765 			break;
766 		default:
767 			dl_err = DL_SYSERR;
768 			break;
769 		}
770 		if (dsp->ds_passivestate == DLD_UNINITIALIZED)
771 			dls_active_clear(dsp);
772 
773 		goto failed2;
774 	}
775 
776 	mac_perim_exit(mph);
777 
778 	if (dsp->ds_passivestate == DLD_UNINITIALIZED)
779 		dsp->ds_passivestate = DLD_ACTIVE;
780 	dlokack(q, mp, DL_ENABMULTI_REQ);
781 	return;
782 
783 failed2:
784 	mac_perim_exit(mph);
785 failed:
786 	dlerrorack(q, mp, DL_ENABMULTI_REQ, dl_err, (t_uscalar_t)err);
787 }
788 
789 /*
790  * DL_DISABMULTI_REQ
791  */
792 static void
793 proto_disabmulti_req(dld_str_t *dsp, mblk_t *mp)
794 {
795 	dl_disabmulti_req_t *dlp = (dl_disabmulti_req_t *)mp->b_rptr;
796 	int		err = 0;
797 	t_uscalar_t	dl_err;
798 	queue_t		*q = dsp->ds_wq;
799 	mac_perim_handle_t	mph;
800 
801 	if (dsp->ds_dlstate == DL_UNATTACHED ||
802 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
803 		dl_err = DL_OUTSTATE;
804 		goto failed;
805 	}
806 
807 	if (MBLKL(mp) < sizeof (dl_disabmulti_req_t) ||
808 	    !MBLKIN(mp, dlp->dl_addr_offset, dlp->dl_addr_length) ||
809 	    dlp->dl_addr_length != dsp->ds_mip->mi_addr_length) {
810 		dl_err = DL_BADPRIM;
811 		goto failed;
812 	}
813 
814 	mac_perim_enter_by_mh(dsp->ds_mh, &mph);
815 	err = dls_multicst_remove(dsp, mp->b_rptr + dlp->dl_addr_offset);
816 	mac_perim_exit(mph);
817 
818 	if (err != 0) {
819 	switch (err) {
820 		case EINVAL:
821 			dl_err = DL_BADADDR;
822 			err = 0;
823 			break;
824 
825 		case ENOENT:
826 			dl_err = DL_NOTENAB;
827 			err = 0;
828 			break;
829 
830 		default:
831 			dl_err = DL_SYSERR;
832 			break;
833 		}
834 		goto failed;
835 	}
836 	dlokack(q, mp, DL_DISABMULTI_REQ);
837 	return;
838 failed:
839 	dlerrorack(q, mp, DL_DISABMULTI_REQ, dl_err, (t_uscalar_t)err);
840 }
841 
842 /*
843  * DL_PHYS_ADDR_REQ
844  */
845 static void
846 proto_physaddr_req(dld_str_t *dsp, mblk_t *mp)
847 {
848 	dl_phys_addr_req_t *dlp = (dl_phys_addr_req_t *)mp->b_rptr;
849 	queue_t		*q = dsp->ds_wq;
850 	t_uscalar_t	dl_err;
851 	char		*addr;
852 	uint_t		addr_length;
853 
854 	if (MBLKL(mp) < sizeof (dl_phys_addr_req_t)) {
855 		dl_err = DL_BADPRIM;
856 		goto failed;
857 	}
858 
859 	if (dsp->ds_dlstate == DL_UNATTACHED ||
860 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
861 		dl_err = DL_OUTSTATE;
862 		goto failed;
863 	}
864 
865 	if (dlp->dl_addr_type != DL_CURR_PHYS_ADDR &&
866 	    dlp->dl_addr_type != DL_FACT_PHYS_ADDR) {
867 		dl_err = DL_UNSUPPORTED;
868 		goto failed;
869 	}
870 
871 	addr_length = dsp->ds_mip->mi_addr_length;
872 	if (addr_length > 0) {
873 		addr = kmem_alloc(addr_length, KM_SLEEP);
874 		if (dlp->dl_addr_type == DL_CURR_PHYS_ADDR)
875 			mac_unicast_primary_get(dsp->ds_mh, (uint8_t *)addr);
876 		else
877 			bcopy(dsp->ds_mip->mi_unicst_addr, addr, addr_length);
878 
879 		dlphysaddrack(q, mp, addr, (t_uscalar_t)addr_length);
880 		kmem_free(addr, addr_length);
881 	} else {
882 		dlphysaddrack(q, mp, NULL, 0);
883 	}
884 	return;
885 failed:
886 	dlerrorack(q, mp, DL_PHYS_ADDR_REQ, dl_err, 0);
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 (dsp->ds_passivestate == DLD_UNINITIALIZED &&
917 	    ((err = dls_active_set(dsp)) != 0)) {
918 		dl_err = DL_SYSERR;
919 		goto failed2;
920 	}
921 
922 	err = mac_unicast_primary_set(dsp->ds_mh,
923 	    mp->b_rptr + dlp->dl_addr_offset);
924 	if (err != 0) {
925 		switch (err) {
926 		case EINVAL:
927 			dl_err = DL_BADADDR;
928 			err = 0;
929 			break;
930 
931 		default:
932 			dl_err = DL_SYSERR;
933 			break;
934 		}
935 		if (dsp->ds_passivestate == DLD_UNINITIALIZED)
936 			dls_active_clear(dsp);
937 
938 		goto failed2;
939 
940 	}
941 
942 	mac_perim_exit(mph);
943 
944 	if (dsp->ds_passivestate == DLD_UNINITIALIZED)
945 		dsp->ds_passivestate = DLD_ACTIVE;
946 	dlokack(q, mp, DL_SET_PHYS_ADDR_REQ);
947 	return;
948 
949 failed2:
950 	mac_perim_exit(mph);
951 failed:
952 	dlerrorack(q, mp, DL_SET_PHYS_ADDR_REQ, dl_err, (t_uscalar_t)err);
953 }
954 
955 /*
956  * DL_UDQOS_REQ
957  */
958 static void
959 proto_udqos_req(dld_str_t *dsp, mblk_t *mp)
960 {
961 	dl_udqos_req_t *dlp = (dl_udqos_req_t *)mp->b_rptr;
962 	dl_qos_cl_sel1_t *selp;
963 	int		off, len;
964 	t_uscalar_t	dl_err;
965 	queue_t		*q = dsp->ds_wq;
966 
967 	off = dlp->dl_qos_offset;
968 	len = dlp->dl_qos_length;
969 
970 	if (MBLKL(mp) < sizeof (dl_udqos_req_t) || !MBLKIN(mp, off, len)) {
971 		dl_err = DL_BADPRIM;
972 		goto failed;
973 	}
974 
975 	selp = (dl_qos_cl_sel1_t *)(mp->b_rptr + off);
976 	if (selp->dl_qos_type != DL_QOS_CL_SEL1) {
977 		dl_err = DL_BADQOSTYPE;
978 		goto failed;
979 	}
980 
981 	if (selp->dl_priority > (1 << VLAN_PRI_SIZE) - 1 ||
982 	    selp->dl_priority < 0) {
983 		dl_err = DL_BADQOSPARAM;
984 		goto failed;
985 	}
986 
987 	dsp->ds_pri = selp->dl_priority;
988 	dlokack(q, mp, DL_UDQOS_REQ);
989 	return;
990 failed:
991 	dlerrorack(q, mp, DL_UDQOS_REQ, dl_err, 0);
992 }
993 
994 static boolean_t
995 check_ip_above(queue_t *q)
996 {
997 	queue_t		*next_q;
998 	boolean_t	ret = B_TRUE;
999 
1000 	claimstr(q);
1001 	next_q = q->q_next;
1002 	if (strcmp(next_q->q_qinfo->qi_minfo->mi_idname, "ip") != 0)
1003 		ret = B_FALSE;
1004 	releasestr(q);
1005 	return (ret);
1006 }
1007 
1008 /*
1009  * DL_CAPABILITY_REQ
1010  */
1011 static void
1012 proto_capability_req(dld_str_t *dsp, mblk_t *mp)
1013 {
1014 	dl_capability_req_t *dlp = (dl_capability_req_t *)mp->b_rptr;
1015 	dl_capability_sub_t *sp;
1016 	size_t		size, len;
1017 	offset_t	off, end;
1018 	t_uscalar_t	dl_err;
1019 	queue_t		*q = dsp->ds_wq;
1020 
1021 	if (MBLKL(mp) < sizeof (dl_capability_req_t)) {
1022 		dl_err = DL_BADPRIM;
1023 		goto failed;
1024 	}
1025 
1026 	if (dsp->ds_dlstate == DL_UNATTACHED ||
1027 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
1028 		dl_err = DL_OUTSTATE;
1029 		goto failed;
1030 	}
1031 
1032 	/*
1033 	 * This request is overloaded. If there are no requested capabilities
1034 	 * then we just want to acknowledge with all the capabilities we
1035 	 * support. Otherwise we enable the set of capabilities requested.
1036 	 */
1037 	if (dlp->dl_sub_length == 0) {
1038 		proto_capability_advertise(dsp, mp);
1039 		return;
1040 	}
1041 
1042 	if (!MBLKIN(mp, dlp->dl_sub_offset, dlp->dl_sub_length)) {
1043 		dl_err = DL_BADPRIM;
1044 		goto failed;
1045 	}
1046 
1047 	dlp->dl_primitive = DL_CAPABILITY_ACK;
1048 
1049 	off = dlp->dl_sub_offset;
1050 	len = dlp->dl_sub_length;
1051 
1052 	/*
1053 	 * Walk the list of capabilities to be enabled.
1054 	 */
1055 	for (end = off + len; off < end; ) {
1056 		sp = (dl_capability_sub_t *)(mp->b_rptr + off);
1057 		size = sizeof (dl_capability_sub_t) + sp->dl_length;
1058 
1059 		if (off + size > end ||
1060 		    !IS_P2ALIGNED(off, sizeof (uint32_t))) {
1061 			dl_err = DL_BADPRIM;
1062 			goto failed;
1063 		}
1064 
1065 		switch (sp->dl_cap) {
1066 		/*
1067 		 * TCP/IP checksum offload to hardware.
1068 		 */
1069 		case DL_CAPAB_HCKSUM: {
1070 			dl_capab_hcksum_t *hcksump;
1071 			dl_capab_hcksum_t hcksum;
1072 
1073 			hcksump = (dl_capab_hcksum_t *)&sp[1];
1074 			/*
1075 			 * Copy for alignment.
1076 			 */
1077 			bcopy(hcksump, &hcksum, sizeof (dl_capab_hcksum_t));
1078 			dlcapabsetqid(&(hcksum.hcksum_mid), dsp->ds_rq);
1079 			bcopy(&hcksum, hcksump, sizeof (dl_capab_hcksum_t));
1080 			break;
1081 		}
1082 
1083 		case DL_CAPAB_DLD: {
1084 			dl_capab_dld_t	*dldp;
1085 			dl_capab_dld_t	dld;
1086 
1087 			dldp = (dl_capab_dld_t *)&sp[1];
1088 			/*
1089 			 * Copy for alignment.
1090 			 */
1091 			bcopy(dldp, &dld, sizeof (dl_capab_dld_t));
1092 			dlcapabsetqid(&(dld.dld_mid), dsp->ds_rq);
1093 			bcopy(&dld, dldp, sizeof (dl_capab_dld_t));
1094 			break;
1095 		}
1096 		default:
1097 			break;
1098 		}
1099 		off += size;
1100 	}
1101 	qreply(q, mp);
1102 	return;
1103 failed:
1104 	dlerrorack(q, mp, DL_CAPABILITY_REQ, dl_err, 0);
1105 }
1106 
1107 /*
1108  * DL_NOTIFY_REQ
1109  */
1110 static void
1111 proto_notify_req(dld_str_t *dsp, mblk_t *mp)
1112 {
1113 	dl_notify_req_t	*dlp = (dl_notify_req_t *)mp->b_rptr;
1114 	t_uscalar_t	dl_err;
1115 	queue_t		*q = dsp->ds_wq;
1116 	uint_t		note =
1117 	    DL_NOTE_PROMISC_ON_PHYS |
1118 	    DL_NOTE_PROMISC_OFF_PHYS |
1119 	    DL_NOTE_PHYS_ADDR |
1120 	    DL_NOTE_LINK_UP |
1121 	    DL_NOTE_LINK_DOWN |
1122 	    DL_NOTE_CAPAB_RENEG |
1123 	    DL_NOTE_FASTPATH_FLUSH |
1124 	    DL_NOTE_SPEED;
1125 
1126 	if (MBLKL(mp) < sizeof (dl_notify_req_t)) {
1127 		dl_err = DL_BADPRIM;
1128 		goto failed;
1129 	}
1130 
1131 	if (dsp->ds_dlstate == DL_UNATTACHED ||
1132 	    DL_ACK_PENDING(dsp->ds_dlstate)) {
1133 		dl_err = DL_OUTSTATE;
1134 		goto failed;
1135 	}
1136 
1137 	note &= ~(mac_no_notification(dsp->ds_mh));
1138 
1139 	/*
1140 	 * Cache the notifications that are being enabled.
1141 	 */
1142 	dsp->ds_notifications = dlp->dl_notifications & note;
1143 	/*
1144 	 * The ACK carries all notifications regardless of which set is
1145 	 * being enabled.
1146 	 */
1147 	dlnotifyack(q, mp, note);
1148 
1149 	/*
1150 	 * Generate DL_NOTIFY_IND messages for each enabled notification.
1151 	 */
1152 	if (dsp->ds_notifications != 0) {
1153 		dld_str_notify_ind(dsp);
1154 	}
1155 	return;
1156 failed:
1157 	dlerrorack(q, mp, DL_NOTIFY_REQ, dl_err, 0);
1158 }
1159 
1160 /*
1161  * DL_UINTDATA_REQ
1162  */
1163 void
1164 proto_unitdata_req(dld_str_t *dsp, mblk_t *mp)
1165 {
1166 	queue_t			*q = dsp->ds_wq;
1167 	dl_unitdata_req_t	*dlp = (dl_unitdata_req_t *)mp->b_rptr;
1168 	off_t			off;
1169 	size_t			len, size;
1170 	const uint8_t		*addr;
1171 	uint16_t		sap;
1172 	uint_t			addr_length;
1173 	mblk_t			*bp, *payload;
1174 	uint32_t		start, stuff, end, value, flags;
1175 	t_uscalar_t		dl_err;
1176 	uint_t			max_sdu;
1177 
1178 	if (MBLKL(mp) < sizeof (dl_unitdata_req_t) || mp->b_cont == NULL) {
1179 		dlerrorack(q, mp, DL_UNITDATA_REQ, DL_BADPRIM, 0);
1180 		return;
1181 	}
1182 
1183 	mutex_enter(&dsp->ds_lock);
1184 	if (dsp->ds_dlstate != DL_IDLE) {
1185 		mutex_exit(&dsp->ds_lock);
1186 		dlerrorack(q, mp, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
1187 		return;
1188 	}
1189 	DLD_DATATHR_INC(dsp);
1190 	mutex_exit(&dsp->ds_lock);
1191 
1192 	addr_length = dsp->ds_mip->mi_addr_length;
1193 
1194 	off = dlp->dl_dest_addr_offset;
1195 	len = dlp->dl_dest_addr_length;
1196 
1197 	if (!MBLKIN(mp, off, len) || !IS_P2ALIGNED(off, sizeof (uint16_t))) {
1198 		dl_err = DL_BADPRIM;
1199 		goto failed;
1200 	}
1201 
1202 	if (len != addr_length + sizeof (uint16_t)) {
1203 		dl_err = DL_BADADDR;
1204 		goto failed;
1205 	}
1206 
1207 	addr = mp->b_rptr + off;
1208 	sap = *(uint16_t *)(mp->b_rptr + off + addr_length);
1209 
1210 	/*
1211 	 * Check the length of the packet and the block types.
1212 	 */
1213 	size = 0;
1214 	payload = mp->b_cont;
1215 	for (bp = payload; bp != NULL; bp = bp->b_cont) {
1216 		if (DB_TYPE(bp) != M_DATA)
1217 			goto baddata;
1218 
1219 		size += MBLKL(bp);
1220 	}
1221 
1222 	mac_sdu_get(dsp->ds_mh, NULL, &max_sdu);
1223 	if (size > max_sdu)
1224 		goto baddata;
1225 
1226 	/*
1227 	 * Build a packet header.
1228 	 */
1229 	if ((bp = dls_header(dsp, addr, sap, dlp->dl_priority.dl_max,
1230 	    &payload)) == NULL) {
1231 		dl_err = DL_BADADDR;
1232 		goto failed;
1233 	}
1234 
1235 	/*
1236 	 * We no longer need the M_PROTO header, so free it.
1237 	 */
1238 	freeb(mp);
1239 
1240 	/*
1241 	 * Transfer the checksum offload information if it is present.
1242 	 */
1243 	hcksum_retrieve(payload, NULL, NULL, &start, &stuff, &end, &value,
1244 	    &flags);
1245 	(void) hcksum_assoc(bp, NULL, NULL, start, stuff, end, value, flags, 0);
1246 
1247 	/*
1248 	 * Link the payload onto the new header.
1249 	 */
1250 	ASSERT(bp->b_cont == NULL);
1251 	bp->b_cont = payload;
1252 
1253 	/*
1254 	 * No lock can be held across modules and putnext()'s,
1255 	 * which can happen here with the call from DLD_TX().
1256 	 */
1257 	if (DLD_TX(dsp, bp, 0, 0) != NULL) {
1258 		/* flow-controlled */
1259 		DLD_SETQFULL(dsp);
1260 	}
1261 	DLD_DATATHR_DCR(dsp);
1262 	return;
1263 
1264 failed:
1265 	dlerrorack(q, mp, DL_UNITDATA_REQ, dl_err, 0);
1266 	DLD_DATATHR_DCR(dsp);
1267 	return;
1268 
1269 baddata:
1270 	dluderrorind(q, mp, (void *)addr, len, DL_BADDATA, 0);
1271 	DLD_DATATHR_DCR(dsp);
1272 }
1273 
1274 /*
1275  * DL_PASSIVE_REQ
1276  */
1277 static void
1278 proto_passive_req(dld_str_t *dsp, mblk_t *mp)
1279 {
1280 	t_uscalar_t dl_err;
1281 
1282 	/*
1283 	 * If we've already become active by issuing an active primitive,
1284 	 * then it's too late to try to become passive.
1285 	 */
1286 	if (dsp->ds_passivestate == DLD_ACTIVE) {
1287 		dl_err = DL_OUTSTATE;
1288 		goto failed;
1289 	}
1290 
1291 	if (MBLKL(mp) < sizeof (dl_passive_req_t)) {
1292 		dl_err = DL_BADPRIM;
1293 		goto failed;
1294 	}
1295 
1296 	dsp->ds_passivestate = DLD_PASSIVE;
1297 	dlokack(dsp->ds_wq, mp, DL_PASSIVE_REQ);
1298 	return;
1299 failed:
1300 	dlerrorack(dsp->ds_wq, mp, DL_PASSIVE_REQ, dl_err, 0);
1301 }
1302 
1303 
1304 /*
1305  * Catch-all handler.
1306  */
1307 static void
1308 proto_req(dld_str_t *dsp, mblk_t *mp)
1309 {
1310 	union DL_primitives	*dlp = (union DL_primitives *)mp->b_rptr;
1311 
1312 	dlerrorack(dsp->ds_wq, mp, dlp->dl_primitive, DL_UNSUPPORTED, 0);
1313 }
1314 
1315 static int
1316 dld_capab_perim(dld_str_t *dsp, void *data, uint_t flags)
1317 {
1318 	switch (flags) {
1319 	case DLD_ENABLE:
1320 		mac_perim_enter_by_mh(dsp->ds_mh, (mac_perim_handle_t *)data);
1321 		return (0);
1322 
1323 	case DLD_DISABLE:
1324 		mac_perim_exit((mac_perim_handle_t)data);
1325 		return (0);
1326 
1327 	case DLD_QUERY:
1328 		return (mac_perim_held(dsp->ds_mh));
1329 	}
1330 	return (0);
1331 }
1332 
1333 static int
1334 dld_capab_direct(dld_str_t *dsp, void *data, uint_t flags)
1335 {
1336 	dld_capab_direct_t	*direct = data;
1337 
1338 	ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
1339 
1340 	switch (flags) {
1341 	case DLD_ENABLE:
1342 		dls_rx_set(dsp, (dls_rx_t)direct->di_rx_cf,
1343 		    direct->di_rx_ch);
1344 
1345 		direct->di_tx_df = (uintptr_t)str_mdata_fastpath_put;
1346 		direct->di_tx_dh = dsp;
1347 		direct->di_tx_cb_df = (uintptr_t)mac_client_tx_notify;
1348 		direct->di_tx_cb_dh = dsp->ds_mch;
1349 		direct->di_tx_fctl_df = (uintptr_t)mac_tx_is_flow_blocked;
1350 		direct->di_tx_fctl_dh = dsp->ds_mch;
1351 
1352 		dsp->ds_direct = B_TRUE;
1353 
1354 		return (0);
1355 
1356 	case DLD_DISABLE:
1357 		dls_rx_set(dsp, (dsp->ds_mode == DLD_FASTPATH) ?
1358 		    dld_str_rx_fastpath : dld_str_rx_unitdata, (void *)dsp);
1359 		dsp->ds_direct = B_FALSE;
1360 
1361 		return (0);
1362 	}
1363 	return (ENOTSUP);
1364 }
1365 
1366 /*
1367  * dld_capab_poll_enable()
1368  *
1369  * This function is misnamed. All polling  and fanouts are run out of the
1370  * lower mac (in case of VNIC and the only mac in case of NICs). The
1371  * availability of Rx ring and promiscous mode is all taken care between
1372  * the soft ring set (mac_srs), the Rx ring, and S/W classifier. Any
1373  * fanout necessary is done by the soft rings that are part of the
1374  * mac_srs (by default mac_srs sends the packets up via a TCP and
1375  * non TCP soft ring).
1376  *
1377  * The mac_srs (or its associated soft rings) always store the ill_rx_ring
1378  * (the cookie returned when they registered with IP during plumb) as their
1379  * 2nd argument which is passed up as mac_resource_handle_t. The upcall
1380  * function and 1st argument is what the caller registered when they
1381  * called mac_rx_classify_flow_add() to register the flow. For VNIC,
1382  * the function is vnic_rx and argument is vnic_t. For regular NIC
1383  * case, it mac_rx_default and mac_handle_t. As explained above, the
1384  * mac_srs (or its soft ring) will add the ill_rx_ring (mac_resource_handle_t)
1385  * from its stored 2nd argument.
1386  */
1387 static int
1388 dld_capab_poll_enable(dld_str_t *dsp, dld_capab_poll_t *poll)
1389 {
1390 	if (dsp->ds_polling)
1391 		return (EINVAL);
1392 
1393 	if ((dld_opt & DLD_OPT_NO_POLL) != 0 || dsp->ds_mode == DLD_RAW)
1394 		return (ENOTSUP);
1395 
1396 	/*
1397 	 * Enable client polling if and only if DLS bypass is possible.
1398 	 * Special cases like VLANs need DLS processing in the Rx data path.
1399 	 * In such a case we can neither allow the client (IP) to directly
1400 	 * poll the softring (since DLS processing hasn't been done) nor can
1401 	 * we allow DLS bypass.
1402 	 */
1403 	if (!mac_rx_bypass_set(dsp->ds_mch, dsp->ds_rx, dsp->ds_rx_arg))
1404 		return (ENOTSUP);
1405 
1406 	/*
1407 	 * Register soft ring resources. This will come in handy later if
1408 	 * the user decides to modify CPU bindings to use more CPUs for the
1409 	 * device in which case we will switch to fanout using soft rings.
1410 	 */
1411 	mac_resource_set_common(dsp->ds_mch,
1412 	    (mac_resource_add_t)poll->poll_ring_add_cf,
1413 	    (mac_resource_remove_t)poll->poll_ring_remove_cf,
1414 	    (mac_resource_quiesce_t)poll->poll_ring_quiesce_cf,
1415 	    (mac_resource_restart_t)poll->poll_ring_restart_cf,
1416 	    (mac_resource_bind_t)poll->poll_ring_bind_cf,
1417 	    poll->poll_ring_ch);
1418 
1419 	mac_client_poll_enable(dsp->ds_mch);
1420 
1421 	dsp->ds_polling = B_TRUE;
1422 	return (0);
1423 }
1424 
1425 /* ARGSUSED */
1426 static int
1427 dld_capab_poll_disable(dld_str_t *dsp, dld_capab_poll_t *poll)
1428 {
1429 	if (!dsp->ds_polling)
1430 		return (EINVAL);
1431 
1432 	mac_client_poll_disable(dsp->ds_mch);
1433 	mac_resource_set(dsp->ds_mch, NULL, NULL);
1434 
1435 	dsp->ds_polling = B_FALSE;
1436 	return (0);
1437 }
1438 
1439 static int
1440 dld_capab_poll(dld_str_t *dsp, void *data, uint_t flags)
1441 {
1442 	dld_capab_poll_t	*poll = data;
1443 
1444 	ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
1445 
1446 	switch (flags) {
1447 	case DLD_ENABLE:
1448 		return (dld_capab_poll_enable(dsp, poll));
1449 	case DLD_DISABLE:
1450 		return (dld_capab_poll_disable(dsp, poll));
1451 	}
1452 	return (ENOTSUP);
1453 }
1454 
1455 static int
1456 dld_capab_lso(dld_str_t *dsp, void *data, uint_t flags)
1457 {
1458 	dld_capab_lso_t		*lso = data;
1459 
1460 	ASSERT(MAC_PERIM_HELD(dsp->ds_mh));
1461 
1462 	switch (flags) {
1463 	case DLD_ENABLE: {
1464 		mac_capab_lso_t		mac_lso;
1465 
1466 		/*
1467 		 * Check if LSO is supported on this MAC & enable LSO
1468 		 * accordingly.
1469 		 */
1470 		if (mac_capab_get(dsp->ds_mh, MAC_CAPAB_LSO, &mac_lso)) {
1471 			lso->lso_max = mac_lso.lso_basic_tcp_ipv4.lso_max;
1472 			lso->lso_flags = 0;
1473 			/* translate the flag for mac clients */
1474 			if ((mac_lso.lso_flags & LSO_TX_BASIC_TCP_IPV4) != 0)
1475 				lso->lso_flags |= DLD_LSO_TX_BASIC_TCP_IPV4;
1476 			dsp->ds_lso = B_TRUE;
1477 			dsp->ds_lso_max = lso->lso_max;
1478 		} else {
1479 			dsp->ds_lso = B_FALSE;
1480 			dsp->ds_lso_max = 0;
1481 			return (ENOTSUP);
1482 		}
1483 		return (0);
1484 	}
1485 	case DLD_DISABLE: {
1486 		dsp->ds_lso = B_FALSE;
1487 		dsp->ds_lso_max = 0;
1488 		return (0);
1489 	}
1490 	}
1491 	return (ENOTSUP);
1492 }
1493 
1494 static int
1495 dld_capab(dld_str_t *dsp, uint_t type, void *data, uint_t flags)
1496 {
1497 	int	err;
1498 
1499 	/*
1500 	 * Don't enable direct callback capabilities unless the caller is
1501 	 * the IP client. When a module is inserted in a stream (_I_INSERT)
1502 	 * the stack initiates capability disable, but due to races, the
1503 	 * module insertion may complete before the capability disable
1504 	 * completes. So we limit the check to DLD_ENABLE case.
1505 	 */
1506 	if ((flags == DLD_ENABLE && type != DLD_CAPAB_PERIM) &&
1507 	    (dsp->ds_sap != ETHERTYPE_IP || !check_ip_above(dsp->ds_rq))) {
1508 		return (ENOTSUP);
1509 	}
1510 
1511 	switch (type) {
1512 	case DLD_CAPAB_DIRECT:
1513 		err = dld_capab_direct(dsp, data, flags);
1514 		break;
1515 
1516 	case DLD_CAPAB_POLL:
1517 		err =  dld_capab_poll(dsp, data, flags);
1518 		break;
1519 
1520 	case DLD_CAPAB_PERIM:
1521 		err = dld_capab_perim(dsp, data, flags);
1522 		break;
1523 
1524 	case DLD_CAPAB_LSO:
1525 		err = dld_capab_lso(dsp, data, flags);
1526 		break;
1527 
1528 	default:
1529 		err = ENOTSUP;
1530 		break;
1531 	}
1532 
1533 	return (err);
1534 }
1535 
1536 /*
1537  * DL_CAPABILITY_ACK/DL_ERROR_ACK
1538  */
1539 static void
1540 proto_capability_advertise(dld_str_t *dsp, mblk_t *mp)
1541 {
1542 	dl_capability_ack_t	*dlap;
1543 	dl_capability_sub_t	*dlsp;
1544 	size_t			subsize;
1545 	dl_capab_dld_t		dld;
1546 	dl_capab_hcksum_t	hcksum;
1547 	dl_capab_zerocopy_t	zcopy;
1548 	uint8_t			*ptr;
1549 	queue_t			*q = dsp->ds_wq;
1550 	mblk_t			*mp1;
1551 	boolean_t		is_vlan;
1552 	boolean_t		hcksum_capable = B_FALSE;
1553 	boolean_t		zcopy_capable = B_FALSE;
1554 	boolean_t		dld_capable = B_FALSE;
1555 
1556 	/*
1557 	 * Initially assume no capabilities.
1558 	 */
1559 	subsize = 0;
1560 	is_vlan = (mac_client_vid(dsp->ds_mch) != VLAN_ID_NONE);
1561 
1562 	/*
1563 	 * Check if checksum offload is supported on this MAC.  Don't
1564 	 * advertise DL_CAPAB_HCKSUM if the underlying MAC is VLAN incapable,
1565 	 * since it might not be able to do the hardware checksum offload
1566 	 * with the correct offset.
1567 	 */
1568 	bzero(&hcksum, sizeof (dl_capab_hcksum_t));
1569 	if ((!is_vlan || (!mac_capab_get(dsp->ds_mh, MAC_CAPAB_NO_NATIVEVLAN,
1570 	    NULL))) && mac_capab_get(dsp->ds_mh, MAC_CAPAB_HCKSUM,
1571 	    &hcksum.hcksum_txflags)) {
1572 		if (hcksum.hcksum_txflags != 0) {
1573 			hcksum_capable = B_TRUE;
1574 			subsize += sizeof (dl_capability_sub_t) +
1575 			    sizeof (dl_capab_hcksum_t);
1576 		}
1577 	}
1578 
1579 	/*
1580 	 * Check if zerocopy is supported on this interface.
1581 	 * If advertising DL_CAPAB_ZEROCOPY has not been explicitly disabled
1582 	 * then reserve space for that capability.
1583 	 */
1584 	if (!mac_capab_get(dsp->ds_mh, MAC_CAPAB_NO_ZCOPY, NULL) &&
1585 	    !(dld_opt & DLD_OPT_NO_ZEROCOPY)) {
1586 		zcopy_capable = B_TRUE;
1587 		subsize += sizeof (dl_capability_sub_t) +
1588 		    sizeof (dl_capab_zerocopy_t);
1589 	}
1590 
1591 	/*
1592 	 * Direct capability negotiation interface between IP and DLD
1593 	 */
1594 	if (dsp->ds_sap == ETHERTYPE_IP && check_ip_above(dsp->ds_rq)) {
1595 		dld_capable = B_TRUE;
1596 		subsize += sizeof (dl_capability_sub_t) +
1597 		    sizeof (dl_capab_dld_t);
1598 	}
1599 
1600 	/*
1601 	 * If there are no capabilities to advertise or if we
1602 	 * can't allocate a response, send a DL_ERROR_ACK.
1603 	 */
1604 	if ((mp1 = reallocb(mp,
1605 	    sizeof (dl_capability_ack_t) + subsize, 0)) == NULL) {
1606 		dlerrorack(q, mp, DL_CAPABILITY_REQ, DL_NOTSUPPORTED, 0);
1607 		return;
1608 	}
1609 
1610 	mp = mp1;
1611 	DB_TYPE(mp) = M_PROTO;
1612 	mp->b_wptr = mp->b_rptr + sizeof (dl_capability_ack_t) + subsize;
1613 	bzero(mp->b_rptr, MBLKL(mp));
1614 	dlap = (dl_capability_ack_t *)mp->b_rptr;
1615 	dlap->dl_primitive = DL_CAPABILITY_ACK;
1616 	dlap->dl_sub_offset = sizeof (dl_capability_ack_t);
1617 	dlap->dl_sub_length = subsize;
1618 	ptr = (uint8_t *)&dlap[1];
1619 
1620 	/*
1621 	 * TCP/IP checksum offload.
1622 	 */
1623 	if (hcksum_capable) {
1624 		dlsp = (dl_capability_sub_t *)ptr;
1625 
1626 		dlsp->dl_cap = DL_CAPAB_HCKSUM;
1627 		dlsp->dl_length = sizeof (dl_capab_hcksum_t);
1628 		ptr += sizeof (dl_capability_sub_t);
1629 
1630 		hcksum.hcksum_version = HCKSUM_VERSION_1;
1631 		dlcapabsetqid(&(hcksum.hcksum_mid), dsp->ds_rq);
1632 		bcopy(&hcksum, ptr, sizeof (dl_capab_hcksum_t));
1633 		ptr += sizeof (dl_capab_hcksum_t);
1634 	}
1635 
1636 	/*
1637 	 * Zero copy
1638 	 */
1639 	if (zcopy_capable) {
1640 		dlsp = (dl_capability_sub_t *)ptr;
1641 
1642 		dlsp->dl_cap = DL_CAPAB_ZEROCOPY;
1643 		dlsp->dl_length = sizeof (dl_capab_zerocopy_t);
1644 		ptr += sizeof (dl_capability_sub_t);
1645 
1646 		bzero(&zcopy, sizeof (dl_capab_zerocopy_t));
1647 		zcopy.zerocopy_version = ZEROCOPY_VERSION_1;
1648 		zcopy.zerocopy_flags = DL_CAPAB_VMSAFE_MEM;
1649 
1650 		dlcapabsetqid(&(zcopy.zerocopy_mid), dsp->ds_rq);
1651 		bcopy(&zcopy, ptr, sizeof (dl_capab_zerocopy_t));
1652 		ptr += sizeof (dl_capab_zerocopy_t);
1653 	}
1654 
1655 	/*
1656 	 * Direct capability negotiation interface between IP and DLD.
1657 	 * Refer to dld.h for details.
1658 	 */
1659 	if (dld_capable) {
1660 		dlsp = (dl_capability_sub_t *)ptr;
1661 		dlsp->dl_cap = DL_CAPAB_DLD;
1662 		dlsp->dl_length = sizeof (dl_capab_dld_t);
1663 		ptr += sizeof (dl_capability_sub_t);
1664 
1665 		bzero(&dld, sizeof (dl_capab_dld_t));
1666 		dld.dld_version = DLD_CURRENT_VERSION;
1667 		dld.dld_capab = (uintptr_t)dld_capab;
1668 		dld.dld_capab_handle = (uintptr_t)dsp;
1669 
1670 		dlcapabsetqid(&(dld.dld_mid), dsp->ds_rq);
1671 		bcopy(&dld, ptr, sizeof (dl_capab_dld_t));
1672 		ptr += sizeof (dl_capab_dld_t);
1673 	}
1674 
1675 	ASSERT(ptr == mp->b_rptr + sizeof (dl_capability_ack_t) + subsize);
1676 	qreply(q, mp);
1677 }
1678 
1679 /*
1680  * Disable any enabled capabilities.
1681  */
1682 void
1683 dld_capabilities_disable(dld_str_t *dsp)
1684 {
1685 	if (dsp->ds_polling)
1686 		(void) dld_capab_poll_disable(dsp, NULL);
1687 }
1688