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