xref: /illumos-gate/usr/src/uts/sun4u/opl/io/oplkmdrv.c (revision 6074f19f)
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 /*
28  * OPL IPSec Key Management Driver.
29  *
30  * This driver runs on a OPL Domain. It processes requests received
31  * from the OPL Service Processor (SP) via mailbox message. It passes
32  * these requests to the sckmd daemon by means of an /ioctl interface.
33  *
34  * Requests received from the SP consist of IPsec security associations
35  * (SAs) needed to secure the communication between SC and Domain daemons
36  * communicating using DSCP.
37  */
38 
39 #include <sys/types.h>
40 #include <sys/cmn_err.h>
41 #include <sys/kmem.h>
42 #include <sys/errno.h>
43 #include <sys/file.h>
44 #include <sys/open.h>
45 #include <sys/stat.h>
46 #include <sys/conf.h>
47 #include <sys/ddi.h>
48 #include <sys/cmn_err.h>
49 #include <sys/sunddi.h>
50 #include <sys/sunndi.h>
51 #include <sys/ddi_impldefs.h>
52 #include <sys/ndi_impldefs.h>
53 #include <sys/modctl.h>
54 #include <sys/disp.h>
55 #include <sys/note.h>
56 #include <sys/byteorder.h>
57 #include <sys/sdt.h>
58 
59 #include <sys/scfd/scfdscpif.h>
60 #include <sys/oplkm_msg.h>
61 #include <sys/sckm_io.h>
62 #include <sys/oplkm.h>
63 
64 #define	OKM_NODENAME	"oplkmdrv"		/* Node name */
65 #define	OKM_TARGET_ID	0			/* Target ID */
66 #define	OKM_SM_TOUT	5000			/* small timeout (5msec) */
67 #define	OKM_LG_TOUT	50000			/* large timeout (50msec) */
68 #define	OKM_MB_TOUT	10000000		/* Mailbox timeout (10sec) */
69 
70 okms_t okms_global;				/* Global instance structure */
71 
72 #ifdef DEBUG
73 uint32_t okm_debug = DBG_WARN;
74 #endif
75 
76 /*
77  * Prototypes for the module related functions.
78  */
79 int okm_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
80 int okm_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
81 int okm_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result);
82 int okm_open(dev_t *devp, int flag, int otyp, struct cred *cred);
83 int okm_close(dev_t dev, int flag, int otyp, struct cred *cred);
84 int okm_ioctl(dev_t dev, int cmd, intptr_t data, int flag,
85 		cred_t *cred, int *rvalp);
86 
87 /*
88  * Prototypes for the internal functions.
89  */
90 int okm_get_req(okms_t *okmsp, sckm_ioctl_getreq_t *ireqp,
91     intptr_t data, int flag);
92 int okm_process_req(okms_t *okmsp, okm_req_hdr_t *reqp, uint32_t len,
93     sckm_ioctl_getreq_t *ireqp, intptr_t data, int flag);
94 int okm_process_status(okms_t *okmsp, sckm_ioctl_status_t *ireply);
95 void okm_event_handler(scf_event_t event, void *arg);
96 int okm_send_reply(okms_t *okmsp, uint32_t transid, uint32_t status,
97     uint32_t sadb_err, uint32_t sadb_ver);
98 int block_until_ready(okms_t *okmsp);
99 static int okm_copyin_ioctl_getreq(intptr_t userarg,
100     sckm_ioctl_getreq_t *driverarg, int flag);
101 static int okm_copyout_ioctl_getreq(sckm_ioctl_getreq_t *driverarg,
102     intptr_t userarg, int flag);
103 static void okm_cleanup(okms_t *okmsp);
104 static int okm_mbox_init(okms_t *okmsp);
105 static void okm_mbox_fini(okms_t *okmsp);
106 static clock_t okm_timeout_val(int error);
107 
108 
109 struct cb_ops okm_cb_ops = {
110 	okm_open,		/* open */
111 	okm_close,		/* close */
112 	nodev,			/* strategy */
113 	nodev,			/* print */
114 	nodev,			/* dump */
115 	nodev,			/* read */
116 	nodev,			/* write */
117 	okm_ioctl,		/* ioctl */
118 	nodev,			/* devmap */
119 	nodev,			/* mmap */
120 	nodev,			/* segmap */
121 	nochpoll,		/* poll */
122 	ddi_prop_op,		/* prop_op */
123 	0,			/* streamtab  */
124 	D_NEW | D_MP		/* Driver compatibility flag */
125 };
126 
127 struct dev_ops okm_ops = {
128 	DEVO_REV,		/* devo_rev, */
129 	0,			/* refcnt  */
130 	okm_info,		/* get_dev_info */
131 	nulldev,		/* identify */
132 	nulldev,		/* probe */
133 	okm_attach,		/* attach */
134 	okm_detach,		/* detach */
135 	nodev,			/* reset */
136 	&okm_cb_ops,		/* driver operations */
137 	(struct bus_ops *)0,	/* no bus operations */
138 	NULL,			/* power */
139 	ddi_quiesce_not_needed,		/* quiesce */
140 };
141 
142 struct modldrv modldrv = {
143 	&mod_driverops,
144 	"OPL Key Management Driver",
145 	&okm_ops,
146 };
147 
148 struct modlinkage modlinkage = {
149 	MODREV_1,
150 	&modldrv,
151 	NULL
152 };
153 
154 
155 /*
156  * _init - Module's init routine.
157  */
158 int
159 _init(void)
160 {
161 	int ret;
162 
163 	if ((ret = mod_install(&modlinkage)) != 0) {
164 		cmn_err(CE_WARN, "mod_install failed, error = %d", ret);
165 	}
166 	return (ret);
167 }
168 
169 /*
170  * _fini - Module's fini routine.
171  */
172 int
173 _fini(void)
174 {
175 	int ret;
176 
177 	if ((ret = mod_remove(&modlinkage)) != 0) {
178 		return (ret);
179 	}
180 	return (ret);
181 }
182 
183 /*
184  * _info - Module's info routine.
185  */
186 int
187 _info(struct modinfo *modinfop)
188 {
189 	return (mod_info(&modlinkage, modinfop));
190 }
191 
192 /*
193  * okm_attach - Module's attach routine.
194  *
195  * Description:	Initializes the modules state structure and create
196  *		the minor device node.
197  */
198 int
199 okm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
200 {
201 	int instance;
202 	okms_t *okmsp = &okms_global;
203 
204 	instance = ddi_get_instance(dip);
205 
206 	/* Only one instance is supported.  */
207 	if (instance != 0) {
208 		return (DDI_FAILURE);
209 	}
210 
211 	if (cmd != DDI_ATTACH) {
212 		return (DDI_FAILURE);
213 	}
214 
215 	okmsp->km_dip = dip;
216 	okmsp->km_major = ddi_driver_major(dip);
217 	okmsp->km_inst = instance;
218 
219 	/*
220 	 * Get an interrupt block cookie corresponding to the
221 	 * interrupt priority of the event handler.
222 	 * Assert that the event priority is not redefined to
223 	 * some other priority.
224 	 */
225 	/* LINTED */
226 	ASSERT(SCF_EVENT_PRI == DDI_SOFTINT_LOW);
227 	if (ddi_get_soft_iblock_cookie(dip, SCF_EVENT_PRI,
228 	    &okmsp->km_ibcookie) != DDI_SUCCESS) {
229 		cmn_err(CE_WARN, "ddi_get_soft_iblock_cookie failed.");
230 		return (DDI_FAILURE);
231 	}
232 	mutex_init(&okmsp->km_lock, NULL, MUTEX_DRIVER,
233 	    (void *)okmsp->km_ibcookie);
234 	okmsp->km_clean |= OKM_CLEAN_LOCK;
235 	cv_init(&okmsp->km_wait, NULL, CV_DRIVER, NULL);
236 	okmsp->km_clean |= OKM_CLEAN_CV;
237 
238 	/*
239 	 * set clean_node ahead as remove_node has to be called even
240 	 * if create node fails.
241 	 */
242 	okmsp->km_clean |= OKM_CLEAN_NODE;
243 	if (ddi_create_minor_node(dip, OKM_NODENAME, S_IFCHR,
244 	    instance, NULL, NULL) == DDI_FAILURE) {
245 		cmn_err(CE_WARN, "Device node creation failed");
246 		okm_cleanup(okmsp);
247 		return (DDI_FAILURE);
248 	}
249 
250 	ddi_set_driver_private(dip, (caddr_t)okmsp);
251 	ddi_report_dev(dip);
252 	return (DDI_SUCCESS);
253 }
254 
255 /*
256  * okm_detach - Module's detach routine.
257  *
258  * Description:	Cleans up the module's state structures and any other
259  *		relevant data.
260  */
261 int
262 okm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
263 {
264 	okms_t *okmsp;
265 
266 	if (cmd != DDI_DETACH) {
267 		return (DDI_FAILURE);
268 	}
269 
270 	if ((okmsp = ddi_get_driver_private(dip)) == NULL) {
271 		return (DDI_FAILURE);
272 	}
273 
274 	mutex_enter(&okmsp->km_lock);
275 	/*
276 	 * Check if the mailbox is still in use.
277 	 */
278 	if (okmsp->km_state & OKM_MB_INITED) {
279 		mutex_exit(&okmsp->km_lock);
280 		cmn_err(CE_WARN, "Detach failure: Mailbox in use");
281 		return (DDI_FAILURE);
282 	}
283 	mutex_exit(&okmsp->km_lock);
284 	okm_cleanup(okmsp);
285 	ddi_set_driver_private(dip, NULL);
286 	return (DDI_SUCCESS);
287 }
288 
289 /*
290  * okm_info - Module's info routine.
291  */
292 /* ARGSUSED */
293 int
294 okm_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
295 {
296 	okms_t	*okmsp = &okms_global;
297 	minor_t	minor;
298 	int	ret = DDI_FAILURE;
299 
300 	switch (infocmd) {
301 	case DDI_INFO_DEVT2DEVINFO:
302 		/*
303 		 * We have the case here where the minor number
304 		 * is the same as the instance number. So, just
305 		 * make sure we have the right minor node in our
306 		 * global state. If we don't, set the result to NULL.
307 		 */
308 		minor = getminor((dev_t)arg);
309 		if (okmsp->km_inst != minor) {
310 			*result = NULL;
311 		} else {
312 			*result = okmsp->km_dip;
313 			ret = DDI_SUCCESS;
314 		}
315 		break;
316 
317 	case DDI_INFO_DEVT2INSTANCE:
318 		minor = getminor((dev_t)arg);
319 		*result = (void *)(uintptr_t)minor;
320 		ret = DDI_SUCCESS;
321 
322 	default:
323 		break;
324 	}
325 	return (ret);
326 }
327 
328 /*
329  * okm_open - Device open routine.
330  *
331  * Description:	Initializes the mailbox and waits until the mailbox
332  *		gets connected. Only one open at a time is supported.
333  */
334 /*ARGSUSED*/
335 int
336 okm_open(dev_t *devp, int flag, int otyp, struct cred *cred)
337 {
338 	okms_t *okmsp = &okms_global;
339 	int ret = 0;
340 
341 	DPRINTF(DBG_DRV, ("okm_open: called\n"));
342 	mutex_enter(&okmsp->km_lock);
343 	if (okmsp->km_state & OKM_OPENED) {
344 		/* Only one open supported */
345 		mutex_exit(&okmsp->km_lock);
346 		DPRINTF(DBG_WARN, ("okm_open: already opened\n"));
347 		return (EBUSY);
348 	}
349 	okmsp->km_state |= OKM_OPENED;
350 	ret = block_until_ready(okmsp);
351 	if (ret != 0) {
352 		okmsp->km_state &= ~OKM_OPENED;
353 	}
354 	mutex_exit(&okmsp->km_lock);
355 	DPRINTF(DBG_DRV, ("okm_open: ret=%d\n", ret));
356 	return (ret);
357 }
358 
359 /*
360  * block_until_ready - Function to wait until the mailbox is ready to use.
361  *
362  * Description:	It initializes the mailbox and waits for the mailbox
363  *		state to transition to connected.
364  */
365 int
366 block_until_ready(okms_t *okmsp)
367 {
368 	int ret = 0;
369 
370 	DPRINTF(DBG_DRV, ("block_until_ready: called\n"));
371 	ASSERT(MUTEX_HELD(&okmsp->km_lock));
372 
373 	if (okmsp->km_state & OKM_MB_DISC) {
374 		DPRINTF(DBG_DRV, ("block_until_ready: closing the mailbox\n"));
375 		okm_mbox_fini(okmsp);
376 	}
377 	if (okmsp->km_state & OKM_MB_CONN) {
378 		DPRINTF(DBG_DRV, ("block_until_ready: mailbox connected\n"));
379 		return (0);
380 	}
381 	/*
382 	 * Initialize mailbox.
383 	 */
384 	if ((ret = okm_mbox_init(okmsp)) != 0) {
385 		DPRINTF(DBG_MBOX,
386 		    ("block_until_ready: mailbox init failed ret=%d\n", ret));
387 		return (ret);
388 	}
389 	DPRINTF(DBG_DRV, ("block_until_ready: ret=%d", ret));
390 	return (ret);
391 }
392 
393 /*
394  * okm_close - Device close routine.
395  *
396  * Description: Closes the mailbox.
397  */
398 /*ARGSUSED*/
399 int
400 okm_close(dev_t dev, int flag, int otyp, struct cred *cred)
401 {
402 	okms_t *okmsp = &okms_global;
403 
404 	DPRINTF(DBG_DRV, ("okm_close: called\n"));
405 	/* Close the lower layer first */
406 	mutex_enter(&okmsp->km_lock);
407 	okm_mbox_fini(okmsp);
408 	okmsp->km_state = 0;
409 	mutex_exit(&okmsp->km_lock);
410 	return (0);
411 }
412 
413 
414 /*
415  * okm_ioctl - Device ioctl routine.
416  *
417  * Description:	Processes ioctls from the daemon.
418  */
419 /*ARGSUSED*/
420 int
421 okm_ioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cred, int *rvalp)
422 {
423 	okms_t *okmsp = &okms_global;
424 	sckm_ioctl_getreq_t ireq;
425 	sckm_ioctl_status_t istatus;
426 	int ret = 0;
427 
428 	switch (cmd) {
429 	case SCKM_IOCTL_GETREQ:
430 
431 		DPRINTF(DBG_DRV, ("okm_ioctl: GETREQ\n"));
432 		if (okm_copyin_ioctl_getreq(data, &ireq, flag)) {
433 			return (EFAULT);
434 		}
435 
436 		ret = okm_get_req(okmsp, &ireq, data, flag);
437 		DPRINTF(DBG_DRV, ("okm_ioctl: GETREQ ret=%d\n", ret));
438 		break;
439 
440 	case SCKM_IOCTL_STATUS:
441 
442 		DPRINTF(DBG_DRV, ("okm_ioctl: STATUS\n"));
443 		if (ddi_copyin((caddr_t)data, &istatus,
444 		    sizeof (sckm_ioctl_status_t), flag)) {
445 			return (EFAULT);
446 		}
447 		ret = okm_process_status(okmsp, &istatus);
448 		DPRINTF(DBG_DRV, ("okm_ioctl: STATUS ret=%d\n", ret));
449 		break;
450 
451 	default:
452 		DPRINTF(DBG_DRV, ("okm_ioctl: UNKNOWN ioctl\n"));
453 		ret = EINVAL;
454 	}
455 	return (ret);
456 }
457 
458 /*
459  * okm_get_req - Get a request from the mailbox.
460  *
461  * Description:	It blocks until a message is received, then processes
462  *		the message and returns it to the requestor.
463  */
464 int
465 okm_get_req(okms_t *okmsp, sckm_ioctl_getreq_t *ireqp, intptr_t data, int flag)
466 {
467 	okm_req_hdr_t *reqp;
468 	caddr_t msgbuf;
469 	uint32_t len;
470 	int ret;
471 
472 	DPRINTF(DBG_DRV, ("okm_getreq: called\n"));
473 	mutex_enter(&okmsp->km_lock);
474 	if ((ret = block_until_ready(okmsp)) != 0) {
475 		mutex_exit(&okmsp->km_lock);
476 		DPRINTF(DBG_WARN, ("okm_getreq: failed ret=%d\n", ret));
477 		return (ret);
478 	}
479 
480 	if (okmsp->km_reqp != NULL) {
481 		DPRINTF(DBG_DRV, ("okm_getreq: req cached\n"));
482 		reqp = okmsp->km_reqp;
483 		len = okmsp->km_reqlen;
484 		okmsp->km_reqp = NULL;
485 		okmsp->km_reqlen = 0;
486 	} else {
487 retry:
488 		while (OKM_MBOX_READY(okmsp) &&
489 		    ((ret = scf_mb_canget(okmsp->km_target,
490 		    okmsp->km_key, &len)) != 0)) {
491 			if (ret != ENOMSG) {
492 				DPRINTF(DBG_WARN, ("okm_getreq: Unknown "
493 				    "mbox failure=%d\n", ret));
494 				mutex_exit(&okmsp->km_lock);
495 				return (EIO);
496 			}
497 			DPRINTF(DBG_MBOX, ("okm_getreq: waiting for mesg\n"));
498 			if (cv_wait_sig(&okmsp->km_wait,
499 			    &okmsp->km_lock) <= 0) {
500 				mutex_exit(&okmsp->km_lock);
501 				DPRINTF(DBG_DRV, ("okm_getreq:interrupted\n"));
502 				return (EINTR);
503 			}
504 		}
505 		if (!OKM_MBOX_READY(okmsp)) {
506 			mutex_exit(&okmsp->km_lock);
507 			DPRINTF(DBG_WARN, ("okm_getreq: mailbox not ready\n"));
508 			return (EIO);
509 		}
510 		ASSERT(len != 0);
511 		msgbuf = kmem_alloc(len, KM_SLEEP);
512 		okmsp->km_sg_rcv.msc_dptr = msgbuf;
513 		okmsp->km_sg_rcv.msc_len = len;
514 
515 		DPRINTF(DBG_MBOX, ("okm_getreq: getmsg\n"));
516 		ret = scf_mb_getmsg(okmsp->km_target, okmsp->km_key, len, 1,
517 		    &okmsp->km_sg_rcv, 0);
518 		if (ret == ENOMSG || ret == EMSGSIZE) {
519 			kmem_free(msgbuf, len);
520 			DPRINTF(DBG_MBOX, ("okm_getreq: nomsg ret=%d\n", ret));
521 			goto retry;
522 		} else if (ret != 0) {
523 			kmem_free(msgbuf, len);
524 			mutex_exit(&okmsp->km_lock);
525 			DPRINTF(DBG_WARN,
526 			    ("okm_getreq: Unknown mbox failure=%d\n", ret));
527 			return (EIO);
528 		}
529 
530 		/* check message length */
531 		if (len < sizeof (okm_req_hdr_t)) {
532 			/* protocol error, drop message */
533 			kmem_free(msgbuf, len);
534 			mutex_exit(&okmsp->km_lock);
535 			DPRINTF(DBG_WARN, ("okm_getreq: Bad message\n"));
536 			return (EBADMSG);
537 		}
538 
539 		reqp = (okm_req_hdr_t *)msgbuf;
540 		reqp->krq_version = ntohl(reqp->krq_version);
541 		reqp->krq_transid = ntohl(reqp->krq_transid);
542 		reqp->krq_cmd = ntohl(reqp->krq_cmd);
543 		reqp->krq_reserved = ntohl(reqp->krq_reserved);
544 
545 		/* check version of the message received */
546 		if (reqp->krq_version != OKM_PROTOCOL_VERSION) {
547 			okm_send_reply(okmsp, reqp->krq_transid,
548 			    OKM_ERR_VERSION, 0, 0);
549 			kmem_free(msgbuf, len);
550 			mutex_exit(&okmsp->km_lock);
551 			DPRINTF(DBG_WARN, ("okm_getreq: Unknown version=%d\n",
552 			    reqp->krq_version));
553 			return (EBADMSG);
554 		}
555 	}
556 
557 	/* process message */
558 	ret = okm_process_req(okmsp, reqp, len, ireqp, data, flag);
559 	if (okmsp->km_reqp == NULL) {
560 		/*
561 		 * The message is not saved, so free the buffer.
562 		 */
563 		kmem_free(reqp, len);
564 	}
565 	mutex_exit(&okmsp->km_lock);
566 	DPRINTF(DBG_DRV, ("okm_getreq: ret=%d\n", ret));
567 	return (ret);
568 }
569 
570 
571 /*
572  * okm_process_req - Process the request.
573  *
574  * Description:	Validate the request and then give the request to the
575  *		daemon.
576  */
577 int
578 okm_process_req(okms_t *okmsp, okm_req_hdr_t *reqp, uint32_t len,
579     sckm_ioctl_getreq_t *ireqp, intptr_t data, int flag)
580 {
581 	void *req_datap = (void *)(((char *)reqp) + sizeof (okm_req_hdr_t));
582 	int sadb_msglen = len - sizeof (okm_req_hdr_t);
583 
584 	DPRINTF(DBG_DRV, ("okm_process_req: called\n"));
585 	DUMP_REQ(reqp, len);
586 
587 	switch (reqp->krq_cmd) {
588 	case OKM_MSG_SADB:
589 		/* sanity check request */
590 		if (sadb_msglen <= 0) {
591 			okm_send_reply(okmsp, reqp->krq_transid,
592 			    OKM_ERR_SADB_MSG, 0, 0);
593 			DPRINTF(DBG_WARN, ("okm_process_req: bad message\n"));
594 			return (EBADMSG);
595 		}
596 
597 		/*
598 		 * Save the message, prior to giving it to the daemon.
599 		 */
600 		okmsp->km_reqp = reqp;
601 		okmsp->km_reqlen = len;
602 
603 		if (ireqp->buf_len < len) {
604 			DPRINTF(DBG_WARN,
605 			    ("okm_process_req: not enough space\n"));
606 			return (ENOSPC);
607 		}
608 
609 		ireqp->transid = reqp->krq_transid;
610 		ireqp->type = SCKM_IOCTL_REQ_SADB;
611 		if (ddi_copyout(req_datap, ireqp->buf, sadb_msglen, flag)) {
612 			DPRINTF(DBG_WARN,
613 			    ("okm_process_req: copyout failed\n"));
614 			return (EFAULT);
615 		}
616 		ireqp->buf_len = sadb_msglen;
617 		if (okm_copyout_ioctl_getreq(ireqp, data, flag)) {
618 			DPRINTF(DBG_WARN,
619 			    ("okm_process_req: copyout failed\n"));
620 			return (EFAULT);
621 		}
622 		break;
623 
624 	default:
625 		cmn_err(CE_WARN, "Unknown cmd 0x%x received", reqp->krq_cmd);
626 		/*
627 		 * Received an unknown command, send corresponding
628 		 * error message.
629 		 */
630 		okm_send_reply(okmsp, reqp->krq_transid, OKM_ERR_BAD_CMD, 0, 0);
631 		return (EBADMSG);
632 	}
633 	DPRINTF(DBG_DRV, ("okm_process_req: ret=0\n"));
634 	return (0);
635 }
636 
637 /*
638  * okm_process_status - Process the status from the daemon.
639  *
640  * Description:	Processes the status received from the daemon and sends
641  *		corresponding message to the SP.
642  */
643 int
644 okm_process_status(okms_t *okmsp, sckm_ioctl_status_t *ireply)
645 {
646 	uint32_t status;
647 	uint32_t sadb_msg_errno = 0;
648 	uint32_t sadb_msg_version = 0;
649 	okm_req_hdr_t *reqp = okmsp->km_reqp;
650 	int ret;
651 
652 	DPRINTF(DBG_DRV, ("okm_process_status: called\n"));
653 	mutex_enter(&okmsp->km_lock);
654 	if ((ret = block_until_ready(okmsp)) != 0) {
655 		mutex_exit(&okmsp->km_lock);
656 		DPRINTF(DBG_WARN,
657 		    ("okm_process_status: Unknown failure=%d\n", ret));
658 		return (ret);
659 	}
660 
661 	/* fail if no status is expected, or if it does not match */
662 	if (!okmsp->km_reqp || (reqp->krq_transid != ireply->transid)) {
663 		mutex_exit(&okmsp->km_lock);
664 		DPRINTF(DBG_WARN,
665 		    ("okm_process_status: req/transid mismatch\n"));
666 		return (EINVAL);
667 	}
668 
669 	switch (ireply->status) {
670 	case SCKM_IOCTL_STAT_SUCCESS:
671 		DPRINTF(DBG_DRV, ("okm_process_status: SUCCESS\n"));
672 		status = OKM_SUCCESS;
673 		break;
674 	case SCKM_IOCTL_STAT_ERR_PFKEY:
675 		DPRINTF(DBG_DRV, ("okm_process_status: PFKEY ERROR\n"));
676 		status = OKM_ERR_SADB_PFKEY;
677 		sadb_msg_errno = ireply->sadb_msg_errno;
678 		break;
679 	case SCKM_IOCTL_STAT_ERR_REQ:
680 		DPRINTF(DBG_DRV, ("okm_process_status: REQ ERROR\n"));
681 		status = OKM_ERR_DAEMON;
682 		break;
683 	case SCKM_IOCTL_STAT_ERR_VERSION:
684 		DPRINTF(DBG_DRV, ("okm_process_status: SADB VERSION ERROR\n"));
685 		status = OKM_ERR_SADB_VERSION;
686 		sadb_msg_version = ireply->sadb_msg_version;
687 		break;
688 	case SCKM_IOCTL_STAT_ERR_TIMEOUT:
689 		DPRINTF(DBG_DRV, ("okm_process_status: TIMEOUT ERR\n"));
690 		status = OKM_ERR_SADB_TIMEOUT;
691 		break;
692 	case SCKM_IOCTL_STAT_ERR_OTHER:
693 		DPRINTF(DBG_DRV, ("okm_process_status: OTHER ERR\n"));
694 		status = OKM_ERR_DAEMON;
695 		break;
696 	case SCKM_IOCTL_STAT_ERR_SADB_TYPE:
697 		DPRINTF(DBG_DRV, ("okm_process_status: SADB TYPE ERR\n"));
698 		status = OKM_ERR_SADB_BAD_TYPE;
699 		break;
700 	default:
701 		cmn_err(CE_WARN, "SCKM daemon returned invalid status %d\n",
702 		    ireply->status);
703 		status = OKM_ERR_DAEMON;
704 	}
705 	ret = okm_send_reply(okmsp, ireply->transid, status,
706 	    sadb_msg_errno, sadb_msg_version);
707 	/*
708 	 * Clean up the cached request now.
709 	 */
710 	if (ret == 0) {
711 		kmem_free(okmsp->km_reqp, okmsp->km_reqlen);
712 		okmsp->km_reqp = NULL;
713 		okmsp->km_reqlen = 0;
714 	}
715 	mutex_exit(&okmsp->km_lock);
716 	DPRINTF(DBG_DRV, ("okm_process_status: ret=%d\n", ret));
717 	return (ret);
718 }
719 
720 /*
721  * okm_copyin_ioctl_getreq - copy-in the ioctl request from the daemon.
722  */
723 
724 static int
725 okm_copyin_ioctl_getreq(intptr_t userarg, sckm_ioctl_getreq_t *driverarg,
726     int flag)
727 {
728 #ifdef _MULTI_DATAMODEL
729 	switch (ddi_model_convert_from(flag & FMODELS)) {
730 	case DDI_MODEL_ILP32: {
731 		sckm_ioctl_getreq32_t driverarg32;
732 		if (ddi_copyin((caddr_t)userarg, &driverarg32,
733 		    sizeof (sckm_ioctl_getreq32_t), flag)) {
734 			return (EFAULT);
735 		}
736 		driverarg->transid = driverarg32.transid;
737 		driverarg->type = driverarg32.type;
738 		driverarg->buf = (caddr_t)(uintptr_t)driverarg32.buf;
739 		driverarg->buf_len = driverarg32.buf_len;
740 		break;
741 	}
742 	case DDI_MODEL_NONE: {
743 		if (ddi_copyin((caddr_t)userarg, &driverarg,
744 		    sizeof (sckm_ioctl_getreq_t), flag)) {
745 			return (EFAULT);
746 		}
747 		break;
748 	}
749 	}
750 #else /* ! _MULTI_DATAMODEL */
751 	if (ddi_copyin((caddr_t)userarg, &driverarg,
752 	    sizeof (sckm_ioctl_getreq_t), flag)) {
753 		return (EFAULT);
754 	}
755 #endif /* _MULTI_DATAMODEL */
756 	return (0);
757 }
758 
759 
760 /*
761  * okm_copyout_ioctl_getreq - copy-out the request to the daemon.
762  */
763 static int
764 okm_copyout_ioctl_getreq(sckm_ioctl_getreq_t *driverarg, intptr_t userarg,
765     int flag)
766 {
767 #ifdef _MULTI_DATAMODEL
768 	switch (ddi_model_convert_from(flag & FMODELS)) {
769 	case DDI_MODEL_ILP32: {
770 		sckm_ioctl_getreq32_t driverarg32;
771 		driverarg32.transid = driverarg->transid;
772 		driverarg32.type = driverarg->type;
773 		driverarg32.buf = (caddr32_t)(uintptr_t)driverarg->buf;
774 		driverarg32.buf_len = driverarg->buf_len;
775 		if (ddi_copyout(&driverarg32, (caddr_t)userarg,
776 		    sizeof (sckm_ioctl_getreq32_t), flag)) {
777 			return (EFAULT);
778 		}
779 		break;
780 	}
781 	case DDI_MODEL_NONE:
782 		if (ddi_copyout(driverarg, (caddr_t)userarg,
783 		    sizeof (sckm_ioctl_getreq_t), flag)) {
784 			return (EFAULT);
785 		}
786 		break;
787 	}
788 #else /* ! _MULTI_DATAMODEL */
789 	if (ddi_copyout(driverarg, (caddr_t)userarg,
790 	    sizeof (sckm_ioctl_getreq_t), flag)) {
791 		return (EFAULT);
792 	}
793 #endif /* _MULTI_DATAMODEL */
794 	return (0);
795 }
796 
797 /*
798  * okm_cleanup - Cleanup routine.
799  */
800 static void
801 okm_cleanup(okms_t *okmsp)
802 {
803 
804 	ASSERT(okmsp != NULL);
805 	if (okmsp->km_clean & OKM_CLEAN_NODE) {
806 		ddi_remove_minor_node(okmsp->km_dip, NULL);
807 	}
808 	if (okmsp->km_clean & OKM_CLEAN_LOCK)
809 		mutex_destroy(&okmsp->km_lock);
810 	if (okmsp->km_clean & OKM_CLEAN_CV)
811 		cv_destroy(&okmsp->km_wait);
812 	if (okmsp->km_reqp != NULL) {
813 		kmem_free(okmsp->km_reqp, okmsp->km_reqlen);
814 		okmsp->km_reqp = NULL;
815 		okmsp->km_reqlen = 0;
816 	}
817 	ddi_set_driver_private(okmsp->km_dip, NULL);
818 }
819 
820 /*
821  * okm_mbox_init - Mailbox specific initialization.
822  */
823 static int
824 okm_mbox_init(okms_t *okmsp)
825 {
826 	int ret;
827 	clock_t tout;
828 
829 	ASSERT(MUTEX_HELD(&okmsp->km_lock));
830 	okmsp->km_target = OKM_TARGET_ID;
831 	okmsp->km_key = DKMD_KEY;
832 	okmsp->km_state &= ~OKM_MB_INITED;
833 
834 	/* Iterate until mailbox gets connected */
835 	while (!(okmsp->km_state & OKM_MB_CONN)) {
836 		DPRINTF(DBG_MBOX, ("okm_mbox_init: calling mb_init\n"));
837 		ret = scf_mb_init(okmsp->km_target, okmsp->km_key,
838 		    okm_event_handler, (void *)okmsp);
839 		DPRINTF(DBG_MBOX, ("okm_mbox_init: mb_init ret=%d\n", ret));
840 
841 		if (ret != 0) {
842 			DPRINTF(DBG_MBOX,
843 			    ("okm_mbox_init: failed ret =%d\n", ret));
844 			DTRACE_PROBE1(okm_mbox_fail, int, ret);
845 		} else {
846 			okmsp->km_state |= OKM_MB_INITED;
847 
848 			/* Block until the mailbox is ready to communicate. */
849 			while (!(okmsp->km_state &
850 			    (OKM_MB_CONN | OKM_MB_DISC))) {
851 
852 				if (cv_wait_sig(&okmsp->km_wait,
853 				    &okmsp->km_lock) <= 0) {
854 					/* interrupted */
855 					ret = EINTR;
856 					break;
857 				}
858 			}
859 		}
860 
861 		if ((ret != 0) || (okmsp->km_state & OKM_MB_DISC)) {
862 
863 			if (okmsp->km_state & OKM_MB_INITED) {
864 				(void) scf_mb_fini(okmsp->km_target,
865 				    okmsp->km_key);
866 			}
867 			if (okmsp->km_state & OKM_MB_DISC) {
868 				DPRINTF(DBG_WARN,
869 				    ("okm_mbox_init: mbox DISC_ERROR\n"));
870 				DTRACE_PROBE1(okm_mbox_fail,
871 				    int, OKM_MB_DISC);
872 			}
873 
874 			okmsp->km_state &= ~(OKM_MB_INITED | OKM_MB_DISC |
875 			    OKM_MB_CONN);
876 
877 			if (ret == EINTR) {
878 				return (ret);
879 			}
880 
881 			/*
882 			 * If there was failure, then wait for
883 			 * OKM_MB_TOUT secs and retry again.
884 			 */
885 
886 			DPRINTF(DBG_MBOX, ("okm_mbox_init: waiting...\n"));
887 			tout = ddi_get_lbolt() + drv_usectohz(OKM_MB_TOUT);
888 			ret = cv_timedwait_sig(&okmsp->km_wait,
889 			    &okmsp->km_lock, tout);
890 			if (ret == 0) {
891 				/* if interrupted, return immediately. */
892 				DPRINTF(DBG_MBOX,
893 				    ("okm_mbox_init: interrupted\n"));
894 				return (EINTR);
895 			}
896 		}
897 	}
898 
899 	ret = scf_mb_ctrl(okmsp->km_target, okmsp->km_key,
900 	    SCF_MBOP_MAXMSGSIZE, &okmsp->km_maxsz);
901 
902 	/*
903 	 * The max msg size should be at least the size of reply
904 	 * we need to send.
905 	 */
906 	if ((ret == 0) && (okmsp->km_maxsz < sizeof (okm_rep_hdr_t))) {
907 		cmn_err(CE_WARN, "Max message size expected >= %ld "
908 		    "but found %d\n", sizeof (okm_rep_hdr_t), okmsp->km_maxsz);
909 		ret = EIO;
910 	}
911 	if (ret != 0) {
912 		okmsp->km_state &= ~OKM_MB_INITED;
913 		(void) scf_mb_fini(okmsp->km_target, okmsp->km_key);
914 	}
915 	DPRINTF(DBG_MBOX, ("okm_mbox_init: mb_init ret=%d\n", ret));
916 	return (ret);
917 }
918 
919 /*
920  * okm_mbox_fini - Mailbox de-initialization.
921  */
922 static void
923 okm_mbox_fini(okms_t *okmsp)
924 {
925 	int ret = 0;
926 
927 	ASSERT(MUTEX_HELD(&okmsp->km_lock));
928 	if (okmsp->km_state & OKM_MB_INITED) {
929 		DPRINTF(DBG_MBOX, ("okm_mbox_fini: calling mb_fini\n"));
930 		ret = scf_mb_fini(okmsp->km_target, okmsp->km_key);
931 		DPRINTF(DBG_MBOX, ("okm_mbox_fini: mb_fini ret=%d\n", ret));
932 		if (ret != 0) {
933 			cmn_err(CE_WARN,
934 			    "Failed to close the Mailbox error=%d", ret);
935 		}
936 		okmsp->km_state &= ~(OKM_MB_INITED | OKM_MB_CONN | OKM_MB_DISC);
937 	}
938 }
939 
940 /*
941  * okm_event_handler - Mailbox event handler.
942  *
943  * Description:	Implements a state machine to handle all the mailbox
944  *		events. For each event, it sets the appropriate state
945  *		flag and wakes up the threads waiting for that event.
946  */
947 void
948 okm_event_handler(scf_event_t event, void *arg)
949 {
950 	okms_t *okmsp = (okms_t *)arg;
951 
952 	DPRINTF(DBG_MBOX, ("okm_event_handler: called\n"));
953 	ASSERT(okmsp != NULL);
954 	mutex_enter(&okmsp->km_lock);
955 	if (!(okmsp->km_state & OKM_MB_INITED)) {
956 		/*
957 		 * Ignore all events if the state flag indicates that the
958 		 * mailbox not initialized, this may happen during the close.
959 		 */
960 		mutex_exit(&okmsp->km_lock);
961 		DPRINTF(DBG_MBOX,
962 		    ("okm_event_handler: event=0x%X - mailbox not inited \n",
963 		    event));
964 		return;
965 	}
966 	switch (event) {
967 	case SCF_MB_CONN_OK:
968 		DPRINTF(DBG_MBOX, ("okm_event_handler: Event CONN_OK\n"));
969 		/*
970 		 * Now the mailbox is ready to use, lets wake up
971 		 * any one waiting for this event.
972 		 */
973 		okmsp->km_state |= OKM_MB_CONN;
974 		cv_broadcast(&okmsp->km_wait);
975 		break;
976 
977 	case SCF_MB_MSG_DATA:
978 		DPRINTF(DBG_MBOX, ("okm_event_handler: Event MSG_DATA\n"));
979 		/*
980 		 * A message is available in the mailbox,
981 		 * wakeup if any one is ready to read the message.
982 		 */
983 		if (OKM_MBOX_READY(okmsp)) {
984 			cv_broadcast(&okmsp->km_wait);
985 		}
986 		break;
987 
988 	case SCF_MB_SPACE:
989 		DPRINTF(DBG_MBOX, ("okm_event_handler: Event MB_SPACE\n"));
990 		/*
991 		 * Now the mailbox is ready to transmit, lets
992 		 * wakeup if any one is waiting to write.
993 		 */
994 		if (OKM_MBOX_READY(okmsp)) {
995 			cv_broadcast(&okmsp->km_wait);
996 		}
997 		break;
998 	case SCF_MB_DISC_ERROR:
999 		DPRINTF(DBG_MBOX, ("okm_event_handler: Event DISC_ERROR\n"));
1000 		okmsp->km_state &= ~OKM_MB_CONN;
1001 		okmsp->km_state |= OKM_MB_DISC;
1002 		cv_broadcast(&okmsp->km_wait);
1003 		break;
1004 	default:
1005 		cmn_err(CE_WARN, "Unexpected event received\n");
1006 	}
1007 	mutex_exit(&okmsp->km_lock);
1008 }
1009 
1010 /*
1011  * okm_send_reply - Send a mailbox reply message.
1012  */
1013 int
1014 okm_send_reply(okms_t *okmsp, uint32_t transid,
1015     uint32_t status, uint32_t sadb_err, uint32_t sadb_ver)
1016 {
1017 	okm_rep_hdr_t reply;
1018 	int ret = EIO;
1019 
1020 	DPRINTF(DBG_DRV, ("okm_send_reply: called\n"));
1021 	ASSERT(MUTEX_HELD(&okmsp->km_lock));
1022 	reply.krp_version = htonl(OKM_PROTOCOL_VERSION);
1023 	reply.krp_transid = htonl(transid);
1024 	reply.krp_status = htonl(status);
1025 	reply.krp_sadb_errno = htonl(sadb_err);
1026 	reply.krp_sadb_version = htonl(sadb_ver);
1027 	okmsp->km_sg_tx.msc_dptr = (caddr_t)&reply;
1028 	okmsp->km_sg_tx.msc_len = sizeof (reply);
1029 	DUMP_REPLY(&reply);
1030 
1031 	while (OKM_MBOX_READY(okmsp)) {
1032 		DPRINTF(DBG_MBOX, ("okm_send_reply: sending reply\n"));
1033 		ret = scf_mb_putmsg(okmsp->km_target, okmsp->km_key,
1034 		    sizeof (reply), 1, &okmsp->km_sg_tx, 0);
1035 		DPRINTF(DBG_MBOX, ("okm_send_reply: putmsg ret=%d\n", ret));
1036 		if (ret == EBUSY || ret == ENOSPC) {
1037 			/* mailbox is busy, poll/retry */
1038 			if (cv_timedwait_sig(&okmsp->km_wait,
1039 			    &okmsp->km_lock, okm_timeout_val(ret)) == 0) {
1040 				/* interrupted */
1041 				ret = EINTR;
1042 				DPRINTF(DBG_DRV,
1043 				    ("okm_send_reply: interrupted\n"));
1044 				break;
1045 			}
1046 		} else {
1047 			break;
1048 		}
1049 	}
1050 	DPRINTF(DBG_DRV, ("okm_send_reply: ret=%d\n", ret));
1051 	return (ret);
1052 }
1053 
1054 /*
1055  * okm_timeout_val -- Return appropriate timeout value.
1056  *
1057  * A small timeout value is returned for EBUSY as the mailbox busy
1058  * condition may go away sooner and we are expected to poll.
1059  *
1060  * A larger timeout value is returned for ENOSPC case, as the condition
1061  * depends on the peer to release buffer space.
1062  * NOTE: there will also be an event(SCF_MB_SPACE) but a timeout is
1063  * used for reliability purposes.
1064  */
1065 static clock_t
1066 okm_timeout_val(int error)
1067 {
1068 	clock_t tval;
1069 
1070 	ASSERT(error == EBUSY || error == ENOSPC);
1071 
1072 	if (error == EBUSY) {
1073 		tval = OKM_SM_TOUT;
1074 	} else {
1075 		tval = OKM_LG_TOUT;
1076 	}
1077 	return (drv_usectohz(tval));
1078 }
1079 
1080 #ifdef DEBUG
1081 static void
1082 okm_print_req(okm_req_hdr_t *reqp, uint32_t len)
1083 {
1084 	uint8_t *datap = (uint8_t *)(((char *)reqp) + sizeof (okm_req_hdr_t));
1085 	int msglen = len - sizeof (okm_req_hdr_t);
1086 	int i, j;
1087 #define	BYTES_PER_LINE	20
1088 	char bytestr[BYTES_PER_LINE * 3 + 1];
1089 
1090 	if (!(okm_debug & DBG_MESG))
1091 		return;
1092 	printf("OKM: Request  ver=%d transid=%d cmd=%s\n",
1093 	    reqp->krq_version, reqp->krq_transid,
1094 	    ((reqp->krq_cmd == OKM_MSG_SADB) ? "MSG_SADB" : "UNKNOWN"));
1095 	for (i = 0; i < msglen; ) {
1096 		for (j = 0; (j < BYTES_PER_LINE) && (i < msglen); j++, i++) {
1097 			sprintf(&bytestr[j * 3], "%02X ", datap[i]);
1098 		}
1099 		if (j != 0) {
1100 			printf("\t%s\n", bytestr);
1101 		}
1102 	}
1103 }
1104 
1105 static void
1106 okm_print_rep(okm_rep_hdr_t *repp)
1107 {
1108 	if (!(okm_debug & DBG_MESG))
1109 		return;
1110 	printf("OKM: Reply Ver=%d Transid=%d Status=%d ",
1111 	    repp->krp_version, repp->krp_transid, repp->krp_status);
1112 	printf("Sadb_errno=%d Sadb_ver=%d\n", repp->krp_sadb_errno,
1113 	    repp->krp_sadb_version);
1114 }
1115 #endif
1116