xref: /illumos-gate/usr/src/uts/sun4v/io/ds_pri.c (revision 08516594b0e540dc0f415fa7ae31f54d943a0913)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * sun4v domain services PRI driver
30  */
31 
32 #include <sys/types.h>
33 #include <sys/file.h>
34 #include <sys/errno.h>
35 #include <sys/open.h>
36 #include <sys/cred.h>
37 #include <sys/uio.h>
38 #include <sys/stat.h>
39 #include <sys/ksynch.h>
40 #include <sys/modctl.h>
41 #include <sys/conf.h>
42 #include <sys/devops.h>
43 #include <sys/debug.h>
44 #include <sys/cmn_err.h>
45 #include <sys/ddi.h>
46 #include <sys/sunddi.h>
47 #include <sys/ds.h>
48 
49 #include <sys/ds_pri.h>
50 
51 static uint_t ds_pri_debug = 0;
52 #define	DS_PRI_DBG	if (ds_pri_debug) printf
53 
54 #define	DS_PRI_NAME	"ds_pri"
55 
56 #define	TEST_HARNESS
57 #ifdef TEST_HARNESS
58 #define	DS_PRI_MAX_PRI_SIZE	(64 * 1024)
59 
60 #define	DSIOC_TEST_REG	97
61 #define	DSIOC_TEST_UNREG	98
62 #define	DSIOC_TEST_DATA	99
63 
64 struct ds_pri_test_data {
65 	size_t		size;
66 	void		*data;
67 };
68 
69 struct ds_pri_test_data32 {
70 	size32_t	size;
71 	caddr32_t	data;
72 };
73 #endif /* TEST_HARNESS */
74 
75 typedef	enum {
76 	DS_PRI_REQUEST	= 0,
77 	DS_PRI_DATA	= 1,
78 	DS_PRI_UPDATE	= 2
79 } ds_pri_msg_type_t;
80 
81 typedef	struct {
82 	struct {
83 		uint64_t	seq_num;
84 		uint64_t	type;
85 	} hdr;
86 	uint8_t		data[1];
87 } ds_pri_msg_t;
88 
89 	/* The following are bit field flags */
90 	/* No service implies no PRI and no outstanding request */
91 typedef enum {
92 	DS_PRI_NO_SERVICE = 0x0,
93 	DS_PRI_HAS_SERVICE = 0x1,
94 	DS_PRI_REQUESTED = 0x2,
95 	DS_PRI_HAS_PRI = 0x4
96 } ds_pri_flags_t;
97 
98 struct ds_pri_state {
99 	dev_info_t	*dip;
100 	int		instance;
101 
102 	kmutex_t	lock;
103 	kcondvar_t	cv;
104 
105 	/* PRI/DS */
106 	ds_pri_flags_t	state;
107 	uint64_t	gencount;
108 	ds_svc_hdl_t	ds_pri_handle;
109 	void		*ds_pri;
110 	size_t		ds_pri_len;
111 	uint64_t	req_id;
112 	uint64_t	last_req_id;
113 	int		num_opens;
114 };
115 
116 typedef struct ds_pri_state ds_pri_state_t;
117 
118 static void *ds_pri_statep;
119 
120 static void request_pri(ds_pri_state_t *sp);
121 
122 static int ds_pri_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
123 static int ds_pri_attach(dev_info_t *, ddi_attach_cmd_t);
124 static int ds_pri_detach(dev_info_t *, ddi_detach_cmd_t);
125 static int ds_pri_open(dev_t *, int, int, cred_t *);
126 static int ds_pri_close(dev_t, int, int, cred_t *);
127 static int ds_pri_read(dev_t, struct uio *, cred_t *);
128 static int ds_pri_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
129 
130 /*
131  * DS Callbacks
132  */
133 static void ds_pri_reg_handler(ds_cb_arg_t, ds_ver_t *, ds_svc_hdl_t);
134 static void ds_pri_unreg_handler(ds_cb_arg_t arg);
135 static void ds_pri_data_handler(ds_cb_arg_t arg, void *buf, size_t buflen);
136 
137 /*
138  * PRI DS capability registration
139  */
140 
141 static ds_ver_t ds_pri_ver_1_0 = { 1, 0 };
142 
143 static ds_capability_t ds_pri_cap = {
144 	"pri",
145 	&ds_pri_ver_1_0,
146 	1
147 };
148 
149 /*
150  * PRI DS Client callback vector
151  */
152 static ds_clnt_ops_t ds_pri_ops = {
153 	ds_pri_reg_handler,	/* ds_reg_cb */
154 	ds_pri_unreg_handler,	/* ds_unreg_cb */
155 	ds_pri_data_handler,	/* ds_data_cb */
156 	NULL			/* cb_arg */
157 };
158 
159 /*
160  * DS PRI driver Ops Vector
161  */
162 static struct cb_ops ds_pri_cb_ops = {
163 	ds_pri_open,		/* cb_open */
164 	ds_pri_close,		/* cb_close */
165 	nodev,			/* cb_strategy */
166 	nodev,			/* cb_print */
167 	nodev,			/* cb_dump */
168 	ds_pri_read,		/* cb_read */
169 	nodev,			/* cb_write */
170 	ds_pri_ioctl,		/* cb_ioctl */
171 	nodev,			/* cb_devmap */
172 	nodev,			/* cb_mmap */
173 	nodev,			/* cb_segmap */
174 	nochpoll,		/* cb_chpoll */
175 	ddi_prop_op,		/* cb_prop_op */
176 	(struct streamtab *)NULL, /* cb_str */
177 	D_MP | D_64BIT,		/* cb_flag */
178 	CB_REV,			/* cb_rev */
179 	nodev,			/* cb_aread */
180 	nodev			/* cb_awrite */
181 };
182 
183 static struct dev_ops ds_pri_dev_ops = {
184 	DEVO_REV,		/* devo_rev */
185 	0,			/* devo_refcnt */
186 	ds_pri_getinfo,		/* devo_getinfo */
187 	nulldev,		/* devo_identify */
188 	nulldev,		/* devo_probe */
189 	ds_pri_attach,		/* devo_attach */
190 	ds_pri_detach,		/* devo_detach */
191 	nodev,			/* devo_reset */
192 	&ds_pri_cb_ops,		/* devo_cb_ops */
193 	(struct bus_ops *)NULL,	/* devo_bus_ops */
194 	nulldev			/* devo_power */
195 };
196 
197 static struct modldrv modldrv = {
198 	&mod_driverops,
199 	"Domain Services PRI Driver 1.0",
200 	&ds_pri_dev_ops
201 };
202 
203 static struct modlinkage modlinkage = {
204 	MODREV_1,
205 	(void *)&modldrv,
206 	NULL
207 };
208 
209 
210 int
211 _init(void)
212 {
213 	int retval;
214 
215 	retval = ddi_soft_state_init(&ds_pri_statep,
216 	    sizeof (ds_pri_state_t), 0);
217 	if (retval != 0)
218 		return (retval);
219 
220 	retval = mod_install(&modlinkage);
221 	if (retval != 0) {
222 		ddi_soft_state_fini(&ds_pri_statep);
223 		return (retval);
224 	}
225 
226 	return (retval);
227 }
228 
229 
230 int
231 _info(struct modinfo *modinfop)
232 {
233 	return (mod_info(&modlinkage, modinfop));
234 }
235 
236 
237 int
238 _fini(void)
239 {
240 	int retval;
241 
242 	if ((retval = mod_remove(&modlinkage)) != 0)
243 		return (retval);
244 
245 	ddi_soft_state_fini(&ds_pri_statep);
246 
247 	return (retval);
248 }
249 
250 
251 /*ARGSUSED*/
252 static int
253 ds_pri_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
254 {
255 	ds_pri_state_t *sp;
256 	int retval = DDI_FAILURE;
257 
258 	ASSERT(resultp != NULL);
259 
260 	switch (cmd) {
261 	case DDI_INFO_DEVT2DEVINFO:
262 		sp = ddi_get_soft_state(ds_pri_statep, getminor((dev_t)arg));
263 		if (sp != NULL) {
264 			*resultp = sp->dip;
265 			retval = DDI_SUCCESS;
266 		} else
267 			*resultp = NULL;
268 		break;
269 
270 	case DDI_INFO_DEVT2INSTANCE:
271 		*resultp = (void *)(uintptr_t)getminor((dev_t)arg);
272 		retval = DDI_SUCCESS;
273 		break;
274 
275 	default:
276 		break;
277 	}
278 
279 	return (retval);
280 }
281 
282 
283 static int
284 ds_pri_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
285 {
286 	int instance;
287 	ds_pri_state_t *sp;
288 	int rv;
289 
290 	switch (cmd) {
291 	case DDI_ATTACH:
292 		break;
293 
294 	case DDI_RESUME:
295 		return (DDI_SUCCESS);
296 
297 	default:
298 		return (DDI_FAILURE);
299 	}
300 
301 	instance = ddi_get_instance(dip);
302 
303 	if (ddi_soft_state_zalloc(ds_pri_statep, instance) !=
304 	    DDI_SUCCESS) {
305 		cmn_err(CE_WARN, "%s@%d: Unable to allocate state",
306 		    DS_PRI_NAME, instance);
307 		return (DDI_FAILURE);
308 	}
309 	sp = ddi_get_soft_state(ds_pri_statep, instance);
310 
311 	mutex_init(&sp->lock, NULL, MUTEX_DEFAULT, NULL);
312 	cv_init(&sp->cv, NULL, CV_DEFAULT, NULL);
313 
314 	if (ddi_create_minor_node(dip, DS_PRI_NAME, S_IFCHR, instance,
315 		DDI_PSEUDO, 0) != DDI_SUCCESS) {
316 		cmn_err(CE_WARN, "%s@%d: Unable to create minor node",
317 		    DS_PRI_NAME, instance);
318 		goto fail;
319 	}
320 
321 	if (ds_pri_ops.cb_arg != NULL)
322 		goto fail;
323 	ds_pri_ops.cb_arg = dip;
324 
325 	sp->state = DS_PRI_NO_SERVICE;
326 
327 	/* Until the service registers the handle is invalid */
328 	sp->ds_pri_handle = DS_INVALID_HDL;
329 
330 	sp->ds_pri = NULL;
331 	sp->ds_pri_len = 0;
332 	sp->req_id = 0;
333 	sp->num_opens = 0;
334 
335 	if ((rv = ds_cap_init(&ds_pri_cap, &ds_pri_ops)) != 0) {
336 		cmn_err(CE_NOTE, "ds_cap_init failed: %d", rv);
337 		goto fail;
338 	}
339 
340 	ddi_report_dev(dip);
341 
342 	return (DDI_SUCCESS);
343 
344 fail:
345 	ddi_remove_minor_node(dip, NULL);
346 	cv_destroy(&sp->cv);
347 	mutex_destroy(&sp->lock);
348 	ddi_soft_state_free(ds_pri_statep, instance);
349 	return (DDI_FAILURE);
350 
351 }
352 
353 
354 /*ARGSUSED*/
355 static int
356 ds_pri_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
357 {
358 	ds_pri_state_t *sp;
359 	int instance;
360 	int rv;
361 
362 	instance = ddi_get_instance(dip);
363 	sp = ddi_get_soft_state(ds_pri_statep, instance);
364 
365 	switch (cmd) {
366 	case DDI_DETACH:
367 		break;
368 
369 	case DDI_SUSPEND:
370 		return (DDI_SUCCESS);
371 
372 	default:
373 		return (DDI_FAILURE);
374 	}
375 
376 	/* This really shouldn't fail - but check anyway */
377 	if ((rv = ds_cap_fini(&ds_pri_cap)) != 0) {
378 		cmn_err(CE_WARN, "ds_cap_fini failed: %d", rv);
379 	}
380 
381 	if (sp != NULL && sp->ds_pri_len != 0)
382 		kmem_free(sp->ds_pri, sp->ds_pri_len);
383 
384 	ddi_remove_minor_node(dip, NULL);
385 	cv_destroy(&sp->cv);
386 	mutex_destroy(&sp->lock);
387 	ddi_soft_state_free(ds_pri_statep, instance);
388 
389 	return (DDI_SUCCESS);
390 }
391 
392 
393 /*ARGSUSED*/
394 static int
395 ds_pri_open(dev_t *devp, int flag, int otyp, cred_t *credp)
396 {
397 	ds_pri_state_t *sp;
398 	int instance;
399 
400 	if (otyp != OTYP_CHR)
401 		return (EINVAL);
402 
403 	instance = getminor(*devp);
404 	sp = ddi_get_soft_state(ds_pri_statep, instance);
405 	if (sp == NULL)
406 		return (ENXIO);
407 
408 	mutex_enter(&sp->lock);
409 
410 	/*
411 	 * If we're here and the state is DS_PRI_NO_SERVICE then this
412 	 * means that ds hasn't yet called the registration callback.
413 	 * Wait here and the callback will signal us when it has completed
414 	 * its work.
415 	 */
416 	if (sp->state == DS_PRI_NO_SERVICE) {
417 		if (cv_wait_sig(&sp->cv, &sp->lock) == 0) {
418 			mutex_exit(&sp->lock);
419 			return (EINTR);
420 		}
421 	}
422 
423 	sp->num_opens++;
424 
425 	mutex_exit(&sp->lock);
426 
427 	/*
428 	 * On open we dont fetch the PRI even if we have a valid service
429 	 * handle. PRI fetch is essentially lazy and on-demand.
430 	 */
431 
432 	DS_PRI_DBG("ds_pri_open: state = 0x%x\n", sp->state);
433 
434 	return (0);
435 }
436 
437 
438 /*ARGSUSED*/
439 static int
440 ds_pri_close(dev_t dev, int flag, int otyp, cred_t *credp)
441 {
442 	int instance;
443 	ds_pri_state_t *sp;
444 
445 	if (otyp != OTYP_CHR)
446 		return (EINVAL);
447 
448 	DS_PRI_DBG("ds_pri_close\n");
449 
450 	instance = getminor(dev);
451 	if ((sp = ddi_get_soft_state(ds_pri_statep, instance)) == NULL)
452 		return (ENXIO);
453 
454 	mutex_enter(&sp->lock);
455 	if (!(sp->state & DS_PRI_HAS_SERVICE)) {
456 		mutex_exit(&sp->lock);
457 		return (0);
458 	}
459 
460 	if (--sp->num_opens > 0) {
461 		mutex_exit(&sp->lock);
462 		return (0);
463 	}
464 
465 	/* If we have an old PRI - remove it */
466 	if (sp->state & DS_PRI_HAS_PRI) {
467 		if (sp->ds_pri != NULL && sp->ds_pri_len > 0) {
468 			/*
469 			 * remove the old data if we have an
470 			 * outstanding request
471 			 */
472 			kmem_free(sp->ds_pri, sp->ds_pri_len);
473 			sp->ds_pri_len = 0;
474 			sp->ds_pri = NULL;
475 		}
476 		sp->state &= ~DS_PRI_HAS_PRI;
477 	}
478 	sp->state &= ~DS_PRI_REQUESTED;
479 	mutex_exit(&sp->lock);
480 	return (0);
481 }
482 
483 
484 /*ARGSUSED*/
485 static int
486 ds_pri_read(dev_t dev, struct uio *uiop, cred_t *credp)
487 {
488 	ds_pri_state_t *sp;
489 	int instance;
490 	size_t len;
491 	int retval;
492 	caddr_t tmpbufp;
493 
494 	instance = getminor(dev);
495 	if ((sp = ddi_get_soft_state(ds_pri_statep, instance)) == NULL)
496 		return (ENXIO);
497 
498 	len = uiop->uio_resid;
499 
500 	if (len == 0)
501 		return (0);
502 
503 	mutex_enter(&sp->lock);
504 
505 	DS_PRI_DBG("ds_pri_read: state = 0x%x\n", sp->state);
506 
507 	/* block or bail if there is no current PRI */
508 	if (!(sp->state & DS_PRI_HAS_PRI)) {
509 		DS_PRI_DBG("ds_pri_read: no PRI held\n");
510 
511 		if (uiop->uio_fmode & (FNDELAY | FNONBLOCK)) {
512 			mutex_exit(&sp->lock);
513 			return (EAGAIN);
514 		}
515 
516 		while (!(sp->state & DS_PRI_HAS_PRI)) {
517 			DS_PRI_DBG("ds_pri_read: state = 0x%x\n", sp->state);
518 			request_pri(sp);
519 			if (cv_wait_sig(&sp->cv, &sp->lock) == 0) {
520 				mutex_exit(&sp->lock);
521 				return (EINTR);
522 			}
523 		}
524 	}
525 
526 	if (uiop->uio_offset < 0 || uiop->uio_offset > sp->ds_pri_len) {
527 		mutex_exit(&sp->lock);
528 		return (EINVAL);
529 	}
530 
531 	if (len > (sp->ds_pri_len - uiop->uio_offset))
532 		len = sp->ds_pri_len - uiop->uio_offset;
533 
534 	/* already checked that offset < ds_pri_len above */
535 	if (len == 0) {
536 		mutex_exit(&sp->lock);
537 		return (0);
538 	}
539 
540 	/*
541 	 * We're supposed to move the data out to userland, but
542 	 * that can suspend because of page faults etc., and meanwhile
543 	 * other parts of this driver want to update the PRI buffer ...
544 	 * we could hold the data buffer locked with a flag etc.,
545 	 * but that's still a lock ... a simpler mechanism - if not quite
546 	 * as performance efficient is to simply clone here the part of
547 	 * the buffer we care about and then the original can be released
548 	 * for further updates while the uiomove continues.
549 	 */
550 
551 	tmpbufp = kmem_alloc(len, KM_SLEEP);
552 	bcopy(((caddr_t)sp->ds_pri) + uiop->uio_offset, tmpbufp, len);
553 	mutex_exit(&sp->lock);
554 
555 	retval = uiomove(tmpbufp, len, UIO_READ, uiop);
556 
557 	kmem_free(tmpbufp, len);
558 
559 	return (retval);
560 }
561 
562 
563 /*ARGSUSED*/
564 static int
565 ds_pri_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
566     int *rvalp)
567 {
568 	ds_pri_state_t *sp;
569 	int instance;
570 
571 	instance = getminor(dev);
572 	if ((sp = ddi_get_soft_state(ds_pri_statep, instance)) == NULL)
573 		return (ENXIO);
574 
575 	switch (cmd) {
576 	case DSPRI_GETINFO: {
577 		struct dspri_info info;
578 
579 		if (!(mode & FREAD))
580 			return (EACCES);
581 
582 		/*
583 		 * We are not guaranteed that ddi_copyout(9F) will read
584 		 * atomically anything larger than a byte.  Therefore we
585 		 * must duplicate the size before copying it out to the user.
586 		 */
587 		mutex_enter(&sp->lock);
588 
589 loop:;
590 		if (sp->state & DS_PRI_HAS_PRI) {
591 			/* If we have a PRI simply return the info */
592 			info.size = sp->ds_pri_len;
593 			info.token = sp->gencount;
594 		} else
595 		if (!(sp->state & DS_PRI_HAS_SERVICE)) {
596 			/* If we have no service return a nil response */
597 			info.size = 0;
598 			info.token = 0;
599 		} else {
600 			request_pri(sp);
601 			/* wait for something & check again */
602 			if (cv_wait_sig(&sp->cv, &sp->lock) == 0) {
603 				mutex_exit(&sp->lock);
604 				return (EINTR);
605 			}
606 			goto loop;
607 		}
608 		DS_PRI_DBG("ds_pri_ioctl: DSPRI_GETINFO sz=0x%lx tok=0x%lx\n",
609 			info.size, info.token);
610 		mutex_exit(&sp->lock);
611 
612 		if (ddi_copyout(&info, (void *)arg, sizeof (info), mode) != 0)
613 			return (EFAULT);
614 		break;
615 	}
616 
617 	case DSPRI_WAIT: {
618 		uint64_t gencount;
619 
620 		if (ddi_copyin((void *)arg, &gencount, sizeof (gencount),
621 		    mode) != 0)
622 			return (EFAULT);
623 
624 		mutex_enter(&sp->lock);
625 
626 		DS_PRI_DBG("ds_pri_ioctl: DSPRI_WAIT gen=0x%lx sp->gen=0x%lx\n",
627 			gencount, sp->gencount);
628 
629 		while ((sp->state & DS_PRI_HAS_PRI) == 0 ||
630 			gencount == sp->gencount) {
631 			if (cv_wait_sig(&sp->cv, &sp->lock) == 0) {
632 				mutex_exit(&sp->lock);
633 				return (EINTR);
634 			}
635 		}
636 		mutex_exit(&sp->lock);
637 		break;
638 	}
639 
640 	default:
641 		return (ENOTTY);
642 	}
643 	return (0);
644 }
645 
646 
647 	/* assumes sp->lock is held when called */
648 static void
649 request_pri(ds_pri_state_t *sp)
650 {
651 	ds_pri_msg_t reqmsg;
652 
653 	ASSERT(MUTEX_HELD(&sp->lock));
654 
655 	/* If a request is already pending we're done */
656 	if (!(sp->state & DS_PRI_HAS_SERVICE))
657 		return;
658 	if (sp->state & DS_PRI_REQUESTED)
659 		return;
660 
661 	/* If we have an old PRI - remove it */
662 	if (sp->state & DS_PRI_HAS_PRI) {
663 		ASSERT(sp->ds_pri_len != 0);
664 		ASSERT(sp->ds_pri != NULL);
665 
666 		/* remove the old data if we have an outstanding request */
667 		kmem_free(sp->ds_pri, sp->ds_pri_len);
668 		sp->ds_pri_len = 0;
669 		sp->ds_pri = NULL;
670 		sp->state &= ~DS_PRI_HAS_PRI;
671 	} else {
672 		ASSERT(sp->ds_pri == NULL);
673 		ASSERT(sp->ds_pri_len == 0);
674 	}
675 
676 	reqmsg.hdr.seq_num = ++(sp->req_id);
677 	reqmsg.hdr.type = DS_PRI_REQUEST;
678 
679 	DS_PRI_DBG("request_pri: request id 0x%lx\n", sp->req_id);
680 
681 		/*
682 		 * Request consists of header only.
683 		 * We don't care about fail status for ds_send;
684 		 * if it does fail we will get an unregister callback
685 		 * from the DS framework and we handle the state change
686 		 * there.
687 		 */
688 	(void) ds_cap_send(sp->ds_pri_handle, &reqmsg, sizeof (reqmsg.hdr));
689 
690 	sp->state |= DS_PRI_REQUESTED;
691 	sp->last_req_id = sp->req_id;
692 }
693 
694 /*
695  * DS Callbacks
696  */
697 /*ARGSUSED*/
698 static void
699 ds_pri_reg_handler(ds_cb_arg_t arg, ds_ver_t *ver, ds_svc_hdl_t hdl)
700 {
701 	dev_info_t *dip = arg;
702 	ds_pri_state_t *sp;
703 	int instance;
704 
705 	instance = ddi_get_instance(dip);
706 	if ((sp = ddi_get_soft_state(ds_pri_statep, instance)) == NULL)
707 		return;
708 
709 	DS_PRI_DBG("ds_pri_reg_handler: registering handle 0x%lx for version "
710 		"0x%x:0x%x\n", (uint64_t)hdl, ver->major, ver->minor);
711 
712 	/* When the domain service comes up automatically req the pri */
713 	mutex_enter(&sp->lock);
714 
715 	ASSERT(sp->ds_pri_handle == DS_INVALID_HDL);
716 	sp->ds_pri_handle = hdl;
717 
718 	ASSERT(sp->state == DS_PRI_NO_SERVICE);
719 	ASSERT(sp->ds_pri == NULL);
720 	ASSERT(sp->ds_pri_len == 0);
721 
722 	/* have service, but no PRI */
723 	sp->state |= DS_PRI_HAS_SERVICE;
724 
725 	/*
726 	 * Cannot request a PRI here, because the reg handler cannot
727 	 * do a DS send operation - we take care of this later.
728 	 */
729 
730 	/* Wake up anyone waiting in open() */
731 	cv_broadcast(&sp->cv);
732 
733 	mutex_exit(&sp->lock);
734 }
735 
736 
737 static void
738 ds_pri_unreg_handler(ds_cb_arg_t arg)
739 {
740 	dev_info_t *dip = arg;
741 	ds_pri_state_t *sp;
742 	int instance;
743 
744 	instance = ddi_get_instance(dip);
745 	if ((sp = ddi_get_soft_state(ds_pri_statep, instance)) == NULL)
746 		return;
747 
748 	DS_PRI_DBG("ds_pri_unreg_handler: un-registering ds_pri service\n");
749 
750 	mutex_enter(&sp->lock);
751 
752 	/* Once the service goes - if we have a PRI at hand free it up */
753 	if (sp->ds_pri_len != 0) {
754 		kmem_free(sp->ds_pri, sp->ds_pri_len);
755 		sp->ds_pri_len = 0;
756 		sp->ds_pri = NULL;
757 	}
758 	sp->ds_pri_handle = DS_INVALID_HDL;
759 	sp->state = DS_PRI_NO_SERVICE;
760 
761 	mutex_exit(&sp->lock);
762 }
763 
764 
765 static void
766 ds_pri_data_handler(ds_cb_arg_t arg, void *buf, size_t buflen)
767 {
768 	dev_info_t *dip = arg;
769 	ds_pri_state_t *sp;
770 	int instance;
771 	void *data;
772 	ds_pri_msg_t	*msgp;
773 	size_t	pri_size;
774 
775 	msgp = (ds_pri_msg_t *)buf;
776 
777 	/* make sure the header is at least valid */
778 	if (buflen < sizeof (msgp->hdr))
779 		return;
780 
781 	DS_PRI_DBG("ds_pri_data_handler: msg buf len 0x%lx : type 0x%lx, "
782 		"seqn 0x%lx\n", buflen, msgp->hdr.type, msgp->hdr.seq_num);
783 
784 	instance = ddi_get_instance(dip);
785 	if ((sp = ddi_get_soft_state(ds_pri_statep, instance)) == NULL)
786 		return;
787 
788 	mutex_enter(&sp->lock);
789 
790 	ASSERT(sp->state & DS_PRI_HAS_SERVICE);
791 
792 	switch (msgp->hdr.type) {
793 	case DS_PRI_DATA:	/* in response to a request from us */
794 		break;
795 	case DS_PRI_UPDATE:	/* aynch notification */
796 			/* our default response to this is to request the PRI */
797 		/* simply issue a request for the new PRI */
798 		request_pri(sp);
799 		goto done;
800 	default:	/* ignore garbage or unknown message types */
801 		goto done;
802 	}
803 
804 	/*
805 	 * If there is no pending PRI request, then we've received a
806 	 * bogus data message ... so ignore it.
807 	 */
808 
809 	if (!(sp->state & DS_PRI_REQUESTED)) {
810 		cmn_err(CE_WARN, "Received DS pri data without request");
811 		goto done;
812 	}
813 
814 	/* response to a request therefore old PRI must be gone */
815 	ASSERT(!(sp->state & DS_PRI_HAS_PRI));
816 	ASSERT(sp->ds_pri_len == 0);
817 	ASSERT(sp->ds_pri == NULL);
818 
819 	/* response seq_num should match our request seq_num */
820 	if (msgp->hdr.seq_num != sp->last_req_id) {
821 		cmn_err(CE_WARN, "Received DS pri data out of sequence with "
822 			"request");
823 		goto done;
824 	}
825 
826 	pri_size = buflen - sizeof (msgp->hdr);
827 	data = kmem_alloc(pri_size, KM_SLEEP);
828 	sp->ds_pri = data;
829 	sp->ds_pri_len = pri_size;
830 	bcopy(msgp->data, data, sp->ds_pri_len);
831 	sp->state &= ~DS_PRI_REQUESTED;
832 	sp->state |= DS_PRI_HAS_PRI;
833 
834 	sp->gencount++;
835 	cv_broadcast(&sp->cv);
836 
837 done:;
838 	mutex_exit(&sp->lock);
839 }
840