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