xref: /illumos-gate/usr/src/uts/common/io/softmac/softmac_dev.c (revision ef2504f26d1ea5859db9838255bb63f488f1b050)
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(list_is_empty(&sup->su_req_list));
184 
185 	list_destroy(&sup->su_req_list);
186 	mutex_destroy(&sup->su_mutex);
187 	cv_destroy(&sup->su_cv);
188 	mutex_destroy(&sup->su_disp_mutex);
189 	cv_destroy(&sup->su_disp_cv);
190 }
191 
192 int
193 _init(void)
194 {
195 	int	err;
196 
197 	softmac_init();
198 
199 	softmac_upper_cachep = kmem_cache_create("softmac_upper_cache",
200 	    sizeof (softmac_upper_t), 0, softmac_upper_constructor,
201 	    softmac_upper_destructor, NULL, NULL, NULL, 0);
202 	ASSERT(softmac_upper_cachep != NULL);
203 
204 	if ((err = mod_install(&softmac_modlinkage)) != 0) {
205 		softmac_fini();
206 		return (err);
207 	}
208 
209 	return (0);
210 }
211 
212 int
213 _fini(void)
214 {
215 	int err;
216 
217 	if (softmac_busy())
218 		return (EBUSY);
219 
220 	if ((err = mod_remove(&softmac_modlinkage)) != 0)
221 		return (err);
222 
223 	kmem_cache_destroy(softmac_upper_cachep);
224 	softmac_fini();
225 
226 	return (0);
227 }
228 
229 int
230 _info(struct modinfo *modinfop)
231 {
232 	return (mod_info(&softmac_modlinkage, modinfop));
233 }
234 
235 static int
236 softmac_cmn_open(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *credp)
237 {
238 	softmac_lower_t	*slp;
239 	/*
240 	 * This is a self-cloning driver so that each queue should only
241 	 * get opened once.
242 	 */
243 	if (rq->q_ptr != NULL)
244 		return (EBUSY);
245 
246 	if (sflag == MODOPEN) {
247 		/*
248 		 * This is the softmac module pushed over an underlying
249 		 * legacy device.  Initialize the lower structure.
250 		 */
251 		if ((slp = kmem_zalloc(sizeof (*slp), KM_NOSLEEP)) == NULL)
252 			return (ENOMEM);
253 
254 		slp->sl_wq = WR(rq);
255 		cv_init(&slp->sl_cv, NULL, CV_DRIVER, NULL);
256 		mutex_init(&slp->sl_mutex, NULL, MUTEX_DRIVER, NULL);
257 		slp->sl_pending_prim = DL_PRIM_INVAL;
258 		rq->q_ptr = WR(rq)->q_ptr = slp;
259 		qprocson(rq);
260 		return (0);
261 	}
262 
263 	/*
264 	 * Regular device open of a softmac DLPI node.  We modify
265 	 * the queues' q_qinfo pointer such that all future STREAMS
266 	 * operations will go through another set of entry points
267 	 */
268 	rq->q_qinfo = &softmac_dld_r_qinit;
269 	WR(rq)->q_qinfo = &softmac_dld_w_qinit;
270 	return (softmac_drv_open(rq, devp, flag, sflag, credp));
271 }
272 
273 static int
274 softmac_mod_close(queue_t *rq)
275 {
276 	softmac_lower_t	*slp = rq->q_ptr;
277 
278 	/*
279 	 * Call the appropriate delete routine depending on whether this is
280 	 * a module or device.
281 	 */
282 	ASSERT(WR(rq)->q_next != NULL);
283 
284 	qprocsoff(rq);
285 
286 	slp->sl_softmac = NULL;
287 	slp->sl_lh = NULL;
288 
289 	ASSERT(slp->sl_ack_mp == NULL);
290 	ASSERT(slp->sl_pending_prim == DL_PRIM_INVAL);
291 	ASSERT(slp->sl_pending_ioctl == B_FALSE);
292 
293 	cv_destroy(&slp->sl_cv);
294 	mutex_destroy(&slp->sl_mutex);
295 
296 	kmem_free(slp, sizeof (*slp));
297 	return (0);
298 }
299 
300 static void
301 softmac_mod_rput(queue_t *rq, mblk_t *mp)
302 {
303 	softmac_lower_t		*slp = rq->q_ptr;
304 	softmac_lower_rxinfo_t	*rxinfo;
305 	union DL_primitives	*dlp;
306 
307 	/*
308 	 * This is the softmac module.
309 	 */
310 	ASSERT(WR(rq)->q_next != NULL);
311 	ASSERT((mp->b_next == NULL) && (mp->b_prev == NULL));
312 
313 	switch (DB_TYPE(mp)) {
314 	case M_DATA: {
315 
316 		/*
317 		 * If sl_rxinfo is non-NULL. This is dedicated-lower-stream
318 		 * created for fastpath. Directly call the rx callback.
319 		 */
320 		if ((rxinfo = slp->sl_rxinfo) != NULL) {
321 			rxinfo->slr_rx(rxinfo->slr_arg, NULL, mp, NULL);
322 			break;
323 		}
324 
325 		/*
326 		 * A shared-lower-stream. Some driver starts to send up
327 		 * packets even it not in the DL_IDLE state, where
328 		 * sl_softmac is not set yet. Drop the packet in this case.
329 		 */
330 		if (slp->sl_softmac == NULL) {
331 			freemsg(mp);
332 			return;
333 		}
334 
335 		/*
336 		 * If this message is looped back from the legacy devices,
337 		 * drop it as the Nemo framework will be responsible for
338 		 * looping it back by the mac_txloop() function.
339 		 */
340 		if (mp->b_flag & MSGNOLOOP) {
341 			freemsg(mp);
342 			return;
343 		}
344 
345 		/*
346 		 * This is the most common case.
347 		 */
348 		if (DB_REF(mp) == 1) {
349 			ASSERT(slp->sl_softmac != NULL);
350 			mac_rx(slp->sl_softmac->smac_mh, NULL, mp);
351 			return;
352 		} else {
353 			softmac_rput_process_data(slp, mp);
354 		}
355 		break;
356 	}
357 	case M_PROTO:
358 	case M_PCPROTO:
359 		if (MBLKL(mp) < sizeof (dlp->dl_primitive)) {
360 			freemsg(mp);
361 			break;
362 		}
363 		dlp = (union DL_primitives *)mp->b_rptr;
364 		if (dlp->dl_primitive == DL_UNITDATA_IND) {
365 
366 			if ((rxinfo = slp->sl_rxinfo) != NULL) {
367 				rxinfo->slr_rx(rxinfo->slr_arg, NULL, mp, NULL);
368 				break;
369 			}
370 
371 			cmn_err(CE_WARN, "got unexpected %s message",
372 			    dl_primstr(DL_UNITDATA_IND));
373 			freemsg(mp);
374 			break;
375 		}
376 		/*FALLTHROUGH*/
377 	default:
378 		softmac_rput_process_notdata(rq, slp->sl_sup, mp);
379 		break;
380 	}
381 }
382 
383 static void
384 softmac_mod_wput(queue_t *wq, mblk_t *mp)
385 {
386 	/*
387 	 * This is the softmac module
388 	 */
389 	ASSERT(wq->q_next != NULL);
390 
391 	switch (DB_TYPE(mp)) {
392 	case M_IOCTL: {
393 		struct iocblk		*ioc = (struct iocblk *)mp->b_rptr;
394 
395 		switch (ioc->ioc_cmd) {
396 		case SMAC_IOC_START: {
397 			softmac_lower_t		*slp = wq->q_ptr;
398 			smac_ioc_start_t	*arg;
399 
400 			if (ioc->ioc_count != sizeof (*arg)) {
401 				miocnak(wq, mp, 0, EINVAL);
402 				break;
403 			}
404 
405 			/*
406 			 * Assign the devname and perstream handle of the
407 			 * specific lower stream and return it as a part
408 			 * of the ioctl.
409 			 */
410 			arg = (smac_ioc_start_t *)mp->b_cont->b_rptr;
411 			arg->si_slp = slp;
412 			miocack(wq, mp, sizeof (*arg), 0);
413 			break;
414 		}
415 		default:
416 			miocnak(wq, mp, 0, EINVAL);
417 			break;
418 		}
419 		break;
420 	}
421 	default:
422 		freemsg(mp);
423 		break;
424 	}
425 }
426 
427 static void
428 softmac_mod_wsrv(queue_t *wq)
429 {
430 	softmac_lower_t *slp = wq->q_ptr;
431 
432 	/*
433 	 * This is the softmac module
434 	 */
435 	ASSERT(wq->q_next != NULL);
436 
437 	/*
438 	 * Inform that the tx resource is available; mac_tx_update() will
439 	 * inform all the upper streams sharing this lower stream.
440 	 */
441 	if (slp->sl_sup != NULL)
442 		qenable(slp->sl_sup->su_wq);
443 	else if (slp->sl_softmac != NULL)
444 		mac_tx_update(slp->sl_softmac->smac_mh);
445 }
446 
447 static int
448 softmac_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
449 {
450 	ASSERT(ddi_get_instance(dip) == 0);
451 
452 	if (cmd != DDI_ATTACH)
453 		return (DDI_FAILURE);
454 
455 	softmac_dip = dip;
456 
457 	return (DDI_SUCCESS);
458 }
459 
460 /* ARGSUSED */
461 static int
462 softmac_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
463 {
464 	if (cmd != DDI_DETACH)
465 		return (DDI_FAILURE);
466 
467 	softmac_dip = NULL;
468 	return (DDI_SUCCESS);
469 }
470 
471 /* ARGSUSED */
472 static int
473 softmac_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
474 {
475 	switch (infocmd) {
476 	case DDI_INFO_DEVT2DEVINFO:
477 		if (softmac_dip != NULL) {
478 			*result = softmac_dip;
479 			return (DDI_SUCCESS);
480 		}
481 		break;
482 
483 	case DDI_INFO_DEVT2INSTANCE:
484 		*result = NULL;
485 		return (DDI_SUCCESS);
486 
487 	}
488 
489 	return (DDI_FAILURE);
490 }
491 
492 /*ARGSUSED*/
493 static void
494 softmac_dedicated_rx(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
495     mac_header_info_t *mhip)
496 {
497 	queue_t *rq = ((softmac_upper_t *)arg)->su_rq;
498 
499 	if (canputnext(rq))
500 		putnext(rq, mp);
501 	else
502 		freemsg(mp);
503 }
504 
505 /*ARGSUSED*/
506 static int
507 softmac_drv_open(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *credp)
508 {
509 	softmac_upper_t	*sup = NULL;
510 	softmac_t	*softmac;
511 	int		err = 0;
512 
513 	/*
514 	 * This is a softmac device created for a legacy device, find the
515 	 * associated softmac and initialize the softmac_upper_t structure.
516 	 */
517 	if ((err = softmac_hold(*devp, &softmac)) != 0)
518 		return (err);
519 
520 	sup = kmem_cache_alloc(softmac_upper_cachep, KM_NOSLEEP);
521 	if (sup == NULL) {
522 		err = ENOMEM;
523 		goto fail;
524 	}
525 
526 	ASSERT(list_is_empty(&sup->su_req_list));
527 
528 	if ((sup->su_tx_flow_mp = allocb(1, BPRI_HI)) == NULL) {
529 		err = ENOMEM;
530 		goto fail;
531 	}
532 
533 	sup->su_rq = rq;
534 	sup->su_wq = WR(rq);
535 	sup->su_softmac = softmac;
536 	sup->su_mode = SOFTMAC_UNKNOWN;
537 
538 	sup->su_rxinfo.slr_arg = sup;
539 	sup->su_rxinfo.slr_rx = softmac_dedicated_rx;
540 	sup->su_direct_rxinfo.slr_arg = sup;
541 	sup->su_direct_rxinfo.slr_rx = softmac_dedicated_rx;
542 
543 	if ((err = dld_str_open(rq, devp, sup)) != 0) {
544 		freeb(sup->su_tx_flow_mp);
545 		sup->su_tx_flow_mp = NULL;
546 		goto fail;
547 	}
548 
549 	return (0);
550 
551 fail:
552 	if (sup != NULL)
553 		kmem_cache_free(softmac_upper_cachep, sup);
554 	softmac_rele(softmac);
555 	return (err);
556 }
557 
558 static int
559 softmac_drv_close(queue_t *rq)
560 {
561 	softmac_upper_t	*sup = dld_str_private(rq);
562 	softmac_t	*softmac = sup->su_softmac;
563 
564 	ASSERT(WR(rq)->q_next == NULL);
565 
566 	qprocsoff(rq);
567 
568 	ASSERT(sup->su_tx_inprocess == 0);
569 
570 	/*
571 	 * Wait until the pending request are processed by the worker thread.
572 	 */
573 	mutex_enter(&sup->su_disp_mutex);
574 	sup->su_closing = B_TRUE;
575 	while (sup->su_dlpi_pending)
576 		cv_wait(&sup->su_disp_cv, &sup->su_disp_mutex);
577 	mutex_exit(&sup->su_disp_mutex);
578 
579 	softmac_upperstream_close(sup);
580 
581 	if (sup->su_tx_flow_mp != NULL) {
582 		freeb(sup->su_tx_flow_mp);
583 		sup->su_tx_flow_mp = NULL;
584 	}
585 
586 	if (sup->su_active) {
587 		mutex_enter(&softmac->smac_active_mutex);
588 		softmac->smac_nactive--;
589 		mutex_exit(&softmac->smac_active_mutex);
590 		sup->su_active = B_FALSE;
591 	}
592 
593 	sup->su_bound = B_FALSE;
594 	sup->su_softmac = NULL;
595 	sup->su_closing = B_FALSE;
596 
597 	kmem_cache_free(softmac_upper_cachep, sup);
598 
599 	softmac_rele(softmac);
600 	return (dld_str_close(rq));
601 }
602 
603 static void
604 softmac_drv_wput(queue_t *wq, mblk_t *mp)
605 {
606 	softmac_upper_t	*sup = dld_str_private(wq);
607 	t_uscalar_t	prim;
608 
609 	ASSERT(wq->q_next == NULL);
610 
611 	switch (DB_TYPE(mp)) {
612 	case M_DATA:
613 	case M_MULTIDATA:
614 		softmac_wput_data(sup, mp);
615 		break;
616 	case M_PROTO:
617 	case M_PCPROTO:
618 
619 		if (MBLKL(mp) < sizeof (t_uscalar_t)) {
620 			freemsg(mp);
621 			return;
622 		}
623 
624 		prim = ((union DL_primitives *)mp->b_rptr)->dl_primitive;
625 		if (prim == DL_UNITDATA_REQ) {
626 			softmac_wput_data(sup, mp);
627 			return;
628 		}
629 
630 		softmac_wput_nondata(sup, mp);
631 		break;
632 	default:
633 		softmac_wput_nondata(sup, mp);
634 		break;
635 	}
636 }
637 
638 static void
639 softmac_drv_wsrv(queue_t *wq)
640 {
641 	softmac_upper_t	*sup = dld_str_private(wq);
642 
643 	ASSERT(wq->q_next == NULL);
644 
645 	mutex_enter(&sup->su_mutex);
646 	if (sup->su_mode != SOFTMAC_FASTPATH) {
647 		/*
648 		 * Bump su_tx_inprocess so that su_mode won't change.
649 		 */
650 		sup->su_tx_inprocess++;
651 		mutex_exit(&sup->su_mutex);
652 		dld_wsrv(wq);
653 		mutex_enter(&sup->su_mutex);
654 		if (--sup->su_tx_inprocess == 0)
655 			cv_signal(&sup->su_cv);
656 	} else if (sup->su_tx_busy && SOFTMAC_CANPUTNEXT(sup->su_slp->sl_wq)) {
657 		/*
658 		 * The flow-conctol of the dedicated-lower-stream is
659 		 * relieved, relieve the flow-control of the
660 		 * upper-stream too.
661 		 */
662 		sup->su_tx_flow_mp = getq(wq);
663 		sup->su_tx_busy = B_FALSE;
664 	}
665 	mutex_exit(&sup->su_mutex);
666 }
667