Home | History | Annotate | Download | only in io
      1   1991      heppo /*
      2   1991      heppo  * CDDL HEADER START
      3   1991      heppo  *
      4   1991      heppo  * The contents of this file are subject to the terms of the
      5   1991      heppo  * Common Development and Distribution License (the "License").
      6   1991      heppo  * You may not use this file except in compliance with the License.
      7   1991      heppo  *
      8   1991      heppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9   1991      heppo  * or http://www.opensolaris.org/os/licensing.
     10   1991      heppo  * See the License for the specific language governing permissions
     11   1991      heppo  * and limitations under the License.
     12   1991      heppo  *
     13   1991      heppo  * When distributing Covered Code, include this CDDL HEADER in each
     14   1991      heppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15   1991      heppo  * If applicable, add the following below this CDDL HEADER, with the
     16   1991      heppo  * fields enclosed by brackets "[]" replaced with your own identifying
     17   1991      heppo  * information: Portions Copyright [yyyy] [name of copyright owner]
     18   1991      heppo  *
     19   1991      heppo  * CDDL HEADER END
     20   1991      heppo  */
     21   1991      heppo 
     22   1991      heppo /*
     23   8528  Alexandre  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24   1991      heppo  * Use is subject to license terms.
     25   1991      heppo  */
     26   1991      heppo 
     27   1991      heppo /*
     28   1991      heppo  * LDoms virtual disk client (vdc) device driver
     29   1991      heppo  *
     30   1991      heppo  * This driver runs on a guest logical domain and communicates with the virtual
     31   1991      heppo  * disk server (vds) driver running on the service domain which is exporting
     32   1991      heppo  * virtualized "disks" to the guest logical domain.
     33   1991      heppo  *
     34   1991      heppo  * The driver can be divided into four sections:
     35   1991      heppo  *
     36   1991      heppo  * 1) generic device driver housekeeping
     37   1991      heppo  *	_init, _fini, attach, detach, ops structures, etc.
     38   1991      heppo  *
     39   1991      heppo  * 2) communication channel setup
     40   1991      heppo  *	Setup the communications link over the LDC channel that vdc uses to
     41   1991      heppo  *	talk to the vDisk server. Initialise the descriptor ring which
     42   1991      heppo  *	allows the LDC clients to transfer data via memory mappings.
     43   1991      heppo  *
     44   1991      heppo  * 3) Support exported to upper layers (filesystems, etc)
     45   1991      heppo  *	The upper layers call into vdc via strategy(9E) and DKIO(7I)
     46   1991      heppo  *	ioctl calls. vdc will copy the data to be written to the descriptor
     47   1991      heppo  *	ring or maps the buffer to store the data read by the vDisk
     48   1991      heppo  *	server into the descriptor ring. It then sends a message to the
     49   1991      heppo  *	vDisk server requesting it to complete the operation.
     50   1991      heppo  *
     51   1991      heppo  * 4) Handling responses from vDisk server.
     52   1991      heppo  *	The vDisk server will ACK some or all of the messages vdc sends to it
     53   1991      heppo  *	(this is configured during the handshake). Upon receipt of an ACK
     54   1991      heppo  *	vdc will check the descriptor ring and signal to the upper layer
     55   1991      heppo  *	code waiting on the IO.
     56   1991      heppo  */
     57   1991      heppo 
     58   2410    lm66018 #include <sys/atomic.h>
     59   1991      heppo #include <sys/conf.h>
     60   1991      heppo #include <sys/disp.h>
     61   1991      heppo #include <sys/ddi.h>
     62   1991      heppo #include <sys/dkio.h>
     63   1991      heppo #include <sys/efi_partition.h>
     64   1991      heppo #include <sys/fcntl.h>
     65   1991      heppo #include <sys/file.h>
     66   6099    lm66018 #include <sys/kstat.h>
     67   1991      heppo #include <sys/mach_descrip.h>
     68   1991      heppo #include <sys/modctl.h>
     69   1991      heppo #include <sys/mdeg.h>
     70   1991      heppo #include <sys/note.h>
     71   1991      heppo #include <sys/open.h>
     72  11004  Alexandre #include <sys/random.h>
     73   2336    narayan #include <sys/sdt.h>
     74   1991      heppo #include <sys/stat.h>
     75   1991      heppo #include <sys/sunddi.h>
     76   1991      heppo #include <sys/types.h>
     77   1991      heppo #include <sys/promif.h>
     78   5658   achartre #include <sys/var.h>
     79   1991      heppo #include <sys/vtoc.h>
     80   1991      heppo #include <sys/archsystm.h>
     81   1991      heppo #include <sys/sysmacros.h>
     82   1991      heppo 
     83   1991      heppo #include <sys/cdio.h>
     84   1991      heppo #include <sys/dktp/fdisk.h>
     85   4696   achartre #include <sys/dktp/dadkio.h>
     86  11004  Alexandre #include <sys/fs/dv_node.h>
     87   5658   achartre #include <sys/mhd.h>
     88   1991      heppo #include <sys/scsi/generic/sense.h>
     89   5658   achartre #include <sys/scsi/impl/uscsi.h>
     90   5658   achartre #include <sys/scsi/impl/services.h>
     91   5658   achartre #include <sys/scsi/targets/sddef.h>
     92   1991      heppo 
     93   1991      heppo #include <sys/ldoms.h>
     94   1991      heppo #include <sys/ldc.h>
     95   1991      heppo #include <sys/vio_common.h>
     96   1991      heppo #include <sys/vio_mailbox.h>
     97   5365    lm66018 #include <sys/vio_util.h>
     98   1991      heppo #include <sys/vdsk_common.h>
     99   1991      heppo #include <sys/vdsk_mailbox.h>
    100   1991      heppo #include <sys/vdc.h>
    101   1991      heppo 
    102   7563     Prasad #define	VD_OLDVTOC_LIMIT	0x7fffffff
    103   7563     Prasad 
    104   1991      heppo /*
    105   1991      heppo  * function prototypes
    106   1991      heppo  */
    107   1991      heppo 
    108   1991      heppo /* standard driver functions */
    109   1991      heppo static int	vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred);
    110   1991      heppo static int	vdc_close(dev_t dev, int flag, int otyp, cred_t *cred);
    111   1991      heppo static int	vdc_strategy(struct buf *buf);
    112   1991      heppo static int	vdc_print(dev_t dev, char *str);
    113   1991      heppo static int	vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
    114   1991      heppo static int	vdc_read(dev_t dev, struct uio *uio, cred_t *cred);
    115   1991      heppo static int	vdc_write(dev_t dev, struct uio *uio, cred_t *cred);
    116   1991      heppo static int	vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
    117   1991      heppo 			cred_t *credp, int *rvalp);
    118   1991      heppo static int	vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred);
    119   1991      heppo static int	vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred);
    120   1991      heppo 
    121   1991      heppo static int	vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
    122   1991      heppo 			void *arg, void **resultp);
    123   1991      heppo static int	vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
    124   1991      heppo static int	vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
    125   6623   achartre static int	vdc_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
    126   6623   achartre 		    int mod_flags, char *name, caddr_t valuep, int *lengthp);
    127   1991      heppo 
    128   1991      heppo /* setup */
    129   3078    narayan static void	vdc_min(struct buf *bufp);
    130   2032    lm66018 static int	vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen);
    131   6480    narayan static int	vdc_do_ldc_init(vdc_t *vdc, vdc_server_t *srvr);
    132   1991      heppo static int	vdc_start_ldc_connection(vdc_t *vdc);
    133   1991      heppo static int	vdc_create_device_nodes(vdc_t *vdc);
    134   2531    narayan static int	vdc_create_device_nodes_efi(vdc_t *vdc);
    135   2531    narayan static int	vdc_create_device_nodes_vtoc(vdc_t *vdc);
    136   6099    lm66018 static void	vdc_create_io_kstats(vdc_t *vdc);
    137   6099    lm66018 static void	vdc_create_err_kstats(vdc_t *vdc);
    138   6099    lm66018 static void	vdc_set_err_kstats(vdc_t *vdc);
    139   4848   achartre static int	vdc_get_md_node(dev_info_t *dip, md_t **mdpp,
    140   6480    narayan 		    mde_cookie_t *vd_nodep);
    141   6480    narayan static int	vdc_init_ports(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_nodep);
    142   6480    narayan static void	vdc_fini_ports(vdc_t *vdc);
    143   6480    narayan static void	vdc_switch_server(vdc_t *vdcp);
    144   2032    lm66018 static int	vdc_do_ldc_up(vdc_t *vdc);
    145   6480    narayan static void	vdc_terminate_ldc(vdc_t *vdc, vdc_server_t *srvr);
    146   1991      heppo static int	vdc_init_descriptor_ring(vdc_t *vdc);
    147   1991      heppo static void	vdc_destroy_descriptor_ring(vdc_t *vdc);
    148   2531    narayan static int	vdc_setup_devid(vdc_t *vdc);
    149   5874   achartre static void	vdc_store_label_efi(vdc_t *, efi_gpt_t *, efi_gpe_t *);
    150   7563     Prasad static void	vdc_store_label_vtoc(vdc_t *, struct dk_geom *,
    151   7563     Prasad 		    struct extvtoc *);
    152   4963   achartre static void	vdc_store_label_unk(vdc_t *vdc);
    153   4963   achartre static boolean_t vdc_is_opened(vdc_t *vdc);
    154   7540     Ramesh static void	vdc_update_size(vdc_t *vdc, size_t, size_t, size_t);
    155   9889      Larry static int	vdc_update_vio_bsize(vdc_t *vdc, uint32_t);
    156   1991      heppo 
    157   1991      heppo /* handshake with vds */
    158   2032    lm66018 static int		vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver);
    159   2793    lm66018 static int		vdc_ver_negotiation(vdc_t *vdcp);
    160   1991      heppo static int		vdc_init_attr_negotiation(vdc_t *vdc);
    161   2793    lm66018 static int		vdc_attr_negotiation(vdc_t *vdcp);
    162   1991      heppo static int		vdc_init_dring_negotiate(vdc_t *vdc);
    163   2793    lm66018 static int		vdc_dring_negotiation(vdc_t *vdcp);
    164   2793    lm66018 static int		vdc_send_rdx(vdc_t *vdcp);
    165   2793    lm66018 static int		vdc_rdx_exchange(vdc_t *vdcp);
    166   2032    lm66018 static boolean_t	vdc_is_supported_version(vio_ver_msg_t *ver_msg);
    167   2032    lm66018 
    168   2032    lm66018 /* processing incoming messages from vDisk server */
    169   1991      heppo static void	vdc_process_msg_thread(vdc_t *vdc);
    170   2793    lm66018 static int	vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp);
    171   2793    lm66018 
    172   1991      heppo static uint_t	vdc_handle_cb(uint64_t event, caddr_t arg);
    173   2793    lm66018 static int	vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg);
    174   2032    lm66018 static int	vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg);
    175   2032    lm66018 static int	vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg);
    176   2032    lm66018 static int	vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg);
    177   2793    lm66018 static int 	vdc_send_request(vdc_t *vdcp, int operation,
    178   2793    lm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
    179  11004  Alexandre 		    buf_t *bufp, vio_desc_direction_t dir, int flags);
    180   2793    lm66018 static int	vdc_map_to_shared_dring(vdc_t *vdcp, int idx);
    181   2793    lm66018 static int 	vdc_populate_descriptor(vdc_t *vdcp, int operation,
    182   2793    lm66018 		    caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
    183  11004  Alexandre 		    buf_t *bufp, vio_desc_direction_t dir, int flags);
    184   5658   achartre static int 	vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr,
    185  11004  Alexandre 		    size_t nbytes, int slice, diskaddr_t offset,
    186  11004  Alexandre 		    vio_desc_direction_t dir, boolean_t);
    187  11004  Alexandre static int	vdc_do_op(vdc_t *vdc, int op, caddr_t addr, size_t nbytes,
    188  11004  Alexandre 		    int slice, diskaddr_t offset, struct buf *bufp,
    189  11004  Alexandre 		    vio_desc_direction_t dir, int flags);
    190   2793    lm66018 
    191   2793    lm66018 static int	vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp);
    192  11004  Alexandre static int	vdc_drain_response(vdc_t *vdcp, struct buf *buf);
    193   1991      heppo static int	vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx);
    194   2793    lm66018 static int	vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep);
    195   2410    lm66018 static int	vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg);
    196   1991      heppo 
    197   1991      heppo /* dkio */
    198   5658   achartre static int	vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode,
    199   5658   achartre 		    int *rvalp);
    200   5874   achartre static int	vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg);
    201   4963   achartre static void	vdc_create_fake_geometry(vdc_t *vdc);
    202   4963   achartre static int	vdc_validate_geometry(vdc_t *vdc);
    203   4963   achartre static void	vdc_validate(vdc_t *vdc);
    204   4963   achartre static void	vdc_validate_task(void *arg);
    205   2336    narayan static int	vdc_null_copy_func(vdc_t *vdc, void *from, void *to,
    206   2336    narayan 		    int mode, int dir);
    207   2531    narayan static int	vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
    208   2531    narayan 		    int mode, int dir);
    209   2531    narayan static int	vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
    210   2531    narayan 		    int mode, int dir);
    211   2336    narayan static int	vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to,
    212   2336    narayan 		    int mode, int dir);
    213   2336    narayan static int	vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to,
    214   7563     Prasad 		    int mode, int dir);
    215   7563     Prasad static int	vdc_get_extvtoc_convert(vdc_t *vdc, void *from, void *to,
    216   7563     Prasad 		    int mode, int dir);
    217   7563     Prasad static int	vdc_set_extvtoc_convert(vdc_t *vdc, void *from, void *to,
    218   2336    narayan 		    int mode, int dir);
    219   2336    narayan static int	vdc_get_geom_convert(vdc_t *vdc, void *from, void *to,
    220   2336    narayan 		    int mode, int dir);
    221   2336    narayan static int	vdc_set_geom_convert(vdc_t *vdc, void *from, void *to,
    222   2336    narayan 		    int mode, int dir);
    223   2531    narayan static int	vdc_get_efi_convert(vdc_t *vdc, void *from, void *to,
    224   2531    narayan 		    int mode, int dir);
    225   2531    narayan static int	vdc_set_efi_convert(vdc_t *vdc, void *from, void *to,
    226   2336    narayan 		    int mode, int dir);
    227   1991      heppo 
    228   5658   achartre static void 	vdc_ownership_update(vdc_t *vdc, int ownership_flags);
    229  11004  Alexandre static int	vdc_access_set(vdc_t *vdc, uint64_t flags);
    230  11004  Alexandre static vdc_io_t	*vdc_eio_queue(vdc_t *vdc, int index);
    231  11004  Alexandre static void	vdc_eio_unqueue(vdc_t *vdc, clock_t deadline,
    232  11004  Alexandre 		    boolean_t complete_io);
    233  11004  Alexandre static int	vdc_eio_check(vdc_t *vdc, int flags);
    234  11004  Alexandre static void	vdc_eio_thread(void *arg);
    235   5658   achartre 
    236   1991      heppo /*
    237   1991      heppo  * Module variables
    238   1991      heppo  */
    239   2410    lm66018 
    240   2410    lm66018 /*
    241   2410    lm66018  * Tunable variables to control how long vdc waits before timing out on
    242   2410    lm66018  * various operations
    243   2410    lm66018  */
    244   3401    narayan static int	vdc_hshake_retries = 3;
    245   4848   achartre 
    246   4848   achartre static int	vdc_timeout = 0; /* units: seconds */
    247   6480    narayan static int 	vdc_ldcup_timeout = 1; /* units: seconds */
    248   2410    lm66018 
    249   2793    lm66018 static uint64_t vdc_hz_min_ldc_delay;
    250   2793    lm66018 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC;
    251   2793    lm66018 static uint64_t vdc_hz_max_ldc_delay;
    252   2793    lm66018 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC;
    253   2793    lm66018 
    254   2793    lm66018 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC;
    255   2793    lm66018 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC;
    256   2410    lm66018 
    257   2410    lm66018 /* values for dumping - need to run in a tighter loop */
    258   2410    lm66018 static uint64_t	vdc_usec_timeout_dump = 100 * MILLISEC;	/* 0.1s units: ns */
    259   2410    lm66018 static int	vdc_dump_retries = 100;
    260   2410    lm66018 
    261   5658   achartre static uint16_t	vdc_scsi_timeout = 60;	/* 60s units: seconds  */
    262   5658   achartre 
    263   5658   achartre static uint64_t vdc_ownership_delay = 6 * MICROSEC; /* 6s units: usec */
    264   5658   achartre 
    265   2410    lm66018 /* Count of the number of vdc instances attached */
    266   2410    lm66018 static volatile uint32_t	vdc_instance_count = 0;
    267   5658   achartre 
    268   5658   achartre /* Tunable to log all SCSI errors */
    269   5658   achartre static boolean_t vdc_scsi_log_error = B_FALSE;
    270   1991      heppo 
    271   1991      heppo /* Soft state pointer */
    272   1991      heppo static void	*vdc_state;
    273   1991      heppo 
    274   2793    lm66018 /*
    275   2793    lm66018  * Controlling the verbosity of the error/debug messages
    276   2793    lm66018  *
    277   2793    lm66018  * vdc_msglevel - controls level of messages
    278   2793    lm66018  * vdc_matchinst - 64-bit variable where each bit corresponds
    279   2793    lm66018  *                 to the vdc instance the vdc_msglevel applies.
    280   2793    lm66018  */
    281   2793    lm66018 int		vdc_msglevel = 0x0;
    282   2793    lm66018 uint64_t	vdc_matchinst = 0ull;
    283   1991      heppo 
    284   2032    lm66018 /*
    285   2032    lm66018  * Supported vDisk protocol version pairs.
    286   2032    lm66018  *
    287   2032    lm66018  * The first array entry is the latest and preferred version.
    288   2032    lm66018  */
    289   5365    lm66018 static const vio_ver_t	vdc_version[] = {{1, 1}};
    290   1991      heppo 
    291   1991      heppo static struct cb_ops vdc_cb_ops = {
    292   1991      heppo 	vdc_open,	/* cb_open */
    293   1991      heppo 	vdc_close,	/* cb_close */
    294   1991      heppo 	vdc_strategy,	/* cb_strategy */
    295   1991      heppo 	vdc_print,	/* cb_print */
    296   1991      heppo 	vdc_dump,	/* cb_dump */
    297   1991      heppo 	vdc_read,	/* cb_read */
    298   1991      heppo 	vdc_write,	/* cb_write */
    299   1991      heppo 	vdc_ioctl,	/* cb_ioctl */
    300   1991      heppo 	nodev,		/* cb_devmap */
    301   1991      heppo 	nodev,		/* cb_mmap */
    302   1991      heppo 	nodev,		/* cb_segmap */
    303   1991      heppo 	nochpoll,	/* cb_chpoll */
    304   6623   achartre 	vdc_prop_op,	/* cb_prop_op */
    305   1991      heppo 	NULL,		/* cb_str */
    306   1991      heppo 	D_MP | D_64BIT,	/* cb_flag */
    307   1991      heppo 	CB_REV,		/* cb_rev */
    308   1991      heppo 	vdc_aread,	/* cb_aread */
    309   1991      heppo 	vdc_awrite	/* cb_awrite */
    310   1991      heppo };
    311   1991      heppo 
    312   1991      heppo static struct dev_ops vdc_ops = {
    313   1991      heppo 	DEVO_REV,	/* devo_rev */
    314   1991      heppo 	0,		/* devo_refcnt */
    315   1991      heppo 	vdc_getinfo,	/* devo_getinfo */
    316   1991      heppo 	nulldev,	/* devo_identify */
    317   1991      heppo 	nulldev,	/* devo_probe */
    318   1991      heppo 	vdc_attach,	/* devo_attach */
    319   1991      heppo 	vdc_detach,	/* devo_detach */
    320   1991      heppo 	nodev,		/* devo_reset */
    321   1991      heppo 	&vdc_cb_ops,	/* devo_cb_ops */
    322   1991      heppo 	NULL,		/* devo_bus_ops */
    323   7656     Sherry 	nulldev,	/* devo_power */
    324   7656     Sherry 	ddi_quiesce_not_needed,	/* devo_quiesce */
    325   1991      heppo };
    326   1991      heppo 
    327   1991      heppo static struct modldrv modldrv = {
    328   1991      heppo 	&mod_driverops,
    329   4838    lm66018 	"virtual disk client",
    330   1991      heppo 	&vdc_ops,
    331   1991      heppo };
    332   1991      heppo 
    333   1991      heppo static struct modlinkage modlinkage = {
    334   1991      heppo 	MODREV_1,
    335   1991      heppo 	&modldrv,
    336   1991      heppo 	NULL
    337   1991      heppo };
    338   1991      heppo 
    339   1991      heppo /* -------------------------------------------------------------------------- */
    340   1991      heppo 
    341   1991      heppo /*
    342   1991      heppo  * Device Driver housekeeping and setup
    343   1991      heppo  */
    344   1991      heppo 
    345   1991      heppo int
    346   1991      heppo _init(void)
    347   1991      heppo {
    348   1991      heppo 	int	status;
    349   1991      heppo 
    350   1991      heppo 	if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0)
    351   1991      heppo 		return (status);
    352   1991      heppo 	if ((status = mod_install(&modlinkage)) != 0)
    353   1991      heppo 		ddi_soft_state_fini(&vdc_state);
    354   1991      heppo 	return (status);
    355   1991      heppo }
    356   1991      heppo 
    357   1991      heppo int
    358   1991      heppo _info(struct modinfo *modinfop)
    359   1991      heppo {
    360   1991      heppo 	return (mod_info(&modlinkage, modinfop));
    361   1991      heppo }
    362   1991      heppo 
    363   1991      heppo int
    364   1991      heppo _fini(void)
    365   1991      heppo {
    366   1991      heppo 	int	status;
    367   1991      heppo 
    368   1991      heppo 	if ((status = mod_remove(&modlinkage)) != 0)
    369   1991      heppo 		return (status);
    370   1991      heppo 	ddi_soft_state_fini(&vdc_state);
    371   1991      heppo 	return (0);
    372   1991      heppo }
    373   1991      heppo 
    374   1991      heppo static int
    375   1991      heppo vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,  void *arg, void **resultp)
    376   1991      heppo {
    377   1991      heppo 	_NOTE(ARGUNUSED(dip))
    378   1991      heppo 
    379   3078    narayan 	int	instance = VDCUNIT((dev_t)arg);
    380   1991      heppo 	vdc_t	*vdc = NULL;
    381   1991      heppo 
    382   1991      heppo 	switch (cmd) {
    383   1991      heppo 	case DDI_INFO_DEVT2DEVINFO:
    384   1991      heppo 		if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
    385   1991      heppo 			*resultp = NULL;
    386   1991      heppo 			return (DDI_FAILURE);
    387   1991      heppo 		}
    388   1991      heppo 		*resultp = vdc->dip;
    389   1991      heppo 		return (DDI_SUCCESS);
    390   1991      heppo 	case DDI_INFO_DEVT2INSTANCE:
    391   1991      heppo 		*resultp = (void *)(uintptr_t)instance;
    392   1991      heppo 		return (DDI_SUCCESS);
    393   1991      heppo 	default:
    394   1991      heppo 		*resultp = NULL;
    395   1991      heppo 		return (DDI_FAILURE);
    396   1991      heppo 	}
    397   1991      heppo }
    398   1991      heppo 
    399   1991      heppo static int
    400   1991      heppo vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
    401   1991      heppo {
    402  11004  Alexandre 	kt_did_t eio_tid, ownership_tid;
    403   1991      heppo 	int	instance;
    404   1991      heppo 	int	rv;
    405   6850   achartre 	vdc_server_t *srvr;
    406   1991      heppo 	vdc_t	*vdc = NULL;
    407   1991      heppo 
    408   1991      heppo 	switch (cmd) {
    409   1991      heppo 	case DDI_DETACH:
    410   1991      heppo 		/* the real work happens below */
    411   1991      heppo 		break;
    412   1991      heppo 	case DDI_SUSPEND:
    413   1991      heppo 		/* nothing to do for this non-device */
    414   1991      heppo 		return (DDI_SUCCESS);
    415   1991      heppo 	default:
    416   1991      heppo 		return (DDI_FAILURE);
    417   1991      heppo 	}
    418   1991      heppo 
    419   1991      heppo 	ASSERT(cmd == DDI_DETACH);
    420   1991      heppo 	instance = ddi_get_instance(dip);
    421   2793    lm66018 	DMSGX(1, "[%d] Entered\n", instance);
    422   2793    lm66018 
    423   2793    lm66018 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
    424   2793    lm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
    425   2793    lm66018 		return (DDI_FAILURE);
    426   2793    lm66018 	}
    427   2793    lm66018 
    428  11004  Alexandre 	if (vdc_is_opened(vdc)) {
    429   2793    lm66018 		DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance);
    430   4963   achartre 		return (DDI_FAILURE);
    431   4963   achartre 	}
    432   4963   achartre 
    433   4963   achartre 	if (vdc->dkio_flush_pending) {
    434   4963   achartre 		DMSG(vdc, 0,
    435   4963   achartre 		    "[%d] Cannot detach: %d outstanding DKIO flushes\n",
    436   4963   achartre 		    instance, vdc->dkio_flush_pending);
    437   4963   achartre 		return (DDI_FAILURE);
    438   4963   achartre 	}
    439   4963   achartre 
    440   4963   achartre 	if (vdc->validate_pending) {
    441   4963   achartre 		DMSG(vdc, 0,
    442   4963   achartre 		    "[%d] Cannot detach: %d outstanding validate request\n",
    443   4963   achartre 		    instance, vdc->validate_pending);
    444   2793    lm66018 		return (DDI_FAILURE);
    445   2793    lm66018 	}
    446   2793    lm66018 
    447   2793    lm66018 	DMSG(vdc, 0, "[%d] proceeding...\n", instance);
    448   5658   achartre 
    449   5658   achartre 	/* If we took ownership, release ownership */
    450   5658   achartre 	mutex_enter(&vdc->ownership_lock);
    451   5658   achartre 	if (vdc->ownership & VDC_OWNERSHIP_GRANTED) {
    452  11004  Alexandre 		rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR);
    453   5658   achartre 		if (rv == 0) {
    454   5658   achartre 			vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
    455   5658   achartre 		}
    456   5658   achartre 	}
    457   5658   achartre 	mutex_exit(&vdc->ownership_lock);
    458   2793    lm66018 
    459   2793    lm66018 	/* mark instance as detaching */
    460   2793    lm66018 	vdc->lifecycle	= VDC_LC_DETACHING;
    461   1991      heppo 
    462   1991      heppo 	/*
    463   6850   achartre 	 * Try and disable callbacks to prevent another handshake. We have to
    464   6850   achartre 	 * disable callbacks for all servers.
    465   6850   achartre 	 */
    466   6850   achartre 	for (srvr = vdc->server_list; srvr != NULL; srvr = srvr->next) {
    467   6850   achartre 		rv = ldc_set_cb_mode(srvr->ldc_handle, LDC_CB_DISABLE);
    468   6850   achartre 		DMSG(vdc, 0, "callback disabled (ldc=%lu, rv=%d)\n",
    469   6850   achartre 		    srvr->ldc_id, rv);
    470   6480    narayan 	}
    471   1991      heppo 
    472   1991      heppo 	if (vdc->initialized & VDC_THREAD) {
    473   2793    lm66018 		mutex_enter(&vdc->read_lock);
    474   2793    lm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
    475   2793    lm66018 		    (vdc->read_state == VDC_READ_RESET)) {
    476   2793    lm66018 			vdc->read_state = VDC_READ_RESET;
    477   2793    lm66018 			cv_signal(&vdc->read_cv);
    478   2793    lm66018 		}
    479   2793    lm66018 
    480   2793    lm66018 		mutex_exit(&vdc->read_lock);
    481   2793    lm66018 
    482   2793    lm66018 		/* wake up any thread waiting for connection to come online */
    483   2793    lm66018 		mutex_enter(&vdc->lock);
    484   2793    lm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
    485   2793    lm66018 			DMSG(vdc, 0,
    486   2793    lm66018 			    "[%d] write reset - move to resetting state...\n",
    487   2793    lm66018 			    instance);
    488   2793    lm66018 			vdc->state = VDC_STATE_RESETTING;
    489   2793    lm66018 			cv_signal(&vdc->initwait_cv);
    490  11004  Alexandre 		} else if (vdc->state == VDC_STATE_FAILED) {
    491  11004  Alexandre 			vdc->io_pending = B_TRUE;
    492  11004  Alexandre 			cv_signal(&vdc->io_pending_cv);
    493   2793    lm66018 		}
    494   2793    lm66018 		mutex_exit(&vdc->lock);
    495   2793    lm66018 
    496   2793    lm66018 		/* now wait until state transitions to VDC_STATE_DETACH */
    497   2793    lm66018 		thread_join(vdc->msg_proc_thr->t_did);
    498   2793    lm66018 		ASSERT(vdc->state == VDC_STATE_DETACH);
    499   2793    lm66018 		DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n",
    500   2793    lm66018 		    vdc->instance);
    501   1991      heppo 	}
    502   1991      heppo 
    503   1991      heppo 	mutex_enter(&vdc->lock);
    504   1991      heppo 
    505   1991      heppo 	if (vdc->initialized & VDC_DRING)
    506   1991      heppo 		vdc_destroy_descriptor_ring(vdc);
    507   1991      heppo 
    508   6480    narayan 	vdc_fini_ports(vdc);
    509   1991      heppo 
    510  11004  Alexandre 	if (vdc->eio_thread) {
    511  11004  Alexandre 		eio_tid = vdc->eio_thread->t_did;
    512   5658   achartre 		vdc->failfast_interval = 0;
    513  11004  Alexandre 		ASSERT(vdc->num_servers == 0);
    514  11004  Alexandre 		cv_signal(&vdc->eio_cv);
    515  11004  Alexandre 	} else {
    516  11004  Alexandre 		eio_tid = 0;
    517   5658   achartre 	}
    518   5658   achartre 
    519   5658   achartre 	if (vdc->ownership & VDC_OWNERSHIP_WANTED) {
    520   5658   achartre 		ownership_tid = vdc->ownership_thread->t_did;
    521   5658   achartre 		vdc->ownership = VDC_OWNERSHIP_NONE;
    522   5658   achartre 		cv_signal(&vdc->ownership_cv);
    523   5658   achartre 	} else {
    524   5658   achartre 		ownership_tid = 0;
    525   5658   achartre 	}
    526   5658   achartre 
    527   5658   achartre 	mutex_exit(&vdc->lock);
    528   5658   achartre 
    529  11004  Alexandre 	if (eio_tid != 0)
    530  11004  Alexandre 		thread_join(eio_tid);
    531   5658   achartre 
    532   5658   achartre 	if (ownership_tid != 0)
    533   5658   achartre 		thread_join(ownership_tid);
    534   1991      heppo 
    535   6623   achartre 	if (vdc->initialized & VDC_MINOR)
    536   1991      heppo 		ddi_remove_minor_node(dip, NULL);
    537   1991      heppo 
    538   6099    lm66018 	if (vdc->io_stats) {
    539   6099    lm66018 		kstat_delete(vdc->io_stats);
    540   6099    lm66018 		vdc->io_stats = NULL;
    541   6099    lm66018 	}
    542   6099    lm66018 
    543   6099    lm66018 	if (vdc->err_stats) {
    544   6099    lm66018 		kstat_delete(vdc->err_stats);
    545   6099    lm66018 		vdc->err_stats = NULL;
    546   6099    lm66018 	}
    547   6099    lm66018 
    548   1991      heppo 	if (vdc->initialized & VDC_LOCKS) {
    549   1991      heppo 		mutex_destroy(&vdc->lock);
    550   2793    lm66018 		mutex_destroy(&vdc->read_lock);
    551   5658   achartre 		mutex_destroy(&vdc->ownership_lock);
    552   2793    lm66018 		cv_destroy(&vdc->initwait_cv);
    553   2793    lm66018 		cv_destroy(&vdc->dring_free_cv);
    554   2793    lm66018 		cv_destroy(&vdc->membind_cv);
    555   2793    lm66018 		cv_destroy(&vdc->sync_blocked_cv);
    556   2793    lm66018 		cv_destroy(&vdc->read_cv);
    557   2793    lm66018 		cv_destroy(&vdc->running_cv);
    558  11004  Alexandre 		cv_destroy(&vdc->io_pending_cv);
    559   5658   achartre 		cv_destroy(&vdc->ownership_cv);
    560  11004  Alexandre 		cv_destroy(&vdc->eio_cv);
    561   1991      heppo 	}
    562   1991      heppo 
    563   1991      heppo 	if (vdc->minfo)
    564   1991      heppo 		kmem_free(vdc->minfo, sizeof (struct dk_minfo));
    565   1991      heppo 
    566   1991      heppo 	if (vdc->cinfo)
    567   1991      heppo 		kmem_free(vdc->cinfo, sizeof (struct dk_cinfo));
    568   1991      heppo 
    569   1991      heppo 	if (vdc->vtoc)
    570   7563     Prasad 		kmem_free(vdc->vtoc, sizeof (struct extvtoc));
    571   1991      heppo 
    572   4963   achartre 	if (vdc->geom)
    573   4963   achartre 		kmem_free(vdc->geom, sizeof (struct dk_geom));
    574   2032    lm66018 
    575   2531    narayan 	if (vdc->devid) {
    576   2531    narayan 		ddi_devid_unregister(dip);
    577   2531    narayan 		ddi_devid_free(vdc->devid);
    578   2531    narayan 	}
    579   2531    narayan 
    580   1991      heppo 	if (vdc->initialized & VDC_SOFT_STATE)
    581   1991      heppo 		ddi_soft_state_free(vdc_state, instance);
    582   1991      heppo 
    583   2793    lm66018 	DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc);
    584   1991      heppo 
    585   1991      heppo 	return (DDI_SUCCESS);
    586   1991      heppo }
    587   1991      heppo 
    588   1991      heppo 
    589   1991      heppo static int
    590   1991      heppo vdc_do_attach(dev_info_t *dip)
    591   1991      heppo {
    592   1991      heppo 	int		instance;
    593   1991      heppo 	vdc_t		*vdc = NULL;
    594   1991      heppo 	int		status;
    595   4848   achartre 	md_t		*mdp;
    596   6480    narayan 	mde_cookie_t	vd_node;
    597   1991      heppo 
    598   1991      heppo 	ASSERT(dip != NULL);
    599   1991      heppo 
    600   1991      heppo 	instance = ddi_get_instance(dip);
    601   1991      heppo 	if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) {
    602   2410    lm66018 		cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure",
    603   2410    lm66018 		    instance);
    604   2410    lm66018 		return (DDI_FAILURE);
    605   2410    lm66018 	}
    606   2410    lm66018 
    607   2410    lm66018 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
    608   2410    lm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
    609   1991      heppo 		return (DDI_FAILURE);
    610   1991      heppo 	}
    611   1991      heppo 
    612   1991      heppo 	/*
    613   1991      heppo 	 * We assign the value to initialized in this case to zero out the
    614   1991      heppo 	 * variable and then set bits in it to indicate what has been done
    615   1991      heppo 	 */
    616   1991      heppo 	vdc->initialized = VDC_SOFT_STATE;
    617   1991      heppo 
    618   2793    lm66018 	vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc);
    619   2793    lm66018 	vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc);
    620   1991      heppo 
    621   1991      heppo 	vdc->dip	= dip;
    622   1991      heppo 	vdc->instance	= instance;
    623   1991      heppo 	vdc->vdisk_type	= VD_DISK_TYPE_UNK;
    624   2531    narayan 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
    625   2793    lm66018 	vdc->state	= VDC_STATE_INIT;
    626   2793    lm66018 	vdc->lifecycle	= VDC_LC_ATTACHING;
    627   1991      heppo 	vdc->session_id = 0;
    628   9889      Larry 	vdc->vdisk_bsize = DEV_BSIZE;
    629   9889      Larry 	vdc->vio_bmask = 0;
    630   9889      Larry 	vdc->vio_bshift = 0;
    631   9889      Larry 	vdc->max_xfer_sz = maxphys / vdc->vdisk_bsize;
    632   1991      heppo 
    633   5365    lm66018 	/*
    634   5365    lm66018 	 * We assume, for now, that the vDisk server will export 'read'
    635   5365    lm66018 	 * operations to us at a minimum (this is needed because of checks
    636   5365    lm66018 	 * in vdc for supported operations early in the handshake process).
    637   5365    lm66018 	 * The vDisk server will return ENOTSUP if this is not the case.
    638   5365    lm66018 	 * The value will be overwritten during the attribute exchange with
    639   5365    lm66018 	 * the bitmask of operations exported by server.
    640   5365    lm66018 	 */
    641   5365    lm66018 	vdc->operations = VD_OP_MASK_READ;
    642   5365    lm66018 
    643   1991      heppo 	vdc->vtoc = NULL;
    644   4963   achartre 	vdc->geom = NULL;
    645   1991      heppo 	vdc->cinfo = NULL;
    646   1991      heppo 	vdc->minfo = NULL;
    647   1991      heppo 
    648   1991      heppo 	mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL);
    649   2793    lm66018 	cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL);
    650   2793    lm66018 	cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL);
    651   2793    lm66018 	cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL);
    652   2793    lm66018 	cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL);
    653  11004  Alexandre 	cv_init(&vdc->io_pending_cv, NULL, CV_DRIVER, NULL);
    654  11004  Alexandre 
    655  11004  Alexandre 	vdc->io_pending = B_FALSE;
    656   2793    lm66018 	vdc->threads_pending = 0;
    657   2793    lm66018 	vdc->sync_op_blocked = B_FALSE;
    658   2793    lm66018 	cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL);
    659   2793    lm66018 
    660   5658   achartre 	mutex_init(&vdc->ownership_lock, NULL, MUTEX_DRIVER, NULL);
    661   5658   achartre 	cv_init(&vdc->ownership_cv, NULL, CV_DRIVER, NULL);
    662  11004  Alexandre 	cv_init(&vdc->eio_cv, NULL, CV_DRIVER, NULL);
    663   5658   achartre 
    664   2793    lm66018 	/* init blocking msg read functionality */
    665   2793    lm66018 	mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL);
    666   2793    lm66018 	cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL);
    667   2793    lm66018 	vdc->read_state = VDC_READ_IDLE;
    668   2793    lm66018 
    669   1991      heppo 	vdc->initialized |= VDC_LOCKS;
    670   1991      heppo 
    671   4848   achartre 	/* get device and port MD node for this disk instance */
    672   6480    narayan 	if (vdc_get_md_node(dip, &mdp, &vd_node) != 0) {
    673   4848   achartre 		cmn_err(CE_NOTE, "[%d] Could not get machine description node",
    674   4848   achartre 		    instance);
    675   4848   achartre 		return (DDI_FAILURE);
    676   4848   achartre 	}
    677   4848   achartre 
    678   6480    narayan 	if (vdc_init_ports(vdc, mdp, vd_node) != 0) {
    679   6480    narayan 		cmn_err(CE_NOTE, "[%d] Error initialising ports", instance);
    680   6480    narayan 		return (DDI_FAILURE);
    681   6480    narayan 	}
    682   4848   achartre 
    683   4848   achartre 	(void) md_fini_handle(mdp);
    684   2793    lm66018 
    685   7540     Ramesh 	/* Create the kstats for saving the I/O statistics used by iostat(1M) */
    686   7540     Ramesh 	vdc_create_io_kstats(vdc);
    687   7540     Ramesh 	vdc_create_err_kstats(vdc);
    688   7540     Ramesh 
    689   7540     Ramesh 	/* Initialize remaining structures before starting the msg thread */
    690   7540     Ramesh 	vdc->vdisk_label = VD_DISK_LABEL_UNK;
    691   7563     Prasad 	vdc->vtoc = kmem_zalloc(sizeof (struct extvtoc), KM_SLEEP);
    692   7540     Ramesh 	vdc->geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
    693   7540     Ramesh 	vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
    694   7540     Ramesh 
    695   2793    lm66018 	/* initialize the thread responsible for managing state with server */
    696   2793    lm66018 	vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread,
    697   2793    lm66018 	    vdc, 0, &p0, TS_RUN, minclsyspri);
    698   2793    lm66018 	if (vdc->msg_proc_thr == NULL) {
    699   1991      heppo 		cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread",
    700   2793    lm66018 		    instance);
    701   2793    lm66018 		return (DDI_FAILURE);
    702   2793    lm66018 	}
    703   2793    lm66018 
    704  11004  Alexandre 	/*
    705  11004  Alexandre 	 * If there are multiple servers then start the eio thread.
    706  11004  Alexandre 	 */
    707  11004  Alexandre 	if (vdc->num_servers > 1) {
    708  11004  Alexandre 		vdc->eio_thread = thread_create(NULL, 0, vdc_eio_thread, vdc, 0,
    709  11004  Alexandre 		    &p0, TS_RUN, v.v_maxsyspri - 2);
    710  11004  Alexandre 		if (vdc->eio_thread == NULL) {
    711  11004  Alexandre 			cmn_err(CE_NOTE, "[%d] Failed to create error "
    712  11004  Alexandre 			    "I/O thread", instance);
    713  11004  Alexandre 			return (DDI_FAILURE);
    714  11004  Alexandre 		}
    715  11004  Alexandre 	}
    716  11004  Alexandre 
    717   1991      heppo 	vdc->initialized |= VDC_THREAD;
    718   1991      heppo 
    719   2410    lm66018 	atomic_inc_32(&vdc_instance_count);
    720   1991      heppo 
    721   2032    lm66018 	/*
    722   4963   achartre 	 * Check the disk label. This will send requests and do the handshake.
    723   4963   achartre 	 * We don't really care about the disk label now. What we really need is
    724   4963   achartre 	 * the handshake do be done so that we know the type of the disk (slice
    725   4963   achartre 	 * or full disk) and the appropriate device nodes can be created.
    726   4963   achartre 	 */
    727   4963   achartre 
    728   4963   achartre 	mutex_enter(&vdc->lock);
    729   4963   achartre 	(void) vdc_validate_geometry(vdc);
    730   4963   achartre 	mutex_exit(&vdc->lock);
    731   1991      heppo 
    732   1991      heppo 	/*
    733   6623   achartre 	 * Now that we have the device info we can create the device nodes
    734   1991      heppo 	 */
    735   1991      heppo 	status = vdc_create_device_nodes(vdc);
    736   1991      heppo 	if (status) {
    737   2793    lm66018 		DMSG(vdc, 0, "[%d] Failed to create device nodes",
    738   4696   achartre 		    instance);
    739   2793    lm66018 		goto return_status;
    740   2531    narayan 	}
    741   6099    lm66018 
    742   6099    lm66018 	/*
    743   6099    lm66018 	 * Fill in the fields of the error statistics kstat that were not
    744   6099    lm66018 	 * available when creating the kstat
    745   6099    lm66018 	 */
    746   6099    lm66018 	vdc_set_err_kstats(vdc);
    747   2531    narayan 
    748   1991      heppo 	ddi_report_dev(dip);
    749   2793    lm66018 	vdc->lifecycle	= VDC_LC_ONLINE;
    750   2793    lm66018 	DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance);
    751   2793    lm66018 
    752   2793    lm66018 return_status:
    753   2793    lm66018 	DMSG(vdc, 0, "[%d] Attach completed\n", instance);
    754   1991      heppo 	return (status);
    755   1991      heppo }
    756   1991      heppo 
    757   1991      heppo static int
    758   1991      heppo vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
    759   1991      heppo {
    760   1991      heppo 	int	status;
    761   1991      heppo 
    762   1991      heppo 	switch (cmd) {
    763   1991      heppo 	case DDI_ATTACH:
    764   1991      heppo 		if ((status = vdc_do_attach(dip)) != 0)
    765   1991      heppo 			(void) vdc_detach(dip, DDI_DETACH);
    766   1991      heppo 		return (status);
    767   1991      heppo 	case DDI_RESUME:
    768   1991      heppo 		/* nothing to do for this non-device */
    769   1991      heppo 		return (DDI_SUCCESS);
    770   1991      heppo 	default:
    771   1991      heppo 		return (DDI_FAILURE);
    772   1991      heppo 	}
    773   1991      heppo }
    774   1991      heppo 
    775   1991      heppo static int
    776   6480    narayan vdc_do_ldc_init(vdc_t *vdc, vdc_server_t *srvr)
    777   1991      heppo {
    778   1991      heppo 	int			status = 0;
    779   1991      heppo 	ldc_status_t		ldc_state;
    780   1991      heppo 	ldc_attr_t		ldc_attr;
    781   6480    narayan 
    782   6480    narayan 	ASSERT(vdc != NULL);
    783   6480    narayan 	ASSERT(srvr != NULL);
    784   1991      heppo 
    785   1991      heppo 	ldc_attr.devclass = LDC_DEV_BLK;
    786   1991      heppo 	ldc_attr.instance = vdc->instance;
    787   1991      heppo 	ldc_attr.mode = LDC_MODE_UNRELIABLE;	/* unreliable transport */
    788   2410    lm66018 	ldc_attr.mtu = VD_LDC_MTU;
    789   1991      heppo 
    790   6480    narayan 	if ((srvr->state & VDC_LDC_INIT) == 0) {
    791   6480    narayan 		status = ldc_init(srvr->ldc_id, &ldc_attr,
    792   6480    narayan 		    &srvr->ldc_handle);
    793   1991      heppo 		if (status != 0) {
    794   2793    lm66018 			DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d",
    795   6480    narayan 			    vdc->instance, srvr->ldc_id, status);
    796   1991      heppo 			return (status);
    797   1991      heppo 		}
    798   6480    narayan 		srvr->state |= VDC_LDC_INIT;
    799   6480    narayan 	}
    800   6480    narayan 	status = ldc_status(srvr->ldc_handle, &ldc_state);
    801   1991      heppo 	if (status != 0) {
    802   2793    lm66018 		DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]",
    803   4696   achartre 		    vdc->instance, status);
    804   6480    narayan 		goto init_exit;
    805   6480    narayan 	}
    806   6480    narayan 	srvr->ldc_state = ldc_state;
    807   6480    narayan 
    808   6480    narayan 	if ((srvr->state & VDC_LDC_CB) == 0) {
    809   6480    narayan 		status = ldc_reg_callback(srvr->ldc_handle, vdc_handle_cb,
    810   6480    narayan 		    (caddr_t)srvr);
    811   1991      heppo 		if (status != 0) {
    812   2793    lm66018 			DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)",
    813   4696   achartre 			    vdc->instance, status);
    814   6480    narayan 			goto init_exit;
    815   6480    narayan 		}
    816   6480    narayan 		srvr->state |= VDC_LDC_CB;
    817   6480    narayan 	}
    818   1991      heppo 
    819   1991      heppo 	/*
    820   1991      heppo 	 * At this stage we have initialised LDC, we will now try and open
    821   1991      heppo 	 * the connection.
    822   1991      heppo 	 */
    823   6480    narayan 	if (srvr->ldc_state == LDC_INIT) {
    824   6480    narayan 		status = ldc_open(srvr->ldc_handle);
    825   1991      heppo 		if (status != 0) {
    826   2793    lm66018 			DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d",
    827   6480    narayan 			    vdc->instance, srvr->ldc_id, status);
    828   6480    narayan 			goto init_exit;
    829   6480    narayan 		}
    830   6480    narayan 		srvr->state |= VDC_LDC_OPEN;
    831   6480    narayan 	}
    832   6480    narayan 
    833   6480    narayan init_exit:
    834   6480    narayan 	if (status) {
    835   6480    narayan 		vdc_terminate_ldc(vdc, srvr);
    836   1991      heppo 	}
    837   1991      heppo 
    838   1991      heppo 	return (status);
    839   1991      heppo }
    840   1991      heppo 
    841   1991      heppo static int
    842   1991      heppo vdc_start_ldc_connection(vdc_t *vdc)
    843   1991      heppo {
    844   1991      heppo 	int		status = 0;
    845   1991      heppo 
    846   1991      heppo 	ASSERT(vdc != NULL);
    847   1991      heppo 
    848   2793    lm66018 	ASSERT(MUTEX_HELD(&vdc->lock));
    849   1991      heppo 
    850   2032    lm66018 	status = vdc_do_ldc_up(vdc);
    851   1991      heppo 
    852   2793    lm66018 	DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance);
    853   2793    lm66018 
    854   2793    lm66018 	return (status);
    855   2793    lm66018 }
    856   2793    lm66018 
    857   2793    lm66018 static int
    858   2793    lm66018 vdc_stop_ldc_connection(vdc_t *vdcp)
    859   2793    lm66018 {
    860   2793    lm66018 	int	status;
    861   6480    narayan 
    862   6480    narayan 	ASSERT(vdcp != NULL);
    863   6480    narayan 
    864   6480    narayan 	ASSERT(MUTEX_HELD(&vdcp->lock));
    865   2793    lm66018 
    866   2793    lm66018 	DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n",
    867   4696   achartre 	    vdcp->state);
    868   2793    lm66018 
    869   6480    narayan 	status = ldc_down(vdcp->curr_server->ldc_handle);
    870   2793    lm66018 	DMSG(vdcp, 0, "ldc_down() = %d\n", status);
    871   2793    lm66018 
    872   2793    lm66018 	vdcp->initialized &= ~VDC_HANDSHAKE;
    873   2793    lm66018 	DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized);
    874   1991      heppo 
    875   1991      heppo 	return (status);
    876   1991      heppo }
    877   1991      heppo 
    878   6099    lm66018 static void
    879   6099    lm66018 vdc_create_io_kstats(vdc_t *vdc)
    880   6099    lm66018 {
    881   6099    lm66018 	if (vdc->io_stats != NULL) {
    882   6099    lm66018 		DMSG(vdc, 0, "[%d] I/O kstat already exists\n", vdc->instance);
    883   6099    lm66018 		return;
    884   6099    lm66018 	}
    885   6099    lm66018 
    886   6099    lm66018 	vdc->io_stats = kstat_create(VDC_DRIVER_NAME, vdc->instance, NULL,
    887   6099    lm66018 	    "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
    888   6099    lm66018 	if (vdc->io_stats != NULL) {
    889   6099    lm66018 		vdc->io_stats->ks_lock = &vdc->lock;
    890   6099    lm66018 		kstat_install(vdc->io_stats);
    891   6099    lm66018 	} else {
    892   6099    lm66018 		cmn_err(CE_NOTE, "[%d] Failed to create kstat: I/O statistics"
    893   6099    lm66018 		    " will not be gathered", vdc->instance);
    894   6099    lm66018 	}
    895   6099    lm66018 }
    896   6099    lm66018 
    897   6099    lm66018 static void
    898   6099    lm66018 vdc_create_err_kstats(vdc_t *vdc)
    899   6099    lm66018 {
    900   6099    lm66018 	vd_err_stats_t	*stp;
    901   6099    lm66018 	char	kstatmodule_err[KSTAT_STRLEN];
    902   6099    lm66018 	char	kstatname[KSTAT_STRLEN];
    903   6099    lm66018 	int	ndata = (sizeof (vd_err_stats_t) / sizeof (kstat_named_t));
    904   6099    lm66018 	int	instance = vdc->instance;
    905   6099    lm66018 
    906   6099    lm66018 	if (vdc->err_stats != NULL) {
    907   6099    lm66018 		DMSG(vdc, 0, "[%d] ERR kstat already exists\n", vdc->instance);
    908   6099    lm66018 		return;
    909   6099    lm66018 	}
    910   6099    lm66018 
    911   6099    lm66018 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
    912   6099    lm66018 	    "%serr", VDC_DRIVER_NAME);
    913   6099    lm66018 	(void) snprintf(kstatname, sizeof (kstatname),
    914   6099    lm66018 	    "%s%d,err", VDC_DRIVER_NAME, instance);
    915   6099    lm66018 
    916   6099    lm66018 	vdc->err_stats = kstat_create(kstatmodule_err, instance, kstatname,
    917   6099    lm66018 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
    918   6099    lm66018 
    919   6099    lm66018 	if (vdc->err_stats == NULL) {
    920   6099    lm66018 		cmn_err(CE_NOTE, "[%d] Failed to create kstat: Error statistics"
    921   6099    lm66018 		    " will not be gathered", instance);
    922   6099    lm66018 		return;
    923   6099    lm66018 	}
    924   6099    lm66018 
    925   6099    lm66018 	stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
    926   6099    lm66018 	kstat_named_init(&stp->vd_softerrs,	"Soft Errors",
    927   6099    lm66018 	    KSTAT_DATA_UINT32);
    928   6099    lm66018 	kstat_named_init(&stp->vd_transerrs,	"Transport Errors",
    929   6099    lm66018 	    KSTAT_DATA_UINT32);
    930   6099    lm66018 	kstat_named_init(&stp->vd_protoerrs,	"Protocol Errors",
    931   6099    lm66018 	    KSTAT_DATA_UINT32);
    932   6099    lm66018 	kstat_named_init(&stp->vd_vid,		"Vendor",
    933   6099    lm66018 	    KSTAT_DATA_CHAR);
    934   6099    lm66018 	kstat_named_init(&stp->vd_pid,		"Product",
    935   6099    lm66018 	    KSTAT_DATA_CHAR);
    936   6099    lm66018 	kstat_named_init(&stp->vd_capacity,	"Size",
    937   6099    lm66018 	    KSTAT_DATA_ULONGLONG);
    938   6099    lm66018 
    939   6099    lm66018 	vdc->err_stats->ks_update  = nulldev;
    940   6099    lm66018 
    941   6099    lm66018 	kstat_install(vdc->err_stats);
    942   6099    lm66018 }
    943   6099    lm66018 
    944   6099    lm66018 static void
    945   6099    lm66018 vdc_set_err_kstats(vdc_t *vdc)
    946   6099    lm66018 {
    947   6099    lm66018 	vd_err_stats_t  *stp;
    948   6099    lm66018 
    949   6099    lm66018 	if (vdc->err_stats == NULL)
    950   6099    lm66018 		return;
    951   6099    lm66018 
    952   6099    lm66018 	mutex_enter(&vdc->lock);
    953   6099    lm66018 
    954   6099    lm66018 	stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
    955   6099    lm66018 	ASSERT(stp != NULL);
    956   6099    lm66018 
    957   9889      Larry 	stp->vd_capacity.value.ui64 = vdc->vdisk_size * vdc->vdisk_bsize;
    958   6099    lm66018 	(void) strcpy(stp->vd_vid.value.c, "SUN");
    959   6099    lm66018 	(void) strcpy(stp->vd_pid.value.c, "VDSK");
    960   6099    lm66018 
    961   6099    lm66018 	mutex_exit(&vdc->lock);
    962   6099    lm66018 }
    963   6099    lm66018 
    964   2531    narayan static int
    965   2531    narayan vdc_create_device_nodes_efi(vdc_t *vdc)
    966   2531    narayan {
    967   2531    narayan 	ddi_remove_minor_node(vdc->dip, "h");
    968   2531    narayan 	ddi_remove_minor_node(vdc->dip, "h,raw");
    969   2531    narayan 
    970   2531    narayan 	if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK,
    971   4696   achartre 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
    972   4696   achartre 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
    973   2531    narayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'",
    974   2531    narayan 		    vdc->instance);
    975   2531    narayan 		return (EIO);
    976   2531    narayan 	}
    977   2531    narayan 
    978   2531    narayan 	/* if any device node is created we set this flag */
    979   2531    narayan 	vdc->initialized |= VDC_MINOR;
    980   2531    narayan 
    981   2531    narayan 	if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR,
    982   4696   achartre 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
    983   4696   achartre 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
    984   2531    narayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'",
    985   2531    narayan 		    vdc->instance);
    986   2531    narayan 		return (EIO);
    987   2531    narayan 	}
    988   2531    narayan 
    989   2531    narayan 	return (0);
    990   2531    narayan }
    991   2531    narayan 
    992   2531    narayan static int
    993   2531    narayan vdc_create_device_nodes_vtoc(vdc_t *vdc)
    994   2531    narayan {
    995   2531    narayan 	ddi_remove_minor_node(vdc->dip, "wd");
    996   2531    narayan 	ddi_remove_minor_node(vdc->dip, "wd,raw");
    997   2531    narayan 
    998   2531    narayan 	if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK,
    999   4696   achartre 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
   1000   4696   achartre 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
   1001   2531    narayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'",
   1002   2531    narayan 		    vdc->instance);
   1003   2531    narayan 		return (EIO);
   1004   2531    narayan 	}
   1005   2531    narayan 
   1006   2531    narayan 	/* if any device node is created we set this flag */
   1007   2531    narayan 	vdc->initialized |= VDC_MINOR;
   1008   2531    narayan 
   1009   2531    narayan 	if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR,
   1010   4696   achartre 	    VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
   1011   4696   achartre 	    DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
   1012   2531    narayan 		cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'",
   1013   2531    narayan 		    vdc->instance);
   1014   2531    narayan 		return (EIO);
   1015   2531    narayan 	}
   1016   2531    narayan 
   1017   2531    narayan 	return (0);
   1018   2531    narayan }
   1019   1991      heppo 
   1020   1991      heppo /*
   1021   1991      heppo  * Function:
   1022   1991      heppo  *	vdc_create_device_nodes
   1023   1991      heppo  *
   1024   1991      heppo  * Description:
   1025   1991      heppo  *	This function creates the block and character device nodes under
   1026   6623   achartre  *	/devices. It is called as part of the attach(9E) of the instance
   1027   6623   achartre  *	during the handshake with vds after vds has sent the attributes
   1028   6623   achartre  *	to vdc.
   1029   1991      heppo  *
   1030   1991      heppo  *	If the device is of type VD_DISK_TYPE_SLICE then the minor node
   1031   1991      heppo  *	of 2 is used in keeping with the Solaris convention that slice 2
   1032   1991      heppo  *	refers to a whole disk. Slices start at 'a'
   1033   1991      heppo  *
   1034   1991      heppo  * Parameters:
   1035   1991      heppo  *	vdc 		- soft state pointer
   1036   1991      heppo  *
   1037   1991      heppo  * Return Values
   1038   1991      heppo  *	0		- Success
   1039   1991      heppo  *	EIO		- Failed to create node
   1040   1991      heppo  */
   1041   1991      heppo static int
   1042   1991      heppo vdc_create_device_nodes(vdc_t *vdc)
   1043   1991      heppo {
   1044   2531    narayan 	char		name[sizeof ("s,raw")];
   1045   1991      heppo 	dev_info_t	*dip = NULL;
   1046   2531    narayan 	int		instance, status;
   1047   1991      heppo 	int		num_slices = 1;
   1048   1991      heppo 	int		i;
   1049   1991      heppo 
   1050   1991      heppo 	ASSERT(vdc != NULL);
   1051   1991      heppo 
   1052   1991      heppo 	instance = vdc->instance;
   1053   1991      heppo 	dip = vdc->dip;
   1054   1991      heppo 
   1055   1991      heppo 	switch (vdc->vdisk_type) {
   1056   1991      heppo 	case VD_DISK_TYPE_DISK:
   1057  11004  Alexandre 	case VD_DISK_TYPE_UNK:
   1058   1991      heppo 		num_slices = V_NUMPAR;
   1059   1991      heppo 		break;
   1060   1991      heppo 	case VD_DISK_TYPE_SLICE:
   1061   1991      heppo 		num_slices = 1;
   1062   1991      heppo 		break;
   1063  11004  Alexandre 	default:
   1064  11004  Alexandre 		ASSERT(0);
   1065   1991      heppo 	}
   1066   1991      heppo 
   1067   2531    narayan 	/*
   1068   2531    narayan 	 * Minor nodes are different for EFI disks: EFI disks do not have
   1069   2531    narayan 	 * a minor node 'g' for the minor number corresponding to slice
   1070   2531    narayan 	 * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd'
   1071   2531    narayan 	 * representing the whole disk.
   1072   2531    narayan 	 */
   1073   1991      heppo 	for (i = 0; i < num_slices; i++) {
   1074   2531    narayan 
   1075   2531    narayan 		if (i == VD_EFI_WD_SLICE) {
   1076   2531    narayan 			if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
   1077   2531    narayan 				status = vdc_create_device_nodes_efi(vdc);
   1078   2531    narayan 			else
   1079   2531    narayan 				status = vdc_create_device_nodes_vtoc(vdc);
   1080   2531    narayan 			if (status != 0)
   1081   2531    narayan 				return (status);
   1082   2531    narayan 			continue;
   1083   2531    narayan 		}
   1084   2531    narayan 
   1085   1991      heppo 		(void) snprintf(name, sizeof (name), "%c", 'a' + i);
   1086   1991      heppo 		if (ddi_create_minor_node(dip, name, S_IFBLK,
   1087   1991      heppo 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
   1088   2410    lm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'",
   1089   4696   achartre 			    instance, name);
   1090   1991      heppo 			return (EIO);
   1091   1991      heppo 		}
   1092   1991      heppo 
   1093   1991      heppo 		/* if any device node is created we set this flag */
   1094   1991      heppo 		vdc->initialized |= VDC_MINOR;
   1095   1991      heppo 
   1096   4696   achartre 		(void) snprintf(name, sizeof (name), "%c%s", 'a' + i, ",raw");
   1097   4696   achartre 
   1098   1991      heppo 		if (ddi_create_minor_node(dip, name, S_IFCHR,
   1099   1991      heppo 		    VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
   1100   2410    lm66018 			cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'",
   1101   4696   achartre 			    instance, name);
   1102   1991      heppo 			return (EIO);
   1103   1991      heppo 		}
   1104   1991      heppo 	}
   1105   1991      heppo 
   1106   1991      heppo 	return (0);
   1107   1991      heppo }
   1108   1991      heppo 
   1109   1991      heppo /*
   1110   6623   achartre  * Driver prop_op(9e) entry point function. Return the number of blocks for
   1111   6623   achartre  * the partition in question or forward the request to the property facilities.
   1112   6623   achartre  */
   1113   6623   achartre static int
   1114   6623   achartre vdc_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
   1115   6623   achartre     char *name, caddr_t valuep, int *lengthp)
   1116   6623   achartre {
   1117   6623   achartre 	int instance = ddi_get_instance(dip);
   1118   6623   achartre 	vdc_t *vdc;
   1119   6623   achartre 	uint64_t nblocks;
   1120   6623   achartre 	uint_t blksize;
   1121   6623   achartre 
   1122   6623   achartre 	vdc = ddi_get_soft_state(vdc_state, instance);
   1123   6623   achartre 
   1124   6623   achartre 	if (dev == DDI_DEV_T_ANY || vdc == NULL) {
   1125   6623   achartre 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
   1126   6623   achartre 		    name, valuep, lengthp));
   1127   6623   achartre 	}
   1128   6623   achartre 
   1129   6623   achartre 	mutex_enter(&vdc->lock);
   1130   6623   achartre 	(void) vdc_validate_geometry(vdc);
   1131   4963   achartre 	if (vdc->vdisk_label == VD_DISK_LABEL_UNK) {
   1132   6623   achartre 		mutex_exit(&vdc->lock);
   1133   6623   achartre 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
   1134   6623   achartre 		    name, valuep, lengthp));
   1135   6623   achartre 	}
   1136   6623   achartre 	nblocks = vdc->slice[VDCPART(dev)].nblocks;
   1137   9889      Larry 	blksize = vdc->vdisk_bsize;
   1138   6623   achartre 	mutex_exit(&vdc->lock);
   1139   6623   achartre 
   1140   6623   achartre 	return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op, mod_flags,
   1141   6623   achartre 	    name, valuep, lengthp, nblocks, blksize));
   1142   1991      heppo }
   1143   1991      heppo 
   1144   4963   achartre /*
   1145   4963   achartre  * Function:
   1146   4963   achartre  *	vdc_is_opened
   1147   4963   achartre  *
   1148   4963   achartre  * Description:
   1149   4963   achartre  *	This function checks if any slice of a given virtual disk is
   1150   4963   achartre  *	currently opened.
   1151   4963   achartre  *
   1152   4963   achartre  * Parameters:
   1153   4963   achartre  *	vdc 		- soft state pointer
   1154   4963   achartre  *
   1155   4963   achartre  * Return Values
   1156   4963   achartre  *	B_TRUE		- at least one slice is opened.
   1157   4963   achartre  *	B_FALSE		- no slice is opened.
   1158   4963   achartre  */
   1159   4963   achartre static boolean_t
   1160   4963   achartre vdc_is_opened(vdc_t *vdc)
   1161   4963   achartre {
   1162  11004  Alexandre 	int i;
   1163   4963   achartre 
   1164   4963   achartre 	/* check if there's any layered open */
   1165  11004  Alexandre 	for (i = 0; i < V_NUMPAR; i++) {
   1166   4963   achartre 		if (vdc->open_lyr[i] > 0)
   1167   4963   achartre 			return (B_TRUE);
   1168   4963   achartre 	}
   1169   4963   achartre 
   1170   4963   achartre 	/* check if there is any other kind of open */
   1171   4963   achartre 	for (i = 0; i < OTYPCNT; i++) {
   1172   4963   achartre 		if (vdc->open[i] != 0)
   1173   4963   achartre 			return (B_TRUE);
   1174   4963   achartre 	}
   1175   4963   achartre 
   1176   4963   achartre 	return (B_FALSE);
   1177   4963   achartre }
   1178   4963   achartre 
   1179   4963   achartre static int
   1180   4963   achartre vdc_mark_opened(vdc_t *vdc, int slice, int flag, int otyp)
   1181   4963   achartre {
   1182   4963   achartre 	uint8_t slicemask;
   1183   4963   achartre 	int i;
   1184   4963   achartre 
   1185   4963   achartre 	ASSERT(otyp < OTYPCNT);
   1186   4963   achartre 	ASSERT(slice < V_NUMPAR);
   1187   4963   achartre 	ASSERT(MUTEX_HELD(&vdc->lock));
   1188   4963   achartre 
   1189   4963   achartre 	slicemask = 1 << slice;
   1190  11004  Alexandre 
   1191  11004  Alexandre 	/*
   1192  11004  Alexandre 	 * If we have a single-slice disk which was unavailable during the
   1193  11004  Alexandre 	 * attach then a device was created for each 8 slices. Now that
   1194  11004  Alexandre 	 * the type is known, we prevent opening any slice other than 0
   1195  11004  Alexandre 	 * even if a device still exists.
   1196  11004  Alexandre 	 */
   1197  11004  Alexandre 	if (vdc->vdisk_type == VD_DISK_TYPE_SLICE && slice != 0)
   1198  11004  Alexandre 		return (EIO);
   1199   4963   achartre 
   1200   4963   achartre 	/* check if slice is already exclusively opened */
   1201   4963   achartre 	if (vdc->open_excl & slicemask)
   1202   4963   achartre 		return (EBUSY);
   1203   4963   achartre 
   1204   4963   achartre 	/* if open exclusive, check if slice is already opened */
   1205   4963   achartre 	if (flag & FEXCL) {
   1206   4963   achartre 		if (vdc->open_lyr[slice] > 0)
   1207   4963   achartre 			return (EBUSY);
   1208   4963   achartre 		for (i = 0; i < OTYPCNT; i++) {
   1209   4963   achartre 			if (vdc->open[i] & slicemask)
   1210   4963   achartre 				return (EBUSY);
   1211   4963   achartre 		}
   1212   4963   achartre 		vdc->open_excl |= slicemask;
   1213   4963   achartre 	}
   1214   4963   achartre 
   1215   4963   achartre 	/* mark slice as opened */
   1216   4963   achartre 	if (otyp == OTYP_LYR) {
   1217   4963   achartre 		vdc->open_lyr[slice]++;
   1218   4963   achartre 	} else {
   1219   4963   achartre 		vdc->open[otyp] |= slicemask;
   1220   4963   achartre 	}
   1221   4963   achartre 
   1222   4963   achartre 	return (0);
   1223   4963   achartre }
   1224   4963   achartre 
   1225   4963   achartre static void
   1226   4963   achartre vdc_mark_closed(vdc_t *vdc, int slice, int flag, int otyp)
   1227   4963   achartre {
   1228   4963   achartre 	uint8_t slicemask;
   1229   4963   achartre 
   1230   4963   achartre 	ASSERT(otyp < OTYPCNT);
   1231   4963   achartre 	ASSERT(slice < V_NUMPAR);
   1232   4963   achartre 	ASSERT(MUTEX_HELD(&vdc->lock));
   1233   4963   achartre 
   1234   4963   achartre 	slicemask = 1 << slice;
   1235   4963   achartre 
   1236   4963   achartre 	if (otyp == OTYP_LYR) {
   1237   4963   achartre 		ASSERT(vdc->open_lyr[slice] > 0);
   1238   4963   achartre 		vdc->open_lyr[slice]--;
   1239   4963   achartre 	} else {
   1240   4963   achartre 		vdc->open[otyp] &= ~slicemask;
   1241   4963   achartre 	}
   1242   4963   achartre 
   1243   4963   achartre 	if (flag & FEXCL)
   1244   4963   achartre 		vdc->open_excl &= ~slicemask;
   1245   4963   achartre }
   1246   4963   achartre 
   1247   1991      heppo static int
   1248   1991      heppo vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred)
   1249   1991      heppo {
   1250   1991      heppo 	_NOTE(ARGUNUSED(cred))
   1251   1991      heppo 
   1252   6327   achartre 	int	instance, nodelay;
   1253   4963   achartre 	int	slice, status = 0;
   1254   4963   achartre 	vdc_t	*vdc;
   1255   1991      heppo 
   1256   1991      heppo 	ASSERT(dev != NULL);
   1257   3078    narayan 	instance = VDCUNIT(*dev);
   1258   1991      heppo 
   1259   4963   achartre 	if (otyp >= OTYPCNT)
   1260   1991      heppo 		return (EINVAL);
   1261   1991      heppo 
   1262   1991      heppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
   1263   2410    lm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
   1264   1991      heppo 		return (ENXIO);
   1265   1991      heppo 	}
   1266   1991      heppo 
   1267   2793    lm66018 	DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n",
   1268   4696   achartre 	    getminor(*dev), flag, otyp);
   1269   1991      heppo 
   1270   4963   achartre 	slice = VDCPART(*dev);
   1271   4963   achartre 
   1272   6327   achartre 	nodelay = flag & (FNDELAY | FNONBLOCK);
   1273   6327   achartre 
   1274   6327   achartre 	if ((flag & FWRITE) && (!nodelay) &&
   1275   6327   achartre 	    !(VD_OP_SUPPORTED(vdc->operations, VD_OP_BWRITE))) {
   1276   6327   achartre 		return (EROFS);
   1277   6327   achartre 	}
   1278   6327   achartre 
   1279   4963   achartre 	mutex_enter(&vdc->lock);
   1280   4963   achartre 
   1281   4963   achartre 	status = vdc_mark_opened(vdc, slice, flag, otyp);
   1282   4963   achartre 
   1283   4963   achartre 	if (status != 0) {
   1284   4963   achartre 		mutex_exit(&vdc->lock);
   1285   4963   achartre 		return (status);
   1286   4963   achartre 	}
   1287   4963   achartre 
   1288  11004  Alexandre 	/*
   1289  11004  Alexandre 	 * If the disk type is unknown then we have to wait for the
   1290  11004  Alexandre 	 * handshake to complete because we don't know if the slice
   1291  11004  Alexandre 	 * device we are opening effectively exists.
   1292  11004  Alexandre 	 */
   1293  11004  Alexandre 	if (vdc->vdisk_type != VD_DISK_TYPE_UNK && nodelay) {
   1294   4963   achartre 
   1295   4963   achartre 		/* don't resubmit a validate request if there's already one */
   1296   4963   achartre 		if (vdc->validate_pending > 0) {
   1297   4963   achartre 			mutex_exit(&vdc->lock);
   1298   4963   achartre 			return (0);
   1299   4963   achartre 		}
   1300   4963   achartre 
   1301   4963   achartre 		/* call vdc_validate() asynchronously to avoid blocking */
   1302   4963   achartre 		if (taskq_dispatch(system_taskq, vdc_validate_task,
   1303   4963   achartre 		    (void *)vdc, TQ_NOSLEEP) == NULL) {
   1304   4963   achartre 			vdc_mark_closed(vdc, slice, flag, otyp);
   1305   4963   achartre 			mutex_exit(&vdc->lock);
   1306   4963   achartre 			return (ENXIO);
   1307   4963   achartre 		}
   1308   4963   achartre 
   1309   4963   achartre 		vdc->validate_pending++;
   1310   4963   achartre 		mutex_exit(&vdc->lock);
   1311   4963   achartre 		return (0);
   1312   4963   achartre 	}
   1313   4963   achartre 
   1314   4963   achartre 	mutex_exit(&vdc->lock);
   1315   4963   achartre 
   1316   4963   achartre 	vdc_validate(vdc);
   1317   4963   achartre 
   1318   4963   achartre 	mutex_enter(&vdc->lock);
   1319   4963   achartre 
   1320  11004  Alexandre 	if (vdc->vdisk_type == VD_DISK_TYPE_UNK ||
   1321  11004  Alexandre 	    (vdc->vdisk_type == VD_DISK_TYPE_SLICE && slice != 0) ||
   1322  11004  Alexandre 	    (!nodelay && (vdc->vdisk_label == VD_DISK_LABEL_UNK ||
   1323  11004  Alexandre 	    vdc->slice[slice].nblocks == 0))) {
   1324   4963   achartre 		vdc_mark_closed(vdc, slice, flag, otyp);
   1325   4963   achartre 		status = EIO;
   1326   4963   achartre 	}
   1327   4963   achartre 
   1328   4963   achartre 	mutex_exit(&vdc->lock);
   1329   4963   achartre 
   1330   4963   achartre 	return (status);
   1331   1991      heppo }
   1332   1991      heppo 
   1333   1991      heppo static int
   1334   1991      heppo vdc_close(dev_t dev, int flag, int otyp, cred_t *cred)
   1335   1991      heppo {
   1336   1991      heppo 	_NOTE(ARGUNUSED(cred))
   1337   1991      heppo 
   1338   1991      heppo 	int	instance;
   1339   4963   achartre 	int	slice;
   1340   5658   achartre 	int	rv, rval;
   1341   1991      heppo 	vdc_t	*vdc;
   1342   1991      heppo 
   1343   3078    narayan 	instance = VDCUNIT(dev);
   1344   1991      heppo 
   1345   4963   achartre 	if (otyp >= OTYPCNT)
   1346   1991      heppo 		return (EINVAL);
   1347   1991      heppo 
   1348   1991      heppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
   1349   2410    lm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
   1350   1991      heppo 		return (ENXIO);
   1351   1991      heppo 	}
   1352   1991      heppo 
   1353   2793    lm66018 	DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp);
   1354   4963   achartre 
   1355   4963   achartre 	slice = VDCPART(dev);
   1356   5188   zk194757 
   1357   5188   zk194757 	/*
   1358   5188   zk194757 	 * Attempt to flush the W$ on a close operation. If this is
   1359   5188   zk194757 	 * not a supported IOCTL command or the backing device is read-only
   1360   5188   zk194757 	 * do not fail the close operation.
   1361   5188   zk194757 	 */
   1362   5658   achartre 	rv = vd_process_ioctl(dev, DKIOCFLUSHWRITECACHE, NULL, FKIOCTL, &rval);
   1363   5188   zk194757 
   1364   5188   zk194757 	if (rv != 0 && rv != ENOTSUP && rv != ENOTTY && rv != EROFS) {
   1365   5188   zk194757 		DMSG(vdc, 0, "[%d] flush failed with error %d on close\n",
   1366   5188   zk194757 		    instance, rv);
   1367   5188   zk194757 		return (EIO);
   1368   5188   zk194757 	}
   1369   4963   achartre 
   1370   4963   achartre 	mutex_enter(&vdc->lock);
   1371   4963   achartre 	vdc_mark_closed(vdc, slice, flag, otyp);
   1372   1991      heppo 	mutex_exit(&vdc->lock);
   1373   1991      heppo 
   1374   1991      heppo 	return (0);
   1375   1991      heppo }
   1376   1991      heppo 
   1377   1991      heppo static int
   1378   1991      heppo vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
   1379   1991      heppo {
   1380   1991      heppo 	_NOTE(ARGUNUSED(credp))
   1381   5658   achartre 
   1382   5658   achartre 	return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode, rvalp));
   1383   1991      heppo }
   1384   1991      heppo 
   1385   1991      heppo static int
   1386   1991      heppo vdc_print(dev_t dev, char *str)
   1387   1991      heppo {
   1388   3078    narayan 	cmn_err(CE_NOTE, "vdc%d:  %s", VDCUNIT(dev), str);
   1389   1991      heppo 	return (0);
   1390   1991      heppo }
   1391   1991      heppo 
   1392   1991      heppo static int
   1393   1991      heppo vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
   1394   1991      heppo {
   1395  11004  Alexandre 	int	rv, flags;
   1396   2336    narayan 	size_t	nbytes = nblk * DEV_BSIZE;
   1397   3078    narayan 	int	instance = VDCUNIT(dev);
   1398   2336    narayan 	vdc_t	*vdc = NULL;
   1399   9889      Larry 	diskaddr_t vio_blkno;
   1400   1991      heppo 
   1401   1991      heppo 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
   1402   2410    lm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
   1403   1991      heppo 		return (ENXIO);
   1404   1991      heppo 	}
   1405   1991      heppo 
   1406   2793    lm66018 	DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n",
   1407   2793    lm66018 	    instance, nbytes, blkno, (void *)addr);
   1408   9889      Larry 
   1409   9889      Larry 	/* convert logical block to vio block */
   1410   9889      Larry 	if ((blkno & vdc->vio_bmask) != 0) {
   1411   9889      Larry 		DMSG(vdc, 0, "Misaligned block number (%lu)\n", blkno);
   1412   9889      Larry 		return (EINVAL);
   1413   9889      Larry 	}
   1414   9889      Larry 	vio_blkno = blkno >> vdc->vio_bshift;
   1415   9889      Larry 
   1416  11004  Alexandre 	/*
   1417  11004  Alexandre 	 * If we are panicking, we need the state to be "running" so that we
   1418  11004  Alexandre 	 * can submit I/Os, but we don't want to check for any backend error.
   1419  11004  Alexandre 	 */
   1420  11004  Alexandre 	flags = (ddi_in_panic())? VDC_OP_STATE_RUNNING : VDC_OP_NORMAL;
   1421  11004  Alexandre 
   1422  11004  Alexandre 	rv = vdc_do_op(vdc, VD_OP_BWRITE, addr, nbytes, VDCPART(dev),
   1423  11004  Alexandre 	    vio_blkno, NULL, VIO_write_dir, flags);
   1424  11004  Alexandre 
   1425   2793    lm66018 	if (rv) {
   1426   2793    lm66018 		DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv);
   1427   2793    lm66018 		return (rv);
   1428   2793    lm66018 	}
   1429   2793    lm66018 
   1430   2793    lm66018 	DMSG(vdc, 0, "[%d] End\n", instance);
   1431   2793    lm66018 
   1432   2793    lm66018 	return (0);
   1433   1991      heppo }
   1434   1991      heppo 
   1435   1991      heppo /* -------------------------------------------------------------------------- */
   1436   1991      heppo 
   1437   1991      heppo /*
   1438   1991      heppo  * Disk access routines
   1439   1991      heppo  *
   1440   1991      heppo  */
   1441   1991      heppo 
   1442   1991      heppo /*
   1443   1991      heppo  * vdc_strategy()
   1444   1991      heppo  *
   1445   1991      heppo  * Return Value:
   1446   1991      heppo  *	0:	As per strategy(9E), the strategy() function must return 0
   1447   1991      heppo  *		[ bioerror(9f) sets b_flags to the proper error code ]
   1448   1991      heppo  */
   1449   1991      heppo static int
   1450   1991      heppo vdc_strategy(struct buf *buf)
   1451   1991      heppo {
   1452   9889      Larry 	diskaddr_t vio_blkno;
   1453   2793    lm66018 	vdc_t	*vdc = NULL;
   1454   3078    narayan 	int	instance = VDCUNIT(buf->b_edev);
   1455   1991      heppo 	int	op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE;
   1456   4696   achartre 	int	slice;
   1457   2410    lm66018 
   1458   2410    lm66018 	if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
   1459   2410    lm66018 		cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
   1460   1991      heppo 		bioerror(buf, ENXIO);
   1461   1991      heppo 		biodone(buf);
   1462   1991      heppo 		return (0);
   1463   1991      heppo 	}
   1464   1991      heppo 
   1465   2793    lm66018 	DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n",
   1466   2793    lm66018 	    instance, (buf->b_flags & B_READ) ? "Read" : "Write",
   1467   2793    lm66018 	    buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr);
   1468   2336    narayan 
   1469   1991      heppo 	bp_mapin(buf);
   1470   1991      heppo 
   1471   4696   achartre 	if ((long)buf->b_private == VD_SLICE_NONE) {
   1472   4696   achartre 		/* I/O using an absolute disk offset */
   1473   4696   achartre 		slice = VD_SLICE_NONE;
   1474   4696   achartre 	} else {
   1475   4696   achartre 		slice = VDCPART(buf->b_edev);
   1476   4696   achartre 	}
   1477   4696   achartre 
   1478   9889      Larry 	/*
   1479   9889      Larry 	 * In the buf structure, b_lblkno represents a logical block number
   1480   9889      Larry 	 * using a block size of 512 bytes. For the VIO request, this block
   1481   9889      Larry 	 * number has to be converted to be represented with the block size
   1482   9889      Larry 	 * used by the VIO protocol.
   1483   9889      Larry 	 */
   1484   9889      Larry 	if ((buf->b_lblkno & vdc->vio_bmask) != 0) {
   1485   9889      Larry 		bioerror(buf, EINVAL);
   1486   9889      Larry 		biodone(buf);
   1487   9889      Larry 		return (0);
   1488   9889      Larry 	}
   1489   9889      Larry 	vio_blkno = buf->b_lblkno >> vdc->vio_bshift;
   1490   9889      Larry 
   1491  11004  Alexandre 	/* submit the I/O, any error will be reported in the buf structure */
   1492  11004  Alexandre 	(void) vdc_do_op(vdc, op, (caddr_t)buf->b_un.b_addr,
   1493   9889      Larry 	    buf->b_bcount, slice, vio_blkno,
   1494  11004  Alexandre 	    buf, (op == VD_OP_BREAD) ? VIO_read_dir : VIO_write_dir,
   1495  11004  Alexandre 	    VDC_OP_NORMAL);
   1496   2336    narayan 
   1497   1991      heppo 	return (0);
   1498   1991      heppo }
   1499   1991      heppo 
   1500   3078    narayan /*
   1501   3078    narayan  * Function:
   1502   3078    narayan  *	vdc_min
   1503   3078    narayan  *
   1504   3078    narayan  * Description:
   1505   3078    narayan  *	Routine to limit the size of a data transfer. Used in
   1506   3078    narayan  *	conjunction with physio(9F).
   1507   3078    narayan  *
   1508   3078    narayan  * Arguments:
   1509   3078    narayan  *	bp - pointer to the indicated buf(9S) struct.
   1510   3078    narayan  *
   1511   3078    narayan  */
   1512   3078    narayan static void
   1513   3078    narayan vdc_min(struct buf *bufp)
   1514   3078    narayan {
   1515   3078    narayan 	vdc_t	*vdc = NULL;
   1516   3078    narayan 	int	instance = VDCUNIT(bufp->b_edev);
   1517   3078    narayan 
   1518   3078    narayan 	vdc = ddi_get_soft_state(vdc_state, instance);
   1519   3078    narayan 	VERIFY(vdc != NULL);
   1520   3078    narayan 
   1521   9889      Larry 	if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->vdisk_bsize)) {
   1522   9889      Larry 		bufp->b_bcount = vdc->max_xfer_sz * vdc->vdisk_bsize;
   1523   3078    narayan 	}
   1524   3078    narayan }
   1525   1991      heppo 
   1526   1991      heppo static int
   1527   1991      heppo vdc_read(dev_t dev, struct uio *uio, cred_t *cred)
   1528   1991      heppo {
   1529   1991      heppo 	_NOTE(ARGUNUSED(cred))
   1530   1991      heppo 
   1531   3078    narayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
   1532   3078    narayan 	return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio));
   1533   1991      heppo }
   1534   1991      heppo 
   1535   1991      heppo static int
   1536   1991      heppo vdc_write(dev_t dev, struct uio *uio, cred_t *cred)
   1537   1991      heppo {
   1538   1991      heppo 	_NOTE(ARGUNUSED(cred))
   1539   1991      heppo 
   1540   3078    narayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
   1541   3078    narayan 	return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio));
   1542   1991      heppo }
   1543   1991      heppo 
   1544   1991      heppo static int
   1545   1991      heppo vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred)
   1546   1991      heppo {
   1547   1991      heppo 	_NOTE(ARGUNUSED(cred))
   1548   1991      heppo 
   1549   3078    narayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
   1550   3078    narayan 	return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio));
   1551   1991      heppo }
   1552   1991      heppo 
   1553   1991      heppo static int
   1554   1991      heppo vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred)
   1555   1991      heppo {
   1556   1991      heppo 	_NOTE(ARGUNUSED(cred))
   1557   1991      heppo 
   1558   3078    narayan 	DMSGX(1, "[%d] Entered", VDCUNIT(dev));
   1559   3078    narayan 	return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio));
   1560   1991      heppo }
   1561   1991      heppo 
   1562   1991      heppo 
   1563   1991      heppo /* -------------------------------------------------------------------------- */
   1564   1991      heppo 
   1565   1991      heppo /*
   1566   1991      heppo  * Handshake support
   1567   1991      heppo  */
   1568   1991      heppo 
   1569   1991      heppo 
   1570   2032    lm66018 /*
   1571   2032    lm66018  * Function:
   1572   2032    lm66018  *	vdc_init_ver_negotiation()
   1573   2032    lm66018  *
   1574   2032    lm66018  * Description:
   1575   2032    lm66018  *
   1576   2032    lm66018  * Arguments:
   1577   2032    lm66018  *	vdc	- soft state pointer for this instance of the device driver.
   1578   2032    lm66018  *
   1579   2032    lm66018  * Return Code:
   1580   2032    lm66018  *	0	- Success
   1581   2032    lm66018  */
   1582   2032    lm66018 static int
   1583   2032    lm66018 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver)
   1584   1991      heppo {
   1585   1991      heppo 	vio_ver_msg_t	pkt;
   1586   1991      heppo 	size_t		msglen = sizeof (pkt);
   1587   1991      heppo 	int		status = -1;
   1588   1991      heppo 
   1589   2410    lm66018 	ASSERT(vdc != NULL);
   1590   2410    lm66018 	ASSERT(mutex_owned(&vdc->lock));
   1591   2410    lm66018 
   1592   2793    lm66018 	DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance);
   1593   1991      heppo 
   1594   1991      heppo 	/*
   1595   1991      heppo 	 * set the Session ID to a unique value
   1596   1991      heppo 	 * (the lower 32 bits of the clock tick)
   1597   1991      heppo 	 */
   1598   1991      heppo 	vdc->session_id = ((uint32_t)gettick() & 0xffffffff);
   1599   2793    lm66018 	DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id);
   1600   1991      heppo 
   1601   1991      heppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
   1602   1991      heppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
   1603   1991      heppo 	pkt.tag.vio_subtype_env = VIO_VER_INFO;
   1604   1991      heppo 	pkt.tag.vio_sid = vdc->session_id;
   1605   1991      heppo 	pkt.dev_class = VDEV_DISK;
   1606   2032    lm66018 	pkt.ver_major = ver.major;
   1607   2032    lm66018 	pkt.ver_minor = ver.minor;
   1608   2032    lm66018 
   1609   2032    lm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
   1610   2793    lm66018 	DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n",
   1611   2793    lm66018 	    vdc->instance, status);
   1612   1991      heppo 	if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
   1613   2793    lm66018 		DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: "
   1614   6480    narayan 		    "id(%lx) rv(%d) size(%ld)", vdc->instance,
   1615   6480    narayan 		    vdc->curr_server->ldc_handle, status, msglen);
   1616   1991      heppo 		if (msglen != sizeof (vio_ver_msg_t))
   1617   1991      heppo 			status = ENOMSG;
   1618   1991      heppo 	}
   1619   1991      heppo 
   1620   1991      heppo 	return (status);
   1621   1991      heppo }
   1622   1991      heppo 
   1623   2032    lm66018 /*
   1624   2032    lm66018  * Function:
   1625   2793    lm66018  *	vdc_ver_negotiation()
   1626   2793    lm66018  *
   1627   2793    lm66018  * Description:
   1628   2793    lm66018  *
   1629   2793    lm66018  * Arguments:
   1630   2793    lm66018  *	vdcp	- soft state pointer for this instance of the device driver.
   1631   2793    lm66018  *
   1632   2793    lm66018  * Return Code:
   1633   2793    lm66018  *	0	- Success
   1634   2793    lm66018  */
   1635   2793    lm66018 static int
   1636   2793    lm66018 vdc_ver_negotiation(vdc_t *vdcp)
   1637   2793    lm66018 {
   1638   2793    lm66018 	vio_msg_t vio_msg;
   1639   2793    lm66018 	int status;
   1640   2793    lm66018 
   1641   2793    lm66018 	if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0]))
   1642   2793    lm66018 		return (status);
   1643   2793    lm66018 
   1644   2793    lm66018 	/* release lock and wait for response */
   1645   2793    lm66018 	mutex_exit(&vdcp->lock);
   1646   2793    lm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
   1647   2793    lm66018 	mutex_enter(&vdcp->lock);
   1648   2793    lm66018 	if (status) {
   1649   2793    lm66018 		DMSG(vdcp, 0,
   1650   2793    lm66018 		    "[%d] Failed waiting for Ver negotiation response, rv(%d)",
   1651   2793    lm66018 		    vdcp->instance, status);
   1652   2793    lm66018 		return (status);
   1653   2793    lm66018 	}
   1654   2793    lm66018 
   1655   2793    lm66018 	/* check type and sub_type ... */
   1656   2793    lm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
   1657   2793    lm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
   1658   2793    lm66018 		DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n",
   1659   4696   achartre 		    vdcp->instance);
   1660   2793    lm66018 		return (EPROTO);
   1661   2793    lm66018 	}
   1662   2793    lm66018 
   1663   2793    lm66018 	return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg));
   1664   2793    lm66018 }
   1665   2793    lm66018 
   1666   2793    lm66018 /*
   1667   2793    lm66018  * Function:
   1668   2032    lm66018  *	vdc_init_attr_negotiation()
   1669   2032    lm66018  *
   1670   2032    lm66018  * Description:
   1671   2032    lm66018  *
   1672   2032    lm66018  * Arguments:
   1673   2032    lm66018  *	vdc	- soft state pointer for this instance of the device driver.
   1674   2032    lm66018  *
   1675   2032    lm66018  * Return Code:
   1676   2032    lm66018  *	0	- Success
   1677   2032    lm66018  */
   1678   1991      heppo static int
   1679   1991      heppo vdc_init_attr_negotiation(vdc_t *vdc)
   1680   1991      heppo {
   1681   1991      heppo 	vd_attr_msg_t	pkt;
   1682   1991      heppo 	size_t		msglen = sizeof (pkt);
   1683   1991      heppo 	int		status;
   1684   1991      heppo 
   1685   1991      heppo 	ASSERT(vdc != NULL);
   1686   1991      heppo 	ASSERT(mutex_owned(&vdc->lock));
   1687   1991      heppo 
   1688   2793    lm66018 	DMSG(vdc, 0, "[%d] entered\n", vdc->instance);
   1689   1991      heppo 
   1690   1991      heppo 	/* fill in tag */
   1691   1991      heppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
   1692   1991      heppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
   1693   1991      heppo 	pkt.tag.vio_subtype_env = VIO_ATTR_INFO;
   1694   1991      heppo 	pkt.tag.vio_sid = vdc->session_id;
   1695   1991      heppo 	/* fill in payload */
   1696   1991      heppo 	pkt.max_xfer_sz = vdc->max_xfer_sz;
   1697   9889      Larry 	pkt.vdisk_block_size = vdc->vdisk_bsize;
   1698   5935   sb155480 	pkt.xfer_mode = VIO_DRING_MODE_V1_0;
   1699   1991      heppo 	pkt.operations = 0;	/* server will set bits of valid operations */
   1700   1991      heppo 	pkt.vdisk_type = 0;	/* server will set to valid device type */
   1701   5365    lm66018 	pkt.vdisk_media = 0;	/* server will set to valid media type */
   1702   1991      heppo 	pkt.vdisk_size = 0;	/* server will set to valid size */
   1703   1991      heppo 
   1704   2032    lm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
   1705   2793    lm66018 	DMSG(vdc, 0, "Attr info sent (status = %d)\n", status);
   1706   1991      heppo 
   1707   6952      anbui 	if ((status != 0) || (msglen != sizeof (vd_attr_msg_t))) {
   1708   2793    lm66018 		DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: "
   1709   6480    narayan 		    "id(%lx) rv(%d) size(%ld)", vdc->instance,
   1710   6480    narayan 		    vdc->curr_server->ldc_handle, status, msglen);
   1711   6952      anbui 		if (msglen != sizeof (vd_attr_msg_t))
   1712   1991      heppo 			status = ENOMSG;
   1713   1991      heppo 	}
   1714   1991      heppo 
   1715   1991      heppo 	return (status);
   1716   1991      heppo }
   1717   1991      heppo 
   1718   2032    lm66018 /*
   1719   2032    lm66018  * Function:
   1720   2793    lm66018  *	vdc_attr_negotiation()
   1721   2793    lm66018  *
   1722   2793    lm66018  * Description:
   1723   2793    lm66018  *
   1724   2793    lm66018  * Arguments:
   1725   2793    lm66018  *	vdc	- soft state pointer for this instance of the device driver.
   1726   2793    lm66018  *
   1727   2793    lm66018  * Return Code:
   1728   2793    lm66018  *	0	- Success
   1729   2793    lm66018  */
   1730   2793    lm66018 static int
   1731   2793    lm66018 vdc_attr_negotiation(vdc_t *vdcp)
   1732   2793    lm66018 {
   1733   2793    lm66018 	int status;
   1734   2793    lm66018 	vio_msg_t vio_msg;
   1735   2793    lm66018 
   1736   2793    lm66018 	if (status = vdc_init_attr_negotiation(vdcp))
   1737   2793    lm66018 		return (status);
   1738   2793    lm66018 
   1739   2793    lm66018 	/* release lock and wait for response */
   1740   2793    lm66018 	mutex_exit(&vdcp->lock);
   1741   2793    lm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
   1742   2793    lm66018 	mutex_enter(&vdcp->lock);
   1743   2793    lm66018 	if (status) {
   1744   2793    lm66018 		DMSG(vdcp, 0,
   1745   2793    lm66018 		    "[%d] Failed waiting for Attr negotiation response, rv(%d)",
   1746   2793    lm66018 		    vdcp->instance, status);
   1747   2793    lm66018 		return (status);
   1748   2793    lm66018 	}
   1749   2793    lm66018 
   1750   2793    lm66018 	/* check type and sub_type ... */
   1751   2793    lm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
   1752   2793    lm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
   1753   2793    lm66018 		DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n",
   1754   4696   achartre 		    vdcp->instance);
   1755   2793    lm66018 		return (EPROTO);
   1756   2793    lm66018 	}
   1757   2793    lm66018 
   1758   2793    lm66018 	return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg));
   1759   2793    lm66018 }
   1760   2793    lm66018 
   1761   2793    lm66018 
   1762   2793    lm66018 /*
   1763   2793    lm66018  * Function:
   1764   2032    lm66018  *	vdc_init_dring_negotiate()
   1765   2032    lm66018  *
   1766   2032    lm66018  * Description:
   1767   2032    lm66018  *
   1768   2032    lm66018  * Arguments:
   1769   2032    lm66018  *	vdc	- soft state pointer for this instance of the device driver.
   1770   2032    lm66018  *
   1771   2032    lm66018  * Return Code:
   1772   2032    lm66018  *	0	- Success
   1773   2032    lm66018  */
   1774   1991      heppo static int
   1775   1991      heppo vdc_init_dring_negotiate(vdc_t *vdc)
   1776   1991      heppo {
   1777   1991      heppo 	vio_dring_reg_msg_t	pkt;
   1778   1991      heppo 	size_t			msglen = sizeof (pkt);
   1779   1991      heppo 	int			status = -1;
   1780   2793    lm66018 	int			retry;
   1781   2793    lm66018 	int			nretries = 10;
   1782   2793    lm66018 
   1783   2793    lm66018 	ASSERT(vdc != NULL);
   1784   2793    lm66018 	ASSERT(mutex_owned(&vdc->lock));
   1785   2793    lm66018 
   1786   2793    lm66018 	for (retry = 0; retry < nretries; retry++) {
   1787   2793    lm66018 		status = vdc_init_descriptor_ring(vdc);
   1788   2793    lm66018 		if (status != EAGAIN)
   1789   2793    lm66018 			break;
   1790   2793    lm66018 		drv_usecwait(vdc_min_timeout_ldc);
   1791   2793    lm66018 	}
   1792   2793    lm66018 
   1793   2793    lm66018 	if (status != 0) {
   1794   2793    lm66018 		DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n",
   1795   4696   achartre 		    vdc->instance, status);
   1796   2793    lm66018 		return (status);
   1797   2793    lm66018 	}
   1798   2793    lm66018 
   1799   2793    lm66018 	DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n",
   1800   4696   achartre 	    vdc->instance, status);
   1801   1991      heppo 
   1802   1991      heppo 	/* fill in tag */
   1803   1991      heppo 	pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
   1804   1991      heppo 	pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
   1805   1991      heppo 	pkt.tag.vio_subtype_env = VIO_DRING_REG;
   1806   1991      heppo 	pkt.tag.vio_sid = vdc->session_id;
   1807   1991      heppo 	/* fill in payload */
   1808   1991      heppo 	pkt.dring_ident = 0;
   1809   2410    lm66018 	pkt.num_descriptors = vdc->dring_len;
   1810   2410    lm66018 	pkt.descriptor_size = vdc->dring_entry_size;
   1811   1991      heppo 	pkt.options = (VIO_TX_DRING | VIO_RX_DRING);
   1812   1991      heppo 	pkt.ncookies = vdc->dring_cookie_count;
   1813   1991      heppo 	pkt.cookie[0] = vdc->dring_cookie[0];	/* for now just one cookie */
   1814   1991      heppo 
   1815   2032    lm66018 	status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
   1816   1991      heppo 	if (status != 0) {
   1817   2793    lm66018 		DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)",
   1818   4696   achartre 		    vdc->instance, status);
   1819   2793    lm66018 	}
   1820   2793    lm66018 
   1821   2793    lm66018 	return (status);
   1822   2793    lm66018 }
   1823   2793    lm66018 
   1824   2793    lm66018 
   1825   2793    lm66018 /*
   1826   2793    lm66018  * Function:
   1827   2793    lm66018  *	vdc_dring_negotiation()
   1828   2793    lm66018  *
   1829   2793    lm66018  * Description:
   1830   2793    lm66018  *
   1831   2793    lm66018  * Arguments:
   1832   2793    lm66018  *	vdc	- soft state pointer for this instance of the device driver.
   1833   2793    lm66018  *
   1834   2793    lm66018  * Return Code:
   1835   2793    lm66018  *	0	- Success
   1836   2793    lm66018  */
   1837   2793    lm66018 static int
   1838   2793    lm66018 vdc_dring_negotiation(vdc_t *vdcp)
   1839   2793    lm66018 {
   1840   2793    lm66018 	int status;
   1841   2793    lm66018 	vio_msg_t vio_msg;
   1842   2793    lm66018 
   1843   2793    lm66018 	if (status = vdc_init_dring_negotiate(vdcp))
   1844   2793    lm66018 		return (status);
   1845   2793    lm66018 
   1846   2793    lm66018 	/* release lock and wait for response */
   1847   2793    lm66018 	mutex_exit(&vdcp->lock);
   1848   2793    lm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
   1849   2793    lm66018 	mutex_enter(&vdcp->lock);
   1850   2793    lm66018 	if (status) {
   1851   2793    lm66018 		DMSG(vdcp, 0,
   1852   2793    lm66018 		    "[%d] Failed waiting for Dring negotiation response,"
   1853   2793    lm66018 		    " rv(%d)", vdcp->instance, status);
   1854   2793    lm66018 		return (status);
   1855   2793    lm66018 	}
   1856   2793    lm66018 
   1857   2793    lm66018 	/* check type and sub_type ... */
   1858   2793    lm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
   1859   2793    lm66018 	    vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
   1860   2793    lm66018 		DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n",
   1861   4696   achartre 		    vdcp->instance);
   1862   2793    lm66018 		return (EPROTO);
   1863   2793    lm66018 	}
   1864   2793    lm66018 
   1865   2793    lm66018 	return (vdc_handle_dring_reg_msg(vdcp,
   1866   4696   achartre 	    (vio_dring_reg_msg_t *)&vio_msg));
   1867   2793    lm66018 }
   1868   2793    lm66018 
   1869   2793    lm66018 
   1870   2793    lm66018 /*
   1871   2793    lm66018  * Function:
   1872   2793    lm66018  *	vdc_send_rdx()
   1873   2793    lm66018  *
   1874   2793    lm66018  * Description:
   1875   2793    lm66018  *
   1876   2793    lm66018  * Arguments:
   1877   2793    lm66018  *	vdc	- soft state pointer for this instance of the device driver.
   1878   2793    lm66018  *
   1879   2793    lm66018  * Return Code:
   1880   2793    lm66018  *	0	- Success
   1881   2793    lm66018  */
   1882   2793    lm66018 static int
   1883   2793    lm66018 vdc_send_rdx(vdc_t *vdcp)
   1884   2793    lm66018 {
   1885   2793    lm66018 	vio_msg_t	msg;
   1886   2793    lm66018 	size_t		msglen = sizeof (vio_msg_t);
   1887   2793    lm66018 	int		status;
   1888   2793    lm66018 
   1889   2793    lm66018 	/*
   1890   2793    lm66018 	 * Send an RDX message to vds to indicate we are ready
   1891   2793    lm66018 	 * to send data
   1892   2793    lm66018 	 */
   1893   2793    lm66018 	msg.tag.vio_msgtype = VIO_TYPE_CTRL;
   1894   2793    lm66018 	msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
   1895   2793    lm66018 	msg.tag.vio_subtype_env = VIO_RDX;
   1896   2793    lm66018 	msg.tag.vio_sid = vdcp->session_id;
   1897   2793    lm66018 	status = vdc_send(vdcp, (caddr_t)&msg, &msglen);
   1898   2793    lm66018 	if (status != 0) {
   1899   2793    lm66018 		DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)",
   1900   2793    lm66018 		    vdcp->instance, status);
   1901   2793    lm66018 	}
   1902   2793    lm66018 
   1903   2793    lm66018 	return (status);
   1904   2793    lm66018 }
   1905   2793    lm66018 
   1906   2793    lm66018 /*
   1907   2793    lm66018  * Function:
   1908   2793    lm66018  *	vdc_handle_rdx()
   1909   2793    lm66018  *
   1910   2793    lm66018  * Description:
   1911   2793    lm66018  *
   1912   2793    lm66018  * Arguments:
   1913   2793    lm66018  *	vdc	- soft state pointer for this instance of the device driver.
   1914   2793    lm66018  *	msgp	- received msg
   1915   2793    lm66018  *
   1916   2793    lm66018  * Return Code:
   1917   2793    lm66018  *	0	- Success
   1918   2793    lm66018  */
   1919   2793    lm66018 static int
   1920   2793    lm66018 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp)
   1921   2793    lm66018 {
   1922   2793    lm66018 	_NOTE(ARGUNUSED(vdcp))
   1923   2793    lm66018 	_NOTE(ARGUNUSED(msgp))
   1924   2793    lm66018 
   1925   2793    lm66018 	ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL);
   1926   2793    lm66018 	ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK);
   1927   2793    lm66018 	ASSERT(msgp->tag.vio_subtype_env == VIO_RDX);
   1928   2793    lm66018 
   1929   2793    lm66018 	DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance);
   1930   2793    lm66018 
   1931   2793    lm66018 	return (0);
   1932   2793    lm66018 }
   1933   2793    lm66018 
   1934   2793    lm66018 /*
   1935   2793    lm66018  * Function:
   1936   2793    lm66018  *	vdc_rdx_exchange()
   1937   2793    lm66018  *
   1938   2793    lm66018  * Description:
   1939   2793    lm66018  *
   1940   2793    lm66018  * Arguments:
   1941   2793    lm66018  *	vdc	- soft state pointer for this instance of the device driver.
   1942   2793    lm66018  *
   1943   2793    lm66018  * Return Code:
   1944   2793    lm66018  *	0	- Success
   1945   2793    lm66018  */
   1946   2793    lm66018 static int
   1947   2793    lm66018 vdc_rdx_exchange(vdc_t *vdcp)
   1948   2793    lm66018 {
   1949   2793    lm66018 	int status;
   1950   2793    lm66018 	vio_msg_t vio_msg;
   1951   2793    lm66018 
   1952   2793    lm66018 	if (status = vdc_send_rdx(vdcp))
   1953   2793    lm66018 		return (status);
   1954   2793    lm66018 
   1955   2793    lm66018 	/* release lock and wait for response */
   1956   2793    lm66018 	mutex_exit(&vdcp->lock);
   1957   2793    lm66018 	status = vdc_wait_for_response(vdcp, &vio_msg);
   1958   2793    lm66018 	mutex_enter(&vdcp->lock);
   1959   2793    lm66018 	if (status) {
   1960   4696   achartre 		DMSG(vdcp, 0, "[%d] Failed waiting for RDX response, rv(%d)",
   1961   4696   achartre 		    vdcp->instance, status);
   1962   2793    lm66018 		return (status);
   1963   2793    lm66018 	}
   1964   2793    lm66018 
   1965   2793    lm66018 	/* check type and sub_type ... */
   1966   2793    lm66018 	if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
   1967   2793    lm66018 	    vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) {
   1968   4696   achartre 		DMSG(vdcp, 0, "[%d] Invalid RDX response\n", vdcp->instance);
   1969   2793    lm66018 		return (EPROTO);
   1970   2793    lm66018 	}
   1971   2793    lm66018 
   1972   2793    lm66018 	return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg));
   1973   1991      heppo }
   1974   1991      heppo 
   1975   1991      heppo 
   1976   1991      heppo /* -------------------------------------------------------------------------- */
   1977   1991      heppo 
   1978   1991      heppo /*
   1979   1991      heppo  * LDC helper routines
   1980   1991      heppo  */
   1981   2793    lm66018 
   1982   2793    lm66018 static int
   1983   2793    lm66018 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp)
   1984   2793    lm66018 {
   1985   2793    lm66018 	int		status;
   1986   5365    lm66018 	uint64_t	delay_time;
   1987   2793    lm66018 	size_t		len;
   1988   2793    lm66018 
   1989   8406  Alexandre 	/*
   1990   8406  Alexandre 	 * Until we get a blocking ldc read we have to retry until the entire
   1991   8406  Alexandre 	 * LDC message has arrived before ldc_read() will return that message.
   1992   8406  Alexandre 	 * If ldc_read() succeed but returns a zero length message then that
   1993   8406  Alexandre 	 * means that the LDC queue is empty and we have to wait for a
   1994   8406  Alexandre 	 * notification from the LDC callback which will set the read_state to
   1995   8406  Alexandre 	 * VDC_READ_PENDING. Note we also bail out if the channel is reset or
   1996   8406  Alexandre 	 * goes away.
   1997   2793    lm66018 	 */
   1998   2793    lm66018 	delay_time = vdc_ldc_read_init_delay;
   1999   8406  Alexandre 
   2000   8406  Alexandre 	for (;;) {
   2001   8406  Alexandre 
   2002   8406  Alexandre 		len = *nbytesp;
   2003   8406  Alexandre 		/*
   2004   8406  Alexandre 		 * vdc->curr_server is protected by vdc->lock but to avoid
   2005   8406  Alexandre 		 * contentions we don't take the lock here. We can do this
   2006   8406  Alexandre 		 * safely because vdc_recv() is only called from thread
   2007   8406  Alexandre 		 * process_msg_thread() which is also the only thread that
   2008   8406  Alexandre 		 * can change vdc->curr_server.
   2009   8406  Alexandre 		 */
   2010   8406  Alexandre 		status = ldc_read(vdc->curr_server->ldc_handle,
   2011   8406  Alexandre 		    (caddr_t)msgp, &len);
   2012   8406  Alexandre 
   2013   8406  Alexandre 		if (status == EAGAIN) {
   2014   8406  Alexandre 			delay_time *= 2;
   2015   8406  Alexandre 			if (delay_time >= vdc_ldc_read_max_delay)
   2016   8406  Alexandre 				delay_time = vdc_ldc_read_max_delay;
   2017   8406  Alexandre 			delay(delay_time);
   2018   8406  Alexandre 			continue;
   2019   8406  Alexandre 		}
   2020   8406  Alexandre 
   2021   8406  Alexandre 		if (status != 0) {
   2022   8406  Alexandre 			DMSG(vdc, 0, "ldc_read returned %d\n", status);
   2023   8406  Alexandre 			break;
   2024   8406  Alexandre 		}
   2025   8406  Alexandre 
   2026   8406  Alexandre 		if (len != 0) {
   2027   8406  Alexandre 			*nbytesp = len;
   2028   8406  Alexandre 			break;
   2029   8406  Alexandre 		}
   2030   8406  Alexandre 
   2031   8406  Alexandre 		mutex_enter(&vdc->read_lock);
   2032   8406  Alexandre 
   2033   8406  Alexandre 		while (vdc->read_state != VDC_READ_PENDING) {
   2034   8406  Alexandre 
   2035   8406  Alexandre 			/* detect if the connection has been reset */
   2036   8406  Alexandre 			if (vdc->read_state == VDC_READ_RESET) {
   2037   8406  Alexandre 				mutex_exit(&vdc->read_lock);
   2038   8406  Alexandre 				return (ECONNRESET);
   2039   8406  Alexandre 			}
   2040   8406  Alexandre 
   2041   8406  Alexandre 			vdc->read_state = VDC_READ_WAITING;
   2042   8406  Alexandre 			cv_wait(&vdc->read_cv, &vdc->read_lock);
   2043   8406  Alexandre 		}
   2044   8406  Alexandre 
   2045   8406  Alexandre 		vdc->read_state = VDC_READ_IDLE;
   2046   8406  Alexandre 		mutex_exit(&vdc->read_lock);
   2047   8406  Alexandre 
   2048   8406  Alexandre 		delay_time = vdc_ldc_read_init_delay;
   2049   8406  Alexandre 	}
   2050   2793    lm66018 
   2051   2793    lm66018 	return (status);
   2052   2793    lm66018 }
   2053   2793    lm66018 
   2054   2793    lm66018 
   2055   2793    lm66018 
   2056   2793    lm66018 #ifdef DEBUG
   2057   2793    lm66018 void
   2058   2793    lm66018 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg)
   2059   2793    lm66018 {
   2060   2793    lm66018 	char *ms, *ss, *ses;
   2061   2793    lm66018 	switch (msg->tag.vio_msgtype) {
   2062   2793    lm66018 #define	Q(_s)	case _s : ms = #_s; break;
   2063   2793    lm66018 	Q(VIO_TYPE_CTRL)
   2064   2793    lm66018 	Q(VIO_TYPE_DATA)
   2065   2793    lm66018 	Q(VIO_TYPE_ERR)
   2066   2793    lm66018 #undef Q
   2067   2793    lm66018 	default: ms = "unknown"; break;
   2068   2793    lm66018 	}
   2069   2793    lm66018 
   2070   2793    lm66018 	switch (msg->tag.vio_subtype) {
   2071   2793    lm66018 #define	Q(_s)	case _s : ss = #_s; break;
   2072   2793    lm66018 	Q(VIO_SUBTYPE_INFO)
   2073   2793    lm66018 	Q(VIO_SUBTYPE_ACK)
   2074   2793    lm66018 	Q(VIO_SUBTYPE_NACK)
   2075   2793    lm66018 #undef Q
   2076   2793    lm66018 	default: ss = "unknown"; break;
   2077   2793    lm66018 	}
   2078   2793    lm66018 
   2079   2793    lm66018 	switch (msg->tag.vio_subtype_env) {
   2080   2793    lm66018 #define	Q(_s)	case _s : ses = #_s; break;
   2081   2793    lm66018 	Q(VIO_VER_INFO)
   2082   2793    lm66018 	Q(VIO_ATTR_INFO)
   2083   2793    lm66018 	Q(VIO_DRING_REG)
   2084   2793    lm66018 	Q(VIO_DRING_UNREG)
   2085   2793    lm66018 	Q(VIO_RDX)
   2086   2793    lm66018 	Q(VIO_PKT_DATA)
   2087   2793    lm66018 	Q(VIO_DESC_DATA)
   2088   2793    lm66018 	Q(VIO_DRING_DATA)
   2089   2793    lm66018 #undef Q
   2090   2793    lm66018 	default: ses = "unknown"; break;
   2091   2793    lm66018 	}
   2092   2793    lm66018 
   2093   2793    lm66018 	DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n",
   2094   2793    lm66018 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
   2095   2793    lm66018 	    msg->tag.vio_subtype_env, ms, ss, ses);
   2096   2793    lm66018 }
   2097   2793    lm66018 #endif
   2098   1991      heppo 
   2099   1991      heppo /*
   2100   1991      heppo  * Function:
   2101   1991      heppo  *	vdc_send()
   2102   1991      heppo  *
   2103   1991      heppo  * Description:
   2104   1991      heppo  *	The function encapsulates the call to write a message using LDC.
   2105   1991      heppo  *	If LDC indicates that the call failed due to the queue being full,
   2106   5365    lm66018  *	we retry the ldc_write(), otherwise we return the error returned by LDC.
   2107   1991      heppo  *
   2108   1991      heppo  * Arguments:
   2109   1991      heppo  *	ldc_handle	- LDC handle for the channel this instance of vdc uses
   2110   1991      heppo  *	pkt		- address of LDC message to be sent
   2111   1991      heppo  *	msglen		- the size of the message being sent. When the function
   2112   1991      heppo  *			  returns, this contains the number of bytes written.
   2113   1991      heppo  *
   2114   1991      heppo  * Return Code:
   2115   1991      heppo  *	0		- Success.
   2116   1991      heppo  *	EINVAL		- pkt or msglen were NULL
   2117   1991      heppo  *	ECONNRESET	- The connection was not up.
   2118   1991      heppo  *	EWOULDBLOCK	- LDC queue is full
   2119   1991      heppo  *	xxx		- other error codes returned by ldc_write
   2120   1991      heppo  */
   2121   1991      heppo static int
   2122   2032    lm66018 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen)
   2123   1991      heppo {
   2124   1991      heppo 	size_t	size = 0;
   2125   1991      heppo 	int	status = 0;
   2126   2793    lm66018 	clock_t delay_ticks;
   2127   1991      heppo 
   2128   2032    lm66018 	ASSERT(vdc != NULL);
   2129   2032    lm66018 	ASSERT(mutex_owned(&vdc->lock));
   2130   1991      heppo 	ASSERT(msglen != NULL);
   2131   1991      heppo 	ASSERT(*msglen != 0);
   2132   1991      heppo 
   2133   2793    lm66018 #ifdef DEBUG
   2134   5365    lm66018 	vdc_decode_tag(vdc, (vio_msg_t *)(uintptr_t)pkt);
   2135   2793    lm66018 #endif
   2136   2793    lm66018 	/*
   2137   2793    lm66018 	 * Wait indefinitely to send if channel
   2138   2793    lm66018 	 * is busy, but bail out if we succeed or
   2139   2793    lm66018 	 * if the channel closes or is reset.
   2140   2793    lm66018 	 */
   2141   2793    lm66018 	delay_ticks = vdc_hz_min_ldc_delay;
   2142   1991      heppo 	do {
   2143   1991      heppo 		size = *msglen;
   2144   6480    narayan 		status = ldc_write(vdc->curr_server->ldc_handle, pkt, &size);
   2145   2793    lm66018 		if (status == EWOULDBLOCK) {
   2146   2793    lm66018 			delay(delay_ticks);
   2147   2793    lm66018 			/* geometric backoff */
   2148   2793    lm66018 			delay_ticks *= 2;
   2149   2793    lm66018 			if (delay_ticks > vdc_hz_max_ldc_delay)
   2150   2793    lm66018 				delay_ticks = vdc_hz_max_ldc_delay;
   2151   2793    lm66018 		}
   2152   2793    lm66018 	} while (status == EWOULDBLOCK);
   2153   2032    lm66018 
   2154   2032    lm66018 	/* if LDC had serious issues --- reset vdc state */
   2155   2032    lm66018 	if (status == EIO || status == ECONNRESET) {
   2156   2793    lm66018 		/* LDC had serious issues --- reset vdc state */
   2157   2793    lm66018 		mutex_enter(&vdc->read_lock);
   2158   2793    lm66018 		if ((vdc->read_state == VDC_READ_WAITING) ||
   2159   2793    lm66018 		    (vdc->read_state == VDC_READ_RESET))
   2160   2793    lm66018 			cv_signal(&vdc->read_cv);
   2161   2793    lm66018 		vdc->read_state = VDC_READ_RESET;
   2162   2793    lm66018 		mutex_exit(&vdc->read_lock);
   2163   2793    lm66018 
   2164   2793    lm66018 		/* wake up any waiters in the reset thread */
   2165   2793    lm66018 		if (vdc->state == VDC_STATE_INIT_WAITING) {
   2166   2793    lm66018 			DMSG(vdc, 0, "[%d] write reset - "
   2167   2793    lm66018 			    "vdc is resetting ..\n", vdc->instance);
   2168   2793    lm66018 			vdc->state = VDC_STATE_RESETTING;
   2169   2793    lm66018 			cv_signal(&vdc->initwait_cv);
   2170   2793    lm66018 		}
   2171   2793    lm66018 
   2172   2793    lm66018 		return (ECONNRESET);
   2173   2032    lm66018 	}
   2174   1991      heppo 
   2175   1991      heppo 	/* return the last size written */
   2176   1991      heppo 	*msglen = size;
   2177   1991      heppo 
   2178   1991      heppo 	return (status);
   2179   1991      heppo }
   2180   1991      heppo 
   2181   1991      heppo /*
   2182   1991      heppo  * Function:
   2183   4848   achartre  *	vdc_get_md_node
   2184   4848   achartre  *
   2185   4848   achartre  * Description:
   2186   6480    narayan  *	Get the MD, the device node for the given disk instance. The
   2187   6480    narayan  *	caller is responsible for cleaning up the reference to the
   2188   6480    narayan  *	returned MD (mdpp) by calling md_fini_handle().
   2189   1991      heppo  *
   2190   1991      heppo  * Arguments:
   2191   1991      heppo  *	dip	- dev info pointer for this instance of the device driver.
   2192   4848   achartre  *	mdpp	- the returned MD.
   2193   4848   achartre  *	vd_nodep - the returned device node.
   2194   1991      heppo  *
   2195   1991      heppo  * Return Code:
   2196   1991      heppo  *	0	- Success.
   2197   1991      heppo  *	ENOENT	- Expected node or property did not exist.
   2198   1991      heppo  *	ENXIO	- Unexpected error communicating with MD framework
   2199   1991      heppo  */
   2200   1991      heppo static int
   2201   6480    narayan vdc_get_md_node(dev_info_t *dip, md_t **mdpp, mde_cookie_t *vd_nodep)
   2202   1991      heppo {
   2203   1991      heppo 	int		status = ENOENT;
   2204   1991      heppo 	char		*node_name = NULL;
   2205   1991      heppo 	md_t		*mdp = NULL;
   2206   1991      heppo 	int		num_nodes;
   2207   1991      heppo 	int		num_vdevs;
   2208   1991      heppo 	mde_cookie_t	rootnode;
   2209   1991      heppo 	mde_cookie_t	*listp = NULL;
   2210   1991      heppo 	boolean_t	found_inst = B_FALSE;
   2211   1991      heppo 	int		listsz;
   2212   1991      heppo 	int		idx;
   2213   1991      heppo 	uint64_t	md_inst;
   2214   1991      heppo 	int		obp_inst;
   2215   1991      heppo 	int		instance = ddi_get_instance(dip);
   2216   1991      heppo 
   2217   1991      heppo 	/*
   2218   1991      heppo 	 * Get the OBP instance number for comparison with the MD instance
   2219   1991      heppo 	 *
   2220   1991      heppo 	 * The "cfg-handle" property of a vdc node in an MD contains the MD's
   2221   1991      heppo 	 * notion of "instance", or unique identifier, for that node; OBP
   2222   1991      heppo 	 * stores the value of the "cfg-handle" MD property as the value of
   2223   1991      heppo 	 * the "reg" property on the node in the device tree it builds from
   2224   1991      heppo 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
   2225   1991      heppo 	 * "reg" property value to uniquely identify this device instance.
   2226   1991      heppo 	 * If the "reg" property cannot be found, the device tree state is
   2227   1991      heppo 	 * presumably so broken that there is no point in continuing.
   2228   1991      heppo 	 */
   2229   1991      heppo 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) {
   2230   1991      heppo 		cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG);
   2231   1991      heppo 		return (ENOENT);
   2232   1991      heppo 	}
   2233   1991      heppo 	obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
   2234   4696   achartre 	    OBP_REG, -1);
   2235   2793    lm66018 	DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst);
   2236   1991      heppo 
   2237   1991      heppo 	/*
   2238   4848   achartre 	 * We now walk the MD nodes to find the node for this vdisk.
   2239   1991      heppo 	 */
   2240   1991      heppo 	if ((mdp = md_get_handle()) == NULL) {
   2241   1991      heppo 		cmn_err(CE_WARN, "unable to init machine description");
   2242   1991      heppo 		return (ENXIO);
   2243   1991      heppo 	}
   2244   1991      heppo 
   2245   1991      heppo 	num_nodes = md_node_count(mdp);
   2246   1991      heppo 	ASSERT(num_nodes > 0);
   2247   1991      heppo 
   2248   1991      heppo 	listsz = num_nodes * sizeof (mde_cookie_t);
   2249   1991      heppo 
   2250   1991      heppo 	/* allocate memory for nodes */
   2251   1991      heppo 	listp = kmem_zalloc(listsz, KM_SLEEP);
   2252   1991      heppo 
   2253   1991      heppo 	rootnode = md_root_node(mdp);
   2254   1991      heppo 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
   2255   1991      heppo 
   2256   1991      heppo 	/*
   2257   1991      heppo 	 * Search for all the virtual devices, we will then check to see which
   2258   1991      heppo 	 * ones are disk nodes.
   2259   1991      heppo 	 */
   2260   1991      heppo 	num_vdevs = md_scan_dag(mdp, rootnode,
   2261   4696   achartre 	    md_find_name(mdp, VDC_MD_VDEV_NAME),
   2262   4696   achartre 	    md_find_name(mdp, "fwd"), listp);
   2263   1991      heppo 
   2264   1991      heppo 	if (num_vdevs <= 0) {
   2265   1991      heppo 		cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME);
   2266   1991      heppo 		status = ENOENT;
   2267   1991      heppo 		goto done;
   2268   1991      heppo 	}
   2269   1991      heppo 
   2270   2793    lm66018 	DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs);
   2271   1991      heppo 	for (idx = 0; idx < num_vdevs; idx++) {
   2272   1991      heppo 		status = md_get_prop_str(mdp, listp[idx], "name", &node_name);
   2273   1991      heppo 		if ((status != 0) || (node_name == NULL)) {
   2274   1991      heppo 			cmn_err(CE_NOTE, "Unable to get name of node type '%s'"
   2275   4696   achartre 			    ": err %d", VDC_MD_VDEV_NAME, status);
   2276   1991      heppo 			continue;
   2277   1991      heppo 		}
   2278   1991      heppo 
   2279   2793    lm66018 		DMSGX(1, "[%d] Found node '%s'\n", instance, node_name);
   2280   1991      heppo 		if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) {
   2281   1991      heppo 			status = md_get_prop_val(mdp, listp[idx],
   2282   4696   achartre 			    VDC_MD_CFG_HDL, &md_inst);
   2283   2793    lm66018 			DMSGX(1, "[%d] vdc inst in MD=%lx\n",
   2284   2793    lm66018 			    instance, md_inst);
   2285   1991      heppo 			if ((status == 0) && (md_inst == obp_inst)) {
   2286   1991      heppo 				found_inst = B_TRUE;
   2287   1991      heppo 				break;
   2288   1991      heppo 			}
   2289   1991      heppo 		}
   2290   1991      heppo 	}
   2291   1991      heppo 
   2292   2032    lm66018 	if (!found_inst) {
   2293   2793    lm66018 		DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME);
   2294   1991      heppo 		status = ENOENT;
   2295   1991      heppo 		goto done;
   2296   1991      heppo 	}
   2297   2793    lm66018 	DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst);
   2298   1991      heppo 
   2299   4848   achartre 	*vd_nodep = listp[idx];
   2300   4848   achartre 	*mdpp = mdp;
   2301   4848   achartre done:
   2302   4848   achartre 	kmem_free(listp, listsz);
   2303   4848   achartre 	return (status);
   2304   4848   achartre }
   2305   4848   achartre 
   2306   4848   achartre /*
   2307   4848   achartre  * Function:
   2308   6480    narayan  *	vdc_init_ports
   2309   6480    narayan  *
   2310   6480    narayan  * Description:
   2311   6480    narayan  *	Initialize all the ports for this vdisk instance.
   2312   6480    narayan  *
   2313   6480    narayan  * Arguments:
   2314   6480    narayan  *	vdc	- soft state pointer for this instance of the device driver.
   2315   6480    narayan  *	mdp	- md pointer
   2316   6480    narayan  *	vd_nodep - device md node.
   2317   4848   achartre  *
   2318   4848   achartre  * Return Code:
   2319   4848   achartre  *	0	- Success.
   2320   4848   achartre  *	ENOENT	- Expected node or property did not exist.
   2321   4848   achartre  */
   2322   4848   achartre static int
   2323   6480    narayan vdc_init_ports(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_nodep)
   2324   6480    narayan {
   2325   6480    narayan 	int		status = 0;
   2326   6480    narayan 	int		idx;
   2327   6480    narayan 	int		num_nodes;
   2328   6480    narayan 	int		num_vports;
   2329   6480    narayan 	int		num_chans;
   2330   6480    narayan 	int		listsz;
   2331   6480    narayan 	mde_cookie_t	vd_port;
   2332   4848   achartre 	mde_cookie_t	*chanp = NULL;
   2333   6480    narayan 	mde_cookie_t	*portp = NULL;
   2334   6480    narayan 	vdc_server_t	*srvr;
   2335   6480    narayan 	vdc_server_t	*prev_srvr = NULL;
   2336   6480    narayan 
   2337   6480    narayan 	/*
   2338   6480    narayan 	 * We now walk the MD nodes to find the port nodes for this vdisk.
   2339   6480    narayan 	 */
   2340   4848   achartre 	num_nodes = md_node_count(mdp);
   2341   4848   achartre 	ASSERT(num_nodes > 0);
   2342   4848   achartre 
   2343   4848   achartre 	listsz = num_nodes * sizeof (mde_cookie_t);
   2344   4848   achartre 
   2345   4848   achartre 	/* allocate memory for nodes */
   2346   6480    narayan 	portp = kmem_zalloc(listsz, KM_SLEEP);
   2347   4848   achartre 	chanp = kmem_zalloc(listsz, KM_SLEEP);
   2348   4848   achartre 
   2349   6480    narayan 	num_vports = md_scan_dag(mdp, vd_nodep,
   2350   6480    narayan 	    md_find_name(mdp, VDC_MD_PORT_NAME),
   2351   6480    narayan 	    md_find_name(mdp, "fwd"), portp);
   2352   6480    narayan 	if (num_vports == 0) {
   2353   6480    narayan 		DMSGX(0, "Found no '%s' node for '%s' port\n",
   2354   6480    narayan 		    VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME);
   2355   1991      heppo 		status = ENOENT;
   2356   1991      heppo 		goto done;
   2357   6480    narayan 	}
   2358   6480    narayan 
   2359   6480    narayan 	DMSGX(1, "Found %d '%s' node(s) for '%s' port\n",
   2360   6480    narayan 	    num_vports, VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME);
   2361   6480    narayan 
   2362   6480    narayan 	vdc->num_servers = 0;
   2363   6480    narayan 	for (idx = 0; idx < num_vports; idx++) {
   2364   6480    narayan 
   2365   6480    narayan 		/* initialize this port */
   2366   6480    narayan 		vd_port = portp[idx];
   2367   6480    narayan 		srvr = kmem_zalloc(sizeof (vdc_server_t), KM_SLEEP);
   2368   6480    narayan 		srvr->vdcp = vdc;
   2369  11004  Alexandre 		srvr->svc_state = VDC_SERVICE_OFFLINE;
   2370  11004  Alexandre 		srvr->log_state = VDC_SERVICE_NONE;
   2371   6480    narayan 
   2372   6480    narayan 		/* get port id */
   2373   6480    narayan 		if (md_get_prop_val(mdp, vd_port, VDC_MD_ID, &srvr->id) != 0) {
   2374   6480    narayan 			cmn_err(CE_NOTE, "vDisk port '%s' property not found",
   2375   6480    narayan 			    VDC_MD_ID);
   2376   6480    narayan 			kmem_free(srvr, sizeof (vdc_server_t));
   2377   6480    narayan 			continue;
   2378   6480    narayan 		}
   2379   6480    narayan 
   2380   6480    narayan 		/* set the connection timeout */
   2381   6480    narayan 		if (md_get_prop_val(mdp, vd_port, VDC_MD_TIMEOUT,
   2382   6480    narayan 		    &srvr->ctimeout) != 0) {
   2383   6480    narayan 			srvr->ctimeout = 0;
   2384   6480    narayan 		}
   2385   6480    narayan 
   2386   6480    narayan 		/* get the ldc id */
   2387   6480    narayan 		num_chans = md_scan_dag(mdp, vd_port,
   2388   6480    narayan 		    md_find_name(mdp, VDC_MD_CHAN_NAME),
   2389   6480    narayan 		    md_find_name(mdp, "fwd"), chanp);
   2390   6480    narayan 
   2391   6480    narayan 		/* expecting at least one channel */
   2392   6480    narayan 		if (num_chans <= 0) {
   2393   6480    narayan 			cmn_err(CE_NOTE, "No '%s' node for '%s' port",
   2394   6480    narayan 			    VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME);
   2395   6480    narayan 			kmem_free(srvr, sizeof (vdc_server_t));
   2396   6480    narayan 			continue;
   2397   6480    narayan 		} else if (num_chans != 1) {
   2398   6480    narayan 			DMSGX(0, "Expected 1 '%s' node for '%s' port, "
   2399   6480    narayan 			    "found %d\n", VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME,
   2400   6480    narayan 			    num_chans);
   2401   6480    narayan 		}
   2402   6480    narayan 
   2403   6480    narayan 		/*
   2404   6480    narayan 		 * We use the first channel found (index 0), irrespective of how
   2405   6480    narayan 		 * many are there in total.
   2406   6480    narayan 		 */
   2407   6480    narayan 		if (md_get_prop_val(mdp, chanp[0], VDC_MD_ID,
   2408   6480    narayan 		    &srvr->ldc_id) != 0) {
   2409   6480    narayan 			cmn_err(CE_NOTE, "Channel '%s' property not found",
   2410   6480    narayan 			    VDC_MD_ID);
   2411   6480    narayan 			kmem_free(srvr, sizeof (vdc_server_t));
   2412   6480    narayan 			continue;
   2413   6480    narayan 		}
   2414   6480    narayan 
   2415   6480    narayan 		/*
   2416   6480    narayan 		 * now initialise LDC channel which will be used to
   2417   6480    narayan 		 * communicate with this server
   2418   6480    narayan 		 */
   2419   6480    narayan 		if (vdc_do_ldc_init(vdc, srvr) != 0) {
   2420   6480    narayan 			kmem_free(srvr, sizeof (vdc_server_t));
   2421   6480    narayan 			continue;
   2422   6480    narayan 		}
   2423   6480    narayan 
   2424   6480    narayan 		/* add server to list */
   2425   6850   achartre 		if (prev_srvr)
   2426   6480    narayan 			prev_srvr->next = srvr;
   2427   6850   achartre 		else
   2428   6480    narayan 			vdc->server_list = srvr;
   2429   6850   achartre 
   2430   6850   achartre 		prev_srvr = srvr;
   2431   6480    narayan 
   2432   6480    narayan 		/* inc numbers of servers */
   2433   6480    narayan 		vdc->num_servers++;
   2434   6480    narayan 	}
   2435   6480    narayan 
   2436   6480    narayan 	/*
   2437   6480    narayan 	 * Adjust the max number of handshake retries to match
   2438   6480    narayan 	 * the number of vdisk servers.
   2439   6480    narayan 	 */
   2440   6480    narayan 	if (vdc_hshake_retries < vdc->num_servers)
   2441   6480    narayan 		vdc_hshake_retries = vdc->num_servers;
   2442   6480    narayan 
   2443   6480    narayan 	/* pick first server as current server */
   2444   6480    narayan 	if (vdc->server_list != NULL) {
   2445   6480    narayan 		vdc->curr_server = vdc->server_list;
   2446   6480    narayan 		status = 0;
   2447   6480    narayan 	} else {
   2448   1991      heppo 		status = ENOENT;
   2449   1991      heppo 	}
   2450   1991      heppo 
   2451   1991      heppo done:
   2452   4848   achartre 	kmem_free(chanp, listsz);
   2453   6480    narayan 	kmem_free(portp, listsz);
   2454   6480    narayan 	return (status);
   2455   6480    narayan }
   2456   6480    narayan 
   2457   6480    narayan 
   2458   6480    narayan /*
   2459   6480    narayan  * Function:
   2460   6480    narayan  *	vdc_do_ldc_up
   2461   6480    narayan  *
   2462   6480    narayan  * Description:
   2463   6480    narayan  *	Bring the channel for the current server up.
   2464   6480    narayan  *
   2465   6480    narayan  * Arguments:
   2466   6480    narayan  *	vdc	- soft state pointer for this instance of the device driver.
   2467   6480    narayan  *
   2468   6480    narayan  * Return Code:
   2469   6480    narayan  *	0		- Success.
   2470   6480    narayan  *	EINVAL		- Driver is detaching / LDC error
   2471   6480    narayan  *	ECONNREFUSED	- Other end is not listening
   2472   6480    narayan  */
   2473   2032    lm66018 static int
   2474   2032    lm66018 vdc_do_ldc_up(vdc_t *vdc)
   2475   2032    lm66018 {
   2476   2793    lm66018 	int		status;
   2477   2793    lm66018 	ldc_status_t	ldc_state;
   2478   2793    lm66018 
   2479   6480    narayan 	ASSERT(MUTEX_HELD(&vdc->lock));
   2480   6480    narayan 
   2481   2793    lm66018 	DMSG(vdc, 0, "[%d] Bringing up channel %lx\n",
   2482   6480    narayan 	    vdc->instance, vdc->curr_server->ldc_id);
   2483   2793    lm66018 
   2484   2793    lm66018 	if (vdc->lifecycle == VDC_LC_DETACHING)
   2485   2793    lm66018 		return (EINVAL);
   2486   2032    lm66018 
   2487   6480    narayan 	if ((status = ldc_up(vdc->curr_server->ldc_handle)) != 0) {
   2488   2032    lm66018 		switch (status) {
   2489   2032    lm66018 		case ECONNREFUSED:	/* listener not ready at other end */
   2490   2793    lm66018 			DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n",
   2491   6480    narayan 			    vdc->instance, vdc->curr_server->ldc_id, status);
   2492   2032    lm66018 			status = 0;
   2493   2032    lm66018 			break;
   2494   2032    lm66018 		default:
   2495   2793    lm66018 			DMSG(vdc, 0, "[%d] Failed to bring up LDC: "
   2496   6480    narayan 			    "channel=%ld, err=%d", vdc->instance,
   2497   6480    narayan 			    vdc->curr_server->ldc_id, status);
   2498   6480    narayan 			break;
   2499   6480    narayan 		}
   2500   6480    narayan 	}
   2501   6480    narayan 
   2502   6480    narayan 	if (ldc_status(vdc->curr_server->ldc_handle, &ldc_state) == 0) {
   2503   6480    narayan 		vdc->curr_server->ldc_state = ldc_state;
   2504   2793    lm66018 		if (ldc_state == LDC_UP) {
   2505   2793    lm66018 			DMSG(vdc, 0, "[%d] LDC channel already up\n",
   2506   2793    lm66018 			    vdc->instance);
   2507   2793    lm66018 			vdc->seq_num = 1;
   2508   2793    lm66018 			vdc->seq_num_reply = 0;
   2509   2793    lm66018 		}
   2510   2793    lm66018 	}
   2511   2793    lm66018 
   2512   2793    lm66018 	return (status);
   2513   2793    lm66018 }
   2514   1991      heppo 
   2515   2032    lm66018 /*
   2516   2032    lm66018  * Function:
   2517   2032    lm66018  *	vdc_terminate_ldc()
   2518   2032    lm66018  *
   2519   2032    lm66018  * Description:
   2520   2032    lm66018  *
   2521   2032    lm66018  * Arguments:
   2522   2032    lm66018  *	vdc	- soft state pointer for this instance of the device driver.
   2523   6480    narayan  *	srvr	- vdc per-server info structure
   2524   2032    lm66018  *
   2525   2032    lm66018  * Return Code:
   2526   2032    lm66018  *	None
   2527   2032    lm66018  */
   2528   1991      heppo static void
   2529   6480    narayan vdc_terminate_ldc(vdc_t *vdc, vdc_server_t *srvr)
   2530   1991      heppo {
   2531   1991      heppo 	int	instance = ddi_get_instance(vdc->dip);
   2532   1991      heppo 
   2533   6480    narayan 	if (srvr->state & VDC_LDC_OPEN) {
   2534   6480    narayan 		DMSG(vdc, 0, "[%d] ldc_close()\n", instance);
   2535   6480    narayan 		(void) ldc_close(srvr->ldc_handle);
   2536   6480    narayan 	}
   2537   6480    narayan 	if (srvr->state & VDC_LDC_CB) {
   2538   6480    narayan 		DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance);
   2539   6480    narayan 		(void) ldc_unreg_callback(srvr->ldc_handle);
   2540   6480    narayan 	}
   2541   6480    narayan 	if (srvr->state & VDC_LDC_INIT) {
   2542   6480    narayan 		DMSG(vdc, 0, "[%d] ldc_fini()\n", instance);
   2543   6480    narayan 		(void) ldc_fini(srvr->ldc_handle);
   2544   6480    narayan 		srvr->ldc_handle = NULL;
   2545   6480    narayan 	}
   2546   6480    narayan 
   2547   6480    narayan 	srvr->state &= ~(VDC_LDC_INIT | VDC_LDC_CB | VDC_LDC_OPEN);
   2548   6480    narayan }
   2549   6480    narayan 
   2550   6480    narayan /*
   2551   6480    narayan  * Function:
   2552   6480    narayan  *	vdc_fini_ports()
   2553   6480    narayan  *
   2554   6480    narayan  * Description:
   2555   6480    narayan  *	Finalize all ports by closing the channel associated with each
   2556   6480    narayan  *	port and also freeing the server structure.
   2557   6480    narayan  *
   2558   6480    narayan  * Arguments:
   2559   6480    narayan  *	vdc	- soft state pointer for this instance of the device driver.
   2560   6480    narayan  *
   2561   6480    narayan  * Return Code:
   2562   6480    narayan  *	None
   2563   6480    narayan  */
   2564   6480    narayan static void
   2565   6480    narayan vdc_fini_ports(vdc_t *vdc)
   2566   6480    narayan {
   2567   6480    narayan 	int		instance = ddi_get_instance(vdc->dip);
   2568   6480    narayan 	vdc_server_t	*srvr, *prev_srvr;
   2569   6480    narayan 
   2570   1991      heppo 	ASSERT(vdc != NULL);
   2571   1991      heppo 	ASSERT(mutex_owned(&vdc->lock));
   2572   1991      heppo 
   2573   2793    lm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized);
   2574   1991      heppo 
   2575   6480    narayan 	srvr = vdc->server_list;
   2576   6480    narayan 
   2577   6480    narayan 	while (srvr) {
   2578   6480    narayan 
   2579   6480    narayan 		vdc_terminate_ldc(vdc, srvr);
   2580   6480    narayan 
   2581   6480    narayan 		/* next server */
   2582   6480    narayan 		prev_srvr = srvr;
   2583   6480    narayan 		srvr = srvr->next;
   2584   6480    narayan 
   2585   6480    narayan 		/* free server */
   2586   6480    narayan 		kmem_free(prev_srvr, sizeof (vdc_server_t));
   2587   6480    narayan 	}
   2588   6480    narayan 
   2589   6480    narayan 	vdc->server_list = NULL;
   2590  11004  Alexandre 	vdc->num_servers = 0;
   2591   1991      heppo }
   2592   1991      heppo 
   2593   1991      heppo /* -------------------------------------------------------------------------- */
   2594   1991      heppo 
   2595   1991      heppo /*
   2596   1991      heppo  * Descriptor Ring helper routines
   2597   1991      heppo  */
   2598   1991      heppo 
   2599   2032    lm66018 /*
   2600   2032    lm66018  * Function:
   2601   2032    lm66018  *	vdc_init_descriptor_ring()
   2602   2032    lm66018  *
   2603   2032    lm66018  * Description:
   2604   2032    lm66018  *
   2605   2032    lm66018  * Arguments:
   2606   2032    lm66018  *	vdc	- soft state pointer for this instance of the device driver.
   2607   2032    lm66018  *
   2608   2032    lm66018  * Return Code:
   2609   2032    lm66018  *	0	- Success
   2610   2032    lm66018  */
   2611   1991      heppo static int
   2612   1991      heppo vdc_init_descriptor_ring(vdc_t *vdc)
   2613   1991      heppo {
   2614   1991      heppo 	vd_dring_entry_t	*dep = NULL;	/* DRing Entry pointer */
   2615   2032    lm66018 	int	status = 0;
   2616   1991      heppo 	int	i;
   2617   1991      heppo 
   2618   2793    lm66018 	DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized);
   2619   1991      heppo 
   2620   1991      heppo 	ASSERT(vdc != NULL);
   2621   1991      heppo 	ASSERT(mutex_owned(&vdc->lock));
   2622   1991      heppo 
   2623   2410    lm66018 	/* ensure we have enough room to store max sized block */
   2624   2410    lm66018 	ASSERT(maxphys <= VD_MAX_BLOCK_SIZE);
   2625   2410    lm66018 
   2626   2032    lm66018 	if ((vdc->initialized & VDC_DRING_INIT) == 0) {
   2627   2793    lm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance);
   2628   2410    lm66018 		/*
   2629   2410    lm66018 		 * Calculate the maximum block size we can transmit using one
   2630   2410    lm66018 		 * Descriptor Ring entry from the attributes returned by the
   2631   2410    lm66018 		 * vDisk server. This is subject to a minimum of 'maxphys'
   2632   2410    lm66018 		 * as we do not have the capability to split requests over
   2633   2410    lm66018 		 * multiple DRing entries.
   2634   2410    lm66018 		 */
   2635   9889      Larry 		if ((vdc->max_xfer_sz * vdc->vdisk_bsize) < maxphys) {
   2636   2793    lm66018 			DMSG(vdc, 0, "[%d] using minimum DRing size\n",
   2637   4696   achartre 			    vdc->instance);
   2638   2410    lm66018 			vdc->dring_max_cookies = maxphys / PAGESIZE;
   2639   2410    lm66018 		} else {
   2640   2410    lm66018 			vdc->dring_max_cookies =
   2641   9889      Larry 			    (vdc->max_xfer_sz * vdc->vdisk_bsize) / PAGESIZE;
   2642   2410    lm66018 		}
   2643   2410    lm66018 		vdc->dring_entry_size = (sizeof (vd_dring_entry_t) +
   2644   4696   achartre 		    (sizeof (ldc_mem_cookie_t) *
   2645   4696   achartre 		    (vdc->dring_max_cookies - 1)));
   2646   2410    lm66018 		vdc->dring_len = VD_DRING_LEN;
   2647   2410    lm66018 
   2648   2410    lm66018 		status = ldc_mem_dring_create(vdc->dring_len,
   2649   6480    narayan 		    vdc->dring_entry_size, &vdc->dring_hdl);
   2650   6480    narayan 		if ((vdc->dring_hdl == NULL) || (status != 0)) {
   2651   2793    lm66018 			DMSG(vdc, 0, "[%d] Descriptor ring creation failed",
   2652   4696   achartre 			    vdc->instance);
   2653   2410    lm66018 			return (status);
   2654   2410    lm66018 		}
   2655   2032    lm66018 		vdc->initialized |= VDC_DRING_INIT;
   2656   2032    lm66018 	}
   2657   2032    lm66018 
   2658   2032    lm66018 	if ((vdc->initialized & VDC_DRING_BOUND) == 0) {
   2659   2793    lm66018 		DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance);
   2660   2032    lm66018 		vdc->dring_cookie =
   2661   4696   achartre 		    kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP);
   2662   2032    lm66018 
   2663   6480    narayan 		status = ldc_mem_dring_bind(vdc->curr_server->ldc_handle,
   2664   6480    narayan 		    vdc->dring_hdl,
   2665   4696   achartre 		    LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW,
   2666   4696   achartre 		    &vdc->dring_cookie[0],
   2667   4696   achartre 		    &vdc->dring_cookie_count);
   2668   2032    lm66018 		if (status != 0) {
   2669   2793    lm66018 			DMSG(vdc, 0, "[%d] Failed to bind descriptor ring "
   2670   4696   achartre 			    "(%lx) to channel (%lx) status=%d\n",
   2671   6480    narayan 			    vdc->instance, vdc->dring_hdl,
   2672   6480    narayan 			    vdc->curr_server->ldc_handle, status);
   2673   2032    lm66018 			return (status);
   2674   2032    lm66018 		}
   2675   2032    lm66018 		ASSERT(vdc->dring_cookie_count == 1);
   2676   2032    lm66018 		vdc->initialized |= VDC_DRING_BOUND;
   2677   2032    lm66018 	}
   2678   1991      heppo 
   2679   6480    narayan 	status = ldc_mem_dring_info(vdc->dring_hdl, &vdc->dring_mem_info);
   2680   1991      heppo 	if (status != 0) {
   2681   2793    lm66018 		DMSG(vdc, 0,
   2682   2793    lm66018 		    "[%d] Failed to get info for descriptor ring (%lx)\n",
   2683   6480    narayan 		    vdc->instance, vdc->dring_hdl);
   2684   1991      heppo 		return (status);
   2685   1991      heppo 	}
   2686   1991      heppo 
   2687   2032    lm66018 	if ((vdc->initialized & VDC_DRING_LOCAL) == 0) {
   2688   2793    lm66018 		DMSG(vdc, 0, "[%d] local dring\n", vdc->instance);
   2689   2032    lm66018 
   2690   2032    lm66018 		/* Allocate the local copy of this dring */
   2691   2032    lm66018 		vdc->local_dring =
   2692   4696   achartre 		    kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t),
   2693   4696   achartre 		    KM_SLEEP);
   2694   2032    lm66018 		vdc->initialized |= VDC_DRING_LOCAL;
   2695   2032    lm66018 	}
   2696   2032    lm66018 
   2697   2032    lm66018 	/*
   2698   2032    lm66018 	 * Mark all DRing entries as free and initialize the private
   2699   2032    lm66018 	 * descriptor's memory handles. If any entry is initialized,
   2700   2032    lm66018 	 * we need to free it later so we set the bit in 'initialized'
   2701   2032    lm66018 	 * at the start.
   2702   1991      heppo 	 */
   2703   1991      heppo 	vdc->initialized |= VDC_DRING_ENTRY;
   2704   2410    lm66018 	for (i = 0; i < vdc->dring_len; i++) {
   2705   1991      heppo 		dep = VDC_GET_DRING_ENTRY_PTR(vdc, i);
   2706   1991      heppo 		dep->hdr.dstate = VIO_DESC_FREE;
   2707   1991      heppo 
   2708   6480    narayan 		status = ldc_mem_alloc_handle(vdc->curr_server->ldc_handle,
   2709   4696   achartre 		    &vdc->local_dring[i].desc_mhdl);
   2710   1991      heppo 		if (status != 0) {
   2711   2793    lm66018 			DMSG(vdc, 0, "![%d] Failed to alloc mem handle for"
   2712   4696   achartre 			    " descriptor %d", vdc->instance, i);
   2713   1991      heppo 			return (status);
   2714   1991      heppo 		}
   2715   2793    lm66018 		vdc->local_dring[i].is_free = B_TRUE;
   2716   1991      heppo 		vdc->local_dring[i].dep = dep;
   2717   2793    lm66018 	}
   2718   2793    lm66018 
   2719   2793    lm66018 	/* Initialize the starting index */
   2720   2793    lm66018 	vdc->dring_curr_idx = 0;
   2721   1991      heppo 
   2722   1991      heppo 	return (status);
   2723   1991      heppo }
   2724   1991      heppo 
   2725   2032    lm66018 /*
   2726   2032    lm66018  * Function:
   2727   2032    lm66018  *	vdc_destroy_descriptor_ring()
   2728   2032    lm66018  *
   2729   2032    lm66018  * Description:
   2730   2032    lm66018  *
   2731   2032    lm66018  * Arguments:
   2732   2032    lm66018  *	vdc	- soft state pointer for this instance of the device driver.
   2733   2032    lm66018  *
   2734   2032    lm66018  * Return Code:
   2735   2032    lm66018  *	None
   2736   2032    lm66018  */
   2737   1991      heppo static void
   2738   1991      heppo vdc_destroy_descriptor_ring(vdc_t *vdc)
   2739   1991      heppo {
   2740   2032    lm66018 	vdc_local_desc_t	*ldep = NULL;	/* Local Dring Entry Pointer */
   2741   1991      heppo 	ldc_mem_handle_t	mhdl = NULL;
   2742   2793    lm66018 	ldc_mem_info_t		minfo;
   2743   2032    lm66018 	int			status = -1;
   2744   2032    lm66018 	int			i;	/* loop */
   2745   1991      heppo 
   2746   1991      heppo 	ASSERT(vdc != NULL);
   2747   1991      heppo 	ASSERT(mutex_owned(&vdc->lock));
   2748   2793    lm66018 
   2749   2793    lm66018 	DMSG(vdc, 0, "[%d] Entered\n", vdc->instance);
   2750   1991      heppo 
   2751   1991      heppo 	if (vdc->initialized & VDC_DRING_ENTRY) {
   2752   2793    lm66018 		DMSG(vdc, 0,
   2753   2793    lm66018 		    "[%d] Removing Local DRing entries\n", vdc->instance);
   2754   2410    lm66018 		for (i = 0; i < vdc->dring_len; i++) {
   2755   2032    lm66018 			ldep = &vdc->local_dring[i];
   2756   2032    lm66018 			mhdl = ldep->desc_mhdl;
   2757   2032    lm66018 
   2758   2032    lm66018 			if (mhdl == NULL)
   2759   2032    lm66018 				continue;
   2760   2032    lm66018 
   2761   2793    lm66018 			if ((status = ldc_mem_info(mhdl, &minfo)) != 0) {
   2762   2793    lm66018 				DMSG(vdc, 0,
   2763   2793    lm66018 				    "ldc_mem_info returned an error: %d\n",
   2764   2793    lm66018 				    status);
   2765   2793    lm66018 
   2766   2793    lm66018 				/*
   2767   2793    lm66018 				 * This must mean that the mem handle
   2768   2793    lm66018 				 * is not valid. Clear it out so that
   2769   2793    lm66018 				 * no one tries to use it.
   2770   2793    lm66018 				 */
   2771   2793    lm66018 				ldep->desc_mhdl = NULL;
   2772   2793    lm66018 				continue;
   2773   2793    lm66018 			}
   2774   2793    lm66018 
   2775   2793    lm66018 			if (minfo.status == LDC_BOUND) {
   2776   2793    lm66018 				(void) ldc_mem_unbind_handle(mhdl);
   2777   2793    lm66018 			}
   2778   2793    lm66018 
   2779   2032    lm66018 			(void) ldc_mem_free_handle(mhdl);
   2780   2793    lm66018 
   2781   2793    lm66018 			ldep->desc_mhdl = NULL;
   2782   1991      heppo 		}
   2783   1991      heppo 		vdc->initialized &= ~VDC_DRING_ENTRY;
   2784   1991      heppo 	}
   2785   1991      heppo 
   2786   1991      heppo 	if (vdc->initialized & VDC_DRING_LOCAL) {
   2787   2793    lm66018 		DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance);
   2788   1991      heppo 		kmem_free(vdc->local_dring,
   2789   4696   achartre 		    vdc->dring_len * sizeof (vdc_local_desc_t));
   2790   1991      heppo 		vdc->initialized &= ~VDC_DRING_LOCAL;
   2791   1991      heppo 	}
   2792   1991      heppo 
   2793   1991      heppo 	if (vdc->initialized & VDC_DRING_BOUND) {
   2794   2793    lm66018 		DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance);
   2795   6480    narayan 		status = ldc_mem_dring_unbind(vdc->dring_hdl);
   2796   1991      heppo 		if (status == 0) {
   2797   1991      heppo 			vdc->initialized &= ~VDC_DRING_BOUND;
   2798   1991      heppo 		} else {
   2799   2793    lm66018 			DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx",
   2800   6480    narayan 			    vdc->instance, status, vdc->dring_hdl);
   2801   1991      heppo 		}
   2802   2793    lm66018 		kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t));
   2803   1991      heppo 	}
   2804   1991      heppo 
   2805   1991      heppo 	if (vdc->initialized & VDC_DRING_INIT) {
   2806   2793    lm66018 		DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance);
   2807   6480    narayan 		status = ldc_mem_dring_destroy(vdc->dring_hdl);
   2808   1991      heppo 		if (status == 0) {
   2809   6480    narayan 			vdc->dring_hdl = NULL;
   2810   1991      heppo 			bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t));
   2811   1991      heppo 			vdc->initialized &= ~VDC_DRING_INIT;
   2812   1991      heppo 		} else {
   2813   2793    lm66018 			DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)",
   2814   6480    narayan 			    vdc->instance, status, vdc->dring_hdl);
   2815   1991      heppo 		}
   2816   1991      heppo 	}
   2817   1991      heppo }
   2818   1991      heppo 
   2819   1991      heppo /*
   2820   2793    lm66018  * Function:
   2821   6468    lm66018  *	vdc_map_to_shared_dring()
   2822   2793    lm66018  *
   2823   2793    lm66018  * Description:
   2824   2793    lm66018  *	Copy contents of the local descriptor to the shared
   2825   2793    lm66018  *	memory descriptor.
   2826   2793    lm66018  *
   2827   2793    lm66018  * Arguments:
   2828   2793    lm66018  *	vdcp	- soft state pointer for this instance of the device driver.
   2829   2793    lm66018  *	idx	- descriptor ring index
   2830   2793    lm66018  *
   2831   2793    lm66018  * Return Code:
   2832   2793    lm66018  *	None
   2833   2793    lm66018  */
   2834   2793    lm66018 static int
   2835   2793    lm66018 vdc_map_to_shared_dring(vdc_t *vdcp, int idx)
   2836   2793    lm66018 {
   2837   2793    lm66018 	vdc_local_desc_t	*ldep;
   2838   2793    lm66018 	vd_dring_entry_t	*dep;
   2839   2793    lm66018 	int			rv;
   2840   2793    lm66018 
   2841   2793    lm66018 	ldep = &(vdcp->local_dring[idx]);
   2842   2793    lm66018 
   2843   2793    lm66018 	/* for now leave in the old pop_mem_hdl stuff */
   2844   2793    lm66018 	if (ldep->nbytes > 0) {
   2845   2793    lm66018 		rv = vdc_populate_mem_hdl(vdcp, ldep);
   2846   2793    lm66018 		if (rv) {
   2847   2793    lm66018 			DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n",
   2848   2793    lm66018 			    vdcp->instance);
   2849   2793    lm66018 			return (rv);
   2850   2793    lm66018 		}
   2851   2793    lm66018 	}
   2852   2793    lm66018 
   2853   2793    lm66018 	/*
   2854   2793    lm66018 	 * fill in the data details into the DRing
   2855   2793    lm66018 	 */
   2856   2336    narayan 	dep = ldep->dep;
   2857   2336    narayan 	ASSERT(dep != NULL);
   2858   2336    narayan 
   2859   2793    lm66018 	dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp);
   2860   2793    lm66018 	dep->payload.operation = ldep->operation;
   2861   2793    lm66018 	dep->payload.addr = ldep->offset;
   2862   2793    lm66018 	dep->payload.nbytes = ldep->nbytes;
   2863   2840   carlsonj 	dep->payload.status = (uint32_t)-1;	/* vds will set valid value */
   2864   2793    lm66018 	dep->payload.slice = ldep->slice;
   2865   2793    lm66018 	dep->hdr.dstate = VIO_DESC_READY;
   2866   2793    lm66018 	dep->hdr.ack = 1;		/* request an ACK for every message */
   2867   2793    lm66018 
   2868   2793    lm66018 	return (0);
   2869   2793    lm66018 }
   2870   2793    lm66018 
   2871   2793    lm66018 /*
   2872   2793    lm66018  * Function:
   2873   2793    lm66018  *	vdc_send_request
   2874   1991      heppo  *
   2875   1991      heppo  * Description:
   2876   1991      heppo  *	This routine writes the data to be transmitted to vds into the
   2877   1991      heppo  *	descriptor, notifies vds that the ring has been updated and
   2878   1991      heppo  *	then waits for the request to be processed.
   2879   1991      heppo  *
   2880   1991      heppo  * Arguments:
   2881   2793    lm66018  *	vdcp	  - the soft state pointer
   2882   1991      heppo  *	operation - operation we want vds to perform (VD_OP_XXX)
   2883   2793    lm66018  *	addr	  - address of data buf to be read/written.
   2884   2793    lm66018  *	nbytes	  - number of bytes to read/write
   2885   2793    lm66018  *	slice	  - the disk slice this request is for
   2886   2793    lm66018  *	offset	  - relative disk offset
   2887  11004  Alexandre  *	bufp	  - buf of operation
   2888   2793    lm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
   2889   1991      heppo  *
   2890   1991      heppo  * Return Codes:
   2891   1991      heppo  *	0
   2892   3560    narayan  *	ENXIO
   2893   1991      heppo  */
   2894   1991      heppo static int
   2895   2793    lm66018 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr,
   2896  11004  Alexandre     size_t nbytes, int slice, diskaddr_t offset, buf_t *bufp,
   2897  11004  Alexandre     vio_desc_direction_t dir, int flags)
   2898   2793    lm66018 {
   2899   6099    lm66018 	int	rv = 0;
   2900   6099    lm66018 
   2901   2793    lm66018 	ASSERT(vdcp != NULL);
   2902   4696   achartre 	ASSERT(slice == VD_SLICE_NONE || slice < V_NUMPAR);
   2903   2793    lm66018 
   2904   2793    lm66018 	mutex_enter(&vdcp->lock);
   2905   2793    lm66018 
   2906   6099    lm66018 	/*
   2907   6099    lm66018 	 * If this is a block read/write operation we update the I/O statistics
   2908   6099    lm66018 	 * to indicate that the request is being put on the waitq to be
   2909   6099    lm66018 	 * serviced.
   2910   6099    lm66018 	 *
   2911   6099    lm66018 	 * We do it here (a common routine for both synchronous and strategy
   2912   6099    lm66018 	 * calls) for performance reasons - we are already holding vdc->lock
   2913   6099    lm66018 	 * so there is no extra locking overhead. We would have to explicitly
   2914   6099    lm66018 	 * grab the 'lock' mutex to update the stats if we were to do this
   2915   6099    lm66018 	 * higher up the stack in vdc_strategy() et. al.
   2916   6099    lm66018 	 */
   2917   6099    lm66018 	if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
   2918  11004  Alexandre 		DTRACE_IO1(start, buf_t *, bufp);
   2919   6468    lm66018 		VD_KSTAT_WAITQ_ENTER(vdcp);
   2920  11004  Alexandre 	}
   2921  11004  Alexandre 
   2922  11004  Alexandre 	/*
   2923  11004  Alexandre 	 * If the request does not expect the state to be VDC_STATE_RUNNING
   2924  11004  Alexandre 	 * then we just try to populate the descriptor ring once.
   2925  11004  Alexandre 	 */
   2926  11004  Alexandre 	if (!(flags & VDC_OP_STATE_RUNNING)) {
   2927  11004  Alexandre 		rv = vdc_populate_descriptor(vdcp, operation, addr,
   2928  11004  Alexandre 		    nbytes, slice, offset, bufp, dir, flags);
   2929  11004  Alexandre 		goto done;
   2930   6099    lm66018 	}
   2931   6099    lm66018 
   2932   2793    lm66018 	do {
   2933   3401    narayan 		while (vdcp->state != VDC_STATE_RUNNING) {
   2934   3401    narayan 
   2935   3401    narayan 			/* return error if detaching */
   2936   3401    narayan 			if (vdcp->state == VDC_STATE_DETACH) {
   2937   6099    lm66018 				rv = ENXIO;
   2938   6099    lm66018 				goto done;
   2939   3401    narayan 			}
   2940   4848   achartre 
   2941   5658   achartre 			/*
   2942   5658   achartre 			 * If we are panicking and the disk is not ready then
   2943   5658   achartre 			 * we can't send any request because we can't complete
   2944   5658   achartre 			 * the handshake now.
   2945   5658   achartre 			 */
   2946   5658   achartre 			if (ddi_in_panic()) {
   2947   6099    lm66018 				rv = EIO;
   2948   6099    lm66018 				goto done;
   2949   5658   achartre 			}
   2950   5658   achartre 
   2951  11004  Alexandre 			/*
   2952  11004  Alexandre 			 * If the state is faulted, notify that a new I/O is
   2953  11004  Alexandre 			 * being submitted to force the system to check if any
   2954  11004  Alexandre 			 * server has recovered.
   2955  11004  Alexandre 			 */
   2956  11004  Alexandre 			if (vdcp->state == VDC_STATE_FAILED) {
   2957  11004  Alexandre 				vdcp->io_pending = B_TRUE;
   2958  11004  Alexandre 				cv_signal(&vdcp->io_pending_cv);
   2959  11004  Alexandre 			}
   2960  11004  Alexandre 
   2961   4848   achartre 			cv_wait(&vdcp->running_cv, &vdcp->lock);
   2962  11004  Alexandre 
   2963  11004  Alexandre 			/* if service is still faulted then fail the request */
   2964  11004  Alexandre 			if (vdcp->state == VDC_STATE_FAILED) {
   2965  11004  Alexandre 				rv = EIO;
   2966  11004  Alexandre 				goto done;
   2967  11004  Alexandre 			}
   2968   3401    narayan 		}
   2969   2793    lm66018 
   2970   2793    lm66018 	} while (vdc_populate_descriptor(vdcp, operation, addr,
   2971  11004  Alexandre 	    nbytes, slice, offset, bufp, dir, flags));
   2972   2793    lm66018 
   2973   6099    lm66018 done:
   2974   6099    lm66018 	/*
   2975   6099    lm66018 	 * If this is a block read/write we update the I/O statistics kstat
   2976   6099    lm66018 	 * to indicate that this request has been placed on the queue for
   2977   6099    lm66018 	 * processing (i.e sent to the vDisk server) - iostat(1M) will
   2978   6099    lm66018 	 * report the time waiting for the vDisk server under the %b column
   2979   6099    lm66018 	 * In the case of an error we simply take it off the wait queue.
   2980   6099    lm66018 	 */
   2981   6099    lm66018 	if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
   2982   6099    lm66018 		if (rv == 0) {
   2983   6468    lm66018 			VD_KSTAT_WAITQ_TO_RUNQ(vdcp);
   2984  11004  Alexandre 			DTRACE_PROBE1(send, buf_t *, bufp);
   2985   6099    lm66018 		} else {
   2986   6099    lm66018 			VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
   2987   6468    lm66018 			VD_KSTAT_WAITQ_EXIT(vdcp);
   2988  11004  Alexandre 			DTRACE_IO1(done, buf_t *, bufp);
   2989   6099    lm66018 		}
   2990   6099    lm66018 	}
   2991   6099    lm66018 
   2992   6099    lm66018 	mutex_exit(&vdcp->lock);
   2993   6099    lm66018 
   2994   6099    lm66018 	return (rv);
   2995   2793    lm66018 }
   2996   2793    lm66018 
   2997   2793    lm66018 
   2998   2793    lm66018 /*
   2999   2793    lm66018  * Function:
   3000   2793    lm66018  *	vdc_populate_descriptor
   3001   2793    lm66018  *
   3002   2793    lm66018  * Description:
   3003   2793    lm66018  *	This routine writes the data to be transmitted to vds into the
   3004   2793    lm66018  *	descriptor, notifies vds that the ring has been updated and
   3005   2793    lm66018  *	then waits for the request to be processed.
   3006   2793    lm66018  *
   3007   2793    lm66018  * Arguments:
   3008   2793    lm66018  *	vdcp	  - the soft state pointer
   3009   2793    lm66018  *	operation - operation we want vds to perform (VD_OP_XXX)
   3010   2793    lm66018  *	addr	  - address of data buf to be read/written.
   3011   2793    lm66018  *	nbytes	  - number of bytes to read/write
   3012   2793    lm66018  *	slice	  - the disk slice this request is for
   3013   2793    lm66018  *	offset	  - relative disk offset
   3014  11004  Alexandre  *	bufp	  - buf of operation
   3015   2793    lm66018  *	dir	  - direction of operation (READ/WRITE/BOTH)
   3016   2793    lm66018  *
   3017   2793    lm66018  * Return Codes:
   3018   2793    lm66018  *	0
   3019   2793    lm66018  *	EAGAIN
   3020   5365    lm66018  *	ECONNRESET
   3021   5365    lm66018  *	ENXIO
   3022   2793    lm66018  */
   3023   2793    lm66018 static int
   3024   2793    lm66018 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr,
   3025  11004  Alexandre     size_t nbytes, int slice, diskaddr_t offset,
   3026  11004  Alexandre     buf_t *bufp, vio_desc_direction_t dir, int flags)
   3027   2793    lm66018 {
   3028   2793    lm66018 	vdc_local_desc_t	*local_dep = NULL; /* Local Dring Pointer */
   3029   2793    lm66018 	int			idx;		/* Index of DRing entry used */
   3030   2793    lm66018 	int			next_idx;
   3031   1991      heppo 	vio_dring_msg_t		dmsg;
   3032   2793    lm66018 	size_t			msglen;
   3033   1991      heppo 	int			rv;
   3034   1991      heppo 
   3035   2793    lm66018 	ASSERT(MUTEX_HELD(&vdcp->lock));
   3036   2793    lm66018 	vdcp->threads_pending++;
   3037   2793    lm66018 loop:
   3038   2793    lm66018 	DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx);
   3039   2793    lm66018 
   3040   2793    lm66018 	/* Get next available D-Ring entry */
   3041   2793    lm66018 	idx = vdcp->dring_curr_idx;
   3042   2793    lm66018 	local_dep = &(vdcp->local_dring[idx]);
   3043   2793    lm66018 
   3044   2793    lm66018 	if (!local_dep->is_free) {
   3045   2793    lm66018 		DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n",
   3046   2793    lm66018 		    vdcp->instance);
   3047   2793    lm66018 		cv_wait(&vdcp->dring_free_cv, &vdcp->lock);
   3048   2793    lm66018 		if (vdcp->state == VDC_STATE_RUNNING ||
   3049   2793    lm66018 		    vdcp->state == VDC_STATE_HANDLE_PENDING) {
   3050   2793    lm66018 			goto loop;
   3051   2793    lm66018 		}
   3052   2793    lm66018 		vdcp->threads_pending--;
   3053   2793    lm66018 		return (ECONNRESET);
   3054   2793    lm66018 	}
   3055   2793    lm66018 
   3056   2793    lm66018 	next_idx = idx + 1;
   3057   2793    lm66018 	if (next_idx >= vdcp->dring_len)
   3058   2793    lm66018