xref: /illumos-gate/usr/src/uts/common/io/softmac/softmac_dev.c (revision 98c507c4288789fc67365c4cb51f80eb641e7182)
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 #include <sys/types.h>
28 #include <inet/common.h>
29 #include <sys/stropts.h>
30 #include <sys/modctl.h>
31 #include <sys/dld.h>
32 #include <sys/softmac_impl.h>
33 
34 dev_info_t		*softmac_dip = NULL;
35 static kmem_cache_t	*softmac_upper_cachep;
36 
37 /*
38  * This function is a generic open(9E) entry point into the softmac for
39  * both the softmac module and the softmac driver.
40  */
41 static int softmac_cmn_open(queue_t *, dev_t *, int, int, cred_t *);
42 
43 /*
44  * The following softmac_mod_xxx() functions are (9E) entry point functions for
45  * the softmac module.
46  */
47 static int softmac_mod_close(queue_t *);
48 static void softmac_mod_rput(queue_t *, mblk_t *);
49 static void softmac_mod_wput(queue_t *, mblk_t *);
50 static void softmac_mod_wsrv(queue_t *);
51 
52 /*
53  * The following softmac_drv_xxx() functions are (9E) entry point functions for
54  * the softmac driver.
55  */
56 static int softmac_drv_open(queue_t *, dev_t *, int, int, cred_t *);
57 static int softmac_drv_close(queue_t *);
58 static void softmac_drv_wput(queue_t *, mblk_t *);
59 static void softmac_drv_wsrv(queue_t *);
60 
61 static int softmac_attach(dev_info_t *, ddi_attach_cmd_t);
62 static int softmac_detach(dev_info_t *, ddi_detach_cmd_t);
63 static int softmac_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
64 
65 static struct module_info softmac_modinfo = {
66 	0,
67 	SOFTMAC_DEV_NAME,
68 	0,
69 	INFPSZ,
70 	65536,
71 	1024
72 };
73 
74 /*
75  * hi-water mark is 1 because of the flow control mechanism implemented in
76  * dld.  Refer to the comments in dld_str.c for details.
77  */
78 static struct module_info softmac_dld_modinfo = {
79 	0,
80 	SOFTMAC_DEV_NAME,
81 	0,
82 	INFPSZ,
83 	1,
84 	0
85 };
86 
87 static struct qinit softmac_urinit = {
88 	(pfi_t)softmac_mod_rput,	/* qi_putp */
89 	(pfi_t)NULL,			/* qi_srvp */
90 	softmac_cmn_open,		/* qi_qopen */
91 	softmac_mod_close,		/* qi_qclose */
92 	NULL,				/* qi_qadmin */
93 	&softmac_modinfo		/* qi_minfo */
94 };
95 
96 static struct qinit softmac_uwinit = {
97 	(pfi_t)softmac_mod_wput,	/* qi_putp */
98 	(pfi_t)softmac_mod_wsrv,	/* qi_srvp */
99 	NULL,				/* qi_qopen */
100 	NULL,				/* qi_qclose */
101 	NULL,				/* qi_qadmin */
102 	&softmac_modinfo		/* qi_minfo */
103 };
104 
105 static struct streamtab softmac_tab = {
106 	&softmac_urinit,	/* st_rdinit */
107 	&softmac_uwinit		/* st_wrinit */
108 };
109 
110 DDI_DEFINE_STREAM_OPS(softmac_ops, nulldev, nulldev, softmac_attach,
111     softmac_detach, nodev, softmac_info, D_MP, &softmac_tab,
112     ddi_quiesce_not_supported);
113 
114 static struct qinit softmac_dld_r_qinit = {
115 	NULL, NULL, softmac_drv_open, softmac_drv_close, NULL,
116 	&softmac_dld_modinfo
117 };
118 
119 static struct qinit softmac_dld_w_qinit = {
120 	(pfi_t)softmac_drv_wput, (pfi_t)softmac_drv_wsrv, NULL, NULL, NULL,
121 	&softmac_dld_modinfo
122 };
123 
124 static struct fmodsw softmac_fmodsw = {
125 	SOFTMAC_DEV_NAME,
126 	&softmac_tab,
127 	D_MP
128 };
129 
130 static struct modldrv softmac_modldrv = {
131 	&mod_driverops,
132 	"softmac driver",
133 	&softmac_ops
134 };
135 
136 static struct modlstrmod softmac_modlstrmod = {
137 	&mod_strmodops,
138 	"softmac module",
139 	&softmac_fmodsw
140 };
141 
142 static struct modlinkage softmac_modlinkage = {
143 	MODREV_1,
144 	&softmac_modlstrmod,
145 	&softmac_modldrv,
146 	NULL
147 };
148 
149 /*ARGSUSED*/
150 static int
151 softmac_upper_constructor(void *buf, void *arg, int kmflag)
152 {
153 	softmac_upper_t	*sup = buf;
154 
155 	bzero(buf, sizeof (softmac_upper_t));
156 
157 	mutex_init(&sup->su_mutex, NULL, MUTEX_DEFAULT, NULL);
158 	cv_init(&sup->su_cv, NULL, CV_DEFAULT, NULL);
159 	mutex_init(&sup->su_disp_mutex, NULL, MUTEX_DEFAULT, NULL);
160 	cv_init(&sup->su_disp_cv, NULL, CV_DEFAULT, NULL);
161 	list_create(&sup->su_req_list, sizeof (softmac_switch_req_t),
162 	    offsetof(softmac_switch_req_t, ssq_req_list_node));
163 	return (0);
164 }
165 
166 /*ARGSUSED*/
167 static void
168 softmac_upper_destructor(void *buf, void *arg)
169 {
170 	softmac_upper_t	*sup = buf;
171 
172 	ASSERT(sup->su_slp == NULL);
173 	ASSERT(sup->su_pending_head == NULL && sup->su_pending_tail == NULL);
174 	ASSERT(!sup->su_dlpi_pending);
175 	ASSERT(!sup->su_active);
176 	ASSERT(!sup->su_closing);
177 	ASSERT(sup->su_tx_flow_mp == NULL);
178 	ASSERT(sup->su_tx_inprocess == 0);
179 	ASSERT(sup->su_mode == SOFTMAC_UNKNOWN);
180 	ASSERT(!sup->su_tx_busy);
181 	ASSERT(!sup->su_bound);
182 	ASSERT(!sup->su_taskq_scheduled);
183 	ASSERT(sup->su_tx_notify_func == NULL);
184 	ASSERT(sup->su_tx_notify_arg == NULL);
185 	ASSERT(list_is_empty(&sup->su_req_list));
186 
187 	list_destroy(&sup->su_req_list);
188 	mutex_destroy(&sup->su_mutex);
189 	cv_destroy(&sup->su_cv);
190 	mutex_destroy(&sup->su_disp_mutex);
191 	cv_destroy(&sup->su_disp_cv);
192 }
193 
194 int
195 _init(void)
196 {
197 	int	err;
198 
199 	mac_init_ops(NULL, SOFTMAC_DEV_NAME);
200 	softmac_init();
201 
202 	softmac_upper_cachep = kmem_cache_create("softmac_upper_cache",
203 	    sizeof (softmac_upper_t), 0, softmac_upper_constructor,
204 	    softmac_upper_destructor, NULL, NULL, NULL, 0);
205 	ASSERT(softmac_upper_cachep != NULL);
206 
207 	if ((err = mod_install(&softmac_modlinkage)) != 0) {
208 		softmac_fini();
209 		return (err);
210 	}
211 
212 	return (0);
213 }
214 
215 int
216 _fini(void)
217 {
218 	int err;
219 
220 	if (softmac_busy())
221 		return (EBUSY);
222 
223 	if ((err = mod_remove(&softmac_modlinkage)) != 0)
224 		return (err);
225 
226 	kmem_cache_destroy(softmac_upper_cachep);
227 	softmac_fini();
228 
229 	return (0);
230 }
231 
232 int
233 _info(struct modinfo *modinfop)
234 {
235 	return (mod_info(&softmac_modlinkage, modinfop));
236 }
237 
238 static int
239 softmac_cmn_open(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *credp)
240 {
241 	softmac_lower_t	*slp;
242 	/*
243 	 * This is a self-cloning driver so that each queue should only
244 	 * get opened once.
245 	 */
246 	if (rq->q_ptr != NULL)
247 		return (EBUSY);
248 
249 	if (sflag == MODOPEN) {
250 		/*
251 		 * This is the softmac module pushed over an underlying
252 		 * legacy device.  Initialize the lower structure.
253 		 */
254 		if ((slp = kmem_zalloc(sizeof (*slp), KM_NOSLEEP)) == NULL)
255 			return (ENOMEM);
256 
257 		slp->sl_wq = WR(rq);
258 		cv_init(&slp->sl_cv, NULL, CV_DRIVER, NULL);
259 		mutex_init(&slp->sl_mutex, NULL, MUTEX_DRIVER, NULL);
260 		slp->sl_pending_prim = DL_PRIM_INVAL;
261 		rq->q_ptr = WR(rq)->q_ptr = slp;
262 		qprocson(rq);
263 		return (0);
264 	}
265 
266 	/*
267 	 * Regular device open of a softmac DLPI node.  We modify
268 	 * the queues' q_qinfo pointer such that all future STREAMS
269 	 * operations will go through another set of entry points
270 	 */
271 	rq->q_qinfo = &softmac_dld_r_qinit;
272 	WR(rq)->q_qinfo = &softmac_dld_w_qinit;
273 	return (softmac_drv_open(rq, devp, flag, sflag, credp));
274 }
275 
276 static int
277 softmac_mod_close(queue_t *rq)
278 {
279 	softmac_lower_t	*slp = rq->q_ptr;
280 
281 	/*
282 	 * Call the appropriate delete routine depending on whether this is
283 	 * a module or device.
284 	 */
285 	ASSERT(WR(rq)->q_next != NULL);
286 
287 	qprocsoff(rq);
288 
289 	slp->sl_softmac = NULL;
290 	slp->sl_lh = NULL;
291 
292 	ASSERT(slp->sl_ack_mp == NULL);
293 	ASSERT(slp->sl_pending_prim == DL_PRIM_INVAL);
294 	ASSERT(slp->sl_pending_ioctl == B_FALSE);
295 
296 	cv_destroy(&slp->sl_cv);
297 	mutex_destroy(&slp->sl_mutex);
298 
299 	kmem_free(slp, sizeof (*slp));
300 	return (0);
301 }
302 
303 static void
304 softmac_mod_rput(queue_t *rq, mblk_t *mp)
305 {
306 	softmac_lower_t		*slp = rq->q_ptr;
307 	softmac_lower_rxinfo_t	*rxinfo;
308 	union DL_primitives	*dlp;
309 
310 	/*
311 	 * This is the softmac module.
312 	 */
313 	ASSERT(WR(rq)->q_next != NULL);
314 	ASSERT((mp->b_next == NULL) && (mp->b_prev == NULL));
315 
316 	switch (DB_TYPE(mp)) {
317 	case M_DATA: {
318 
319 		/*
320 		 * If sl_rxinfo is non-NULL. This is dedicated-lower-stream
321 		 * created for fastpath. Directly call the rx callback.
322 		 */
323 		if ((rxinfo = slp->sl_rxinfo) != NULL) {
324 			rxinfo->slr_rx(rxinfo->slr_arg, NULL, mp, NULL);
325 			break;
326 		}
327 
328 		/*
329 		 * A shared-lower-stream. Some driver starts to send up
330 		 * packets even it not in the DL_IDLE state, where
331 		 * sl_softmac is not set yet. Drop the packet in this case.
332 		 */
333 		if (slp->sl_softmac == NULL) {
334 			freemsg(mp);
335 			return;
336 		}
337 
338 		/*
339 		 * If this message is looped back from the legacy devices,
340 		 * drop it as the Nemo framework will be responsible for
341 		 * looping it back by the mac_txloop() function.
342 		 */
343 		if (mp->b_flag & MSGNOLOOP) {
344 			freemsg(mp);
345 			return;
346 		}
347 
348 		/*
349 		 * This is the most common case.
350 		 */
351 		if (DB_REF(mp) == 1) {
352 			ASSERT(slp->sl_softmac != NULL);
353 			mac_rx(slp->sl_softmac->smac_mh, NULL, mp);
354 			return;
355 		} else {
356 			softmac_rput_process_data(slp, mp);
357 		}
358 		break;
359 	}
360 	case M_PROTO:
361 	case M_PCPROTO:
362 		if (MBLKL(mp) < sizeof (dlp->dl_primitive)) {
363 			freemsg(mp);
364 			break;
365 		}
366 		dlp = (union DL_primitives *)mp->b_rptr;
367 		if (dlp->dl_primitive == DL_UNITDATA_IND) {
368 
369 			if ((rxinfo = slp->sl_rxinfo) != NULL) {
370 				rxinfo->slr_rx(rxinfo->slr_arg, NULL, mp, NULL);
371 				break;
372 			}
373 
374 			cmn_err(CE_WARN, "got unexpected %s message",
375 			    dl_primstr(DL_UNITDATA_IND));
376 			freemsg(mp);
377 			break;
378 		}
379 		/*FALLTHROUGH*/
380 	default:
381 		softmac_rput_process_notdata(rq, slp->sl_sup, mp);
382 		break;
383 	}
384 }
385 
386 static void
387 softmac_mod_wput(queue_t *wq, mblk_t *mp)
388 {
389 	/*
390 	 * This is the softmac module
391 	 */
392 	ASSERT(wq->q_next != NULL);
393 
394 	switch (DB_TYPE(mp)) {
395 	case M_IOCTL: {
396 		struct iocblk		*ioc = (struct iocblk *)mp->b_rptr;
397 
398 		switch (ioc->ioc_cmd) {
399 		case SMAC_IOC_START: {
400 			softmac_lower_t		*slp = wq->q_ptr;
401 			smac_ioc_start_t	*arg;
402 
403 			if (ioc->ioc_count != sizeof (*arg)) {
404 				miocnak(wq, mp, 0, EINVAL);
405 				break;
406 			}
407 
408 			/*
409 			 * Assign the devname and perstream handle of the
410 			 * specific lower stream and return it as a part
411 			 * of the ioctl.
412 			 */
413 			arg = (smac_ioc_start_t *)mp->b_cont->b_rptr;
414 			arg->si_slp = slp;
415 			miocack(wq, mp, sizeof (*arg), 0);
416 			break;
417 		}
418 		default:
419 			miocnak(wq, mp, 0, EINVAL);
420 			break;
421 		}
422 		break;
423 	}
424 	default:
425 		freemsg(mp);
426 		break;
427 	}
428 }
429 
430 static void
431 softmac_mod_wsrv(queue_t *wq)
432 {
433 	softmac_lower_t *slp = wq->q_ptr;
434 
435 	/*
436 	 * This is the softmac module
437 	 */
438 	ASSERT(wq->q_next != NULL);
439 
440 	/*
441 	 * Inform that the tx resource is available; mac_tx_update() will
442 	 * inform all the upper streams sharing this lower stream.
443 	 */
444 	if (slp->sl_sup != NULL)
445 		qenable(slp->sl_sup->su_wq);
446 	else if (slp->sl_softmac != NULL)
447 		mac_tx_update(slp->sl_softmac->smac_mh);
448 }
449 
450 static int
451 softmac_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
452 {
453 	ASSERT(ddi_get_instance(dip) == 0);
454 
455 	if (cmd != DDI_ATTACH)
456 		return (DDI_FAILURE);
457 
458 	softmac_dip = dip;
459 
460 	return (DDI_SUCCESS);
461 }
462 
463 /* ARGSUSED */
464 static int
465 softmac_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
466 {
467 	if (cmd != DDI_DETACH)
468 		return (DDI_FAILURE);
469 
470 	softmac_dip = NULL;
471 	return (DDI_SUCCESS);
472 }
473 
474 /* ARGSUSED */
475 static int
476 softmac_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
477 {
478 	switch (infocmd) {
479 	case DDI_INFO_DEVT2DEVINFO:
480 		if (softmac_dip != NULL) {
481 			*result = softmac_dip;
482 			return (DDI_SUCCESS);
483 		}
484 		break;
485 
486 	case DDI_INFO_DEVT2INSTANCE:
487 		*result = NULL;
488 		return (DDI_SUCCESS);
489 
490 	}
491 
492 	return (DDI_FAILURE);
493 }
494 
495 /*ARGSUSED*/
496 static void
497 softmac_dedicated_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
498     mac_header_info_t *mhip)
499 {
500 	queue_t *rq = ((softmac_upper_t *)arg)->su_rq;
501 
502 	if (canputnext(rq))
503 		putnext(rq, mp);
504 	else
505 		freemsg(mp);
506 }
507 
508 /*ARGSUSED*/
509 static int
510 softmac_drv_open(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *credp)
511 {
512 	softmac_upper_t	*sup = NULL;
513 	softmac_t	*softmac;
514 	int		err = 0;
515 
516 	/*
517 	 * This is a softmac device created for a legacy device, find the
518 	 * associated softmac and initialize the softmac_upper_t structure.
519 	 */
520 	if ((err = softmac_hold(*devp, &softmac)) != 0)
521 		return (err);
522 
523 	sup = kmem_cache_alloc(softmac_upper_cachep, KM_NOSLEEP);
524 	if (sup == NULL) {
525 		err = ENOMEM;
526 		goto fail;
527 	}
528 
529 	ASSERT(list_is_empty(&sup->su_req_list));
530 
531 	if ((sup->su_tx_flow_mp = allocb(1, BPRI_HI)) == NULL) {
532 		err = ENOMEM;
533 		goto fail;
534 	}
535 
536 	sup->su_rq = rq;
537 	sup->su_wq = WR(rq);
538 	sup->su_softmac = softmac;
539 	sup->su_mode = SOFTMAC_UNKNOWN;
540 
541 	sup->su_rxinfo.slr_arg = sup;
542 	sup->su_rxinfo.slr_rx = softmac_dedicated_rx;
543 	sup->su_direct_rxinfo.slr_arg = sup;
544 	sup->su_direct_rxinfo.slr_rx = softmac_dedicated_rx;
545 
546 	if ((err = dld_str_open(rq, devp, sup)) != 0) {
547 		freeb(sup->su_tx_flow_mp);
548 		sup->su_tx_flow_mp = NULL;
549 		goto fail;
550 	}
551 
552 	return (0);
553 
554 fail:
555 	if (sup != NULL)
556 		kmem_cache_free(softmac_upper_cachep, sup);
557 	softmac_rele(softmac);
558 	return (err);
559 }
560 
561 static int
562 softmac_drv_close(queue_t *rq)
563 {
564 	softmac_upper_t	*sup = dld_str_private(rq);
565 	softmac_t	*softmac = sup->su_softmac;
566 
567 	ASSERT(WR(rq)->q_next == NULL);
568 
569 	qprocsoff(rq);
570 
571 	ASSERT(sup->su_tx_inprocess == 0);
572 
573 	/*
574 	 * Wait until the pending request are processed by the worker thread.
575 	 */
576 	mutex_enter(&sup->su_disp_mutex);
577 	sup->su_closing = B_TRUE;
578 	while (sup->su_dlpi_pending)
579 		cv_wait(&sup->su_disp_cv, &sup->su_disp_mutex);
580 	mutex_exit(&sup->su_disp_mutex);
581 
582 	softmac_upperstream_close(sup);
583 
584 	if (sup->su_tx_flow_mp != NULL) {
585 		freeb(sup->su_tx_flow_mp);
586 		sup->su_tx_flow_mp = NULL;
587 	}
588 
589 	if (sup->su_active) {
590 		mutex_enter(&softmac->smac_active_mutex);
591 		softmac->smac_nactive--;
592 		mutex_exit(&softmac->smac_active_mutex);
593 		sup->su_active = B_FALSE;
594 	}
595 
596 	sup->su_bound = B_FALSE;
597 	sup->su_softmac = NULL;
598 	sup->su_closing = B_FALSE;
599 
600 	kmem_cache_free(softmac_upper_cachep, sup);
601 
602 	softmac_rele(softmac);
603 	return (dld_str_close(rq));
604 }
605 
606 static void
607 softmac_drv_wput(queue_t *wq, mblk_t *mp)
608 {
609 	softmac_upper_t	*sup = dld_str_private(wq);
610 	t_uscalar_t	prim;
611 
612 	ASSERT(wq->q_next == NULL);
613 
614 	switch (DB_TYPE(mp)) {
615 	case M_DATA:
616 	case M_MULTIDATA:
617 		softmac_wput_data(sup, mp);
618 		break;
619 	case M_PROTO:
620 	case M_PCPROTO:
621 
622 		if (MBLKL(mp) < sizeof (t_uscalar_t)) {
623 			freemsg(mp);
624 			return;
625 		}
626 
627 		prim = ((union DL_primitives *)mp->b_rptr)->dl_primitive;
628 		if (prim == DL_UNITDATA_REQ) {
629 			softmac_wput_data(sup, mp);
630 			return;
631 		}
632 
633 		softmac_wput_nondata(sup, mp);
634 		break;
635 	default:
636 		softmac_wput_nondata(sup, mp);
637 		break;
638 	}
639 }
640 
641 static void
642 softmac_drv_wsrv(queue_t *wq)
643 {
644 	softmac_upper_t	*sup = dld_str_private(wq);
645 
646 	ASSERT(wq->q_next == NULL);
647 
648 	mutex_enter(&sup->su_mutex);
649 	if (sup->su_mode != SOFTMAC_FASTPATH) {
650 		/*
651 		 * Bump su_tx_inprocess so that su_mode won't change.
652 		 */
653 		sup->su_tx_inprocess++;
654 		mutex_exit(&sup->su_mutex);
655 		dld_wsrv(wq);
656 		mutex_enter(&sup->su_mutex);
657 		if (--sup->su_tx_inprocess == 0)
658 			cv_signal(&sup->su_cv);
659 	} else if (sup->su_tx_busy && SOFTMAC_CANPUTNEXT(sup->su_slp->sl_wq)) {
660 		/*
661 		 * The flow-conctol of the dedicated-lower-stream is
662 		 * relieved. If DLD_CAPAB_DIRECT is enabled, call tx_notify
663 		 * callback to relieve the flow-control of the specific client,
664 		 * otherwise relieve the flow-control of all the upper-stream
665 		 * using the traditional STREAM mechanism.
666 		 */
667 		if (sup->su_tx_notify_func != NULL) {
668 			sup->su_tx_inprocess++;
669 			mutex_exit(&sup->su_mutex);
670 			sup->su_tx_notify_func(sup->su_tx_notify_arg,
671 			    (mac_tx_cookie_t)sup);
672 			mutex_enter(&sup->su_mutex);
673 			if (--sup->su_tx_inprocess == 0)
674 				cv_signal(&sup->su_cv);
675 		}
676 		ASSERT(sup->su_tx_flow_mp == NULL);
677 		VERIFY((sup->su_tx_flow_mp = getq(wq)) != NULL);
678 		sup->su_tx_busy = B_FALSE;
679 	}
680 	mutex_exit(&sup->su_mutex);
681 }
682