xref: /titanic_44/usr/src/uts/sun4v/io/vdc.c (revision 62a1b812a0c45c0d4c10ef645dda42bf71080108)
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 2007 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  * LDoms virtual disk client (vdc) device driver
31  *
32  * This driver runs on a guest logical domain and communicates with the virtual
33  * disk server (vds) driver running on the service domain which is exporting
34  * virtualized "disks" to the guest logical domain.
35  *
36  * The driver can be divided into four sections:
37  *
38  * 1) generic device driver housekeeping
39  *	_init, _fini, attach, detach, ops structures, etc.
40  *
41  * 2) communication channel setup
42  *	Setup the communications link over the LDC channel that vdc uses to
43  *	talk to the vDisk server. Initialise the descriptor ring which
44  *	allows the LDC clients to transfer data via memory mappings.
45  *
46  * 3) Support exported to upper layers (filesystems, etc)
47  *	The upper layers call into vdc via strategy(9E) and DKIO(7I)
48  *	ioctl calls. vdc will copy the data to be written to the descriptor
49  *	ring or maps the buffer to store the data read by the vDisk
50  *	server into the descriptor ring. It then sends a message to the
51  *	vDisk server requesting it to complete the operation.
52  *
53  * 4) Handling responses from vDisk server.
54  *	The vDisk server will ACK some or all of the messages vdc sends to it
55  *	(this is configured during the handshake). Upon receipt of an ACK
56  *	vdc will check the descriptor ring and signal to the upper layer
57  *	code waiting on the IO.
58  */
59 
60 #include <sys/atomic.h>
61 #include <sys/conf.h>
62 #include <sys/disp.h>
63 #include <sys/ddi.h>
64 #include <sys/dkio.h>
65 #include <sys/efi_partition.h>
66 #include <sys/fcntl.h>
67 #include <sys/file.h>
68 #include <sys/mach_descrip.h>
69 #include <sys/modctl.h>
70 #include <sys/mdeg.h>
71 #include <sys/note.h>
72 #include <sys/open.h>
73 #include <sys/sdt.h>
74 #include <sys/stat.h>
75 #include <sys/sunddi.h>
76 #include <sys/types.h>
77 #include <sys/promif.h>
78 #include <sys/vtoc.h>
79 #include <sys/archsystm.h>
80 #include <sys/sysmacros.h>
81 
82 #include <sys/cdio.h>
83 #include <sys/dktp/fdisk.h>
84 #include <sys/scsi/generic/sense.h>
85 #include <sys/scsi/impl/uscsi.h>	/* Needed for defn of USCSICMD ioctl */
86 
87 #include <sys/ldoms.h>
88 #include <sys/ldc.h>
89 #include <sys/vio_common.h>
90 #include <sys/vio_mailbox.h>
91 #include <sys/vdsk_common.h>
92 #include <sys/vdsk_mailbox.h>
93 #include <sys/vdc.h>
94 
95 /*
96  * function prototypes
97  */
98 
99 /* standard driver functions */
100 static int	vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred);
101 static int	vdc_close(dev_t dev, int flag, int otyp, cred_t *cred);
102 static int	vdc_strategy(struct buf *buf);
103 static int	vdc_print(dev_t dev, char *str);
104 static int	vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
105 static int	vdc_read(dev_t dev, struct uio *uio, cred_t *cred);
106 static int	vdc_write(dev_t dev, struct uio *uio, cred_t *cred);
107 static int	vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
108 			cred_t *credp, int *rvalp);
109 static int	vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred);
110 static int	vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred);
111 
112 static int	vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
113 			void *arg, void **resultp);
114 static int	vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
115 static int	vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
116 
117 /* setup */
118 static void	vdc_min(struct buf *bufp);
119 static int	vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen);
120 static int	vdc_do_ldc_init(vdc_t *vdc);
121 static int	vdc_start_ldc_connection(vdc_t *vdc);
122 static int	vdc_create_device_nodes(vdc_t *vdc);
123 static int	vdc_create_device_nodes_efi(vdc_t *vdc);
124 static int	vdc_create_device_nodes_vtoc(vdc_t *vdc);
125 static int	vdc_create_device_nodes_props(vdc_t *vdc);
126 static int	vdc_get_ldc_id(dev_info_t *dip, uint64_t *ldc_id);
127 static int	vdc_do_ldc_up(vdc_t *vdc);
128 static void	vdc_terminate_ldc(vdc_t *vdc);
129 static int	vdc_init_descriptor_ring(vdc_t *vdc);
130 static void	vdc_destroy_descriptor_ring(vdc_t *vdc);
131 static int	vdc_setup_devid(vdc_t *vdc);
132 static void	vdc_store_efi(vdc_t *vdc, struct dk_gpt *efi);
133 
134 /* handshake with vds */
135 static int		vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver);
136 static int		vdc_ver_negotiation(vdc_t *vdcp);
137 static int		vdc_init_attr_negotiation(vdc_t *vdc);
138 static int		vdc_attr_negotiation(vdc_t *vdcp);
139 static int		vdc_init_dring_negotiate(vdc_t *vdc);
140 static int		vdc_dring_negotiation(vdc_t *vdcp);
141 static int		vdc_send_rdx(vdc_t *vdcp);
142 static int		vdc_rdx_exchange(vdc_t *vdcp);
143 static boolean_t	vdc_is_supported_version(vio_ver_msg_t *ver_msg);
144 
145 /* processing incoming messages from vDisk server */
146 static void	vdc_process_msg_thread(vdc_t *vdc);
147 static int	vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp);
148 
149 static uint_t	vdc_handle_cb(uint64_t event, caddr_t arg);
150 static int	vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg);
151 static int	vdc_process_err_msg(vdc_t *vdc, vio_msg_t msg);
152 static int	vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg);
153 static int	vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg);
154 static int	vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg);
155 static int 	vdc_send_request(vdc_t *vdcp, int operation,
156 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
157 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
158 static int	vdc_map_to_shared_dring(vdc_t *vdcp, int idx);
159 static int 	vdc_populate_descriptor(vdc_t *vdcp, int operation,
160 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
161 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
162 static int 	vdc_do_sync_op(vdc_t *vdcp, int operation,
163 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
164 		    int cb_type, void *cb_arg, vio_desc_direction_t dir);
165 
166 static int	vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp);
167 static int	vdc_drain_response(vdc_t *vdcp);
168 static int	vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx);
169 static int	vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep);
170 static int	vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg);
171 
172 /* dkio */
173 static int	vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode);
174 static int	vdc_create_fake_geometry(vdc_t *vdc);
175 static int	vdc_setup_disk_layout(vdc_t *vdc);
176 static int	vdc_null_copy_func(vdc_t *vdc, void *from, void *to,
177 		    int mode, int dir);
178 static int	vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
179 		    int mode, int dir);
180 static int	vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
181 		    int mode, int dir);
182 static int	vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to,
183 		    int mode, int dir);
184 static int	vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to,
185 		    int mode, int dir);
186 static int	vdc_get_geom_convert(vdc_t *vdc, void *from, void *to,
187 		    int mode, int dir);
188 static int	vdc_set_geom_convert(vdc_t *vdc, void *from, void *to,
189 		    int mode, int dir);
190 static int	vdc_uscsicmd_convert(vdc_t *vdc, void *from, void *to,
191 		    int mode, int dir);
192 static int	vdc_get_efi_convert(vdc_t *vdc, void *from, void *to,
193 		    int mode, int dir);
194 static int	vdc_set_efi_convert(vdc_t *vdc, void *from, void *to,
195 		    int mode, int dir);
196 
197 /*
198  * Module variables
199  */
200 
201 /*
202  * Tunable variables to control how long vdc waits before timing out on
203  * various operations
204  */
205 static int	vdc_retries = 10;
206 static int	vdc_hshake_retries = 3;
207 
208 /* calculated from 'vdc_usec_timeout' during attach */
209 static uint64_t	vdc_hz_timeout;				/* units: Hz */
210 static uint64_t	vdc_usec_timeout = 30 * MICROSEC;	/* 30s units: ns */
211 
212 static uint64_t vdc_hz_min_ldc_delay;
213 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC;
214 static uint64_t vdc_hz_max_ldc_delay;
215 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC;
216 
217 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC;
218 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC;
219 
220 /* values for dumping - need to run in a tighter loop */
221 static uint64_t	vdc_usec_timeout_dump = 100 * MILLISEC;	/* 0.1s units: ns */
222 static int	vdc_dump_retries = 100;
223 
224 /* Count of the number of vdc instances attached */
225 static volatile uint32_t	vdc_instance_count = 0;
226 
227 /* Soft state pointer */
228 static void	*vdc_state;
229 
230 /*
231  * Controlling the verbosity of the error/debug messages
232  *
233  * vdc_msglevel - controls level of messages
234  * vdc_matchinst - 64-bit variable where each bit corresponds
235  *                 to the vdc instance the vdc_msglevel applies.
236  */
237 int		vdc_msglevel = 0x0;
238 uint64_t	vdc_matchinst = 0ull;
239 
240 /*
241  * Supported vDisk protocol version pairs.
242  *
243  * The first array entry is the latest and preferred version.
244  */
245 static const vio_ver_t	vdc_version[] = {{1, 0}};
246 
247 static struct cb_ops vdc_cb_ops = {
248 	vdc_open,	/* cb_open */
249 	vdc_close,	/* cb_close */
250 	vdc_strategy,	/* cb_strategy */
251 	vdc_print,	/* cb_print */
252 	vdc_dump,	/* cb_dump */
253 	vdc_read,	/* cb_read */
254 	vdc_write,	/* cb_write */
255 	vdc_ioctl,	/* cb_ioctl */
256 	nodev,		/* cb_devmap */
257 	nodev,		/* cb_mmap */
258 	nodev,		/* cb_segmap */
259 	nochpoll,	/* cb_chpoll */
260 	ddi_prop_op,	/* cb_prop_op */
261 	NULL,		/* cb_str */
262 	D_MP | D_64BIT,	/* cb_flag */
263 	CB_REV,		/* cb_rev */
264 	vdc_aread,	/* cb_aread */
265 	vdc_awrite	/* cb_awrite */
266 };
267 
268 static struct dev_ops vdc_ops = {
269 	DEVO_REV,	/* devo_rev */
270 	0,		/* devo_refcnt */
271 	vdc_getinfo,	/* devo_getinfo */
272 	nulldev,	/* devo_identify */
273 	nulldev,	/* devo_probe */
274 	vdc_attach,	/* devo_attach */
275 	vdc_detach,	/* devo_detach */
276 	nodev,		/* devo_reset */
277 	&vdc_cb_ops,	/* devo_cb_ops */
278 	NULL,		/* devo_bus_ops */
279 	nulldev		/* devo_power */
280 };
281 
282 static struct modldrv modldrv = {
283 	&mod_driverops,
284 	"virtual disk client %I%",
285 	&vdc_ops,
286 };
287 
288 static struct modlinkage modlinkage = {
289 	MODREV_1,
290 	&modldrv,
291 	NULL
292 };
293 
294 /* -------------------------------------------------------------------------- */
295 
296 /*
297  * Device Driver housekeeping and setup
298  */
299 
300 int
301 _init(void)
302 {
303 	int	status;
304 
305 	if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0)
306 		return (status);
307 	if ((status = mod_install(&modlinkage)) != 0)
308 		ddi_soft_state_fini(&vdc_state);
309 	vdc_efi_init(vd_process_ioctl);
310 	return (status);
311 }
312 
313 int
314 _info(struct modinfo *modinfop)
315 {
316 	return (mod_info(&modlinkage, modinfop));
317 }
318 
319 int
320 _fini(void)
321 {
322 	int	status;
323 
324 	if ((status = mod_remove(&modlinkage)) != 0)
325 		return (status);
326 	vdc_efi_fini();
327 	ddi_soft_state_fini(&vdc_state);
328 	return (0);
329 }
330 
331 static int
332 vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,  void *arg, void **resultp)
333 {
334 	_NOTE(ARGUNUSED(dip))
335 
336 	int	instance = VDCUNIT((dev_t)arg);
337 	vdc_t	*vdc = NULL;
338 
339 	switch (cmd) {
340 	case DDI_INFO_DEVT2DEVINFO:
341 		if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
342 			*resultp = NULL;
343 			return (DDI_FAILURE);
344 		}
345 		*resultp = vdc->dip;
346 		return (DDI_SUCCESS);
347 	case DDI_INFO_DEVT2INSTANCE:
348 		*resultp = (void *)(uintptr_t)instance;
349 		return (DDI_SUCCESS);
350 	default:
351 		*resultp = NULL;
352 		return (DDI_FAILURE);
353 	}
354 }
355 
356 static int
357 vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
358 {
359 	int	instance;
360 	int	rv;
361 	vdc_t	*vdc = NULL;
362 
363 	switch (cmd) {
364 	case DDI_DETACH:
365 		/* the real work happens below */
366 		break;
367 	case DDI_SUSPEND:
368 		/* nothing to do for this non-device */
369 		return (DDI_SUCCESS);
370 	default:
371 		return (DDI_FAILURE);
372 	}
373 
374 	ASSERT(cmd == DDI_DETACH);
375 	instance = ddi_get_instance(dip);
376 	DMSGX(1, "[%d] Entered\n", instance);
377 
378 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
379 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
380 		return (DDI_FAILURE);
381 	}
382 
383 	if (vdc->open_count) {
384 		DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance);
385 		return (DDI_FAILURE);
386 	}
387 
388 	DMSG(vdc, 0, "[%d] proceeding...\n", instance);
389 
390 	/* mark instance as detaching */
391 	vdc->lifecycle	= VDC_LC_DETACHING;
392 
393 	/*
394 	 * try and disable callbacks to prevent another handshake
395 	 */
396 	rv = ldc_set_cb_mode(vdc->ldc_handle, LDC_CB_DISABLE);
397 	DMSG(vdc, 0, "callback disabled (rv=%d)\n", rv);
398 
399 	if (vdc->initialized & VDC_THREAD) {
400 		mutex_enter(&vdc->read_lock);
401 		if ((vdc->read_state == VDC_READ_WAITING) ||
402 		    (vdc->read_state == VDC_READ_RESET)) {
403 			vdc->read_state = VDC_READ_RESET;
404 			cv_signal(&vdc->read_cv);
405 		}
406 
407 		mutex_exit(&vdc->read_lock);
408 
409 		/* wake up any thread waiting for connection to come online */
410 		mutex_enter(&vdc->lock);
411 		if (vdc->state == VDC_STATE_INIT_WAITING) {
412 			DMSG(vdc, 0,
413 			    "[%d] write reset - move to resetting state...\n",
414 			    instance);
415 			vdc->state = VDC_STATE_RESETTING;
416 			cv_signal(&vdc->initwait_cv);
417 		}
418 		mutex_exit(&vdc->lock);
419 
420 		/* now wait until state transitions to VDC_STATE_DETACH */
421 		thread_join(vdc->msg_proc_thr->t_did);
422 		ASSERT(vdc->state == VDC_STATE_DETACH);
423 		DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n",
424 		    vdc->instance);
425 	}
426 
427 	mutex_enter(&vdc->lock);
428 
429 	if (vdc->initialized & VDC_DRING)
430 		vdc_destroy_descriptor_ring(vdc);
431 
432 	if (vdc->initialized & VDC_LDC)
433 		vdc_terminate_ldc(vdc);
434 
435 	mutex_exit(&vdc->lock);
436 
437 	if (vdc->initialized & VDC_MINOR) {
438 		ddi_prop_remove_all(dip);
439 		ddi_remove_minor_node(dip, NULL);
440 	}
441 
442 	if (vdc->initialized & VDC_LOCKS) {
443 		mutex_destroy(&vdc->lock);
444 		mutex_destroy(&vdc->read_lock);
445 		cv_destroy(&vdc->initwait_cv);
446 		cv_destroy(&vdc->dring_free_cv);
447 		cv_destroy(&vdc->membind_cv);
448 		cv_destroy(&vdc->sync_pending_cv);
449 		cv_destroy(&vdc->sync_blocked_cv);
450 		cv_destroy(&vdc->read_cv);
451 		cv_destroy(&vdc->running_cv);
452 	}
453 
454 	if (vdc->minfo)
455 		kmem_free(vdc->minfo, sizeof (struct dk_minfo));
456 
457 	if (vdc->cinfo)
458 		kmem_free(vdc->cinfo, sizeof (struct dk_cinfo));
459 
460 	if (vdc->vtoc)
461 		kmem_free(vdc->vtoc, sizeof (struct vtoc));
462 
463 	if (vdc->label)
464 		kmem_free(vdc->label, DK_LABEL_SIZE);
465 
466 	if (vdc->devid) {
467 		ddi_devid_unregister(dip);
468 		ddi_devid_free(vdc->devid);
469 	}
470 
471 	if (vdc->initialized & VDC_SOFT_STATE)
472 		ddi_soft_state_free(vdc_state, instance);
473 
474 	DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc);
475 
476 	return (DDI_SUCCESS);
477 }
478 
479 
480 static int
481 vdc_do_attach(dev_info_t *dip)
482 {
483 	int		instance;
484 	vdc_t		*vdc = NULL;
485 	int		status;
486 
487 	ASSERT(dip != NULL);
488 
489 	instance = ddi_get_instance(dip);
490 	if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) {
491 		cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure",
492 		    instance);
493 		return (DDI_FAILURE);
494 	}
495 
496 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
497 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
498 		return (DDI_FAILURE);
499 	}
500 
501 	/*
502 	 * We assign the value to initialized in this case to zero out the
503 	 * variable and then set bits in it to indicate what has been done
504 	 */
505 	vdc->initialized = VDC_SOFT_STATE;
506 
507 	vdc_hz_timeout = drv_usectohz(vdc_usec_timeout);
508 
509 	vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc);
510 	vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc);
511 
512 	vdc->dip	= dip;
513 	vdc->instance	= instance;
514 	vdc->open_count	= 0;
515 	vdc->vdisk_type	= VD_DISK_TYPE_UNK;
516 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
517 	vdc->state	= VDC_STATE_INIT;
518 	vdc->lifecycle	= VDC_LC_ATTACHING;
519 	vdc->ldc_state	= 0;
520 	vdc->session_id = 0;
521 	vdc->block_size = DEV_BSIZE;
522 	vdc->max_xfer_sz = maxphys / DEV_BSIZE;
523 
524 	vdc->vtoc = NULL;
525 	vdc->cinfo = NULL;
526 	vdc->minfo = NULL;
527 
528 	mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL);
529 	cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL);
530 	cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL);
531 	cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL);
532 	cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL);
533 
534 	vdc->threads_pending = 0;
535 	vdc->sync_op_pending = B_FALSE;
536 	vdc->sync_op_blocked = B_FALSE;
537 	cv_init(&vdc->sync_pending_cv, NULL, CV_DRIVER, NULL);
538 	cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL);
539 
540 	/* init blocking msg read functionality */
541 	mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL);
542 	cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL);
543 	vdc->read_state = VDC_READ_IDLE;
544 
545 	vdc->initialized |= VDC_LOCKS;
546 
547 	/* initialise LDC channel which will be used to communicate with vds */
548 	if ((status = vdc_do_ldc_init(vdc)) != 0) {
549 		cmn_err(CE_NOTE, "[%d] Couldn't initialize LDC", instance);
550 		goto return_status;
551 	}
552 
553 	/* initialize the thread responsible for managing state with server */
554 	vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread,
555 	    vdc, 0, &p0, TS_RUN, minclsyspri);
556 	if (vdc->msg_proc_thr == NULL) {
557 		cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread",
558 		    instance);
559 		return (DDI_FAILURE);
560 	}
561 
562 	vdc->initialized |= VDC_THREAD;
563 
564 	atomic_inc_32(&vdc_instance_count);
565 
566 	/*
567 	 * Once the handshake is complete, we can use the DRing to send
568 	 * requests to the vDisk server to calculate the geometry and
569 	 * VTOC of the "disk"
570 	 */
571 	status = vdc_setup_disk_layout(vdc);
572 	if (status != 0) {
573 		DMSG(vdc, 0, "[%d] Failed to discover disk layout (err%d)",
574 			vdc->instance, status);
575 		goto return_status;
576 	}
577 
578 	/*
579 	 * Now that we have the device info we can create the
580 	 * device nodes and properties
581 	 */
582 	status = vdc_create_device_nodes(vdc);
583 	if (status) {
584 		DMSG(vdc, 0, "[%d] Failed to create device nodes",
585 				instance);
586 		goto return_status;
587 	}
588 	status = vdc_create_device_nodes_props(vdc);
589 	if (status) {
590 		DMSG(vdc, 0, "[%d] Failed to create device nodes"
591 				" properties (%d)", instance, status);
592 		goto return_status;
593 	}
594 
595 	/*
596 	 * Setup devid
597 	 */
598 	if (vdc_setup_devid(vdc)) {
599 		DMSG(vdc, 0, "[%d] No device id available\n", instance);
600 	}
601 
602 	ddi_report_dev(dip);
603 	vdc->lifecycle	= VDC_LC_ONLINE;
604 	DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance);
605 
606 return_status:
607 	DMSG(vdc, 0, "[%d] Attach completed\n", instance);
608 	return (status);
609 }
610 
611 static int
612 vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
613 {
614 	int	status;
615 
616 	switch (cmd) {
617 	case DDI_ATTACH:
618 		if ((status = vdc_do_attach(dip)) != 0)
619 			(void) vdc_detach(dip, DDI_DETACH);
620 		return (status);
621 	case DDI_RESUME:
622 		/* nothing to do for this non-device */
623 		return (DDI_SUCCESS);
624 	default:
625 		return (DDI_FAILURE);
626 	}
627 }
628 
629 static int
630 vdc_do_ldc_init(vdc_t *vdc)
631 {
632 	int			status = 0;
633 	ldc_status_t		ldc_state;
634 	ldc_attr_t		ldc_attr;
635 	uint64_t		ldc_id = 0;
636 	dev_info_t		*dip = NULL;
637 
638 	ASSERT(vdc != NULL);
639 
640 	dip = vdc->dip;
641 	vdc->initialized |= VDC_LDC;
642 
643 	if ((status = vdc_get_ldc_id(dip, &ldc_id)) != 0) {
644 		DMSG(vdc, 0, "[%d] Failed to get LDC channel ID property",
645 				vdc->instance);
646 		return (EIO);
647 	}
648 	vdc->ldc_id = ldc_id;
649 
650 	ldc_attr.devclass = LDC_DEV_BLK;
651 	ldc_attr.instance = vdc->instance;
652 	ldc_attr.mode = LDC_MODE_UNRELIABLE;	/* unreliable transport */
653 	ldc_attr.mtu = VD_LDC_MTU;
654 
655 	if ((vdc->initialized & VDC_LDC_INIT) == 0) {
656 		status = ldc_init(ldc_id, &ldc_attr, &vdc->ldc_handle);
657 		if (status != 0) {
658 			DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d",
659 					vdc->instance, ldc_id, status);
660 			return (status);
661 		}
662 		vdc->initialized |= VDC_LDC_INIT;
663 	}
664 	status = ldc_status(vdc->ldc_handle, &ldc_state);
665 	if (status != 0) {
666 		DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]",
667 				vdc->instance, status);
668 		return (status);
669 	}
670 	vdc->ldc_state = ldc_state;
671 
672 	if ((vdc->initialized & VDC_LDC_CB) == 0) {
673 		status = ldc_reg_callback(vdc->ldc_handle, vdc_handle_cb,
674 		    (caddr_t)vdc);
675 		if (status != 0) {
676 			DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)",
677 					vdc->instance, status);
678 			return (status);
679 		}
680 		vdc->initialized |= VDC_LDC_CB;
681 	}
682 
683 	vdc->initialized |= VDC_LDC;
684 
685 	/*
686 	 * At this stage we have initialised LDC, we will now try and open
687 	 * the connection.
688 	 */
689 	if (vdc->ldc_state == LDC_INIT) {
690 		status = ldc_open(vdc->ldc_handle);
691 		if (status != 0) {
692 			DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d",
693 					vdc->instance, vdc->ldc_id, status);
694 			return (status);
695 		}
696 		vdc->initialized |= VDC_LDC_OPEN;
697 	}
698 
699 	return (status);
700 }
701 
702 static int
703 vdc_start_ldc_connection(vdc_t *vdc)
704 {
705 	int		status = 0;
706 
707 	ASSERT(vdc != NULL);
708 
709 	ASSERT(MUTEX_HELD(&vdc->lock));
710 
711 	status = vdc_do_ldc_up(vdc);
712 
713 	DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance);
714 
715 	return (status);
716 }
717 
718 static int
719 vdc_stop_ldc_connection(vdc_t *vdcp)
720 {
721 	int	status;
722 
723 	DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n",
724 		vdcp->state);
725 
726 	status = ldc_down(vdcp->ldc_handle);
727 	DMSG(vdcp, 0, "ldc_down() = %d\n", status);
728 
729 	vdcp->initialized &= ~VDC_HANDSHAKE;
730 	DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized);
731 
732 	return (status);
733 }
734 
735 static int
736 vdc_create_device_nodes_efi(vdc_t *vdc)
737 {
738 	ddi_remove_minor_node(vdc->dip, "h");
739 	ddi_remove_minor_node(vdc->dip, "h,raw");
740 
741 	if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK,
742 		VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
743 		DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
744 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'",
745 		    vdc->instance);
746 		return (EIO);
747 	}
748 
749 	/* if any device node is created we set this flag */
750 	vdc->initialized |= VDC_MINOR;
751 
752 	if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR,
753 		VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
754 		DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
755 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'",
756 		    vdc->instance);
757 		return (EIO);
758 	}
759 
760 	return (0);
761 }
762 
763 static int
764 vdc_create_device_nodes_vtoc(vdc_t *vdc)
765 {
766 	ddi_remove_minor_node(vdc->dip, "wd");
767 	ddi_remove_minor_node(vdc->dip, "wd,raw");
768 
769 	if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK,
770 		VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
771 		DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
772 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'",
773 		    vdc->instance);
774 		return (EIO);
775 	}
776 
777 	/* if any device node is created we set this flag */
778 	vdc->initialized |= VDC_MINOR;
779 
780 	if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR,
781 		VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
782 		DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
783 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'",
784 		    vdc->instance);
785 		return (EIO);
786 	}
787 
788 	return (0);
789 }
790 
791 /*
792  * Function:
793  *	vdc_create_device_nodes
794  *
795  * Description:
796  *	This function creates the block and character device nodes under
797  *	/devices along with the node properties. It is called as part of
798  *	the attach(9E) of the instance during the handshake with vds after
799  *	vds has sent the attributes to vdc.
800  *
801  *	If the device is of type VD_DISK_TYPE_SLICE then the minor node
802  *	of 2 is used in keeping with the Solaris convention that slice 2
803  *	refers to a whole disk. Slices start at 'a'
804  *
805  * Parameters:
806  *	vdc 		- soft state pointer
807  *
808  * Return Values
809  *	0		- Success
810  *	EIO		- Failed to create node
811  *	EINVAL		- Unknown type of disk exported
812  */
813 static int
814 vdc_create_device_nodes(vdc_t *vdc)
815 {
816 	char		name[sizeof ("s,raw")];
817 	dev_info_t	*dip = NULL;
818 	int		instance, status;
819 	int		num_slices = 1;
820 	int		i;
821 
822 	ASSERT(vdc != NULL);
823 
824 	instance = vdc->instance;
825 	dip = vdc->dip;
826 
827 	switch (vdc->vdisk_type) {
828 	case VD_DISK_TYPE_DISK:
829 		num_slices = V_NUMPAR;
830 		break;
831 	case VD_DISK_TYPE_SLICE:
832 		num_slices = 1;
833 		break;
834 	case VD_DISK_TYPE_UNK:
835 	default:
836 		return (EINVAL);
837 	}
838 
839 	/*
840 	 * Minor nodes are different for EFI disks: EFI disks do not have
841 	 * a minor node 'g' for the minor number corresponding to slice
842 	 * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd'
843 	 * representing the whole disk.
844 	 */
845 	for (i = 0; i < num_slices; i++) {
846 
847 		if (i == VD_EFI_WD_SLICE) {
848 			if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
849 				status = vdc_create_device_nodes_efi(vdc);
850 			else
851 				status = vdc_create_device_nodes_vtoc(vdc);
852 			if (status != 0)
853 				return (status);
854 			continue;
855 		}
856 
857 		(void) snprintf(name, sizeof (name), "%c", 'a' + i);
858 		if (ddi_create_minor_node(dip, name, S_IFBLK,
859 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
860 			cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'",
861 				instance, name);
862 			return (EIO);
863 		}
864 
865 		/* if any device node is created we set this flag */
866 		vdc->initialized |= VDC_MINOR;
867 
868 		(void) snprintf(name, sizeof (name), "%c%s",
869 			'a' + i, ",raw");
870 		if (ddi_create_minor_node(dip, name, S_IFCHR,
871 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
872 			cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'",
873 				instance, name);
874 			return (EIO);
875 		}
876 	}
877 
878 	return (0);
879 }
880 
881 /*
882  * Function:
883  *	vdc_create_device_nodes_props
884  *
885  * Description:
886  *	This function creates the block and character device nodes under
887  *	/devices along with the node properties. It is called as part of
888  *	the attach(9E) of the instance during the handshake with vds after
889  *	vds has sent the attributes to vdc.
890  *
891  * Parameters:
892  *	vdc 		- soft state pointer
893  *
894  * Return Values
895  *	0		- Success
896  *	EIO		- Failed to create device node property
897  *	EINVAL		- Unknown type of disk exported
898  */
899 static int
900 vdc_create_device_nodes_props(vdc_t *vdc)
901 {
902 	dev_info_t	*dip = NULL;
903 	int		instance;
904 	int		num_slices = 1;
905 	int64_t		size = 0;
906 	dev_t		dev;
907 	int		rv;
908 	int		i;
909 
910 	ASSERT(vdc != NULL);
911 
912 	instance = vdc->instance;
913 	dip = vdc->dip;
914 
915 	if ((vdc->vtoc == NULL) || (vdc->vtoc->v_sanity != VTOC_SANE)) {
916 		DMSG(vdc, 0, "![%d] Could not create device node property."
917 				" No VTOC available", instance);
918 		return (ENXIO);
919 	}
920 
921 	switch (vdc->vdisk_type) {
922 	case VD_DISK_TYPE_DISK:
923 		num_slices = V_NUMPAR;
924 		break;
925 	case VD_DISK_TYPE_SLICE:
926 		num_slices = 1;
927 		break;
928 	case VD_DISK_TYPE_UNK:
929 	default:
930 		return (EINVAL);
931 	}
932 
933 	for (i = 0; i < num_slices; i++) {
934 		dev = makedevice(ddi_driver_major(dip),
935 			VD_MAKE_DEV(instance, i));
936 
937 		size = vdc->vtoc->v_part[i].p_size * vdc->vtoc->v_sectorsz;
938 		DMSG(vdc, 0, "[%d] sz %ld (%ld Mb)  p_size %lx\n",
939 				instance, size, size / (1024 * 1024),
940 				vdc->vtoc->v_part[i].p_size);
941 
942 		rv = ddi_prop_update_int64(dev, dip, VDC_SIZE_PROP_NAME, size);
943 		if (rv != DDI_PROP_SUCCESS) {
944 			cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop of [%ld]",
945 				instance, VDC_SIZE_PROP_NAME, size);
946 			return (EIO);
947 		}
948 
949 		rv = ddi_prop_update_int64(dev, dip, VDC_NBLOCKS_PROP_NAME,
950 			lbtodb(size));
951 		if (rv != DDI_PROP_SUCCESS) {
952 			cmn_err(CE_NOTE, "[%d] Couldn't add '%s' prop [%llu]",
953 				instance, VDC_NBLOCKS_PROP_NAME, lbtodb(size));
954 			return (EIO);
955 		}
956 	}
957 
958 	return (0);
959 }
960 
961 static int
962 vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred)
963 {
964 	_NOTE(ARGUNUSED(cred))
965 
966 	int		instance;
967 	vdc_t		*vdc;
968 
969 	ASSERT(dev != NULL);
970 	instance = VDCUNIT(*dev);
971 
972 	if ((otyp != OTYP_CHR) && (otyp != OTYP_BLK))
973 		return (EINVAL);
974 
975 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
976 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
977 		return (ENXIO);
978 	}
979 
980 	DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n",
981 			getminor(*dev), flag, otyp);
982 
983 	mutex_enter(&vdc->lock);
984 	vdc->open_count++;
985 	mutex_exit(&vdc->lock);
986 
987 	return (0);
988 }
989 
990 static int
991 vdc_close(dev_t dev, int flag, int otyp, cred_t *cred)
992 {
993 	_NOTE(ARGUNUSED(cred))
994 
995 	int	instance;
996 	vdc_t	*vdc;
997 
998 	instance = VDCUNIT(dev);
999 
1000 	if ((otyp != OTYP_CHR) && (otyp != OTYP_BLK))
1001 		return (EINVAL);
1002 
1003 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1004 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
1005 		return (ENXIO);
1006 	}
1007 
1008 	DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp);
1009 	if (vdc->dkio_flush_pending) {
1010 		DMSG(vdc, 0,
1011 		    "[%d] Cannot detach: %d outstanding DKIO flushes\n",
1012 		    instance, vdc->dkio_flush_pending);
1013 		return (EBUSY);
1014 	}
1015 
1016 	/*
1017 	 * Should not need the mutex here, since the framework should protect
1018 	 * against more opens on this device, but just in case.
1019 	 */
1020 	mutex_enter(&vdc->lock);
1021 	vdc->open_count--;
1022 	mutex_exit(&vdc->lock);
1023 
1024 	return (0);
1025 }
1026 
1027 static int
1028 vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
1029 {
1030 	_NOTE(ARGUNUSED(credp))
1031 	_NOTE(ARGUNUSED(rvalp))
1032 
1033 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode));
1034 }
1035 
1036 static int
1037 vdc_print(dev_t dev, char *str)
1038 {
1039 	cmn_err(CE_NOTE, "vdc%d:  %s", VDCUNIT(dev), str);
1040 	return (0);
1041 }
1042 
1043 static int
1044 vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
1045 {
1046 	int	rv;
1047 	size_t	nbytes = nblk * DEV_BSIZE;
1048 	int	instance = VDCUNIT(dev);
1049 	vdc_t	*vdc = NULL;
1050 
1051 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1052 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
1053 		return (ENXIO);
1054 	}
1055 
1056 	DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n",
1057 	    instance, nbytes, blkno, (void *)addr);
1058 	rv = vdc_send_request(vdc, VD_OP_BWRITE, addr, nbytes,
1059 	    VDCPART(dev), blkno, CB_STRATEGY, 0, VIO_write_dir);
1060 	if (rv) {
1061 		DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv);
1062 		return (rv);
1063 	}
1064 
1065 	if (ddi_in_panic())
1066 		(void) vdc_drain_response(vdc);
1067 
1068 	DMSG(vdc, 0, "[%d] End\n", instance);
1069 
1070 	return (0);
1071 }
1072 
1073 /* -------------------------------------------------------------------------- */
1074 
1075 /*
1076  * Disk access routines
1077  *
1078  */
1079 
1080 /*
1081  * vdc_strategy()
1082  *
1083  * Return Value:
1084  *	0:	As per strategy(9E), the strategy() function must return 0
1085  *		[ bioerror(9f) sets b_flags to the proper error code ]
1086  */
1087 static int
1088 vdc_strategy(struct buf *buf)
1089 {
1090 	int	rv = -1;
1091 	vdc_t	*vdc = NULL;
1092 	int	instance = VDCUNIT(buf->b_edev);
1093 	int	op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE;
1094 
1095 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1096 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
1097 		bioerror(buf, ENXIO);
1098 		biodone(buf);
1099 		return (0);
1100 	}
1101 
1102 	DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n",
1103 	    instance, (buf->b_flags & B_READ) ? "Read" : "Write",
1104 	    buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr);
1105 	DTRACE_IO2(vstart, buf_t *, buf, vdc_t *, vdc);
1106 
1107 	bp_mapin(buf);
1108 
1109 	rv = vdc_send_request(vdc, op, (caddr_t)buf->b_un.b_addr,
1110 	    buf->b_bcount, VDCPART(buf->b_edev), buf->b_lblkno,
1111 	    CB_STRATEGY, buf, (op == VD_OP_BREAD) ? VIO_read_dir :
1112 	    VIO_write_dir);
1113 
1114 	/*
1115 	 * If the request was successfully sent, the strategy call returns and
1116 	 * the ACK handler calls the bioxxx functions when the vDisk server is
1117 	 * done.
1118 	 */
1119 	if (rv) {
1120 		DMSG(vdc, 0, "Failed to read/write (err=%d)\n", rv);
1121 		bioerror(buf, rv);
1122 		biodone(buf);
1123 	}
1124 
1125 	return (0);
1126 }
1127 
1128 /*
1129  * Function:
1130  *	vdc_min
1131  *
1132  * Description:
1133  *	Routine to limit the size of a data transfer. Used in
1134  *	conjunction with physio(9F).
1135  *
1136  * Arguments:
1137  *	bp - pointer to the indicated buf(9S) struct.
1138  *
1139  */
1140 static void
1141 vdc_min(struct buf *bufp)
1142 {
1143 	vdc_t	*vdc = NULL;
1144 	int	instance = VDCUNIT(bufp->b_edev);
1145 
1146 	vdc = ddi_get_soft_state(vdc_state, instance);
1147 	VERIFY(vdc != NULL);
1148 
1149 	if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->block_size)) {
1150 		bufp->b_bcount = vdc->max_xfer_sz * vdc->block_size;
1151 	}
1152 }
1153 
1154 static int
1155 vdc_read(dev_t dev, struct uio *uio, cred_t *cred)
1156 {
1157 	_NOTE(ARGUNUSED(cred))
1158 
1159 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
1160 	return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio));
1161 }
1162 
1163 static int
1164 vdc_write(dev_t dev, struct uio *uio, cred_t *cred)
1165 {
1166 	_NOTE(ARGUNUSED(cred))
1167 
1168 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
1169 	return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio));
1170 }
1171 
1172 static int
1173 vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred)
1174 {
1175 	_NOTE(ARGUNUSED(cred))
1176 
1177 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
1178 	return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio));
1179 }
1180 
1181 static int
1182 vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred)
1183 {
1184 	_NOTE(ARGUNUSED(cred))
1185 
1186 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
1187 	return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio));
1188 }
1189 
1190 
1191 /* -------------------------------------------------------------------------- */
1192 
1193 /*
1194  * Handshake support
1195  */
1196 
1197 
1198 /*
1199  * Function:
1200  *	vdc_init_ver_negotiation()
1201  *
1202  * Description:
1203  *
1204  * Arguments:
1205  *	vdc	- soft state pointer for this instance of the device driver.
1206  *
1207  * Return Code:
1208  *	0	- Success
1209  */
1210 static int
1211 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver)
1212 {
1213 	vio_ver_msg_t	pkt;
1214 	size_t		msglen = sizeof (pkt);
1215 	int		status = -1;
1216 
1217 	ASSERT(vdc != NULL);
1218 	ASSERT(mutex_owned(&vdc->lock));
1219 
1220 	DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance);
1221 
1222 	/*
1223 	 * set the Session ID to a unique value
1224 	 * (the lower 32 bits of the clock tick)
1225 	 */
1226 	vdc->session_id = ((uint32_t)gettick() & 0xffffffff);
1227 	DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id);
1228 
1229 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
1230 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
1231 	pkt.tag.vio_subtype_env = VIO_VER_INFO;
1232 	pkt.tag.vio_sid = vdc->session_id;
1233 	pkt.dev_class = VDEV_DISK;
1234 	pkt.ver_major = ver.major;
1235 	pkt.ver_minor = ver.minor;
1236 
1237 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
1238 	DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n",
1239 	    vdc->instance, status);
1240 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
1241 		DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: "
1242 				"id(%lx) rv(%d) size(%ld)",
1243 				vdc->instance, vdc->ldc_handle,
1244 				status, msglen);
1245 		if (msglen != sizeof (vio_ver_msg_t))
1246 			status = ENOMSG;
1247 	}
1248 
1249 	return (status);
1250 }
1251 
1252 /*
1253  * Function:
1254  *	vdc_ver_negotiation()
1255  *
1256  * Description:
1257  *
1258  * Arguments:
1259  *	vdcp	- soft state pointer for this instance of the device driver.
1260  *
1261  * Return Code:
1262  *	0	- Success
1263  */
1264 static int
1265 vdc_ver_negotiation(vdc_t *vdcp)
1266 {
1267 	vio_msg_t vio_msg;
1268 	int status;
1269 
1270 	if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0]))
1271 		return (status);
1272 
1273 	/* release lock and wait for response */
1274 	mutex_exit(&vdcp->lock);
1275 	status = vdc_wait_for_response(vdcp, &vio_msg);
1276 	mutex_enter(&vdcp->lock);
1277 	if (status) {
1278 		DMSG(vdcp, 0,
1279 		    "[%d] Failed waiting for Ver negotiation response, rv(%d)",
1280 		    vdcp->instance, status);
1281 		return (status);
1282 	}
1283 
1284 	/* check type and sub_type ... */
1285 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
1286 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
1287 		DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n",
1288 				vdcp->instance);
1289 		return (EPROTO);
1290 	}
1291 
1292 	return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg));
1293 }
1294 
1295 /*
1296  * Function:
1297  *	vdc_init_attr_negotiation()
1298  *
1299  * Description:
1300  *
1301  * Arguments:
1302  *	vdc	- soft state pointer for this instance of the device driver.
1303  *
1304  * Return Code:
1305  *	0	- Success
1306  */
1307 static int
1308 vdc_init_attr_negotiation(vdc_t *vdc)
1309 {
1310 	vd_attr_msg_t	pkt;
1311 	size_t		msglen = sizeof (pkt);
1312 	int		status;
1313 
1314 	ASSERT(vdc != NULL);
1315 	ASSERT(mutex_owned(&vdc->lock));
1316 
1317 	DMSG(vdc, 0, "[%d] entered\n", vdc->instance);
1318 
1319 	/* fill in tag */
1320 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
1321 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
1322 	pkt.tag.vio_subtype_env = VIO_ATTR_INFO;
1323 	pkt.tag.vio_sid = vdc->session_id;
1324 	/* fill in payload */
1325 	pkt.max_xfer_sz = vdc->max_xfer_sz;
1326 	pkt.vdisk_block_size = vdc->block_size;
1327 	pkt.xfer_mode = VIO_DRING_MODE;
1328 	pkt.operations = 0;	/* server will set bits of valid operations */
1329 	pkt.vdisk_type = 0;	/* server will set to valid device type */
1330 	pkt.vdisk_size = 0;	/* server will set to valid size */
1331 
1332 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
1333 	DMSG(vdc, 0, "Attr info sent (status = %d)\n", status);
1334 
1335 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
1336 		DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: "
1337 				"id(%lx) rv(%d) size(%ld)",
1338 				vdc->instance, vdc->ldc_handle,
1339 				status, msglen);
1340 		if (msglen != sizeof (vio_ver_msg_t))
1341 			status = ENOMSG;
1342 	}
1343 
1344 	return (status);
1345 }
1346 
1347 /*
1348  * Function:
1349  *	vdc_attr_negotiation()
1350  *
1351  * Description:
1352  *
1353  * Arguments:
1354  *	vdc	- soft state pointer for this instance of the device driver.
1355  *
1356  * Return Code:
1357  *	0	- Success
1358  */
1359 static int
1360 vdc_attr_negotiation(vdc_t *vdcp)
1361 {
1362 	int status;
1363 	vio_msg_t vio_msg;
1364 
1365 	if (status = vdc_init_attr_negotiation(vdcp))
1366 		return (status);
1367 
1368 	/* release lock and wait for response */
1369 	mutex_exit(&vdcp->lock);
1370 	status = vdc_wait_for_response(vdcp, &vio_msg);
1371 	mutex_enter(&vdcp->lock);
1372 	if (status) {
1373 		DMSG(vdcp, 0,
1374 		    "[%d] Failed waiting for Attr negotiation response, rv(%d)",
1375 		    vdcp->instance, status);
1376 		return (status);
1377 	}
1378 
1379 	/* check type and sub_type ... */
1380 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
1381 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
1382 		DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n",
1383 				vdcp->instance);
1384 		return (EPROTO);
1385 	}
1386 
1387 	return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg));
1388 }
1389 
1390 
1391 /*
1392  * Function:
1393  *	vdc_init_dring_negotiate()
1394  *
1395  * Description:
1396  *
1397  * Arguments:
1398  *	vdc	- soft state pointer for this instance of the device driver.
1399  *
1400  * Return Code:
1401  *	0	- Success
1402  */
1403 static int
1404 vdc_init_dring_negotiate(vdc_t *vdc)
1405 {
1406 	vio_dring_reg_msg_t	pkt;
1407 	size_t			msglen = sizeof (pkt);
1408 	int			status = -1;
1409 	int			retry;
1410 	int			nretries = 10;
1411 
1412 	ASSERT(vdc != NULL);
1413 	ASSERT(mutex_owned(&vdc->lock));
1414 
1415 	for (retry = 0; retry < nretries; retry++) {
1416 		status = vdc_init_descriptor_ring(vdc);
1417 		if (status != EAGAIN)
1418 			break;
1419 		drv_usecwait(vdc_min_timeout_ldc);
1420 	}
1421 
1422 	if (status != 0) {
1423 		DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n",
1424 				vdc->instance, status);
1425 		return (status);
1426 	}
1427 
1428 	DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n",
1429 			vdc->instance, status);
1430 
1431 	/* fill in tag */
1432 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
1433 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
1434 	pkt.tag.vio_subtype_env = VIO_DRING_REG;
1435 	pkt.tag.vio_sid = vdc->session_id;
1436 	/* fill in payload */
1437 	pkt.dring_ident = 0;
1438 	pkt.num_descriptors = vdc->dring_len;
1439 	pkt.descriptor_size = vdc->dring_entry_size;
1440 	pkt.options = (VIO_TX_DRING | VIO_RX_DRING);
1441 	pkt.ncookies = vdc->dring_cookie_count;
1442 	pkt.cookie[0] = vdc->dring_cookie[0];	/* for now just one cookie */
1443 
1444 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
1445 	if (status != 0) {
1446 		DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)",
1447 				vdc->instance, status);
1448 	}
1449 
1450 	return (status);
1451 }
1452 
1453 
1454 /*
1455  * Function:
1456  *	vdc_dring_negotiation()
1457  *
1458  * Description:
1459  *
1460  * Arguments:
1461  *	vdc	- soft state pointer for this instance of the device driver.
1462  *
1463  * Return Code:
1464  *	0	- Success
1465  */
1466 static int
1467 vdc_dring_negotiation(vdc_t *vdcp)
1468 {
1469 	int status;
1470 	vio_msg_t vio_msg;
1471 
1472 	if (status = vdc_init_dring_negotiate(vdcp))
1473 		return (status);
1474 
1475 	/* release lock and wait for response */
1476 	mutex_exit(&vdcp->lock);
1477 	status = vdc_wait_for_response(vdcp, &vio_msg);
1478 	mutex_enter(&vdcp->lock);
1479 	if (status) {
1480 		DMSG(vdcp, 0,
1481 		    "[%d] Failed waiting for Dring negotiation response,"
1482 		    " rv(%d)", vdcp->instance, status);
1483 		return (status);
1484 	}
1485 
1486 	/* check type and sub_type ... */
1487 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
1488 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
1489 		DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n",
1490 				vdcp->instance);
1491 		return (EPROTO);
1492 	}
1493 
1494 	return (vdc_handle_dring_reg_msg(vdcp,
1495 		    (vio_dring_reg_msg_t *)&vio_msg));
1496 }
1497 
1498 
1499 /*
1500  * Function:
1501  *	vdc_send_rdx()
1502  *
1503  * Description:
1504  *
1505  * Arguments:
1506  *	vdc	- soft state pointer for this instance of the device driver.
1507  *
1508  * Return Code:
1509  *	0	- Success
1510  */
1511 static int
1512 vdc_send_rdx(vdc_t *vdcp)
1513 {
1514 	vio_msg_t	msg;
1515 	size_t		msglen = sizeof (vio_msg_t);
1516 	int		status;
1517 
1518 	/*
1519 	 * Send an RDX message to vds to indicate we are ready
1520 	 * to send data
1521 	 */
1522 	msg.tag.vio_msgtype = VIO_TYPE_CTRL;
1523 	msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
1524 	msg.tag.vio_subtype_env = VIO_RDX;
1525 	msg.tag.vio_sid = vdcp->session_id;
1526 	status = vdc_send(vdcp, (caddr_t)&msg, &msglen);
1527 	if (status != 0) {
1528 		DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)",
1529 		    vdcp->instance, status);
1530 	}
1531 
1532 	return (status);
1533 }
1534 
1535 /*
1536  * Function:
1537  *	vdc_handle_rdx()
1538  *
1539  * Description:
1540  *
1541  * Arguments:
1542  *	vdc	- soft state pointer for this instance of the device driver.
1543  *	msgp	- received msg
1544  *
1545  * Return Code:
1546  *	0	- Success
1547  */
1548 static int
1549 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp)
1550 {
1551 	_NOTE(ARGUNUSED(vdcp))
1552 	_NOTE(ARGUNUSED(msgp))
1553 
1554 	ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL);
1555 	ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK);
1556 	ASSERT(msgp->tag.vio_subtype_env == VIO_RDX);
1557 
1558 	DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance);
1559 
1560 	return (0);
1561 }
1562 
1563 /*
1564  * Function:
1565  *	vdc_rdx_exchange()
1566  *
1567  * Description:
1568  *
1569  * Arguments:
1570  *	vdc	- soft state pointer for this instance of the device driver.
1571  *
1572  * Return Code:
1573  *	0	- Success
1574  */
1575 static int
1576 vdc_rdx_exchange(vdc_t *vdcp)
1577 {
1578 	int status;
1579 	vio_msg_t vio_msg;
1580 
1581 	if (status = vdc_send_rdx(vdcp))
1582 		return (status);
1583 
1584 	/* release lock and wait for response */
1585 	mutex_exit(&vdcp->lock);
1586 	status = vdc_wait_for_response(vdcp, &vio_msg);
1587 	mutex_enter(&vdcp->lock);
1588 	if (status) {
1589 		DMSG(vdcp, 0,
1590 		    "[%d] Failed waiting for RDX response,"
1591 		    " rv(%d)", vdcp->instance, status);
1592 		return (status);
1593 	}
1594 
1595 	/* check type and sub_type ... */
1596 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
1597 	    vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) {
1598 		DMSG(vdcp, 0, "[%d] Invalid RDX response\n",
1599 				vdcp->instance);
1600 		return (EPROTO);
1601 	}
1602 
1603 	return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg));
1604 }
1605 
1606 
1607 /* -------------------------------------------------------------------------- */
1608 
1609 /*
1610  * LDC helper routines
1611  */
1612 
1613 static int
1614 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp)
1615 {
1616 	int		status;
1617 	boolean_t	q_has_pkts = B_FALSE;
1618 	int		delay_time;
1619 	size_t		len;
1620 
1621 	mutex_enter(&vdc->read_lock);
1622 
1623 	if (vdc->read_state == VDC_READ_IDLE)
1624 		vdc->read_state = VDC_READ_WAITING;
1625 
1626 	while (vdc->read_state != VDC_READ_PENDING) {
1627 
1628 		/* detect if the connection has been reset */
1629 		if (vdc->read_state == VDC_READ_RESET) {
1630 			status = ECONNRESET;
1631 			goto done;
1632 		}
1633 
1634 		cv_wait(&vdc->read_cv, &vdc->read_lock);
1635 	}
1636 
1637 	/*
1638 	 * Until we get a blocking ldc read we have to retry
1639 	 * until the entire LDC message has arrived before
1640 	 * ldc_read() will succeed. Note we also bail out if
1641 	 * the chanel is reset or goes away.
1642 	 */
1643 	delay_time = vdc_ldc_read_init_delay;
1644 loop:
1645 	len = *nbytesp;
1646 	status = ldc_read(vdc->ldc_handle, (caddr_t)msgp, &len);
1647 	switch (status) {
1648 	case EAGAIN:
1649 		delay_time *= 2;
1650 		if (delay_time >= vdc_ldc_read_max_delay)
1651 			delay_time = vdc_ldc_read_max_delay;
1652 		delay(delay_time);
1653 		goto loop;
1654 
1655 	case 0:
1656 		if (len == 0) {
1657 			DMSG(vdc, 0, "[%d] ldc_read returned 0 bytes with "
1658 				"no error!\n", vdc->instance);
1659 			goto loop;
1660 		}
1661 
1662 		*nbytesp = len;
1663 
1664 		/*
1665 		 * If there are pending messages, leave the
1666 		 * read state as pending. Otherwise, set the state
1667 		 * back to idle.
1668 		 */
1669 		status = ldc_chkq(vdc->ldc_handle, &q_has_pkts);
1670 		if (status == 0 && !q_has_pkts)
1671 			vdc->read_state = VDC_READ_IDLE;
1672 
1673 		break;
1674 	default:
1675 		DMSG(vdc, 0, "ldc_read returned %d\n", status);
1676 		break;
1677 	}
1678 
1679 done:
1680 	mutex_exit(&vdc->read_lock);
1681 
1682 	return (status);
1683 }
1684 
1685 
1686 
1687 #ifdef DEBUG
1688 void
1689 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg)
1690 {
1691 	char *ms, *ss, *ses;
1692 	switch (msg->tag.vio_msgtype) {
1693 #define	Q(_s)	case _s : ms = #_s; break;
1694 	Q(VIO_TYPE_CTRL)
1695 	Q(VIO_TYPE_DATA)
1696 	Q(VIO_TYPE_ERR)
1697 #undef Q
1698 	default: ms = "unknown"; break;
1699 	}
1700 
1701 	switch (msg->tag.vio_subtype) {
1702 #define	Q(_s)	case _s : ss = #_s; break;
1703 	Q(VIO_SUBTYPE_INFO)
1704 	Q(VIO_SUBTYPE_ACK)
1705 	Q(VIO_SUBTYPE_NACK)
1706 #undef Q
1707 	default: ss = "unknown"; break;
1708 	}
1709 
1710 	switch (msg->tag.vio_subtype_env) {
1711 #define	Q(_s)	case _s : ses = #_s; break;
1712 	Q(VIO_VER_INFO)
1713 	Q(VIO_ATTR_INFO)
1714 	Q(VIO_DRING_REG)
1715 	Q(VIO_DRING_UNREG)
1716 	Q(VIO_RDX)
1717 	Q(VIO_PKT_DATA)
1718 	Q(VIO_DESC_DATA)
1719 	Q(VIO_DRING_DATA)
1720 #undef Q
1721 	default: ses = "unknown"; break;
1722 	}
1723 
1724 	DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n",
1725 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
1726 	    msg->tag.vio_subtype_env, ms, ss, ses);
1727 }
1728 #endif
1729 
1730 /*
1731  * Function:
1732  *	vdc_send()
1733  *
1734  * Description:
1735  *	The function encapsulates the call to write a message using LDC.
1736  *	If LDC indicates that the call failed due to the queue being full,
1737  *	we retry the ldc_write() [ up to 'vdc_retries' time ], otherwise
1738  *	we return the error returned by LDC.
1739  *
1740  * Arguments:
1741  *	ldc_handle	- LDC handle for the channel this instance of vdc uses
1742  *	pkt		- address of LDC message to be sent
1743  *	msglen		- the size of the message being sent. When the function
1744  *			  returns, this contains the number of bytes written.
1745  *
1746  * Return Code:
1747  *	0		- Success.
1748  *	EINVAL		- pkt or msglen were NULL
1749  *	ECONNRESET	- The connection was not up.
1750  *	EWOULDBLOCK	- LDC queue is full
1751  *	xxx		- other error codes returned by ldc_write
1752  */
1753 static int
1754 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen)
1755 {
1756 	size_t	size = 0;
1757 	int	status = 0;
1758 	clock_t delay_ticks;
1759 
1760 	ASSERT(vdc != NULL);
1761 	ASSERT(mutex_owned(&vdc->lock));
1762 	ASSERT(msglen != NULL);
1763 	ASSERT(*msglen != 0);
1764 
1765 #ifdef DEBUG
1766 	vdc_decode_tag(vdc, (vio_msg_t *)pkt);
1767 #endif
1768 	/*
1769 	 * Wait indefinitely to send if channel
1770 	 * is busy, but bail out if we succeed or
1771 	 * if the channel closes or is reset.
1772 	 */
1773 	delay_ticks = vdc_hz_min_ldc_delay;
1774 	do {
1775 		size = *msglen;
1776 		status = ldc_write(vdc->ldc_handle, pkt, &size);
1777 		if (status == EWOULDBLOCK) {
1778 			delay(delay_ticks);
1779 			/* geometric backoff */
1780 			delay_ticks *= 2;
1781 			if (delay_ticks > vdc_hz_max_ldc_delay)
1782 				delay_ticks = vdc_hz_max_ldc_delay;
1783 		}
1784 	} while (status == EWOULDBLOCK);
1785 
1786 	/* if LDC had serious issues --- reset vdc state */
1787 	if (status == EIO || status == ECONNRESET) {
1788 		/* LDC had serious issues --- reset vdc state */
1789 		mutex_enter(&vdc->read_lock);
1790 		if ((vdc->read_state == VDC_READ_WAITING) ||
1791 		    (vdc->read_state == VDC_READ_RESET))
1792 			cv_signal(&vdc->read_cv);
1793 		vdc->read_state = VDC_READ_RESET;
1794 		mutex_exit(&vdc->read_lock);
1795 
1796 		/* wake up any waiters in the reset thread */
1797 		if (vdc->state == VDC_STATE_INIT_WAITING) {
1798 			DMSG(vdc, 0, "[%d] write reset - "
1799 			    "vdc is resetting ..\n", vdc->instance);
1800 			vdc->state = VDC_STATE_RESETTING;
1801 			cv_signal(&vdc->initwait_cv);
1802 		}
1803 
1804 		return (ECONNRESET);
1805 	}
1806 
1807 	/* return the last size written */
1808 	*msglen = size;
1809 
1810 	return (status);
1811 }
1812 
1813 /*
1814  * Function:
1815  *	vdc_get_ldc_id()
1816  *
1817  * Description:
1818  *	This function gets the 'ldc-id' for this particular instance of vdc.
1819  *	The id returned is the guest domain channel endpoint LDC uses for
1820  *	communication with vds.
1821  *
1822  * Arguments:
1823  *	dip	- dev info pointer for this instance of the device driver.
1824  *	ldc_id	- pointer to variable used to return the 'ldc-id' found.
1825  *
1826  * Return Code:
1827  *	0	- Success.
1828  *	ENOENT	- Expected node or property did not exist.
1829  *	ENXIO	- Unexpected error communicating with MD framework
1830  */
1831 static int
1832 vdc_get_ldc_id(dev_info_t *dip, uint64_t *ldc_id)
1833 {
1834 	int		status = ENOENT;
1835 	char		*node_name = NULL;
1836 	md_t		*mdp = NULL;
1837 	int		num_nodes;
1838 	int		num_vdevs;
1839 	int		num_chans;
1840 	mde_cookie_t	rootnode;
1841 	mde_cookie_t	*listp = NULL;
1842 	mde_cookie_t	*chanp = NULL;
1843 	boolean_t	found_inst = B_FALSE;
1844 	int		listsz;
1845 	int		idx;
1846 	uint64_t	md_inst;
1847 	int		obp_inst;
1848 	int		instance = ddi_get_instance(dip);
1849 
1850 	ASSERT(ldc_id != NULL);
1851 	*ldc_id = 0;
1852 
1853 	/*
1854 	 * Get the OBP instance number for comparison with the MD instance
1855 	 *
1856 	 * The "cfg-handle" property of a vdc node in an MD contains the MD's
1857 	 * notion of "instance", or unique identifier, for that node; OBP
1858 	 * stores the value of the "cfg-handle" MD property as the value of
1859 	 * the "reg" property on the node in the device tree it builds from
1860 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
1861 	 * "reg" property value to uniquely identify this device instance.
1862 	 * If the "reg" property cannot be found, the device tree state is
1863 	 * presumably so broken that there is no point in continuing.
1864 	 */
1865 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) {
1866 		cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG);
1867 		return (ENOENT);
1868 	}
1869 	obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1870 			OBP_REG, -1);
1871 	DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst);
1872 
1873 	/*
1874 	 * We now walk the MD nodes and if an instance of a vdc node matches
1875 	 * the instance got from OBP we get the ldc-id property.
1876 	 */
1877 	if ((mdp = md_get_handle()) == NULL) {
1878 		cmn_err(CE_WARN, "unable to init machine description");
1879 		return (ENXIO);
1880 	}
1881 
1882 	num_nodes = md_node_count(mdp);
1883 	ASSERT(num_nodes > 0);
1884 
1885 	listsz = num_nodes * sizeof (mde_cookie_t);
1886 
1887 	/* allocate memory for nodes */
1888 	listp = kmem_zalloc(listsz, KM_SLEEP);
1889 	chanp = kmem_zalloc(listsz, KM_SLEEP);
1890 
1891 	rootnode = md_root_node(mdp);
1892 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
1893 
1894 	/*
1895 	 * Search for all the virtual devices, we will then check to see which
1896 	 * ones are disk nodes.
1897 	 */
1898 	num_vdevs = md_scan_dag(mdp, rootnode,
1899 			md_find_name(mdp, VDC_MD_VDEV_NAME),
1900 			md_find_name(mdp, "fwd"), listp);
1901 
1902 	if (num_vdevs <= 0) {
1903 		cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME);
1904 		status = ENOENT;
1905 		goto done;
1906 	}
1907 
1908 	DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs);
1909 	for (idx = 0; idx < num_vdevs; idx++) {
1910 		status = md_get_prop_str(mdp, listp[idx], "name", &node_name);
1911 		if ((status != 0) || (node_name == NULL)) {
1912 			cmn_err(CE_NOTE, "Unable to get name of node type '%s'"
1913 					": err %d", VDC_MD_VDEV_NAME, status);
1914 			continue;
1915 		}
1916 
1917 		DMSGX(1, "[%d] Found node '%s'\n", instance, node_name);
1918 		if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) {
1919 			status = md_get_prop_val(mdp, listp[idx],
1920 					VDC_MD_CFG_HDL, &md_inst);
1921 			DMSGX(1, "[%d] vdc inst in MD=%lx\n",
1922 			    instance, md_inst);
1923 			if ((status == 0) && (md_inst == obp_inst)) {
1924 				found_inst = B_TRUE;
1925 				break;
1926 			}
1927 		}
1928 	}
1929 
1930 	if (!found_inst) {
1931 		DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME);
1932 		status = ENOENT;
1933 		goto done;
1934 	}
1935 	DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst);
1936 
1937 	/* get the channels for this node */
1938 	num_chans = md_scan_dag(mdp, listp[idx],
1939 			md_find_name(mdp, VDC_MD_CHAN_NAME),
1940 			md_find_name(mdp, "fwd"), chanp);
1941 
1942 	/* expecting at least one channel */
1943 	if (num_chans <= 0) {
1944 		cmn_err(CE_NOTE, "No '%s' node for '%s' port",
1945 				VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME);
1946 		status = ENOENT;
1947 		goto done;
1948 
1949 	} else if (num_chans != 1) {
1950 		DMSGX(0, "[%d] Expected 1 '%s' node for '%s' port, found %d\n",
1951 			instance, VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME,
1952 			num_chans);
1953 	}
1954 
1955 	/*
1956 	 * We use the first channel found (index 0), irrespective of how
1957 	 * many are there in total.
1958 	 */
1959 	if (md_get_prop_val(mdp, chanp[0], VDC_ID_PROP, ldc_id) != 0) {
1960 		cmn_err(CE_NOTE, "Channel '%s' property not found",
1961 				VDC_ID_PROP);
1962 		status = ENOENT;
1963 	}
1964 
1965 	DMSGX(0, "[%d] LDC id is 0x%lx\n", instance, *ldc_id);
1966 
1967 done:
1968 	if (chanp)
1969 		kmem_free(chanp, listsz);
1970 	if (listp)
1971 		kmem_free(listp, listsz);
1972 
1973 	(void) md_fini_handle(mdp);
1974 
1975 	return (status);
1976 }
1977 
1978 static int
1979 vdc_do_ldc_up(vdc_t *vdc)
1980 {
1981 	int		status;
1982 	ldc_status_t	ldc_state;
1983 
1984 	DMSG(vdc, 0, "[%d] Bringing up channel %lx\n",
1985 	    vdc->instance, vdc->ldc_id);
1986 
1987 	if (vdc->lifecycle == VDC_LC_DETACHING)
1988 		return (EINVAL);
1989 
1990 	if ((status = ldc_up(vdc->ldc_handle)) != 0) {
1991 		switch (status) {
1992 		case ECONNREFUSED:	/* listener not ready at other end */
1993 			DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n",
1994 					vdc->instance, vdc->ldc_id, status);
1995 			status = 0;
1996 			break;
1997 		default:
1998 			DMSG(vdc, 0, "[%d] Failed to bring up LDC: "
1999 			    "channel=%ld, err=%d", vdc->instance, vdc->ldc_id,
2000 			    status);
2001 			break;
2002 		}
2003 	}
2004 
2005 	if (ldc_status(vdc->ldc_handle, &ldc_state) == 0) {
2006 		vdc->ldc_state = ldc_state;
2007 		if (ldc_state == LDC_UP) {
2008 			DMSG(vdc, 0, "[%d] LDC channel already up\n",
2009 			    vdc->instance);
2010 			vdc->seq_num = 1;
2011 			vdc->seq_num_reply = 0;
2012 		}
2013 	}
2014 
2015 	return (status);
2016 }
2017 
2018 /*
2019  * Function:
2020  *	vdc_terminate_ldc()
2021  *
2022  * Description:
2023  *
2024  * Arguments:
2025  *	vdc	- soft state pointer for this instance of the device driver.
2026  *
2027  * Return Code:
2028  *	None
2029  */
2030 static void
2031 vdc_terminate_ldc(vdc_t *vdc)
2032 {
2033 	int	instance = ddi_get_instance(vdc->dip);
2034 
2035 	ASSERT(vdc != NULL);
2036 	ASSERT(mutex_owned(&vdc->lock));
2037 
2038 	DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized);
2039 
2040 	if (vdc->initialized & VDC_LDC_OPEN) {
2041 		DMSG(vdc, 0, "[%d] ldc_close()\n", instance);
2042 		(void) ldc_close(vdc->ldc_handle);
2043 	}
2044 	if (vdc->initialized & VDC_LDC_CB) {
2045 		DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance);
2046 		(void) ldc_unreg_callback(vdc->ldc_handle);
2047 	}
2048 	if (vdc->initialized & VDC_LDC) {
2049 		DMSG(vdc, 0, "[%d] ldc_fini()\n", instance);
2050 		(void) ldc_fini(vdc->ldc_handle);
2051 		vdc->ldc_handle = NULL;
2052 	}
2053 
2054 	vdc->initialized &= ~(VDC_LDC | VDC_LDC_CB | VDC_LDC_OPEN);
2055 }
2056 
2057 /* -------------------------------------------------------------------------- */
2058 
2059 /*
2060  * Descriptor Ring helper routines
2061  */
2062 
2063 /*
2064  * Function:
2065  *	vdc_init_descriptor_ring()
2066  *
2067  * Description:
2068  *
2069  * Arguments:
2070  *	vdc	- soft state pointer for this instance of the device driver.
2071  *
2072  * Return Code:
2073  *	0	- Success
2074  */
2075 static int
2076 vdc_init_descriptor_ring(vdc_t *vdc)
2077 {
2078 	vd_dring_entry_t	*dep = NULL;	/* DRing Entry pointer */
2079 	int	status = 0;
2080 	int	i;
2081 
2082 	DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized);
2083 
2084 	ASSERT(vdc != NULL);
2085 	ASSERT(mutex_owned(&vdc->lock));
2086 	ASSERT(vdc->ldc_handle != NULL);
2087 
2088 	/* ensure we have enough room to store max sized block */
2089 	ASSERT(maxphys <= VD_MAX_BLOCK_SIZE);
2090 
2091 	if ((vdc->initialized & VDC_DRING_INIT) == 0) {
2092 		DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance);
2093 		/*
2094 		 * Calculate the maximum block size we can transmit using one
2095 		 * Descriptor Ring entry from the attributes returned by the
2096 		 * vDisk server. This is subject to a minimum of 'maxphys'
2097 		 * as we do not have the capability to split requests over
2098 		 * multiple DRing entries.
2099 		 */
2100 		if ((vdc->max_xfer_sz * vdc->block_size) < maxphys) {
2101 			DMSG(vdc, 0, "[%d] using minimum DRing size\n",
2102 					vdc->instance);
2103 			vdc->dring_max_cookies = maxphys / PAGESIZE;
2104 		} else {
2105 			vdc->dring_max_cookies =
2106 				(vdc->max_xfer_sz * vdc->block_size) / PAGESIZE;
2107 		}
2108 		vdc->dring_entry_size = (sizeof (vd_dring_entry_t) +
2109 				(sizeof (ldc_mem_cookie_t) *
2110 					(vdc->dring_max_cookies - 1)));
2111 		vdc->dring_len = VD_DRING_LEN;
2112 
2113 		status = ldc_mem_dring_create(vdc->dring_len,
2114 				vdc->dring_entry_size, &vdc->ldc_dring_hdl);
2115 		if ((vdc->ldc_dring_hdl == NULL) || (status != 0)) {
2116 			DMSG(vdc, 0, "[%d] Descriptor ring creation failed",
2117 					vdc->instance);
2118 			return (status);
2119 		}
2120 		vdc->initialized |= VDC_DRING_INIT;
2121 	}
2122 
2123 	if ((vdc->initialized & VDC_DRING_BOUND) == 0) {
2124 		DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance);
2125 		vdc->dring_cookie =
2126 			kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP);
2127 
2128 		status = ldc_mem_dring_bind(vdc->ldc_handle, vdc->ldc_dring_hdl,
2129 				LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW,
2130 				&vdc->dring_cookie[0],
2131 				&vdc->dring_cookie_count);
2132 		if (status != 0) {
2133 			DMSG(vdc, 0, "[%d] Failed to bind descriptor ring "
2134 				"(%lx) to channel (%lx) status=%d\n",
2135 				vdc->instance, vdc->ldc_dring_hdl,
2136 				vdc->ldc_handle, status);
2137 			return (status);
2138 		}
2139 		ASSERT(vdc->dring_cookie_count == 1);
2140 		vdc->initialized |= VDC_DRING_BOUND;
2141 	}
2142 
2143 	status = ldc_mem_dring_info(vdc->ldc_dring_hdl, &vdc->dring_mem_info);
2144 	if (status != 0) {
2145 		DMSG(vdc, 0,
2146 		    "[%d] Failed to get info for descriptor ring (%lx)\n",
2147 		    vdc->instance, vdc->ldc_dring_hdl);
2148 		return (status);
2149 	}
2150 
2151 	if ((vdc->initialized & VDC_DRING_LOCAL) == 0) {
2152 		DMSG(vdc, 0, "[%d] local dring\n", vdc->instance);
2153 
2154 		/* Allocate the local copy of this dring */
2155 		vdc->local_dring =
2156 			kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t),
2157 						KM_SLEEP);
2158 		vdc->initialized |= VDC_DRING_LOCAL;
2159 	}
2160 
2161 	/*
2162 	 * Mark all DRing entries as free and initialize the private
2163 	 * descriptor's memory handles. If any entry is initialized,
2164 	 * we need to free it later so we set the bit in 'initialized'
2165 	 * at the start.
2166 	 */
2167 	vdc->initialized |= VDC_DRING_ENTRY;
2168 	for (i = 0; i < vdc->dring_len; i++) {
2169 		dep = VDC_GET_DRING_ENTRY_PTR(vdc, i);
2170 		dep->hdr.dstate = VIO_DESC_FREE;
2171 
2172 		status = ldc_mem_alloc_handle(vdc->ldc_handle,
2173 				&vdc->local_dring[i].desc_mhdl);
2174 		if (status != 0) {
2175 			DMSG(vdc, 0, "![%d] Failed to alloc mem handle for"
2176 					" descriptor %d", vdc->instance, i);
2177 			return (status);
2178 		}
2179 		vdc->local_dring[i].is_free = B_TRUE;
2180 		vdc->local_dring[i].dep = dep;
2181 	}
2182 
2183 	/* Initialize the starting index */
2184 	vdc->dring_curr_idx = 0;
2185 
2186 	return (status);
2187 }
2188 
2189 /*
2190  * Function:
2191  *	vdc_destroy_descriptor_ring()
2192  *
2193  * Description:
2194  *
2195  * Arguments:
2196  *	vdc	- soft state pointer for this instance of the device driver.
2197  *
2198  * Return Code:
2199  *	None
2200  */
2201 static void
2202 vdc_destroy_descriptor_ring(vdc_t *vdc)
2203 {
2204 	vdc_local_desc_t	*ldep = NULL;	/* Local Dring Entry Pointer */
2205 	ldc_mem_handle_t	mhdl = NULL;
2206 	ldc_mem_info_t		minfo;
2207 	int			status = -1;
2208 	int			i;	/* loop */
2209 
2210 	ASSERT(vdc != NULL);
2211 	ASSERT(mutex_owned(&vdc->lock));
2212 
2213 	DMSG(vdc, 0, "[%d] Entered\n", vdc->instance);
2214 
2215 	if (vdc->initialized & VDC_DRING_ENTRY) {
2216 		DMSG(vdc, 0,
2217 		    "[%d] Removing Local DRing entries\n", vdc->instance);
2218 		for (i = 0; i < vdc->dring_len; i++) {
2219 			ldep = &vdc->local_dring[i];
2220 			mhdl = ldep->desc_mhdl;
2221 
2222 			if (mhdl == NULL)
2223 				continue;
2224 
2225 			if ((status = ldc_mem_info(mhdl, &minfo)) != 0) {
2226 				DMSG(vdc, 0,
2227 				    "ldc_mem_info returned an error: %d\n",
2228 				    status);
2229 
2230 				/*
2231 				 * This must mean that the mem handle
2232 				 * is not valid. Clear it out so that
2233 				 * no one tries to use it.
2234 				 */
2235 				ldep->desc_mhdl = NULL;
2236 				continue;
2237 			}
2238 
2239 			if (minfo.status == LDC_BOUND) {
2240 				(void) ldc_mem_unbind_handle(mhdl);
2241 			}
2242 
2243 			(void) ldc_mem_free_handle(mhdl);
2244 
2245 			ldep->desc_mhdl = NULL;
2246 		}
2247 		vdc->initialized &= ~VDC_DRING_ENTRY;
2248 	}
2249 
2250 	if (vdc->initialized & VDC_DRING_LOCAL) {
2251 		DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance);
2252 		kmem_free(vdc->local_dring,
2253 				vdc->dring_len * sizeof (vdc_local_desc_t));
2254 		vdc->initialized &= ~VDC_DRING_LOCAL;
2255 	}
2256 
2257 	if (vdc->initialized & VDC_DRING_BOUND) {
2258 		DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance);
2259 		status = ldc_mem_dring_unbind(vdc->ldc_dring_hdl);
2260 		if (status == 0) {
2261 			vdc->initialized &= ~VDC_DRING_BOUND;
2262 		} else {
2263 			DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx",
2264 				vdc->instance, status, vdc->ldc_dring_hdl);
2265 		}
2266 		kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t));
2267 	}
2268 
2269 	if (vdc->initialized & VDC_DRING_INIT) {
2270 		DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance);
2271 		status = ldc_mem_dring_destroy(vdc->ldc_dring_hdl);
2272 		if (status == 0) {
2273 			vdc->ldc_dring_hdl = NULL;
2274 			bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t));
2275 			vdc->initialized &= ~VDC_DRING_INIT;
2276 		} else {
2277 			DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)",
2278 				vdc->instance, status, vdc->ldc_dring_hdl);
2279 		}
2280 	}
2281 }
2282 
2283 /*
2284  * Function:
2285  *	vdc_map_to_shared_ring()
2286  *
2287  * Description:
2288  *	Copy contents of the local descriptor to the shared
2289  *	memory descriptor.
2290  *
2291  * Arguments:
2292  *	vdcp	- soft state pointer for this instance of the device driver.
2293  *	idx	- descriptor ring index
2294  *
2295  * Return Code:
2296  *	None
2297  */
2298 static int
2299 vdc_map_to_shared_dring(vdc_t *vdcp, int idx)
2300 {
2301 	vdc_local_desc_t	*ldep;
2302 	vd_dring_entry_t	*dep;
2303 	int			rv;
2304 
2305 	ldep = &(vdcp->local_dring[idx]);
2306 
2307 	/* for now leave in the old pop_mem_hdl stuff */
2308 	if (ldep->nbytes > 0) {
2309 		rv = vdc_populate_mem_hdl(vdcp, ldep);
2310 		if (rv) {
2311 			DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n",
2312 			    vdcp->instance);
2313 			return (rv);
2314 		}
2315 	}
2316 
2317 	/*
2318 	 * fill in the data details into the DRing
2319 	 */
2320 	dep = ldep->dep;
2321 	ASSERT(dep != NULL);
2322 
2323 	dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp);
2324 	dep->payload.operation = ldep->operation;
2325 	dep->payload.addr = ldep->offset;
2326 	dep->payload.nbytes = ldep->nbytes;
2327 	dep->payload.status = (uint32_t)-1;	/* vds will set valid value */
2328 	dep->payload.slice = ldep->slice;
2329 	dep->hdr.dstate = VIO_DESC_READY;
2330 	dep->hdr.ack = 1;		/* request an ACK for every message */
2331 
2332 	return (0);
2333 }
2334 
2335 /*
2336  * Function:
2337  *	vdc_send_request
2338  *
2339  * Description:
2340  *	This routine writes the data to be transmitted to vds into the
2341  *	descriptor, notifies vds that the ring has been updated and
2342  *	then waits for the request to be processed.
2343  *
2344  * Arguments:
2345  *	vdcp	  - the soft state pointer
2346  *	operation - operation we want vds to perform (VD_OP_XXX)
2347  *	addr	  - address of data buf to be read/written.
2348  *	nbytes	  - number of bytes to read/write
2349  *	slice	  - the disk slice this request is for
2350  *	offset	  - relative disk offset
2351  *	cb_type   - type of call - STRATEGY or SYNC
2352  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
2353  *			. mode for ioctl(9e)
2354  *			. LP64 diskaddr_t (block I/O)
2355  *	dir	  - direction of operation (READ/WRITE/BOTH)
2356  *
2357  * Return Codes:
2358  *	0
2359  *	ENXIO
2360  */
2361 static int
2362 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr,
2363     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
2364     void *cb_arg, vio_desc_direction_t dir)
2365 {
2366 	ASSERT(vdcp != NULL);
2367 	ASSERT(slice < V_NUMPAR);
2368 
2369 	mutex_enter(&vdcp->lock);
2370 
2371 	do {
2372 		while (vdcp->state != VDC_STATE_RUNNING) {
2373 			cv_wait(&vdcp->running_cv, &vdcp->lock);
2374 
2375 			/* return error if detaching */
2376 			if (vdcp->state == VDC_STATE_DETACH) {
2377 				mutex_exit(&vdcp->lock);
2378 				return (ENXIO);
2379 			}
2380 		}
2381 
2382 	} while (vdc_populate_descriptor(vdcp, operation, addr,
2383 	    nbytes, slice, offset, cb_type, cb_arg, dir));
2384 
2385 	mutex_exit(&vdcp->lock);
2386 	return (0);
2387 }
2388 
2389 
2390 /*
2391  * Function:
2392  *	vdc_populate_descriptor
2393  *
2394  * Description:
2395  *	This routine writes the data to be transmitted to vds into the
2396  *	descriptor, notifies vds that the ring has been updated and
2397  *	then waits for the request to be processed.
2398  *
2399  * Arguments:
2400  *	vdcp	  - the soft state pointer
2401  *	operation - operation we want vds to perform (VD_OP_XXX)
2402  *	addr	  - address of data buf to be read/written.
2403  *	nbytes	  - number of bytes to read/write
2404  *	slice	  - the disk slice this request is for
2405  *	offset	  - relative disk offset
2406  *	cb_type   - type of call - STRATEGY or SYNC
2407  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
2408  *			. mode for ioctl(9e)
2409  *			. LP64 diskaddr_t (block I/O)
2410  *	dir	  - direction of operation (READ/WRITE/BOTH)
2411  *
2412  * Return Codes:
2413  *	0
2414  *	EAGAIN
2415  *		EFAULT
2416  *		ENXIO
2417  *		EIO
2418  */
2419 static int
2420 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr,
2421     size_t nbytes, int slice, diskaddr_t offset, int cb_type,
2422     void *cb_arg, vio_desc_direction_t dir)
2423 {
2424 	vdc_local_desc_t	*local_dep = NULL; /* Local Dring Pointer */
2425 	int			idx;		/* Index of DRing entry used */
2426 	int			next_idx;
2427 	vio_dring_msg_t		dmsg;
2428 	size_t			msglen;
2429 	int			rv;
2430 
2431 	ASSERT(MUTEX_HELD(&vdcp->lock));
2432 	vdcp->threads_pending++;
2433 loop:
2434 	DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx);
2435 
2436 	/* Get next available D-Ring entry */
2437 	idx = vdcp->dring_curr_idx;
2438 	local_dep = &(vdcp->local_dring[idx]);
2439 
2440 	if (!local_dep->is_free) {
2441 		DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n",
2442 		    vdcp->instance);
2443 		cv_wait(&vdcp->dring_free_cv, &vdcp->lock);
2444 		if (vdcp->state == VDC_STATE_RUNNING ||
2445 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
2446 			goto loop;
2447 		}
2448 		vdcp->threads_pending--;
2449 		return (ECONNRESET);
2450 	}
2451 
2452 	next_idx = idx + 1;
2453 	if (next_idx >= vdcp->dring_len)
2454 		next_idx = 0;
2455 	vdcp->dring_curr_idx = next_idx;
2456 
2457 	ASSERT(local_dep->is_free);
2458 
2459 	local_dep->operation = operation;
2460 	local_dep->addr = addr;
2461 	local_dep->nbytes = nbytes;
2462 	local_dep->slice = slice;
2463 	local_dep->offset = offset;
2464 	local_dep->cb_type = cb_type;
2465 	local_dep->cb_arg = cb_arg;
2466 	local_dep->dir = dir;
2467 
2468 	local_dep->is_free = B_FALSE;
2469 
2470 	rv = vdc_map_to_shared_dring(vdcp, idx);
2471 	if (rv) {
2472 		DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n",
2473 		    vdcp->instance);
2474 		/* free the descriptor */
2475 		local_dep->is_free = B_TRUE;
2476 		vdcp->dring_curr_idx = idx;
2477 		cv_wait(&vdcp->membind_cv, &vdcp->lock);
2478 		if (vdcp->state == VDC_STATE_RUNNING ||
2479 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
2480 			goto loop;
2481 		}
2482 		vdcp->threads_pending--;
2483 		return (ECONNRESET);
2484 	}
2485 
2486 	/*
2487 	 * Send a msg with the DRing details to vds
2488 	 */
2489 	VIO_INIT_DRING_DATA_TAG(dmsg);
2490 	VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp);
2491 	dmsg.dring_ident = vdcp->dring_ident;
2492 	dmsg.start_idx = idx;
2493 	dmsg.end_idx = idx;
2494 	vdcp->seq_num++;
2495 
2496 	DTRACE_IO2(send, vio_dring_msg_t *, &dmsg, vdc_t *, vdcp);
2497 
2498 	DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n",
2499 	    vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num);
2500 
2501 	/*
2502 	 * note we're still holding the lock here to
2503 	 * make sure the message goes out in order !!!...
2504 	 */
2505 	msglen = sizeof (dmsg);
2506 	rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen);
2507 	switch (rv) {
2508 	case ECONNRESET:
2509 		/*
2510 		 * vdc_send initiates the reset on failure.
2511 		 * Since the transaction has already been put
2512 		 * on the local dring, it will automatically get
2513 		 * retried when the channel is reset. Given that,
2514 		 * it is ok to just return success even though the
2515 		 * send failed.
2516 		 */
2517 		rv = 0;
2518 		break;
2519 
2520 	case 0: /* EOK */
2521 		DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv);
2522 		break;
2523 
2524 	default:
2525 		goto cleanup_and_exit;
2526 	}
2527 
2528 	vdcp->threads_pending--;
2529 	return (rv);
2530 
2531 cleanup_and_exit:
2532 	DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv);
2533 	return (ENXIO);
2534 }
2535 
2536 /*
2537  * Function:
2538  *	vdc_do_sync_op
2539  *
2540  * Description:
2541  * 	Wrapper around vdc_populate_descriptor that blocks until the
2542  * 	response to the message is available.
2543  *
2544  * Arguments:
2545  *	vdcp	  - the soft state pointer
2546  *	operation - operation we want vds to perform (VD_OP_XXX)
2547  *	addr	  - address of data buf to be read/written.
2548  *	nbytes	  - number of bytes to read/write
2549  *	slice	  - the disk slice this request is for
2550  *	offset	  - relative disk offset
2551  *	cb_type   - type of call - STRATEGY or SYNC
2552  *	cb_arg	  - parameter to be sent to server (depends on VD_OP_XXX type)
2553  *			. mode for ioctl(9e)
2554  *			. LP64 diskaddr_t (block I/O)
2555  *	dir	  - direction of operation (READ/WRITE/BOTH)
2556  *
2557  * Return Codes:
2558  *	0
2559  *	EAGAIN
2560  *		EFAULT
2561  *		ENXIO
2562  *		EIO
2563  */
2564 static int
2565 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes,
2566     int slice, diskaddr_t offset, int cb_type, void *cb_arg,
2567     vio_desc_direction_t dir)
2568 {
2569 	int status;
2570 
2571 	ASSERT(cb_type == CB_SYNC);
2572 
2573 	/*
2574 	 * Grab the lock, if blocked wait until the server
2575 	 * response causes us to wake up again.
2576 	 */
2577 	mutex_enter(&vdcp->lock);
2578 	vdcp->sync_op_cnt++;
2579 	while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH)
2580 		cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock);
2581 
2582 	if (vdcp->state == VDC_STATE_DETACH) {
2583 		cv_broadcast(&vdcp->sync_blocked_cv);
2584 		vdcp->sync_op_cnt--;
2585 		mutex_exit(&vdcp->lock);
2586 		return (ENXIO);
2587 	}
2588 
2589 	/* now block anyone other thread entering after us */
2590 	vdcp->sync_op_blocked = B_TRUE;
2591 	vdcp->sync_op_pending = B_TRUE;
2592 	mutex_exit(&vdcp->lock);
2593 
2594 	/*
2595 	 * No need to check return value - will return error only
2596 	 * in the DETACH case and we can fall through
2597 	 */
2598 	(void) vdc_send_request(vdcp, operation, addr,
2599 	    nbytes, slice, offset, cb_type, cb_arg, dir);
2600 
2601 	/*
2602 	 * block until our transaction completes.
2603 	 * Also anyone else waiting also gets to go next.
2604 	 */
2605 	mutex_enter(&vdcp->lock);
2606 	while (vdcp->sync_op_pending && vdcp->state != VDC_STATE_DETACH)
2607 		cv_wait(&vdcp->sync_pending_cv, &vdcp->lock);
2608 
2609 	DMSG(vdcp, 2, ": operation returned %d\n", vdcp->sync_op_status);
2610 	if (vdcp->state == VDC_STATE_DETACH) {
2611 		vdcp->sync_op_pending = B_FALSE;
2612 		status = ENXIO;
2613 	} else {
2614 		status = vdcp->sync_op_status;
2615 	}
2616 
2617 	vdcp->sync_op_status = 0;
2618 	vdcp->sync_op_blocked = B_FALSE;
2619 	vdcp->sync_op_cnt--;
2620 
2621 	/* signal the next waiting thread */
2622 	cv_signal(&vdcp->sync_blocked_cv);
2623 	mutex_exit(&vdcp->lock);
2624 
2625 	return (status);
2626 }
2627 
2628 
2629 /*
2630  * Function:
2631  *	vdc_drain_response()
2632  *
2633  * Description:
2634  * 	When a guest is panicking, the completion of requests needs to be
2635  * 	handled differently because interrupts are disabled and vdc
2636  * 	will not get messages. We have to poll for the messages instead.
2637  *
2638  * Arguments:
2639  *	vdc	- soft state pointer for this instance of the device driver.
2640  *
2641  * Return Code:
2642  *	0	- Success
2643  */
2644 static int
2645 vdc_drain_response(vdc_t *vdc)
2646 {
2647 	int 			rv, idx, retries;
2648 	size_t			msglen;
2649 	vdc_local_desc_t 	*ldep = NULL;	/* Local Dring Entry Pointer */
2650 	vio_dring_msg_t		dmsg;
2651 
2652 	mutex_enter(&vdc->lock);
2653 
2654 	retries = 0;
2655 	for (;;) {
2656 		msglen = sizeof (dmsg);
2657 		rv = ldc_read(vdc->ldc_handle, (caddr_t)&dmsg, &msglen);
2658 		if (rv) {
2659 			rv = EINVAL;
2660 			break;
2661 		}
2662 
2663 		/*
2664 		 * if there are no packets wait and check again
2665 		 */
2666 		if ((rv == 0) && (msglen == 0)) {
2667 			if (retries++ > vdc_dump_retries) {
2668 				rv = EAGAIN;
2669 				break;
2670 			}
2671 
2672 			drv_usecwait(vdc_usec_timeout_dump);
2673 			continue;
2674 		}
2675 
2676 		/*
2677 		 * Ignore all messages that are not ACKs/NACKs to
2678 		 * DRing requests.
2679 		 */
2680 		if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) ||
2681 		    (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) {
2682 			DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n",
2683 			    dmsg.tag.vio_msgtype,
2684 			    dmsg.tag.vio_subtype,
2685 			    dmsg.tag.vio_subtype_env);
2686 			continue;
2687 		}
2688 
2689 		/*
2690 		 * set the appropriate return value for the current request.
2691 		 */
2692 		switch (dmsg.tag.vio_subtype) {
2693 		case VIO_SUBTYPE_ACK:
2694 			rv = 0;
2695 			break;
2696 		case VIO_SUBTYPE_NACK:
2697 			rv = EAGAIN;
2698 			break;
2699 		default:
2700 			continue;
2701 		}
2702 
2703 		idx = dmsg.start_idx;
2704 		if (idx >= vdc->dring_len) {
2705 			DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n",
2706 			    vdc->instance, idx);
2707 			continue;
2708 		}
2709 		ldep = &vdc->local_dring[idx];
2710 		if (ldep->dep->hdr.dstate != VIO_DESC_DONE) {
2711 			DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n",
2712 			    vdc->instance, idx, ldep->dep->hdr.dstate);
2713 			continue;
2714 		}
2715 
2716 		DMSG(vdc, 1, "[%d] Depopulating idx=%d state=%d\n",
2717 		    vdc->instance, idx, ldep->dep->hdr.dstate);
2718 		rv = vdc_depopulate_descriptor(vdc, idx);
2719 		if (rv) {
2720 			DMSG(vdc, 0,
2721 			    "[%d] Entry @ %d - depopulate failed ..\n",
2722 			    vdc->instance, idx);
2723 		}
2724 
2725 		/* if this is the last descriptor - break out of loop */
2726 		if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx)
2727 			break;
2728 	}
2729 
2730 	mutex_exit(&vdc->lock);
2731 	DMSG(vdc, 0, "End idx=%d\n", idx);
2732 
2733 	return (rv);
2734 }
2735 
2736 
2737 /*
2738  * Function:
2739  *	vdc_depopulate_descriptor()
2740  *
2741  * Description:
2742  *
2743  * Arguments:
2744  *	vdc	- soft state pointer for this instance of the device driver.
2745  *	idx	- Index of the Descriptor Ring entry being modified
2746  *
2747  * Return Code:
2748  *	0	- Success
2749  */
2750 static int
2751 vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx)
2752 {
2753 	vd_dring_entry_t *dep = NULL;		/* Dring Entry Pointer */
2754 	vdc_local_desc_t *ldep = NULL;		/* Local Dring Entry Pointer */
2755 	int		status = ENXIO;
2756 	int		operation;
2757 	int		rv = 0;
2758 
2759 	ASSERT(vdc != NULL);
2760 	ASSERT(idx < vdc->dring_len);
2761 	ldep = &vdc->local_dring[idx];
2762 	ASSERT(ldep != NULL);
2763 	ASSERT(MUTEX_HELD(&vdc->lock));
2764 
2765 	DMSG(vdc, 2, ": idx = %d\n", idx);
2766 	dep = ldep->dep;
2767 	ASSERT(dep != NULL);
2768 	ASSERT((dep->hdr.dstate == VIO_DESC_DONE) ||
2769 			(dep->payload.status == ECANCELED));
2770 
2771 	VDC_MARK_DRING_ENTRY_FREE(vdc, idx);
2772 
2773 	ldep->is_free = B_TRUE;
2774 	DMSG(vdc, 2, ": is_free = %d\n", ldep->is_free);
2775 	status = dep->payload.status;
2776 	operation = dep->payload.operation;
2777 
2778 	/* the DKIO FLUSH operation never bind handles so we can return now */
2779 	if (operation == VD_OP_FLUSH)
2780 		return (status);
2781 
2782 	/*
2783 	 * If the upper layer passed in a misaligned address we copied the
2784 	 * data into an aligned buffer before sending it to LDC - we now
2785 	 * copy it back to the original buffer.
2786 	 */
2787 	if (ldep->align_addr) {
2788 		ASSERT(ldep->addr != NULL);
2789 
2790 		if (dep->payload.nbytes > 0)
2791 			bcopy(ldep->align_addr, ldep->addr,
2792 			    dep->payload.nbytes);
2793 		kmem_free(ldep->align_addr,
2794 			sizeof (caddr_t) * P2ROUNDUP(ldep->nbytes, 8));
2795 		ldep->align_addr = NULL;
2796 	}
2797 
2798 	rv = ldc_mem_unbind_handle(ldep->desc_mhdl);
2799 	if (rv != 0) {
2800 		DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)",
2801 				vdc->instance, ldep->desc_mhdl, idx, rv);
2802 		/*
2803 		 * The error returned by the vDisk server is more informative
2804 		 * and thus has a higher priority but if it isn't set we ensure
2805 		 * that this function returns an error.
2806 		 */
2807 		if (status == 0)
2808 			status = EINVAL;
2809 	}
2810 
2811 	cv_signal(&vdc->membind_cv);
2812 	cv_signal(&vdc->dring_free_cv);
2813 
2814 	return (status);
2815 }
2816 
2817 /*
2818  * Function:
2819  *	vdc_populate_mem_hdl()
2820  *
2821  * Description:
2822  *
2823  * Arguments:
2824  *	vdc	- soft state pointer for this instance of the device driver.
2825  *	idx	- Index of the Descriptor Ring entry being modified
2826  *	addr	- virtual address being mapped in
2827  *	nybtes	- number of bytes in 'addr'
2828  *	operation - the vDisk operation being performed (VD_OP_xxx)
2829  *
2830  * Return Code:
2831  *	0	- Success
2832  */
2833 static int
2834 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep)
2835 {
2836 	vd_dring_entry_t	*dep = NULL;
2837 	ldc_mem_handle_t	mhdl;
2838 	caddr_t			vaddr;
2839 	size_t			nbytes;
2840 	uint8_t			perm = LDC_MEM_RW;
2841 	uint8_t			maptype;
2842 	int			rv = 0;
2843 	int			i;
2844 
2845 	ASSERT(vdcp != NULL);
2846 
2847 	dep = ldep->dep;
2848 	mhdl = ldep->desc_mhdl;
2849 
2850 	switch (ldep->dir) {
2851 	case VIO_read_dir:
2852 		perm = LDC_MEM_W;
2853 		break;
2854 
2855 	case VIO_write_dir:
2856 		perm = LDC_MEM_R;
2857 		break;
2858 
2859 	case VIO_both_dir:
2860 		perm = LDC_MEM_RW;
2861 		break;
2862 
2863 	default:
2864 		ASSERT(0);	/* catch bad programming in vdc */
2865 	}
2866 
2867 	/*
2868 	 * LDC expects any addresses passed in to be 8-byte aligned. We need
2869 	 * to copy the contents of any misaligned buffers to a newly allocated
2870 	 * buffer and bind it instead (and copy the the contents back to the
2871 	 * original buffer passed in when depopulating the descriptor)
2872 	 */
2873 	vaddr = ldep->addr;
2874 	nbytes = ldep->nbytes;
2875 	if (((uint64_t)vaddr & 0x7) != 0) {
2876 		ASSERT(ldep->align_addr == NULL);
2877 		ldep->align_addr =
2878 			kmem_alloc(sizeof (caddr_t) *
2879 				P2ROUNDUP(nbytes, 8), KM_SLEEP);
2880 		DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating "
2881 		    "(buf=%p nb=%ld op=%d)\n",
2882 		    vdcp->instance, (void *)vaddr, (void *)ldep->align_addr,
2883 		    nbytes, ldep->operation);
2884 		if (perm != LDC_MEM_W)
2885 			bcopy(vaddr, ldep->align_addr, nbytes);
2886 		vaddr = ldep->align_addr;
2887 	}
2888 
2889 	maptype = LDC_IO_MAP|LDC_SHADOW_MAP|LDC_DIRECT_MAP;
2890 	rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8),
2891 		maptype, perm, &dep->payload.cookie[0],
2892 		&dep->payload.ncookies);
2893 	DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n",
2894 			vdcp->instance, dep->payload.ncookies);
2895 	if (rv != 0) {
2896 		DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle "
2897 		    "(mhdl=%p, buf=%p, err=%d)\n",
2898 		    vdcp->instance, (void *)mhdl, (void *)vaddr, rv);
2899 		if (ldep->align_addr) {
2900 			kmem_free(ldep->align_addr,
2901 				sizeof (caddr_t) * P2ROUNDUP(nbytes, 8));
2902 			ldep->align_addr = NULL;
2903 		}
2904 		return (EAGAIN);
2905 	}
2906 
2907 	/*
2908 	 * Get the other cookies (if any).
2909 	 */
2910 	for (i = 1; i < dep->payload.ncookies; i++) {
2911 		rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]);
2912 		if (rv != 0) {
2913 			(void) ldc_mem_unbind_handle(mhdl);
2914 			DMSG(vdcp, 0, "?[%d] Failed to get next cookie "
2915 					"(mhdl=%lx cnum=%d), err=%d",
2916 					vdcp->instance, mhdl, i, rv);
2917 			if (ldep->align_addr) {
2918 				kmem_free(ldep->align_addr,
2919 					sizeof (caddr_t) * ldep->nbytes);
2920 				ldep->align_addr = NULL;
2921 			}
2922 			return (EAGAIN);
2923 		}
2924 	}
2925 
2926 	return (rv);
2927 }
2928 
2929 /*
2930  * Interrupt handlers for messages from LDC
2931  */
2932 
2933 /*
2934  * Function:
2935  *	vdc_handle_cb()
2936  *
2937  * Description:
2938  *
2939  * Arguments:
2940  *	event	- Type of event (LDC_EVT_xxx) that triggered the callback
2941  *	arg	- soft state pointer for this instance of the device driver.
2942  *
2943  * Return Code:
2944  *	0	- Success
2945  */
2946 static uint_t
2947 vdc_handle_cb(uint64_t event, caddr_t arg)
2948 {
2949 	ldc_status_t	ldc_state;
2950 	int		rv = 0;
2951 
2952 	vdc_t	*vdc = (vdc_t *)(void *)arg;
2953 
2954 	ASSERT(vdc != NULL);
2955 
2956 	DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num);
2957 
2958 	/*
2959 	 * Depending on the type of event that triggered this callback,
2960 	 * we modify the handshake state or read the data.
2961 	 *
2962 	 * NOTE: not done as a switch() as event could be triggered by
2963 	 * a state change and a read request. Also the ordering	of the
2964 	 * check for the event types is deliberate.
2965 	 */
2966 	if (event & LDC_EVT_UP) {
2967 		DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance);
2968 
2969 		mutex_enter(&vdc->lock);
2970 
2971 		/* get LDC state */
2972 		rv = ldc_status(vdc->ldc_handle, &ldc_state);
2973 		if (rv != 0) {
2974 			DMSG(vdc, 0, "[%d] Couldn't get LDC status %d",
2975 			    vdc->instance, rv);
2976 			return (LDC_SUCCESS);
2977 		}
2978 		if (vdc->ldc_state != LDC_UP && ldc_state == LDC_UP) {
2979 			/*
2980 			 * Reset the transaction sequence numbers when
2981 			 * LDC comes up. We then kick off the handshake
2982 			 * negotiation with the vDisk server.
2983 			 */
2984 			vdc->seq_num = 1;
2985 			vdc->seq_num_reply = 0;
2986 			vdc->ldc_state = ldc_state;
2987 			cv_signal(&vdc->initwait_cv);
2988 		}
2989 
2990 		mutex_exit(&vdc->lock);
2991 	}
2992 
2993 	if (event & LDC_EVT_READ) {
2994 		DMSG(vdc, 0, "[%d] Received LDC_EVT_READ\n", vdc->instance);
2995 		mutex_enter(&vdc->read_lock);
2996 		cv_signal(&vdc->read_cv);
2997 		vdc->read_state = VDC_READ_PENDING;
2998 		mutex_exit(&vdc->read_lock);
2999 
3000 		/* that's all we have to do - no need to handle DOWN/RESET */
3001 		return (LDC_SUCCESS);
3002 	}
3003 
3004 	if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) {
3005 
3006 		DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance);
3007 
3008 		mutex_enter(&vdc->lock);
3009 		/*
3010 		 * Need to wake up any readers so they will
3011 		 * detect that a reset has occurred.
3012 		 */
3013 		mutex_enter(&vdc->read_lock);
3014 		if ((vdc->read_state == VDC_READ_WAITING) ||
3015 		    (vdc->read_state == VDC_READ_RESET))
3016 			cv_signal(&vdc->read_cv);
3017 		vdc->read_state = VDC_READ_RESET;
3018 		mutex_exit(&vdc->read_lock);
3019 
3020 		/* wake up any threads waiting for connection to come up */
3021 		if (vdc->state == VDC_STATE_INIT_WAITING) {
3022 			vdc->state = VDC_STATE_RESETTING;
3023 			cv_signal(&vdc->initwait_cv);
3024 		}
3025 
3026 		mutex_exit(&vdc->lock);
3027 	}
3028 
3029 	if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ))
3030 		DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received",
3031 				vdc->instance, event);
3032 
3033 	return (LDC_SUCCESS);
3034 }
3035 
3036 /*
3037  * Function:
3038  *	vdc_wait_for_response()
3039  *
3040  * Description:
3041  *	Block waiting for a response from the server. If there is
3042  *	no data the thread block on the read_cv that is signalled
3043  *	by the callback when an EVT_READ occurs.
3044  *
3045  * Arguments:
3046  *	vdcp	- soft state pointer for this instance of the device driver.
3047  *
3048  * Return Code:
3049  *	0	- Success
3050  */
3051 static int
3052 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp)
3053 {
3054 	size_t		nbytes = sizeof (*msgp);
3055 	int		status;
3056 
3057 	ASSERT(vdcp != NULL);
3058 
3059 	DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance);
3060 
3061 	status = vdc_recv(vdcp, msgp, &nbytes);
3062 	DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n",
3063 		status, (int)nbytes);
3064 	if (status) {
3065 		DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n",
3066 				vdcp->instance, status);
3067 		return (status);
3068 	}
3069 
3070 	if (nbytes < sizeof (vio_msg_tag_t)) {
3071 		DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n",
3072 			vdcp->instance, sizeof (vio_msg_tag_t), nbytes);
3073 		return (ENOMSG);
3074 	}
3075 
3076 	DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance,
3077 	    msgp->tag.vio_msgtype,
3078 	    msgp->tag.vio_subtype,
3079 	    msgp->tag.vio_subtype_env);
3080 
3081 	/*
3082 	 * Verify the Session ID of the message
3083 	 *
3084 	 * Every message after the Version has been negotiated should
3085 	 * have the correct session ID set.
3086 	 */
3087 	if ((msgp->tag.vio_sid != vdcp->session_id) &&
3088 	    (msgp->tag.vio_subtype_env != VIO_VER_INFO)) {
3089 		DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, "
3090 				"expected 0x%lx [seq num %lx @ %d]",
3091 			vdcp->instance, msgp->tag.vio_sid,
3092 			vdcp->session_id,
3093 			((vio_dring_msg_t *)msgp)->seq_num,
3094 			((vio_dring_msg_t *)msgp)->start_idx);
3095 		return (ENOMSG);
3096 	}
3097 	return (0);
3098 }
3099 
3100 
3101 /*
3102  * Function:
3103  *	vdc_resubmit_backup_dring()
3104  *
3105  * Description:
3106  *	Resubmit each descriptor in the backed up dring to
3107  * 	vDisk server. The Dring was backed up during connection
3108  *	reset.
3109  *
3110  * Arguments:
3111  *	vdcp	- soft state pointer for this instance of the device driver.
3112  *
3113  * Return Code:
3114  *	0	- Success
3115  */
3116 static int
3117 vdc_resubmit_backup_dring(vdc_t *vdcp)
3118 {
3119 	int		count;
3120 	int		b_idx;
3121 	int		rv;
3122 	int		dring_size;
3123 	int		status;
3124 	vio_msg_t	vio_msg;
3125 	vdc_local_desc_t	*curr_ldep;
3126 
3127 	ASSERT(MUTEX_NOT_HELD(&vdcp->lock));
3128 	ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING);
3129 
3130 	DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n",
3131 	    vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
3132 
3133 	/*
3134 	 * Walk the backup copy of the local descriptor ring and
3135 	 * resubmit all the outstanding transactions.
3136 	 */
3137 	b_idx = vdcp->local_dring_backup_tail;
3138 	for (count = 0; count < vdcp->local_dring_backup_len; count++) {
3139 
3140 		curr_ldep = &(vdcp->local_dring_backup[b_idx]);
3141 
3142 		/* only resubmit oustanding transactions */
3143 		if (!curr_ldep->is_free) {
3144 
3145 			DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx);
3146 			mutex_enter(&vdcp->lock);
3147 			rv = vdc_populate_descriptor(vdcp, curr_ldep->operation,
3148 			    curr_ldep->addr, curr_ldep->nbytes,
3149 			    curr_ldep->slice, curr_ldep->offset,
3150 			    curr_ldep->cb_type, curr_ldep->cb_arg,
3151 			    curr_ldep->dir);
3152 			mutex_exit(&vdcp->lock);
3153 			if (rv) {
3154 				DMSG(vdcp, 1, "[%d] cannot resubmit entry %d\n",
3155 				    vdcp->instance, b_idx);
3156 				return (rv);
3157 			}
3158 
3159 			/* Wait for the response message. */
3160 			DMSG(vdcp, 1, "waiting for response to idx=%x\n",
3161 			    b_idx);
3162 			status = vdc_wait_for_response(vdcp, &vio_msg);
3163 			if (status) {
3164 				DMSG(vdcp, 1, "[%d] wait_for_response "
3165 				    "returned err=%d\n", vdcp->instance,
3166 				    status);
3167 				return (status);
3168 			}
3169 
3170 			DMSG(vdcp, 1, "processing msg for idx=%x\n", b_idx);
3171 			status = vdc_process_data_msg(vdcp, &vio_msg);
3172 			if (status) {
3173 				DMSG(vdcp, 1, "[%d] process_data_msg "
3174 				    "returned err=%d\n", vdcp->instance,
3175 				    status);
3176 				return (status);
3177 			}
3178 		}
3179 
3180 		/* get the next element to submit */
3181 		if (++b_idx >= vdcp->local_dring_backup_len)
3182 			b_idx = 0;
3183 	}
3184 
3185 	/* all done - now clear up pending dring copy */
3186 	dring_size = vdcp->local_dring_backup_len *
3187 		sizeof (vdcp->local_dring_backup[0]);
3188 
3189 	(void) kmem_free(vdcp->local_dring_backup, dring_size);
3190 
3191 	vdcp->local_dring_backup = NULL;
3192 
3193 	return (0);
3194 }
3195 
3196 /*
3197  * Function:
3198  *	vdc_backup_local_dring()
3199  *
3200  * Description:
3201  *	Backup the current dring in the event of a reset. The Dring
3202  *	transactions will be resubmitted to the server when the
3203  *	connection is restored.
3204  *
3205  * Arguments:
3206  *	vdcp	- soft state pointer for this instance of the device driver.
3207  *
3208  * Return Code:
3209  *	NONE
3210  */
3211 static void
3212 vdc_backup_local_dring(vdc_t *vdcp)
3213 {
3214 	int dring_size;
3215 
3216 	ASSERT(vdcp->state == VDC_STATE_RESETTING);
3217 
3218 	/*
3219 	 * If the backup dring is stil around, it means
3220 	 * that the last restore did not complete. However,
3221 	 * since we never got back into the running state,
3222 	 * the backup copy we have is still valid.
3223 	 */
3224 	if (vdcp->local_dring_backup != NULL) {
3225 		DMSG(vdcp, 1, "reusing local descriptor ring backup "
3226 		    "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len,
3227 		    vdcp->local_dring_backup_tail);
3228 		return;
3229 	}
3230 
3231 	DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, "
3232 	    "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx);
3233 
3234 	dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]);
3235 
3236 	vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP);
3237 	bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size);
3238 
3239 	vdcp->local_dring_backup_tail = vdcp->dring_curr_idx;
3240 	vdcp->local_dring_backup_len = vdcp->dring_len;
3241 }
3242 
3243 /* -------------------------------------------------------------------------- */
3244 
3245 /*
3246  * The following functions process the incoming messages from vds
3247  */
3248 
3249 /*
3250  * Function:
3251  *      vdc_process_msg_thread()
3252  *
3253  * Description:
3254  *
3255  *	Main VDC message processing thread. Each vDisk instance
3256  * 	consists of a copy of this thread. This thread triggers
3257  * 	all the handshakes and data exchange with the server. It
3258  * 	also handles all channel resets
3259  *
3260  * Arguments:
3261  *      vdc     - soft state pointer for this instance of the device driver.
3262  *
3263  * Return Code:
3264  *      None
3265  */
3266 static void
3267 vdc_process_msg_thread(vdc_t *vdcp)
3268 {
3269 	int	status;
3270 
3271 	mutex_enter(&vdcp->lock);
3272 
3273 	for (;;) {
3274 
3275 #define	Q(_s)	(vdcp->state == _s) ? #_s :
3276 		DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state,
3277 		Q(VDC_STATE_INIT)
3278 		Q(VDC_STATE_INIT_WAITING)
3279 		Q(VDC_STATE_NEGOTIATE)
3280 		Q(VDC_STATE_HANDLE_PENDING)
3281 		Q(VDC_STATE_RUNNING)
3282 		Q(VDC_STATE_RESETTING)
3283 		Q(VDC_STATE_DETACH)
3284 		"UNKNOWN");
3285 
3286 		switch (vdcp->state) {
3287 		case VDC_STATE_INIT:
3288 
3289 			/* Check if have re-initializing repeatedly */
3290 			if (vdcp->hshake_cnt++ > vdc_hshake_retries) {
3291 				cmn_err(CE_NOTE, "[%d] disk access failed.\n",
3292 				    vdcp->instance);
3293 				vdcp->state = VDC_STATE_DETACH;
3294 				break;
3295 			}
3296 
3297 			/* Bring up connection with vds via LDC */
3298 			status = vdc_start_ldc_connection(vdcp);
3299 			switch (status) {
3300 			case EINVAL:
3301 				DMSG(vdcp, 0, "[%d] Could not start LDC",
3302 				    vdcp->instance);
3303 				vdcp->state = VDC_STATE_DETACH;
3304 				break;
3305 			case 0:
3306 				vdcp->state = VDC_STATE_INIT_WAITING;
3307 				break;
3308 			default:
3309 				vdcp->state = VDC_STATE_INIT_WAITING;
3310 				break;
3311 			}
3312 			break;
3313 
3314 		case VDC_STATE_INIT_WAITING:
3315 
3316 			/*
3317 			 * Let the callback event move us on
3318 			 * when channel is open to server
3319 			 */
3320 			while (vdcp->ldc_state != LDC_UP) {
3321 				cv_wait(&vdcp->initwait_cv, &vdcp->lock);
3322 				if (vdcp->state != VDC_STATE_INIT_WAITING) {
3323 					DMSG(vdcp, 0,
3324 				"state moved to %d out from under us...\n",
3325 					    vdcp->state);
3326 
3327 					break;
3328 				}
3329 			}
3330 			if (vdcp->state == VDC_STATE_INIT_WAITING &&
3331 			    vdcp->ldc_state == LDC_UP) {
3332 				vdcp->state = VDC_STATE_NEGOTIATE;
3333 			}
3334 			break;
3335 
3336 		case VDC_STATE_NEGOTIATE:
3337 			switch (status = vdc_ver_negotiation(vdcp)) {
3338 			case 0:
3339 				break;
3340 			default:
3341 				DMSG(vdcp, 0, "ver negotiate failed (%d)..\n",
3342 				    status);
3343 				goto reset;
3344 			}
3345 
3346 			switch (status = vdc_attr_negotiation(vdcp)) {
3347 			case 0:
3348 				break;
3349 			default:
3350 				DMSG(vdcp, 0, "attr negotiate failed (%d)..\n",
3351 				    status);
3352 				goto reset;
3353 			}
3354 
3355 			switch (status = vdc_dring_negotiation(vdcp)) {
3356 			case 0:
3357 				break;
3358 			default:
3359 				DMSG(vdcp, 0, "dring negotiate failed (%d)..\n",
3360 				    status);
3361 				goto reset;
3362 			}
3363 
3364 			switch (status = vdc_rdx_exchange(vdcp)) {
3365 			case 0:
3366 				vdcp->state = VDC_STATE_HANDLE_PENDING;
3367 				goto done;
3368 			default:
3369 				DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n",
3370 				    status);
3371 				goto reset;
3372 			}
3373 reset:
3374 			DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n",
3375 			    status);
3376 			vdcp->state = VDC_STATE_RESETTING;
3377 done:
3378 			DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n",
3379 			    vdcp->state);
3380 			break;
3381 
3382 		case VDC_STATE_HANDLE_PENDING:
3383 
3384 			mutex_exit(&vdcp->lock);
3385 			status = vdc_resubmit_backup_dring(vdcp);
3386 			mutex_enter(&vdcp->lock);
3387 
3388 			if (status)
3389 				vdcp->state = VDC_STATE_RESETTING;
3390 			else
3391 				vdcp->state = VDC_STATE_RUNNING;
3392 
3393 			break;
3394 
3395 		/* enter running state */
3396 		case VDC_STATE_RUNNING:
3397 			/*
3398 			 * Signal anyone waiting for the connection
3399 			 * to come on line.
3400 			 */
3401 			vdcp->hshake_cnt = 0;
3402 			cv_broadcast(&vdcp->running_cv);
3403 			mutex_exit(&vdcp->lock);
3404 
3405 			for (;;) {
3406 				vio_msg_t msg;
3407 				status = vdc_wait_for_response(vdcp, &msg);
3408 				if (status) break;
3409 
3410 				DMSG(vdcp, 1, "[%d] new pkt(s) available\n",
3411 					vdcp->instance);
3412 				status = vdc_process_data_msg(vdcp, &msg);
3413 				if (status) {
3414 					DMSG(vdcp, 1, "[%d] process_data_msg "
3415 					    "returned err=%d\n", vdcp->instance,
3416 					    status);
3417 					break;
3418 				}
3419 
3420 			}
3421 
3422 			mutex_enter(&vdcp->lock);
3423 
3424 			vdcp->state = VDC_STATE_RESETTING;
3425 			break;
3426 
3427 		case VDC_STATE_RESETTING:
3428 			DMSG(vdcp, 0, "Initiating channel reset "
3429 			    "(pending = %d)\n", (int)vdcp->threads_pending);
3430 
3431 			if (vdcp->self_reset) {
3432 				DMSG(vdcp, 0,
3433 				    "[%d] calling stop_ldc_connection.\n",
3434 				    vdcp->instance);
3435 				status = vdc_stop_ldc_connection(vdcp);
3436 				vdcp->self_reset = B_FALSE;
3437 			}
3438 
3439 			/*
3440 			 * Wait for all threads currently waiting
3441 			 * for a free dring entry to use.
3442 			 */
3443 			while (vdcp->threads_pending) {
3444 				cv_broadcast(&vdcp->membind_cv);
3445 				cv_broadcast(&vdcp->dring_free_cv);
3446 				mutex_exit(&vdcp->lock);
3447 				/* let them wake up */
3448 				drv_usecwait(vdc_min_timeout_ldc);
3449 				mutex_enter(&vdcp->lock);
3450 			}
3451 
3452 			ASSERT(vdcp->threads_pending == 0);
3453 
3454 			/* Sanity check that no thread is receiving */
3455 			ASSERT(vdcp->read_state != VDC_READ_WAITING);
3456 
3457 			vdcp->read_state = VDC_READ_IDLE;
3458 
3459 			vdc_backup_local_dring(vdcp);
3460 
3461 			/* cleanup the old d-ring */
3462 			vdc_destroy_descriptor_ring(vdcp);
3463 
3464 			/* go and start again */
3465 			vdcp->state = VDC_STATE_INIT;
3466 
3467 			break;
3468 
3469 		case VDC_STATE_DETACH:
3470 			DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n",
3471 			    vdcp->instance);
3472 
3473 			/*
3474 			 * Signal anyone waiting for connection
3475 			 * to come online
3476 			 */
3477 			cv_broadcast(&vdcp->running_cv);
3478 
3479 			while (vdcp->sync_op_pending) {
3480 				cv_signal(&vdcp->sync_pending_cv);
3481 				cv_signal(&vdcp->sync_blocked_cv);
3482 				mutex_exit(&vdcp->lock);
3483 				drv_usecwait(vdc_min_timeout_ldc);
3484 				mutex_enter(&vdcp->lock);
3485 			}
3486 
3487 			mutex_exit(&vdcp->lock);
3488 
3489 			DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n",
3490 				vdcp->instance);
3491 			thread_exit();
3492 			break;
3493 		}
3494 	}
3495 }
3496 
3497 
3498 /*
3499  * Function:
3500  *	vdc_process_data_msg()
3501  *
3502  * Description:
3503  *	This function is called by the message processing thread each time
3504  *	a message with a msgtype of VIO_TYPE_DATA is received. It will either
3505  *	be an ACK or NACK from vds[1] which vdc handles as follows.
3506  *		ACK	- wake up the waiting thread
3507  *		NACK	- resend any messages necessary
3508  *
3509  *	[1] Although the message format allows it, vds should not send a
3510  *	    VIO_SUBTYPE_INFO message to vdc asking it to read data; if for
3511  *	    some bizarre reason it does, vdc will reset the connection.
3512  *
3513  * Arguments:
3514  *	vdc	- soft state pointer for this instance of the device driver.
3515  *	msg	- the LDC message sent by vds
3516  *
3517  * Return Code:
3518  *	0	- Success.
3519  *	> 0	- error value returned by LDC
3520  */
3521 static int
3522 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg)
3523 {
3524 	int			status = 0;
3525 	vio_dring_msg_t		*dring_msg;
3526 	vdc_local_desc_t	*ldep = NULL;
3527 	int			start, end;
3528 	int			idx;
3529 
3530 	dring_msg = (vio_dring_msg_t *)msg;
3531 
3532 	ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA);
3533 	ASSERT(vdcp != NULL);
3534 
3535 	mutex_enter(&vdcp->lock);
3536 
3537 	/*
3538 	 * Check to see if the message has bogus data
3539 	 */
3540 	idx = start = dring_msg->start_idx;
3541 	end = dring_msg->end_idx;
3542 	if ((start >= vdcp->dring_len) ||
3543 	    (end >= vdcp->dring_len) || (end < -1)) {
3544 		DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n",
3545 			vdcp->instance, start, end);
3546 		mutex_exit(&vdcp->lock);
3547 		return (EINVAL);
3548 	}
3549 
3550 	/*
3551 	 * Verify that the sequence number is what vdc expects.
3552 	 */
3553 	switch (vdc_verify_seq_num(vdcp, dring_msg)) {
3554 	case VDC_SEQ_NUM_TODO:
3555 		break;	/* keep processing this message */
3556 	case VDC_SEQ_NUM_SKIP:
3557 		mutex_exit(&vdcp->lock);
3558 		return (0);
3559 	case VDC_SEQ_NUM_INVALID:
3560 		mutex_exit(&vdcp->lock);
3561 		DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance);
3562 		return (ENXIO);
3563 	}
3564 
3565 	if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) {
3566 		DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance);
3567 		VDC_DUMP_DRING_MSG(dring_msg);
3568 		mutex_exit(&vdcp->lock);
3569 		return (EIO);
3570 
3571 	} else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) {
3572 		mutex_exit(&vdcp->lock);
3573 		return (EPROTO);
3574 	}
3575 
3576 	DTRACE_IO2(recv, vio_dring_msg_t, dring_msg, vdc_t *, vdcp);
3577 	DMSG(vdcp, 1, ": start %d end %d\n", start, end);
3578 	ASSERT(start == end);
3579 
3580 	ldep = &vdcp->local_dring[idx];
3581 
3582 	DMSG(vdcp, 1, ": state 0x%x - cb_type 0x%x\n",
3583 		ldep->dep->hdr.dstate, ldep->cb_type);
3584 
3585 	if (ldep->dep->hdr.dstate == VIO_DESC_DONE) {
3586 		struct buf *bufp;
3587 
3588 		switch (ldep->cb_type) {
3589 		case CB_SYNC:
3590 			ASSERT(vdcp->sync_op_pending);
3591 
3592 			status = vdc_depopulate_descriptor(vdcp, idx);
3593 			vdcp->sync_op_status = status;
3594 			vdcp->sync_op_pending = B_FALSE;
3595 			cv_signal(&vdcp->sync_pending_cv);
3596 			break;
3597 
3598 		case CB_STRATEGY:
3599 			bufp = ldep->cb_arg;
3600 			ASSERT(bufp != NULL);
3601 			bufp->b_resid =
3602 				bufp->b_bcount - ldep->dep->payload.nbytes;
3603 			status = ldep->dep->payload.status; /* Future:ntoh */
3604 			if (status != 0) {
3605 				DMSG(vdcp, 1, "strategy status=%d\n", status);
3606 				bioerror(bufp, status);
3607 			}
3608 			status = vdc_depopulate_descriptor(vdcp, idx);
3609 			biodone(bufp);
3610 
3611 			DMSG(vdcp, 1,
3612 			    "strategy complete req=%ld bytes resp=%ld bytes\n",
3613 			    bufp->b_bcount, ldep->dep->payload.nbytes);
3614 			break;
3615 
3616 		default:
3617 			ASSERT(0);
3618 		}
3619 	}
3620 
3621 	/* let the arrival signal propogate */
3622 	mutex_exit(&vdcp->lock);
3623 
3624 	/* probe gives the count of how many entries were processed */
3625 	DTRACE_IO2(processed, int, 1, vdc_t *, vdcp);
3626 
3627 	return (0);
3628 }
3629 
3630 /*
3631  * Function:
3632  *	vdc_process_err_msg()
3633  *
3634  * NOTE: No error messages are used as part of the vDisk protocol
3635  */
3636 static int
3637 vdc_process_err_msg(vdc_t *vdc, vio_msg_t msg)
3638 {
3639 	_NOTE(ARGUNUSED(vdc))
3640 	_NOTE(ARGUNUSED(msg))
3641 
3642 	ASSERT(msg.tag.vio_msgtype == VIO_TYPE_ERR);
3643 	DMSG(vdc, 1, "[%d] Got an ERR msg", vdc->instance);
3644 
3645 	return (ENOTSUP);
3646 }
3647 
3648 /*
3649  * Function:
3650  *	vdc_handle_ver_msg()
3651  *
3652  * Description:
3653  *
3654  * Arguments:
3655  *	vdc	- soft state pointer for this instance of the device driver.
3656  *	ver_msg	- LDC message sent by vDisk server
3657  *
3658  * Return Code:
3659  *	0	- Success
3660  */
3661 static int
3662 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg)
3663 {
3664 	int status = 0;
3665 
3666 	ASSERT(vdc != NULL);
3667 	ASSERT(mutex_owned(&vdc->lock));
3668 
3669 	if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) {
3670 		return (EPROTO);
3671 	}
3672 
3673 	if (ver_msg->dev_class != VDEV_DISK_SERVER) {
3674 		return (EINVAL);
3675 	}
3676 
3677 	switch (ver_msg->tag.vio_subtype) {
3678 	case VIO_SUBTYPE_ACK:
3679 		/*
3680 		 * We check to see if the version returned is indeed supported
3681 		 * (The server may have also adjusted the minor number downwards
3682 		 * and if so 'ver_msg' will contain the actual version agreed)
3683 		 */
3684 		if (vdc_is_supported_version(ver_msg)) {
3685 			vdc->ver.major = ver_msg->ver_major;
3686 			vdc->ver.minor = ver_msg->ver_minor;
3687 			ASSERT(vdc->ver.major > 0);
3688 		} else {
3689 			status = EPROTO;
3690 		}
3691 		break;
3692 
3693 	case VIO_SUBTYPE_NACK:
3694 		/*
3695 		 * call vdc_is_supported_version() which will return the next
3696 		 * supported version (if any) in 'ver_msg'
3697 		 */
3698 		(void) vdc_is_supported_version(ver_msg);
3699 		if (ver_msg->ver_major > 0) {
3700 			size_t len = sizeof (*ver_msg);
3701 
3702 			ASSERT(vdc->ver.major > 0);
3703 
3704 			/* reset the necessary fields and resend */
3705 			ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO;
3706 			ver_msg->dev_class = VDEV_DISK;
3707 
3708 			status = vdc_send(vdc, (caddr_t)ver_msg, &len);
3709 			DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n",
3710 					vdc->instance, status);
3711 			if (len != sizeof (*ver_msg))
3712 				status = EBADMSG;
3713 		} else {
3714 			DMSG(vdc, 0, "[%d] No common version with "
3715 					"vDisk server", vdc->instance);
3716 			status = ENOTSUP;
3717 		}
3718 
3719 		break;
3720 	case VIO_SUBTYPE_INFO:
3721 		/*
3722 		 * Handle the case where vds starts handshake
3723 		 * (for now only vdc is the instigatior)
3724 		 */
3725 		status = ENOTSUP;
3726 		break;
3727 
3728 	default:
3729 		status = EINVAL;
3730 		break;
3731 	}
3732 
3733 	return (status);
3734 }
3735 
3736 /*
3737  * Function:
3738  *	vdc_handle_attr_msg()
3739  *
3740  * Description:
3741  *
3742  * Arguments:
3743  *	vdc	- soft state pointer for this instance of the device driver.
3744  *	attr_msg	- LDC message sent by vDisk server
3745  *
3746  * Return Code:
3747  *	0	- Success
3748  */
3749 static int
3750 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg)
3751 {
3752 	int status = 0;
3753 
3754 	ASSERT(vdc != NULL);
3755 	ASSERT(mutex_owned(&vdc->lock));
3756 
3757 	if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) {
3758 		return (EPROTO);
3759 	}
3760 
3761 	switch (attr_msg->tag.vio_subtype) {
3762 	case VIO_SUBTYPE_ACK:
3763 		/*
3764 		 * We now verify the attributes sent by vds.
3765 		 */
3766 		vdc->vdisk_size = attr_msg->vdisk_size;
3767 		vdc->vdisk_type = attr_msg->vdisk_type;
3768 
3769 		DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n",
3770 			vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz);
3771 		DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n",
3772 			vdc->instance, vdc->block_size,
3773 			attr_msg->vdisk_block_size);
3774 
3775 		/*
3776 		 * We don't know at compile time what the vDisk server will
3777 		 * think are good values but we apply an large (arbitrary)
3778 		 * upper bound to prevent memory exhaustion in vdc if it was
3779 		 * allocating a DRing based of huge values sent by the server.
3780 		 * We probably will never exceed this except if the message
3781 		 * was garbage.
3782 		 */
3783 		if ((attr_msg->max_xfer_sz * attr_msg->vdisk_block_size) <=
3784 				(PAGESIZE * DEV_BSIZE)) {
3785 			vdc->max_xfer_sz = attr_msg->max_xfer_sz;
3786 			vdc->block_size = attr_msg->vdisk_block_size;
3787 		} else {
3788 			DMSG(vdc, 0, "[%d] vds block transfer size too big;"
3789 				" using max supported by vdc", vdc->instance);
3790 		}
3791 
3792 		if ((attr_msg->xfer_mode != VIO_DRING_MODE) ||
3793 		    (attr_msg->vdisk_size > INT64_MAX) ||
3794 		    (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) {
3795 			DMSG(vdc, 0, "[%d] Invalid attributes from vds",
3796 					vdc->instance);
3797 			status = EINVAL;
3798 			break;
3799 		}
3800 
3801 		break;
3802 
3803 	case VIO_SUBTYPE_NACK:
3804 		/*
3805 		 * vds could not handle the attributes we sent so we
3806 		 * stop negotiating.
3807 		 */
3808 		status = EPROTO;
3809 		break;
3810 
3811 	case VIO_SUBTYPE_INFO:
3812 		/*
3813 		 * Handle the case where vds starts the handshake
3814 		 * (for now; vdc is the only supported instigatior)
3815 		 */
3816 		status = ENOTSUP;
3817 		break;
3818 
3819 	default:
3820 		status = ENOTSUP;
3821 		break;
3822 	}
3823 
3824 	return (status);
3825 }
3826 
3827 /*
3828  * Function:
3829  *	vdc_handle_dring_reg_msg()
3830  *
3831  * Description:
3832  *
3833  * Arguments:
3834  *	vdc		- soft state pointer for this instance of the driver.
3835  *	dring_msg	- LDC message sent by vDisk server
3836  *
3837  * Return Code:
3838  *	0	- Success
3839  */
3840 static int
3841 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg)
3842 {
3843 	int		status = 0;
3844 
3845 	ASSERT(vdc != NULL);
3846 	ASSERT(mutex_owned(&vdc->lock));
3847 
3848 	if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) {
3849 		return (EPROTO);
3850 	}
3851 
3852 	switch (dring_msg->tag.vio_subtype) {
3853 	case VIO_SUBTYPE_ACK:
3854 		/* save the received dring_ident */
3855 		vdc->dring_ident = dring_msg->dring_ident;
3856 		DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n",
3857 			vdc->instance, vdc->dring_ident);
3858 		break;
3859 
3860 	case VIO_SUBTYPE_NACK:
3861 		/*
3862 		 * vds could not handle the DRing info we sent so we
3863 		 * stop negotiating.
3864 		 */
3865 		DMSG(vdc, 0, "[%d] server could not register DRing\n",
3866 		    vdc->instance);
3867 		status = EPROTO;
3868 		break;
3869 
3870 	case VIO_SUBTYPE_INFO:
3871 		/*
3872 		 * Handle the case where vds starts handshake
3873 		 * (for now only vdc is the instigatior)
3874 		 */
3875 		status = ENOTSUP;
3876 		break;
3877 	default:
3878 		status = ENOTSUP;
3879 	}
3880 
3881 	return (status);
3882 }
3883 
3884 /*
3885  * Function:
3886  *	vdc_verify_seq_num()
3887  *
3888  * Description:
3889  *	This functions verifies that the sequence number sent back by the vDisk
3890  *	server with the latest message is what is expected (i.e. it is greater
3891  *	than the last seq num sent by the vDisk server and less than or equal
3892  *	to the last seq num generated by vdc).
3893  *
3894  *	It then checks the request ID to see if any requests need processing
3895  *	in the DRing.
3896  *
3897  * Arguments:
3898  *	vdc		- soft state pointer for this instance of the driver.
3899  *	dring_msg	- pointer to the LDC message sent by vds
3900  *
3901  * Return Code:
3902  *	VDC_SEQ_NUM_TODO	- Message needs to be processed
3903  *	VDC_SEQ_NUM_SKIP	- Message has already been processed
3904  *	VDC_SEQ_NUM_INVALID	- The seq numbers are so out of sync,
3905  *				  vdc cannot deal with them
3906  */
3907 static int
3908 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg)
3909 {
3910 	ASSERT(vdc != NULL);
3911 	ASSERT(dring_msg != NULL);
3912 	ASSERT(mutex_owned(&vdc->lock));
3913 
3914 	/*
3915 	 * Check to see if the messages were responded to in the correct
3916 	 * order by vds.
3917 	 */
3918 	if ((dring_msg->seq_num <= vdc->seq_num_reply) ||
3919 	    (dring_msg->seq_num > vdc->seq_num)) {
3920 		DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: "
3921 			"%lu > expected <= %lu (last proc req %lu sent %lu)\n",
3922 				vdc->instance, dring_msg->seq_num,
3923 				vdc->seq_num_reply, vdc->seq_num,
3924 				vdc->req_id_proc, vdc->req_id);
3925 		return (VDC_SEQ_NUM_INVALID);
3926 	}
3927 	vdc->seq_num_reply = dring_msg->seq_num;
3928 
3929 	if (vdc->req_id_proc < vdc->req_id)
3930 		return (VDC_SEQ_NUM_TODO);
3931 	else
3932 		return (VDC_SEQ_NUM_SKIP);
3933 }
3934 
3935 
3936 /*
3937  * Function:
3938  *	vdc_is_supported_version()
3939  *
3940  * Description:
3941  *	This routine checks if the major/minor version numbers specified in
3942  *	'ver_msg' are supported. If not it finds the next version that is
3943  *	in the supported version list 'vdc_version[]' and sets the fields in
3944  *	'ver_msg' to those values
3945  *
3946  * Arguments:
3947  *	ver_msg	- LDC message sent by vDisk server
3948  *
3949  * Return Code:
3950  *	B_TRUE	- Success
3951  *	B_FALSE	- Version not supported
3952  */
3953 static boolean_t
3954 vdc_is_supported_version(vio_ver_msg_t *ver_msg)
3955 {
3956 	int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]);
3957 
3958 	for (int i = 0; i < vdc_num_versions; i++) {
3959 		ASSERT(vdc_version[i].major > 0);
3960 		ASSERT((i == 0) ||
3961 		    (vdc_version[i].major < vdc_version[i-1].major));
3962 
3963 		/*
3964 		 * If the major versions match, adjust the minor version, if
3965 		 * necessary, down to the highest value supported by this
3966 		 * client. The server should support all minor versions lower
3967 		 * than the value it sent
3968 		 */
3969 		if (ver_msg->ver_major == vdc_version[i].major) {
3970 			if (ver_msg->ver_minor > vdc_version[i].minor) {
3971 				DMSGX(0,
3972 				    "Adjusting minor version from %u to %u",
3973 				    ver_msg->ver_minor, vdc_version[i].minor);
3974 				ver_msg->ver_minor = vdc_version[i].minor;
3975 			}
3976 			return (B_TRUE);
3977 		}
3978 
3979 		/*
3980 		 * If the message contains a higher major version number, set
3981 		 * the message's major/minor versions to the current values
3982 		 * and return false, so this message will get resent with
3983 		 * these values, and the server will potentially try again
3984 		 * with the same or a lower version
3985 		 */
3986 		if (ver_msg->ver_major > vdc_version[i].major) {
3987 			ver_msg->ver_major = vdc_version[i].major;
3988 			ver_msg->ver_minor = vdc_version[i].minor;
3989 			DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n",
3990 				ver_msg->ver_major, ver_msg->ver_minor);
3991 
3992 			return (B_FALSE);
3993 		}
3994 
3995 		/*
3996 		 * Otherwise, the message's major version is less than the
3997 		 * current major version, so continue the loop to the next
3998 		 * (lower) supported version
3999 		 */
4000 	}
4001 
4002 	/*
4003 	 * No common version was found; "ground" the version pair in the
4004 	 * message to terminate negotiation
4005 	 */
4006 	ver_msg->ver_major = 0;
4007 	ver_msg->ver_minor = 0;
4008 
4009 	return (B_FALSE);
4010 }
4011 /* -------------------------------------------------------------------------- */
4012 
4013 /*
4014  * DKIO(7) support
4015  */
4016 
4017 typedef struct vdc_dk_arg {
4018 	struct dk_callback	dkc;
4019 	int			mode;
4020 	dev_t			dev;
4021 	vdc_t			*vdc;
4022 } vdc_dk_arg_t;
4023 
4024 /*
4025  * Function:
4026  * 	vdc_dkio_flush_cb()
4027  *
4028  * Description:
4029  *	This routine is a callback for DKIOCFLUSHWRITECACHE which can be called
4030  *	by kernel code.
4031  *
4032  * Arguments:
4033  *	arg	- a pointer to a vdc_dk_arg_t structure.
4034  */
4035 void
4036 vdc_dkio_flush_cb(void *arg)
4037 {
4038 	struct vdc_dk_arg	*dk_arg = (struct vdc_dk_arg *)arg;
4039 	struct dk_callback	*dkc = NULL;
4040 	vdc_t			*vdc = NULL;
4041 	int			rv;
4042 
4043 	if (dk_arg == NULL) {
4044 		cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n");
4045 		return;
4046 	}
4047 	dkc = &dk_arg->dkc;
4048 	vdc = dk_arg->vdc;
4049 	ASSERT(vdc != NULL);
4050 
4051 	rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0,
4052 	    VDCPART(dk_arg->dev), 0, CB_SYNC, 0, VIO_both_dir);
4053 	if (rv != 0) {
4054 		DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n",
4055 			vdc->instance, rv,
4056 			ddi_model_convert_from(dk_arg->mode & FMODELS));
4057 	}
4058 
4059 	/*
4060 	 * Trigger the call back to notify the caller the the ioctl call has
4061 	 * been completed.
4062 	 */
4063 	if ((dk_arg->mode & FKIOCTL) &&
4064 	    (dkc != NULL) &&
4065 	    (dkc->dkc_callback != NULL)) {
4066 		ASSERT(dkc->dkc_cookie != NULL);
4067 		(*dkc->dkc_callback)(dkc->dkc_cookie, rv);
4068 	}
4069 
4070 	/* Indicate that one less DKIO write flush is outstanding */
4071 	mutex_enter(&vdc->lock);
4072 	vdc->dkio_flush_pending--;
4073 	ASSERT(vdc->dkio_flush_pending >= 0);
4074 	mutex_exit(&vdc->lock);
4075 
4076 	/* free the mem that was allocated when the callback was dispatched */
4077 	kmem_free(arg, sizeof (vdc_dk_arg_t));
4078 }
4079 
4080 /*
4081  * This structure is used in the DKIO(7I) array below.
4082  */
4083 typedef struct vdc_dk_ioctl {
4084 	uint8_t		op;		/* VD_OP_XXX value */
4085 	int		cmd;		/* Solaris ioctl operation number */
4086 	size_t		nbytes;		/* size of structure to be copied */
4087 
4088 	/* function to convert between vDisk and Solaris structure formats */
4089 	int	(*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg,
4090 	    int mode, int dir);
4091 } vdc_dk_ioctl_t;
4092 
4093 /*
4094  * Subset of DKIO(7I) operations currently supported
4095  */
4096 static vdc_dk_ioctl_t	dk_ioctl[] = {
4097 	{VD_OP_FLUSH,		DKIOCFLUSHWRITECACHE,	sizeof (int),
4098 		vdc_null_copy_func},
4099 	{VD_OP_GET_WCE,		DKIOCGETWCE,		sizeof (int),
4100 		vdc_get_wce_convert},
4101 	{VD_OP_SET_WCE,		DKIOCSETWCE,		sizeof (int),
4102 		vdc_set_wce_convert},
4103 	{VD_OP_GET_VTOC,	DKIOCGVTOC,		sizeof (vd_vtoc_t),
4104 		vdc_get_vtoc_convert},
4105 	{VD_OP_SET_VTOC,	DKIOCSVTOC,		sizeof (vd_vtoc_t),
4106 		vdc_set_vtoc_convert},
4107 	{VD_OP_GET_DISKGEOM,	DKIOCGGEOM,		sizeof (vd_geom_t),
4108 		vdc_get_geom_convert},
4109 	{VD_OP_GET_DISKGEOM,	DKIOCG_PHYGEOM,		sizeof (vd_geom_t),
4110 		vdc_get_geom_convert},
4111 	{VD_OP_GET_DISKGEOM, 	DKIOCG_VIRTGEOM,	sizeof (vd_geom_t),
4112 		vdc_get_geom_convert},
4113 	{VD_OP_SET_DISKGEOM,	DKIOCSGEOM,		sizeof (vd_geom_t),
4114 		vdc_set_geom_convert},
4115 	{VD_OP_GET_EFI,		DKIOCGETEFI,		0,
4116 		vdc_get_efi_convert},
4117 	{VD_OP_SET_EFI,		DKIOCSETEFI,		0,
4118 		vdc_set_efi_convert},
4119 
4120 	/*
4121 	 * These particular ioctls are not sent to the server - vdc fakes up
4122 	 * the necessary info.
4123 	 */
4124 	{0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func},
4125 	{0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func},
4126 	{0, USCSICMD,	sizeof (struct uscsi_cmd), vdc_null_copy_func},
4127 	{0, DKIOCREMOVABLE, 0, vdc_null_copy_func},
4128 	{0, CDROMREADOFFSET, 0, vdc_null_copy_func}
4129 };
4130 
4131 /*
4132  * Function:
4133  *	vd_process_ioctl()
4134  *
4135  * Description:
4136  *	This routine processes disk specific ioctl calls
4137  *
4138  * Arguments:
4139  *	dev	- the device number
4140  *	cmd	- the operation [dkio(7I)] to be processed
4141  *	arg	- pointer to user provided structure
4142  *		  (contains data to be set or reference parameter for get)
4143  *	mode	- bit flag, indicating open settings, 32/64 bit type, etc
4144  *
4145  * Return Code:
4146  *	0
4147  *	EFAULT
4148  *	ENXIO
4149  *	EIO
4150  *	ENOTSUP
4151  */
4152 static int
4153 vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode)
4154 {
4155 	int		instance = VDCUNIT(dev);
4156 	vdc_t		*vdc = NULL;
4157 	int		rv = -1;
4158 	int		idx = 0;		/* index into dk_ioctl[] */
4159 	size_t		len = 0;		/* #bytes to send to vds */
4160 	size_t		alloc_len = 0;		/* #bytes to allocate mem for */
4161 	caddr_t		mem_p = NULL;
4162 	size_t		nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0]));
4163 	struct vtoc	vtoc_saved;
4164 	vdc_dk_ioctl_t	*iop;
4165 
4166 	vdc = ddi_get_soft_state(vdc_state, instance);
4167 	if (vdc == NULL) {
4168 		cmn_err(CE_NOTE, "![%d] Could not get soft state structure",
4169 		    instance);
4170 		return (ENXIO);
4171 	}
4172 
4173 	DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n",
4174 		instance, cmd, dev, ddi_model_convert_from(mode & FMODELS));
4175 
4176 	/*
4177 	 * Validate the ioctl operation to be performed.
4178 	 *
4179 	 * If we have looped through the array without finding a match then we
4180 	 * don't support this ioctl.
4181 	 */
4182 	for (idx = 0; idx < nioctls; idx++) {
4183 		if (cmd == dk_ioctl[idx].cmd)
4184 			break;
4185 	}
4186 
4187 	if (idx >= nioctls) {
4188 		DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n",
4189 		    vdc->instance, cmd);
4190 		return (ENOTSUP);
4191 	}
4192 
4193 	iop = &(dk_ioctl[idx]);
4194 
4195 	if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) {
4196 		/* size is not fixed for EFI ioctls, it depends on ioctl arg */
4197 		dk_efi_t	dk_efi;
4198 
4199 		rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode);
4200 		if (rv != 0)
4201 			return (EFAULT);
4202 
4203 		len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length;
4204 	} else {
4205 		len = iop->nbytes;
4206 	}
4207 
4208 	/*
4209 	 * Deal with the ioctls which the server does not provide. vdc can
4210 	 * fake these up and return immediately
4211 	 */
4212 	switch (cmd) {
4213 	case CDROMREADOFFSET:
4214 	case DKIOCREMOVABLE:
4215 	case USCSICMD:
4216 		return (ENOTTY);
4217 
4218 	case DKIOCINFO:
4219 		{
4220 			struct dk_cinfo	cinfo;
4221 			if (vdc->cinfo == NULL)
4222 				return (ENXIO);
4223 
4224 			bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo));
4225 			cinfo.dki_partition = VDCPART(dev);
4226 
4227 			rv = ddi_copyout(&cinfo, (void *)arg,
4228 					sizeof (struct dk_cinfo), mode);
4229 			if (rv != 0)
4230 				return (EFAULT);
4231 
4232 			return (0);
4233 		}
4234 
4235 	case DKIOCGMEDIAINFO:
4236 		{
4237 			if (vdc->minfo == NULL)
4238 				return (ENXIO);
4239 
4240 			rv = ddi_copyout(vdc->minfo, (void *)arg,
4241 					sizeof (struct dk_minfo), mode);
4242 			if (rv != 0)
4243 				return (EFAULT);
4244 
4245 			return (0);
4246 		}
4247 
4248 	case DKIOCFLUSHWRITECACHE:
4249 		{
4250 			struct dk_callback *dkc = (struct dk_callback *)arg;
4251 			vdc_dk_arg_t	*dkarg = NULL;
4252 
4253 			DMSG(vdc, 1, "[%d] Flush W$: mode %x\n",
4254 			    instance, mode);
4255 
4256 			/*
4257 			 * If the backing device is not a 'real' disk then the
4258 			 * W$ operation request to the vDisk server will fail
4259 			 * so we might as well save the cycles and return now.
4260 			 */
4261 			if (vdc->vdisk_type != VD_DISK_TYPE_DISK)
4262 				return (ENOTTY);
4263 
4264 			/*
4265 			 * If arg is NULL, then there is no callback function
4266 			 * registered and the call operates synchronously; we
4267 			 * break and continue with the rest of the function and
4268 			 * wait for vds to return (i.e. after the request to
4269 			 * vds returns successfully, all writes completed prior
4270 			 * to the ioctl will have been flushed from the disk
4271 			 * write cache to persistent media.
4272 			 *
4273 			 * If a callback function is registered, we dispatch
4274 			 * the request on a task queue and return immediately.
4275 			 * The callback will deal with informing the calling
4276 			 * thread that the flush request is completed.
4277 			 */
4278 			if (dkc == NULL)
4279 				break;
4280 
4281 			dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP);
4282 
4283 			dkarg->mode = mode;
4284 			dkarg->dev = dev;
4285 			bcopy(dkc, &dkarg->dkc, sizeof (*dkc));
4286 
4287 			mutex_enter(&vdc->lock);
4288 			vdc->dkio_flush_pending++;
4289 			dkarg->vdc = vdc;
4290 			mutex_exit(&vdc->lock);
4291 
4292 			/* put the request on a task queue */
4293 			rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb,
4294 				(void *)dkarg, DDI_SLEEP);
4295 			if (rv == NULL) {
4296 				/* clean up if dispatch fails */
4297 				mutex_enter(&vdc->lock);
4298 				vdc->dkio_flush_pending--;
4299 				kmem_free(dkarg, sizeof (vdc_dk_arg_t));
4300 			}
4301 
4302 			return (rv == NULL ? ENOMEM : 0);
4303 		}
4304 	}
4305 
4306 	/* catch programming error in vdc - should be a VD_OP_XXX ioctl */
4307 	ASSERT(iop->op != 0);
4308 
4309 	/* LDC requires that the memory being mapped is 8-byte aligned */
4310 	alloc_len = P2ROUNDUP(len, sizeof (uint64_t));
4311 	DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n",
4312 	    instance, len, alloc_len);
4313 
4314 	ASSERT(alloc_len != 0);	/* sanity check */
4315 	mem_p = kmem_zalloc(alloc_len, KM_SLEEP);
4316 
4317 	if (cmd == DKIOCSVTOC) {
4318 		/*
4319 		 * Save a copy of the current VTOC so that we can roll back
4320 		 * if the setting of the new VTOC fails.
4321 		 */
4322 		bcopy(vdc->vtoc, &vtoc_saved, sizeof (struct vtoc));
4323 	}
4324 
4325 	/*
4326 	 * Call the conversion function for this ioctl whhich if necessary
4327 	 * converts from the Solaris format to the format ARC'ed
4328 	 * as part of the vDisk protocol (FWARC 2006/195)
4329 	 */
4330 	ASSERT(iop->convert != NULL);
4331 	rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN);
4332 	if (rv != 0) {
4333 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
4334 				instance, rv, cmd);
4335 		if (mem_p != NULL)
4336 			kmem_free(mem_p, alloc_len);
4337 		return (rv);
4338 	}
4339 
4340 	/*
4341 	 * send request to vds to service the ioctl.
4342 	 */
4343 	rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len,
4344 	    VDCPART(dev), 0, CB_SYNC, (void *)(uint64_t)mode,
4345 	    VIO_both_dir);
4346 
4347 	if (rv != 0) {
4348 		/*
4349 		 * This is not necessarily an error. The ioctl could
4350 		 * be returning a value such as ENOTTY to indicate
4351 		 * that the ioctl is not applicable.
4352 		 */
4353 		DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n",
4354 			instance, rv, cmd);
4355 		if (mem_p != NULL)
4356 			kmem_free(mem_p, alloc_len);
4357 
4358 		if (cmd == DKIOCSVTOC) {
4359 			/* update of the VTOC has failed, roll back */
4360 			bcopy(&vtoc_saved, vdc->vtoc, sizeof (struct vtoc));
4361 		}
4362 
4363 		return (rv);
4364 	}
4365 
4366 	if (cmd == DKIOCSVTOC) {
4367 		/*
4368 		 * The VTOC has been changed. We need to update the device
4369 		 * nodes to handle the case where an EFI label has been
4370 		 * changed to a VTOC label. We also try and update the device
4371 		 * node properties. Failing to set the properties should
4372 		 * not cause an error to be return the caller though.
4373 		 */
4374 		vdc->vdisk_label = VD_DISK_LABEL_VTOC;
4375 		(void) vdc_create_device_nodes_vtoc(vdc);
4376 
4377 		if (vdc_create_device_nodes_props(vdc)) {
4378 			DMSG(vdc, 0, "![%d] Failed to update device nodes"
4379 			    " properties", vdc->instance);
4380 		}
4381 
4382 	} else if (cmd == DKIOCSETEFI) {
4383 		/*
4384 		 * The EFI has been changed. We need to update the device
4385 		 * nodes to handle the case where a VTOC label has been
4386 		 * changed to an EFI label. We also try and update the device
4387 		 * node properties. Failing to set the properties should
4388 		 * not cause an error to be return the caller though.
4389 		 */
4390 		struct dk_gpt *efi;
4391 		size_t efi_len;
4392 
4393 		vdc->vdisk_label = VD_DISK_LABEL_EFI;
4394 		(void) vdc_create_device_nodes_efi(vdc);
4395 
4396 		rv = vdc_efi_alloc_and_read(dev, &efi, &efi_len);
4397 
4398 		if (rv == 0) {
4399 			vdc_store_efi(vdc, efi);
4400 			rv = vdc_create_device_nodes_props(vdc);
4401 			vd_efi_free(efi, efi_len);
4402 		}
4403 
4404 		if (rv) {
4405 			DMSG(vdc, 0, "![%d] Failed to update device nodes"
4406 			    " properties", vdc->instance);
4407 		}
4408 	}
4409 
4410 	/*
4411 	 * Call the conversion function (if it exists) for this ioctl
4412 	 * which converts from the format ARC'ed as part of the vDisk
4413 	 * protocol (FWARC 2006/195) back to a format understood by
4414 	 * the rest of Solaris.
4415 	 */
4416 	rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT);
4417 	if (rv != 0) {
4418 		DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
4419 				instance, rv, cmd);
4420 		if (mem_p != NULL)
4421 			kmem_free(mem_p, alloc_len);
4422 		return (rv);
4423 	}
4424 
4425 	if (mem_p != NULL)
4426 		kmem_free(mem_p, alloc_len);
4427 
4428 	return (rv);
4429 }
4430 
4431 /*
4432  * Function:
4433  *
4434  * Description:
4435  *	This is an empty conversion function used by ioctl calls which
4436  *	do not need to convert the data being passed in/out to userland
4437  */
4438 static int
4439 vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir)
4440 {
4441 	_NOTE(ARGUNUSED(vdc))
4442 	_NOTE(ARGUNUSED(from))
4443 	_NOTE(ARGUNUSED(to))
4444 	_NOTE(ARGUNUSED(mode))
4445 	_NOTE(ARGUNUSED(dir))
4446 
4447 	return (0);
4448 }
4449 
4450 static int
4451 vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
4452     int mode, int dir)
4453 {
4454 	_NOTE(ARGUNUSED(vdc))
4455 
4456 	if (dir == VD_COPYIN)
4457 		return (0);		/* nothing to do */
4458 
4459 	if (ddi_copyout(from, to, sizeof (int), mode) != 0)
4460 		return (EFAULT);
4461 
4462 	return (0);
4463 }
4464 
4465 static int
4466 vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
4467     int mode, int dir)
4468 {
4469 	_NOTE(ARGUNUSED(vdc))
4470 
4471 	if (dir == VD_COPYOUT)
4472 		return (0);		/* nothing to do */
4473 
4474 	if (ddi_copyin(from, to, sizeof (int), mode) != 0)
4475 		return (EFAULT);
4476 
4477 	return (0);
4478 }
4479 
4480 /*
4481  * Function:
4482  *	vdc_get_vtoc_convert()
4483  *
4484  * Description:
4485  *	This routine performs the necessary convertions from the DKIOCGVTOC
4486  *	Solaris structure to the format defined in FWARC 2006/195.
4487  *
4488  *	In the struct vtoc definition, the timestamp field is marked as not
4489  *	supported so it is not part of vDisk protocol (FWARC 2006/195).
4490  *	However SVM uses that field to check it can write into the VTOC,
4491  *	so we fake up the info of that field.
4492  *
4493  * Arguments:
4494  *	vdc	- the vDisk client
4495  *	from	- the buffer containing the data to be copied from
4496  *	to	- the buffer to be copied to
4497  *	mode	- flags passed to ioctl() call
4498  *	dir	- the "direction" of the copy - VD_COPYIN or VD_COPYOUT
4499  *
4500  * Return Code:
4501  *	0	- Success
4502  *	ENXIO	- incorrect buffer passed in.
4503  *	EFAULT	- ddi_copyout routine encountered an error.
4504  */
4505 static int
4506 vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
4507 {
4508 	int		i;
4509 	void		*tmp_mem = NULL;
4510 	void		*tmp_memp;
4511 	struct vtoc	vt;
4512 	struct vtoc32	vt32;
4513 	int		copy_len = 0;
4514 	int		rv = 0;
4515 
4516 	if (dir != VD_COPYOUT)
4517 		return (0);	/* nothing to do */
4518 
4519 	if ((from == NULL) || (to == NULL))
4520 		return (ENXIO);
4521 
4522 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
4523 		copy_len = sizeof (struct vtoc32);
4524 	else
4525 		copy_len = sizeof (struct vtoc);
4526 
4527 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
4528 
4529 	VD_VTOC2VTOC((vd_vtoc_t *)from, &vt);
4530 
4531 	/* fake the VTOC timestamp field */
4532 	for (i = 0; i < V_NUMPAR; i++) {
4533 		vt.timestamp[i] = vdc->vtoc->timestamp[i];
4534 	}
4535 
4536 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
4537 		vtoctovtoc32(vt, vt32);
4538 		tmp_memp = &vt32;
4539 	} else {
4540 		tmp_memp = &vt;
4541 	}
4542 	rv = ddi_copyout(tmp_memp, to, copy_len, mode);
4543 	if (rv != 0)
4544 		rv = EFAULT;
4545 
4546 	kmem_free(tmp_mem, copy_len);
4547 	return (rv);
4548 }
4549 
4550 /*
4551  * Function:
4552  *	vdc_set_vtoc_convert()
4553  *
4554  * Description:
4555  *	This routine performs the necessary convertions from the DKIOCSVTOC
4556  *	Solaris structure to the format defined in FWARC 2006/195.
4557  *
4558  * Arguments:
4559  *	vdc	- the vDisk client
4560  *	from	- Buffer with data
4561  *	to	- Buffer where data is to be copied to
4562  *	mode	- flags passed to ioctl
4563  *	dir	- direction of copy (in or out)
4564  *
4565  * Return Code:
4566  *	0	- Success
4567  *	ENXIO	- Invalid buffer passed in
4568  *	EFAULT	- ddi_copyin of data failed
4569  */
4570 static int
4571 vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
4572 {
4573 	void		*tmp_mem = NULL;
4574 	struct vtoc	vt;
4575 	struct vtoc	*vtp = &vt;
4576 	vd_vtoc_t	vtvd;
4577 	int		copy_len = 0;
4578 	int		rv = 0;
4579 
4580 	if (dir != VD_COPYIN)
4581 		return (0);	/* nothing to do */
4582 
4583 	if ((from == NULL) || (to == NULL))
4584 		return (ENXIO);
4585 
4586 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32)
4587 		copy_len = sizeof (struct vtoc32);
4588 	else
4589 		copy_len = sizeof (struct vtoc);
4590 
4591 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
4592 
4593 	rv = ddi_copyin(from, tmp_mem, copy_len, mode);
4594 	if (rv != 0) {
4595 		kmem_free(tmp_mem, copy_len);
4596 		return (EFAULT);
4597 	}
4598 
4599 	if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
4600 		vtoc32tovtoc((*(struct vtoc32 *)tmp_mem), vt);
4601 	} else {
4602 		vtp = tmp_mem;
4603 	}
4604 
4605 	/*
4606 	 * The VTOC is being changed, then vdc needs to update the copy
4607 	 * it saved in the soft state structure.
4608 	 */
4609 	bcopy(vtp, vdc->vtoc, sizeof (struct vtoc));
4610 
4611 	VTOC2VD_VTOC(vtp, &vtvd);
4612 	bcopy(&vtvd, to, sizeof (vd_vtoc_t));
4613 	kmem_free(tmp_mem, copy_len);
4614 
4615 	return (0);
4616 }
4617 
4618 /*
4619  * Function:
4620  *	vdc_get_geom_convert()
4621  *
4622  * Description:
4623  *	This routine performs the necessary convertions from the DKIOCGGEOM,
4624  *	DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format
4625  *	defined in FWARC 2006/195
4626  *
4627  * Arguments:
4628  *	vdc	- the vDisk client
4629  *	from	- Buffer with data
4630  *	to	- Buffer where data is to be copied to
4631  *	mode	- flags passed to ioctl
4632  *	dir	- direction of copy (in or out)
4633  *
4634  * Return Code:
4635  *	0	- Success
4636  *	ENXIO	- Invalid buffer passed in
4637  *	EFAULT	- ddi_copyout of data failed
4638  */
4639 static int
4640 vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
4641 {
4642 	_NOTE(ARGUNUSED(vdc))
4643 
4644 	struct dk_geom	geom;
4645 	int	copy_len = sizeof (struct dk_geom);
4646 	int	rv = 0;
4647 
4648 	if (dir != VD_COPYOUT)
4649 		return (0);	/* nothing to do */
4650 
4651 	if ((from == NULL) || (to == NULL))
4652 		return (ENXIO);
4653 
4654 	VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom);
4655 	rv = ddi_copyout(&geom, to, copy_len, mode);
4656 	if (rv != 0)
4657 		rv = EFAULT;
4658 
4659 	return (rv);
4660 }
4661 
4662 /*
4663  * Function:
4664  *	vdc_set_geom_convert()
4665  *
4666  * Description:
4667  *	This routine performs the necessary convertions from the DKIOCSGEOM
4668  *	Solaris structure to the format defined in FWARC 2006/195.
4669  *
4670  * Arguments:
4671  *	vdc	- the vDisk client
4672  *	from	- Buffer with data
4673  *	to	- Buffer where data is to be copied to
4674  *	mode	- flags passed to ioctl
4675  *	dir	- direction of copy (in or out)
4676  *
4677  * Return Code:
4678  *	0	- Success
4679  *	ENXIO	- Invalid buffer passed in
4680  *	EFAULT	- ddi_copyin of data failed
4681  */
4682 static int
4683 vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
4684 {
4685 	_NOTE(ARGUNUSED(vdc))
4686 
4687 	vd_geom_t	vdgeom;
4688 	void		*tmp_mem = NULL;
4689 	int		copy_len = sizeof (struct dk_geom);
4690 	int		rv = 0;
4691 
4692 	if (dir != VD_COPYIN)
4693 		return (0);	/* nothing to do */
4694 
4695 	if ((from == NULL) || (to == NULL))
4696 		return (ENXIO);
4697 
4698 	tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
4699 
4700 	rv = ddi_copyin(from, tmp_mem, copy_len, mode);
4701 	if (rv != 0) {
4702 		kmem_free(tmp_mem, copy_len);
4703 		return (EFAULT);
4704 	}
4705 	DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom);
4706 	bcopy(&vdgeom, to, sizeof (vdgeom));
4707 	kmem_free(tmp_mem, copy_len);
4708 
4709 	return (0);
4710 }
4711 
4712 static int
4713 vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
4714 {
4715 	_NOTE(ARGUNUSED(vdc))
4716 
4717 	vd_efi_t	*vd_efi;
4718 	dk_efi_t	dk_efi;
4719 	int		rv = 0;
4720 	void		*uaddr;
4721 
4722 	if ((from == NULL) || (to == NULL))
4723 		return (ENXIO);
4724 
4725 	if (dir == VD_COPYIN) {
4726 
4727 		vd_efi = (vd_efi_t *)to;
4728 
4729 		rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode);
4730 		if (rv != 0)
4731 			return (EFAULT);
4732 
4733 		vd_efi->lba = dk_efi.dki_lba;
4734 		vd_efi->length = dk_efi.dki_length;
4735 		bzero(vd_efi->data, vd_efi->length);
4736 
4737 	} else {
4738 
4739 		rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode);
4740 		if (rv != 0)
4741 			return (EFAULT);
4742 
4743 		uaddr = dk_efi.dki_data;
4744 
4745 		dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
4746 
4747 		VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi);
4748 
4749 		rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length,
4750 		    mode);
4751 		if (rv != 0)
4752 			return (EFAULT);
4753 
4754 		kmem_free(dk_efi.dki_data, dk_efi.dki_length);
4755 	}
4756 
4757 	return (0);
4758 }
4759 
4760 static int
4761 vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
4762 {
4763 	_NOTE(ARGUNUSED(vdc))
4764 
4765 	dk_efi_t	dk_efi;
4766 	void		*uaddr;
4767 
4768 	if (dir == VD_COPYOUT)
4769 		return (0);	/* nothing to do */
4770 
4771 	if ((from == NULL) || (to == NULL))
4772 		return (ENXIO);
4773 
4774 	if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0)
4775 		return (EFAULT);
4776 
4777 	uaddr = dk_efi.dki_data;
4778 
4779 	dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
4780 
4781 	if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0)
4782 		return (EFAULT);
4783 
4784 	DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to);
4785 
4786 	kmem_free(dk_efi.dki_data, dk_efi.dki_length);
4787 
4788 	return (0);
4789 }
4790 
4791 /*
4792  * Function:
4793  *	vdc_create_fake_geometry()
4794  *
4795  * Description:
4796  *	This routine fakes up the disk info needed for some DKIO ioctls.
4797  *		- DKIOCINFO
4798  *		- DKIOCGMEDIAINFO
4799  *
4800  *	[ just like lofi(7D) and ramdisk(7D) ]
4801  *
4802  * Arguments:
4803  *	vdc	- soft state pointer for this instance of the device driver.
4804  *
4805  * Return Code:
4806  *	0	- Success
4807  */
4808 static int
4809 vdc_create_fake_geometry(vdc_t *vdc)
4810 {
4811 	ASSERT(vdc != NULL);
4812 
4813 	/*
4814 	 * Check if max_xfer_sz and vdisk_size are valid
4815 	 */
4816 	if (vdc->vdisk_size == 0 || vdc->max_xfer_sz == 0)
4817 		return (EIO);
4818 
4819 	/*
4820 	 * DKIOCINFO support
4821 	 */
4822 	vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
4823 
4824 	(void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME);
4825 	(void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME);
4826 	/* max_xfer_sz is #blocks so we don't need to divide by DEV_BSIZE */
4827 	vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz;
4828 	vdc->cinfo->dki_ctype = DKC_SCSI_CCS;
4829 	vdc->cinfo->dki_flags = DKI_FMTVOL;
4830 	vdc->cinfo->dki_cnum = 0;
4831 	vdc->cinfo->dki_addr = 0;
4832 	vdc->cinfo->dki_space = 0;
4833 	vdc->cinfo->dki_prio = 0;
4834 	vdc->cinfo->dki_vec = 0;
4835 	vdc->cinfo->dki_unit = vdc->instance;
4836 	vdc->cinfo->dki_slave = 0;
4837 	/*
4838 	 * The partition number will be created on the fly depending on the
4839 	 * actual slice (i.e. minor node) that is used to request the data.
4840 	 */
4841 	vdc->cinfo->dki_partition = 0;
4842 
4843 	/*
4844 	 * DKIOCGMEDIAINFO support
4845 	 */
4846 	if (vdc->minfo == NULL)
4847 		vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
4848 	vdc->minfo->dki_media_type = DK_FIXED_DISK;
4849 	vdc->minfo->dki_capacity = vdc->vdisk_size;
4850 	vdc->minfo->dki_lbsize = DEV_BSIZE;
4851 
4852 	return (0);
4853 }
4854 
4855 /*
4856  * Function:
4857  *	vdc_setup_disk_layout()
4858  *
4859  * Description:
4860  *	This routine discovers all the necessary details about the "disk"
4861  *	by requesting the data that is available from the vDisk server and by
4862  *	faking up the rest of the data.
4863  *
4864  * Arguments:
4865  *	vdc	- soft state pointer for this instance of the device driver.
4866  *
4867  * Return Code:
4868  *	0	- Success
4869  */
4870 static int
4871 vdc_setup_disk_layout(vdc_t *vdc)
4872 {
4873 	buf_t	*buf;	/* BREAD requests need to be in a buf_t structure */
4874 	dev_t	dev;
4875 	int	slice = 0;
4876 	int	rv, error;
4877 
4878 	ASSERT(vdc != NULL);
4879 
4880 	if (vdc->vtoc == NULL)
4881 		vdc->vtoc = kmem_zalloc(sizeof (struct vtoc), KM_SLEEP);
4882 
4883 	dev = makedevice(ddi_driver_major(vdc->dip),
4884 				VD_MAKE_DEV(vdc->instance, 0));
4885 	rv = vd_process_ioctl(dev, DKIOCGVTOC, (caddr_t)vdc->vtoc, FKIOCTL);
4886 
4887 	if (rv && rv != ENOTSUP) {
4888 		DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)",
4889 				vdc->instance, rv);
4890 		return (rv);
4891 	}
4892 
4893 	/*
4894 	 * The process of attempting to read VTOC will initiate
4895 	 * the handshake and establish a connection. Following
4896 	 * handshake, go ahead and create geometry.
4897 	 */
4898 	error = vdc_create_fake_geometry(vdc);
4899 	if (error != 0) {
4900 		DMSG(vdc, 0, "[%d] Failed to create disk geometry (err%d)",
4901 		    vdc->instance, error);
4902 		return (error);
4903 	}
4904 
4905 	if (rv == ENOTSUP) {
4906 		/*
4907 		 * If the device does not support VTOC then we try
4908 		 * to read an EFI label.
4909 		 */
4910 		struct dk_gpt *efi;
4911 		size_t efi_len;
4912 
4913 		rv = vdc_efi_alloc_and_read(dev, &efi, &efi_len);
4914 
4915 		if (rv) {
4916 			DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)",
4917 			    vdc->instance, rv);
4918 			return (rv);
4919 		}
4920 
4921 		vdc->vdisk_label = VD_DISK_LABEL_EFI;
4922 		vdc_store_efi(vdc, efi);
4923 		vd_efi_free(efi, efi_len);
4924 
4925 		return (0);
4926 	}
4927 
4928 	vdc->vdisk_label = VD_DISK_LABEL_VTOC;
4929 
4930 	/*
4931 	 * FUTURE: This could be default way for reading the VTOC
4932 	 * from the disk as supposed to sending the VD_OP_GET_VTOC
4933 	 * to the server. Currently this is a sanity check.
4934 	 *
4935 	 * find the slice that represents the entire "disk" and use that to
4936 	 * read the disk label. The convention in Solaris is that slice 2
4937 	 * represents the whole disk so we check that it is, otherwise we
4938 	 * default to slice 0
4939 	 */
4940 	if ((vdc->vdisk_type == VD_DISK_TYPE_DISK) &&
4941 	    (vdc->vtoc->v_part[2].p_tag == V_BACKUP)) {
4942 		slice = 2;
4943 	} else {
4944 		slice = 0;
4945 	}
4946 
4947 	/*
4948 	 * Read disk label from start of disk
4949 	 */
4950 	vdc->label = kmem_zalloc(DK_LABEL_SIZE, KM_SLEEP);
4951 	buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
4952 	bioinit(buf);
4953 	buf->b_un.b_addr = (caddr_t)vdc->label;
4954 	buf->b_bcount = DK_LABEL_SIZE;
4955 	buf->b_flags = B_BUSY | B_READ;
4956 	buf->b_dev = dev;
4957 	rv = vdc_send_request(vdc, VD_OP_BREAD, (caddr_t)vdc->label,
4958 	    DK_LABEL_SIZE, slice, 0, CB_STRATEGY, buf, VIO_read_dir);
4959 	if (rv) {
4960 		DMSG(vdc, 1, "[%d] Failed to read disk block 0\n",
4961 		    vdc->instance);
4962 		kmem_free(buf, sizeof (buf_t));
4963 		return (rv);
4964 	}
4965 	rv = biowait(buf);
4966 	biofini(buf);
4967 	kmem_free(buf, sizeof (buf_t));
4968 
4969 	return (rv);
4970 }
4971 
4972 /*
4973  * Function:
4974  *	vdc_setup_devid()
4975  *
4976  * Description:
4977  *	This routine discovers the devid of a vDisk. It requests the devid of
4978  *	the underlying device from the vDisk server, builds an encapsulated
4979  *	devid based on the retrieved devid and registers that new devid to
4980  *	the vDisk.
4981  *
4982  * Arguments:
4983  *	vdc	- soft state pointer for this instance of the device driver.
4984  *
4985  * Return Code:
4986  *	0	- A devid was succesfully registered for the vDisk
4987  */
4988 static int
4989 vdc_setup_devid(vdc_t *vdc)
4990 {
4991 	int rv;
4992 	vd_devid_t *vd_devid;
4993 	size_t bufsize, bufid_len;
4994 
4995 	/*
4996 	 * At first sight, we don't know the size of the devid that the
4997 	 * server will return but this size will be encoded into the
4998 	 * reply. So we do a first request using a default size then we
4999 	 * check if this size was large enough. If not then we do a second
5000 	 * request with the correct size returned by the server. Note that
5001 	 * ldc requires size to be 8-byte aligned.
5002 	 */
5003 	bufsize = P2ROUNDUP(VD_DEVID_SIZE(VD_DEVID_DEFAULT_LEN),
5004 	    sizeof (uint64_t));
5005 	vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
5006 	bufid_len = bufsize - sizeof (vd_efi_t) - 1;
5007 
5008 	rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid,
5009 	    bufsize, 0, 0, CB_SYNC, 0, VIO_both_dir);
5010 
5011 	DMSG(vdc, 2, "sync_op returned %d\n", rv);
5012 
5013 	if (rv) {
5014 		kmem_free(vd_devid, bufsize);
5015 		return (rv);
5016 	}
5017 
5018 	if (vd_devid->length > bufid_len) {
5019 		/*
5020 		 * The returned devid is larger than the buffer used. Try again
5021 		 * with a buffer with the right size.
5022 		 */
5023 		kmem_free(vd_devid, bufsize);
5024 		bufsize = P2ROUNDUP(VD_DEVID_SIZE(vd_devid->length),
5025 		    sizeof (uint64_t));
5026 		vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
5027 		bufid_len = bufsize - sizeof (vd_efi_t) - 1;
5028 
5029 		rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID,
5030 		    (caddr_t)vd_devid, bufsize, 0, 0, CB_SYNC, 0,
5031 		    VIO_both_dir);
5032 
5033 		if (rv) {
5034 			kmem_free(vd_devid, bufsize);
5035 			return (rv);
5036 		}
5037 	}
5038 
5039 	/*
5040 	 * The virtual disk should have the same device id as the one associated
5041 	 * with the physical disk it is mapped on, otherwise sharing a disk
5042 	 * between a LDom and a non-LDom may not work (for example for a shared
5043 	 * SVM disk set).
5044 	 *
5045 	 * The DDI framework does not allow creating a device id with any
5046 	 * type so we first create a device id of type DEVID_ENCAP and then
5047 	 * we restore the orignal type of the physical device.
5048 	 */
5049 
5050 	DMSG(vdc, 2, ": devid length = %d\n", vd_devid->length);
5051 
5052 	/* build an encapsulated devid based on the returned devid */
5053 	if (ddi_devid_init(vdc->dip, DEVID_ENCAP, vd_devid->length,
5054 		vd_devid->id, &vdc->devid) != DDI_SUCCESS) {
5055 		DMSG(vdc, 1, "[%d] Fail to created devid\n", vdc->instance);
5056 		kmem_free(vd_devid, bufsize);
5057 		return (1);
5058 	}
5059 
5060 	DEVID_FORMTYPE((impl_devid_t *)vdc->devid, vd_devid->type);
5061 
5062 	ASSERT(ddi_devid_valid(vdc->devid) == DDI_SUCCESS);
5063 
5064 	kmem_free(vd_devid, bufsize);
5065 
5066 	if (ddi_devid_register(vdc->dip, vdc->devid) != DDI_SUCCESS) {
5067 		DMSG(vdc, 1, "[%d] Fail to register devid\n", vdc->instance);
5068 		return (1);
5069 	}
5070 
5071 	return (0);
5072 }
5073 
5074 static void
5075 vdc_store_efi(vdc_t *vdc, struct dk_gpt *efi)
5076 {
5077 	struct vtoc *vtoc = vdc->vtoc;
5078 
5079 	vd_efi_to_vtoc(efi, vtoc);
5080 	if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) {
5081 		/*
5082 		 * vd_efi_to_vtoc() will store information about the EFI Sun
5083 		 * reserved partition (representing the entire disk) into
5084 		 * partition 7. However single-slice device will only have
5085 		 * that single partition and the vdc driver expects to find
5086 		 * information about that partition in slice 0. So we need
5087 		 * to copy information from slice 7 to slice 0.
5088 		 */
5089 		vtoc->v_part[0].p_tag = vtoc->v_part[VD_EFI_WD_SLICE].p_tag;
5090 		vtoc->v_part[0].p_flag = vtoc->v_part[VD_EFI_WD_SLICE].p_flag;
5091 		vtoc->v_part[0].p_start = vtoc->v_part[VD_EFI_WD_SLICE].p_start;
5092 		vtoc->v_part[0].p_size =  vtoc->v_part[VD_EFI_WD_SLICE].p_size;
5093 	}
5094 }
5095