xref: /titanic_41/usr/src/uts/sun4v/io/drctl.c (revision 95c2d3023b88b9097d9822eb47ace5466e6d1cf4)
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 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * DR control module for LDoms
29  */
30 
31 #include <sys/sysmacros.h>
32 #include <sys/modctl.h>
33 #include <sys/conf.h>
34 #include <sys/ddi.h>
35 #include <sys/sunddi.h>
36 #include <sys/ddi_impldefs.h>
37 #include <sys/stat.h>
38 #include <sys/door.h>
39 #include <sys/open.h>
40 #include <sys/note.h>
41 #include <sys/ldoms.h>
42 #include <sys/dr_util.h>
43 #include <sys/drctl.h>
44 #include <sys/drctl_impl.h>
45 
46 
47 static int drctl_attach(dev_info_t *, ddi_attach_cmd_t);
48 static int drctl_detach(dev_info_t *, ddi_detach_cmd_t);
49 static int drctl_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
50 
51 static int drctl_open(dev_t *, int, int, cred_t *);
52 static int drctl_close(dev_t, int, int, cred_t *);
53 static int drctl_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
54 
55 static void *pack_message(int, int, int, void *, size_t *, size_t *);
56 static int send_message(void *, size_t, drctl_resp_t **, size_t *);
57 
58 
59 /*
60  * Configuration data structures
61  */
62 static struct cb_ops drctl_cb_ops = {
63 	drctl_open,		/* open */
64 	drctl_close,		/* close */
65 	nodev,			/* strategy */
66 	nodev,			/* print */
67 	nodev,			/* dump */
68 	nodev,			/* read */
69 	nodev,			/* write */
70 	drctl_ioctl,		/* ioctl */
71 	nodev,			/* devmap */
72 	nodev,			/* mmap */
73 	nodev,			/* segmap */
74 	nochpoll,		/* poll */
75 	ddi_prop_op,		/* prop_op */
76 	NULL,			/* streamtab */
77 	D_MP | D_NEW,		/* driver compatibility flag */
78 	CB_REV,			/* cb_ops revision */
79 	nodev,			/* async read */
80 	nodev			/* async write */
81 };
82 
83 
84 static struct dev_ops drctl_ops = {
85 	DEVO_REV,		/* devo_rev */
86 	0,			/* refcnt */
87 	drctl_getinfo,		/* info */
88 	nulldev,		/* identify */
89 	nulldev,		/* probe */
90 	drctl_attach,		/* attach */
91 	drctl_detach,		/* detach */
92 	nodev,			/* reset */
93 	&drctl_cb_ops,		/* driver operations */
94 	NULL,			/* bus operations */
95 	NULL,			/* power */
96 	ddi_quiesce_not_needed,		/* quiesce */
97 };
98 
99 static struct modldrv modldrv = {
100 	&mod_driverops,		/* type of module - driver */
101 	"DR Control pseudo driver",
102 	&drctl_ops
103 };
104 
105 static struct modlinkage modlinkage = {
106 	MODREV_1,
107 	&modldrv,
108 	NULL
109 };
110 
111 
112 /*
113  * Locking strategy
114  *
115  * One of the reasons for this module's existence is to serialize
116  * DR requests which might be coming from different sources.  Only
117  * one operation is allowed to be in progress at any given time.
118  *
119  * A single lock word (the 'drc_busy' element below) is NULL
120  * when there is no operation in progress.  When a client of this
121  * module initiates an operation it grabs the mutex 'drc_lock' in
122  * order to examine the lock word ('drc_busy').  If no other
123  * operation is in progress, the lock word will be NULL.  If so,
124  * a cookie which uniquely identifies the requestor is stored in
125  * the lock word, and the mutex is released.  Attempts by other
126  * clients to initiate an operation will fail.
127  *
128  * When the lock-holding client's operation is completed, the
129  * client will call a "finalize" function in this module, providing
130  * the cookie passed with the original request.  Since the cookie
131  * matches, the operation will succeed and the lock word will be
132  * cleared.  At this point, an new operation may be initiated.
133  */
134 
135 /*
136  * Driver private data
137  */
138 static struct drctl_unit {
139 	kmutex_t		drc_lock;	/* global driver lock */
140 	dev_info_t		*drc_dip;	/* dev_info pointer */
141 	kcondvar_t		drc_busy_cv;	/* block for !busy */
142 	drctl_cookie_t		drc_busy;	/* NULL if free else a unique */
143 						/* identifier for caller */
144 	int			drc_cmd;	/* the cmd underway (or -1) */
145 	int			drc_flags;	/* saved flag from above cmd */
146 	int			drc_inst;	/* our single instance */
147 	uint_t			drc_state;	/* driver state */
148 } drctl_state;
149 
150 static struct drctl_unit *drctlp = &drctl_state;
151 
152 int
153 _init(void)
154 {
155 	int rv;
156 
157 	drctlp->drc_inst = -1;
158 	mutex_init(&drctlp->drc_lock, NULL, MUTEX_DRIVER, NULL);
159 
160 	if ((rv = mod_install(&modlinkage)) != 0)
161 		mutex_destroy(&drctlp->drc_lock);
162 
163 	return (rv);
164 }
165 
166 
167 int
168 _fini(void)
169 {
170 	int rv;
171 
172 	if ((rv = mod_remove(&modlinkage)) != 0)
173 		return (rv);
174 
175 	mutex_destroy(&drctlp->drc_lock);
176 	return (0);
177 }
178 
179 
180 int
181 _info(struct modinfo *modinfop)
182 {
183 	return (mod_info(&modlinkage, modinfop));
184 }
185 
186 
187 /*
188  * Do the attach work
189  */
190 static int
191 drctl_do_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
192 {
193 	_NOTE(ARGUNUSED(cmd))
194 
195 	char *str = "drctl_do_attach";
196 	int retval = DDI_SUCCESS;
197 
198 	if (drctlp->drc_inst != -1) {
199 		cmn_err(CE_WARN, "%s: an instance is already attached!", str);
200 		return (DDI_FAILURE);
201 	}
202 	drctlp->drc_inst = ddi_get_instance(dip);
203 
204 	retval = ddi_create_minor_node(dip, "drctl", S_IFCHR,
205 	    drctlp->drc_inst, DDI_PSEUDO, 0);
206 	if (retval != DDI_SUCCESS) {
207 		cmn_err(CE_WARN, "%s: can't create minor node", str);
208 		drctlp->drc_inst = -1;
209 		return (retval);
210 	}
211 
212 	drctlp->drc_dip = dip;
213 	ddi_report_dev(dip);
214 
215 	return (retval);
216 }
217 
218 
219 static int
220 drctl_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
221 {
222 	switch (cmd) {
223 	case DDI_ATTACH:
224 		return (drctl_do_attach(dip, cmd));
225 
226 	default:
227 		return (DDI_FAILURE);
228 	}
229 }
230 
231 
232 /* ARGSUSED */
233 static int
234 drctl_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
235 {
236 	switch (cmd) {
237 	case DDI_DETACH:
238 		drctlp->drc_inst = -1;
239 		ddi_remove_minor_node(dip, "drctl");
240 		return (DDI_SUCCESS);
241 
242 	default:
243 		return (DDI_FAILURE);
244 	}
245 }
246 
247 static int
248 drctl_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
249 {
250 	_NOTE(ARGUNUSED(dip, cmd, arg, resultp))
251 
252 	return (0);
253 }
254 
255 static int
256 drctl_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
257 {
258 	_NOTE(ARGUNUSED(devp, flag, cred_p))
259 
260 	if (otyp != OTYP_CHR)
261 		return (EINVAL);
262 
263 	return (0);
264 }
265 
266 static int
267 drctl_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
268 {
269 	_NOTE(ARGUNUSED(dev, flag, otyp, cred_p))
270 
271 	return (0);
272 }
273 
274 /*
275  * Create a reponse structure which includes an array of drctl_rsrc_t
276  * structures in which each status element is set to the 'status'
277  * arg.  There is no error text, so set the 'offset' elements to 0.
278  */
279 static drctl_resp_t *
280 drctl_generate_resp(drctl_rsrc_t *res,
281     int count, size_t *rsize, drctl_status_t status)
282 {
283 	int		i;
284 	size_t		size;
285 	drctl_rsrc_t	*rsrc;
286 	drctl_resp_t	*resp;
287 
288 	size = offsetof(drctl_resp_t, resp_resources) + (count * sizeof (*res));
289 	resp  = kmem_alloc(size, KM_SLEEP);
290 	DR_DBG_KMEM("%s: alloc addr %p size %ld\n",
291 	    __func__, (void *)resp, size);
292 
293 	resp->resp_type = DRCTL_RESP_OK;
294 	rsrc = resp->resp_resources;
295 
296 	bcopy(res, rsrc, count * sizeof (*res));
297 
298 	for (i = 0; i < count; i++) {
299 		rsrc[i].status = status;
300 		rsrc[i].offset = 0;
301 	}
302 
303 	*rsize = size;
304 
305 	return (resp);
306 }
307 
308 /*
309  * Generate an error response message.
310  */
311 static drctl_resp_t *
312 drctl_generate_err_resp(char *msg, size_t *size)
313 {
314 	drctl_resp_t	*resp;
315 
316 	ASSERT(msg != NULL);
317 	ASSERT(size != NULL);
318 
319 	*size = offsetof(drctl_resp_t, resp_err_msg) + strlen(msg) + 1;
320 	resp = kmem_alloc(*size, KM_SLEEP);
321 	DR_DBG_KMEM("%s: alloc addr %p size %ld\n",
322 	    __func__, (void *)resp, *size);
323 
324 	resp->resp_type = DRCTL_RESP_ERR;
325 	(void) strcpy(resp->resp_err_msg, msg);
326 
327 	return (resp);
328 }
329 
330 /*
331  * Since response comes from userland, verify that it is at least the
332  * minimum size based on the size of the original request.  Verify
333  * that any offsets to error strings are within the string area of
334  * the response and, force the string area to be null-terminated.
335  */
336 static int
337 verify_response(int cmd,
338     int count, drctl_resp_t *resp, size_t sent_len, size_t resp_len)
339 {
340 	drctl_rsrc_t *rsrc = resp->resp_resources;
341 	size_t rcvd_len = resp_len - (offsetof(drctl_resp_t, resp_resources));
342 	int is_cpu = 0;
343 	int i;
344 
345 	switch (cmd) {
346 	case DRCTL_CPU_CONFIG_REQUEST:
347 	case DRCTL_CPU_UNCONFIG_REQUEST:
348 		if (rcvd_len < sent_len)
349 			return (EIO);
350 		is_cpu = 1;
351 		break;
352 	case DRCTL_IO_UNCONFIG_REQUEST:
353 	case DRCTL_IO_CONFIG_REQUEST:
354 		if (count != 1)
355 			return (EIO);
356 		break;
357 	default:
358 		return (EIO);
359 	}
360 
361 	for (i = 0; i < count; i++)
362 		if ((rsrc[i].offset > 0) &&
363 		    /* string can't be inside the bounds of original request */
364 		    (((rsrc[i].offset < sent_len) && is_cpu) ||
365 		    /* string must start inside the message */
366 		    (rsrc[i].offset >= rcvd_len)))
367 			return (EIO);
368 
369 	/* If there are any strings, terminate the string area. */
370 	if (rcvd_len > sent_len)
371 		*((char *)rsrc + rcvd_len - 1) = '\0';
372 
373 	return (0);
374 }
375 
376 
377 static int
378 drctl_config_common(int cmd, int flags, drctl_rsrc_t *res,
379     int count, drctl_resp_t **rbuf, size_t *rsize, size_t *rq_size)
380 {
381 	int	rv = 0;
382 	size_t	size;
383 	char	*bufp;
384 
385 	switch (cmd) {
386 	case DRCTL_CPU_CONFIG_REQUEST:
387 	case DRCTL_CPU_CONFIG_NOTIFY:
388 	case DRCTL_CPU_UNCONFIG_REQUEST:
389 	case DRCTL_CPU_UNCONFIG_NOTIFY:
390 	case DRCTL_IO_UNCONFIG_REQUEST:
391 	case DRCTL_IO_UNCONFIG_NOTIFY:
392 	case DRCTL_IO_CONFIG_REQUEST:
393 	case DRCTL_IO_CONFIG_NOTIFY:
394 		rv = 0;
395 		break;
396 	case DRCTL_MEM_CONFIG_REQUEST:
397 	case DRCTL_MEM_CONFIG_NOTIFY:
398 	case DRCTL_MEM_UNCONFIG_REQUEST:
399 	case DRCTL_MEM_UNCONFIG_NOTIFY:
400 		rv = ENOTSUP;
401 		break;
402 	}
403 
404 	if (rv != 0) {
405 		DR_DBG_CTL("%s: invalid cmd %d\n", __func__, cmd);
406 		return (rv);
407 	}
408 
409 	/*
410 	 * If the operation is a FORCE, we don't send a message to
411 	 * the daemon.  But, the upstream clients still expect a
412 	 * response, so generate a response with all ops 'allowed'.
413 	 */
414 	if (flags == DRCTL_FLAG_FORCE) {
415 		if (rbuf != NULL)
416 			*rbuf = drctl_generate_resp(res,
417 			    count, rsize, DRCTL_STATUS_ALLOW);
418 		return (0);
419 	}
420 
421 	bufp = pack_message(cmd, flags, count, (void *)res, &size, rq_size);
422 	DR_DBG_CTL("%s: from pack_message, bufp = %p size %ld\n",
423 	    __func__, (void *)bufp, size);
424 
425 	if (bufp == NULL || size == 0)
426 		return (EINVAL);
427 
428 	return (send_message(bufp, size, rbuf, rsize));
429 }
430 
431 /*
432  * Prepare for a reconfig operation.
433  */
434 int
435 drctl_config_init(int cmd, int flags, drctl_rsrc_t *res,
436     int count, drctl_resp_t **rbuf, size_t *rsize, drctl_cookie_t ck)
437 {
438 	static char inval_msg[] = "Invalid command format received.\n";
439 	static char unsup_msg[] = "Unsuppported command received.\n";
440 	static char unk_msg  [] = "Failure reason unknown.\n";
441 	static char rsp_msg  [] = "Invalid response from "
442 	    "reconfiguration daemon.\n";
443 	static char drd_msg  [] = "Cannot communicate with reconfiguration "
444 	    "daemon (drd) in target domain.\n"
445 	    "drd(1M) SMF service may not be enabled.\n";
446 	static char busy_msg [] = "Busy executing earlier command; "
447 	    "please try again later.\n";
448 	size_t rq_size;
449 	char *ermsg;
450 	int rv;
451 
452 	if (ck == 0) {
453 		*rbuf = drctl_generate_err_resp(inval_msg, rsize);
454 
455 		return (EINVAL);
456 	}
457 
458 	mutex_enter(&drctlp->drc_lock);
459 
460 	if (drctlp->drc_busy != NULL) {
461 		mutex_exit(&drctlp->drc_lock);
462 		*rbuf = drctl_generate_err_resp(busy_msg, rsize);
463 
464 		return (EBUSY);
465 	}
466 
467 	DR_DBG_CTL("%s: cmd %d flags %d res %p count %d\n",
468 	    __func__, cmd, flags, (void *)res, count);
469 
470 	/* Mark the link busy.  Below we will fill in the actual cookie. */
471 	drctlp->drc_busy = (drctl_cookie_t)-1;
472 	mutex_exit(&drctlp->drc_lock);
473 
474 	rv = drctl_config_common(cmd, flags, res, count, rbuf, rsize, &rq_size);
475 	if (rv == 0) {
476 		/*
477 		 * If the upcall to the daemon returned successfully, we
478 		 * still need to validate the format of the returned msg.
479 		 */
480 		if ((rv = verify_response(cmd,
481 		    count, *rbuf, rq_size, *rsize)) != 0) {
482 			DR_DBG_KMEM("%s: free addr %p size %ld\n",
483 			    __func__, (void *)*rbuf, *rsize);
484 			kmem_free(*rbuf, *rsize);
485 			*rbuf = drctl_generate_err_resp(rsp_msg, rsize);
486 			drctlp->drc_busy = NULL;
487 		} else { /* message format is valid */
488 			drctlp->drc_busy = ck;
489 			drctlp->drc_cmd = cmd;
490 			drctlp->drc_flags = flags;
491 		}
492 	} else {
493 		switch (rv) {
494 		case ENOTSUP:
495 			ermsg = unsup_msg;
496 			break;
497 		case EIO:
498 			ermsg = drd_msg;
499 			break;
500 		default:
501 			ermsg = unk_msg;
502 			break;
503 		}
504 
505 		*rbuf = drctl_generate_err_resp(ermsg, rsize);
506 
507 		drctlp->drc_cmd = -1;
508 		drctlp->drc_flags = 0;
509 		drctlp->drc_busy = NULL;
510 	}
511 
512 	return (rv);
513 }
514 
515 /*
516  * Complete a reconfig operation.
517  */
518 int
519 drctl_config_fini(drctl_cookie_t ck, drctl_rsrc_t *res, int count)
520 {
521 	int rv;
522 	int notify_cmd;
523 	int flags;
524 	size_t rq_size;
525 
526 	mutex_enter(&drctlp->drc_lock);
527 
528 	if (drctlp->drc_busy != ck) {
529 		mutex_exit(&drctlp->drc_lock);
530 		return (EBUSY);
531 	}
532 
533 	mutex_exit(&drctlp->drc_lock);
534 
535 	flags = drctlp->drc_flags;
536 	/*
537 	 * Flip the saved _REQUEST command to its corresponding
538 	 * _NOTIFY command.
539 	 */
540 	switch (drctlp->drc_cmd) {
541 	case DRCTL_CPU_CONFIG_REQUEST:
542 		notify_cmd = DRCTL_CPU_CONFIG_NOTIFY;
543 		break;
544 
545 	case DRCTL_CPU_UNCONFIG_REQUEST:
546 		notify_cmd = DRCTL_CPU_UNCONFIG_NOTIFY;
547 		break;
548 
549 	case DRCTL_IO_UNCONFIG_REQUEST:
550 		notify_cmd = DRCTL_IO_UNCONFIG_NOTIFY;
551 		break;
552 
553 	case DRCTL_IO_CONFIG_REQUEST:
554 		notify_cmd = DRCTL_IO_CONFIG_NOTIFY;
555 		break;
556 
557 	case DRCTL_MEM_CONFIG_REQUEST:
558 	case DRCTL_MEM_CONFIG_NOTIFY:
559 	case DRCTL_MEM_UNCONFIG_REQUEST:
560 	case DRCTL_MEM_UNCONFIG_NOTIFY:
561 	default:
562 		/* none of the above should have been accepted in _init */
563 		ASSERT(0);
564 		cmn_err(CE_CONT,
565 		    "drctl_config_fini: bad cmd %d\n", drctlp->drc_cmd);
566 		rv = EINVAL;
567 		goto done;
568 	}
569 
570 	rv = drctl_config_common(notify_cmd,
571 	    flags, res, count, NULL, 0, &rq_size);
572 
573 done:
574 	drctlp->drc_cmd = -1;
575 	drctlp->drc_flags = 0;
576 	drctlp->drc_busy = NULL;
577 
578 	return (rv);
579 }
580 
581 static int
582 drctl_ioctl(dev_t dev,
583     int cmd, intptr_t arg, int mode, cred_t *cred_p, int *rval_p)
584 {
585 	_NOTE(ARGUNUSED(dev, mode, cred_p, rval_p))
586 
587 	int rv;
588 
589 	switch (cmd) {
590 	case DRCTL_IOCTL_CONNECT_SERVER:
591 		rv = i_drctl_ioctl(cmd, arg);
592 		break;
593 	default:
594 		rv = ENOTSUP;
595 	}
596 
597 	*rval_p = (rv == 0) ? 0 : -1;
598 
599 	return (rv);
600 }
601 
602 /*
603  * Accept a preformatted request from caller and send a message to
604  * the daemon.  A pointer to the daemon's response buffer is passed
605  * back in obufp, its size in osize.
606  */
607 static int
608 send_message(void *msg, size_t size, drctl_resp_t **obufp, size_t *osize)
609 {
610 	drctl_resp_t *bufp;
611 	drctl_rsrc_t *rsrcs;
612 	size_t rsrcs_size;
613 	int rv;
614 
615 	rv = i_drctl_send(msg, size, (void **)&rsrcs, &rsrcs_size);
616 
617 	if ((rv == 0) && ((rsrcs == NULL) ||(rsrcs_size == 0)))
618 		rv = EINVAL;
619 
620 	if (rv == 0) {
621 		if (obufp != NULL) {
622 			ASSERT(osize != NULL);
623 
624 			*osize =
625 			    offsetof(drctl_resp_t, resp_resources) + rsrcs_size;
626 			bufp =
627 			    kmem_alloc(*osize, KM_SLEEP);
628 			DR_DBG_KMEM("%s: alloc addr %p size %ld\n",
629 			    __func__, (void *)bufp, *osize);
630 			bufp->resp_type = DRCTL_RESP_OK;
631 			bcopy(rsrcs, bufp->resp_resources, rsrcs_size);
632 			*obufp = bufp;
633 		}
634 
635 		DR_DBG_KMEM("%s: free addr %p size %ld\n",
636 		    __func__, (void *)rsrcs, rsrcs_size);
637 		kmem_free(rsrcs, rsrcs_size);
638 	}
639 
640 	DR_DBG_KMEM("%s:free addr %p size %ld\n", __func__, msg, size);
641 	kmem_free(msg, size);
642 
643 	return (rv);
644 }
645 
646 static void *
647 pack_message(int cmd,
648     int flags, int count, void *data, size_t *osize, size_t *data_size)
649 {
650 	drd_msg_t *msgp = NULL;
651 	size_t hdr_size = offsetof(drd_msg_t, data);
652 
653 	switch (cmd) {
654 	case DRCTL_CPU_CONFIG_REQUEST:
655 	case DRCTL_CPU_CONFIG_NOTIFY:
656 	case DRCTL_CPU_UNCONFIG_REQUEST:
657 	case DRCTL_CPU_UNCONFIG_NOTIFY:
658 		*data_size = count * sizeof (drctl_rsrc_t);
659 		break;
660 	case DRCTL_IO_CONFIG_REQUEST:
661 	case DRCTL_IO_CONFIG_NOTIFY:
662 	case DRCTL_IO_UNCONFIG_REQUEST:
663 	case DRCTL_IO_UNCONFIG_NOTIFY:
664 		*data_size = sizeof (drctl_rsrc_t) +
665 		    strlen(((drctl_rsrc_t *)data)->res_dev_path);
666 		break;
667 	default:
668 		cmn_err(CE_WARN,
669 		    "drctl: pack_message received invalid cmd %d", cmd);
670 		break;
671 	}
672 
673 	if (data_size) {
674 		*osize = hdr_size + *data_size;
675 		msgp = kmem_alloc(*osize, KM_SLEEP);
676 		DR_DBG_KMEM("%s: alloc addr %p size %ld\n",
677 		    __func__, (void *)msgp, *osize);
678 		msgp->cmd = cmd;
679 		msgp->count = count;
680 		msgp->flags = flags;
681 		bcopy(data, msgp->data, *data_size);
682 	}
683 
684 	return (msgp);
685 }
686