Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/scsi/adapters/scsi_vhci/scsi_vhci.c
+++ new/usr/src/uts/common/io/scsi/adapters/scsi_vhci/scsi_vhci.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 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 */
24 24 /*
25 25 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
26 26 */
27 27
28 28 /*
29 29 * Multiplexed I/O SCSI vHCI implementation
30 30 */
31 31
32 32 #include <sys/conf.h>
33 33 #include <sys/file.h>
34 34 #include <sys/ddi.h>
35 35 #include <sys/sunddi.h>
36 36 #include <sys/scsi/scsi.h>
37 37 #include <sys/scsi/impl/scsi_reset_notify.h>
38 38 #include <sys/scsi/impl/services.h>
39 39 #include <sys/sunmdi.h>
40 40 #include <sys/mdi_impldefs.h>
41 41 #include <sys/scsi/adapters/scsi_vhci.h>
42 42 #include <sys/disp.h>
43 43 #include <sys/byteorder.h>
44 44
45 45 extern uintptr_t scsi_callback_id;
46 46 extern ddi_dma_attr_t scsi_alloc_attr;
47 47
48 48 #ifdef DEBUG
49 49 int vhci_debug = VHCI_DEBUG_DEFAULT_VAL;
50 50 #endif
51 51
52 52 /* retry for the vhci_do_prout command when a not ready is returned */
53 53 int vhci_prout_not_ready_retry = 180;
54 54
55 55 /*
56 56 * These values are defined to support the internal retry of
57 57 * SCSI packets for better sense code handling.
58 58 */
59 59 #define VHCI_CMD_CMPLT 0
60 60 #define VHCI_CMD_RETRY 1
61 61 #define VHCI_CMD_ERROR -1
62 62
63 63 #define PROPFLAGS (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM)
64 64 #define VHCI_SCSI_PERR 0x47
65 65 #define VHCI_PGR_ILLEGALOP -2
66 66 #define VHCI_NUM_UPDATE_TASKQ 8
67 67 /* changed to 132 to accomodate HDS */
68 68
69 69 /*
70 70 * Version Macros
71 71 */
72 72 #define VHCI_NAME_VERSION "SCSI VHCI Driver"
73 73 char vhci_version_name[] = VHCI_NAME_VERSION;
74 74
75 75 int vhci_first_time = 0;
76 76 clock_t vhci_to_ticks = 0;
77 77 int vhci_init_wait_timeout = VHCI_INIT_WAIT_TIMEOUT;
78 78 kcondvar_t vhci_cv;
79 79 kmutex_t vhci_global_mutex;
80 80 void *vhci_softstate = NULL; /* for soft state */
81 81
82 82 /*
83 83 * Flag to delay the retry of the reserve command
84 84 */
85 85 int vhci_reserve_delay = 100000;
86 86 static int vhci_path_quiesce_timeout = 60;
87 87 static uchar_t zero_key[MHIOC_RESV_KEY_SIZE];
88 88
89 89 /* uscsi delay for a TRAN_BUSY */
90 90 static int vhci_uscsi_delay = 100000;
91 91 static int vhci_uscsi_retry_count = 180;
92 92 /* uscsi_restart_sense timeout id in case it needs to get canceled */
93 93 static timeout_id_t vhci_restart_timeid = 0;
94 94
95 95 static int vhci_bus_config_debug = 0;
96 96
97 97 /*
98 98 * Bidirectional map of 'target-port' to port id <pid> for support of
99 99 * iostat(1M) '-Xx' and '-Yx' output.
100 100 */
101 101 static kmutex_t vhci_targetmap_mutex;
102 102 static uint_t vhci_targetmap_pid = 1;
103 103 static mod_hash_t *vhci_targetmap_bypid; /* <pid> -> 'target-port' */
104 104 static mod_hash_t *vhci_targetmap_byport; /* 'target-port' -> <pid> */
105 105
106 106 /*
107 107 * functions exported by scsi_vhci struct cb_ops
108 108 */
109 109 static int vhci_open(dev_t *, int, int, cred_t *);
110 110 static int vhci_close(dev_t, int, int, cred_t *);
111 111 static int vhci_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
112 112
113 113 /*
114 114 * functions exported by scsi_vhci struct dev_ops
115 115 */
116 116 static int vhci_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
117 117 static int vhci_attach(dev_info_t *, ddi_attach_cmd_t);
118 118 static int vhci_detach(dev_info_t *, ddi_detach_cmd_t);
119 119
120 120 /*
121 121 * functions exported by scsi_vhci scsi_hba_tran_t transport table
122 122 */
123 123 static int vhci_scsi_tgt_init(dev_info_t *, dev_info_t *,
124 124 scsi_hba_tran_t *, struct scsi_device *);
125 125 static void vhci_scsi_tgt_free(dev_info_t *, dev_info_t *, scsi_hba_tran_t *,
126 126 struct scsi_device *);
127 127 static int vhci_pgr_register_start(scsi_vhci_lun_t *, struct scsi_pkt *);
128 128 static int vhci_scsi_start(struct scsi_address *, struct scsi_pkt *);
129 129 static int vhci_scsi_abort(struct scsi_address *, struct scsi_pkt *);
130 130 static int vhci_scsi_reset(struct scsi_address *, int);
131 131 static int vhci_scsi_reset_target(struct scsi_address *, int level,
132 132 uint8_t select_path);
133 133 static int vhci_scsi_reset_bus(struct scsi_address *);
134 134 static int vhci_scsi_getcap(struct scsi_address *, char *, int);
135 135 static int vhci_scsi_setcap(struct scsi_address *, char *, int, int);
136 136 static int vhci_commoncap(struct scsi_address *, char *, int, int, int);
137 137 static int vhci_pHCI_cap(struct scsi_address *ap, char *cap, int val, int whom,
138 138 mdi_pathinfo_t *pip);
139 139 static struct scsi_pkt *vhci_scsi_init_pkt(struct scsi_address *,
140 140 struct scsi_pkt *, struct buf *, int, int, int, int, int (*)(), caddr_t);
141 141 static void vhci_scsi_destroy_pkt(struct scsi_address *, struct scsi_pkt *);
142 142 static void vhci_scsi_dmafree(struct scsi_address *, struct scsi_pkt *);
143 143 static void vhci_scsi_sync_pkt(struct scsi_address *, struct scsi_pkt *);
144 144 static int vhci_scsi_reset_notify(struct scsi_address *, int, void (*)(caddr_t),
145 145 caddr_t);
146 146 static int vhci_scsi_get_bus_addr(struct scsi_device *, char *, int);
147 147 static int vhci_scsi_get_name(struct scsi_device *, char *, int);
148 148 static int vhci_scsi_bus_power(dev_info_t *, void *, pm_bus_power_op_t,
149 149 void *, void *);
150 150 static int vhci_scsi_bus_config(dev_info_t *, uint_t, ddi_bus_config_op_t,
151 151 void *, dev_info_t **);
152 152 static int vhci_scsi_bus_unconfig(dev_info_t *, uint_t, ddi_bus_config_op_t,
153 153 void *);
154 154 static struct scsi_failover_ops *vhci_dev_fo(dev_info_t *, struct scsi_device *,
155 155 void **, char **);
156 156
157 157 /*
158 158 * functions registered with the mpxio framework via mdi_vhci_ops_t
159 159 */
160 160 static int vhci_pathinfo_init(dev_info_t *, mdi_pathinfo_t *, int);
161 161 static int vhci_pathinfo_uninit(dev_info_t *, mdi_pathinfo_t *, int);
162 162 static int vhci_pathinfo_state_change(dev_info_t *, mdi_pathinfo_t *,
163 163 mdi_pathinfo_state_t, uint32_t, int);
164 164 static int vhci_pathinfo_online(dev_info_t *, mdi_pathinfo_t *, int);
165 165 static int vhci_pathinfo_offline(dev_info_t *, mdi_pathinfo_t *, int);
166 166 static int vhci_failover(dev_info_t *, dev_info_t *, int);
167 167 static void vhci_client_attached(dev_info_t *);
168 168 static int vhci_is_dev_supported(dev_info_t *, dev_info_t *, void *);
169 169
170 170 static int vhci_ctl(dev_t, int, intptr_t, int, cred_t *, int *);
171 171 static int vhci_devctl(dev_t, int, intptr_t, int, cred_t *, int *);
172 172 static int vhci_ioc_get_phci_path(sv_iocdata_t *, caddr_t, int, caddr_t);
173 173 static int vhci_ioc_get_client_path(sv_iocdata_t *, caddr_t, int, caddr_t);
174 174 static int vhci_ioc_get_paddr(sv_iocdata_t *, caddr_t, int, caddr_t);
175 175 static int vhci_ioc_send_client_path(caddr_t, sv_iocdata_t *, int, caddr_t);
176 176 static void vhci_ioc_devi_to_path(dev_info_t *, caddr_t);
177 177 static int vhci_get_phci_path_list(dev_info_t *, sv_path_info_t *, uint_t);
178 178 static int vhci_get_client_path_list(dev_info_t *, sv_path_info_t *, uint_t);
179 179 static int vhci_get_iocdata(const void *, sv_iocdata_t *, int, caddr_t);
180 180 static int vhci_get_iocswitchdata(const void *, sv_switch_to_cntlr_iocdata_t *,
181 181 int, caddr_t);
182 182 static int vhci_ioc_alloc_pathinfo(sv_path_info_t **, sv_path_info_t **,
183 183 uint_t, sv_iocdata_t *, int, caddr_t);
184 184 static void vhci_ioc_free_pathinfo(sv_path_info_t *, sv_path_info_t *, uint_t);
185 185 static int vhci_ioc_send_pathinfo(sv_path_info_t *, sv_path_info_t *, uint_t,
186 186 sv_iocdata_t *, int, caddr_t);
187 187 static int vhci_handle_ext_fo(struct scsi_pkt *, int);
188 188 static int vhci_efo_watch_cb(caddr_t, struct scsi_watch_result *);
189 189 static int vhci_quiesce_lun(struct scsi_vhci_lun *);
190 190 static int vhci_pgr_validate_and_register(scsi_vhci_priv_t *);
191 191 static void vhci_dispatch_scsi_start(void *);
192 192 static void vhci_efo_done(void *);
193 193 static void vhci_initiate_auto_failback(void *);
194 194 static void vhci_update_pHCI_pkt(struct vhci_pkt *, struct scsi_pkt *);
195 195 static int vhci_update_pathinfo(struct scsi_device *, mdi_pathinfo_t *,
196 196 struct scsi_failover_ops *, scsi_vhci_lun_t *, struct scsi_vhci *);
197 197 static void vhci_kstat_create_pathinfo(mdi_pathinfo_t *);
198 198 static int vhci_quiesce_paths(dev_info_t *, dev_info_t *,
199 199 scsi_vhci_lun_t *, char *, char *);
200 200
201 201 static char *vhci_devnm_to_guid(char *);
202 202 static int vhci_bind_transport(struct scsi_address *, struct vhci_pkt *,
203 203 int, int (*func)(caddr_t));
204 204 static void vhci_intr(struct scsi_pkt *);
205 205 static int vhci_do_prout(scsi_vhci_priv_t *);
206 206 static void vhci_run_cmd(void *);
207 207 static int vhci_do_prin(struct vhci_pkt **);
208 208 static struct scsi_pkt *vhci_create_retry_pkt(struct vhci_pkt *);
209 209 static struct vhci_pkt *vhci_sync_retry_pkt(struct vhci_pkt *);
210 210 static struct scsi_vhci_lun *vhci_lun_lookup(dev_info_t *);
211 211 static struct scsi_vhci_lun *vhci_lun_lookup_alloc(dev_info_t *, char *, int *);
212 212 static void vhci_lun_free(struct scsi_vhci_lun *dvlp, struct scsi_device *sd);
213 213 static int vhci_recovery_reset(scsi_vhci_lun_t *, struct scsi_address *,
214 214 uint8_t, uint8_t);
215 215 void vhci_update_pathstates(void *);
216 216
217 217 #ifdef DEBUG
218 218 static void vhci_print_prin_keys(vhci_prin_readkeys_t *, int);
219 219 static void vhci_print_cdb(dev_info_t *dip, uint_t level,
220 220 char *title, uchar_t *cdb);
221 221 static void vhci_clean_print(dev_info_t *dev, uint_t level,
222 222 char *title, uchar_t *data, int len);
223 223 #endif
224 224 static void vhci_print_prout_keys(scsi_vhci_lun_t *, char *);
225 225 static void vhci_uscsi_iodone(struct scsi_pkt *pkt);
226 226 static void vhci_invalidate_mpapi_lu(struct scsi_vhci *, scsi_vhci_lun_t *);
227 227
228 228 /*
229 229 * MP-API related functions
230 230 */
231 231 extern int vhci_mpapi_init(struct scsi_vhci *);
232 232 extern void vhci_mpapi_add_dev_prod(struct scsi_vhci *, char *);
233 233 extern int vhci_mpapi_ctl(dev_t, int, intptr_t, int, cred_t *, int *);
234 234 extern void vhci_update_mpapi_data(struct scsi_vhci *,
235 235 scsi_vhci_lun_t *, mdi_pathinfo_t *);
236 236 extern void* vhci_get_mpapi_item(struct scsi_vhci *, mpapi_list_header_t *,
237 237 uint8_t, void*);
238 238 extern void vhci_mpapi_set_path_state(dev_info_t *, mdi_pathinfo_t *, int);
239 239 extern int vhci_mpapi_update_tpg_acc_state_for_lu(struct scsi_vhci *,
240 240 scsi_vhci_lun_t *);
241 241
242 242 #define VHCI_DMA_MAX_XFER_CAP INT_MAX
243 243
244 244 #define VHCI_MAX_PGR_RETRIES 3
245 245
246 246 /*
247 247 * Macros for the device-type mpxio options
248 248 */
249 249 #define LOAD_BALANCE_OPTIONS "load-balance-options"
250 250 #define LOGICAL_BLOCK_REGION_SIZE "region-size"
251 251 #define MPXIO_OPTIONS_LIST "device-type-mpxio-options-list"
252 252 #define DEVICE_TYPE_STR "device-type"
253 253 #define isdigit(ch) ((ch) >= '0' && (ch) <= '9')
254 254
255 255 static struct cb_ops vhci_cb_ops = {
256 256 vhci_open, /* open */
257 257 vhci_close, /* close */
258 258 nodev, /* strategy */
259 259 nodev, /* print */
260 260 nodev, /* dump */
261 261 nodev, /* read */
262 262 nodev, /* write */
263 263 vhci_ioctl, /* ioctl */
264 264 nodev, /* devmap */
265 265 nodev, /* mmap */
266 266 nodev, /* segmap */
267 267 nochpoll, /* chpoll */
268 268 ddi_prop_op, /* cb_prop_op */
269 269 0, /* streamtab */
270 270 D_NEW | D_MP, /* cb_flag */
271 271 CB_REV, /* rev */
272 272 nodev, /* aread */
273 273 nodev /* awrite */
274 274 };
275 275
276 276 static struct dev_ops vhci_ops = {
277 277 DEVO_REV,
278 278 0,
279 279 vhci_getinfo,
280 280 nulldev, /* identify */
281 281 nulldev, /* probe */
282 282 vhci_attach, /* attach and detach are mandatory */
283 283 vhci_detach,
284 284 nodev, /* reset */
285 285 &vhci_cb_ops, /* cb_ops */
286 286 NULL, /* bus_ops */
287 287 NULL, /* power */
288 288 ddi_quiesce_not_needed, /* quiesce */
289 289 };
290 290
↓ open down ↓ |
290 lines elided |
↑ open up ↑ |
291 291 extern struct mod_ops mod_driverops;
292 292
293 293 static struct modldrv modldrv = {
294 294 &mod_driverops,
295 295 vhci_version_name, /* module name */
296 296 &vhci_ops
297 297 };
298 298
299 299 static struct modlinkage modlinkage = {
300 300 MODREV_1,
301 - &modldrv,
302 - NULL
301 + { &modldrv, NULL }
303 302 };
304 303
305 304 static mdi_vhci_ops_t vhci_opinfo = {
306 305 MDI_VHCI_OPS_REV,
307 306 vhci_pathinfo_init, /* Pathinfo node init callback */
308 307 vhci_pathinfo_uninit, /* Pathinfo uninit callback */
309 308 vhci_pathinfo_state_change, /* Pathinfo node state change */
310 309 vhci_failover, /* failover callback */
311 310 vhci_client_attached, /* client attached callback */
312 311 vhci_is_dev_supported /* is device supported by mdi */
313 312 };
314 313
315 314 /*
316 315 * The scsi_failover table defines an ordered set of 'fops' modules supported
317 316 * by scsi_vhci. Currently, initialize this table from the 'ddi-forceload'
318 317 * property specified in scsi_vhci.conf.
319 318 */
320 319 static struct scsi_failover {
321 320 ddi_modhandle_t sf_mod;
322 321 struct scsi_failover_ops *sf_sfo;
323 322 } *scsi_failover_table;
324 323 static uint_t scsi_nfailover;
325 324
326 325 int
327 326 _init(void)
328 327 {
329 328 int rval;
330 329
331 330 /*
332 331 * Allocate soft state and prepare to do ddi_soft_state_zalloc()
333 332 * before registering with the transport first.
334 333 */
335 334 if ((rval = ddi_soft_state_init(&vhci_softstate,
336 335 sizeof (struct scsi_vhci), 1)) != 0) {
337 336 VHCI_DEBUG(1, (CE_NOTE, NULL,
338 337 "!_init:soft state init failed\n"));
339 338 return (rval);
340 339 }
341 340
342 341 if ((rval = scsi_hba_init(&modlinkage)) != 0) {
343 342 VHCI_DEBUG(1, (CE_NOTE, NULL,
344 343 "!_init: scsi hba init failed\n"));
345 344 ddi_soft_state_fini(&vhci_softstate);
346 345 return (rval);
347 346 }
348 347
349 348 mutex_init(&vhci_global_mutex, NULL, MUTEX_DRIVER, NULL);
350 349 cv_init(&vhci_cv, NULL, CV_DRIVER, NULL);
351 350
352 351 mutex_init(&vhci_targetmap_mutex, NULL, MUTEX_DRIVER, NULL);
353 352 vhci_targetmap_byport = mod_hash_create_strhash(
354 353 "vhci_targetmap_byport", 256, mod_hash_null_valdtor);
355 354 vhci_targetmap_bypid = mod_hash_create_idhash(
356 355 "vhci_targetmap_bypid", 256, mod_hash_null_valdtor);
357 356
358 357 if ((rval = mod_install(&modlinkage)) != 0) {
359 358 VHCI_DEBUG(1, (CE_NOTE, NULL, "!_init: mod_install failed\n"));
360 359 if (vhci_targetmap_bypid)
361 360 mod_hash_destroy_idhash(vhci_targetmap_bypid);
362 361 if (vhci_targetmap_byport)
363 362 mod_hash_destroy_strhash(vhci_targetmap_byport);
364 363 mutex_destroy(&vhci_targetmap_mutex);
365 364 cv_destroy(&vhci_cv);
366 365 mutex_destroy(&vhci_global_mutex);
367 366 scsi_hba_fini(&modlinkage);
368 367 ddi_soft_state_fini(&vhci_softstate);
369 368 }
370 369 return (rval);
371 370 }
372 371
373 372
374 373 /*
375 374 * the system is done with us as a driver, so clean up
376 375 */
377 376 int
378 377 _fini(void)
379 378 {
380 379 int rval;
381 380
382 381 /*
383 382 * don't start cleaning up until we know that the module remove
384 383 * has worked -- if this works, then we know that each instance
385 384 * has successfully been DDI_DETACHed
386 385 */
387 386 if ((rval = mod_remove(&modlinkage)) != 0) {
388 387 VHCI_DEBUG(4, (CE_NOTE, NULL, "!_fini: mod_remove failed\n"));
389 388 return (rval);
390 389 }
391 390
392 391 if (vhci_targetmap_bypid)
393 392 mod_hash_destroy_idhash(vhci_targetmap_bypid);
394 393 if (vhci_targetmap_byport)
395 394 mod_hash_destroy_strhash(vhci_targetmap_byport);
396 395 mutex_destroy(&vhci_targetmap_mutex);
397 396 cv_destroy(&vhci_cv);
398 397 mutex_destroy(&vhci_global_mutex);
399 398 scsi_hba_fini(&modlinkage);
400 399 ddi_soft_state_fini(&vhci_softstate);
401 400
402 401 return (rval);
403 402 }
404 403
405 404 int
406 405 _info(struct modinfo *modinfop)
407 406 {
408 407 return (mod_info(&modlinkage, modinfop));
409 408 }
410 409
411 410 /*
412 411 * Lookup scsi_failover by "short name" of failover module.
413 412 */
414 413 struct scsi_failover_ops *
415 414 vhci_failover_ops_by_name(char *name)
416 415 {
417 416 struct scsi_failover *sf;
418 417
419 418 for (sf = scsi_failover_table; sf->sf_mod; sf++) {
420 419 if (sf->sf_sfo == NULL)
421 420 continue;
422 421 if (strcmp(sf->sf_sfo->sfo_name, name) == 0)
423 422 return (sf->sf_sfo);
424 423 }
425 424 return (NULL);
426 425 }
427 426
428 427 /*
429 428 * Load all scsi_failover_ops 'fops' modules.
430 429 */
431 430 static void
432 431 vhci_failover_modopen(struct scsi_vhci *vhci)
433 432 {
434 433 char **module;
435 434 int i;
436 435 struct scsi_failover *sf;
437 436 char **dt;
438 437 int e;
439 438
440 439 if (scsi_failover_table)
441 440 return;
442 441
443 442 /* Get the list of modules from scsi_vhci.conf */
444 443 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY,
445 444 vhci->vhci_dip, DDI_PROP_DONTPASS, "ddi-forceload",
446 445 &module, &scsi_nfailover) != DDI_PROP_SUCCESS) {
447 446 cmn_err(CE_WARN, "scsi_vhci: "
448 447 "scsi_vhci.conf is missing 'ddi-forceload'");
449 448 return;
450 449 }
451 450 if (scsi_nfailover == 0) {
452 451 cmn_err(CE_WARN, "scsi_vhci: "
453 452 "scsi_vhci.conf has empty 'ddi-forceload'");
454 453 ddi_prop_free(module);
455 454 return;
456 455 }
457 456
458 457 /* allocate failover table based on number of modules */
459 458 scsi_failover_table = (struct scsi_failover *)
460 459 kmem_zalloc(sizeof (struct scsi_failover) * (scsi_nfailover + 1),
461 460 KM_SLEEP);
462 461
463 462 /* loop over modules specified in scsi_vhci.conf and open each module */
464 463 for (i = 0, sf = scsi_failover_table; i < scsi_nfailover; i++) {
465 464 if (module[i] == NULL)
466 465 continue;
467 466
468 467 sf->sf_mod = ddi_modopen(module[i], KRTLD_MODE_FIRST, &e);
469 468 if (sf->sf_mod == NULL) {
470 469 /*
471 470 * A module returns EEXIST if other software is
472 471 * supporting the intended function: for example
473 472 * the scsi_vhci_f_sum_emc module returns EEXIST
474 473 * from _init if EMC powerpath software is installed.
475 474 */
476 475 if (e != EEXIST)
477 476 cmn_err(CE_WARN, "scsi_vhci: unable to open "
478 477 "module '%s', error %d", module[i], e);
479 478 continue;
480 479 }
481 480 sf->sf_sfo = ddi_modsym(sf->sf_mod,
482 481 "scsi_vhci_failover_ops", &e);
483 482 if (sf->sf_sfo == NULL) {
484 483 cmn_err(CE_WARN, "scsi_vhci: "
485 484 "unable to import 'scsi_failover_ops' from '%s', "
486 485 "error %d", module[i], e);
487 486 (void) ddi_modclose(sf->sf_mod);
488 487 sf->sf_mod = NULL;
489 488 continue;
490 489 }
491 490
492 491 /* register vid/pid of devices supported with mpapi */
493 492 for (dt = sf->sf_sfo->sfo_devices; *dt; dt++)
494 493 vhci_mpapi_add_dev_prod(vhci, *dt);
495 494 sf++;
496 495 }
497 496
498 497 /* verify that at least the "well-known" modules were there */
499 498 if (vhci_failover_ops_by_name(SFO_NAME_SYM) == NULL)
500 499 cmn_err(CE_WARN, "scsi_vhci: well-known module \""
501 500 SFO_NAME_SYM "\" not defined in scsi_vhci.conf's "
502 501 "'ddi-forceload'");
503 502 if (vhci_failover_ops_by_name(SFO_NAME_TPGS) == NULL)
504 503 cmn_err(CE_WARN, "scsi_vhci: well-known module \""
505 504 SFO_NAME_TPGS "\" not defined in scsi_vhci.conf's "
506 505 "'ddi-forceload'");
507 506
508 507 /* call sfo_init for modules that need it */
509 508 for (sf = scsi_failover_table; sf->sf_mod; sf++) {
510 509 if (sf->sf_sfo && sf->sf_sfo->sfo_init)
511 510 sf->sf_sfo->sfo_init();
512 511 }
513 512
514 513 ddi_prop_free(module);
515 514 }
516 515
517 516 /*
518 517 * unload all loaded scsi_failover_ops modules
519 518 */
520 519 static void
521 520 vhci_failover_modclose()
522 521 {
523 522 struct scsi_failover *sf;
524 523
525 524 for (sf = scsi_failover_table; sf->sf_mod; sf++) {
526 525 if ((sf->sf_mod == NULL) || (sf->sf_sfo == NULL))
527 526 continue;
528 527 (void) ddi_modclose(sf->sf_mod);
529 528 sf->sf_mod = NULL;
530 529 sf->sf_sfo = NULL;
531 530 }
532 531
533 532 if (scsi_failover_table && scsi_nfailover)
534 533 kmem_free(scsi_failover_table,
535 534 sizeof (struct scsi_failover) * (scsi_nfailover + 1));
536 535 scsi_failover_table = NULL;
537 536 scsi_nfailover = 0;
538 537 }
539 538
540 539 /* ARGSUSED */
541 540 static int
542 541 vhci_open(dev_t *devp, int flag, int otype, cred_t *credp)
543 542 {
544 543 struct scsi_vhci *vhci;
545 544
546 545 if (otype != OTYP_CHR) {
547 546 return (EINVAL);
548 547 }
549 548
550 549 vhci = ddi_get_soft_state(vhci_softstate, MINOR2INST(getminor(*devp)));
551 550 if (vhci == NULL) {
552 551 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_open: failed ENXIO\n"));
553 552 return (ENXIO);
554 553 }
555 554
556 555 mutex_enter(&vhci->vhci_mutex);
557 556 if ((flag & FEXCL) && (vhci->vhci_state & VHCI_STATE_OPEN)) {
558 557 mutex_exit(&vhci->vhci_mutex);
559 558 vhci_log(CE_NOTE, vhci->vhci_dip,
560 559 "!vhci%d: Already open\n", getminor(*devp));
561 560 return (EBUSY);
562 561 }
563 562
564 563 vhci->vhci_state |= VHCI_STATE_OPEN;
565 564 mutex_exit(&vhci->vhci_mutex);
566 565 return (0);
567 566 }
568 567
569 568
570 569 /* ARGSUSED */
571 570 static int
572 571 vhci_close(dev_t dev, int flag, int otype, cred_t *credp)
573 572 {
574 573 struct scsi_vhci *vhci;
575 574
576 575 if (otype != OTYP_CHR) {
577 576 return (EINVAL);
578 577 }
579 578
580 579 vhci = ddi_get_soft_state(vhci_softstate, MINOR2INST(getminor(dev)));
581 580 if (vhci == NULL) {
582 581 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_close: failed ENXIO\n"));
583 582 return (ENXIO);
584 583 }
585 584
586 585 mutex_enter(&vhci->vhci_mutex);
587 586 vhci->vhci_state &= ~VHCI_STATE_OPEN;
588 587 mutex_exit(&vhci->vhci_mutex);
589 588
590 589 return (0);
591 590 }
592 591
593 592 /* ARGSUSED */
594 593 static int
595 594 vhci_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
596 595 cred_t *credp, int *rval)
597 596 {
598 597 if (IS_DEVCTL(cmd)) {
599 598 return (vhci_devctl(dev, cmd, data, mode, credp, rval));
600 599 } else if (cmd == MP_CMD) {
601 600 return (vhci_mpapi_ctl(dev, cmd, data, mode, credp, rval));
602 601 } else {
603 602 return (vhci_ctl(dev, cmd, data, mode, credp, rval));
604 603 }
605 604 }
606 605
607 606 /*
608 607 * attach the module
609 608 */
610 609 static int
611 610 vhci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
612 611 {
613 612 int rval = DDI_FAILURE;
614 613 int scsi_hba_attached = 0;
615 614 int vhci_attached = 0;
616 615 int mutex_initted = 0;
617 616 int instance;
618 617 struct scsi_vhci *vhci;
619 618 scsi_hba_tran_t *tran;
620 619 char cache_name_buf[64];
621 620 char *data;
622 621
623 622 VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_attach: cmd=0x%x\n", cmd));
624 623
625 624 instance = ddi_get_instance(dip);
626 625
627 626 switch (cmd) {
628 627 case DDI_ATTACH:
629 628 break;
630 629
631 630 case DDI_RESUME:
632 631 case DDI_PM_RESUME:
633 632 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_attach: resume not yet"
634 633 "implemented\n"));
635 634 return (rval);
636 635
637 636 default:
638 637 VHCI_DEBUG(1, (CE_NOTE, NULL,
639 638 "!vhci_attach: unknown ddi command\n"));
640 639 return (rval);
641 640 }
642 641
643 642 /*
644 643 * Allocate vhci data structure.
645 644 */
646 645 if (ddi_soft_state_zalloc(vhci_softstate, instance) != DDI_SUCCESS) {
647 646 VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:"
648 647 "soft state alloc failed\n"));
649 648 return (DDI_FAILURE);
650 649 }
651 650
652 651 if ((vhci = ddi_get_soft_state(vhci_softstate, instance)) == NULL) {
653 652 VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:"
654 653 "bad soft state\n"));
655 654 ddi_soft_state_free(vhci_softstate, instance);
656 655 return (DDI_FAILURE);
657 656 }
658 657
659 658 /* Allocate packet cache */
660 659 (void) snprintf(cache_name_buf, sizeof (cache_name_buf),
661 660 "vhci%d_cache", instance);
662 661
663 662 mutex_init(&vhci->vhci_mutex, NULL, MUTEX_DRIVER, NULL);
664 663 mutex_initted++;
665 664
666 665 /*
667 666 * Allocate a transport structure
668 667 */
669 668 tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
670 669 ASSERT(tran != NULL);
671 670
672 671 vhci->vhci_tran = tran;
673 672 vhci->vhci_dip = dip;
674 673 vhci->vhci_instance = instance;
675 674
676 675 tran->tran_hba_private = vhci;
677 676 tran->tran_tgt_init = vhci_scsi_tgt_init;
678 677 tran->tran_tgt_probe = NULL;
679 678 tran->tran_tgt_free = vhci_scsi_tgt_free;
680 679
681 680 tran->tran_start = vhci_scsi_start;
682 681 tran->tran_abort = vhci_scsi_abort;
683 682 tran->tran_reset = vhci_scsi_reset;
684 683 tran->tran_getcap = vhci_scsi_getcap;
685 684 tran->tran_setcap = vhci_scsi_setcap;
686 685 tran->tran_init_pkt = vhci_scsi_init_pkt;
687 686 tran->tran_destroy_pkt = vhci_scsi_destroy_pkt;
688 687 tran->tran_dmafree = vhci_scsi_dmafree;
689 688 tran->tran_sync_pkt = vhci_scsi_sync_pkt;
690 689 tran->tran_reset_notify = vhci_scsi_reset_notify;
691 690
692 691 tran->tran_get_bus_addr = vhci_scsi_get_bus_addr;
693 692 tran->tran_get_name = vhci_scsi_get_name;
694 693 tran->tran_bus_reset = NULL;
695 694 tran->tran_quiesce = NULL;
696 695 tran->tran_unquiesce = NULL;
697 696
698 697 /*
699 698 * register event notification routines with scsa
700 699 */
701 700 tran->tran_get_eventcookie = NULL;
702 701 tran->tran_add_eventcall = NULL;
703 702 tran->tran_remove_eventcall = NULL;
704 703 tran->tran_post_event = NULL;
705 704
706 705 tran->tran_bus_power = vhci_scsi_bus_power;
707 706
708 707 tran->tran_bus_config = vhci_scsi_bus_config;
709 708 tran->tran_bus_unconfig = vhci_scsi_bus_unconfig;
710 709
711 710 /*
712 711 * Attach this instance with the mpxio framework
713 712 */
714 713 if (mdi_vhci_register(MDI_HCI_CLASS_SCSI, dip, &vhci_opinfo, 0)
715 714 != MDI_SUCCESS) {
716 715 VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:"
717 716 "mdi_vhci_register failed\n"));
718 717 goto attach_fail;
719 718 }
720 719 vhci_attached++;
721 720
722 721 /*
723 722 * Attach this instance of the hba.
724 723 *
725 724 * Regarding dma attributes: Since scsi_vhci is a virtual scsi HBA
726 725 * driver, it has nothing to do with DMA. However, when calling
727 726 * scsi_hba_attach_setup() we need to pass something valid in the
728 727 * dma attributes parameter. So we just use scsi_alloc_attr.
729 728 * SCSA itself seems to care only for dma_attr_minxfer and
730 729 * dma_attr_burstsizes fields of dma attributes structure.
731 730 * It expects those fileds to be non-zero.
732 731 */
733 732 if (scsi_hba_attach_setup(dip, &scsi_alloc_attr, tran,
734 733 SCSI_HBA_ADDR_COMPLEX) != DDI_SUCCESS) {
735 734 VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:"
736 735 "hba attach failed\n"));
737 736 goto attach_fail;
738 737 }
739 738 scsi_hba_attached++;
740 739
741 740 if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
742 741 INST2DEVCTL(instance), DDI_NT_SCSI_NEXUS, 0) != DDI_SUCCESS) {
743 742 VHCI_DEBUG(1, (CE_NOTE, dip, "!vhci_attach:"
744 743 " ddi_create_minor_node failed\n"));
745 744 goto attach_fail;
746 745 }
747 746
748 747 /*
749 748 * Set pm-want-child-notification property for
750 749 * power management of the phci and client
751 750 */
752 751 if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
753 752 "pm-want-child-notification?", NULL, NULL) != DDI_PROP_SUCCESS) {
754 753 cmn_err(CE_WARN,
755 754 "%s%d fail to create pm-want-child-notification? prop",
756 755 ddi_driver_name(dip), ddi_get_instance(dip));
757 756 goto attach_fail;
758 757 }
759 758
760 759 vhci->vhci_taskq = taskq_create("vhci_taskq", 1, MINCLSYSPRI, 1, 4, 0);
761 760 vhci->vhci_update_pathstates_taskq =
762 761 taskq_create("vhci_update_pathstates", VHCI_NUM_UPDATE_TASKQ,
763 762 MINCLSYSPRI, 1, 4, 0);
764 763 ASSERT(vhci->vhci_taskq);
765 764 ASSERT(vhci->vhci_update_pathstates_taskq);
766 765
767 766 /*
768 767 * Set appropriate configuration flags based on options set in
769 768 * conf file.
770 769 */
771 770 vhci->vhci_conf_flags = 0;
772 771 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, PROPFLAGS,
773 772 "auto-failback", &data) == DDI_SUCCESS) {
774 773 if (strcmp(data, "enable") == 0)
775 774 vhci->vhci_conf_flags |= VHCI_CONF_FLAGS_AUTO_FAILBACK;
776 775 ddi_prop_free(data);
777 776 }
778 777
779 778 if (!(vhci->vhci_conf_flags & VHCI_CONF_FLAGS_AUTO_FAILBACK))
780 779 vhci_log(CE_NOTE, dip, "!Auto-failback capability "
781 780 "disabled through scsi_vhci.conf file.");
782 781
783 782 /*
784 783 * Allocate an mpapi private structure
785 784 */
786 785 vhci->mp_priv = kmem_zalloc(sizeof (mpapi_priv_t), KM_SLEEP);
787 786 if (vhci_mpapi_init(vhci) != 0) {
788 787 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_attach: "
789 788 "vhci_mpapi_init() failed"));
790 789 }
791 790
792 791 vhci_failover_modopen(vhci); /* load failover modules */
793 792
794 793 ddi_report_dev(dip);
795 794 return (DDI_SUCCESS);
796 795
797 796 attach_fail:
798 797 if (vhci_attached)
799 798 (void) mdi_vhci_unregister(dip, 0);
800 799
801 800 if (scsi_hba_attached)
802 801 (void) scsi_hba_detach(dip);
803 802
804 803 if (vhci->vhci_tran)
805 804 scsi_hba_tran_free(vhci->vhci_tran);
806 805
807 806 if (mutex_initted) {
808 807 mutex_destroy(&vhci->vhci_mutex);
809 808 }
810 809
811 810 ddi_soft_state_free(vhci_softstate, instance);
812 811 return (DDI_FAILURE);
813 812 }
814 813
815 814
816 815 /*ARGSUSED*/
817 816 static int
818 817 vhci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
819 818 {
820 819 int instance = ddi_get_instance(dip);
821 820 scsi_hba_tran_t *tran;
822 821 struct scsi_vhci *vhci;
823 822
824 823 VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_detach: cmd=0x%x\n", cmd));
825 824
826 825 if ((tran = ddi_get_driver_private(dip)) == NULL)
827 826 return (DDI_FAILURE);
828 827
829 828 vhci = TRAN2HBAPRIVATE(tran);
830 829 if (!vhci) {
831 830 return (DDI_FAILURE);
832 831 }
833 832
834 833 switch (cmd) {
835 834 case DDI_DETACH:
836 835 break;
837 836
838 837 case DDI_SUSPEND:
839 838 case DDI_PM_SUSPEND:
840 839 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_detach: suspend/pm not yet"
841 840 "implemented\n"));
842 841 return (DDI_FAILURE);
843 842
844 843 default:
845 844 VHCI_DEBUG(1, (CE_NOTE, NULL,
846 845 "!vhci_detach: unknown ddi command\n"));
847 846 return (DDI_FAILURE);
848 847 }
849 848
850 849 (void) mdi_vhci_unregister(dip, 0);
851 850 (void) scsi_hba_detach(dip);
852 851 scsi_hba_tran_free(tran);
853 852
854 853 if (ddi_prop_remove(DDI_DEV_T_NONE, dip,
855 854 "pm-want-child-notification?") != DDI_PROP_SUCCESS) {
856 855 cmn_err(CE_WARN,
857 856 "%s%d unable to remove prop pm-want_child_notification?",
858 857 ddi_driver_name(dip), ddi_get_instance(dip));
859 858 }
860 859 if (vhci_restart_timeid != 0) {
861 860 (void) untimeout(vhci_restart_timeid);
862 861 }
863 862 vhci_restart_timeid = 0;
864 863
865 864 mutex_destroy(&vhci->vhci_mutex);
866 865 vhci->vhci_dip = NULL;
867 866 vhci->vhci_tran = NULL;
868 867 taskq_destroy(vhci->vhci_taskq);
869 868 taskq_destroy(vhci->vhci_update_pathstates_taskq);
870 869 ddi_remove_minor_node(dip, NULL);
871 870 ddi_soft_state_free(vhci_softstate, instance);
872 871
873 872 vhci_failover_modclose(); /* unload failover modules */
874 873 return (DDI_SUCCESS);
875 874 }
876 875
877 876 /*
878 877 * vhci_getinfo()
879 878 * Given the device number, return the devinfo pointer or the
880 879 * instance number.
881 880 * Note: always succeed DDI_INFO_DEVT2INSTANCE, even before attach.
882 881 */
883 882
884 883 /*ARGSUSED*/
885 884 static int
886 885 vhci_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
887 886 {
888 887 struct scsi_vhci *vhcip;
889 888 int instance = MINOR2INST(getminor((dev_t)arg));
890 889
891 890 switch (cmd) {
892 891 case DDI_INFO_DEVT2DEVINFO:
893 892 vhcip = ddi_get_soft_state(vhci_softstate, instance);
894 893 if (vhcip != NULL)
895 894 *result = vhcip->vhci_dip;
896 895 else {
897 896 *result = NULL;
898 897 return (DDI_FAILURE);
899 898 }
900 899 break;
901 900
902 901 case DDI_INFO_DEVT2INSTANCE:
903 902 *result = (void *)(uintptr_t)instance;
904 903 break;
905 904
906 905 default:
907 906 return (DDI_FAILURE);
908 907 }
909 908
910 909 return (DDI_SUCCESS);
911 910 }
912 911
913 912 /*ARGSUSED*/
914 913 static int
915 914 vhci_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
916 915 scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
917 916 {
918 917 char *guid;
919 918 scsi_vhci_lun_t *vlun;
920 919 struct scsi_vhci *vhci;
921 920 clock_t from_ticks;
922 921 mdi_pathinfo_t *pip;
923 922 int rval;
924 923
925 924 ASSERT(hba_dip != NULL);
926 925 ASSERT(tgt_dip != NULL);
927 926
928 927 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip, PROPFLAGS,
929 928 MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS) {
930 929 /*
931 930 * This must be the .conf node without GUID property.
932 931 * The node under fp already inserts a delay, so we
933 932 * just return from here. We rely on this delay to have
934 933 * all dips be posted to the ndi hotplug thread's newdev
935 934 * list. This is necessary for the deferred attach
936 935 * mechanism to work and opens() done soon after boot to
937 936 * succeed.
938 937 */
939 938 VHCI_DEBUG(4, (CE_WARN, hba_dip, "tgt_init: lun guid "
940 939 "property failed"));
941 940 return (DDI_NOT_WELL_FORMED);
942 941 }
943 942
944 943 if (ndi_dev_is_persistent_node(tgt_dip) == 0) {
945 944 /*
946 945 * This must be .conf node with the GUID property. We don't
947 946 * merge property by ndi_merge_node() here because the
948 947 * devi_addr_buf of .conf node is "" always according the
949 948 * implementation of vhci_scsi_get_name_bus_addr().
950 949 */
951 950 ddi_set_name_addr(tgt_dip, NULL);
952 951 return (DDI_FAILURE);
953 952 }
954 953
955 954 vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(hba_dip));
956 955 ASSERT(vhci != NULL);
957 956
958 957 VHCI_DEBUG(4, (CE_NOTE, hba_dip,
959 958 "!tgt_init: called for %s (instance %d)\n",
960 959 ddi_driver_name(tgt_dip), ddi_get_instance(tgt_dip)));
961 960
962 961 vlun = vhci_lun_lookup(tgt_dip);
963 962
964 963 mutex_enter(&vhci_global_mutex);
965 964
966 965 from_ticks = ddi_get_lbolt();
967 966 if (vhci_to_ticks == 0) {
968 967 vhci_to_ticks = from_ticks +
969 968 drv_usectohz(vhci_init_wait_timeout);
970 969 }
971 970
972 971 #if DEBUG
973 972 if (vlun) {
974 973 VHCI_DEBUG(1, (CE_WARN, hba_dip, "tgt_init: "
975 974 "vhci_scsi_tgt_init: guid %s : found vlun 0x%p "
976 975 "from_ticks %lx to_ticks %lx",
977 976 guid, (void *)vlun, from_ticks, vhci_to_ticks));
978 977 } else {
979 978 VHCI_DEBUG(1, (CE_WARN, hba_dip, "tgt_init: "
980 979 "vhci_scsi_tgt_init: guid %s : vlun not found "
981 980 "from_ticks %lx to_ticks %lx", guid, from_ticks,
982 981 vhci_to_ticks));
983 982 }
984 983 #endif
985 984
986 985 rval = mdi_select_path(tgt_dip, NULL,
987 986 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH), NULL, &pip);
988 987 if (rval == MDI_SUCCESS) {
989 988 mdi_rele_path(pip);
990 989 }
991 990
992 991 /*
993 992 * Wait for the following conditions :
994 993 * 1. no vlun available yet
995 994 * 2. no path established
996 995 * 3. timer did not expire
997 996 */
998 997 while ((vlun == NULL) || (mdi_client_get_path_count(tgt_dip) == 0) ||
999 998 (rval != MDI_SUCCESS)) {
1000 999 if (vlun && vlun->svl_not_supported) {
1001 1000 VHCI_DEBUG(1, (CE_WARN, hba_dip, "tgt_init: "
1002 1001 "vlun 0x%p lun guid %s not supported!",
1003 1002 (void *)vlun, guid));
1004 1003 mutex_exit(&vhci_global_mutex);
1005 1004 ddi_prop_free(guid);
1006 1005 return (DDI_NOT_WELL_FORMED);
1007 1006 }
1008 1007 if ((vhci_first_time == 0) && (from_ticks >= vhci_to_ticks)) {
1009 1008 vhci_first_time = 1;
1010 1009 }
1011 1010 if (vhci_first_time == 1) {
1012 1011 VHCI_DEBUG(1, (CE_WARN, hba_dip, "vhci_scsi_tgt_init: "
1013 1012 "no wait for %s. from_tick %lx, to_tick %lx",
1014 1013 guid, from_ticks, vhci_to_ticks));
1015 1014 mutex_exit(&vhci_global_mutex);
1016 1015 ddi_prop_free(guid);
1017 1016 return (DDI_NOT_WELL_FORMED);
1018 1017 }
1019 1018
1020 1019 if (cv_timedwait(&vhci_cv,
1021 1020 &vhci_global_mutex, vhci_to_ticks) == -1) {
1022 1021 /* Timed out */
1023 1022 #ifdef DEBUG
1024 1023 if (vlun == NULL) {
1025 1024 VHCI_DEBUG(1, (CE_WARN, hba_dip,
1026 1025 "tgt_init: no vlun for %s!", guid));
1027 1026 } else if (mdi_client_get_path_count(tgt_dip) == 0) {
1028 1027 VHCI_DEBUG(1, (CE_WARN, hba_dip,
1029 1028 "tgt_init: client path count is "
1030 1029 "zero for %s!", guid));
1031 1030 } else {
1032 1031 VHCI_DEBUG(1, (CE_WARN, hba_dip,
1033 1032 "tgt_init: client path not "
1034 1033 "available yet for %s!", guid));
1035 1034 }
1036 1035 #endif /* DEBUG */
1037 1036 mutex_exit(&vhci_global_mutex);
1038 1037 ddi_prop_free(guid);
1039 1038 return (DDI_NOT_WELL_FORMED);
1040 1039 }
1041 1040 vlun = vhci_lun_lookup(tgt_dip);
1042 1041 rval = mdi_select_path(tgt_dip, NULL,
1043 1042 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH),
1044 1043 NULL, &pip);
1045 1044 if (rval == MDI_SUCCESS) {
1046 1045 mdi_rele_path(pip);
1047 1046 }
1048 1047 from_ticks = ddi_get_lbolt();
1049 1048 }
1050 1049 mutex_exit(&vhci_global_mutex);
1051 1050
1052 1051 ASSERT(vlun != NULL);
1053 1052 ddi_prop_free(guid);
1054 1053
1055 1054 scsi_device_hba_private_set(sd, vlun);
1056 1055
1057 1056 return (DDI_SUCCESS);
1058 1057 }
1059 1058
1060 1059 /*ARGSUSED*/
1061 1060 static void
1062 1061 vhci_scsi_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
1063 1062 scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
1064 1063 {
1065 1064 struct scsi_vhci_lun *dvlp;
1066 1065 ASSERT(mdi_client_get_path_count(tgt_dip) <= 0);
1067 1066 dvlp = (struct scsi_vhci_lun *)scsi_device_hba_private_get(sd);
1068 1067 ASSERT(dvlp != NULL);
1069 1068
1070 1069 vhci_lun_free(dvlp, sd);
1071 1070 }
1072 1071
1073 1072 /*
1074 1073 * a PGR register command has started; copy the info we need
1075 1074 */
1076 1075 int
1077 1076 vhci_pgr_register_start(scsi_vhci_lun_t *vlun, struct scsi_pkt *pkt)
1078 1077 {
1079 1078 struct vhci_pkt *vpkt = TGTPKT2VHCIPKT(pkt);
1080 1079 void *addr;
1081 1080
1082 1081 if (!vpkt->vpkt_tgt_init_bp)
1083 1082 return (TRAN_BADPKT);
1084 1083
1085 1084 addr = bp_mapin_common(vpkt->vpkt_tgt_init_bp,
1086 1085 (vpkt->vpkt_flags & CFLAG_NOWAIT) ? VM_NOSLEEP : VM_SLEEP);
1087 1086 if (addr == NULL)
1088 1087 return (TRAN_BUSY);
1089 1088
1090 1089 mutex_enter(&vlun->svl_mutex);
1091 1090
1092 1091 vhci_print_prout_keys(vlun, "v_pgr_reg_start: before bcopy:");
1093 1092
1094 1093 bcopy(addr, &vlun->svl_prout, sizeof (vhci_prout_t) -
1095 1094 (2 * MHIOC_RESV_KEY_SIZE*sizeof (char)));
1096 1095 bcopy(pkt->pkt_cdbp, vlun->svl_cdb, sizeof (vlun->svl_cdb));
1097 1096
1098 1097 vhci_print_prout_keys(vlun, "v_pgr_reg_start: after bcopy:");
1099 1098
1100 1099 vlun->svl_time = pkt->pkt_time;
1101 1100 vlun->svl_bcount = vpkt->vpkt_tgt_init_bp->b_bcount;
1102 1101 vlun->svl_first_path = vpkt->vpkt_path;
1103 1102 mutex_exit(&vlun->svl_mutex);
1104 1103 return (0);
1105 1104 }
1106 1105
1107 1106 /*
1108 1107 * Function name : vhci_scsi_start()
1109 1108 *
1110 1109 * Return Values : TRAN_FATAL_ERROR - vhci has been shutdown
1111 1110 * or other fatal failure
1112 1111 * preventing packet transportation
1113 1112 * TRAN_BUSY - request queue is full
1114 1113 * TRAN_ACCEPT - pkt has been submitted to phci
1115 1114 * (or is held in the waitQ)
1116 1115 * Description : Implements SCSA's tran_start() entry point for
1117 1116 * packet transport
1118 1117 *
1119 1118 */
1120 1119 static int
1121 1120 vhci_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
1122 1121 {
1123 1122 int rval = TRAN_ACCEPT;
1124 1123 int instance, held;
1125 1124 struct scsi_vhci *vhci = ADDR2VHCI(ap);
1126 1125 struct scsi_vhci_lun *vlun = ADDR2VLUN(ap);
1127 1126 struct vhci_pkt *vpkt = TGTPKT2VHCIPKT(pkt);
1128 1127 int flags = 0;
1129 1128 scsi_vhci_priv_t *svp, *svp_resrv;
1130 1129 dev_info_t *cdip;
1131 1130 client_lb_t lbp;
1132 1131 int restore_lbp = 0;
1133 1132 /* set if pkt is SCSI-II RESERVE cmd */
1134 1133 int pkt_reserve_cmd = 0;
1135 1134 int reserve_failed = 0;
1136 1135 int resrv_instance = 0;
1137 1136 mdi_pathinfo_t *pip;
1138 1137 struct scsi_pkt *rel_pkt;
1139 1138
1140 1139 ASSERT(vhci != NULL);
1141 1140 ASSERT(vpkt != NULL);
1142 1141 ASSERT(vpkt->vpkt_state != VHCI_PKT_ISSUED);
1143 1142 cdip = ADDR2DIP(ap);
1144 1143
1145 1144 /*
1146 1145 * Block IOs if LUN is held or QUIESCED for IOs.
1147 1146 */
1148 1147 if ((VHCI_LUN_IS_HELD(vlun)) ||
1149 1148 ((vlun->svl_flags & VLUN_QUIESCED_FLG) == VLUN_QUIESCED_FLG)) {
1150 1149 return (TRAN_BUSY);
1151 1150 }
1152 1151
1153 1152 /*
1154 1153 * vhci_lun needs to be quiesced before SCSI-II RESERVE command
1155 1154 * can be issued. This may require a cv_timedwait, which is
1156 1155 * dangerous to perform in an interrupt context. So if this
1157 1156 * is a RESERVE command a taskq is dispatched to service it.
1158 1157 * This taskq shall again call vhci_scsi_start, but we shall be
1159 1158 * sure its not in an interrupt context.
1160 1159 */
1161 1160 if ((pkt->pkt_cdbp[0] == SCMD_RESERVE) ||
1162 1161 (pkt->pkt_cdbp[0] == SCMD_RESERVE_G1)) {
1163 1162 if (!(vpkt->vpkt_state & VHCI_PKT_THRU_TASKQ)) {
1164 1163 if (taskq_dispatch(vhci->vhci_taskq,
1165 1164 vhci_dispatch_scsi_start, (void *) vpkt,
1166 1165 KM_NOSLEEP)) {
1167 1166 return (TRAN_ACCEPT);
1168 1167 } else {
1169 1168 return (TRAN_BUSY);
1170 1169 }
1171 1170 }
1172 1171
1173 1172 /*
1174 1173 * Here we ensure that simultaneous SCSI-II RESERVE cmds don't
1175 1174 * get serviced for a lun.
1176 1175 */
1177 1176 VHCI_HOLD_LUN(vlun, VH_NOSLEEP, held);
1178 1177 if (!held) {
1179 1178 return (TRAN_BUSY);
1180 1179 } else if ((vlun->svl_flags & VLUN_QUIESCED_FLG) ==
1181 1180 VLUN_QUIESCED_FLG) {
1182 1181 VHCI_RELEASE_LUN(vlun);
1183 1182 return (TRAN_BUSY);
1184 1183 }
1185 1184
1186 1185 /*
1187 1186 * To ensure that no IOs occur for this LUN for the duration
1188 1187 * of this pkt set the VLUN_QUIESCED_FLG.
1189 1188 * In case this routine needs to exit on error make sure that
1190 1189 * this flag is cleared.
1191 1190 */
1192 1191 vlun->svl_flags |= VLUN_QUIESCED_FLG;
1193 1192 pkt_reserve_cmd = 1;
1194 1193
1195 1194 /*
1196 1195 * if this is a SCSI-II RESERVE command, set load balancing
1197 1196 * policy to be ALTERNATE PATH to ensure that all subsequent
1198 1197 * IOs are routed on the same path. This is because if commands
1199 1198 * are routed across multiple paths then IOs on paths other than
1200 1199 * the one on which the RESERVE was executed will get a
1201 1200 * RESERVATION CONFLICT
1202 1201 */
1203 1202 lbp = mdi_get_lb_policy(cdip);
1204 1203 if (lbp != LOAD_BALANCE_NONE) {
1205 1204 if (vhci_quiesce_lun(vlun) != 1) {
1206 1205 vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
1207 1206 VHCI_RELEASE_LUN(vlun);
1208 1207 return (TRAN_FATAL_ERROR);
1209 1208 }
1210 1209 vlun->svl_lb_policy_save = lbp;
1211 1210 if (mdi_set_lb_policy(cdip, LOAD_BALANCE_NONE) !=
1212 1211 MDI_SUCCESS) {
1213 1212 vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
1214 1213 VHCI_RELEASE_LUN(vlun);
1215 1214 return (TRAN_FATAL_ERROR);
1216 1215 }
1217 1216 restore_lbp = 1;
1218 1217 }
1219 1218
1220 1219 VHCI_DEBUG(2, (CE_NOTE, vhci->vhci_dip,
1221 1220 "!vhci_scsi_start: sending SCSI-2 RESERVE, vlun 0x%p, "
1222 1221 "svl_resrv_pip 0x%p, svl_flags: %x, lb_policy %x",
1223 1222 (void *)vlun, (void *)vlun->svl_resrv_pip, vlun->svl_flags,
1224 1223 mdi_get_lb_policy(cdip)));
1225 1224
1226 1225 /*
1227 1226 * See comments for VLUN_RESERVE_ACTIVE_FLG in scsi_vhci.h
1228 1227 * To narrow this window where a reserve command may be sent
1229 1228 * down an inactive path the path states first need to be
1230 1229 * updated. Before calling vhci_update_pathstates reset
1231 1230 * VLUN_RESERVE_ACTIVE_FLG, just in case it was already set
1232 1231 * for this lun. This shall prevent an unnecessary reset
1233 1232 * from being sent out. Also remember currently reserved path
1234 1233 * just for a case the new reservation will go to another path.
1235 1234 */
1236 1235 if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) {
1237 1236 resrv_instance = mdi_pi_get_path_instance(
1238 1237 vlun->svl_resrv_pip);
1239 1238 }
1240 1239 vlun->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG;
1241 1240 vhci_update_pathstates((void *)vlun);
1242 1241 }
1243 1242
1244 1243 instance = ddi_get_instance(vhci->vhci_dip);
1245 1244
1246 1245 /*
1247 1246 * If the command is PRIN with action of zero, then the cmd
1248 1247 * is reading PR keys which requires filtering on completion.
1249 1248 * Data cache sync must be guaranteed.
1250 1249 */
1251 1250 if ((pkt->pkt_cdbp[0] == SCMD_PRIN) && (pkt->pkt_cdbp[1] == 0) &&
1252 1251 (vpkt->vpkt_org_vpkt == NULL)) {
1253 1252 vpkt->vpkt_tgt_init_pkt_flags |= PKT_CONSISTENT;
1254 1253 }
1255 1254
1256 1255 /*
1257 1256 * Do not defer bind for PKT_DMA_PARTIAL
1258 1257 */
1259 1258 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) {
1260 1259
1261 1260 /* This is a non pkt_dma_partial case */
1262 1261 if ((rval = vhci_bind_transport(
1263 1262 ap, vpkt, vpkt->vpkt_tgt_init_pkt_flags, NULL_FUNC))
1264 1263 != TRAN_ACCEPT) {
1265 1264 VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip,
1266 1265 "!vhci%d %x: failed to bind transport: "
1267 1266 "vlun 0x%p pkt_reserved %x restore_lbp %x,"
1268 1267 "lbp %x", instance, rval, (void *)vlun,
1269 1268 pkt_reserve_cmd, restore_lbp, lbp));
1270 1269 if (restore_lbp)
1271 1270 (void) mdi_set_lb_policy(cdip, lbp);
1272 1271 if (pkt_reserve_cmd)
1273 1272 vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
1274 1273 return (rval);
1275 1274 }
1276 1275 VHCI_DEBUG(8, (CE_NOTE, NULL,
1277 1276 "vhci_scsi_start: v_b_t called 0x%p\n", (void *)vpkt));
1278 1277 }
1279 1278 ASSERT(vpkt->vpkt_hba_pkt != NULL);
1280 1279 ASSERT(vpkt->vpkt_path != NULL);
1281 1280
1282 1281 /*
1283 1282 * This is the chance to adjust the pHCI's pkt and other information
1284 1283 * from target driver's pkt.
1285 1284 */
1286 1285 VHCI_DEBUG(8, (CE_NOTE, vhci->vhci_dip, "vhci_scsi_start vpkt %p\n",
1287 1286 (void *)vpkt));
1288 1287 vhci_update_pHCI_pkt(vpkt, pkt);
1289 1288
1290 1289 if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) {
1291 1290 if (vpkt->vpkt_path != vlun->svl_resrv_pip) {
1292 1291 VHCI_DEBUG(1, (CE_WARN, vhci->vhci_dip,
1293 1292 "!vhci_bind: reserve flag set for vlun 0x%p, but, "
1294 1293 "pktpath 0x%p resrv path 0x%p differ. lb_policy %x",
1295 1294 (void *)vlun, (void *)vpkt->vpkt_path,
1296 1295 (void *)vlun->svl_resrv_pip,
1297 1296 mdi_get_lb_policy(cdip)));
1298 1297 reserve_failed = 1;
1299 1298 }
1300 1299 }
1301 1300
1302 1301 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(vpkt->vpkt_path);
1303 1302 if (svp == NULL || reserve_failed) {
1304 1303 if (pkt_reserve_cmd) {
1305 1304 VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip,
1306 1305 "!vhci_bind returned null svp vlun 0x%p",
1307 1306 (void *)vlun));
1308 1307 vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
1309 1308 if (restore_lbp)
1310 1309 (void) mdi_set_lb_policy(cdip, lbp);
1311 1310 }
1312 1311 pkt_cleanup:
1313 1312 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) {
1314 1313 scsi_destroy_pkt(vpkt->vpkt_hba_pkt);
1315 1314 vpkt->vpkt_hba_pkt = NULL;
1316 1315 if (vpkt->vpkt_path) {
1317 1316 mdi_rele_path(vpkt->vpkt_path);
1318 1317 vpkt->vpkt_path = NULL;
1319 1318 }
1320 1319 }
1321 1320 if ((pkt->pkt_cdbp[0] == SCMD_PROUT) &&
1322 1321 (((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) ||
1323 1322 ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_R_AND_IGNORE))) {
1324 1323 sema_v(&vlun->svl_pgr_sema);
1325 1324 }
1326 1325 return (TRAN_BUSY);
1327 1326 }
1328 1327
1329 1328 if ((resrv_instance != 0) && (resrv_instance !=
1330 1329 mdi_pi_get_path_instance(vpkt->vpkt_path))) {
1331 1330 /*
1332 1331 * This is an attempt to reserve vpkt->vpkt_path. But the
1333 1332 * previously reserved path referred by resrv_instance might
1334 1333 * still be reserved. Hence we will send a release command
1335 1334 * there in order to avoid a reservation conflict.
1336 1335 */
1337 1336 VHCI_DEBUG(1, (CE_NOTE, vhci->vhci_dip, "!vhci_scsi_start: "
1338 1337 "conflicting reservation on another path, vlun 0x%p, "
1339 1338 "reserved instance %d, new instance: %d, pip: 0x%p",
1340 1339 (void *)vlun, resrv_instance,
1341 1340 mdi_pi_get_path_instance(vpkt->vpkt_path),
1342 1341 (void *)vpkt->vpkt_path));
1343 1342
1344 1343 /*
1345 1344 * In rare cases, the path referred by resrv_instance could
1346 1345 * disappear in the meantime. Calling mdi_select_path() below
1347 1346 * is an attempt to find out if the path still exists. It also
1348 1347 * ensures that the path will be held when the release is sent.
1349 1348 */
1350 1349 rval = mdi_select_path(cdip, NULL, MDI_SELECT_PATH_INSTANCE,
1351 1350 (void *)(intptr_t)resrv_instance, &pip);
1352 1351
1353 1352 if ((rval == MDI_SUCCESS) && (pip != NULL)) {
1354 1353 svp_resrv = (scsi_vhci_priv_t *)
1355 1354 mdi_pi_get_vhci_private(pip);
1356 1355 rel_pkt = scsi_init_pkt(&svp_resrv->svp_psd->sd_address,
1357 1356 NULL, NULL, CDB_GROUP0,
1358 1357 sizeof (struct scsi_arq_status), 0, 0, SLEEP_FUNC,
1359 1358 NULL);
1360 1359
1361 1360 if (rel_pkt == NULL) {
1362 1361 char *p_path;
1363 1362
1364 1363 /*
1365 1364 * This is very unlikely.
1366 1365 * scsi_init_pkt(SLEEP_FUNC) does not fail
1367 1366 * because of resources. But in theory it could
1368 1367 * fail for some other reason. There is not an
1369 1368 * easy way how to recover though. Log a warning
1370 1369 * and return.
1371 1370 */
1372 1371 p_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1373 1372 vhci_log(CE_WARN, vhci->vhci_dip, "!Sending "
1374 1373 "RELEASE(6) to %s failed, a potential "
1375 1374 "reservation conflict ahead.",
1376 1375 ddi_pathname(mdi_pi_get_phci(pip), p_path));
1377 1376 kmem_free(p_path, MAXPATHLEN);
1378 1377
1379 1378 if (restore_lbp)
1380 1379 (void) mdi_set_lb_policy(cdip, lbp);
1381 1380
1382 1381 /* no need to check pkt_reserve_cmd here */
1383 1382 vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
1384 1383 return (TRAN_FATAL_ERROR);
1385 1384 }
1386 1385
1387 1386 rel_pkt->pkt_cdbp[0] = SCMD_RELEASE;
1388 1387 rel_pkt->pkt_time = 60;
1389 1388
1390 1389 /*
1391 1390 * Ignore the return value. If it will fail
1392 1391 * then most likely it is no longer reserved
1393 1392 * anyway.
1394 1393 */
1395 1394 (void) vhci_do_scsi_cmd(rel_pkt);
1396 1395 VHCI_DEBUG(1, (CE_NOTE, NULL,
1397 1396 "!vhci_scsi_start: path 0x%p, issued SCSI-2"
1398 1397 " RELEASE\n", (void *)pip));
1399 1398 scsi_destroy_pkt(rel_pkt);
1400 1399 mdi_rele_path(pip);
1401 1400 }
1402 1401 }
1403 1402
1404 1403 VHCI_INCR_PATH_CMDCOUNT(svp);
1405 1404
1406 1405 /*
1407 1406 * Ensure that no other IOs raced ahead, while a RESERVE cmd was
1408 1407 * QUIESCING the same lun.
1409 1408 */
1410 1409 if ((!pkt_reserve_cmd) &&
1411 1410 ((vlun->svl_flags & VLUN_QUIESCED_FLG) == VLUN_QUIESCED_FLG)) {
1412 1411 VHCI_DECR_PATH_CMDCOUNT(svp);
1413 1412 goto pkt_cleanup;
1414 1413 }
1415 1414
1416 1415 if ((pkt->pkt_cdbp[0] == SCMD_PRIN) ||
1417 1416 (pkt->pkt_cdbp[0] == SCMD_PROUT)) {
1418 1417 /*
1419 1418 * currently this thread only handles running PGR
1420 1419 * commands, so don't bother creating it unless
1421 1420 * something interesting is going to happen (like
1422 1421 * either a PGR out, or a PGR in with enough space
1423 1422 * to hold the keys that are getting returned)
1424 1423 */
1425 1424 mutex_enter(&vlun->svl_mutex);
1426 1425 if (((vlun->svl_flags & VLUN_TASK_D_ALIVE_FLG) == 0) &&
1427 1426 (pkt->pkt_cdbp[0] == SCMD_PROUT)) {
1428 1427 vlun->svl_taskq = taskq_create("vlun_pgr_task_daemon",
1429 1428 1, MINCLSYSPRI, 1, 4, 0);
1430 1429 vlun->svl_flags |= VLUN_TASK_D_ALIVE_FLG;
1431 1430 }
1432 1431 mutex_exit(&vlun->svl_mutex);
1433 1432 if ((pkt->pkt_cdbp[0] == SCMD_PROUT) &&
1434 1433 (((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) ||
1435 1434 ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_R_AND_IGNORE))) {
1436 1435 if (rval = vhci_pgr_register_start(vlun, pkt)) {
1437 1436 /* an error */
1438 1437 sema_v(&vlun->svl_pgr_sema);
1439 1438 return (rval);
1440 1439 }
1441 1440 }
1442 1441 }
1443 1442
1444 1443 /*
1445 1444 * SCSI-II RESERVE cmd is not expected in polled mode.
1446 1445 * If this changes it needs to be handled for the polled scenario.
1447 1446 */
1448 1447 flags = vpkt->vpkt_hba_pkt->pkt_flags;
1449 1448
1450 1449 /*
1451 1450 * Set the path_instance *before* sending the scsi_pkt down the path
1452 1451 * to mpxio's pHCI so that additional path abstractions at a pHCI
1453 1452 * level (like maybe iSCSI at some point in the future) can update
1454 1453 * the path_instance.
1455 1454 */
1456 1455 if (scsi_pkt_allocated_correctly(vpkt->vpkt_hba_pkt))
1457 1456 vpkt->vpkt_hba_pkt->pkt_path_instance =
1458 1457 mdi_pi_get_path_instance(vpkt->vpkt_path);
1459 1458
1460 1459 rval = scsi_transport(vpkt->vpkt_hba_pkt);
1461 1460 if (rval == TRAN_ACCEPT) {
1462 1461 if (flags & FLAG_NOINTR) {
1463 1462 struct scsi_pkt *tpkt = vpkt->vpkt_tgt_pkt;
1464 1463 struct scsi_pkt *pkt = vpkt->vpkt_hba_pkt;
1465 1464
1466 1465 ASSERT(tpkt != NULL);
1467 1466 *(tpkt->pkt_scbp) = *(pkt->pkt_scbp);
1468 1467 tpkt->pkt_resid = pkt->pkt_resid;
1469 1468 tpkt->pkt_state = pkt->pkt_state;
1470 1469 tpkt->pkt_statistics = pkt->pkt_statistics;
1471 1470 tpkt->pkt_reason = pkt->pkt_reason;
1472 1471
1473 1472 if ((*(pkt->pkt_scbp) == STATUS_CHECK) &&
1474 1473 (pkt->pkt_state & STATE_ARQ_DONE)) {
1475 1474 bcopy(pkt->pkt_scbp, tpkt->pkt_scbp,
1476 1475 vpkt->vpkt_tgt_init_scblen);
1477 1476 }
1478 1477
1479 1478 VHCI_DECR_PATH_CMDCOUNT(svp);
1480 1479 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) {
1481 1480 scsi_destroy_pkt(vpkt->vpkt_hba_pkt);
1482 1481 vpkt->vpkt_hba_pkt = NULL;
1483 1482 if (vpkt->vpkt_path) {
1484 1483 mdi_rele_path(vpkt->vpkt_path);
1485 1484 vpkt->vpkt_path = NULL;
1486 1485 }
1487 1486 }
1488 1487 /*
1489 1488 * This path will not automatically retry pkts
1490 1489 * internally, therefore, vpkt_org_vpkt should
1491 1490 * never be set.
1492 1491 */
1493 1492 ASSERT(vpkt->vpkt_org_vpkt == NULL);
1494 1493 scsi_hba_pkt_comp(tpkt);
1495 1494 }
1496 1495 return (rval);
1497 1496 } else if ((pkt->pkt_cdbp[0] == SCMD_PROUT) &&
1498 1497 (((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) ||
1499 1498 ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_R_AND_IGNORE))) {
1500 1499 /* the command exited with bad status */
1501 1500 sema_v(&vlun->svl_pgr_sema);
1502 1501 } else if (vpkt->vpkt_tgt_pkt->pkt_cdbp[0] == SCMD_PRIN) {
1503 1502 /* the command exited with bad status */
1504 1503 sema_v(&vlun->svl_pgr_sema);
1505 1504 } else if (pkt_reserve_cmd) {
1506 1505 VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip,
1507 1506 "!vhci_scsi_start: reserve failed vlun 0x%p",
1508 1507 (void *)vlun));
1509 1508 vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
1510 1509 if (restore_lbp)
1511 1510 (void) mdi_set_lb_policy(cdip, lbp);
1512 1511 }
1513 1512
1514 1513 ASSERT(vpkt->vpkt_hba_pkt != NULL);
1515 1514 VHCI_DECR_PATH_CMDCOUNT(svp);
1516 1515
1517 1516 /* Do not destroy phci packet information for PKT_DMA_PARTIAL */
1518 1517 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) {
1519 1518 scsi_destroy_pkt(vpkt->vpkt_hba_pkt);
1520 1519 vpkt->vpkt_hba_pkt = NULL;
1521 1520 if (vpkt->vpkt_path) {
1522 1521 MDI_PI_ERRSTAT(vpkt->vpkt_path, MDI_PI_TRANSERR);
1523 1522 mdi_rele_path(vpkt->vpkt_path);
1524 1523 vpkt->vpkt_path = NULL;
1525 1524 }
1526 1525 }
1527 1526 return (TRAN_BUSY);
1528 1527 }
1529 1528
1530 1529 /*
1531 1530 * Function name : vhci_scsi_reset()
1532 1531 *
1533 1532 * Return Values : 0 - reset failed
1534 1533 * 1 - reset succeeded
1535 1534 */
1536 1535
1537 1536 /* ARGSUSED */
1538 1537 static int
1539 1538 vhci_scsi_reset(struct scsi_address *ap, int level)
1540 1539 {
1541 1540 int rval = 0;
1542 1541
1543 1542 cmn_err(CE_WARN, "!vhci_scsi_reset 0x%x", level);
1544 1543 if ((level == RESET_TARGET) || (level == RESET_LUN)) {
1545 1544 return (vhci_scsi_reset_target(ap, level, TRUE));
1546 1545 } else if (level == RESET_ALL) {
1547 1546 return (vhci_scsi_reset_bus(ap));
1548 1547 }
1549 1548
1550 1549 return (rval);
1551 1550 }
1552 1551
1553 1552 /*
1554 1553 * vhci_recovery_reset:
1555 1554 * Issues reset to the device
1556 1555 * Input:
1557 1556 * vlun - vhci lun pointer of the device
1558 1557 * ap - address of the device
1559 1558 * select_path:
1560 1559 * If select_path is FALSE, then the address specified in ap is
1561 1560 * the path on which reset will be issued.
1562 1561 * If select_path is TRUE, then path is obtained by calling
1563 1562 * mdi_select_path.
1564 1563 *
1565 1564 * recovery_depth:
1566 1565 * Caller can specify the level of reset.
1567 1566 * VHCI_DEPTH_LUN -
1568 1567 * Issues LUN RESET if device supports lun reset.
1569 1568 * VHCI_DEPTH_TARGET -
1570 1569 * If Lun Reset fails or the device does not support
1571 1570 * Lun Reset, issues TARGET RESET
1572 1571 * VHCI_DEPTH_ALL -
1573 1572 * If Lun Reset fails or the device does not support
1574 1573 * Lun Reset, issues TARGET RESET.
1575 1574 * If TARGET RESET does not succeed, issues Bus Reset.
1576 1575 */
1577 1576
1578 1577 static int
1579 1578 vhci_recovery_reset(scsi_vhci_lun_t *vlun, struct scsi_address *ap,
1580 1579 uint8_t select_path, uint8_t recovery_depth)
1581 1580 {
1582 1581 int ret = 0;
1583 1582
1584 1583 ASSERT(ap != NULL);
1585 1584
1586 1585 if (vlun && vlun->svl_support_lun_reset == 1) {
1587 1586 ret = vhci_scsi_reset_target(ap, RESET_LUN,
1588 1587 select_path);
1589 1588 }
1590 1589
1591 1590 recovery_depth--;
1592 1591
1593 1592 if ((ret == 0) && recovery_depth) {
1594 1593 ret = vhci_scsi_reset_target(ap, RESET_TARGET,
1595 1594 select_path);
1596 1595 recovery_depth--;
1597 1596 }
1598 1597
1599 1598 if ((ret == 0) && recovery_depth) {
1600 1599 (void) scsi_reset(ap, RESET_ALL);
1601 1600 }
1602 1601
1603 1602 return (ret);
1604 1603 }
1605 1604
1606 1605 /*
1607 1606 * Note: The scsi_address passed to this routine could be the scsi_address
1608 1607 * for the virtual device or the physical device. No assumptions should be
1609 1608 * made in this routine about the contents of the ap structure.
1610 1609 * Further, note that the child dip would be the dip of the ssd node regardless
1611 1610 * of the scsi_address passed in.
1612 1611 */
1613 1612 static int
1614 1613 vhci_scsi_reset_target(struct scsi_address *ap, int level, uint8_t select_path)
1615 1614 {
1616 1615 dev_info_t *vdip, *cdip;
1617 1616 mdi_pathinfo_t *pip = NULL;
1618 1617 mdi_pathinfo_t *npip = NULL;
1619 1618 int rval = -1;
1620 1619 scsi_vhci_priv_t *svp = NULL;
1621 1620 struct scsi_address *pap = NULL;
1622 1621 scsi_hba_tran_t *hba = NULL;
1623 1622 int sps;
1624 1623 struct scsi_vhci *vhci = NULL;
1625 1624
1626 1625 if (select_path != TRUE) {
1627 1626 ASSERT(ap != NULL);
1628 1627 if (level == RESET_LUN) {
1629 1628 hba = ap->a_hba_tran;
1630 1629 ASSERT(hba != NULL);
1631 1630 return (hba->tran_reset(ap, RESET_LUN));
1632 1631 }
1633 1632 return (scsi_reset(ap, level));
1634 1633 }
1635 1634
1636 1635 cdip = ADDR2DIP(ap);
1637 1636 ASSERT(cdip != NULL);
1638 1637 vdip = ddi_get_parent(cdip);
1639 1638 ASSERT(vdip != NULL);
1640 1639 vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip));
1641 1640 ASSERT(vhci != NULL);
1642 1641
1643 1642 rval = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH, NULL, &pip);
1644 1643 if ((rval != MDI_SUCCESS) || (pip == NULL)) {
1645 1644 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_scsi_reset_target: "
1646 1645 "Unable to get a path, dip 0x%p", (void *)cdip));
1647 1646 return (0);
1648 1647 }
1649 1648 again:
1650 1649 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
1651 1650 if (svp == NULL) {
1652 1651 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_scsi_reset_target: "
1653 1652 "priv is NULL, pip 0x%p", (void *)pip));
1654 1653 mdi_rele_path(pip);
1655 1654 return (0);
1656 1655 }
1657 1656
1658 1657 if (svp->svp_psd == NULL) {
1659 1658 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_scsi_reset_target: "
1660 1659 "psd is NULL, pip 0x%p, svp 0x%p",
1661 1660 (void *)pip, (void *)svp));
1662 1661 mdi_rele_path(pip);
1663 1662 return (0);
1664 1663 }
1665 1664
1666 1665 pap = &svp->svp_psd->sd_address;
1667 1666 hba = pap->a_hba_tran;
1668 1667
1669 1668 ASSERT(pap != NULL);
1670 1669 ASSERT(hba != NULL);
1671 1670
1672 1671 if (hba->tran_reset != NULL) {
1673 1672 if (hba->tran_reset(pap, level) == 0) {
1674 1673 vhci_log(CE_WARN, vdip, "!%s%d: "
1675 1674 "path %s, reset %d failed",
1676 1675 ddi_driver_name(cdip), ddi_get_instance(cdip),
1677 1676 mdi_pi_spathname(pip), level);
1678 1677
1679 1678 /*
1680 1679 * Select next path and issue the reset, repeat
1681 1680 * until all paths are exhausted
1682 1681 */
1683 1682 sps = mdi_select_path(cdip, NULL,
1684 1683 MDI_SELECT_ONLINE_PATH, pip, &npip);
1685 1684 if ((sps != MDI_SUCCESS) || (npip == NULL)) {
1686 1685 mdi_rele_path(pip);
1687 1686 return (0);
1688 1687 }
1689 1688 mdi_rele_path(pip);
1690 1689 pip = npip;
1691 1690 goto again;
1692 1691 }
1693 1692 mdi_rele_path(pip);
1694 1693 mutex_enter(&vhci->vhci_mutex);
1695 1694 scsi_hba_reset_notify_callback(&vhci->vhci_mutex,
1696 1695 &vhci->vhci_reset_notify_listf);
1697 1696 mutex_exit(&vhci->vhci_mutex);
1698 1697 VHCI_DEBUG(6, (CE_NOTE, NULL, "!vhci_scsi_reset_target: "
1699 1698 "reset %d sent down pip:%p for cdip:%p\n", level,
1700 1699 (void *)pip, (void *)cdip));
1701 1700 return (1);
1702 1701 }
1703 1702 mdi_rele_path(pip);
1704 1703 return (0);
1705 1704 }
1706 1705
1707 1706
1708 1707 /* ARGSUSED */
1709 1708 static int
1710 1709 vhci_scsi_reset_bus(struct scsi_address *ap)
1711 1710 {
1712 1711 return (1);
1713 1712 }
1714 1713
1715 1714
1716 1715 /*
1717 1716 * called by vhci_getcap and vhci_setcap to get and set (respectively)
1718 1717 * SCSI capabilities
1719 1718 */
1720 1719 /* ARGSUSED */
1721 1720 static int
1722 1721 vhci_commoncap(struct scsi_address *ap, char *cap,
1723 1722 int val, int tgtonly, int doset)
1724 1723 {
1725 1724 struct scsi_vhci *vhci = ADDR2VHCI(ap);
1726 1725 struct scsi_vhci_lun *vlun = ADDR2VLUN(ap);
1727 1726 int cidx;
1728 1727 int rval = 0;
1729 1728
1730 1729 if (cap == (char *)0) {
1731 1730 VHCI_DEBUG(3, (CE_WARN, vhci->vhci_dip,
1732 1731 "!vhci_commoncap: invalid arg"));
1733 1732 return (rval);
1734 1733 }
1735 1734
1736 1735 if (vlun == NULL) {
1737 1736 VHCI_DEBUG(3, (CE_WARN, vhci->vhci_dip,
1738 1737 "!vhci_commoncap: vlun is null"));
1739 1738 return (rval);
1740 1739 }
1741 1740
1742 1741 if ((cidx = scsi_hba_lookup_capstr(cap)) == -1) {
1743 1742 return (UNDEFINED);
1744 1743 }
1745 1744
1746 1745 /*
1747 1746 * Process setcap request.
1748 1747 */
1749 1748 if (doset) {
1750 1749 /*
1751 1750 * At present, we can only set binary (0/1) values
1752 1751 */
1753 1752 switch (cidx) {
1754 1753 case SCSI_CAP_ARQ:
1755 1754 if (val == 0) {
1756 1755 rval = 0;
1757 1756 } else {
1758 1757 rval = 1;
1759 1758 }
1760 1759 break;
1761 1760
1762 1761 case SCSI_CAP_LUN_RESET:
1763 1762 if (tgtonly == 0) {
1764 1763 VHCI_DEBUG(1, (CE_WARN, vhci->vhci_dip,
1765 1764 "scsi_vhci_setcap: "
1766 1765 "Returning error since whom = 0"));
1767 1766 rval = -1;
1768 1767 break;
1769 1768 }
1770 1769 /*
1771 1770 * Set the capability accordingly.
1772 1771 */
1773 1772 mutex_enter(&vlun->svl_mutex);
1774 1773 vlun->svl_support_lun_reset = val;
1775 1774 rval = val;
1776 1775 mutex_exit(&vlun->svl_mutex);
1777 1776 break;
1778 1777
1779 1778 case SCSI_CAP_SECTOR_SIZE:
1780 1779 mutex_enter(&vlun->svl_mutex);
1781 1780 vlun->svl_sector_size = val;
1782 1781 vlun->svl_setcap_done = 1;
1783 1782 mutex_exit(&vlun->svl_mutex);
1784 1783 (void) vhci_pHCI_cap(ap, cap, val, tgtonly, NULL);
1785 1784
1786 1785 /* Always return success */
1787 1786 rval = 1;
1788 1787 break;
1789 1788
1790 1789 default:
1791 1790 VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip,
1792 1791 "!vhci_setcap: unsupported %d", cidx));
1793 1792 rval = UNDEFINED;
1794 1793 break;
1795 1794 }
1796 1795
1797 1796 VHCI_DEBUG(6, (CE_NOTE, vhci->vhci_dip,
1798 1797 "!set cap: cap=%s, val/tgtonly/doset/rval = "
1799 1798 "0x%x/0x%x/0x%x/%d\n",
1800 1799 cap, val, tgtonly, doset, rval));
1801 1800
1802 1801 } else {
1803 1802 /*
1804 1803 * Process getcap request.
1805 1804 */
1806 1805 switch (cidx) {
1807 1806 case SCSI_CAP_DMA_MAX:
1808 1807 /*
1809 1808 * For X86 this capability is caught in scsi_ifgetcap().
1810 1809 * XXX Should this be getting the value from the pHCI?
1811 1810 */
1812 1811 rval = (int)VHCI_DMA_MAX_XFER_CAP;
1813 1812 break;
1814 1813
1815 1814 case SCSI_CAP_INITIATOR_ID:
1816 1815 rval = 0x00;
1817 1816 break;
1818 1817
1819 1818 case SCSI_CAP_ARQ:
1820 1819 case SCSI_CAP_RESET_NOTIFICATION:
1821 1820 case SCSI_CAP_TAGGED_QING:
1822 1821 rval = 1;
1823 1822 break;
1824 1823
1825 1824 case SCSI_CAP_SCSI_VERSION:
1826 1825 rval = 3;
1827 1826 break;
1828 1827
1829 1828 case SCSI_CAP_INTERCONNECT_TYPE:
1830 1829 rval = INTERCONNECT_FABRIC;
1831 1830 break;
1832 1831
1833 1832 case SCSI_CAP_LUN_RESET:
1834 1833 /*
1835 1834 * scsi_vhci will always return success for LUN reset.
1836 1835 * When request for doing LUN reset comes
1837 1836 * through scsi_reset entry point, at that time attempt
1838 1837 * will be made to do reset through all the possible
1839 1838 * paths.
1840 1839 */
1841 1840 mutex_enter(&vlun->svl_mutex);
1842 1841 rval = vlun->svl_support_lun_reset;
1843 1842 mutex_exit(&vlun->svl_mutex);
1844 1843 VHCI_DEBUG(4, (CE_WARN, vhci->vhci_dip,
1845 1844 "scsi_vhci_getcap:"
1846 1845 "Getting the Lun reset capability %d", rval));
1847 1846 break;
1848 1847
1849 1848 case SCSI_CAP_SECTOR_SIZE:
1850 1849 mutex_enter(&vlun->svl_mutex);
1851 1850 rval = vlun->svl_sector_size;
1852 1851 mutex_exit(&vlun->svl_mutex);
1853 1852 break;
1854 1853
1855 1854 case SCSI_CAP_CDB_LEN:
1856 1855 rval = VHCI_SCSI_CDB_SIZE;
1857 1856 break;
1858 1857
1859 1858 case SCSI_CAP_DMA_MAX_ARCH:
1860 1859 /*
1861 1860 * For X86 this capability is caught in scsi_ifgetcap().
1862 1861 * XXX Should this be getting the value from the pHCI?
1863 1862 */
1864 1863 rval = 0;
1865 1864 break;
1866 1865
1867 1866 default:
1868 1867 VHCI_DEBUG(6, (CE_WARN, vhci->vhci_dip,
1869 1868 "!vhci_getcap: unsupported %d", cidx));
1870 1869 rval = UNDEFINED;
1871 1870 break;
1872 1871 }
1873 1872
1874 1873 VHCI_DEBUG(6, (CE_NOTE, vhci->vhci_dip,
1875 1874 "!get cap: cap=%s, val/tgtonly/doset/rval = "
1876 1875 "0x%x/0x%x/0x%x/%d\n",
1877 1876 cap, val, tgtonly, doset, rval));
1878 1877 }
1879 1878 return (rval);
1880 1879 }
1881 1880
1882 1881
1883 1882 /*
1884 1883 * Function name : vhci_scsi_getcap()
1885 1884 *
1886 1885 */
1887 1886 static int
1888 1887 vhci_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
1889 1888 {
1890 1889 return (vhci_commoncap(ap, cap, 0, whom, 0));
1891 1890 }
1892 1891
1893 1892 static int
1894 1893 vhci_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
1895 1894 {
1896 1895 return (vhci_commoncap(ap, cap, value, whom, 1));
1897 1896 }
1898 1897
1899 1898 /*
1900 1899 * Function name : vhci_scsi_abort()
1901 1900 */
1902 1901 /* ARGSUSED */
1903 1902 static int
1904 1903 vhci_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
1905 1904 {
1906 1905 return (0);
1907 1906 }
1908 1907
1909 1908 /*
1910 1909 * Function name : vhci_scsi_init_pkt
1911 1910 *
1912 1911 * Return Values : pointer to scsi_pkt, or NULL
1913 1912 */
1914 1913 /* ARGSUSED */
1915 1914 static struct scsi_pkt *
1916 1915 vhci_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
1917 1916 struct buf *bp, int cmdlen, int statuslen, int tgtlen,
1918 1917 int flags, int (*callback)(caddr_t), caddr_t arg)
1919 1918 {
1920 1919 struct scsi_vhci *vhci = ADDR2VHCI(ap);
1921 1920 struct vhci_pkt *vpkt;
1922 1921 int rval;
1923 1922 int newpkt = 0;
1924 1923 struct scsi_pkt *pktp;
1925 1924
1926 1925
1927 1926 if (pkt == NULL) {
1928 1927 if (cmdlen > VHCI_SCSI_CDB_SIZE) {
1929 1928 if ((cmdlen != VHCI_SCSI_OSD_CDB_SIZE) ||
1930 1929 ((flags & VHCI_SCSI_OSD_PKT_FLAGS) !=
1931 1930 VHCI_SCSI_OSD_PKT_FLAGS)) {
1932 1931 VHCI_DEBUG(1, (CE_NOTE, NULL,
1933 1932 "!init pkt: cdb size not supported\n"));
1934 1933 return (NULL);
1935 1934 }
1936 1935 }
1937 1936
1938 1937 pktp = scsi_hba_pkt_alloc(vhci->vhci_dip,
1939 1938 ap, cmdlen, statuslen, tgtlen, sizeof (*vpkt), callback,
1940 1939 arg);
1941 1940
1942 1941 if (pktp == NULL) {
1943 1942 return (NULL);
1944 1943 }
1945 1944
1946 1945 /* Get the vhci's private structure */
1947 1946 vpkt = (struct vhci_pkt *)(pktp->pkt_ha_private);
1948 1947 ASSERT(vpkt);
1949 1948
1950 1949 /* Save the target driver's packet */
1951 1950 vpkt->vpkt_tgt_pkt = pktp;
1952 1951
1953 1952 /*
1954 1953 * Save pkt_tgt_init_pkt fields if deferred binding
1955 1954 * is needed or for other purposes.
1956 1955 */
1957 1956 vpkt->vpkt_tgt_init_pkt_flags = flags;
1958 1957 vpkt->vpkt_flags = (callback == NULL_FUNC) ? CFLAG_NOWAIT : 0;
1959 1958 vpkt->vpkt_state = VHCI_PKT_IDLE;
1960 1959 vpkt->vpkt_tgt_init_cdblen = cmdlen;
1961 1960 vpkt->vpkt_tgt_init_scblen = statuslen;
1962 1961 newpkt = 1;
1963 1962 } else { /* pkt not NULL */
1964 1963 vpkt = pkt->pkt_ha_private;
1965 1964 }
1966 1965
1967 1966 VHCI_DEBUG(8, (CE_NOTE, NULL, "vhci_scsi_init_pkt "
1968 1967 "vpkt %p flags %x\n", (void *)vpkt, flags));
1969 1968
1970 1969 /* Clear any stale error flags */
1971 1970 if (bp) {
1972 1971 bioerror(bp, 0);
1973 1972 }
1974 1973
1975 1974 vpkt->vpkt_tgt_init_bp = bp;
1976 1975
1977 1976 if (flags & PKT_DMA_PARTIAL) {
1978 1977
1979 1978 /*
1980 1979 * Immediate binding is needed.
1981 1980 * Target driver may not set this flag in next invocation.
1982 1981 * vhci has to remember this flag was set during first
1983 1982 * invocation of vhci_scsi_init_pkt.
1984 1983 */
1985 1984 vpkt->vpkt_flags |= CFLAG_DMA_PARTIAL;
1986 1985 }
1987 1986
1988 1987 if (vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) {
1989 1988
1990 1989 /*
1991 1990 * Re-initialize some of the target driver packet state
1992 1991 * information.
1993 1992 */
1994 1993 vpkt->vpkt_tgt_pkt->pkt_state = 0;
1995 1994 vpkt->vpkt_tgt_pkt->pkt_statistics = 0;
1996 1995 vpkt->vpkt_tgt_pkt->pkt_reason = 0;
1997 1996
1998 1997 /*
1999 1998 * Binding a vpkt->vpkt_path for this IO at init_time.
2000 1999 * If an IO error happens later, target driver will clear
2001 2000 * this vpkt->vpkt_path binding before re-init IO again.
2002 2001 */
2003 2002 VHCI_DEBUG(8, (CE_NOTE, NULL,
2004 2003 "vhci_scsi_init_pkt: calling v_b_t %p, newpkt %d\n",
2005 2004 (void *)vpkt, newpkt));
2006 2005 if (pkt && vpkt->vpkt_hba_pkt) {
2007 2006 VHCI_DEBUG(4, (CE_NOTE, NULL,
2008 2007 "v_s_i_p calling update_pHCI_pkt resid %ld\n",
2009 2008 pkt->pkt_resid));
2010 2009 vhci_update_pHCI_pkt(vpkt, pkt);
2011 2010 }
2012 2011 if (callback == SLEEP_FUNC) {
2013 2012 rval = vhci_bind_transport(
2014 2013 ap, vpkt, flags, callback);
2015 2014 } else {
2016 2015 rval = vhci_bind_transport(
2017 2016 ap, vpkt, flags, NULL_FUNC);
2018 2017 }
2019 2018 VHCI_DEBUG(8, (CE_NOTE, NULL,
2020 2019 "vhci_scsi_init_pkt: v_b_t called 0x%p rval 0x%x\n",
2021 2020 (void *)vpkt, rval));
2022 2021 if (bp) {
2023 2022 if (rval == TRAN_FATAL_ERROR) {
2024 2023 /*
2025 2024 * No paths available. Could not bind
2026 2025 * any pHCI. Setting EFAULT as a way
2027 2026 * to indicate no DMA is mapped.
2028 2027 */
2029 2028 bioerror(bp, EFAULT);
2030 2029 } else {
2031 2030 /*
2032 2031 * Do not indicate any pHCI errors to
2033 2032 * target driver otherwise.
2034 2033 */
2035 2034 bioerror(bp, 0);
2036 2035 }
2037 2036 }
2038 2037 if (rval != TRAN_ACCEPT) {
2039 2038 VHCI_DEBUG(8, (CE_NOTE, NULL,
2040 2039 "vhci_scsi_init_pkt: "
2041 2040 "v_b_t failed 0x%p newpkt %x\n",
2042 2041 (void *)vpkt, newpkt));
2043 2042 if (newpkt) {
2044 2043 scsi_hba_pkt_free(ap,
2045 2044 vpkt->vpkt_tgt_pkt);
2046 2045 }
2047 2046 return (NULL);
2048 2047 }
2049 2048 ASSERT(vpkt->vpkt_hba_pkt != NULL);
2050 2049 ASSERT(vpkt->vpkt_path != NULL);
2051 2050
2052 2051 /* Update the resid for the target driver */
2053 2052 vpkt->vpkt_tgt_pkt->pkt_resid =
2054 2053 vpkt->vpkt_hba_pkt->pkt_resid;
2055 2054 }
2056 2055
2057 2056 return (vpkt->vpkt_tgt_pkt);
2058 2057 }
2059 2058
2060 2059 /*
2061 2060 * Function name : vhci_scsi_destroy_pkt
2062 2061 *
2063 2062 * Return Values : none
2064 2063 */
2065 2064 static void
2066 2065 vhci_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2067 2066 {
2068 2067 struct vhci_pkt *vpkt = (struct vhci_pkt *)pkt->pkt_ha_private;
2069 2068
2070 2069 VHCI_DEBUG(8, (CE_NOTE, NULL,
2071 2070 "vhci_scsi_destroy_pkt: vpkt 0x%p\n", (void *)vpkt));
2072 2071
2073 2072 vpkt->vpkt_tgt_init_pkt_flags = 0;
2074 2073 if (vpkt->vpkt_hba_pkt) {
2075 2074 scsi_destroy_pkt(vpkt->vpkt_hba_pkt);
2076 2075 vpkt->vpkt_hba_pkt = NULL;
2077 2076 }
2078 2077 if (vpkt->vpkt_path) {
2079 2078 mdi_rele_path(vpkt->vpkt_path);
2080 2079 vpkt->vpkt_path = NULL;
2081 2080 }
2082 2081
2083 2082 ASSERT(vpkt->vpkt_state != VHCI_PKT_ISSUED);
2084 2083 scsi_hba_pkt_free(ap, vpkt->vpkt_tgt_pkt);
2085 2084 }
2086 2085
2087 2086 /*
2088 2087 * Function name : vhci_scsi_dmafree()
2089 2088 *
2090 2089 * Return Values : none
2091 2090 */
2092 2091 /*ARGSUSED*/
2093 2092 static void
2094 2093 vhci_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2095 2094 {
2096 2095 struct vhci_pkt *vpkt = (struct vhci_pkt *)pkt->pkt_ha_private;
2097 2096
2098 2097 VHCI_DEBUG(6, (CE_NOTE, NULL,
2099 2098 "vhci_scsi_dmafree: vpkt 0x%p\n", (void *)vpkt));
2100 2099
2101 2100 ASSERT(vpkt != NULL);
2102 2101 if (vpkt->vpkt_hba_pkt) {
2103 2102 scsi_destroy_pkt(vpkt->vpkt_hba_pkt);
2104 2103 vpkt->vpkt_hba_pkt = NULL;
2105 2104 }
2106 2105 if (vpkt->vpkt_path) {
2107 2106 mdi_rele_path(vpkt->vpkt_path);
2108 2107 vpkt->vpkt_path = NULL;
2109 2108 }
2110 2109 }
2111 2110
2112 2111 /*
2113 2112 * Function name : vhci_scsi_sync_pkt()
2114 2113 *
2115 2114 * Return Values : none
2116 2115 */
2117 2116 /*ARGSUSED*/
2118 2117 static void
2119 2118 vhci_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2120 2119 {
2121 2120 struct vhci_pkt *vpkt = (struct vhci_pkt *)pkt->pkt_ha_private;
2122 2121
2123 2122 ASSERT(vpkt != NULL);
2124 2123 if (vpkt->vpkt_hba_pkt) {
2125 2124 scsi_sync_pkt(vpkt->vpkt_hba_pkt);
2126 2125 }
2127 2126 }
2128 2127
2129 2128 /*
2130 2129 * routine for reset notification setup, to register or cancel.
2131 2130 */
2132 2131 static int
2133 2132 vhci_scsi_reset_notify(struct scsi_address *ap, int flag,
2134 2133 void (*callback)(caddr_t), caddr_t arg)
2135 2134 {
2136 2135 struct scsi_vhci *vhci = ADDR2VHCI(ap);
2137 2136 return (scsi_hba_reset_notify_setup(ap, flag, callback, arg,
2138 2137 &vhci->vhci_mutex, &vhci->vhci_reset_notify_listf));
2139 2138 }
2140 2139
2141 2140 static int
2142 2141 vhci_scsi_get_name_bus_addr(struct scsi_device *sd,
2143 2142 char *name, int len, int bus_addr)
2144 2143 {
2145 2144 dev_info_t *cdip;
2146 2145 char *guid;
2147 2146 scsi_vhci_lun_t *vlun;
2148 2147
2149 2148 ASSERT(sd != NULL);
2150 2149 ASSERT(name != NULL);
2151 2150
2152 2151 *name = 0;
2153 2152 cdip = sd->sd_dev;
2154 2153
2155 2154 ASSERT(cdip != NULL);
2156 2155
2157 2156 if (mdi_component_is_client(cdip, NULL) != MDI_SUCCESS)
2158 2157 return (1);
2159 2158
2160 2159 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, cdip, PROPFLAGS,
2161 2160 MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS)
2162 2161 return (1);
2163 2162
2164 2163 /*
2165 2164 * Message is "sd# at scsi_vhci0: unit-address <guid>: <bus_addr>".
2166 2165 * <guid> bus_addr argument == 0
2167 2166 * <bus_addr> bus_addr argument != 0
2168 2167 * Since the <guid> is already provided with unit-address, we just
2169 2168 * provide failover module in <bus_addr> to keep output shorter.
2170 2169 */
2171 2170 vlun = ADDR2VLUN(&sd->sd_address);
2172 2171 if (bus_addr == 0) {
2173 2172 /* report the guid: */
2174 2173 (void) snprintf(name, len, "g%s", guid);
2175 2174 } else if (vlun && vlun->svl_fops_name) {
2176 2175 /* report the name of the failover module */
2177 2176 (void) snprintf(name, len, "%s", vlun->svl_fops_name);
2178 2177 }
2179 2178
2180 2179 ddi_prop_free(guid);
2181 2180 return (1);
2182 2181 }
2183 2182
2184 2183 static int
2185 2184 vhci_scsi_get_bus_addr(struct scsi_device *sd, char *name, int len)
2186 2185 {
2187 2186 return (vhci_scsi_get_name_bus_addr(sd, name, len, 1));
2188 2187 }
2189 2188
2190 2189 static int
2191 2190 vhci_scsi_get_name(struct scsi_device *sd, char *name, int len)
2192 2191 {
2193 2192 return (vhci_scsi_get_name_bus_addr(sd, name, len, 0));
2194 2193 }
2195 2194
2196 2195 /*
2197 2196 * Return a pointer to the guid part of the devnm.
2198 2197 * devnm format is "nodename@busaddr", busaddr format is "gGUID".
2199 2198 */
2200 2199 static char *
2201 2200 vhci_devnm_to_guid(char *devnm)
2202 2201 {
2203 2202 char *cp = devnm;
2204 2203
2205 2204 if (devnm == NULL)
2206 2205 return (NULL);
2207 2206
2208 2207 while (*cp != '\0' && *cp != '@')
2209 2208 cp++;
2210 2209 if (*cp == '@' && *(cp + 1) == 'g')
2211 2210 return (cp + 2);
2212 2211 return (NULL);
2213 2212 }
2214 2213
2215 2214 static int
2216 2215 vhci_bind_transport(struct scsi_address *ap, struct vhci_pkt *vpkt, int flags,
2217 2216 int (*func)(caddr_t))
2218 2217 {
2219 2218 struct scsi_vhci *vhci = ADDR2VHCI(ap);
2220 2219 dev_info_t *cdip = ADDR2DIP(ap);
2221 2220 mdi_pathinfo_t *pip = NULL;
2222 2221 mdi_pathinfo_t *npip = NULL;
2223 2222 scsi_vhci_priv_t *svp = NULL;
2224 2223 struct scsi_device *psd = NULL;
2225 2224 struct scsi_address *address = NULL;
2226 2225 struct scsi_pkt *pkt = NULL;
2227 2226 int rval = -1;
2228 2227 int pgr_sema_held = 0;
2229 2228 int held;
2230 2229 int mps_flag = MDI_SELECT_ONLINE_PATH;
2231 2230 struct scsi_vhci_lun *vlun;
2232 2231 int path_instance = 0;
2233 2232
2234 2233 vlun = ADDR2VLUN(ap);
2235 2234 ASSERT(vlun != 0);
2236 2235
2237 2236 if ((vpkt->vpkt_tgt_pkt->pkt_cdbp[0] == SCMD_PROUT) &&
2238 2237 (((vpkt->vpkt_tgt_pkt->pkt_cdbp[1] & 0x1f) ==
2239 2238 VHCI_PROUT_REGISTER) ||
2240 2239 ((vpkt->vpkt_tgt_pkt->pkt_cdbp[1] & 0x1f) ==
2241 2240 VHCI_PROUT_R_AND_IGNORE))) {
2242 2241 if (!sema_tryp(&vlun->svl_pgr_sema))
2243 2242 return (TRAN_BUSY);
2244 2243 pgr_sema_held = 1;
2245 2244 if (vlun->svl_first_path != NULL) {
2246 2245 rval = mdi_select_path(cdip, NULL,
2247 2246 MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH,
2248 2247 NULL, &pip);
2249 2248 if ((rval != MDI_SUCCESS) || (pip == NULL)) {
2250 2249 VHCI_DEBUG(4, (CE_NOTE, NULL,
2251 2250 "vhci_bind_transport: path select fail\n"));
2252 2251 } else {
2253 2252 npip = pip;
2254 2253 do {
2255 2254 if (npip == vlun->svl_first_path) {
2256 2255 VHCI_DEBUG(4, (CE_NOTE, NULL,
2257 2256 "vhci_bind_transport: "
2258 2257 "valid first path 0x%p\n",
2259 2258 (void *)
2260 2259 vlun->svl_first_path));
2261 2260 pip = vlun->svl_first_path;
2262 2261 goto bind_path;
2263 2262 }
2264 2263 pip = npip;
2265 2264 rval = mdi_select_path(cdip, NULL,
2266 2265 MDI_SELECT_ONLINE_PATH |
2267 2266 MDI_SELECT_STANDBY_PATH,
2268 2267 pip, &npip);
2269 2268 mdi_rele_path(pip);
2270 2269 } while ((rval == MDI_SUCCESS) &&
2271 2270 (npip != NULL));
2272 2271 }
2273 2272 }
2274 2273
2275 2274 if (vlun->svl_first_path) {
2276 2275 VHCI_DEBUG(4, (CE_NOTE, NULL,
2277 2276 "vhci_bind_transport: invalid first path 0x%p\n",
2278 2277 (void *)vlun->svl_first_path));
2279 2278 vlun->svl_first_path = NULL;
2280 2279 }
2281 2280 } else if (vpkt->vpkt_tgt_pkt->pkt_cdbp[0] == SCMD_PRIN) {
2282 2281 if ((vpkt->vpkt_state & VHCI_PKT_THRU_TASKQ) == 0) {
2283 2282 if (!sema_tryp(&vlun->svl_pgr_sema))
2284 2283 return (TRAN_BUSY);
2285 2284 }
2286 2285 pgr_sema_held = 1;
2287 2286 }
2288 2287
2289 2288 /*
2290 2289 * If the path is already bound for PKT_PARTIAL_DMA case,
2291 2290 * try to use the same path.
2292 2291 */
2293 2292 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) && vpkt->vpkt_path) {
2294 2293 VHCI_DEBUG(4, (CE_NOTE, NULL,
2295 2294 "vhci_bind_transport: PKT_PARTIAL_DMA "
2296 2295 "vpkt 0x%p, path 0x%p\n",
2297 2296 (void *)vpkt, (void *)vpkt->vpkt_path));
2298 2297 pip = vpkt->vpkt_path;
2299 2298 goto bind_path;
2300 2299 }
2301 2300
2302 2301 /*
2303 2302 * Get path_instance. Non-zero with FLAG_PKT_PATH_INSTANCE set
2304 2303 * indicates that mdi_select_path should be called to select a
2305 2304 * specific instance.
2306 2305 *
2307 2306 * NB: Condition pkt_path_instance reference on proper allocation.
2308 2307 */
2309 2308 if ((vpkt->vpkt_tgt_pkt->pkt_flags & FLAG_PKT_PATH_INSTANCE) &&
2310 2309 scsi_pkt_allocated_correctly(vpkt->vpkt_tgt_pkt)) {
2311 2310 path_instance = vpkt->vpkt_tgt_pkt->pkt_path_instance;
2312 2311 }
2313 2312
2314 2313 /*
2315 2314 * If reservation is active bind the transport directly to the pip
2316 2315 * with the reservation.
2317 2316 */
2318 2317 if (vpkt->vpkt_hba_pkt == NULL) {
2319 2318 if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) {
2320 2319 if (MDI_PI_IS_ONLINE(vlun->svl_resrv_pip)) {
2321 2320 pip = vlun->svl_resrv_pip;
2322 2321 mdi_hold_path(pip);
2323 2322 vlun->svl_waiting_for_activepath = 0;
2324 2323 rval = MDI_SUCCESS;
2325 2324 goto bind_path;
2326 2325 } else {
2327 2326 if (pgr_sema_held) {
2328 2327 sema_v(&vlun->svl_pgr_sema);
2329 2328 }
2330 2329 return (TRAN_BUSY);
2331 2330 }
2332 2331 }
2333 2332 try_again:
2334 2333 rval = mdi_select_path(cdip, vpkt->vpkt_tgt_init_bp,
2335 2334 path_instance ? MDI_SELECT_PATH_INSTANCE : 0,
2336 2335 (void *)(intptr_t)path_instance, &pip);
2337 2336 if (rval == MDI_BUSY) {
2338 2337 if (pgr_sema_held) {
2339 2338 sema_v(&vlun->svl_pgr_sema);
2340 2339 }
2341 2340 return (TRAN_BUSY);
2342 2341 } else if (rval == MDI_DEVI_ONLINING) {
2343 2342 /*
2344 2343 * if we are here then we are in the midst of
2345 2344 * an attach/probe of the client device.
2346 2345 * We attempt to bind to ONLINE path if available,
2347 2346 * else it is OK to bind to a STANDBY path (instead
2348 2347 * of triggering a failover) because IO associated
2349 2348 * with attach/probe (eg. INQUIRY, block 0 read)
2350 2349 * are completed by targets even on passive paths
2351 2350 * If no ONLINE paths available, it is important
2352 2351 * to set svl_waiting_for_activepath for two
2353 2352 * reasons: (1) avoid sense analysis in the
2354 2353 * "external failure detection" codepath in
2355 2354 * vhci_intr(). Failure to do so will result in
2356 2355 * infinite loop (unless an ONLINE path becomes
2357 2356 * available at some point) (2) avoid
2358 2357 * unnecessary failover (see "---Waiting For Active
2359 2358 * Path---" comment below).
2360 2359 */
2361 2360 VHCI_DEBUG(1, (CE_NOTE, NULL, "!%p in onlining "
2362 2361 "state\n", (void *)cdip));
2363 2362 pip = NULL;
2364 2363 rval = mdi_select_path(cdip, vpkt->vpkt_tgt_init_bp,
2365 2364 mps_flag, NULL, &pip);
2366 2365 if ((rval != MDI_SUCCESS) || (pip == NULL)) {
2367 2366 if (vlun->svl_waiting_for_activepath == 0) {
2368 2367 vlun->svl_waiting_for_activepath = 1;
2369 2368 vlun->svl_wfa_time = gethrtime();
2370 2369 }
2371 2370 mps_flag |= MDI_SELECT_STANDBY_PATH;
2372 2371 rval = mdi_select_path(cdip,
2373 2372 vpkt->vpkt_tgt_init_bp,
2374 2373 mps_flag, NULL, &pip);
2375 2374 if ((rval != MDI_SUCCESS) || (pip == NULL)) {
2376 2375 if (pgr_sema_held) {
2377 2376 sema_v(&vlun->svl_pgr_sema);
2378 2377 }
2379 2378 return (TRAN_FATAL_ERROR);
2380 2379 }
2381 2380 goto bind_path;
2382 2381 }
2383 2382 } else if ((rval == MDI_FAILURE) ||
2384 2383 ((rval == MDI_NOPATH) && (path_instance))) {
2385 2384 if (pgr_sema_held) {
2386 2385 sema_v(&vlun->svl_pgr_sema);
2387 2386 }
2388 2387 return (TRAN_FATAL_ERROR);
2389 2388 }
2390 2389
2391 2390 if ((pip == NULL) || (rval == MDI_NOPATH)) {
2392 2391 while (vlun->svl_waiting_for_activepath) {
2393 2392 /*
2394 2393 * ---Waiting For Active Path---
2395 2394 * This device was discovered across a
2396 2395 * passive path; lets wait for a little
2397 2396 * bit, hopefully an active path will
2398 2397 * show up obviating the need for a
2399 2398 * failover
2400 2399 */
2401 2400 if ((gethrtime() - vlun->svl_wfa_time) >=
2402 2401 (60 * NANOSEC)) {
2403 2402 vlun->svl_waiting_for_activepath = 0;
2404 2403 } else {
2405 2404 drv_usecwait(1000);
2406 2405 if (vlun->svl_waiting_for_activepath
2407 2406 == 0) {
2408 2407 /*
2409 2408 * an active path has come
2410 2409 * online!
2411 2410 */
2412 2411 goto try_again;
2413 2412 }
2414 2413 }
2415 2414 }
2416 2415 VHCI_HOLD_LUN(vlun, VH_NOSLEEP, held);
2417 2416 if (!held) {
2418 2417 VHCI_DEBUG(4, (CE_NOTE, NULL,
2419 2418 "!Lun not held\n"));
2420 2419 if (pgr_sema_held) {
2421 2420 sema_v(&vlun->svl_pgr_sema);
2422 2421 }
2423 2422 return (TRAN_BUSY);
2424 2423 }
2425 2424 /*
2426 2425 * now that the LUN is stable, one last check
2427 2426 * to make sure no other changes sneaked in
2428 2427 * (like a path coming online or a
2429 2428 * failover initiated by another thread)
2430 2429 */
2431 2430 pip = NULL;
2432 2431 rval = mdi_select_path(cdip, vpkt->vpkt_tgt_init_bp,
2433 2432 0, NULL, &pip);
2434 2433 if (pip != NULL) {
2435 2434 VHCI_RELEASE_LUN(vlun);
2436 2435 vlun->svl_waiting_for_activepath = 0;
2437 2436 goto bind_path;
2438 2437 }
2439 2438
2440 2439 /*
2441 2440 * Check if there is an ONLINE path OR a STANDBY path
2442 2441 * available. If none is available, do not attempt
2443 2442 * to do a failover, just return a fatal error at this
2444 2443 * point.
2445 2444 */
2446 2445 npip = NULL;
2447 2446 rval = mdi_select_path(cdip, NULL,
2448 2447 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH),
2449 2448 NULL, &npip);
2450 2449 if ((npip == NULL) || (rval != MDI_SUCCESS)) {
2451 2450 /*
2452 2451 * No paths available, jus return FATAL error.
2453 2452 */
2454 2453 VHCI_RELEASE_LUN(vlun);
2455 2454 if (pgr_sema_held) {
2456 2455 sema_v(&vlun->svl_pgr_sema);
2457 2456 }
2458 2457 return (TRAN_FATAL_ERROR);
2459 2458 }
2460 2459 mdi_rele_path(npip);
2461 2460 if (!(vpkt->vpkt_state & VHCI_PKT_IN_FAILOVER)) {
2462 2461 VHCI_DEBUG(1, (CE_NOTE, NULL, "!invoking "
2463 2462 "mdi_failover\n"));
2464 2463 rval = mdi_failover(vhci->vhci_dip, cdip,
2465 2464 MDI_FAILOVER_ASYNC);
2466 2465 } else {
2467 2466 rval = vlun->svl_failover_status;
2468 2467 }
2469 2468 if (rval == MDI_FAILURE) {
2470 2469 VHCI_RELEASE_LUN(vlun);
2471 2470 if (pgr_sema_held) {
2472 2471 sema_v(&vlun->svl_pgr_sema);
2473 2472 }
2474 2473 return (TRAN_FATAL_ERROR);
2475 2474 } else if (rval == MDI_BUSY) {
2476 2475 VHCI_RELEASE_LUN(vlun);
2477 2476 if (pgr_sema_held) {
2478 2477 sema_v(&vlun->svl_pgr_sema);
2479 2478 }
2480 2479 return (TRAN_BUSY);
2481 2480 } else {
2482 2481 if (pgr_sema_held) {
2483 2482 sema_v(&vlun->svl_pgr_sema);
2484 2483 }
2485 2484 vpkt->vpkt_state |= VHCI_PKT_IN_FAILOVER;
2486 2485 return (TRAN_BUSY);
2487 2486 }
2488 2487 }
2489 2488 vlun->svl_waiting_for_activepath = 0;
2490 2489 bind_path:
2491 2490 vpkt->vpkt_path = pip;
2492 2491 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
2493 2492 ASSERT(svp != NULL);
2494 2493
2495 2494 psd = svp->svp_psd;
2496 2495 ASSERT(psd != NULL);
2497 2496 address = &psd->sd_address;
2498 2497 } else {
2499 2498 pkt = vpkt->vpkt_hba_pkt;
2500 2499 address = &pkt->pkt_address;
2501 2500 }
2502 2501
2503 2502 /* Verify match of specified path_instance and selected path_instance */
2504 2503 ASSERT((path_instance == 0) ||
2505 2504 (path_instance == mdi_pi_get_path_instance(vpkt->vpkt_path)));
2506 2505
2507 2506 /*
2508 2507 * For PKT_PARTIAL_DMA case, call pHCI's scsi_init_pkt whenever
2509 2508 * target driver calls vhci_scsi_init_pkt.
2510 2509 */
2511 2510 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) &&
2512 2511 vpkt->vpkt_path && vpkt->vpkt_hba_pkt) {
2513 2512 VHCI_DEBUG(4, (CE_NOTE, NULL,
2514 2513 "vhci_bind_transport: PKT_PARTIAL_DMA "
2515 2514 "vpkt 0x%p, path 0x%p hba_pkt 0x%p\n",
2516 2515 (void *)vpkt, (void *)vpkt->vpkt_path, (void *)pkt));
2517 2516 pkt = vpkt->vpkt_hba_pkt;
2518 2517 address = &pkt->pkt_address;
2519 2518 }
2520 2519
2521 2520 if (pkt == NULL || (vpkt->vpkt_flags & CFLAG_DMA_PARTIAL)) {
2522 2521 pkt = scsi_init_pkt(address, pkt,
2523 2522 vpkt->vpkt_tgt_init_bp, vpkt->vpkt_tgt_init_cdblen,
2524 2523 vpkt->vpkt_tgt_init_scblen, 0, flags, func, NULL);
2525 2524
2526 2525 if (pkt == NULL) {
2527 2526 VHCI_DEBUG(4, (CE_NOTE, NULL,
2528 2527 "!bind transport: 0x%p 0x%p 0x%p\n",
2529 2528 (void *)vhci, (void *)psd, (void *)vpkt));
2530 2529 if ((vpkt->vpkt_hba_pkt == NULL) && vpkt->vpkt_path) {
2531 2530 MDI_PI_ERRSTAT(vpkt->vpkt_path,
2532 2531 MDI_PI_TRANSERR);
2533 2532 mdi_rele_path(vpkt->vpkt_path);
2534 2533 vpkt->vpkt_path = NULL;
2535 2534 }
2536 2535 if (pgr_sema_held) {
2537 2536 sema_v(&vlun->svl_pgr_sema);
2538 2537 }
2539 2538 /*
2540 2539 * Consider it a fatal error if b_error is
2541 2540 * set as a result of DMA binding failure
2542 2541 * vs. a condition of being temporarily out of
2543 2542 * some resource
2544 2543 */
2545 2544 if (vpkt->vpkt_tgt_init_bp == NULL ||
2546 2545 geterror(vpkt->vpkt_tgt_init_bp))
2547 2546 return (TRAN_FATAL_ERROR);
2548 2547 else
2549 2548 return (TRAN_BUSY);
2550 2549 }
2551 2550 }
2552 2551
2553 2552 pkt->pkt_private = vpkt;
2554 2553 vpkt->vpkt_hba_pkt = pkt;
2555 2554 return (TRAN_ACCEPT);
2556 2555 }
2557 2556
2558 2557
2559 2558 /*PRINTFLIKE3*/
2560 2559 void
2561 2560 vhci_log(int level, dev_info_t *dip, const char *fmt, ...)
2562 2561 {
2563 2562 char buf[256];
2564 2563 va_list ap;
2565 2564
2566 2565 va_start(ap, fmt);
2567 2566 (void) vsprintf(buf, fmt, ap);
2568 2567 va_end(ap);
2569 2568
2570 2569 scsi_log(dip, "scsi_vhci", level, buf);
2571 2570 }
2572 2571
2573 2572 /* do a PGR out with the information we've saved away */
2574 2573 static int
2575 2574 vhci_do_prout(scsi_vhci_priv_t *svp)
2576 2575 {
2577 2576
2578 2577 struct scsi_pkt *new_pkt;
2579 2578 struct buf *bp;
2580 2579 scsi_vhci_lun_t *vlun = svp->svp_svl;
2581 2580 int rval, retry, nr_retry, ua_retry;
2582 2581 uint8_t *sns, skey;
2583 2582
2584 2583 bp = getrbuf(KM_SLEEP);
2585 2584 bp->b_flags = B_WRITE;
2586 2585 bp->b_resid = 0;
2587 2586 bp->b_un.b_addr = (caddr_t)&vlun->svl_prout;
2588 2587 bp->b_bcount = vlun->svl_bcount;
2589 2588
2590 2589 VHCI_INCR_PATH_CMDCOUNT(svp);
2591 2590
2592 2591 new_pkt = scsi_init_pkt(&svp->svp_psd->sd_address, NULL, bp,
2593 2592 CDB_GROUP1, sizeof (struct scsi_arq_status), 0, 0,
2594 2593 SLEEP_FUNC, NULL);
2595 2594 if (new_pkt == NULL) {
2596 2595 VHCI_DECR_PATH_CMDCOUNT(svp);
2597 2596 freerbuf(bp);
2598 2597 cmn_err(CE_WARN, "!vhci_do_prout: scsi_init_pkt failed");
2599 2598 return (0);
2600 2599 }
2601 2600 mutex_enter(&vlun->svl_mutex);
2602 2601 bp->b_un.b_addr = (caddr_t)&vlun->svl_prout;
2603 2602 bp->b_bcount = vlun->svl_bcount;
2604 2603 bcopy(vlun->svl_cdb, new_pkt->pkt_cdbp,
2605 2604 sizeof (vlun->svl_cdb));
2606 2605 new_pkt->pkt_time = vlun->svl_time;
2607 2606 mutex_exit(&vlun->svl_mutex);
2608 2607 new_pkt->pkt_flags = FLAG_NOINTR;
2609 2608
2610 2609 ua_retry = nr_retry = retry = 0;
2611 2610 again:
2612 2611 rval = vhci_do_scsi_cmd(new_pkt);
2613 2612 if (rval != 1) {
2614 2613 if ((new_pkt->pkt_reason == CMD_CMPLT) &&
2615 2614 (SCBP_C(new_pkt) == STATUS_CHECK) &&
2616 2615 (new_pkt->pkt_state & STATE_ARQ_DONE)) {
2617 2616 sns = (uint8_t *)
2618 2617 &(((struct scsi_arq_status *)(uintptr_t)
2619 2618 (new_pkt->pkt_scbp))->sts_sensedata);
2620 2619 skey = scsi_sense_key(sns);
2621 2620 if ((skey == KEY_UNIT_ATTENTION) ||
2622 2621 (skey == KEY_NOT_READY)) {
2623 2622 int max_retry;
2624 2623 struct scsi_failover_ops *fops;
2625 2624 fops = vlun->svl_fops;
2626 2625 rval = fops->sfo_analyze_sense(svp->svp_psd,
2627 2626 sns, vlun->svl_fops_ctpriv);
2628 2627 if (rval == SCSI_SENSE_NOT_READY) {
2629 2628 max_retry = vhci_prout_not_ready_retry;
2630 2629 retry = nr_retry++;
2631 2630 delay(1*drv_usectohz(1000000));
2632 2631 } else {
2633 2632 /* chk for state change and update */
2634 2633 if (rval == SCSI_SENSE_STATE_CHANGED) {
2635 2634 int held;
2636 2635 VHCI_HOLD_LUN(vlun,
2637 2636 VH_NOSLEEP, held);
2638 2637 if (!held) {
2639 2638 rval = TRAN_BUSY;
2640 2639 } else {
2641 2640 /* chk for alua first */
2642 2641 vhci_update_pathstates(
2643 2642 (void *)vlun);
2644 2643 }
2645 2644 }
2646 2645 retry = ua_retry++;
2647 2646 max_retry = VHCI_MAX_PGR_RETRIES;
2648 2647 }
2649 2648 if (retry < max_retry) {
2650 2649 VHCI_DEBUG(4, (CE_WARN, NULL,
2651 2650 "!vhci_do_prout retry 0x%x "
2652 2651 "(0x%x 0x%x 0x%x)",
2653 2652 SCBP_C(new_pkt),
2654 2653 new_pkt->pkt_cdbp[0],
2655 2654 new_pkt->pkt_cdbp[1],
2656 2655 new_pkt->pkt_cdbp[2]));
2657 2656 goto again;
2658 2657 }
2659 2658 rval = 0;
2660 2659 VHCI_DEBUG(4, (CE_WARN, NULL,
2661 2660 "!vhci_do_prout 0x%x "
2662 2661 "(0x%x 0x%x 0x%x)",
2663 2662 SCBP_C(new_pkt),
2664 2663 new_pkt->pkt_cdbp[0],
2665 2664 new_pkt->pkt_cdbp[1],
2666 2665 new_pkt->pkt_cdbp[2]));
2667 2666 } else if (skey == KEY_ILLEGAL_REQUEST)
2668 2667 rval = VHCI_PGR_ILLEGALOP;
2669 2668 }
2670 2669 } else {
2671 2670 rval = 1;
2672 2671 }
2673 2672 scsi_destroy_pkt(new_pkt);
2674 2673 VHCI_DECR_PATH_CMDCOUNT(svp);
2675 2674 freerbuf(bp);
2676 2675 return (rval);
2677 2676 }
2678 2677
2679 2678 static void
2680 2679 vhci_run_cmd(void *arg)
2681 2680 {
2682 2681 struct scsi_pkt *pkt = (struct scsi_pkt *)arg;
2683 2682 struct scsi_pkt *tpkt;
2684 2683 scsi_vhci_priv_t *svp;
2685 2684 mdi_pathinfo_t *pip, *npip;
2686 2685 scsi_vhci_lun_t *vlun;
2687 2686 dev_info_t *cdip;
2688 2687 scsi_vhci_priv_t *nsvp;
2689 2688 int fail = 0;
2690 2689 int rval;
2691 2690 struct vhci_pkt *vpkt;
2692 2691 uchar_t cdb_1;
2693 2692 vhci_prout_t *prout;
2694 2693
2695 2694 vpkt = (struct vhci_pkt *)pkt->pkt_private;
2696 2695 tpkt = vpkt->vpkt_tgt_pkt;
2697 2696 pip = vpkt->vpkt_path;
2698 2697 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
2699 2698 if (svp == NULL) {
2700 2699 tpkt->pkt_reason = CMD_TRAN_ERR;
2701 2700 tpkt->pkt_statistics = STAT_ABORTED;
2702 2701 goto done;
2703 2702 }
2704 2703 vlun = svp->svp_svl;
2705 2704 prout = &vlun->svl_prout;
2706 2705 if (SCBP_C(pkt) != STATUS_GOOD)
2707 2706 fail++;
2708 2707 cdip = vlun->svl_dip;
2709 2708 pip = npip = NULL;
2710 2709 rval = mdi_select_path(cdip, NULL,
2711 2710 MDI_SELECT_ONLINE_PATH|MDI_SELECT_STANDBY_PATH, NULL, &npip);
2712 2711 if ((rval != MDI_SUCCESS) || (npip == NULL)) {
2713 2712 VHCI_DEBUG(4, (CE_NOTE, NULL,
2714 2713 "vhci_run_cmd: no path! 0x%p\n", (void *)svp));
2715 2714 tpkt->pkt_reason = CMD_TRAN_ERR;
2716 2715 tpkt->pkt_statistics = STAT_ABORTED;
2717 2716 goto done;
2718 2717 }
2719 2718
2720 2719 cdb_1 = vlun->svl_cdb[1];
2721 2720 vlun->svl_cdb[1] &= 0xe0;
2722 2721 vlun->svl_cdb[1] |= VHCI_PROUT_R_AND_IGNORE;
2723 2722
2724 2723 do {
2725 2724 nsvp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(npip);
2726 2725 if (nsvp == NULL) {
2727 2726 VHCI_DEBUG(4, (CE_NOTE, NULL,
2728 2727 "vhci_run_cmd: no "
2729 2728 "client priv! 0x%p offlined?\n",
2730 2729 (void *)npip));
2731 2730 goto next_path;
2732 2731 }
2733 2732 if (vlun->svl_first_path == npip) {
2734 2733 goto next_path;
2735 2734 } else {
2736 2735 if (vhci_do_prout(nsvp) != 1)
2737 2736 fail++;
2738 2737 }
2739 2738 next_path:
2740 2739 pip = npip;
2741 2740 rval = mdi_select_path(cdip, NULL,
2742 2741 MDI_SELECT_ONLINE_PATH|MDI_SELECT_STANDBY_PATH,
2743 2742 pip, &npip);
2744 2743 mdi_rele_path(pip);
2745 2744 } while ((rval == MDI_SUCCESS) && (npip != NULL));
2746 2745
2747 2746 vlun->svl_cdb[1] = cdb_1;
2748 2747
2749 2748 if (fail) {
2750 2749 VHCI_DEBUG(4, (CE_WARN, NULL, "%s%d: key registration failed, "
2751 2750 "couldn't be replicated on all paths",
2752 2751 ddi_driver_name(cdip), ddi_get_instance(cdip)));
2753 2752 vhci_print_prout_keys(vlun, "vhci_run_cmd: ");
2754 2753
2755 2754 if (SCBP_C(pkt) != STATUS_GOOD) {
2756 2755 tpkt->pkt_reason = CMD_TRAN_ERR;
2757 2756 tpkt->pkt_statistics = STAT_ABORTED;
2758 2757 }
2759 2758 } else {
2760 2759 vlun->svl_pgr_active = 1;
2761 2760 vhci_print_prout_keys(vlun, "vhci_run_cmd: before bcopy:");
2762 2761
2763 2762 bcopy((const void *)prout->service_key,
2764 2763 (void *)prout->active_service_key, MHIOC_RESV_KEY_SIZE);
2765 2764 bcopy((const void *)prout->res_key,
2766 2765 (void *)prout->active_res_key, MHIOC_RESV_KEY_SIZE);
2767 2766
2768 2767 vhci_print_prout_keys(vlun, "vhci_run_cmd: after bcopy:");
2769 2768 }
2770 2769 done:
2771 2770 if (SCBP_C(pkt) == STATUS_GOOD)
2772 2771 vlun->svl_first_path = NULL;
2773 2772
2774 2773 if (svp)
2775 2774 VHCI_DECR_PATH_CMDCOUNT(svp);
2776 2775
2777 2776 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) {
2778 2777 scsi_destroy_pkt(pkt);
2779 2778 vpkt->vpkt_hba_pkt = NULL;
2780 2779 if (vpkt->vpkt_path) {
2781 2780 mdi_rele_path(vpkt->vpkt_path);
2782 2781 vpkt->vpkt_path = NULL;
2783 2782 }
2784 2783 }
2785 2784
2786 2785 sema_v(&vlun->svl_pgr_sema);
2787 2786 /*
2788 2787 * The PROUT commands are not included in the automatic retry
2789 2788 * mechanism, therefore, vpkt_org_vpkt should never be set here.
2790 2789 */
2791 2790 ASSERT(vpkt->vpkt_org_vpkt == NULL);
2792 2791 scsi_hba_pkt_comp(tpkt);
2793 2792 }
2794 2793
2795 2794 /*
2796 2795 * Get the keys registered with this target. Since we will have
2797 2796 * registered the same key with multiple initiators, strip out
2798 2797 * any duplicate keys.
2799 2798 *
2800 2799 * The pointers which will be used to filter the registered keys from
2801 2800 * the device will be stored in filter_prin and filter_pkt. If the
2802 2801 * allocation length of the buffer was sufficient for the number of
2803 2802 * parameter data bytes available to be returned by the device then the
2804 2803 * key filtering will use the keylist returned from the original
2805 2804 * request. If the allocation length of the buffer was not sufficient,
2806 2805 * then the filtering will use the keylist returned from the request
2807 2806 * that is resent below.
2808 2807 *
2809 2808 * If the device returns an additional length field that is greater than
2810 2809 * the allocation length of the buffer, then allocate a new buffer which
2811 2810 * can accommodate the number of parameter data bytes available to be
2812 2811 * returned. Resend the scsi PRIN command, filter out the duplicate
2813 2812 * keys and return as many of the unique keys found that was originally
2814 2813 * requested and set the additional length field equal to the data bytes
2815 2814 * of unique reservation keys available to be returned.
2816 2815 *
2817 2816 * If the device returns an additional length field that is less than or
2818 2817 * equal to the allocation length of the buffer, then all the available
2819 2818 * keys registered were returned by the device. Filter out the
2820 2819 * duplicate keys and return all of the unique keys found and set the
2821 2820 * additional length field equal to the data bytes of the reservation
2822 2821 * keys to be returned.
2823 2822 */
2824 2823
2825 2824 #define VHCI_PRIN_HEADER_SZ (sizeof (prin->length) + sizeof (prin->generation))
2826 2825
2827 2826 static int
2828 2827 vhci_do_prin(struct vhci_pkt **intr_vpkt)
2829 2828 {
2830 2829 scsi_vhci_priv_t *svp;
2831 2830 struct vhci_pkt *vpkt = *intr_vpkt;
2832 2831 vhci_prin_readkeys_t *prin;
2833 2832 scsi_vhci_lun_t *vlun;
2834 2833 struct scsi_vhci *vhci = ADDR2VHCI(&vpkt->vpkt_tgt_pkt->pkt_address);
2835 2834
2836 2835 struct buf *new_bp = NULL;
2837 2836 struct scsi_pkt *new_pkt = NULL;
2838 2837 struct vhci_pkt *new_vpkt = NULL;
2839 2838 uint32_t needed_length;
2840 2839 int rval = VHCI_CMD_CMPLT;
2841 2840 uint32_t prin_length = 0;
2842 2841 uint32_t svl_prin_length = 0;
2843 2842
2844 2843 ASSERT(vpkt->vpkt_path);
2845 2844 svp = mdi_pi_get_vhci_private(vpkt->vpkt_path);
2846 2845 ASSERT(svp);
2847 2846 vlun = svp->svp_svl;
2848 2847 ASSERT(vlun);
2849 2848
2850 2849 /*
2851 2850 * If the caller only asked for an amount of data that would not
2852 2851 * be enough to include any key data it is likely that they will
2853 2852 * send the next command with a buffer size based on the information
2854 2853 * from this header. Doing recovery on this would be a duplication
2855 2854 * of efforts.
2856 2855 */
2857 2856 if (vpkt->vpkt_tgt_init_bp->b_bcount <= VHCI_PRIN_HEADER_SZ) {
2858 2857 rval = VHCI_CMD_CMPLT;
2859 2858 goto exit;
2860 2859 }
2861 2860
2862 2861 if (vpkt->vpkt_org_vpkt == NULL) {
2863 2862 /*
2864 2863 * Can fail as sleep is not allowed.
2865 2864 */
2866 2865 prin = (vhci_prin_readkeys_t *)
2867 2866 bp_mapin_common(vpkt->vpkt_tgt_init_bp, VM_NOSLEEP);
2868 2867 } else {
2869 2868 /*
2870 2869 * The retry buf doesn't need to be mapped in.
2871 2870 */
2872 2871 prin = (vhci_prin_readkeys_t *)
2873 2872 vpkt->vpkt_tgt_init_bp->b_un.b_daddr;
2874 2873 }
2875 2874
2876 2875 if (prin == NULL) {
2877 2876 VHCI_DEBUG(5, (CE_WARN, NULL,
2878 2877 "vhci_do_prin: bp_mapin_common failed."));
2879 2878 rval = VHCI_CMD_ERROR;
2880 2879 goto fail;
2881 2880 }
2882 2881
2883 2882 prin_length = BE_32(prin->length);
2884 2883
2885 2884 /*
2886 2885 * According to SPC-3r22, sec 4.3.4.6: "If the amount of
2887 2886 * information to be transferred exceeds the maximum value
2888 2887 * that the ALLOCATION LENGTH field is capable of specifying,
2889 2888 * the device server shall...terminate the command with CHECK
2890 2889 * CONDITION status". The ALLOCATION LENGTH field of the
2891 2890 * PERSISTENT RESERVE IN command is 2 bytes. We should never
2892 2891 * get here with an ADDITIONAL LENGTH greater than 0xFFFF
2893 2892 * so if we do, then it is an error!
2894 2893 */
2895 2894
2896 2895
2897 2896 if ((prin_length + VHCI_PRIN_HEADER_SZ) > 0xFFFF) {
2898 2897 VHCI_DEBUG(5, (CE_NOTE, NULL,
2899 2898 "vhci_do_prin: Device returned invalid "
2900 2899 "length 0x%x\n", prin_length));
2901 2900 rval = VHCI_CMD_ERROR;
2902 2901 goto fail;
2903 2902 }
2904 2903 needed_length = prin_length + VHCI_PRIN_HEADER_SZ;
2905 2904
2906 2905 /*
2907 2906 * If prin->length is greater than the byte count allocated in the
2908 2907 * original buffer, then resend the request with enough buffer
2909 2908 * allocated to get all of the available registered keys.
2910 2909 */
2911 2910 if ((vpkt->vpkt_tgt_init_bp->b_bcount < needed_length) &&
2912 2911 (vpkt->vpkt_org_vpkt == NULL)) {
2913 2912
2914 2913 new_pkt = vhci_create_retry_pkt(vpkt);
2915 2914 if (new_pkt == NULL) {
2916 2915 rval = VHCI_CMD_ERROR;
2917 2916 goto fail;
2918 2917 }
2919 2918 new_vpkt = TGTPKT2VHCIPKT(new_pkt);
2920 2919
2921 2920 /*
2922 2921 * This is the buf with buffer pointer
2923 2922 * where the prin readkeys will be
2924 2923 * returned from the device
2925 2924 */
2926 2925 new_bp = scsi_alloc_consistent_buf(&svp->svp_psd->sd_address,
2927 2926 NULL, needed_length, B_READ, NULL_FUNC, NULL);
2928 2927 if ((new_bp == NULL) || (new_bp->b_un.b_addr == NULL)) {
2929 2928 if (new_bp) {
2930 2929 scsi_free_consistent_buf(new_bp);
2931 2930 }
2932 2931 vhci_scsi_destroy_pkt(&new_pkt->pkt_address, new_pkt);
2933 2932 rval = VHCI_CMD_ERROR;
2934 2933 goto fail;
2935 2934 }
2936 2935 new_bp->b_bcount = needed_length;
2937 2936 new_pkt->pkt_cdbp[7] = (uchar_t)(needed_length >> 8);
2938 2937 new_pkt->pkt_cdbp[8] = (uchar_t)needed_length;
2939 2938
2940 2939 rval = VHCI_CMD_RETRY;
2941 2940
2942 2941 new_vpkt->vpkt_tgt_init_bp = new_bp;
2943 2942 }
2944 2943
2945 2944 if (rval == VHCI_CMD_RETRY) {
2946 2945
2947 2946 /*
2948 2947 * There were more keys then the original request asked for.
2949 2948 */
2950 2949 mdi_pathinfo_t *path_holder = vpkt->vpkt_path;
2951 2950
2952 2951 /*
2953 2952 * Release the old path because it does not matter which path
2954 2953 * this command is sent down. This allows the normal bind
2955 2954 * transport mechanism to be used.
2956 2955 */
2957 2956 if (vpkt->vpkt_path != NULL) {
2958 2957 mdi_rele_path(vpkt->vpkt_path);
2959 2958 vpkt->vpkt_path = NULL;
2960 2959 }
2961 2960
2962 2961 /*
2963 2962 * Dispatch the retry command
2964 2963 */
2965 2964 if (taskq_dispatch(vhci->vhci_taskq, vhci_dispatch_scsi_start,
2966 2965 (void *) new_vpkt, KM_NOSLEEP) == NULL) {
2967 2966 if (path_holder) {
2968 2967 vpkt->vpkt_path = path_holder;
2969 2968 mdi_hold_path(path_holder);
2970 2969 }
2971 2970 scsi_free_consistent_buf(new_bp);
2972 2971 vhci_scsi_destroy_pkt(&new_pkt->pkt_address, new_pkt);
2973 2972 rval = VHCI_CMD_ERROR;
2974 2973 goto fail;
2975 2974 }
2976 2975
2977 2976 /*
2978 2977 * If we return VHCI_CMD_RETRY, that means the caller
2979 2978 * is going to bail and wait for the reissued command
2980 2979 * to complete. In that case, we need to decrement
2981 2980 * the path command count right now. In any other
2982 2981 * case, it'll be decremented by the caller.
2983 2982 */
2984 2983 VHCI_DECR_PATH_CMDCOUNT(svp);
2985 2984 goto exit;
2986 2985
2987 2986 }
2988 2987
2989 2988 if (rval == VHCI_CMD_CMPLT) {
2990 2989 /*
2991 2990 * The original request got all of the keys or the recovery
2992 2991 * packet returns.
2993 2992 */
2994 2993 int new;
2995 2994 int old;
2996 2995 int num_keys = prin_length / MHIOC_RESV_KEY_SIZE;
2997 2996
2998 2997 VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_do_prin: %d keys read\n",
2999 2998 num_keys));
3000 2999
3001 3000 #ifdef DEBUG
3002 3001 VHCI_DEBUG(5, (CE_NOTE, NULL, "vhci_do_prin: from storage\n"));
3003 3002 if (vhci_debug == 5)
3004 3003 vhci_print_prin_keys(prin, num_keys);
3005 3004 VHCI_DEBUG(5, (CE_NOTE, NULL,
3006 3005 "vhci_do_prin: MPxIO old keys:\n"));
3007 3006 if (vhci_debug == 5)
3008 3007 vhci_print_prin_keys(&vlun->svl_prin, num_keys);
3009 3008 #endif
3010 3009
3011 3010 /*
3012 3011 * Filter out all duplicate keys returned from the device
3013 3012 * We know that we use a different key for every host, so we
3014 3013 * can simply strip out duplicates. Otherwise we would need to
3015 3014 * do more bookkeeping to figure out which keys to strip out.
3016 3015 */
3017 3016
3018 3017 new = 0;
3019 3018
3020 3019 /*
3021 3020 * If we got at least 1 key copy it.
3022 3021 */
3023 3022 if (num_keys > 0) {
3024 3023 vlun->svl_prin.keylist[0] = prin->keylist[0];
3025 3024 new++;
3026 3025 }
3027 3026
3028 3027 /*
3029 3028 * find next unique key.
3030 3029 */
3031 3030 for (old = 1; old < num_keys; old++) {
3032 3031 int j;
3033 3032 int match = 0;
3034 3033
3035 3034 if (new >= VHCI_NUM_RESV_KEYS)
3036 3035 break;
3037 3036 for (j = 0; j < new; j++) {
3038 3037 if (bcmp(&prin->keylist[old],
3039 3038 &vlun->svl_prin.keylist[j],
3040 3039 sizeof (mhioc_resv_key_t)) == 0) {
3041 3040 match = 1;
3042 3041 break;
3043 3042 }
3044 3043 }
3045 3044 if (!match) {
3046 3045 vlun->svl_prin.keylist[new] =
3047 3046 prin->keylist[old];
3048 3047 new++;
3049 3048 }
3050 3049 }
3051 3050
3052 3051 /* Stored Big Endian */
3053 3052 vlun->svl_prin.generation = prin->generation;
3054 3053 svl_prin_length = new * sizeof (mhioc_resv_key_t);
3055 3054 /* Stored Big Endian */
3056 3055 vlun->svl_prin.length = BE_32(svl_prin_length);
3057 3056 svl_prin_length += VHCI_PRIN_HEADER_SZ;
3058 3057
3059 3058 /*
3060 3059 * If we arrived at this point after issuing a retry, make sure
3061 3060 * that we put everything back the way it originally was so
3062 3061 * that the target driver can complete the command correctly.
3063 3062 */
3064 3063 if (vpkt->vpkt_org_vpkt != NULL) {
3065 3064 new_bp = vpkt->vpkt_tgt_init_bp;
3066 3065
3067 3066 scsi_free_consistent_buf(new_bp);
3068 3067
3069 3068 vpkt = vhci_sync_retry_pkt(vpkt);
3070 3069 *intr_vpkt = vpkt;
3071 3070
3072 3071 /*
3073 3072 * Make sure the original buffer is mapped into kernel
3074 3073 * space before we try to copy the filtered keys into
3075 3074 * it.
3076 3075 */
3077 3076 prin = (vhci_prin_readkeys_t *)bp_mapin_common(
3078 3077 vpkt->vpkt_tgt_init_bp, VM_NOSLEEP);
3079 3078 }
3080 3079
3081 3080 /*
3082 3081 * Now copy the desired number of prin keys into the original
3083 3082 * target buffer.
3084 3083 */
3085 3084 if (svl_prin_length <= vpkt->vpkt_tgt_init_bp->b_bcount) {
3086 3085 /*
3087 3086 * It is safe to return all of the available unique
3088 3087 * keys
3089 3088 */
3090 3089 bcopy(&vlun->svl_prin, prin, svl_prin_length);
3091 3090 } else {
3092 3091 /*
3093 3092 * Not all of the available keys were requested by the
3094 3093 * original command.
3095 3094 */
3096 3095 bcopy(&vlun->svl_prin, prin,
3097 3096 vpkt->vpkt_tgt_init_bp->b_bcount);
3098 3097 }
3099 3098 #ifdef DEBUG
3100 3099 VHCI_DEBUG(5, (CE_NOTE, NULL,
3101 3100 "vhci_do_prin: To Application:\n"));
3102 3101 if (vhci_debug == 5)
3103 3102 vhci_print_prin_keys(prin, new);
3104 3103 VHCI_DEBUG(5, (CE_NOTE, NULL,
3105 3104 "vhci_do_prin: MPxIO new keys:\n"));
3106 3105 if (vhci_debug == 5)
3107 3106 vhci_print_prin_keys(&vlun->svl_prin, new);
3108 3107 #endif
3109 3108 }
3110 3109 fail:
3111 3110 if (rval == VHCI_CMD_ERROR) {
3112 3111 /*
3113 3112 * If we arrived at this point after issuing a
3114 3113 * retry, make sure that we put everything back
3115 3114 * the way it originally was so that ssd can
3116 3115 * complete the command correctly.
3117 3116 */
3118 3117
3119 3118 if (vpkt->vpkt_org_vpkt != NULL) {
3120 3119 new_bp = vpkt->vpkt_tgt_init_bp;
3121 3120 if (new_bp != NULL) {
3122 3121 scsi_free_consistent_buf(new_bp);
3123 3122 }
3124 3123
3125 3124 new_vpkt = vpkt;
3126 3125 vpkt = vpkt->vpkt_org_vpkt;
3127 3126
3128 3127 vhci_scsi_destroy_pkt(&svp->svp_psd->sd_address,
3129 3128 new_vpkt->vpkt_tgt_pkt);
3130 3129 }
3131 3130
3132 3131 /*
3133 3132 * Mark this command completion as having an error so that
3134 3133 * ssd will retry the command.
3135 3134 */
3136 3135
3137 3136 vpkt->vpkt_tgt_pkt->pkt_reason = CMD_ABORTED;
3138 3137 vpkt->vpkt_tgt_pkt->pkt_statistics |= STAT_ABORTED;
3139 3138
3140 3139 rval = VHCI_CMD_CMPLT;
3141 3140 }
3142 3141 exit:
3143 3142 /*
3144 3143 * Make sure that the semaphore is only released once.
3145 3144 */
3146 3145 if (rval == VHCI_CMD_CMPLT) {
3147 3146 sema_v(&vlun->svl_pgr_sema);
3148 3147 }
3149 3148
3150 3149 return (rval);
3151 3150 }
3152 3151
3153 3152 static void
3154 3153 vhci_intr(struct scsi_pkt *pkt)
3155 3154 {
3156 3155 struct vhci_pkt *vpkt = (struct vhci_pkt *)pkt->pkt_private;
3157 3156 struct scsi_pkt *tpkt;
3158 3157 scsi_vhci_priv_t *svp;
3159 3158 scsi_vhci_lun_t *vlun;
3160 3159 int rval, held;
3161 3160 struct scsi_failover_ops *fops;
3162 3161 uint8_t *sns, skey, asc, ascq;
3163 3162 mdi_pathinfo_t *lpath;
3164 3163 static char *timeout_err = "Command Timeout";
3165 3164 static char *parity_err = "Parity Error";
3166 3165 char *err_str = NULL;
3167 3166 dev_info_t *vdip, *cdip;
3168 3167 char *cpath;
3169 3168
3170 3169 ASSERT(vpkt != NULL);
3171 3170 tpkt = vpkt->vpkt_tgt_pkt;
3172 3171 ASSERT(tpkt != NULL);
3173 3172 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(vpkt->vpkt_path);
3174 3173 ASSERT(svp != NULL);
3175 3174 vlun = svp->svp_svl;
3176 3175 ASSERT(vlun != NULL);
3177 3176 lpath = vpkt->vpkt_path;
3178 3177
3179 3178 /*
3180 3179 * sync up the target driver's pkt with the pkt that
3181 3180 * we actually used
3182 3181 */
3183 3182 *(tpkt->pkt_scbp) = *(pkt->pkt_scbp);
3184 3183 tpkt->pkt_resid = pkt->pkt_resid;
3185 3184 tpkt->pkt_state = pkt->pkt_state;
3186 3185 tpkt->pkt_statistics = pkt->pkt_statistics;
3187 3186 tpkt->pkt_reason = pkt->pkt_reason;
3188 3187
3189 3188 /* Return path_instance information back to the target driver. */
3190 3189 if (scsi_pkt_allocated_correctly(tpkt)) {
3191 3190 if (scsi_pkt_allocated_correctly(pkt)) {
3192 3191 /*
3193 3192 * If both packets were correctly allocated,
3194 3193 * return path returned by pHCI.
3195 3194 */
3196 3195 tpkt->pkt_path_instance = pkt->pkt_path_instance;
3197 3196 } else {
3198 3197 /* Otherwise return path of pHCI we used */
3199 3198 tpkt->pkt_path_instance =
3200 3199 mdi_pi_get_path_instance(lpath);
3201 3200 }
3202 3201 }
3203 3202
3204 3203 if (pkt->pkt_cdbp[0] == SCMD_PROUT &&
3205 3204 ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) ||
3206 3205 ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_R_AND_IGNORE)) {
3207 3206 if ((SCBP_C(pkt) != STATUS_GOOD) ||
3208 3207 (pkt->pkt_reason != CMD_CMPLT)) {
3209 3208 sema_v(&vlun->svl_pgr_sema);
3210 3209 }
3211 3210 } else if (pkt->pkt_cdbp[0] == SCMD_PRIN) {
3212 3211 if (pkt->pkt_reason != CMD_CMPLT ||
3213 3212 (SCBP_C(pkt) != STATUS_GOOD)) {
3214 3213 sema_v(&vlun->svl_pgr_sema);
3215 3214 }
3216 3215 }
3217 3216
3218 3217 switch (pkt->pkt_reason) {
3219 3218 case CMD_CMPLT:
3220 3219 /*
3221 3220 * cmd completed successfully, check for scsi errors
3222 3221 */
3223 3222 switch (*(pkt->pkt_scbp)) {
3224 3223 case STATUS_CHECK:
3225 3224 if (pkt->pkt_state & STATE_ARQ_DONE) {
3226 3225 sns = (uint8_t *)
3227 3226 &(((struct scsi_arq_status *)(uintptr_t)
3228 3227 (pkt->pkt_scbp))->sts_sensedata);
3229 3228 skey = scsi_sense_key(sns);
3230 3229 asc = scsi_sense_asc(sns);
3231 3230 ascq = scsi_sense_ascq(sns);
3232 3231 fops = vlun->svl_fops;
3233 3232 ASSERT(fops != NULL);
3234 3233 VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_intr: "
3235 3234 "Received sns key %x esc %x escq %x\n",
3236 3235 skey, asc, ascq));
3237 3236
3238 3237 if (vlun->svl_waiting_for_activepath == 1) {
3239 3238 /*
3240 3239 * if we are here it means we are
3241 3240 * in the midst of a probe/attach
3242 3241 * through a passive path; this
3243 3242 * case is exempt from sense analysis
3244 3243 * for detection of ext. failover
3245 3244 * because that would unnecessarily
3246 3245 * increase attach time.
3247 3246 */
3248 3247 bcopy(pkt->pkt_scbp, tpkt->pkt_scbp,
3249 3248 vpkt->vpkt_tgt_init_scblen);
3250 3249 break;
3251 3250 }
3252 3251 if (asc == VHCI_SCSI_PERR) {
3253 3252 /*
3254 3253 * parity error
3255 3254 */
3256 3255 err_str = parity_err;
3257 3256 bcopy(pkt->pkt_scbp, tpkt->pkt_scbp,
3258 3257 vpkt->vpkt_tgt_init_scblen);
3259 3258 break;
3260 3259 }
3261 3260 rval = fops->sfo_analyze_sense(svp->svp_psd,
3262 3261 sns, vlun->svl_fops_ctpriv);
3263 3262 if ((rval == SCSI_SENSE_NOFAILOVER) ||
3264 3263 (rval == SCSI_SENSE_UNKNOWN) ||
3265 3264 (rval == SCSI_SENSE_NOT_READY)) {
3266 3265 bcopy(pkt->pkt_scbp, tpkt->pkt_scbp,
3267 3266 vpkt->vpkt_tgt_init_scblen);
3268 3267 break;
3269 3268 } else if (rval == SCSI_SENSE_STATE_CHANGED) {
3270 3269 struct scsi_vhci *vhci;
3271 3270 vhci = ADDR2VHCI(&tpkt->pkt_address);
3272 3271 VHCI_HOLD_LUN(vlun, VH_NOSLEEP, held);
3273 3272 if (!held) {
3274 3273 /*
3275 3274 * looks like some other thread
3276 3275 * has already detected this
3277 3276 * condition
3278 3277 */
3279 3278 tpkt->pkt_state &=
3280 3279 ~STATE_ARQ_DONE;
3281 3280 *(tpkt->pkt_scbp) =
3282 3281 STATUS_BUSY;
3283 3282 break;
3284 3283 }
3285 3284 (void) taskq_dispatch(
3286 3285 vhci->vhci_update_pathstates_taskq,
3287 3286 vhci_update_pathstates,
3288 3287 (void *)vlun, KM_SLEEP);
3289 3288 } else {
3290 3289 /*
3291 3290 * externally initiated failover
3292 3291 * has occurred or is in progress
3293 3292 */
3294 3293 VHCI_HOLD_LUN(vlun, VH_NOSLEEP, held);
3295 3294 if (!held) {
3296 3295 /*
3297 3296 * looks like some other thread
3298 3297 * has already detected this
3299 3298 * condition
3300 3299 */
3301 3300 tpkt->pkt_state &=
3302 3301 ~STATE_ARQ_DONE;
3303 3302 *(tpkt->pkt_scbp) =
3304 3303 STATUS_BUSY;
3305 3304 break;
3306 3305 } else {
3307 3306 rval = vhci_handle_ext_fo
3308 3307 (pkt, rval);
3309 3308 if (rval == BUSY_RETURN) {
3310 3309 tpkt->pkt_state &=
3311 3310 ~STATE_ARQ_DONE;
3312 3311 *(tpkt->pkt_scbp) =
3313 3312 STATUS_BUSY;
3314 3313 break;
3315 3314 }
3316 3315 bcopy(pkt->pkt_scbp,
3317 3316 tpkt->pkt_scbp,
3318 3317 vpkt->vpkt_tgt_init_scblen);
3319 3318 break;
3320 3319 }
3321 3320 }
3322 3321 }
3323 3322 break;
3324 3323
3325 3324 /*
3326 3325 * If this is a good SCSI-II RELEASE cmd completion then restore
3327 3326 * the load balancing policy and reset VLUN_RESERVE_ACTIVE_FLG.
3328 3327 * If this is a good SCSI-II RESERVE cmd completion then set
3329 3328 * VLUN_RESERVE_ACTIVE_FLG.
3330 3329 */
3331 3330 case STATUS_GOOD:
3332 3331 if ((pkt->pkt_cdbp[0] == SCMD_RELEASE) ||
3333 3332 (pkt->pkt_cdbp[0] == SCMD_RELEASE_G1)) {
3334 3333 (void) mdi_set_lb_policy(vlun->svl_dip,
3335 3334 vlun->svl_lb_policy_save);
3336 3335 vlun->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG;
3337 3336 VHCI_DEBUG(1, (CE_WARN, NULL,
3338 3337 "!vhci_intr: vlun 0x%p release path 0x%p",
3339 3338 (void *)vlun, (void *)vpkt->vpkt_path));
3340 3339 }
3341 3340
3342 3341 if ((pkt->pkt_cdbp[0] == SCMD_RESERVE) ||
3343 3342 (pkt->pkt_cdbp[0] == SCMD_RESERVE_G1)) {
3344 3343 vlun->svl_flags |= VLUN_RESERVE_ACTIVE_FLG;
3345 3344 vlun->svl_resrv_pip = vpkt->vpkt_path;
3346 3345 VHCI_DEBUG(1, (CE_WARN, NULL,
3347 3346 "!vhci_intr: vlun 0x%p reserved path 0x%p",
3348 3347 (void *)vlun, (void *)vpkt->vpkt_path));
3349 3348 }
3350 3349 break;
3351 3350
3352 3351 case STATUS_RESERVATION_CONFLICT:
3353 3352 VHCI_DEBUG(1, (CE_WARN, NULL,
3354 3353 "!vhci_intr: vlun 0x%p "
3355 3354 "reserve conflict on path 0x%p",
3356 3355 (void *)vlun, (void *)vpkt->vpkt_path));
3357 3356 /* FALLTHROUGH */
3358 3357 default:
3359 3358 break;
3360 3359 }
3361 3360
3362 3361 /*
3363 3362 * Update I/O completion statistics for the path
3364 3363 */
3365 3364 mdi_pi_kstat_iosupdate(vpkt->vpkt_path, vpkt->vpkt_tgt_init_bp);
3366 3365
3367 3366 /*
3368 3367 * Command completed successfully, release the dma binding and
3369 3368 * destroy the transport side of the packet.
3370 3369 */
3371 3370 if ((pkt->pkt_cdbp[0] == SCMD_PROUT) &&
3372 3371 (((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_REGISTER) ||
3373 3372 ((pkt->pkt_cdbp[1] & 0x1f) == VHCI_PROUT_R_AND_IGNORE))) {
3374 3373 if (SCBP_C(pkt) == STATUS_GOOD) {
3375 3374 ASSERT(vlun->svl_taskq);
3376 3375 svp->svp_last_pkt_reason = pkt->pkt_reason;
3377 3376 (void) taskq_dispatch(vlun->svl_taskq,
3378 3377 vhci_run_cmd, pkt, KM_SLEEP);
3379 3378 return;
3380 3379 }
3381 3380 }
3382 3381 if ((SCBP_C(pkt) == STATUS_GOOD) &&
3383 3382 (pkt->pkt_cdbp[0] == SCMD_PRIN) && vpkt->vpkt_tgt_init_bp) {
3384 3383 /*
3385 3384 * If the action (value in byte 1 of the cdb) is zero,
3386 3385 * we're reading keys, and that's the only condition
3387 3386 * where we need to be concerned with filtering keys
3388 3387 * and potential retries. Otherwise, we simply signal
3389 3388 * the semaphore and move on.
3390 3389 */
3391 3390 if (pkt->pkt_cdbp[1] == 0) {
3392 3391 /*
3393 3392 * If this is the completion of an internal
3394 3393 * retry then we need to make sure that the
3395 3394 * pkt and tpkt pointers are readjusted so
3396 3395 * the calls to scsi_destroy_pkt and pkt_comp
3397 3396 * below work * correctly.
3398 3397 */
3399 3398 if (vpkt->vpkt_org_vpkt != NULL) {
3400 3399 pkt = vpkt->vpkt_org_vpkt->vpkt_hba_pkt;
3401 3400 tpkt = vpkt->vpkt_org_vpkt->
3402 3401 vpkt_tgt_pkt;
3403 3402
3404 3403 /*
3405 3404 * If this command was issued through
3406 3405 * the taskq then we need to clear
3407 3406 * this flag for proper processing in
3408 3407 * the case of a retry from the target
3409 3408 * driver.
3410 3409 */
3411 3410 vpkt->vpkt_state &=
3412 3411 ~VHCI_PKT_THRU_TASKQ;
3413 3412 }
3414 3413
3415 3414 /*
3416 3415 * if vhci_do_prin returns VHCI_CMD_CMPLT then
3417 3416 * vpkt will contain the address of the
3418 3417 * original vpkt
3419 3418 */
3420 3419 if (vhci_do_prin(&vpkt) == VHCI_CMD_RETRY) {
3421 3420 /*
3422 3421 * The command has been resent to get
3423 3422 * all the keys from the device. Don't
3424 3423 * complete the command with ssd until
3425 3424 * the retry completes.
3426 3425 */
3427 3426 return;
3428 3427 }
3429 3428 } else {
3430 3429 sema_v(&vlun->svl_pgr_sema);
3431 3430 }
3432 3431 }
3433 3432
3434 3433 break;
3435 3434
3436 3435 case CMD_TIMEOUT:
3437 3436 if ((pkt->pkt_statistics &
3438 3437 (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
3439 3438
3440 3439 VHCI_DEBUG(1, (CE_NOTE, NULL,
3441 3440 "!scsi vhci timeout invoked\n"));
3442 3441
3443 3442 (void) vhci_recovery_reset(vlun, &pkt->pkt_address,
3444 3443 FALSE, VHCI_DEPTH_ALL);
3445 3444 }
3446 3445 MDI_PI_ERRSTAT(lpath, MDI_PI_TRANSERR);
3447 3446 tpkt->pkt_statistics |= STAT_ABORTED;
3448 3447 err_str = timeout_err;
3449 3448 break;
3450 3449
3451 3450 case CMD_TRAN_ERR:
3452 3451 /*
3453 3452 * This status is returned if the transport has sent the cmd
3454 3453 * down the link to the target and then some error occurs.
3455 3454 * In case of SCSI-II RESERVE cmd, we don't know if the
3456 3455 * reservation been accepted by the target or not, so we need
3457 3456 * to clear the reservation.
3458 3457 */
3459 3458 if ((pkt->pkt_cdbp[0] == SCMD_RESERVE) ||
3460 3459 (pkt->pkt_cdbp[0] == SCMD_RESERVE_G1)) {
3461 3460 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_intr received"
3462 3461 " cmd_tran_err for scsi-2 reserve cmd\n"));
3463 3462 if (!vhci_recovery_reset(vlun, &pkt->pkt_address,
3464 3463 TRUE, VHCI_DEPTH_TARGET)) {
3465 3464 VHCI_DEBUG(1, (CE_WARN, NULL,
3466 3465 "!vhci_intr cmd_tran_err reset failed!"));
3467 3466 }
3468 3467 }
3469 3468 break;
3470 3469
3471 3470 case CMD_DEV_GONE:
3472 3471 /*
3473 3472 * If this is the last path then report CMD_DEV_GONE to the
3474 3473 * target driver, otherwise report BUSY to triggger retry.
3475 3474 */
3476 3475 if (vlun->svl_dip &&
3477 3476 (mdi_client_get_path_count(vlun->svl_dip) <= 1)) {
3478 3477 struct scsi_vhci *vhci;
3479 3478 vhci = ADDR2VHCI(&tpkt->pkt_address);
3480 3479 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_intr received "
3481 3480 "cmd_dev_gone on last path\n"));
3482 3481 (void) vhci_invalidate_mpapi_lu(vhci, vlun);
3483 3482 break;
3484 3483 }
3485 3484
3486 3485 /* Report CMD_CMPLT-with-BUSY to cause retry. */
3487 3486 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_intr received "
3488 3487 "cmd_dev_gone\n"));
3489 3488 tpkt->pkt_reason = CMD_CMPLT;
3490 3489 tpkt->pkt_state = STATE_GOT_BUS |
3491 3490 STATE_GOT_TARGET | STATE_SENT_CMD |
3492 3491 STATE_GOT_STATUS;
3493 3492 *(tpkt->pkt_scbp) = STATUS_BUSY;
3494 3493 break;
3495 3494
3496 3495 default:
3497 3496 break;
3498 3497 }
3499 3498
3500 3499 /*
3501 3500 * SCSI-II RESERVE cmd has been serviced by the lower layers clear
3502 3501 * the flag so the lun is not QUIESCED any longer.
3503 3502 * Also clear the VHCI_PKT_THRU_TASKQ flag, to ensure that if this pkt
3504 3503 * is retried, a taskq shall again be dispatched to service it. Else
3505 3504 * it may lead to a system hang if the retry is within interrupt
3506 3505 * context.
3507 3506 */
3508 3507 if ((pkt->pkt_cdbp[0] == SCMD_RESERVE) ||
3509 3508 (pkt->pkt_cdbp[0] == SCMD_RESERVE_G1)) {
3510 3509 vlun->svl_flags &= ~VLUN_QUIESCED_FLG;
3511 3510 vpkt->vpkt_state &= ~VHCI_PKT_THRU_TASKQ;
3512 3511 }
3513 3512
3514 3513 /*
3515 3514 * vpkt_org_vpkt should always be NULL here if the retry command
3516 3515 * has been successfully processed. If vpkt_org_vpkt != NULL at
3517 3516 * this point, it is an error so restore the original vpkt and
3518 3517 * return an error to the target driver so it can retry the
3519 3518 * command as appropriate.
3520 3519 */
3521 3520 if (vpkt->vpkt_org_vpkt != NULL) {
3522 3521 struct vhci_pkt *new_vpkt = vpkt;
3523 3522 vpkt = vpkt->vpkt_org_vpkt;
3524 3523
3525 3524 vhci_scsi_destroy_pkt(&svp->svp_psd->sd_address,
3526 3525 new_vpkt->vpkt_tgt_pkt);
3527 3526
3528 3527 /*
3529 3528 * Mark this command completion as having an error so that
3530 3529 * ssd will retry the command.
3531 3530 */
3532 3531 vpkt->vpkt_tgt_pkt->pkt_reason = CMD_ABORTED;
3533 3532 vpkt->vpkt_tgt_pkt->pkt_statistics |= STAT_ABORTED;
3534 3533
3535 3534 pkt = vpkt->vpkt_hba_pkt;
3536 3535 tpkt = vpkt->vpkt_tgt_pkt;
3537 3536 }
3538 3537
3539 3538 if ((err_str != NULL) && (pkt->pkt_reason !=
3540 3539 svp->svp_last_pkt_reason)) {
3541 3540 cdip = vlun->svl_dip;
3542 3541 vdip = ddi_get_parent(cdip);
3543 3542 cpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3544 3543 vhci_log(CE_WARN, vdip, "!%s (%s%d): %s on path %s",
3545 3544 ddi_pathname(cdip, cpath), ddi_driver_name(cdip),
3546 3545 ddi_get_instance(cdip), err_str,
3547 3546 mdi_pi_spathname(vpkt->vpkt_path));
3548 3547 kmem_free(cpath, MAXPATHLEN);
3549 3548 }
3550 3549 svp->svp_last_pkt_reason = pkt->pkt_reason;
3551 3550 VHCI_DECR_PATH_CMDCOUNT(svp);
3552 3551
3553 3552 /*
3554 3553 * For PARTIAL_DMA, vhci should not free the path.
3555 3554 * Target driver will call into vhci_scsi_dmafree or
3556 3555 * destroy pkt to release this path.
3557 3556 */
3558 3557 if ((vpkt->vpkt_flags & CFLAG_DMA_PARTIAL) == 0) {
3559 3558 scsi_destroy_pkt(pkt);
3560 3559 vpkt->vpkt_hba_pkt = NULL;
3561 3560 if (vpkt->vpkt_path) {
3562 3561 mdi_rele_path(vpkt->vpkt_path);
3563 3562 vpkt->vpkt_path = NULL;
3564 3563 }
3565 3564 }
3566 3565
3567 3566 scsi_hba_pkt_comp(tpkt);
3568 3567 }
3569 3568
3570 3569 /*
3571 3570 * two possibilities: (1) failover has completed
3572 3571 * or (2) is in progress; update our path states for
3573 3572 * the former case; for the latter case,
3574 3573 * initiate a scsi_watch request to
3575 3574 * determine when failover completes - vlun is HELD
3576 3575 * until failover completes; BUSY is returned to upper
3577 3576 * layer in both the cases
3578 3577 */
3579 3578 static int
3580 3579 vhci_handle_ext_fo(struct scsi_pkt *pkt, int fostat)
3581 3580 {
3582 3581 struct vhci_pkt *vpkt = (struct vhci_pkt *)pkt->pkt_private;
3583 3582 struct scsi_pkt *tpkt;
3584 3583 scsi_vhci_priv_t *svp;
3585 3584 scsi_vhci_lun_t *vlun;
3586 3585 struct scsi_vhci *vhci;
3587 3586 scsi_vhci_swarg_t *swarg;
3588 3587 char *path;
3589 3588
3590 3589 ASSERT(vpkt != NULL);
3591 3590 tpkt = vpkt->vpkt_tgt_pkt;
3592 3591 ASSERT(tpkt != NULL);
3593 3592 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(vpkt->vpkt_path);
3594 3593 ASSERT(svp != NULL);
3595 3594 vlun = svp->svp_svl;
3596 3595 ASSERT(vlun != NULL);
3597 3596 ASSERT(VHCI_LUN_IS_HELD(vlun));
3598 3597
3599 3598 vhci = ADDR2VHCI(&tpkt->pkt_address);
3600 3599
3601 3600 if (fostat == SCSI_SENSE_INACTIVE) {
3602 3601 VHCI_DEBUG(1, (CE_NOTE, NULL, "!Failover "
3603 3602 "detected for %s; updating path states...\n",
3604 3603 vlun->svl_lun_wwn));
3605 3604 /*
3606 3605 * set the vlun flag to indicate to the task that the target
3607 3606 * port group needs updating
3608 3607 */
3609 3608 vlun->svl_flags |= VLUN_UPDATE_TPG;
3610 3609 (void) taskq_dispatch(vhci->vhci_update_pathstates_taskq,
3611 3610 vhci_update_pathstates, (void *)vlun, KM_SLEEP);
3612 3611 } else {
3613 3612 path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3614 3613 vhci_log(CE_NOTE, ddi_get_parent(vlun->svl_dip),
3615 3614 "!%s (%s%d): Waiting for externally initiated failover "
3616 3615 "to complete", ddi_pathname(vlun->svl_dip, path),
3617 3616 ddi_driver_name(vlun->svl_dip),
3618 3617 ddi_get_instance(vlun->svl_dip));
3619 3618 kmem_free(path, MAXPATHLEN);
3620 3619 swarg = kmem_alloc(sizeof (*swarg), KM_NOSLEEP);
3621 3620 if (swarg == NULL) {
3622 3621 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_handle_ext_fo: "
3623 3622 "request packet allocation for %s failed....\n",
3624 3623 vlun->svl_lun_wwn));
3625 3624 VHCI_RELEASE_LUN(vlun);
3626 3625 return (PKT_RETURN);
3627 3626 }
3628 3627 swarg->svs_svp = svp;
3629 3628 swarg->svs_tos = gethrtime();
3630 3629 swarg->svs_pi = vpkt->vpkt_path;
3631 3630 swarg->svs_release_lun = 0;
3632 3631 swarg->svs_done = 0;
3633 3632 /*
3634 3633 * place a hold on the path...we don't want it to
3635 3634 * vanish while scsi_watch is in progress
3636 3635 */
3637 3636 mdi_hold_path(vpkt->vpkt_path);
3638 3637 svp->svp_sw_token = scsi_watch_request_submit(svp->svp_psd,
3639 3638 VHCI_FOWATCH_INTERVAL, SENSE_LENGTH, vhci_efo_watch_cb,
3640 3639 (caddr_t)swarg);
3641 3640 }
3642 3641 return (BUSY_RETURN);
3643 3642 }
3644 3643
3645 3644 /*
3646 3645 * vhci_efo_watch_cb:
3647 3646 * Callback from scsi_watch request to check the failover status.
3648 3647 * Completion is either due to successful failover or timeout.
3649 3648 * Upon successful completion, vhci_update_path_states is called.
3650 3649 * For timeout condition, vhci_efo_done is called.
3651 3650 * Always returns 0 to scsi_watch to keep retrying till vhci_efo_done
3652 3651 * terminates this request properly in a separate thread.
3653 3652 */
3654 3653
3655 3654 static int
3656 3655 vhci_efo_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
3657 3656 {
3658 3657 struct scsi_status *statusp = resultp->statusp;
3659 3658 uint8_t *sensep = (uint8_t *)resultp->sensep;
3660 3659 struct scsi_pkt *pkt = resultp->pkt;
3661 3660 scsi_vhci_swarg_t *swarg;
3662 3661 scsi_vhci_priv_t *svp;
3663 3662 scsi_vhci_lun_t *vlun;
3664 3663 struct scsi_vhci *vhci;
3665 3664 dev_info_t *vdip;
3666 3665 int rval, updt_paths;
3667 3666
3668 3667 swarg = (scsi_vhci_swarg_t *)(uintptr_t)arg;
3669 3668 svp = swarg->svs_svp;
3670 3669 if (swarg->svs_done) {
3671 3670 /*
3672 3671 * Already completed failover or timedout.
3673 3672 * Waiting for vhci_efo_done to terminate this scsi_watch.
3674 3673 */
3675 3674 return (0);
3676 3675 }
3677 3676
3678 3677 ASSERT(svp != NULL);
3679 3678 vlun = svp->svp_svl;
3680 3679 ASSERT(vlun != NULL);
3681 3680 ASSERT(VHCI_LUN_IS_HELD(vlun));
3682 3681 vlun->svl_efo_update_path = 0;
3683 3682 vdip = ddi_get_parent(vlun->svl_dip);
3684 3683 vhci = ddi_get_soft_state(vhci_softstate,
3685 3684 ddi_get_instance(vdip));
3686 3685
3687 3686 updt_paths = 0;
3688 3687
3689 3688 if (pkt->pkt_reason != CMD_CMPLT) {
3690 3689 if ((gethrtime() - swarg->svs_tos) >= VHCI_EXTFO_TIMEOUT) {
3691 3690 swarg->svs_release_lun = 1;
3692 3691 goto done;
3693 3692 }
3694 3693 return (0);
3695 3694 }
3696 3695 if (*((unsigned char *)statusp) == STATUS_CHECK) {
3697 3696 rval = vlun->svl_fops->sfo_analyze_sense(svp->svp_psd, sensep,
3698 3697 vlun->svl_fops_ctpriv);
3699 3698 switch (rval) {
3700 3699 /*
3701 3700 * Only update path states in case path is definitely
3702 3701 * inactive, or no failover occurred. For all other
3703 3702 * check conditions continue pinging. A unexpected
3704 3703 * check condition shouldn't cause pinging to complete
3705 3704 * prematurely.
3706 3705 */
3707 3706 case SCSI_SENSE_INACTIVE:
3708 3707 case SCSI_SENSE_NOFAILOVER:
3709 3708 updt_paths = 1;
3710 3709 break;
3711 3710 default:
3712 3711 if ((gethrtime() - swarg->svs_tos)
3713 3712 >= VHCI_EXTFO_TIMEOUT) {
3714 3713 swarg->svs_release_lun = 1;
3715 3714 goto done;
3716 3715 }
3717 3716 return (0);
3718 3717 }
3719 3718 } else if (*((unsigned char *)statusp) ==
3720 3719 STATUS_RESERVATION_CONFLICT) {
3721 3720 updt_paths = 1;
3722 3721 } else if ((*((unsigned char *)statusp)) &
3723 3722 (STATUS_BUSY | STATUS_QFULL)) {
3724 3723 return (0);
3725 3724 }
3726 3725 if ((*((unsigned char *)statusp) == STATUS_GOOD) ||
3727 3726 (updt_paths == 1)) {
3728 3727 /*
3729 3728 * we got here because we had detected an
3730 3729 * externally initiated failover; things
3731 3730 * have settled down now, so let's
3732 3731 * start up a task to update the
3733 3732 * path states and target port group
3734 3733 */
3735 3734 vlun->svl_efo_update_path = 1;
3736 3735 swarg->svs_done = 1;
3737 3736 vlun->svl_swarg = swarg;
3738 3737 vlun->svl_flags |= VLUN_UPDATE_TPG;
3739 3738 (void) taskq_dispatch(vhci->vhci_update_pathstates_taskq,
3740 3739 vhci_update_pathstates, (void *)vlun,
3741 3740 KM_SLEEP);
3742 3741 return (0);
3743 3742 }
3744 3743 if ((gethrtime() - swarg->svs_tos) >= VHCI_EXTFO_TIMEOUT) {
3745 3744 swarg->svs_release_lun = 1;
3746 3745 goto done;
3747 3746 }
3748 3747 return (0);
3749 3748 done:
3750 3749 swarg->svs_done = 1;
3751 3750 (void) taskq_dispatch(vhci->vhci_taskq,
3752 3751 vhci_efo_done, (void *)swarg, KM_SLEEP);
3753 3752 return (0);
3754 3753 }
3755 3754
3756 3755 /*
3757 3756 * vhci_efo_done:
3758 3757 * cleanly terminates scsi_watch and free up resources.
3759 3758 * Called as taskq function in vhci_efo_watch_cb for EFO timeout condition
3760 3759 * or by vhci_update_path_states invoked during external initiated
3761 3760 * failover completion.
3762 3761 */
3763 3762 static void
3764 3763 vhci_efo_done(void *arg)
3765 3764 {
3766 3765 scsi_vhci_lun_t *vlun;
3767 3766 scsi_vhci_swarg_t *swarg = (scsi_vhci_swarg_t *)arg;
3768 3767 scsi_vhci_priv_t *svp = swarg->svs_svp;
3769 3768 ASSERT(svp);
3770 3769
3771 3770 vlun = svp->svp_svl;
3772 3771 ASSERT(vlun);
3773 3772
3774 3773 /* Wait for clean termination of scsi_watch */
3775 3774 (void) scsi_watch_request_terminate(svp->svp_sw_token,
3776 3775 SCSI_WATCH_TERMINATE_ALL_WAIT);
3777 3776 svp->svp_sw_token = NULL;
3778 3777
3779 3778 /* release path and freeup resources to indicate failover completion */
3780 3779 mdi_rele_path(swarg->svs_pi);
3781 3780 if (swarg->svs_release_lun) {
3782 3781 VHCI_RELEASE_LUN(vlun);
3783 3782 }
3784 3783 kmem_free((void *)swarg, sizeof (*swarg));
3785 3784 }
3786 3785
3787 3786 /*
3788 3787 * Update the path states
3789 3788 * vlun should be HELD when this is invoked.
3790 3789 * Calls vhci_efo_done to cleanup resources allocated for EFO.
3791 3790 */
3792 3791 void
3793 3792 vhci_update_pathstates(void *arg)
3794 3793 {
3795 3794 mdi_pathinfo_t *pip, *npip;
3796 3795 dev_info_t *dip;
3797 3796 struct scsi_failover_ops *fo;
3798 3797 struct scsi_vhci_priv *svp;
3799 3798 struct scsi_device *psd;
3800 3799 struct scsi_path_opinfo opinfo;
3801 3800 char *pclass, *tptr;
3802 3801 struct scsi_vhci_lun *vlun = (struct scsi_vhci_lun *)arg;
3803 3802 int sps; /* mdi_select_path() status */
3804 3803 char *cpath;
3805 3804 struct scsi_vhci *vhci;
3806 3805 struct scsi_pkt *pkt;
3807 3806 struct buf *bp;
3808 3807 struct scsi_vhci_priv *svp_conflict = NULL;
3809 3808
3810 3809 ASSERT(VHCI_LUN_IS_HELD(vlun));
3811 3810 dip = vlun->svl_dip;
3812 3811 pip = npip = NULL;
3813 3812
3814 3813 vhci = ddi_get_soft_state(vhci_softstate,
3815 3814 ddi_get_instance(ddi_get_parent(dip)));
3816 3815
3817 3816 sps = mdi_select_path(dip, NULL, (MDI_SELECT_ONLINE_PATH |
3818 3817 MDI_SELECT_STANDBY_PATH | MDI_SELECT_NO_PREFERRED), NULL, &npip);
3819 3818 if ((npip == NULL) || (sps != MDI_SUCCESS)) {
3820 3819 goto done;
3821 3820 }
3822 3821
3823 3822 fo = vlun->svl_fops;
3824 3823 do {
3825 3824 pip = npip;
3826 3825 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
3827 3826 psd = svp->svp_psd;
3828 3827 if (fo->sfo_path_get_opinfo(psd, &opinfo,
3829 3828 vlun->svl_fops_ctpriv) != 0) {
3830 3829 sps = mdi_select_path(dip, NULL,
3831 3830 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH |
3832 3831 MDI_SELECT_NO_PREFERRED), pip, &npip);
3833 3832 mdi_rele_path(pip);
3834 3833 continue;
3835 3834 }
3836 3835
3837 3836 if (mdi_prop_lookup_string(pip, "path-class", &pclass) !=
3838 3837 MDI_SUCCESS) {
3839 3838 VHCI_DEBUG(1, (CE_NOTE, NULL,
3840 3839 "!vhci_update_pathstates: prop lookup failed for "
3841 3840 "path 0x%p\n", (void *)pip));
3842 3841 sps = mdi_select_path(dip, NULL,
3843 3842 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH |
3844 3843 MDI_SELECT_NO_PREFERRED), pip, &npip);
3845 3844 mdi_rele_path(pip);
3846 3845 continue;
3847 3846 }
3848 3847
3849 3848 /*
3850 3849 * Need to update the "path-class" property
3851 3850 * value in the device tree if different
3852 3851 * from the existing value.
3853 3852 */
3854 3853 if (strcmp(pclass, opinfo.opinfo_path_attr) != 0) {
3855 3854 (void) mdi_prop_update_string(pip, "path-class",
3856 3855 opinfo.opinfo_path_attr);
3857 3856 }
3858 3857
3859 3858 /*
3860 3859 * Only change the state if needed. i.e. Don't call
3861 3860 * mdi_pi_set_state to ONLINE a path if its already
3862 3861 * ONLINE. Same for STANDBY paths.
3863 3862 */
3864 3863
3865 3864 if ((opinfo.opinfo_path_state == SCSI_PATH_ACTIVE ||
3866 3865 opinfo.opinfo_path_state == SCSI_PATH_ACTIVE_NONOPT)) {
3867 3866 if (!(MDI_PI_IS_ONLINE(pip))) {
3868 3867 VHCI_DEBUG(1, (CE_NOTE, NULL,
3869 3868 "!vhci_update_pathstates: marking path"
3870 3869 " 0x%p as ONLINE\n", (void *)pip));
3871 3870 cpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3872 3871 vhci_log(CE_NOTE, ddi_get_parent(dip), "!%s "
3873 3872 "(%s%d): path %s "
3874 3873 "is now ONLINE because of "
3875 3874 "an externally initiated failover",
3876 3875 ddi_pathname(dip, cpath),
3877 3876 ddi_driver_name(dip),
3878 3877 ddi_get_instance(dip),
3879 3878 mdi_pi_spathname(pip));
3880 3879 kmem_free(cpath, MAXPATHLEN);
3881 3880 mdi_pi_set_state(pip,
3882 3881 MDI_PATHINFO_STATE_ONLINE);
3883 3882 mdi_pi_set_preferred(pip,
3884 3883 opinfo.opinfo_preferred);
3885 3884 tptr = kmem_alloc(strlen
3886 3885 (opinfo.opinfo_path_attr)+1, KM_SLEEP);
3887 3886 (void) strlcpy(tptr, opinfo.opinfo_path_attr,
3888 3887 (strlen(opinfo.opinfo_path_attr)+1));
3889 3888 mutex_enter(&vlun->svl_mutex);
3890 3889 if (vlun->svl_active_pclass != NULL) {
3891 3890 kmem_free(vlun->svl_active_pclass,
3892 3891 strlen(vlun->svl_active_pclass)+1);
3893 3892 }
3894 3893 vlun->svl_active_pclass = tptr;
3895 3894 if (vlun->svl_waiting_for_activepath) {
3896 3895 vlun->svl_waiting_for_activepath = 0;
3897 3896 }
3898 3897 mutex_exit(&vlun->svl_mutex);
3899 3898 } else if (MDI_PI_IS_ONLINE(pip)) {
3900 3899 if (strcmp(pclass, opinfo.opinfo_path_attr)
3901 3900 != 0) {
3902 3901 mdi_pi_set_preferred(pip,
3903 3902 opinfo.opinfo_preferred);
3904 3903 mutex_enter(&vlun->svl_mutex);
3905 3904 if (vlun->svl_active_pclass == NULL ||
3906 3905 strcmp(opinfo.opinfo_path_attr,
3907 3906 vlun->svl_active_pclass) != 0) {
3908 3907 mutex_exit(&vlun->svl_mutex);
3909 3908 tptr = kmem_alloc(strlen
3910 3909 (opinfo.opinfo_path_attr)+1,
3911 3910 KM_SLEEP);
3912 3911 (void) strlcpy(tptr,
3913 3912 opinfo.opinfo_path_attr,
3914 3913 (strlen
3915 3914 (opinfo.opinfo_path_attr)
3916 3915 +1));
3917 3916 mutex_enter(&vlun->svl_mutex);
3918 3917 } else {
3919 3918 /*
3920 3919 * No need to update
3921 3920 * svl_active_pclass
3922 3921 */
3923 3922 tptr = NULL;
3924 3923 mutex_exit(&vlun->svl_mutex);
3925 3924 }
3926 3925 if (tptr) {
3927 3926 if (vlun->svl_active_pclass
3928 3927 != NULL) {
3929 3928 kmem_free(vlun->
3930 3929 svl_active_pclass,
3931 3930 strlen(vlun->
3932 3931 svl_active_pclass)
3933 3932 +1);
3934 3933 }
3935 3934 vlun->svl_active_pclass = tptr;
3936 3935 mutex_exit(&vlun->svl_mutex);
3937 3936 }
3938 3937 }
3939 3938 }
3940 3939
3941 3940 /* Check for Reservation Conflict */
3942 3941 bp = scsi_alloc_consistent_buf(
3943 3942 &svp->svp_psd->sd_address, (struct buf *)NULL,
3944 3943 DEV_BSIZE, B_READ, NULL, NULL);
3945 3944 if (!bp) {
3946 3945 VHCI_DEBUG(1, (CE_NOTE, NULL,
3947 3946 "!vhci_update_pathstates: No resources "
3948 3947 "(buf)\n"));
3949 3948 mdi_rele_path(pip);
3950 3949 goto done;
3951 3950 }
3952 3951 pkt = scsi_init_pkt(&svp->svp_psd->sd_address, NULL, bp,
3953 3952 CDB_GROUP1, sizeof (struct scsi_arq_status), 0,
3954 3953 PKT_CONSISTENT, NULL, NULL);
3955 3954 if (pkt) {
3956 3955 (void) scsi_setup_cdb((union scsi_cdb *)
3957 3956 (uintptr_t)pkt->pkt_cdbp, SCMD_READ, 1, 1,
3958 3957 0);
3959 3958 pkt->pkt_time = 3*30;
3960 3959 pkt->pkt_flags = FLAG_NOINTR;
3961 3960 pkt->pkt_path_instance =
3962 3961 mdi_pi_get_path_instance(pip);
3963 3962
3964 3963 if ((scsi_transport(pkt) == TRAN_ACCEPT) &&
3965 3964 (pkt->pkt_reason == CMD_CMPLT) &&
3966 3965 (SCBP_C(pkt) ==
3967 3966 STATUS_RESERVATION_CONFLICT)) {
3968 3967 VHCI_DEBUG(1, (CE_NOTE, NULL,
3969 3968 "!vhci_update_pathstates: reserv. "
3970 3969 "conflict to be resolved on 0x%p\n",
3971 3970 (void *)pip));
3972 3971 svp_conflict = svp;
3973 3972 }
3974 3973 scsi_destroy_pkt(pkt);
3975 3974 }
3976 3975 scsi_free_consistent_buf(bp);
3977 3976 } else if ((opinfo.opinfo_path_state == SCSI_PATH_INACTIVE) &&
3978 3977 !(MDI_PI_IS_STANDBY(pip))) {
3979 3978 VHCI_DEBUG(1, (CE_NOTE, NULL,
3980 3979 "!vhci_update_pathstates: marking path"
3981 3980 " 0x%p as STANDBY\n", (void *)pip));
3982 3981 cpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3983 3982 vhci_log(CE_NOTE, ddi_get_parent(dip), "!%s "
3984 3983 "(%s%d): path %s "
3985 3984 "is now STANDBY because of "
3986 3985 "an externally initiated failover",
3987 3986 ddi_pathname(dip, cpath),
3988 3987 ddi_driver_name(dip),
3989 3988 ddi_get_instance(dip),
3990 3989 mdi_pi_spathname(pip));
3991 3990 kmem_free(cpath, MAXPATHLEN);
3992 3991 mdi_pi_set_state(pip,
3993 3992 MDI_PATHINFO_STATE_STANDBY);
3994 3993 mdi_pi_set_preferred(pip,
3995 3994 opinfo.opinfo_preferred);
3996 3995 mutex_enter(&vlun->svl_mutex);
3997 3996 if (vlun->svl_active_pclass != NULL) {
3998 3997 if (strcmp(vlun->svl_active_pclass,
3999 3998 opinfo.opinfo_path_attr) == 0) {
4000 3999 kmem_free(vlun->
4001 4000 svl_active_pclass,
4002 4001 strlen(vlun->
4003 4002 svl_active_pclass)+1);
4004 4003 vlun->svl_active_pclass = NULL;
4005 4004 }
4006 4005 }
4007 4006 mutex_exit(&vlun->svl_mutex);
4008 4007 }
4009 4008 (void) mdi_prop_free(pclass);
4010 4009 sps = mdi_select_path(dip, NULL,
4011 4010 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH |
4012 4011 MDI_SELECT_NO_PREFERRED), pip, &npip);
4013 4012 mdi_rele_path(pip);
4014 4013
4015 4014 } while ((npip != NULL) && (sps == MDI_SUCCESS));
4016 4015
4017 4016 /*
4018 4017 * Check to see if this vlun has an active SCSI-II RESERVE. If so
4019 4018 * clear the reservation by sending a reset, so the host doesn't
4020 4019 * receive a reservation conflict. The reset has to be sent via a
4021 4020 * working path. Let's use a path referred to by svp_conflict as it
4022 4021 * should be working.
4023 4022 * Reset VLUN_RESERVE_ACTIVE_FLG for this vlun. Also notify ssd
4024 4023 * of the reset, explicitly.
4025 4024 */
4026 4025 if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) {
4027 4026 if (svp_conflict && (vlun->svl_xlf_capable == 0)) {
4028 4027 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_update_pathstates:"
4029 4028 " sending recovery reset on 0x%p, path_state: %x",
4030 4029 svp_conflict->svp_psd->sd_private,
4031 4030 mdi_pi_get_state((mdi_pathinfo_t *)
4032 4031 svp_conflict->svp_psd->sd_private)));
4033 4032
4034 4033 (void) vhci_recovery_reset(vlun,
4035 4034 &svp_conflict->svp_psd->sd_address, FALSE,
4036 4035 VHCI_DEPTH_TARGET);
4037 4036 }
4038 4037 vlun->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG;
4039 4038 mutex_enter(&vhci->vhci_mutex);
4040 4039 scsi_hba_reset_notify_callback(&vhci->vhci_mutex,
4041 4040 &vhci->vhci_reset_notify_listf);
4042 4041 mutex_exit(&vhci->vhci_mutex);
4043 4042 }
4044 4043 if (vlun->svl_flags & VLUN_UPDATE_TPG) {
4045 4044 /*
4046 4045 * Update the AccessState of related MP-API TPGs
4047 4046 */
4048 4047 (void) vhci_mpapi_update_tpg_acc_state_for_lu(vhci, vlun);
4049 4048 vlun->svl_flags &= ~VLUN_UPDATE_TPG;
4050 4049 }
4051 4050 done:
4052 4051 if (vlun->svl_efo_update_path) {
4053 4052 vlun->svl_efo_update_path = 0;
4054 4053 vhci_efo_done(vlun->svl_swarg);
4055 4054 vlun->svl_swarg = 0;
4056 4055 }
4057 4056 VHCI_RELEASE_LUN(vlun);
4058 4057 }
4059 4058
4060 4059 /* ARGSUSED */
4061 4060 static int
4062 4061 vhci_pathinfo_init(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags)
4063 4062 {
4064 4063 scsi_hba_tran_t *hba = NULL;
4065 4064 struct scsi_device *psd = NULL;
4066 4065 scsi_vhci_lun_t *vlun = NULL;
4067 4066 dev_info_t *pdip = NULL;
4068 4067 dev_info_t *tgt_dip;
4069 4068 struct scsi_vhci *vhci;
4070 4069 char *guid;
4071 4070 scsi_vhci_priv_t *svp = NULL;
4072 4071 int rval = MDI_FAILURE;
4073 4072 int vlun_alloced = 0;
4074 4073
4075 4074 ASSERT(vdip != NULL);
4076 4075 ASSERT(pip != NULL);
4077 4076
4078 4077 vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip));
4079 4078 ASSERT(vhci != NULL);
4080 4079
4081 4080 pdip = mdi_pi_get_phci(pip);
4082 4081 ASSERT(pdip != NULL);
4083 4082
4084 4083 hba = ddi_get_driver_private(pdip);
4085 4084 ASSERT(hba != NULL);
4086 4085
4087 4086 tgt_dip = mdi_pi_get_client(pip);
4088 4087 ASSERT(tgt_dip != NULL);
4089 4088
4090 4089 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip, PROPFLAGS,
4091 4090 MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS) {
4092 4091 VHCI_DEBUG(1, (CE_WARN, NULL,
4093 4092 "vhci_pathinfo_init: lun guid property failed"));
4094 4093 goto failure;
4095 4094 }
4096 4095
4097 4096 vlun = vhci_lun_lookup_alloc(tgt_dip, guid, &vlun_alloced);
4098 4097 ddi_prop_free(guid);
4099 4098
4100 4099 vlun->svl_dip = tgt_dip;
4101 4100
4102 4101 svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
4103 4102 svp->svp_svl = vlun;
4104 4103
4105 4104 /*
4106 4105 * Initialize svl_lb_policy_save only for newly allocated vlun. Writing
4107 4106 * to svl_lb_policy_save later could accidentally overwrite saved lb
4108 4107 * policy.
4109 4108 */
4110 4109 if (vlun_alloced) {
4111 4110 vlun->svl_lb_policy_save = mdi_get_lb_policy(tgt_dip);
4112 4111 }
4113 4112
4114 4113 mutex_init(&svp->svp_mutex, NULL, MUTEX_DRIVER, NULL);
4115 4114 cv_init(&svp->svp_cv, NULL, CV_DRIVER, NULL);
4116 4115
4117 4116 psd = kmem_zalloc(sizeof (*psd), KM_SLEEP);
4118 4117 mutex_init(&psd->sd_mutex, NULL, MUTEX_DRIVER, NULL);
4119 4118
4120 4119 if (hba->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX) {
4121 4120 /*
4122 4121 * For a SCSI_HBA_ADDR_COMPLEX transport we store a pointer to
4123 4122 * scsi_device in the scsi_address structure. This allows an
4124 4123 * an HBA driver to find its scsi_device(9S) and
4125 4124 * per-scsi_device(9S) HBA private data given a
4126 4125 * scsi_address(9S) by using scsi_address_device(9F) and
4127 4126 * scsi_device_hba_private_get(9F)).
4128 4127 */
4129 4128 psd->sd_address.a.a_sd = psd;
4130 4129 } else if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
4131 4130 /*
4132 4131 * Clone transport structure if requested, so
4133 4132 * Self enumerating HBAs always need to use cloning
4134 4133 */
4135 4134 scsi_hba_tran_t *clone =
4136 4135 kmem_alloc(sizeof (scsi_hba_tran_t), KM_SLEEP);
4137 4136 bcopy(hba, clone, sizeof (scsi_hba_tran_t));
4138 4137 hba = clone;
4139 4138 hba->tran_sd = psd;
4140 4139 } else {
4141 4140 /*
4142 4141 * SPI pHCI unit-address. If we ever need to support this
4143 4142 * we could set a.spi.a_target/a.spi.a_lun based on pathinfo
4144 4143 * node unit-address properties. For now we fail...
4145 4144 */
4146 4145 goto failure;
4147 4146 }
4148 4147
4149 4148 psd->sd_dev = tgt_dip;
4150 4149 psd->sd_address.a_hba_tran = hba;
4151 4150
4152 4151 /*
4153 4152 * Mark scsi_device as being associated with a pathinfo node. For
4154 4153 * a scsi_device structure associated with a devinfo node,
4155 4154 * scsi_ctlops_initchild sets this field to NULL.
4156 4155 */
4157 4156 psd->sd_pathinfo = pip;
4158 4157
4159 4158 /*
4160 4159 * LEGACY: sd_private: set for older mpxio-capable pHCI drivers with
4161 4160 * too much scsi_vhci/mdi/ndi knowledge. Remove this code when all
4162 4161 * mpxio-capable pHCI drivers use SCSA enumeration services (or at
4163 4162 * least have been changed to use sd_pathinfo instead).
4164 4163 */
4165 4164 psd->sd_private = (caddr_t)pip;
4166 4165
4167 4166 /* See scsi_hba.c for info on sd_tran_safe kludge */
4168 4167 psd->sd_tran_safe = hba;
4169 4168
4170 4169 svp->svp_psd = psd;
4171 4170 mdi_pi_set_vhci_private(pip, (caddr_t)svp);
4172 4171
4173 4172 /*
4174 4173 * call hba's target init entry point if it exists
4175 4174 */
4176 4175 if (hba->tran_tgt_init != NULL) {
4177 4176 psd->sd_tran_tgt_free_done = 0;
4178 4177 if ((rval = (*hba->tran_tgt_init)(pdip, tgt_dip,
4179 4178 hba, psd)) != DDI_SUCCESS) {
4180 4179 VHCI_DEBUG(1, (CE_WARN, pdip,
4181 4180 "!vhci_pathinfo_init: tran_tgt_init failed for "
4182 4181 "path=0x%p rval=%x", (void *)pip, rval));
4183 4182 goto failure;
4184 4183 }
4185 4184 }
4186 4185
4187 4186 svp->svp_new_path = 1;
4188 4187
4189 4188 VHCI_DEBUG(4, (CE_NOTE, NULL, "!vhci_pathinfo_init: path:%p\n",
4190 4189 (void *)pip));
4191 4190 return (MDI_SUCCESS);
4192 4191
4193 4192 failure:
4194 4193 if (psd) {
4195 4194 mutex_destroy(&psd->sd_mutex);
4196 4195 kmem_free(psd, sizeof (*psd));
4197 4196 }
4198 4197 if (svp) {
4199 4198 mdi_pi_set_vhci_private(pip, NULL);
4200 4199 mutex_destroy(&svp->svp_mutex);
4201 4200 cv_destroy(&svp->svp_cv);
4202 4201 kmem_free(svp, sizeof (*svp));
4203 4202 }
4204 4203 if (hba && (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE))
4205 4204 kmem_free(hba, sizeof (scsi_hba_tran_t));
4206 4205
4207 4206 if (vlun_alloced)
4208 4207 vhci_lun_free(vlun, NULL);
4209 4208
4210 4209 return (rval);
4211 4210 }
4212 4211
4213 4212 /* ARGSUSED */
4214 4213 static int
4215 4214 vhci_pathinfo_uninit(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags)
4216 4215 {
4217 4216 scsi_hba_tran_t *hba = NULL;
4218 4217 struct scsi_device *psd = NULL;
4219 4218 dev_info_t *pdip = NULL;
4220 4219 dev_info_t *cdip = NULL;
4221 4220 scsi_vhci_priv_t *svp = NULL;
4222 4221
4223 4222 ASSERT(vdip != NULL);
4224 4223 ASSERT(pip != NULL);
4225 4224
4226 4225 pdip = mdi_pi_get_phci(pip);
4227 4226 ASSERT(pdip != NULL);
4228 4227
4229 4228 cdip = mdi_pi_get_client(pip);
4230 4229 ASSERT(cdip != NULL);
4231 4230
4232 4231 hba = ddi_get_driver_private(pdip);
4233 4232 ASSERT(hba != NULL);
4234 4233
4235 4234 vhci_mpapi_set_path_state(vdip, pip, MP_DRVR_PATH_STATE_UNINIT);
4236 4235 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
4237 4236 if (svp == NULL) {
4238 4237 /* path already freed. Nothing to do. */
4239 4238 return (MDI_SUCCESS);
4240 4239 }
4241 4240
4242 4241 psd = svp->svp_psd;
4243 4242 ASSERT(psd != NULL);
4244 4243
4245 4244 if (hba->tran_hba_flags & SCSI_HBA_ADDR_COMPLEX) {
4246 4245 /* Verify plumbing */
4247 4246 ASSERT(psd->sd_address.a_hba_tran == hba);
4248 4247 ASSERT(psd->sd_address.a.a_sd == psd);
4249 4248 } else if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
4250 4249 /* Switch to cloned scsi_hba_tran(9S) structure */
4251 4250 hba = psd->sd_address.a_hba_tran;
4252 4251 ASSERT(hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE);
4253 4252 ASSERT(hba->tran_sd == psd);
4254 4253 }
4255 4254
4256 4255 if ((hba->tran_tgt_free != NULL) && !psd->sd_tran_tgt_free_done) {
4257 4256 (*hba->tran_tgt_free) (pdip, cdip, hba, psd);
4258 4257 psd->sd_tran_tgt_free_done = 1;
4259 4258 }
4260 4259 mutex_destroy(&psd->sd_mutex);
4261 4260 if (hba->tran_hba_flags & SCSI_HBA_TRAN_CLONE) {
4262 4261 kmem_free(hba, sizeof (*hba));
4263 4262 }
4264 4263
4265 4264 mdi_pi_set_vhci_private(pip, NULL);
4266 4265
4267 4266 /*
4268 4267 * Free the pathinfo related scsi_device inquiry data. Note that this
4269 4268 * matches what happens for scsi_hba.c devinfo case at uninitchild time.
4270 4269 */
4271 4270 if (psd->sd_inq)
4272 4271 kmem_free((caddr_t)psd->sd_inq, sizeof (struct scsi_inquiry));
4273 4272 kmem_free((caddr_t)psd, sizeof (*psd));
4274 4273
4275 4274 mutex_destroy(&svp->svp_mutex);
4276 4275 cv_destroy(&svp->svp_cv);
4277 4276 kmem_free((caddr_t)svp, sizeof (*svp));
4278 4277
4279 4278 VHCI_DEBUG(4, (CE_NOTE, NULL, "!vhci_pathinfo_uninit: path=0x%p\n",
4280 4279 (void *)pip));
4281 4280 return (MDI_SUCCESS);
4282 4281 }
4283 4282
4284 4283 /* ARGSUSED */
4285 4284 static int
4286 4285 vhci_pathinfo_state_change(dev_info_t *vdip, mdi_pathinfo_t *pip,
4287 4286 mdi_pathinfo_state_t state, uint32_t ext_state, int flags)
4288 4287 {
4289 4288 int rval = MDI_SUCCESS;
4290 4289 scsi_vhci_priv_t *svp;
4291 4290 scsi_vhci_lun_t *vlun;
4292 4291 int held;
4293 4292 int op = (flags & 0xf00) >> 8;
4294 4293 struct scsi_vhci *vhci;
4295 4294
4296 4295 vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip));
4297 4296
4298 4297 if (flags & MDI_EXT_STATE_CHANGE) {
4299 4298 /*
4300 4299 * We do not want to issue any commands down the path in case
4301 4300 * sync flag is set. Lower layers might not be ready to accept
4302 4301 * any I/O commands.
4303 4302 */
4304 4303 if (op == DRIVER_DISABLE)
4305 4304 return (MDI_SUCCESS);
4306 4305
4307 4306 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
4308 4307 if (svp == NULL) {
4309 4308 return (MDI_FAILURE);
4310 4309 }
4311 4310 vlun = svp->svp_svl;
4312 4311
4313 4312 if (flags & MDI_BEFORE_STATE_CHANGE) {
4314 4313 /*
4315 4314 * Hold the LUN.
4316 4315 */
4317 4316 VHCI_HOLD_LUN(vlun, VH_SLEEP, held);
4318 4317 if (flags & MDI_DISABLE_OP) {
4319 4318 /*
4320 4319 * Issue scsi reset if it happens to be
4321 4320 * reserved path.
4322 4321 */
4323 4322 if (vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) {
4324 4323 /*
4325 4324 * if reservation pending on
4326 4325 * this path, dont' mark the
4327 4326 * path busy
4328 4327 */
4329 4328 if (op == DRIVER_DISABLE_TRANSIENT) {
4330 4329 VHCI_DEBUG(1, (CE_NOTE, NULL,
4331 4330 "!vhci_pathinfo"
4332 4331 "_state_change (pip:%p): "
4333 4332 " reservation: fail busy\n",
4334 4333 (void *)pip));
4335 4334 return (MDI_FAILURE);
4336 4335 }
4337 4336 if (pip == vlun->svl_resrv_pip) {
4338 4337 if (vhci_recovery_reset(
4339 4338 svp->svp_svl,
4340 4339 &svp->svp_psd->sd_address,
4341 4340 TRUE,
4342 4341 VHCI_DEPTH_TARGET) == 0) {
4343 4342 VHCI_DEBUG(1,
4344 4343 (CE_NOTE, NULL,
4345 4344 "!vhci_pathinfo"
4346 4345 "_state_change "
4347 4346 " (pip:%p): "
4348 4347 "reset failed, "
4349 4348 "give up!\n",
4350 4349 (void *)pip));
4351 4350 }
4352 4351 vlun->svl_flags &=
4353 4352 ~VLUN_RESERVE_ACTIVE_FLG;
4354 4353 }
4355 4354 }
4356 4355 } else if (flags & MDI_ENABLE_OP) {
4357 4356 if (((vhci->vhci_conf_flags &
4358 4357 VHCI_CONF_FLAGS_AUTO_FAILBACK) ==
4359 4358 VHCI_CONF_FLAGS_AUTO_FAILBACK) &&
4360 4359 MDI_PI_IS_USER_DISABLE(pip) &&
4361 4360 MDI_PI_IS_STANDBY(pip)) {
4362 4361 struct scsi_failover_ops *fo;
4363 4362 char *best_pclass, *pclass = NULL;
4364 4363 int best_class, rv;
4365 4364 /*
4366 4365 * Failback if enabling a standby path
4367 4366 * and it is the primary class or
4368 4367 * preferred class
4369 4368 */
4370 4369 best_class = mdi_pi_get_preferred(pip);
4371 4370 if (best_class == 0) {
4372 4371 /*
4373 4372 * if not preferred - compare
4374 4373 * path-class with class
4375 4374 */
4376 4375 fo = vlun->svl_fops;
4377 4376 (void) fo->sfo_pathclass_next(
4378 4377 NULL, &best_pclass,
4379 4378 vlun->svl_fops_ctpriv);
4380 4379 pclass = NULL;
4381 4380 rv = mdi_prop_lookup_string(pip,
4382 4381 "path-class", &pclass);
4383 4382 if (rv != MDI_SUCCESS ||
4384 4383 pclass == NULL) {
4385 4384 vhci_log(CE_NOTE, vdip,
4386 4385 "!path-class "
4387 4386 " lookup "
4388 4387 "failed. rv: %d"
4389 4388 "class: %p", rv,
4390 4389 (void *)pclass);
4391 4390 } else if (strncmp(pclass,
4392 4391 best_pclass,
4393 4392 strlen(best_pclass)) == 0) {
4394 4393 best_class = 1;
4395 4394 }
4396 4395 if (rv == MDI_SUCCESS &&
4397 4396 pclass != NULL) {
4398 4397 rv = mdi_prop_free(
4399 4398 pclass);
4400 4399 if (rv !=
4401 4400 DDI_PROP_SUCCESS) {
4402 4401 vhci_log(
4403 4402 CE_NOTE,
4404 4403 vdip,
4405 4404 "!path-"
4406 4405 "class"
4407 4406 " free"
4408 4407 " failed"
4409 4408 " rv: %d"
4410 4409 " class: "
4411 4410 "%p",
4412 4411 rv,
4413 4412 (void *)
4414 4413 pclass);
4415 4414 }
4416 4415 }
4417 4416 }
4418 4417 if (best_class == 1) {
4419 4418 VHCI_DEBUG(1, (CE_NOTE, NULL,
4420 4419 "preferred path: %p "
4421 4420 "USER_DISABLE->USER_ENABLE "
4422 4421 "transition for lun %s\n",
4423 4422 (void *)pip,
4424 4423 vlun->svl_lun_wwn));
4425 4424 (void) taskq_dispatch(
4426 4425 vhci->vhci_taskq,
4427 4426 vhci_initiate_auto_failback,
4428 4427 (void *) vlun, KM_SLEEP);
4429 4428 }
4430 4429 }
4431 4430 /*
4432 4431 * if PGR is active, revalidate key and
4433 4432 * register on this path also, if key is
4434 4433 * still valid
4435 4434 */
4436 4435 sema_p(&vlun->svl_pgr_sema);
4437 4436 if (vlun->svl_pgr_active)
4438 4437 (void)
4439 4438 vhci_pgr_validate_and_register(svp);
4440 4439 sema_v(&vlun->svl_pgr_sema);
4441 4440 /*
4442 4441 * Inform target driver about any
4443 4442 * reservations to be reinstated if target
4444 4443 * has dropped reservation during the busy
4445 4444 * period.
4446 4445 */
4447 4446 mutex_enter(&vhci->vhci_mutex);
4448 4447 scsi_hba_reset_notify_callback(
4449 4448 &vhci->vhci_mutex,
4450 4449 &vhci->vhci_reset_notify_listf);
4451 4450 mutex_exit(&vhci->vhci_mutex);
4452 4451 }
4453 4452 }
4454 4453 if (flags & MDI_AFTER_STATE_CHANGE) {
4455 4454 if (flags & MDI_ENABLE_OP) {
4456 4455 mutex_enter(&vhci_global_mutex);
4457 4456 cv_broadcast(&vhci_cv);
4458 4457 mutex_exit(&vhci_global_mutex);
4459 4458 }
4460 4459 if (vlun->svl_setcap_done) {
4461 4460 (void) vhci_pHCI_cap(&svp->svp_psd->sd_address,
4462 4461 "sector-size", vlun->svl_sector_size,
4463 4462 1, pip);
4464 4463 }
4465 4464
4466 4465 /*
4467 4466 * Release the LUN
4468 4467 */
4469 4468 VHCI_RELEASE_LUN(vlun);
4470 4469
4471 4470 /*
4472 4471 * Path transition is complete.
4473 4472 * Run callback to indicate target driver to
4474 4473 * retry to prevent IO starvation.
4475 4474 */
4476 4475 if (scsi_callback_id != 0) {
4477 4476 ddi_run_callback(&scsi_callback_id);
4478 4477 }
4479 4478 }
4480 4479 } else {
4481 4480 switch (state) {
4482 4481 case MDI_PATHINFO_STATE_ONLINE:
4483 4482 rval = vhci_pathinfo_online(vdip, pip, flags);
4484 4483 break;
4485 4484
4486 4485 case MDI_PATHINFO_STATE_OFFLINE:
4487 4486 rval = vhci_pathinfo_offline(vdip, pip, flags);
4488 4487 break;
4489 4488
4490 4489 default:
4491 4490 break;
4492 4491 }
4493 4492 /*
4494 4493 * Path transition is complete.
4495 4494 * Run callback to indicate target driver to
4496 4495 * retry to prevent IO starvation.
4497 4496 */
4498 4497 if ((rval == MDI_SUCCESS) && (scsi_callback_id != 0)) {
4499 4498 ddi_run_callback(&scsi_callback_id);
4500 4499 }
4501 4500 return (rval);
4502 4501 }
4503 4502
4504 4503 return (MDI_SUCCESS);
4505 4504 }
4506 4505
4507 4506 /*
4508 4507 * Parse the mpxio load balancing options. The datanameptr
4509 4508 * will point to a string containing the load-balance-options value.
4510 4509 * The load-balance-options value will be a property that
4511 4510 * defines the load-balance algorithm and any arguments to that
4512 4511 * algorithm.
4513 4512 * For example:
4514 4513 * device-type-mpxio-options-list=
4515 4514 * "device-type=SUN SENA", "load-balance-options=logical-block-options"
4516 4515 * "device-type=SUN SE6920", "round-robin-options";
4517 4516 * logical-block-options="load-balance=logical-block", "region-size=15";
4518 4517 * round-robin-options="load-balance=round-robin";
4519 4518 *
4520 4519 * If the load-balance is not defined the load balance algorithm will
4521 4520 * default to the global setting. There will be default values assigned
4522 4521 * to the arguments (region-size=18) and if an argument is one
4523 4522 * that is not known, it will be ignored.
4524 4523 */
4525 4524 static void
4526 4525 vhci_parse_mpxio_lb_options(dev_info_t *dip, dev_info_t *cdip,
4527 4526 caddr_t datanameptr)
4528 4527 {
4529 4528 char *dataptr, *next_entry;
4530 4529 caddr_t config_list = NULL;
4531 4530 int config_list_len = 0, list_len = 0;
4532 4531 int region_size = -1;
4533 4532 client_lb_t load_balance;
4534 4533
4535 4534 if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, datanameptr,
4536 4535 (caddr_t)&config_list, &config_list_len) != DDI_PROP_SUCCESS) {
4537 4536 return;
4538 4537 }
4539 4538
4540 4539 list_len = config_list_len;
4541 4540 next_entry = config_list;
4542 4541 while (config_list_len > 0) {
4543 4542 dataptr = next_entry;
4544 4543
4545 4544 if (strncmp(mdi_load_balance, dataptr,
4546 4545 strlen(mdi_load_balance)) == 0) {
4547 4546 /* get the load-balance scheme */
4548 4547 dataptr += strlen(mdi_load_balance) + 1;
4549 4548 if (strcmp(dataptr, LOAD_BALANCE_PROP_RR) == 0) {
4550 4549 (void) mdi_set_lb_policy(cdip, LOAD_BALANCE_RR);
4551 4550 load_balance = LOAD_BALANCE_RR;
4552 4551 } else if (strcmp(dataptr,
4553 4552 LOAD_BALANCE_PROP_LBA) == 0) {
4554 4553 (void) mdi_set_lb_policy(cdip,
4555 4554 LOAD_BALANCE_LBA);
4556 4555 load_balance = LOAD_BALANCE_LBA;
4557 4556 } else if (strcmp(dataptr,
4558 4557 LOAD_BALANCE_PROP_NONE) == 0) {
4559 4558 (void) mdi_set_lb_policy(cdip,
4560 4559 LOAD_BALANCE_NONE);
4561 4560 load_balance = LOAD_BALANCE_NONE;
4562 4561 }
4563 4562 } else if (strncmp(dataptr, LOGICAL_BLOCK_REGION_SIZE,
4564 4563 strlen(LOGICAL_BLOCK_REGION_SIZE)) == 0) {
4565 4564 int i = 0;
4566 4565 char *ptr;
4567 4566 char *tmp;
4568 4567
4569 4568 tmp = dataptr + (strlen(LOGICAL_BLOCK_REGION_SIZE) + 1);
4570 4569 /* check for numeric value */
4571 4570 for (ptr = tmp; i < strlen(tmp); i++, ptr++) {
4572 4571 if (!isdigit(*ptr)) {
4573 4572 cmn_err(CE_WARN,
4574 4573 "Illegal region size: %s."
4575 4574 " Setting to default value: %d",
4576 4575 tmp,
4577 4576 LOAD_BALANCE_DEFAULT_REGION_SIZE);
4578 4577 region_size =
4579 4578 LOAD_BALANCE_DEFAULT_REGION_SIZE;
4580 4579 break;
4581 4580 }
4582 4581 }
4583 4582 if (i >= strlen(tmp)) {
4584 4583 region_size = stoi(&tmp);
4585 4584 }
4586 4585 (void) mdi_set_lb_region_size(cdip, region_size);
4587 4586 }
4588 4587 config_list_len -= (strlen(next_entry) + 1);
4589 4588 next_entry += strlen(next_entry) + 1;
4590 4589 }
4591 4590 #ifdef DEBUG
4592 4591 if ((region_size >= 0) && (load_balance != LOAD_BALANCE_LBA)) {
4593 4592 VHCI_DEBUG(1, (CE_NOTE, dip,
4594 4593 "!vhci_parse_mpxio_lb_options: region-size: %d"
4595 4594 "only valid for load-balance=logical-block\n",
4596 4595 region_size));
4597 4596 }
4598 4597 #endif
4599 4598 if ((region_size == -1) && (load_balance == LOAD_BALANCE_LBA)) {
4600 4599 VHCI_DEBUG(1, (CE_NOTE, dip,
4601 4600 "!vhci_parse_mpxio_lb_options: No region-size"
4602 4601 " defined load-balance=logical-block."
4603 4602 " Default to: %d\n", LOAD_BALANCE_DEFAULT_REGION_SIZE));
4604 4603 (void) mdi_set_lb_region_size(cdip,
4605 4604 LOAD_BALANCE_DEFAULT_REGION_SIZE);
4606 4605 }
4607 4606 if (list_len > 0) {
4608 4607 kmem_free(config_list, list_len);
4609 4608 }
4610 4609 }
4611 4610
4612 4611 /*
4613 4612 * Parse the device-type-mpxio-options-list looking for the key of
4614 4613 * "load-balance-options". If found, parse the load balancing options.
4615 4614 * Check the comment of the vhci_get_device_type_mpxio_options()
4616 4615 * for the device-type-mpxio-options-list.
4617 4616 */
4618 4617 static void
4619 4618 vhci_parse_mpxio_options(dev_info_t *dip, dev_info_t *cdip,
4620 4619 caddr_t datanameptr, int list_len)
4621 4620 {
4622 4621 char *dataptr;
4623 4622 int len;
4624 4623
4625 4624 /*
4626 4625 * get the data list
4627 4626 */
4628 4627 dataptr = datanameptr;
4629 4628 len = 0;
4630 4629 while (len < list_len &&
4631 4630 strncmp(dataptr, DEVICE_TYPE_STR, strlen(DEVICE_TYPE_STR))
4632 4631 != 0) {
4633 4632 if (strncmp(dataptr, LOAD_BALANCE_OPTIONS,
4634 4633 strlen(LOAD_BALANCE_OPTIONS)) == 0) {
4635 4634 len += strlen(LOAD_BALANCE_OPTIONS) + 1;
4636 4635 dataptr += strlen(LOAD_BALANCE_OPTIONS) + 1;
4637 4636 vhci_parse_mpxio_lb_options(dip, cdip, dataptr);
4638 4637 }
4639 4638 len += strlen(dataptr) + 1;
4640 4639 dataptr += strlen(dataptr) + 1;
4641 4640 }
4642 4641 }
4643 4642
4644 4643 /*
4645 4644 * Check the inquriy string returned from the device with the device-type
4646 4645 * Check for the existence of the device-type-mpxio-options-list and
4647 4646 * if found parse the list checking for a match with the device-type
4648 4647 * value and the inquiry string returned from the device. If a match
4649 4648 * is found, parse the mpxio options list. The format of the
4650 4649 * device-type-mpxio-options-list is:
4651 4650 * device-type-mpxio-options-list=
4652 4651 * "device-type=SUN SENA", "load-balance-options=logical-block-options"
4653 4652 * "device-type=SUN SE6920", "round-robin-options";
4654 4653 * logical-block-options="load-balance=logical-block", "region-size=15";
4655 4654 * round-robin-options="load-balance=round-robin";
4656 4655 */
4657 4656 void
4658 4657 vhci_get_device_type_mpxio_options(dev_info_t *dip, dev_info_t *cdip,
4659 4658 struct scsi_device *devp)
4660 4659 {
4661 4660
4662 4661 caddr_t config_list = NULL;
4663 4662 caddr_t vidptr, datanameptr;
4664 4663 int vidlen, dupletlen = 0;
4665 4664 int config_list_len = 0, len;
4666 4665 struct scsi_inquiry *inq = devp->sd_inq;
4667 4666
4668 4667 /*
4669 4668 * look up the device-type-mpxio-options-list and walk thru
4670 4669 * the list compare the vendor ids of the earlier inquiry command and
4671 4670 * with those vids in the list if there is a match, lookup
4672 4671 * the mpxio-options value
4673 4672 */
4674 4673 if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
4675 4674 MPXIO_OPTIONS_LIST,
4676 4675 (caddr_t)&config_list, &config_list_len) == DDI_PROP_SUCCESS) {
4677 4676
4678 4677 /*
4679 4678 * Compare vids in each duplet - if it matches,
4680 4679 * parse the mpxio options list.
4681 4680 */
4682 4681 for (len = config_list_len, vidptr = config_list; len > 0;
4683 4682 len -= dupletlen) {
4684 4683
4685 4684 dupletlen = 0;
4686 4685
4687 4686 if (strlen(vidptr) != 0 &&
4688 4687 strncmp(vidptr, DEVICE_TYPE_STR,
4689 4688 strlen(DEVICE_TYPE_STR)) == 0) {
4690 4689 /* point to next duplet */
4691 4690 datanameptr = vidptr + strlen(vidptr) + 1;
4692 4691 /* add len of this duplet */
4693 4692 dupletlen += strlen(vidptr) + 1;
4694 4693 /* get to device type */
4695 4694 vidptr += strlen(DEVICE_TYPE_STR) + 1;
4696 4695 vidlen = strlen(vidptr);
4697 4696 if ((vidlen != 0) &&
4698 4697 bcmp(inq->inq_vid, vidptr, vidlen) == 0) {
4699 4698 vhci_parse_mpxio_options(dip, cdip,
4700 4699 datanameptr, len - dupletlen);
4701 4700 break;
4702 4701 }
4703 4702 /* get to next duplet */
4704 4703 vidptr += strlen(vidptr) + 1;
4705 4704 }
4706 4705 /* get to the next device-type */
4707 4706 while (len - dupletlen > 0 &&
4708 4707 strlen(vidptr) != 0 &&
4709 4708 strncmp(vidptr, DEVICE_TYPE_STR,
4710 4709 strlen(DEVICE_TYPE_STR)) != 0) {
4711 4710 dupletlen += strlen(vidptr) + 1;
4712 4711 vidptr += strlen(vidptr) + 1;
4713 4712 }
4714 4713 }
4715 4714 if (config_list_len > 0) {
4716 4715 kmem_free(config_list, config_list_len);
4717 4716 }
4718 4717 }
4719 4718 }
4720 4719
4721 4720 static int
4722 4721 vhci_update_pathinfo(struct scsi_device *psd, mdi_pathinfo_t *pip,
4723 4722 struct scsi_failover_ops *fo,
4724 4723 scsi_vhci_lun_t *vlun,
4725 4724 struct scsi_vhci *vhci)
4726 4725 {
4727 4726 struct scsi_path_opinfo opinfo;
4728 4727 char *pclass, *best_pclass;
4729 4728 char *resrv_pclass = NULL;
4730 4729 int force_rereserve = 0;
4731 4730 int update_pathinfo_done = 0;
4732 4731
4733 4732 if (fo->sfo_path_get_opinfo(psd, &opinfo, vlun->svl_fops_ctpriv) != 0) {
4734 4733 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_update_pathinfo: "
4735 4734 "Failed to get operation info for path:%p\n", (void *)pip));
4736 4735 return (MDI_FAILURE);
4737 4736 }
4738 4737 /* set the xlf capable flag in the vlun for future use */
4739 4738 vlun->svl_xlf_capable = opinfo.opinfo_xlf_capable;
4740 4739 (void) mdi_prop_update_string(pip, "path-class",
4741 4740 opinfo.opinfo_path_attr);
4742 4741
4743 4742 pclass = opinfo.opinfo_path_attr;
4744 4743 if (opinfo.opinfo_path_state == SCSI_PATH_ACTIVE) {
4745 4744 mutex_enter(&vlun->svl_mutex);
4746 4745 if (vlun->svl_active_pclass != NULL) {
4747 4746 if (strcmp(vlun->svl_active_pclass, pclass) != 0) {
4748 4747 mutex_exit(&vlun->svl_mutex);
4749 4748 /*
4750 4749 * Externally initiated failover has happened;
4751 4750 * force the path state to be STANDBY/ONLINE,
4752 4751 * next IO will trigger failover and thus
4753 4752 * sync-up the pathstates. Reason we don't
4754 4753 * sync-up immediately by invoking
4755 4754 * vhci_update_pathstates() is because it
4756 4755 * needs a VHCI_HOLD_LUN() and we don't
4757 4756 * want to block here.
4758 4757 *
4759 4758 * Further, if the device is an ALUA device,
4760 4759 * then failure to exactly match 'pclass' and
4761 4760 * 'svl_active_pclass'(as is the case here)
4762 4761 * indicates that the currently active path
4763 4762 * is a 'non-optimized' path - which means
4764 4763 * that 'svl_active_pclass' needs to be
4765 4764 * replaced with opinfo.opinfo_path_state
4766 4765 * value.
4767 4766 */
4768 4767
4769 4768 if (SCSI_FAILOVER_IS_TPGS(vlun->svl_fops)) {
4770 4769 char *tptr;
4771 4770
4772 4771 /*
4773 4772 * The device is ALUA compliant. The
4774 4773 * state need to be changed to online
4775 4774 * rather than standby state which is
4776 4775 * done typically for a asymmetric
4777 4776 * device that is non ALUA compliant.
4778 4777 */
4779 4778 mdi_pi_set_state(pip,
4780 4779 MDI_PATHINFO_STATE_ONLINE);
4781 4780 tptr = kmem_alloc(strlen
4782 4781 (opinfo.opinfo_path_attr)+1,
4783 4782 KM_SLEEP);
4784 4783 (void) strlcpy(tptr,
4785 4784 opinfo.opinfo_path_attr,
4786 4785 (strlen(opinfo.opinfo_path_attr)
4787 4786 +1));
4788 4787 mutex_enter(&vlun->svl_mutex);
4789 4788 kmem_free(vlun->svl_active_pclass,
4790 4789 strlen(vlun->svl_active_pclass)+1);
4791 4790 vlun->svl_active_pclass = tptr;
4792 4791 mutex_exit(&vlun->svl_mutex);
4793 4792 } else {
4794 4793 /*
4795 4794 * Non ALUA device case.
4796 4795 */
4797 4796 mdi_pi_set_state(pip,
4798 4797 MDI_PATHINFO_STATE_STANDBY);
4799 4798 }
4800 4799 vlun->svl_fo_support = opinfo.opinfo_mode;
4801 4800 mdi_pi_set_preferred(pip,
4802 4801 opinfo.opinfo_preferred);
4803 4802 update_pathinfo_done = 1;
4804 4803 }
4805 4804
4806 4805 /*
4807 4806 * Find out a class of currently reserved path if there
4808 4807 * is any.
4809 4808 */
4810 4809 if ((vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) &&
4811 4810 mdi_prop_lookup_string(vlun->svl_resrv_pip,
4812 4811 "path-class", &resrv_pclass) != MDI_SUCCESS) {
4813 4812 VHCI_DEBUG(1, (CE_NOTE, NULL,
4814 4813 "!vhci_update_pathinfo: prop lookup "
4815 4814 "failed for path 0x%p\n",
4816 4815 (void *)vlun->svl_resrv_pip));
4817 4816 /*
4818 4817 * Something is wrong with the reserved path.
4819 4818 * We can't do much with that right here. Just
4820 4819 * force re-reservation to another path.
4821 4820 */
4822 4821 force_rereserve = 1;
4823 4822 }
4824 4823
4825 4824 (void) fo->sfo_pathclass_next(NULL, &best_pclass,
4826 4825 vlun->svl_fops_ctpriv);
4827 4826 if ((force_rereserve == 1) || ((resrv_pclass != NULL) &&
4828 4827 (strcmp(pclass, best_pclass) == 0) &&
4829 4828 (strcmp(resrv_pclass, best_pclass) != 0))) {
4830 4829 /*
4831 4830 * Inform target driver that a reservation
4832 4831 * should be reinstated because the reserved
4833 4832 * path is not the most preferred one.
4834 4833 */
4835 4834 mutex_enter(&vhci->vhci_mutex);
4836 4835 scsi_hba_reset_notify_callback(
4837 4836 &vhci->vhci_mutex,
4838 4837 &vhci->vhci_reset_notify_listf);
4839 4838 mutex_exit(&vhci->vhci_mutex);
4840 4839 }
4841 4840
4842 4841 if (update_pathinfo_done == 1) {
4843 4842 return (MDI_SUCCESS);
4844 4843 }
4845 4844 } else {
4846 4845 char *tptr;
4847 4846
4848 4847 /*
4849 4848 * lets release the mutex before we try to
4850 4849 * allocate since the potential to sleep is
4851 4850 * possible.
4852 4851 */
4853 4852 mutex_exit(&vlun->svl_mutex);
4854 4853 tptr = kmem_alloc(strlen(pclass)+1, KM_SLEEP);
4855 4854 (void) strlcpy(tptr, pclass, (strlen(pclass)+1));
4856 4855 mutex_enter(&vlun->svl_mutex);
4857 4856 vlun->svl_active_pclass = tptr;
4858 4857 }
4859 4858 mutex_exit(&vlun->svl_mutex);
4860 4859 mdi_pi_set_state(pip, MDI_PATHINFO_STATE_ONLINE);
4861 4860 vlun->svl_waiting_for_activepath = 0;
4862 4861 } else if (opinfo.opinfo_path_state == SCSI_PATH_ACTIVE_NONOPT) {
4863 4862 mutex_enter(&vlun->svl_mutex);
4864 4863 if (vlun->svl_active_pclass == NULL) {
4865 4864 char *tptr;
4866 4865
4867 4866 mutex_exit(&vlun->svl_mutex);
4868 4867 tptr = kmem_alloc(strlen(pclass)+1, KM_SLEEP);
4869 4868 (void) strlcpy(tptr, pclass, (strlen(pclass)+1));
4870 4869 mutex_enter(&vlun->svl_mutex);
4871 4870 vlun->svl_active_pclass = tptr;
4872 4871 }
4873 4872 mutex_exit(&vlun->svl_mutex);
4874 4873 mdi_pi_set_state(pip, MDI_PATHINFO_STATE_ONLINE);
4875 4874 vlun->svl_waiting_for_activepath = 0;
4876 4875 } else if (opinfo.opinfo_path_state == SCSI_PATH_INACTIVE) {
4877 4876 mutex_enter(&vlun->svl_mutex);
4878 4877 if (vlun->svl_active_pclass != NULL) {
4879 4878 if (strcmp(vlun->svl_active_pclass, pclass) == 0) {
4880 4879 mutex_exit(&vlun->svl_mutex);
4881 4880 /*
4882 4881 * externally initiated failover has happened;
4883 4882 * force state to ONLINE (see comment above)
4884 4883 */
4885 4884 mdi_pi_set_state(pip,
4886 4885 MDI_PATHINFO_STATE_ONLINE);
4887 4886 vlun->svl_fo_support = opinfo.opinfo_mode;
4888 4887 mdi_pi_set_preferred(pip,
4889 4888 opinfo.opinfo_preferred);
4890 4889 return (MDI_SUCCESS);
4891 4890 }
4892 4891 }
4893 4892 mutex_exit(&vlun->svl_mutex);
4894 4893 mdi_pi_set_state(pip, MDI_PATHINFO_STATE_STANDBY);
4895 4894
4896 4895 /*
4897 4896 * Initiate auto-failback, if enabled, for path if path-state
4898 4897 * is transitioning from OFFLINE->STANDBY and pathclass is the
4899 4898 * preferred pathclass for this storage.
4900 4899 * NOTE: In case where opinfo_path_state is SCSI_PATH_ACTIVE
4901 4900 * (above), where the pi state is set to STANDBY, we don't
4902 4901 * initiate auto-failback as the next IO shall take care of.
4903 4902 * this. See comment above.
4904 4903 */
4905 4904 (void) fo->sfo_pathclass_next(NULL, &best_pclass,
4906 4905 vlun->svl_fops_ctpriv);
4907 4906 if (((vhci->vhci_conf_flags & VHCI_CONF_FLAGS_AUTO_FAILBACK) ==
4908 4907 VHCI_CONF_FLAGS_AUTO_FAILBACK) &&
4909 4908 (strcmp(pclass, best_pclass) == 0) &&
4910 4909 ((MDI_PI_OLD_STATE(pip) == MDI_PATHINFO_STATE_OFFLINE)||
4911 4910 (MDI_PI_OLD_STATE(pip) == MDI_PATHINFO_STATE_INIT))) {
4912 4911 VHCI_DEBUG(1, (CE_NOTE, NULL, "%s pathclass path: %p"
4913 4912 " OFFLINE->STANDBY transition for lun %s\n",
4914 4913 best_pclass, (void *)pip, vlun->svl_lun_wwn));
4915 4914 (void) taskq_dispatch(vhci->vhci_taskq,
4916 4915 vhci_initiate_auto_failback, (void *) vlun,
4917 4916 KM_SLEEP);
4918 4917 }
4919 4918 }
4920 4919 vlun->svl_fo_support = opinfo.opinfo_mode;
4921 4920 mdi_pi_set_preferred(pip, opinfo.opinfo_preferred);
4922 4921
4923 4922 VHCI_DEBUG(8, (CE_NOTE, NULL, "vhci_update_pathinfo: opinfo_rev = %x,"
4924 4923 " opinfo_path_state = %x opinfo_preferred = %x, opinfo_mode = %x\n",
4925 4924 opinfo.opinfo_rev, opinfo.opinfo_path_state,
4926 4925 opinfo.opinfo_preferred, opinfo.opinfo_mode));
4927 4926
4928 4927 return (MDI_SUCCESS);
4929 4928 }
4930 4929
4931 4930 /*
4932 4931 * Form the kstat name and and call mdi_pi_kstat_create()
4933 4932 */
4934 4933 void
4935 4934 vhci_kstat_create_pathinfo(mdi_pathinfo_t *pip)
4936 4935 {
4937 4936 dev_info_t *tgt_dip;
4938 4937 dev_info_t *pdip;
4939 4938 char *guid;
4940 4939 char *target_port, *target_port_dup;
4941 4940 char ks_name[KSTAT_STRLEN];
4942 4941 uint_t pid;
4943 4942 int by_id;
4944 4943 mod_hash_val_t hv;
4945 4944
4946 4945
4947 4946 /* return if we have already allocated kstats */
4948 4947 if (mdi_pi_kstat_exists(pip))
4949 4948 return;
4950 4949
4951 4950 /*
4952 4951 * We need instance numbers to create a kstat name, return if we don't
4953 4952 * have instance numbers assigned yet.
4954 4953 */
4955 4954 tgt_dip = mdi_pi_get_client(pip);
4956 4955 pdip = mdi_pi_get_phci(pip);
4957 4956 if ((ddi_get_instance(tgt_dip) == -1) || (ddi_get_instance(pdip) == -1))
4958 4957 return;
4959 4958
4960 4959 /*
4961 4960 * A path oriented kstat has a ks_name of the form:
4962 4961 *
4963 4962 * <client-driver><instance>.t<pid>.<pHCI-driver><instance>
4964 4963 *
4965 4964 * We maintain a bidirectional 'target-port' to <pid> map,
4966 4965 * called targetmap. All pathinfo nodes with the same
4967 4966 * 'target-port' map to the same <pid>. The iostat(1M) code,
4968 4967 * when parsing a path oriented kstat name, uses the <pid> as
4969 4968 * a SCSI_VHCI_GET_TARGET_LONGNAME ioctl argument in order
4970 4969 * to get the 'target-port'. For KSTAT_FLAG_PERSISTENT kstats,
4971 4970 * this ioctl needs to translate a <pid> to a 'target-port'
4972 4971 * even after all pathinfo nodes associated with the
4973 4972 * 'target-port' have been destroyed. This is needed to support
4974 4973 * consistent first-iteration activity-since-boot iostat(1M)
4975 4974 * output. Because of this requirement, the mapping can't be
4976 4975 * based on pathinfo information in a devinfo snapshot.
4977 4976 */
4978 4977
4979 4978 /* determine 'target-port' */
4980 4979 if (mdi_prop_lookup_string(pip,
4981 4980 SCSI_ADDR_PROP_TARGET_PORT, &target_port) == MDI_SUCCESS) {
4982 4981 target_port_dup = i_ddi_strdup(target_port, KM_SLEEP);
4983 4982 (void) mdi_prop_free(target_port);
4984 4983 by_id = 1;
4985 4984 } else {
4986 4985 /*
4987 4986 * If the pHCI did not set up 'target-port' on this
4988 4987 * pathinfo node, assume that our client is the only
4989 4988 * one with paths to the device by using the guid
4990 4989 * value as the 'target-port'. Since no other client
4991 4990 * will have the same guid, no other client will use
4992 4991 * the same <pid>. NOTE: a client with an instance
4993 4992 * number always has a guid.
4994 4993 */
4995 4994 (void) ddi_prop_lookup_string(DDI_DEV_T_ANY, tgt_dip,
4996 4995 PROPFLAGS, MDI_CLIENT_GUID_PROP, &guid);
4997 4996 target_port_dup = i_ddi_strdup(guid, KM_SLEEP);
4998 4997 ddi_prop_free(guid);
4999 4998
5000 4999 /*
5001 5000 * For this type of mapping we don't want the
5002 5001 * <id> -> 'target-port' mapping to be made. This
5003 5002 * will cause the SCSI_VHCI_GET_TARGET_LONGNAME ioctl
5004 5003 * to fail, and the iostat(1M) long '-n' output will
5005 5004 * still use the <pid>. We do this because we just
5006 5005 * made up the 'target-port' using the guid, and we
5007 5006 * don't want to expose that fact in iostat output.
5008 5007 */
5009 5008 by_id = 0;
5010 5009 }
5011 5010
5012 5011 /* find/establish <pid> given 'target-port' */
5013 5012 mutex_enter(&vhci_targetmap_mutex);
5014 5013 if (mod_hash_find(vhci_targetmap_byport,
5015 5014 (mod_hash_key_t)target_port_dup, &hv) == 0) {
5016 5015 pid = (int)(intptr_t)hv; /* mapping exists */
5017 5016 } else {
5018 5017 pid = vhci_targetmap_pid++; /* new mapping */
5019 5018
5020 5019 (void) mod_hash_insert(vhci_targetmap_byport,
5021 5020 (mod_hash_key_t)target_port_dup,
5022 5021 (mod_hash_val_t)(intptr_t)pid);
5023 5022 if (by_id) {
5024 5023 (void) mod_hash_insert(vhci_targetmap_bypid,
5025 5024 (mod_hash_key_t)(uintptr_t)pid,
5026 5025 (mod_hash_val_t)(uintptr_t)target_port_dup);
5027 5026 }
5028 5027 target_port_dup = NULL; /* owned by hash */
5029 5028 }
5030 5029 mutex_exit(&vhci_targetmap_mutex);
5031 5030
5032 5031 /* form kstat name */
5033 5032 (void) snprintf(ks_name, KSTAT_STRLEN, "%s%d.t%d.%s%d",
5034 5033 ddi_driver_name(tgt_dip), ddi_get_instance(tgt_dip),
5035 5034 pid, ddi_driver_name(pdip), ddi_get_instance(pdip));
5036 5035
5037 5036 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_path_online: path:%p "
5038 5037 "kstat %s: pid %x <-> port %s\n", (void *)pip,
5039 5038 ks_name, pid, target_port_dup));
5040 5039 if (target_port_dup)
5041 5040 kmem_free(target_port_dup, strlen(target_port_dup) + 1);
5042 5041
5043 5042 /* call mdi to create kstats with the name we built */
5044 5043 (void) mdi_pi_kstat_create(pip, ks_name);
5045 5044 }
5046 5045
5047 5046 /* ARGSUSED */
5048 5047 static int
5049 5048 vhci_pathinfo_online(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags)
5050 5049 {
5051 5050 scsi_hba_tran_t *hba = NULL;
5052 5051 struct scsi_device *psd = NULL;
5053 5052 scsi_vhci_lun_t *vlun = NULL;
5054 5053 dev_info_t *pdip = NULL;
5055 5054 dev_info_t *cdip;
5056 5055 dev_info_t *tgt_dip;
5057 5056 struct scsi_vhci *vhci;
5058 5057 char *guid;
5059 5058 struct scsi_failover_ops *sfo;
5060 5059 scsi_vhci_priv_t *svp = NULL;
5061 5060 struct scsi_address *ap;
5062 5061 struct scsi_pkt *pkt;
5063 5062 int rval = MDI_FAILURE;
5064 5063 mpapi_item_list_t *list_ptr;
5065 5064 mpapi_lu_data_t *ld;
5066 5065
5067 5066 ASSERT(vdip != NULL);
5068 5067 ASSERT(pip != NULL);
5069 5068
5070 5069 vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip));
5071 5070 ASSERT(vhci != NULL);
5072 5071
5073 5072 pdip = mdi_pi_get_phci(pip);
5074 5073 hba = ddi_get_driver_private(pdip);
5075 5074 ASSERT(hba != NULL);
5076 5075
5077 5076 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
5078 5077 ASSERT(svp != NULL);
5079 5078
5080 5079 cdip = mdi_pi_get_client(pip);
5081 5080 ASSERT(cdip != NULL);
5082 5081 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, cdip, PROPFLAGS,
5083 5082 MDI_CLIENT_GUID_PROP, &guid) != DDI_SUCCESS) {
5084 5083 VHCI_DEBUG(1, (CE_WARN, NULL, "vhci_path_online: lun guid "
5085 5084 "property failed"));
5086 5085 goto failure;
5087 5086 }
5088 5087
5089 5088 vlun = vhci_lun_lookup(cdip);
5090 5089 ASSERT(vlun != NULL);
5091 5090
5092 5091 ddi_prop_free(guid);
5093 5092
5094 5093 vlun->svl_dip = mdi_pi_get_client(pip);
5095 5094 ASSERT(vlun->svl_dip != NULL);
5096 5095
5097 5096 psd = svp->svp_psd;
5098 5097 ASSERT(psd != NULL);
5099 5098
5100 5099 ap = &psd->sd_address;
5101 5100
5102 5101 /*
5103 5102 * Get inquiry data into pathinfo related scsi_device structure.
5104 5103 * Free sq_inq when pathinfo related scsi_device structure is destroyed
5105 5104 * by vhci_pathinfo_uninit(). In other words, vhci maintains its own
5106 5105 * copy of scsi_device and scsi_inquiry data on a per-path basis.
5107 5106 */
5108 5107 if (scsi_probe(psd, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
5109 5108 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_pathinfo_online: "
5110 5109 "scsi_probe failed path:%p rval:%x\n", (void *)pip, rval));
5111 5110 rval = MDI_FAILURE;
5112 5111 goto failure;
5113 5112 }
5114 5113
5115 5114 /*
5116 5115 * See if we have a failover module to support the device.
5117 5116 *
5118 5117 * We re-probe to determine the failover ops for each path. This
5119 5118 * is done in case there are any path-specific side-effects associated
5120 5119 * with the sfo_device_probe implementation.
5121 5120 *
5122 5121 * Give the first successfull sfo_device_probe the opportunity to
5123 5122 * establish 'ctpriv', vlun/client private data. The ctpriv will
5124 5123 * then be passed into the failover module on all other sfo_device_*()
5125 5124 * operations (and must be freed by sfo_device_unprobe implementation).
5126 5125 *
5127 5126 * NOTE: While sfo_device_probe is done once per path,
5128 5127 * sfo_device_unprobe only occurs once - when the vlun is destroyed.
5129 5128 *
5130 5129 * NOTE: We don't currently support per-path fops private data
5131 5130 * mechanism.
5132 5131 */
5133 5132 sfo = vhci_dev_fo(vdip, psd,
5134 5133 &vlun->svl_fops_ctpriv, &vlun->svl_fops_name);
5135 5134
5136 5135 /* check path configuration result with current vlun state */
5137 5136 if (((sfo && vlun->svl_fops) && (sfo != vlun->svl_fops)) ||
5138 5137 (sfo && vlun->svl_not_supported) ||
5139 5138 ((sfo == NULL) && vlun->svl_fops)) {
5140 5139 /* Getting different results for different paths. */
5141 5140 VHCI_DEBUG(1, (CE_NOTE, vhci->vhci_dip,
5142 5141 "!vhci_pathinfo_online: dev (path 0x%p) contradiction\n",
5143 5142 (void *)pip));
5144 5143 cmn_err(CE_WARN, "scsi_vhci: failover contradiction: "
5145 5144 "'%s'.vs.'%s': path %s\n",
5146 5145 vlun->svl_fops ? vlun->svl_fops->sfo_name : "NULL",
5147 5146 sfo ? sfo->sfo_name : "NULL", mdi_pi_pathname(pip));
5148 5147 vlun->svl_not_supported = 1;
5149 5148 rval = MDI_NOT_SUPPORTED;
5150 5149 goto done;
5151 5150 } else if (sfo == NULL) {
5152 5151 /* No failover module - device not supported under vHCI. */
5153 5152 VHCI_DEBUG(1, (CE_NOTE, vhci->vhci_dip,
5154 5153 "!vhci_pathinfo_online: dev (path 0x%p) not "
5155 5154 "supported\n", (void *)pip));
5156 5155
5157 5156 /* XXX does this contradict vhci_is_dev_supported ? */
5158 5157 vlun->svl_not_supported = 1;
5159 5158 rval = MDI_NOT_SUPPORTED;
5160 5159 goto done;
5161 5160 }
5162 5161
5163 5162 /* failover supported for device - save failover_ops in vlun */
5164 5163 vlun->svl_fops = sfo;
5165 5164 ASSERT(vlun->svl_fops_name != NULL);
5166 5165
5167 5166 /*
5168 5167 * Obtain the device-type based mpxio options as specified in
5169 5168 * scsi_vhci.conf file.
5170 5169 *
5171 5170 * NOTE: currently, the end result is a call to
5172 5171 * mdi_set_lb_region_size().
5173 5172 */
5174 5173 tgt_dip = psd->sd_dev;
5175 5174 ASSERT(tgt_dip != NULL);
5176 5175 vhci_get_device_type_mpxio_options(vdip, tgt_dip, psd);
5177 5176
5178 5177 /*
5179 5178 * if PGR is active, revalidate key and register on this path also,
5180 5179 * if key is still valid
5181 5180 */
5182 5181 sema_p(&vlun->svl_pgr_sema);
5183 5182 if (vlun->svl_pgr_active) {
5184 5183 rval = vhci_pgr_validate_and_register(svp);
5185 5184 if (rval != 1) {
5186 5185 rval = MDI_FAILURE;
5187 5186 sema_v(&vlun->svl_pgr_sema);
5188 5187 goto failure;
5189 5188 }
5190 5189 }
5191 5190 sema_v(&vlun->svl_pgr_sema);
5192 5191
5193 5192 if (svp->svp_new_path) {
5194 5193 /*
5195 5194 * Last chance to perform any cleanup operations on this
5196 5195 * new path before making this path completely online.
5197 5196 */
5198 5197 svp->svp_new_path = 0;
5199 5198
5200 5199 /*
5201 5200 * If scsi_vhci knows the lun is alread RESERVE'd,
5202 5201 * then skip the issue of RELEASE on new path.
5203 5202 */
5204 5203 if ((vlun->svl_flags & VLUN_RESERVE_ACTIVE_FLG) == 0) {
5205 5204 /*
5206 5205 * Issue SCSI-2 RELEASE only for the first time on
5207 5206 * a new path just in case the host rebooted and
5208 5207 * a reservation is still pending on this path.
5209 5208 * IBM Shark storage does not clear RESERVE upon
5210 5209 * host reboot.
5211 5210 */
5212 5211 pkt = scsi_init_pkt(ap, NULL, NULL, CDB_GROUP0,
5213 5212 sizeof (struct scsi_arq_status), 0, 0,
5214 5213 SLEEP_FUNC, NULL);
5215 5214 if (pkt == NULL) {
5216 5215 VHCI_DEBUG(1, (CE_NOTE, NULL,
5217 5216 "!vhci_pathinfo_online: "
5218 5217 "Release init_pkt failed :%p\n",
5219 5218 (void *)pip));
5220 5219 rval = MDI_FAILURE;
5221 5220 goto failure;
5222 5221 }
5223 5222 pkt->pkt_cdbp[0] = SCMD_RELEASE;
5224 5223 pkt->pkt_time = 60;
5225 5224
5226 5225 VHCI_DEBUG(1, (CE_NOTE, NULL,
5227 5226 "!vhci_path_online: path:%p "
5228 5227 "Issued SCSI-2 RELEASE\n", (void *)pip));
5229 5228
5230 5229 /* Ignore the return value */
5231 5230 (void) vhci_do_scsi_cmd(pkt);
5232 5231 scsi_destroy_pkt(pkt);
5233 5232 }
5234 5233 }
5235 5234
5236 5235 rval = vhci_update_pathinfo(psd, pip, sfo, vlun, vhci);
5237 5236 if (rval == MDI_FAILURE) {
5238 5237 goto failure;
5239 5238 }
5240 5239
5241 5240 /* Initialize MP-API data */
5242 5241 vhci_update_mpapi_data(vhci, vlun, pip);
5243 5242
5244 5243 /*
5245 5244 * MP-API also needs the Inquiry data to be maintained in the
5246 5245 * mp_vendor_prop_t structure, so find the lun and update its
5247 5246 * structure with this data.
5248 5247 */
5249 5248 list_ptr = (mpapi_item_list_t *)vhci_get_mpapi_item(vhci, NULL,
5250 5249 MP_OBJECT_TYPE_MULTIPATH_LU, (void *)vlun);
5251 5250 ld = (mpapi_lu_data_t *)list_ptr->item->idata;
5252 5251 if (ld != NULL) {
5253 5252 bcopy(psd->sd_inq->inq_vid, ld->prop.prodInfo.vendor, 8);
5254 5253 bcopy(psd->sd_inq->inq_pid, ld->prop.prodInfo.product, 16);
5255 5254 bcopy(psd->sd_inq->inq_revision, ld->prop.prodInfo.revision, 4);
5256 5255 } else {
5257 5256 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_pathinfo_online: "
5258 5257 "mpapi_lu_data_t is NULL"));
5259 5258 }
5260 5259
5261 5260 /* create kstats for path */
5262 5261 vhci_kstat_create_pathinfo(pip);
5263 5262
5264 5263 done:
5265 5264 mutex_enter(&vhci_global_mutex);
5266 5265 cv_broadcast(&vhci_cv);
5267 5266 mutex_exit(&vhci_global_mutex);
5268 5267
5269 5268 if (vlun->svl_setcap_done) {
5270 5269 (void) vhci_pHCI_cap(ap, "sector-size",
5271 5270 vlun->svl_sector_size, 1, pip);
5272 5271 }
5273 5272
5274 5273 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_path_online: path:%p\n",
5275 5274 (void *)pip));
5276 5275
5277 5276 failure:
5278 5277 return (rval);
5279 5278 }
5280 5279
5281 5280 /*
5282 5281 * path offline handler. Release all bindings that will not be
5283 5282 * released by the normal packet transport/completion code path.
5284 5283 * Since we don't (presently) keep any bindings alive outside of
5285 5284 * the in-transport packets (which will be released on completion)
5286 5285 * there is not much to do here.
5287 5286 */
5288 5287 /* ARGSUSED */
5289 5288 static int
5290 5289 vhci_pathinfo_offline(dev_info_t *vdip, mdi_pathinfo_t *pip, int flags)
5291 5290 {
5292 5291 scsi_hba_tran_t *hba = NULL;
5293 5292 struct scsi_device *psd = NULL;
5294 5293 dev_info_t *pdip = NULL;
5295 5294 dev_info_t *cdip = NULL;
5296 5295 scsi_vhci_priv_t *svp = NULL;
5297 5296
5298 5297 ASSERT(vdip != NULL);
5299 5298 ASSERT(pip != NULL);
5300 5299
5301 5300 pdip = mdi_pi_get_phci(pip);
5302 5301 ASSERT(pdip != NULL);
5303 5302 if (pdip == NULL) {
5304 5303 VHCI_DEBUG(1, (CE_WARN, vdip, "Invalid path 0x%p: NULL "
5305 5304 "phci dip", (void *)pip));
5306 5305 return (MDI_FAILURE);
5307 5306 }
5308 5307
5309 5308 cdip = mdi_pi_get_client(pip);
5310 5309 ASSERT(cdip != NULL);
5311 5310 if (cdip == NULL) {
5312 5311 VHCI_DEBUG(1, (CE_WARN, vdip, "Invalid path 0x%p: NULL "
5313 5312 "client dip", (void *)pip));
5314 5313 return (MDI_FAILURE);
5315 5314 }
5316 5315
5317 5316 hba = ddi_get_driver_private(pdip);
5318 5317 ASSERT(hba != NULL);
5319 5318
5320 5319 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
5321 5320 if (svp == NULL) {
5322 5321 /*
5323 5322 * mdi_pathinfo node in INIT state can have vHCI private
5324 5323 * information set to null
5325 5324 */
5326 5325 VHCI_DEBUG(1, (CE_NOTE, vdip, "!vhci_pathinfo_offline: "
5327 5326 "svp is NULL for pip 0x%p\n", (void *)pip));
5328 5327 return (MDI_SUCCESS);
5329 5328 }
5330 5329
5331 5330 psd = svp->svp_psd;
5332 5331 ASSERT(psd != NULL);
5333 5332
5334 5333 mutex_enter(&svp->svp_mutex);
5335 5334
5336 5335 VHCI_DEBUG(1, (CE_NOTE, vdip, "!vhci_pathinfo_offline: "
5337 5336 "%d cmds pending on path: 0x%p\n", svp->svp_cmds, (void *)pip));
5338 5337 while (svp->svp_cmds != 0) {
5339 5338 if (cv_reltimedwait(&svp->svp_cv, &svp->svp_mutex,
5340 5339 drv_usectohz(vhci_path_quiesce_timeout * 1000000),
5341 5340 TR_CLOCK_TICK) == -1) {
5342 5341 /*
5343 5342 * The timeout time reached without the condition
5344 5343 * being signaled.
5345 5344 */
5346 5345 VHCI_DEBUG(1, (CE_NOTE, vdip, "!vhci_pathinfo_offline: "
5347 5346 "Timeout reached on path 0x%p without the cond\n",
5348 5347 (void *)pip));
5349 5348 VHCI_DEBUG(1, (CE_NOTE, vdip, "!vhci_pathinfo_offline: "
5350 5349 "%d cmds still pending on path: 0x%p\n",
5351 5350 svp->svp_cmds, (void *)pip));
5352 5351 break;
5353 5352 }
5354 5353 }
5355 5354 mutex_exit(&svp->svp_mutex);
5356 5355
5357 5356 /*
5358 5357 * Check to see if this vlun has an active SCSI-II RESERVE. And this
5359 5358 * is the pip for the path that has been reserved.
5360 5359 * If so clear the reservation by sending a reset, so the host will not
5361 5360 * get a reservation conflict. Reset the flag VLUN_RESERVE_ACTIVE_FLG
5362 5361 * for this lun. Also a reset notify is sent to the target driver
5363 5362 * just in case the POR check condition is cleared by some other layer
5364 5363 * in the stack.
5365 5364 */
5366 5365 if (svp->svp_svl->svl_flags & VLUN_RESERVE_ACTIVE_FLG) {
5367 5366 if (pip == svp->svp_svl->svl_resrv_pip) {
5368 5367 if (vhci_recovery_reset(svp->svp_svl,
5369 5368 &svp->svp_psd->sd_address, TRUE,
5370 5369 VHCI_DEPTH_TARGET) == 0) {
5371 5370 VHCI_DEBUG(1, (CE_NOTE, NULL,
5372 5371 "!vhci_pathinfo_offline (pip:%p):"
5373 5372 "reset failed, retrying\n", (void *)pip));
5374 5373 delay(1*drv_usectohz(1000000));
5375 5374 if (vhci_recovery_reset(svp->svp_svl,
5376 5375 &svp->svp_psd->sd_address, TRUE,
5377 5376 VHCI_DEPTH_TARGET) == 0) {
5378 5377 VHCI_DEBUG(1, (CE_NOTE, NULL,
5379 5378 "!vhci_pathinfo_offline "
5380 5379 "(pip:%p): reset failed, "
5381 5380 "giving up!\n", (void *)pip));
5382 5381 }
5383 5382 }
5384 5383 svp->svp_svl->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG;
5385 5384 }
5386 5385 }
5387 5386
5388 5387 mdi_pi_set_state(pip, MDI_PATHINFO_STATE_OFFLINE);
5389 5388 vhci_mpapi_set_path_state(vdip, pip, MP_DRVR_PATH_STATE_REMOVED);
5390 5389
5391 5390 VHCI_DEBUG(1, (CE_NOTE, NULL,
5392 5391 "!vhci_pathinfo_offline: offlined path 0x%p\n", (void *)pip));
5393 5392 return (MDI_SUCCESS);
5394 5393 }
5395 5394
5396 5395
5397 5396 /*
5398 5397 * routine for SCSI VHCI IOCTL implementation.
5399 5398 */
5400 5399 /* ARGSUSED */
5401 5400 static int
5402 5401 vhci_ctl(dev_t dev, int cmd, intptr_t data, int mode, cred_t *credp, int *rval)
5403 5402 {
5404 5403 struct scsi_vhci *vhci;
5405 5404 dev_info_t *vdip;
5406 5405 mdi_pathinfo_t *pip;
5407 5406 int instance, held;
5408 5407 int retval = 0;
5409 5408 caddr_t phci_path = NULL, client_path = NULL;
5410 5409 caddr_t paddr = NULL;
5411 5410 sv_iocdata_t ioc;
5412 5411 sv_iocdata_t *pioc = &ioc;
5413 5412 sv_switch_to_cntlr_iocdata_t iocsc;
5414 5413 sv_switch_to_cntlr_iocdata_t *piocsc = &iocsc;
5415 5414 caddr_t s;
5416 5415 scsi_vhci_lun_t *vlun;
5417 5416 struct scsi_failover_ops *fo;
5418 5417 char *pclass;
5419 5418
5420 5419 /* Check for validity of vhci structure */
5421 5420 vhci = ddi_get_soft_state(vhci_softstate, MINOR2INST(getminor(dev)));
5422 5421 if (vhci == NULL) {
5423 5422 return (ENXIO);
5424 5423 }
5425 5424
5426 5425 mutex_enter(&vhci->vhci_mutex);
5427 5426 if ((vhci->vhci_state & VHCI_STATE_OPEN) == 0) {
5428 5427 mutex_exit(&vhci->vhci_mutex);
5429 5428 return (ENXIO);
5430 5429 }
5431 5430 mutex_exit(&vhci->vhci_mutex);
5432 5431
5433 5432 /* Get the vhci dip */
5434 5433 vdip = vhci->vhci_dip;
5435 5434 ASSERT(vdip != NULL);
5436 5435 instance = ddi_get_instance(vdip);
5437 5436
5438 5437 /* Allocate memory for getting parameters from userland */
5439 5438 phci_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5440 5439 client_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5441 5440 paddr = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
5442 5441
5443 5442 /*
5444 5443 * Set a local variable indicating the ioctl name. Used for
5445 5444 * printing debug strings.
5446 5445 */
5447 5446 switch (cmd) {
5448 5447 case SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO:
5449 5448 s = "GET_CLIENT_MULTIPATH_INFO";
5450 5449 break;
5451 5450
5452 5451 case SCSI_VHCI_GET_PHCI_MULTIPATH_INFO:
5453 5452 s = "GET_PHCI_MULTIPATH_INFO";
5454 5453 break;
5455 5454
5456 5455 case SCSI_VHCI_GET_CLIENT_NAME:
5457 5456 s = "GET_CLIENT_NAME";
5458 5457 break;
5459 5458
5460 5459 case SCSI_VHCI_PATH_ONLINE:
5461 5460 s = "PATH_ONLINE";
5462 5461 break;
5463 5462
5464 5463 case SCSI_VHCI_PATH_OFFLINE:
5465 5464 s = "PATH_OFFLINE";
5466 5465 break;
5467 5466
5468 5467 case SCSI_VHCI_PATH_STANDBY:
5469 5468 s = "PATH_STANDBY";
5470 5469 break;
5471 5470
5472 5471 case SCSI_VHCI_PATH_TEST:
5473 5472 s = "PATH_TEST";
5474 5473 break;
5475 5474
5476 5475 case SCSI_VHCI_SWITCH_TO_CNTLR:
5477 5476 s = "SWITCH_TO_CNTLR";
5478 5477 break;
5479 5478 case SCSI_VHCI_PATH_DISABLE:
5480 5479 s = "PATH_DISABLE";
5481 5480 break;
5482 5481 case SCSI_VHCI_PATH_ENABLE:
5483 5482 s = "PATH_ENABLE";
5484 5483 break;
5485 5484
5486 5485 case SCSI_VHCI_GET_TARGET_LONGNAME:
5487 5486 s = "GET_TARGET_LONGNAME";
5488 5487 break;
5489 5488
5490 5489 #ifdef DEBUG
5491 5490 case SCSI_VHCI_CONFIGURE_PHCI:
5492 5491 s = "CONFIGURE_PHCI";
5493 5492 break;
5494 5493
5495 5494 case SCSI_VHCI_UNCONFIGURE_PHCI:
5496 5495 s = "UNCONFIGURE_PHCI";
5497 5496 break;
5498 5497 #endif
5499 5498
5500 5499 default:
5501 5500 s = "Unknown";
5502 5501 vhci_log(CE_NOTE, vdip,
5503 5502 "!vhci%d: ioctl %x (unsupported ioctl)", instance, cmd);
5504 5503 retval = ENOTSUP;
5505 5504 break;
5506 5505 }
5507 5506 if (retval != 0) {
5508 5507 goto end;
5509 5508 }
5510 5509
5511 5510 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci%d: ioctl <%s>", instance, s));
5512 5511
5513 5512 /*
5514 5513 * Get IOCTL parameters from userland
5515 5514 */
5516 5515 switch (cmd) {
5517 5516 case SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO:
5518 5517 case SCSI_VHCI_GET_PHCI_MULTIPATH_INFO:
5519 5518 case SCSI_VHCI_GET_CLIENT_NAME:
5520 5519 case SCSI_VHCI_PATH_ONLINE:
5521 5520 case SCSI_VHCI_PATH_OFFLINE:
5522 5521 case SCSI_VHCI_PATH_STANDBY:
5523 5522 case SCSI_VHCI_PATH_TEST:
5524 5523 case SCSI_VHCI_PATH_DISABLE:
5525 5524 case SCSI_VHCI_PATH_ENABLE:
5526 5525 case SCSI_VHCI_GET_TARGET_LONGNAME:
5527 5526 #ifdef DEBUG
5528 5527 case SCSI_VHCI_CONFIGURE_PHCI:
5529 5528 case SCSI_VHCI_UNCONFIGURE_PHCI:
5530 5529 #endif
5531 5530 retval = vhci_get_iocdata((const void *)data, pioc, mode, s);
5532 5531 break;
5533 5532
5534 5533 case SCSI_VHCI_SWITCH_TO_CNTLR:
5535 5534 retval = vhci_get_iocswitchdata((const void *)data, piocsc,
5536 5535 mode, s);
5537 5536 break;
5538 5537 }
5539 5538 if (retval != 0) {
5540 5539 goto end;
5541 5540 }
5542 5541
5543 5542
5544 5543 /*
5545 5544 * Process the IOCTL
5546 5545 */
5547 5546 switch (cmd) {
5548 5547 case SCSI_VHCI_GET_CLIENT_MULTIPATH_INFO:
5549 5548 {
5550 5549 uint_t num_paths; /* Num paths to client dev */
5551 5550 sv_path_info_t *upibuf = NULL; /* To keep userland values */
5552 5551 sv_path_info_t *kpibuf = NULL; /* Kernel data for ioctls */
5553 5552 dev_info_t *cdip; /* Client device dip */
5554 5553
5555 5554 if (pioc->ret_elem == NULL) {
5556 5555 retval = EINVAL;
5557 5556 break;
5558 5557 }
5559 5558
5560 5559 /* Get client device path from user land */
5561 5560 if (vhci_ioc_get_client_path(pioc, client_path, mode, s)) {
5562 5561 retval = EFAULT;
5563 5562 break;
5564 5563 }
5565 5564
5566 5565 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5567 5566 "client <%s>", s, client_path));
5568 5567
5569 5568 /* Get number of paths to this client device */
5570 5569 if ((cdip = mdi_client_path2devinfo(vdip, client_path))
5571 5570 == NULL) {
5572 5571 retval = ENXIO;
5573 5572 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5574 5573 "client dip doesn't exist. invalid path <%s>",
5575 5574 s, client_path));
5576 5575 break;
5577 5576 }
5578 5577 num_paths = mdi_client_get_path_count(cdip);
5579 5578
5580 5579 if (ddi_copyout(&num_paths, pioc->ret_elem,
5581 5580 sizeof (num_paths), mode)) {
5582 5581 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5583 5582 "num_paths copyout failed", s));
5584 5583 retval = EFAULT;
5585 5584 break;
5586 5585 }
5587 5586
5588 5587 /* If user just wanted num_paths, then return */
5589 5588 if (pioc->buf_elem == 0 || pioc->ret_buf == NULL ||
5590 5589 num_paths == 0) {
5591 5590 break;
5592 5591 }
5593 5592
5594 5593 /* Set num_paths to value as much as can be sent to userland */
5595 5594 if (num_paths > pioc->buf_elem) {
5596 5595 num_paths = pioc->buf_elem;
5597 5596 }
5598 5597
5599 5598 /* Allocate memory and get userland pointers */
5600 5599 if (vhci_ioc_alloc_pathinfo(&upibuf, &kpibuf, num_paths,
5601 5600 pioc, mode, s) != 0) {
5602 5601 retval = EFAULT;
5603 5602 break;
5604 5603 }
5605 5604 ASSERT(upibuf != NULL);
5606 5605 ASSERT(kpibuf != NULL);
5607 5606
5608 5607 /*
5609 5608 * Get the path information and send it to userland.
5610 5609 */
5611 5610 if (vhci_get_client_path_list(cdip, kpibuf, num_paths)
5612 5611 != MDI_SUCCESS) {
5613 5612 retval = ENXIO;
5614 5613 vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths);
5615 5614 break;
5616 5615 }
5617 5616
5618 5617 if (vhci_ioc_send_pathinfo(upibuf, kpibuf, num_paths,
5619 5618 pioc, mode, s)) {
5620 5619 retval = EFAULT;
5621 5620 vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths);
5622 5621 break;
5623 5622 }
5624 5623
5625 5624 /* Free the memory allocated for path information */
5626 5625 vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths);
5627 5626 break;
5628 5627 }
5629 5628
5630 5629 case SCSI_VHCI_GET_PHCI_MULTIPATH_INFO:
5631 5630 {
5632 5631 uint_t num_paths; /* Num paths to client dev */
5633 5632 sv_path_info_t *upibuf = NULL; /* To keep userland values */
5634 5633 sv_path_info_t *kpibuf = NULL; /* Kernel data for ioctls */
5635 5634 dev_info_t *pdip; /* PHCI device dip */
5636 5635
5637 5636 if (pioc->ret_elem == NULL) {
5638 5637 retval = EINVAL;
5639 5638 break;
5640 5639 }
5641 5640
5642 5641 /* Get PHCI device path from user land */
5643 5642 if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s)) {
5644 5643 retval = EFAULT;
5645 5644 break;
5646 5645 }
5647 5646
5648 5647 VHCI_DEBUG(6, (CE_WARN, vdip,
5649 5648 "!vhci_ioctl: ioctl <%s> phci <%s>", s, phci_path));
5650 5649
5651 5650 /* Get number of devices associated with this PHCI device */
5652 5651 if ((pdip = mdi_phci_path2devinfo(vdip, phci_path)) == NULL) {
5653 5652 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5654 5653 "phci dip doesn't exist. invalid path <%s>",
5655 5654 s, phci_path));
5656 5655 retval = ENXIO;
5657 5656 break;
5658 5657 }
5659 5658
5660 5659 num_paths = mdi_phci_get_path_count(pdip);
5661 5660
5662 5661 if (ddi_copyout(&num_paths, pioc->ret_elem,
5663 5662 sizeof (num_paths), mode)) {
5664 5663 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5665 5664 "num_paths copyout failed", s));
5666 5665 retval = EFAULT;
5667 5666 break;
5668 5667 }
5669 5668
5670 5669 /* If user just wanted num_paths, then return */
5671 5670 if (pioc->buf_elem == 0 || pioc->ret_buf == NULL ||
5672 5671 num_paths == 0) {
5673 5672 break;
5674 5673 }
5675 5674
5676 5675 /* Set num_paths to value as much as can be sent to userland */
5677 5676 if (num_paths > pioc->buf_elem) {
5678 5677 num_paths = pioc->buf_elem;
5679 5678 }
5680 5679
5681 5680 /* Allocate memory and get userland pointers */
5682 5681 if (vhci_ioc_alloc_pathinfo(&upibuf, &kpibuf, num_paths,
5683 5682 pioc, mode, s) != 0) {
5684 5683 retval = EFAULT;
5685 5684 break;
5686 5685 }
5687 5686 ASSERT(upibuf != NULL);
5688 5687 ASSERT(kpibuf != NULL);
5689 5688
5690 5689 /*
5691 5690 * Get the path information and send it to userland.
5692 5691 */
5693 5692 if (vhci_get_phci_path_list(pdip, kpibuf, num_paths)
5694 5693 != MDI_SUCCESS) {
5695 5694 retval = ENXIO;
5696 5695 vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths);
5697 5696 break;
5698 5697 }
5699 5698
5700 5699 if (vhci_ioc_send_pathinfo(upibuf, kpibuf, num_paths,
5701 5700 pioc, mode, s)) {
5702 5701 retval = EFAULT;
5703 5702 vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths);
5704 5703 break;
5705 5704 }
5706 5705
5707 5706 /* Free the memory allocated for path information */
5708 5707 vhci_ioc_free_pathinfo(upibuf, kpibuf, num_paths);
5709 5708 break;
5710 5709 }
5711 5710
5712 5711 case SCSI_VHCI_GET_CLIENT_NAME:
5713 5712 {
5714 5713 dev_info_t *cdip, *pdip;
5715 5714
5716 5715 /* Get PHCI path and device address from user land */
5717 5716 if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s) ||
5718 5717 vhci_ioc_get_paddr(pioc, paddr, mode, s)) {
5719 5718 retval = EFAULT;
5720 5719 break;
5721 5720 }
5722 5721
5723 5722 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5724 5723 "phci <%s>, paddr <%s>", s, phci_path, paddr));
5725 5724
5726 5725 /* Get the PHCI dip */
5727 5726 if ((pdip = mdi_phci_path2devinfo(vdip, phci_path)) == NULL) {
5728 5727 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5729 5728 "phci dip doesn't exist. invalid path <%s>",
5730 5729 s, phci_path));
5731 5730 retval = ENXIO;
5732 5731 break;
5733 5732 }
5734 5733
5735 5734 if ((pip = mdi_pi_find(pdip, NULL, paddr)) == NULL) {
5736 5735 VHCI_DEBUG(1, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5737 5736 "pathinfo doesn't exist. invalid device addr", s));
5738 5737 retval = ENXIO;
5739 5738 break;
5740 5739 }
5741 5740
5742 5741 /* Get the client device pathname and send to userland */
5743 5742 cdip = mdi_pi_get_client(pip);
5744 5743 vhci_ioc_devi_to_path(cdip, client_path);
5745 5744
5746 5745 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5747 5746 "client <%s>", s, client_path));
5748 5747
5749 5748 if (vhci_ioc_send_client_path(client_path, pioc, mode, s)) {
5750 5749 retval = EFAULT;
5751 5750 break;
5752 5751 }
5753 5752 break;
5754 5753 }
5755 5754
5756 5755 case SCSI_VHCI_PATH_ONLINE:
5757 5756 case SCSI_VHCI_PATH_OFFLINE:
5758 5757 case SCSI_VHCI_PATH_STANDBY:
5759 5758 case SCSI_VHCI_PATH_TEST:
5760 5759 {
5761 5760 dev_info_t *pdip; /* PHCI dip */
5762 5761
5763 5762 /* Get PHCI path and device address from user land */
5764 5763 if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s) ||
5765 5764 vhci_ioc_get_paddr(pioc, paddr, mode, s)) {
5766 5765 retval = EFAULT;
5767 5766 break;
5768 5767 }
5769 5768
5770 5769 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5771 5770 "phci <%s>, paddr <%s>", s, phci_path, paddr));
5772 5771
5773 5772 /* Get the PHCI dip */
5774 5773 if ((pdip = mdi_phci_path2devinfo(vdip, phci_path)) == NULL) {
5775 5774 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5776 5775 "phci dip doesn't exist. invalid path <%s>",
5777 5776 s, phci_path));
5778 5777 retval = ENXIO;
5779 5778 break;
5780 5779 }
5781 5780
5782 5781 if ((pip = mdi_pi_find(pdip, NULL, paddr)) == NULL) {
5783 5782 VHCI_DEBUG(1, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5784 5783 "pathinfo doesn't exist. invalid device addr", s));
5785 5784 retval = ENXIO;
5786 5785 break;
5787 5786 }
5788 5787
5789 5788 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5790 5789 "Calling MDI function to change device state", s));
5791 5790
5792 5791 switch (cmd) {
5793 5792 case SCSI_VHCI_PATH_ONLINE:
5794 5793 retval = mdi_pi_online(pip, 0);
5795 5794 break;
5796 5795
5797 5796 case SCSI_VHCI_PATH_OFFLINE:
5798 5797 retval = mdi_pi_offline(pip, 0);
5799 5798 break;
5800 5799
5801 5800 case SCSI_VHCI_PATH_STANDBY:
5802 5801 retval = mdi_pi_standby(pip, 0);
5803 5802 break;
5804 5803
5805 5804 case SCSI_VHCI_PATH_TEST:
5806 5805 break;
5807 5806 }
5808 5807 break;
5809 5808 }
5810 5809
5811 5810 case SCSI_VHCI_SWITCH_TO_CNTLR:
5812 5811 {
5813 5812 dev_info_t *cdip;
5814 5813 struct scsi_device *devp;
5815 5814
5816 5815 /* Get the client device pathname */
5817 5816 if (ddi_copyin(piocsc->client, client_path,
5818 5817 MAXPATHLEN, mode)) {
5819 5818 VHCI_DEBUG(2, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5820 5819 "client_path copyin failed", s));
5821 5820 retval = EFAULT;
5822 5821 break;
5823 5822 }
5824 5823
5825 5824 /* Get the path class to which user wants to switch */
5826 5825 if (ddi_copyin(piocsc->class, paddr, MAXNAMELEN, mode)) {
5827 5826 VHCI_DEBUG(2, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
5828 5827 "controller_class copyin failed", s));
5829 5828 retval = EFAULT;
5830 5829 break;
5831 5830 }
5832 5831
5833 5832 /* Perform validity checks */
5834 5833 if ((cdip = mdi_client_path2devinfo(vdip,
5835 5834 client_path)) == NULL) {
5836 5835 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5837 5836 "client dip doesn't exist. invalid path <%s>",
5838 5837 s, client_path));
5839 5838 retval = ENXIO;
5840 5839 break;
5841 5840 }
5842 5841
5843 5842 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: Calling MDI func "
5844 5843 "to switch controller"));
5845 5844 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: client <%s> "
5846 5845 "class <%s>", client_path, paddr));
5847 5846
5848 5847 if (strcmp(paddr, PCLASS_PRIMARY) &&
5849 5848 strcmp(paddr, PCLASS_SECONDARY)) {
5850 5849 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5851 5850 "invalid path class <%s>", s, paddr));
5852 5851 retval = ENXIO;
5853 5852 break;
5854 5853 }
5855 5854
5856 5855 devp = ddi_get_driver_private(cdip);
5857 5856 if (devp == NULL) {
5858 5857 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5859 5858 "invalid scsi device <%s>", s, client_path));
5860 5859 retval = ENXIO;
5861 5860 break;
5862 5861 }
5863 5862 vlun = ADDR2VLUN(&devp->sd_address);
5864 5863 ASSERT(vlun);
5865 5864
5866 5865 /*
5867 5866 * Checking to see if device has only one pclass, PRIMARY.
5868 5867 * If so this device doesn't support failovers. Assumed
5869 5868 * that the devices with one pclass is PRIMARY, as thats the
5870 5869 * case today. If this is not true and in future other
5871 5870 * symmetric devices are supported with other pclass, this
5872 5871 * IOCTL shall have to be overhauled anyways as now the only
5873 5872 * arguments it accepts are PRIMARY and SECONDARY.
5874 5873 */
5875 5874 fo = vlun->svl_fops;
5876 5875 if (fo->sfo_pathclass_next(PCLASS_PRIMARY, &pclass,
5877 5876 vlun->svl_fops_ctpriv)) {
5878 5877 retval = ENOTSUP;
5879 5878 break;
5880 5879 }
5881 5880
5882 5881 VHCI_HOLD_LUN(vlun, VH_SLEEP, held);
5883 5882 mutex_enter(&vlun->svl_mutex);
5884 5883 if (vlun->svl_active_pclass != NULL) {
5885 5884 if (strcmp(vlun->svl_active_pclass, paddr) == 0) {
5886 5885 mutex_exit(&vlun->svl_mutex);
5887 5886 retval = EALREADY;
5888 5887 VHCI_RELEASE_LUN(vlun);
5889 5888 break;
5890 5889 }
5891 5890 }
5892 5891 mutex_exit(&vlun->svl_mutex);
5893 5892 /* Call mdi function to cause a switch over */
5894 5893 retval = mdi_failover(vdip, cdip, MDI_FAILOVER_SYNC);
5895 5894 if (retval == MDI_SUCCESS) {
5896 5895 retval = 0;
5897 5896 } else if (retval == MDI_BUSY) {
5898 5897 retval = EBUSY;
5899 5898 } else {
5900 5899 retval = EIO;
5901 5900 }
5902 5901 VHCI_RELEASE_LUN(vlun);
5903 5902 break;
5904 5903 }
5905 5904
5906 5905 case SCSI_VHCI_PATH_ENABLE:
5907 5906 case SCSI_VHCI_PATH_DISABLE:
5908 5907 {
5909 5908 dev_info_t *cdip, *pdip;
5910 5909
5911 5910 /*
5912 5911 * Get client device path from user land
5913 5912 */
5914 5913 if (vhci_ioc_get_client_path(pioc, client_path, mode, s)) {
5915 5914 retval = EFAULT;
5916 5915 break;
5917 5916 }
5918 5917
5919 5918 /*
5920 5919 * Get Phci device path from user land
5921 5920 */
5922 5921 if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s)) {
5923 5922 retval = EFAULT;
5924 5923 break;
5925 5924 }
5926 5925
5927 5926 /*
5928 5927 * Get the devinfo for the Phci.
5929 5928 */
5930 5929 if ((pdip = mdi_phci_path2devinfo(vdip, phci_path)) == NULL) {
5931 5930 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5932 5931 "phci dip doesn't exist. invalid path <%s>",
5933 5932 s, phci_path));
5934 5933 retval = ENXIO;
5935 5934 break;
5936 5935 }
5937 5936
5938 5937 /*
5939 5938 * If the client path is set to /scsi_vhci then we need
5940 5939 * to do the operation on all the clients so set cdip to NULL.
5941 5940 * Else, try to get the client dip.
5942 5941 */
5943 5942 if (strcmp(client_path, "/scsi_vhci") == 0) {
5944 5943 cdip = NULL;
5945 5944 } else {
5946 5945 if ((cdip = mdi_client_path2devinfo(vdip,
5947 5946 client_path)) == NULL) {
5948 5947 retval = ENXIO;
5949 5948 VHCI_DEBUG(1, (CE_WARN, NULL,
5950 5949 "!vhci_ioctl: ioctl <%s> client dip "
5951 5950 "doesn't exist. invalid path <%s>",
5952 5951 s, client_path));
5953 5952 break;
5954 5953 }
5955 5954 }
5956 5955
5957 5956 if (cmd == SCSI_VHCI_PATH_ENABLE)
5958 5957 retval = mdi_pi_enable(cdip, pdip, USER_DISABLE);
5959 5958 else
5960 5959 retval = mdi_pi_disable(cdip, pdip, USER_DISABLE);
5961 5960
5962 5961 break;
5963 5962 }
5964 5963
5965 5964 case SCSI_VHCI_GET_TARGET_LONGNAME:
5966 5965 {
5967 5966 uint_t pid = pioc->buf_elem;
5968 5967 char *target_port;
5969 5968 mod_hash_val_t hv;
5970 5969
5971 5970 /* targetmap lookup of 'target-port' by <pid> */
5972 5971 if (mod_hash_find(vhci_targetmap_bypid,
5973 5972 (mod_hash_key_t)(uintptr_t)pid, &hv) != 0) {
5974 5973 /*
5975 5974 * NOTE: failure to find the mapping is OK for guid
5976 5975 * based 'target-port' values.
5977 5976 */
5978 5977 VHCI_DEBUG(3, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5979 5978 "targetport mapping doesn't exist: pid %d",
5980 5979 s, pid));
5981 5980 retval = ENXIO;
5982 5981 break;
5983 5982 }
5984 5983
5985 5984 /* copyout 'target-port' result */
5986 5985 target_port = (char *)hv;
5987 5986 if (copyoutstr(target_port, pioc->addr, MAXNAMELEN, NULL)) {
5988 5987 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
5989 5988 "targetport copyout failed: len: %d",
5990 5989 s, (int)strlen(target_port)));
5991 5990 retval = EFAULT;
5992 5991 }
5993 5992 break;
5994 5993 }
5995 5994
5996 5995 #ifdef DEBUG
5997 5996 case SCSI_VHCI_CONFIGURE_PHCI:
5998 5997 {
5999 5998 dev_info_t *pdip;
6000 5999
6001 6000 /* Get PHCI path and device address from user land */
6002 6001 if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s)) {
6003 6002 retval = EFAULT;
6004 6003 break;
6005 6004 }
6006 6005
6007 6006 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
6008 6007 "phci <%s>", s, phci_path));
6009 6008
6010 6009 /* Get the PHCI dip */
6011 6010 if ((pdip = e_ddi_hold_devi_by_path(phci_path, 0)) == NULL) {
6012 6011 VHCI_DEBUG(3, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
6013 6012 "phci dip doesn't exist. invalid path <%s>",
6014 6013 s, phci_path));
6015 6014 retval = ENXIO;
6016 6015 break;
6017 6016 }
6018 6017
6019 6018 if (ndi_devi_config(pdip,
6020 6019 NDI_DEVFS_CLEAN|NDI_DEVI_PERSIST) != NDI_SUCCESS) {
6021 6020 retval = EIO;
6022 6021 }
6023 6022
6024 6023 ddi_release_devi(pdip);
6025 6024 break;
6026 6025 }
6027 6026
6028 6027 case SCSI_VHCI_UNCONFIGURE_PHCI:
6029 6028 {
6030 6029 dev_info_t *pdip;
6031 6030
6032 6031 /* Get PHCI path and device address from user land */
6033 6032 if (vhci_ioc_get_phci_path(pioc, phci_path, mode, s)) {
6034 6033 retval = EFAULT;
6035 6034 break;
6036 6035 }
6037 6036
6038 6037 VHCI_DEBUG(6, (CE_WARN, vdip, "!vhci_ioctl: ioctl <%s> "
6039 6038 "phci <%s>", s, phci_path));
6040 6039
6041 6040 /* Get the PHCI dip */
6042 6041 if ((pdip = e_ddi_hold_devi_by_path(phci_path, 0)) == NULL) {
6043 6042 VHCI_DEBUG(3, (CE_WARN, NULL, "!vhci_ioctl: ioctl <%s> "
6044 6043 "phci dip doesn't exist. invalid path <%s>",
6045 6044 s, phci_path));
6046 6045 retval = ENXIO;
6047 6046 break;
6048 6047 }
6049 6048
6050 6049 if (ndi_devi_unconfig(pdip,
6051 6050 NDI_DEVI_REMOVE|NDI_DEVFS_CLEAN) != NDI_SUCCESS) {
6052 6051 retval = EBUSY;
6053 6052 }
6054 6053
6055 6054 ddi_release_devi(pdip);
6056 6055 break;
6057 6056 }
6058 6057 #endif
6059 6058 }
6060 6059
6061 6060 end:
6062 6061 /* Free the memory allocated above */
6063 6062 if (phci_path != NULL) {
6064 6063 kmem_free(phci_path, MAXPATHLEN);
6065 6064 }
6066 6065 if (client_path != NULL) {
6067 6066 kmem_free(client_path, MAXPATHLEN);
6068 6067 }
6069 6068 if (paddr != NULL) {
6070 6069 kmem_free(paddr, MAXNAMELEN);
6071 6070 }
6072 6071 return (retval);
6073 6072 }
6074 6073
6075 6074 /*
6076 6075 * devctl IOCTL support for client device DR
6077 6076 */
6078 6077 /* ARGSUSED */
6079 6078 int
6080 6079 vhci_devctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
6081 6080 int *rvalp)
6082 6081 {
6083 6082 dev_info_t *self;
6084 6083 dev_info_t *child;
6085 6084 scsi_hba_tran_t *hba;
6086 6085 struct devctl_iocdata *dcp;
6087 6086 struct scsi_vhci *vhci;
6088 6087 int rv = 0;
6089 6088 int retval = 0;
6090 6089 scsi_vhci_priv_t *svp;
6091 6090 mdi_pathinfo_t *pip;
6092 6091
6093 6092 if ((vhci = ddi_get_soft_state(vhci_softstate,
6094 6093 MINOR2INST(getminor(dev)))) == NULL)
6095 6094 return (ENXIO);
6096 6095
6097 6096 /*
6098 6097 * check if :devctl minor device has been opened
6099 6098 */
6100 6099 mutex_enter(&vhci->vhci_mutex);
6101 6100 if ((vhci->vhci_state & VHCI_STATE_OPEN) == 0) {
6102 6101 mutex_exit(&vhci->vhci_mutex);
6103 6102 return (ENXIO);
6104 6103 }
6105 6104 mutex_exit(&vhci->vhci_mutex);
6106 6105
6107 6106 self = vhci->vhci_dip;
6108 6107 hba = ddi_get_driver_private(self);
6109 6108 if (hba == NULL)
6110 6109 return (ENXIO);
6111 6110
6112 6111 /*
6113 6112 * We can use the generic implementation for these ioctls
6114 6113 */
6115 6114 switch (cmd) {
6116 6115 case DEVCTL_DEVICE_GETSTATE:
6117 6116 case DEVCTL_DEVICE_ONLINE:
6118 6117 case DEVCTL_DEVICE_OFFLINE:
6119 6118 case DEVCTL_DEVICE_REMOVE:
6120 6119 case DEVCTL_BUS_GETSTATE:
6121 6120 return (ndi_devctl_ioctl(self, cmd, arg, mode, 0));
6122 6121 }
6123 6122
6124 6123 /*
6125 6124 * read devctl ioctl data
6126 6125 */
6127 6126 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
6128 6127 return (EFAULT);
6129 6128
6130 6129 switch (cmd) {
6131 6130
6132 6131 case DEVCTL_DEVICE_RESET:
6133 6132 /*
6134 6133 * lookup and hold child device
6135 6134 */
6136 6135 if ((child = ndi_devi_find(self, ndi_dc_getname(dcp),
6137 6136 ndi_dc_getaddr(dcp))) == NULL) {
6138 6137 rv = ENXIO;
6139 6138 break;
6140 6139 }
6141 6140 retval = mdi_select_path(child, NULL,
6142 6141 (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH),
6143 6142 NULL, &pip);
6144 6143 if ((retval != MDI_SUCCESS) || (pip == NULL)) {
6145 6144 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioctl:"
6146 6145 "Unable to get a path, dip 0x%p", (void *)child));
6147 6146 rv = ENXIO;
6148 6147 break;
6149 6148 }
6150 6149 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
6151 6150 if (vhci_recovery_reset(svp->svp_svl,
6152 6151 &svp->svp_psd->sd_address, TRUE,
6153 6152 VHCI_DEPTH_TARGET) == 0) {
6154 6153 VHCI_DEBUG(1, (CE_NOTE, NULL,
6155 6154 "!vhci_ioctl(pip:%p): "
6156 6155 "reset failed\n", (void *)pip));
6157 6156 rv = ENXIO;
6158 6157 }
6159 6158 mdi_rele_path(pip);
6160 6159 break;
6161 6160
6162 6161 case DEVCTL_BUS_QUIESCE:
6163 6162 case DEVCTL_BUS_UNQUIESCE:
6164 6163 case DEVCTL_BUS_RESET:
6165 6164 case DEVCTL_BUS_RESETALL:
6166 6165 #ifdef DEBUG
6167 6166 case DEVCTL_BUS_CONFIGURE:
6168 6167 case DEVCTL_BUS_UNCONFIGURE:
6169 6168 #endif
6170 6169 rv = ENOTSUP;
6171 6170 break;
6172 6171
6173 6172 default:
6174 6173 rv = ENOTTY;
6175 6174 } /* end of outer switch */
6176 6175
6177 6176 ndi_dc_freehdl(dcp);
6178 6177 return (rv);
6179 6178 }
6180 6179
6181 6180 /*
6182 6181 * Routine to get the PHCI pathname from ioctl structures in userland
6183 6182 */
6184 6183 /* ARGSUSED */
6185 6184 static int
6186 6185 vhci_ioc_get_phci_path(sv_iocdata_t *pioc, caddr_t phci_path,
6187 6186 int mode, caddr_t s)
6188 6187 {
6189 6188 int retval = 0;
6190 6189
6191 6190 if (ddi_copyin(pioc->phci, phci_path, MAXPATHLEN, mode)) {
6192 6191 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioc_get_phci: ioctl <%s> "
6193 6192 "phci_path copyin failed", s));
6194 6193 retval = EFAULT;
6195 6194 }
6196 6195 return (retval);
6197 6196
6198 6197 }
6199 6198
6200 6199
6201 6200 /*
6202 6201 * Routine to get the Client device pathname from ioctl structures in userland
6203 6202 */
6204 6203 /* ARGSUSED */
6205 6204 static int
6206 6205 vhci_ioc_get_client_path(sv_iocdata_t *pioc, caddr_t client_path,
6207 6206 int mode, caddr_t s)
6208 6207 {
6209 6208 int retval = 0;
6210 6209
6211 6210 if (ddi_copyin(pioc->client, client_path, MAXPATHLEN, mode)) {
6212 6211 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioc_get_client: "
6213 6212 "ioctl <%s> client_path copyin failed", s));
6214 6213 retval = EFAULT;
6215 6214 }
6216 6215 return (retval);
6217 6216 }
6218 6217
6219 6218
6220 6219 /*
6221 6220 * Routine to get physical device address from ioctl structure in userland
6222 6221 */
6223 6222 /* ARGSUSED */
6224 6223 static int
6225 6224 vhci_ioc_get_paddr(sv_iocdata_t *pioc, caddr_t paddr, int mode, caddr_t s)
6226 6225 {
6227 6226 int retval = 0;
6228 6227
6229 6228 if (ddi_copyin(pioc->addr, paddr, MAXNAMELEN, mode)) {
6230 6229 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioc_get_paddr: "
6231 6230 "ioctl <%s> device addr copyin failed", s));
6232 6231 retval = EFAULT;
6233 6232 }
6234 6233 return (retval);
6235 6234 }
6236 6235
6237 6236
6238 6237 /*
6239 6238 * Routine to send client device pathname to userland.
6240 6239 */
6241 6240 /* ARGSUSED */
6242 6241 static int
6243 6242 vhci_ioc_send_client_path(caddr_t client_path, sv_iocdata_t *pioc,
6244 6243 int mode, caddr_t s)
6245 6244 {
6246 6245 int retval = 0;
6247 6246
6248 6247 if (ddi_copyout(client_path, pioc->client, MAXPATHLEN, mode)) {
6249 6248 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_ioc_send_client: "
6250 6249 "ioctl <%s> client_path copyout failed", s));
6251 6250 retval = EFAULT;
6252 6251 }
6253 6252 return (retval);
6254 6253 }
6255 6254
6256 6255
6257 6256 /*
6258 6257 * Routine to translated dev_info pointer (dip) to device pathname.
6259 6258 */
6260 6259 static void
6261 6260 vhci_ioc_devi_to_path(dev_info_t *dip, caddr_t path)
6262 6261 {
6263 6262 (void) ddi_pathname(dip, path);
6264 6263 }
6265 6264
6266 6265
6267 6266 /*
6268 6267 * vhci_get_phci_path_list:
6269 6268 * get information about devices associated with a
6270 6269 * given PHCI device.
6271 6270 *
6272 6271 * Return Values:
6273 6272 * path information elements
6274 6273 */
6275 6274 int
6276 6275 vhci_get_phci_path_list(dev_info_t *pdip, sv_path_info_t *pibuf,
6277 6276 uint_t num_elems)
6278 6277 {
6279 6278 uint_t count, done;
6280 6279 mdi_pathinfo_t *pip;
6281 6280 sv_path_info_t *ret_pip;
6282 6281 int status;
6283 6282 size_t prop_size;
6284 6283 int circular;
6285 6284
6286 6285 /*
6287 6286 * Get the PHCI structure and retrieve the path information
6288 6287 * from the GUID hash table.
6289 6288 */
6290 6289
6291 6290 ret_pip = pibuf;
6292 6291 count = 0;
6293 6292
6294 6293 ndi_devi_enter(pdip, &circular);
6295 6294
6296 6295 done = (count >= num_elems);
6297 6296 pip = mdi_get_next_client_path(pdip, NULL);
6298 6297 while (pip && !done) {
6299 6298 mdi_pi_lock(pip);
6300 6299 (void) ddi_pathname(mdi_pi_get_phci(pip),
6301 6300 ret_pip->device.ret_phci);
6302 6301 (void) strcpy(ret_pip->ret_addr, mdi_pi_get_addr(pip));
6303 6302 (void) mdi_pi_get_state2(pip, &ret_pip->ret_state,
6304 6303 &ret_pip->ret_ext_state);
6305 6304
6306 6305 status = mdi_prop_size(pip, &prop_size);
6307 6306 if (status == MDI_SUCCESS && ret_pip->ret_prop.ret_buf_size) {
6308 6307 *ret_pip->ret_prop.ret_buf_size = (uint_t)prop_size;
6309 6308 }
6310 6309
6311 6310 #ifdef DEBUG
6312 6311 if (status != MDI_SUCCESS) {
6313 6312 VHCI_DEBUG(2, (CE_WARN, NULL,
6314 6313 "!vhci_get_phci_path_list: "
6315 6314 "phci <%s>, prop size failure 0x%x",
6316 6315 ret_pip->device.ret_phci, status));
6317 6316 }
6318 6317 #endif /* DEBUG */
6319 6318
6320 6319
6321 6320 if (status == MDI_SUCCESS && ret_pip->ret_prop.buf &&
6322 6321 prop_size && ret_pip->ret_prop.buf_size >= prop_size) {
6323 6322 status = mdi_prop_pack(pip,
6324 6323 &ret_pip->ret_prop.buf,
6325 6324 ret_pip->ret_prop.buf_size);
6326 6325
6327 6326 #ifdef DEBUG
6328 6327 if (status != MDI_SUCCESS) {
6329 6328 VHCI_DEBUG(2, (CE_WARN, NULL,
6330 6329 "!vhci_get_phci_path_list: "
6331 6330 "phci <%s>, prop pack failure 0x%x",
6332 6331 ret_pip->device.ret_phci, status));
6333 6332 }
6334 6333 #endif /* DEBUG */
6335 6334 }
6336 6335
6337 6336 mdi_pi_unlock(pip);
6338 6337 pip = mdi_get_next_client_path(pdip, pip);
6339 6338 ret_pip++;
6340 6339 count++;
6341 6340 done = (count >= num_elems);
6342 6341 }
6343 6342
6344 6343 ndi_devi_exit(pdip, circular);
6345 6344
6346 6345 return (MDI_SUCCESS);
6347 6346 }
6348 6347
6349 6348
6350 6349 /*
6351 6350 * vhci_get_client_path_list:
6352 6351 * get information about various paths associated with a
6353 6352 * given client device.
6354 6353 *
6355 6354 * Return Values:
6356 6355 * path information elements
6357 6356 */
6358 6357 int
6359 6358 vhci_get_client_path_list(dev_info_t *cdip, sv_path_info_t *pibuf,
6360 6359 uint_t num_elems)
6361 6360 {
6362 6361 uint_t count, done;
6363 6362 mdi_pathinfo_t *pip;
6364 6363 sv_path_info_t *ret_pip;
6365 6364 int status;
6366 6365 size_t prop_size;
6367 6366 int circular;
6368 6367
6369 6368 ret_pip = pibuf;
6370 6369 count = 0;
6371 6370
6372 6371 ndi_devi_enter(cdip, &circular);
6373 6372
6374 6373 done = (count >= num_elems);
6375 6374 pip = mdi_get_next_phci_path(cdip, NULL);
6376 6375 while (pip && !done) {
6377 6376 mdi_pi_lock(pip);
6378 6377 (void) ddi_pathname(mdi_pi_get_phci(pip),
6379 6378 ret_pip->device.ret_phci);
6380 6379 (void) strcpy(ret_pip->ret_addr, mdi_pi_get_addr(pip));
6381 6380 (void) mdi_pi_get_state2(pip, &ret_pip->ret_state,
6382 6381 &ret_pip->ret_ext_state);
6383 6382
6384 6383 status = mdi_prop_size(pip, &prop_size);
6385 6384 if (status == MDI_SUCCESS && ret_pip->ret_prop.ret_buf_size) {
6386 6385 *ret_pip->ret_prop.ret_buf_size = (uint_t)prop_size;
6387 6386 }
6388 6387
6389 6388 #ifdef DEBUG
6390 6389 if (status != MDI_SUCCESS) {
6391 6390 VHCI_DEBUG(2, (CE_WARN, NULL,
6392 6391 "!vhci_get_client_path_list: "
6393 6392 "phci <%s>, prop size failure 0x%x",
6394 6393 ret_pip->device.ret_phci, status));
6395 6394 }
6396 6395 #endif /* DEBUG */
6397 6396
6398 6397
6399 6398 if (status == MDI_SUCCESS && ret_pip->ret_prop.buf &&
6400 6399 prop_size && ret_pip->ret_prop.buf_size >= prop_size) {
6401 6400 status = mdi_prop_pack(pip,
6402 6401 &ret_pip->ret_prop.buf,
6403 6402 ret_pip->ret_prop.buf_size);
6404 6403
6405 6404 #ifdef DEBUG
6406 6405 if (status != MDI_SUCCESS) {
6407 6406 VHCI_DEBUG(2, (CE_WARN, NULL,
6408 6407 "!vhci_get_client_path_list: "
6409 6408 "phci <%s>, prop pack failure 0x%x",
6410 6409 ret_pip->device.ret_phci, status));
6411 6410 }
6412 6411 #endif /* DEBUG */
6413 6412 }
6414 6413
6415 6414 mdi_pi_unlock(pip);
6416 6415 pip = mdi_get_next_phci_path(cdip, pip);
6417 6416 ret_pip++;
6418 6417 count++;
6419 6418 done = (count >= num_elems);
6420 6419 }
6421 6420
6422 6421 ndi_devi_exit(cdip, circular);
6423 6422
6424 6423 return (MDI_SUCCESS);
6425 6424 }
6426 6425
6427 6426
6428 6427 /*
6429 6428 * Routine to get ioctl argument structure from userland.
6430 6429 */
6431 6430 /* ARGSUSED */
6432 6431 static int
6433 6432 vhci_get_iocdata(const void *data, sv_iocdata_t *pioc, int mode, caddr_t s)
6434 6433 {
6435 6434 int retval = 0;
6436 6435
6437 6436 #ifdef _MULTI_DATAMODEL
6438 6437 switch (ddi_model_convert_from(mode & FMODELS)) {
6439 6438 case DDI_MODEL_ILP32:
6440 6439 {
6441 6440 sv_iocdata32_t ioc32;
6442 6441
6443 6442 if (ddi_copyin(data, &ioc32, sizeof (ioc32), mode)) {
6444 6443 retval = EFAULT;
6445 6444 break;
6446 6445 }
6447 6446 pioc->client = (caddr_t)(uintptr_t)ioc32.client;
6448 6447 pioc->phci = (caddr_t)(uintptr_t)ioc32.phci;
6449 6448 pioc->addr = (caddr_t)(uintptr_t)ioc32.addr;
6450 6449 pioc->buf_elem = (uint_t)ioc32.buf_elem;
6451 6450 pioc->ret_buf = (sv_path_info_t *)(uintptr_t)ioc32.ret_buf;
6452 6451 pioc->ret_elem = (uint_t *)(uintptr_t)ioc32.ret_elem;
6453 6452 break;
6454 6453 }
6455 6454
6456 6455 case DDI_MODEL_NONE:
6457 6456 if (ddi_copyin(data, pioc, sizeof (*pioc), mode)) {
6458 6457 retval = EFAULT;
6459 6458 break;
6460 6459 }
6461 6460 break;
6462 6461 }
6463 6462 #else /* _MULTI_DATAMODEL */
6464 6463 if (ddi_copyin(data, pioc, sizeof (*pioc), mode)) {
6465 6464 retval = EFAULT;
6466 6465 }
6467 6466 #endif /* _MULTI_DATAMODEL */
6468 6467
6469 6468 #ifdef DEBUG
6470 6469 if (retval) {
6471 6470 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_get_ioc: cmd <%s> "
6472 6471 "iocdata copyin failed", s));
6473 6472 }
6474 6473 #endif
6475 6474
6476 6475 return (retval);
6477 6476 }
6478 6477
6479 6478
6480 6479 /*
6481 6480 * Routine to get the ioctl argument for ioctl causing controller switchover.
6482 6481 */
6483 6482 /* ARGSUSED */
6484 6483 static int
6485 6484 vhci_get_iocswitchdata(const void *data, sv_switch_to_cntlr_iocdata_t *piocsc,
6486 6485 int mode, caddr_t s)
6487 6486 {
6488 6487 int retval = 0;
6489 6488
6490 6489 #ifdef _MULTI_DATAMODEL
6491 6490 switch (ddi_model_convert_from(mode & FMODELS)) {
6492 6491 case DDI_MODEL_ILP32:
6493 6492 {
6494 6493 sv_switch_to_cntlr_iocdata32_t ioc32;
6495 6494
6496 6495 if (ddi_copyin(data, &ioc32, sizeof (ioc32), mode)) {
6497 6496 retval = EFAULT;
6498 6497 break;
6499 6498 }
6500 6499 piocsc->client = (caddr_t)(uintptr_t)ioc32.client;
6501 6500 piocsc->class = (caddr_t)(uintptr_t)ioc32.class;
6502 6501 break;
6503 6502 }
6504 6503
6505 6504 case DDI_MODEL_NONE:
6506 6505 if (ddi_copyin(data, piocsc, sizeof (*piocsc), mode)) {
6507 6506 retval = EFAULT;
6508 6507 }
6509 6508 break;
6510 6509 }
6511 6510 #else /* _MULTI_DATAMODEL */
6512 6511 if (ddi_copyin(data, piocsc, sizeof (*piocsc), mode)) {
6513 6512 retval = EFAULT;
6514 6513 }
6515 6514 #endif /* _MULTI_DATAMODEL */
6516 6515
6517 6516 #ifdef DEBUG
6518 6517 if (retval) {
6519 6518 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_get_ioc: cmd <%s> "
6520 6519 "switch_to_cntlr_iocdata copyin failed", s));
6521 6520 }
6522 6521 #endif
6523 6522
6524 6523 return (retval);
6525 6524 }
6526 6525
6527 6526
6528 6527 /*
6529 6528 * Routine to allocate memory for the path information structures.
6530 6529 * It allocates two chunks of memory - one for keeping userland
6531 6530 * pointers/values for path information and path properties, second for
6532 6531 * keeping allocating kernel memory for path properties. These path
6533 6532 * properties are finally copied to userland.
6534 6533 */
6535 6534 /* ARGSUSED */
6536 6535 static int
6537 6536 vhci_ioc_alloc_pathinfo(sv_path_info_t **upibuf, sv_path_info_t **kpibuf,
6538 6537 uint_t num_paths, sv_iocdata_t *pioc, int mode, caddr_t s)
6539 6538 {
6540 6539 sv_path_info_t *pi;
6541 6540 uint_t bufsize;
6542 6541 int retval = 0;
6543 6542 int index;
6544 6543
6545 6544 /* Allocate memory */
6546 6545 *upibuf = (sv_path_info_t *)
6547 6546 kmem_zalloc(sizeof (sv_path_info_t) * num_paths, KM_SLEEP);
6548 6547 ASSERT(*upibuf != NULL);
6549 6548 *kpibuf = (sv_path_info_t *)
6550 6549 kmem_zalloc(sizeof (sv_path_info_t) * num_paths, KM_SLEEP);
6551 6550 ASSERT(*kpibuf != NULL);
6552 6551
6553 6552 /*
6554 6553 * Get the path info structure from the user space.
6555 6554 * We are interested in the following fields:
6556 6555 * - user size of buffer for per path properties.
6557 6556 * - user address of buffer for path info properties.
6558 6557 * - user pointer for returning actual buffer size
6559 6558 * Keep these fields in the 'upibuf' structures.
6560 6559 * Allocate buffer for per path info properties in kernel
6561 6560 * structure ('kpibuf').
6562 6561 * Size of these buffers will be equal to the size of buffers
6563 6562 * in the user space.
6564 6563 */
6565 6564 #ifdef _MULTI_DATAMODEL
6566 6565 switch (ddi_model_convert_from(mode & FMODELS)) {
6567 6566 case DDI_MODEL_ILP32:
6568 6567 {
6569 6568 sv_path_info32_t *src;
6570 6569 sv_path_info32_t pi32;
6571 6570
6572 6571 src = (sv_path_info32_t *)pioc->ret_buf;
6573 6572 pi = (sv_path_info_t *)*upibuf;
6574 6573 for (index = 0; index < num_paths; index++, src++, pi++) {
6575 6574 if (ddi_copyin(src, &pi32, sizeof (pi32), mode)) {
6576 6575 retval = EFAULT;
6577 6576 break;
6578 6577 }
6579 6578
6580 6579 pi->ret_prop.buf_size =
6581 6580 (uint_t)pi32.ret_prop.buf_size;
6582 6581 pi->ret_prop.ret_buf_size =
6583 6582 (uint_t *)(uintptr_t)pi32.ret_prop.ret_buf_size;
6584 6583 pi->ret_prop.buf =
6585 6584 (caddr_t)(uintptr_t)pi32.ret_prop.buf;
6586 6585 }
6587 6586 break;
6588 6587 }
6589 6588
6590 6589 case DDI_MODEL_NONE:
6591 6590 if (ddi_copyin(pioc->ret_buf, *upibuf,
6592 6591 sizeof (sv_path_info_t) * num_paths, mode)) {
6593 6592 retval = EFAULT;
6594 6593 }
6595 6594 break;
6596 6595 }
6597 6596 #else /* _MULTI_DATAMODEL */
6598 6597 if (ddi_copyin(pioc->ret_buf, *upibuf,
6599 6598 sizeof (sv_path_info_t) * num_paths, mode)) {
6600 6599 retval = EFAULT;
6601 6600 }
6602 6601 #endif /* _MULTI_DATAMODEL */
6603 6602
6604 6603 if (retval != 0) {
6605 6604 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_alloc_path_info: "
6606 6605 "ioctl <%s> normal: path_info copyin failed", s));
6607 6606 kmem_free(*upibuf, sizeof (sv_path_info_t) * num_paths);
6608 6607 kmem_free(*kpibuf, sizeof (sv_path_info_t) * num_paths);
6609 6608 *upibuf = NULL;
6610 6609 *kpibuf = NULL;
6611 6610 return (retval);
6612 6611 }
6613 6612
6614 6613 /*
6615 6614 * Allocate memory for per path properties.
6616 6615 */
6617 6616 for (index = 0, pi = *kpibuf; index < num_paths; index++, pi++) {
6618 6617 bufsize = (*upibuf)[index].ret_prop.buf_size;
6619 6618
6620 6619 if (bufsize && bufsize <= SV_PROP_MAX_BUF_SIZE) {
6621 6620 pi->ret_prop.buf_size = bufsize;
6622 6621 pi->ret_prop.buf = (caddr_t)
6623 6622 kmem_zalloc(bufsize, KM_SLEEP);
6624 6623 ASSERT(pi->ret_prop.buf != NULL);
6625 6624 } else {
6626 6625 pi->ret_prop.buf_size = 0;
6627 6626 pi->ret_prop.buf = NULL;
6628 6627 }
6629 6628
6630 6629 if ((*upibuf)[index].ret_prop.ret_buf_size != NULL) {
6631 6630 pi->ret_prop.ret_buf_size = (uint_t *)kmem_zalloc(
6632 6631 sizeof (*pi->ret_prop.ret_buf_size), KM_SLEEP);
6633 6632 ASSERT(pi->ret_prop.ret_buf_size != NULL);
6634 6633 } else {
6635 6634 pi->ret_prop.ret_buf_size = NULL;
6636 6635 }
6637 6636 }
6638 6637
6639 6638 return (0);
6640 6639 }
6641 6640
6642 6641
6643 6642 /*
6644 6643 * Routine to free memory for the path information structures.
6645 6644 * This is the memory which was allocated earlier.
6646 6645 */
6647 6646 /* ARGSUSED */
6648 6647 static void
6649 6648 vhci_ioc_free_pathinfo(sv_path_info_t *upibuf, sv_path_info_t *kpibuf,
6650 6649 uint_t num_paths)
6651 6650 {
6652 6651 sv_path_info_t *pi;
6653 6652 int index;
6654 6653
6655 6654 /* Free memory for per path properties */
6656 6655 for (index = 0, pi = kpibuf; index < num_paths; index++, pi++) {
6657 6656 if (pi->ret_prop.ret_buf_size != NULL) {
6658 6657 kmem_free(pi->ret_prop.ret_buf_size,
6659 6658 sizeof (*pi->ret_prop.ret_buf_size));
6660 6659 }
6661 6660
6662 6661 if (pi->ret_prop.buf != NULL) {
6663 6662 kmem_free(pi->ret_prop.buf, pi->ret_prop.buf_size);
6664 6663 }
6665 6664 }
6666 6665
6667 6666 /* Free memory for path info structures */
6668 6667 kmem_free(upibuf, sizeof (sv_path_info_t) * num_paths);
6669 6668 kmem_free(kpibuf, sizeof (sv_path_info_t) * num_paths);
6670 6669 }
6671 6670
6672 6671
6673 6672 /*
6674 6673 * Routine to copy path information and path properties to userland.
6675 6674 */
6676 6675 /* ARGSUSED */
6677 6676 static int
6678 6677 vhci_ioc_send_pathinfo(sv_path_info_t *upibuf, sv_path_info_t *kpibuf,
6679 6678 uint_t num_paths, sv_iocdata_t *pioc, int mode, caddr_t s)
6680 6679 {
6681 6680 int retval = 0, index;
6682 6681 sv_path_info_t *upi_ptr;
6683 6682 sv_path_info32_t *upi32_ptr;
6684 6683
6685 6684 #ifdef _MULTI_DATAMODEL
6686 6685 switch (ddi_model_convert_from(mode & FMODELS)) {
6687 6686 case DDI_MODEL_ILP32:
6688 6687 goto copy_32bit;
6689 6688
6690 6689 case DDI_MODEL_NONE:
6691 6690 goto copy_normal;
6692 6691 }
6693 6692 #else /* _MULTI_DATAMODEL */
6694 6693
6695 6694 goto copy_normal;
6696 6695
6697 6696 #endif /* _MULTI_DATAMODEL */
6698 6697
6699 6698 copy_normal:
6700 6699
6701 6700 /*
6702 6701 * Copy path information and path properties to user land.
6703 6702 * Pointer fields inside the path property structure were
6704 6703 * saved in the 'upibuf' structure earlier.
6705 6704 */
6706 6705 upi_ptr = pioc->ret_buf;
6707 6706 for (index = 0; index < num_paths; index++) {
6708 6707 if (ddi_copyout(kpibuf[index].device.ret_ct,
6709 6708 upi_ptr[index].device.ret_ct, MAXPATHLEN, mode)) {
6710 6709 retval = EFAULT;
6711 6710 break;
6712 6711 }
6713 6712
6714 6713 if (ddi_copyout(kpibuf[index].ret_addr,
6715 6714 upi_ptr[index].ret_addr, MAXNAMELEN, mode)) {
6716 6715 retval = EFAULT;
6717 6716 break;
6718 6717 }
6719 6718
6720 6719 if (ddi_copyout(&kpibuf[index].ret_state,
6721 6720 &upi_ptr[index].ret_state, sizeof (kpibuf[index].ret_state),
6722 6721 mode)) {
6723 6722 retval = EFAULT;
6724 6723 break;
6725 6724 }
6726 6725
6727 6726 if (ddi_copyout(&kpibuf[index].ret_ext_state,
6728 6727 &upi_ptr[index].ret_ext_state,
6729 6728 sizeof (kpibuf[index].ret_ext_state), mode)) {
6730 6729 retval = EFAULT;
6731 6730 break;
6732 6731 }
6733 6732
6734 6733 if ((kpibuf[index].ret_prop.ret_buf_size != NULL) &&
6735 6734 ddi_copyout(kpibuf[index].ret_prop.ret_buf_size,
6736 6735 upibuf[index].ret_prop.ret_buf_size,
6737 6736 sizeof (*upibuf[index].ret_prop.ret_buf_size), mode)) {
6738 6737 retval = EFAULT;
6739 6738 break;
6740 6739 }
6741 6740
6742 6741 if ((kpibuf[index].ret_prop.buf != NULL) &&
6743 6742 ddi_copyout(kpibuf[index].ret_prop.buf,
6744 6743 upibuf[index].ret_prop.buf,
6745 6744 upibuf[index].ret_prop.buf_size, mode)) {
6746 6745 retval = EFAULT;
6747 6746 break;
6748 6747 }
6749 6748 }
6750 6749
6751 6750 #ifdef DEBUG
6752 6751 if (retval) {
6753 6752 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_get_ioc: ioctl <%s> "
6754 6753 "normal: path_info copyout failed", s));
6755 6754 }
6756 6755 #endif
6757 6756
6758 6757 return (retval);
6759 6758
6760 6759 copy_32bit:
6761 6760 /*
6762 6761 * Copy path information and path properties to user land.
6763 6762 * Pointer fields inside the path property structure were
6764 6763 * saved in the 'upibuf' structure earlier.
6765 6764 */
6766 6765 upi32_ptr = (sv_path_info32_t *)pioc->ret_buf;
6767 6766 for (index = 0; index < num_paths; index++) {
6768 6767 if (ddi_copyout(kpibuf[index].device.ret_ct,
6769 6768 upi32_ptr[index].device.ret_ct, MAXPATHLEN, mode)) {
6770 6769 retval = EFAULT;
6771 6770 break;
6772 6771 }
6773 6772
6774 6773 if (ddi_copyout(kpibuf[index].ret_addr,
6775 6774 upi32_ptr[index].ret_addr, MAXNAMELEN, mode)) {
6776 6775 retval = EFAULT;
6777 6776 break;
6778 6777 }
6779 6778
6780 6779 if (ddi_copyout(&kpibuf[index].ret_state,
6781 6780 &upi32_ptr[index].ret_state,
6782 6781 sizeof (kpibuf[index].ret_state), mode)) {
6783 6782 retval = EFAULT;
6784 6783 break;
6785 6784 }
6786 6785
6787 6786 if (ddi_copyout(&kpibuf[index].ret_ext_state,
6788 6787 &upi32_ptr[index].ret_ext_state,
6789 6788 sizeof (kpibuf[index].ret_ext_state), mode)) {
6790 6789 retval = EFAULT;
6791 6790 break;
6792 6791 }
6793 6792 if ((kpibuf[index].ret_prop.ret_buf_size != NULL) &&
6794 6793 ddi_copyout(kpibuf[index].ret_prop.ret_buf_size,
6795 6794 upibuf[index].ret_prop.ret_buf_size,
6796 6795 sizeof (*upibuf[index].ret_prop.ret_buf_size), mode)) {
6797 6796 retval = EFAULT;
6798 6797 break;
6799 6798 }
6800 6799
6801 6800 if ((kpibuf[index].ret_prop.buf != NULL) &&
6802 6801 ddi_copyout(kpibuf[index].ret_prop.buf,
6803 6802 upibuf[index].ret_prop.buf,
6804 6803 upibuf[index].ret_prop.buf_size, mode)) {
6805 6804 retval = EFAULT;
6806 6805 break;
6807 6806 }
6808 6807 }
6809 6808
6810 6809 #ifdef DEBUG
6811 6810 if (retval) {
6812 6811 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_get_ioc: ioctl <%s> "
6813 6812 "normal: path_info copyout failed", s));
6814 6813 }
6815 6814 #endif
6816 6815
6817 6816 return (retval);
6818 6817 }
6819 6818
6820 6819
6821 6820 /*
6822 6821 * vhci_failover()
6823 6822 * This routine expects VHCI_HOLD_LUN before being invoked. It can be invoked
6824 6823 * as MDI_FAILOVER_ASYNC or MDI_FAILOVER_SYNC. For Asynchronous failovers
6825 6824 * this routine shall VHCI_RELEASE_LUN on exiting. For synchronous failovers
6826 6825 * it is the callers responsibility to release lun.
6827 6826 */
6828 6827
6829 6828 /* ARGSUSED */
6830 6829 static int
6831 6830 vhci_failover(dev_info_t *vdip, dev_info_t *cdip, int flags)
6832 6831 {
6833 6832 char *guid;
6834 6833 scsi_vhci_lun_t *vlun = NULL;
6835 6834 struct scsi_vhci *vhci;
6836 6835 mdi_pathinfo_t *pip, *npip;
6837 6836 char *s_pclass, *pclass1, *pclass2, *pclass;
6838 6837 char active_pclass_copy[255], *active_pclass_ptr;
6839 6838 char *ptr1, *ptr2;
6840 6839 mdi_pathinfo_state_t pi_state;
6841 6840 uint32_t pi_ext_state;
6842 6841 scsi_vhci_priv_t *svp;
6843 6842 struct scsi_device *sd;
6844 6843 struct scsi_failover_ops *sfo;
6845 6844 int sps; /* mdi_select_path() status */
6846 6845 int activation_done = 0;
6847 6846 int rval, retval = MDI_FAILURE;
6848 6847 int reserve_pending, check_condition, UA_condition;
6849 6848 struct scsi_pkt *pkt;
6850 6849 struct buf *bp;
6851 6850
6852 6851 vhci = ddi_get_soft_state(vhci_softstate, ddi_get_instance(vdip));
6853 6852 sd = ddi_get_driver_private(cdip);
6854 6853 vlun = ADDR2VLUN(&sd->sd_address);
6855 6854 ASSERT(vlun != 0);
6856 6855 ASSERT(VHCI_LUN_IS_HELD(vlun));
6857 6856 guid = vlun->svl_lun_wwn;
6858 6857 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(1): guid %s\n", guid));
6859 6858 vhci_log(CE_NOTE, vdip, "!Initiating failover for device %s "
6860 6859 "(GUID %s)", ddi_node_name(cdip), guid);
6861 6860
6862 6861 /*
6863 6862 * Lets maintain a local copy of the vlun->svl_active_pclass
6864 6863 * for the rest of the processing. Accessing the field
6865 6864 * directly in the loop below causes loop logic to break
6866 6865 * especially when the field gets updated by other threads
6867 6866 * update path status etc and causes 'paths are not currently
6868 6867 * available' condition to be declared prematurely.
6869 6868 */
6870 6869 mutex_enter(&vlun->svl_mutex);
6871 6870 if (vlun->svl_active_pclass != NULL) {
6872 6871 (void) strlcpy(active_pclass_copy, vlun->svl_active_pclass,
6873 6872 sizeof (active_pclass_copy));
6874 6873 active_pclass_ptr = &active_pclass_copy[0];
6875 6874 mutex_exit(&vlun->svl_mutex);
6876 6875 if (vhci_quiesce_paths(vdip, cdip, vlun, guid,
6877 6876 active_pclass_ptr) != 0) {
6878 6877 retval = MDI_FAILURE;
6879 6878 }
6880 6879 } else {
6881 6880 /*
6882 6881 * can happen only when the available path to device
6883 6882 * discovered is a STANDBY path.
6884 6883 */
6885 6884 mutex_exit(&vlun->svl_mutex);
6886 6885 active_pclass_copy[0] = '\0';
6887 6886 active_pclass_ptr = NULL;
6888 6887 }
6889 6888
6890 6889 sfo = vlun->svl_fops;
6891 6890 ASSERT(sfo != NULL);
6892 6891 pclass1 = s_pclass = active_pclass_ptr;
6893 6892 VHCI_DEBUG(1, (CE_NOTE, NULL, "!(%s)failing over from %s\n", guid,
6894 6893 (s_pclass == NULL ? "<none>" : s_pclass)));
6895 6894
6896 6895 next_pathclass:
6897 6896
6898 6897 rval = sfo->sfo_pathclass_next(pclass1, &pclass2,
6899 6898 vlun->svl_fops_ctpriv);
6900 6899 if (rval == ENOENT) {
6901 6900 if (s_pclass == NULL) {
6902 6901 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(4)(%s): "
6903 6902 "failed, no more pathclasses\n", guid));
6904 6903 goto done;
6905 6904 } else {
6906 6905 (void) sfo->sfo_pathclass_next(NULL, &pclass2,
6907 6906 vlun->svl_fops_ctpriv);
6908 6907 }
6909 6908 } else if (rval == EINVAL) {
6910 6909 vhci_log(CE_NOTE, vdip, "!Failover operation failed for "
6911 6910 "device %s (GUID %s): Invalid path-class %s",
6912 6911 ddi_node_name(cdip), guid,
6913 6912 ((pclass1 == NULL) ? "<none>" : pclass1));
6914 6913 goto done;
6915 6914 }
6916 6915 if ((s_pclass != NULL) && (strcmp(pclass2, s_pclass) == 0)) {
6917 6916 /*
6918 6917 * paths are not currently available
6919 6918 */
6920 6919 vhci_log(CE_NOTE, vdip, "!Failover path currently unavailable"
6921 6920 " for device %s (GUID %s)",
6922 6921 ddi_node_name(cdip), guid);
6923 6922 goto done;
6924 6923 }
6925 6924 pip = npip = NULL;
6926 6925 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(5.2)(%s): considering "
6927 6926 "%s as failover destination\n", guid, pclass2));
6928 6927 sps = mdi_select_path(cdip, NULL, MDI_SELECT_STANDBY_PATH, NULL, &npip);
6929 6928 if ((npip == NULL) || (sps != MDI_SUCCESS)) {
6930 6929 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(%s): no "
6931 6930 "STANDBY paths found (status:%x)!\n", guid, sps));
6932 6931 pclass1 = pclass2;
6933 6932 goto next_pathclass;
6934 6933 }
6935 6934 do {
6936 6935 pclass = NULL;
6937 6936 if ((mdi_prop_lookup_string(npip, "path-class",
6938 6937 &pclass) != MDI_SUCCESS) || (strcmp(pclass2,
6939 6938 pclass) != 0)) {
6940 6939 VHCI_DEBUG(1, (CE_NOTE, NULL,
6941 6940 "!vhci_failover(5.5)(%s): skipping path "
6942 6941 "%p(%s)...\n", guid, (void *)npip, pclass));
6943 6942 pip = npip;
6944 6943 sps = mdi_select_path(cdip, NULL,
6945 6944 MDI_SELECT_STANDBY_PATH, pip, &npip);
6946 6945 mdi_rele_path(pip);
6947 6946 (void) mdi_prop_free(pclass);
6948 6947 continue;
6949 6948 }
6950 6949 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(npip);
6951 6950
6952 6951 /*
6953 6952 * Issue READ at non-zer block on this STANDBY path.
6954 6953 * Purple returns
6955 6954 * 1. RESERVATION_CONFLICT if reservation is pending
6956 6955 * 2. POR check condition if it reset happened.
6957 6956 * 2. failover Check Conditions if one is already in progress.
6958 6957 */
6959 6958 reserve_pending = 0;
6960 6959 check_condition = 0;
6961 6960 UA_condition = 0;
6962 6961
6963 6962 bp = scsi_alloc_consistent_buf(&svp->svp_psd->sd_address,
6964 6963 (struct buf *)NULL, DEV_BSIZE, B_READ, NULL, NULL);
6965 6964 if (!bp) {
6966 6965 VHCI_DEBUG(1, (CE_NOTE, NULL,
6967 6966 "vhci_failover !No resources (buf)\n"));
6968 6967 mdi_rele_path(npip);
6969 6968 goto done;
6970 6969 }
6971 6970 pkt = scsi_init_pkt(&svp->svp_psd->sd_address, NULL, bp,
6972 6971 CDB_GROUP1, sizeof (struct scsi_arq_status), 0,
6973 6972 PKT_CONSISTENT, NULL, NULL);
6974 6973 if (pkt) {
6975 6974 (void) scsi_setup_cdb((union scsi_cdb *)(uintptr_t)
6976 6975 pkt->pkt_cdbp, SCMD_READ, 1, 1, 0);
6977 6976 pkt->pkt_flags = FLAG_NOINTR;
6978 6977 check_path_again:
6979 6978 pkt->pkt_path_instance = mdi_pi_get_path_instance(npip);
6980 6979 pkt->pkt_time = 3*30;
6981 6980
6982 6981 if (scsi_transport(pkt) == TRAN_ACCEPT) {
6983 6982 switch (pkt->pkt_reason) {
6984 6983 case CMD_CMPLT:
6985 6984 switch (SCBP_C(pkt)) {
6986 6985 case STATUS_GOOD:
6987 6986 /* Already failed over */
6988 6987 activation_done = 1;
6989 6988 break;
6990 6989 case STATUS_RESERVATION_CONFLICT:
6991 6990 reserve_pending = 1;
6992 6991 break;
6993 6992 case STATUS_CHECK:
6994 6993 check_condition = 1;
6995 6994 break;
6996 6995 }
6997 6996 }
6998 6997 }
6999 6998 if (check_condition &&
7000 6999 (pkt->pkt_state & STATE_ARQ_DONE)) {
7001 7000 uint8_t *sns, skey, asc, ascq;
7002 7001 sns = (uint8_t *)
7003 7002 &(((struct scsi_arq_status *)(uintptr_t)
7004 7003 (pkt->pkt_scbp))->sts_sensedata);
7005 7004 skey = scsi_sense_key(sns);
7006 7005 asc = scsi_sense_asc(sns);
7007 7006 ascq = scsi_sense_ascq(sns);
7008 7007 if (skey == KEY_UNIT_ATTENTION &&
7009 7008 asc == 0x29) {
7010 7009 /* Already failed over */
7011 7010 VHCI_DEBUG(1, (CE_NOTE, NULL,
7012 7011 "!vhci_failover(7)(%s): "
7013 7012 "path 0x%p POR UA condition\n",
7014 7013 guid, (void *)npip));
7015 7014 if (UA_condition == 0) {
7016 7015 UA_condition = 1;
7017 7016 goto check_path_again;
7018 7017 }
7019 7018 } else {
7020 7019 activation_done = 0;
7021 7020 VHCI_DEBUG(1, (CE_NOTE, NULL,
7022 7021 "!vhci_failover(%s): path 0x%p "
7023 7022 "unhandled chkcond %x %x %x\n",
7024 7023 guid, (void *)npip, skey,
7025 7024 asc, ascq));
7026 7025 }
7027 7026 }
7028 7027 scsi_destroy_pkt(pkt);
7029 7028 }
7030 7029 scsi_free_consistent_buf(bp);
7031 7030
7032 7031 if (activation_done) {
7033 7032 mdi_rele_path(npip);
7034 7033 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(7)(%s): "
7035 7034 "path 0x%p already failedover\n", guid,
7036 7035 (void *)npip));
7037 7036 break;
7038 7037 }
7039 7038 if (reserve_pending && (vlun->svl_xlf_capable == 0)) {
7040 7039 (void) vhci_recovery_reset(vlun,
7041 7040 &svp->svp_psd->sd_address,
7042 7041 FALSE, VHCI_DEPTH_ALL);
7043 7042 }
7044 7043 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(6)(%s): "
7045 7044 "activating path 0x%p(psd:%p)\n", guid, (void *)npip,
7046 7045 (void *)svp->svp_psd));
7047 7046 if (sfo->sfo_path_activate(svp->svp_psd, pclass2,
7048 7047 vlun->svl_fops_ctpriv) == 0) {
7049 7048 activation_done = 1;
7050 7049 mdi_rele_path(npip);
7051 7050 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(7)(%s): "
7052 7051 "path 0x%p successfully activated\n", guid,
7053 7052 (void *)npip));
7054 7053 break;
7055 7054 }
7056 7055 pip = npip;
7057 7056 sps = mdi_select_path(cdip, NULL, MDI_SELECT_STANDBY_PATH,
7058 7057 pip, &npip);
7059 7058 mdi_rele_path(pip);
7060 7059 } while ((npip != NULL) && (sps == MDI_SUCCESS));
7061 7060 if (activation_done == 0) {
7062 7061 pclass1 = pclass2;
7063 7062 goto next_pathclass;
7064 7063 }
7065 7064
7066 7065 /*
7067 7066 * if we are here, we have succeeded in activating path npip of
7068 7067 * pathclass pclass2; let us validate all paths of pclass2 by
7069 7068 * "ping"-ing each one and mark the good ones ONLINE
7070 7069 * Also, set the state of the paths belonging to the previously
7071 7070 * active pathclass to STANDBY
7072 7071 */
7073 7072 pip = npip = NULL;
7074 7073 sps = mdi_select_path(cdip, NULL, (MDI_SELECT_ONLINE_PATH |
7075 7074 MDI_SELECT_STANDBY_PATH | MDI_SELECT_USER_DISABLE_PATH),
7076 7075 NULL, &npip);
7077 7076 if (npip == NULL || sps != MDI_SUCCESS) {
7078 7077 VHCI_DEBUG(1, (CE_NOTE, NULL, "!Failover operation failed for "
7079 7078 "device %s (GUID %s): paths may be busy\n",
7080 7079 ddi_node_name(cdip), guid));
7081 7080 goto done;
7082 7081 }
7083 7082 do {
7084 7083 (void) mdi_pi_get_state2(npip, &pi_state, &pi_ext_state);
7085 7084 if (mdi_prop_lookup_string(npip, "path-class", &pclass)
7086 7085 != MDI_SUCCESS) {
7087 7086 pip = npip;
7088 7087 sps = mdi_select_path(cdip, NULL,
7089 7088 (MDI_SELECT_ONLINE_PATH |
7090 7089 MDI_SELECT_STANDBY_PATH |
7091 7090 MDI_SELECT_USER_DISABLE_PATH),
7092 7091 pip, &npip);
7093 7092 mdi_rele_path(pip);
7094 7093 continue;
7095 7094 }
7096 7095 if (strcmp(pclass, pclass2) == 0) {
7097 7096 if (pi_state == MDI_PATHINFO_STATE_STANDBY) {
7098 7097 svp = (scsi_vhci_priv_t *)
7099 7098 mdi_pi_get_vhci_private(npip);
7100 7099 VHCI_DEBUG(1, (CE_NOTE, NULL,
7101 7100 "!vhci_failover(8)(%s): "
7102 7101 "pinging path 0x%p\n",
7103 7102 guid, (void *)npip));
7104 7103 if (sfo->sfo_path_ping(svp->svp_psd,
7105 7104 vlun->svl_fops_ctpriv) == 1) {
7106 7105 mdi_pi_set_state(npip,
7107 7106 MDI_PATHINFO_STATE_ONLINE);
7108 7107 VHCI_DEBUG(1, (CE_NOTE, NULL,
7109 7108 "!vhci_failover(9)(%s): "
7110 7109 "path 0x%p ping successful, "
7111 7110 "marked online\n", guid,
7112 7111 (void *)npip));
7113 7112 MDI_PI_ERRSTAT(npip, MDI_PI_FAILTO);
7114 7113 }
7115 7114 }
7116 7115 } else if ((s_pclass != NULL) && (strcmp(pclass, s_pclass)
7117 7116 == 0)) {
7118 7117 if (pi_state == MDI_PATHINFO_STATE_ONLINE) {
7119 7118 mdi_pi_set_state(npip,
7120 7119 MDI_PATHINFO_STATE_STANDBY);
7121 7120 VHCI_DEBUG(1, (CE_NOTE, NULL,
7122 7121 "!vhci_failover(10)(%s): path 0x%p marked "
7123 7122 "STANDBY\n", guid, (void *)npip));
7124 7123 MDI_PI_ERRSTAT(npip, MDI_PI_FAILFROM);
7125 7124 }
7126 7125 }
7127 7126 (void) mdi_prop_free(pclass);
7128 7127 pip = npip;
7129 7128 sps = mdi_select_path(cdip, NULL, (MDI_SELECT_ONLINE_PATH |
7130 7129 MDI_SELECT_STANDBY_PATH|MDI_SELECT_USER_DISABLE_PATH),
7131 7130 pip, &npip);
7132 7131 mdi_rele_path(pip);
7133 7132 } while ((npip != NULL) && (sps == MDI_SUCCESS));
7134 7133
7135 7134 /*
7136 7135 * Update the AccessState of related MP-API TPGs
7137 7136 */
7138 7137 (void) vhci_mpapi_update_tpg_acc_state_for_lu(vhci, vlun);
7139 7138
7140 7139 vhci_log(CE_NOTE, vdip, "!Failover operation completed successfully "
7141 7140 "for device %s (GUID %s): failed over from %s to %s",
7142 7141 ddi_node_name(cdip), guid, ((s_pclass == NULL) ? "<none>" :
7143 7142 s_pclass), pclass2);
7144 7143 ptr1 = kmem_alloc(strlen(pclass2)+1, KM_SLEEP);
7145 7144 (void) strlcpy(ptr1, pclass2, (strlen(pclass2)+1));
7146 7145 mutex_enter(&vlun->svl_mutex);
7147 7146 ptr2 = vlun->svl_active_pclass;
7148 7147 vlun->svl_active_pclass = ptr1;
7149 7148 mutex_exit(&vlun->svl_mutex);
7150 7149 if (ptr2) {
7151 7150 kmem_free(ptr2, strlen(ptr2)+1);
7152 7151 }
7153 7152 mutex_enter(&vhci->vhci_mutex);
7154 7153 scsi_hba_reset_notify_callback(&vhci->vhci_mutex,
7155 7154 &vhci->vhci_reset_notify_listf);
7156 7155 /* All reservations are cleared upon these resets. */
7157 7156 vlun->svl_flags &= ~VLUN_RESERVE_ACTIVE_FLG;
7158 7157 mutex_exit(&vhci->vhci_mutex);
7159 7158 VHCI_DEBUG(1, (CE_NOTE, NULL, "!vhci_failover(11): DONE! Active "
7160 7159 "pathclass for %s is now %s\n", guid, pclass2));
7161 7160 retval = MDI_SUCCESS;
7162 7161
7163 7162 done:
7164 7163 vlun->svl_failover_status = retval;
7165 7164 if (flags == MDI_FAILOVER_ASYNC) {
7166 7165 VHCI_RELEASE_LUN(vlun);
7167 7166 VHCI_DEBUG(6, (CE_NOTE, NULL, "!vhci_failover(12): DONE! "
7168 7167 "releasing lun, as failover was ASYNC\n"));
7169 7168 } else {
7170 7169 VHCI_DEBUG(6, (CE_NOTE, NULL, "!vhci_failover(12): DONE! "
7171 7170 "NOT releasing lun, as failover was SYNC\n"));
7172 7171 }
7173 7172 return (retval);
7174 7173 }
7175 7174
7176 7175 /*
7177 7176 * vhci_client_attached is called after the successful attach of a
7178 7177 * client devinfo node.
7179 7178 */
7180 7179 static void
7181 7180 vhci_client_attached(dev_info_t *cdip)
7182 7181 {
7183 7182 mdi_pathinfo_t *pip;
7184 7183 int circular;
7185 7184
7186 7185 /*
7187 7186 * At this point the client has attached and it's instance number is
7188 7187 * valid, so we can set up kstats. We need to do this here because it
7189 7188 * is possible for paths to go online prior to client attach, in which
7190 7189 * case the call to vhci_kstat_create_pathinfo in vhci_pathinfo_online
7191 7190 * was a noop.
7192 7191 */
7193 7192 ndi_devi_enter(cdip, &circular);
7194 7193 for (pip = mdi_get_next_phci_path(cdip, NULL); pip;
7195 7194 pip = mdi_get_next_phci_path(cdip, pip))
7196 7195 vhci_kstat_create_pathinfo(pip);
7197 7196 ndi_devi_exit(cdip, circular);
7198 7197 }
7199 7198
7200 7199 /*
7201 7200 * quiesce all of the online paths
7202 7201 */
7203 7202 static int
7204 7203 vhci_quiesce_paths(dev_info_t *vdip, dev_info_t *cdip, scsi_vhci_lun_t *vlun,
7205 7204 char *guid, char *active_pclass_ptr)
7206 7205 {
7207 7206 scsi_vhci_priv_t *svp;
7208 7207 char *s_pclass = NULL;
7209 7208 mdi_pathinfo_t *npip, *pip;
7210 7209 int sps;
7211 7210
7212 7211 /* quiesce currently active paths */
7213 7212 s_pclass = NULL;
7214 7213 pip = npip = NULL;
7215 7214 sps = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH, NULL, &npip);
7216 7215 if ((npip == NULL) || (sps != MDI_SUCCESS)) {
7217 7216 return (1);
7218 7217 }
7219 7218 do {
7220 7219 if (mdi_prop_lookup_string(npip, "path-class",
7221 7220 &s_pclass) != MDI_SUCCESS) {
7222 7221 mdi_rele_path(npip);
7223 7222 vhci_log(CE_NOTE, vdip, "!Failover operation failed "
7224 7223 "for device %s (GUID %s) due to an internal "
7225 7224 "error", ddi_node_name(cdip), guid);
7226 7225 return (1);
7227 7226 }
7228 7227 if (strcmp(s_pclass, active_pclass_ptr) == 0) {
7229 7228 /*
7230 7229 * quiesce path. Free s_pclass since
7231 7230 * we don't need it anymore
7232 7231 */
7233 7232 VHCI_DEBUG(1, (CE_NOTE, NULL,
7234 7233 "!vhci_failover(2)(%s): failing over "
7235 7234 "from %s; quiescing path %p\n",
7236 7235 guid, s_pclass, (void *)npip));
7237 7236 (void) mdi_prop_free(s_pclass);
7238 7237 svp = (scsi_vhci_priv_t *)
7239 7238 mdi_pi_get_vhci_private(npip);
7240 7239 if (svp == NULL) {
7241 7240 VHCI_DEBUG(1, (CE_NOTE, NULL,
7242 7241 "!vhci_failover(2.5)(%s): no "
7243 7242 "client priv! %p offlined?\n",
7244 7243 guid, (void *)npip));
7245 7244 pip = npip;
7246 7245 sps = mdi_select_path(cdip, NULL,
7247 7246 MDI_SELECT_ONLINE_PATH, pip, &npip);
7248 7247 mdi_rele_path(pip);
7249 7248 continue;
7250 7249 }
7251 7250 if (scsi_abort(&svp->svp_psd->sd_address, NULL)
7252 7251 == 0) {
7253 7252 (void) vhci_recovery_reset(vlun,
7254 7253 &svp->svp_psd->sd_address, FALSE,
7255 7254 VHCI_DEPTH_TARGET);
7256 7255 }
7257 7256 mutex_enter(&svp->svp_mutex);
7258 7257 if (svp->svp_cmds == 0) {
7259 7258 VHCI_DEBUG(1, (CE_NOTE, NULL,
7260 7259 "!vhci_failover(3)(%s):"
7261 7260 "quiesced path %p\n", guid, (void *)npip));
7262 7261 } else {
7263 7262 while (svp->svp_cmds != 0) {
7264 7263 cv_wait(&svp->svp_cv, &svp->svp_mutex);
7265 7264 VHCI_DEBUG(1, (CE_NOTE, NULL,
7266 7265 "!vhci_failover(3.cv)(%s):"
7267 7266 "quiesced path %p\n", guid,
7268 7267 (void *)npip));
7269 7268 }
7270 7269 }
7271 7270 mutex_exit(&svp->svp_mutex);
7272 7271 } else {
7273 7272 /*
7274 7273 * make sure we freeup the memory
7275 7274 */
7276 7275 (void) mdi_prop_free(s_pclass);
7277 7276 }
7278 7277 pip = npip;
7279 7278 sps = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH,
7280 7279 pip, &npip);
7281 7280 mdi_rele_path(pip);
7282 7281 } while ((npip != NULL) && (sps == MDI_SUCCESS));
7283 7282 return (0);
7284 7283 }
7285 7284
7286 7285 static struct scsi_vhci_lun *
7287 7286 vhci_lun_lookup(dev_info_t *tgt_dip)
7288 7287 {
7289 7288 return ((struct scsi_vhci_lun *)
7290 7289 mdi_client_get_vhci_private(tgt_dip));
7291 7290 }
7292 7291
7293 7292 static struct scsi_vhci_lun *
7294 7293 vhci_lun_lookup_alloc(dev_info_t *tgt_dip, char *guid, int *didalloc)
7295 7294 {
7296 7295 struct scsi_vhci_lun *svl;
7297 7296
7298 7297 if (svl = vhci_lun_lookup(tgt_dip)) {
7299 7298 return (svl);
7300 7299 }
7301 7300
7302 7301 svl = kmem_zalloc(sizeof (*svl), KM_SLEEP);
7303 7302 svl->svl_lun_wwn = kmem_zalloc(strlen(guid)+1, KM_SLEEP);
7304 7303 (void) strcpy(svl->svl_lun_wwn, guid);
7305 7304 mutex_init(&svl->svl_mutex, NULL, MUTEX_DRIVER, NULL);
7306 7305 cv_init(&svl->svl_cv, NULL, CV_DRIVER, NULL);
7307 7306 sema_init(&svl->svl_pgr_sema, 1, NULL, SEMA_DRIVER, NULL);
7308 7307 svl->svl_waiting_for_activepath = 1;
7309 7308 svl->svl_sector_size = 1;
7310 7309 mdi_client_set_vhci_private(tgt_dip, svl);
7311 7310 *didalloc = 1;
7312 7311 VHCI_DEBUG(1, (CE_NOTE, NULL,
7313 7312 "vhci_lun_lookup_alloc: guid %s vlun 0x%p\n",
7314 7313 guid, (void *)svl));
7315 7314 return (svl);
7316 7315 }
7317 7316
7318 7317 static void
7319 7318 vhci_lun_free(struct scsi_vhci_lun *dvlp, struct scsi_device *sd)
7320 7319 {
7321 7320 char *guid;
7322 7321
7323 7322 guid = dvlp->svl_lun_wwn;
7324 7323 ASSERT(guid != NULL);
7325 7324 VHCI_DEBUG(4, (CE_NOTE, NULL, "!vhci_lun_free: %s\n", guid));
7326 7325
7327 7326 mutex_enter(&dvlp->svl_mutex);
7328 7327 if (dvlp->svl_active_pclass != NULL) {
7329 7328 kmem_free(dvlp->svl_active_pclass,
7330 7329 strlen(dvlp->svl_active_pclass)+1);
7331 7330 }
7332 7331 dvlp->svl_active_pclass = NULL;
7333 7332 mutex_exit(&dvlp->svl_mutex);
7334 7333
7335 7334 if (dvlp->svl_lun_wwn != NULL) {
7336 7335 kmem_free(dvlp->svl_lun_wwn, strlen(dvlp->svl_lun_wwn)+1);
7337 7336 }
7338 7337 dvlp->svl_lun_wwn = NULL;
7339 7338
7340 7339 if (dvlp->svl_fops_name) {
7341 7340 kmem_free(dvlp->svl_fops_name, strlen(dvlp->svl_fops_name)+1);
7342 7341 }
7343 7342 dvlp->svl_fops_name = NULL;
7344 7343
7345 7344 if (dvlp->svl_fops_ctpriv != NULL &&
7346 7345 dvlp->svl_fops != NULL) {
7347 7346 dvlp->svl_fops->sfo_device_unprobe(sd, dvlp->svl_fops_ctpriv);
7348 7347 }
7349 7348
7350 7349 if (dvlp->svl_flags & VLUN_TASK_D_ALIVE_FLG)
7351 7350 taskq_destroy(dvlp->svl_taskq);
7352 7351
7353 7352 mutex_destroy(&dvlp->svl_mutex);
7354 7353 cv_destroy(&dvlp->svl_cv);
7355 7354 sema_destroy(&dvlp->svl_pgr_sema);
7356 7355 kmem_free(dvlp, sizeof (*dvlp));
7357 7356 /*
7358 7357 * vhci_lun_free may be called before the tgt_dip
7359 7358 * initialization so check if the sd is NULL.
7360 7359 */
7361 7360 if (sd != NULL)
7362 7361 scsi_device_hba_private_set(sd, NULL);
7363 7362 }
7364 7363
7365 7364 int
7366 7365 vhci_do_scsi_cmd(struct scsi_pkt *pkt)
7367 7366 {
7368 7367 int err = 0;
7369 7368 int retry_cnt = 0;
7370 7369 uint8_t *sns, skey;
7371 7370
7372 7371 #ifdef DEBUG
7373 7372 if (vhci_debug > 5) {
7374 7373 vhci_print_cdb(pkt->pkt_address.a_hba_tran->tran_hba_dip,
7375 7374 CE_WARN, "Vhci command", pkt->pkt_cdbp);
7376 7375 }
7377 7376 #endif
7378 7377
7379 7378 retry:
7380 7379 err = scsi_poll(pkt);
7381 7380 if (err) {
7382 7381 if (pkt->pkt_cdbp[0] == SCMD_RELEASE) {
7383 7382 if (SCBP_C(pkt) == STATUS_RESERVATION_CONFLICT) {
7384 7383 VHCI_DEBUG(1, (CE_NOTE, NULL,
7385 7384 "!v_s_do_s_c: RELEASE conflict\n"));
7386 7385 return (0);
7387 7386 }
7388 7387 }
7389 7388 if (retry_cnt++ < 6) {
7390 7389 VHCI_DEBUG(1, (CE_WARN, NULL,
7391 7390 "!v_s_do_s_c:retry packet 0x%p "
7392 7391 "status 0x%x reason %s",
7393 7392 (void *)pkt, SCBP_C(pkt),
7394 7393 scsi_rname(pkt->pkt_reason)));
7395 7394 if ((pkt->pkt_reason == CMD_CMPLT) &&
7396 7395 (SCBP_C(pkt) == STATUS_CHECK) &&
7397 7396 (pkt->pkt_state & STATE_ARQ_DONE)) {
7398 7397 sns = (uint8_t *)
7399 7398 &(((struct scsi_arq_status *)(uintptr_t)
7400 7399 (pkt->pkt_scbp))->sts_sensedata);
7401 7400 skey = scsi_sense_key(sns);
7402 7401 VHCI_DEBUG(1, (CE_WARN, NULL,
7403 7402 "!v_s_do_s_c:retry "
7404 7403 "packet 0x%p sense data %s", (void *)pkt,
7405 7404 scsi_sname(skey)));
7406 7405 }
7407 7406 goto retry;
7408 7407 }
7409 7408 VHCI_DEBUG(1, (CE_WARN, NULL,
7410 7409 "!v_s_do_s_c: failed transport 0x%p 0x%x",
7411 7410 (void *)pkt, SCBP_C(pkt)));
7412 7411 return (0);
7413 7412 }
7414 7413
7415 7414 switch (pkt->pkt_reason) {
7416 7415 case CMD_TIMEOUT:
7417 7416 VHCI_DEBUG(1, (CE_WARN, NULL, "!pkt timed "
7418 7417 "out (pkt 0x%p)", (void *)pkt));
7419 7418 return (0);
7420 7419 case CMD_CMPLT:
7421 7420 switch (SCBP_C(pkt)) {
7422 7421 case STATUS_GOOD:
7423 7422 break;
7424 7423 case STATUS_CHECK:
7425 7424 if (pkt->pkt_state & STATE_ARQ_DONE) {
7426 7425 sns = (uint8_t *)&(((
7427 7426 struct scsi_arq_status *)
7428 7427 (uintptr_t)
7429 7428 (pkt->pkt_scbp))->
7430 7429 sts_sensedata);
7431 7430 skey = scsi_sense_key(sns);
7432 7431 if ((skey ==
7433 7432 KEY_UNIT_ATTENTION) ||
7434 7433 (skey ==
7435 7434 KEY_NOT_READY)) {
7436 7435 /*
7437 7436 * clear unit attn.
7438 7437 */
7439 7438
7440 7439 VHCI_DEBUG(1,
7441 7440 (CE_WARN, NULL,
7442 7441 "!v_s_do_s_c: "
7443 7442 "retry "
7444 7443 "packet 0x%p sense "
7445 7444 "data %s",
7446 7445 (void *)pkt,
7447 7446 scsi_sname
7448 7447 (skey)));
7449 7448 goto retry;
7450 7449 }
7451 7450 VHCI_DEBUG(4, (CE_WARN, NULL,
7452 7451 "!ARQ while "
7453 7452 "transporting "
7454 7453 "(pkt 0x%p)",
7455 7454 (void *)pkt));
7456 7455 return (0);
7457 7456 }
7458 7457 return (0);
7459 7458 default:
7460 7459 VHCI_DEBUG(1, (CE_WARN, NULL,
7461 7460 "!Bad status returned "
7462 7461 "(pkt 0x%p, status %x)",
7463 7462 (void *)pkt, SCBP_C(pkt)));
7464 7463 return (0);
7465 7464 }
7466 7465 break;
7467 7466 case CMD_INCOMPLETE:
7468 7467 case CMD_RESET:
7469 7468 case CMD_ABORTED:
7470 7469 case CMD_TRAN_ERR:
7471 7470 if (retry_cnt++ < 1) {
7472 7471 VHCI_DEBUG(1, (CE_WARN, NULL,
7473 7472 "!v_s_do_s_c: retry packet 0x%p %s",
7474 7473 (void *)pkt, scsi_rname(pkt->pkt_reason)));
7475 7474 goto retry;
7476 7475 }
7477 7476 /* FALLTHROUGH */
7478 7477 default:
7479 7478 VHCI_DEBUG(1, (CE_WARN, NULL, "!pkt did not "
7480 7479 "complete successfully (pkt 0x%p,"
7481 7480 "reason %x)", (void *)pkt, pkt->pkt_reason));
7482 7481 return (0);
7483 7482 }
7484 7483 return (1);
7485 7484 }
7486 7485
7487 7486 static int
7488 7487 vhci_quiesce_lun(struct scsi_vhci_lun *vlun)
7489 7488 {
7490 7489 mdi_pathinfo_t *pip, *spip;
7491 7490 dev_info_t *cdip;
7492 7491 struct scsi_vhci_priv *svp;
7493 7492 mdi_pathinfo_state_t pstate;
7494 7493 uint32_t p_ext_state;
7495 7494 int circular;
7496 7495
7497 7496 cdip = vlun->svl_dip;
7498 7497 pip = spip = NULL;
7499 7498 ndi_devi_enter(cdip, &circular);
7500 7499 pip = mdi_get_next_phci_path(cdip, NULL);
7501 7500 while (pip != NULL) {
7502 7501 (void) mdi_pi_get_state2(pip, &pstate, &p_ext_state);
7503 7502 if (pstate != MDI_PATHINFO_STATE_ONLINE) {
7504 7503 spip = pip;
7505 7504 pip = mdi_get_next_phci_path(cdip, spip);
7506 7505 continue;
7507 7506 }
7508 7507 mdi_hold_path(pip);
7509 7508 ndi_devi_exit(cdip, circular);
7510 7509 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
7511 7510 mutex_enter(&svp->svp_mutex);
7512 7511 while (svp->svp_cmds != 0) {
7513 7512 if (cv_reltimedwait(&svp->svp_cv, &svp->svp_mutex,
7514 7513 drv_usectohz(vhci_path_quiesce_timeout * 1000000),
7515 7514 TR_CLOCK_TICK) == -1) {
7516 7515 mutex_exit(&svp->svp_mutex);
7517 7516 mdi_rele_path(pip);
7518 7517 VHCI_DEBUG(1, (CE_WARN, NULL,
7519 7518 "Quiesce of lun is not successful "
7520 7519 "vlun: 0x%p.", (void *)vlun));
7521 7520 return (0);
7522 7521 }
7523 7522 }
7524 7523 mutex_exit(&svp->svp_mutex);
7525 7524 ndi_devi_enter(cdip, &circular);
7526 7525 spip = pip;
7527 7526 pip = mdi_get_next_phci_path(cdip, spip);
7528 7527 mdi_rele_path(spip);
7529 7528 }
7530 7529 ndi_devi_exit(cdip, circular);
7531 7530 return (1);
7532 7531 }
7533 7532
7534 7533 static int
7535 7534 vhci_pgr_validate_and_register(scsi_vhci_priv_t *svp)
7536 7535 {
7537 7536 scsi_vhci_lun_t *vlun;
7538 7537 vhci_prout_t *prout;
7539 7538 int rval, success;
7540 7539 mdi_pathinfo_t *pip, *npip;
7541 7540 scsi_vhci_priv_t *osvp;
7542 7541 dev_info_t *cdip;
7543 7542 uchar_t cdb_1;
7544 7543 uchar_t temp_res_key[MHIOC_RESV_KEY_SIZE];
7545 7544
7546 7545
7547 7546 /*
7548 7547 * see if there are any other paths available; if none,
7549 7548 * then there is nothing to do.
7550 7549 */
7551 7550 cdip = svp->svp_svl->svl_dip;
7552 7551 rval = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH |
7553 7552 MDI_SELECT_STANDBY_PATH, NULL, &pip);
7554 7553 if ((rval != MDI_SUCCESS) || (pip == NULL)) {
7555 7554 VHCI_DEBUG(4, (CE_NOTE, NULL,
7556 7555 "%s%d: vhci_pgr_validate_and_register: first path\n",
7557 7556 ddi_driver_name(cdip), ddi_get_instance(cdip)));
7558 7557 return (1);
7559 7558 }
7560 7559
7561 7560 vlun = svp->svp_svl;
7562 7561 prout = &vlun->svl_prout;
7563 7562 ASSERT(vlun->svl_pgr_active != 0);
7564 7563
7565 7564 /*
7566 7565 * When the path was busy/offlined, some other host might have
7567 7566 * cleared this key. Validate key on some other path first.
7568 7567 * If it fails, return failure.
7569 7568 */
7570 7569
7571 7570 npip = pip;
7572 7571 pip = NULL;
7573 7572 success = 0;
7574 7573
7575 7574 /* Save the res key */
7576 7575 bcopy(prout->res_key, temp_res_key, MHIOC_RESV_KEY_SIZE);
7577 7576
7578 7577 /*
7579 7578 * Sometimes CDB from application can be a Register_And_Ignore.
7580 7579 * Instead of validation, this cdb would result in force registration.
7581 7580 * Convert it to normal cdb for validation.
7582 7581 * After that be sure to restore the cdb.
7583 7582 */
7584 7583 cdb_1 = vlun->svl_cdb[1];
7585 7584 vlun->svl_cdb[1] &= 0xe0;
7586 7585
7587 7586 do {
7588 7587 osvp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(npip);
7589 7588 if (osvp == NULL) {
7590 7589 VHCI_DEBUG(4, (CE_NOTE, NULL,
7591 7590 "vhci_pgr_validate_and_register: no "
7592 7591 "client priv! 0x%p offlined?\n",
7593 7592 (void *)npip));
7594 7593 goto next_path_1;
7595 7594 }
7596 7595
7597 7596 if (osvp == svp) {
7598 7597 VHCI_DEBUG(4, (CE_NOTE, NULL,
7599 7598 "vhci_pgr_validate_and_register: same svp 0x%p"
7600 7599 " npip 0x%p vlun 0x%p\n",
7601 7600 (void *)svp, (void *)npip, (void *)vlun));
7602 7601 goto next_path_1;
7603 7602 }
7604 7603
7605 7604 VHCI_DEBUG(4, (CE_NOTE, NULL,
7606 7605 "vhci_pgr_validate_and_register: First validate on"
7607 7606 " osvp 0x%p being done. vlun 0x%p thread 0x%p Before bcopy"
7608 7607 " cdb1 %x\n", (void *)osvp, (void *)vlun,
7609 7608 (void *)curthread, vlun->svl_cdb[1]));
7610 7609 vhci_print_prout_keys(vlun, "v_pgr_val_reg: before bcopy:");
7611 7610
7612 7611 bcopy(prout->service_key, prout->res_key, MHIOC_RESV_KEY_SIZE);
7613 7612
7614 7613 VHCI_DEBUG(4, (CE_WARN, NULL, "vlun 0x%p After bcopy",
7615 7614 (void *)vlun));
7616 7615 vhci_print_prout_keys(vlun, "v_pgr_val_reg: after bcopy: ");
7617 7616
7618 7617 rval = vhci_do_prout(osvp);
7619 7618 if (rval == 1) {
7620 7619 VHCI_DEBUG(4, (CE_NOTE, NULL,
7621 7620 "%s%d: vhci_pgr_validate_and_register: key"
7622 7621 " validated thread 0x%p\n", ddi_driver_name(cdip),
7623 7622 ddi_get_instance(cdip), (void *)curthread));
7624 7623 pip = npip;
7625 7624 success = 1;
7626 7625 break;
7627 7626 } else {
7628 7627 VHCI_DEBUG(4, (CE_NOTE, NULL,
7629 7628 "vhci_pgr_validate_and_register: First validation"
7630 7629 " on osvp 0x%p failed %x\n", (void *)osvp, rval));
7631 7630 vhci_print_prout_keys(vlun, "v_pgr_val_reg: failed:");
7632 7631 }
7633 7632
7634 7633 /*
7635 7634 * Try other paths
7636 7635 */
7637 7636 next_path_1:
7638 7637 pip = npip;
7639 7638 rval = mdi_select_path(cdip, NULL,
7640 7639 MDI_SELECT_ONLINE_PATH|MDI_SELECT_STANDBY_PATH,
7641 7640 pip, &npip);
7642 7641 mdi_rele_path(pip);
7643 7642 } while ((rval == MDI_SUCCESS) && (npip != NULL));
7644 7643
7645 7644
7646 7645 /* Be sure to restore original cdb */
7647 7646 vlun->svl_cdb[1] = cdb_1;
7648 7647
7649 7648 /* Restore the res_key */
7650 7649 bcopy(temp_res_key, prout->res_key, MHIOC_RESV_KEY_SIZE);
7651 7650
7652 7651 /*
7653 7652 * If key could not be registered on any path for the first time,
7654 7653 * return success as online should still continue.
7655 7654 */
7656 7655 if (success == 0) {
7657 7656 return (1);
7658 7657 }
7659 7658
7660 7659 ASSERT(pip != NULL);
7661 7660
7662 7661 /*
7663 7662 * Force register on new path
7664 7663 */
7665 7664 cdb_1 = vlun->svl_cdb[1]; /* store the cdb */
7666 7665
7667 7666 vlun->svl_cdb[1] &= 0xe0;
7668 7667 vlun->svl_cdb[1] |= VHCI_PROUT_R_AND_IGNORE;
7669 7668
7670 7669 vhci_print_prout_keys(vlun, "v_pgr_val_reg: keys before bcopy: ");
7671 7670
7672 7671 bcopy(prout->active_service_key, prout->service_key,
7673 7672 MHIOC_RESV_KEY_SIZE);
7674 7673 bcopy(prout->active_res_key, prout->res_key, MHIOC_RESV_KEY_SIZE);
7675 7674
7676 7675 vhci_print_prout_keys(vlun, "v_pgr_val_reg:keys after bcopy: ");
7677 7676
7678 7677 rval = vhci_do_prout(svp);
7679 7678 vlun->svl_cdb[1] = cdb_1; /* restore the cdb */
7680 7679 if (rval != 1) {
7681 7680 VHCI_DEBUG(4, (CE_NOTE, NULL,
7682 7681 "vhci_pgr_validate_and_register: register on new"
7683 7682 " path 0x%p svp 0x%p failed %x\n",
7684 7683 (void *)pip, (void *)svp, rval));
7685 7684 vhci_print_prout_keys(vlun, "v_pgr_val_reg: reg failed: ");
7686 7685 mdi_rele_path(pip);
7687 7686 return (0);
7688 7687 }
7689 7688
7690 7689 if (bcmp(prout->service_key, zero_key, MHIOC_RESV_KEY_SIZE) == 0) {
7691 7690 VHCI_DEBUG(4, (CE_NOTE, NULL,
7692 7691 "vhci_pgr_validate_and_register: zero service key\n"));
7693 7692 mdi_rele_path(pip);
7694 7693 return (rval);
7695 7694 }
7696 7695
7697 7696 /*
7698 7697 * While the key was force registered, some other host might have
7699 7698 * cleared the key. Re-validate key on another pre-existing path
7700 7699 * before declaring success.
7701 7700 */
7702 7701 npip = pip;
7703 7702 pip = NULL;
7704 7703
7705 7704 /*
7706 7705 * Sometimes CDB from application can be Register and Ignore.
7707 7706 * Instead of validation, it would result in force registration.
7708 7707 * Convert it to normal cdb for validation.
7709 7708 * After that be sure to restore the cdb.
7710 7709 */
7711 7710 cdb_1 = vlun->svl_cdb[1];
7712 7711 vlun->svl_cdb[1] &= 0xe0;
7713 7712 success = 0;
7714 7713
7715 7714 do {
7716 7715 osvp = (scsi_vhci_priv_t *)
7717 7716 mdi_pi_get_vhci_private(npip);
7718 7717 if (osvp == NULL) {
7719 7718 VHCI_DEBUG(4, (CE_NOTE, NULL,
7720 7719 "vhci_pgr_validate_and_register: no "
7721 7720 "client priv! 0x%p offlined?\n",
7722 7721 (void *)npip));
7723 7722 goto next_path_2;
7724 7723 }
7725 7724
7726 7725 if (osvp == svp) {
7727 7726 VHCI_DEBUG(4, (CE_NOTE, NULL,
7728 7727 "vhci_pgr_validate_and_register: same osvp 0x%p"
7729 7728 " npip 0x%p vlun 0x%p\n",
7730 7729 (void *)svp, (void *)npip, (void *)vlun));
7731 7730 goto next_path_2;
7732 7731 }
7733 7732
7734 7733 VHCI_DEBUG(4, (CE_NOTE, NULL,
7735 7734 "vhci_pgr_validate_and_register: Re-validation on"
7736 7735 " osvp 0x%p being done. vlun 0x%p Before bcopy cdb1 %x\n",
7737 7736 (void *)osvp, (void *)vlun, vlun->svl_cdb[1]));
7738 7737 vhci_print_prout_keys(vlun, "v_pgr_val_reg: before bcopy: ");
7739 7738
7740 7739 bcopy(prout->service_key, prout->res_key, MHIOC_RESV_KEY_SIZE);
7741 7740
7742 7741 vhci_print_prout_keys(vlun, "v_pgr_val_reg: after bcopy: ");
7743 7742
7744 7743 rval = vhci_do_prout(osvp);
7745 7744 if (rval == 1) {
7746 7745 VHCI_DEBUG(4, (CE_NOTE, NULL,
7747 7746 "%s%d: vhci_pgr_validate_and_register: key"
7748 7747 " validated thread 0x%p\n", ddi_driver_name(cdip),
7749 7748 ddi_get_instance(cdip), (void *)curthread));
7750 7749 pip = npip;
7751 7750 success = 1;
7752 7751 break;
7753 7752 } else {
7754 7753 VHCI_DEBUG(4, (CE_NOTE, NULL,
7755 7754 "vhci_pgr_validate_and_register: Re-validation on"
7756 7755 " osvp 0x%p failed %x\n", (void *)osvp, rval));
7757 7756 vhci_print_prout_keys(vlun,
7758 7757 "v_pgr_val_reg: reval failed: ");
7759 7758 }
7760 7759
7761 7760 /*
7762 7761 * Try other paths
7763 7762 */
7764 7763 next_path_2:
7765 7764 pip = npip;
7766 7765 rval = mdi_select_path(cdip, NULL,
7767 7766 MDI_SELECT_ONLINE_PATH|MDI_SELECT_STANDBY_PATH,
7768 7767 pip, &npip);
7769 7768 mdi_rele_path(pip);
7770 7769 } while ((rval == MDI_SUCCESS) && (npip != NULL));
7771 7770
7772 7771 /* Be sure to restore original cdb */
7773 7772 vlun->svl_cdb[1] = cdb_1;
7774 7773
7775 7774 if (success == 1) {
7776 7775 /* Successfully validated registration */
7777 7776 mdi_rele_path(pip);
7778 7777 return (1);
7779 7778 }
7780 7779
7781 7780 VHCI_DEBUG(4, (CE_WARN, NULL, "key validation failed"));
7782 7781
7783 7782 /*
7784 7783 * key invalid, back out by registering key value of 0
7785 7784 */
7786 7785 VHCI_DEBUG(4, (CE_NOTE, NULL,
7787 7786 "vhci_pgr_validate_and_register: backout on"
7788 7787 " svp 0x%p being done\n", (void *)svp));
7789 7788 vhci_print_prout_keys(vlun, "v_pgr_val_reg: before bcopy: ");
7790 7789
7791 7790 bcopy(prout->service_key, prout->res_key, MHIOC_RESV_KEY_SIZE);
7792 7791 bzero(prout->service_key, MHIOC_RESV_KEY_SIZE);
7793 7792
7794 7793 vhci_print_prout_keys(vlun, "v_pgr_val_reg: before bcopy: ");
7795 7794
7796 7795 /*
7797 7796 * Get a new path
7798 7797 */
7799 7798 rval = mdi_select_path(cdip, NULL, MDI_SELECT_ONLINE_PATH |
7800 7799 MDI_SELECT_STANDBY_PATH, NULL, &pip);
7801 7800 if ((rval != MDI_SUCCESS) || (pip == NULL)) {
7802 7801 VHCI_DEBUG(4, (CE_NOTE, NULL,
7803 7802 "%s%d: vhci_pgr_validate_and_register: no valid pip\n",
7804 7803 ddi_driver_name(cdip), ddi_get_instance(cdip)));
7805 7804 return (0);
7806 7805 }
7807 7806
7808 7807 if ((rval = vhci_do_prout(svp)) != 1) {
7809 7808 VHCI_DEBUG(4, (CE_NOTE, NULL,
7810 7809 "vhci_pgr_validate_and_register: backout on"
7811 7810 " svp 0x%p failed\n", (void *)svp));
7812 7811 vhci_print_prout_keys(vlun, "backout failed");
7813 7812
7814 7813 VHCI_DEBUG(4, (CE_WARN, NULL,
7815 7814 "%s%d: vhci_pgr_validate_and_register: key"
7816 7815 " validation and backout failed", ddi_driver_name(cdip),
7817 7816 ddi_get_instance(cdip)));
7818 7817 if (rval == VHCI_PGR_ILLEGALOP) {
7819 7818 VHCI_DEBUG(4, (CE_WARN, NULL,
7820 7819 "%s%d: vhci_pgr_validate_and_register: key"
7821 7820 " already cleared", ddi_driver_name(cdip),
7822 7821 ddi_get_instance(cdip)));
7823 7822 rval = 1;
7824 7823 } else
7825 7824 rval = 0;
7826 7825 } else {
7827 7826 VHCI_DEBUG(4, (CE_NOTE, NULL,
7828 7827 "%s%d: vhci_pgr_validate_and_register: key"
7829 7828 " validation failed, key backed out\n",
7830 7829 ddi_driver_name(cdip), ddi_get_instance(cdip)));
7831 7830 vhci_print_prout_keys(vlun, "v_pgr_val_reg: key backed out: ");
7832 7831 }
7833 7832 mdi_rele_path(pip);
7834 7833
7835 7834 return (rval);
7836 7835 }
7837 7836
7838 7837 /*
7839 7838 * taskq routine to dispatch a scsi cmd to vhci_scsi_start. This ensures
7840 7839 * that vhci_scsi_start is not called in interrupt context.
7841 7840 * As the upper layer gets TRAN_ACCEPT when the command is dispatched, we
7842 7841 * need to complete the command if something goes wrong.
7843 7842 */
7844 7843 static void
7845 7844 vhci_dispatch_scsi_start(void *arg)
7846 7845 {
7847 7846 struct vhci_pkt *vpkt = (struct vhci_pkt *)arg;
7848 7847 struct scsi_pkt *tpkt = vpkt->vpkt_tgt_pkt;
7849 7848 int rval = TRAN_BUSY;
7850 7849
7851 7850 VHCI_DEBUG(6, (CE_NOTE, NULL, "!vhci_dispatch_scsi_start: sending"
7852 7851 " scsi-2 reserve for 0x%p\n",
7853 7852 (void *)ADDR2DIP(&(vpkt->vpkt_tgt_pkt->pkt_address))));
7854 7853
7855 7854 /*
7856 7855 * To prevent the taskq from being called recursively we set the
7857 7856 * the VHCI_PKT_THRU_TASKQ bit in the vhci_pkt_states.
7858 7857 */
7859 7858 vpkt->vpkt_state |= VHCI_PKT_THRU_TASKQ;
7860 7859
7861 7860 /*
7862 7861 * Wait for the transport to get ready to send packets
7863 7862 * and if it times out, it will return something other than
7864 7863 * TRAN_BUSY. The vhci_reserve_delay may want to
7865 7864 * get tuned for other transports and is therefore a global.
7866 7865 * Using delay since this routine is called by taskq dispatch
7867 7866 * and not called during interrupt context.
7868 7867 */
7869 7868 while ((rval = vhci_scsi_start(&(vpkt->vpkt_tgt_pkt->pkt_address),
7870 7869 vpkt->vpkt_tgt_pkt)) == TRAN_BUSY) {
7871 7870 delay(drv_usectohz(vhci_reserve_delay));
7872 7871 }
7873 7872
7874 7873 switch (rval) {
7875 7874 case TRAN_ACCEPT:
7876 7875 return;
7877 7876
7878 7877 default:
7879 7878 /*
7880 7879 * This pkt shall be retried, and to ensure another taskq
7881 7880 * is dispatched for it, clear the VHCI_PKT_THRU_TASKQ
7882 7881 * flag.
7883 7882 */
7884 7883 vpkt->vpkt_state &= ~VHCI_PKT_THRU_TASKQ;
7885 7884
7886 7885 /* Ensure that the pkt is retried without a reset */
7887 7886 tpkt->pkt_reason = CMD_ABORTED;
7888 7887 tpkt->pkt_statistics |= STAT_ABORTED;
7889 7888 VHCI_DEBUG(1, (CE_WARN, NULL, "!vhci_dispatch_scsi_start: "
7890 7889 "TRAN_rval %d returned for dip 0x%p", rval,
7891 7890 (void *)ADDR2DIP(&(vpkt->vpkt_tgt_pkt->pkt_address))));
7892 7891 break;
7893 7892 }
7894 7893
7895 7894 /*
7896 7895 * vpkt_org_vpkt should always be NULL here if the retry command
7897 7896 * has been successfully dispatched. If vpkt_org_vpkt != NULL at
7898 7897 * this point, it is an error so restore the original vpkt and
7899 7898 * return an error to the target driver so it can retry the
7900 7899 * command as appropriate.
7901 7900 */
7902 7901 if (vpkt->vpkt_org_vpkt != NULL) {
7903 7902 struct vhci_pkt *new_vpkt = vpkt;
7904 7903 scsi_vhci_priv_t *svp = (scsi_vhci_priv_t *)
7905 7904 mdi_pi_get_vhci_private(vpkt->vpkt_path);
7906 7905
7907 7906 vpkt = vpkt->vpkt_org_vpkt;
7908 7907
7909 7908 vpkt->vpkt_tgt_pkt->pkt_reason = tpkt->pkt_reason;
7910 7909 vpkt->vpkt_tgt_pkt->pkt_statistics = tpkt->pkt_statistics;
7911 7910
7912 7911 vhci_scsi_destroy_pkt(&svp->svp_psd->sd_address,
7913 7912 new_vpkt->vpkt_tgt_pkt);
7914 7913
7915 7914 tpkt = vpkt->vpkt_tgt_pkt;
7916 7915 }
7917 7916
7918 7917 scsi_hba_pkt_comp(tpkt);
7919 7918 }
7920 7919
7921 7920 static void
7922 7921 vhci_initiate_auto_failback(void *arg)
7923 7922 {
7924 7923 struct scsi_vhci_lun *vlun = (struct scsi_vhci_lun *)arg;
7925 7924 dev_info_t *vdip, *cdip;
7926 7925 int held;
7927 7926
7928 7927 cdip = vlun->svl_dip;
7929 7928 vdip = ddi_get_parent(cdip);
7930 7929
7931 7930 VHCI_HOLD_LUN(vlun, VH_SLEEP, held);
7932 7931
7933 7932 /*
7934 7933 * Perform a final check to see if the active path class is indeed
7935 7934 * not the preferred path class. As in the time the auto failback
7936 7935 * was dispatched, an external failover could have been detected.
7937 7936 * [Some other host could have detected this condition and triggered
7938 7937 * the auto failback before].
7939 7938 * In such a case if we go ahead with failover we will be negating the
7940 7939 * whole purpose of auto failback.
7941 7940 */
7942 7941 mutex_enter(&vlun->svl_mutex);
7943 7942 if (vlun->svl_active_pclass != NULL) {
7944 7943 char *best_pclass;
7945 7944 struct scsi_failover_ops *fo;
7946 7945
7947 7946 fo = vlun->svl_fops;
7948 7947
7949 7948 (void) fo->sfo_pathclass_next(NULL, &best_pclass,
7950 7949 vlun->svl_fops_ctpriv);
7951 7950 if (strcmp(vlun->svl_active_pclass, best_pclass) == 0) {
7952 7951 mutex_exit(&vlun->svl_mutex);
7953 7952 VHCI_RELEASE_LUN(vlun);
7954 7953 VHCI_DEBUG(1, (CE_NOTE, NULL, "Not initiating "
7955 7954 "auto failback for %s as %s pathclass already "
7956 7955 "active.\n", vlun->svl_lun_wwn, best_pclass));
7957 7956 return;
7958 7957 }
7959 7958 }
7960 7959 mutex_exit(&vlun->svl_mutex);
7961 7960 if (mdi_failover(vdip, vlun->svl_dip, MDI_FAILOVER_SYNC)
7962 7961 == MDI_SUCCESS) {
7963 7962 vhci_log(CE_NOTE, vdip, "!Auto failback operation "
7964 7963 "succeeded for device %s (GUID %s)",
7965 7964 ddi_node_name(cdip), vlun->svl_lun_wwn);
7966 7965 } else {
7967 7966 vhci_log(CE_NOTE, vdip, "!Auto failback operation "
7968 7967 "failed for device %s (GUID %s)",
7969 7968 ddi_node_name(cdip), vlun->svl_lun_wwn);
7970 7969 }
7971 7970 VHCI_RELEASE_LUN(vlun);
7972 7971 }
7973 7972
7974 7973 #ifdef DEBUG
7975 7974 static void
7976 7975 vhci_print_prin_keys(vhci_prin_readkeys_t *prin, int numkeys)
7977 7976 {
7978 7977 vhci_clean_print(NULL, 5, "Current PGR Keys",
7979 7978 (uchar_t *)prin, numkeys * 8);
7980 7979 }
7981 7980 #endif
7982 7981
7983 7982 static void
7984 7983 vhci_print_prout_keys(scsi_vhci_lun_t *vlun, char *msg)
7985 7984 {
7986 7985 int i;
7987 7986 vhci_prout_t *prout;
7988 7987 char buf1[4*MHIOC_RESV_KEY_SIZE + 1];
7989 7988 char buf2[4*MHIOC_RESV_KEY_SIZE + 1];
7990 7989 char buf3[4*MHIOC_RESV_KEY_SIZE + 1];
7991 7990 char buf4[4*MHIOC_RESV_KEY_SIZE + 1];
7992 7991
7993 7992 prout = &vlun->svl_prout;
7994 7993
7995 7994 for (i = 0; i < MHIOC_RESV_KEY_SIZE; i++)
7996 7995 (void) sprintf(&buf1[4*i], "[%02x]", prout->res_key[i]);
7997 7996 for (i = 0; i < MHIOC_RESV_KEY_SIZE; i++)
7998 7997 (void) sprintf(&buf2[(4*i)], "[%02x]", prout->service_key[i]);
7999 7998 for (i = 0; i < MHIOC_RESV_KEY_SIZE; i++)
8000 7999 (void) sprintf(&buf3[4*i], "[%02x]", prout->active_res_key[i]);
8001 8000 for (i = 0; i < MHIOC_RESV_KEY_SIZE; i++)
8002 8001 (void) sprintf(&buf4[4*i], "[%02x]",
8003 8002 prout->active_service_key[i]);
8004 8003
8005 8004 /* Printing all in one go. Otherwise it will jumble up */
8006 8005 VHCI_DEBUG(5, (CE_CONT, NULL, "%s vlun 0x%p, thread 0x%p\n"
8007 8006 "res_key: : %s\n"
8008 8007 "service_key : %s\n"
8009 8008 "active_res_key : %s\n"
8010 8009 "active_service_key: %s\n",
8011 8010 msg, (void *)vlun, (void *)curthread, buf1, buf2, buf3, buf4));
8012 8011 }
8013 8012
8014 8013 /*
8015 8014 * Called from vhci_scsi_start to update the pHCI pkt with target packet.
8016 8015 */
8017 8016 static void
8018 8017 vhci_update_pHCI_pkt(struct vhci_pkt *vpkt, struct scsi_pkt *pkt)
8019 8018 {
8020 8019
8021 8020 ASSERT(vpkt->vpkt_hba_pkt);
8022 8021
8023 8022 vpkt->vpkt_hba_pkt->pkt_flags = pkt->pkt_flags;
8024 8023 vpkt->vpkt_hba_pkt->pkt_flags |= FLAG_NOQUEUE;
8025 8024
8026 8025 if ((vpkt->vpkt_hba_pkt->pkt_flags & FLAG_NOINTR) ||
8027 8026 MDI_PI_IS_SUSPENDED(vpkt->vpkt_path)) {
8028 8027 /*
8029 8028 * Polled Command is requested or HBA is in
8030 8029 * suspended state
8031 8030 */
8032 8031 vpkt->vpkt_hba_pkt->pkt_flags |= FLAG_NOINTR;
8033 8032 vpkt->vpkt_hba_pkt->pkt_comp = NULL;
8034 8033 } else {
8035 8034 vpkt->vpkt_hba_pkt->pkt_comp = vhci_intr;
8036 8035 }
8037 8036 vpkt->vpkt_hba_pkt->pkt_time = pkt->pkt_time;
8038 8037 bcopy(pkt->pkt_cdbp, vpkt->vpkt_hba_pkt->pkt_cdbp,
8039 8038 vpkt->vpkt_tgt_init_cdblen);
8040 8039 vpkt->vpkt_hba_pkt->pkt_resid = pkt->pkt_resid;
8041 8040
8042 8041 /* Re-initialize the following pHCI packet state information */
8043 8042 vpkt->vpkt_hba_pkt->pkt_state = 0;
8044 8043 vpkt->vpkt_hba_pkt->pkt_statistics = 0;
8045 8044 vpkt->vpkt_hba_pkt->pkt_reason = 0;
8046 8045 }
8047 8046
8048 8047 static int
8049 8048 vhci_scsi_bus_power(dev_info_t *parent, void *impl_arg, pm_bus_power_op_t op,
8050 8049 void *arg, void *result)
8051 8050 {
8052 8051 int ret = DDI_SUCCESS;
8053 8052
8054 8053 /*
8055 8054 * Generic processing in MPxIO framework
8056 8055 */
8057 8056 ret = mdi_bus_power(parent, impl_arg, op, arg, result);
8058 8057
8059 8058 switch (ret) {
8060 8059 case MDI_SUCCESS:
8061 8060 ret = DDI_SUCCESS;
8062 8061 break;
8063 8062 case MDI_FAILURE:
8064 8063 ret = DDI_FAILURE;
8065 8064 break;
8066 8065 default:
8067 8066 break;
8068 8067 }
8069 8068
8070 8069 return (ret);
8071 8070 }
8072 8071
8073 8072 static int
8074 8073 vhci_pHCI_cap(struct scsi_address *ap, char *cap, int val, int whom,
8075 8074 mdi_pathinfo_t *pip)
8076 8075 {
8077 8076 dev_info_t *cdip;
8078 8077 mdi_pathinfo_t *npip = NULL;
8079 8078 scsi_vhci_priv_t *svp = NULL;
8080 8079 struct scsi_address *pap = NULL;
8081 8080 scsi_hba_tran_t *hba = NULL;
8082 8081 int sps;
8083 8082 int mps_flag;
8084 8083 int rval = 0;
8085 8084
8086 8085 mps_flag = (MDI_SELECT_ONLINE_PATH | MDI_SELECT_STANDBY_PATH);
8087 8086 if (pip) {
8088 8087 /*
8089 8088 * If the call is from vhci_pathinfo_state_change,
8090 8089 * then this path was busy and is becoming ready to accept IO.
8091 8090 */
8092 8091 ASSERT(ap != NULL);
8093 8092 hba = ap->a_hba_tran;
8094 8093 ASSERT(hba != NULL);
8095 8094 rval = scsi_ifsetcap(ap, cap, val, whom);
8096 8095
8097 8096 VHCI_DEBUG(2, (CE_NOTE, NULL,
8098 8097 "!vhci_pHCI_cap: only on path %p, ap %p, rval %x\n",
8099 8098 (void *)pip, (void *)ap, rval));
8100 8099
8101 8100 return (rval);
8102 8101 }
8103 8102
8104 8103 /*
8105 8104 * Set capability on all the pHCIs.
8106 8105 * If any path is busy, then the capability would be set by
8107 8106 * vhci_pathinfo_state_change.
8108 8107 */
8109 8108
8110 8109 cdip = ADDR2DIP(ap);
8111 8110 ASSERT(cdip != NULL);
8112 8111 sps = mdi_select_path(cdip, NULL, mps_flag, NULL, &pip);
8113 8112 if ((sps != MDI_SUCCESS) || (pip == NULL)) {
8114 8113 VHCI_DEBUG(2, (CE_WARN, NULL,
8115 8114 "!vhci_pHCI_cap: Unable to get a path, dip 0x%p",
8116 8115 (void *)cdip));
8117 8116 return (0);
8118 8117 }
8119 8118
8120 8119 again:
8121 8120 svp = (scsi_vhci_priv_t *)mdi_pi_get_vhci_private(pip);
8122 8121 if (svp == NULL) {
8123 8122 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_pHCI_cap: "
8124 8123 "priv is NULL, pip 0x%p", (void *)pip));
8125 8124 mdi_rele_path(pip);
8126 8125 return (rval);
8127 8126 }
8128 8127
8129 8128 if (svp->svp_psd == NULL) {
8130 8129 VHCI_DEBUG(2, (CE_WARN, NULL, "!vhci_pHCI_cap: "
8131 8130 "psd is NULL, pip 0x%p, svp 0x%p",
8132 8131 (void *)pip, (void *)svp));
8133 8132 mdi_rele_path(pip);
8134 8133 return (rval);
8135 8134 }
8136 8135
8137 8136 pap = &svp->svp_psd->sd_address;
8138 8137 ASSERT(pap != NULL);
8139 8138 hba = pap->a_hba_tran;
8140 8139 ASSERT(hba != NULL);
8141 8140
8142 8141 if (hba->tran_setcap != NULL) {
8143 8142 rval = scsi_ifsetcap(pap, cap, val, whom);
8144 8143
8145 8144 VHCI_DEBUG(2, (CE_NOTE, NULL,
8146 8145 "!vhci_pHCI_cap: path %p, ap %p, rval %x\n",
8147 8146 (void *)pip, (void *)ap, rval));
8148 8147
8149 8148 /*
8150 8149 * Select next path and issue the setcap, repeat
8151 8150 * until all paths are exhausted
8152 8151 */
8153 8152 sps = mdi_select_path(cdip, NULL, mps_flag, pip, &npip);
8154 8153 if ((sps != MDI_SUCCESS) || (npip == NULL)) {
8155 8154 mdi_rele_path(pip);
8156 8155 return (1);
8157 8156 }
8158 8157 mdi_rele_path(pip);
8159 8158 pip = npip;
8160 8159 goto again;
8161 8160 }
8162 8161 mdi_rele_path(pip);
8163 8162 return (rval);
8164 8163 }
8165 8164
8166 8165 static int
8167 8166 vhci_scsi_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
8168 8167 void *arg, dev_info_t **child)
8169 8168 {
8170 8169 char *guid;
8171 8170
8172 8171 if (vhci_bus_config_debug)
8173 8172 flags |= NDI_DEVI_DEBUG;
8174 8173
8175 8174 if (op == BUS_CONFIG_ONE || op == BUS_UNCONFIG_ONE)
8176 8175 guid = vhci_devnm_to_guid((char *)arg);
8177 8176 else
8178 8177 guid = NULL;
8179 8178
8180 8179 if (mdi_vhci_bus_config(pdip, flags, op, arg, child, guid)
8181 8180 == MDI_SUCCESS)
8182 8181 return (NDI_SUCCESS);
8183 8182 else
8184 8183 return (NDI_FAILURE);
8185 8184 }
8186 8185
8187 8186 static int
8188 8187 vhci_scsi_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
8189 8188 void *arg)
8190 8189 {
8191 8190 if (vhci_bus_config_debug)
8192 8191 flags |= NDI_DEVI_DEBUG;
8193 8192
8194 8193 return (ndi_busop_bus_unconfig(pdip, flags, op, arg));
8195 8194 }
8196 8195
8197 8196 /*
8198 8197 * Take the original vhci_pkt, create a duplicate of the pkt for resending
8199 8198 * as though it originated in ssd.
8200 8199 */
8201 8200 static struct scsi_pkt *
8202 8201 vhci_create_retry_pkt(struct vhci_pkt *vpkt)
8203 8202 {
8204 8203 struct vhci_pkt *new_vpkt = NULL;
8205 8204 struct scsi_pkt *pkt = NULL;
8206 8205
8207 8206 scsi_vhci_priv_t *svp = (scsi_vhci_priv_t *)
8208 8207 mdi_pi_get_vhci_private(vpkt->vpkt_path);
8209 8208
8210 8209 /*
8211 8210 * Ensure consistent data at completion time by setting PKT_CONSISTENT
8212 8211 */
8213 8212 pkt = vhci_scsi_init_pkt(&svp->svp_psd->sd_address, pkt,
8214 8213 vpkt->vpkt_tgt_init_bp, vpkt->vpkt_tgt_init_cdblen,
8215 8214 vpkt->vpkt_tgt_init_scblen, 0, PKT_CONSISTENT, NULL_FUNC, NULL);
8216 8215 if (pkt != NULL) {
8217 8216 new_vpkt = TGTPKT2VHCIPKT(pkt);
8218 8217
8219 8218 pkt->pkt_address = vpkt->vpkt_tgt_pkt->pkt_address;
8220 8219 pkt->pkt_flags = vpkt->vpkt_tgt_pkt->pkt_flags;
8221 8220 pkt->pkt_time = vpkt->vpkt_tgt_pkt->pkt_time;
8222 8221 pkt->pkt_comp = vpkt->vpkt_tgt_pkt->pkt_comp;
8223 8222
8224 8223 pkt->pkt_resid = 0;
8225 8224 pkt->pkt_statistics = 0;
8226 8225 pkt->pkt_reason = 0;
8227 8226
8228 8227 bcopy(vpkt->vpkt_tgt_pkt->pkt_cdbp,
8229 8228 pkt->pkt_cdbp, vpkt->vpkt_tgt_init_cdblen);
8230 8229
8231 8230 /*
8232 8231 * Save a pointer to the original vhci_pkt
8233 8232 */
8234 8233 new_vpkt->vpkt_org_vpkt = vpkt;
8235 8234 }
8236 8235
8237 8236 return (pkt);
8238 8237 }
8239 8238
8240 8239 /*
8241 8240 * Copy the successful completion information from the hba packet into
8242 8241 * the original target pkt from the upper layer. Returns the original
8243 8242 * vpkt and destroys the new vpkt from the internal retry.
8244 8243 */
8245 8244 static struct vhci_pkt *
8246 8245 vhci_sync_retry_pkt(struct vhci_pkt *vpkt)
8247 8246 {
8248 8247 struct vhci_pkt *ret_vpkt = NULL;
8249 8248 struct scsi_pkt *tpkt = NULL;
8250 8249 struct scsi_pkt *hba_pkt = NULL;
8251 8250 scsi_vhci_priv_t *svp = (scsi_vhci_priv_t *)
8252 8251 mdi_pi_get_vhci_private(vpkt->vpkt_path);
8253 8252
8254 8253 ASSERT(vpkt->vpkt_org_vpkt != NULL);
8255 8254 VHCI_DEBUG(0, (CE_NOTE, NULL, "vhci_sync_retry_pkt: Retry pkt "
8256 8255 "completed successfully!\n"));
8257 8256
8258 8257 ret_vpkt = vpkt->vpkt_org_vpkt;
8259 8258 tpkt = ret_vpkt->vpkt_tgt_pkt;
8260 8259 hba_pkt = vpkt->vpkt_hba_pkt;
8261 8260
8262 8261 /*
8263 8262 * Copy the good status into the target driver's packet
8264 8263 */
8265 8264 *(tpkt->pkt_scbp) = *(hba_pkt->pkt_scbp);
8266 8265 tpkt->pkt_resid = hba_pkt->pkt_resid;
8267 8266 tpkt->pkt_state = hba_pkt->pkt_state;
8268 8267 tpkt->pkt_statistics = hba_pkt->pkt_statistics;
8269 8268 tpkt->pkt_reason = hba_pkt->pkt_reason;
8270 8269
8271 8270 /*
8272 8271 * Destroy the internally created vpkt for the retry
8273 8272 */
8274 8273 vhci_scsi_destroy_pkt(&svp->svp_psd->sd_address,
8275 8274 vpkt->vpkt_tgt_pkt);
8276 8275
8277 8276 return (ret_vpkt);
8278 8277 }
8279 8278
8280 8279 /* restart the request sense request */
8281 8280 static void
8282 8281 vhci_uscsi_restart_sense(void *arg)
8283 8282 {
8284 8283 struct buf *rqbp;
8285 8284 struct buf *bp;
8286 8285 struct scsi_pkt *rqpkt = (struct scsi_pkt *)arg;
8287 8286 mp_uscsi_cmd_t *mp_uscmdp;
8288 8287
8289 8288 VHCI_DEBUG(4, (CE_WARN, NULL,
8290 8289 "vhci_uscsi_restart_sense: enter: rqpkt: %p", (void *)rqpkt));
8291 8290
8292 8291 if (scsi_transport(rqpkt) != TRAN_ACCEPT) {
8293 8292 /* if it fails - need to wakeup the original command */
8294 8293 mp_uscmdp = rqpkt->pkt_private;
8295 8294 bp = mp_uscmdp->cmdbp;
8296 8295 rqbp = mp_uscmdp->rqbp;
8297 8296 ASSERT(mp_uscmdp && bp && rqbp);
8298 8297 scsi_free_consistent_buf(rqbp);
8299 8298 scsi_destroy_pkt(rqpkt);
8300 8299 bp->b_resid = bp->b_bcount;
8301 8300 bioerror(bp, EIO);
8302 8301 biodone(bp);
8303 8302 }
8304 8303 }
8305 8304
8306 8305 /*
8307 8306 * auto-rqsense is not enabled so we have to retrieve the request sense
8308 8307 * manually.
8309 8308 */
8310 8309 static int
8311 8310 vhci_uscsi_send_sense(struct scsi_pkt *pkt, mp_uscsi_cmd_t *mp_uscmdp)
8312 8311 {
8313 8312 struct buf *rqbp, *cmdbp;
8314 8313 struct scsi_pkt *rqpkt;
8315 8314 int rval = 0;
8316 8315
8317 8316 cmdbp = mp_uscmdp->cmdbp;
8318 8317 ASSERT(cmdbp != NULL);
8319 8318
8320 8319 VHCI_DEBUG(4, (CE_WARN, NULL,
8321 8320 "vhci_uscsi_send_sense: enter: bp: %p pkt: %p scmd: %p",
8322 8321 (void *)cmdbp, (void *)pkt, (void *)mp_uscmdp));
8323 8322 /* set up the packet information and cdb */
8324 8323 if ((rqbp = scsi_alloc_consistent_buf(mp_uscmdp->ap, NULL,
8325 8324 SENSE_LENGTH, B_READ, NULL, NULL)) == NULL) {
8326 8325 return (-1);
8327 8326 }
8328 8327
8329 8328 if ((rqpkt = scsi_init_pkt(mp_uscmdp->ap, NULL, rqbp,
8330 8329 CDB_GROUP0, 1, 0, PKT_CONSISTENT, NULL, NULL)) == NULL) {
8331 8330 scsi_free_consistent_buf(rqbp);
8332 8331 return (-1);
8333 8332 }
8334 8333
8335 8334 (void) scsi_setup_cdb((union scsi_cdb *)(intptr_t)rqpkt->pkt_cdbp,
8336 8335 SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
8337 8336
8338 8337 mp_uscmdp->rqbp = rqbp;
8339 8338 rqbp->b_private = mp_uscmdp;
8340 8339 rqpkt->pkt_flags |= FLAG_SENSING;
8341 8340 rqpkt->pkt_time = 60;
8342 8341 rqpkt->pkt_comp = vhci_uscsi_iodone;
8343 8342 rqpkt->pkt_private = mp_uscmdp;
8344 8343
8345 8344 /*
8346 8345 * NOTE: This code path is related to MPAPI uscsi(7I), so path
8347 8346 * selection is not based on path_instance.
8348 8347 */
8349 8348 if (scsi_pkt_allocated_correctly(rqpkt))
8350 8349 rqpkt->pkt_path_instance = 0;
8351 8350
8352 8351 /* get her done */
8353 8352 switch (scsi_transport(rqpkt)) {
8354 8353 case TRAN_ACCEPT:
8355 8354 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_uscsi_send_sense: "
8356 8355 "transport accepted."));
8357 8356 break;
8358 8357 case TRAN_BUSY:
8359 8358 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_uscsi_send_sense: "
8360 8359 "transport busy, setting timeout."));
8361 8360 vhci_restart_timeid = timeout(vhci_uscsi_restart_sense, rqpkt,
8362 8361 (drv_usectohz(5 * 1000000)));
8363 8362 break;
8364 8363 default:
8365 8364 VHCI_DEBUG(1, (CE_NOTE, NULL, "vhci_uscsi_send_sense: "
8366 8365 "transport failed"));
8367 8366 scsi_free_consistent_buf(rqbp);
8368 8367 scsi_destroy_pkt(rqpkt);
8369 8368 rval = -1;
8370 8369 }
8371 8370
8372 8371 return (rval);
8373 8372 }
8374 8373
8375 8374 /*
8376 8375 * done routine for the mpapi uscsi command - this is behaving as though
8377 8376 * FLAG_DIAGNOSE is set meaning there are no retries except for a manual
8378 8377 * request sense.
8379 8378 */
8380 8379 void
8381 8380 vhci_uscsi_iodone(struct scsi_pkt *pkt)
8382 8381 {
8383 8382 struct buf *bp;
8384 8383 mp_uscsi_cmd_t *mp_uscmdp;
8385 8384 struct uscsi_cmd *uscmdp;
8386 8385 struct scsi_arq_status *arqstat;
8387 8386 int err;
8388 8387
8389 8388 mp_uscmdp = (mp_uscsi_cmd_t *)pkt->pkt_private;
8390 8389 uscmdp = mp_uscmdp->uscmdp;
8391 8390 bp = mp_uscmdp->cmdbp;
8392 8391 ASSERT(bp != NULL);
8393 8392 VHCI_DEBUG(4, (CE_WARN, NULL,
8394 8393 "vhci_uscsi_iodone: enter: bp: %p pkt: %p scmd: %p",
8395 8394 (void *)bp, (void *)pkt, (void *)mp_uscmdp));
8396 8395 /* Save the status and the residual into the uscsi_cmd struct */
8397 8396 uscmdp->uscsi_status = ((*(pkt)->pkt_scbp) & STATUS_MASK);
8398 8397 uscmdp->uscsi_resid = bp->b_resid;
8399 8398
8400 8399 /* return on a very successful command */
8401 8400 if (pkt->pkt_reason == CMD_CMPLT &&
8402 8401 SCBP_C(pkt) == 0 && ((pkt->pkt_flags & FLAG_SENSING) == 0) &&
8403 8402 pkt->pkt_resid == 0) {
8404 8403 mdi_pi_kstat_iosupdate(mp_uscmdp->pip, bp);
8405 8404 scsi_destroy_pkt(pkt);
8406 8405 biodone(bp);
8407 8406 return;
8408 8407 }
8409 8408 VHCI_DEBUG(4, (CE_NOTE, NULL, "iodone: reason=0x%x "
8410 8409 " pkt_resid=%ld pkt_state: 0x%x b_count: %ld b_resid: %ld",
8411 8410 pkt->pkt_reason, pkt->pkt_resid,
8412 8411 pkt->pkt_state, bp->b_bcount, bp->b_resid));
8413 8412
8414 8413 err = EIO;
8415 8414
8416 8415 arqstat = (struct scsi_arq_status *)(intptr_t)(pkt->pkt_scbp);
8417 8416 if (pkt->pkt_reason != CMD_CMPLT) {
8418 8417 /*
8419 8418 * The command did not complete.
8420 8419 */
8421 8420 VHCI_DEBUG(4, (CE_NOTE, NULL,
8422 8421 "vhci_uscsi_iodone: command did not complete."
8423 8422 " reason: %x flag: %x", pkt->pkt_reason, pkt->pkt_flags));
8424 8423 if (pkt->pkt_flags & FLAG_SENSING) {
8425 8424 MDI_PI_ERRSTAT(mp_uscmdp->pip, MDI_PI_TRANSERR);
8426 8425 } else if (pkt->pkt_reason == CMD_TIMEOUT) {
8427 8426 MDI_PI_ERRSTAT(mp_uscmdp->pip, MDI_PI_HARDERR);
8428 8427 err = ETIMEDOUT;
8429 8428 }
8430 8429 } else if (pkt->pkt_state & STATE_ARQ_DONE && mp_uscmdp->arq_enabled) {
8431 8430 /*
8432 8431 * The auto-rqsense happened, and the packet has a filled-in
8433 8432 * scsi_arq_status structure, pointed to by pkt_scbp.
8434 8433 */
8435 8434 VHCI_DEBUG(4, (CE_NOTE, NULL,
8436 8435 "vhci_uscsi_iodone: received auto-requested sense"));
8437 8436 if (uscmdp->uscsi_flags & USCSI_RQENABLE) {
8438 8437 /* get the amount of data to copy into rqbuf */
8439 8438 int rqlen = SENSE_LENGTH - arqstat->sts_rqpkt_resid;
8440 8439 rqlen = min(((int)uscmdp->uscsi_rqlen), rqlen);
8441 8440 uscmdp->uscsi_rqresid = uscmdp->uscsi_rqlen - rqlen;
8442 8441 uscmdp->uscsi_rqstatus =
8443 8442 *((char *)&arqstat->sts_rqpkt_status);
8444 8443 if (uscmdp->uscsi_rqbuf && uscmdp->uscsi_rqlen &&
8445 8444 rqlen != 0) {
8446 8445 bcopy(&(arqstat->sts_sensedata),
8447 8446 uscmdp->uscsi_rqbuf, rqlen);
8448 8447 }
8449 8448 mdi_pi_kstat_iosupdate(mp_uscmdp->pip, bp);
8450 8449 VHCI_DEBUG(4, (CE_NOTE, NULL,
8451 8450 "vhci_uscsi_iodone: ARQ "
8452 8451 "uscsi_rqstatus=0x%x uscsi_rqresid=%d rqlen: %d "
8453 8452 "xfer: %d rqpkt_resid: %d\n",
8454 8453 uscmdp->uscsi_rqstatus, uscmdp->uscsi_rqresid,
8455 8454 uscmdp->uscsi_rqlen, rqlen,
8456 8455 arqstat->sts_rqpkt_resid));
8457 8456 }
8458 8457 } else if (pkt->pkt_flags & FLAG_SENSING) {
8459 8458 struct buf *rqbp;
8460 8459 struct scsi_status *rqstatus;
8461 8460
8462 8461 rqstatus = (struct scsi_status *)pkt->pkt_scbp;
8463 8462 /* a manual request sense was done - get the information */
8464 8463 if (uscmdp->uscsi_flags & USCSI_RQENABLE) {
8465 8464 int rqlen = SENSE_LENGTH - pkt->pkt_resid;
8466 8465
8467 8466 rqbp = mp_uscmdp->rqbp;
8468 8467 /* get the amount of data to copy into rqbuf */
8469 8468 rqlen = min(((int)uscmdp->uscsi_rqlen), rqlen);
8470 8469 uscmdp->uscsi_rqresid = uscmdp->uscsi_rqlen - rqlen;
8471 8470 uscmdp->uscsi_rqstatus = *((char *)rqstatus);
8472 8471 if (uscmdp->uscsi_rqlen && uscmdp->uscsi_rqbuf) {
8473 8472 bcopy(rqbp->b_un.b_addr, uscmdp->uscsi_rqbuf,
8474 8473 rqlen);
8475 8474 }
8476 8475 MDI_PI_ERRSTAT(mp_uscmdp->pip, MDI_PI_TRANSERR);
8477 8476 scsi_free_consistent_buf(rqbp);
8478 8477 }
8479 8478 VHCI_DEBUG(4, (CE_NOTE, NULL, "vhci_uscsi_iodone: FLAG_SENSING"
8480 8479 "uscsi_rqstatus=0x%x uscsi_rqresid=%d\n",
8481 8480 uscmdp->uscsi_rqstatus, uscmdp->uscsi_rqresid));
8482 8481 } else {
8483 8482 struct scsi_status *status =
8484 8483 (struct scsi_status *)pkt->pkt_scbp;
8485 8484 /*
8486 8485 * Command completed and we're not getting sense. Check for
8487 8486 * errors and decide what to do next.
8488 8487 */
8489 8488 VHCI_DEBUG(4, (CE_NOTE, NULL,
8490 8489 "vhci_uscsi_iodone: command appears complete: reason: %x",
8491 8490 pkt->pkt_reason));
8492 8491 if (status->sts_chk) {
8493 8492 /* need to manually get the request sense */
8494 8493 if (vhci_uscsi_send_sense(pkt, mp_uscmdp) == 0) {
8495 8494 scsi_destroy_pkt(pkt);
8496 8495 return;
8497 8496 }
8498 8497 } else {
8499 8498 VHCI_DEBUG(4, (CE_NOTE, NULL,
8500 8499 "vhci_chk_err: appears complete"));
8501 8500 err = 0;
8502 8501 mdi_pi_kstat_iosupdate(mp_uscmdp->pip, bp);
8503 8502 if (pkt->pkt_resid) {
8504 8503 bp->b_resid += pkt->pkt_resid;
8505 8504 }
8506 8505 }
8507 8506 }
8508 8507
8509 8508 if (err) {
8510 8509 if (bp->b_resid == 0)
8511 8510 bp->b_resid = bp->b_bcount;
8512 8511 bioerror(bp, err);
8513 8512 bp->b_flags |= B_ERROR;
8514 8513 }
8515 8514
8516 8515 scsi_destroy_pkt(pkt);
8517 8516 biodone(bp);
8518 8517
8519 8518 VHCI_DEBUG(4, (CE_WARN, NULL, "vhci_uscsi_iodone: exit"));
8520 8519 }
8521 8520
8522 8521 /*
8523 8522 * start routine for the mpapi uscsi command
8524 8523 */
8525 8524 int
8526 8525 vhci_uscsi_iostart(struct buf *bp)
8527 8526 {
8528 8527 struct scsi_pkt *pkt;
8529 8528 struct uscsi_cmd *uscmdp;
8530 8529 mp_uscsi_cmd_t *mp_uscmdp;
8531 8530 int stat_size, rval;
8532 8531 int retry = 0;
8533 8532
8534 8533 ASSERT(bp->b_private != NULL);
8535 8534
8536 8535 mp_uscmdp = (mp_uscsi_cmd_t *)bp->b_private;
8537 8536 uscmdp = mp_uscmdp->uscmdp;
8538 8537 if (uscmdp->uscsi_flags & USCSI_RQENABLE) {
8539 8538 stat_size = SENSE_LENGTH;
8540 8539 } else {
8541 8540 stat_size = 1;
8542 8541 }
8543 8542
8544 8543 pkt = scsi_init_pkt(mp_uscmdp->ap, NULL, bp, uscmdp->uscsi_cdblen,
8545 8544 stat_size, 0, 0, SLEEP_FUNC, NULL);
8546 8545 if (pkt == NULL) {
8547 8546 VHCI_DEBUG(4, (CE_NOTE, NULL,
8548 8547 "vhci_uscsi_iostart: rval: EINVAL"));
8549 8548 bp->b_resid = bp->b_bcount;
8550 8549 uscmdp->uscsi_resid = bp->b_bcount;
8551 8550 bioerror(bp, EINVAL);
8552 8551 biodone(bp);
8553 8552 return (EINVAL);
8554 8553 }
8555 8554
8556 8555 pkt->pkt_time = uscmdp->uscsi_timeout;
8557 8556 bcopy(uscmdp->uscsi_cdb, pkt->pkt_cdbp, (size_t)uscmdp->uscsi_cdblen);
8558 8557 pkt->pkt_comp = vhci_uscsi_iodone;
8559 8558 pkt->pkt_private = mp_uscmdp;
8560 8559 if (uscmdp->uscsi_flags & USCSI_SILENT)
8561 8560 pkt->pkt_flags |= FLAG_SILENT;
8562 8561 if (uscmdp->uscsi_flags & USCSI_ISOLATE)
8563 8562 pkt->pkt_flags |= FLAG_ISOLATE;
8564 8563 if (uscmdp->uscsi_flags & USCSI_DIAGNOSE)
8565 8564 pkt->pkt_flags |= FLAG_DIAGNOSE;
8566 8565 if (uscmdp->uscsi_flags & USCSI_RENEGOT) {
8567 8566 pkt->pkt_flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
8568 8567 }
8569 8568 VHCI_DEBUG(4, (CE_WARN, NULL,
8570 8569 "vhci_uscsi_iostart: ap: %p pkt: %p pcdbp: %p uscmdp: %p"
8571 8570 " ucdbp: %p pcdblen: %d bp: %p count: %ld pip: %p"
8572 8571 " stat_size: %d",
8573 8572 (void *)mp_uscmdp->ap, (void *)pkt, (void *)pkt->pkt_cdbp,
8574 8573 (void *)uscmdp, (void *)uscmdp->uscsi_cdb, pkt->pkt_cdblen,
8575 8574 (void *)bp, bp->b_bcount, (void *)mp_uscmdp->pip, stat_size));
8576 8575
8577 8576 /*
8578 8577 * NOTE: This code path is related to MPAPI uscsi(7I), so path
8579 8578 * selection is not based on path_instance.
8580 8579 */
8581 8580 if (scsi_pkt_allocated_correctly(pkt))
8582 8581 pkt->pkt_path_instance = 0;
8583 8582
8584 8583 while (((rval = scsi_transport(pkt)) == TRAN_BUSY) &&
8585 8584 retry < vhci_uscsi_retry_count) {
8586 8585 delay(drv_usectohz(vhci_uscsi_delay));
8587 8586 retry++;
8588 8587 }
8589 8588 if (retry >= vhci_uscsi_retry_count) {
8590 8589 VHCI_DEBUG(4, (CE_NOTE, NULL,
8591 8590 "vhci_uscsi_iostart: tran_busy - retry: %d", retry));
8592 8591 }
8593 8592 switch (rval) {
8594 8593 case TRAN_ACCEPT:
8595 8594 rval = 0;
8596 8595 break;
8597 8596
8598 8597 default:
8599 8598 VHCI_DEBUG(4, (CE_NOTE, NULL,
8600 8599 "vhci_uscsi_iostart: rval: %d count: %ld res: %ld",
8601 8600 rval, bp->b_bcount, bp->b_resid));
8602 8601 bp->b_resid = bp->b_bcount;
8603 8602 uscmdp->uscsi_resid = bp->b_bcount;
8604 8603 bioerror(bp, EIO);
8605 8604 scsi_destroy_pkt(pkt);
8606 8605 biodone(bp);
8607 8606 rval = EIO;
8608 8607 MDI_PI_ERRSTAT(mp_uscmdp->pip, MDI_PI_TRANSERR);
8609 8608 break;
8610 8609 }
8611 8610 VHCI_DEBUG(4, (CE_NOTE, NULL,
8612 8611 "vhci_uscsi_iostart: exit: rval: %d", rval));
8613 8612 return (rval);
8614 8613 }
8615 8614
8616 8615 /* ARGSUSED */
8617 8616 static struct scsi_failover_ops *
8618 8617 vhci_dev_fo(dev_info_t *vdip, struct scsi_device *psd,
8619 8618 void **ctprivp, char **fo_namep)
8620 8619 {
8621 8620 struct scsi_failover_ops *sfo;
8622 8621 char *sfo_name;
8623 8622 char *override;
8624 8623 struct scsi_failover *sf;
8625 8624
8626 8625 ASSERT(psd && psd->sd_inq);
8627 8626 if ((psd == NULL) || (psd->sd_inq == NULL)) {
8628 8627 VHCI_DEBUG(1, (CE_NOTE, NULL,
8629 8628 "!vhci_dev_fo:return NULL no scsi_device or inquiry"));
8630 8629 return (NULL);
8631 8630 }
8632 8631
8633 8632 /*
8634 8633 * Determine if device is supported under scsi_vhci, and select
8635 8634 * failover module.
8636 8635 *
8637 8636 * See if there is a scsi_vhci.conf file override for this devices's
8638 8637 * VID/PID. The following values can be returned:
8639 8638 *
8640 8639 * NULL If the NULL is returned then there is no scsi_vhci.conf
8641 8640 * override. For NULL, we determine the failover_ops for
8642 8641 * this device by checking the sfo_device_probe entry
8643 8642 * point for each 'fops' module, in order.
8644 8643 *
8645 8644 * NOTE: Correct operation may depend on module ordering
8646 8645 * of 'specific' (failover modules that are completely
8647 8646 * VID/PID table based) to 'generic' (failover modules
8648 8647 * that based on T10 standards like TPGS). Currently,
8649 8648 * the value of 'ddi-forceload' in scsi_vhci.conf is used
8650 8649 * to establish the module list and probe order.
8651 8650 *
8652 8651 * "NONE" If value "NONE" is returned then there is a
8653 8652 * scsi_vhci.conf VID/PID override to indicate the device
8654 8653 * should not be supported under scsi_vhci (even if there
8655 8654 * is an 'fops' module supporting the device).
8656 8655 *
8657 8656 * "<other>" If another value is returned then that value is the
8658 8657 * name of the 'fops' module that should be used.
8659 8658 */
8660 8659 sfo = NULL; /* "NONE" */
8661 8660 override = scsi_get_device_type_string(
8662 8661 "scsi-vhci-failover-override", vdip, psd);
8663 8662 if (override == NULL) {
8664 8663 /* NULL: default: select based on sfo_device_probe results */
8665 8664 for (sf = scsi_failover_table; sf->sf_mod; sf++) {
8666 8665 if ((sf->sf_sfo == NULL) ||
8667 8666 sf->sf_sfo->sfo_device_probe(psd, psd->sd_inq,
8668 8667 ctprivp) == SFO_DEVICE_PROBE_PHCI)
8669 8668 continue;
8670 8669
8671 8670 /* found failover module, supported under scsi_vhci */
8672 8671 sfo = sf->sf_sfo;
8673 8672 if (fo_namep && (*fo_namep == NULL)) {
8674 8673 sfo_name = i_ddi_strdup(sfo->sfo_name,
8675 8674 KM_SLEEP);
8676 8675 *fo_namep = sfo_name;
8677 8676 }
8678 8677 break;
8679 8678 }
8680 8679 } else if (strcasecmp(override, "NONE")) {
8681 8680 /* !"NONE": select based on driver.conf specified name */
8682 8681 for (sf = scsi_failover_table, sfo = NULL; sf->sf_mod; sf++) {
8683 8682 if ((sf->sf_sfo == NULL) ||
8684 8683 (sf->sf_sfo->sfo_name == NULL) ||
8685 8684 strcmp(override, sf->sf_sfo->sfo_name))
8686 8685 continue;
8687 8686
8688 8687 /*
8689 8688 * NOTE: If sfo_device_probe() has side-effects,
8690 8689 * including setting *ctprivp, these are not going
8691 8690 * to occur with override config.
8692 8691 */
8693 8692
8694 8693 /* found failover module, supported under scsi_vhci */
8695 8694 sfo = sf->sf_sfo;
8696 8695 if (fo_namep && (*fo_namep == NULL)) {
8697 8696 sfo_name = kmem_alloc(strlen("conf ") +
8698 8697 strlen(sfo->sfo_name) + 1, KM_SLEEP);
8699 8698 (void) sprintf(sfo_name, "conf %s",
8700 8699 sfo->sfo_name);
8701 8700 *fo_namep = sfo_name;
8702 8701 }
8703 8702 break;
8704 8703 }
8705 8704 }
8706 8705 if (override)
8707 8706 kmem_free(override, strlen(override) + 1);
8708 8707 return (sfo);
8709 8708 }
8710 8709
8711 8710 /*
8712 8711 * Determine the device described by cinfo should be enumerated under
8713 8712 * the vHCI or the pHCI - if there is a failover ops then device is
8714 8713 * supported under vHCI. By agreement with SCSA cinfo is a pointer
8715 8714 * to a scsi_device structure associated with a decorated pHCI probe node.
8716 8715 */
8717 8716 /* ARGSUSED */
8718 8717 int
8719 8718 vhci_is_dev_supported(dev_info_t *vdip, dev_info_t *pdip, void *cinfo)
8720 8719 {
8721 8720 struct scsi_device *psd = (struct scsi_device *)cinfo;
8722 8721
8723 8722 return (vhci_dev_fo(vdip, psd, NULL, NULL) ? MDI_SUCCESS : MDI_FAILURE);
8724 8723 }
8725 8724
8726 8725
8727 8726 #ifdef DEBUG
8728 8727 extern struct scsi_key_strings scsi_cmds[];
8729 8728
8730 8729 static char *
8731 8730 vhci_print_scsi_cmd(char cmd)
8732 8731 {
8733 8732 char tmp[64];
8734 8733 char *cpnt;
8735 8734
8736 8735 cpnt = scsi_cmd_name(cmd, scsi_cmds, tmp);
8737 8736 /* tmp goes out of scope on return and caller sees garbage */
8738 8737 if (cpnt == tmp) {
8739 8738 cpnt = "Unknown Command";
8740 8739 }
8741 8740 return (cpnt);
8742 8741 }
8743 8742
8744 8743 extern uchar_t scsi_cdb_size[];
8745 8744
8746 8745 static void
8747 8746 vhci_print_cdb(dev_info_t *dip, uint_t level, char *title, uchar_t *cdb)
8748 8747 {
8749 8748 int len = scsi_cdb_size[CDB_GROUPID(cdb[0])];
8750 8749 char buf[256];
8751 8750
8752 8751 if (level == CE_NOTE) {
8753 8752 vhci_log(level, dip, "path cmd %s\n",
8754 8753 vhci_print_scsi_cmd(*cdb));
8755 8754 return;
8756 8755 }
8757 8756
8758 8757 (void) sprintf(buf, "%s for cmd(%s)", title, vhci_print_scsi_cmd(*cdb));
8759 8758 vhci_clean_print(dip, level, buf, cdb, len);
8760 8759 }
8761 8760
8762 8761 static void
8763 8762 vhci_clean_print(dev_info_t *dev, uint_t level, char *title, uchar_t *data,
8764 8763 int len)
8765 8764 {
8766 8765 int i;
8767 8766 int c;
8768 8767 char *format;
8769 8768 char buf[256];
8770 8769 uchar_t byte;
8771 8770
8772 8771 (void) sprintf(buf, "%s:\n", title);
8773 8772 vhci_log(level, dev, "%s", buf);
8774 8773 level = CE_CONT;
8775 8774 for (i = 0; i < len; ) {
8776 8775 buf[0] = 0;
8777 8776 for (c = 0; c < 8 && i < len; c++, i++) {
8778 8777 byte = (uchar_t)data[i];
8779 8778 if (byte < 0x10)
8780 8779 format = "0x0%x ";
8781 8780 else
8782 8781 format = "0x%x ";
8783 8782 (void) sprintf(&buf[(int)strlen(buf)], format, byte);
8784 8783 }
8785 8784 (void) sprintf(&buf[(int)strlen(buf)], "\n");
8786 8785
8787 8786 vhci_log(level, dev, "%s\n", buf);
8788 8787 }
8789 8788 }
8790 8789 #endif
8791 8790 static void
8792 8791 vhci_invalidate_mpapi_lu(struct scsi_vhci *vhci, scsi_vhci_lun_t *vlun)
8793 8792 {
8794 8793 char *svl_wwn;
8795 8794 mpapi_item_list_t *ilist;
8796 8795 mpapi_lu_data_t *ld;
8797 8796
8798 8797 if (vlun == NULL) {
8799 8798 return;
8800 8799 } else {
8801 8800 svl_wwn = vlun->svl_lun_wwn;
8802 8801 }
8803 8802
8804 8803 ilist = vhci->mp_priv->obj_hdr_list[MP_OBJECT_TYPE_MULTIPATH_LU]->head;
8805 8804
8806 8805 while (ilist != NULL) {
8807 8806 ld = (mpapi_lu_data_t *)(ilist->item->idata);
8808 8807 if ((ld != NULL) && (strncmp(ld->prop.name, svl_wwn,
8809 8808 strlen(svl_wwn)) == 0)) {
8810 8809 ld->valid = 0;
8811 8810 VHCI_DEBUG(6, (CE_WARN, NULL,
8812 8811 "vhci_invalidate_mpapi_lu: "
8813 8812 "Invalidated LU(%s)", svl_wwn));
8814 8813 return;
8815 8814 }
8816 8815 ilist = ilist->next;
8817 8816 }
8818 8817 VHCI_DEBUG(6, (CE_WARN, NULL, "vhci_invalidate_mpapi_lu: "
8819 8818 "Could not find LU(%s) to invalidate.", svl_wwn));
8820 8819 }
↓ open down ↓ |
8508 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX