Print this page
XXXX introduce drv_sectohz
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/sun4v/io/vdc.c
+++ new/usr/src/uts/sun4v/io/vdc.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 /*
27 27 * LDoms virtual disk client (vdc) device driver
28 28 *
29 29 * This driver runs on a guest logical domain and communicates with the virtual
30 30 * disk server (vds) driver running on the service domain which is exporting
31 31 * virtualized "disks" to the guest logical domain.
32 32 *
33 33 * The driver can be divided into four sections:
34 34 *
35 35 * 1) generic device driver housekeeping
36 36 * _init, _fini, attach, detach, ops structures, etc.
37 37 *
38 38 * 2) communication channel setup
39 39 * Setup the communications link over the LDC channel that vdc uses to
40 40 * talk to the vDisk server. Initialise the descriptor ring which
41 41 * allows the LDC clients to transfer data via memory mappings.
42 42 *
43 43 * 3) Support exported to upper layers (filesystems, etc)
44 44 * The upper layers call into vdc via strategy(9E) and DKIO(7I)
45 45 * ioctl calls. vdc will copy the data to be written to the descriptor
46 46 * ring or maps the buffer to store the data read by the vDisk
47 47 * server into the descriptor ring. It then sends a message to the
48 48 * vDisk server requesting it to complete the operation.
49 49 *
50 50 * 4) Handling responses from vDisk server.
51 51 * The vDisk server will ACK some or all of the messages vdc sends to it
52 52 * (this is configured during the handshake). Upon receipt of an ACK
53 53 * vdc will check the descriptor ring and signal to the upper layer
54 54 * code waiting on the IO.
55 55 */
56 56
57 57 #include <sys/atomic.h>
58 58 #include <sys/conf.h>
59 59 #include <sys/disp.h>
60 60 #include <sys/ddi.h>
61 61 #include <sys/dkio.h>
62 62 #include <sys/efi_partition.h>
63 63 #include <sys/fcntl.h>
64 64 #include <sys/file.h>
65 65 #include <sys/kstat.h>
66 66 #include <sys/mach_descrip.h>
67 67 #include <sys/modctl.h>
68 68 #include <sys/mdeg.h>
69 69 #include <sys/note.h>
70 70 #include <sys/open.h>
71 71 #include <sys/random.h>
72 72 #include <sys/sdt.h>
73 73 #include <sys/stat.h>
74 74 #include <sys/sunddi.h>
75 75 #include <sys/types.h>
76 76 #include <sys/promif.h>
77 77 #include <sys/var.h>
78 78 #include <sys/vtoc.h>
79 79 #include <sys/archsystm.h>
80 80 #include <sys/sysmacros.h>
81 81
82 82 #include <sys/cdio.h>
83 83 #include <sys/dktp/fdisk.h>
84 84 #include <sys/dktp/dadkio.h>
85 85 #include <sys/fs/dv_node.h>
86 86 #include <sys/mhd.h>
87 87 #include <sys/scsi/generic/sense.h>
88 88 #include <sys/scsi/impl/uscsi.h>
89 89 #include <sys/scsi/impl/services.h>
90 90 #include <sys/scsi/targets/sddef.h>
91 91
92 92 #include <sys/ldoms.h>
93 93 #include <sys/ldc.h>
94 94 #include <sys/vio_common.h>
95 95 #include <sys/vio_mailbox.h>
96 96 #include <sys/vio_util.h>
97 97 #include <sys/vdsk_common.h>
98 98 #include <sys/vdsk_mailbox.h>
99 99 #include <sys/vdc.h>
100 100
101 101 #define VD_OLDVTOC_LIMIT 0x7fffffff
102 102
103 103 /*
104 104 * function prototypes
105 105 */
106 106
107 107 /* standard driver functions */
108 108 static int vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred);
109 109 static int vdc_close(dev_t dev, int flag, int otyp, cred_t *cred);
110 110 static int vdc_strategy(struct buf *buf);
111 111 static int vdc_print(dev_t dev, char *str);
112 112 static int vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
113 113 static int vdc_read(dev_t dev, struct uio *uio, cred_t *cred);
114 114 static int vdc_write(dev_t dev, struct uio *uio, cred_t *cred);
115 115 static int vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
116 116 cred_t *credp, int *rvalp);
117 117 static int vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred);
118 118 static int vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred);
119 119
120 120 static int vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
121 121 void *arg, void **resultp);
122 122 static int vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
123 123 static int vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
124 124 static int vdc_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
125 125 int mod_flags, char *name, caddr_t valuep, int *lengthp);
126 126
127 127 /* setup */
128 128 static void vdc_min(struct buf *bufp);
129 129 static int vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen);
130 130 static int vdc_do_ldc_init(vdc_t *vdc, vdc_server_t *srvr);
131 131 static int vdc_start_ldc_connection(vdc_t *vdc);
132 132 static int vdc_create_device_nodes(vdc_t *vdc);
133 133 static int vdc_create_device_nodes_efi(vdc_t *vdc);
134 134 static int vdc_create_device_nodes_vtoc(vdc_t *vdc);
135 135 static void vdc_create_io_kstats(vdc_t *vdc);
136 136 static void vdc_create_err_kstats(vdc_t *vdc);
137 137 static void vdc_set_err_kstats(vdc_t *vdc);
138 138 static int vdc_get_md_node(dev_info_t *dip, md_t **mdpp,
139 139 mde_cookie_t *vd_nodep);
140 140 static int vdc_init_ports(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_nodep);
141 141 static void vdc_fini_ports(vdc_t *vdc);
142 142 static void vdc_switch_server(vdc_t *vdcp);
143 143 static int vdc_do_ldc_up(vdc_t *vdc);
144 144 static void vdc_terminate_ldc(vdc_t *vdc, vdc_server_t *srvr);
145 145 static int vdc_init_descriptor_ring(vdc_t *vdc);
146 146 static void vdc_destroy_descriptor_ring(vdc_t *vdc);
147 147 static int vdc_setup_devid(vdc_t *vdc);
148 148 static void vdc_store_label_efi(vdc_t *, efi_gpt_t *, efi_gpe_t *);
149 149 static void vdc_store_label_vtoc(vdc_t *, struct dk_geom *,
150 150 struct extvtoc *);
151 151 static void vdc_store_label_unk(vdc_t *vdc);
152 152 static boolean_t vdc_is_opened(vdc_t *vdc);
153 153 static void vdc_update_size(vdc_t *vdc, size_t, size_t, size_t);
154 154 static int vdc_update_vio_bsize(vdc_t *vdc, uint32_t);
155 155
156 156 /* handshake with vds */
157 157 static int vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver);
158 158 static int vdc_ver_negotiation(vdc_t *vdcp);
159 159 static int vdc_init_attr_negotiation(vdc_t *vdc);
160 160 static int vdc_attr_negotiation(vdc_t *vdcp);
161 161 static int vdc_init_dring_negotiate(vdc_t *vdc);
162 162 static int vdc_dring_negotiation(vdc_t *vdcp);
163 163 static int vdc_send_rdx(vdc_t *vdcp);
164 164 static int vdc_rdx_exchange(vdc_t *vdcp);
165 165 static boolean_t vdc_is_supported_version(vio_ver_msg_t *ver_msg);
166 166
167 167 /* processing incoming messages from vDisk server */
168 168 static void vdc_process_msg_thread(vdc_t *vdc);
169 169 static int vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp);
170 170
171 171 static uint_t vdc_handle_cb(uint64_t event, caddr_t arg);
172 172 static int vdc_process_data_msg(vdc_t *vdc, vio_msg_t *msg);
173 173 static int vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg);
174 174 static int vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg);
175 175 static int vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *msg);
176 176 static int vdc_send_request(vdc_t *vdcp, int operation,
177 177 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
178 178 buf_t *bufp, vio_desc_direction_t dir, int flags);
179 179 static int vdc_map_to_shared_dring(vdc_t *vdcp, int idx);
180 180 static int vdc_populate_descriptor(vdc_t *vdcp, int operation,
181 181 caddr_t addr, size_t nbytes, int slice, diskaddr_t offset,
182 182 buf_t *bufp, vio_desc_direction_t dir, int flags);
183 183 static int vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr,
184 184 size_t nbytes, int slice, diskaddr_t offset,
185 185 vio_desc_direction_t dir, boolean_t);
186 186 static int vdc_do_op(vdc_t *vdc, int op, caddr_t addr, size_t nbytes,
187 187 int slice, diskaddr_t offset, struct buf *bufp,
188 188 vio_desc_direction_t dir, int flags);
189 189
190 190 static int vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp);
191 191 static int vdc_drain_response(vdc_t *vdcp, struct buf *buf);
192 192 static int vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx);
193 193 static int vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep);
194 194 static int vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg);
195 195
196 196 /* dkio */
197 197 static int vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode,
198 198 int *rvalp);
199 199 static int vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg);
200 200 static void vdc_create_fake_geometry(vdc_t *vdc);
201 201 static int vdc_validate_geometry(vdc_t *vdc);
202 202 static void vdc_validate(vdc_t *vdc);
203 203 static void vdc_validate_task(void *arg);
204 204 static int vdc_null_copy_func(vdc_t *vdc, void *from, void *to,
205 205 int mode, int dir);
206 206 static int vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
207 207 int mode, int dir);
208 208 static int vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
209 209 int mode, int dir);
210 210 static int vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to,
211 211 int mode, int dir);
212 212 static int vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to,
213 213 int mode, int dir);
214 214 static int vdc_get_extvtoc_convert(vdc_t *vdc, void *from, void *to,
215 215 int mode, int dir);
216 216 static int vdc_set_extvtoc_convert(vdc_t *vdc, void *from, void *to,
217 217 int mode, int dir);
218 218 static int vdc_get_geom_convert(vdc_t *vdc, void *from, void *to,
219 219 int mode, int dir);
220 220 static int vdc_set_geom_convert(vdc_t *vdc, void *from, void *to,
221 221 int mode, int dir);
222 222 static int vdc_get_efi_convert(vdc_t *vdc, void *from, void *to,
223 223 int mode, int dir);
224 224 static int vdc_set_efi_convert(vdc_t *vdc, void *from, void *to,
225 225 int mode, int dir);
226 226
227 227 static void vdc_ownership_update(vdc_t *vdc, int ownership_flags);
228 228 static int vdc_access_set(vdc_t *vdc, uint64_t flags);
229 229 static vdc_io_t *vdc_eio_queue(vdc_t *vdc, int index);
230 230 static void vdc_eio_unqueue(vdc_t *vdc, clock_t deadline,
231 231 boolean_t complete_io);
232 232 static int vdc_eio_check(vdc_t *vdc, int flags);
233 233 static void vdc_eio_thread(void *arg);
234 234
235 235 /*
236 236 * Module variables
237 237 */
238 238
239 239 /*
240 240 * Number of handshake retries with the current server before switching to
241 241 * a different server. These retries are done so that we stick with the same
242 242 * server if vdc receives a LDC reset event during the initiation of the
243 243 * handshake. This can happen if vdc reset the LDC channel and then immediately
244 244 * retry a connexion before it has received the LDC reset event.
245 245 *
246 246 * If there is only one server then we "switch" to the same server. We also
247 247 * switch if the handshake has reached the attribute negotiate step whatever
248 248 * the number of handshake retries might be.
249 249 */
250 250 static uint_t vdc_hshake_retries = VDC_HSHAKE_RETRIES;
251 251
252 252 /*
253 253 * If the handshake done during the attach fails then the two following
254 254 * variables will also be used to control the number of retries for the
255 255 * next handshakes. In that case, when a handshake is done after the
256 256 * attach (i.e. the vdc lifecycle is VDC_ONLINE_PENDING) then the handshake
257 257 * will be retried until we have done an attribution negotiation with each
258 258 * server, with a specified minimum total number of negotations (the value
259 259 * of the vdc_hattr_min_initial or vdc_hattr_min variable).
260 260 *
261 261 * This prevents new I/Os on a newly used vdisk to block forever if the
262 262 * attribute negotiations can not be done, and to limit the amount of time
263 263 * before I/Os will fail. Basically, attribute negotiations will fail when
264 264 * the service is up but the backend does not exist. In that case, vds will
265 265 * typically retry to access the backend during 50 seconds. So I/Os will fail
266 266 * after the following amount of time:
267 267 *
268 268 * 50 seconds x max(number of servers, vdc->hattr_min)
269 269 *
270 270 * After that the handshake done during the attach has failed then the next
271 271 * handshake will use vdc_attr_min_initial. This handshake will correspond to
272 272 * the very first I/O to the device. If this handshake also fails then
273 273 * vdc_hattr_min will be used for subsequent handshakes. We typically allow
274 274 * more retries for the first handshake (VDC_HATTR_MIN_INITIAL = 3) to give more
275 275 * time for the backend to become available (50s x VDC_HATTR_MIN_INITIAL = 150s)
276 276 * in case this is a critical vdisk (e.g. vdisk access during boot). Then we use
277 277 * a smaller value (VDC_HATTR_MIN = 1) to avoid waiting too long for each I/O.
278 278 */
279 279 static uint_t vdc_hattr_min_initial = VDC_HATTR_MIN_INITIAL;
280 280 static uint_t vdc_hattr_min = VDC_HATTR_MIN;
281 281
282 282 /*
283 283 * Tunable variables to control how long vdc waits before timing out on
284 284 * various operations
285 285 */
286 286 static int vdc_timeout = 0; /* units: seconds */
287 287 static int vdc_ldcup_timeout = 1; /* units: seconds */
288 288
289 289 static uint64_t vdc_hz_min_ldc_delay;
290 290 static uint64_t vdc_min_timeout_ldc = 1 * MILLISEC;
291 291 static uint64_t vdc_hz_max_ldc_delay;
292 292 static uint64_t vdc_max_timeout_ldc = 100 * MILLISEC;
293 293
294 294 static uint64_t vdc_ldc_read_init_delay = 1 * MILLISEC;
295 295 static uint64_t vdc_ldc_read_max_delay = 100 * MILLISEC;
296 296
297 297 /* values for dumping - need to run in a tighter loop */
298 298 static uint64_t vdc_usec_timeout_dump = 100 * MILLISEC; /* 0.1s units: ns */
299 299 static int vdc_dump_retries = 100;
300 300
301 301 static uint16_t vdc_scsi_timeout = 60; /* 60s units: seconds */
302 302
303 303 static uint64_t vdc_ownership_delay = 6 * MICROSEC; /* 6s units: usec */
304 304
305 305 /* Count of the number of vdc instances attached */
306 306 static volatile uint32_t vdc_instance_count = 0;
307 307
308 308 /* Tunable to log all SCSI errors */
309 309 static boolean_t vdc_scsi_log_error = B_FALSE;
310 310
311 311 /* Soft state pointer */
312 312 static void *vdc_state;
313 313
314 314 /*
315 315 * Controlling the verbosity of the error/debug messages
316 316 *
317 317 * vdc_msglevel - controls level of messages
318 318 * vdc_matchinst - 64-bit variable where each bit corresponds
319 319 * to the vdc instance the vdc_msglevel applies.
320 320 */
321 321 int vdc_msglevel = 0x0;
322 322 uint64_t vdc_matchinst = 0ull;
323 323
324 324 /*
325 325 * Supported vDisk protocol version pairs.
326 326 *
327 327 * The first array entry is the latest and preferred version.
328 328 */
329 329 static const vio_ver_t vdc_version[] = {{1, 1}};
330 330
331 331 static struct cb_ops vdc_cb_ops = {
332 332 vdc_open, /* cb_open */
333 333 vdc_close, /* cb_close */
334 334 vdc_strategy, /* cb_strategy */
335 335 vdc_print, /* cb_print */
336 336 vdc_dump, /* cb_dump */
337 337 vdc_read, /* cb_read */
338 338 vdc_write, /* cb_write */
339 339 vdc_ioctl, /* cb_ioctl */
340 340 nodev, /* cb_devmap */
341 341 nodev, /* cb_mmap */
342 342 nodev, /* cb_segmap */
343 343 nochpoll, /* cb_chpoll */
344 344 vdc_prop_op, /* cb_prop_op */
345 345 NULL, /* cb_str */
346 346 D_MP | D_64BIT, /* cb_flag */
347 347 CB_REV, /* cb_rev */
348 348 vdc_aread, /* cb_aread */
349 349 vdc_awrite /* cb_awrite */
350 350 };
351 351
352 352 static struct dev_ops vdc_ops = {
353 353 DEVO_REV, /* devo_rev */
354 354 0, /* devo_refcnt */
355 355 vdc_getinfo, /* devo_getinfo */
356 356 nulldev, /* devo_identify */
357 357 nulldev, /* devo_probe */
358 358 vdc_attach, /* devo_attach */
359 359 vdc_detach, /* devo_detach */
360 360 nodev, /* devo_reset */
361 361 &vdc_cb_ops, /* devo_cb_ops */
362 362 NULL, /* devo_bus_ops */
363 363 nulldev, /* devo_power */
364 364 ddi_quiesce_not_needed, /* devo_quiesce */
365 365 };
366 366
367 367 static struct modldrv modldrv = {
368 368 &mod_driverops,
369 369 "virtual disk client",
370 370 &vdc_ops,
371 371 };
372 372
373 373 static struct modlinkage modlinkage = {
374 374 MODREV_1,
375 375 &modldrv,
376 376 NULL
377 377 };
378 378
379 379 /* -------------------------------------------------------------------------- */
380 380
381 381 /*
382 382 * Device Driver housekeeping and setup
383 383 */
384 384
385 385 int
386 386 _init(void)
387 387 {
388 388 int status;
389 389
390 390 if ((status = ddi_soft_state_init(&vdc_state, sizeof (vdc_t), 1)) != 0)
391 391 return (status);
392 392 if ((status = mod_install(&modlinkage)) != 0)
393 393 ddi_soft_state_fini(&vdc_state);
394 394 return (status);
395 395 }
396 396
397 397 int
398 398 _info(struct modinfo *modinfop)
399 399 {
400 400 return (mod_info(&modlinkage, modinfop));
401 401 }
402 402
403 403 int
404 404 _fini(void)
405 405 {
406 406 int status;
407 407
408 408 if ((status = mod_remove(&modlinkage)) != 0)
409 409 return (status);
410 410 ddi_soft_state_fini(&vdc_state);
411 411 return (0);
412 412 }
413 413
414 414 static int
415 415 vdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resultp)
416 416 {
417 417 _NOTE(ARGUNUSED(dip))
418 418
419 419 int instance = VDCUNIT((dev_t)arg);
420 420 vdc_t *vdc = NULL;
421 421
422 422 switch (cmd) {
423 423 case DDI_INFO_DEVT2DEVINFO:
424 424 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
425 425 *resultp = NULL;
426 426 return (DDI_FAILURE);
427 427 }
428 428 *resultp = vdc->dip;
429 429 return (DDI_SUCCESS);
430 430 case DDI_INFO_DEVT2INSTANCE:
431 431 *resultp = (void *)(uintptr_t)instance;
432 432 return (DDI_SUCCESS);
433 433 default:
434 434 *resultp = NULL;
435 435 return (DDI_FAILURE);
436 436 }
437 437 }
438 438
439 439 static int
440 440 vdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
441 441 {
442 442 kt_did_t eio_tid, ownership_tid;
443 443 int instance;
444 444 int rv;
445 445 vdc_server_t *srvr;
446 446 vdc_t *vdc = NULL;
447 447
448 448 switch (cmd) {
449 449 case DDI_DETACH:
450 450 /* the real work happens below */
451 451 break;
452 452 case DDI_SUSPEND:
453 453 /* nothing to do for this non-device */
454 454 return (DDI_SUCCESS);
455 455 default:
456 456 return (DDI_FAILURE);
457 457 }
458 458
459 459 ASSERT(cmd == DDI_DETACH);
460 460 instance = ddi_get_instance(dip);
461 461 DMSGX(1, "[%d] Entered\n", instance);
462 462
463 463 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
464 464 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
465 465 return (DDI_FAILURE);
466 466 }
467 467
468 468 if (vdc_is_opened(vdc)) {
469 469 DMSG(vdc, 0, "[%d] Cannot detach: device is open", instance);
470 470 return (DDI_FAILURE);
471 471 }
472 472
473 473 if (vdc->dkio_flush_pending) {
474 474 DMSG(vdc, 0,
475 475 "[%d] Cannot detach: %d outstanding DKIO flushes\n",
476 476 instance, vdc->dkio_flush_pending);
477 477 return (DDI_FAILURE);
478 478 }
479 479
480 480 if (vdc->validate_pending) {
481 481 DMSG(vdc, 0,
482 482 "[%d] Cannot detach: %d outstanding validate request\n",
483 483 instance, vdc->validate_pending);
484 484 return (DDI_FAILURE);
485 485 }
486 486
487 487 DMSG(vdc, 0, "[%d] proceeding...\n", instance);
488 488
489 489 /* If we took ownership, release ownership */
490 490 mutex_enter(&vdc->ownership_lock);
491 491 if (vdc->ownership & VDC_OWNERSHIP_GRANTED) {
492 492 rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR);
493 493 if (rv == 0) {
494 494 vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
495 495 }
496 496 }
497 497 mutex_exit(&vdc->ownership_lock);
498 498
499 499 /* mark instance as detaching */
500 500 mutex_enter(&vdc->lock);
501 501 vdc->lifecycle = VDC_LC_DETACHING;
502 502 mutex_exit(&vdc->lock);
503 503
504 504 /*
505 505 * Try and disable callbacks to prevent another handshake. We have to
506 506 * disable callbacks for all servers.
507 507 */
508 508 for (srvr = vdc->server_list; srvr != NULL; srvr = srvr->next) {
509 509 rv = ldc_set_cb_mode(srvr->ldc_handle, LDC_CB_DISABLE);
510 510 DMSG(vdc, 0, "callback disabled (ldc=%lu, rv=%d)\n",
511 511 srvr->ldc_id, rv);
512 512 }
513 513
514 514 if (vdc->initialized & VDC_THREAD) {
515 515 mutex_enter(&vdc->read_lock);
516 516 if ((vdc->read_state == VDC_READ_WAITING) ||
517 517 (vdc->read_state == VDC_READ_RESET)) {
518 518 vdc->read_state = VDC_READ_RESET;
519 519 cv_signal(&vdc->read_cv);
520 520 }
521 521
522 522 mutex_exit(&vdc->read_lock);
523 523
524 524 /* wake up any thread waiting for connection to come online */
525 525 mutex_enter(&vdc->lock);
526 526 if (vdc->state == VDC_STATE_INIT_WAITING) {
527 527 DMSG(vdc, 0,
528 528 "[%d] write reset - move to resetting state...\n",
529 529 instance);
530 530 vdc->state = VDC_STATE_RESETTING;
531 531 cv_signal(&vdc->initwait_cv);
532 532 } else if (vdc->state == VDC_STATE_FAILED) {
533 533 vdc->io_pending = B_TRUE;
534 534 cv_signal(&vdc->io_pending_cv);
535 535 }
536 536 mutex_exit(&vdc->lock);
537 537
538 538 /* now wait until state transitions to VDC_STATE_DETACH */
539 539 thread_join(vdc->msg_proc_thr->t_did);
540 540 ASSERT(vdc->state == VDC_STATE_DETACH);
541 541 DMSG(vdc, 0, "[%d] Reset thread exit and join ..\n",
542 542 vdc->instance);
543 543 }
544 544
545 545 mutex_enter(&vdc->lock);
546 546
547 547 if (vdc->initialized & VDC_DRING)
548 548 vdc_destroy_descriptor_ring(vdc);
549 549
550 550 vdc_fini_ports(vdc);
551 551
552 552 if (vdc->eio_thread) {
553 553 eio_tid = vdc->eio_thread->t_did;
554 554 vdc->failfast_interval = 0;
555 555 ASSERT(vdc->num_servers == 0);
556 556 cv_signal(&vdc->eio_cv);
557 557 } else {
558 558 eio_tid = 0;
559 559 }
560 560
561 561 if (vdc->ownership & VDC_OWNERSHIP_WANTED) {
562 562 ownership_tid = vdc->ownership_thread->t_did;
563 563 vdc->ownership = VDC_OWNERSHIP_NONE;
564 564 cv_signal(&vdc->ownership_cv);
565 565 } else {
566 566 ownership_tid = 0;
567 567 }
568 568
569 569 mutex_exit(&vdc->lock);
570 570
571 571 if (eio_tid != 0)
572 572 thread_join(eio_tid);
573 573
574 574 if (ownership_tid != 0)
575 575 thread_join(ownership_tid);
576 576
577 577 if (vdc->initialized & VDC_MINOR)
578 578 ddi_remove_minor_node(dip, NULL);
579 579
580 580 if (vdc->io_stats) {
581 581 kstat_delete(vdc->io_stats);
582 582 vdc->io_stats = NULL;
583 583 }
584 584
585 585 if (vdc->err_stats) {
586 586 kstat_delete(vdc->err_stats);
587 587 vdc->err_stats = NULL;
588 588 }
589 589
590 590 if (vdc->initialized & VDC_LOCKS) {
591 591 mutex_destroy(&vdc->lock);
592 592 mutex_destroy(&vdc->read_lock);
593 593 mutex_destroy(&vdc->ownership_lock);
594 594 cv_destroy(&vdc->initwait_cv);
595 595 cv_destroy(&vdc->dring_free_cv);
596 596 cv_destroy(&vdc->membind_cv);
597 597 cv_destroy(&vdc->sync_blocked_cv);
598 598 cv_destroy(&vdc->read_cv);
599 599 cv_destroy(&vdc->running_cv);
600 600 cv_destroy(&vdc->io_pending_cv);
601 601 cv_destroy(&vdc->ownership_cv);
602 602 cv_destroy(&vdc->eio_cv);
603 603 }
604 604
605 605 if (vdc->minfo)
606 606 kmem_free(vdc->minfo, sizeof (struct dk_minfo));
607 607
608 608 if (vdc->cinfo)
609 609 kmem_free(vdc->cinfo, sizeof (struct dk_cinfo));
610 610
611 611 if (vdc->vtoc)
612 612 kmem_free(vdc->vtoc, sizeof (struct extvtoc));
613 613
614 614 if (vdc->geom)
615 615 kmem_free(vdc->geom, sizeof (struct dk_geom));
616 616
617 617 if (vdc->devid) {
618 618 ddi_devid_unregister(dip);
619 619 ddi_devid_free(vdc->devid);
620 620 }
621 621
622 622 if (vdc->initialized & VDC_SOFT_STATE)
623 623 ddi_soft_state_free(vdc_state, instance);
624 624
625 625 DMSG(vdc, 0, "[%d] End %p\n", instance, (void *)vdc);
626 626
627 627 return (DDI_SUCCESS);
628 628 }
629 629
630 630
631 631 static int
632 632 vdc_do_attach(dev_info_t *dip)
633 633 {
634 634 int instance;
635 635 vdc_t *vdc = NULL;
636 636 int status;
637 637 md_t *mdp;
638 638 mde_cookie_t vd_node;
639 639
640 640 ASSERT(dip != NULL);
641 641
642 642 instance = ddi_get_instance(dip);
643 643 if (ddi_soft_state_zalloc(vdc_state, instance) != DDI_SUCCESS) {
644 644 cmn_err(CE_NOTE, "[%d] Couldn't alloc state structure",
645 645 instance);
646 646 return (DDI_FAILURE);
647 647 }
648 648
649 649 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
650 650 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
651 651 return (DDI_FAILURE);
652 652 }
653 653
654 654 /*
655 655 * We assign the value to initialized in this case to zero out the
656 656 * variable and then set bits in it to indicate what has been done
657 657 */
658 658 vdc->initialized = VDC_SOFT_STATE;
659 659
660 660 vdc_hz_min_ldc_delay = drv_usectohz(vdc_min_timeout_ldc);
661 661 vdc_hz_max_ldc_delay = drv_usectohz(vdc_max_timeout_ldc);
662 662
663 663 vdc->dip = dip;
664 664 vdc->instance = instance;
665 665 vdc->vdisk_type = VD_DISK_TYPE_UNK;
666 666 vdc->vdisk_label = VD_DISK_LABEL_UNK;
667 667 vdc->state = VDC_STATE_INIT;
668 668 vdc->lifecycle = VDC_LC_ATTACHING;
669 669 vdc->session_id = 0;
670 670 vdc->vdisk_bsize = DEV_BSIZE;
671 671 vdc->vio_bmask = 0;
672 672 vdc->vio_bshift = 0;
673 673 vdc->max_xfer_sz = maxphys / vdc->vdisk_bsize;
674 674
675 675 /*
676 676 * We assume, for now, that the vDisk server will export 'read'
677 677 * operations to us at a minimum (this is needed because of checks
678 678 * in vdc for supported operations early in the handshake process).
679 679 * The vDisk server will return ENOTSUP if this is not the case.
680 680 * The value will be overwritten during the attribute exchange with
681 681 * the bitmask of operations exported by server.
682 682 */
683 683 vdc->operations = VD_OP_MASK_READ;
684 684
685 685 vdc->vtoc = NULL;
686 686 vdc->geom = NULL;
687 687 vdc->cinfo = NULL;
688 688 vdc->minfo = NULL;
689 689
690 690 mutex_init(&vdc->lock, NULL, MUTEX_DRIVER, NULL);
691 691 cv_init(&vdc->initwait_cv, NULL, CV_DRIVER, NULL);
692 692 cv_init(&vdc->dring_free_cv, NULL, CV_DRIVER, NULL);
693 693 cv_init(&vdc->membind_cv, NULL, CV_DRIVER, NULL);
694 694 cv_init(&vdc->running_cv, NULL, CV_DRIVER, NULL);
695 695 cv_init(&vdc->io_pending_cv, NULL, CV_DRIVER, NULL);
696 696
697 697 vdc->io_pending = B_FALSE;
698 698 vdc->threads_pending = 0;
699 699 vdc->sync_op_blocked = B_FALSE;
700 700 cv_init(&vdc->sync_blocked_cv, NULL, CV_DRIVER, NULL);
701 701
702 702 mutex_init(&vdc->ownership_lock, NULL, MUTEX_DRIVER, NULL);
703 703 cv_init(&vdc->ownership_cv, NULL, CV_DRIVER, NULL);
704 704 cv_init(&vdc->eio_cv, NULL, CV_DRIVER, NULL);
705 705
706 706 /* init blocking msg read functionality */
707 707 mutex_init(&vdc->read_lock, NULL, MUTEX_DRIVER, NULL);
708 708 cv_init(&vdc->read_cv, NULL, CV_DRIVER, NULL);
709 709 vdc->read_state = VDC_READ_IDLE;
710 710
711 711 vdc->initialized |= VDC_LOCKS;
712 712
713 713 /* get device and port MD node for this disk instance */
714 714 if (vdc_get_md_node(dip, &mdp, &vd_node) != 0) {
715 715 cmn_err(CE_NOTE, "[%d] Could not get machine description node",
716 716 instance);
717 717 return (DDI_FAILURE);
718 718 }
719 719
720 720 if (vdc_init_ports(vdc, mdp, vd_node) != 0) {
721 721 cmn_err(CE_NOTE, "[%d] Error initialising ports", instance);
722 722 return (DDI_FAILURE);
723 723 }
724 724
725 725 (void) md_fini_handle(mdp);
726 726
727 727 /* Create the kstats for saving the I/O statistics used by iostat(1M) */
728 728 vdc_create_io_kstats(vdc);
729 729 vdc_create_err_kstats(vdc);
730 730
731 731 /* Initialize remaining structures before starting the msg thread */
732 732 vdc->vdisk_label = VD_DISK_LABEL_UNK;
733 733 vdc->vtoc = kmem_zalloc(sizeof (struct extvtoc), KM_SLEEP);
734 734 vdc->geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
735 735 vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
736 736
737 737 /* initialize the thread responsible for managing state with server */
738 738 vdc->msg_proc_thr = thread_create(NULL, 0, vdc_process_msg_thread,
739 739 vdc, 0, &p0, TS_RUN, minclsyspri);
740 740 if (vdc->msg_proc_thr == NULL) {
741 741 cmn_err(CE_NOTE, "[%d] Failed to create msg processing thread",
742 742 instance);
743 743 return (DDI_FAILURE);
744 744 }
745 745
746 746 /*
747 747 * If there are multiple servers then start the eio thread.
748 748 */
749 749 if (vdc->num_servers > 1) {
750 750 vdc->eio_thread = thread_create(NULL, 0, vdc_eio_thread, vdc, 0,
751 751 &p0, TS_RUN, v.v_maxsyspri - 2);
752 752 if (vdc->eio_thread == NULL) {
753 753 cmn_err(CE_NOTE, "[%d] Failed to create error "
754 754 "I/O thread", instance);
755 755 return (DDI_FAILURE);
756 756 }
757 757 }
758 758
759 759 vdc->initialized |= VDC_THREAD;
760 760
761 761 atomic_inc_32(&vdc_instance_count);
762 762
763 763 /*
764 764 * Check the disk label. This will send requests and do the handshake.
765 765 * We don't really care about the disk label now. What we really need is
766 766 * the handshake do be done so that we know the type of the disk (slice
767 767 * or full disk) and the appropriate device nodes can be created.
768 768 */
769 769
770 770 mutex_enter(&vdc->lock);
771 771 (void) vdc_validate_geometry(vdc);
772 772 mutex_exit(&vdc->lock);
773 773
774 774 /*
775 775 * Now that we have the device info we can create the device nodes
776 776 */
777 777 status = vdc_create_device_nodes(vdc);
778 778 if (status) {
779 779 DMSG(vdc, 0, "[%d] Failed to create device nodes",
780 780 instance);
781 781 goto return_status;
782 782 }
783 783
784 784 /*
785 785 * Fill in the fields of the error statistics kstat that were not
786 786 * available when creating the kstat
787 787 */
788 788 vdc_set_err_kstats(vdc);
789 789 ddi_report_dev(dip);
790 790 ASSERT(vdc->lifecycle == VDC_LC_ONLINE ||
791 791 vdc->lifecycle == VDC_LC_ONLINE_PENDING);
792 792 DMSG(vdc, 0, "[%d] Attach tasks successful\n", instance);
793 793
794 794 return_status:
795 795 DMSG(vdc, 0, "[%d] Attach completed\n", instance);
796 796 return (status);
797 797 }
798 798
799 799 static int
800 800 vdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
801 801 {
802 802 int status;
803 803
804 804 switch (cmd) {
805 805 case DDI_ATTACH:
806 806 if ((status = vdc_do_attach(dip)) != 0)
807 807 (void) vdc_detach(dip, DDI_DETACH);
808 808 return (status);
809 809 case DDI_RESUME:
810 810 /* nothing to do for this non-device */
811 811 return (DDI_SUCCESS);
812 812 default:
813 813 return (DDI_FAILURE);
814 814 }
815 815 }
816 816
817 817 static int
818 818 vdc_do_ldc_init(vdc_t *vdc, vdc_server_t *srvr)
819 819 {
820 820 int status = 0;
821 821 ldc_status_t ldc_state;
822 822 ldc_attr_t ldc_attr;
823 823
824 824 ASSERT(vdc != NULL);
825 825 ASSERT(srvr != NULL);
826 826
827 827 ldc_attr.devclass = LDC_DEV_BLK;
828 828 ldc_attr.instance = vdc->instance;
829 829 ldc_attr.mode = LDC_MODE_UNRELIABLE; /* unreliable transport */
830 830 ldc_attr.mtu = VD_LDC_MTU;
831 831
832 832 if ((srvr->state & VDC_LDC_INIT) == 0) {
833 833 status = ldc_init(srvr->ldc_id, &ldc_attr,
834 834 &srvr->ldc_handle);
835 835 if (status != 0) {
836 836 DMSG(vdc, 0, "[%d] ldc_init(chan %ld) returned %d",
837 837 vdc->instance, srvr->ldc_id, status);
838 838 return (status);
839 839 }
840 840 srvr->state |= VDC_LDC_INIT;
841 841 }
842 842 status = ldc_status(srvr->ldc_handle, &ldc_state);
843 843 if (status != 0) {
844 844 DMSG(vdc, 0, "[%d] Cannot discover LDC status [err=%d]",
845 845 vdc->instance, status);
846 846 goto init_exit;
847 847 }
848 848 srvr->ldc_state = ldc_state;
849 849
850 850 if ((srvr->state & VDC_LDC_CB) == 0) {
851 851 status = ldc_reg_callback(srvr->ldc_handle, vdc_handle_cb,
852 852 (caddr_t)srvr);
853 853 if (status != 0) {
854 854 DMSG(vdc, 0, "[%d] LDC callback reg. failed (%d)",
855 855 vdc->instance, status);
856 856 goto init_exit;
857 857 }
858 858 srvr->state |= VDC_LDC_CB;
859 859 }
860 860
861 861 /*
862 862 * At this stage we have initialised LDC, we will now try and open
863 863 * the connection.
864 864 */
865 865 if (srvr->ldc_state == LDC_INIT) {
866 866 status = ldc_open(srvr->ldc_handle);
867 867 if (status != 0) {
868 868 DMSG(vdc, 0, "[%d] ldc_open(chan %ld) returned %d",
869 869 vdc->instance, srvr->ldc_id, status);
870 870 goto init_exit;
871 871 }
872 872 srvr->state |= VDC_LDC_OPEN;
873 873 }
874 874
875 875 init_exit:
876 876 if (status) {
877 877 vdc_terminate_ldc(vdc, srvr);
878 878 }
879 879
880 880 return (status);
881 881 }
882 882
883 883 static int
884 884 vdc_start_ldc_connection(vdc_t *vdc)
885 885 {
886 886 int status = 0;
887 887
888 888 ASSERT(vdc != NULL);
889 889
890 890 ASSERT(MUTEX_HELD(&vdc->lock));
891 891
892 892 status = vdc_do_ldc_up(vdc);
893 893
894 894 DMSG(vdc, 0, "[%d] Finished bringing up LDC\n", vdc->instance);
895 895
896 896 return (status);
897 897 }
898 898
899 899 static int
900 900 vdc_stop_ldc_connection(vdc_t *vdcp)
901 901 {
902 902 int status;
903 903
904 904 ASSERT(vdcp != NULL);
905 905
906 906 ASSERT(MUTEX_HELD(&vdcp->lock));
907 907
908 908 DMSG(vdcp, 0, ": Resetting connection to vDisk server : state %d\n",
909 909 vdcp->state);
910 910
911 911 status = ldc_down(vdcp->curr_server->ldc_handle);
912 912 DMSG(vdcp, 0, "ldc_down() = %d\n", status);
913 913
914 914 vdcp->initialized &= ~VDC_HANDSHAKE;
915 915 DMSG(vdcp, 0, "initialized=%x\n", vdcp->initialized);
916 916
917 917 return (status);
918 918 }
919 919
920 920 static void
921 921 vdc_create_io_kstats(vdc_t *vdc)
922 922 {
923 923 if (vdc->io_stats != NULL) {
924 924 DMSG(vdc, 0, "[%d] I/O kstat already exists\n", vdc->instance);
925 925 return;
926 926 }
927 927
928 928 vdc->io_stats = kstat_create(VDC_DRIVER_NAME, vdc->instance, NULL,
929 929 "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
930 930 if (vdc->io_stats != NULL) {
931 931 vdc->io_stats->ks_lock = &vdc->lock;
932 932 kstat_install(vdc->io_stats);
933 933 } else {
934 934 cmn_err(CE_NOTE, "[%d] Failed to create kstat: I/O statistics"
935 935 " will not be gathered", vdc->instance);
936 936 }
937 937 }
938 938
939 939 static void
940 940 vdc_create_err_kstats(vdc_t *vdc)
941 941 {
942 942 vd_err_stats_t *stp;
943 943 char kstatmodule_err[KSTAT_STRLEN];
944 944 char kstatname[KSTAT_STRLEN];
945 945 int ndata = (sizeof (vd_err_stats_t) / sizeof (kstat_named_t));
946 946 int instance = vdc->instance;
947 947
948 948 if (vdc->err_stats != NULL) {
949 949 DMSG(vdc, 0, "[%d] ERR kstat already exists\n", vdc->instance);
950 950 return;
951 951 }
952 952
953 953 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
954 954 "%serr", VDC_DRIVER_NAME);
955 955 (void) snprintf(kstatname, sizeof (kstatname),
956 956 "%s%d,err", VDC_DRIVER_NAME, instance);
957 957
958 958 vdc->err_stats = kstat_create(kstatmodule_err, instance, kstatname,
959 959 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
960 960
961 961 if (vdc->err_stats == NULL) {
962 962 cmn_err(CE_NOTE, "[%d] Failed to create kstat: Error statistics"
963 963 " will not be gathered", instance);
964 964 return;
965 965 }
966 966
967 967 stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
968 968 kstat_named_init(&stp->vd_softerrs, "Soft Errors",
969 969 KSTAT_DATA_UINT32);
970 970 kstat_named_init(&stp->vd_transerrs, "Transport Errors",
971 971 KSTAT_DATA_UINT32);
972 972 kstat_named_init(&stp->vd_protoerrs, "Protocol Errors",
973 973 KSTAT_DATA_UINT32);
974 974 kstat_named_init(&stp->vd_vid, "Vendor",
975 975 KSTAT_DATA_CHAR);
976 976 kstat_named_init(&stp->vd_pid, "Product",
977 977 KSTAT_DATA_CHAR);
978 978 kstat_named_init(&stp->vd_capacity, "Size",
979 979 KSTAT_DATA_ULONGLONG);
980 980
981 981 vdc->err_stats->ks_update = nulldev;
982 982
983 983 kstat_install(vdc->err_stats);
984 984 }
985 985
986 986 static void
987 987 vdc_set_err_kstats(vdc_t *vdc)
988 988 {
989 989 vd_err_stats_t *stp;
990 990
991 991 if (vdc->err_stats == NULL)
992 992 return;
993 993
994 994 mutex_enter(&vdc->lock);
995 995
996 996 stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
997 997 ASSERT(stp != NULL);
998 998
999 999 stp->vd_capacity.value.ui64 = vdc->vdisk_size * vdc->vdisk_bsize;
1000 1000 (void) strcpy(stp->vd_vid.value.c, "SUN");
1001 1001 (void) strcpy(stp->vd_pid.value.c, "VDSK");
1002 1002
1003 1003 mutex_exit(&vdc->lock);
1004 1004 }
1005 1005
1006 1006 static int
1007 1007 vdc_create_device_nodes_efi(vdc_t *vdc)
1008 1008 {
1009 1009 ddi_remove_minor_node(vdc->dip, "h");
1010 1010 ddi_remove_minor_node(vdc->dip, "h,raw");
1011 1011
1012 1012 if (ddi_create_minor_node(vdc->dip, "wd", S_IFBLK,
1013 1013 VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
1014 1014 DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
1015 1015 cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd'",
1016 1016 vdc->instance);
1017 1017 return (EIO);
1018 1018 }
1019 1019
1020 1020 /* if any device node is created we set this flag */
1021 1021 vdc->initialized |= VDC_MINOR;
1022 1022
1023 1023 if (ddi_create_minor_node(vdc->dip, "wd,raw", S_IFCHR,
1024 1024 VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
1025 1025 DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
1026 1026 cmn_err(CE_NOTE, "[%d] Couldn't add block node 'wd,raw'",
1027 1027 vdc->instance);
1028 1028 return (EIO);
1029 1029 }
1030 1030
1031 1031 return (0);
1032 1032 }
1033 1033
1034 1034 static int
1035 1035 vdc_create_device_nodes_vtoc(vdc_t *vdc)
1036 1036 {
1037 1037 ddi_remove_minor_node(vdc->dip, "wd");
1038 1038 ddi_remove_minor_node(vdc->dip, "wd,raw");
1039 1039
1040 1040 if (ddi_create_minor_node(vdc->dip, "h", S_IFBLK,
1041 1041 VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
1042 1042 DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
1043 1043 cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h'",
1044 1044 vdc->instance);
1045 1045 return (EIO);
1046 1046 }
1047 1047
1048 1048 /* if any device node is created we set this flag */
1049 1049 vdc->initialized |= VDC_MINOR;
1050 1050
1051 1051 if (ddi_create_minor_node(vdc->dip, "h,raw", S_IFCHR,
1052 1052 VD_MAKE_DEV(vdc->instance, VD_EFI_WD_SLICE),
1053 1053 DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
1054 1054 cmn_err(CE_NOTE, "[%d] Couldn't add block node 'h,raw'",
1055 1055 vdc->instance);
1056 1056 return (EIO);
1057 1057 }
1058 1058
1059 1059 return (0);
1060 1060 }
1061 1061
1062 1062 /*
1063 1063 * Function:
1064 1064 * vdc_create_device_nodes
1065 1065 *
1066 1066 * Description:
1067 1067 * This function creates the block and character device nodes under
1068 1068 * /devices. It is called as part of the attach(9E) of the instance
1069 1069 * during the handshake with vds after vds has sent the attributes
1070 1070 * to vdc.
1071 1071 *
1072 1072 * If the device is of type VD_DISK_TYPE_SLICE then the minor node
1073 1073 * of 2 is used in keeping with the Solaris convention that slice 2
1074 1074 * refers to a whole disk. Slices start at 'a'
1075 1075 *
1076 1076 * Parameters:
1077 1077 * vdc - soft state pointer
1078 1078 *
1079 1079 * Return Values
1080 1080 * 0 - Success
1081 1081 * EIO - Failed to create node
1082 1082 */
1083 1083 static int
1084 1084 vdc_create_device_nodes(vdc_t *vdc)
1085 1085 {
1086 1086 char name[sizeof ("s,raw")];
1087 1087 dev_info_t *dip = NULL;
1088 1088 int instance, status;
1089 1089 int num_slices = 1;
1090 1090 int i;
1091 1091
1092 1092 ASSERT(vdc != NULL);
1093 1093
1094 1094 instance = vdc->instance;
1095 1095 dip = vdc->dip;
1096 1096
1097 1097 switch (vdc->vdisk_type) {
1098 1098 case VD_DISK_TYPE_DISK:
1099 1099 case VD_DISK_TYPE_UNK:
1100 1100 num_slices = V_NUMPAR;
1101 1101 break;
1102 1102 case VD_DISK_TYPE_SLICE:
1103 1103 num_slices = 1;
1104 1104 break;
1105 1105 default:
1106 1106 ASSERT(0);
1107 1107 }
1108 1108
1109 1109 /*
1110 1110 * Minor nodes are different for EFI disks: EFI disks do not have
1111 1111 * a minor node 'g' for the minor number corresponding to slice
1112 1112 * VD_EFI_WD_SLICE (slice 7) instead they have a minor node 'wd'
1113 1113 * representing the whole disk.
1114 1114 */
1115 1115 for (i = 0; i < num_slices; i++) {
1116 1116
1117 1117 if (i == VD_EFI_WD_SLICE) {
1118 1118 if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
1119 1119 status = vdc_create_device_nodes_efi(vdc);
1120 1120 else
1121 1121 status = vdc_create_device_nodes_vtoc(vdc);
1122 1122 if (status != 0)
1123 1123 return (status);
1124 1124 continue;
1125 1125 }
1126 1126
1127 1127 (void) snprintf(name, sizeof (name), "%c", 'a' + i);
1128 1128 if (ddi_create_minor_node(dip, name, S_IFBLK,
1129 1129 VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
1130 1130 cmn_err(CE_NOTE, "[%d] Couldn't add block node '%s'",
1131 1131 instance, name);
1132 1132 return (EIO);
1133 1133 }
1134 1134
1135 1135 /* if any device node is created we set this flag */
1136 1136 vdc->initialized |= VDC_MINOR;
1137 1137
1138 1138 (void) snprintf(name, sizeof (name), "%c%s", 'a' + i, ",raw");
1139 1139
1140 1140 if (ddi_create_minor_node(dip, name, S_IFCHR,
1141 1141 VD_MAKE_DEV(instance, i), DDI_NT_BLOCK, 0) != DDI_SUCCESS) {
1142 1142 cmn_err(CE_NOTE, "[%d] Couldn't add raw node '%s'",
1143 1143 instance, name);
1144 1144 return (EIO);
1145 1145 }
1146 1146 }
1147 1147
1148 1148 return (0);
1149 1149 }
1150 1150
1151 1151 /*
1152 1152 * Driver prop_op(9e) entry point function. Return the number of blocks for
1153 1153 * the partition in question or forward the request to the property facilities.
1154 1154 */
1155 1155 static int
1156 1156 vdc_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
1157 1157 char *name, caddr_t valuep, int *lengthp)
1158 1158 {
1159 1159 int instance = ddi_get_instance(dip);
1160 1160 vdc_t *vdc;
1161 1161 uint64_t nblocks;
1162 1162 uint_t blksize;
1163 1163
1164 1164 vdc = ddi_get_soft_state(vdc_state, instance);
1165 1165
1166 1166 if (dev == DDI_DEV_T_ANY || vdc == NULL) {
1167 1167 return (ddi_prop_op(dev, dip, prop_op, mod_flags,
1168 1168 name, valuep, lengthp));
1169 1169 }
1170 1170
1171 1171 mutex_enter(&vdc->lock);
1172 1172 (void) vdc_validate_geometry(vdc);
1173 1173 if (vdc->vdisk_label == VD_DISK_LABEL_UNK) {
1174 1174 mutex_exit(&vdc->lock);
1175 1175 return (ddi_prop_op(dev, dip, prop_op, mod_flags,
1176 1176 name, valuep, lengthp));
1177 1177 }
1178 1178 nblocks = vdc->slice[VDCPART(dev)].nblocks;
1179 1179 blksize = vdc->vdisk_bsize;
1180 1180 mutex_exit(&vdc->lock);
1181 1181
1182 1182 return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op, mod_flags,
1183 1183 name, valuep, lengthp, nblocks, blksize));
1184 1184 }
1185 1185
1186 1186 /*
1187 1187 * Function:
1188 1188 * vdc_is_opened
1189 1189 *
1190 1190 * Description:
1191 1191 * This function checks if any slice of a given virtual disk is
1192 1192 * currently opened.
1193 1193 *
1194 1194 * Parameters:
1195 1195 * vdc - soft state pointer
1196 1196 *
1197 1197 * Return Values
1198 1198 * B_TRUE - at least one slice is opened.
1199 1199 * B_FALSE - no slice is opened.
1200 1200 */
1201 1201 static boolean_t
1202 1202 vdc_is_opened(vdc_t *vdc)
1203 1203 {
1204 1204 int i;
1205 1205
1206 1206 /* check if there's any layered open */
1207 1207 for (i = 0; i < V_NUMPAR; i++) {
1208 1208 if (vdc->open_lyr[i] > 0)
1209 1209 return (B_TRUE);
1210 1210 }
1211 1211
1212 1212 /* check if there is any other kind of open */
1213 1213 for (i = 0; i < OTYPCNT; i++) {
1214 1214 if (vdc->open[i] != 0)
1215 1215 return (B_TRUE);
1216 1216 }
1217 1217
1218 1218 return (B_FALSE);
1219 1219 }
1220 1220
1221 1221 static int
1222 1222 vdc_mark_opened(vdc_t *vdc, int slice, int flag, int otyp)
1223 1223 {
1224 1224 uint8_t slicemask;
1225 1225 int i;
1226 1226
1227 1227 ASSERT(otyp < OTYPCNT);
1228 1228 ASSERT(slice < V_NUMPAR);
1229 1229 ASSERT(MUTEX_HELD(&vdc->lock));
1230 1230
1231 1231 slicemask = 1 << slice;
1232 1232
1233 1233 /*
1234 1234 * If we have a single-slice disk which was unavailable during the
1235 1235 * attach then a device was created for each 8 slices. Now that
1236 1236 * the type is known, we prevent opening any slice other than 0
1237 1237 * even if a device still exists.
1238 1238 */
1239 1239 if (vdc->vdisk_type == VD_DISK_TYPE_SLICE && slice != 0)
1240 1240 return (EIO);
1241 1241
1242 1242 /* check if slice is already exclusively opened */
1243 1243 if (vdc->open_excl & slicemask)
1244 1244 return (EBUSY);
1245 1245
1246 1246 /* if open exclusive, check if slice is already opened */
1247 1247 if (flag & FEXCL) {
1248 1248 if (vdc->open_lyr[slice] > 0)
1249 1249 return (EBUSY);
1250 1250 for (i = 0; i < OTYPCNT; i++) {
1251 1251 if (vdc->open[i] & slicemask)
1252 1252 return (EBUSY);
1253 1253 }
1254 1254 vdc->open_excl |= slicemask;
1255 1255 }
1256 1256
1257 1257 /* mark slice as opened */
1258 1258 if (otyp == OTYP_LYR) {
1259 1259 vdc->open_lyr[slice]++;
1260 1260 } else {
1261 1261 vdc->open[otyp] |= slicemask;
1262 1262 }
1263 1263
1264 1264 return (0);
1265 1265 }
1266 1266
1267 1267 static void
1268 1268 vdc_mark_closed(vdc_t *vdc, int slice, int flag, int otyp)
1269 1269 {
1270 1270 uint8_t slicemask;
1271 1271
1272 1272 ASSERT(otyp < OTYPCNT);
1273 1273 ASSERT(slice < V_NUMPAR);
1274 1274 ASSERT(MUTEX_HELD(&vdc->lock));
1275 1275
1276 1276 slicemask = 1 << slice;
1277 1277
1278 1278 if (otyp == OTYP_LYR) {
1279 1279 ASSERT(vdc->open_lyr[slice] > 0);
1280 1280 vdc->open_lyr[slice]--;
1281 1281 } else {
1282 1282 vdc->open[otyp] &= ~slicemask;
1283 1283 }
1284 1284
1285 1285 if (flag & FEXCL)
1286 1286 vdc->open_excl &= ~slicemask;
1287 1287 }
1288 1288
1289 1289 static int
1290 1290 vdc_open(dev_t *dev, int flag, int otyp, cred_t *cred)
1291 1291 {
1292 1292 _NOTE(ARGUNUSED(cred))
1293 1293
1294 1294 int instance, nodelay;
1295 1295 int slice, status = 0;
1296 1296 vdc_t *vdc;
1297 1297
1298 1298 ASSERT(dev != NULL);
1299 1299 instance = VDCUNIT(*dev);
1300 1300
1301 1301 if (otyp >= OTYPCNT)
1302 1302 return (EINVAL);
1303 1303
1304 1304 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1305 1305 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
1306 1306 return (ENXIO);
1307 1307 }
1308 1308
1309 1309 DMSG(vdc, 0, "minor = %d flag = %x, otyp = %x\n",
1310 1310 getminor(*dev), flag, otyp);
1311 1311
1312 1312 slice = VDCPART(*dev);
1313 1313
1314 1314 nodelay = flag & (FNDELAY | FNONBLOCK);
1315 1315
1316 1316 if ((flag & FWRITE) && (!nodelay) &&
1317 1317 !(VD_OP_SUPPORTED(vdc->operations, VD_OP_BWRITE))) {
1318 1318 return (EROFS);
1319 1319 }
1320 1320
1321 1321 mutex_enter(&vdc->lock);
1322 1322
1323 1323 status = vdc_mark_opened(vdc, slice, flag, otyp);
1324 1324
1325 1325 if (status != 0) {
1326 1326 mutex_exit(&vdc->lock);
1327 1327 return (status);
1328 1328 }
1329 1329
1330 1330 /*
1331 1331 * If the disk type is unknown then we have to wait for the
1332 1332 * handshake to complete because we don't know if the slice
1333 1333 * device we are opening effectively exists.
1334 1334 */
1335 1335 if (vdc->vdisk_type != VD_DISK_TYPE_UNK && nodelay) {
1336 1336
1337 1337 /* don't resubmit a validate request if there's already one */
1338 1338 if (vdc->validate_pending > 0) {
1339 1339 mutex_exit(&vdc->lock);
1340 1340 return (0);
1341 1341 }
1342 1342
1343 1343 /* call vdc_validate() asynchronously to avoid blocking */
1344 1344 if (taskq_dispatch(system_taskq, vdc_validate_task,
1345 1345 (void *)vdc, TQ_NOSLEEP) == NULL) {
1346 1346 vdc_mark_closed(vdc, slice, flag, otyp);
1347 1347 mutex_exit(&vdc->lock);
1348 1348 return (ENXIO);
1349 1349 }
1350 1350
1351 1351 vdc->validate_pending++;
1352 1352 mutex_exit(&vdc->lock);
1353 1353 return (0);
1354 1354 }
1355 1355
1356 1356 mutex_exit(&vdc->lock);
1357 1357
1358 1358 vdc_validate(vdc);
1359 1359
1360 1360 mutex_enter(&vdc->lock);
1361 1361
1362 1362 if (vdc->vdisk_type == VD_DISK_TYPE_UNK ||
1363 1363 (vdc->vdisk_type == VD_DISK_TYPE_SLICE && slice != 0) ||
1364 1364 (!nodelay && (vdc->vdisk_label == VD_DISK_LABEL_UNK ||
1365 1365 vdc->slice[slice].nblocks == 0))) {
1366 1366 vdc_mark_closed(vdc, slice, flag, otyp);
1367 1367 status = EIO;
1368 1368 }
1369 1369
1370 1370 mutex_exit(&vdc->lock);
1371 1371
1372 1372 return (status);
1373 1373 }
1374 1374
1375 1375 static int
1376 1376 vdc_close(dev_t dev, int flag, int otyp, cred_t *cred)
1377 1377 {
1378 1378 _NOTE(ARGUNUSED(cred))
1379 1379
1380 1380 int instance;
1381 1381 int slice;
1382 1382 int rv, rval;
1383 1383 vdc_t *vdc;
1384 1384
1385 1385 instance = VDCUNIT(dev);
1386 1386
1387 1387 if (otyp >= OTYPCNT)
1388 1388 return (EINVAL);
1389 1389
1390 1390 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1391 1391 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
1392 1392 return (ENXIO);
1393 1393 }
1394 1394
1395 1395 DMSG(vdc, 0, "[%d] flag = %x, otyp = %x\n", instance, flag, otyp);
1396 1396
1397 1397 slice = VDCPART(dev);
1398 1398
1399 1399 /*
1400 1400 * Attempt to flush the W$ on a close operation. If this is
1401 1401 * not a supported IOCTL command or the backing device is read-only
1402 1402 * do not fail the close operation.
1403 1403 */
1404 1404 rv = vd_process_ioctl(dev, DKIOCFLUSHWRITECACHE, NULL, FKIOCTL, &rval);
1405 1405
1406 1406 if (rv != 0 && rv != ENOTSUP && rv != ENOTTY && rv != EROFS) {
1407 1407 DMSG(vdc, 0, "[%d] flush failed with error %d on close\n",
1408 1408 instance, rv);
1409 1409 return (EIO);
1410 1410 }
1411 1411
1412 1412 mutex_enter(&vdc->lock);
1413 1413 vdc_mark_closed(vdc, slice, flag, otyp);
1414 1414 mutex_exit(&vdc->lock);
1415 1415
1416 1416 return (0);
1417 1417 }
1418 1418
1419 1419 static int
1420 1420 vdc_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
1421 1421 {
1422 1422 _NOTE(ARGUNUSED(credp))
1423 1423
1424 1424 return (vd_process_ioctl(dev, cmd, (caddr_t)arg, mode, rvalp));
1425 1425 }
1426 1426
1427 1427 static int
1428 1428 vdc_print(dev_t dev, char *str)
1429 1429 {
1430 1430 cmn_err(CE_NOTE, "vdc%d: %s", VDCUNIT(dev), str);
1431 1431 return (0);
1432 1432 }
1433 1433
1434 1434 static int
1435 1435 vdc_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
1436 1436 {
1437 1437 int rv, flags;
1438 1438 size_t nbytes = nblk * DEV_BSIZE;
1439 1439 int instance = VDCUNIT(dev);
1440 1440 vdc_t *vdc = NULL;
1441 1441 diskaddr_t vio_blkno;
1442 1442
1443 1443 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1444 1444 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
1445 1445 return (ENXIO);
1446 1446 }
1447 1447
1448 1448 DMSG(vdc, 2, "[%d] dump %ld bytes at block 0x%lx : addr=0x%p\n",
1449 1449 instance, nbytes, blkno, (void *)addr);
1450 1450
1451 1451 /* convert logical block to vio block */
1452 1452 if ((blkno & vdc->vio_bmask) != 0) {
1453 1453 DMSG(vdc, 0, "Misaligned block number (%lu)\n", blkno);
1454 1454 return (EINVAL);
1455 1455 }
1456 1456 vio_blkno = blkno >> vdc->vio_bshift;
1457 1457
1458 1458 /*
1459 1459 * If we are panicking, we need the state to be "running" so that we
1460 1460 * can submit I/Os, but we don't want to check for any backend error.
1461 1461 */
1462 1462 flags = (ddi_in_panic())? VDC_OP_STATE_RUNNING : VDC_OP_NORMAL;
1463 1463
1464 1464 rv = vdc_do_op(vdc, VD_OP_BWRITE, addr, nbytes, VDCPART(dev),
1465 1465 vio_blkno, NULL, VIO_write_dir, flags);
1466 1466
1467 1467 if (rv) {
1468 1468 DMSG(vdc, 0, "Failed to do a disk dump (err=%d)\n", rv);
1469 1469 return (rv);
1470 1470 }
1471 1471
1472 1472 DMSG(vdc, 0, "[%d] End\n", instance);
1473 1473
1474 1474 return (0);
1475 1475 }
1476 1476
1477 1477 /* -------------------------------------------------------------------------- */
1478 1478
1479 1479 /*
1480 1480 * Disk access routines
1481 1481 *
1482 1482 */
1483 1483
1484 1484 /*
1485 1485 * vdc_strategy()
1486 1486 *
1487 1487 * Return Value:
1488 1488 * 0: As per strategy(9E), the strategy() function must return 0
1489 1489 * [ bioerror(9f) sets b_flags to the proper error code ]
1490 1490 */
1491 1491 static int
1492 1492 vdc_strategy(struct buf *buf)
1493 1493 {
1494 1494 diskaddr_t vio_blkno;
1495 1495 vdc_t *vdc = NULL;
1496 1496 int instance = VDCUNIT(buf->b_edev);
1497 1497 int op = (buf->b_flags & B_READ) ? VD_OP_BREAD : VD_OP_BWRITE;
1498 1498 int slice;
1499 1499
1500 1500 if ((vdc = ddi_get_soft_state(vdc_state, instance)) == NULL) {
1501 1501 cmn_err(CE_NOTE, "[%d] Couldn't get state structure", instance);
1502 1502 bioerror(buf, ENXIO);
1503 1503 biodone(buf);
1504 1504 return (0);
1505 1505 }
1506 1506
1507 1507 DMSG(vdc, 2, "[%d] %s %ld bytes at block %llx : b_addr=0x%p\n",
1508 1508 instance, (buf->b_flags & B_READ) ? "Read" : "Write",
1509 1509 buf->b_bcount, buf->b_lblkno, (void *)buf->b_un.b_addr);
1510 1510
1511 1511 bp_mapin(buf);
1512 1512
1513 1513 if ((long)buf->b_private == VD_SLICE_NONE) {
1514 1514 /* I/O using an absolute disk offset */
1515 1515 slice = VD_SLICE_NONE;
1516 1516 } else {
1517 1517 slice = VDCPART(buf->b_edev);
1518 1518 }
1519 1519
1520 1520 /*
1521 1521 * In the buf structure, b_lblkno represents a logical block number
1522 1522 * using a block size of 512 bytes. For the VIO request, this block
1523 1523 * number has to be converted to be represented with the block size
1524 1524 * used by the VIO protocol.
1525 1525 */
1526 1526 if ((buf->b_lblkno & vdc->vio_bmask) != 0) {
1527 1527 bioerror(buf, EINVAL);
1528 1528 biodone(buf);
1529 1529 return (0);
1530 1530 }
1531 1531 vio_blkno = buf->b_lblkno >> vdc->vio_bshift;
1532 1532
1533 1533 /* submit the I/O, any error will be reported in the buf structure */
1534 1534 (void) vdc_do_op(vdc, op, (caddr_t)buf->b_un.b_addr,
1535 1535 buf->b_bcount, slice, vio_blkno,
1536 1536 buf, (op == VD_OP_BREAD) ? VIO_read_dir : VIO_write_dir,
1537 1537 VDC_OP_NORMAL);
1538 1538
1539 1539 return (0);
1540 1540 }
1541 1541
1542 1542 /*
1543 1543 * Function:
1544 1544 * vdc_min
1545 1545 *
1546 1546 * Description:
1547 1547 * Routine to limit the size of a data transfer. Used in
1548 1548 * conjunction with physio(9F).
1549 1549 *
1550 1550 * Arguments:
1551 1551 * bp - pointer to the indicated buf(9S) struct.
1552 1552 *
1553 1553 */
1554 1554 static void
1555 1555 vdc_min(struct buf *bufp)
1556 1556 {
1557 1557 vdc_t *vdc = NULL;
1558 1558 int instance = VDCUNIT(bufp->b_edev);
1559 1559
1560 1560 vdc = ddi_get_soft_state(vdc_state, instance);
1561 1561 VERIFY(vdc != NULL);
1562 1562
1563 1563 if (bufp->b_bcount > (vdc->max_xfer_sz * vdc->vdisk_bsize)) {
1564 1564 bufp->b_bcount = vdc->max_xfer_sz * vdc->vdisk_bsize;
1565 1565 }
1566 1566 }
1567 1567
1568 1568 static int
1569 1569 vdc_read(dev_t dev, struct uio *uio, cred_t *cred)
1570 1570 {
1571 1571 _NOTE(ARGUNUSED(cred))
1572 1572
1573 1573 DMSGX(1, "[%d] Entered", VDCUNIT(dev));
1574 1574 return (physio(vdc_strategy, NULL, dev, B_READ, vdc_min, uio));
1575 1575 }
1576 1576
1577 1577 static int
1578 1578 vdc_write(dev_t dev, struct uio *uio, cred_t *cred)
1579 1579 {
1580 1580 _NOTE(ARGUNUSED(cred))
1581 1581
1582 1582 DMSGX(1, "[%d] Entered", VDCUNIT(dev));
1583 1583 return (physio(vdc_strategy, NULL, dev, B_WRITE, vdc_min, uio));
1584 1584 }
1585 1585
1586 1586 static int
1587 1587 vdc_aread(dev_t dev, struct aio_req *aio, cred_t *cred)
1588 1588 {
1589 1589 _NOTE(ARGUNUSED(cred))
1590 1590
1591 1591 DMSGX(1, "[%d] Entered", VDCUNIT(dev));
1592 1592 return (aphysio(vdc_strategy, anocancel, dev, B_READ, vdc_min, aio));
1593 1593 }
1594 1594
1595 1595 static int
1596 1596 vdc_awrite(dev_t dev, struct aio_req *aio, cred_t *cred)
1597 1597 {
1598 1598 _NOTE(ARGUNUSED(cred))
1599 1599
1600 1600 DMSGX(1, "[%d] Entered", VDCUNIT(dev));
1601 1601 return (aphysio(vdc_strategy, anocancel, dev, B_WRITE, vdc_min, aio));
1602 1602 }
1603 1603
1604 1604
1605 1605 /* -------------------------------------------------------------------------- */
1606 1606
1607 1607 /*
1608 1608 * Handshake support
1609 1609 */
1610 1610
1611 1611
1612 1612 /*
1613 1613 * Function:
1614 1614 * vdc_init_ver_negotiation()
1615 1615 *
1616 1616 * Description:
1617 1617 *
1618 1618 * Arguments:
1619 1619 * vdc - soft state pointer for this instance of the device driver.
1620 1620 *
1621 1621 * Return Code:
1622 1622 * 0 - Success
1623 1623 */
1624 1624 static int
1625 1625 vdc_init_ver_negotiation(vdc_t *vdc, vio_ver_t ver)
1626 1626 {
1627 1627 vio_ver_msg_t pkt;
1628 1628 size_t msglen = sizeof (pkt);
1629 1629 int status = -1;
1630 1630
1631 1631 ASSERT(vdc != NULL);
1632 1632 ASSERT(mutex_owned(&vdc->lock));
1633 1633
1634 1634 DMSG(vdc, 0, "[%d] Entered.\n", vdc->instance);
1635 1635
1636 1636 /*
1637 1637 * set the Session ID to a unique value
1638 1638 * (the lower 32 bits of the clock tick)
1639 1639 */
1640 1640 vdc->session_id = ((uint32_t)gettick() & 0xffffffff);
1641 1641 DMSG(vdc, 0, "[%d] Set SID to 0x%lx\n", vdc->instance, vdc->session_id);
1642 1642
1643 1643 pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
1644 1644 pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
1645 1645 pkt.tag.vio_subtype_env = VIO_VER_INFO;
1646 1646 pkt.tag.vio_sid = vdc->session_id;
1647 1647 pkt.dev_class = VDEV_DISK;
1648 1648 pkt.ver_major = ver.major;
1649 1649 pkt.ver_minor = ver.minor;
1650 1650
1651 1651 status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
1652 1652 DMSG(vdc, 0, "[%d] Ver info sent (status = %d)\n",
1653 1653 vdc->instance, status);
1654 1654 if ((status != 0) || (msglen != sizeof (vio_ver_msg_t))) {
1655 1655 DMSG(vdc, 0, "[%d] Failed to send Ver negotiation info: "
1656 1656 "id(%lx) rv(%d) size(%ld)", vdc->instance,
1657 1657 vdc->curr_server->ldc_handle, status, msglen);
1658 1658 if (msglen != sizeof (vio_ver_msg_t))
1659 1659 status = ENOMSG;
1660 1660 }
1661 1661
1662 1662 return (status);
1663 1663 }
1664 1664
1665 1665 /*
1666 1666 * Function:
1667 1667 * vdc_ver_negotiation()
1668 1668 *
1669 1669 * Description:
1670 1670 *
1671 1671 * Arguments:
1672 1672 * vdcp - soft state pointer for this instance of the device driver.
1673 1673 *
1674 1674 * Return Code:
1675 1675 * 0 - Success
1676 1676 */
1677 1677 static int
1678 1678 vdc_ver_negotiation(vdc_t *vdcp)
1679 1679 {
1680 1680 vio_msg_t vio_msg;
1681 1681 int status;
1682 1682
1683 1683 if (status = vdc_init_ver_negotiation(vdcp, vdc_version[0]))
1684 1684 return (status);
1685 1685
1686 1686 /* release lock and wait for response */
1687 1687 mutex_exit(&vdcp->lock);
1688 1688 status = vdc_wait_for_response(vdcp, &vio_msg);
1689 1689 mutex_enter(&vdcp->lock);
1690 1690 if (status) {
1691 1691 DMSG(vdcp, 0,
1692 1692 "[%d] Failed waiting for Ver negotiation response, rv(%d)",
1693 1693 vdcp->instance, status);
1694 1694 return (status);
1695 1695 }
1696 1696
1697 1697 /* check type and sub_type ... */
1698 1698 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
1699 1699 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
1700 1700 DMSG(vdcp, 0, "[%d] Invalid ver negotiation response\n",
1701 1701 vdcp->instance);
1702 1702 return (EPROTO);
1703 1703 }
1704 1704
1705 1705 return (vdc_handle_ver_msg(vdcp, (vio_ver_msg_t *)&vio_msg));
1706 1706 }
1707 1707
1708 1708 /*
1709 1709 * Function:
1710 1710 * vdc_init_attr_negotiation()
1711 1711 *
1712 1712 * Description:
1713 1713 *
1714 1714 * Arguments:
1715 1715 * vdc - soft state pointer for this instance of the device driver.
1716 1716 *
1717 1717 * Return Code:
1718 1718 * 0 - Success
1719 1719 */
1720 1720 static int
1721 1721 vdc_init_attr_negotiation(vdc_t *vdc)
1722 1722 {
1723 1723 vd_attr_msg_t pkt;
1724 1724 size_t msglen = sizeof (pkt);
1725 1725 int status;
1726 1726
1727 1727 ASSERT(vdc != NULL);
1728 1728 ASSERT(mutex_owned(&vdc->lock));
1729 1729
1730 1730 DMSG(vdc, 0, "[%d] entered\n", vdc->instance);
1731 1731
1732 1732 /* fill in tag */
1733 1733 pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
1734 1734 pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
1735 1735 pkt.tag.vio_subtype_env = VIO_ATTR_INFO;
1736 1736 pkt.tag.vio_sid = vdc->session_id;
1737 1737 /* fill in payload */
1738 1738 pkt.max_xfer_sz = vdc->max_xfer_sz;
1739 1739 pkt.vdisk_block_size = vdc->vdisk_bsize;
1740 1740 pkt.xfer_mode = VIO_DRING_MODE_V1_0;
1741 1741 pkt.operations = 0; /* server will set bits of valid operations */
1742 1742 pkt.vdisk_type = 0; /* server will set to valid device type */
1743 1743 pkt.vdisk_media = 0; /* server will set to valid media type */
1744 1744 pkt.vdisk_size = 0; /* server will set to valid size */
1745 1745
1746 1746 status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
1747 1747 DMSG(vdc, 0, "Attr info sent (status = %d)\n", status);
1748 1748
1749 1749 if ((status != 0) || (msglen != sizeof (vd_attr_msg_t))) {
1750 1750 DMSG(vdc, 0, "[%d] Failed to send Attr negotiation info: "
1751 1751 "id(%lx) rv(%d) size(%ld)", vdc->instance,
1752 1752 vdc->curr_server->ldc_handle, status, msglen);
1753 1753 if (msglen != sizeof (vd_attr_msg_t))
1754 1754 status = ENOMSG;
1755 1755 }
1756 1756
1757 1757 return (status);
1758 1758 }
1759 1759
1760 1760 /*
1761 1761 * Function:
1762 1762 * vdc_attr_negotiation()
1763 1763 *
1764 1764 * Description:
1765 1765 *
1766 1766 * Arguments:
1767 1767 * vdc - soft state pointer for this instance of the device driver.
1768 1768 *
1769 1769 * Return Code:
1770 1770 * 0 - Success
1771 1771 */
1772 1772 static int
1773 1773 vdc_attr_negotiation(vdc_t *vdcp)
1774 1774 {
1775 1775 int status;
1776 1776 vio_msg_t vio_msg;
1777 1777
1778 1778 if (status = vdc_init_attr_negotiation(vdcp))
1779 1779 return (status);
1780 1780
1781 1781 /* release lock and wait for response */
1782 1782 mutex_exit(&vdcp->lock);
1783 1783 status = vdc_wait_for_response(vdcp, &vio_msg);
1784 1784 mutex_enter(&vdcp->lock);
1785 1785 if (status) {
1786 1786 DMSG(vdcp, 0,
1787 1787 "[%d] Failed waiting for Attr negotiation response, rv(%d)",
1788 1788 vdcp->instance, status);
1789 1789 return (status);
1790 1790 }
1791 1791
1792 1792 /* check type and sub_type ... */
1793 1793 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
1794 1794 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
1795 1795 DMSG(vdcp, 0, "[%d] Invalid attr negotiation response\n",
1796 1796 vdcp->instance);
1797 1797 return (EPROTO);
1798 1798 }
1799 1799
1800 1800 return (vdc_handle_attr_msg(vdcp, (vd_attr_msg_t *)&vio_msg));
1801 1801 }
1802 1802
1803 1803
1804 1804 /*
1805 1805 * Function:
1806 1806 * vdc_init_dring_negotiate()
1807 1807 *
1808 1808 * Description:
1809 1809 *
1810 1810 * Arguments:
1811 1811 * vdc - soft state pointer for this instance of the device driver.
1812 1812 *
1813 1813 * Return Code:
1814 1814 * 0 - Success
1815 1815 */
1816 1816 static int
1817 1817 vdc_init_dring_negotiate(vdc_t *vdc)
1818 1818 {
1819 1819 vio_dring_reg_msg_t pkt;
1820 1820 size_t msglen = sizeof (pkt);
1821 1821 int status = -1;
1822 1822 int retry;
1823 1823 int nretries = 10;
1824 1824
1825 1825 ASSERT(vdc != NULL);
1826 1826 ASSERT(mutex_owned(&vdc->lock));
1827 1827
1828 1828 for (retry = 0; retry < nretries; retry++) {
1829 1829 status = vdc_init_descriptor_ring(vdc);
1830 1830 if (status != EAGAIN)
1831 1831 break;
1832 1832 drv_usecwait(vdc_min_timeout_ldc);
1833 1833 }
1834 1834
1835 1835 if (status != 0) {
1836 1836 DMSG(vdc, 0, "[%d] Failed to init DRing (status = %d)\n",
1837 1837 vdc->instance, status);
1838 1838 return (status);
1839 1839 }
1840 1840
1841 1841 DMSG(vdc, 0, "[%d] Init of descriptor ring completed (status = %d)\n",
1842 1842 vdc->instance, status);
1843 1843
1844 1844 /* fill in tag */
1845 1845 pkt.tag.vio_msgtype = VIO_TYPE_CTRL;
1846 1846 pkt.tag.vio_subtype = VIO_SUBTYPE_INFO;
1847 1847 pkt.tag.vio_subtype_env = VIO_DRING_REG;
1848 1848 pkt.tag.vio_sid = vdc->session_id;
1849 1849 /* fill in payload */
1850 1850 pkt.dring_ident = 0;
1851 1851 pkt.num_descriptors = vdc->dring_len;
1852 1852 pkt.descriptor_size = vdc->dring_entry_size;
1853 1853 pkt.options = (VIO_TX_DRING | VIO_RX_DRING);
1854 1854 pkt.ncookies = vdc->dring_cookie_count;
1855 1855 pkt.cookie[0] = vdc->dring_cookie[0]; /* for now just one cookie */
1856 1856
1857 1857 status = vdc_send(vdc, (caddr_t)&pkt, &msglen);
1858 1858 if (status != 0) {
1859 1859 DMSG(vdc, 0, "[%d] Failed to register DRing (err = %d)",
1860 1860 vdc->instance, status);
1861 1861 }
1862 1862
1863 1863 return (status);
1864 1864 }
1865 1865
1866 1866
1867 1867 /*
1868 1868 * Function:
1869 1869 * vdc_dring_negotiation()
1870 1870 *
1871 1871 * Description:
1872 1872 *
1873 1873 * Arguments:
1874 1874 * vdc - soft state pointer for this instance of the device driver.
1875 1875 *
1876 1876 * Return Code:
1877 1877 * 0 - Success
1878 1878 */
1879 1879 static int
1880 1880 vdc_dring_negotiation(vdc_t *vdcp)
1881 1881 {
1882 1882 int status;
1883 1883 vio_msg_t vio_msg;
1884 1884
1885 1885 if (status = vdc_init_dring_negotiate(vdcp))
1886 1886 return (status);
1887 1887
1888 1888 /* release lock and wait for response */
1889 1889 mutex_exit(&vdcp->lock);
1890 1890 status = vdc_wait_for_response(vdcp, &vio_msg);
1891 1891 mutex_enter(&vdcp->lock);
1892 1892 if (status) {
1893 1893 DMSG(vdcp, 0,
1894 1894 "[%d] Failed waiting for Dring negotiation response,"
1895 1895 " rv(%d)", vdcp->instance, status);
1896 1896 return (status);
1897 1897 }
1898 1898
1899 1899 /* check type and sub_type ... */
1900 1900 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
1901 1901 vio_msg.tag.vio_subtype == VIO_SUBTYPE_INFO) {
1902 1902 DMSG(vdcp, 0, "[%d] Invalid Dring negotiation response\n",
1903 1903 vdcp->instance);
1904 1904 return (EPROTO);
1905 1905 }
1906 1906
1907 1907 return (vdc_handle_dring_reg_msg(vdcp,
1908 1908 (vio_dring_reg_msg_t *)&vio_msg));
1909 1909 }
1910 1910
1911 1911
1912 1912 /*
1913 1913 * Function:
1914 1914 * vdc_send_rdx()
1915 1915 *
1916 1916 * Description:
1917 1917 *
1918 1918 * Arguments:
1919 1919 * vdc - soft state pointer for this instance of the device driver.
1920 1920 *
1921 1921 * Return Code:
1922 1922 * 0 - Success
1923 1923 */
1924 1924 static int
1925 1925 vdc_send_rdx(vdc_t *vdcp)
1926 1926 {
1927 1927 vio_msg_t msg;
1928 1928 size_t msglen = sizeof (vio_msg_t);
1929 1929 int status;
1930 1930
1931 1931 /*
1932 1932 * Send an RDX message to vds to indicate we are ready
1933 1933 * to send data
1934 1934 */
1935 1935 msg.tag.vio_msgtype = VIO_TYPE_CTRL;
1936 1936 msg.tag.vio_subtype = VIO_SUBTYPE_INFO;
1937 1937 msg.tag.vio_subtype_env = VIO_RDX;
1938 1938 msg.tag.vio_sid = vdcp->session_id;
1939 1939 status = vdc_send(vdcp, (caddr_t)&msg, &msglen);
1940 1940 if (status != 0) {
1941 1941 DMSG(vdcp, 0, "[%d] Failed to send RDX message (%d)",
1942 1942 vdcp->instance, status);
1943 1943 }
1944 1944
1945 1945 return (status);
1946 1946 }
1947 1947
1948 1948 /*
1949 1949 * Function:
1950 1950 * vdc_handle_rdx()
1951 1951 *
1952 1952 * Description:
1953 1953 *
1954 1954 * Arguments:
1955 1955 * vdc - soft state pointer for this instance of the device driver.
1956 1956 * msgp - received msg
1957 1957 *
1958 1958 * Return Code:
1959 1959 * 0 - Success
1960 1960 */
1961 1961 static int
1962 1962 vdc_handle_rdx(vdc_t *vdcp, vio_rdx_msg_t *msgp)
1963 1963 {
1964 1964 _NOTE(ARGUNUSED(vdcp))
1965 1965 _NOTE(ARGUNUSED(msgp))
1966 1966
1967 1967 ASSERT(msgp->tag.vio_msgtype == VIO_TYPE_CTRL);
1968 1968 ASSERT(msgp->tag.vio_subtype == VIO_SUBTYPE_ACK);
1969 1969 ASSERT(msgp->tag.vio_subtype_env == VIO_RDX);
1970 1970
1971 1971 DMSG(vdcp, 1, "[%d] Got an RDX msg", vdcp->instance);
1972 1972
1973 1973 return (0);
1974 1974 }
1975 1975
1976 1976 /*
1977 1977 * Function:
1978 1978 * vdc_rdx_exchange()
1979 1979 *
1980 1980 * Description:
1981 1981 *
1982 1982 * Arguments:
1983 1983 * vdc - soft state pointer for this instance of the device driver.
1984 1984 *
1985 1985 * Return Code:
1986 1986 * 0 - Success
1987 1987 */
1988 1988 static int
1989 1989 vdc_rdx_exchange(vdc_t *vdcp)
1990 1990 {
1991 1991 int status;
1992 1992 vio_msg_t vio_msg;
1993 1993
1994 1994 if (status = vdc_send_rdx(vdcp))
1995 1995 return (status);
1996 1996
1997 1997 /* release lock and wait for response */
1998 1998 mutex_exit(&vdcp->lock);
1999 1999 status = vdc_wait_for_response(vdcp, &vio_msg);
2000 2000 mutex_enter(&vdcp->lock);
2001 2001 if (status) {
2002 2002 DMSG(vdcp, 0, "[%d] Failed waiting for RDX response, rv(%d)",
2003 2003 vdcp->instance, status);
2004 2004 return (status);
2005 2005 }
2006 2006
2007 2007 /* check type and sub_type ... */
2008 2008 if (vio_msg.tag.vio_msgtype != VIO_TYPE_CTRL ||
2009 2009 vio_msg.tag.vio_subtype != VIO_SUBTYPE_ACK) {
2010 2010 DMSG(vdcp, 0, "[%d] Invalid RDX response\n", vdcp->instance);
2011 2011 return (EPROTO);
2012 2012 }
2013 2013
2014 2014 return (vdc_handle_rdx(vdcp, (vio_rdx_msg_t *)&vio_msg));
2015 2015 }
2016 2016
2017 2017
2018 2018 /* -------------------------------------------------------------------------- */
2019 2019
2020 2020 /*
2021 2021 * LDC helper routines
2022 2022 */
2023 2023
2024 2024 static int
2025 2025 vdc_recv(vdc_t *vdc, vio_msg_t *msgp, size_t *nbytesp)
2026 2026 {
2027 2027 int status;
2028 2028 uint64_t delay_time;
2029 2029 size_t len;
2030 2030
2031 2031 /*
2032 2032 * Until we get a blocking ldc read we have to retry until the entire
2033 2033 * LDC message has arrived before ldc_read() will return that message.
2034 2034 * If ldc_read() succeed but returns a zero length message then that
2035 2035 * means that the LDC queue is empty and we have to wait for a
2036 2036 * notification from the LDC callback which will set the read_state to
2037 2037 * VDC_READ_PENDING. Note we also bail out if the channel is reset or
2038 2038 * goes away.
2039 2039 */
2040 2040 delay_time = vdc_ldc_read_init_delay;
2041 2041
2042 2042 for (;;) {
2043 2043
2044 2044 len = *nbytesp;
2045 2045 /*
2046 2046 * vdc->curr_server is protected by vdc->lock but to avoid
2047 2047 * contentions we don't take the lock here. We can do this
2048 2048 * safely because vdc_recv() is only called from thread
2049 2049 * process_msg_thread() which is also the only thread that
2050 2050 * can change vdc->curr_server.
2051 2051 */
2052 2052 status = ldc_read(vdc->curr_server->ldc_handle,
2053 2053 (caddr_t)msgp, &len);
2054 2054
2055 2055 if (status == EAGAIN) {
2056 2056 delay_time *= 2;
2057 2057 if (delay_time >= vdc_ldc_read_max_delay)
2058 2058 delay_time = vdc_ldc_read_max_delay;
2059 2059 delay(delay_time);
2060 2060 continue;
2061 2061 }
2062 2062
2063 2063 if (status != 0) {
2064 2064 DMSG(vdc, 0, "ldc_read returned %d\n", status);
2065 2065 break;
2066 2066 }
2067 2067
2068 2068 if (len != 0) {
2069 2069 *nbytesp = len;
2070 2070 break;
2071 2071 }
2072 2072
2073 2073 mutex_enter(&vdc->read_lock);
2074 2074
2075 2075 while (vdc->read_state != VDC_READ_PENDING) {
2076 2076
2077 2077 /* detect if the connection has been reset */
2078 2078 if (vdc->read_state == VDC_READ_RESET) {
2079 2079 mutex_exit(&vdc->read_lock);
2080 2080 return (ECONNRESET);
2081 2081 }
2082 2082
2083 2083 vdc->read_state = VDC_READ_WAITING;
2084 2084 cv_wait(&vdc->read_cv, &vdc->read_lock);
2085 2085 }
2086 2086
2087 2087 vdc->read_state = VDC_READ_IDLE;
2088 2088 mutex_exit(&vdc->read_lock);
2089 2089
2090 2090 delay_time = vdc_ldc_read_init_delay;
2091 2091 }
2092 2092
2093 2093 return (status);
2094 2094 }
2095 2095
2096 2096
2097 2097
2098 2098 #ifdef DEBUG
2099 2099 void
2100 2100 vdc_decode_tag(vdc_t *vdcp, vio_msg_t *msg)
2101 2101 {
2102 2102 char *ms, *ss, *ses;
2103 2103 switch (msg->tag.vio_msgtype) {
2104 2104 #define Q(_s) case _s : ms = #_s; break;
2105 2105 Q(VIO_TYPE_CTRL)
2106 2106 Q(VIO_TYPE_DATA)
2107 2107 Q(VIO_TYPE_ERR)
2108 2108 #undef Q
2109 2109 default: ms = "unknown"; break;
2110 2110 }
2111 2111
2112 2112 switch (msg->tag.vio_subtype) {
2113 2113 #define Q(_s) case _s : ss = #_s; break;
2114 2114 Q(VIO_SUBTYPE_INFO)
2115 2115 Q(VIO_SUBTYPE_ACK)
2116 2116 Q(VIO_SUBTYPE_NACK)
2117 2117 #undef Q
2118 2118 default: ss = "unknown"; break;
2119 2119 }
2120 2120
2121 2121 switch (msg->tag.vio_subtype_env) {
2122 2122 #define Q(_s) case _s : ses = #_s; break;
2123 2123 Q(VIO_VER_INFO)
2124 2124 Q(VIO_ATTR_INFO)
2125 2125 Q(VIO_DRING_REG)
2126 2126 Q(VIO_DRING_UNREG)
2127 2127 Q(VIO_RDX)
2128 2128 Q(VIO_PKT_DATA)
2129 2129 Q(VIO_DESC_DATA)
2130 2130 Q(VIO_DRING_DATA)
2131 2131 #undef Q
2132 2132 default: ses = "unknown"; break;
2133 2133 }
2134 2134
2135 2135 DMSG(vdcp, 3, "(%x/%x/%x) message : (%s/%s/%s)\n",
2136 2136 msg->tag.vio_msgtype, msg->tag.vio_subtype,
2137 2137 msg->tag.vio_subtype_env, ms, ss, ses);
2138 2138 }
2139 2139 #endif
2140 2140
2141 2141 /*
2142 2142 * Function:
2143 2143 * vdc_send()
2144 2144 *
2145 2145 * Description:
2146 2146 * The function encapsulates the call to write a message using LDC.
2147 2147 * If LDC indicates that the call failed due to the queue being full,
2148 2148 * we retry the ldc_write(), otherwise we return the error returned by LDC.
2149 2149 *
2150 2150 * Arguments:
2151 2151 * ldc_handle - LDC handle for the channel this instance of vdc uses
2152 2152 * pkt - address of LDC message to be sent
2153 2153 * msglen - the size of the message being sent. When the function
2154 2154 * returns, this contains the number of bytes written.
2155 2155 *
2156 2156 * Return Code:
2157 2157 * 0 - Success.
2158 2158 * EINVAL - pkt or msglen were NULL
2159 2159 * ECONNRESET - The connection was not up.
2160 2160 * EWOULDBLOCK - LDC queue is full
2161 2161 * xxx - other error codes returned by ldc_write
2162 2162 */
2163 2163 static int
2164 2164 vdc_send(vdc_t *vdc, caddr_t pkt, size_t *msglen)
2165 2165 {
2166 2166 size_t size = 0;
2167 2167 int status = 0;
2168 2168 clock_t delay_ticks;
2169 2169
2170 2170 ASSERT(vdc != NULL);
2171 2171 ASSERT(mutex_owned(&vdc->lock));
2172 2172 ASSERT(msglen != NULL);
2173 2173 ASSERT(*msglen != 0);
2174 2174
2175 2175 #ifdef DEBUG
2176 2176 vdc_decode_tag(vdc, (vio_msg_t *)(uintptr_t)pkt);
2177 2177 #endif
2178 2178 /*
2179 2179 * Wait indefinitely to send if channel
2180 2180 * is busy, but bail out if we succeed or
2181 2181 * if the channel closes or is reset.
2182 2182 */
2183 2183 delay_ticks = vdc_hz_min_ldc_delay;
2184 2184 do {
2185 2185 size = *msglen;
2186 2186 status = ldc_write(vdc->curr_server->ldc_handle, pkt, &size);
2187 2187 if (status == EWOULDBLOCK) {
2188 2188 delay(delay_ticks);
2189 2189 /* geometric backoff */
2190 2190 delay_ticks *= 2;
2191 2191 if (delay_ticks > vdc_hz_max_ldc_delay)
2192 2192 delay_ticks = vdc_hz_max_ldc_delay;
2193 2193 }
2194 2194 } while (status == EWOULDBLOCK);
2195 2195
2196 2196 /* if LDC had serious issues --- reset vdc state */
2197 2197 if (status == EIO || status == ECONNRESET) {
2198 2198 /* LDC had serious issues --- reset vdc state */
2199 2199 mutex_enter(&vdc->read_lock);
2200 2200 if ((vdc->read_state == VDC_READ_WAITING) ||
2201 2201 (vdc->read_state == VDC_READ_RESET))
2202 2202 cv_signal(&vdc->read_cv);
2203 2203 vdc->read_state = VDC_READ_RESET;
2204 2204 mutex_exit(&vdc->read_lock);
2205 2205
2206 2206 /* wake up any waiters in the reset thread */
2207 2207 if (vdc->state == VDC_STATE_INIT_WAITING) {
2208 2208 DMSG(vdc, 0, "[%d] write reset - "
2209 2209 "vdc is resetting ..\n", vdc->instance);
2210 2210 vdc->state = VDC_STATE_RESETTING;
2211 2211 cv_signal(&vdc->initwait_cv);
2212 2212 }
2213 2213
2214 2214 return (ECONNRESET);
2215 2215 }
2216 2216
2217 2217 /* return the last size written */
2218 2218 *msglen = size;
2219 2219
2220 2220 return (status);
2221 2221 }
2222 2222
2223 2223 /*
2224 2224 * Function:
2225 2225 * vdc_get_md_node
2226 2226 *
2227 2227 * Description:
2228 2228 * Get the MD, the device node for the given disk instance. The
2229 2229 * caller is responsible for cleaning up the reference to the
2230 2230 * returned MD (mdpp) by calling md_fini_handle().
2231 2231 *
2232 2232 * Arguments:
2233 2233 * dip - dev info pointer for this instance of the device driver.
2234 2234 * mdpp - the returned MD.
2235 2235 * vd_nodep - the returned device node.
2236 2236 *
2237 2237 * Return Code:
2238 2238 * 0 - Success.
2239 2239 * ENOENT - Expected node or property did not exist.
2240 2240 * ENXIO - Unexpected error communicating with MD framework
2241 2241 */
2242 2242 static int
2243 2243 vdc_get_md_node(dev_info_t *dip, md_t **mdpp, mde_cookie_t *vd_nodep)
2244 2244 {
2245 2245 int status = ENOENT;
2246 2246 char *node_name = NULL;
2247 2247 md_t *mdp = NULL;
2248 2248 int num_nodes;
2249 2249 int num_vdevs;
2250 2250 mde_cookie_t rootnode;
2251 2251 mde_cookie_t *listp = NULL;
2252 2252 boolean_t found_inst = B_FALSE;
2253 2253 int listsz;
2254 2254 int idx;
2255 2255 uint64_t md_inst;
2256 2256 int obp_inst;
2257 2257 int instance = ddi_get_instance(dip);
2258 2258
2259 2259 /*
2260 2260 * Get the OBP instance number for comparison with the MD instance
2261 2261 *
2262 2262 * The "cfg-handle" property of a vdc node in an MD contains the MD's
2263 2263 * notion of "instance", or unique identifier, for that node; OBP
2264 2264 * stores the value of the "cfg-handle" MD property as the value of
2265 2265 * the "reg" property on the node in the device tree it builds from
2266 2266 * the MD and passes to Solaris. Thus, we look up the devinfo node's
2267 2267 * "reg" property value to uniquely identify this device instance.
2268 2268 * If the "reg" property cannot be found, the device tree state is
2269 2269 * presumably so broken that there is no point in continuing.
2270 2270 */
2271 2271 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, OBP_REG)) {
2272 2272 cmn_err(CE_WARN, "'%s' property does not exist", OBP_REG);
2273 2273 return (ENOENT);
2274 2274 }
2275 2275 obp_inst = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2276 2276 OBP_REG, -1);
2277 2277 DMSGX(1, "[%d] OBP inst=%d\n", instance, obp_inst);
2278 2278
2279 2279 /*
2280 2280 * We now walk the MD nodes to find the node for this vdisk.
2281 2281 */
2282 2282 if ((mdp = md_get_handle()) == NULL) {
2283 2283 cmn_err(CE_WARN, "unable to init machine description");
2284 2284 return (ENXIO);
2285 2285 }
2286 2286
2287 2287 num_nodes = md_node_count(mdp);
2288 2288 ASSERT(num_nodes > 0);
2289 2289
2290 2290 listsz = num_nodes * sizeof (mde_cookie_t);
2291 2291
2292 2292 /* allocate memory for nodes */
2293 2293 listp = kmem_zalloc(listsz, KM_SLEEP);
2294 2294
2295 2295 rootnode = md_root_node(mdp);
2296 2296 ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
2297 2297
2298 2298 /*
2299 2299 * Search for all the virtual devices, we will then check to see which
2300 2300 * ones are disk nodes.
2301 2301 */
2302 2302 num_vdevs = md_scan_dag(mdp, rootnode,
2303 2303 md_find_name(mdp, VDC_MD_VDEV_NAME),
2304 2304 md_find_name(mdp, "fwd"), listp);
2305 2305
2306 2306 if (num_vdevs <= 0) {
2307 2307 cmn_err(CE_NOTE, "No '%s' node found", VDC_MD_VDEV_NAME);
2308 2308 status = ENOENT;
2309 2309 goto done;
2310 2310 }
2311 2311
2312 2312 DMSGX(1, "[%d] num_vdevs=%d\n", instance, num_vdevs);
2313 2313 for (idx = 0; idx < num_vdevs; idx++) {
2314 2314 status = md_get_prop_str(mdp, listp[idx], "name", &node_name);
2315 2315 if ((status != 0) || (node_name == NULL)) {
2316 2316 cmn_err(CE_NOTE, "Unable to get name of node type '%s'"
2317 2317 ": err %d", VDC_MD_VDEV_NAME, status);
2318 2318 continue;
2319 2319 }
2320 2320
2321 2321 DMSGX(1, "[%d] Found node '%s'\n", instance, node_name);
2322 2322 if (strcmp(VDC_MD_DISK_NAME, node_name) == 0) {
2323 2323 status = md_get_prop_val(mdp, listp[idx],
2324 2324 VDC_MD_CFG_HDL, &md_inst);
2325 2325 DMSGX(1, "[%d] vdc inst in MD=%lx\n",
2326 2326 instance, md_inst);
2327 2327 if ((status == 0) && (md_inst == obp_inst)) {
2328 2328 found_inst = B_TRUE;
2329 2329 break;
2330 2330 }
2331 2331 }
2332 2332 }
2333 2333
2334 2334 if (!found_inst) {
2335 2335 DMSGX(0, "Unable to find correct '%s' node", VDC_MD_DISK_NAME);
2336 2336 status = ENOENT;
2337 2337 goto done;
2338 2338 }
2339 2339 DMSGX(0, "[%d] MD inst=%lx\n", instance, md_inst);
2340 2340
2341 2341 *vd_nodep = listp[idx];
2342 2342 *mdpp = mdp;
2343 2343 done:
2344 2344 kmem_free(listp, listsz);
2345 2345 return (status);
2346 2346 }
2347 2347
2348 2348 /*
2349 2349 * Function:
2350 2350 * vdc_init_ports
2351 2351 *
2352 2352 * Description:
2353 2353 * Initialize all the ports for this vdisk instance.
2354 2354 *
2355 2355 * Arguments:
2356 2356 * vdc - soft state pointer for this instance of the device driver.
2357 2357 * mdp - md pointer
2358 2358 * vd_nodep - device md node.
2359 2359 *
2360 2360 * Return Code:
2361 2361 * 0 - Success.
2362 2362 * ENOENT - Expected node or property did not exist.
2363 2363 */
2364 2364 static int
2365 2365 vdc_init_ports(vdc_t *vdc, md_t *mdp, mde_cookie_t vd_nodep)
2366 2366 {
2367 2367 int status = 0;
2368 2368 int idx;
2369 2369 int num_nodes;
2370 2370 int num_vports;
2371 2371 int num_chans;
2372 2372 int listsz;
2373 2373 mde_cookie_t vd_port;
2374 2374 mde_cookie_t *chanp = NULL;
2375 2375 mde_cookie_t *portp = NULL;
2376 2376 vdc_server_t *srvr;
2377 2377 vdc_server_t *prev_srvr = NULL;
2378 2378
2379 2379 /*
2380 2380 * We now walk the MD nodes to find the port nodes for this vdisk.
2381 2381 */
2382 2382 num_nodes = md_node_count(mdp);
2383 2383 ASSERT(num_nodes > 0);
2384 2384
2385 2385 listsz = num_nodes * sizeof (mde_cookie_t);
2386 2386
2387 2387 /* allocate memory for nodes */
2388 2388 portp = kmem_zalloc(listsz, KM_SLEEP);
2389 2389 chanp = kmem_zalloc(listsz, KM_SLEEP);
2390 2390
2391 2391 num_vports = md_scan_dag(mdp, vd_nodep,
2392 2392 md_find_name(mdp, VDC_MD_PORT_NAME),
2393 2393 md_find_name(mdp, "fwd"), portp);
2394 2394 if (num_vports == 0) {
2395 2395 DMSGX(0, "Found no '%s' node for '%s' port\n",
2396 2396 VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME);
2397 2397 status = ENOENT;
2398 2398 goto done;
2399 2399 }
2400 2400
2401 2401 DMSGX(1, "Found %d '%s' node(s) for '%s' port\n",
2402 2402 num_vports, VDC_MD_PORT_NAME, VDC_MD_VDEV_NAME);
2403 2403
2404 2404 vdc->num_servers = 0;
2405 2405 for (idx = 0; idx < num_vports; idx++) {
2406 2406
2407 2407 /* initialize this port */
2408 2408 vd_port = portp[idx];
2409 2409 srvr = kmem_zalloc(sizeof (vdc_server_t), KM_SLEEP);
2410 2410 srvr->vdcp = vdc;
2411 2411 srvr->svc_state = VDC_SERVICE_OFFLINE;
2412 2412 srvr->log_state = VDC_SERVICE_NONE;
2413 2413
2414 2414 /* get port id */
2415 2415 if (md_get_prop_val(mdp, vd_port, VDC_MD_ID, &srvr->id) != 0) {
2416 2416 cmn_err(CE_NOTE, "vDisk port '%s' property not found",
2417 2417 VDC_MD_ID);
2418 2418 kmem_free(srvr, sizeof (vdc_server_t));
2419 2419 continue;
2420 2420 }
2421 2421
2422 2422 /* set the connection timeout */
2423 2423 if (md_get_prop_val(mdp, vd_port, VDC_MD_TIMEOUT,
2424 2424 &srvr->ctimeout) != 0) {
2425 2425 srvr->ctimeout = 0;
2426 2426 }
2427 2427
2428 2428 /* get the ldc id */
2429 2429 num_chans = md_scan_dag(mdp, vd_port,
2430 2430 md_find_name(mdp, VDC_MD_CHAN_NAME),
2431 2431 md_find_name(mdp, "fwd"), chanp);
2432 2432
2433 2433 /* expecting at least one channel */
2434 2434 if (num_chans <= 0) {
2435 2435 cmn_err(CE_NOTE, "No '%s' node for '%s' port",
2436 2436 VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME);
2437 2437 kmem_free(srvr, sizeof (vdc_server_t));
2438 2438 continue;
2439 2439 } else if (num_chans != 1) {
2440 2440 DMSGX(0, "Expected 1 '%s' node for '%s' port, "
2441 2441 "found %d\n", VDC_MD_CHAN_NAME, VDC_MD_VDEV_NAME,
2442 2442 num_chans);
2443 2443 }
2444 2444
2445 2445 /*
2446 2446 * We use the first channel found (index 0), irrespective of how
2447 2447 * many are there in total.
2448 2448 */
2449 2449 if (md_get_prop_val(mdp, chanp[0], VDC_MD_ID,
2450 2450 &srvr->ldc_id) != 0) {
2451 2451 cmn_err(CE_NOTE, "Channel '%s' property not found",
2452 2452 VDC_MD_ID);
2453 2453 kmem_free(srvr, sizeof (vdc_server_t));
2454 2454 continue;
2455 2455 }
2456 2456
2457 2457 /*
2458 2458 * now initialise LDC channel which will be used to
2459 2459 * communicate with this server
2460 2460 */
2461 2461 if (vdc_do_ldc_init(vdc, srvr) != 0) {
2462 2462 kmem_free(srvr, sizeof (vdc_server_t));
2463 2463 continue;
2464 2464 }
2465 2465
2466 2466 /* add server to list */
2467 2467 if (prev_srvr)
2468 2468 prev_srvr->next = srvr;
2469 2469 else
2470 2470 vdc->server_list = srvr;
2471 2471
2472 2472 prev_srvr = srvr;
2473 2473
2474 2474 /* inc numbers of servers */
2475 2475 vdc->num_servers++;
2476 2476 }
2477 2477
2478 2478 /* pick first server as current server */
2479 2479 if (vdc->server_list != NULL) {
2480 2480 vdc->curr_server = vdc->server_list;
2481 2481 status = 0;
2482 2482 } else {
2483 2483 status = ENOENT;
2484 2484 }
2485 2485
2486 2486 done:
2487 2487 kmem_free(chanp, listsz);
2488 2488 kmem_free(portp, listsz);
2489 2489 return (status);
2490 2490 }
2491 2491
2492 2492
2493 2493 /*
2494 2494 * Function:
2495 2495 * vdc_do_ldc_up
2496 2496 *
2497 2497 * Description:
2498 2498 * Bring the channel for the current server up.
2499 2499 *
2500 2500 * Arguments:
2501 2501 * vdc - soft state pointer for this instance of the device driver.
2502 2502 *
2503 2503 * Return Code:
2504 2504 * 0 - Success.
2505 2505 * EINVAL - Driver is detaching / LDC error
2506 2506 * ECONNREFUSED - Other end is not listening
2507 2507 */
2508 2508 static int
2509 2509 vdc_do_ldc_up(vdc_t *vdc)
2510 2510 {
2511 2511 int status;
2512 2512 ldc_status_t ldc_state;
2513 2513
2514 2514 ASSERT(MUTEX_HELD(&vdc->lock));
2515 2515
2516 2516 DMSG(vdc, 0, "[%d] Bringing up channel %lx\n",
2517 2517 vdc->instance, vdc->curr_server->ldc_id);
2518 2518
2519 2519 if (vdc->lifecycle == VDC_LC_DETACHING)
2520 2520 return (EINVAL);
2521 2521
2522 2522 if ((status = ldc_up(vdc->curr_server->ldc_handle)) != 0) {
2523 2523 switch (status) {
2524 2524 case ECONNREFUSED: /* listener not ready at other end */
2525 2525 DMSG(vdc, 0, "[%d] ldc_up(%lx,...) return %d\n",
2526 2526 vdc->instance, vdc->curr_server->ldc_id, status);
2527 2527 status = 0;
2528 2528 break;
2529 2529 default:
2530 2530 DMSG(vdc, 0, "[%d] Failed to bring up LDC: "
2531 2531 "channel=%ld, err=%d", vdc->instance,
2532 2532 vdc->curr_server->ldc_id, status);
2533 2533 break;
2534 2534 }
2535 2535 }
2536 2536
2537 2537 if (ldc_status(vdc->curr_server->ldc_handle, &ldc_state) == 0) {
2538 2538 vdc->curr_server->ldc_state = ldc_state;
2539 2539 if (ldc_state == LDC_UP) {
2540 2540 DMSG(vdc, 0, "[%d] LDC channel already up\n",
2541 2541 vdc->instance);
2542 2542 vdc->seq_num = 1;
2543 2543 vdc->seq_num_reply = 0;
2544 2544 }
2545 2545 }
2546 2546
2547 2547 return (status);
2548 2548 }
2549 2549
2550 2550 /*
2551 2551 * Function:
2552 2552 * vdc_terminate_ldc()
2553 2553 *
2554 2554 * Description:
2555 2555 *
2556 2556 * Arguments:
2557 2557 * vdc - soft state pointer for this instance of the device driver.
2558 2558 * srvr - vdc per-server info structure
2559 2559 *
2560 2560 * Return Code:
2561 2561 * None
2562 2562 */
2563 2563 static void
2564 2564 vdc_terminate_ldc(vdc_t *vdc, vdc_server_t *srvr)
2565 2565 {
2566 2566 int instance = ddi_get_instance(vdc->dip);
2567 2567
2568 2568 if (srvr->state & VDC_LDC_OPEN) {
2569 2569 DMSG(vdc, 0, "[%d] ldc_close()\n", instance);
2570 2570 (void) ldc_close(srvr->ldc_handle);
2571 2571 }
2572 2572 if (srvr->state & VDC_LDC_CB) {
2573 2573 DMSG(vdc, 0, "[%d] ldc_unreg_callback()\n", instance);
2574 2574 (void) ldc_unreg_callback(srvr->ldc_handle);
2575 2575 }
2576 2576 if (srvr->state & VDC_LDC_INIT) {
2577 2577 DMSG(vdc, 0, "[%d] ldc_fini()\n", instance);
2578 2578 (void) ldc_fini(srvr->ldc_handle);
2579 2579 srvr->ldc_handle = NULL;
2580 2580 }
2581 2581
2582 2582 srvr->state &= ~(VDC_LDC_INIT | VDC_LDC_CB | VDC_LDC_OPEN);
2583 2583 }
2584 2584
2585 2585 /*
2586 2586 * Function:
2587 2587 * vdc_fini_ports()
2588 2588 *
2589 2589 * Description:
2590 2590 * Finalize all ports by closing the channel associated with each
2591 2591 * port and also freeing the server structure.
2592 2592 *
2593 2593 * Arguments:
2594 2594 * vdc - soft state pointer for this instance of the device driver.
2595 2595 *
2596 2596 * Return Code:
2597 2597 * None
2598 2598 */
2599 2599 static void
2600 2600 vdc_fini_ports(vdc_t *vdc)
2601 2601 {
2602 2602 int instance = ddi_get_instance(vdc->dip);
2603 2603 vdc_server_t *srvr, *prev_srvr;
2604 2604
2605 2605 ASSERT(vdc != NULL);
2606 2606 ASSERT(mutex_owned(&vdc->lock));
2607 2607
2608 2608 DMSG(vdc, 0, "[%d] initialized=%x\n", instance, vdc->initialized);
2609 2609
2610 2610 srvr = vdc->server_list;
2611 2611
2612 2612 while (srvr) {
2613 2613
2614 2614 vdc_terminate_ldc(vdc, srvr);
2615 2615
2616 2616 /* next server */
2617 2617 prev_srvr = srvr;
2618 2618 srvr = srvr->next;
2619 2619
2620 2620 /* free server */
2621 2621 kmem_free(prev_srvr, sizeof (vdc_server_t));
2622 2622 }
2623 2623
2624 2624 vdc->server_list = NULL;
2625 2625 vdc->num_servers = 0;
2626 2626 }
2627 2627
2628 2628 /* -------------------------------------------------------------------------- */
2629 2629
2630 2630 /*
2631 2631 * Descriptor Ring helper routines
2632 2632 */
2633 2633
2634 2634 /*
2635 2635 * Function:
2636 2636 * vdc_init_descriptor_ring()
2637 2637 *
2638 2638 * Description:
2639 2639 *
2640 2640 * Arguments:
2641 2641 * vdc - soft state pointer for this instance of the device driver.
2642 2642 *
2643 2643 * Return Code:
2644 2644 * 0 - Success
2645 2645 */
2646 2646 static int
2647 2647 vdc_init_descriptor_ring(vdc_t *vdc)
2648 2648 {
2649 2649 vd_dring_entry_t *dep = NULL; /* DRing Entry pointer */
2650 2650 int status = 0;
2651 2651 int i;
2652 2652
2653 2653 DMSG(vdc, 0, "[%d] initialized=%x\n", vdc->instance, vdc->initialized);
2654 2654
2655 2655 ASSERT(vdc != NULL);
2656 2656 ASSERT(mutex_owned(&vdc->lock));
2657 2657
2658 2658 /* ensure we have enough room to store max sized block */
2659 2659 ASSERT(maxphys <= VD_MAX_BLOCK_SIZE);
2660 2660
2661 2661 if ((vdc->initialized & VDC_DRING_INIT) == 0) {
2662 2662 DMSG(vdc, 0, "[%d] ldc_mem_dring_create\n", vdc->instance);
2663 2663 /*
2664 2664 * Calculate the maximum block size we can transmit using one
2665 2665 * Descriptor Ring entry from the attributes returned by the
2666 2666 * vDisk server. This is subject to a minimum of 'maxphys'
2667 2667 * as we do not have the capability to split requests over
2668 2668 * multiple DRing entries.
2669 2669 */
2670 2670 if ((vdc->max_xfer_sz * vdc->vdisk_bsize) < maxphys) {
2671 2671 DMSG(vdc, 0, "[%d] using minimum DRing size\n",
2672 2672 vdc->instance);
2673 2673 vdc->dring_max_cookies = maxphys / PAGESIZE;
2674 2674 } else {
2675 2675 vdc->dring_max_cookies =
2676 2676 (vdc->max_xfer_sz * vdc->vdisk_bsize) / PAGESIZE;
2677 2677 }
2678 2678 vdc->dring_entry_size = (sizeof (vd_dring_entry_t) +
2679 2679 (sizeof (ldc_mem_cookie_t) *
2680 2680 (vdc->dring_max_cookies - 1)));
2681 2681 vdc->dring_len = VD_DRING_LEN;
2682 2682
2683 2683 status = ldc_mem_dring_create(vdc->dring_len,
2684 2684 vdc->dring_entry_size, &vdc->dring_hdl);
2685 2685 if ((vdc->dring_hdl == NULL) || (status != 0)) {
2686 2686 DMSG(vdc, 0, "[%d] Descriptor ring creation failed",
2687 2687 vdc->instance);
2688 2688 return (status);
2689 2689 }
2690 2690 vdc->initialized |= VDC_DRING_INIT;
2691 2691 }
2692 2692
2693 2693 if ((vdc->initialized & VDC_DRING_BOUND) == 0) {
2694 2694 DMSG(vdc, 0, "[%d] ldc_mem_dring_bind\n", vdc->instance);
2695 2695 vdc->dring_cookie =
2696 2696 kmem_zalloc(sizeof (ldc_mem_cookie_t), KM_SLEEP);
2697 2697
2698 2698 status = ldc_mem_dring_bind(vdc->curr_server->ldc_handle,
2699 2699 vdc->dring_hdl,
2700 2700 LDC_SHADOW_MAP|LDC_DIRECT_MAP, LDC_MEM_RW,
2701 2701 &vdc->dring_cookie[0],
2702 2702 &vdc->dring_cookie_count);
2703 2703 if (status != 0) {
2704 2704 DMSG(vdc, 0, "[%d] Failed to bind descriptor ring "
2705 2705 "(%lx) to channel (%lx) status=%d\n",
2706 2706 vdc->instance, vdc->dring_hdl,
2707 2707 vdc->curr_server->ldc_handle, status);
2708 2708 return (status);
2709 2709 }
2710 2710 ASSERT(vdc->dring_cookie_count == 1);
2711 2711 vdc->initialized |= VDC_DRING_BOUND;
2712 2712 }
2713 2713
2714 2714 status = ldc_mem_dring_info(vdc->dring_hdl, &vdc->dring_mem_info);
2715 2715 if (status != 0) {
2716 2716 DMSG(vdc, 0,
2717 2717 "[%d] Failed to get info for descriptor ring (%lx)\n",
2718 2718 vdc->instance, vdc->dring_hdl);
2719 2719 return (status);
2720 2720 }
2721 2721
2722 2722 if ((vdc->initialized & VDC_DRING_LOCAL) == 0) {
2723 2723 DMSG(vdc, 0, "[%d] local dring\n", vdc->instance);
2724 2724
2725 2725 /* Allocate the local copy of this dring */
2726 2726 vdc->local_dring =
2727 2727 kmem_zalloc(vdc->dring_len * sizeof (vdc_local_desc_t),
2728 2728 KM_SLEEP);
2729 2729 vdc->initialized |= VDC_DRING_LOCAL;
2730 2730 }
2731 2731
2732 2732 /*
2733 2733 * Mark all DRing entries as free and initialize the private
2734 2734 * descriptor's memory handles. If any entry is initialized,
2735 2735 * we need to free it later so we set the bit in 'initialized'
2736 2736 * at the start.
2737 2737 */
2738 2738 vdc->initialized |= VDC_DRING_ENTRY;
2739 2739 for (i = 0; i < vdc->dring_len; i++) {
2740 2740 dep = VDC_GET_DRING_ENTRY_PTR(vdc, i);
2741 2741 dep->hdr.dstate = VIO_DESC_FREE;
2742 2742
2743 2743 status = ldc_mem_alloc_handle(vdc->curr_server->ldc_handle,
2744 2744 &vdc->local_dring[i].desc_mhdl);
2745 2745 if (status != 0) {
2746 2746 DMSG(vdc, 0, "![%d] Failed to alloc mem handle for"
2747 2747 " descriptor %d", vdc->instance, i);
2748 2748 return (status);
2749 2749 }
2750 2750 vdc->local_dring[i].is_free = B_TRUE;
2751 2751 vdc->local_dring[i].dep = dep;
2752 2752 }
2753 2753
2754 2754 /* Initialize the starting index */
2755 2755 vdc->dring_curr_idx = VDC_DRING_FIRST_ENTRY;
2756 2756
2757 2757 return (status);
2758 2758 }
2759 2759
2760 2760 /*
2761 2761 * Function:
2762 2762 * vdc_destroy_descriptor_ring()
2763 2763 *
2764 2764 * Description:
2765 2765 *
2766 2766 * Arguments:
2767 2767 * vdc - soft state pointer for this instance of the device driver.
2768 2768 *
2769 2769 * Return Code:
2770 2770 * None
2771 2771 */
2772 2772 static void
2773 2773 vdc_destroy_descriptor_ring(vdc_t *vdc)
2774 2774 {
2775 2775 vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */
2776 2776 ldc_mem_handle_t mhdl = NULL;
2777 2777 ldc_mem_info_t minfo;
2778 2778 int status = -1;
2779 2779 int i; /* loop */
2780 2780
2781 2781 ASSERT(vdc != NULL);
2782 2782 ASSERT(mutex_owned(&vdc->lock));
2783 2783
2784 2784 DMSG(vdc, 0, "[%d] Entered\n", vdc->instance);
2785 2785
2786 2786 if (vdc->initialized & VDC_DRING_ENTRY) {
2787 2787 DMSG(vdc, 0,
2788 2788 "[%d] Removing Local DRing entries\n", vdc->instance);
2789 2789 for (i = 0; i < vdc->dring_len; i++) {
2790 2790 ldep = &vdc->local_dring[i];
2791 2791 mhdl = ldep->desc_mhdl;
2792 2792
2793 2793 if (mhdl == NULL)
2794 2794 continue;
2795 2795
2796 2796 if ((status = ldc_mem_info(mhdl, &minfo)) != 0) {
2797 2797 DMSG(vdc, 0,
2798 2798 "ldc_mem_info returned an error: %d\n",
2799 2799 status);
2800 2800
2801 2801 /*
2802 2802 * This must mean that the mem handle
2803 2803 * is not valid. Clear it out so that
2804 2804 * no one tries to use it.
2805 2805 */
2806 2806 ldep->desc_mhdl = NULL;
2807 2807 continue;
2808 2808 }
2809 2809
2810 2810 if (minfo.status == LDC_BOUND) {
2811 2811 (void) ldc_mem_unbind_handle(mhdl);
2812 2812 }
2813 2813
2814 2814 (void) ldc_mem_free_handle(mhdl);
2815 2815
2816 2816 ldep->desc_mhdl = NULL;
2817 2817 }
2818 2818 vdc->initialized &= ~VDC_DRING_ENTRY;
2819 2819 }
2820 2820
2821 2821 if (vdc->initialized & VDC_DRING_LOCAL) {
2822 2822 DMSG(vdc, 0, "[%d] Freeing Local DRing\n", vdc->instance);
2823 2823 kmem_free(vdc->local_dring,
2824 2824 vdc->dring_len * sizeof (vdc_local_desc_t));
2825 2825 vdc->initialized &= ~VDC_DRING_LOCAL;
2826 2826 }
2827 2827
2828 2828 if (vdc->initialized & VDC_DRING_BOUND) {
2829 2829 DMSG(vdc, 0, "[%d] Unbinding DRing\n", vdc->instance);
2830 2830 status = ldc_mem_dring_unbind(vdc->dring_hdl);
2831 2831 if (status == 0) {
2832 2832 vdc->initialized &= ~VDC_DRING_BOUND;
2833 2833 } else {
2834 2834 DMSG(vdc, 0, "[%d] Error %d unbinding DRing %lx",
2835 2835 vdc->instance, status, vdc->dring_hdl);
2836 2836 }
2837 2837 kmem_free(vdc->dring_cookie, sizeof (ldc_mem_cookie_t));
2838 2838 }
2839 2839
2840 2840 if (vdc->initialized & VDC_DRING_INIT) {
2841 2841 DMSG(vdc, 0, "[%d] Destroying DRing\n", vdc->instance);
2842 2842 status = ldc_mem_dring_destroy(vdc->dring_hdl);
2843 2843 if (status == 0) {
2844 2844 vdc->dring_hdl = NULL;
2845 2845 bzero(&vdc->dring_mem_info, sizeof (ldc_mem_info_t));
2846 2846 vdc->initialized &= ~VDC_DRING_INIT;
2847 2847 } else {
2848 2848 DMSG(vdc, 0, "[%d] Error %d destroying DRing (%lx)",
2849 2849 vdc->instance, status, vdc->dring_hdl);
2850 2850 }
2851 2851 }
2852 2852 }
2853 2853
2854 2854 /*
2855 2855 * Function:
2856 2856 * vdc_map_to_shared_dring()
2857 2857 *
2858 2858 * Description:
2859 2859 * Copy contents of the local descriptor to the shared
2860 2860 * memory descriptor.
2861 2861 *
2862 2862 * Arguments:
2863 2863 * vdcp - soft state pointer for this instance of the device driver.
2864 2864 * idx - descriptor ring index
2865 2865 *
2866 2866 * Return Code:
2867 2867 * None
2868 2868 */
2869 2869 static int
2870 2870 vdc_map_to_shared_dring(vdc_t *vdcp, int idx)
2871 2871 {
2872 2872 vdc_local_desc_t *ldep;
2873 2873 vd_dring_entry_t *dep;
2874 2874 int rv;
2875 2875
2876 2876 ldep = &(vdcp->local_dring[idx]);
2877 2877
2878 2878 /* for now leave in the old pop_mem_hdl stuff */
2879 2879 if (ldep->nbytes > 0) {
2880 2880 rv = vdc_populate_mem_hdl(vdcp, ldep);
2881 2881 if (rv) {
2882 2882 DMSG(vdcp, 0, "[%d] Cannot populate mem handle\n",
2883 2883 vdcp->instance);
2884 2884 return (rv);
2885 2885 }
2886 2886 }
2887 2887
2888 2888 /*
2889 2889 * fill in the data details into the DRing
2890 2890 */
2891 2891 dep = ldep->dep;
2892 2892 ASSERT(dep != NULL);
2893 2893
2894 2894 dep->payload.req_id = VDC_GET_NEXT_REQ_ID(vdcp);
2895 2895 dep->payload.operation = ldep->operation;
2896 2896 dep->payload.addr = ldep->offset;
2897 2897 dep->payload.nbytes = ldep->nbytes;
2898 2898 dep->payload.status = (uint32_t)-1; /* vds will set valid value */
2899 2899 dep->payload.slice = ldep->slice;
2900 2900 dep->hdr.dstate = VIO_DESC_READY;
2901 2901 dep->hdr.ack = 1; /* request an ACK for every message */
2902 2902
2903 2903 return (0);
2904 2904 }
2905 2905
2906 2906 /*
2907 2907 * Function:
2908 2908 * vdc_send_request
2909 2909 *
2910 2910 * Description:
2911 2911 * This routine writes the data to be transmitted to vds into the
2912 2912 * descriptor, notifies vds that the ring has been updated and
2913 2913 * then waits for the request to be processed.
2914 2914 *
2915 2915 * Arguments:
2916 2916 * vdcp - the soft state pointer
2917 2917 * operation - operation we want vds to perform (VD_OP_XXX)
2918 2918 * addr - address of data buf to be read/written.
2919 2919 * nbytes - number of bytes to read/write
2920 2920 * slice - the disk slice this request is for
2921 2921 * offset - relative disk offset
2922 2922 * bufp - buf of operation
2923 2923 * dir - direction of operation (READ/WRITE/BOTH)
2924 2924 *
2925 2925 * Return Codes:
2926 2926 * 0
2927 2927 * ENXIO
2928 2928 */
2929 2929 static int
2930 2930 vdc_send_request(vdc_t *vdcp, int operation, caddr_t addr,
2931 2931 size_t nbytes, int slice, diskaddr_t offset, buf_t *bufp,
2932 2932 vio_desc_direction_t dir, int flags)
2933 2933 {
2934 2934 int rv = 0;
2935 2935
2936 2936 ASSERT(vdcp != NULL);
2937 2937 ASSERT(slice == VD_SLICE_NONE || slice < V_NUMPAR);
2938 2938
2939 2939 mutex_enter(&vdcp->lock);
2940 2940
2941 2941 /*
2942 2942 * If this is a block read/write operation we update the I/O statistics
2943 2943 * to indicate that the request is being put on the waitq to be
2944 2944 * serviced. Operations which are resubmitted are already in the waitq.
2945 2945 *
2946 2946 * We do it here (a common routine for both synchronous and strategy
2947 2947 * calls) for performance reasons - we are already holding vdc->lock
2948 2948 * so there is no extra locking overhead. We would have to explicitly
2949 2949 * grab the 'lock' mutex to update the stats if we were to do this
2950 2950 * higher up the stack in vdc_strategy() et. al.
2951 2951 */
2952 2952 if (((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) &&
2953 2953 !(flags & VDC_OP_RESUBMIT)) {
2954 2954 DTRACE_IO1(start, buf_t *, bufp);
2955 2955 VD_KSTAT_WAITQ_ENTER(vdcp);
2956 2956 }
2957 2957
2958 2958 /*
2959 2959 * If the request does not expect the state to be VDC_STATE_RUNNING
2960 2960 * then we just try to populate the descriptor ring once.
2961 2961 */
2962 2962 if (!(flags & VDC_OP_STATE_RUNNING)) {
2963 2963 rv = vdc_populate_descriptor(vdcp, operation, addr,
2964 2964 nbytes, slice, offset, bufp, dir, flags);
2965 2965 goto done;
2966 2966 }
2967 2967
2968 2968 do {
2969 2969 while (vdcp->state != VDC_STATE_RUNNING) {
2970 2970
2971 2971 /* return error if detaching */
2972 2972 if (vdcp->state == VDC_STATE_DETACH) {
2973 2973 rv = ENXIO;
2974 2974 goto done;
2975 2975 }
2976 2976
2977 2977 /*
2978 2978 * If we are panicking and the disk is not ready then
2979 2979 * we can't send any request because we can't complete
2980 2980 * the handshake now.
2981 2981 */
2982 2982 if (ddi_in_panic()) {
2983 2983 rv = EIO;
2984 2984 goto done;
2985 2985 }
2986 2986
2987 2987 /*
2988 2988 * If the state is faulted, notify that a new I/O is
2989 2989 * being submitted to force the system to check if any
2990 2990 * server has recovered.
2991 2991 */
2992 2992 if (vdcp->state == VDC_STATE_FAILED) {
2993 2993 vdcp->io_pending = B_TRUE;
2994 2994 cv_signal(&vdcp->io_pending_cv);
2995 2995 }
2996 2996
2997 2997 cv_wait(&vdcp->running_cv, &vdcp->lock);
2998 2998
2999 2999 /* if service is still faulted then fail the request */
3000 3000 if (vdcp->state == VDC_STATE_FAILED) {
3001 3001 rv = EIO;
3002 3002 goto done;
3003 3003 }
3004 3004 }
3005 3005
3006 3006 } while (vdc_populate_descriptor(vdcp, operation, addr,
3007 3007 nbytes, slice, offset, bufp, dir, flags & ~VDC_OP_RESUBMIT));
3008 3008
3009 3009 done:
3010 3010 /*
3011 3011 * If this is a block read/write we update the I/O statistics kstat
3012 3012 * to indicate that this request has been placed on the queue for
3013 3013 * processing (i.e sent to the vDisk server) - iostat(1M) will
3014 3014 * report the time waiting for the vDisk server under the %b column
3015 3015 *
3016 3016 * In the case of an error we take it off the wait queue only if
3017 3017 * the I/O was not resubmited.
3018 3018 */
3019 3019 if ((operation == VD_OP_BREAD) || (operation == VD_OP_BWRITE)) {
3020 3020 if (rv == 0) {
3021 3021 VD_KSTAT_WAITQ_TO_RUNQ(vdcp);
3022 3022 DTRACE_PROBE1(send, buf_t *, bufp);
3023 3023 } else {
3024 3024 VD_UPDATE_ERR_STATS(vdcp, vd_transerrs);
3025 3025 if (!(flags & VDC_OP_RESUBMIT)) {
3026 3026 VD_KSTAT_WAITQ_EXIT(vdcp);
3027 3027 DTRACE_IO1(done, buf_t *, bufp);
3028 3028 }
3029 3029 }
3030 3030 }
3031 3031
3032 3032 mutex_exit(&vdcp->lock);
3033 3033
3034 3034 return (rv);
3035 3035 }
3036 3036
3037 3037
3038 3038 /*
3039 3039 * Function:
3040 3040 * vdc_populate_descriptor
3041 3041 *
3042 3042 * Description:
3043 3043 * This routine writes the data to be transmitted to vds into the
3044 3044 * descriptor, notifies vds that the ring has been updated and
3045 3045 * then waits for the request to be processed.
3046 3046 *
3047 3047 * Arguments:
3048 3048 * vdcp - the soft state pointer
3049 3049 * operation - operation we want vds to perform (VD_OP_XXX)
3050 3050 * addr - address of data buf to be read/written.
3051 3051 * nbytes - number of bytes to read/write
3052 3052 * slice - the disk slice this request is for
3053 3053 * offset - relative disk offset
3054 3054 * bufp - buf of operation
3055 3055 * dir - direction of operation (READ/WRITE/BOTH)
3056 3056 *
3057 3057 * Return Codes:
3058 3058 * 0
3059 3059 * EAGAIN
3060 3060 * ECONNRESET
3061 3061 * ENXIO
3062 3062 */
3063 3063 static int
3064 3064 vdc_populate_descriptor(vdc_t *vdcp, int operation, caddr_t addr,
3065 3065 size_t nbytes, int slice, diskaddr_t offset,
3066 3066 buf_t *bufp, vio_desc_direction_t dir, int flags)
3067 3067 {
3068 3068 vdc_local_desc_t *local_dep = NULL; /* Local Dring Pointer */
3069 3069 int idx; /* Index of DRing entry used */
3070 3070 int next_idx;
3071 3071 vio_dring_msg_t dmsg;
3072 3072 size_t msglen;
3073 3073 int rv;
3074 3074
3075 3075 ASSERT(MUTEX_HELD(&vdcp->lock));
3076 3076 vdcp->threads_pending++;
3077 3077 loop:
3078 3078 DMSG(vdcp, 2, ": dring_curr_idx = %d\n", vdcp->dring_curr_idx);
3079 3079
3080 3080 if (flags & VDC_OP_DRING_RESERVED) {
3081 3081 /* use D-Ring reserved entry */
3082 3082 idx = VDC_DRING_FIRST_RESV;
3083 3083 local_dep = &(vdcp->local_dring[idx]);
3084 3084 } else {
3085 3085 /* Get next available D-Ring entry */
3086 3086 idx = vdcp->dring_curr_idx;
3087 3087 local_dep = &(vdcp->local_dring[idx]);
3088 3088
3089 3089 if (!local_dep->is_free) {
3090 3090 DMSG(vdcp, 2, "[%d]: dring full - waiting for space\n",
3091 3091 vdcp->instance);
3092 3092 cv_wait(&vdcp->dring_free_cv, &vdcp->lock);
3093 3093 if (vdcp->state == VDC_STATE_RUNNING ||
3094 3094 vdcp->state == VDC_STATE_HANDLE_PENDING) {
3095 3095 goto loop;
3096 3096 }
3097 3097 vdcp->threads_pending--;
3098 3098 return (ECONNRESET);
3099 3099 }
3100 3100
3101 3101 next_idx = idx + 1;
3102 3102 if (next_idx >= vdcp->dring_len)
3103 3103 next_idx = VDC_DRING_FIRST_ENTRY;
3104 3104 vdcp->dring_curr_idx = next_idx;
3105 3105 }
3106 3106
3107 3107 ASSERT(local_dep->is_free);
3108 3108
3109 3109 local_dep->operation = operation;
3110 3110 local_dep->addr = addr;
3111 3111 local_dep->nbytes = nbytes;
3112 3112 local_dep->slice = slice;
3113 3113 local_dep->offset = offset;
3114 3114 local_dep->buf = bufp;
3115 3115 local_dep->dir = dir;
3116 3116 local_dep->flags = flags;
3117 3117
3118 3118 local_dep->is_free = B_FALSE;
3119 3119
3120 3120 rv = vdc_map_to_shared_dring(vdcp, idx);
3121 3121 if (rv) {
3122 3122 if (flags & VDC_OP_DRING_RESERVED) {
3123 3123 DMSG(vdcp, 0, "[%d]: cannot bind memory - error\n",
3124 3124 vdcp->instance);
3125 3125 /*
3126 3126 * We can't wait if we are using reserved slot.
3127 3127 * Free the descriptor and return.
3128 3128 */
3129 3129 local_dep->is_free = B_TRUE;
3130 3130 vdcp->threads_pending--;
3131 3131 return (rv);
3132 3132 }
3133 3133 DMSG(vdcp, 0, "[%d]: cannot bind memory - waiting ..\n",
3134 3134 vdcp->instance);
3135 3135 /* free the descriptor */
3136 3136 local_dep->is_free = B_TRUE;
3137 3137 vdcp->dring_curr_idx = idx;
3138 3138 cv_wait(&vdcp->membind_cv, &vdcp->lock);
3139 3139 if (vdcp->state == VDC_STATE_RUNNING ||
3140 3140 vdcp->state == VDC_STATE_HANDLE_PENDING) {
3141 3141 goto loop;
3142 3142 }
3143 3143 vdcp->threads_pending--;
3144 3144 return (ECONNRESET);
3145 3145 }
3146 3146
3147 3147 /*
3148 3148 * Send a msg with the DRing details to vds
3149 3149 */
3150 3150 VIO_INIT_DRING_DATA_TAG(dmsg);
3151 3151 VDC_INIT_DRING_DATA_MSG_IDS(dmsg, vdcp);
3152 3152 dmsg.dring_ident = vdcp->dring_ident;
3153 3153 dmsg.start_idx = idx;
3154 3154 dmsg.end_idx = idx;
3155 3155 vdcp->seq_num++;
3156 3156
3157 3157 DTRACE_PROBE2(populate, int, vdcp->instance,
3158 3158 vdc_local_desc_t *, local_dep);
3159 3159 DMSG(vdcp, 2, "ident=0x%lx, st=%u, end=%u, seq=%ld\n",
3160 3160 vdcp->dring_ident, dmsg.start_idx, dmsg.end_idx, dmsg.seq_num);
3161 3161
3162 3162 /*
3163 3163 * note we're still holding the lock here to
3164 3164 * make sure the message goes out in order !!!...
3165 3165 */
3166 3166 msglen = sizeof (dmsg);
3167 3167 rv = vdc_send(vdcp, (caddr_t)&dmsg, &msglen);
3168 3168 switch (rv) {
3169 3169 case ECONNRESET:
3170 3170 /*
3171 3171 * vdc_send initiates the reset on failure.
3172 3172 * Since the transaction has already been put
3173 3173 * on the local dring, it will automatically get
3174 3174 * retried when the channel is reset. Given that,
3175 3175 * it is ok to just return success even though the
3176 3176 * send failed.
3177 3177 */
3178 3178 rv = 0;
3179 3179 break;
3180 3180
3181 3181 case 0: /* EOK */
3182 3182 DMSG(vdcp, 1, "sent via LDC: rv=%d\n", rv);
3183 3183 break;
3184 3184
3185 3185 default:
3186 3186 DMSG(vdcp, 0, "unexpected error, rv=%d\n", rv);
3187 3187 rv = ENXIO;
3188 3188 break;
3189 3189 }
3190 3190
3191 3191 vdcp->threads_pending--;
3192 3192 return (rv);
3193 3193 }
3194 3194
3195 3195 /*
3196 3196 * Function:
3197 3197 * vdc_do_op
3198 3198 *
3199 3199 * Description:
3200 3200 * Wrapper around vdc_submit_request(). Each request is associated with a
3201 3201 * buf structure. If a buf structure is provided (bufp != NULL) then the
3202 3202 * request will be submitted with that buf, and the caller can wait for
3203 3203 * completion of the request with biowait(). If a buf structure is not
3204 3204 * provided (bufp == NULL) then a buf structure is created and the function
3205 3205 * waits for the completion of the request.
3206 3206 *
3207 3207 * If the flag VD_OP_STATE_RUNNING is set then vdc_submit_request() will
3208 3208 * submit the request only when the vdisk is in state VD_STATE_RUNNING.
3209 3209 * If the vdisk is not in that state then the vdc_submit_request() will
3210 3210 * wait for that state to be reached. After the request is submitted, the
3211 3211 * reply will be processed asynchronously by the vdc_process_msg_thread()
3212 3212 * thread.
3213 3213 *
3214 3214 * If the flag VD_OP_STATE_RUNNING is not set then vdc_submit_request()
3215 3215 * submit the request whatever the state of the vdisk is. Then vdc_do_op()
3216 3216 * will wait for a reply message, process the reply and complete the
3217 3217 * request.
3218 3218 *
3219 3219 * Arguments:
3220 3220 * vdc - the soft state pointer
3221 3221 * op - operation we want vds to perform (VD_OP_XXX)
3222 3222 * addr - address of data buf to be read/written.
3223 3223 * nbytes - number of bytes to read/write
3224 3224 * slice - the disk slice this request is for
3225 3225 * offset - relative disk offset
3226 3226 * bufp - buf structure associated with the request (can be NULL).
3227 3227 * dir - direction of operation (READ/WRITE/BOTH)
3228 3228 * flags - flags for the request.
3229 3229 *
3230 3230 * Return Codes:
3231 3231 * 0 - the request has been succesfully submitted and completed.
3232 3232 * != 0 - the request has failed. In that case, if a buf structure
3233 3233 * was provided (bufp != NULL) then the B_ERROR flag is set
3234 3234 * and the b_error field of the buf structure is set to EIO.
3235 3235 */
3236 3236 static int
3237 3237 vdc_do_op(vdc_t *vdc, int op, caddr_t addr, size_t nbytes, int slice,
3238 3238 diskaddr_t offset, struct buf *bufp, vio_desc_direction_t dir, int flags)
3239 3239 {
3240 3240 vio_msg_t vio_msg;
3241 3241 struct buf buf;
3242 3242 int rv;
3243 3243
3244 3244 if (bufp == NULL) {
3245 3245 /*
3246 3246 * We use buf just as a convenient way to get a notification
3247 3247 * that the request is completed, so we initialize buf to the
3248 3248 * minimum we need.
3249 3249 */
3250 3250 bioinit(&buf);
3251 3251 buf.b_bcount = nbytes;
3252 3252 buf.b_flags = B_BUSY;
3253 3253 bufp = &buf;
3254 3254 }
3255 3255
3256 3256 rv = vdc_send_request(vdc, op, addr, nbytes, slice, offset, bufp,
3257 3257 dir, flags);
3258 3258
3259 3259 if (rv != 0)
3260 3260 goto done;
3261 3261
3262 3262 /*
3263 3263 * If the request should be done in VDC_STATE_RUNNING state then the
3264 3264 * reply will be received and processed by vdc_process_msg_thread()
3265 3265 * and we just have to handle the panic case. Otherwise we have to
3266 3266 * wait for the reply message and process it.
3267 3267 */
3268 3268 if (flags & VDC_OP_STATE_RUNNING) {
3269 3269
3270 3270 if (ddi_in_panic()) {
3271 3271 rv = vdc_drain_response(vdc, bufp);
3272 3272 goto done;
3273 3273 }
3274 3274
3275 3275 } else {
3276 3276 /* wait for the response message */
3277 3277 rv = vdc_wait_for_response(vdc, &vio_msg);
3278 3278
3279 3279 if (rv == 0)
3280 3280 rv = vdc_process_data_msg(vdc, &vio_msg);
3281 3281
3282 3282 if (rv) {
3283 3283 /*
3284 3284 * If this is a block read/write we update the I/O
3285 3285 * statistics kstat to take it off the run queue.
3286 3286 * If it is a resubmit then it needs to stay in
3287 3287 * in the waitq, and it will be removed when the
3288 3288 * I/O is eventually completed or cancelled.
3289 3289 */
3290 3290 mutex_enter(&vdc->lock);
3291 3291 if (op == VD_OP_BREAD || op == VD_OP_BWRITE) {
3292 3292 if (flags & VDC_OP_RESUBMIT) {
3293 3293 VD_KSTAT_RUNQ_BACK_TO_WAITQ(vdc);
3294 3294 } else {
3295 3295 VD_KSTAT_RUNQ_EXIT(vdc);
3296 3296 DTRACE_IO1(done, buf_t *, bufp);
3297 3297 }
3298 3298 }
3299 3299 mutex_exit(&vdc->lock);
3300 3300 goto done;
3301 3301 }
3302 3302
3303 3303 }
3304 3304
3305 3305 if (bufp == &buf)
3306 3306 rv = biowait(bufp);
3307 3307
3308 3308 done:
3309 3309 if (bufp == &buf) {
3310 3310 biofini(bufp);
3311 3311 } else if (rv != 0) {
3312 3312 bioerror(bufp, EIO);
3313 3313 biodone(bufp);
3314 3314 }
3315 3315
3316 3316 return (rv);
3317 3317 }
3318 3318
3319 3319 /*
3320 3320 * Function:
3321 3321 * vdc_do_sync_op
3322 3322 *
3323 3323 * Description:
3324 3324 * Wrapper around vdc_do_op that serializes requests.
3325 3325 *
3326 3326 * Arguments:
3327 3327 * vdcp - the soft state pointer
3328 3328 * operation - operation we want vds to perform (VD_OP_XXX)
3329 3329 * addr - address of data buf to be read/written.
3330 3330 * nbytes - number of bytes to read/write
3331 3331 * slice - the disk slice this request is for
3332 3332 * offset - relative disk offset
3333 3333 * dir - direction of operation (READ/WRITE/BOTH)
3334 3334 * rconflict - check for reservation conflict in case of failure
3335 3335 *
3336 3336 * rconflict should be set to B_TRUE by most callers. Callers invoking the
3337 3337 * VD_OP_SCSICMD operation can set rconflict to B_FALSE if they check the
3338 3338 * result of a successful operation with vdc_scsi_status().
3339 3339 *
3340 3340 * Return Codes:
3341 3341 * 0
3342 3342 * EAGAIN
3343 3343 * EFAULT
3344 3344 * ENXIO
3345 3345 * EIO
3346 3346 */
3347 3347 static int
3348 3348 vdc_do_sync_op(vdc_t *vdcp, int operation, caddr_t addr, size_t nbytes,
3349 3349 int slice, diskaddr_t offset, vio_desc_direction_t dir, boolean_t rconflict)
3350 3350 {
3351 3351 int status;
3352 3352 int flags = VDC_OP_NORMAL;
3353 3353
3354 3354 /*
3355 3355 * Grab the lock, if blocked wait until the server
3356 3356 * response causes us to wake up again.
3357 3357 */
3358 3358 mutex_enter(&vdcp->lock);
3359 3359 vdcp->sync_op_cnt++;
3360 3360 while (vdcp->sync_op_blocked && vdcp->state != VDC_STATE_DETACH) {
3361 3361 if (ddi_in_panic()) {
3362 3362 /* don't block if we are panicking */
3363 3363 vdcp->sync_op_cnt--;
3364 3364 mutex_exit(&vdcp->lock);
3365 3365 return (EIO);
3366 3366 } else {
3367 3367 cv_wait(&vdcp->sync_blocked_cv, &vdcp->lock);
3368 3368 }
3369 3369 }
3370 3370
3371 3371 if (vdcp->state == VDC_STATE_DETACH) {
3372 3372 cv_broadcast(&vdcp->sync_blocked_cv);
3373 3373 vdcp->sync_op_cnt--;
3374 3374 mutex_exit(&vdcp->lock);
3375 3375 return (ENXIO);
3376 3376 }
3377 3377
3378 3378 /* now block anyone other thread entering after us */
3379 3379 vdcp->sync_op_blocked = B_TRUE;
3380 3380
3381 3381 mutex_exit(&vdcp->lock);
3382 3382
3383 3383 if (!rconflict)
3384 3384 flags &= ~VDC_OP_ERRCHK_CONFLICT;
3385 3385
3386 3386 status = vdc_do_op(vdcp, operation, addr, nbytes, slice, offset,
3387 3387 NULL, dir, flags);
3388 3388
3389 3389 mutex_enter(&vdcp->lock);
3390 3390
3391 3391 DMSG(vdcp, 2, ": operation returned %d\n", status);
3392 3392
3393 3393 if (vdcp->state == VDC_STATE_DETACH) {
3394 3394 status = ENXIO;
3395 3395 }
3396 3396
3397 3397 vdcp->sync_op_blocked = B_FALSE;
3398 3398 vdcp->sync_op_cnt--;
3399 3399
3400 3400 /* signal the next waiting thread */
3401 3401 cv_signal(&vdcp->sync_blocked_cv);
3402 3402
3403 3403 mutex_exit(&vdcp->lock);
3404 3404
3405 3405 return (status);
3406 3406 }
3407 3407
3408 3408
3409 3409 /*
3410 3410 * Function:
3411 3411 * vdc_drain_response()
3412 3412 *
3413 3413 * Description:
3414 3414 * When a guest is panicking, the completion of requests needs to be
3415 3415 * handled differently because interrupts are disabled and vdc
3416 3416 * will not get messages. We have to poll for the messages instead.
3417 3417 *
3418 3418 * Note: since we are panicking we don't implement the io:::done
3419 3419 * DTrace probe or update the I/O statistics kstats.
3420 3420 *
3421 3421 * Arguments:
3422 3422 * vdc - soft state pointer for this instance of the device driver.
3423 3423 * buf - if buf is NULL then we drain all responses, otherwise we
3424 3424 * poll until we receive a ACK/NACK for the specific I/O
3425 3425 * described by buf.
3426 3426 *
3427 3427 * Return Code:
3428 3428 * 0 - Success. If we were expecting a response to a particular
3429 3429 * request then this means that a response has been received.
3430 3430 */
3431 3431 static int
3432 3432 vdc_drain_response(vdc_t *vdc, struct buf *buf)
3433 3433 {
3434 3434 int rv, idx, retries;
3435 3435 size_t msglen;
3436 3436 vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */
3437 3437 vio_dring_msg_t dmsg;
3438 3438 struct buf *mbuf;
3439 3439 boolean_t ack;
3440 3440
3441 3441 mutex_enter(&vdc->lock);
3442 3442
3443 3443 retries = 0;
3444 3444 for (;;) {
3445 3445 msglen = sizeof (dmsg);
3446 3446 rv = ldc_read(vdc->curr_server->ldc_handle, (caddr_t)&dmsg,
3447 3447 &msglen);
3448 3448 if (rv) {
3449 3449 rv = EINVAL;
3450 3450 break;
3451 3451 }
3452 3452
3453 3453 /*
3454 3454 * if there are no packets wait and check again
3455 3455 */
3456 3456 if ((rv == 0) && (msglen == 0)) {
3457 3457 if (retries++ > vdc_dump_retries) {
3458 3458 rv = EAGAIN;
3459 3459 break;
3460 3460 }
3461 3461
3462 3462 drv_usecwait(vdc_usec_timeout_dump);
3463 3463 continue;
3464 3464 }
3465 3465
3466 3466 /*
3467 3467 * Ignore all messages that are not ACKs/NACKs to
3468 3468 * DRing requests.
3469 3469 */
3470 3470 if ((dmsg.tag.vio_msgtype != VIO_TYPE_DATA) ||
3471 3471 (dmsg.tag.vio_subtype_env != VIO_DRING_DATA)) {
3472 3472 DMSG(vdc, 0, "discard pkt: type=%d sub=%d env=%d\n",
3473 3473 dmsg.tag.vio_msgtype,
3474 3474 dmsg.tag.vio_subtype,
3475 3475 dmsg.tag.vio_subtype_env);
3476 3476 continue;
3477 3477 }
3478 3478
3479 3479 /*
3480 3480 * Record if the packet was ACK'ed or not. If the packet was not
3481 3481 * ACK'ed then we will just mark the request as failed; we don't
3482 3482 * want to reset the connection at this point.
3483 3483 */
3484 3484 switch (dmsg.tag.vio_subtype) {
3485 3485 case VIO_SUBTYPE_ACK:
3486 3486 ack = B_TRUE;
3487 3487 break;
3488 3488 case VIO_SUBTYPE_NACK:
3489 3489 ack = B_FALSE;
3490 3490 break;
3491 3491 default:
3492 3492 continue;
3493 3493 }
3494 3494
3495 3495 idx = dmsg.start_idx;
3496 3496 if (idx >= vdc->dring_len) {
3497 3497 DMSG(vdc, 0, "[%d] Bogus ack data : start %d\n",
3498 3498 vdc->instance, idx);
3499 3499 continue;
3500 3500 }
3501 3501 ldep = &vdc->local_dring[idx];
3502 3502 if (ldep->dep->hdr.dstate != VIO_DESC_DONE) {
3503 3503 DMSG(vdc, 0, "[%d] Entry @ %d - state !DONE %d\n",
3504 3504 vdc->instance, idx, ldep->dep->hdr.dstate);
3505 3505 continue;
3506 3506 }
3507 3507
3508 3508 mbuf = ldep->buf;
3509 3509 ASSERT(mbuf != NULL);
3510 3510 mbuf->b_resid = mbuf->b_bcount - ldep->dep->payload.nbytes;
3511 3511 bioerror(mbuf, ack ? ldep->dep->payload.status : EIO);
3512 3512 biodone(mbuf);
3513 3513
3514 3514 rv = vdc_depopulate_descriptor(vdc, idx);
3515 3515 if (buf != NULL && buf == mbuf) {
3516 3516 rv = 0;
3517 3517 goto done;
3518 3518 }
3519 3519
3520 3520 /* if this is the last descriptor - break out of loop */
3521 3521 if ((idx + 1) % vdc->dring_len == vdc->dring_curr_idx) {
3522 3522 /*
3523 3523 * If we were expecting a response for a particular
3524 3524 * request then we return with an error otherwise we
3525 3525 * have successfully completed the drain.
3526 3526 */
3527 3527 rv = (buf != NULL)? ESRCH: 0;
3528 3528 break;
3529 3529 }
3530 3530 }
3531 3531
3532 3532 done:
3533 3533 mutex_exit(&vdc->lock);
3534 3534 DMSG(vdc, 0, "End idx=%d\n", idx);
3535 3535
3536 3536 return (rv);
3537 3537 }
3538 3538
3539 3539
3540 3540 /*
3541 3541 * Function:
3542 3542 * vdc_depopulate_descriptor()
3543 3543 *
3544 3544 * Description:
3545 3545 *
3546 3546 * Arguments:
3547 3547 * vdc - soft state pointer for this instance of the device driver.
3548 3548 * idx - Index of the Descriptor Ring entry being modified
3549 3549 *
3550 3550 * Return Code:
3551 3551 * 0 - Success
3552 3552 */
3553 3553 static int
3554 3554 vdc_depopulate_descriptor(vdc_t *vdc, uint_t idx)
3555 3555 {
3556 3556 vd_dring_entry_t *dep = NULL; /* Dring Entry Pointer */
3557 3557 vdc_local_desc_t *ldep = NULL; /* Local Dring Entry Pointer */
3558 3558 int status = ENXIO;
3559 3559 int rv = 0;
3560 3560
3561 3561 ASSERT(vdc != NULL);
3562 3562 ASSERT(idx < vdc->dring_len);
3563 3563 ldep = &vdc->local_dring[idx];
3564 3564 ASSERT(ldep != NULL);
3565 3565 ASSERT(MUTEX_HELD(&vdc->lock));
3566 3566
3567 3567 DTRACE_PROBE2(depopulate, int, vdc->instance, vdc_local_desc_t *, ldep);
3568 3568 DMSG(vdc, 2, ": idx = %d\n", idx);
3569 3569
3570 3570 dep = ldep->dep;
3571 3571 ASSERT(dep != NULL);
3572 3572 ASSERT((dep->hdr.dstate == VIO_DESC_DONE) ||
3573 3573 (dep->payload.status == ECANCELED));
3574 3574
3575 3575 VDC_MARK_DRING_ENTRY_FREE(vdc, idx);
3576 3576
3577 3577 ldep->is_free = B_TRUE;
3578 3578 status = dep->payload.status;
3579 3579 DMSG(vdc, 2, ": is_free = %d : status = %d\n", ldep->is_free, status);
3580 3580
3581 3581 /*
3582 3582 * If no buffers were used to transfer information to the server when
3583 3583 * populating the descriptor then no memory handles need to be unbound
3584 3584 * and we can return now.
3585 3585 */
3586 3586 if (ldep->nbytes == 0) {
3587 3587 cv_signal(&vdc->dring_free_cv);
3588 3588 return (status);
3589 3589 }
3590 3590
3591 3591 /*
3592 3592 * If the upper layer passed in a misaligned address we copied the
3593 3593 * data into an aligned buffer before sending it to LDC - we now
3594 3594 * copy it back to the original buffer.
3595 3595 */
3596 3596 if (ldep->align_addr) {
3597 3597 ASSERT(ldep->addr != NULL);
3598 3598
3599 3599 if (dep->payload.nbytes > 0)
3600 3600 bcopy(ldep->align_addr, ldep->addr,
3601 3601 dep->payload.nbytes);
3602 3602 kmem_free(ldep->align_addr,
3603 3603 sizeof (caddr_t) * P2ROUNDUP(ldep->nbytes, 8));
3604 3604 ldep->align_addr = NULL;
3605 3605 }
3606 3606
3607 3607 rv = ldc_mem_unbind_handle(ldep->desc_mhdl);
3608 3608 if (rv != 0) {
3609 3609 DMSG(vdc, 0, "?[%d] unbind mhdl 0x%lx @ idx %d failed (%d)",
3610 3610 vdc->instance, ldep->desc_mhdl, idx, rv);
3611 3611 /*
3612 3612 * The error returned by the vDisk server is more informative
3613 3613 * and thus has a higher priority but if it isn't set we ensure
3614 3614 * that this function returns an error.
3615 3615 */
3616 3616 if (status == 0)
3617 3617 status = EINVAL;
3618 3618 }
3619 3619
3620 3620 cv_signal(&vdc->membind_cv);
3621 3621 cv_signal(&vdc->dring_free_cv);
3622 3622
3623 3623 return (status);
3624 3624 }
3625 3625
3626 3626 /*
3627 3627 * Function:
3628 3628 * vdc_populate_mem_hdl()
3629 3629 *
3630 3630 * Description:
3631 3631 *
3632 3632 * Arguments:
3633 3633 * vdc - soft state pointer for this instance of the device driver.
3634 3634 * idx - Index of the Descriptor Ring entry being modified
3635 3635 * addr - virtual address being mapped in
3636 3636 * nybtes - number of bytes in 'addr'
3637 3637 * operation - the vDisk operation being performed (VD_OP_xxx)
3638 3638 *
3639 3639 * Return Code:
3640 3640 * 0 - Success
3641 3641 */
3642 3642 static int
3643 3643 vdc_populate_mem_hdl(vdc_t *vdcp, vdc_local_desc_t *ldep)
3644 3644 {
3645 3645 vd_dring_entry_t *dep = NULL;
3646 3646 ldc_mem_handle_t mhdl;
3647 3647 caddr_t vaddr;
3648 3648 size_t nbytes;
3649 3649 uint8_t perm = LDC_MEM_RW;
3650 3650 uint8_t maptype;
3651 3651 int rv = 0;
3652 3652 int i;
3653 3653
3654 3654 ASSERT(vdcp != NULL);
3655 3655
3656 3656 dep = ldep->dep;
3657 3657 mhdl = ldep->desc_mhdl;
3658 3658
3659 3659 switch (ldep->dir) {
3660 3660 case VIO_read_dir:
3661 3661 perm = LDC_MEM_W;
3662 3662 break;
3663 3663
3664 3664 case VIO_write_dir:
3665 3665 perm = LDC_MEM_R;
3666 3666 break;
3667 3667
3668 3668 case VIO_both_dir:
3669 3669 perm = LDC_MEM_RW;
3670 3670 break;
3671 3671
3672 3672 default:
3673 3673 ASSERT(0); /* catch bad programming in vdc */
3674 3674 }
3675 3675
3676 3676 /*
3677 3677 * LDC expects any addresses passed in to be 8-byte aligned. We need
3678 3678 * to copy the contents of any misaligned buffers to a newly allocated
3679 3679 * buffer and bind it instead (and copy the the contents back to the
3680 3680 * original buffer passed in when depopulating the descriptor)
3681 3681 */
3682 3682 vaddr = ldep->addr;
3683 3683 nbytes = ldep->nbytes;
3684 3684 if (((uint64_t)vaddr & 0x7) != 0) {
3685 3685 ASSERT(ldep->align_addr == NULL);
3686 3686 ldep->align_addr =
3687 3687 kmem_alloc(sizeof (caddr_t) *
3688 3688 P2ROUNDUP(nbytes, 8), KM_SLEEP);
3689 3689 DMSG(vdcp, 0, "[%d] Misaligned address %p reallocating "
3690 3690 "(buf=%p nb=%ld op=%d)\n",
3691 3691 vdcp->instance, (void *)vaddr, (void *)ldep->align_addr,
3692 3692 nbytes, ldep->operation);
3693 3693 if (perm != LDC_MEM_W)
3694 3694 bcopy(vaddr, ldep->align_addr, nbytes);
3695 3695 vaddr = ldep->align_addr;
3696 3696 }
3697 3697
3698 3698 maptype = LDC_IO_MAP|LDC_SHADOW_MAP;
3699 3699 rv = ldc_mem_bind_handle(mhdl, vaddr, P2ROUNDUP(nbytes, 8),
3700 3700 maptype, perm, &dep->payload.cookie[0], &dep->payload.ncookies);
3701 3701 DMSG(vdcp, 2, "[%d] bound mem handle; ncookies=%d\n",
3702 3702 vdcp->instance, dep->payload.ncookies);
3703 3703 if (rv != 0) {
3704 3704 DMSG(vdcp, 0, "[%d] Failed to bind LDC memory handle "
3705 3705 "(mhdl=%p, buf=%p, err=%d)\n",
3706 3706 vdcp->instance, (void *)mhdl, (void *)vaddr, rv);
3707 3707 if (ldep->align_addr) {
3708 3708 kmem_free(ldep->align_addr,
3709 3709 sizeof (caddr_t) * P2ROUNDUP(nbytes, 8));
3710 3710 ldep->align_addr = NULL;
3711 3711 }
3712 3712 return (EAGAIN);
3713 3713 }
3714 3714
3715 3715 /*
3716 3716 * Get the other cookies (if any).
3717 3717 */
3718 3718 for (i = 1; i < dep->payload.ncookies; i++) {
3719 3719 rv = ldc_mem_nextcookie(mhdl, &dep->payload.cookie[i]);
3720 3720 if (rv != 0) {
3721 3721 (void) ldc_mem_unbind_handle(mhdl);
3722 3722 DMSG(vdcp, 0, "?[%d] Failed to get next cookie "
3723 3723 "(mhdl=%lx cnum=%d), err=%d",
3724 3724 vdcp->instance, mhdl, i, rv);
3725 3725 if (ldep->align_addr) {
3726 3726 kmem_free(ldep->align_addr,
3727 3727 sizeof (caddr_t) * ldep->nbytes);
3728 3728 ldep->align_addr = NULL;
3729 3729 }
3730 3730 return (EAGAIN);
3731 3731 }
3732 3732 }
3733 3733
3734 3734 return (rv);
3735 3735 }
3736 3736
3737 3737 /*
3738 3738 * Interrupt handlers for messages from LDC
3739 3739 */
3740 3740
3741 3741 /*
3742 3742 * Function:
3743 3743 * vdc_handle_cb()
3744 3744 *
3745 3745 * Description:
3746 3746 *
3747 3747 * Arguments:
3748 3748 * event - Type of event (LDC_EVT_xxx) that triggered the callback
3749 3749 * arg - soft state pointer for this instance of the device driver.
3750 3750 *
3751 3751 * Return Code:
3752 3752 * 0 - Success
3753 3753 */
3754 3754 static uint_t
3755 3755 vdc_handle_cb(uint64_t event, caddr_t arg)
3756 3756 {
3757 3757 ldc_status_t ldc_state;
3758 3758 int rv = 0;
3759 3759 vdc_server_t *srvr = (vdc_server_t *)(void *)arg;
3760 3760 vdc_t *vdc = srvr->vdcp;
3761 3761
3762 3762 ASSERT(vdc != NULL);
3763 3763
3764 3764 DMSG(vdc, 1, "evt=%lx seqID=%ld\n", event, vdc->seq_num);
3765 3765
3766 3766 /* If callback is not for the current server, ignore it */
3767 3767 mutex_enter(&vdc->lock);
3768 3768
3769 3769 if (vdc->curr_server != srvr) {
3770 3770 DMSG(vdc, 0, "[%d] Ignoring event 0x%lx for port@%ld\n",
3771 3771 vdc->instance, event, srvr->id);
3772 3772 mutex_exit(&vdc->lock);
3773 3773 return (LDC_SUCCESS);
3774 3774 }
3775 3775
3776 3776 /*
3777 3777 * Depending on the type of event that triggered this callback,
3778 3778 * we modify the handshake state or read the data.
3779 3779 *
3780 3780 * NOTE: not done as a switch() as event could be triggered by
3781 3781 * a state change and a read request. Also the ordering of the
3782 3782 * check for the event types is deliberate.
3783 3783 */
3784 3784 if (event & LDC_EVT_UP) {
3785 3785 DMSG(vdc, 0, "[%d] Received LDC_EVT_UP\n", vdc->instance);
3786 3786
3787 3787 /* get LDC state */
3788 3788 rv = ldc_status(srvr->ldc_handle, &ldc_state);
3789 3789 if (rv != 0) {
3790 3790 DMSG(vdc, 0, "[%d] Couldn't get LDC status %d",
3791 3791 vdc->instance, rv);
3792 3792 mutex_exit(&vdc->lock);
3793 3793 return (LDC_SUCCESS);
3794 3794 }
3795 3795 if (srvr->ldc_state != LDC_UP &&
3796 3796 ldc_state == LDC_UP) {
3797 3797 /*
3798 3798 * Reset the transaction sequence numbers when
3799 3799 * LDC comes up. We then kick off the handshake
3800 3800 * negotiation with the vDisk server.
3801 3801 */
3802 3802 vdc->seq_num = 1;
3803 3803 vdc->seq_num_reply = 0;
3804 3804 vdc->io_pending = B_TRUE;
3805 3805 srvr->ldc_state = ldc_state;
3806 3806 cv_signal(&vdc->initwait_cv);
3807 3807 cv_signal(&vdc->io_pending_cv);
3808 3808 }
3809 3809 }
3810 3810
3811 3811 if (event & LDC_EVT_READ) {
3812 3812 DMSG(vdc, 1, "[%d] Received LDC_EVT_READ\n", vdc->instance);
3813 3813 mutex_enter(&vdc->read_lock);
3814 3814 cv_signal(&vdc->read_cv);
3815 3815 vdc->read_state = VDC_READ_PENDING;
3816 3816 mutex_exit(&vdc->read_lock);
3817 3817 mutex_exit(&vdc->lock);
3818 3818
3819 3819 /* that's all we have to do - no need to handle DOWN/RESET */
3820 3820 return (LDC_SUCCESS);
3821 3821 }
3822 3822
3823 3823 if (event & (LDC_EVT_RESET|LDC_EVT_DOWN)) {
3824 3824
3825 3825 DMSG(vdc, 0, "[%d] Received LDC RESET event\n", vdc->instance);
3826 3826
3827 3827 /*
3828 3828 * Need to wake up any readers so they will
3829 3829 * detect that a reset has occurred.
3830 3830 */
3831 3831 mutex_enter(&vdc->read_lock);
3832 3832 if ((vdc->read_state == VDC_READ_WAITING) ||
3833 3833 (vdc->read_state == VDC_READ_RESET))
3834 3834 cv_signal(&vdc->read_cv);
3835 3835 vdc->read_state = VDC_READ_RESET;
3836 3836 mutex_exit(&vdc->read_lock);
3837 3837
3838 3838 /* wake up any threads waiting for connection to come up */
3839 3839 if (vdc->state == VDC_STATE_INIT_WAITING) {
3840 3840 vdc->state = VDC_STATE_RESETTING;
3841 3841 cv_signal(&vdc->initwait_cv);
3842 3842 } else if (vdc->state == VDC_STATE_FAILED) {
3843 3843 vdc->io_pending = B_TRUE;
3844 3844 cv_signal(&vdc->io_pending_cv);
3845 3845 }
3846 3846
3847 3847 }
3848 3848
3849 3849 mutex_exit(&vdc->lock);
3850 3850
3851 3851 if (event & ~(LDC_EVT_UP | LDC_EVT_RESET | LDC_EVT_DOWN | LDC_EVT_READ))
3852 3852 DMSG(vdc, 0, "![%d] Unexpected LDC event (%lx) received",
3853 3853 vdc->instance, event);
3854 3854
3855 3855 return (LDC_SUCCESS);
3856 3856 }
3857 3857
3858 3858 /*
3859 3859 * Function:
3860 3860 * vdc_wait_for_response()
3861 3861 *
3862 3862 * Description:
3863 3863 * Block waiting for a response from the server. If there is
3864 3864 * no data the thread block on the read_cv that is signalled
3865 3865 * by the callback when an EVT_READ occurs.
3866 3866 *
3867 3867 * Arguments:
3868 3868 * vdcp - soft state pointer for this instance of the device driver.
3869 3869 *
3870 3870 * Return Code:
3871 3871 * 0 - Success
3872 3872 */
3873 3873 static int
3874 3874 vdc_wait_for_response(vdc_t *vdcp, vio_msg_t *msgp)
3875 3875 {
3876 3876 size_t nbytes = sizeof (*msgp);
3877 3877 int status;
3878 3878
3879 3879 ASSERT(vdcp != NULL);
3880 3880
3881 3881 DMSG(vdcp, 1, "[%d] Entered\n", vdcp->instance);
3882 3882
3883 3883 status = vdc_recv(vdcp, msgp, &nbytes);
3884 3884 DMSG(vdcp, 3, "vdc_read() done.. status=0x%x size=0x%x\n",
3885 3885 status, (int)nbytes);
3886 3886 if (status) {
3887 3887 DMSG(vdcp, 0, "?[%d] Error %d reading LDC msg\n",
3888 3888 vdcp->instance, status);
3889 3889 return (status);
3890 3890 }
3891 3891
3892 3892 if (nbytes < sizeof (vio_msg_tag_t)) {
3893 3893 DMSG(vdcp, 0, "?[%d] Expect %lu bytes; recv'd %lu\n",
3894 3894 vdcp->instance, sizeof (vio_msg_tag_t), nbytes);
3895 3895 return (ENOMSG);
3896 3896 }
3897 3897
3898 3898 DMSG(vdcp, 2, "[%d] (%x/%x/%x)\n", vdcp->instance,
3899 3899 msgp->tag.vio_msgtype,
3900 3900 msgp->tag.vio_subtype,
3901 3901 msgp->tag.vio_subtype_env);
3902 3902
3903 3903 /*
3904 3904 * Verify the Session ID of the message
3905 3905 *
3906 3906 * Every message after the Version has been negotiated should
3907 3907 * have the correct session ID set.
3908 3908 */
3909 3909 if ((msgp->tag.vio_sid != vdcp->session_id) &&
3910 3910 (msgp->tag.vio_subtype_env != VIO_VER_INFO)) {
3911 3911 DMSG(vdcp, 0, "[%d] Invalid SID: received 0x%x, "
3912 3912 "expected 0x%lx [seq num %lx @ %d]",
3913 3913 vdcp->instance, msgp->tag.vio_sid,
3914 3914 vdcp->session_id,
3915 3915 ((vio_dring_msg_t *)msgp)->seq_num,
3916 3916 ((vio_dring_msg_t *)msgp)->start_idx);
3917 3917 return (ENOMSG);
3918 3918 }
3919 3919 return (0);
3920 3920 }
3921 3921
3922 3922
3923 3923 /*
3924 3924 * Function:
3925 3925 * vdc_resubmit_backup_dring()
3926 3926 *
3927 3927 * Description:
3928 3928 * Resubmit each descriptor in the backed up dring to
3929 3929 * vDisk server. The Dring was backed up during connection
3930 3930 * reset.
3931 3931 *
3932 3932 * Arguments:
3933 3933 * vdcp - soft state pointer for this instance of the device driver.
3934 3934 *
3935 3935 * Return Code:
3936 3936 * 0 - Success
3937 3937 */
3938 3938 static int
3939 3939 vdc_resubmit_backup_dring(vdc_t *vdcp)
3940 3940 {
3941 3941 int processed = 0;
3942 3942 int count;
3943 3943 int b_idx;
3944 3944 int rv = 0;
3945 3945 int dring_size;
3946 3946 vdc_local_desc_t *curr_ldep;
3947 3947
3948 3948 ASSERT(MUTEX_NOT_HELD(&vdcp->lock));
3949 3949 ASSERT(vdcp->state == VDC_STATE_HANDLE_PENDING);
3950 3950
3951 3951 if (vdcp->local_dring_backup == NULL) {
3952 3952 /* the pending requests have already been processed */
3953 3953 return (0);
3954 3954 }
3955 3955
3956 3956 DMSG(vdcp, 1, "restoring pending dring entries (len=%d, tail=%d)\n",
3957 3957 vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
3958 3958
3959 3959 /*
3960 3960 * Walk the backup copy of the local descriptor ring and
3961 3961 * resubmit all the outstanding transactions.
3962 3962 */
3963 3963 b_idx = vdcp->local_dring_backup_tail;
3964 3964 for (count = 0; count < vdcp->local_dring_backup_len; count++) {
3965 3965
3966 3966 curr_ldep = &(vdcp->local_dring_backup[b_idx]);
3967 3967
3968 3968 /* only resubmit outstanding transactions */
3969 3969 if (!curr_ldep->is_free) {
3970 3970
3971 3971 DMSG(vdcp, 1, "resubmitting entry idx=%x\n", b_idx);
3972 3972
3973 3973 rv = vdc_do_op(vdcp, curr_ldep->operation,
3974 3974 curr_ldep->addr, curr_ldep->nbytes,
3975 3975 curr_ldep->slice, curr_ldep->offset,
3976 3976 curr_ldep->buf, curr_ldep->dir,
3977 3977 (curr_ldep->flags & ~VDC_OP_STATE_RUNNING) |
3978 3978 VDC_OP_RESUBMIT);
3979 3979
3980 3980 if (rv) {
3981 3981 DMSG(vdcp, 1, "[%d] resubmit entry %d failed\n",
3982 3982 vdcp->instance, b_idx);
3983 3983 goto done;
3984 3984 }
3985 3985
3986 3986 /*
3987 3987 * Mark this entry as free so that we will not resubmit
3988 3988 * this "done" request again, if we were to use the same
3989 3989 * backup_dring again in future. This could happen when
3990 3990 * a reset happens while processing the backup_dring.
3991 3991 */
3992 3992 curr_ldep->is_free = B_TRUE;
3993 3993 processed++;
3994 3994 }
3995 3995
3996 3996 /* get the next element to submit */
3997 3997 if (++b_idx >= vdcp->local_dring_backup_len)
3998 3998 b_idx = 0;
3999 3999 }
4000 4000
4001 4001 /* all done - now clear up pending dring copy */
4002 4002 dring_size = vdcp->local_dring_backup_len *
4003 4003 sizeof (vdcp->local_dring_backup[0]);
4004 4004
4005 4005 (void) kmem_free(vdcp->local_dring_backup, dring_size);
4006 4006
4007 4007 vdcp->local_dring_backup = NULL;
4008 4008
4009 4009 done:
4010 4010 DTRACE_PROBE2(processed, int, processed, vdc_t *, vdcp);
4011 4011
4012 4012 return (rv);
4013 4013 }
4014 4014
4015 4015 /*
4016 4016 * Function:
4017 4017 * vdc_cancel_backup_dring
4018 4018 *
4019 4019 * Description:
4020 4020 * Cancel each descriptor in the backed up dring to vDisk server.
4021 4021 * The Dring was backed up during connection reset.
4022 4022 *
4023 4023 * Arguments:
4024 4024 * vdcp - soft state pointer for this instance of the device driver.
4025 4025 *
4026 4026 * Return Code:
4027 4027 * None
4028 4028 */
4029 4029 void
4030 4030 vdc_cancel_backup_dring(vdc_t *vdcp)
4031 4031 {
4032 4032 vdc_local_desc_t *ldep;
4033 4033 struct buf *bufp;
4034 4034 int count;
4035 4035 int b_idx;
4036 4036 int dring_size;
4037 4037 int cancelled = 0;
4038 4038
4039 4039 ASSERT(MUTEX_HELD(&vdcp->lock));
4040 4040 ASSERT(vdcp->state == VDC_STATE_FAILED);
4041 4041
4042 4042 if (vdcp->local_dring_backup == NULL) {
4043 4043 /* the pending requests have already been processed */
4044 4044 return;
4045 4045 }
4046 4046
4047 4047 DMSG(vdcp, 1, "cancelling pending dring entries (len=%d, tail=%d)\n",
4048 4048 vdcp->local_dring_backup_len, vdcp->local_dring_backup_tail);
4049 4049
4050 4050 /*
4051 4051 * Walk the backup copy of the local descriptor ring and
4052 4052 * cancel all the outstanding transactions.
4053 4053 */
4054 4054 b_idx = vdcp->local_dring_backup_tail;
4055 4055 for (count = 0; count < vdcp->local_dring_backup_len; count++) {
4056 4056
4057 4057 ldep = &(vdcp->local_dring_backup[b_idx]);
4058 4058
4059 4059 /* only cancel outstanding transactions */
4060 4060 if (!ldep->is_free) {
4061 4061
4062 4062 DMSG(vdcp, 1, "cancelling entry idx=%x\n", b_idx);
4063 4063 cancelled++;
4064 4064
4065 4065 /*
4066 4066 * All requests have already been cleared from the
4067 4067 * local descriptor ring and the LDC channel has been
4068 4068 * reset so we will never get any reply for these
4069 4069 * requests. Now we just have to notify threads waiting
4070 4070 * for replies that the request has failed.
4071 4071 */
4072 4072 bufp = ldep->buf;
4073 4073 ASSERT(bufp != NULL);
4074 4074 bufp->b_resid = bufp->b_bcount;
4075 4075 if (ldep->operation == VD_OP_BREAD ||
4076 4076 ldep->operation == VD_OP_BWRITE) {
4077 4077 VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
4078 4078 VD_KSTAT_WAITQ_EXIT(vdcp);
4079 4079 DTRACE_IO1(done, buf_t *, bufp);
4080 4080 }
4081 4081 bioerror(bufp, EIO);
4082 4082 biodone(bufp);
4083 4083 }
4084 4084
4085 4085 /* get the next element to cancel */
4086 4086 if (++b_idx >= vdcp->local_dring_backup_len)
4087 4087 b_idx = 0;
4088 4088 }
4089 4089
4090 4090 /* all done - now clear up pending dring copy */
4091 4091 dring_size = vdcp->local_dring_backup_len *
4092 4092 sizeof (vdcp->local_dring_backup[0]);
4093 4093
4094 4094 (void) kmem_free(vdcp->local_dring_backup, dring_size);
4095 4095
4096 4096 vdcp->local_dring_backup = NULL;
4097 4097
4098 4098 DTRACE_PROBE2(cancelled, int, cancelled, vdc_t *, vdcp);
4099 4099 }
4100 4100
4101 4101 /*
4102 4102 * Function:
4103 4103 * vdc_connection_timeout
4104 4104 *
4105 4105 * Description:
4106 4106 * This function is invoked if the timeout set to establish the connection
4107 4107 * with vds expires. This will happen if we spend too much time in the
4108 4108 * VDC_STATE_INIT_WAITING, VDC_STATE_NEGOTIATE or VDC_STATE_HANDLE_PENDING
4109 4109 * states.
4110 4110 *
4111 4111 * Arguments:
4112 4112 * arg - argument of the timeout function actually a soft state
4113 4113 * pointer for the instance of the device driver.
4114 4114 *
4115 4115 * Return Code:
4116 4116 * None
4117 4117 */
4118 4118 void
4119 4119 vdc_connection_timeout(void *arg)
4120 4120 {
4121 4121 vdc_t *vdcp = (vdc_t *)arg;
4122 4122
4123 4123 mutex_enter(&vdcp->lock);
4124 4124
4125 4125 vdcp->ctimeout_reached = B_TRUE;
4126 4126
4127 4127 mutex_exit(&vdcp->lock);
4128 4128 }
4129 4129
4130 4130 /*
4131 4131 * Function:
4132 4132 * vdc_backup_local_dring()
4133 4133 *
4134 4134 * Description:
4135 4135 * Backup the current dring in the event of a reset. The Dring
4136 4136 * transactions will be resubmitted to the server when the
4137 4137 * connection is restored.
4138 4138 *
4139 4139 * Arguments:
4140 4140 * vdcp - soft state pointer for this instance of the device driver.
4141 4141 *
4142 4142 * Return Code:
4143 4143 * NONE
4144 4144 */
4145 4145 static void
4146 4146 vdc_backup_local_dring(vdc_t *vdcp)
4147 4147 {
4148 4148 int b_idx, count, dring_size;
4149 4149 vdc_local_desc_t *curr_ldep;
4150 4150
4151 4151 ASSERT(MUTEX_HELD(&vdcp->lock));
4152 4152 ASSERT(vdcp->state == VDC_STATE_RESETTING);
4153 4153
4154 4154 /*
4155 4155 * If the backup dring is stil around, it means
4156 4156 * that the last restore did not complete. However,
4157 4157 * since we never got back into the running state,
4158 4158 * the backup copy we have is still valid.
4159 4159 */
4160 4160 if (vdcp->local_dring_backup != NULL) {
4161 4161 DMSG(vdcp, 1, "reusing local descriptor ring backup "
4162 4162 "(len=%d, tail=%d)\n", vdcp->local_dring_backup_len,
4163 4163 vdcp->local_dring_backup_tail);
4164 4164 return;
4165 4165 }
4166 4166
4167 4167 /*
4168 4168 * The backup dring can be NULL and the local dring may not be
4169 4169 * initialized. This can happen if we had a reset while establishing
4170 4170 * a new connection but after the connection has timed out. In that
4171 4171 * case the backup dring is NULL because the requests have been
4172 4172 * cancelled and the request occured before the local dring is
4173 4173 * initialized.
4174 4174 */
4175 4175 if (!(vdcp->initialized & VDC_DRING_LOCAL))
4176 4176 return;
4177 4177
4178 4178 DMSG(vdcp, 1, "backing up the local descriptor ring (len=%d, "
4179 4179 "tail=%d)\n", vdcp->dring_len, vdcp->dring_curr_idx);
4180 4180
4181 4181 dring_size = vdcp->dring_len * sizeof (vdcp->local_dring[0]);
4182 4182
4183 4183 vdcp->local_dring_backup = kmem_alloc(dring_size, KM_SLEEP);
4184 4184 bcopy(vdcp->local_dring, vdcp->local_dring_backup, dring_size);
4185 4185
4186 4186 vdcp->local_dring_backup_tail = vdcp->dring_curr_idx;
4187 4187 vdcp->local_dring_backup_len = vdcp->dring_len;
4188 4188
4189 4189 /*
4190 4190 * At this point, pending read or write I/Os are recorded in the
4191 4191 * runq. We update the I/O statistics to indicate that they are now
4192 4192 * back in the waitq.
4193 4193 */
4194 4194 b_idx = vdcp->local_dring_backup_tail;
4195 4195 for (count = 0; count < vdcp->local_dring_backup_len; count++) {
4196 4196
4197 4197 curr_ldep = &(vdcp->local_dring_backup[b_idx]);
4198 4198
4199 4199 if (!curr_ldep->is_free &&
4200 4200 (curr_ldep->operation == VD_OP_BREAD ||
4201 4201 curr_ldep->operation == VD_OP_BWRITE)) {
4202 4202 VD_KSTAT_RUNQ_BACK_TO_WAITQ(vdcp);
4203 4203 }
4204 4204
4205 4205 /* get the next element */
4206 4206 if (++b_idx >= vdcp->local_dring_backup_len)
4207 4207 b_idx = 0;
4208 4208 }
4209 4209
4210 4210 }
4211 4211
4212 4212 static void
4213 4213 vdc_switch_server(vdc_t *vdcp)
4214 4214 {
4215 4215 int rv;
4216 4216 vdc_server_t *curr_server, *new_server;
4217 4217
4218 4218 ASSERT(MUTEX_HELD(&vdcp->lock));
4219 4219
4220 4220 /* if there is only one server return back */
4221 4221 if (vdcp->num_servers == 1) {
4222 4222 return;
4223 4223 }
4224 4224
4225 4225 /* Get current and next server */
4226 4226 curr_server = vdcp->curr_server;
4227 4227 new_server =
4228 4228 (curr_server->next) ? curr_server->next : vdcp->server_list;
4229 4229 ASSERT(curr_server != new_server);
4230 4230
4231 4231 /* bring current server's channel down */
4232 4232 rv = ldc_down(curr_server->ldc_handle);
4233 4233 if (rv) {
4234 4234 DMSG(vdcp, 0, "[%d] Cannot bring channel down, port %ld\n",
4235 4235 vdcp->instance, curr_server->id);
4236 4236 return;
4237 4237 }
4238 4238
4239 4239 /* switch the server */
4240 4240 vdcp->curr_server = new_server;
4241 4241
4242 4242 DMSG(vdcp, 0, "[%d] Switched to next vdisk server, port@%ld, ldc@%ld\n",
4243 4243 vdcp->instance, vdcp->curr_server->id, vdcp->curr_server->ldc_id);
4244 4244 }
4245 4245
4246 4246 static void
4247 4247 vdc_print_svc_status(vdc_t *vdcp)
4248 4248 {
4249 4249 int instance;
4250 4250 uint64_t ldc_id, port_id;
4251 4251 vdc_service_state_t svc_state;
4252 4252
4253 4253 ASSERT(mutex_owned(&vdcp->lock));
4254 4254
4255 4255 svc_state = vdcp->curr_server->svc_state;
4256 4256
4257 4257 if (vdcp->curr_server->log_state == svc_state)
4258 4258 return;
4259 4259
4260 4260 instance = vdcp->instance;
4261 4261 ldc_id = vdcp->curr_server->ldc_id;
4262 4262 port_id = vdcp->curr_server->id;
4263 4263
4264 4264 switch (svc_state) {
4265 4265
4266 4266 case VDC_SERVICE_OFFLINE:
4267 4267 cmn_err(CE_CONT, "?vdisk@%d is offline\n", instance);
4268 4268 break;
4269 4269
4270 4270 case VDC_SERVICE_CONNECTED:
4271 4271 cmn_err(CE_CONT, "?vdisk@%d is connected using ldc@%ld,%ld\n",
4272 4272 instance, ldc_id, port_id);
4273 4273 break;
4274 4274
4275 4275 case VDC_SERVICE_ONLINE:
4276 4276 cmn_err(CE_CONT, "?vdisk@%d is online using ldc@%ld,%ld\n",
4277 4277 instance, ldc_id, port_id);
4278 4278 break;
4279 4279
4280 4280 case VDC_SERVICE_FAILED:
4281 4281 cmn_err(CE_CONT, "?vdisk@%d access to service failed "
4282 4282 "using ldc@%ld,%ld\n", instance, ldc_id, port_id);
4283 4283 break;
4284 4284
4285 4285 case VDC_SERVICE_FAULTED:
4286 4286 cmn_err(CE_CONT, "?vdisk@%d access to backend failed "
4287 4287 "using ldc@%ld,%ld\n", instance, ldc_id, port_id);
4288 4288 break;
4289 4289
4290 4290 default:
4291 4291 ASSERT(0);
4292 4292 break;
4293 4293 }
4294 4294
4295 4295 vdcp->curr_server->log_state = svc_state;
4296 4296 }
4297 4297
4298 4298 /*
4299 4299 * Function:
4300 4300 * vdc_handshake_retry
4301 4301 *
4302 4302 * Description:
4303 4303 * This function indicates if the handshake should be retried or not.
4304 4304 * This depends on the lifecycle of the driver:
4305 4305 *
4306 4306 * VDC_LC_ATTACHING: the handshake is retried until we have tried
4307 4307 * a handshake with each server. We don't care how far each handshake
4308 4308 * went, the goal is just to try the handshake. We want to minimize the
4309 4309 * the time spent doing the attach because this is locking the device
4310 4310 * tree.
4311 4311 *
4312 4312 * VDC_LC_ONLINE_PENDING: the handshake is retried while we haven't done
4313 4313 * consecutive attribute negotiations with each server, and we haven't
4314 4314 * reached a minimum total of consecutive negotiations (hattr_min). The
4315 4315 * number of attribution negotiations determines the time spent before
4316 4316 * failing pending I/Os if the handshake is not successful.
4317 4317 *
4318 4318 * VDC_LC_ONLINE: the handshake is always retried, until we have a
4319 4319 * successful handshake with a server.
4320 4320 *
4321 4321 * VDC_LC_DETACHING: N/A
4322 4322 *
4323 4323 * Arguments:
4324 4324 * hshake_cnt - number of handshake attempts
4325 4325 * hattr_cnt - number of attribute negotiation attempts
4326 4326 *
4327 4327 * Return Code:
4328 4328 * B_TRUE - handshake should be retried
4329 4329 * B_FALSE - handshake should not be retried
4330 4330 */
4331 4331 static boolean_t
4332 4332 vdc_handshake_retry(vdc_t *vdcp, int hshake_cnt, int hattr_cnt)
4333 4333 {
4334 4334 int hattr_total = 0;
4335 4335 vdc_server_t *srvr;
4336 4336
4337 4337 ASSERT(vdcp->lifecycle != VDC_LC_DETACHING);
4338 4338
4339 4339 /* update handshake counters */
4340 4340 vdcp->curr_server->hshake_cnt = hshake_cnt;
4341 4341 vdcp->curr_server->hattr_cnt = hattr_cnt;
4342 4342
4343 4343 /*
4344 4344 * If no attribute negotiation was done then we reset the total
4345 4345 * number otherwise we cumulate the number.
4346 4346 */
4347 4347 if (hattr_cnt == 0)
4348 4348 vdcp->curr_server->hattr_total = 0;
4349 4349 else
4350 4350 vdcp->curr_server->hattr_total += hattr_cnt;
4351 4351
4352 4352 /*
4353 4353 * If we are online (i.e. at least one handshake was successfully
4354 4354 * completed) then we always retry the handshake.
4355 4355 */
4356 4356 if (vdcp->lifecycle == VDC_LC_ONLINE)
4357 4357 return (B_TRUE);
4358 4358
4359 4359 /*
4360 4360 * If we are attaching then we retry the handshake only if we haven't
4361 4361 * tried with all servers.
4362 4362 */
4363 4363 if (vdcp->lifecycle == VDC_LC_ATTACHING) {
4364 4364
4365 4365 for (srvr = vdcp->server_list; srvr != NULL;
4366 4366 srvr = srvr->next) {
4367 4367 if (srvr->hshake_cnt == 0) {
4368 4368 return (B_TRUE);
4369 4369 }
4370 4370 }
4371 4371
4372 4372 return (B_FALSE);
4373 4373 }
4374 4374
4375 4375 /*
4376 4376 * Here we are in the case where we haven't completed any handshake
4377 4377 * successfully yet.
4378 4378 */
4379 4379 ASSERT(vdcp->lifecycle == VDC_LC_ONLINE_PENDING);
4380 4380
4381 4381 /*
4382 4382 * We retry the handshake if we haven't done an attribute negotiation
4383 4383 * with each server. This is to handle the case where one service domain
4384 4384 * is down.
4385 4385 */
4386 4386 for (srvr = vdcp->server_list; srvr != NULL; srvr = srvr->next) {
4387 4387 if (srvr->hattr_cnt == 0) {
4388 4388 return (B_TRUE);
4389 4389 }
4390 4390 hattr_total += srvr->hattr_total;
4391 4391 }
4392 4392
4393 4393 /*
4394 4394 * We retry the handshake if we haven't reached the minimum number of
4395 4395 * attribute negotiation.
4396 4396 */
4397 4397 return (hattr_total < vdcp->hattr_min);
4398 4398 }
4399 4399
4400 4400 /* -------------------------------------------------------------------------- */
4401 4401
4402 4402 /*
4403 4403 * The following functions process the incoming messages from vds
4404 4404 */
4405 4405
4406 4406 /*
4407 4407 * Function:
4408 4408 * vdc_process_msg_thread()
4409 4409 *
4410 4410 * Description:
4411 4411 *
4412 4412 * Main VDC message processing thread. Each vDisk instance
4413 4413 * consists of a copy of this thread. This thread triggers
4414 4414 * all the handshakes and data exchange with the server. It
4415 4415 * also handles all channel resets
4416 4416 *
4417 4417 * Arguments:
4418 4418 * vdc - soft state pointer for this instance of the device driver.
4419 4419 *
4420 4420 * Return Code:
4421 4421 * None
4422 4422 */
4423 4423 static void
4424 4424 vdc_process_msg_thread(vdc_t *vdcp)
4425 4425 {
4426 4426 boolean_t failure_msg = B_FALSE;
4427 4427 int status;
4428 4428 int ctimeout;
4429 4429 timeout_id_t tmid = 0;
4430 4430 clock_t ldcup_timeout = 0;
4431 4431 vdc_server_t *srvr;
4432 4432 vdc_service_state_t svc_state;
4433 4433 int hshake_cnt = 0;
4434 4434 int hattr_cnt = 0;
4435 4435
4436 4436 mutex_enter(&vdcp->lock);
4437 4437
4438 4438 ASSERT(vdcp->lifecycle == VDC_LC_ATTACHING);
4439 4439
4440 4440 for (;;) {
4441 4441
4442 4442 #define Q(_s) (vdcp->state == _s) ? #_s :
4443 4443 DMSG(vdcp, 3, "state = %d (%s)\n", vdcp->state,
4444 4444 Q(VDC_STATE_INIT)
4445 4445 Q(VDC_STATE_INIT_WAITING)
4446 4446 Q(VDC_STATE_NEGOTIATE)
4447 4447 Q(VDC_STATE_HANDLE_PENDING)
4448 4448 Q(VDC_STATE_FAULTED)
4449 4449 Q(VDC_STATE_FAILED)
4450 4450 Q(VDC_STATE_RUNNING)
4451 4451 Q(VDC_STATE_RESETTING)
4452 4452 Q(VDC_STATE_DETACH)
4453 4453 "UNKNOWN");
4454 4454 #undef Q
4455 4455
4456 4456 switch (vdcp->state) {
4457 4457 case VDC_STATE_INIT:
4458 4458
4459 4459 /*
4460 4460 * If requested, start a timeout to check if the
4461 4461 * connection with vds is established in the
4462 4462 * specified delay. If the timeout expires, we
4463 4463 * will cancel any pending request.
4464 4464 *
4465 4465 * If some reset have occurred while establishing
4466 4466 * the connection, we already have a timeout armed
4467 4467 * and in that case we don't need to arm a new one.
4468 4468 *
4469 4469 * The same rule applies when there are multiple vds'.
4470 4470 * If either a connection cannot be established or
4471 4471 * the handshake times out, the connection thread will
↓ open down ↓ |
4471 lines elided |
↑ open up ↑ |
4472 4472 * try another server. The 'ctimeout' will report
4473 4473 * back an error after it expires irrespective of
4474 4474 * whether the vdisk is trying to connect to just
4475 4475 * one or multiple servers.
4476 4476 */
4477 4477 ctimeout = (vdc_timeout != 0)?
4478 4478 vdc_timeout : vdcp->curr_server->ctimeout;
4479 4479
4480 4480 if (ctimeout != 0 && tmid == 0) {
4481 4481 tmid = timeout(vdc_connection_timeout, vdcp,
4482 - ctimeout * drv_usectohz(MICROSEC));
4482 + drv_sectohz(ctimeout));
4483 4483 }
4484 4484
4485 4485 /* Switch to STATE_DETACH if drv is detaching */
4486 4486 if (vdcp->lifecycle == VDC_LC_DETACHING) {
4487 4487 vdcp->state = VDC_STATE_DETACH;
4488 4488 break;
4489 4489 }
4490 4490
4491 4491 /* Check if the timeout has been reached */
4492 4492 if (vdcp->ctimeout_reached) {
4493 4493 ASSERT(tmid != 0);
4494 4494 tmid = 0;
4495 4495 vdcp->state = VDC_STATE_FAILED;
4496 4496 break;
4497 4497 }
4498 4498
4499 4499 /*
4500 4500 * Switch to another server when we reach the limit of
4501 4501 * the number of handshake per server or if we have done
4502 4502 * an attribute negotiation.
4503 4503 */
4504 4504 if (hshake_cnt >= vdc_hshake_retries || hattr_cnt > 0) {
4505 4505
4506 4506 if (!vdc_handshake_retry(vdcp, hshake_cnt,
4507 4507 hattr_cnt)) {
4508 4508 DMSG(vdcp, 0, "[%d] too many "
4509 4509 "handshakes", vdcp->instance);
4510 4510 vdcp->state = VDC_STATE_FAILED;
4511 4511 break;
4512 4512 }
4513 4513
4514 4514 vdc_switch_server(vdcp);
4515 4515
4516 4516 hshake_cnt = 0;
4517 4517 hattr_cnt = 0;
4518 4518 }
4519 4519
4520 4520 hshake_cnt++;
4521 4521
4522 4522 /* Bring up connection with vds via LDC */
4523 4523 status = vdc_start_ldc_connection(vdcp);
4524 4524 if (status != EINVAL) {
4525 4525 vdcp->state = VDC_STATE_INIT_WAITING;
4526 4526 } else {
4527 4527 vdcp->curr_server->svc_state =
4528 4528 VDC_SERVICE_FAILED;
4529 4529 vdc_print_svc_status(vdcp);
4530 4530 }
4531 4531 break;
4532 4532
4533 4533 case VDC_STATE_INIT_WAITING:
4534 4534
↓ open down ↓ |
42 lines elided |
↑ open up ↑ |
4535 4535 /* if channel is UP, start negotiation */
4536 4536 if (vdcp->curr_server->ldc_state == LDC_UP) {
4537 4537 vdcp->state = VDC_STATE_NEGOTIATE;
4538 4538 break;
4539 4539 }
4540 4540
4541 4541 /*
4542 4542 * Wait for LDC_UP. If it times out and we have multiple
4543 4543 * servers then we will retry using a different server.
4544 4544 */
4545 - ldcup_timeout = ddi_get_lbolt() + (vdc_ldcup_timeout *
4546 - drv_usectohz(MICROSEC));
4545 + ldcup_timeout = ddi_get_lbolt() + drv_sectohz(vdc_ldcup_timeout);
4547 4546 status = cv_timedwait(&vdcp->initwait_cv, &vdcp->lock,
4548 4547 ldcup_timeout);
4549 4548 if (status == -1 &&
4550 4549 vdcp->state == VDC_STATE_INIT_WAITING &&
4551 4550 vdcp->curr_server->ldc_state != LDC_UP) {
4552 4551 /* timed out & still waiting */
4553 4552 vdcp->curr_server->svc_state =
4554 4553 VDC_SERVICE_FAILED;
4555 4554 vdc_print_svc_status(vdcp);
4556 4555 vdcp->state = VDC_STATE_INIT;
4557 4556 break;
4558 4557 }
4559 4558
4560 4559 if (vdcp->state != VDC_STATE_INIT_WAITING) {
4561 4560 DMSG(vdcp, 0,
4562 4561 "state moved to %d out from under us...\n",
4563 4562 vdcp->state);
4564 4563 }
4565 4564 break;
4566 4565
4567 4566 case VDC_STATE_NEGOTIATE:
4568 4567 switch (status = vdc_ver_negotiation(vdcp)) {
4569 4568 case 0:
4570 4569 break;
4571 4570 default:
4572 4571 DMSG(vdcp, 0, "ver negotiate failed (%d)..\n",
4573 4572 status);
4574 4573 goto reset;
4575 4574 }
4576 4575
4577 4576 hattr_cnt++;
4578 4577
4579 4578 switch (status = vdc_attr_negotiation(vdcp)) {
4580 4579 case 0:
4581 4580 break;
4582 4581 default:
4583 4582 DMSG(vdcp, 0, "attr negotiate failed (%d)..\n",
4584 4583 status);
4585 4584 goto reset;
4586 4585 }
4587 4586
4588 4587 switch (status = vdc_dring_negotiation(vdcp)) {
4589 4588 case 0:
4590 4589 break;
4591 4590 default:
4592 4591 DMSG(vdcp, 0, "dring negotiate failed (%d)..\n",
4593 4592 status);
4594 4593 goto reset;
4595 4594 }
4596 4595
4597 4596 switch (status = vdc_rdx_exchange(vdcp)) {
4598 4597 case 0:
4599 4598 vdcp->state = VDC_STATE_HANDLE_PENDING;
4600 4599 goto done;
4601 4600 default:
4602 4601 DMSG(vdcp, 0, "RDX xchg failed ..(%d)\n",
4603 4602 status);
4604 4603 goto reset;
4605 4604 }
4606 4605 reset:
4607 4606 DMSG(vdcp, 0, "negotiation failed: resetting (%d)\n",
4608 4607 status);
4609 4608 vdcp->state = VDC_STATE_RESETTING;
4610 4609 vdcp->self_reset = B_TRUE;
4611 4610 vdcp->curr_server->svc_state = VDC_SERVICE_FAILED;
4612 4611 vdc_print_svc_status(vdcp);
4613 4612 done:
4614 4613 DMSG(vdcp, 0, "negotiation complete (state=0x%x)...\n",
4615 4614 vdcp->state);
4616 4615 break;
4617 4616
4618 4617 case VDC_STATE_HANDLE_PENDING:
4619 4618
4620 4619 DMSG(vdcp, 0, "[%d] connection to service domain is up",
4621 4620 vdcp->instance);
4622 4621 vdcp->curr_server->svc_state = VDC_SERVICE_CONNECTED;
4623 4622
4624 4623 mutex_exit(&vdcp->lock);
4625 4624
4626 4625 /*
4627 4626 * If we have multiple servers, check that the backend
4628 4627 * is effectively available before resubmitting any IO.
4629 4628 */
4630 4629 if (vdcp->num_servers > 1 &&
4631 4630 vdc_eio_check(vdcp, 0) != 0) {
4632 4631 mutex_enter(&vdcp->lock);
4633 4632 vdcp->curr_server->svc_state =
4634 4633 VDC_SERVICE_FAULTED;
4635 4634 vdcp->state = VDC_STATE_FAULTED;
4636 4635 break;
4637 4636 }
4638 4637
4639 4638 if (tmid != 0) {
4640 4639 (void) untimeout(tmid);
4641 4640 tmid = 0;
4642 4641 vdcp->ctimeout_reached = B_FALSE;
4643 4642 }
4644 4643
4645 4644 /*
4646 4645 * Setup devid
4647 4646 */
4648 4647 (void) vdc_setup_devid(vdcp);
4649 4648
4650 4649 status = vdc_resubmit_backup_dring(vdcp);
4651 4650
4652 4651 mutex_enter(&vdcp->lock);
4653 4652
4654 4653 if (status) {
4655 4654 vdcp->state = VDC_STATE_RESETTING;
4656 4655 vdcp->self_reset = B_TRUE;
4657 4656 vdcp->curr_server->svc_state =
4658 4657 VDC_SERVICE_FAILED;
4659 4658 vdc_print_svc_status(vdcp);
4660 4659 } else {
4661 4660 vdcp->state = VDC_STATE_RUNNING;
4662 4661 }
4663 4662 break;
4664 4663
4665 4664 case VDC_STATE_FAULTED:
4666 4665 /*
4667 4666 * Server is faulted because the backend is unavailable.
4668 4667 * If all servers are faulted then we mark the service
4669 4668 * as failed, otherwise we reset to switch to another
4670 4669 * server.
4671 4670 */
4672 4671 vdc_print_svc_status(vdcp);
4673 4672
4674 4673 /* check if all servers are faulted */
4675 4674 for (srvr = vdcp->server_list; srvr != NULL;
4676 4675 srvr = srvr->next) {
4677 4676 svc_state = srvr->svc_state;
4678 4677 if (svc_state != VDC_SERVICE_FAULTED)
4679 4678 break;
4680 4679 }
4681 4680
4682 4681 if (srvr != NULL) {
4683 4682 vdcp->state = VDC_STATE_RESETTING;
4684 4683 vdcp->self_reset = B_TRUE;
4685 4684 } else {
4686 4685 vdcp->state = VDC_STATE_FAILED;
4687 4686 }
4688 4687 break;
4689 4688
4690 4689 case VDC_STATE_FAILED:
4691 4690 /*
4692 4691 * We reach this state when we are unable to access the
4693 4692 * backend from any server, either because of a maximum
4694 4693 * connection retries or timeout, or because the backend
4695 4694 * is unavailable.
4696 4695 *
4697 4696 * Then we cancel the backup DRing so that errors get
4698 4697 * reported and we wait for a new I/O before attempting
4699 4698 * another connection.
4700 4699 */
4701 4700
4702 4701 cmn_err(CE_NOTE, "vdisk@%d disk access failed",
4703 4702 vdcp->instance);
4704 4703 failure_msg = B_TRUE;
4705 4704
4706 4705 if (vdcp->lifecycle == VDC_LC_ATTACHING) {
4707 4706 vdcp->lifecycle = VDC_LC_ONLINE_PENDING;
4708 4707 vdcp->hattr_min = vdc_hattr_min_initial;
4709 4708 } else {
4710 4709 vdcp->hattr_min = vdc_hattr_min;
4711 4710 }
4712 4711
4713 4712 /* cancel any timeout */
4714 4713 if (tmid != 0) {
4715 4714 (void) untimeout(tmid);
4716 4715 tmid = 0;
4717 4716 }
4718 4717
4719 4718 /* cancel pending I/Os */
4720 4719 cv_broadcast(&vdcp->running_cv);
4721 4720 vdc_cancel_backup_dring(vdcp);
4722 4721
4723 4722 /* wait for new I/O */
4724 4723 while (!vdcp->io_pending)
4725 4724 cv_wait(&vdcp->io_pending_cv, &vdcp->lock);
4726 4725
4727 4726 /*
4728 4727 * There's a new IO pending. Try to re-establish a
4729 4728 * connection. Mark all services as offline, so that
4730 4729 * we don't stop again before having retried all
4731 4730 * servers.
4732 4731 */
4733 4732 for (srvr = vdcp->server_list; srvr != NULL;
4734 4733 srvr = srvr->next) {
4735 4734 srvr->svc_state = VDC_SERVICE_OFFLINE;
4736 4735 srvr->hshake_cnt = 0;
4737 4736 srvr->hattr_cnt = 0;
4738 4737 srvr->hattr_total = 0;
4739 4738 }
4740 4739
4741 4740 /* reset variables */
4742 4741 hshake_cnt = 0;
4743 4742 hattr_cnt = 0;
4744 4743 vdcp->ctimeout_reached = B_FALSE;
4745 4744
4746 4745 vdcp->state = VDC_STATE_RESETTING;
4747 4746 vdcp->self_reset = B_TRUE;
4748 4747 break;
4749 4748
4750 4749 /* enter running state */
4751 4750 case VDC_STATE_RUNNING:
4752 4751
4753 4752 if (vdcp->lifecycle == VDC_LC_DETACHING) {
4754 4753 vdcp->state = VDC_STATE_DETACH;
4755 4754 break;
4756 4755 }
4757 4756
4758 4757 vdcp->lifecycle = VDC_LC_ONLINE;
4759 4758
4760 4759 if (failure_msg) {
4761 4760 cmn_err(CE_NOTE, "vdisk@%d disk access "
4762 4761 "recovered", vdcp->instance);
4763 4762 failure_msg = B_FALSE;
4764 4763 }
4765 4764
4766 4765 /*
4767 4766 * Signal anyone waiting for the connection
4768 4767 * to come on line.
4769 4768 */
4770 4769 cv_broadcast(&vdcp->running_cv);
4771 4770
4772 4771 /* backend has to be checked after reset */
4773 4772 if (vdcp->failfast_interval != 0 ||
4774 4773 vdcp->num_servers > 1)
4775 4774 cv_signal(&vdcp->eio_cv);
4776 4775
4777 4776 /* ownership is lost during reset */
4778 4777 if (vdcp->ownership & VDC_OWNERSHIP_WANTED)
4779 4778 vdcp->ownership |= VDC_OWNERSHIP_RESET;
4780 4779 cv_signal(&vdcp->ownership_cv);
4781 4780
4782 4781 vdcp->curr_server->svc_state = VDC_SERVICE_ONLINE;
4783 4782 vdc_print_svc_status(vdcp);
4784 4783
4785 4784 mutex_exit(&vdcp->lock);
4786 4785
4787 4786 for (;;) {
4788 4787 vio_msg_t msg;
4789 4788 status = vdc_wait_for_response(vdcp, &msg);
4790 4789 if (status) break;
4791 4790
4792 4791 DMSG(vdcp, 1, "[%d] new pkt(s) available\n",
4793 4792 vdcp->instance);
4794 4793 status = vdc_process_data_msg(vdcp, &msg);
4795 4794 if (status) {
4796 4795 DMSG(vdcp, 1, "[%d] process_data_msg "
4797 4796 "returned err=%d\n", vdcp->instance,
4798 4797 status);
4799 4798 break;
4800 4799 }
4801 4800
4802 4801 }
4803 4802
4804 4803 mutex_enter(&vdcp->lock);
4805 4804
4806 4805 /* all servers are now offline */
4807 4806 for (srvr = vdcp->server_list; srvr != NULL;
4808 4807 srvr = srvr->next) {
4809 4808 srvr->svc_state = VDC_SERVICE_OFFLINE;
4810 4809 srvr->log_state = VDC_SERVICE_NONE;
4811 4810 srvr->hshake_cnt = 0;
4812 4811 srvr->hattr_cnt = 0;
4813 4812 srvr->hattr_total = 0;
4814 4813 }
4815 4814
4816 4815 hshake_cnt = 0;
4817 4816 hattr_cnt = 0;
4818 4817
4819 4818 vdc_print_svc_status(vdcp);
4820 4819
4821 4820 vdcp->state = VDC_STATE_RESETTING;
4822 4821 vdcp->self_reset = B_TRUE;
4823 4822 break;
4824 4823
4825 4824 case VDC_STATE_RESETTING:
4826 4825 /*
4827 4826 * When we reach this state, we either come from the
4828 4827 * VDC_STATE_RUNNING state and we can have pending
4829 4828 * request but no timeout is armed; or we come from
4830 4829 * the VDC_STATE_INIT_WAITING, VDC_NEGOTIATE or
4831 4830 * VDC_HANDLE_PENDING state and there is no pending
4832 4831 * request or pending requests have already been copied
4833 4832 * into the backup dring. So we can safely keep the
4834 4833 * connection timeout armed while we are in this state.
4835 4834 */
4836 4835
4837 4836 DMSG(vdcp, 0, "Initiating channel reset "
4838 4837 "(pending = %d)\n", (int)vdcp->threads_pending);
4839 4838
4840 4839 if (vdcp->self_reset) {
4841 4840 DMSG(vdcp, 0,
4842 4841 "[%d] calling stop_ldc_connection.\n",
4843 4842 vdcp->instance);
4844 4843 status = vdc_stop_ldc_connection(vdcp);
4845 4844 vdcp->self_reset = B_FALSE;
4846 4845 }
4847 4846
4848 4847 /*
4849 4848 * Wait for all threads currently waiting
4850 4849 * for a free dring entry to use.
4851 4850 */
4852 4851 while (vdcp->threads_pending) {
4853 4852 cv_broadcast(&vdcp->membind_cv);
4854 4853 cv_broadcast(&vdcp->dring_free_cv);
4855 4854 mutex_exit(&vdcp->lock);
4856 4855 /* give the waiters enough time to wake up */
4857 4856 delay(vdc_hz_min_ldc_delay);
4858 4857 mutex_enter(&vdcp->lock);
4859 4858 }
4860 4859
4861 4860 ASSERT(vdcp->threads_pending == 0);
4862 4861
4863 4862 /* Sanity check that no thread is receiving */
4864 4863 ASSERT(vdcp->read_state != VDC_READ_WAITING);
4865 4864
4866 4865 vdcp->read_state = VDC_READ_IDLE;
4867 4866 vdcp->io_pending = B_FALSE;
4868 4867
4869 4868 /*
4870 4869 * Cleanup any pending eio. These I/Os are going to
4871 4870 * be resubmitted.
4872 4871 */
4873 4872 vdc_eio_unqueue(vdcp, 0, B_FALSE);
4874 4873
4875 4874 vdc_backup_local_dring(vdcp);
4876 4875
4877 4876 /* cleanup the old d-ring */
4878 4877 vdc_destroy_descriptor_ring(vdcp);
4879 4878
4880 4879 /* go and start again */
4881 4880 vdcp->state = VDC_STATE_INIT;
4882 4881
4883 4882 break;
4884 4883
4885 4884 case VDC_STATE_DETACH:
4886 4885 DMSG(vdcp, 0, "[%d] Reset thread exit cleanup ..\n",
4887 4886 vdcp->instance);
4888 4887
4889 4888 /* cancel any pending timeout */
4890 4889 mutex_exit(&vdcp->lock);
4891 4890 if (tmid != 0) {
4892 4891 (void) untimeout(tmid);
4893 4892 tmid = 0;
4894 4893 }
4895 4894 mutex_enter(&vdcp->lock);
4896 4895
4897 4896 /*
4898 4897 * Signal anyone waiting for connection
4899 4898 * to come online
4900 4899 */
4901 4900 cv_broadcast(&vdcp->running_cv);
4902 4901
4903 4902 while (vdcp->sync_op_cnt > 0) {
4904 4903 cv_broadcast(&vdcp->sync_blocked_cv);
4905 4904 mutex_exit(&vdcp->lock);
4906 4905 /* give the waiters enough time to wake up */
4907 4906 delay(vdc_hz_min_ldc_delay);
4908 4907 mutex_enter(&vdcp->lock);
4909 4908 }
4910 4909
4911 4910 mutex_exit(&vdcp->lock);
4912 4911
4913 4912 DMSG(vdcp, 0, "[%d] Msg processing thread exiting ..\n",
4914 4913 vdcp->instance);
4915 4914 thread_exit();
4916 4915 break;
4917 4916 }
4918 4917 }
4919 4918 }
4920 4919
4921 4920
4922 4921 /*
4923 4922 * Function:
4924 4923 * vdc_process_data_msg()
4925 4924 *
4926 4925 * Description:
4927 4926 * This function is called by the message processing thread each time
4928 4927 * a message with a msgtype of VIO_TYPE_DATA is received. It will either
4929 4928 * be an ACK or NACK from vds[1] which vdc handles as follows.
4930 4929 * ACK - wake up the waiting thread
4931 4930 * NACK - resend any messages necessary
4932 4931 *
4933 4932 * [1] Although the message format allows it, vds should not send a
4934 4933 * VIO_SUBTYPE_INFO message to vdc asking it to read data; if for
4935 4934 * some bizarre reason it does, vdc will reset the connection.
4936 4935 *
4937 4936 * Arguments:
4938 4937 * vdc - soft state pointer for this instance of the device driver.
4939 4938 * msg - the LDC message sent by vds
4940 4939 *
4941 4940 * Return Code:
4942 4941 * 0 - Success.
4943 4942 * > 0 - error value returned by LDC
4944 4943 */
4945 4944 static int
4946 4945 vdc_process_data_msg(vdc_t *vdcp, vio_msg_t *msg)
4947 4946 {
4948 4947 int status = 0;
4949 4948 vio_dring_msg_t *dring_msg;
4950 4949 vdc_local_desc_t *ldep = NULL;
4951 4950 int start, end;
4952 4951 int idx;
4953 4952 int op;
4954 4953
4955 4954 dring_msg = (vio_dring_msg_t *)msg;
4956 4955
4957 4956 ASSERT(msg->tag.vio_msgtype == VIO_TYPE_DATA);
4958 4957 ASSERT(vdcp != NULL);
4959 4958
4960 4959 mutex_enter(&vdcp->lock);
4961 4960
4962 4961 /*
4963 4962 * Check to see if the message has bogus data
4964 4963 */
4965 4964 idx = start = dring_msg->start_idx;
4966 4965 end = dring_msg->end_idx;
4967 4966 if ((start >= vdcp->dring_len) ||
4968 4967 (end >= vdcp->dring_len) || (end < -1)) {
4969 4968 /*
4970 4969 * Update the I/O statistics to indicate that an error ocurred.
4971 4970 * No need to update the wait/run queues as no specific read or
4972 4971 * write request is being completed in response to this 'msg'.
4973 4972 */
4974 4973 VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
4975 4974 DMSG(vdcp, 0, "[%d] Bogus ACK data : start %d, end %d\n",
4976 4975 vdcp->instance, start, end);
4977 4976 mutex_exit(&vdcp->lock);
4978 4977 return (EINVAL);
4979 4978 }
4980 4979
4981 4980 /*
4982 4981 * Verify that the sequence number is what vdc expects.
4983 4982 */
4984 4983 switch (vdc_verify_seq_num(vdcp, dring_msg)) {
4985 4984 case VDC_SEQ_NUM_TODO:
4986 4985 break; /* keep processing this message */
4987 4986 case VDC_SEQ_NUM_SKIP:
4988 4987 mutex_exit(&vdcp->lock);
4989 4988 return (0);
4990 4989 case VDC_SEQ_NUM_INVALID:
4991 4990 /*
4992 4991 * Update the I/O statistics to indicate that an error ocurred.
4993 4992 * No need to update the wait/run queues as no specific read or
4994 4993 * write request is being completed in response to this 'msg'.
4995 4994 */
4996 4995 VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
4997 4996 DMSG(vdcp, 0, "[%d] invalid seqno\n", vdcp->instance);
4998 4997 mutex_exit(&vdcp->lock);
4999 4998 return (ENXIO);
5000 4999 }
5001 5000
5002 5001 if (msg->tag.vio_subtype == VIO_SUBTYPE_NACK) {
5003 5002 /*
5004 5003 * Update the I/O statistics to indicate that an error ocurred.
5005 5004 * No need to update the wait/run queues, this will be done by
5006 5005 * the thread calling this function.
5007 5006 */
5008 5007 VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
5009 5008 VDC_DUMP_DRING_MSG(dring_msg);
5010 5009 DMSG(vdcp, 0, "[%d] DATA NACK\n", vdcp->instance);
5011 5010 mutex_exit(&vdcp->lock);
5012 5011 return (EIO);
5013 5012
5014 5013 } else if (msg->tag.vio_subtype == VIO_SUBTYPE_INFO) {
5015 5014 /*
5016 5015 * Update the I/O statistics to indicate that an error occurred.
5017 5016 * No need to update the wait/run queues as no specific read or
5018 5017 * write request is being completed in response to this 'msg'.
5019 5018 */
5020 5019 VD_UPDATE_ERR_STATS(vdcp, vd_protoerrs);
5021 5020 mutex_exit(&vdcp->lock);
5022 5021 return (EPROTO);
5023 5022 }
5024 5023
5025 5024 DMSG(vdcp, 1, ": start %d end %d\n", start, end);
5026 5025 ASSERT(start == end);
5027 5026
5028 5027 ldep = &vdcp->local_dring[idx];
5029 5028
5030 5029 DMSG(vdcp, 1, ": state 0x%x\n", ldep->dep->hdr.dstate);
5031 5030
5032 5031 if (ldep->dep->hdr.dstate == VIO_DESC_DONE) {
5033 5032 struct buf *bufp;
5034 5033
5035 5034 status = ldep->dep->payload.status;
5036 5035
5037 5036 bufp = ldep->buf;
5038 5037 ASSERT(bufp != NULL);
5039 5038
5040 5039 bufp->b_resid = bufp->b_bcount - ldep->dep->payload.nbytes;
5041 5040 bioerror(bufp, status);
5042 5041
5043 5042 if (status != 0) {
5044 5043 DMSG(vdcp, 1, "I/O status=%d\n", status);
5045 5044 }
5046 5045
5047 5046 DMSG(vdcp, 1,
5048 5047 "I/O complete req=%ld bytes resp=%ld bytes\n",
5049 5048 bufp->b_bcount, ldep->dep->payload.nbytes);
5050 5049
5051 5050 /*
5052 5051 * If the request has failed and we have multiple servers or
5053 5052 * failfast is enabled then we will have to defer the completion
5054 5053 * of the request until we have checked that the vdisk backend
5055 5054 * is effectively available (if multiple server) or that there
5056 5055 * is no reservation conflict (if failfast).
5057 5056 */
5058 5057 if (status != 0 &&
5059 5058 ((vdcp->num_servers > 1 &&
5060 5059 (ldep->flags & VDC_OP_ERRCHK_BACKEND)) ||
5061 5060 (vdcp->failfast_interval != 0 &&
5062 5061 (ldep->flags & VDC_OP_ERRCHK_CONFLICT)))) {
5063 5062 /*
5064 5063 * The I/O has failed and we need to check the error.
5065 5064 */
5066 5065 (void) vdc_eio_queue(vdcp, idx);
5067 5066 } else {
5068 5067 op = ldep->operation;
5069 5068 if (op == VD_OP_BREAD || op == VD_OP_BWRITE) {
5070 5069 if (status == 0) {
5071 5070 VD_UPDATE_IO_STATS(vdcp, op,
5072 5071 ldep->dep->payload.nbytes);
5073 5072 } else {
5074 5073 VD_UPDATE_ERR_STATS(vdcp, vd_softerrs);
5075 5074 }
5076 5075 VD_KSTAT_RUNQ_EXIT(vdcp);
5077 5076 DTRACE_IO1(done, buf_t *, bufp);
5078 5077 }
5079 5078 (void) vdc_depopulate_descriptor(vdcp, idx);
5080 5079 biodone(bufp);
5081 5080 }
5082 5081 }
5083 5082
5084 5083 /* let the arrival signal propogate */
5085 5084 mutex_exit(&vdcp->lock);
5086 5085
5087 5086 /* probe gives the count of how many entries were processed */
5088 5087 DTRACE_PROBE2(processed, int, 1, vdc_t *, vdcp);
5089 5088
5090 5089 return (0);
5091 5090 }
5092 5091
5093 5092
5094 5093 /*
5095 5094 * Function:
5096 5095 * vdc_handle_ver_msg()
5097 5096 *
5098 5097 * Description:
5099 5098 *
5100 5099 * Arguments:
5101 5100 * vdc - soft state pointer for this instance of the device driver.
5102 5101 * ver_msg - LDC message sent by vDisk server
5103 5102 *
5104 5103 * Return Code:
5105 5104 * 0 - Success
5106 5105 */
5107 5106 static int
5108 5107 vdc_handle_ver_msg(vdc_t *vdc, vio_ver_msg_t *ver_msg)
5109 5108 {
5110 5109 int status = 0;
5111 5110
5112 5111 ASSERT(vdc != NULL);
5113 5112 ASSERT(mutex_owned(&vdc->lock));
5114 5113
5115 5114 if (ver_msg->tag.vio_subtype_env != VIO_VER_INFO) {
5116 5115 return (EPROTO);
5117 5116 }
5118 5117
5119 5118 if (ver_msg->dev_class != VDEV_DISK_SERVER) {
5120 5119 return (EINVAL);
5121 5120 }
5122 5121
5123 5122 switch (ver_msg->tag.vio_subtype) {
5124 5123 case VIO_SUBTYPE_ACK:
5125 5124 /*
5126 5125 * We check to see if the version returned is indeed supported
5127 5126 * (The server may have also adjusted the minor number downwards
5128 5127 * and if so 'ver_msg' will contain the actual version agreed)
5129 5128 */
5130 5129 if (vdc_is_supported_version(ver_msg)) {
5131 5130 vdc->ver.major = ver_msg->ver_major;
5132 5131 vdc->ver.minor = ver_msg->ver_minor;
5133 5132 ASSERT(vdc->ver.major > 0);
5134 5133 } else {
5135 5134 status = EPROTO;
5136 5135 }
5137 5136 break;
5138 5137
5139 5138 case VIO_SUBTYPE_NACK:
5140 5139 /*
5141 5140 * call vdc_is_supported_version() which will return the next
5142 5141 * supported version (if any) in 'ver_msg'
5143 5142 */
5144 5143 (void) vdc_is_supported_version(ver_msg);
5145 5144 if (ver_msg->ver_major > 0) {
5146 5145 size_t len = sizeof (*ver_msg);
5147 5146
5148 5147 ASSERT(vdc->ver.major > 0);
5149 5148
5150 5149 /* reset the necessary fields and resend */
5151 5150 ver_msg->tag.vio_subtype = VIO_SUBTYPE_INFO;
5152 5151 ver_msg->dev_class = VDEV_DISK;
5153 5152
5154 5153 status = vdc_send(vdc, (caddr_t)ver_msg, &len);
5155 5154 DMSG(vdc, 0, "[%d] Resend VER info (LDC status = %d)\n",
5156 5155 vdc->instance, status);
5157 5156 if (len != sizeof (*ver_msg))
5158 5157 status = EBADMSG;
5159 5158 } else {
5160 5159 DMSG(vdc, 0, "[%d] No common version with vDisk server",
5161 5160 vdc->instance);
5162 5161 status = ENOTSUP;
5163 5162 }
5164 5163
5165 5164 break;
5166 5165 case VIO_SUBTYPE_INFO:
5167 5166 /*
5168 5167 * Handle the case where vds starts handshake
5169 5168 * (for now only vdc is the instigator)
5170 5169 */
5171 5170 status = ENOTSUP;
5172 5171 break;
5173 5172
5174 5173 default:
5175 5174 status = EINVAL;
5176 5175 break;
5177 5176 }
5178 5177
5179 5178 return (status);
5180 5179 }
5181 5180
5182 5181 /*
5183 5182 * Function:
5184 5183 * vdc_handle_attr_msg()
5185 5184 *
5186 5185 * Description:
5187 5186 *
5188 5187 * Arguments:
5189 5188 * vdc - soft state pointer for this instance of the device driver.
5190 5189 * attr_msg - LDC message sent by vDisk server
5191 5190 *
5192 5191 * Return Code:
5193 5192 * 0 - Success
5194 5193 */
5195 5194 static int
5196 5195 vdc_handle_attr_msg(vdc_t *vdc, vd_attr_msg_t *attr_msg)
5197 5196 {
5198 5197 int status = 0;
5199 5198 vd_disk_type_t old_type;
5200 5199
5201 5200 ASSERT(vdc != NULL);
5202 5201 ASSERT(mutex_owned(&vdc->lock));
5203 5202
5204 5203 if (attr_msg->tag.vio_subtype_env != VIO_ATTR_INFO) {
5205 5204 return (EPROTO);
5206 5205 }
5207 5206
5208 5207 switch (attr_msg->tag.vio_subtype) {
5209 5208 case VIO_SUBTYPE_ACK:
5210 5209 /*
5211 5210 * We now verify the attributes sent by vds.
5212 5211 */
5213 5212 if (attr_msg->vdisk_size == 0) {
5214 5213 DMSG(vdc, 0, "[%d] Invalid disk size from vds",
5215 5214 vdc->instance);
5216 5215 status = EINVAL;
5217 5216 break;
5218 5217 }
5219 5218
5220 5219 if (attr_msg->max_xfer_sz == 0) {
5221 5220 DMSG(vdc, 0, "[%d] Invalid transfer size from vds",
5222 5221 vdc->instance);
5223 5222 status = EINVAL;
5224 5223 break;
5225 5224 }
5226 5225
5227 5226 if (attr_msg->vdisk_size == VD_SIZE_UNKNOWN) {
5228 5227 DMSG(vdc, 0, "[%d] Unknown disk size from vds",
5229 5228 vdc->instance);
5230 5229 attr_msg->vdisk_size = 0;
5231 5230 }
5232 5231
5233 5232 /* update the VIO block size */
5234 5233 if (attr_msg->vdisk_block_size > 0 &&
5235 5234 vdc_update_vio_bsize(vdc,
5236 5235 attr_msg->vdisk_block_size) != 0) {
5237 5236 DMSG(vdc, 0, "[%d] Invalid block size (%u) from vds",
5238 5237 vdc->instance, attr_msg->vdisk_block_size);
5239 5238 status = EINVAL;
5240 5239 break;
5241 5240 }
5242 5241
5243 5242 /* update disk, block and transfer sizes */
5244 5243 old_type = vdc->vdisk_type;
5245 5244 vdc_update_size(vdc, attr_msg->vdisk_size,
5246 5245 attr_msg->vdisk_block_size, attr_msg->max_xfer_sz);
5247 5246 vdc->vdisk_type = attr_msg->vdisk_type;
5248 5247 vdc->operations = attr_msg->operations;
5249 5248 if (vio_ver_is_supported(vdc->ver, 1, 1))
5250 5249 vdc->vdisk_media = attr_msg->vdisk_media;
5251 5250 else
5252 5251 vdc->vdisk_media = 0;
5253 5252
5254 5253 DMSG(vdc, 0, "[%d] max_xfer_sz: sent %lx acked %lx\n",
5255 5254 vdc->instance, vdc->max_xfer_sz, attr_msg->max_xfer_sz);
5256 5255 DMSG(vdc, 0, "[%d] vdisk_block_size: sent %lx acked %x\n",
5257 5256 vdc->instance, vdc->vdisk_bsize,
5258 5257 attr_msg->vdisk_block_size);
5259 5258
5260 5259 if ((attr_msg->xfer_mode != VIO_DRING_MODE_V1_0) ||
5261 5260 (attr_msg->vdisk_size > INT64_MAX) ||
5262 5261 (attr_msg->operations == 0) ||
5263 5262 (attr_msg->vdisk_type > VD_DISK_TYPE_DISK)) {
5264 5263 DMSG(vdc, 0, "[%d] Invalid attributes from vds",
5265 5264 vdc->instance);
5266 5265 status = EINVAL;
5267 5266 break;
5268 5267 }
5269 5268
5270 5269 /*
5271 5270 * Now that we have received all attributes we can create a
5272 5271 * fake geometry for the disk.
5273 5272 */
5274 5273 vdc_create_fake_geometry(vdc);
5275 5274
5276 5275 /*
5277 5276 * If the disk type was previously unknown and device nodes
5278 5277 * were created then the driver would have created 8 device
5279 5278 * nodes. If we now find out that this is a single-slice disk
5280 5279 * then we need to re-create the appropriate device nodes.
5281 5280 */
5282 5281 if (old_type == VD_DISK_TYPE_UNK &&
5283 5282 (vdc->initialized & VDC_MINOR) &&
5284 5283 vdc->vdisk_type == VD_DISK_TYPE_SLICE) {
5285 5284 ddi_remove_minor_node(vdc->dip, NULL);
5286 5285 (void) devfs_clean(ddi_get_parent(vdc->dip),
5287 5286 NULL, DV_CLEAN_FORCE);
5288 5287 if (vdc_create_device_nodes(vdc) != 0) {
5289 5288 DMSG(vdc, 0, "![%d] Failed to update "
5290 5289 "device nodes", vdc->instance);
5291 5290 }
5292 5291 }
5293 5292
5294 5293 break;
5295 5294
5296 5295 case VIO_SUBTYPE_NACK:
5297 5296 /*
5298 5297 * vds could not handle the attributes we sent so we
5299 5298 * stop negotiating.
5300 5299 */
5301 5300 status = EPROTO;
5302 5301 break;
5303 5302
5304 5303 case VIO_SUBTYPE_INFO:
5305 5304 /*
5306 5305 * Handle the case where vds starts the handshake
5307 5306 * (for now; vdc is the only supported instigatior)
5308 5307 */
5309 5308 status = ENOTSUP;
5310 5309 break;
5311 5310
5312 5311 default:
5313 5312 status = ENOTSUP;
5314 5313 break;
5315 5314 }
5316 5315
5317 5316 return (status);
5318 5317 }
5319 5318
5320 5319 /*
5321 5320 * Function:
5322 5321 * vdc_handle_dring_reg_msg()
5323 5322 *
5324 5323 * Description:
5325 5324 *
5326 5325 * Arguments:
5327 5326 * vdc - soft state pointer for this instance of the driver.
5328 5327 * dring_msg - LDC message sent by vDisk server
5329 5328 *
5330 5329 * Return Code:
5331 5330 * 0 - Success
5332 5331 */
5333 5332 static int
5334 5333 vdc_handle_dring_reg_msg(vdc_t *vdc, vio_dring_reg_msg_t *dring_msg)
5335 5334 {
5336 5335 int status = 0;
5337 5336
5338 5337 ASSERT(vdc != NULL);
5339 5338 ASSERT(mutex_owned(&vdc->lock));
5340 5339
5341 5340 if (dring_msg->tag.vio_subtype_env != VIO_DRING_REG) {
5342 5341 return (EPROTO);
5343 5342 }
5344 5343
5345 5344 switch (dring_msg->tag.vio_subtype) {
5346 5345 case VIO_SUBTYPE_ACK:
5347 5346 /* save the received dring_ident */
5348 5347 vdc->dring_ident = dring_msg->dring_ident;
5349 5348 DMSG(vdc, 0, "[%d] Received dring ident=0x%lx\n",
5350 5349 vdc->instance, vdc->dring_ident);
5351 5350 break;
5352 5351
5353 5352 case VIO_SUBTYPE_NACK:
5354 5353 /*
5355 5354 * vds could not handle the DRing info we sent so we
5356 5355 * stop negotiating.
5357 5356 */
5358 5357 DMSG(vdc, 0, "[%d] server could not register DRing\n",
5359 5358 vdc->instance);
5360 5359 status = EPROTO;
5361 5360 break;
5362 5361
5363 5362 case VIO_SUBTYPE_INFO:
5364 5363 /*
5365 5364 * Handle the case where vds starts handshake
5366 5365 * (for now only vdc is the instigatior)
5367 5366 */
5368 5367 status = ENOTSUP;
5369 5368 break;
5370 5369 default:
5371 5370 status = ENOTSUP;
5372 5371 }
5373 5372
5374 5373 return (status);
5375 5374 }
5376 5375
5377 5376 /*
5378 5377 * Function:
5379 5378 * vdc_verify_seq_num()
5380 5379 *
5381 5380 * Description:
5382 5381 * This functions verifies that the sequence number sent back by the vDisk
5383 5382 * server with the latest message is what is expected (i.e. it is greater
5384 5383 * than the last seq num sent by the vDisk server and less than or equal
5385 5384 * to the last seq num generated by vdc).
5386 5385 *
5387 5386 * It then checks the request ID to see if any requests need processing
5388 5387 * in the DRing.
5389 5388 *
5390 5389 * Arguments:
5391 5390 * vdc - soft state pointer for this instance of the driver.
5392 5391 * dring_msg - pointer to the LDC message sent by vds
5393 5392 *
5394 5393 * Return Code:
5395 5394 * VDC_SEQ_NUM_TODO - Message needs to be processed
5396 5395 * VDC_SEQ_NUM_SKIP - Message has already been processed
5397 5396 * VDC_SEQ_NUM_INVALID - The seq numbers are so out of sync,
5398 5397 * vdc cannot deal with them
5399 5398 */
5400 5399 static int
5401 5400 vdc_verify_seq_num(vdc_t *vdc, vio_dring_msg_t *dring_msg)
5402 5401 {
5403 5402 ASSERT(vdc != NULL);
5404 5403 ASSERT(dring_msg != NULL);
5405 5404 ASSERT(mutex_owned(&vdc->lock));
5406 5405
5407 5406 /*
5408 5407 * Check to see if the messages were responded to in the correct
5409 5408 * order by vds.
5410 5409 */
5411 5410 if ((dring_msg->seq_num <= vdc->seq_num_reply) ||
5412 5411 (dring_msg->seq_num > vdc->seq_num)) {
5413 5412 DMSG(vdc, 0, "?[%d] Bogus sequence_number %lu: "
5414 5413 "%lu > expected <= %lu (last proc req %lu sent %lu)\n",
5415 5414 vdc->instance, dring_msg->seq_num,
5416 5415 vdc->seq_num_reply, vdc->seq_num,
5417 5416 vdc->req_id_proc, vdc->req_id);
5418 5417 return (VDC_SEQ_NUM_INVALID);
5419 5418 }
5420 5419 vdc->seq_num_reply = dring_msg->seq_num;
5421 5420
5422 5421 if (vdc->req_id_proc < vdc->req_id)
5423 5422 return (VDC_SEQ_NUM_TODO);
5424 5423 else
5425 5424 return (VDC_SEQ_NUM_SKIP);
5426 5425 }
5427 5426
5428 5427
5429 5428 /*
5430 5429 * Function:
5431 5430 * vdc_is_supported_version()
5432 5431 *
5433 5432 * Description:
5434 5433 * This routine checks if the major/minor version numbers specified in
5435 5434 * 'ver_msg' are supported. If not it finds the next version that is
5436 5435 * in the supported version list 'vdc_version[]' and sets the fields in
5437 5436 * 'ver_msg' to those values
5438 5437 *
5439 5438 * Arguments:
5440 5439 * ver_msg - LDC message sent by vDisk server
5441 5440 *
5442 5441 * Return Code:
5443 5442 * B_TRUE - Success
5444 5443 * B_FALSE - Version not supported
5445 5444 */
5446 5445 static boolean_t
5447 5446 vdc_is_supported_version(vio_ver_msg_t *ver_msg)
5448 5447 {
5449 5448 int vdc_num_versions = sizeof (vdc_version) / sizeof (vdc_version[0]);
5450 5449
5451 5450 for (int i = 0; i < vdc_num_versions; i++) {
5452 5451 ASSERT(vdc_version[i].major > 0);
5453 5452 ASSERT((i == 0) ||
5454 5453 (vdc_version[i].major < vdc_version[i-1].major));
5455 5454
5456 5455 /*
5457 5456 * If the major versions match, adjust the minor version, if
5458 5457 * necessary, down to the highest value supported by this
5459 5458 * client. The server should support all minor versions lower
5460 5459 * than the value it sent
5461 5460 */
5462 5461 if (ver_msg->ver_major == vdc_version[i].major) {
5463 5462 if (ver_msg->ver_minor > vdc_version[i].minor) {
5464 5463 DMSGX(0,
5465 5464 "Adjusting minor version from %u to %u",
5466 5465 ver_msg->ver_minor, vdc_version[i].minor);
5467 5466 ver_msg->ver_minor = vdc_version[i].minor;
5468 5467 }
5469 5468 return (B_TRUE);
5470 5469 }
5471 5470
5472 5471 /*
5473 5472 * If the message contains a higher major version number, set
5474 5473 * the message's major/minor versions to the current values
5475 5474 * and return false, so this message will get resent with
5476 5475 * these values, and the server will potentially try again
5477 5476 * with the same or a lower version
5478 5477 */
5479 5478 if (ver_msg->ver_major > vdc_version[i].major) {
5480 5479 ver_msg->ver_major = vdc_version[i].major;
5481 5480 ver_msg->ver_minor = vdc_version[i].minor;
5482 5481 DMSGX(0, "Suggesting major/minor (0x%x/0x%x)\n",
5483 5482 ver_msg->ver_major, ver_msg->ver_minor);
5484 5483
5485 5484 return (B_FALSE);
5486 5485 }
5487 5486
5488 5487 /*
5489 5488 * Otherwise, the message's major version is less than the
5490 5489 * current major version, so continue the loop to the next
5491 5490 * (lower) supported version
5492 5491 */
5493 5492 }
5494 5493
5495 5494 /*
5496 5495 * No common version was found; "ground" the version pair in the
5497 5496 * message to terminate negotiation
5498 5497 */
5499 5498 ver_msg->ver_major = 0;
5500 5499 ver_msg->ver_minor = 0;
5501 5500
5502 5501 return (B_FALSE);
5503 5502 }
5504 5503 /* -------------------------------------------------------------------------- */
5505 5504
5506 5505 /*
5507 5506 * DKIO(7) support
5508 5507 */
5509 5508
5510 5509 typedef struct vdc_dk_arg {
5511 5510 struct dk_callback dkc;
5512 5511 int mode;
5513 5512 dev_t dev;
5514 5513 vdc_t *vdc;
5515 5514 } vdc_dk_arg_t;
5516 5515
5517 5516 /*
5518 5517 * Function:
5519 5518 * vdc_dkio_flush_cb()
5520 5519 *
5521 5520 * Description:
5522 5521 * This routine is a callback for DKIOCFLUSHWRITECACHE which can be called
5523 5522 * by kernel code.
5524 5523 *
5525 5524 * Arguments:
5526 5525 * arg - a pointer to a vdc_dk_arg_t structure.
5527 5526 */
5528 5527 void
5529 5528 vdc_dkio_flush_cb(void *arg)
5530 5529 {
5531 5530 struct vdc_dk_arg *dk_arg = (struct vdc_dk_arg *)arg;
5532 5531 struct dk_callback *dkc = NULL;
5533 5532 vdc_t *vdc = NULL;
5534 5533 int rv;
5535 5534
5536 5535 if (dk_arg == NULL) {
5537 5536 cmn_err(CE_NOTE, "?[Unk] DKIOCFLUSHWRITECACHE arg is NULL\n");
5538 5537 return;
5539 5538 }
5540 5539 dkc = &dk_arg->dkc;
5541 5540 vdc = dk_arg->vdc;
5542 5541 ASSERT(vdc != NULL);
5543 5542
5544 5543 rv = vdc_do_sync_op(vdc, VD_OP_FLUSH, NULL, 0,
5545 5544 VDCPART(dk_arg->dev), 0, VIO_both_dir, B_TRUE);
5546 5545 if (rv != 0) {
5547 5546 DMSG(vdc, 0, "[%d] DKIOCFLUSHWRITECACHE failed %d : model %x\n",
5548 5547 vdc->instance, rv,
5549 5548 ddi_model_convert_from(dk_arg->mode & FMODELS));
5550 5549 }
5551 5550
5552 5551 /*
5553 5552 * Trigger the call back to notify the caller the the ioctl call has
5554 5553 * been completed.
5555 5554 */
5556 5555 if ((dk_arg->mode & FKIOCTL) &&
5557 5556 (dkc != NULL) &&
5558 5557 (dkc->dkc_callback != NULL)) {
5559 5558 ASSERT(dkc->dkc_cookie != NULL);
5560 5559 (*dkc->dkc_callback)(dkc->dkc_cookie, rv);
5561 5560 }
5562 5561
5563 5562 /* Indicate that one less DKIO write flush is outstanding */
5564 5563 mutex_enter(&vdc->lock);
5565 5564 vdc->dkio_flush_pending--;
5566 5565 ASSERT(vdc->dkio_flush_pending >= 0);
5567 5566 mutex_exit(&vdc->lock);
5568 5567
5569 5568 /* free the mem that was allocated when the callback was dispatched */
5570 5569 kmem_free(arg, sizeof (vdc_dk_arg_t));
5571 5570 }
5572 5571
5573 5572 /*
5574 5573 * Function:
5575 5574 * vdc_dkio_gapart()
5576 5575 *
5577 5576 * Description:
5578 5577 * This function implements the DKIOCGAPART ioctl.
5579 5578 *
5580 5579 * Arguments:
5581 5580 * vdc - soft state pointer
5582 5581 * arg - a pointer to a dk_map[NDKMAP] or dk_map32[NDKMAP] structure
5583 5582 * flag - ioctl flags
5584 5583 */
5585 5584 static int
5586 5585 vdc_dkio_gapart(vdc_t *vdc, caddr_t arg, int flag)
5587 5586 {
5588 5587 struct dk_geom *geom;
5589 5588 struct extvtoc *vtoc;
5590 5589 union {
5591 5590 struct dk_map map[NDKMAP];
5592 5591 struct dk_map32 map32[NDKMAP];
5593 5592 } data;
5594 5593 int i, rv, size;
5595 5594
5596 5595 mutex_enter(&vdc->lock);
5597 5596
5598 5597 if ((rv = vdc_validate_geometry(vdc)) != 0) {
5599 5598 mutex_exit(&vdc->lock);
5600 5599 return (rv);
5601 5600 }
5602 5601
5603 5602 if (vdc->vdisk_size > VD_OLDVTOC_LIMIT) {
5604 5603 mutex_exit(&vdc->lock);
5605 5604 return (EOVERFLOW);
5606 5605 }
5607 5606
5608 5607 vtoc = vdc->vtoc;
5609 5608 geom = vdc->geom;
5610 5609
5611 5610 if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
5612 5611
5613 5612 for (i = 0; i < vtoc->v_nparts; i++) {
5614 5613 data.map32[i].dkl_cylno = vtoc->v_part[i].p_start /
5615 5614 (geom->dkg_nhead * geom->dkg_nsect);
5616 5615 data.map32[i].dkl_nblk = vtoc->v_part[i].p_size;
5617 5616 }
5618 5617 size = NDKMAP * sizeof (struct dk_map32);
5619 5618
5620 5619 } else {
5621 5620
5622 5621 for (i = 0; i < vtoc->v_nparts; i++) {
5623 5622 data.map[i].dkl_cylno = vtoc->v_part[i].p_start /
5624 5623 (geom->dkg_nhead * geom->dkg_nsect);
5625 5624 data.map[i].dkl_nblk = vtoc->v_part[i].p_size;
5626 5625 }
5627 5626 size = NDKMAP * sizeof (struct dk_map);
5628 5627
5629 5628 }
5630 5629
5631 5630 mutex_exit(&vdc->lock);
5632 5631
5633 5632 if (ddi_copyout(&data, arg, size, flag) != 0)
5634 5633 return (EFAULT);
5635 5634
5636 5635 return (0);
5637 5636 }
5638 5637
5639 5638 /*
5640 5639 * Function:
5641 5640 * vdc_dkio_partition()
5642 5641 *
5643 5642 * Description:
5644 5643 * This function implements the DKIOCPARTITION ioctl.
5645 5644 *
5646 5645 * Arguments:
5647 5646 * vdc - soft state pointer
5648 5647 * arg - a pointer to a struct partition64 structure
5649 5648 * flag - ioctl flags
5650 5649 */
5651 5650 static int
5652 5651 vdc_dkio_partition(vdc_t *vdc, caddr_t arg, int flag)
5653 5652 {
5654 5653 struct partition64 p64;
5655 5654 efi_gpt_t *gpt;
5656 5655 efi_gpe_t *gpe;
5657 5656 vd_efi_dev_t edev;
5658 5657 uint_t partno;
5659 5658 int rv;
5660 5659
5661 5660 if (ddi_copyin(arg, &p64, sizeof (struct partition64), flag)) {
5662 5661 return (EFAULT);
5663 5662 }
5664 5663
5665 5664 VDC_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl);
5666 5665
5667 5666 if ((rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe)) != 0) {
5668 5667 return (rv);
5669 5668 }
5670 5669
5671 5670 partno = p64.p_partno;
5672 5671
5673 5672 if (partno >= gpt->efi_gpt_NumberOfPartitionEntries) {
5674 5673 vd_efi_free(&edev, gpt, gpe);
5675 5674 return (ESRCH);
5676 5675 }
5677 5676
5678 5677 bcopy(&gpe[partno].efi_gpe_PartitionTypeGUID, &p64.p_type,
5679 5678 sizeof (struct uuid));
5680 5679 p64.p_start = gpe[partno].efi_gpe_StartingLBA;
5681 5680 p64.p_size = gpe[partno].efi_gpe_EndingLBA - p64.p_start + 1;
5682 5681
5683 5682 if (ddi_copyout(&p64, arg, sizeof (struct partition64), flag)) {
5684 5683 vd_efi_free(&edev, gpt, gpe);
5685 5684 return (EFAULT);
5686 5685 }
5687 5686
5688 5687 vd_efi_free(&edev, gpt, gpe);
5689 5688 return (0);
5690 5689 }
5691 5690
5692 5691 /*
5693 5692 * Function:
5694 5693 * vdc_dioctl_rwcmd()
5695 5694 *
5696 5695 * Description:
5697 5696 * This function implements the DIOCTL_RWCMD ioctl. This ioctl is used
5698 5697 * for DKC_DIRECT disks to read or write at an absolute disk offset.
5699 5698 *
5700 5699 * Arguments:
5701 5700 * dev - device
5702 5701 * arg - a pointer to a dadkio_rwcmd or dadkio_rwcmd32 structure
5703 5702 * flag - ioctl flags
5704 5703 */
5705 5704 static int
5706 5705 vdc_dioctl_rwcmd(vdc_t *vdc, caddr_t arg, int flag)
5707 5706 {
5708 5707 struct dadkio_rwcmd32 rwcmd32;
5709 5708 struct dadkio_rwcmd rwcmd;
5710 5709 struct iovec aiov;
5711 5710 struct uio auio;
5712 5711 int rw, status;
5713 5712 struct buf *buf;
5714 5713
5715 5714 if (ddi_model_convert_from(flag & FMODELS) == DDI_MODEL_ILP32) {
5716 5715 if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd32,
5717 5716 sizeof (struct dadkio_rwcmd32), flag)) {
5718 5717 return (EFAULT);
5719 5718 }
5720 5719 rwcmd.cmd = rwcmd32.cmd;
5721 5720 rwcmd.flags = rwcmd32.flags;
5722 5721 rwcmd.blkaddr = (daddr_t)rwcmd32.blkaddr;
5723 5722 rwcmd.buflen = rwcmd32.buflen;
5724 5723 rwcmd.bufaddr = (caddr_t)(uintptr_t)rwcmd32.bufaddr;
5725 5724 } else {
5726 5725 if (ddi_copyin((caddr_t)arg, (caddr_t)&rwcmd,
5727 5726 sizeof (struct dadkio_rwcmd), flag)) {
5728 5727 return (EFAULT);
5729 5728 }
5730 5729 }
5731 5730
5732 5731 switch (rwcmd.cmd) {
5733 5732 case DADKIO_RWCMD_READ:
5734 5733 rw = B_READ;
5735 5734 break;
5736 5735 case DADKIO_RWCMD_WRITE:
5737 5736 rw = B_WRITE;
5738 5737 break;
5739 5738 default:
5740 5739 return (EINVAL);
5741 5740 }
5742 5741
5743 5742 bzero((caddr_t)&aiov, sizeof (struct iovec));
5744 5743 aiov.iov_base = rwcmd.bufaddr;
5745 5744 aiov.iov_len = rwcmd.buflen;
5746 5745
5747 5746 bzero((caddr_t)&auio, sizeof (struct uio));
5748 5747 auio.uio_iov = &aiov;
5749 5748 auio.uio_iovcnt = 1;
5750 5749 auio.uio_loffset = rwcmd.blkaddr * vdc->vdisk_bsize;
5751 5750 auio.uio_resid = rwcmd.buflen;
5752 5751 auio.uio_segflg = flag & FKIOCTL ? UIO_SYSSPACE : UIO_USERSPACE;
5753 5752
5754 5753 buf = kmem_alloc(sizeof (buf_t), KM_SLEEP);
5755 5754 bioinit(buf);
5756 5755 /*
5757 5756 * We use the private field of buf to specify that this is an
5758 5757 * I/O using an absolute offset.
5759 5758 */
5760 5759 buf->b_private = (void *)VD_SLICE_NONE;
5761 5760
5762 5761 status = physio(vdc_strategy, buf, VD_MAKE_DEV(vdc->instance, 0),
5763 5762 rw, vdc_min, &auio);
5764 5763
5765 5764 biofini(buf);
5766 5765 kmem_free(buf, sizeof (buf_t));
5767 5766
5768 5767 return (status);
5769 5768 }
5770 5769
5771 5770 /*
5772 5771 * Allocate a buffer for a VD_OP_SCSICMD operation. The size of the allocated
5773 5772 * buffer is returned in alloc_len.
5774 5773 */
5775 5774 static vd_scsi_t *
5776 5775 vdc_scsi_alloc(int cdb_len, int sense_len, int datain_len, int dataout_len,
5777 5776 int *alloc_len)
5778 5777 {
5779 5778 vd_scsi_t *vd_scsi;
5780 5779 int vd_scsi_len = VD_SCSI_SIZE;
5781 5780
5782 5781 vd_scsi_len += P2ROUNDUP(cdb_len, sizeof (uint64_t));
5783 5782 vd_scsi_len += P2ROUNDUP(sense_len, sizeof (uint64_t));
5784 5783 vd_scsi_len += P2ROUNDUP(datain_len, sizeof (uint64_t));
5785 5784 vd_scsi_len += P2ROUNDUP(dataout_len, sizeof (uint64_t));
5786 5785
5787 5786 ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
5788 5787
5789 5788 vd_scsi = kmem_zalloc(vd_scsi_len, KM_SLEEP);
5790 5789
5791 5790 vd_scsi->cdb_len = cdb_len;
5792 5791 vd_scsi->sense_len = sense_len;
5793 5792 vd_scsi->datain_len = datain_len;
5794 5793 vd_scsi->dataout_len = dataout_len;
5795 5794
5796 5795 *alloc_len = vd_scsi_len;
5797 5796
5798 5797 return (vd_scsi);
5799 5798 }
5800 5799
5801 5800 /*
5802 5801 * Convert the status of a SCSI command to a Solaris return code.
5803 5802 *
5804 5803 * Arguments:
5805 5804 * vd_scsi - The SCSI operation buffer.
5806 5805 * log_error - indicate if an error message should be logged.
5807 5806 *
5808 5807 * Note that our SCSI error messages are rather primitive for the moment
5809 5808 * and could be improved by decoding some data like the SCSI command and
5810 5809 * the sense key.
5811 5810 *
5812 5811 * Return value:
5813 5812 * 0 - Status is good.
5814 5813 * EACCES - Status reports a reservation conflict.
5815 5814 * ENOTSUP - Status reports a check condition and sense key
5816 5815 * reports an illegal request.
5817 5816 * EIO - Any other status.
5818 5817 */
5819 5818 static int
5820 5819 vdc_scsi_status(vdc_t *vdc, vd_scsi_t *vd_scsi, boolean_t log_error)
5821 5820 {
5822 5821 int rv;
5823 5822 char path_str[MAXPATHLEN];
5824 5823 char panic_str[VDC_RESV_CONFLICT_FMT_LEN + MAXPATHLEN];
5825 5824 union scsi_cdb *cdb;
5826 5825 struct scsi_extended_sense *sense;
5827 5826
5828 5827 if (vd_scsi->cmd_status == STATUS_GOOD)
5829 5828 /* no error */
5830 5829 return (0);
5831 5830
5832 5831 /* when the tunable vdc_scsi_log_error is true we log all errors */
5833 5832 if (vdc_scsi_log_error)
5834 5833 log_error = B_TRUE;
5835 5834
5836 5835 if (log_error) {
5837 5836 cmn_err(CE_WARN, "%s (vdc%d):\tError for Command: 0x%x)\n",
5838 5837 ddi_pathname(vdc->dip, path_str), vdc->instance,
5839 5838 GETCMD(VD_SCSI_DATA_CDB(vd_scsi)));
5840 5839 }
5841 5840
5842 5841 /* default returned value */
5843 5842 rv = EIO;
5844 5843
5845 5844 switch (vd_scsi->cmd_status) {
5846 5845
5847 5846 case STATUS_CHECK:
5848 5847 case STATUS_TERMINATED:
5849 5848 if (log_error)
5850 5849 cmn_err(CE_CONT, "\tCheck Condition Error\n");
5851 5850
5852 5851 /* check sense buffer */
5853 5852 if (vd_scsi->sense_len == 0 ||
5854 5853 vd_scsi->sense_status != STATUS_GOOD) {
5855 5854 if (log_error)
5856 5855 cmn_err(CE_CONT, "\tNo Sense Data Available\n");
5857 5856 break;
5858 5857 }
5859 5858
5860 5859 sense = VD_SCSI_DATA_SENSE(vd_scsi);
5861 5860
5862 5861 if (log_error) {
5863 5862 cmn_err(CE_CONT, "\tSense Key: 0x%x\n"
5864 5863 "\tASC: 0x%x, ASCQ: 0x%x\n",
5865 5864 scsi_sense_key((uint8_t *)sense),
5866 5865 scsi_sense_asc((uint8_t *)sense),
5867 5866 scsi_sense_ascq((uint8_t *)sense));
5868 5867 }
5869 5868
5870 5869 if (scsi_sense_key((uint8_t *)sense) == KEY_ILLEGAL_REQUEST)
5871 5870 rv = ENOTSUP;
5872 5871 break;
5873 5872
5874 5873 case STATUS_BUSY:
5875 5874 if (log_error)
5876 5875 cmn_err(CE_NOTE, "\tDevice Busy\n");
5877 5876 break;
5878 5877
5879 5878 case STATUS_RESERVATION_CONFLICT:
5880 5879 /*
5881 5880 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then
5882 5881 * reservation conflict could be due to various reasons like
5883 5882 * incorrect keys, not registered or not reserved etc. So,
5884 5883 * we should not panic in that case.
5885 5884 */
5886 5885 cdb = VD_SCSI_DATA_CDB(vd_scsi);
5887 5886 if (vdc->failfast_interval != 0 &&
5888 5887 cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_IN &&
5889 5888 cdb->scc_cmd != SCMD_PERSISTENT_RESERVE_OUT) {
5890 5889 /* failfast is enabled so we have to panic */
5891 5890 (void) snprintf(panic_str, sizeof (panic_str),
5892 5891 VDC_RESV_CONFLICT_FMT_STR "%s",
5893 5892 ddi_pathname(vdc->dip, path_str));
5894 5893 panic(panic_str);
5895 5894 }
5896 5895 if (log_error)
5897 5896 cmn_err(CE_NOTE, "\tReservation Conflict\n");
5898 5897 rv = EACCES;
5899 5898 break;
5900 5899
5901 5900 case STATUS_QFULL:
5902 5901 if (log_error)
5903 5902 cmn_err(CE_NOTE, "\tQueue Full\n");
5904 5903 break;
5905 5904
5906 5905 case STATUS_MET:
5907 5906 case STATUS_INTERMEDIATE:
5908 5907 case STATUS_SCSI2:
5909 5908 case STATUS_INTERMEDIATE_MET:
5910 5909 case STATUS_ACA_ACTIVE:
5911 5910 if (log_error)
5912 5911 cmn_err(CE_CONT,
5913 5912 "\tUnexpected SCSI status received: 0x%x\n",
5914 5913 vd_scsi->cmd_status);
5915 5914 break;
5916 5915
5917 5916 default:
5918 5917 if (log_error)
5919 5918 cmn_err(CE_CONT,
5920 5919 "\tInvalid SCSI status received: 0x%x\n",
5921 5920 vd_scsi->cmd_status);
5922 5921 break;
5923 5922 }
5924 5923
5925 5924 return (rv);
5926 5925 }
5927 5926
5928 5927 /*
5929 5928 * Implemented the USCSICMD uscsi(7I) ioctl. This ioctl is converted to
5930 5929 * a VD_OP_SCSICMD operation which is sent to the vdisk server. If a SCSI
5931 5930 * reset is requested (i.e. a flag USCSI_RESET* is set) then the ioctl is
5932 5931 * converted to a VD_OP_RESET operation.
5933 5932 */
5934 5933 static int
5935 5934 vdc_uscsi_cmd(vdc_t *vdc, caddr_t arg, int mode)
5936 5935 {
5937 5936 struct uscsi_cmd uscsi;
5938 5937 struct uscsi_cmd32 uscsi32;
5939 5938 vd_scsi_t *vd_scsi;
5940 5939 int vd_scsi_len;
5941 5940 union scsi_cdb *cdb;
5942 5941 struct scsi_extended_sense *sense;
5943 5942 char *datain, *dataout;
5944 5943 size_t cdb_len, datain_len, dataout_len, sense_len;
5945 5944 int rv;
5946 5945
5947 5946 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
5948 5947 if (ddi_copyin(arg, &uscsi32, sizeof (struct uscsi_cmd32),
5949 5948 mode) != 0)
5950 5949 return (EFAULT);
5951 5950 uscsi_cmd32touscsi_cmd((&uscsi32), (&uscsi));
5952 5951 } else {
5953 5952 if (ddi_copyin(arg, &uscsi, sizeof (struct uscsi_cmd),
5954 5953 mode) != 0)
5955 5954 return (EFAULT);
5956 5955 }
5957 5956
5958 5957 /* a uscsi reset is converted to a VD_OP_RESET operation */
5959 5958 if (uscsi.uscsi_flags & (USCSI_RESET | USCSI_RESET_LUN |
5960 5959 USCSI_RESET_ALL)) {
5961 5960 rv = vdc_do_sync_op(vdc, VD_OP_RESET, NULL, 0, 0, 0,
5962 5961 VIO_both_dir, B_TRUE);
5963 5962 return (rv);
5964 5963 }
5965 5964
5966 5965 /* cdb buffer length */
5967 5966 cdb_len = uscsi.uscsi_cdblen;
5968 5967
5969 5968 /* data in and out buffers length */
5970 5969 if (uscsi.uscsi_flags & USCSI_READ) {
5971 5970 datain_len = uscsi.uscsi_buflen;
5972 5971 dataout_len = 0;
5973 5972 } else {
5974 5973 datain_len = 0;
5975 5974 dataout_len = uscsi.uscsi_buflen;
5976 5975 }
5977 5976
5978 5977 /* sense buffer length */
5979 5978 if (uscsi.uscsi_flags & USCSI_RQENABLE)
5980 5979 sense_len = uscsi.uscsi_rqlen;
5981 5980 else
5982 5981 sense_len = 0;
5983 5982
5984 5983 /* allocate buffer for the VD_SCSICMD_OP operation */
5985 5984 vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
5986 5985 &vd_scsi_len);
5987 5986
5988 5987 /*
5989 5988 * The documentation of USCSI_ISOLATE and USCSI_DIAGNOSE is very vague,
5990 5989 * but basically they prevent a SCSI command from being retried in case
5991 5990 * of an error.
5992 5991 */
5993 5992 if ((uscsi.uscsi_flags & USCSI_ISOLATE) ||
5994 5993 (uscsi.uscsi_flags & USCSI_DIAGNOSE))
5995 5994 vd_scsi->options |= VD_SCSI_OPT_NORETRY;
5996 5995
5997 5996 /* set task attribute */
5998 5997 if (uscsi.uscsi_flags & USCSI_NOTAG) {
5999 5998 vd_scsi->task_attribute = 0;
6000 5999 } else {
6001 6000 if (uscsi.uscsi_flags & USCSI_HEAD)
6002 6001 vd_scsi->task_attribute = VD_SCSI_TASK_ACA;
6003 6002 else if (uscsi.uscsi_flags & USCSI_HTAG)
6004 6003 vd_scsi->task_attribute = VD_SCSI_TASK_HQUEUE;
6005 6004 else if (uscsi.uscsi_flags & USCSI_OTAG)
6006 6005 vd_scsi->task_attribute = VD_SCSI_TASK_ORDERED;
6007 6006 else
6008 6007 vd_scsi->task_attribute = 0;
6009 6008 }
6010 6009
6011 6010 /* set timeout */
6012 6011 vd_scsi->timeout = uscsi.uscsi_timeout;
6013 6012
6014 6013 /* copy-in cdb data */
6015 6014 cdb = VD_SCSI_DATA_CDB(vd_scsi);
6016 6015 if (ddi_copyin(uscsi.uscsi_cdb, cdb, cdb_len, mode) != 0) {
6017 6016 rv = EFAULT;
6018 6017 goto done;
6019 6018 }
6020 6019
6021 6020 /* keep a pointer to the sense buffer */
6022 6021 sense = VD_SCSI_DATA_SENSE(vd_scsi);
6023 6022
6024 6023 /* keep a pointer to the data-in buffer */
6025 6024 datain = (char *)VD_SCSI_DATA_IN(vd_scsi);
6026 6025
6027 6026 /* copy-in request data to the data-out buffer */
6028 6027 dataout = (char *)VD_SCSI_DATA_OUT(vd_scsi);
6029 6028 if (!(uscsi.uscsi_flags & USCSI_READ)) {
6030 6029 if (ddi_copyin(uscsi.uscsi_bufaddr, dataout, dataout_len,
6031 6030 mode)) {
6032 6031 rv = EFAULT;
6033 6032 goto done;
6034 6033 }
6035 6034 }
6036 6035
6037 6036 /* submit the request */
6038 6037 rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
6039 6038 0, 0, VIO_both_dir, B_FALSE);
6040 6039
6041 6040 if (rv != 0)
6042 6041 goto done;
6043 6042
6044 6043 /* update scsi status */
6045 6044 uscsi.uscsi_status = vd_scsi->cmd_status;
6046 6045
6047 6046 /* update sense data */
6048 6047 if ((uscsi.uscsi_flags & USCSI_RQENABLE) &&
6049 6048 (uscsi.uscsi_status == STATUS_CHECK ||
6050 6049 uscsi.uscsi_status == STATUS_TERMINATED)) {
6051 6050
6052 6051 uscsi.uscsi_rqstatus = vd_scsi->sense_status;
6053 6052
6054 6053 if (uscsi.uscsi_rqstatus == STATUS_GOOD) {
6055 6054 uscsi.uscsi_rqresid = uscsi.uscsi_rqlen -
6056 6055 vd_scsi->sense_len;
6057 6056 if (ddi_copyout(sense, uscsi.uscsi_rqbuf,
6058 6057 vd_scsi->sense_len, mode) != 0) {
6059 6058 rv = EFAULT;
6060 6059 goto done;
6061 6060 }
6062 6061 }
6063 6062 }
6064 6063
6065 6064 /* update request data */
6066 6065 if (uscsi.uscsi_status == STATUS_GOOD) {
6067 6066 if (uscsi.uscsi_flags & USCSI_READ) {
6068 6067 uscsi.uscsi_resid = uscsi.uscsi_buflen -
6069 6068 vd_scsi->datain_len;
6070 6069 if (ddi_copyout(datain, uscsi.uscsi_bufaddr,
6071 6070 vd_scsi->datain_len, mode) != 0) {
6072 6071 rv = EFAULT;
6073 6072 goto done;
6074 6073 }
6075 6074 } else {
6076 6075 uscsi.uscsi_resid = uscsi.uscsi_buflen -
6077 6076 vd_scsi->dataout_len;
6078 6077 }
6079 6078 }
6080 6079
6081 6080 /* copy-out result */
6082 6081 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
6083 6082 uscsi_cmdtouscsi_cmd32((&uscsi), (&uscsi32));
6084 6083 if (ddi_copyout(&uscsi32, arg, sizeof (struct uscsi_cmd32),
6085 6084 mode) != 0) {
6086 6085 rv = EFAULT;
6087 6086 goto done;
6088 6087 }
6089 6088 } else {
6090 6089 if (ddi_copyout(&uscsi, arg, sizeof (struct uscsi_cmd),
6091 6090 mode) != 0) {
6092 6091 rv = EFAULT;
6093 6092 goto done;
6094 6093 }
6095 6094 }
6096 6095
6097 6096 /* get the return code from the SCSI command status */
6098 6097 rv = vdc_scsi_status(vdc, vd_scsi,
6099 6098 !(uscsi.uscsi_flags & USCSI_SILENT));
6100 6099
6101 6100 done:
6102 6101 kmem_free(vd_scsi, vd_scsi_len);
6103 6102 return (rv);
6104 6103 }
6105 6104
6106 6105 /*
6107 6106 * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT IN command.
6108 6107 *
6109 6108 * Arguments:
6110 6109 * cmd - SCSI PERSISTENT IN command
6111 6110 * len - length of the SCSI input buffer
6112 6111 * vd_scsi_len - return the length of the allocated buffer
6113 6112 *
6114 6113 * Returned Value:
6115 6114 * a pointer to the allocated VD_OP_SCSICMD buffer.
6116 6115 */
6117 6116 static vd_scsi_t *
6118 6117 vdc_scsi_alloc_persistent_in(uchar_t cmd, int len, int *vd_scsi_len)
6119 6118 {
6120 6119 int cdb_len, sense_len, datain_len, dataout_len;
6121 6120 vd_scsi_t *vd_scsi;
6122 6121 union scsi_cdb *cdb;
6123 6122
6124 6123 cdb_len = CDB_GROUP1;
6125 6124 sense_len = sizeof (struct scsi_extended_sense);
6126 6125 datain_len = len;
6127 6126 dataout_len = 0;
6128 6127
6129 6128 vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
6130 6129 vd_scsi_len);
6131 6130
6132 6131 cdb = VD_SCSI_DATA_CDB(vd_scsi);
6133 6132
6134 6133 /* set cdb */
6135 6134 cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
6136 6135 cdb->cdb_opaque[1] = cmd;
6137 6136 FORMG1COUNT(cdb, datain_len);
6138 6137
6139 6138 vd_scsi->timeout = vdc_scsi_timeout;
6140 6139
6141 6140 return (vd_scsi);
6142 6141 }
6143 6142
6144 6143 /*
6145 6144 * Create a VD_OP_SCSICMD buffer for a SCSI PERSISTENT OUT command.
6146 6145 *
6147 6146 * Arguments:
6148 6147 * cmd - SCSI PERSISTENT OUT command
6149 6148 * len - length of the SCSI output buffer
6150 6149 * vd_scsi_len - return the length of the allocated buffer
6151 6150 *
6152 6151 * Returned Code:
6153 6152 * a pointer to the allocated VD_OP_SCSICMD buffer.
6154 6153 */
6155 6154 static vd_scsi_t *
6156 6155 vdc_scsi_alloc_persistent_out(uchar_t cmd, int len, int *vd_scsi_len)
6157 6156 {
6158 6157 int cdb_len, sense_len, datain_len, dataout_len;
6159 6158 vd_scsi_t *vd_scsi;
6160 6159 union scsi_cdb *cdb;
6161 6160
6162 6161 cdb_len = CDB_GROUP1;
6163 6162 sense_len = sizeof (struct scsi_extended_sense);
6164 6163 datain_len = 0;
6165 6164 dataout_len = len;
6166 6165
6167 6166 vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, datain_len, dataout_len,
6168 6167 vd_scsi_len);
6169 6168
6170 6169 cdb = VD_SCSI_DATA_CDB(vd_scsi);
6171 6170
6172 6171 /* set cdb */
6173 6172 cdb->scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
6174 6173 cdb->cdb_opaque[1] = cmd;
6175 6174 FORMG1COUNT(cdb, dataout_len);
6176 6175
6177 6176 vd_scsi->timeout = vdc_scsi_timeout;
6178 6177
6179 6178 return (vd_scsi);
6180 6179 }
6181 6180
6182 6181 /*
6183 6182 * Implement the MHIOCGRP_INKEYS mhd(7i) ioctl. The ioctl is converted
6184 6183 * to a SCSI PERSISTENT IN READ KEYS command which is sent to the vdisk
6185 6184 * server with a VD_OP_SCSICMD operation.
6186 6185 */
6187 6186 static int
6188 6187 vdc_mhd_inkeys(vdc_t *vdc, caddr_t arg, int mode)
6189 6188 {
6190 6189 vd_scsi_t *vd_scsi;
6191 6190 mhioc_inkeys_t inkeys;
6192 6191 mhioc_key_list_t klist;
6193 6192 struct mhioc_inkeys32 inkeys32;
6194 6193 struct mhioc_key_list32 klist32;
6195 6194 sd_prin_readkeys_t *scsi_keys;
6196 6195 void *user_keys;
6197 6196 int vd_scsi_len;
6198 6197 int listsize, listlen, rv;
6199 6198
6200 6199 /* copyin arguments */
6201 6200 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
6202 6201 rv = ddi_copyin(arg, &inkeys32, sizeof (inkeys32), mode);
6203 6202 if (rv != 0)
6204 6203 return (EFAULT);
6205 6204
6206 6205 rv = ddi_copyin((caddr_t)(uintptr_t)inkeys32.li, &klist32,
6207 6206 sizeof (klist32), mode);
6208 6207 if (rv != 0)
6209 6208 return (EFAULT);
6210 6209
6211 6210 listsize = klist32.listsize;
6212 6211 } else {
6213 6212 rv = ddi_copyin(arg, &inkeys, sizeof (inkeys), mode);
6214 6213 if (rv != 0)
6215 6214 return (EFAULT);
6216 6215
6217 6216 rv = ddi_copyin(inkeys.li, &klist, sizeof (klist), mode);
6218 6217 if (rv != 0)
6219 6218 return (EFAULT);
6220 6219
6221 6220 listsize = klist.listsize;
6222 6221 }
6223 6222
6224 6223 /* build SCSI VD_OP request */
6225 6224 vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_KEYS,
6226 6225 sizeof (sd_prin_readkeys_t) - sizeof (caddr_t) +
6227 6226 (sizeof (mhioc_resv_key_t) * listsize), &vd_scsi_len);
6228 6227
6229 6228 scsi_keys = (sd_prin_readkeys_t *)VD_SCSI_DATA_IN(vd_scsi);
6230 6229
6231 6230 /* submit the request */
6232 6231 rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
6233 6232 0, 0, VIO_both_dir, B_FALSE);
6234 6233
6235 6234 if (rv != 0)
6236 6235 goto done;
6237 6236
6238 6237 listlen = scsi_keys->len / MHIOC_RESV_KEY_SIZE;
6239 6238
6240 6239 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
6241 6240 inkeys32.generation = scsi_keys->generation;
6242 6241 rv = ddi_copyout(&inkeys32, arg, sizeof (inkeys32), mode);
6243 6242 if (rv != 0) {
6244 6243 rv = EFAULT;
6245 6244 goto done;
6246 6245 }
6247 6246
6248 6247 klist32.listlen = listlen;
6249 6248 rv = ddi_copyout(&klist32, (caddr_t)(uintptr_t)inkeys32.li,
6250 6249 sizeof (klist32), mode);
6251 6250 if (rv != 0) {
6252 6251 rv = EFAULT;
6253 6252 goto done;
6254 6253 }
6255 6254
6256 6255 user_keys = (caddr_t)(uintptr_t)klist32.list;
6257 6256 } else {
6258 6257 inkeys.generation = scsi_keys->generation;
6259 6258 rv = ddi_copyout(&inkeys, arg, sizeof (inkeys), mode);
6260 6259 if (rv != 0) {
6261 6260 rv = EFAULT;
6262 6261 goto done;
6263 6262 }
6264 6263
6265 6264 klist.listlen = listlen;
6266 6265 rv = ddi_copyout(&klist, inkeys.li, sizeof (klist), mode);
6267 6266 if (rv != 0) {
6268 6267 rv = EFAULT;
6269 6268 goto done;
6270 6269 }
6271 6270
6272 6271 user_keys = klist.list;
6273 6272 }
6274 6273
6275 6274 /* copy out keys */
6276 6275 if (listlen > 0 && listsize > 0) {
6277 6276 if (listsize < listlen)
6278 6277 listlen = listsize;
6279 6278 rv = ddi_copyout(&scsi_keys->keylist, user_keys,
6280 6279 listlen * MHIOC_RESV_KEY_SIZE, mode);
6281 6280 if (rv != 0)
6282 6281 rv = EFAULT;
6283 6282 }
6284 6283
6285 6284 if (rv == 0)
6286 6285 rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
6287 6286
6288 6287 done:
6289 6288 kmem_free(vd_scsi, vd_scsi_len);
6290 6289
6291 6290 return (rv);
6292 6291 }
6293 6292
6294 6293 /*
6295 6294 * Implement the MHIOCGRP_INRESV mhd(7i) ioctl. The ioctl is converted
6296 6295 * to a SCSI PERSISTENT IN READ RESERVATION command which is sent to
6297 6296 * the vdisk server with a VD_OP_SCSICMD operation.
6298 6297 */
6299 6298 static int
6300 6299 vdc_mhd_inresv(vdc_t *vdc, caddr_t arg, int mode)
6301 6300 {
6302 6301 vd_scsi_t *vd_scsi;
6303 6302 mhioc_inresvs_t inresv;
6304 6303 mhioc_resv_desc_list_t rlist;
6305 6304 struct mhioc_inresvs32 inresv32;
6306 6305 struct mhioc_resv_desc_list32 rlist32;
6307 6306 mhioc_resv_desc_t mhd_resv;
6308 6307 sd_prin_readresv_t *scsi_resv;
6309 6308 sd_readresv_desc_t *resv;
6310 6309 mhioc_resv_desc_t *user_resv;
6311 6310 int vd_scsi_len;
6312 6311 int listsize, listlen, i, rv;
6313 6312
6314 6313 /* copyin arguments */
6315 6314 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
6316 6315 rv = ddi_copyin(arg, &inresv32, sizeof (inresv32), mode);
6317 6316 if (rv != 0)
6318 6317 return (EFAULT);
6319 6318
6320 6319 rv = ddi_copyin((caddr_t)(uintptr_t)inresv32.li, &rlist32,
6321 6320 sizeof (rlist32), mode);
6322 6321 if (rv != 0)
6323 6322 return (EFAULT);
6324 6323
6325 6324 listsize = rlist32.listsize;
6326 6325 } else {
6327 6326 rv = ddi_copyin(arg, &inresv, sizeof (inresv), mode);
6328 6327 if (rv != 0)
6329 6328 return (EFAULT);
6330 6329
6331 6330 rv = ddi_copyin(inresv.li, &rlist, sizeof (rlist), mode);
6332 6331 if (rv != 0)
6333 6332 return (EFAULT);
6334 6333
6335 6334 listsize = rlist.listsize;
6336 6335 }
6337 6336
6338 6337 /* build SCSI VD_OP request */
6339 6338 vd_scsi = vdc_scsi_alloc_persistent_in(SD_READ_RESV,
6340 6339 sizeof (sd_prin_readresv_t) - sizeof (caddr_t) +
6341 6340 (SCSI3_RESV_DESC_LEN * listsize), &vd_scsi_len);
6342 6341
6343 6342 scsi_resv = (sd_prin_readresv_t *)VD_SCSI_DATA_IN(vd_scsi);
6344 6343
6345 6344 /* submit the request */
6346 6345 rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
6347 6346 0, 0, VIO_both_dir, B_FALSE);
6348 6347
6349 6348 if (rv != 0)
6350 6349 goto done;
6351 6350
6352 6351 listlen = scsi_resv->len / SCSI3_RESV_DESC_LEN;
6353 6352
6354 6353 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
6355 6354 inresv32.generation = scsi_resv->generation;
6356 6355 rv = ddi_copyout(&inresv32, arg, sizeof (inresv32), mode);
6357 6356 if (rv != 0) {
6358 6357 rv = EFAULT;
6359 6358 goto done;
6360 6359 }
6361 6360
6362 6361 rlist32.listlen = listlen;
6363 6362 rv = ddi_copyout(&rlist32, (caddr_t)(uintptr_t)inresv32.li,
6364 6363 sizeof (rlist32), mode);
6365 6364 if (rv != 0) {
6366 6365 rv = EFAULT;
6367 6366 goto done;
6368 6367 }
6369 6368
6370 6369 user_resv = (mhioc_resv_desc_t *)(uintptr_t)rlist32.list;
6371 6370 } else {
6372 6371 inresv.generation = scsi_resv->generation;
6373 6372 rv = ddi_copyout(&inresv, arg, sizeof (inresv), mode);
6374 6373 if (rv != 0) {
6375 6374 rv = EFAULT;
6376 6375 goto done;
6377 6376 }
6378 6377
6379 6378 rlist.listlen = listlen;
6380 6379 rv = ddi_copyout(&rlist, inresv.li, sizeof (rlist), mode);
6381 6380 if (rv != 0) {
6382 6381 rv = EFAULT;
6383 6382 goto done;
6384 6383 }
6385 6384
6386 6385 user_resv = rlist.list;
6387 6386 }
6388 6387
6389 6388 /* copy out reservations */
6390 6389 if (listsize > 0 && listlen > 0) {
6391 6390 if (listsize < listlen)
6392 6391 listlen = listsize;
6393 6392 resv = (sd_readresv_desc_t *)&scsi_resv->readresv_desc;
6394 6393
6395 6394 for (i = 0; i < listlen; i++) {
6396 6395 mhd_resv.type = resv->type;
6397 6396 mhd_resv.scope = resv->scope;
6398 6397 mhd_resv.scope_specific_addr =
6399 6398 BE_32(resv->scope_specific_addr);
6400 6399 bcopy(&resv->resvkey, &mhd_resv.key,
6401 6400 MHIOC_RESV_KEY_SIZE);
6402 6401
6403 6402 rv = ddi_copyout(&mhd_resv, user_resv,
6404 6403 sizeof (mhd_resv), mode);
6405 6404 if (rv != 0) {
6406 6405 rv = EFAULT;
6407 6406 goto done;
6408 6407 }
6409 6408 resv++;
6410 6409 user_resv++;
6411 6410 }
6412 6411 }
6413 6412
6414 6413 if (rv == 0)
6415 6414 rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
6416 6415
6417 6416 done:
6418 6417 kmem_free(vd_scsi, vd_scsi_len);
6419 6418 return (rv);
6420 6419 }
6421 6420
6422 6421 /*
6423 6422 * Implement the MHIOCGRP_REGISTER mhd(7i) ioctl. The ioctl is converted
6424 6423 * to a SCSI PERSISTENT OUT REGISTER command which is sent to the vdisk
6425 6424 * server with a VD_OP_SCSICMD operation.
6426 6425 */
6427 6426 static int
6428 6427 vdc_mhd_register(vdc_t *vdc, caddr_t arg, int mode)
6429 6428 {
6430 6429 vd_scsi_t *vd_scsi;
6431 6430 sd_prout_t *scsi_prout;
6432 6431 mhioc_register_t mhd_reg;
6433 6432 int vd_scsi_len, rv;
6434 6433
6435 6434 /* copyin arguments */
6436 6435 rv = ddi_copyin(arg, &mhd_reg, sizeof (mhd_reg), mode);
6437 6436 if (rv != 0)
6438 6437 return (EFAULT);
6439 6438
6440 6439 /* build SCSI VD_OP request */
6441 6440 vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTER,
6442 6441 sizeof (sd_prout_t), &vd_scsi_len);
6443 6442
6444 6443 /* set parameters */
6445 6444 scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
6446 6445 bcopy(mhd_reg.oldkey.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE);
6447 6446 bcopy(mhd_reg.newkey.key, scsi_prout->service_key, MHIOC_RESV_KEY_SIZE);
6448 6447 scsi_prout->aptpl = (uchar_t)mhd_reg.aptpl;
6449 6448
6450 6449 /* submit the request */
6451 6450 rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
6452 6451 0, 0, VIO_both_dir, B_FALSE);
6453 6452
6454 6453 if (rv == 0)
6455 6454 rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
6456 6455
6457 6456 kmem_free(vd_scsi, vd_scsi_len);
6458 6457 return (rv);
6459 6458 }
6460 6459
6461 6460 /*
6462 6461 * Implement the MHIOCGRP_RESERVE mhd(7i) ioctl. The ioctl is converted
6463 6462 * to a SCSI PERSISTENT OUT RESERVE command which is sent to the vdisk
6464 6463 * server with a VD_OP_SCSICMD operation.
6465 6464 */
6466 6465 static int
6467 6466 vdc_mhd_reserve(vdc_t *vdc, caddr_t arg, int mode)
6468 6467 {
6469 6468 union scsi_cdb *cdb;
6470 6469 vd_scsi_t *vd_scsi;
6471 6470 sd_prout_t *scsi_prout;
6472 6471 mhioc_resv_desc_t mhd_resv;
6473 6472 int vd_scsi_len, rv;
6474 6473
6475 6474 /* copyin arguments */
6476 6475 rv = ddi_copyin(arg, &mhd_resv, sizeof (mhd_resv), mode);
6477 6476 if (rv != 0)
6478 6477 return (EFAULT);
6479 6478
6480 6479 /* build SCSI VD_OP request */
6481 6480 vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_RESERVE,
6482 6481 sizeof (sd_prout_t), &vd_scsi_len);
6483 6482
6484 6483 /* set parameters */
6485 6484 cdb = VD_SCSI_DATA_CDB(vd_scsi);
6486 6485 scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
6487 6486 bcopy(mhd_resv.key.key, scsi_prout->res_key, MHIOC_RESV_KEY_SIZE);
6488 6487 scsi_prout->scope_address = mhd_resv.scope_specific_addr;
6489 6488 cdb->cdb_opaque[2] = mhd_resv.type;
6490 6489
6491 6490 /* submit the request */
6492 6491 rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
6493 6492 0, 0, VIO_both_dir, B_FALSE);
6494 6493
6495 6494 if (rv == 0)
6496 6495 rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
6497 6496
6498 6497 kmem_free(vd_scsi, vd_scsi_len);
6499 6498 return (rv);
6500 6499 }
6501 6500
6502 6501 /*
6503 6502 * Implement the MHIOCGRP_PREEMPTANDABORT mhd(7i) ioctl. The ioctl is
6504 6503 * converted to a SCSI PERSISTENT OUT PREEMPT AND ABORT command which
6505 6504 * is sent to the vdisk server with a VD_OP_SCSICMD operation.
6506 6505 */
6507 6506 static int
6508 6507 vdc_mhd_preemptabort(vdc_t *vdc, caddr_t arg, int mode)
6509 6508 {
6510 6509 union scsi_cdb *cdb;
6511 6510 vd_scsi_t *vd_scsi;
6512 6511 sd_prout_t *scsi_prout;
6513 6512 mhioc_preemptandabort_t mhd_preempt;
6514 6513 int vd_scsi_len, rv;
6515 6514
6516 6515 /* copyin arguments */
6517 6516 rv = ddi_copyin(arg, &mhd_preempt, sizeof (mhd_preempt), mode);
6518 6517 if (rv != 0)
6519 6518 return (EFAULT);
6520 6519
6521 6520 /* build SCSI VD_OP request */
6522 6521 vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_PREEMPTANDABORT,
6523 6522 sizeof (sd_prout_t), &vd_scsi_len);
6524 6523
6525 6524 /* set parameters */
6526 6525 vd_scsi->task_attribute = VD_SCSI_TASK_ACA;
6527 6526 cdb = VD_SCSI_DATA_CDB(vd_scsi);
6528 6527 scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
6529 6528 bcopy(mhd_preempt.resvdesc.key.key, scsi_prout->res_key,
6530 6529 MHIOC_RESV_KEY_SIZE);
6531 6530 bcopy(mhd_preempt.victim_key.key, scsi_prout->service_key,
6532 6531 MHIOC_RESV_KEY_SIZE);
6533 6532 scsi_prout->scope_address = mhd_preempt.resvdesc.scope_specific_addr;
6534 6533 cdb->cdb_opaque[2] = mhd_preempt.resvdesc.type;
6535 6534
6536 6535 /* submit the request */
6537 6536 rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
6538 6537 0, 0, VIO_both_dir, B_FALSE);
6539 6538
6540 6539 if (rv == 0)
6541 6540 rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
6542 6541
6543 6542 kmem_free(vd_scsi, vd_scsi_len);
6544 6543 return (rv);
6545 6544 }
6546 6545
6547 6546 /*
6548 6547 * Implement the MHIOCGRP_REGISTERANDIGNOREKEY mhd(7i) ioctl. The ioctl
6549 6548 * is converted to a SCSI PERSISTENT OUT REGISTER AND IGNORE EXISTING KEY
6550 6549 * command which is sent to the vdisk server with a VD_OP_SCSICMD operation.
6551 6550 */
6552 6551 static int
6553 6552 vdc_mhd_registerignore(vdc_t *vdc, caddr_t arg, int mode)
6554 6553 {
6555 6554 vd_scsi_t *vd_scsi;
6556 6555 sd_prout_t *scsi_prout;
6557 6556 mhioc_registerandignorekey_t mhd_regi;
6558 6557 int vd_scsi_len, rv;
6559 6558
6560 6559 /* copyin arguments */
6561 6560 rv = ddi_copyin(arg, &mhd_regi, sizeof (mhd_regi), mode);
6562 6561 if (rv != 0)
6563 6562 return (EFAULT);
6564 6563
6565 6564 /* build SCSI VD_OP request */
6566 6565 vd_scsi = vdc_scsi_alloc_persistent_out(SD_SCSI3_REGISTERANDIGNOREKEY,
6567 6566 sizeof (sd_prout_t), &vd_scsi_len);
6568 6567
6569 6568 /* set parameters */
6570 6569 scsi_prout = (sd_prout_t *)VD_SCSI_DATA_OUT(vd_scsi);
6571 6570 bcopy(mhd_regi.newkey.key, scsi_prout->service_key,
6572 6571 MHIOC_RESV_KEY_SIZE);
6573 6572 scsi_prout->aptpl = (uchar_t)mhd_regi.aptpl;
6574 6573
6575 6574 /* submit the request */
6576 6575 rv = vdc_do_sync_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
6577 6576 0, 0, VIO_both_dir, B_FALSE);
6578 6577
6579 6578 if (rv == 0)
6580 6579 rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
6581 6580
6582 6581 kmem_free(vd_scsi, vd_scsi_len);
6583 6582 return (rv);
6584 6583 }
6585 6584
6586 6585 /*
6587 6586 * This function is used to send a (simple) SCSI command and check errors.
6588 6587 */
6589 6588 static int
6590 6589 vdc_eio_scsi_cmd(vdc_t *vdc, uchar_t scmd, int flags)
6591 6590 {
6592 6591 int cdb_len, sense_len, vd_scsi_len;
6593 6592 vd_scsi_t *vd_scsi;
6594 6593 union scsi_cdb *cdb;
6595 6594 int rv;
6596 6595
6597 6596 ASSERT(scmd == SCMD_TEST_UNIT_READY || scmd == SCMD_WRITE_G1);
6598 6597
6599 6598 if (scmd == SCMD_WRITE_G1)
6600 6599 cdb_len = CDB_GROUP1;
6601 6600 else
6602 6601 cdb_len = CDB_GROUP0;
6603 6602
6604 6603 sense_len = sizeof (struct scsi_extended_sense);
6605 6604
6606 6605 vd_scsi = vdc_scsi_alloc(cdb_len, sense_len, 0, 0, &vd_scsi_len);
6607 6606
6608 6607 /* set cdb */
6609 6608 cdb = VD_SCSI_DATA_CDB(vd_scsi);
6610 6609 cdb->scc_cmd = scmd;
6611 6610
6612 6611 vd_scsi->timeout = vdc_scsi_timeout;
6613 6612
6614 6613 /*
6615 6614 * Submit the request. Note the operation should not request that any
6616 6615 * error is checked because this function is precisely called when
6617 6616 * checking errors.
6618 6617 */
6619 6618 ASSERT((flags & VDC_OP_ERRCHK) == 0);
6620 6619
6621 6620 rv = vdc_do_op(vdc, VD_OP_SCSICMD, (caddr_t)vd_scsi, vd_scsi_len,
6622 6621 0, 0, NULL, VIO_both_dir, flags);
6623 6622
6624 6623 if (rv == 0)
6625 6624 rv = vdc_scsi_status(vdc, vd_scsi, B_FALSE);
6626 6625
6627 6626 kmem_free(vd_scsi, vd_scsi_len);
6628 6627 return (rv);
6629 6628 }
6630 6629
6631 6630 /*
6632 6631 * This function is used to check if a SCSI backend is accessible. It will
6633 6632 * also detect reservation conflict if failfast is enabled, and panic the
6634 6633 * system in that case.
6635 6634 *
6636 6635 * Returned Code:
6637 6636 * 0 - disk is accessible
6638 6637 * != 0 - disk is inaccessible or unable to check if disk is accessible
6639 6638 */
6640 6639 static int
6641 6640 vdc_eio_scsi_check(vdc_t *vdc, int flags)
6642 6641 {
6643 6642 int failure = 0;
6644 6643 int rv;
6645 6644
6646 6645 /*
6647 6646 * Send a TEST UNIT READY command. The command will panic
6648 6647 * the system if it fails with a reservation conflict and
6649 6648 * failfast is enabled. If there is a reservation conflict
6650 6649 * and failfast is not enabled then the function will return
6651 6650 * EACCES. In that case, there's no problem with accessing
6652 6651 * the backend, it is just reserved.
6653 6652 */
6654 6653 rv = vdc_eio_scsi_cmd(vdc, SCMD_TEST_UNIT_READY, flags);
6655 6654 if (rv != 0 && rv != EACCES)
6656 6655 failure++;
6657 6656
6658 6657 /* we don't need to do more checking if failfast is not enabled */
6659 6658 if (vdc->failfast_interval == 0)
6660 6659 return (failure);
6661 6660
6662 6661 /*
6663 6662 * With SPC-3 compliant devices TEST UNIT READY will succeed on
6664 6663 * a reserved device, so we also do a WRITE(10) of zero byte in
6665 6664 * order to provoke a Reservation Conflict status on those newer
6666 6665 * devices.
6667 6666 */
6668 6667 if (vdc_eio_scsi_cmd(vdc, SCMD_WRITE_G1, flags) != 0)
6669 6668 failure++;
6670 6669
6671 6670 return (failure);
6672 6671 }
6673 6672
6674 6673 /*
6675 6674 * This function is used to check if a backend is effectively accessible.
6676 6675 *
6677 6676 * Returned Code:
6678 6677 * 0 - disk is accessible
6679 6678 * != 0 - disk is inaccessible or unable to check if disk is accessible
6680 6679 */
6681 6680 static int
6682 6681 vdc_eio_check(vdc_t *vdc, int flags)
6683 6682 {
6684 6683 char *buffer;
6685 6684 diskaddr_t blkno;
6686 6685 int rv;
6687 6686
6688 6687 ASSERT((flags & VDC_OP_ERRCHK) == 0);
6689 6688
6690 6689 flags |= VDC_OP_DRING_RESERVED;
6691 6690
6692 6691 if (VD_OP_SUPPORTED(vdc->operations, VD_OP_SCSICMD))
6693 6692 return (vdc_eio_scsi_check(vdc, flags));
6694 6693
6695 6694 ASSERT(vdc->failfast_interval == 0);
6696 6695
6697 6696 /*
6698 6697 * If the backend does not support SCSI operations then we simply
6699 6698 * check if the backend is accessible by reading some data blocks.
6700 6699 * We first try to read a random block, to try to avoid getting
6701 6700 * a block that might have been cached on the service domain. Then
6702 6701 * we try the last block, and finally the first block.
6703 6702 *
6704 6703 * We return success as soon as we are able to read any block.
6705 6704 */
6706 6705 buffer = kmem_alloc(vdc->vdisk_bsize, KM_SLEEP);
6707 6706
6708 6707 if (vdc->vdisk_size > 0) {
6709 6708
6710 6709 /* try a random block */
6711 6710 (void) random_get_pseudo_bytes((uint8_t *)&blkno,
6712 6711 sizeof (diskaddr_t));
6713 6712 blkno = blkno % vdc->vdisk_size;
6714 6713 rv = vdc_do_op(vdc, VD_OP_BREAD, (caddr_t)buffer,
6715 6714 vdc->vdisk_bsize, VD_SLICE_NONE, blkno, NULL,
6716 6715 VIO_read_dir, flags);
6717 6716
6718 6717 if (rv == 0)
6719 6718 goto done;
6720 6719
6721 6720 /* try the last block */
6722 6721 blkno = vdc->vdisk_size - 1;
6723 6722 rv = vdc_do_op(vdc, VD_OP_BREAD, (caddr_t)buffer,
6724 6723 vdc->vdisk_bsize, VD_SLICE_NONE, blkno, NULL,
6725 6724 VIO_read_dir, flags);
6726 6725
6727 6726 if (rv == 0)
6728 6727 goto done;
6729 6728 }
6730 6729
6731 6730 /* try block 0 */
6732 6731 blkno = 0;
6733 6732 rv = vdc_do_op(vdc, VD_OP_BREAD, (caddr_t)buffer, vdc->vdisk_bsize,
6734 6733 VD_SLICE_NONE, blkno, NULL, VIO_read_dir, flags);
6735 6734
6736 6735 done:
6737 6736 kmem_free(buffer, vdc->vdisk_bsize);
6738 6737 return (rv);
6739 6738 }
6740 6739
6741 6740 /*
6742 6741 * Add a pending I/O to the eio queue. An I/O is added to this queue
6743 6742 * when it has failed and failfast is enabled or the vdisk has multiple
6744 6743 * servers. It will then be handled by the eio thread (vdc_eio_thread).
6745 6744 * The eio queue is ordered starting with the most recent I/O added.
6746 6745 */
6747 6746 static vdc_io_t *
6748 6747 vdc_eio_queue(vdc_t *vdc, int index)
6749 6748 {
6750 6749 vdc_io_t *vio;
6751 6750
6752 6751 ASSERT(MUTEX_HELD(&vdc->lock));
6753 6752
6754 6753 vio = kmem_alloc(sizeof (vdc_io_t), KM_SLEEP);
6755 6754 vio->vio_next = vdc->eio_queue;
6756 6755 vio->vio_index = index;
6757 6756 vio->vio_qtime = ddi_get_lbolt();
6758 6757
6759 6758 vdc->eio_queue = vio;
6760 6759
6761 6760 /* notify the eio thread that a new I/O is queued */
6762 6761 cv_signal(&vdc->eio_cv);
6763 6762
6764 6763 return (vio);
6765 6764 }
6766 6765
6767 6766 /*
6768 6767 * Remove I/Os added before the indicated deadline from the eio queue. A
6769 6768 * deadline of 0 means that all I/Os have to be unqueued. The complete_io
6770 6769 * boolean specifies if unqueued I/Os should be marked as completed or not.
6771 6770 */
6772 6771 static void
6773 6772 vdc_eio_unqueue(vdc_t *vdc, clock_t deadline, boolean_t complete_io)
6774 6773 {
6775 6774 struct buf *buf;
6776 6775 vdc_io_t *vio, *vio_tmp;
6777 6776 int index, op;
6778 6777
6779 6778 ASSERT(MUTEX_HELD(&vdc->lock));
6780 6779
6781 6780 vio_tmp = NULL;
6782 6781 vio = vdc->eio_queue;
6783 6782
6784 6783 if (deadline != 0) {
6785 6784 /*
6786 6785 * Skip any io queued after the deadline. The eio queue is
6787 6786 * ordered starting with the last I/O added to the queue.
6788 6787 */
6789 6788 while (vio != NULL && vio->vio_qtime > deadline) {
6790 6789 vio_tmp = vio;
6791 6790 vio = vio->vio_next;
6792 6791 }
6793 6792 }
6794 6793
6795 6794 if (vio == NULL)
6796 6795 /* nothing to unqueue */
6797 6796 return;
6798 6797
6799 6798 /* update the queue */
6800 6799 if (vio_tmp == NULL)
6801 6800 vdc->eio_queue = NULL;
6802 6801 else
6803 6802 vio_tmp->vio_next = NULL;
6804 6803
6805 6804 /*
6806 6805 * Free and complete unqueued I/Os if this was requested. All I/Os
6807 6806 * have a block I/O data transfer structure (buf) and they are
6808 6807 * completed by calling biodone().
6809 6808 */
6810 6809 while (vio != NULL) {
6811 6810 vio_tmp = vio->vio_next;
6812 6811
6813 6812 if (complete_io) {
6814 6813 index = vio->vio_index;
6815 6814 op = vdc->local_dring[index].operation;
6816 6815 buf = vdc->local_dring[index].buf;
6817 6816 (void) vdc_depopulate_descriptor(vdc, index);
6818 6817 ASSERT(buf->b_flags & B_ERROR);
6819 6818 if (op == VD_OP_BREAD || op == VD_OP_BWRITE) {
6820 6819 VD_UPDATE_ERR_STATS(vdc, vd_softerrs);
6821 6820 VD_KSTAT_RUNQ_EXIT(vdc);
6822 6821 DTRACE_IO1(done, buf_t *, buf);
6823 6822 }
6824 6823 biodone(buf);
6825 6824 }
6826 6825
6827 6826 kmem_free(vio, sizeof (vdc_io_t));
6828 6827 vio = vio_tmp;
6829 6828 }
6830 6829 }
6831 6830
6832 6831 /*
6833 6832 * Error I/O Thread. There is one eio thread for each virtual disk that
6834 6833 * has multiple servers or for which failfast is enabled. Failfast can only
6835 6834 * be enabled for vdisk supporting SCSI commands.
6836 6835 *
6837 6836 * While failfast is enabled, the eio thread sends a TEST UNIT READY
6838 6837 * and a zero size WRITE(10) SCSI commands on a regular basis to check that
6839 6838 * we still have access to the disk. If a command fails with a RESERVATION
6840 6839 * CONFLICT error then the system will immediatly panic.
6841 6840 *
6842 6841 * The eio thread is also woken up when an I/O has failed. It then checks
6843 6842 * the access to the disk to ensure that the I/O failure was not due to a
6844 6843 * reservation conflict or to the backend been inaccessible.
6845 6844 *
6846 6845 */
6847 6846 static void
6848 6847 vdc_eio_thread(void *arg)
6849 6848 {
6850 6849 int status;
6851 6850 vdc_t *vdc = (vdc_t *)arg;
6852 6851 clock_t starttime, timeout = drv_usectohz(vdc->failfast_interval);
6853 6852
6854 6853 mutex_enter(&vdc->lock);
6855 6854
6856 6855 while (vdc->failfast_interval != 0 || vdc->num_servers > 1) {
6857 6856 /*
6858 6857 * Wait if there is nothing in the eio queue or if the state
6859 6858 * is not VDC_STATE_RUNNING.
6860 6859 */
6861 6860 if (vdc->eio_queue == NULL || vdc->state != VDC_STATE_RUNNING) {
6862 6861 if (vdc->failfast_interval != 0) {
6863 6862 timeout = ddi_get_lbolt() +
6864 6863 drv_usectohz(vdc->failfast_interval);
6865 6864 (void) cv_timedwait(&vdc->eio_cv, &vdc->lock,
6866 6865 timeout);
6867 6866 } else {
6868 6867 ASSERT(vdc->num_servers > 1);
6869 6868 (void) cv_wait(&vdc->eio_cv, &vdc->lock);
6870 6869 }
6871 6870
6872 6871 if (vdc->state != VDC_STATE_RUNNING)
6873 6872 continue;
6874 6873 }
6875 6874
6876 6875 mutex_exit(&vdc->lock);
6877 6876
6878 6877 starttime = ddi_get_lbolt();
6879 6878
6880 6879 /* check error */
6881 6880 status = vdc_eio_check(vdc, VDC_OP_STATE_RUNNING);
6882 6881
6883 6882 mutex_enter(&vdc->lock);
6884 6883 /*
6885 6884 * We have dropped the lock to check the backend so we have
6886 6885 * to check that the eio thread is still enabled.
6887 6886 */
6888 6887 if (vdc->failfast_interval == 0 && vdc->num_servers <= 1)
6889 6888 break;
6890 6889
6891 6890 /*
6892 6891 * If the eio queue is empty or we are not in running state
6893 6892 * anymore then there is nothing to do.
6894 6893 */
6895 6894 if (vdc->state != VDC_STATE_RUNNING || vdc->eio_queue == NULL)
6896 6895 continue;
6897 6896
6898 6897 if (status == 0) {
6899 6898 /*
6900 6899 * The backend access has been successfully checked,
6901 6900 * we can complete any I/O queued before the last check.
6902 6901 */
6903 6902 vdc_eio_unqueue(vdc, starttime, B_TRUE);
6904 6903
6905 6904 } else if (vdc->num_servers > 1) {
6906 6905 /*
6907 6906 * The backend is inaccessible for a disk with multiple
6908 6907 * servers. So we force a reset to switch to another
6909 6908 * server. The reset will also clear the eio queue and
6910 6909 * resubmit all pending I/Os.
6911 6910 */
6912 6911 mutex_enter(&vdc->read_lock);
6913 6912 vdc->read_state = VDC_READ_RESET;
6914 6913 cv_signal(&vdc->read_cv);
6915 6914 mutex_exit(&vdc->read_lock);
6916 6915 } else {
6917 6916 /*
6918 6917 * There is only one path and the backend is not
6919 6918 * accessible, so I/Os are actually failing because
6920 6919 * of that. So we can complete I/O queued before the
6921 6920 * last check.
6922 6921 */
6923 6922 vdc_eio_unqueue(vdc, starttime, B_TRUE);
6924 6923 }
6925 6924 }
6926 6925
6927 6926 /*
6928 6927 * The thread is being stopped so we can complete any queued I/O.
6929 6928 */
6930 6929 vdc_eio_unqueue(vdc, 0, B_TRUE);
6931 6930 vdc->eio_thread = NULL;
6932 6931 mutex_exit(&vdc->lock);
6933 6932 thread_exit();
6934 6933 }
6935 6934
6936 6935 /*
6937 6936 * Implement the MHIOCENFAILFAST mhd(7i) ioctl.
6938 6937 */
6939 6938 static int
6940 6939 vdc_failfast(vdc_t *vdc, caddr_t arg, int mode)
6941 6940 {
6942 6941 unsigned int mh_time;
6943 6942
6944 6943 if (ddi_copyin((void *)arg, &mh_time, sizeof (int), mode))
6945 6944 return (EFAULT);
6946 6945
6947 6946 mutex_enter(&vdc->lock);
6948 6947 if (mh_time != 0 && vdc->eio_thread == NULL) {
6949 6948 vdc->eio_thread = thread_create(NULL, 0,
6950 6949 vdc_eio_thread, vdc, 0, &p0, TS_RUN,
6951 6950 v.v_maxsyspri - 2);
6952 6951 }
6953 6952
6954 6953 vdc->failfast_interval = ((long)mh_time) * MILLISEC;
6955 6954 cv_signal(&vdc->eio_cv);
6956 6955 mutex_exit(&vdc->lock);
6957 6956
6958 6957 return (0);
6959 6958 }
6960 6959
6961 6960 /*
6962 6961 * Implement the MHIOCTKOWN and MHIOCRELEASE mhd(7i) ioctls. These ioctls are
6963 6962 * converted to VD_OP_SET_ACCESS operations.
6964 6963 */
6965 6964 static int
6966 6965 vdc_access_set(vdc_t *vdc, uint64_t flags)
6967 6966 {
6968 6967 int rv;
6969 6968
6970 6969 /* submit owership command request */
6971 6970 rv = vdc_do_sync_op(vdc, VD_OP_SET_ACCESS, (caddr_t)&flags,
6972 6971 sizeof (uint64_t), 0, 0, VIO_both_dir, B_TRUE);
6973 6972
6974 6973 return (rv);
6975 6974 }
6976 6975
6977 6976 /*
6978 6977 * Implement the MHIOCSTATUS mhd(7i) ioctl. This ioctl is converted to a
6979 6978 * VD_OP_GET_ACCESS operation.
6980 6979 */
6981 6980 static int
6982 6981 vdc_access_get(vdc_t *vdc, uint64_t *status)
6983 6982 {
6984 6983 int rv;
6985 6984
6986 6985 /* submit owership command request */
6987 6986 rv = vdc_do_sync_op(vdc, VD_OP_GET_ACCESS, (caddr_t)status,
6988 6987 sizeof (uint64_t), 0, 0, VIO_both_dir, B_TRUE);
6989 6988
6990 6989 return (rv);
6991 6990 }
6992 6991
6993 6992 /*
6994 6993 * Disk Ownership Thread.
6995 6994 *
6996 6995 * When we have taken the ownership of a disk, this thread waits to be
6997 6996 * notified when the LDC channel is reset so that it can recover the
6998 6997 * ownership.
6999 6998 *
7000 6999 * Note that the thread handling the LDC reset (vdc_process_msg_thread())
7001 7000 * can not be used to do the ownership recovery because it has to be
7002 7001 * running to handle the reply message to the ownership operation.
7003 7002 */
7004 7003 static void
7005 7004 vdc_ownership_thread(void *arg)
7006 7005 {
7007 7006 vdc_t *vdc = (vdc_t *)arg;
7008 7007 clock_t timeout;
7009 7008 uint64_t status;
7010 7009
7011 7010 mutex_enter(&vdc->ownership_lock);
7012 7011 mutex_enter(&vdc->lock);
7013 7012
7014 7013 while (vdc->ownership & VDC_OWNERSHIP_WANTED) {
7015 7014
7016 7015 if ((vdc->ownership & VDC_OWNERSHIP_RESET) ||
7017 7016 !(vdc->ownership & VDC_OWNERSHIP_GRANTED)) {
7018 7017 /*
7019 7018 * There was a reset so the ownership has been lost,
7020 7019 * try to recover. We do this without using the preempt
7021 7020 * option so that we don't steal the ownership from
7022 7021 * someone who has preempted us.
7023 7022 */
7024 7023 DMSG(vdc, 0, "[%d] Ownership lost, recovering",
7025 7024 vdc->instance);
7026 7025
7027 7026 vdc->ownership &= ~(VDC_OWNERSHIP_RESET |
7028 7027 VDC_OWNERSHIP_GRANTED);
7029 7028
7030 7029 mutex_exit(&vdc->lock);
7031 7030
7032 7031 status = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE |
7033 7032 VD_ACCESS_SET_PRESERVE);
7034 7033
7035 7034 mutex_enter(&vdc->lock);
7036 7035
7037 7036 if (status == 0) {
7038 7037 DMSG(vdc, 0, "[%d] Ownership recovered",
7039 7038 vdc->instance);
7040 7039 vdc->ownership |= VDC_OWNERSHIP_GRANTED;
7041 7040 } else {
7042 7041 DMSG(vdc, 0, "[%d] Fail to recover ownership",
7043 7042 vdc->instance);
7044 7043 }
7045 7044
7046 7045 }
7047 7046
7048 7047 /*
7049 7048 * If we have the ownership then we just wait for an event
7050 7049 * to happen (LDC reset), otherwise we will retry to recover
7051 7050 * after a delay.
7052 7051 */
7053 7052 if (vdc->ownership & VDC_OWNERSHIP_GRANTED)
7054 7053 timeout = 0;
7055 7054 else
7056 7055 timeout = drv_usectohz(vdc_ownership_delay);
7057 7056
7058 7057 /* Release the ownership_lock and wait on the vdc lock */
7059 7058 mutex_exit(&vdc->ownership_lock);
7060 7059
7061 7060 if (timeout == 0)
7062 7061 (void) cv_wait(&vdc->ownership_cv, &vdc->lock);
7063 7062 else
7064 7063 (void) cv_reltimedwait(&vdc->ownership_cv, &vdc->lock,
7065 7064 timeout, TR_CLOCK_TICK);
7066 7065
7067 7066 mutex_exit(&vdc->lock);
7068 7067
7069 7068 mutex_enter(&vdc->ownership_lock);
7070 7069 mutex_enter(&vdc->lock);
7071 7070 }
7072 7071
7073 7072 vdc->ownership_thread = NULL;
7074 7073 mutex_exit(&vdc->lock);
7075 7074 mutex_exit(&vdc->ownership_lock);
7076 7075
7077 7076 thread_exit();
7078 7077 }
7079 7078
7080 7079 static void
7081 7080 vdc_ownership_update(vdc_t *vdc, int ownership_flags)
7082 7081 {
7083 7082 ASSERT(MUTEX_HELD(&vdc->ownership_lock));
7084 7083
7085 7084 mutex_enter(&vdc->lock);
7086 7085 vdc->ownership = ownership_flags;
7087 7086 if ((vdc->ownership & VDC_OWNERSHIP_WANTED) &&
7088 7087 vdc->ownership_thread == NULL) {
7089 7088 /* start ownership thread */
7090 7089 vdc->ownership_thread = thread_create(NULL, 0,
7091 7090 vdc_ownership_thread, vdc, 0, &p0, TS_RUN,
7092 7091 v.v_maxsyspri - 2);
7093 7092 } else {
7094 7093 /* notify the ownership thread */
7095 7094 cv_signal(&vdc->ownership_cv);
7096 7095 }
7097 7096 mutex_exit(&vdc->lock);
7098 7097 }
7099 7098
7100 7099 /*
7101 7100 * Get the size and the block size of a virtual disk from the vdisk server.
7102 7101 */
7103 7102 static int
7104 7103 vdc_get_capacity(vdc_t *vdc, size_t *dsk_size, size_t *blk_size)
7105 7104 {
7106 7105 int rv = 0;
7107 7106 size_t alloc_len;
7108 7107 vd_capacity_t *vd_cap;
7109 7108
7110 7109 ASSERT(MUTEX_NOT_HELD(&vdc->lock));
7111 7110
7112 7111 alloc_len = P2ROUNDUP(sizeof (vd_capacity_t), sizeof (uint64_t));
7113 7112
7114 7113 vd_cap = kmem_zalloc(alloc_len, KM_SLEEP);
7115 7114
7116 7115 rv = vdc_do_sync_op(vdc, VD_OP_GET_CAPACITY, (caddr_t)vd_cap, alloc_len,
7117 7116 0, 0, VIO_both_dir, B_TRUE);
7118 7117
7119 7118 *dsk_size = vd_cap->vdisk_size;
7120 7119 *blk_size = vd_cap->vdisk_block_size;
7121 7120
7122 7121 kmem_free(vd_cap, alloc_len);
7123 7122 return (rv);
7124 7123 }
7125 7124
7126 7125 /*
7127 7126 * Check the disk capacity. Disk size information is updated if size has
7128 7127 * changed.
7129 7128 *
7130 7129 * Return 0 if the disk capacity is available, or non-zero if it is not.
7131 7130 */
7132 7131 static int
7133 7132 vdc_check_capacity(vdc_t *vdc)
7134 7133 {
7135 7134 size_t dsk_size, blk_size;
7136 7135 int rv;
7137 7136
7138 7137 /*
7139 7138 * If the vdisk does not support the VD_OP_GET_CAPACITY operation
7140 7139 * then the disk capacity has been retrieved during the handshake
7141 7140 * and there's nothing more to do here.
7142 7141 */
7143 7142 if (!VD_OP_SUPPORTED(vdc->operations, VD_OP_GET_CAPACITY))
7144 7143 return (0);
7145 7144
7146 7145 if ((rv = vdc_get_capacity(vdc, &dsk_size, &blk_size)) != 0)
7147 7146 return (rv);
7148 7147
7149 7148 if (dsk_size == VD_SIZE_UNKNOWN || dsk_size == 0 || blk_size == 0)
7150 7149 return (EINVAL);
7151 7150
7152 7151 mutex_enter(&vdc->lock);
7153 7152 /*
7154 7153 * First try to update the VIO block size (which is the same as the
7155 7154 * vdisk block size). If this returns an error then that means that
7156 7155 * we can not use that block size so basically the vdisk is unusable
7157 7156 * and we return an error.
7158 7157 */
7159 7158 rv = vdc_update_vio_bsize(vdc, blk_size);
7160 7159 if (rv == 0)
7161 7160 vdc_update_size(vdc, dsk_size, blk_size, vdc->max_xfer_sz);
7162 7161
7163 7162 mutex_exit(&vdc->lock);
7164 7163
7165 7164 return (rv);
7166 7165 }
7167 7166
7168 7167 /*
7169 7168 * This structure is used in the DKIO(7I) array below.
7170 7169 */
7171 7170 typedef struct vdc_dk_ioctl {
7172 7171 uint8_t op; /* VD_OP_XXX value */
7173 7172 int cmd; /* Solaris ioctl operation number */
7174 7173 size_t nbytes; /* size of structure to be copied */
7175 7174
7176 7175 /* function to convert between vDisk and Solaris structure formats */
7177 7176 int (*convert)(vdc_t *vdc, void *vd_buf, void *ioctl_arg,
7178 7177 int mode, int dir);
7179 7178 } vdc_dk_ioctl_t;
7180 7179
7181 7180 /*
7182 7181 * Subset of DKIO(7I) operations currently supported
7183 7182 */
7184 7183 static vdc_dk_ioctl_t dk_ioctl[] = {
7185 7184 {VD_OP_FLUSH, DKIOCFLUSHWRITECACHE, 0,
7186 7185 vdc_null_copy_func},
7187 7186 {VD_OP_GET_WCE, DKIOCGETWCE, sizeof (int),
7188 7187 vdc_get_wce_convert},
7189 7188 {VD_OP_SET_WCE, DKIOCSETWCE, sizeof (int),
7190 7189 vdc_set_wce_convert},
7191 7190 {VD_OP_GET_VTOC, DKIOCGVTOC, sizeof (vd_vtoc_t),
7192 7191 vdc_get_vtoc_convert},
7193 7192 {VD_OP_SET_VTOC, DKIOCSVTOC, sizeof (vd_vtoc_t),
7194 7193 vdc_set_vtoc_convert},
7195 7194 {VD_OP_GET_VTOC, DKIOCGEXTVTOC, sizeof (vd_vtoc_t),
7196 7195 vdc_get_extvtoc_convert},
7197 7196 {VD_OP_SET_VTOC, DKIOCSEXTVTOC, sizeof (vd_vtoc_t),
7198 7197 vdc_set_extvtoc_convert},
7199 7198 {VD_OP_GET_DISKGEOM, DKIOCGGEOM, sizeof (vd_geom_t),
7200 7199 vdc_get_geom_convert},
7201 7200 {VD_OP_GET_DISKGEOM, DKIOCG_PHYGEOM, sizeof (vd_geom_t),
7202 7201 vdc_get_geom_convert},
7203 7202 {VD_OP_GET_DISKGEOM, DKIOCG_VIRTGEOM, sizeof (vd_geom_t),
7204 7203 vdc_get_geom_convert},
7205 7204 {VD_OP_SET_DISKGEOM, DKIOCSGEOM, sizeof (vd_geom_t),
7206 7205 vdc_set_geom_convert},
7207 7206 {VD_OP_GET_EFI, DKIOCGETEFI, 0,
7208 7207 vdc_get_efi_convert},
7209 7208 {VD_OP_SET_EFI, DKIOCSETEFI, 0,
7210 7209 vdc_set_efi_convert},
7211 7210
7212 7211 /* DIOCTL_RWCMD is converted to a read or a write */
7213 7212 {0, DIOCTL_RWCMD, sizeof (struct dadkio_rwcmd), NULL},
7214 7213
7215 7214 /* mhd(7I) non-shared multihost disks ioctls */
7216 7215 {0, MHIOCTKOWN, 0, vdc_null_copy_func},
7217 7216 {0, MHIOCRELEASE, 0, vdc_null_copy_func},
7218 7217 {0, MHIOCSTATUS, 0, vdc_null_copy_func},
7219 7218 {0, MHIOCQRESERVE, 0, vdc_null_copy_func},
7220 7219
7221 7220 /* mhd(7I) shared multihost disks ioctls */
7222 7221 {0, MHIOCGRP_INKEYS, 0, vdc_null_copy_func},
7223 7222 {0, MHIOCGRP_INRESV, 0, vdc_null_copy_func},
7224 7223 {0, MHIOCGRP_REGISTER, 0, vdc_null_copy_func},
7225 7224 {0, MHIOCGRP_RESERVE, 0, vdc_null_copy_func},
7226 7225 {0, MHIOCGRP_PREEMPTANDABORT, 0, vdc_null_copy_func},
7227 7226 {0, MHIOCGRP_REGISTERANDIGNOREKEY, 0, vdc_null_copy_func},
7228 7227
7229 7228 /* mhd(7I) failfast ioctl */
7230 7229 {0, MHIOCENFAILFAST, 0, vdc_null_copy_func},
7231 7230
7232 7231 /*
7233 7232 * These particular ioctls are not sent to the server - vdc fakes up
7234 7233 * the necessary info.
7235 7234 */
7236 7235 {0, DKIOCINFO, sizeof (struct dk_cinfo), vdc_null_copy_func},
7237 7236 {0, DKIOCGMEDIAINFO, sizeof (struct dk_minfo), vdc_null_copy_func},
7238 7237 {0, USCSICMD, sizeof (struct uscsi_cmd), vdc_null_copy_func},
7239 7238 {0, DKIOCPARTITION, 0, vdc_null_copy_func },
7240 7239 {0, DKIOCGAPART, 0, vdc_null_copy_func },
7241 7240 {0, DKIOCREMOVABLE, 0, vdc_null_copy_func},
7242 7241 {0, CDROMREADOFFSET, 0, vdc_null_copy_func}
7243 7242 };
7244 7243
7245 7244 /*
7246 7245 * This function handles ioctl requests from the vd_efi_alloc_and_read()
7247 7246 * function and forward them to the vdisk.
7248 7247 */
7249 7248 static int
7250 7249 vd_process_efi_ioctl(void *vdisk, int cmd, uintptr_t arg)
7251 7250 {
7252 7251 vdc_t *vdc = (vdc_t *)vdisk;
7253 7252 dev_t dev;
7254 7253 int rval;
7255 7254
7256 7255 dev = makedevice(ddi_driver_major(vdc->dip),
7257 7256 VD_MAKE_DEV(vdc->instance, 0));
7258 7257
7259 7258 return (vd_process_ioctl(dev, cmd, (caddr_t)arg, FKIOCTL, &rval));
7260 7259 }
7261 7260
7262 7261 /*
7263 7262 * Function:
7264 7263 * vd_process_ioctl()
7265 7264 *
7266 7265 * Description:
7267 7266 * This routine processes disk specific ioctl calls
7268 7267 *
7269 7268 * Arguments:
7270 7269 * dev - the device number
7271 7270 * cmd - the operation [dkio(7I)] to be processed
7272 7271 * arg - pointer to user provided structure
7273 7272 * (contains data to be set or reference parameter for get)
7274 7273 * mode - bit flag, indicating open settings, 32/64 bit type, etc
7275 7274 * rvalp - pointer to return value for calling process.
7276 7275 *
7277 7276 * Return Code:
7278 7277 * 0
7279 7278 * EFAULT
7280 7279 * ENXIO
7281 7280 * EIO
7282 7281 * ENOTSUP
7283 7282 */
7284 7283 static int
7285 7284 vd_process_ioctl(dev_t dev, int cmd, caddr_t arg, int mode, int *rvalp)
7286 7285 {
7287 7286 int instance = VDCUNIT(dev);
7288 7287 vdc_t *vdc = NULL;
7289 7288 int rv = -1;
7290 7289 int idx = 0; /* index into dk_ioctl[] */
7291 7290 size_t len = 0; /* #bytes to send to vds */
7292 7291 size_t alloc_len = 0; /* #bytes to allocate mem for */
7293 7292 caddr_t mem_p = NULL;
7294 7293 size_t nioctls = (sizeof (dk_ioctl)) / (sizeof (dk_ioctl[0]));
7295 7294 vdc_dk_ioctl_t *iop;
7296 7295
7297 7296 vdc = ddi_get_soft_state(vdc_state, instance);
7298 7297 if (vdc == NULL) {
7299 7298 cmn_err(CE_NOTE, "![%d] Could not get soft state structure",
7300 7299 instance);
7301 7300 return (ENXIO);
7302 7301 }
7303 7302
7304 7303 DMSG(vdc, 0, "[%d] Processing ioctl(%x) for dev %lx : model %x\n",
7305 7304 instance, cmd, dev, ddi_model_convert_from(mode & FMODELS));
7306 7305
7307 7306 if (rvalp != NULL) {
7308 7307 /* the return value of the ioctl is 0 by default */
7309 7308 *rvalp = 0;
7310 7309 }
7311 7310
7312 7311 /*
7313 7312 * Validate the ioctl operation to be performed.
7314 7313 *
7315 7314 * If we have looped through the array without finding a match then we
7316 7315 * don't support this ioctl.
7317 7316 */
7318 7317 for (idx = 0; idx < nioctls; idx++) {
7319 7318 if (cmd == dk_ioctl[idx].cmd)
7320 7319 break;
7321 7320 }
7322 7321
7323 7322 if (idx >= nioctls) {
7324 7323 DMSG(vdc, 0, "[%d] Unsupported ioctl (0x%x)\n",
7325 7324 vdc->instance, cmd);
7326 7325 return (ENOTSUP);
7327 7326 }
7328 7327
7329 7328 iop = &(dk_ioctl[idx]);
7330 7329
7331 7330 if (cmd == DKIOCGETEFI || cmd == DKIOCSETEFI) {
7332 7331 /* size is not fixed for EFI ioctls, it depends on ioctl arg */
7333 7332 dk_efi_t dk_efi;
7334 7333
7335 7334 rv = ddi_copyin(arg, &dk_efi, sizeof (dk_efi_t), mode);
7336 7335 if (rv != 0)
7337 7336 return (EFAULT);
7338 7337
7339 7338 len = sizeof (vd_efi_t) - 1 + dk_efi.dki_length;
7340 7339 } else {
7341 7340 len = iop->nbytes;
7342 7341 }
7343 7342
7344 7343 /* check if the ioctl is applicable */
7345 7344 switch (cmd) {
7346 7345 case CDROMREADOFFSET:
7347 7346 case DKIOCREMOVABLE:
7348 7347 return (ENOTTY);
7349 7348
7350 7349 case USCSICMD:
7351 7350 case MHIOCTKOWN:
7352 7351 case MHIOCSTATUS:
7353 7352 case MHIOCQRESERVE:
7354 7353 case MHIOCRELEASE:
7355 7354 case MHIOCGRP_INKEYS:
7356 7355 case MHIOCGRP_INRESV:
7357 7356 case MHIOCGRP_REGISTER:
7358 7357 case MHIOCGRP_RESERVE:
7359 7358 case MHIOCGRP_PREEMPTANDABORT:
7360 7359 case MHIOCGRP_REGISTERANDIGNOREKEY:
7361 7360 case MHIOCENFAILFAST:
7362 7361 if (vdc->cinfo == NULL)
7363 7362 return (ENXIO);
7364 7363 if (vdc->cinfo->dki_ctype != DKC_SCSI_CCS)
7365 7364 return (ENOTTY);
7366 7365 break;
7367 7366
7368 7367 case DIOCTL_RWCMD:
7369 7368 if (vdc->cinfo == NULL)
7370 7369 return (ENXIO);
7371 7370 if (vdc->cinfo->dki_ctype != DKC_DIRECT)
7372 7371 return (ENOTTY);
7373 7372 break;
7374 7373
7375 7374 case DKIOCINFO:
7376 7375 if (vdc->cinfo == NULL)
7377 7376 return (ENXIO);
7378 7377 break;
7379 7378
7380 7379 case DKIOCGMEDIAINFO:
7381 7380 if (vdc->minfo == NULL)
7382 7381 return (ENXIO);
7383 7382 if (vdc_check_capacity(vdc) != 0)
7384 7383 /* disk capacity is not available */
7385 7384 return (EIO);
7386 7385 break;
7387 7386 }
7388 7387
7389 7388 /*
7390 7389 * Deal with ioctls which require a processing different than
7391 7390 * converting ioctl arguments and sending a corresponding
7392 7391 * VD operation.
7393 7392 */
7394 7393 switch (cmd) {
7395 7394
7396 7395 case USCSICMD:
7397 7396 {
7398 7397 return (vdc_uscsi_cmd(vdc, arg, mode));
7399 7398 }
7400 7399
7401 7400 case MHIOCTKOWN:
7402 7401 {
7403 7402 mutex_enter(&vdc->ownership_lock);
7404 7403 /*
7405 7404 * We have to set VDC_OWNERSHIP_WANTED now so that the ownership
7406 7405 * can be flagged with VDC_OWNERSHIP_RESET if the LDC is reset
7407 7406 * while we are processing the ioctl.
7408 7407 */
7409 7408 vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED);
7410 7409
7411 7410 rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE |
7412 7411 VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE);
7413 7412 if (rv == 0) {
7414 7413 vdc_ownership_update(vdc, VDC_OWNERSHIP_WANTED |
7415 7414 VDC_OWNERSHIP_GRANTED);
7416 7415 } else {
7417 7416 vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
7418 7417 }
7419 7418 mutex_exit(&vdc->ownership_lock);
7420 7419 return (rv);
7421 7420 }
7422 7421
7423 7422 case MHIOCRELEASE:
7424 7423 {
7425 7424 mutex_enter(&vdc->ownership_lock);
7426 7425 rv = vdc_access_set(vdc, VD_ACCESS_SET_CLEAR);
7427 7426 if (rv == 0) {
7428 7427 vdc_ownership_update(vdc, VDC_OWNERSHIP_NONE);
7429 7428 }
7430 7429 mutex_exit(&vdc->ownership_lock);
7431 7430 return (rv);
7432 7431 }
7433 7432
7434 7433 case MHIOCSTATUS:
7435 7434 {
7436 7435 uint64_t status;
7437 7436
7438 7437 rv = vdc_access_get(vdc, &status);
7439 7438 if (rv == 0 && rvalp != NULL)
7440 7439 *rvalp = (status & VD_ACCESS_ALLOWED)? 0 : 1;
7441 7440 return (rv);
7442 7441 }
7443 7442
7444 7443 case MHIOCQRESERVE:
7445 7444 {
7446 7445 rv = vdc_access_set(vdc, VD_ACCESS_SET_EXCLUSIVE);
7447 7446 return (rv);
7448 7447 }
7449 7448
7450 7449 case MHIOCGRP_INKEYS:
7451 7450 {
7452 7451 return (vdc_mhd_inkeys(vdc, arg, mode));
7453 7452 }
7454 7453
7455 7454 case MHIOCGRP_INRESV:
7456 7455 {
7457 7456 return (vdc_mhd_inresv(vdc, arg, mode));
7458 7457 }
7459 7458
7460 7459 case MHIOCGRP_REGISTER:
7461 7460 {
7462 7461 return (vdc_mhd_register(vdc, arg, mode));
7463 7462 }
7464 7463
7465 7464 case MHIOCGRP_RESERVE:
7466 7465 {
7467 7466 return (vdc_mhd_reserve(vdc, arg, mode));
7468 7467 }
7469 7468
7470 7469 case MHIOCGRP_PREEMPTANDABORT:
7471 7470 {
7472 7471 return (vdc_mhd_preemptabort(vdc, arg, mode));
7473 7472 }
7474 7473
7475 7474 case MHIOCGRP_REGISTERANDIGNOREKEY:
7476 7475 {
7477 7476 return (vdc_mhd_registerignore(vdc, arg, mode));
7478 7477 }
7479 7478
7480 7479 case MHIOCENFAILFAST:
7481 7480 {
7482 7481 rv = vdc_failfast(vdc, arg, mode);
7483 7482 return (rv);
7484 7483 }
7485 7484
7486 7485 case DIOCTL_RWCMD:
7487 7486 {
7488 7487 return (vdc_dioctl_rwcmd(vdc, arg, mode));
7489 7488 }
7490 7489
7491 7490 case DKIOCGAPART:
7492 7491 {
7493 7492 return (vdc_dkio_gapart(vdc, arg, mode));
7494 7493 }
7495 7494
7496 7495 case DKIOCPARTITION:
7497 7496 {
7498 7497 return (vdc_dkio_partition(vdc, arg, mode));
7499 7498 }
7500 7499
7501 7500 case DKIOCINFO:
7502 7501 {
7503 7502 struct dk_cinfo cinfo;
7504 7503
7505 7504 bcopy(vdc->cinfo, &cinfo, sizeof (struct dk_cinfo));
7506 7505 cinfo.dki_partition = VDCPART(dev);
7507 7506
7508 7507 rv = ddi_copyout(&cinfo, (void *)arg,
7509 7508 sizeof (struct dk_cinfo), mode);
7510 7509 if (rv != 0)
7511 7510 return (EFAULT);
7512 7511
7513 7512 return (0);
7514 7513 }
7515 7514
7516 7515 case DKIOCGMEDIAINFO:
7517 7516 {
7518 7517 ASSERT(vdc->vdisk_size != 0);
7519 7518 ASSERT(vdc->minfo->dki_capacity != 0);
7520 7519 rv = ddi_copyout(vdc->minfo, (void *)arg,
7521 7520 sizeof (struct dk_minfo), mode);
7522 7521 if (rv != 0)
7523 7522 return (EFAULT);
7524 7523
7525 7524 return (0);
7526 7525 }
7527 7526
7528 7527 case DKIOCFLUSHWRITECACHE:
7529 7528 {
7530 7529 struct dk_callback *dkc =
7531 7530 (struct dk_callback *)(uintptr_t)arg;
7532 7531 vdc_dk_arg_t *dkarg = NULL;
7533 7532
7534 7533 DMSG(vdc, 1, "[%d] Flush W$: mode %x\n",
7535 7534 instance, mode);
7536 7535
7537 7536 /*
7538 7537 * If arg is NULL, then there is no callback function
7539 7538 * registered and the call operates synchronously; we
7540 7539 * break and continue with the rest of the function and
7541 7540 * wait for vds to return (i.e. after the request to
7542 7541 * vds returns successfully, all writes completed prior
7543 7542 * to the ioctl will have been flushed from the disk
7544 7543 * write cache to persistent media.
7545 7544 *
7546 7545 * If a callback function is registered, we dispatch
7547 7546 * the request on a task queue and return immediately.
7548 7547 * The callback will deal with informing the calling
7549 7548 * thread that the flush request is completed.
7550 7549 */
7551 7550 if (dkc == NULL)
7552 7551 break;
7553 7552
7554 7553 /*
7555 7554 * the asynchronous callback is only supported if
7556 7555 * invoked from within the kernel
7557 7556 */
7558 7557 if ((mode & FKIOCTL) == 0)
7559 7558 return (ENOTSUP);
7560 7559
7561 7560 dkarg = kmem_zalloc(sizeof (vdc_dk_arg_t), KM_SLEEP);
7562 7561
7563 7562 dkarg->mode = mode;
7564 7563 dkarg->dev = dev;
7565 7564 bcopy(dkc, &dkarg->dkc, sizeof (*dkc));
7566 7565
7567 7566 mutex_enter(&vdc->lock);
7568 7567 vdc->dkio_flush_pending++;
7569 7568 dkarg->vdc = vdc;
7570 7569 mutex_exit(&vdc->lock);
7571 7570
7572 7571 /* put the request on a task queue */
7573 7572 rv = taskq_dispatch(system_taskq, vdc_dkio_flush_cb,
7574 7573 (void *)dkarg, DDI_SLEEP);
7575 7574 if (rv == NULL) {
7576 7575 /* clean up if dispatch fails */
7577 7576 mutex_enter(&vdc->lock);
7578 7577 vdc->dkio_flush_pending--;
7579 7578 mutex_exit(&vdc->lock);
7580 7579 kmem_free(dkarg, sizeof (vdc_dk_arg_t));
7581 7580 }
7582 7581
7583 7582 return (rv == NULL ? ENOMEM : 0);
7584 7583 }
7585 7584 }
7586 7585
7587 7586 /* catch programming error in vdc - should be a VD_OP_XXX ioctl */
7588 7587 ASSERT(iop->op != 0);
7589 7588
7590 7589 /* check if the vDisk server handles the operation for this vDisk */
7591 7590 if (VD_OP_SUPPORTED(vdc->operations, iop->op) == B_FALSE) {
7592 7591 DMSG(vdc, 0, "[%d] Unsupported VD_OP operation (0x%x)\n",
7593 7592 vdc->instance, iop->op);
7594 7593 return (ENOTSUP);
7595 7594 }
7596 7595
7597 7596 /* LDC requires that the memory being mapped is 8-byte aligned */
7598 7597 alloc_len = P2ROUNDUP(len, sizeof (uint64_t));
7599 7598 DMSG(vdc, 1, "[%d] struct size %ld alloc %ld\n",
7600 7599 instance, len, alloc_len);
7601 7600
7602 7601 if (alloc_len > 0)
7603 7602 mem_p = kmem_zalloc(alloc_len, KM_SLEEP);
7604 7603
7605 7604 /*
7606 7605 * Call the conversion function for this ioctl which, if necessary,
7607 7606 * converts from the Solaris format to the format ARC'ed
7608 7607 * as part of the vDisk protocol (FWARC 2006/195)
7609 7608 */
7610 7609 ASSERT(iop->convert != NULL);
7611 7610 rv = (iop->convert)(vdc, arg, mem_p, mode, VD_COPYIN);
7612 7611 if (rv != 0) {
7613 7612 DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
7614 7613 instance, rv, cmd);
7615 7614 if (mem_p != NULL)
7616 7615 kmem_free(mem_p, alloc_len);
7617 7616 return (rv);
7618 7617 }
7619 7618
7620 7619 /*
7621 7620 * send request to vds to service the ioctl.
7622 7621 */
7623 7622 rv = vdc_do_sync_op(vdc, iop->op, mem_p, alloc_len,
7624 7623 VDCPART(dev), 0, VIO_both_dir, B_TRUE);
7625 7624
7626 7625 if (rv != 0) {
7627 7626 /*
7628 7627 * This is not necessarily an error. The ioctl could
7629 7628 * be returning a value such as ENOTTY to indicate
7630 7629 * that the ioctl is not applicable.
7631 7630 */
7632 7631 DMSG(vdc, 0, "[%d] vds returned %d for ioctl 0x%x\n",
7633 7632 instance, rv, cmd);
7634 7633 if (mem_p != NULL)
7635 7634 kmem_free(mem_p, alloc_len);
7636 7635
7637 7636 return (rv);
7638 7637 }
7639 7638
7640 7639 /*
7641 7640 * Call the conversion function (if it exists) for this ioctl
7642 7641 * which converts from the format ARC'ed as part of the vDisk
7643 7642 * protocol (FWARC 2006/195) back to a format understood by
7644 7643 * the rest of Solaris.
7645 7644 */
7646 7645 rv = (iop->convert)(vdc, mem_p, arg, mode, VD_COPYOUT);
7647 7646 if (rv != 0) {
7648 7647 DMSG(vdc, 0, "[%d] convert func returned %d for ioctl 0x%x\n",
7649 7648 instance, rv, cmd);
7650 7649 if (mem_p != NULL)
7651 7650 kmem_free(mem_p, alloc_len);
7652 7651 return (rv);
7653 7652 }
7654 7653
7655 7654 if (mem_p != NULL)
7656 7655 kmem_free(mem_p, alloc_len);
7657 7656
7658 7657 return (rv);
7659 7658 }
7660 7659
7661 7660 /*
7662 7661 * Function:
7663 7662 *
7664 7663 * Description:
7665 7664 * This is an empty conversion function used by ioctl calls which
7666 7665 * do not need to convert the data being passed in/out to userland
7667 7666 */
7668 7667 static int
7669 7668 vdc_null_copy_func(vdc_t *vdc, void *from, void *to, int mode, int dir)
7670 7669 {
7671 7670 _NOTE(ARGUNUSED(vdc))
7672 7671 _NOTE(ARGUNUSED(from))
7673 7672 _NOTE(ARGUNUSED(to))
7674 7673 _NOTE(ARGUNUSED(mode))
7675 7674 _NOTE(ARGUNUSED(dir))
7676 7675
7677 7676 return (0);
7678 7677 }
7679 7678
7680 7679 static int
7681 7680 vdc_get_wce_convert(vdc_t *vdc, void *from, void *to,
7682 7681 int mode, int dir)
7683 7682 {
7684 7683 _NOTE(ARGUNUSED(vdc))
7685 7684
7686 7685 if (dir == VD_COPYIN)
7687 7686 return (0); /* nothing to do */
7688 7687
7689 7688 if (ddi_copyout(from, to, sizeof (int), mode) != 0)
7690 7689 return (EFAULT);
7691 7690
7692 7691 return (0);
7693 7692 }
7694 7693
7695 7694 static int
7696 7695 vdc_set_wce_convert(vdc_t *vdc, void *from, void *to,
7697 7696 int mode, int dir)
7698 7697 {
7699 7698 _NOTE(ARGUNUSED(vdc))
7700 7699
7701 7700 if (dir == VD_COPYOUT)
7702 7701 return (0); /* nothing to do */
7703 7702
7704 7703 if (ddi_copyin(from, to, sizeof (int), mode) != 0)
7705 7704 return (EFAULT);
7706 7705
7707 7706 return (0);
7708 7707 }
7709 7708
7710 7709 /*
7711 7710 * Function:
7712 7711 * vdc_get_vtoc_convert()
7713 7712 *
7714 7713 * Description:
7715 7714 * This routine performs the necessary convertions from the DKIOCGVTOC
7716 7715 * Solaris structure to the format defined in FWARC 2006/195.
7717 7716 *
7718 7717 * In the struct vtoc definition, the timestamp field is marked as not
7719 7718 * supported so it is not part of vDisk protocol (FWARC 2006/195).
7720 7719 * However SVM uses that field to check it can write into the VTOC,
7721 7720 * so we fake up the info of that field.
7722 7721 *
7723 7722 * Arguments:
7724 7723 * vdc - the vDisk client
7725 7724 * from - the buffer containing the data to be copied from
7726 7725 * to - the buffer to be copied to
7727 7726 * mode - flags passed to ioctl() call
7728 7727 * dir - the "direction" of the copy - VD_COPYIN or VD_COPYOUT
7729 7728 *
7730 7729 * Return Code:
7731 7730 * 0 - Success
7732 7731 * ENXIO - incorrect buffer passed in.
7733 7732 * EFAULT - ddi_copyout routine encountered an error.
7734 7733 */
7735 7734 static int
7736 7735 vdc_get_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
7737 7736 {
7738 7737 int i;
7739 7738 struct vtoc vtoc;
7740 7739 struct vtoc32 vtoc32;
7741 7740 struct extvtoc evtoc;
7742 7741 int rv;
7743 7742
7744 7743 if (dir != VD_COPYOUT)
7745 7744 return (0); /* nothing to do */
7746 7745
7747 7746 if ((from == NULL) || (to == NULL))
7748 7747 return (ENXIO);
7749 7748
7750 7749 if (vdc->vdisk_size > VD_OLDVTOC_LIMIT)
7751 7750 return (EOVERFLOW);
7752 7751
7753 7752 VD_VTOC2VTOC((vd_vtoc_t *)from, &evtoc);
7754 7753
7755 7754 /* fake the VTOC timestamp field */
7756 7755 for (i = 0; i < V_NUMPAR; i++) {
7757 7756 evtoc.timestamp[i] = vdc->vtoc->timestamp[i];
7758 7757 }
7759 7758
7760 7759 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
7761 7760 /* LINTED E_ASSIGN_NARROW_CONV */
7762 7761 extvtoctovtoc32(evtoc, vtoc32);
7763 7762 rv = ddi_copyout(&vtoc32, to, sizeof (vtoc32), mode);
7764 7763 if (rv != 0)
7765 7764 rv = EFAULT;
7766 7765 } else {
7767 7766 extvtoctovtoc(evtoc, vtoc);
7768 7767 rv = ddi_copyout(&vtoc, to, sizeof (vtoc), mode);
7769 7768 if (rv != 0)
7770 7769 rv = EFAULT;
7771 7770 }
7772 7771
7773 7772 return (rv);
7774 7773 }
7775 7774
7776 7775 /*
7777 7776 * Function:
7778 7777 * vdc_set_vtoc_convert()
7779 7778 *
7780 7779 * Description:
7781 7780 * This routine performs the necessary convertions from the DKIOCSVTOC
7782 7781 * Solaris structure to the format defined in FWARC 2006/195.
7783 7782 *
7784 7783 * Arguments:
7785 7784 * vdc - the vDisk client
7786 7785 * from - Buffer with data
7787 7786 * to - Buffer where data is to be copied to
7788 7787 * mode - flags passed to ioctl
7789 7788 * dir - direction of copy (in or out)
7790 7789 *
7791 7790 * Return Code:
7792 7791 * 0 - Success
7793 7792 * ENXIO - Invalid buffer passed in
7794 7793 * EFAULT - ddi_copyin of data failed
7795 7794 */
7796 7795 static int
7797 7796 vdc_set_vtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
7798 7797 {
7799 7798 void *uvtoc;
7800 7799 struct vtoc vtoc;
7801 7800 struct vtoc32 vtoc32;
7802 7801 struct extvtoc evtoc;
7803 7802 int i, rv;
7804 7803
7805 7804 if ((from == NULL) || (to == NULL))
7806 7805 return (ENXIO);
7807 7806
7808 7807 if (vdc->vdisk_size > VD_OLDVTOC_LIMIT)
7809 7808 return (EOVERFLOW);
7810 7809
7811 7810 uvtoc = (dir == VD_COPYIN)? from : to;
7812 7811
7813 7812 if (ddi_model_convert_from(mode & FMODELS) == DDI_MODEL_ILP32) {
7814 7813 rv = ddi_copyin(uvtoc, &vtoc32, sizeof (vtoc32), mode);
7815 7814 if (rv != 0)
7816 7815 return (EFAULT);
7817 7816 vtoc32toextvtoc(vtoc32, evtoc);
7818 7817 } else {
7819 7818 rv = ddi_copyin(uvtoc, &vtoc, sizeof (vtoc), mode);
7820 7819 if (rv != 0)
7821 7820 return (EFAULT);
7822 7821 vtoctoextvtoc(vtoc, evtoc);
7823 7822 }
7824 7823
7825 7824 if (dir == VD_COPYOUT) {
7826 7825 /*
7827 7826 * The disk label may have changed. Revalidate the disk
7828 7827 * geometry. This will also update the device nodes.
7829 7828 */
7830 7829 vdc_validate(vdc);
7831 7830
7832 7831 /*
7833 7832 * We also need to keep track of the timestamp fields.
7834 7833 */
7835 7834 for (i = 0; i < V_NUMPAR; i++) {
7836 7835 vdc->vtoc->timestamp[i] = evtoc.timestamp[i];
7837 7836 }
7838 7837
7839 7838 } else {
7840 7839 VTOC2VD_VTOC(&evtoc, (vd_vtoc_t *)to);
7841 7840 }
7842 7841
7843 7842 return (0);
7844 7843 }
7845 7844
7846 7845 static int
7847 7846 vdc_get_extvtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
7848 7847 {
7849 7848 int i, rv;
7850 7849 struct extvtoc evtoc;
7851 7850
7852 7851 if (dir != VD_COPYOUT)
7853 7852 return (0); /* nothing to do */
7854 7853
7855 7854 if ((from == NULL) || (to == NULL))
7856 7855 return (ENXIO);
7857 7856
7858 7857 VD_VTOC2VTOC((vd_vtoc_t *)from, &evtoc);
7859 7858
7860 7859 /* fake the VTOC timestamp field */
7861 7860 for (i = 0; i < V_NUMPAR; i++) {
7862 7861 evtoc.timestamp[i] = vdc->vtoc->timestamp[i];
7863 7862 }
7864 7863
7865 7864 rv = ddi_copyout(&evtoc, to, sizeof (struct extvtoc), mode);
7866 7865 if (rv != 0)
7867 7866 rv = EFAULT;
7868 7867
7869 7868 return (rv);
7870 7869 }
7871 7870
7872 7871 static int
7873 7872 vdc_set_extvtoc_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
7874 7873 {
7875 7874 void *uvtoc;
7876 7875 struct extvtoc evtoc;
7877 7876 int i, rv;
7878 7877
7879 7878 if ((from == NULL) || (to == NULL))
7880 7879 return (ENXIO);
7881 7880
7882 7881 uvtoc = (dir == VD_COPYIN)? from : to;
7883 7882
7884 7883 rv = ddi_copyin(uvtoc, &evtoc, sizeof (struct extvtoc), mode);
7885 7884 if (rv != 0)
7886 7885 return (EFAULT);
7887 7886
7888 7887 if (dir == VD_COPYOUT) {
7889 7888 /*
7890 7889 * The disk label may have changed. Revalidate the disk
7891 7890 * geometry. This will also update the device nodes.
7892 7891 */
7893 7892 vdc_validate(vdc);
7894 7893
7895 7894 /*
7896 7895 * We also need to keep track of the timestamp fields.
7897 7896 */
7898 7897 for (i = 0; i < V_NUMPAR; i++) {
7899 7898 vdc->vtoc->timestamp[i] = evtoc.timestamp[i];
7900 7899 }
7901 7900
7902 7901 } else {
7903 7902 VTOC2VD_VTOC(&evtoc, (vd_vtoc_t *)to);
7904 7903 }
7905 7904
7906 7905 return (0);
7907 7906 }
7908 7907
7909 7908 /*
7910 7909 * Function:
7911 7910 * vdc_get_geom_convert()
7912 7911 *
7913 7912 * Description:
7914 7913 * This routine performs the necessary convertions from the DKIOCGGEOM,
7915 7914 * DKIOCG_PHYSGEOM and DKIOG_VIRTGEOM Solaris structures to the format
7916 7915 * defined in FWARC 2006/195
7917 7916 *
7918 7917 * Arguments:
7919 7918 * vdc - the vDisk client
7920 7919 * from - Buffer with data
7921 7920 * to - Buffer where data is to be copied to
7922 7921 * mode - flags passed to ioctl
7923 7922 * dir - direction of copy (in or out)
7924 7923 *
7925 7924 * Return Code:
7926 7925 * 0 - Success
7927 7926 * ENXIO - Invalid buffer passed in
7928 7927 * EFAULT - ddi_copyout of data failed
7929 7928 */
7930 7929 static int
7931 7930 vdc_get_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
7932 7931 {
7933 7932 _NOTE(ARGUNUSED(vdc))
7934 7933
7935 7934 struct dk_geom geom;
7936 7935 int copy_len = sizeof (struct dk_geom);
7937 7936 int rv = 0;
7938 7937
7939 7938 if (dir != VD_COPYOUT)
7940 7939 return (0); /* nothing to do */
7941 7940
7942 7941 if ((from == NULL) || (to == NULL))
7943 7942 return (ENXIO);
7944 7943
7945 7944 VD_GEOM2DK_GEOM((vd_geom_t *)from, &geom);
7946 7945 rv = ddi_copyout(&geom, to, copy_len, mode);
7947 7946 if (rv != 0)
7948 7947 rv = EFAULT;
7949 7948
7950 7949 return (rv);
7951 7950 }
7952 7951
7953 7952 /*
7954 7953 * Function:
7955 7954 * vdc_set_geom_convert()
7956 7955 *
7957 7956 * Description:
7958 7957 * This routine performs the necessary convertions from the DKIOCSGEOM
7959 7958 * Solaris structure to the format defined in FWARC 2006/195.
7960 7959 *
7961 7960 * Arguments:
7962 7961 * vdc - the vDisk client
7963 7962 * from - Buffer with data
7964 7963 * to - Buffer where data is to be copied to
7965 7964 * mode - flags passed to ioctl
7966 7965 * dir - direction of copy (in or out)
7967 7966 *
7968 7967 * Return Code:
7969 7968 * 0 - Success
7970 7969 * ENXIO - Invalid buffer passed in
7971 7970 * EFAULT - ddi_copyin of data failed
7972 7971 */
7973 7972 static int
7974 7973 vdc_set_geom_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
7975 7974 {
7976 7975 _NOTE(ARGUNUSED(vdc))
7977 7976
7978 7977 vd_geom_t vdgeom;
7979 7978 void *tmp_mem = NULL;
7980 7979 int copy_len = sizeof (struct dk_geom);
7981 7980 int rv = 0;
7982 7981
7983 7982 if (dir != VD_COPYIN)
7984 7983 return (0); /* nothing to do */
7985 7984
7986 7985 if ((from == NULL) || (to == NULL))
7987 7986 return (ENXIO);
7988 7987
7989 7988 tmp_mem = kmem_alloc(copy_len, KM_SLEEP);
7990 7989
7991 7990 rv = ddi_copyin(from, tmp_mem, copy_len, mode);
7992 7991 if (rv != 0) {
7993 7992 kmem_free(tmp_mem, copy_len);
7994 7993 return (EFAULT);
7995 7994 }
7996 7995 DK_GEOM2VD_GEOM((struct dk_geom *)tmp_mem, &vdgeom);
7997 7996 bcopy(&vdgeom, to, sizeof (vdgeom));
7998 7997 kmem_free(tmp_mem, copy_len);
7999 7998
8000 7999 return (0);
8001 8000 }
8002 8001
8003 8002 static int
8004 8003 vdc_get_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
8005 8004 {
8006 8005 _NOTE(ARGUNUSED(vdc))
8007 8006
8008 8007 vd_efi_t *vd_efi;
8009 8008 dk_efi_t dk_efi;
8010 8009 int rv = 0;
8011 8010 void *uaddr;
8012 8011
8013 8012 if ((from == NULL) || (to == NULL))
8014 8013 return (ENXIO);
8015 8014
8016 8015 if (dir == VD_COPYIN) {
8017 8016
8018 8017 vd_efi = (vd_efi_t *)to;
8019 8018
8020 8019 rv = ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode);
8021 8020 if (rv != 0)
8022 8021 return (EFAULT);
8023 8022
8024 8023 vd_efi->lba = dk_efi.dki_lba;
8025 8024 vd_efi->length = dk_efi.dki_length;
8026 8025 bzero(vd_efi->data, vd_efi->length);
8027 8026
8028 8027 } else {
8029 8028
8030 8029 rv = ddi_copyin(to, &dk_efi, sizeof (dk_efi_t), mode);
8031 8030 if (rv != 0)
8032 8031 return (EFAULT);
8033 8032
8034 8033 uaddr = dk_efi.dki_data;
8035 8034
8036 8035 dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
8037 8036
8038 8037 VD_EFI2DK_EFI((vd_efi_t *)from, &dk_efi);
8039 8038
8040 8039 rv = ddi_copyout(dk_efi.dki_data, uaddr, dk_efi.dki_length,
8041 8040 mode);
8042 8041 if (rv != 0)
8043 8042 return (EFAULT);
8044 8043
8045 8044 kmem_free(dk_efi.dki_data, dk_efi.dki_length);
8046 8045 }
8047 8046
8048 8047 return (0);
8049 8048 }
8050 8049
8051 8050 static int
8052 8051 vdc_set_efi_convert(vdc_t *vdc, void *from, void *to, int mode, int dir)
8053 8052 {
8054 8053 _NOTE(ARGUNUSED(vdc))
8055 8054
8056 8055 dk_efi_t dk_efi;
8057 8056 void *uaddr;
8058 8057
8059 8058 if (dir == VD_COPYOUT) {
8060 8059 /*
8061 8060 * The disk label may have changed. Revalidate the disk
8062 8061 * geometry. This will also update the device nodes.
8063 8062 */
8064 8063 vdc_validate(vdc);
8065 8064 return (0);
8066 8065 }
8067 8066
8068 8067 if ((from == NULL) || (to == NULL))
8069 8068 return (ENXIO);
8070 8069
8071 8070 if (ddi_copyin(from, &dk_efi, sizeof (dk_efi_t), mode) != 0)
8072 8071 return (EFAULT);
8073 8072
8074 8073 uaddr = dk_efi.dki_data;
8075 8074
8076 8075 dk_efi.dki_data = kmem_alloc(dk_efi.dki_length, KM_SLEEP);
8077 8076
8078 8077 if (ddi_copyin(uaddr, dk_efi.dki_data, dk_efi.dki_length, mode) != 0)
8079 8078 return (EFAULT);
8080 8079
8081 8080 DK_EFI2VD_EFI(&dk_efi, (vd_efi_t *)to);
8082 8081
8083 8082 kmem_free(dk_efi.dki_data, dk_efi.dki_length);
8084 8083
8085 8084 return (0);
8086 8085 }
8087 8086
8088 8087
8089 8088 /* -------------------------------------------------------------------------- */
8090 8089
8091 8090 /*
8092 8091 * Function:
8093 8092 * vdc_create_fake_geometry()
8094 8093 *
8095 8094 * Description:
8096 8095 * This routine fakes up the disk info needed for some DKIO ioctls such
8097 8096 * as DKIOCINFO and DKIOCGMEDIAINFO [just like lofi(7D) and ramdisk(7D) do]
8098 8097 *
8099 8098 * Note: This function must not be called until the vDisk attributes have
8100 8099 * been exchanged as part of the handshake with the vDisk server.
8101 8100 *
8102 8101 * Arguments:
8103 8102 * vdc - soft state pointer for this instance of the device driver.
8104 8103 *
8105 8104 * Return Code:
8106 8105 * none.
8107 8106 */
8108 8107 static void
8109 8108 vdc_create_fake_geometry(vdc_t *vdc)
8110 8109 {
8111 8110 ASSERT(vdc != NULL);
8112 8111 ASSERT(vdc->max_xfer_sz != 0);
8113 8112
8114 8113 /*
8115 8114 * DKIOCINFO support
8116 8115 */
8117 8116 if (vdc->cinfo == NULL)
8118 8117 vdc->cinfo = kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
8119 8118
8120 8119 (void) strcpy(vdc->cinfo->dki_cname, VDC_DRIVER_NAME);
8121 8120 (void) strcpy(vdc->cinfo->dki_dname, VDC_DRIVER_NAME);
8122 8121 /* max_xfer_sz is #blocks so we don't need to divide by vdisk_bsize */
8123 8122 vdc->cinfo->dki_maxtransfer = vdc->max_xfer_sz;
8124 8123
8125 8124 /*
8126 8125 * We set the controller type to DKC_SCSI_CCS only if the VD_OP_SCSICMD
8127 8126 * operation is supported, otherwise the controller type is DKC_DIRECT.
8128 8127 * Version 1.0 does not support the VD_OP_SCSICMD operation, so the
8129 8128 * controller type is always DKC_DIRECT in that case.
8130 8129 *
8131 8130 * If the virtual disk is backed by a physical CD/DVD device or
8132 8131 * an ISO image, modify the controller type to indicate this
8133 8132 */
8134 8133 switch (vdc->vdisk_media) {
8135 8134 case VD_MEDIA_CD:
8136 8135 case VD_MEDIA_DVD:
8137 8136 vdc->cinfo->dki_ctype = DKC_CDROM;
8138 8137 break;
8139 8138 case VD_MEDIA_FIXED:
8140 8139 if (VD_OP_SUPPORTED(vdc->operations, VD_OP_SCSICMD))
8141 8140 vdc->cinfo->dki_ctype = DKC_SCSI_CCS;
8142 8141 else
8143 8142 vdc->cinfo->dki_ctype = DKC_DIRECT;
8144 8143 break;
8145 8144 default:
8146 8145 /* in the case of v1.0 we default to a fixed disk */
8147 8146 vdc->cinfo->dki_ctype = DKC_DIRECT;
8148 8147 break;
8149 8148 }
8150 8149 vdc->cinfo->dki_flags = DKI_FMTVOL;
8151 8150 vdc->cinfo->dki_cnum = 0;
8152 8151 vdc->cinfo->dki_addr = 0;
8153 8152 vdc->cinfo->dki_space = 0;
8154 8153 vdc->cinfo->dki_prio = 0;
8155 8154 vdc->cinfo->dki_vec = 0;
8156 8155 vdc->cinfo->dki_unit = vdc->instance;
8157 8156 vdc->cinfo->dki_slave = 0;
8158 8157 /*
8159 8158 * The partition number will be created on the fly depending on the
8160 8159 * actual slice (i.e. minor node) that is used to request the data.
8161 8160 */
8162 8161 vdc->cinfo->dki_partition = 0;
8163 8162
8164 8163 /*
8165 8164 * DKIOCGMEDIAINFO support
8166 8165 */
8167 8166 if (vdc->minfo == NULL)
8168 8167 vdc->minfo = kmem_zalloc(sizeof (struct dk_minfo), KM_SLEEP);
8169 8168
8170 8169 if (vio_ver_is_supported(vdc->ver, 1, 1)) {
8171 8170 vdc->minfo->dki_media_type =
8172 8171 VD_MEDIATYPE2DK_MEDIATYPE(vdc->vdisk_media);
8173 8172 } else {
8174 8173 vdc->minfo->dki_media_type = DK_FIXED_DISK;
8175 8174 }
8176 8175
8177 8176 vdc->minfo->dki_capacity = vdc->vdisk_size;
8178 8177 vdc->minfo->dki_lbsize = vdc->vdisk_bsize;
8179 8178 }
8180 8179
8181 8180 static ushort_t
8182 8181 vdc_lbl2cksum(struct dk_label *label)
8183 8182 {
8184 8183 int count;
8185 8184 ushort_t sum, *sp;
8186 8185
8187 8186 count = (sizeof (struct dk_label)) / (sizeof (short)) - 1;
8188 8187 sp = (ushort_t *)label;
8189 8188 sum = 0;
8190 8189 while (count--) {
8191 8190 sum ^= *sp++;
8192 8191 }
8193 8192
8194 8193 return (sum);
8195 8194 }
8196 8195
8197 8196 static void
8198 8197 vdc_update_size(vdc_t *vdc, size_t dsk_size, size_t blk_size, size_t xfr_size)
8199 8198 {
8200 8199 vd_err_stats_t *stp;
8201 8200
8202 8201 ASSERT(MUTEX_HELD(&vdc->lock));
8203 8202 ASSERT(xfr_size != 0);
8204 8203
8205 8204 /*
8206 8205 * If the disk size is unknown or sizes are unchanged then don't
8207 8206 * update anything.
8208 8207 */
8209 8208 if (dsk_size == VD_SIZE_UNKNOWN || dsk_size == 0 ||
8210 8209 (blk_size == vdc->vdisk_bsize && dsk_size == vdc->vdisk_size &&
8211 8210 xfr_size == vdc->max_xfer_sz))
8212 8211 return;
8213 8212
8214 8213 /*
8215 8214 * We don't know at compile time what the vDisk server will think
8216 8215 * are good values but we apply a large (arbitrary) upper bound to
8217 8216 * prevent memory exhaustion in vdc if it was allocating a DRing
8218 8217 * based of huge values sent by the server. We probably will never
8219 8218 * exceed this except if the message was garbage.
8220 8219 */
8221 8220 if ((xfr_size * blk_size) > (PAGESIZE * DEV_BSIZE)) {
8222 8221 DMSG(vdc, 0, "[%d] vds block transfer size too big;"
8223 8222 " using max supported by vdc", vdc->instance);
8224 8223 xfr_size = maxphys / blk_size;
8225 8224 }
8226 8225
8227 8226 vdc->max_xfer_sz = xfr_size;
8228 8227 vdc->vdisk_bsize = blk_size;
8229 8228 vdc->vdisk_size = dsk_size;
8230 8229
8231 8230 stp = (vd_err_stats_t *)vdc->err_stats->ks_data;
8232 8231 stp->vd_capacity.value.ui64 = dsk_size * blk_size;
8233 8232
8234 8233 vdc->minfo->dki_capacity = dsk_size;
8235 8234 vdc->minfo->dki_lbsize = (uint_t)blk_size;
8236 8235 }
8237 8236
8238 8237 /*
8239 8238 * Update information about the VIO block size. The VIO block size is the
8240 8239 * same as the vdisk block size which is stored in vdc->vdisk_bsize so we
8241 8240 * do not store that information again.
8242 8241 *
8243 8242 * However, buf structures will always use a logical block size of 512 bytes
8244 8243 * (DEV_BSIZE) and we will need to convert logical block numbers to VIO block
8245 8244 * numbers for each read or write operation using vdc_strategy(). To speed up
8246 8245 * this conversion, we expect the VIO block size to be a power of 2 and a
8247 8246 * multiple 512 bytes (DEV_BSIZE), and we cache some useful information.
8248 8247 *
8249 8248 * The function return EINVAL if the new VIO block size (blk_size) is not a
8250 8249 * power of 2 or not a multiple of 512 bytes, otherwise it returns 0.
8251 8250 */
8252 8251 static int
8253 8252 vdc_update_vio_bsize(vdc_t *vdc, uint32_t blk_size)
8254 8253 {
8255 8254 uint32_t ratio, n;
8256 8255 int nshift = 0;
8257 8256
8258 8257 vdc->vio_bmask = 0;
8259 8258 vdc->vio_bshift = 0;
8260 8259
8261 8260 ASSERT(blk_size > 0);
8262 8261
8263 8262 if ((blk_size % DEV_BSIZE) != 0)
8264 8263 return (EINVAL);
8265 8264
8266 8265 ratio = blk_size / DEV_BSIZE;
8267 8266
8268 8267 for (n = ratio; n > 1; n >>= 1) {
8269 8268 if ((n & 0x1) != 0) {
8270 8269 /* blk_size is not a power of 2 */
8271 8270 return (EINVAL);
8272 8271 }
8273 8272 nshift++;
8274 8273 }
8275 8274
8276 8275 vdc->vio_bshift = nshift;
8277 8276 vdc->vio_bmask = ratio - 1;
8278 8277
8279 8278 return (0);
8280 8279 }
8281 8280
8282 8281 /*
8283 8282 * Function:
8284 8283 * vdc_validate_geometry
8285 8284 *
8286 8285 * Description:
8287 8286 * This routine discovers the label and geometry of the disk. It stores
8288 8287 * the disk label and related information in the vdc structure. If it
8289 8288 * fails to validate the geometry or to discover the disk label then
8290 8289 * the label is marked as unknown (VD_DISK_LABEL_UNK).
8291 8290 *
8292 8291 * Arguments:
8293 8292 * vdc - soft state pointer for this instance of the device driver.
8294 8293 *
8295 8294 * Return Code:
8296 8295 * 0 - success.
8297 8296 * EINVAL - unknown disk label.
8298 8297 * ENOTSUP - geometry not applicable (EFI label).
8299 8298 * EIO - error accessing the disk.
8300 8299 */
8301 8300 static int
8302 8301 vdc_validate_geometry(vdc_t *vdc)
8303 8302 {
8304 8303 dev_t dev;
8305 8304 int rv, rval;
8306 8305 struct dk_label *label;
8307 8306 struct dk_geom geom;
8308 8307 struct extvtoc vtoc;
8309 8308 efi_gpt_t *gpt;
8310 8309 efi_gpe_t *gpe;
8311 8310 vd_efi_dev_t edev;
8312 8311
8313 8312 ASSERT(vdc != NULL);
8314 8313 ASSERT(vdc->vtoc != NULL && vdc->geom != NULL);
8315 8314 ASSERT(MUTEX_HELD(&vdc->lock));
8316 8315
8317 8316 mutex_exit(&vdc->lock);
8318 8317 /*
8319 8318 * Check the disk capacity in case it has changed. If that fails then
8320 8319 * we proceed and we will be using the disk size we currently have.
8321 8320 */
8322 8321 (void) vdc_check_capacity(vdc);
8323 8322 dev = makedevice(ddi_driver_major(vdc->dip),
8324 8323 VD_MAKE_DEV(vdc->instance, 0));
8325 8324
8326 8325 rv = vd_process_ioctl(dev, DKIOCGGEOM, (caddr_t)&geom, FKIOCTL, &rval);
8327 8326 if (rv == 0)
8328 8327 rv = vd_process_ioctl(dev, DKIOCGEXTVTOC, (caddr_t)&vtoc,
8329 8328 FKIOCTL, &rval);
8330 8329
8331 8330 if (rv == ENOTSUP) {
8332 8331 /*
8333 8332 * If the device does not support VTOC then we try
8334 8333 * to read an EFI label.
8335 8334 *
8336 8335 * We need to know the block size and the disk size to
8337 8336 * be able to read an EFI label.
8338 8337 */
8339 8338 if (vdc->vdisk_size == 0) {
8340 8339 mutex_enter(&vdc->lock);
8341 8340 vdc_store_label_unk(vdc);
8342 8341 return (EIO);
8343 8342 }
8344 8343
8345 8344 VDC_EFI_DEV_SET(edev, vdc, vd_process_efi_ioctl);
8346 8345
8347 8346 rv = vd_efi_alloc_and_read(&edev, &gpt, &gpe);
8348 8347
8349 8348 if (rv) {
8350 8349 DMSG(vdc, 0, "[%d] Failed to get EFI (err=%d)",
8351 8350 vdc->instance, rv);
8352 8351 mutex_enter(&vdc->lock);
8353 8352 vdc_store_label_unk(vdc);
8354 8353 return (EIO);
8355 8354 }
8356 8355
8357 8356 mutex_enter(&vdc->lock);
8358 8357 vdc_store_label_efi(vdc, gpt, gpe);
8359 8358 vd_efi_free(&edev, gpt, gpe);
8360 8359 return (ENOTSUP);
8361 8360 }
8362 8361
8363 8362 if (rv != 0) {
8364 8363 DMSG(vdc, 0, "[%d] Failed to get VTOC (err=%d)",
8365 8364 vdc->instance, rv);
8366 8365 mutex_enter(&vdc->lock);
8367 8366 vdc_store_label_unk(vdc);
8368 8367 if (rv != EINVAL)
8369 8368 rv = EIO;
8370 8369 return (rv);
8371 8370 }
8372 8371
8373 8372 /* check that geometry and vtoc are valid */
8374 8373 if (geom.dkg_nhead == 0 || geom.dkg_nsect == 0 ||
8375 8374 vtoc.v_sanity != VTOC_SANE) {
8376 8375 mutex_enter(&vdc->lock);
8377 8376 vdc_store_label_unk(vdc);
8378 8377 return (EINVAL);
8379 8378 }
8380 8379
8381 8380 /*
8382 8381 * We have a disk and a valid VTOC. However this does not mean
8383 8382 * that the disk currently have a VTOC label. The returned VTOC may
8384 8383 * be a default VTOC to be used for configuring the disk (this is
8385 8384 * what is done for disk image). So we read the label from the
8386 8385 * beginning of the disk to ensure we really have a VTOC label.
8387 8386 *
8388 8387 * FUTURE: This could be the default way for reading the VTOC
8389 8388 * from the disk as opposed to sending the VD_OP_GET_VTOC
8390 8389 * to the server. This will be the default if vdc is implemented
8391 8390 * ontop of cmlb.
8392 8391 */
8393 8392
8394 8393 /*
8395 8394 * Single slice disk does not support read using an absolute disk
8396 8395 * offset so we just rely on the DKIOCGVTOC ioctl in that case.
8397 8396 */
8398 8397 if (vdc->vdisk_type == VD_DISK_TYPE_SLICE) {
8399 8398 mutex_enter(&vdc->lock);
8400 8399 if (vtoc.v_nparts != 1) {
8401 8400 vdc_store_label_unk(vdc);
8402 8401 return (EINVAL);
8403 8402 }
8404 8403 vdc_store_label_vtoc(vdc, &geom, &vtoc);
8405 8404 return (0);
8406 8405 }
8407 8406
8408 8407 if (vtoc.v_nparts != V_NUMPAR) {
8409 8408 mutex_enter(&vdc->lock);
8410 8409 vdc_store_label_unk(vdc);
8411 8410 return (EINVAL);
8412 8411 }
8413 8412
8414 8413 /*
8415 8414 * Most CD/DVDs do not have a disk label and the label is
8416 8415 * generated by the disk driver. So the on-disk label check
8417 8416 * below may fail and we return now to avoid this problem.
8418 8417 */
8419 8418 if (vdc->vdisk_media == VD_MEDIA_CD ||
8420 8419 vdc->vdisk_media == VD_MEDIA_DVD) {
8421 8420 mutex_enter(&vdc->lock);
8422 8421 vdc_store_label_vtoc(vdc, &geom, &vtoc);
8423 8422 return (0);
8424 8423 }
8425 8424
8426 8425 /*
8427 8426 * Read disk label from start of disk
8428 8427 */
8429 8428 label = kmem_alloc(vdc->vdisk_bsize, KM_SLEEP);
8430 8429
8431 8430 rv = vdc_do_op(vdc, VD_OP_BREAD, (caddr_t)label, vdc->vdisk_bsize,
8432 8431 VD_SLICE_NONE, 0, NULL, VIO_read_dir, VDC_OP_NORMAL);
8433 8432
8434 8433 if (rv != 0 || label->dkl_magic != DKL_MAGIC ||
8435 8434 label->dkl_cksum != vdc_lbl2cksum(label)) {
8436 8435 DMSG(vdc, 1, "[%d] Got VTOC with invalid label\n",
8437 8436 vdc->instance);
8438 8437 kmem_free(label, vdc->vdisk_bsize);
8439 8438 mutex_enter(&vdc->lock);
8440 8439 vdc_store_label_unk(vdc);
8441 8440 return (EINVAL);
8442 8441 }
8443 8442
8444 8443 kmem_free(label, vdc->vdisk_bsize);
8445 8444 mutex_enter(&vdc->lock);
8446 8445 vdc_store_label_vtoc(vdc, &geom, &vtoc);
8447 8446 return (0);
8448 8447 }
8449 8448
8450 8449 /*
8451 8450 * Function:
8452 8451 * vdc_validate
8453 8452 *
8454 8453 * Description:
8455 8454 * This routine discovers the label of the disk and create the
8456 8455 * appropriate device nodes if the label has changed.
8457 8456 *
8458 8457 * Arguments:
8459 8458 * vdc - soft state pointer for this instance of the device driver.
8460 8459 *
8461 8460 * Return Code:
8462 8461 * none.
8463 8462 */
8464 8463 static void
8465 8464 vdc_validate(vdc_t *vdc)
8466 8465 {
8467 8466 vd_disk_label_t old_label;
8468 8467 vd_slice_t old_slice[V_NUMPAR];
8469 8468 int rv;
8470 8469
8471 8470 ASSERT(!MUTEX_HELD(&vdc->lock));
8472 8471
8473 8472 mutex_enter(&vdc->lock);
8474 8473
8475 8474 /* save the current label and vtoc */
8476 8475 old_label = vdc->vdisk_label;
8477 8476 bcopy(vdc->slice, &old_slice, sizeof (vd_slice_t) * V_NUMPAR);
8478 8477
8479 8478 /* check the geometry */
8480 8479 (void) vdc_validate_geometry(vdc);
8481 8480
8482 8481 /* if the disk label has changed, update device nodes */
8483 8482 if (vdc->vdisk_type == VD_DISK_TYPE_DISK &&
8484 8483 vdc->vdisk_label != old_label) {
8485 8484
8486 8485 if (vdc->vdisk_label == VD_DISK_LABEL_EFI)
8487 8486 rv = vdc_create_device_nodes_efi(vdc);
8488 8487 else
8489 8488 rv = vdc_create_device_nodes_vtoc(vdc);
8490 8489
8491 8490 if (rv != 0) {
8492 8491 DMSG(vdc, 0, "![%d] Failed to update device nodes",
8493 8492 vdc->instance);
8494 8493 }
8495 8494 }
8496 8495
8497 8496 mutex_exit(&vdc->lock);
8498 8497 }
8499 8498
8500 8499 static void
8501 8500 vdc_validate_task(void *arg)
8502 8501 {
8503 8502 vdc_t *vdc = (vdc_t *)arg;
8504 8503
8505 8504 vdc_validate(vdc);
8506 8505
8507 8506 mutex_enter(&vdc->lock);
8508 8507 ASSERT(vdc->validate_pending > 0);
8509 8508 vdc->validate_pending--;
8510 8509 mutex_exit(&vdc->lock);
8511 8510 }
8512 8511
8513 8512 /*
8514 8513 * Function:
8515 8514 * vdc_setup_devid()
8516 8515 *
8517 8516 * Description:
8518 8517 * This routine discovers the devid of a vDisk. It requests the devid of
8519 8518 * the underlying device from the vDisk server, builds an encapsulated
8520 8519 * devid based on the retrieved devid and registers that new devid to
8521 8520 * the vDisk.
8522 8521 *
8523 8522 * Arguments:
8524 8523 * vdc - soft state pointer for this instance of the device driver.
8525 8524 *
8526 8525 * Return Code:
8527 8526 * 0 - A devid was succesfully registered for the vDisk
8528 8527 */
8529 8528 static int
8530 8529 vdc_setup_devid(vdc_t *vdc)
8531 8530 {
8532 8531 int rv;
8533 8532 vd_devid_t *vd_devid;
8534 8533 size_t bufsize, bufid_len;
8535 8534 ddi_devid_t vdisk_devid;
8536 8535 char *devid_str;
8537 8536
8538 8537 /*
8539 8538 * At first sight, we don't know the size of the devid that the
8540 8539 * server will return but this size will be encoded into the
8541 8540 * reply. So we do a first request using a default size then we
8542 8541 * check if this size was large enough. If not then we do a second
8543 8542 * request with the correct size returned by the server. Note that
8544 8543 * ldc requires size to be 8-byte aligned.
8545 8544 */
8546 8545 bufsize = P2ROUNDUP(VD_DEVID_SIZE(VD_DEVID_DEFAULT_LEN),
8547 8546 sizeof (uint64_t));
8548 8547 vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
8549 8548 bufid_len = bufsize - sizeof (vd_efi_t) - 1;
8550 8549
8551 8550 rv = vdc_do_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid,
8552 8551 bufsize, 0, 0, NULL, VIO_both_dir, 0);
8553 8552
8554 8553 DMSG(vdc, 2, "do_op returned %d\n", rv);
8555 8554
8556 8555 if (rv) {
8557 8556 kmem_free(vd_devid, bufsize);
8558 8557 return (rv);
8559 8558 }
8560 8559
8561 8560 if (vd_devid->length > bufid_len) {
8562 8561 /*
8563 8562 * The returned devid is larger than the buffer used. Try again
8564 8563 * with a buffer with the right size.
8565 8564 */
8566 8565 kmem_free(vd_devid, bufsize);
8567 8566 bufsize = P2ROUNDUP(VD_DEVID_SIZE(vd_devid->length),
8568 8567 sizeof (uint64_t));
8569 8568 vd_devid = kmem_zalloc(bufsize, KM_SLEEP);
8570 8569 bufid_len = bufsize - sizeof (vd_efi_t) - 1;
8571 8570
8572 8571 rv = vdc_do_sync_op(vdc, VD_OP_GET_DEVID, (caddr_t)vd_devid,
8573 8572 bufsize, 0, 0, VIO_both_dir, B_TRUE);
8574 8573
8575 8574 if (rv) {
8576 8575 kmem_free(vd_devid, bufsize);
8577 8576 return (rv);
8578 8577 }
8579 8578 }
8580 8579
8581 8580 /*
8582 8581 * The virtual disk should have the same device id as the one associated
8583 8582 * with the physical disk it is mapped on, otherwise sharing a disk
8584 8583 * between a LDom and a non-LDom may not work (for example for a shared
8585 8584 * SVM disk set).
8586 8585 *
8587 8586 * The DDI framework does not allow creating a device id with any
8588 8587 * type so we first create a device id of type DEVID_ENCAP and then
8589 8588 * we restore the orignal type of the physical device.
8590 8589 */
8591 8590
8592 8591 DMSG(vdc, 2, ": devid length = %d\n", vd_devid->length);
8593 8592
8594 8593 /* build an encapsulated devid based on the returned devid */
8595 8594 if (ddi_devid_init(vdc->dip, DEVID_ENCAP, vd_devid->length,
8596 8595 vd_devid->id, &vdisk_devid) != DDI_SUCCESS) {
8597 8596 DMSG(vdc, 1, "[%d] Fail to created devid\n", vdc->instance);
8598 8597 kmem_free(vd_devid, bufsize);
8599 8598 return (1);
8600 8599 }
8601 8600
8602 8601 DEVID_FORMTYPE((impl_devid_t *)vdisk_devid, vd_devid->type);
8603 8602
8604 8603 ASSERT(ddi_devid_valid(vdisk_devid) == DDI_SUCCESS);
8605 8604
8606 8605 kmem_free(vd_devid, bufsize);
8607 8606
8608 8607 if (vdc->devid != NULL) {
8609 8608 /* check that the devid hasn't changed */
8610 8609 if (ddi_devid_compare(vdisk_devid, vdc->devid) == 0) {
8611 8610 ddi_devid_free(vdisk_devid);
8612 8611 return (0);
8613 8612 }
8614 8613
8615 8614 cmn_err(CE_WARN, "vdisk@%d backend devid has changed",
8616 8615 vdc->instance);
8617 8616
8618 8617 devid_str = ddi_devid_str_encode(vdc->devid, NULL);
8619 8618
8620 8619 cmn_err(CE_CONT, "vdisk@%d backend initial devid: %s",
8621 8620 vdc->instance,
8622 8621 (devid_str)? devid_str : "<encoding error>");
8623 8622
8624 8623 if (devid_str)
8625 8624 ddi_devid_str_free(devid_str);
8626 8625
8627 8626 devid_str = ddi_devid_str_encode(vdisk_devid, NULL);
8628 8627
8629 8628 cmn_err(CE_CONT, "vdisk@%d backend current devid: %s",
8630 8629 vdc->instance,
8631 8630 (devid_str)? devid_str : "<encoding error>");
8632 8631
8633 8632 if (devid_str)
8634 8633 ddi_devid_str_free(devid_str);
8635 8634
8636 8635 ddi_devid_free(vdisk_devid);
8637 8636 return (1);
8638 8637 }
8639 8638
8640 8639 if (ddi_devid_register(vdc->dip, vdisk_devid) != DDI_SUCCESS) {
8641 8640 DMSG(vdc, 1, "[%d] Fail to register devid\n", vdc->instance);
8642 8641 ddi_devid_free(vdisk_devid);
8643 8642 return (1);
8644 8643 }
8645 8644
8646 8645 vdc->devid = vdisk_devid;
8647 8646
8648 8647 return (0);
8649 8648 }
8650 8649
8651 8650 static void
8652 8651 vdc_store_label_efi(vdc_t *vdc, efi_gpt_t *gpt, efi_gpe_t *gpe)
8653 8652 {
8654 8653 int i, nparts;
8655 8654
8656 8655 ASSERT(MUTEX_HELD(&vdc->lock));
8657 8656
8658 8657 vdc->vdisk_label = VD_DISK_LABEL_EFI;
8659 8658 bzero(vdc->vtoc, sizeof (struct extvtoc));
8660 8659 bzero(vdc->geom, sizeof (struct dk_geom));
8661 8660 bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
8662 8661
8663 8662 nparts = gpt->efi_gpt_NumberOfPartitionEntries;
8664 8663
8665 8664 for (i = 0; i < nparts && i < VD_EFI_WD_SLICE; i++) {
8666 8665
8667 8666 if (gpe[i].efi_gpe_StartingLBA == 0 &&
8668 8667 gpe[i].efi_gpe_EndingLBA == 0) {
8669 8668 continue;
8670 8669 }
8671 8670
8672 8671 vdc->slice[i].start = gpe[i].efi_gpe_StartingLBA;
8673 8672 vdc->slice[i].nblocks = gpe[i].efi_gpe_EndingLBA -
8674 8673 gpe[i].efi_gpe_StartingLBA + 1;
8675 8674 }
8676 8675
8677 8676 ASSERT(vdc->vdisk_size != 0);
8678 8677 vdc->slice[VD_EFI_WD_SLICE].start = 0;
8679 8678 vdc->slice[VD_EFI_WD_SLICE].nblocks = vdc->vdisk_size;
8680 8679
8681 8680 }
8682 8681
8683 8682 static void
8684 8683 vdc_store_label_vtoc(vdc_t *vdc, struct dk_geom *geom, struct extvtoc *vtoc)
8685 8684 {
8686 8685 int i;
8687 8686
8688 8687 ASSERT(MUTEX_HELD(&vdc->lock));
8689 8688 ASSERT(vdc->vdisk_bsize == vtoc->v_sectorsz);
8690 8689
8691 8690 vdc->vdisk_label = VD_DISK_LABEL_VTOC;
8692 8691 bcopy(vtoc, vdc->vtoc, sizeof (struct extvtoc));
8693 8692 bcopy(geom, vdc->geom, sizeof (struct dk_geom));
8694 8693 bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
8695 8694
8696 8695 for (i = 0; i < vtoc->v_nparts; i++) {
8697 8696 vdc->slice[i].start = vtoc->v_part[i].p_start;
8698 8697 vdc->slice[i].nblocks = vtoc->v_part[i].p_size;
8699 8698 }
8700 8699 }
8701 8700
8702 8701 static void
8703 8702 vdc_store_label_unk(vdc_t *vdc)
8704 8703 {
8705 8704 ASSERT(MUTEX_HELD(&vdc->lock));
8706 8705
8707 8706 vdc->vdisk_label = VD_DISK_LABEL_UNK;
8708 8707 bzero(vdc->vtoc, sizeof (struct extvtoc));
8709 8708 bzero(vdc->geom, sizeof (struct dk_geom));
8710 8709 bzero(vdc->slice, sizeof (vd_slice_t) * V_NUMPAR);
8711 8710 }
↓ open down ↓ |
4155 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX