xref: /illumos-gate/usr/src/uts/common/io/dld/dld_drv.c (revision 360e6f5e7a29d5950aa1985f56811731715da7e5)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Data-Link Driver
31  */
32 
33 #include	<sys/conf.h>
34 #include	<sys/mkdev.h>
35 #include	<sys/modctl.h>
36 #include	<sys/stat.h>
37 #include	<sys/strsun.h>
38 #include	<sys/dld.h>
39 #include	<sys/dld_impl.h>
40 #include	<sys/dls_impl.h>
41 #include	<inet/common.h>
42 
43 /*
44  * dld control node state, one per open control node session.
45  */
46 typedef struct dld_ctl_str_s {
47 	minor_t cs_minor;
48 	queue_t *cs_wq;
49 } dld_ctl_str_t;
50 
51 static void	drv_init(void);
52 static int	drv_fini(void);
53 
54 static int	drv_getinfo(dev_info_t	*, ddi_info_cmd_t, void *, void **);
55 static int	drv_attach(dev_info_t *, ddi_attach_cmd_t);
56 static int	drv_detach(dev_info_t *, ddi_detach_cmd_t);
57 
58 /*
59  * The following entry points are private to dld and are used for control
60  * operations only. The entry points exported to mac drivers are defined
61  * in dld_str.c. Refer to the comment on top of dld_str.c for details.
62  */
63 static int	drv_open(queue_t *, dev_t *, int, int, cred_t *);
64 static int	drv_close(queue_t *);
65 
66 static void	drv_uw_put(queue_t *, mblk_t *);
67 static void	drv_uw_srv(queue_t *);
68 
69 dev_info_t	*dld_dip;		/* dev_info_t for the driver */
70 uint32_t	dld_opt = 0;		/* Global options */
71 static vmem_t	*dld_ctl_vmem;		/* for control minor numbers */
72 
73 static	struct	module_info	drv_info = {
74 	0,			/* mi_idnum */
75 	DLD_DRIVER_NAME,	/* mi_idname */
76 	0,			/* mi_minpsz */
77 	(64 * 1024),		/* mi_maxpsz */
78 	1,			/* mi_hiwat */
79 	0			/* mi_lowat */
80 };
81 
82 static	struct qinit		drv_ur_init = {
83 	NULL,			/* qi_putp */
84 	NULL,			/* qi_srvp */
85 	drv_open,		/* qi_qopen */
86 	drv_close,		/* qi_qclose */
87 	NULL,			/* qi_qadmin */
88 	&drv_info,		/* qi_minfo */
89 	NULL			/* qi_mstat */
90 };
91 
92 static	struct qinit		drv_uw_init = {
93 	(pfi_t)drv_uw_put,	/* qi_putp */
94 	(pfi_t)drv_uw_srv,	/* qi_srvp */
95 	NULL,			/* qi_qopen */
96 	NULL,			/* qi_qclose */
97 	NULL,			/* qi_qadmin */
98 	&drv_info,		/* qi_minfo */
99 	NULL			/* qi_mstat */
100 };
101 
102 static	struct streamtab	drv_stream = {
103 	&drv_ur_init,		/* st_rdinit */
104 	&drv_uw_init,		/* st_wrinit */
105 	NULL,			/* st_muxrinit */
106 	NULL			/* st_muxwinit */
107 };
108 
109 DDI_DEFINE_STREAM_OPS(drv_ops, nulldev, nulldev, drv_attach, drv_detach,
110     nodev, drv_getinfo, D_MP, &drv_stream);
111 
112 /*
113  * Module linkage information for the kernel.
114  */
115 
116 extern	struct mod_ops		mod_driverops;
117 
118 static	struct modldrv		drv_modldrv = {
119 	&mod_driverops,
120 	DLD_INFO,
121 	&drv_ops
122 };
123 
124 static	struct modlinkage	drv_modlinkage = {
125 	MODREV_1,
126 	&drv_modldrv,
127 	NULL
128 };
129 
130 int
131 _init(void)
132 {
133 	int	err;
134 
135 	drv_init();
136 
137 	if ((err = mod_install(&drv_modlinkage)) != 0)
138 		return (err);
139 
140 	return (0);
141 }
142 
143 int
144 _fini(void)
145 {
146 	int	err;
147 
148 	if ((err = mod_remove(&drv_modlinkage)) != 0)
149 		return (err);
150 
151 	if (drv_fini() != 0) {
152 		(void) mod_install(&drv_modlinkage);
153 		return (DDI_FAILURE);
154 	}
155 
156 	return (err);
157 }
158 
159 int
160 _info(struct modinfo *modinfop)
161 {
162 	return (mod_info(&drv_modlinkage, modinfop));
163 }
164 
165 
166 /*
167  * Initialize component modules.
168  */
169 static void
170 drv_init(void)
171 {
172 	dld_ctl_vmem = vmem_create("dld_ctl", (void *)1, MAXMIN, 1,
173 	    NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER);
174 	dld_str_init();
175 }
176 
177 static int
178 drv_fini(void)
179 {
180 	int	err;
181 
182 	if ((err = dld_str_fini()) != 0)
183 		return (err);
184 
185 	vmem_destroy(dld_ctl_vmem);
186 	return (0);
187 }
188 
189 /*
190  * devo_getinfo: getinfo(9e)
191  */
192 /*ARGSUSED*/
193 static int
194 drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resp)
195 {
196 	if (dld_dip == NULL)
197 		return (DDI_FAILURE);
198 
199 	switch (cmd) {
200 	case DDI_INFO_DEVT2INSTANCE:
201 		*resp = (void *)0;
202 		break;
203 	case DDI_INFO_DEVT2DEVINFO:
204 		*resp = (void *)dld_dip;
205 		break;
206 	default:
207 		return (DDI_FAILURE);
208 	}
209 
210 	return (DDI_SUCCESS);
211 }
212 
213 /*
214  * Check properties to set options. (See dld.h for property definitions).
215  */
216 static void
217 drv_set_opt(dev_info_t *dip)
218 {
219 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
220 	    DLD_PROP_NO_FASTPATH, 0) != 0) {
221 		dld_opt |= DLD_OPT_NO_FASTPATH;
222 	}
223 
224 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
225 	    DLD_PROP_NO_POLL, 0) != 0) {
226 		dld_opt |= DLD_OPT_NO_POLL;
227 	}
228 
229 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
230 	    DLD_PROP_NO_ZEROCOPY, 0) != 0) {
231 		dld_opt |= DLD_OPT_NO_ZEROCOPY;
232 	}
233 }
234 
235 /*
236  * devo_attach: attach(9e)
237  */
238 static int
239 drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
240 {
241 	if (cmd != DDI_ATTACH)
242 		return (DDI_FAILURE);
243 
244 	ASSERT(ddi_get_instance(dip) == 0);
245 
246 	drv_set_opt(dip);
247 
248 	/*
249 	 * Create control node. DLPI provider nodes will be created on demand.
250 	 */
251 	if (ddi_create_minor_node(dip, DLD_CONTROL_MINOR_NAME, S_IFCHR,
252 	    DLD_CONTROL_MINOR, DDI_PSEUDO, 0) != DDI_SUCCESS)
253 		return (DDI_FAILURE);
254 
255 	dld_dip = dip;
256 
257 	/*
258 	 * Log the fact that the driver is now attached.
259 	 */
260 	ddi_report_dev(dip);
261 	return (DDI_SUCCESS);
262 }
263 
264 /*
265  * devo_detach: detach(9e)
266  */
267 static int
268 drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
269 {
270 	if (cmd != DDI_DETACH)
271 		return (DDI_FAILURE);
272 
273 	ASSERT(dld_dip == dip);
274 
275 	/*
276 	 * Remove the control node.
277 	 */
278 	ddi_remove_minor_node(dip, DLD_CONTROL_MINOR_NAME);
279 	dld_dip = NULL;
280 
281 	return (DDI_SUCCESS);
282 }
283 
284 /*
285  * dld control node open procedure.
286  */
287 /*ARGSUSED*/
288 static int
289 drv_open(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *credp)
290 {
291 	dld_ctl_str_t	*ctls;
292 	minor_t		minor;
293 	queue_t *oq =	OTHERQ(rq);
294 
295 	if (sflag == MODOPEN)
296 		return (ENOTSUP);
297 
298 	/*
299 	 * This is a cloning driver and therefore each queue should only
300 	 * ever get opened once.
301 	 */
302 	if (rq->q_ptr != NULL)
303 		return (EBUSY);
304 
305 	minor = (minor_t)(uintptr_t)vmem_alloc(dld_ctl_vmem, 1, VM_NOSLEEP);
306 	if (minor == 0)
307 		return (ENOMEM);
308 
309 	ctls = kmem_zalloc(sizeof (dld_ctl_str_t), KM_NOSLEEP);
310 	if (ctls == NULL) {
311 		vmem_free(dld_ctl_vmem, (void *)(uintptr_t)minor, 1);
312 		return (ENOMEM);
313 	}
314 
315 	ctls->cs_minor = minor;
316 	ctls->cs_wq = WR(rq);
317 
318 	rq->q_ptr = ctls;
319 	oq->q_ptr = ctls;
320 
321 	/*
322 	 * Enable the queue srv(9e) routine.
323 	 */
324 	qprocson(rq);
325 
326 	/*
327 	 * Construct a cloned dev_t to hand back.
328 	 */
329 	*devp = makedevice(getmajor(*devp), ctls->cs_minor);
330 	return (0);
331 }
332 
333 /*
334  * dld control node close procedure.
335  */
336 static int
337 drv_close(queue_t *rq)
338 {
339 	dld_ctl_str_t	*ctls;
340 
341 	ctls = rq->q_ptr;
342 	ASSERT(ctls != NULL);
343 
344 	/*
345 	 * Disable the queue srv(9e) routine.
346 	 */
347 	qprocsoff(rq);
348 
349 	vmem_free(dld_ctl_vmem, (void *)(uintptr_t)ctls->cs_minor, 1);
350 
351 	kmem_free(ctls, sizeof (dld_ctl_str_t));
352 
353 	return (0);
354 }
355 
356 /*
357  * DLDIOCATTR
358  */
359 static void
360 drv_ioc_attr(dld_ctl_str_t *ctls, mblk_t *mp)
361 {
362 	dld_ioc_attr_t *diap;
363 	dls_vlan_t	*dvp = NULL;
364 	dls_link_t	*dlp = NULL;
365 	int		err;
366 	queue_t		*q = ctls->cs_wq;
367 
368 	if ((err = miocpullup(mp, sizeof (dld_ioc_attr_t))) != 0)
369 		goto failed;
370 
371 	diap = (dld_ioc_attr_t *)mp->b_cont->b_rptr;
372 	diap->dia_name[IFNAMSIZ - 1] = '\0';
373 
374 	if (dls_vlan_hold(diap->dia_name, &dvp, B_FALSE) != 0) {
375 		err = ENOENT;
376 		goto failed;
377 	}
378 
379 	dlp = dvp->dv_dlp;
380 	(void) strlcpy(diap->dia_dev, dlp->dl_dev, MAXNAMELEN);
381 	diap->dia_port = dlp->dl_port;
382 	diap->dia_vid = dvp->dv_id;
383 	diap->dia_max_sdu = dlp->dl_mip->mi_sdu_max;
384 
385 	dls_vlan_rele(dvp);
386 	miocack(q, mp, sizeof (dld_ioc_attr_t), 0);
387 	return;
388 
389 failed:
390 	ASSERT(err != 0);
391 	if (err == ENOENT) {
392 		char	devname[MAXNAMELEN];
393 		uint_t	instance;
394 		major_t	major;
395 
396 		/*
397 		 * Try to detect if the specified device is gldv3
398 		 * and return ENODEV if it is not.
399 		 */
400 		if (ddi_parse(diap->dia_name, devname, &instance) == 0 &&
401 		    (major = ddi_name_to_major(devname)) != (major_t)-1 &&
402 		    !GLDV3_DRV(major))
403 			err = ENODEV;
404 	}
405 	miocnak(q, mp, 0, err);
406 }
407 
408 
409 /*
410  * DLDIOCVLAN
411  */
412 typedef struct dld_ioc_vlan_state {
413 	uint_t		bytes_left;
414 	dld_ioc_vlan_t	*divp;
415 	dld_vlan_info_t	*vlanp;
416 } dld_ioc_vlan_state_t;
417 
418 static int
419 drv_ioc_vlan_info(dls_vlan_t *dvp, void *arg)
420 {
421 	dld_ioc_vlan_state_t	*statep = arg;
422 
423 	/*
424 	 * passed buffer space is limited to 65536 bytes. So
425 	 * copy only the vlans associated with the passed link.
426 	 */
427 	if (strcmp(dvp->dv_dlp->dl_dev, statep->divp->div_name) == 0 &&
428 	    dvp->dv_dlp->dl_port == statep->divp->div_port &&
429 	    dvp->dv_id != 0) {
430 		if (statep->bytes_left < sizeof (dld_vlan_info_t))
431 			return (ENOSPC);
432 
433 		(void) strlcpy(statep->vlanp->dvi_name,
434 		    dvp->dv_name, IFNAMSIZ);
435 		statep->divp->div_count++;
436 		statep->bytes_left -= sizeof (dld_vlan_info_t);
437 		statep->vlanp += 1;
438 	}
439 	return (0);
440 }
441 
442 static void
443 drv_ioc_vlan(dld_ctl_str_t *ctls, mblk_t *mp)
444 {
445 	dld_ioc_vlan_t		*divp;
446 	dld_ioc_vlan_state_t	state;
447 	int			err = EINVAL;
448 	queue_t			*q = ctls->cs_wq;
449 	mblk_t			*bp;
450 
451 	if ((err = miocpullup(mp, sizeof (dld_ioc_vlan_t))) != 0)
452 		goto failed;
453 
454 	if ((bp = msgpullup(mp->b_cont, -1)) == NULL)
455 		goto failed;
456 
457 	freemsg(mp->b_cont);
458 	mp->b_cont = bp;
459 	divp = (dld_ioc_vlan_t *)bp->b_rptr;
460 	divp->div_count = 0;
461 	state.bytes_left = MBLKL(bp) - sizeof (dld_ioc_vlan_t);
462 	state.divp = divp;
463 	state.vlanp = (dld_vlan_info_t *)(divp + 1);
464 
465 	err = dls_vlan_walk(drv_ioc_vlan_info, &state);
466 	if (err != 0)
467 		goto failed;
468 
469 	miocack(q, mp, sizeof (dld_ioc_vlan_t) +
470 	    state.divp->div_count * sizeof (dld_vlan_info_t), 0);
471 	return;
472 
473 failed:
474 	ASSERT(err != 0);
475 	miocnak(q, mp, 0, err);
476 }
477 
478 
479 /*
480  * Process an IOCTL message received by the control node.
481  */
482 static void
483 drv_ioc(dld_ctl_str_t *ctls, mblk_t *mp)
484 {
485 	uint_t	cmd;
486 
487 	cmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
488 	switch (cmd) {
489 	case DLDIOCATTR:
490 		drv_ioc_attr(ctls, mp);
491 		return;
492 	case DLDIOCVLAN:
493 		drv_ioc_vlan(ctls, mp);
494 		return;
495 	default:
496 		miocnak(ctls->cs_wq, mp, 0, ENOTSUP);
497 		return;
498 	}
499 }
500 
501 /*
502  * Write side put routine of the dld control node.
503  */
504 static void
505 drv_uw_put(queue_t *q, mblk_t *mp)
506 {
507 	dld_ctl_str_t *ctls = q->q_ptr;
508 
509 	switch (mp->b_datap->db_type) {
510 	case M_IOCTL:
511 		drv_ioc(ctls, mp);
512 		break;
513 	default:
514 		freemsg(mp);
515 		break;
516 	}
517 }
518 
519 /*
520  * Write-side service procedure.
521  */
522 void
523 drv_uw_srv(queue_t *q)
524 {
525 	mblk_t *mp;
526 
527 	while (mp = getq(q))
528 		drv_uw_put(q, mp);
529 }
530