xref: /titanic_50/usr/src/uts/sun4v/io/vds.c (revision ba4e3c84e6b9390bbf7df80b5f1d11dec34cc525)
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 2006 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  * Virtual disk server
31  */
32 
33 
34 #include <sys/types.h>
35 #include <sys/conf.h>
36 #include <sys/ddi.h>
37 #include <sys/dkio.h>
38 #include <sys/file.h>
39 #include <sys/mdeg.h>
40 #include <sys/modhash.h>
41 #include <sys/note.h>
42 #include <sys/pathname.h>
43 #include <sys/sunddi.h>
44 #include <sys/sunldi.h>
45 #include <sys/sysmacros.h>
46 #include <sys/vio_common.h>
47 #include <sys/vdsk_mailbox.h>
48 #include <sys/vdsk_common.h>
49 #include <sys/vtoc.h>
50 
51 
52 /* Virtual disk server initialization flags */
53 #define	VDS_LDI			0x01
54 #define	VDS_MDEG		0x02
55 
56 /* Virtual disk server tunable parameters */
57 #define	VDS_LDC_RETRIES		3
58 #define	VDS_NCHAINS		32
59 
60 /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
61 #define	VDS_NAME		"virtual-disk-server"
62 
63 #define	VD_NAME			"vd"
64 #define	VD_VOLUME_NAME		"vdisk"
65 #define	VD_ASCIILABEL		"Virtual Disk"
66 
67 #define	VD_CHANNEL_ENDPOINT	"channel-endpoint"
68 #define	VD_ID_PROP		"id"
69 #define	VD_BLOCK_DEVICE_PROP	"vds-block-device"
70 
71 /* Virtual disk initialization flags */
72 #define	VD_LOCKING		0x01
73 #define	VD_LDC			0x02
74 #define	VD_DRING		0x04
75 #define	VD_SID			0x08
76 #define	VD_SEQ_NUM		0x10
77 
78 /* Flags for opening/closing backing devices via LDI */
79 #define	VD_OPEN_FLAGS		(FEXCL | FREAD | FWRITE)
80 
81 /*
82  * By Solaris convention, slice/partition 2 represents the entire disk;
83  * unfortunately, this convention does not appear to be codified.
84  */
85 #define	VD_ENTIRE_DISK_SLICE	2
86 
87 /* Return a cpp token as a string */
88 #define	STRINGIZE(token)	#token
89 
90 /*
91  * Print a message prefixed with the current function name to the message log
92  * (and optionally to the console for verbose boots); these macros use cpp's
93  * concatenation of string literals and C99 variable-length-argument-list
94  * macros
95  */
96 #define	PRN(...)	_PRN("?%s():  "__VA_ARGS__, "")
97 #define	_PRN(format, ...)					\
98 	cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
99 
100 /* Return a pointer to the "i"th vdisk dring element */
101 #define	VD_DRING_ELEM(i)	((vd_dring_entry_t *)(void *)	\
102 	    (vd->dring + (i)*vd->descriptor_size))
103 
104 /* Return the virtual disk client's type as a string (for use in messages) */
105 #define	VD_CLIENT(vd)							\
106 	(((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" :	\
107 	    (((vd)->xfer_mode == VIO_DRING_MODE) ? "dring client" :	\
108 		(((vd)->xfer_mode == 0) ? "null client" :		\
109 		    "unsupported client")))
110 
111 /* Debugging macros */
112 #ifdef DEBUG
113 #define	PR0 if (vd_msglevel > 0)	PRN
114 #define	PR1 if (vd_msglevel > 1)	PRN
115 #define	PR2 if (vd_msglevel > 2)	PRN
116 
117 #define	VD_DUMP_DRING_ELEM(elem)					\
118 	PRN("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n",		\
119 	    elem->hdr.dstate,						\
120 	    elem->payload.operation,					\
121 	    elem->payload.status,					\
122 	    elem->payload.nbytes,					\
123 	    elem->payload.addr,						\
124 	    elem->payload.ncookies);
125 
126 #else	/* !DEBUG */
127 #define	PR0(...)
128 #define	PR1(...)
129 #define	PR2(...)
130 
131 #define	VD_DUMP_DRING_ELEM(elem)
132 
133 #endif	/* DEBUG */
134 
135 
136 /*
137  * Soft state structure for a vds instance
138  */
139 typedef struct vds {
140 	uint_t		initialized;	/* driver inst initialization flags */
141 	dev_info_t	*dip;		/* driver inst devinfo pointer */
142 	ldi_ident_t	ldi_ident;	/* driver's identifier for LDI */
143 	mod_hash_t	*vd_table;	/* table of virtual disks served */
144 	mdeg_handle_t	mdeg;		/* handle for MDEG operations  */
145 } vds_t;
146 
147 /*
148  * Types of descriptor-processing tasks
149  */
150 typedef enum vd_task_type {
151 	VD_NONFINAL_RANGE_TASK,	/* task for intermediate descriptor in range */
152 	VD_FINAL_RANGE_TASK,	/* task for last in a range of descriptors */
153 } vd_task_type_t;
154 
155 /*
156  * Structure describing the task for processing a descriptor
157  */
158 typedef struct vd_task {
159 	struct vd		*vd;		/* vd instance task is for */
160 	vd_task_type_t		type;		/* type of descriptor task */
161 	int			index;		/* dring elem index for task */
162 	vio_msg_t		*msg;		/* VIO message task is for */
163 	size_t			msglen;		/* length of message content */
164 	size_t			msgsize;	/* size of message buffer */
165 	vd_dring_payload_t	*request;	/* request task will perform */
166 	struct buf		buf;		/* buf(9s) for I/O request */
167 
168 } vd_task_t;
169 
170 /*
171  * Soft state structure for a virtual disk instance
172  */
173 typedef struct vd {
174 	uint_t			initialized;	/* vdisk initialization flags */
175 	vds_t			*vds;		/* server for this vdisk */
176 	ddi_taskq_t		*startq;	/* queue for I/O start tasks */
177 	ddi_taskq_t		*completionq;	/* queue for completion tasks */
178 	ldi_handle_t		ldi_handle[V_NUMPAR];	/* LDI slice handles */
179 	dev_t			dev[V_NUMPAR];	/* dev numbers for slices */
180 	uint_t			nslices;	/* number for slices */
181 	size_t			vdisk_size;	/* number of blocks in vdisk */
182 	vd_disk_type_t		vdisk_type;	/* slice or entire disk */
183 	boolean_t		pseudo;		/* underlying pseudo dev */
184 	struct dk_geom		dk_geom;	/* synthetic for slice type */
185 	struct vtoc		vtoc;		/* synthetic for slice type */
186 	ldc_status_t		ldc_state;	/* LDC connection state */
187 	ldc_handle_t		ldc_handle;	/* handle for LDC comm */
188 	size_t			max_msglen;	/* largest LDC message len */
189 	vd_state_t		state;		/* client handshake state */
190 	uint8_t			xfer_mode;	/* transfer mode with client */
191 	uint32_t		sid;		/* client's session ID */
192 	uint64_t		seq_num;	/* message sequence number */
193 	uint64_t		dring_ident;	/* identifier of dring */
194 	ldc_dring_handle_t	dring_handle;	/* handle for dring ops */
195 	uint32_t		descriptor_size;	/* num bytes in desc */
196 	uint32_t		dring_len;	/* number of dring elements */
197 	caddr_t			dring;		/* address of dring */
198 	vd_task_t		inband_task;	/* task for inband descriptor */
199 	vd_task_t		*dring_task;	/* tasks dring elements */
200 
201 	kmutex_t		lock;		/* protects variables below */
202 	boolean_t		enabled;	/* is vdisk enabled? */
203 	boolean_t		reset_state;	/* reset connection state? */
204 	boolean_t		reset_ldc;	/* reset LDC channel? */
205 } vd_t;
206 
207 typedef struct vds_operation {
208 	uint8_t	operation;
209 	int	(*start)(vd_task_t *task);
210 	void	(*complete)(void *arg);
211 } vds_operation_t;
212 
213 typedef struct vd_ioctl {
214 	uint8_t		operation;		/* vdisk operation */
215 	const char	*operation_name;	/* vdisk operation name */
216 	size_t		nbytes;			/* size of operation buffer */
217 	int		cmd;			/* corresponding ioctl cmd */
218 	const char	*cmd_name;		/* ioctl cmd name */
219 	void		*arg;			/* ioctl cmd argument */
220 	/* convert input vd_buf to output ioctl_arg */
221 	void		(*copyin)(void *vd_buf, void *ioctl_arg);
222 	/* convert input ioctl_arg to output vd_buf */
223 	void		(*copyout)(void *ioctl_arg, void *vd_buf);
224 } vd_ioctl_t;
225 
226 /* Define trivial copyin/copyout conversion function flag */
227 #define	VD_IDENTITY	((void (*)(void *, void *))-1)
228 
229 
230 static int	vds_ldc_retries = VDS_LDC_RETRIES;
231 static void	*vds_state;
232 static uint64_t	vds_operations;	/* see vds_operation[] definition below */
233 
234 static int	vd_open_flags = VD_OPEN_FLAGS;
235 
236 /*
237  * Supported protocol version pairs, from highest (newest) to lowest (oldest)
238  *
239  * Each supported major version should appear only once, paired with (and only
240  * with) its highest supported minor version number (as the protocol requires
241  * supporting all lower minor version numbers as well)
242  */
243 static const vio_ver_t	vds_version[] = {{1, 0}};
244 static const size_t	vds_num_versions =
245     sizeof (vds_version)/sizeof (vds_version[0]);
246 
247 #ifdef DEBUG
248 static int	vd_msglevel;
249 #endif /* DEBUG */
250 
251 
252 static int
253 vd_start_bio(vd_task_t *task)
254 {
255 	int			status = 0;
256 	vd_t			*vd		= task->vd;
257 	vd_dring_payload_t	*request	= task->request;
258 	struct buf		*buf		= &task->buf;
259 
260 
261 	ASSERT(vd != NULL);
262 	ASSERT(request != NULL);
263 	ASSERT(request->slice < vd->nslices);
264 	ASSERT((request->operation == VD_OP_BREAD) ||
265 	    (request->operation == VD_OP_BWRITE));
266 
267 	if (request->nbytes == 0)
268 		return (EINVAL);	/* no service for trivial requests */
269 
270 	PR1("%s %lu bytes at block %lu",
271 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
272 	    request->nbytes, request->addr);
273 
274 	bioinit(buf);
275 	buf->b_flags		= B_BUSY;
276 	buf->b_bcount		= request->nbytes;
277 	buf->b_un.b_addr	= kmem_alloc(buf->b_bcount, KM_SLEEP);
278 	buf->b_lblkno		= request->addr;
279 	buf->b_edev		= vd->dev[request->slice];
280 
281 	if (request->operation == VD_OP_BREAD) {
282 		buf->b_flags |= B_READ;
283 	} else {
284 		buf->b_flags |= B_WRITE;
285 		/* Get data to write from client */
286 		if ((status = ldc_mem_copy(vd->ldc_handle, buf->b_un.b_addr, 0,
287 			    &request->nbytes, request->cookie,
288 			    request->ncookies, LDC_COPY_IN)) != 0) {
289 			PRN("ldc_mem_copy() returned errno %d "
290 			    "copying from client", status);
291 		}
292 	}
293 
294 	/* Start the block I/O */
295 	if ((status == 0) &&
296 	    ((status = ldi_strategy(vd->ldi_handle[request->slice], buf)) == 0))
297 		return (EINPROGRESS);	/* will complete on completionq */
298 
299 	/* Clean up after error */
300 	kmem_free(buf->b_un.b_addr, buf->b_bcount);
301 	biofini(buf);
302 	return (status);
303 }
304 
305 static int
306 send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
307 {
308 	int	retry, status;
309 	size_t	nbytes;
310 
311 
312 	for (retry = 0, status = EWOULDBLOCK;
313 	    retry < vds_ldc_retries && status == EWOULDBLOCK;
314 	    retry++) {
315 		PR1("ldc_write() attempt %d", (retry + 1));
316 		nbytes = msglen;
317 		status = ldc_write(ldc_handle, msg, &nbytes);
318 	}
319 
320 	if (status != 0) {
321 		PRN("ldc_write() returned errno %d", status);
322 		return (status);
323 	} else if (nbytes != msglen) {
324 		PRN("ldc_write() performed only partial write");
325 		return (EIO);
326 	}
327 
328 	PR1("SENT %lu bytes", msglen);
329 	return (0);
330 }
331 
332 static void
333 vd_need_reset(vd_t *vd, boolean_t reset_ldc)
334 {
335 	mutex_enter(&vd->lock);
336 	vd->reset_state	= B_TRUE;
337 	vd->reset_ldc	= reset_ldc;
338 	mutex_exit(&vd->lock);
339 }
340 
341 /*
342  * Reset the state of the connection with a client, if needed; reset the LDC
343  * transport as well, if needed.  This function should only be called from the
344  * "startq", as it waits for tasks on the "completionq" and will deadlock if
345  * called from that queue.
346  */
347 static void
348 vd_reset_if_needed(vd_t *vd)
349 {
350 	int		status = 0;
351 
352 
353 	mutex_enter(&vd->lock);
354 	if (!vd->reset_state) {
355 		ASSERT(!vd->reset_ldc);
356 		mutex_exit(&vd->lock);
357 		return;
358 	}
359 	mutex_exit(&vd->lock);
360 
361 
362 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
363 
364 	/*
365 	 * Let any asynchronous I/O complete before possibly pulling the rug
366 	 * out from under it; defer checking vd->reset_ldc, as one of the
367 	 * asynchronous tasks might set it
368 	 */
369 	ddi_taskq_wait(vd->completionq);
370 
371 
372 	if ((vd->initialized & VD_DRING) &&
373 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
374 		PRN("ldc_mem_dring_unmap() returned errno %d", status);
375 
376 	if (vd->dring_task != NULL) {
377 		ASSERT(vd->dring_len != 0);
378 		kmem_free(vd->dring_task,
379 		    (sizeof (*vd->dring_task)) * vd->dring_len);
380 		vd->dring_task = NULL;
381 	}
382 
383 
384 	mutex_enter(&vd->lock);
385 	if (vd->reset_ldc && ((status = ldc_reset(vd->ldc_handle)) != 0))
386 		PRN("ldc_reset() returned errno %d", status);
387 
388 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
389 	vd->state	= VD_STATE_INIT;
390 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
391 
392 	vd->reset_state	= B_FALSE;
393 	vd->reset_ldc	= B_FALSE;
394 	mutex_exit(&vd->lock);
395 }
396 
397 static int
398 vd_mark_elem_done(vd_t *vd, int idx, int elem_status)
399 {
400 	boolean_t		accepted;
401 	int			status;
402 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
403 
404 
405 	/* Acquire the element */
406 	if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
407 		PRN("ldc_mem_dring_acquire() returned errno %d", status);
408 		return (status);
409 	}
410 
411 	/* Set the element's status and mark it done */
412 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
413 	if (accepted) {
414 		elem->payload.status	= elem_status;
415 		elem->hdr.dstate	= VIO_DESC_DONE;
416 	} else {
417 		/* Perhaps client timed out waiting for I/O... */
418 		PRN("element %u no longer \"accepted\"", idx);
419 		VD_DUMP_DRING_ELEM(elem);
420 	}
421 	/* Release the element */
422 	if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
423 		PRN("ldc_mem_dring_release() returned errno %d", status);
424 		return (status);
425 	}
426 
427 	return (accepted ? 0 : EINVAL);
428 }
429 
430 static void
431 vd_complete_bio(void *arg)
432 {
433 	int			status		= 0;
434 	vd_task_t		*task		= (vd_task_t *)arg;
435 	vd_t			*vd		= task->vd;
436 	vd_dring_payload_t	*request	= task->request;
437 	struct buf		*buf		= &task->buf;
438 
439 
440 	ASSERT(vd != NULL);
441 	ASSERT(request != NULL);
442 	ASSERT(task->msg != NULL);
443 	ASSERT(task->msglen >= sizeof (*task->msg));
444 	ASSERT(task->msgsize >= task->msglen);
445 
446 	/* Wait for the I/O to complete */
447 	request->status = biowait(buf);
448 
449 	/* If data was read, copy it to the client */
450 	if ((request->status == 0) && (request->operation == VD_OP_BREAD) &&
451 	    ((status = ldc_mem_copy(vd->ldc_handle, buf->b_un.b_addr, 0,
452 		    &request->nbytes, request->cookie, request->ncookies,
453 		    LDC_COPY_OUT)) != 0)) {
454 		PRN("ldc_mem_copy() returned errno %d copying to client",
455 		    status);
456 	}
457 
458 	/* Release I/O buffer */
459 	kmem_free(buf->b_un.b_addr, buf->b_bcount);
460 	biofini(buf);
461 
462 	/* Update the dring element for a dring client */
463 	if ((status == 0) && (vd->xfer_mode == VIO_DRING_MODE))
464 		status = vd_mark_elem_done(vd, task->index, request->status);
465 
466 	/*
467 	 * If a transport error occurred, arrange to "nack" the message when
468 	 * the final task in the descriptor element range completes
469 	 */
470 	if (status != 0)
471 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
472 
473 	/*
474 	 * Only the final task for a range of elements will respond to and
475 	 * free the message
476 	 */
477 	if (task->type == VD_NONFINAL_RANGE_TASK)
478 		return;
479 
480 	/*
481 	 * Send the "ack" or "nack" back to the client; if sending the message
482 	 * via LDC fails, arrange to reset both the connection state and LDC
483 	 * itself
484 	 */
485 	PR1("Sending %s",
486 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
487 	if (send_msg(vd->ldc_handle, task->msg, task->msglen) != 0)
488 		vd_need_reset(vd, B_TRUE);
489 
490 	/* Free the message now that it has been used for the reply */
491 	kmem_free(task->msg, task->msgsize);
492 }
493 
494 static void
495 vd_geom2dk_geom(void *vd_buf, void *ioctl_arg)
496 {
497 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
498 }
499 
500 static void
501 vd_vtoc2vtoc(void *vd_buf, void *ioctl_arg)
502 {
503 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
504 }
505 
506 static void
507 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
508 {
509 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
510 }
511 
512 static void
513 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
514 {
515 	VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
516 }
517 
518 static int
519 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
520 {
521 	switch (cmd) {
522 	case DKIOCGGEOM:
523 		ASSERT(ioctl_arg != NULL);
524 		bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
525 		return (0);
526 	case DKIOCGVTOC:
527 		ASSERT(ioctl_arg != NULL);
528 		bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
529 		return (0);
530 	default:
531 		return (ENOTSUP);
532 	}
533 }
534 
535 static int
536 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
537 {
538 	int	rval = 0, status;
539 	size_t	nbytes = request->nbytes;	/* modifiable copy */
540 
541 
542 	ASSERT(request->slice < vd->nslices);
543 	PR0("Performing %s", ioctl->operation_name);
544 
545 	/* Get data from client and convert, if necessary */
546 	if (ioctl->copyin != NULL)  {
547 		ASSERT(nbytes != 0 && buf != NULL);
548 		PR1("Getting \"arg\" data from client");
549 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
550 			    request->cookie, request->ncookies,
551 			    LDC_COPY_IN)) != 0) {
552 			PRN("ldc_mem_copy() returned errno %d "
553 			    "copying from client", status);
554 			return (status);
555 		}
556 
557 		/* Convert client's data, if necessary */
558 		if (ioctl->copyin == VD_IDENTITY)	/* use client buffer */
559 			ioctl->arg = buf;
560 		else	/* convert client vdisk operation data to ioctl data */
561 			(ioctl->copyin)(buf, (void *)ioctl->arg);
562 	}
563 
564 	/*
565 	 * Handle single-slice block devices internally; otherwise, have the
566 	 * real driver perform the ioctl()
567 	 */
568 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE && !vd->pseudo) {
569 		if ((status = vd_do_slice_ioctl(vd, ioctl->cmd,
570 			    (void *)ioctl->arg)) != 0)
571 			return (status);
572 	} else if ((status = ldi_ioctl(vd->ldi_handle[request->slice],
573 		    ioctl->cmd, (intptr_t)ioctl->arg, (vd_open_flags | FKIOCTL),
574 		    kcred, &rval)) != 0) {
575 		PR0("ldi_ioctl(%s) = errno %d", ioctl->cmd_name, status);
576 		return (status);
577 	}
578 #ifdef DEBUG
579 	if (rval != 0) {
580 		PRN("%s set rval = %d, which is not being returned to client",
581 		    ioctl->cmd_name, rval);
582 	}
583 #endif /* DEBUG */
584 
585 	/* Convert data and send to client, if necessary */
586 	if (ioctl->copyout != NULL)  {
587 		ASSERT(nbytes != 0 && buf != NULL);
588 		PR1("Sending \"arg\" data to client");
589 
590 		/* Convert ioctl data to vdisk operation data, if necessary */
591 		if (ioctl->copyout != VD_IDENTITY)
592 			(ioctl->copyout)((void *)ioctl->arg, buf);
593 
594 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
595 			    request->cookie, request->ncookies,
596 			    LDC_COPY_OUT)) != 0) {
597 			PRN("ldc_mem_copy() returned errno %d "
598 			    "copying to client", status);
599 			return (status);
600 		}
601 	}
602 
603 	return (status);
604 }
605 
606 /*
607  * Open any slices which have become non-empty as a result of performing a
608  * set-VTOC operation for the client.
609  *
610  * When serving a full disk, vds attempts to exclusively open all of the
611  * disk's slices to prevent another thread or process in the service domain
612  * from "stealing" a slice or from performing I/O to a slice while a vds
613  * client is accessing it.  Unfortunately, underlying drivers, such as sd(7d)
614  * and cmdk(7d), return an error when attempting to open the device file for a
615  * slice which is currently empty according to the VTOC.  This driver behavior
616  * means that vds must skip opening empty slices when initializing a vdisk for
617  * full-disk service and try to open slices that become non-empty (via a
618  * set-VTOC operation) during use of the full disk in order to begin serving
619  * such slices to the client.  This approach has an inherent (and therefore
620  * unavoidable) race condition; it also means that failure to open a
621  * newly-non-empty slice has different semantics than failure to open an
622  * initially-non-empty slice:  Due to driver bahavior, opening a
623  * newly-non-empty slice is a necessary side effect of vds performing a
624  * (successful) set-VTOC operation for a client on an in-service (and in-use)
625  * disk in order to begin serving the slice; failure of this side-effect
626  * operation does not mean that the client's set-VTOC operation failed or that
627  * operations on other slices must fail.  Therefore, this function prints an
628  * error message on failure to open a slice, but does not return an error to
629  * its caller--unlike failure to open a slice initially, which results in an
630  * error that prevents serving the vdisk (and thereby requires an
631  * administrator to resolve the problem).  Note that, apart from another
632  * thread or process opening a new slice during the race-condition window,
633  * failure to open a slice in this function will likely indicate an underlying
634  * drive problem, which will also likely become evident in errors returned by
635  * operations on other slices, and which will require administrative
636  * intervention and possibly servicing the drive.
637  */
638 static void
639 vd_open_new_slices(vd_t *vd)
640 {
641 	int		rval, status;
642 	struct vtoc	vtoc;
643 
644 
645 	/* Get the (new) VTOC for updated slice sizes */
646 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC, (intptr_t)&vtoc,
647 		    (vd_open_flags | FKIOCTL), kcred, &rval)) != 0) {
648 		PRN("ldi_ioctl(DKIOCGVTOC) returned errno %d", status);
649 		return;
650 	}
651 
652 	/* Open any newly-non-empty slices */
653 	for (int slice = 0; slice < vd->nslices; slice++) {
654 		/* Skip zero-length slices */
655 		if (vtoc.v_part[slice].p_size == 0) {
656 			if (vd->ldi_handle[slice] != NULL)
657 				PR0("Open slice %u now has zero length", slice);
658 			continue;
659 		}
660 
661 		/* Skip already-open slices */
662 		if (vd->ldi_handle[slice] != NULL)
663 			continue;
664 
665 		PR0("Opening newly-non-empty slice %u", slice);
666 		if ((status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
667 			    vd_open_flags, kcred, &vd->ldi_handle[slice],
668 			    vd->vds->ldi_ident)) != 0) {
669 			PRN("ldi_open_by_dev() returned errno %d "
670 			    "for slice %u", status, slice);
671 		}
672 	}
673 }
674 
675 #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
676 static int
677 vd_ioctl(vd_task_t *task)
678 {
679 	int			i, status;
680 	void			*buf = NULL;
681 	struct dk_geom		dk_geom = {0};
682 	struct vtoc		vtoc = {0};
683 	vd_t			*vd		= task->vd;
684 	vd_dring_payload_t	*request	= task->request;
685 	vd_ioctl_t		ioctl[] = {
686 		/* Command (no-copy) operations */
687 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
688 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
689 		    NULL, NULL, NULL},
690 
691 		/* "Get" (copy-out) operations */
692 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
693 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
694 		    NULL, NULL, VD_IDENTITY},
695 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
696 		    RNDSIZE(vd_geom_t),
697 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
698 		    &dk_geom, NULL, dk_geom2vd_geom},
699 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
700 		    DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
701 		    &vtoc, NULL, vtoc2vd_vtoc},
702 
703 		/* "Set" (copy-in) operations */
704 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
705 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
706 		    NULL, VD_IDENTITY, NULL},
707 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
708 		    RNDSIZE(vd_geom_t),
709 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
710 		    &dk_geom, vd_geom2dk_geom, NULL},
711 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
712 		    DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
713 		    &vtoc, vd_vtoc2vtoc, NULL},
714 	};
715 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
716 
717 
718 	ASSERT(vd != NULL);
719 	ASSERT(request != NULL);
720 	ASSERT(request->slice < vd->nslices);
721 
722 	/*
723 	 * Determine ioctl corresponding to caller's "operation" and
724 	 * validate caller's "nbytes"
725 	 */
726 	for (i = 0; i < nioctls; i++) {
727 		if (request->operation == ioctl[i].operation) {
728 			/* LDC memory operations require 8-byte multiples */
729 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
730 
731 			if (request->nbytes != ioctl[i].nbytes) {
732 				PRN("%s:  Expected nbytes = %lu, got %lu",
733 				    ioctl[i].operation_name, ioctl[i].nbytes,
734 				    request->nbytes);
735 				return (EINVAL);
736 			}
737 
738 			break;
739 		}
740 	}
741 	ASSERT(i < nioctls);	/* because "operation" already validated */
742 
743 	if (request->nbytes)
744 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
745 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
746 	if (request->nbytes)
747 		kmem_free(buf, request->nbytes);
748 	if ((request->operation == VD_OP_SET_VTOC) &&
749 	    (vd->vdisk_type == VD_DISK_TYPE_DISK))
750 		vd_open_new_slices(vd);
751 	PR0("Returning %d", status);
752 	return (status);
753 }
754 
755 /*
756  * Define the supported operations once the functions for performing them have
757  * been defined
758  */
759 static const vds_operation_t	vds_operation[] = {
760 	{VD_OP_BREAD,		vd_start_bio,	vd_complete_bio},
761 	{VD_OP_BWRITE,		vd_start_bio,	vd_complete_bio},
762 	{VD_OP_FLUSH,		vd_ioctl,	NULL},
763 	{VD_OP_GET_WCE,		vd_ioctl,	NULL},
764 	{VD_OP_SET_WCE,		vd_ioctl,	NULL},
765 	{VD_OP_GET_VTOC,	vd_ioctl,	NULL},
766 	{VD_OP_SET_VTOC,	vd_ioctl,	NULL},
767 	{VD_OP_GET_DISKGEOM,	vd_ioctl,	NULL},
768 	{VD_OP_SET_DISKGEOM,	vd_ioctl,	NULL}
769 };
770 
771 static const size_t	vds_noperations =
772 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
773 
774 /*
775  * Process a task specifying a client I/O request
776  */
777 static int
778 vd_process_task(vd_task_t *task)
779 {
780 	int			i, status;
781 	vd_t			*vd		= task->vd;
782 	vd_dring_payload_t	*request	= task->request;
783 
784 
785 	ASSERT(vd != NULL);
786 	ASSERT(request != NULL);
787 
788 	/* Range-check slice */
789 	if (request->slice >= vd->nslices) {
790 		PRN("Invalid \"slice\" %u (max %u) for virtual disk",
791 		    request->slice, (vd->nslices - 1));
792 		return (EINVAL);
793 	}
794 
795 	/* Find the requested operation */
796 	for (i = 0; i < vds_noperations; i++)
797 		if (request->operation == vds_operation[i].operation)
798 			break;
799 	if (i == vds_noperations) {
800 		PRN("Unsupported operation %u", request->operation);
801 		return (ENOTSUP);
802 	}
803 
804 	/* Start the operation */
805 	if ((status = vds_operation[i].start(task)) != EINPROGRESS) {
806 		request->status = status;	/* op succeeded or failed */
807 		return (0);			/* but request completed */
808 	}
809 
810 	ASSERT(vds_operation[i].complete != NULL);	/* debug case */
811 	if (vds_operation[i].complete == NULL) {	/* non-debug case */
812 		PRN("Unexpected return of EINPROGRESS "
813 		    "with no I/O completion handler");
814 		request->status = EIO;	/* operation failed */
815 		return (0);		/* but request completed */
816 	}
817 
818 	/* Queue a task to complete the operation */
819 	status = ddi_taskq_dispatch(vd->completionq, vds_operation[i].complete,
820 	    task, DDI_SLEEP);
821 	/* ddi_taskq_dispatch(9f) guarantees success with DDI_SLEEP */
822 	ASSERT(status == DDI_SUCCESS);
823 
824 	PR1("Operation in progress");
825 	return (EINPROGRESS);	/* completion handler will finish request */
826 }
827 
828 /*
829  * Return true if the "type", "subtype", and "env" fields of the "tag" first
830  * argument match the corresponding remaining arguments; otherwise, return false
831  */
832 boolean_t
833 vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
834 {
835 	return ((tag->vio_msgtype == type) &&
836 		(tag->vio_subtype == subtype) &&
837 		(tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
838 }
839 
840 /*
841  * Check whether the major/minor version specified in "ver_msg" is supported
842  * by this server.
843  */
844 static boolean_t
845 vds_supported_version(vio_ver_msg_t *ver_msg)
846 {
847 	for (int i = 0; i < vds_num_versions; i++) {
848 		ASSERT(vds_version[i].major > 0);
849 		ASSERT((i == 0) ||
850 		    (vds_version[i].major < vds_version[i-1].major));
851 
852 		/*
853 		 * If the major versions match, adjust the minor version, if
854 		 * necessary, down to the highest value supported by this
855 		 * server and return true so this message will get "ack"ed;
856 		 * the client should also support all minor versions lower
857 		 * than the value it sent
858 		 */
859 		if (ver_msg->ver_major == vds_version[i].major) {
860 			if (ver_msg->ver_minor > vds_version[i].minor) {
861 				PR0("Adjusting minor version from %u to %u",
862 				    ver_msg->ver_minor, vds_version[i].minor);
863 				ver_msg->ver_minor = vds_version[i].minor;
864 			}
865 			return (B_TRUE);
866 		}
867 
868 		/*
869 		 * If the message contains a higher major version number, set
870 		 * the message's major/minor versions to the current values
871 		 * and return false, so this message will get "nack"ed with
872 		 * these values, and the client will potentially try again
873 		 * with the same or a lower version
874 		 */
875 		if (ver_msg->ver_major > vds_version[i].major) {
876 			ver_msg->ver_major = vds_version[i].major;
877 			ver_msg->ver_minor = vds_version[i].minor;
878 			return (B_FALSE);
879 		}
880 
881 		/*
882 		 * Otherwise, the message's major version is less than the
883 		 * current major version, so continue the loop to the next
884 		 * (lower) supported version
885 		 */
886 	}
887 
888 	/*
889 	 * No common version was found; "ground" the version pair in the
890 	 * message to terminate negotiation
891 	 */
892 	ver_msg->ver_major = 0;
893 	ver_msg->ver_minor = 0;
894 	return (B_FALSE);
895 }
896 
897 /*
898  * Process a version message from a client.  vds expects to receive version
899  * messages from clients seeking service, but never issues version messages
900  * itself; therefore, vds can ACK or NACK client version messages, but does
901  * not expect to receive version-message ACKs or NACKs (and will treat such
902  * messages as invalid).
903  */
904 static int
905 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
906 {
907 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
908 
909 
910 	ASSERT(msglen >= sizeof (msg->tag));
911 
912 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
913 		VIO_VER_INFO)) {
914 		return (ENOMSG);	/* not a version message */
915 	}
916 
917 	if (msglen != sizeof (*ver_msg)) {
918 		PRN("Expected %lu-byte version message; "
919 		    "received %lu bytes", sizeof (*ver_msg), msglen);
920 		return (EBADMSG);
921 	}
922 
923 	if (ver_msg->dev_class != VDEV_DISK) {
924 		PRN("Expected device class %u (disk); received %u",
925 		    VDEV_DISK, ver_msg->dev_class);
926 		return (EBADMSG);
927 	}
928 
929 	/*
930 	 * We're talking to the expected kind of client; set our device class
931 	 * for "ack/nack" back to the client
932 	 */
933 	ver_msg->dev_class = VDEV_DISK_SERVER;
934 
935 	/*
936 	 * Check whether the (valid) version message specifies a version
937 	 * supported by this server.  If the version is not supported, return
938 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
939 	 * will have updated the message with a supported version for the
940 	 * client to consider
941 	 */
942 	if (!vds_supported_version(ver_msg))
943 		return (EBADMSG);
944 
945 
946 	/*
947 	 * A version has been agreed upon; use the client's SID for
948 	 * communication on this channel now
949 	 */
950 	ASSERT(!(vd->initialized & VD_SID));
951 	vd->sid = ver_msg->tag.vio_sid;
952 	vd->initialized |= VD_SID;
953 
954 	/*
955 	 * When multiple versions are supported, this function should store
956 	 * the negotiated major and minor version values in the "vd" data
957 	 * structure to govern further communication; in particular, note that
958 	 * the client might have specified a lower minor version for the
959 	 * agreed major version than specifed in the vds_version[] array.  The
960 	 * following assertions should help remind future maintainers to make
961 	 * the appropriate changes to support multiple versions.
962 	 */
963 	ASSERT(vds_num_versions == 1);
964 	ASSERT(ver_msg->ver_major == vds_version[0].major);
965 	ASSERT(ver_msg->ver_minor == vds_version[0].minor);
966 
967 	PR0("Using major version %u, minor version %u",
968 	    ver_msg->ver_major, ver_msg->ver_minor);
969 	return (0);
970 }
971 
972 static int
973 vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
974 {
975 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
976 
977 
978 	ASSERT(msglen >= sizeof (msg->tag));
979 
980 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
981 		VIO_ATTR_INFO)) {
982 		PR0("Message is not an attribute message");
983 		return (ENOMSG);
984 	}
985 
986 	if (msglen != sizeof (*attr_msg)) {
987 		PRN("Expected %lu-byte attribute message; "
988 		    "received %lu bytes", sizeof (*attr_msg), msglen);
989 		return (EBADMSG);
990 	}
991 
992 	if (attr_msg->max_xfer_sz == 0) {
993 		PRN("Received maximum transfer size of 0 from client");
994 		return (EBADMSG);
995 	}
996 
997 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
998 	    (attr_msg->xfer_mode != VIO_DRING_MODE)) {
999 		PRN("Client requested unsupported transfer mode");
1000 		return (EBADMSG);
1001 	}
1002 
1003 
1004 	/* Success:  valid message and transfer mode */
1005 	vd->xfer_mode = attr_msg->xfer_mode;
1006 	if (vd->xfer_mode == VIO_DESC_MODE) {
1007 		/*
1008 		 * The vd_dring_inband_msg_t contains one cookie; need room
1009 		 * for up to n-1 more cookies, where "n" is the number of full
1010 		 * pages plus possibly one partial page required to cover
1011 		 * "max_xfer_sz".  Add room for one more cookie if
1012 		 * "max_xfer_sz" isn't an integral multiple of the page size.
1013 		 * Must first get the maximum transfer size in bytes.
1014 		 */
1015 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
1016 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
1017 		    attr_msg->max_xfer_sz;
1018 		size_t	max_inband_msglen =
1019 		    sizeof (vd_dring_inband_msg_t) +
1020 		    ((max_xfer_bytes/PAGESIZE +
1021 			((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
1022 			(sizeof (ldc_mem_cookie_t)));
1023 
1024 		/*
1025 		 * Set the maximum expected message length to
1026 		 * accommodate in-band-descriptor messages with all
1027 		 * their cookies
1028 		 */
1029 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
1030 
1031 		/*
1032 		 * Initialize the data structure for processing in-band I/O
1033 		 * request descriptors
1034 		 */
1035 		vd->inband_task.vd	= vd;
1036 		vd->inband_task.index	= 0;
1037 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
1038 	}
1039 
1040 	attr_msg->vdisk_size = vd->vdisk_size;
1041 	attr_msg->vdisk_type = vd->vdisk_type;
1042 	attr_msg->operations = vds_operations;
1043 	PR0("%s", VD_CLIENT(vd));
1044 	return (0);
1045 }
1046 
1047 static int
1048 vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
1049 {
1050 	int			status;
1051 	size_t			expected;
1052 	ldc_mem_info_t		dring_minfo;
1053 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
1054 
1055 
1056 	ASSERT(msglen >= sizeof (msg->tag));
1057 
1058 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
1059 		VIO_DRING_REG)) {
1060 		PR0("Message is not a register-dring message");
1061 		return (ENOMSG);
1062 	}
1063 
1064 	if (msglen < sizeof (*reg_msg)) {
1065 		PRN("Expected at least %lu-byte register-dring message; "
1066 		    "received %lu bytes", sizeof (*reg_msg), msglen);
1067 		return (EBADMSG);
1068 	}
1069 
1070 	expected = sizeof (*reg_msg) +
1071 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
1072 	if (msglen != expected) {
1073 		PRN("Expected %lu-byte register-dring message; "
1074 		    "received %lu bytes", expected, msglen);
1075 		return (EBADMSG);
1076 	}
1077 
1078 	if (vd->initialized & VD_DRING) {
1079 		PRN("A dring was previously registered; only support one");
1080 		return (EBADMSG);
1081 	}
1082 
1083 	if (reg_msg->num_descriptors > INT32_MAX) {
1084 		PRN("reg_msg->num_descriptors = %u; must be <= %u (%s)",
1085 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
1086 		return (EBADMSG);
1087 	}
1088 
1089 	if (reg_msg->ncookies != 1) {
1090 		/*
1091 		 * In addition to fixing the assertion in the success case
1092 		 * below, supporting drings which require more than one
1093 		 * "cookie" requires increasing the value of vd->max_msglen
1094 		 * somewhere in the code path prior to receiving the message
1095 		 * which results in calling this function.  Note that without
1096 		 * making this change, the larger message size required to
1097 		 * accommodate multiple cookies cannot be successfully
1098 		 * received, so this function will not even get called.
1099 		 * Gracefully accommodating more dring cookies might
1100 		 * reasonably demand exchanging an additional attribute or
1101 		 * making a minor protocol adjustment
1102 		 */
1103 		PRN("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
1104 		return (EBADMSG);
1105 	}
1106 
1107 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
1108 	    reg_msg->ncookies, reg_msg->num_descriptors,
1109 	    reg_msg->descriptor_size, LDC_SHADOW_MAP, &vd->dring_handle);
1110 	if (status != 0) {
1111 		PRN("ldc_mem_dring_map() returned errno %d", status);
1112 		return (status);
1113 	}
1114 
1115 	/*
1116 	 * To remove the need for this assertion, must call
1117 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
1118 	 * successful call to ldc_mem_dring_map()
1119 	 */
1120 	ASSERT(reg_msg->ncookies == 1);
1121 
1122 	if ((status =
1123 		ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
1124 		PRN("ldc_mem_dring_info() returned errno %d", status);
1125 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
1126 			PRN("ldc_mem_dring_unmap() returned errno %d", status);
1127 		return (status);
1128 	}
1129 
1130 	if (dring_minfo.vaddr == NULL) {
1131 		PRN("Descriptor ring virtual address is NULL");
1132 		return (ENXIO);
1133 	}
1134 
1135 
1136 	/* Initialize for valid message and mapped dring */
1137 	PR1("descriptor size = %u, dring length = %u",
1138 	    vd->descriptor_size, vd->dring_len);
1139 	vd->initialized |= VD_DRING;
1140 	vd->dring_ident = 1;	/* "There Can Be Only One" */
1141 	vd->dring = dring_minfo.vaddr;
1142 	vd->descriptor_size = reg_msg->descriptor_size;
1143 	vd->dring_len = reg_msg->num_descriptors;
1144 	reg_msg->dring_ident = vd->dring_ident;
1145 
1146 	/*
1147 	 * Allocate and initialize a "shadow" array of data structures for
1148 	 * tasks to process I/O requests in dring elements
1149 	 */
1150 	vd->dring_task =
1151 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
1152 	for (int i = 0; i < vd->dring_len; i++) {
1153 		vd->dring_task[i].vd		= vd;
1154 		vd->dring_task[i].index		= i;
1155 		vd->dring_task[i].request	= &VD_DRING_ELEM(i)->payload;
1156 	}
1157 
1158 	return (0);
1159 }
1160 
1161 static int
1162 vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
1163 {
1164 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
1165 
1166 
1167 	ASSERT(msglen >= sizeof (msg->tag));
1168 
1169 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
1170 		VIO_DRING_UNREG)) {
1171 		PR0("Message is not an unregister-dring message");
1172 		return (ENOMSG);
1173 	}
1174 
1175 	if (msglen != sizeof (*unreg_msg)) {
1176 		PRN("Expected %lu-byte unregister-dring message; "
1177 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
1178 		return (EBADMSG);
1179 	}
1180 
1181 	if (unreg_msg->dring_ident != vd->dring_ident) {
1182 		PRN("Expected dring ident %lu; received %lu",
1183 		    vd->dring_ident, unreg_msg->dring_ident);
1184 		return (EBADMSG);
1185 	}
1186 
1187 	return (0);
1188 }
1189 
1190 static int
1191 process_rdx_msg(vio_msg_t *msg, size_t msglen)
1192 {
1193 	ASSERT(msglen >= sizeof (msg->tag));
1194 
1195 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
1196 		PR0("Message is not an RDX message");
1197 		return (ENOMSG);
1198 	}
1199 
1200 	if (msglen != sizeof (vio_rdx_msg_t)) {
1201 		PRN("Expected %lu-byte RDX message; received %lu bytes",
1202 		    sizeof (vio_rdx_msg_t), msglen);
1203 		return (EBADMSG);
1204 	}
1205 
1206 	PR0("Valid RDX message");
1207 	return (0);
1208 }
1209 
1210 static int
1211 vd_check_seq_num(vd_t *vd, uint64_t seq_num)
1212 {
1213 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
1214 		PRN("Received seq_num %lu; expected %lu",
1215 		    seq_num, (vd->seq_num + 1));
1216 		vd_need_reset(vd, B_FALSE);
1217 		return (1);
1218 	}
1219 
1220 	vd->seq_num = seq_num;
1221 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
1222 	return (0);
1223 }
1224 
1225 /*
1226  * Return the expected size of an inband-descriptor message with all the
1227  * cookies it claims to include
1228  */
1229 static size_t
1230 expected_inband_size(vd_dring_inband_msg_t *msg)
1231 {
1232 	return ((sizeof (*msg)) +
1233 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
1234 }
1235 
1236 /*
1237  * Process an in-band descriptor message:  used with clients like OBP, with
1238  * which vds exchanges descriptors within VIO message payloads, rather than
1239  * operating on them within a descriptor ring
1240  */
1241 static int
1242 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen, size_t msgsize)
1243 {
1244 	size_t			expected;
1245 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
1246 
1247 
1248 	ASSERT(msglen >= sizeof (msg->tag));
1249 
1250 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
1251 		VIO_DESC_DATA)) {
1252 		PR1("Message is not an in-band-descriptor message");
1253 		return (ENOMSG);
1254 	}
1255 
1256 	if (msglen < sizeof (*desc_msg)) {
1257 		PRN("Expected at least %lu-byte descriptor message; "
1258 		    "received %lu bytes", sizeof (*desc_msg), msglen);
1259 		return (EBADMSG);
1260 	}
1261 
1262 	if (msglen != (expected = expected_inband_size(desc_msg))) {
1263 		PRN("Expected %lu-byte descriptor message; "
1264 		    "received %lu bytes", expected, msglen);
1265 		return (EBADMSG);
1266 	}
1267 
1268 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
1269 		return (EBADMSG);
1270 
1271 	/*
1272 	 * Valid message:  Set up the in-band descriptor task and process the
1273 	 * request.  Arrange to acknowledge the client's message, unless an
1274 	 * error processing the descriptor task results in setting
1275 	 * VIO_SUBTYPE_NACK
1276 	 */
1277 	PR1("Valid in-band-descriptor message");
1278 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
1279 	vd->inband_task.msg	= msg;
1280 	vd->inband_task.msglen	= msglen;
1281 	vd->inband_task.msgsize	= msgsize;
1282 	vd->inband_task.request	= &desc_msg->payload;
1283 	return (vd_process_task(&vd->inband_task));
1284 }
1285 
1286 static int
1287 vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
1288     vio_msg_t *msg, size_t msglen, size_t msgsize)
1289 {
1290 	int			status;
1291 	boolean_t		ready;
1292 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
1293 
1294 
1295 	/* Accept the updated dring element */
1296 	if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
1297 		PRN("ldc_mem_dring_acquire() returned errno %d", status);
1298 		return (status);
1299 	}
1300 	ready = (elem->hdr.dstate == VIO_DESC_READY);
1301 	if (ready) {
1302 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
1303 	} else {
1304 		PRN("descriptor %u not ready", idx);
1305 		VD_DUMP_DRING_ELEM(elem);
1306 	}
1307 	if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
1308 		PRN("ldc_mem_dring_release() returned errno %d", status);
1309 		return (status);
1310 	}
1311 	if (!ready)
1312 		return (EBUSY);
1313 
1314 
1315 	/* Initialize a task and process the accepted element */
1316 	PR1("Processing dring element %u", idx);
1317 	vd->dring_task[idx].type	= type;
1318 	vd->dring_task[idx].msg		= msg;
1319 	vd->dring_task[idx].msglen	= msglen;
1320 	vd->dring_task[idx].msgsize	= msgsize;
1321 	if ((status = vd_process_task(&vd->dring_task[idx])) != EINPROGRESS)
1322 		status = vd_mark_elem_done(vd, idx, elem->payload.status);
1323 
1324 	return (status);
1325 }
1326 
1327 static int
1328 vd_process_element_range(vd_t *vd, int start, int end,
1329     vio_msg_t *msg, size_t msglen, size_t msgsize)
1330 {
1331 	int		i, n, nelem, status = 0;
1332 	boolean_t	inprogress = B_FALSE;
1333 	vd_task_type_t	type;
1334 
1335 
1336 	ASSERT(start >= 0);
1337 	ASSERT(end >= 0);
1338 
1339 	/*
1340 	 * Arrange to acknowledge the client's message, unless an error
1341 	 * processing one of the dring elements results in setting
1342 	 * VIO_SUBTYPE_NACK
1343 	 */
1344 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
1345 
1346 	/*
1347 	 * Process the dring elements in the range
1348 	 */
1349 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
1350 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
1351 		((vio_dring_msg_t *)msg)->end_idx = i;
1352 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
1353 		status = vd_process_element(vd, type, i, msg, msglen, msgsize);
1354 		if (status == EINPROGRESS)
1355 			inprogress = B_TRUE;
1356 		else if (status != 0)
1357 			break;
1358 	}
1359 
1360 	/*
1361 	 * If some, but not all, operations of a multi-element range are in
1362 	 * progress, wait for other operations to complete before returning
1363 	 * (which will result in "ack" or "nack" of the message).  Note that
1364 	 * all outstanding operations will need to complete, not just the ones
1365 	 * corresponding to the current range of dring elements; howevever, as
1366 	 * this situation is an error case, performance is less critical.
1367 	 */
1368 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
1369 		ddi_taskq_wait(vd->completionq);
1370 
1371 	return (status);
1372 }
1373 
1374 static int
1375 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen, size_t msgsize)
1376 {
1377 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
1378 
1379 
1380 	ASSERT(msglen >= sizeof (msg->tag));
1381 
1382 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
1383 		VIO_DRING_DATA)) {
1384 		PR1("Message is not a dring-data message");
1385 		return (ENOMSG);
1386 	}
1387 
1388 	if (msglen != sizeof (*dring_msg)) {
1389 		PRN("Expected %lu-byte dring message; received %lu bytes",
1390 		    sizeof (*dring_msg), msglen);
1391 		return (EBADMSG);
1392 	}
1393 
1394 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
1395 		return (EBADMSG);
1396 
1397 	if (dring_msg->dring_ident != vd->dring_ident) {
1398 		PRN("Expected dring ident %lu; received ident %lu",
1399 		    vd->dring_ident, dring_msg->dring_ident);
1400 		return (EBADMSG);
1401 	}
1402 
1403 	if (dring_msg->start_idx >= vd->dring_len) {
1404 		PRN("\"start_idx\" = %u; must be less than %u",
1405 		    dring_msg->start_idx, vd->dring_len);
1406 		return (EBADMSG);
1407 	}
1408 
1409 	if ((dring_msg->end_idx < 0) ||
1410 	    (dring_msg->end_idx >= vd->dring_len)) {
1411 		PRN("\"end_idx\" = %u; must be >= 0 and less than %u",
1412 		    dring_msg->end_idx, vd->dring_len);
1413 		return (EBADMSG);
1414 	}
1415 
1416 	/* Valid message; process range of updated dring elements */
1417 	PR1("Processing descriptor range, start = %u, end = %u",
1418 	    dring_msg->start_idx, dring_msg->end_idx);
1419 	return (vd_process_element_range(vd, dring_msg->start_idx,
1420 		dring_msg->end_idx, msg, msglen, msgsize));
1421 }
1422 
1423 static int
1424 recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
1425 {
1426 	int	retry, status;
1427 	size_t	size = *nbytes;
1428 
1429 
1430 	for (retry = 0, status = ETIMEDOUT;
1431 	    retry < vds_ldc_retries && status == ETIMEDOUT;
1432 	    retry++) {
1433 		PR1("ldc_read() attempt %d", (retry + 1));
1434 		*nbytes = size;
1435 		status = ldc_read(ldc_handle, msg, nbytes);
1436 	}
1437 
1438 	if (status != 0) {
1439 		PRN("ldc_read() returned errno %d", status);
1440 		return (status);
1441 	} else if (*nbytes == 0) {
1442 		PR1("ldc_read() returned 0 and no message read");
1443 		return (ENOMSG);
1444 	}
1445 
1446 	PR1("RCVD %lu-byte message", *nbytes);
1447 	return (0);
1448 }
1449 
1450 static int
1451 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen, size_t msgsize)
1452 {
1453 	int		status;
1454 
1455 
1456 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
1457 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
1458 
1459 	/*
1460 	 * Validate session ID up front, since it applies to all messages
1461 	 * once set
1462 	 */
1463 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
1464 		PRN("Expected SID %u, received %u", vd->sid,
1465 		    msg->tag.vio_sid);
1466 		return (EBADMSG);
1467 	}
1468 
1469 
1470 	/*
1471 	 * Process the received message based on connection state
1472 	 */
1473 	switch (vd->state) {
1474 	case VD_STATE_INIT:	/* expect version message */
1475 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
1476 			return (status);
1477 
1478 		/* Version negotiated, move to that state */
1479 		vd->state = VD_STATE_VER;
1480 		return (0);
1481 
1482 	case VD_STATE_VER:	/* expect attribute message */
1483 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
1484 			return (status);
1485 
1486 		/* Attributes exchanged, move to that state */
1487 		vd->state = VD_STATE_ATTR;
1488 		return (0);
1489 
1490 	case VD_STATE_ATTR:
1491 		switch (vd->xfer_mode) {
1492 		case VIO_DESC_MODE:	/* expect RDX message */
1493 			if ((status = process_rdx_msg(msg, msglen)) != 0)
1494 				return (status);
1495 
1496 			/* Ready to receive in-band descriptors */
1497 			vd->state = VD_STATE_DATA;
1498 			return (0);
1499 
1500 		case VIO_DRING_MODE:	/* expect register-dring message */
1501 			if ((status =
1502 				vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
1503 				return (status);
1504 
1505 			/* One dring negotiated, move to that state */
1506 			vd->state = VD_STATE_DRING;
1507 			return (0);
1508 
1509 		default:
1510 			ASSERT("Unsupported transfer mode");
1511 			PRN("Unsupported transfer mode");
1512 			return (ENOTSUP);
1513 		}
1514 
1515 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
1516 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
1517 			/* Ready to receive data */
1518 			vd->state = VD_STATE_DATA;
1519 			return (0);
1520 		} else if (status != ENOMSG) {
1521 			return (status);
1522 		}
1523 
1524 
1525 		/*
1526 		 * If another register-dring message is received, stay in
1527 		 * dring state in case the client sends RDX; although the
1528 		 * protocol allows multiple drings, this server does not
1529 		 * support using more than one
1530 		 */
1531 		if ((status =
1532 			vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
1533 			return (status);
1534 
1535 		/*
1536 		 * Acknowledge an unregister-dring message, but reset the
1537 		 * connection anyway:  Although the protocol allows
1538 		 * unregistering drings, this server cannot serve a vdisk
1539 		 * without its only dring
1540 		 */
1541 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
1542 		return ((status == 0) ? ENOTSUP : status);
1543 
1544 	case VD_STATE_DATA:
1545 		switch (vd->xfer_mode) {
1546 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
1547 			return (vd_process_desc_msg(vd, msg, msglen, msgsize));
1548 
1549 		case VIO_DRING_MODE:	/* expect dring-data or unreg-dring */
1550 			/*
1551 			 * Typically expect dring-data messages, so handle
1552 			 * them first
1553 			 */
1554 			if ((status = vd_process_dring_msg(vd, msg,
1555 				    msglen, msgsize)) != ENOMSG)
1556 				return (status);
1557 
1558 			/*
1559 			 * Acknowledge an unregister-dring message, but reset
1560 			 * the connection anyway:  Although the protocol
1561 			 * allows unregistering drings, this server cannot
1562 			 * serve a vdisk without its only dring
1563 			 */
1564 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
1565 			return ((status == 0) ? ENOTSUP : status);
1566 
1567 		default:
1568 			ASSERT("Unsupported transfer mode");
1569 			PRN("Unsupported transfer mode");
1570 			return (ENOTSUP);
1571 		}
1572 
1573 	default:
1574 		ASSERT("Invalid client connection state");
1575 		PRN("Invalid client connection state");
1576 		return (ENOTSUP);
1577 	}
1578 }
1579 
1580 static int
1581 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen, size_t msgsize)
1582 {
1583 	int		status;
1584 	boolean_t	reset_ldc = B_FALSE;
1585 
1586 
1587 	/*
1588 	 * Check that the message is at least big enough for a "tag", so that
1589 	 * message processing can proceed based on tag-specified message type
1590 	 */
1591 	if (msglen < sizeof (vio_msg_tag_t)) {
1592 		PRN("Received short (%lu-byte) message", msglen);
1593 		/* Can't "nack" short message, so drop the big hammer */
1594 		vd_need_reset(vd, B_TRUE);
1595 		return (EBADMSG);
1596 	}
1597 
1598 	/*
1599 	 * Process the message
1600 	 */
1601 	switch (status = vd_do_process_msg(vd, msg, msglen, msgsize)) {
1602 	case 0:
1603 		/* "ack" valid, successfully-processed messages */
1604 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
1605 		break;
1606 
1607 	case EINPROGRESS:
1608 		/* The completion handler will "ack" or "nack" the message */
1609 		return (EINPROGRESS);
1610 	case ENOMSG:
1611 		PRN("Received unexpected message");
1612 		_NOTE(FALLTHROUGH);
1613 	case EBADMSG:
1614 	case ENOTSUP:
1615 		/* "nack" invalid messages */
1616 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
1617 		break;
1618 
1619 	default:
1620 		/* "nack" failed messages */
1621 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
1622 		/* An LDC error probably occurred, so try resetting it */
1623 		reset_ldc = B_TRUE;
1624 		break;
1625 	}
1626 
1627 	/* Send the "ack" or "nack" to the client */
1628 	PR1("Sending %s",
1629 	    (msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
1630 	if (send_msg(vd->ldc_handle, msg, msglen) != 0)
1631 		reset_ldc = B_TRUE;
1632 
1633 	/* Arrange to reset the connection for nack'ed or failed messages */
1634 	if ((status != 0) || reset_ldc)
1635 		vd_need_reset(vd, reset_ldc);
1636 
1637 	return (status);
1638 }
1639 
1640 static boolean_t
1641 vd_enabled(vd_t *vd)
1642 {
1643 	boolean_t	enabled;
1644 
1645 
1646 	mutex_enter(&vd->lock);
1647 	enabled = vd->enabled;
1648 	mutex_exit(&vd->lock);
1649 	return (enabled);
1650 }
1651 
1652 static void
1653 vd_recv_msg(void *arg)
1654 {
1655 	vd_t	*vd = (vd_t *)arg;
1656 	int	status = 0;
1657 
1658 
1659 	ASSERT(vd != NULL);
1660 	PR2("New task to receive incoming message(s)");
1661 	while (vd_enabled(vd) && status == 0) {
1662 		size_t		msglen, msgsize;
1663 		vio_msg_t	*vio_msg;
1664 
1665 
1666 		/*
1667 		 * Receive and process a message
1668 		 */
1669 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
1670 		msgsize = vd->max_msglen;	/* stable copy for alloc/free */
1671 		msglen	= msgsize;	/* actual length after recv_msg() */
1672 		vio_msg = kmem_alloc(msgsize, KM_SLEEP);
1673 		if ((status = recv_msg(vd->ldc_handle, vio_msg, &msglen)) ==
1674 		    0) {
1675 			if (vd_process_msg(vd, vio_msg, msglen, msgsize) ==
1676 			    EINPROGRESS)
1677 				continue;	/* handler will free msg */
1678 		} else if (status != ENOMSG) {
1679 			/* Probably an LDC failure; arrange to reset it */
1680 			vd_need_reset(vd, B_TRUE);
1681 		}
1682 		kmem_free(vio_msg, msgsize);
1683 	}
1684 	PR2("Task finished");
1685 }
1686 
1687 static uint_t
1688 vd_handle_ldc_events(uint64_t event, caddr_t arg)
1689 {
1690 	vd_t	*vd = (vd_t *)(void *)arg;
1691 
1692 
1693 	ASSERT(vd != NULL);
1694 
1695 	if (!vd_enabled(vd))
1696 		return (LDC_SUCCESS);
1697 
1698 	if (event & LDC_EVT_RESET) {
1699 		PR0("LDC channel was reset");
1700 		return (LDC_SUCCESS);
1701 	}
1702 
1703 	if (event & LDC_EVT_UP) {
1704 		PR0("LDC channel came up:  Resetting client connection state");
1705 		vd_need_reset(vd, B_FALSE);
1706 	}
1707 
1708 	if (event & LDC_EVT_READ) {
1709 		int	status;
1710 
1711 		PR1("New data available");
1712 		/* Queue a task to receive the new data */
1713 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
1714 		    DDI_SLEEP);
1715 		/* ddi_taskq_dispatch(9f) guarantees success with DDI_SLEEP */
1716 		ASSERT(status == DDI_SUCCESS);
1717 	}
1718 
1719 	return (LDC_SUCCESS);
1720 }
1721 
1722 static uint_t
1723 vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
1724 {
1725 	_NOTE(ARGUNUSED(key, val))
1726 	(*((uint_t *)arg))++;
1727 	return (MH_WALK_TERMINATE);
1728 }
1729 
1730 
1731 static int
1732 vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1733 {
1734 	uint_t	vd_present = 0;
1735 	minor_t	instance;
1736 	vds_t	*vds;
1737 
1738 
1739 	switch (cmd) {
1740 	case DDI_DETACH:
1741 		/* the real work happens below */
1742 		break;
1743 	case DDI_SUSPEND:
1744 		PR0("No action required for DDI_SUSPEND");
1745 		return (DDI_SUCCESS);
1746 	default:
1747 		PRN("Unrecognized \"cmd\"");
1748 		return (DDI_FAILURE);
1749 	}
1750 
1751 	ASSERT(cmd == DDI_DETACH);
1752 	instance = ddi_get_instance(dip);
1753 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
1754 		PRN("Could not get state for instance %u", instance);
1755 		ddi_soft_state_free(vds_state, instance);
1756 		return (DDI_FAILURE);
1757 	}
1758 
1759 	/* Do no detach when serving any vdisks */
1760 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
1761 	if (vd_present) {
1762 		PR0("Not detaching because serving vdisks");
1763 		return (DDI_FAILURE);
1764 	}
1765 
1766 	PR0("Detaching");
1767 	if (vds->initialized & VDS_MDEG)
1768 		(void) mdeg_unregister(vds->mdeg);
1769 	if (vds->initialized & VDS_LDI)
1770 		(void) ldi_ident_release(vds->ldi_ident);
1771 	mod_hash_destroy_hash(vds->vd_table);
1772 	ddi_soft_state_free(vds_state, instance);
1773 	return (DDI_SUCCESS);
1774 }
1775 
1776 static boolean_t
1777 is_pseudo_device(dev_info_t *dip)
1778 {
1779 	dev_info_t	*parent, *root = ddi_root_node();
1780 
1781 
1782 	for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root);
1783 	    parent = ddi_get_parent(parent)) {
1784 		if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0)
1785 			return (B_TRUE);
1786 	}
1787 
1788 	return (B_FALSE);
1789 }
1790 
1791 static int
1792 vd_setup_full_disk(vd_t *vd)
1793 {
1794 	int		rval, status;
1795 	major_t		major = getmajor(vd->dev[0]);
1796 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
1797 	struct vtoc	vtoc;
1798 
1799 
1800 	/* Get the VTOC for slice sizes */
1801 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC, (intptr_t)&vtoc,
1802 		    (vd_open_flags | FKIOCTL), kcred, &rval)) != 0) {
1803 		PRN("ldi_ioctl(DKIOCGVTOC) returned errno %d", status);
1804 		return (status);
1805 	}
1806 
1807 	/* Set full-disk parameters */
1808 	vd->vdisk_type	= VD_DISK_TYPE_DISK;
1809 	vd->nslices	= (sizeof (vd->dev))/(sizeof (vd->dev[0]));
1810 
1811 	/* Move dev number and LDI handle to entire-disk-slice array elements */
1812 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
1813 	vd->dev[0]				= 0;
1814 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
1815 	vd->ldi_handle[0]			= NULL;
1816 
1817 	/* Initialize device numbers for remaining slices and open them */
1818 	for (int slice = 0; slice < vd->nslices; slice++) {
1819 		/*
1820 		 * Skip the entire-disk slice, as it's already open and its
1821 		 * device known
1822 		 */
1823 		if (slice == VD_ENTIRE_DISK_SLICE)
1824 			continue;
1825 		ASSERT(vd->dev[slice] == 0);
1826 		ASSERT(vd->ldi_handle[slice] == NULL);
1827 
1828 		/*
1829 		 * Construct the device number for the current slice
1830 		 */
1831 		vd->dev[slice] = makedevice(major, (minor + slice));
1832 
1833 		/*
1834 		 * At least some underlying drivers refuse to open
1835 		 * devices for (currently) zero-length slices, so skip
1836 		 * them for now
1837 		 */
1838 		if (vtoc.v_part[slice].p_size == 0) {
1839 			PR0("Skipping zero-length slice %u", slice);
1840 			continue;
1841 		}
1842 
1843 		/*
1844 		 * Open all non-empty slices of the disk to serve them to the
1845 		 * client.  Slices are opened exclusively to prevent other
1846 		 * threads or processes in the service domain from performing
1847 		 * I/O to slices being accessed by a client.  Failure to open
1848 		 * a slice results in vds not serving this disk, as the client
1849 		 * could attempt (and should be able) to access any non-empty
1850 		 * slice immediately.  Any slices successfully opened before a
1851 		 * failure will get closed by vds_destroy_vd() as a result of
1852 		 * the error returned by this function.
1853 		 */
1854 		PR0("Opening device major %u, minor %u = slice %u",
1855 		    major, minor, slice);
1856 		if ((status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
1857 			    vd_open_flags, kcred, &vd->ldi_handle[slice],
1858 			    vd->vds->ldi_ident)) != 0) {
1859 			PRN("ldi_open_by_dev() returned errno %d "
1860 			    "for slice %u", status, slice);
1861 			/* vds_destroy_vd() will close any open slices */
1862 			return (status);
1863 		}
1864 	}
1865 
1866 	return (0);
1867 }
1868 
1869 static int
1870 vd_setup_vd(char *block_device, vd_t *vd)
1871 {
1872 	int		otyp, rval, status;
1873 	dev_info_t	*dip;
1874 	struct dk_cinfo	dk_cinfo;
1875 
1876 
1877 	if ((status = ldi_open_by_name(block_device, vd_open_flags, kcred,
1878 		    &vd->ldi_handle[0], vd->vds->ldi_ident)) != 0) {
1879 		PRN("ldi_open_by_name(%s) = errno %d", block_device, status);
1880 		return (status);
1881 	}
1882 
1883 	/* Get block device's device number, otyp, and size */
1884 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
1885 		PRN("ldi_get_dev() returned errno %d for %s",
1886 		    status, block_device);
1887 		return (status);
1888 	}
1889 	if ((status = ldi_get_otyp(vd->ldi_handle[0], &otyp)) != 0) {
1890 		PRN("ldi_get_otyp() returned errno %d for %s",
1891 		    status, block_device);
1892 		return (status);
1893 	}
1894 	if (otyp != OTYP_BLK) {
1895 		PRN("Cannot serve non-block device %s", block_device);
1896 		return (ENOTBLK);
1897 	}
1898 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
1899 		PRN("ldi_get_size() failed for %s", block_device);
1900 		return (EIO);
1901 	}
1902 
1903 	/* Determine if backing block device is a pseudo device */
1904 	if ((dip = ddi_hold_devi_by_instance(getmajor(vd->dev[0]),
1905 		    dev_to_instance(vd->dev[0]), 0))  == NULL) {
1906 		PRN("%s is no longer accessible", block_device);
1907 		return (EIO);
1908 	}
1909 	vd->pseudo = is_pseudo_device(dip);
1910 	ddi_release_devi(dip);
1911 	if (vd->pseudo) {
1912 		vd->vdisk_type	= VD_DISK_TYPE_SLICE;
1913 		vd->nslices	= 1;
1914 		return (0);	/* ...and we're done */
1915 	}
1916 
1917 	/* Get dk_cinfo to determine slice of backing block device */
1918 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
1919 		    (intptr_t)&dk_cinfo, (vd_open_flags | FKIOCTL), kcred,
1920 		    &rval)) != 0) {
1921 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
1922 		    status, block_device);
1923 		return (status);
1924 	}
1925 
1926 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
1927 		PRN("slice %u >= maximum slice %u for %s",
1928 		    dk_cinfo.dki_partition, V_NUMPAR, block_device);
1929 		return (EIO);
1930 	}
1931 
1932 
1933 	/* If slice is entire-disk slice, initialize for full disk */
1934 	if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE)
1935 		return (vd_setup_full_disk(vd));
1936 
1937 
1938 	/* Otherwise, we have a non-entire slice of a block device */
1939 	vd->vdisk_type	= VD_DISK_TYPE_SLICE;
1940 	vd->nslices	= 1;
1941 
1942 
1943 	/* Initialize dk_geom structure for single-slice block device */
1944 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
1945 		    (intptr_t)&vd->dk_geom, (vd_open_flags | FKIOCTL), kcred,
1946 		    &rval)) != 0) {
1947 		PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s",
1948 		    status, block_device);
1949 		return (status);
1950 	}
1951 	if (vd->dk_geom.dkg_nsect == 0) {
1952 		PRN("%s geometry claims 0 sectors per track", block_device);
1953 		return (EIO);
1954 	}
1955 	if (vd->dk_geom.dkg_nhead == 0) {
1956 		PRN("%s geometry claims 0 heads", block_device);
1957 		return (EIO);
1958 	}
1959 	vd->dk_geom.dkg_ncyl =
1960 	    lbtodb(vd->vdisk_size)/vd->dk_geom.dkg_nsect/vd->dk_geom.dkg_nhead;
1961 	vd->dk_geom.dkg_acyl = 0;
1962 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
1963 
1964 
1965 	/* Initialize vtoc structure for single-slice block device */
1966 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC,
1967 		    (intptr_t)&vd->vtoc, (vd_open_flags | FKIOCTL), kcred,
1968 		    &rval)) != 0) {
1969 		PRN("ldi_ioctl(DKIOCGVTOC) returned errno %d for %s",
1970 		    status, block_device);
1971 		return (status);
1972 	}
1973 	bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
1974 	    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
1975 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
1976 	vd->vtoc.v_nparts = 1;
1977 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
1978 	vd->vtoc.v_part[0].p_flag = 0;
1979 	vd->vtoc.v_part[0].p_start = 0;
1980 	vd->vtoc.v_part[0].p_size = lbtodb(vd->vdisk_size);
1981 	bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
1982 	    MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel)));
1983 
1984 
1985 	return (0);
1986 }
1987 
1988 static int
1989 vds_do_init_vd(vds_t *vds, uint64_t id, char *block_device, uint64_t ldc_id,
1990     vd_t **vdp)
1991 {
1992 	char			tq_name[TASKQ_NAMELEN];
1993 	int			status;
1994 	ddi_iblock_cookie_t	iblock = NULL;
1995 	ldc_attr_t		ldc_attr;
1996 	vd_t			*vd;
1997 
1998 
1999 	ASSERT(vds != NULL);
2000 	ASSERT(block_device != NULL);
2001 	ASSERT(vdp != NULL);
2002 	PR0("Adding vdisk for %s", block_device);
2003 
2004 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
2005 		PRN("No memory for virtual disk");
2006 		return (EAGAIN);
2007 	}
2008 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
2009 	vd->vds = vds;
2010 
2011 
2012 	/* Open vdisk and initialize parameters */
2013 	if ((status = vd_setup_vd(block_device, vd)) != 0)
2014 		return (status);
2015 	ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
2016 	PR0("vdisk_type = %s, pseudo = %s, nslices = %u",
2017 	    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
2018 	    (vd->pseudo ? "yes" : "no"), vd->nslices);
2019 
2020 
2021 	/* Initialize locking */
2022 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
2023 		&iblock) != DDI_SUCCESS) {
2024 		PRN("Could not get iblock cookie.");
2025 		return (EIO);
2026 	}
2027 
2028 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
2029 	vd->initialized |= VD_LOCKING;
2030 
2031 
2032 	/* Create start and completion task queues for the vdisk */
2033 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
2034 	PR1("tq_name = %s", tq_name);
2035 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
2036 		    TASKQ_DEFAULTPRI, 0)) == NULL) {
2037 		PRN("Could not create task queue");
2038 		return (EIO);
2039 	}
2040 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
2041 	PR1("tq_name = %s", tq_name);
2042 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
2043 		    TASKQ_DEFAULTPRI, 0)) == NULL) {
2044 		PRN("Could not create task queue");
2045 		return (EIO);
2046 	}
2047 	vd->enabled = 1;	/* before callback can dispatch to startq */
2048 
2049 
2050 	/* Bring up LDC */
2051 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
2052 	ldc_attr.instance	= ddi_get_instance(vds->dip);
2053 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
2054 	ldc_attr.qlen		= VD_LDC_QLEN;
2055 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
2056 		PRN("ldc_init(%lu) = errno %d", ldc_id, status);
2057 		return (status);
2058 	}
2059 	vd->initialized |= VD_LDC;
2060 
2061 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
2062 		(caddr_t)vd)) != 0) {
2063 		PRN("ldc_reg_callback() returned errno %d", status);
2064 		return (status);
2065 	}
2066 
2067 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
2068 		PRN("ldc_open() returned errno %d", status);
2069 		return (status);
2070 	}
2071 
2072 
2073 	/* Add the successfully-initialized vdisk to the server's table */
2074 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
2075 		PRN("Error adding vdisk ID %lu to table", id);
2076 		return (EIO);
2077 	}
2078 
2079 	return (0);
2080 }
2081 
2082 /*
2083  * Destroy the state associated with a virtual disk
2084  */
2085 static void
2086 vds_destroy_vd(void *arg)
2087 {
2088 	vd_t	*vd = (vd_t *)arg;
2089 
2090 
2091 	if (vd == NULL)
2092 		return;
2093 
2094 	PR0("Destroying vdisk state");
2095 
2096 	/* Disable queuing requests for the vdisk */
2097 	if (vd->initialized & VD_LOCKING) {
2098 		mutex_enter(&vd->lock);
2099 		vd->enabled = 0;
2100 		mutex_exit(&vd->lock);
2101 	}
2102 
2103 	/* Drain and destroy start queue (*before* destroying completionq) */
2104 	if (vd->startq != NULL)
2105 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
2106 
2107 	/* Drain and destroy completion queue (*before* shutting down LDC) */
2108 	if (vd->completionq != NULL)
2109 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
2110 
2111 	if (vd->dring_task != NULL) {
2112 		ASSERT(vd->dring_len != 0);
2113 		kmem_free(vd->dring_task,
2114 		    (sizeof (*vd->dring_task)) * vd->dring_len);
2115 	}
2116 
2117 	/* Shut down LDC */
2118 	if (vd->initialized & VD_LDC) {
2119 		if (vd->initialized & VD_DRING)
2120 			(void) ldc_mem_dring_unmap(vd->dring_handle);
2121 		(void) ldc_unreg_callback(vd->ldc_handle);
2122 		(void) ldc_close(vd->ldc_handle);
2123 		(void) ldc_fini(vd->ldc_handle);
2124 	}
2125 
2126 	/* Close any open backing-device slices */
2127 	for (uint_t slice = 0; slice < vd->nslices; slice++) {
2128 		if (vd->ldi_handle[slice] != NULL) {
2129 			PR0("Closing slice %u", slice);
2130 			(void) ldi_close(vd->ldi_handle[slice],
2131 			    vd_open_flags, kcred);
2132 		}
2133 	}
2134 
2135 	/* Free lock */
2136 	if (vd->initialized & VD_LOCKING)
2137 		mutex_destroy(&vd->lock);
2138 
2139 	/* Finally, free the vdisk structure itself */
2140 	kmem_free(vd, sizeof (*vd));
2141 }
2142 
2143 static int
2144 vds_init_vd(vds_t *vds, uint64_t id, char *block_device, uint64_t ldc_id)
2145 {
2146 	int	status;
2147 	vd_t	*vd = NULL;
2148 
2149 
2150 #ifdef lint
2151 	(void) vd;
2152 #endif	/* lint */
2153 
2154 	if ((status = vds_do_init_vd(vds, id, block_device, ldc_id, &vd)) != 0)
2155 		vds_destroy_vd(vd);
2156 
2157 	return (status);
2158 }
2159 
2160 static int
2161 vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
2162     uint64_t *ldc_id)
2163 {
2164 	int	num_channels;
2165 
2166 
2167 	/* Look for channel endpoint child(ren) of the vdisk MD node */
2168 	if ((num_channels = md_scan_dag(md, vd_node,
2169 		    md_find_name(md, VD_CHANNEL_ENDPOINT),
2170 		    md_find_name(md, "fwd"), channel)) <= 0) {
2171 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
2172 		return (-1);
2173 	}
2174 
2175 	/* Get the "id" value for the first channel endpoint node */
2176 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
2177 		PRN("No \"%s\" property found for \"%s\" of vdisk",
2178 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
2179 		return (-1);
2180 	}
2181 
2182 	if (num_channels > 1) {
2183 		PRN("Using ID of first of multiple channels for this vdisk");
2184 	}
2185 
2186 	return (0);
2187 }
2188 
2189 static int
2190 vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
2191 {
2192 	int		num_nodes, status;
2193 	size_t		size;
2194 	mde_cookie_t	*channel;
2195 
2196 
2197 	if ((num_nodes = md_node_count(md)) <= 0) {
2198 		PRN("Invalid node count in Machine Description subtree");
2199 		return (-1);
2200 	}
2201 	size = num_nodes*(sizeof (*channel));
2202 	channel = kmem_zalloc(size, KM_SLEEP);
2203 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
2204 	kmem_free(channel, size);
2205 
2206 	return (status);
2207 }
2208 
2209 static void
2210 vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
2211 {
2212 	char		*block_device = NULL;
2213 	uint64_t	id = 0, ldc_id = 0;
2214 
2215 
2216 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
2217 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
2218 		return;
2219 	}
2220 	PR0("Adding vdisk ID %lu", id);
2221 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
2222 		&block_device) != 0) {
2223 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
2224 		return;
2225 	}
2226 
2227 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
2228 		PRN("Error getting LDC ID for vdisk %lu", id);
2229 		return;
2230 	}
2231 
2232 	if (vds_init_vd(vds, id, block_device, ldc_id) != 0) {
2233 		PRN("Failed to add vdisk ID %lu", id);
2234 		return;
2235 	}
2236 }
2237 
2238 static void
2239 vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
2240 {
2241 	uint64_t	id = 0;
2242 
2243 
2244 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
2245 		PRN("Unable to get \"%s\" property from vdisk's MD node",
2246 		    VD_ID_PROP);
2247 		return;
2248 	}
2249 	PR0("Removing vdisk ID %lu", id);
2250 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
2251 		PRN("No vdisk entry found for vdisk ID %lu", id);
2252 }
2253 
2254 static void
2255 vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
2256     md_t *curr_md, mde_cookie_t curr_vd_node)
2257 {
2258 	char		*curr_dev, *prev_dev;
2259 	uint64_t	curr_id = 0, curr_ldc_id = 0;
2260 	uint64_t	prev_id = 0, prev_ldc_id = 0;
2261 	size_t		len;
2262 
2263 
2264 	/* Validate that vdisk ID has not changed */
2265 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
2266 		PRN("Error getting previous vdisk \"%s\" property",
2267 		    VD_ID_PROP);
2268 		return;
2269 	}
2270 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
2271 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
2272 		return;
2273 	}
2274 	if (curr_id != prev_id) {
2275 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
2276 		    prev_id, curr_id);
2277 		return;
2278 	}
2279 
2280 	/* Validate that LDC ID has not changed */
2281 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
2282 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
2283 		return;
2284 	}
2285 
2286 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
2287 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
2288 		return;
2289 	}
2290 	if (curr_ldc_id != prev_ldc_id) {
2291 		_NOTE(NOTREACHED);	/* lint is confused */
2292 		PRN("Not changing vdisk:  "
2293 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
2294 		return;
2295 	}
2296 
2297 	/* Determine whether device path has changed */
2298 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
2299 		&prev_dev) != 0) {
2300 		PRN("Error getting previous vdisk \"%s\"",
2301 		    VD_BLOCK_DEVICE_PROP);
2302 		return;
2303 	}
2304 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
2305 		&curr_dev) != 0) {
2306 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
2307 		return;
2308 	}
2309 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
2310 	    (strncmp(curr_dev, prev_dev, len) == 0))
2311 		return;	/* no relevant (supported) change */
2312 
2313 	PR0("Changing vdisk ID %lu", prev_id);
2314 	/* Remove old state, which will close vdisk and reset */
2315 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
2316 		PRN("No entry found for vdisk ID %lu", prev_id);
2317 	/* Re-initialize vdisk with new state */
2318 	if (vds_init_vd(vds, curr_id, curr_dev, curr_ldc_id) != 0) {
2319 		PRN("Failed to change vdisk ID %lu", curr_id);
2320 		return;
2321 	}
2322 }
2323 
2324 static int
2325 vds_process_md(void *arg, mdeg_result_t *md)
2326 {
2327 	int	i;
2328 	vds_t	*vds = arg;
2329 
2330 
2331 	if (md == NULL)
2332 		return (MDEG_FAILURE);
2333 	ASSERT(vds != NULL);
2334 
2335 	for (i = 0; i < md->removed.nelem; i++)
2336 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
2337 	for (i = 0; i < md->match_curr.nelem; i++)
2338 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
2339 		    md->match_curr.mdp, md->match_curr.mdep[i]);
2340 	for (i = 0; i < md->added.nelem; i++)
2341 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
2342 
2343 	return (MDEG_SUCCESS);
2344 }
2345 
2346 static int
2347 vds_do_attach(dev_info_t *dip)
2348 {
2349 	static char	reg_prop[] = "reg";	/* devinfo ID prop */
2350 
2351 	/* MDEG specification for a (particular) vds node */
2352 	static mdeg_prop_spec_t	vds_prop_spec[] = {
2353 		{MDET_PROP_STR, "name", {VDS_NAME}},
2354 		{MDET_PROP_VAL, "cfg-handle", {0}},
2355 		{MDET_LIST_END, NULL, {0}}};
2356 	static mdeg_node_spec_t	vds_spec = {"virtual-device", vds_prop_spec};
2357 
2358 	/* MDEG specification for matching a vd node */
2359 	static md_prop_match_t	vd_prop_spec[] = {
2360 		{MDET_PROP_VAL, VD_ID_PROP},
2361 		{MDET_LIST_END, NULL}};
2362 	static mdeg_node_match_t vd_spec = {"virtual-device-port",
2363 					    vd_prop_spec};
2364 
2365 	int			status;
2366 	uint64_t		cfg_handle;
2367 	minor_t			instance = ddi_get_instance(dip);
2368 	vds_t			*vds;
2369 
2370 
2371 	/*
2372 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
2373 	 * notion of "instance", or unique identifier, for that node; OBP
2374 	 * stores the value of the "cfg-handle" MD property as the value of
2375 	 * the "reg" property on the node in the device tree it builds from
2376 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
2377 	 * "reg" property value to uniquely identify this device instance when
2378 	 * registering with the MD event-generation framework.  If the "reg"
2379 	 * property cannot be found, the device tree state is presumably so
2380 	 * broken that there is no point in continuing.
2381 	 */
2382 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, reg_prop)) {
2383 		PRN("vds \"%s\" property does not exist", reg_prop);
2384 		return (DDI_FAILURE);
2385 	}
2386 
2387 	/* Get the MD instance for later MDEG registration */
2388 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2389 	    reg_prop, -1);
2390 
2391 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
2392 		PRN("Could not allocate state for instance %u", instance);
2393 		return (DDI_FAILURE);
2394 	}
2395 
2396 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
2397 		PRN("Could not get state for instance %u", instance);
2398 		ddi_soft_state_free(vds_state, instance);
2399 		return (DDI_FAILURE);
2400 	}
2401 
2402 
2403 	vds->dip	= dip;
2404 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
2405 							vds_destroy_vd,
2406 							sizeof (void *));
2407 	ASSERT(vds->vd_table != NULL);
2408 
2409 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
2410 		PRN("ldi_ident_from_dip() returned errno %d", status);
2411 		return (DDI_FAILURE);
2412 	}
2413 	vds->initialized |= VDS_LDI;
2414 
2415 	/* Register for MD updates */
2416 	vds_prop_spec[1].ps_val = cfg_handle;
2417 	if (mdeg_register(&vds_spec, &vd_spec, vds_process_md, vds,
2418 		&vds->mdeg) != MDEG_SUCCESS) {
2419 		PRN("Unable to register for MD updates");
2420 		return (DDI_FAILURE);
2421 	}
2422 	vds->initialized |= VDS_MDEG;
2423 
2424 	/* Prevent auto-detaching so driver is available whenever MD changes */
2425 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
2426 	    DDI_PROP_SUCCESS) {
2427 		PRN("failed to set \"%s\" property for instance %u",
2428 		    DDI_NO_AUTODETACH, instance);
2429 	}
2430 
2431 	ddi_report_dev(dip);
2432 	return (DDI_SUCCESS);
2433 }
2434 
2435 static int
2436 vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2437 {
2438 	int	status;
2439 
2440 	switch (cmd) {
2441 	case DDI_ATTACH:
2442 		PR0("Attaching");
2443 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
2444 			(void) vds_detach(dip, DDI_DETACH);
2445 		return (status);
2446 	case DDI_RESUME:
2447 		PR0("No action required for DDI_RESUME");
2448 		return (DDI_SUCCESS);
2449 	default:
2450 		return (DDI_FAILURE);
2451 	}
2452 }
2453 
2454 static struct dev_ops vds_ops = {
2455 	DEVO_REV,	/* devo_rev */
2456 	0,		/* devo_refcnt */
2457 	ddi_no_info,	/* devo_getinfo */
2458 	nulldev,	/* devo_identify */
2459 	nulldev,	/* devo_probe */
2460 	vds_attach,	/* devo_attach */
2461 	vds_detach,	/* devo_detach */
2462 	nodev,		/* devo_reset */
2463 	NULL,		/* devo_cb_ops */
2464 	NULL,		/* devo_bus_ops */
2465 	nulldev		/* devo_power */
2466 };
2467 
2468 static struct modldrv modldrv = {
2469 	&mod_driverops,
2470 	"virtual disk server v%I%",
2471 	&vds_ops,
2472 };
2473 
2474 static struct modlinkage modlinkage = {
2475 	MODREV_1,
2476 	&modldrv,
2477 	NULL
2478 };
2479 
2480 
2481 int
2482 _init(void)
2483 {
2484 	int		i, status;
2485 
2486 
2487 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
2488 		return (status);
2489 	if ((status = mod_install(&modlinkage)) != 0) {
2490 		ddi_soft_state_fini(&vds_state);
2491 		return (status);
2492 	}
2493 
2494 	/* Fill in the bit-mask of server-supported operations */
2495 	for (i = 0; i < vds_noperations; i++)
2496 		vds_operations |= 1 << (vds_operation[i].operation - 1);
2497 
2498 	return (0);
2499 }
2500 
2501 int
2502 _info(struct modinfo *modinfop)
2503 {
2504 	return (mod_info(&modlinkage, modinfop));
2505 }
2506 
2507 int
2508 _fini(void)
2509 {
2510 	int	status;
2511 
2512 
2513 	if ((status = mod_remove(&modlinkage)) != 0)
2514 		return (status);
2515 	ddi_soft_state_fini(&vds_state);
2516 	return (0);
2517 }
2518